blob: c58d22ae4163a872c8d1c2142b8122ef0503f2ac [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
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020060#if defined(PSA_SUCCESS)
61/* If PSA_SUCCESS is defined, assume that PSA crypto is being used
62 * together with PSA IPC, which also defines the identifier
63 * PSA_SUCCESS. We must not define PSA_SUCCESS ourselves in that case;
64 * the other error code names don't clash. Also define psa_status_t as
65 * an alias for the type used by PSA IPC. This is a temporary hack
mohammad160313f43942018-08-05 12:09:44 +030066 * until we unify error reporting in PSA IPC and PSA crypto.
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020067 *
68 * Note that psa_defs.h must be included before this header!
69 */
70typedef psa_error_t psa_status_t;
71
72#else /* defined(PSA_SUCCESS) */
73
Gilles Peskinee59236f2018-01-27 23:32:46 +010074/**
75 * \brief Function return status.
76 *
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020077 * This is either #PSA_SUCCESS (which is zero), indicating success,
78 * or a nonzero value indicating that an error occurred. Errors are
79 * encoded as one of the \c PSA_ERROR_xxx values defined here.
Gilles Peskinee59236f2018-01-27 23:32:46 +010080 */
itayzafrirc2a79762018-06-18 16:20:16 +030081typedef int32_t psa_status_t;
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020082
itayzafrirc2a79762018-06-18 16:20:16 +030083/** The action was completed successfully. */
84#define PSA_SUCCESS ((psa_status_t)0)
Gilles Peskinee9a0a9d2018-06-20 13:59:04 +020085
86#endif /* !defined(PSA_SUCCESS) */
itayzafrirc2a79762018-06-18 16:20:16 +030087
itayzafrirf26dbfc2018-08-01 16:09:08 +030088/** An error occurred that does not correspond to any defined
89 * failure cause.
90 *
91 * Implementations may use this error code if none of the other standard
92 * error codes are applicable. */
93#define PSA_ERROR_UNKNOWN_ERROR ((psa_status_t)1)
94
itayzafrirc2a79762018-06-18 16:20:16 +030095/** The requested operation or a parameter is not supported
96 * by this implementation.
97 *
98 * Implementations should return this error code when an enumeration
99 * parameter such as a key type, algorithm, etc. is not recognized.
100 * If a combination of parameters is recognized and identified as
101 * not valid, return #PSA_ERROR_INVALID_ARGUMENT instead. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300102#define PSA_ERROR_NOT_SUPPORTED ((psa_status_t)2)
itayzafrirc2a79762018-06-18 16:20:16 +0300103
104/** The requested action is denied by a policy.
105 *
106 * Implementations should return this error code when the parameters
107 * are recognized as valid and supported, and a policy explicitly
108 * denies the requested operation.
109 *
110 * If a subset of the parameters of a function call identify a
111 * forbidden operation, and another subset of the parameters are
112 * not valid or not supported, it is unspecified whether the function
113 * returns #PSA_ERROR_NOT_PERMITTED, #PSA_ERROR_NOT_SUPPORTED or
114 * #PSA_ERROR_INVALID_ARGUMENT. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300115#define PSA_ERROR_NOT_PERMITTED ((psa_status_t)3)
itayzafrirc2a79762018-06-18 16:20:16 +0300116
117/** An output buffer is too small.
118 *
Gilles Peskinebe42f312018-07-13 14:38:15 +0200119 * Applications can call the \c PSA_xxx_SIZE macro listed in the function
itayzafrirc2a79762018-06-18 16:20:16 +0300120 * description to determine a sufficient buffer size.
121 *
122 * Implementations should preferably return this error code only
123 * in cases when performing the operation with a larger output
124 * buffer would succeed. However implementations may return this
125 * error if a function has invalid or unsupported parameters in addition
126 * to the parameters that determine the necessary output buffer size. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300127#define PSA_ERROR_BUFFER_TOO_SMALL ((psa_status_t)4)
itayzafrirc2a79762018-06-18 16:20:16 +0300128
129/** A slot is occupied, but must be empty to carry out the
130 * requested action.
131 *
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100132 * If a handle is invalid, it does not designate an occupied slot.
133 * The error for an invalid handle is #PSA_ERROR_INVALID_HANDLE.
134 */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300135#define PSA_ERROR_OCCUPIED_SLOT ((psa_status_t)5)
itayzafrirc2a79762018-06-18 16:20:16 +0300136
137/** A slot is empty, but must be occupied to carry out the
138 * requested action.
139 *
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100140 * If a handle is invalid, it does not designate an empty slot.
141 * The error for an invalid handle is #PSA_ERROR_INVALID_HANDLE.
142 */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300143#define PSA_ERROR_EMPTY_SLOT ((psa_status_t)6)
itayzafrirc2a79762018-06-18 16:20:16 +0300144
145/** The requested action cannot be performed in the current state.
146 *
147 * Multipart operations return this error when one of the
148 * functions is called out of sequence. Refer to the function
149 * descriptions for permitted sequencing of functions.
150 *
151 * Implementations shall not return this error code to indicate
152 * that a key slot is occupied when it needs to be free or vice versa,
153 * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT
154 * as applicable. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300155#define PSA_ERROR_BAD_STATE ((psa_status_t)7)
itayzafrirc2a79762018-06-18 16:20:16 +0300156
157/** The parameters passed to the function are invalid.
158 *
159 * Implementations may return this error any time a parameter or
160 * combination of parameters are recognized as invalid.
161 *
162 * Implementations shall not return this error code to indicate
163 * that a key slot is occupied when it needs to be free or vice versa,
164 * but shall return #PSA_ERROR_OCCUPIED_SLOT or #PSA_ERROR_EMPTY_SLOT
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100165 * as applicable.
166 *
167 * Implementation shall not return this error code to indicate that a
168 * key handle is invalid, but shall return #PSA_ERROR_INVALID_HANDLE
169 * instead.
170 */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300171#define PSA_ERROR_INVALID_ARGUMENT ((psa_status_t)8)
itayzafrirc2a79762018-06-18 16:20:16 +0300172
173/** There is not enough runtime memory.
174 *
175 * If the action is carried out across multiple security realms, this
176 * error can refer to available memory in any of the security realms. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300177#define PSA_ERROR_INSUFFICIENT_MEMORY ((psa_status_t)9)
itayzafrirc2a79762018-06-18 16:20:16 +0300178
179/** There is not enough persistent storage.
180 *
181 * Functions that modify the key storage return this error code if
182 * there is insufficient storage space on the host media. In addition,
183 * many functions that do not otherwise access storage may return this
184 * error code if the implementation requires a mandatory log entry for
185 * the requested action and the log storage space is full. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300186#define PSA_ERROR_INSUFFICIENT_STORAGE ((psa_status_t)10)
itayzafrirc2a79762018-06-18 16:20:16 +0300187
188/** There was a communication failure inside the implementation.
189 *
190 * This can indicate a communication failure between the application
191 * and an external cryptoprocessor or between the cryptoprocessor and
192 * an external volatile or persistent memory. A communication failure
193 * may be transient or permanent depending on the cause.
194 *
195 * \warning If a function returns this error, it is undetermined
196 * whether the requested action has completed or not. Implementations
197 * should return #PSA_SUCCESS on successful completion whenver
198 * possible, however functions may return #PSA_ERROR_COMMUNICATION_FAILURE
199 * if the requested action was completed successfully in an external
200 * cryptoprocessor but there was a breakdown of communication before
201 * the cryptoprocessor could report the status to the application.
202 */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300203#define PSA_ERROR_COMMUNICATION_FAILURE ((psa_status_t)11)
itayzafrirc2a79762018-06-18 16:20:16 +0300204
205/** There was a storage failure that may have led to data loss.
206 *
207 * This error indicates that some persistent storage is corrupted.
208 * It should not be used for a corruption of volatile memory
209 * (use #PSA_ERROR_TAMPERING_DETECTED), for a communication error
210 * between the cryptoprocessor and its external storage (use
211 * #PSA_ERROR_COMMUNICATION_FAILURE), or when the storage is
212 * in a valid state but is full (use #PSA_ERROR_INSUFFICIENT_STORAGE).
213 *
214 * Note that a storage failure does not indicate that any data that was
215 * previously read is invalid. However this previously read data may no
216 * longer be readable from storage.
217 *
218 * When a storage failure occurs, it is no longer possible to ensure
219 * the global integrity of the keystore. Depending on the global
220 * integrity guarantees offered by the implementation, access to other
221 * data may or may not fail even if the data is still readable but
222 * its integrity canont be guaranteed.
223 *
224 * Implementations should only use this error code to report a
225 * permanent storage corruption. However application writers should
226 * keep in mind that transient errors while reading the storage may be
227 * reported using this error code. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300228#define PSA_ERROR_STORAGE_FAILURE ((psa_status_t)12)
itayzafrirc2a79762018-06-18 16:20:16 +0300229
230/** A hardware failure was detected.
231 *
232 * A hardware failure may be transient or permanent depending on the
233 * cause. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300234#define PSA_ERROR_HARDWARE_FAILURE ((psa_status_t)13)
itayzafrirc2a79762018-06-18 16:20:16 +0300235
236/** A tampering attempt was detected.
237 *
238 * If an application receives this error code, there is no guarantee
239 * that previously accessed or computed data was correct and remains
240 * confidential. Applications should not perform any security function
241 * and should enter a safe failure state.
242 *
243 * Implementations may return this error code if they detect an invalid
244 * state that cannot happen during normal operation and that indicates
245 * that the implementation's security guarantees no longer hold. Depending
246 * on the implementation architecture and on its security and safety goals,
247 * the implementation may forcibly terminate the application.
248 *
249 * This error code is intended as a last resort when a security breach
250 * is detected and it is unsure whether the keystore data is still
251 * protected. Implementations shall only return this error code
252 * to report an alarm from a tampering detector, to indicate that
253 * the confidentiality of stored data can no longer be guaranteed,
254 * or to indicate that the integrity of previously returned data is now
255 * considered compromised. Implementations shall not use this error code
256 * to indicate a hardware failure that merely makes it impossible to
257 * perform the requested operation (use #PSA_ERROR_COMMUNICATION_FAILURE,
258 * #PSA_ERROR_STORAGE_FAILURE, #PSA_ERROR_HARDWARE_FAILURE,
259 * #PSA_ERROR_INSUFFICIENT_ENTROPY or other applicable error code
260 * instead).
261 *
262 * This error indicates an attack against the application. Implementations
263 * shall not return this error code as a consequence of the behavior of
264 * the application itself. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300265#define PSA_ERROR_TAMPERING_DETECTED ((psa_status_t)14)
itayzafrirc2a79762018-06-18 16:20:16 +0300266
267/** There is not enough entropy to generate random data needed
268 * for the requested action.
269 *
270 * This error indicates a failure of a hardware random generator.
271 * Application writers should note that this error can be returned not
272 * only by functions whose purpose is to generate random data, such
273 * as key, IV or nonce generation, but also by functions that execute
274 * an algorithm with a randomized result, as well as functions that
275 * use randomization of intermediate computations as a countermeasure
276 * to certain attacks.
277 *
278 * Implementations should avoid returning this error after psa_crypto_init()
279 * has succeeded. Implementations should generate sufficient
280 * entropy during initialization and subsequently use a cryptographically
281 * secure pseudorandom generator (PRNG). However implementations may return
282 * this error at any time if a policy requires the PRNG to be reseeded
283 * during normal operation. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300284#define PSA_ERROR_INSUFFICIENT_ENTROPY ((psa_status_t)15)
itayzafrirc2a79762018-06-18 16:20:16 +0300285
286/** The signature, MAC or hash is incorrect.
287 *
288 * Verification functions return this error if the verification
289 * calculations completed successfully, and the value to be verified
290 * was determined to be incorrect.
291 *
292 * If the value to verify has an invalid size, implementations may return
293 * either #PSA_ERROR_INVALID_ARGUMENT or #PSA_ERROR_INVALID_SIGNATURE. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300294#define PSA_ERROR_INVALID_SIGNATURE ((psa_status_t)16)
itayzafrirc2a79762018-06-18 16:20:16 +0300295
296/** The decrypted padding is incorrect.
297 *
298 * \warning In some protocols, when decrypting data, it is essential that
299 * the behavior of the application does not depend on whether the padding
300 * is correct, down to precise timing. Applications should prefer
301 * protocols that use authenticated encryption rather than plain
302 * encryption. If the application must perform a decryption of
303 * unauthenticated data, the application writer should take care not
304 * to reveal whether the padding is invalid.
305 *
306 * Implementations should strive to make valid and invalid padding
307 * as close as possible to indistinguishable to an external observer.
308 * In particular, the timing of a decryption operation should not
309 * depend on the validity of the padding. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300310#define PSA_ERROR_INVALID_PADDING ((psa_status_t)17)
itayzafrirc2a79762018-06-18 16:20:16 +0300311
Gilles Peskineeab56e42018-07-12 17:12:33 +0200312/** The generator has insufficient capacity left.
313 *
314 * Once a function returns this error, attempts to read from the
315 * generator will always return this error. */
itayzafrirf26dbfc2018-08-01 16:09:08 +0300316#define PSA_ERROR_INSUFFICIENT_CAPACITY ((psa_status_t)18)
Gilles Peskinee59236f2018-01-27 23:32:46 +0100317
Gilles Peskinef535eb22018-11-30 14:08:36 +0100318/** The key handle is not valid.
319 */
320#define PSA_ERROR_INVALID_HANDLE ((psa_status_t)19)
321
Gilles Peskinee59236f2018-01-27 23:32:46 +0100322/**
323 * \brief Library initialization.
324 *
325 * Applications must call this function before calling any other
326 * function in this module.
327 *
328 * Applications may call this function more than once. Once a call
329 * succeeds, subsequent calls are guaranteed to succeed.
330 *
itayzafrir18617092018-09-16 12:22:41 +0300331 * If the application calls other functions before calling psa_crypto_init(),
332 * the behavior is undefined. Implementations are encouraged to either perform
333 * the operation as if the library had been initialized or to return
334 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
335 * implementations should not return a success status if the lack of
336 * initialization may have security implications, for example due to improper
337 * seeding of the random number generator.
338 *
Gilles Peskine28538492018-07-11 17:34:00 +0200339 * \retval #PSA_SUCCESS
340 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
341 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
342 * \retval #PSA_ERROR_HARDWARE_FAILURE
343 * \retval #PSA_ERROR_TAMPERING_DETECTED
344 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +0100345 */
346psa_status_t psa_crypto_init(void);
347
Gilles Peskine2905a7a2018-03-07 16:39:31 +0100348#define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8)
349#define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8)
Gilles Peskine0189e752018-02-03 23:57:22 +0100350
Gilles Peskinee59236f2018-01-27 23:32:46 +0100351/**@}*/
352
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100353/** \defgroup crypto_types Key and algorithm types
354 * @{
355 */
356
Gilles Peskine308b91d2018-02-08 09:47:44 +0100357/** \brief Encoding of a key type.
358 */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100359typedef uint32_t psa_key_type_t;
360
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100361/** An invalid key type value.
362 *
363 * Zero is not the encoding of any key type.
364 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100365#define PSA_KEY_TYPE_NONE ((psa_key_type_t)0x00000000)
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100366
367/** Vendor-defined flag
368 *
369 * Key types defined by this standard will never have the
370 * #PSA_KEY_TYPE_VENDOR_FLAG bit set. Vendors who define additional key types
371 * must use an encoding with the #PSA_KEY_TYPE_VENDOR_FLAG bit set and should
372 * respect the bitwise structure used by standard encodings whenever practical.
373 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100374#define PSA_KEY_TYPE_VENDOR_FLAG ((psa_key_type_t)0x80000000)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100375
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200376#define PSA_KEY_TYPE_CATEGORY_MASK ((psa_key_type_t)0x70000000)
377#define PSA_KEY_TYPE_CATEGORY_SYMMETRIC ((psa_key_type_t)0x40000000)
378#define PSA_KEY_TYPE_CATEGORY_RAW ((psa_key_type_t)0x50000000)
379#define PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY ((psa_key_type_t)0x60000000)
380#define PSA_KEY_TYPE_CATEGORY_KEY_PAIR ((psa_key_type_t)0x70000000)
381
382#define PSA_KEY_TYPE_CATEGORY_FLAG_PAIR ((psa_key_type_t)0x10000000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200383
Gilles Peskinee8779742018-08-10 16:10:56 +0200384/** Whether a key type is vendor-defined. */
385#define PSA_KEY_TYPE_IS_VENDOR_DEFINED(type) \
386 (((type) & PSA_KEY_TYPE_VENDOR_FLAG) != 0)
387
388/** Whether a key type is an unstructured array of bytes.
389 *
390 * This encompasses both symmetric keys and non-key data.
391 */
392#define PSA_KEY_TYPE_IS_UNSTRUCTURED(type) \
393 (((type) & PSA_KEY_TYPE_CATEGORY_MASK & ~(psa_key_type_t)0x10000000) == \
394 PSA_KEY_TYPE_CATEGORY_SYMMETRIC)
395
396/** Whether a key type is asymmetric: either a key pair or a public key. */
397#define PSA_KEY_TYPE_IS_ASYMMETRIC(type) \
398 (((type) & PSA_KEY_TYPE_CATEGORY_MASK \
399 & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR) == \
400 PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
401/** Whether a key type is the public part of a key pair. */
402#define PSA_KEY_TYPE_IS_PUBLIC_KEY(type) \
403 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_PUBLIC_KEY)
404/** Whether a key type is a key pair containing a private part and a public
405 * part. */
406#define PSA_KEY_TYPE_IS_KEYPAIR(type) \
407 (((type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_KEY_PAIR)
408/** The key pair type corresponding to a public key type.
409 *
410 * You may also pass a key pair type as \p type, it will be left unchanged.
411 *
412 * \param type A public key type or key pair type.
413 *
414 * \return The corresponding key pair type.
415 * If \p type is not a public key or a key pair,
416 * the return value is undefined.
417 */
418#define PSA_KEY_TYPE_KEYPAIR_OF_PUBLIC_KEY(type) \
419 ((type) | PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
420/** The public key type corresponding to a key pair type.
421 *
422 * You may also pass a key pair type as \p type, it will be left unchanged.
423 *
424 * \param type A public key type or key pair type.
425 *
426 * \return The corresponding public key type.
427 * If \p type is not a public key or a key pair,
428 * the return value is undefined.
429 */
430#define PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) \
431 ((type) & ~PSA_KEY_TYPE_CATEGORY_FLAG_PAIR)
Gilles Peskinee8779742018-08-10 16:10:56 +0200432
Gilles Peskine35855962018-04-19 08:39:16 +0200433/** Raw data.
434 *
435 * A "key" of this type cannot be used for any cryptographic operation.
436 * Applications may use this type to store arbitrary data in the keystore. */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200437#define PSA_KEY_TYPE_RAW_DATA ((psa_key_type_t)0x50000001)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100438
Gilles Peskine35855962018-04-19 08:39:16 +0200439/** HMAC key.
440 *
441 * The key policy determines which underlying hash algorithm the key can be
442 * used for.
443 *
444 * HMAC keys should generally have the same size as the underlying hash.
Gilles Peskinebe42f312018-07-13 14:38:15 +0200445 * This size can be calculated with #PSA_HASH_SIZE(\c alg) where
446 * \c alg is the HMAC algorithm or the underlying hash algorithm. */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200447#define PSA_KEY_TYPE_HMAC ((psa_key_type_t)0x51000000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200448
Gilles Peskineea0fb492018-07-12 17:17:20 +0200449/** A secret for key derivation.
450 *
451 * The key policy determines which key derivation algorithm the key
452 * can be used for.
453 */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200454#define PSA_KEY_TYPE_DERIVE ((psa_key_type_t)0x52000000)
Gilles Peskineea0fb492018-07-12 17:17:20 +0200455
Gilles Peskine35855962018-04-19 08:39:16 +0200456/** Key for an cipher, AEAD or MAC algorithm based on the AES block cipher.
457 *
458 * The size of the key can be 16 bytes (AES-128), 24 bytes (AES-192) or
459 * 32 bytes (AES-256).
460 */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200461#define PSA_KEY_TYPE_AES ((psa_key_type_t)0x40000001)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200462
Gilles Peskine35855962018-04-19 08:39:16 +0200463/** Key for a cipher or MAC algorithm based on DES or 3DES (Triple-DES).
464 *
465 * The size of the key can be 8 bytes (single DES), 16 bytes (2-key 3DES) or
466 * 24 bytes (3-key 3DES).
467 *
468 * Note that single DES and 2-key 3DES are weak and strongly
469 * deprecated and should only be used to decrypt legacy data. 3-key 3DES
470 * is weak and deprecated and should only be used in legacy protocols.
471 */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200472#define PSA_KEY_TYPE_DES ((psa_key_type_t)0x40000002)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200473
Gilles Peskine35855962018-04-19 08:39:16 +0200474/** Key for an cipher, AEAD or MAC algorithm based on the
475 * Camellia block cipher. */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200476#define PSA_KEY_TYPE_CAMELLIA ((psa_key_type_t)0x40000003)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200477
Gilles Peskine35855962018-04-19 08:39:16 +0200478/** Key for the RC4 stream cipher.
479 *
480 * Note that RC4 is weak and deprecated and should only be used in
481 * legacy protocols. */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200482#define PSA_KEY_TYPE_ARC4 ((psa_key_type_t)0x40000004)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100483
Gilles Peskine308b91d2018-02-08 09:47:44 +0100484/** RSA public key. */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200485#define PSA_KEY_TYPE_RSA_PUBLIC_KEY ((psa_key_type_t)0x60010000)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100486/** RSA key pair (private and public key). */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200487#define PSA_KEY_TYPE_RSA_KEYPAIR ((psa_key_type_t)0x70010000)
Gilles Peskine583b55d2018-08-22 18:21:32 +0200488/** Whether a key type is an RSA key (pair or public-only). */
489#define PSA_KEY_TYPE_IS_RSA(type) \
490 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200491
Gilles Peskine06dc2632018-03-08 07:47:25 +0100492/** DSA public key. */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200493#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x60020000)
Gilles Peskine06dc2632018-03-08 07:47:25 +0100494/** DSA key pair (private and public key). */
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200495#define PSA_KEY_TYPE_DSA_KEYPAIR ((psa_key_type_t)0x70020000)
Gilles Peskine583b55d2018-08-22 18:21:32 +0200496/** Whether a key type is an DSA key (pair or public-only). */
497#define PSA_KEY_TYPE_IS_DSA(type) \
498 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200499
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200500#define PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((psa_key_type_t)0x60030000)
501#define PSA_KEY_TYPE_ECC_KEYPAIR_BASE ((psa_key_type_t)0x70030000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100502#define PSA_KEY_TYPE_ECC_CURVE_MASK ((psa_key_type_t)0x0000ffff)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200503/** Elliptic curve key pair. */
Gilles Peskine06dc2632018-03-08 07:47:25 +0100504#define PSA_KEY_TYPE_ECC_KEYPAIR(curve) \
505 (PSA_KEY_TYPE_ECC_KEYPAIR_BASE | (curve))
Gilles Peskinedcd14942018-07-12 00:30:52 +0200506/** Elliptic curve public key. */
Gilles Peskine06dc2632018-03-08 07:47:25 +0100507#define PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve) \
508 (PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve))
Gilles Peskine98f0a242018-02-06 18:57:29 +0100509
Gilles Peskined8008d62018-06-29 19:51:51 +0200510/** Whether a key type is an elliptic curve key (pair or public-only). */
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100511#define PSA_KEY_TYPE_IS_ECC(type) \
Gilles Peskine06dc2632018-03-08 07:47:25 +0100512 ((PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(type) & \
513 ~PSA_KEY_TYPE_ECC_CURVE_MASK) == PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
Gilles Peskine55728b02018-07-16 23:08:16 +0200514#define PSA_KEY_TYPE_IS_ECC_KEYPAIR(type) \
515 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
516 PSA_KEY_TYPE_ECC_KEYPAIR_BASE)
517#define PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(type) \
518 (((type) & ~PSA_KEY_TYPE_ECC_CURVE_MASK) == \
519 PSA_KEY_TYPE_ECC_PUBLIC_KEY_BASE)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100520
Gilles Peskinee1fed0d2018-06-18 20:45:45 +0200521/** The type of PSA elliptic curve identifiers. */
522typedef uint16_t psa_ecc_curve_t;
523/** Extract the curve from an elliptic curve key type. */
524#define PSA_KEY_TYPE_GET_CURVE(type) \
525 ((psa_ecc_curve_t) (PSA_KEY_TYPE_IS_ECC(type) ? \
526 ((type) & PSA_KEY_TYPE_ECC_CURVE_MASK) : \
527 0))
528
529/* The encoding of curve identifiers is currently aligned with the
530 * TLS Supported Groups Registry (formerly known as the
531 * TLS EC Named Curve Registry)
532 * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8
Gilles Peskine70ce2c62018-08-22 18:21:57 +0200533 * The values are defined by RFC 8422 and RFC 7027. */
Gilles Peskinee1fed0d2018-06-18 20:45:45 +0200534#define PSA_ECC_CURVE_SECT163K1 ((psa_ecc_curve_t) 0x0001)
535#define PSA_ECC_CURVE_SECT163R1 ((psa_ecc_curve_t) 0x0002)
536#define PSA_ECC_CURVE_SECT163R2 ((psa_ecc_curve_t) 0x0003)
537#define PSA_ECC_CURVE_SECT193R1 ((psa_ecc_curve_t) 0x0004)
538#define PSA_ECC_CURVE_SECT193R2 ((psa_ecc_curve_t) 0x0005)
539#define PSA_ECC_CURVE_SECT233K1 ((psa_ecc_curve_t) 0x0006)
540#define PSA_ECC_CURVE_SECT233R1 ((psa_ecc_curve_t) 0x0007)
541#define PSA_ECC_CURVE_SECT239K1 ((psa_ecc_curve_t) 0x0008)
542#define PSA_ECC_CURVE_SECT283K1 ((psa_ecc_curve_t) 0x0009)
543#define PSA_ECC_CURVE_SECT283R1 ((psa_ecc_curve_t) 0x000a)
544#define PSA_ECC_CURVE_SECT409K1 ((psa_ecc_curve_t) 0x000b)
545#define PSA_ECC_CURVE_SECT409R1 ((psa_ecc_curve_t) 0x000c)
546#define PSA_ECC_CURVE_SECT571K1 ((psa_ecc_curve_t) 0x000d)
547#define PSA_ECC_CURVE_SECT571R1 ((psa_ecc_curve_t) 0x000e)
548#define PSA_ECC_CURVE_SECP160K1 ((psa_ecc_curve_t) 0x000f)
549#define PSA_ECC_CURVE_SECP160R1 ((psa_ecc_curve_t) 0x0010)
550#define PSA_ECC_CURVE_SECP160R2 ((psa_ecc_curve_t) 0x0011)
551#define PSA_ECC_CURVE_SECP192K1 ((psa_ecc_curve_t) 0x0012)
552#define PSA_ECC_CURVE_SECP192R1 ((psa_ecc_curve_t) 0x0013)
553#define PSA_ECC_CURVE_SECP224K1 ((psa_ecc_curve_t) 0x0014)
554#define PSA_ECC_CURVE_SECP224R1 ((psa_ecc_curve_t) 0x0015)
555#define PSA_ECC_CURVE_SECP256K1 ((psa_ecc_curve_t) 0x0016)
556#define PSA_ECC_CURVE_SECP256R1 ((psa_ecc_curve_t) 0x0017)
557#define PSA_ECC_CURVE_SECP384R1 ((psa_ecc_curve_t) 0x0018)
558#define PSA_ECC_CURVE_SECP521R1 ((psa_ecc_curve_t) 0x0019)
559#define PSA_ECC_CURVE_BRAINPOOL_P256R1 ((psa_ecc_curve_t) 0x001a)
560#define PSA_ECC_CURVE_BRAINPOOL_P384R1 ((psa_ecc_curve_t) 0x001b)
561#define PSA_ECC_CURVE_BRAINPOOL_P512R1 ((psa_ecc_curve_t) 0x001c)
562#define PSA_ECC_CURVE_CURVE25519 ((psa_ecc_curve_t) 0x001d)
563#define PSA_ECC_CURVE_CURVE448 ((psa_ecc_curve_t) 0x001e)
Gilles Peskinee1fed0d2018-06-18 20:45:45 +0200564
Gilles Peskine7e198532018-03-08 07:50:30 +0100565/** The block size of a block cipher.
566 *
567 * \param type A cipher key type (value of type #psa_key_type_t).
568 *
569 * \return The block size for a block cipher, or 1 for a stream cipher.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200570 * The return value is undefined if \p type is not a supported
Gilles Peskine35855962018-04-19 08:39:16 +0200571 * cipher key type.
572 *
573 * \note It is possible to build stream cipher algorithms on top of a block
574 * cipher, for example CTR mode (#PSA_ALG_CTR).
575 * This macro only takes the key type into account, so it cannot be
576 * used to determine the size of the data that #psa_cipher_update()
577 * might buffer for future processing in general.
Gilles Peskine7e198532018-03-08 07:50:30 +0100578 *
579 * \note This macro returns a compile-time constant if its argument is one.
580 *
581 * \warning This macro may evaluate its argument multiple times.
582 */
Gilles Peskine03182e92018-03-07 16:40:52 +0100583#define PSA_BLOCK_CIPHER_BLOCK_SIZE(type) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100584 ( \
585 (type) == PSA_KEY_TYPE_AES ? 16 : \
586 (type) == PSA_KEY_TYPE_DES ? 8 : \
587 (type) == PSA_KEY_TYPE_CAMELLIA ? 16 : \
Gilles Peskine7e198532018-03-08 07:50:30 +0100588 (type) == PSA_KEY_TYPE_ARC4 ? 1 : \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100589 0)
590
Gilles Peskine308b91d2018-02-08 09:47:44 +0100591/** \brief Encoding of a cryptographic algorithm.
592 *
593 * For algorithms that can be applied to multiple key types, this type
594 * does not encode the key type. For example, for symmetric ciphers
595 * based on a block cipher, #psa_algorithm_t encodes the block cipher
596 * mode and the padding mode while the block cipher itself is encoded
597 * via #psa_key_type_t.
598 */
Gilles Peskine20035e32018-02-03 22:44:14 +0100599typedef uint32_t psa_algorithm_t;
600
Gilles Peskine98f0a242018-02-06 18:57:29 +0100601#define PSA_ALG_VENDOR_FLAG ((psa_algorithm_t)0x80000000)
602#define PSA_ALG_CATEGORY_MASK ((psa_algorithm_t)0x7f000000)
603#define PSA_ALG_CATEGORY_HASH ((psa_algorithm_t)0x01000000)
604#define PSA_ALG_CATEGORY_MAC ((psa_algorithm_t)0x02000000)
605#define PSA_ALG_CATEGORY_CIPHER ((psa_algorithm_t)0x04000000)
606#define PSA_ALG_CATEGORY_AEAD ((psa_algorithm_t)0x06000000)
607#define PSA_ALG_CATEGORY_SIGN ((psa_algorithm_t)0x10000000)
608#define PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION ((psa_algorithm_t)0x12000000)
609#define PSA_ALG_CATEGORY_KEY_AGREEMENT ((psa_algorithm_t)0x22000000)
610#define PSA_ALG_CATEGORY_KEY_DERIVATION ((psa_algorithm_t)0x30000000)
Gilles Peskinee8f0e3d2018-09-18 11:52:10 +0200611#define PSA_ALG_CATEGORY_KEY_SELECTION ((psa_algorithm_t)0x31000000)
Gilles Peskine20035e32018-02-03 22:44:14 +0100612
Gilles Peskine98f0a242018-02-06 18:57:29 +0100613#define PSA_ALG_IS_VENDOR_DEFINED(alg) \
614 (((alg) & PSA_ALG_VENDOR_FLAG) != 0)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200615
Gilles Peskine308b91d2018-02-08 09:47:44 +0100616/** Whether the specified algorithm is a hash algorithm.
617 *
Gilles Peskine7e198532018-03-08 07:50:30 +0100618 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100619 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200620 * \return 1 if \p alg is a hash algorithm, 0 otherwise.
621 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskine7e198532018-03-08 07:50:30 +0100622 * algorithm identifier.
623 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100624#define PSA_ALG_IS_HASH(alg) \
625 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_HASH)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200626
627/** Whether the specified algorithm is a MAC algorithm.
628 *
629 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
630 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200631 * \return 1 if \p alg is a MAC algorithm, 0 otherwise.
632 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200633 * algorithm identifier.
634 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100635#define PSA_ALG_IS_MAC(alg) \
636 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_MAC)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200637
638/** Whether the specified algorithm is a symmetric cipher algorithm.
639 *
640 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
641 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200642 * \return 1 if \p alg is a symmetric cipher algorithm, 0 otherwise.
643 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200644 * algorithm identifier.
645 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100646#define PSA_ALG_IS_CIPHER(alg) \
647 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_CIPHER)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200648
649/** Whether the specified algorithm is an authenticated encryption
650 * with associated data (AEAD) algorithm.
651 *
652 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
653 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200654 * \return 1 if \p alg is an AEAD algorithm, 0 otherwise.
655 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200656 * algorithm identifier.
657 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100658#define PSA_ALG_IS_AEAD(alg) \
659 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_AEAD)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200660
661/** Whether the specified algorithm is a public-key signature algorithm.
662 *
663 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
664 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200665 * \return 1 if \p alg is a public-key signature algorithm, 0 otherwise.
666 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200667 * algorithm identifier.
668 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100669#define PSA_ALG_IS_SIGN(alg) \
670 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_SIGN)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200671
672/** Whether the specified algorithm is a public-key encryption algorithm.
673 *
674 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
675 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200676 * \return 1 if \p alg is a public-key encryption algorithm, 0 otherwise.
677 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200678 * algorithm identifier.
679 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100680#define PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg) \
681 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_ASYMMETRIC_ENCRYPTION)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200682
Gilles Peskinee8f0e3d2018-09-18 11:52:10 +0200683#define PSA_ALG_KEY_SELECTION_FLAG ((psa_algorithm_t)0x01000000)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200684/** Whether the specified algorithm is a key agreement algorithm.
685 *
686 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
687 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200688 * \return 1 if \p alg is a key agreement algorithm, 0 otherwise.
689 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200690 * algorithm identifier.
691 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100692#define PSA_ALG_IS_KEY_AGREEMENT(alg) \
Gilles Peskinee8f0e3d2018-09-18 11:52:10 +0200693 (((alg) & PSA_ALG_CATEGORY_MASK & ~PSA_ALG_KEY_SELECTION_FLAG) == \
694 PSA_ALG_CATEGORY_KEY_AGREEMENT)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200695
696/** Whether the specified algorithm is a key derivation algorithm.
697 *
698 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
699 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200700 * \return 1 if \p alg is a key derivation algorithm, 0 otherwise.
701 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200702 * algorithm identifier.
703 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100704#define PSA_ALG_IS_KEY_DERIVATION(alg) \
705 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_DERIVATION)
706
Gilles Peskinee8f0e3d2018-09-18 11:52:10 +0200707/** Whether the specified algorithm is a key selection algorithm.
708 *
709 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
710 *
711 * \return 1 if \p alg is a key selection algorithm, 0 otherwise.
712 * This macro may return either 0 or 1 if \p alg is not a supported
713 * algorithm identifier.
714 */
715#define PSA_ALG_IS_KEY_SELECTION(alg) \
716 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_KEY_SELECTION)
717
Gilles Peskine98f0a242018-02-06 18:57:29 +0100718#define PSA_ALG_HASH_MASK ((psa_algorithm_t)0x000000ff)
719#define PSA_ALG_MD2 ((psa_algorithm_t)0x01000001)
720#define PSA_ALG_MD4 ((psa_algorithm_t)0x01000002)
721#define PSA_ALG_MD5 ((psa_algorithm_t)0x01000003)
Gilles Peskinee3f694f2018-03-08 07:48:40 +0100722#define PSA_ALG_RIPEMD160 ((psa_algorithm_t)0x01000004)
723#define PSA_ALG_SHA_1 ((psa_algorithm_t)0x01000005)
Gilles Peskineedd76872018-07-20 17:42:05 +0200724/** SHA2-224 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100725#define PSA_ALG_SHA_224 ((psa_algorithm_t)0x01000008)
Gilles Peskineedd76872018-07-20 17:42:05 +0200726/** SHA2-256 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100727#define PSA_ALG_SHA_256 ((psa_algorithm_t)0x01000009)
Gilles Peskineedd76872018-07-20 17:42:05 +0200728/** SHA2-384 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100729#define PSA_ALG_SHA_384 ((psa_algorithm_t)0x0100000a)
Gilles Peskineedd76872018-07-20 17:42:05 +0200730/** SHA2-512 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100731#define PSA_ALG_SHA_512 ((psa_algorithm_t)0x0100000b)
Gilles Peskineedd76872018-07-20 17:42:05 +0200732/** SHA2-512/224 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100733#define PSA_ALG_SHA_512_224 ((psa_algorithm_t)0x0100000c)
Gilles Peskineedd76872018-07-20 17:42:05 +0200734/** SHA2-512/256 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100735#define PSA_ALG_SHA_512_256 ((psa_algorithm_t)0x0100000d)
Gilles Peskineedd76872018-07-20 17:42:05 +0200736/** SHA3-224 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100737#define PSA_ALG_SHA3_224 ((psa_algorithm_t)0x01000010)
Gilles Peskineedd76872018-07-20 17:42:05 +0200738/** SHA3-256 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100739#define PSA_ALG_SHA3_256 ((psa_algorithm_t)0x01000011)
Gilles Peskineedd76872018-07-20 17:42:05 +0200740/** SHA3-384 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100741#define PSA_ALG_SHA3_384 ((psa_algorithm_t)0x01000012)
Gilles Peskineedd76872018-07-20 17:42:05 +0200742/** SHA3-512 */
Gilles Peskine98f0a242018-02-06 18:57:29 +0100743#define PSA_ALG_SHA3_512 ((psa_algorithm_t)0x01000013)
744
Gilles Peskine8c9def32018-02-08 10:02:12 +0100745#define PSA_ALG_MAC_SUBCATEGORY_MASK ((psa_algorithm_t)0x00c00000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100746#define PSA_ALG_HMAC_BASE ((psa_algorithm_t)0x02800000)
Gilles Peskine35855962018-04-19 08:39:16 +0200747/** Macro to build an HMAC algorithm.
748 *
Gilles Peskinedda3bd32018-07-12 19:40:46 +0200749 * For example, #PSA_ALG_HMAC(#PSA_ALG_SHA_256) is HMAC-SHA-256.
Gilles Peskine35855962018-04-19 08:39:16 +0200750 *
Gilles Peskineea4469f2018-06-28 13:57:23 +0200751 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200752 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine35855962018-04-19 08:39:16 +0200753 *
Gilles Peskineea4469f2018-06-28 13:57:23 +0200754 * \return The corresponding HMAC algorithm.
755 * \return Unspecified if \p alg is not a supported
756 * hash algorithm.
Gilles Peskine35855962018-04-19 08:39:16 +0200757 */
758#define PSA_ALG_HMAC(hash_alg) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100759 (PSA_ALG_HMAC_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
Gilles Peskinedcd14942018-07-12 00:30:52 +0200760
Gilles Peskine00709fa2018-08-22 18:25:41 +0200761#define PSA_ALG_HMAC_GET_HASH(hmac_alg) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100762 (PSA_ALG_CATEGORY_HASH | ((hmac_alg) & PSA_ALG_HASH_MASK))
Gilles Peskinedcd14942018-07-12 00:30:52 +0200763
764/** Whether the specified algorithm is an HMAC algorithm.
765 *
766 * HMAC is a family of MAC algorithms that are based on a hash function.
767 *
768 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
769 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200770 * \return 1 if \p alg is an HMAC algorithm, 0 otherwise.
771 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200772 * algorithm identifier.
773 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100774#define PSA_ALG_IS_HMAC(alg) \
775 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
776 PSA_ALG_HMAC_BASE)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200777
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200778/* In the encoding of a MAC algorithm, the bits corresponding to
779 * PSA_ALG_MAC_TRUNCATION_MASK encode the length to which the MAC is
780 * truncated. As an exception, the value 0 means the untruncated algorithm,
781 * whatever its length is. The length is encoded in 6 bits, so it can
782 * reach up to 63; the largest MAC is 64 bytes so its trivial truncation
783 * to full length is correctly encoded as 0 and any non-trivial truncation
784 * is correctly encoded as a value between 1 and 63. */
Gilles Peskined911eb72018-08-14 15:18:45 +0200785#define PSA_ALG_MAC_TRUNCATION_MASK ((psa_algorithm_t)0x00003f00)
786#define PSA_MAC_TRUNCATION_OFFSET 8
787
788/** Macro to build a truncated MAC algorithm.
789 *
790 * A truncated MAC algorithm is identical to the corresponding MAC
791 * algorithm except that the MAC value for the truncated algorithm
792 * consists of only the first \p mac_length bytes of the MAC value
793 * for the untruncated algorithm.
794 *
795 * \note This macro may allow constructing algorithm identifiers that
796 * are not valid, either because the specified length is larger
797 * than the untruncated MAC or because the specified length is
798 * smaller than permitted by the implementation.
799 *
800 * \note It is implementation-defined whether a truncated MAC that
801 * is truncated to the same length as the MAC of the untruncated
802 * algorithm is considered identical to the untruncated algorithm
803 * for policy comparison purposes.
804 *
805 * \param alg A MAC algorithm identifier (value of type
806 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg)
807 * is true). This may be a truncated or untruncated
808 * MAC algorithm.
809 * \param mac_length Desired length of the truncated MAC in bytes.
Gilles Peskine6d72ff92018-08-21 14:55:08 +0200810 * This must be at most the full length of the MAC
811 * and must be at least an implementation-specified
812 * minimum. The implementation-specified minimum
813 * shall not be zero.
Gilles Peskined911eb72018-08-14 15:18:45 +0200814 *
815 * \return The corresponding MAC algorithm with the specified
816 * length.
817 * \return Unspecified if \p alg is not a supported
818 * MAC algorithm or if \p mac_length is too small or
819 * too large for the specified MAC algorithm.
820 */
821#define PSA_ALG_TRUNCATED_MAC(alg, mac_length) \
822 (((alg) & ~PSA_ALG_MAC_TRUNCATION_MASK) | \
823 ((mac_length) << PSA_MAC_TRUNCATION_OFFSET & PSA_ALG_MAC_TRUNCATION_MASK))
824
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +0200825/** Macro to build the base MAC algorithm corresponding to a truncated
826 * MAC algorithm.
827 *
828 * \param alg A MAC algorithm identifier (value of type
829 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg)
830 * is true). This may be a truncated or untruncated
831 * MAC algorithm.
832 *
833 * \return The corresponding base MAC algorithm.
834 * \return Unspecified if \p alg is not a supported
835 * MAC algorithm.
836 */
837#define PSA_ALG_FULL_LENGTH_MAC(alg) \
838 ((alg) & ~PSA_ALG_MAC_TRUNCATION_MASK)
839
Gilles Peskined911eb72018-08-14 15:18:45 +0200840/** Length to which a MAC algorithm is truncated.
841 *
842 * \param alg A MAC algorithm identifier (value of type
843 * #psa_algorithm_t such that #PSA_ALG_IS_MAC(\p alg)
844 * is true).
845 *
846 * \return Length of the truncated MAC in bytes.
847 * \return 0 if \p alg is a non-truncated MAC algorithm.
848 * \return Unspecified if \p alg is not a supported
849 * MAC algorithm.
850 */
851#define PSA_MAC_TRUNCATED_LENGTH(alg) \
852 (((alg) & PSA_ALG_MAC_TRUNCATION_MASK) >> PSA_MAC_TRUNCATION_OFFSET)
853
Gilles Peskine8c9def32018-02-08 10:02:12 +0100854#define PSA_ALG_CIPHER_MAC_BASE ((psa_algorithm_t)0x02c00000)
855#define PSA_ALG_CBC_MAC ((psa_algorithm_t)0x02c00001)
856#define PSA_ALG_CMAC ((psa_algorithm_t)0x02c00002)
857#define PSA_ALG_GMAC ((psa_algorithm_t)0x02c00003)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200858
859/** Whether the specified algorithm is a MAC algorithm based on a block cipher.
860 *
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200861 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
862 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200863 * \return 1 if \p alg is a MAC algorithm based on a block cipher, 0 otherwise.
864 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200865 * algorithm identifier.
866 */
Gilles Peskine9df2dc82018-08-22 18:24:17 +0200867#define PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) \
Gilles Peskine8c9def32018-02-08 10:02:12 +0100868 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_MAC_SUBCATEGORY_MASK)) == \
869 PSA_ALG_CIPHER_MAC_BASE)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100870
Gilles Peskinedaea26f2018-08-21 14:02:45 +0200871#define PSA_ALG_CIPHER_STREAM_FLAG ((psa_algorithm_t)0x00800000)
872#define PSA_ALG_CIPHER_FROM_BLOCK_FLAG ((psa_algorithm_t)0x00400000)
Gilles Peskine98f0a242018-02-06 18:57:29 +0100873
Gilles Peskinedcd14942018-07-12 00:30:52 +0200874/** Whether the specified algorithm is a stream cipher.
875 *
876 * A stream cipher is a symmetric cipher that encrypts or decrypts messages
877 * by applying a bitwise-xor with a stream of bytes that is generated
878 * from a key.
879 *
880 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
881 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200882 * \return 1 if \p alg is a stream cipher algorithm, 0 otherwise.
883 * This macro may return either 0 or 1 if \p alg is not a supported
Gilles Peskinedcd14942018-07-12 00:30:52 +0200884 * algorithm identifier or if it is not a symmetric cipher algorithm.
885 */
Moran Pekerbed71a22018-04-22 20:19:20 +0300886#define PSA_ALG_IS_STREAM_CIPHER(alg) \
Gilles Peskinedaea26f2018-08-21 14:02:45 +0200887 (((alg) & (PSA_ALG_CATEGORY_MASK | PSA_ALG_CIPHER_STREAM_FLAG)) == \
888 (PSA_ALG_CATEGORY_CIPHER | PSA_ALG_CIPHER_STREAM_FLAG))
889
890/** The ARC4 stream cipher algorithm.
891 */
892#define PSA_ALG_ARC4 ((psa_algorithm_t)0x04800001)
893
894/** The CTR stream cipher mode.
895 *
896 * CTR is a stream cipher which is built from a block cipher.
897 * The underlying block cipher is determined by the key type.
898 * For example, to use AES-128-CTR, use this algorithm with
899 * a key of type #PSA_KEY_TYPE_AES and a length of 128 bits (16 bytes).
900 */
901#define PSA_ALG_CTR ((psa_algorithm_t)0x04c00001)
902
903#define PSA_ALG_CFB ((psa_algorithm_t)0x04c00002)
904
905#define PSA_ALG_OFB ((psa_algorithm_t)0x04c00003)
906
907/** The XTS cipher mode.
908 *
909 * XTS is a cipher mode which is built from a block cipher. It requires at
910 * least one full block of input, but beyond this minimum the input
911 * does not need to be a whole number of blocks.
912 */
913#define PSA_ALG_XTS ((psa_algorithm_t)0x044000ff)
914
915/** The CBC block cipher chaining mode, with no padding.
916 *
917 * The underlying block cipher is determined by the key type.
918 *
919 * This symmetric cipher mode can only be used with messages whose lengths
920 * are whole number of blocks for the chosen block cipher.
921 */
922#define PSA_ALG_CBC_NO_PADDING ((psa_algorithm_t)0x04600100)
923
924/** The CBC block cipher chaining mode with PKCS#7 padding.
925 *
926 * The underlying block cipher is determined by the key type.
927 *
928 * This is the padding method defined by PKCS#7 (RFC 2315) &sect;10.3.
929 */
930#define PSA_ALG_CBC_PKCS7 ((psa_algorithm_t)0x04600101)
Moran Pekerbed71a22018-04-22 20:19:20 +0300931
Gilles Peskine23cc2ff2018-08-17 19:47:52 +0200932#define PSA_ALG_CCM ((psa_algorithm_t)0x06001001)
933#define PSA_ALG_GCM ((psa_algorithm_t)0x06001002)
934
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200935/* In the encoding of a AEAD algorithm, the bits corresponding to
936 * PSA_ALG_AEAD_TAG_LENGTH_MASK encode the length of the AEAD tag.
937 * The constants for default lengths follow this encoding.
938 */
Gilles Peskine23cc2ff2018-08-17 19:47:52 +0200939#define PSA_ALG_AEAD_TAG_LENGTH_MASK ((psa_algorithm_t)0x00003f00)
940#define PSA_AEAD_TAG_LENGTH_OFFSET 8
941
942/** Macro to build a shortened AEAD algorithm.
943 *
944 * A shortened AEAD algorithm is similar to the corresponding AEAD
945 * algorithm, but has an authentication tag that consists of fewer bytes.
946 * Depending on the algorithm, the tag length may affect the calculation
947 * of the ciphertext.
948 *
949 * \param alg A AEAD algorithm identifier (value of type
950 * #psa_algorithm_t such that #PSA_ALG_IS_AEAD(\p alg)
951 * is true).
Gilles Peskine31119812018-08-21 14:47:48 +0200952 * \param tag_length Desired length of the authentication tag in bytes.
Gilles Peskine23cc2ff2018-08-17 19:47:52 +0200953 *
954 * \return The corresponding AEAD algorithm with the specified
955 * length.
956 * \return Unspecified if \p alg is not a supported
957 * AEAD algorithm or if \p tag_length is not valid
958 * for the specified AEAD algorithm.
959 */
960#define PSA_ALG_AEAD_WITH_TAG_LENGTH(alg, tag_length) \
961 (((alg) & ~PSA_ALG_AEAD_TAG_LENGTH_MASK) | \
962 ((tag_length) << PSA_AEAD_TAG_LENGTH_OFFSET & \
963 PSA_ALG_AEAD_TAG_LENGTH_MASK))
Gilles Peskine98f0a242018-02-06 18:57:29 +0100964
Gilles Peskine70f46e12018-08-20 15:07:53 +0200965/** Calculate the corresponding AEAD algorithm with the default tag length.
966 *
967 * \param alg An AEAD algorithm (\c PSA_ALG_XXX value such that
968 * #PSA_ALG_IS_AEAD(\p alg) is true).
969 *
970 * \return The corresponding AEAD algorithm with the default tag length
971 * for that algorithm.
972 */
973#define PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH(alg) \
974 ( \
975 PSA__ALG_AEAD_WITH_DEFAULT_TAG_LENGTH__CASE(alg, PSA_ALG_CCM) \
976 PSA__ALG_AEAD_WITH_DEFAULT_TAG_LENGTH__CASE(alg, PSA_ALG_GCM) \
977 0)
978#define PSA__ALG_AEAD_WITH_DEFAULT_TAG_LENGTH__CASE(alg, ref) \
979 PSA_ALG_AEAD_WITH_TAG_LENGTH(alg, 0) == \
980 PSA_ALG_AEAD_WITH_TAG_LENGTH(ref, 0) ? \
981 ref :
982
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200983#define PSA_ALG_RSA_PKCS1V15_SIGN_BASE ((psa_algorithm_t)0x10020000)
984/** RSA PKCS#1 v1.5 signature with hashing.
985 *
986 * This is the signature scheme defined by RFC 8017
987 * (PKCS#1: RSA Cryptography Specifications) under the name
988 * RSASSA-PKCS1-v1_5.
989 *
990 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200991 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200992 *
993 * \return The corresponding RSA PKCS#1 v1.5 signature algorithm.
994 * \return Unspecified if \p alg is not a supported
995 * hash algorithm.
996 */
Gilles Peskinea5926232018-03-28 14:16:50 +0200997#define PSA_ALG_RSA_PKCS1V15_SIGN(hash_alg) \
Gilles Peskine55bf3d12018-06-26 15:53:48 +0200998 (PSA_ALG_RSA_PKCS1V15_SIGN_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
999/** Raw PKCS#1 v1.5 signature.
1000 *
1001 * The input to this algorithm is the DigestInfo structure used by
1002 * RFC 8017 (PKCS#1: RSA Cryptography Specifications), &sect;9.2
1003 * steps 3&ndash;6.
1004 */
1005#define PSA_ALG_RSA_PKCS1V15_SIGN_RAW PSA_ALG_RSA_PKCS1V15_SIGN_BASE
Gilles Peskinea5926232018-03-28 14:16:50 +02001006#define PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) \
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001007 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PKCS1V15_SIGN_BASE)
Gilles Peskinedcd14942018-07-12 00:30:52 +02001008
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001009#define PSA_ALG_RSA_PSS_BASE ((psa_algorithm_t)0x10030000)
1010/** RSA PSS signature with hashing.
1011 *
1012 * This is the signature scheme defined by RFC 8017
1013 * (PKCS#1: RSA Cryptography Specifications) under the name
Gilles Peskinea4d20bd2018-06-29 23:35:02 +02001014 * RSASSA-PSS, with the message generation function MGF1, and with
1015 * a salt length equal to the length of the hash. The specified
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001016 * hash algorithm is used to hash the input message, to create the
1017 * salted hash, and for the mask generation.
1018 *
1019 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001020 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001021 *
1022 * \return The corresponding RSA PSS signature algorithm.
1023 * \return Unspecified if \p alg is not a supported
1024 * hash algorithm.
1025 */
1026#define PSA_ALG_RSA_PSS(hash_alg) \
1027 (PSA_ALG_RSA_PSS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1028#define PSA_ALG_IS_RSA_PSS(alg) \
1029 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_PSS_BASE)
1030
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001031#define PSA_ALG_DSA_BASE ((psa_algorithm_t)0x10040000)
1032/** DSA signature with hashing.
1033 *
1034 * This is the signature scheme defined by FIPS 186-4,
1035 * with a random per-message secret number (*k*).
1036 *
1037 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001038 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001039 *
1040 * \return The corresponding DSA signature algorithm.
1041 * \return Unspecified if \p alg is not a supported
1042 * hash algorithm.
1043 */
1044#define PSA_ALG_DSA(hash_alg) \
1045 (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1046#define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t)0x10050000)
1047#define PSA_ALG_DSA_DETERMINISTIC_FLAG ((psa_algorithm_t)0x00010000)
1048#define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
1049 (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1050#define PSA_ALG_IS_DSA(alg) \
1051 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
1052 PSA_ALG_DSA_BASE)
1053#define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
1054 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
Gilles Peskine55728b02018-07-16 23:08:16 +02001055#define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \
1056 (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg))
1057#define PSA_ALG_IS_RANDOMIZED_DSA(alg) \
1058 (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg))
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001059
1060#define PSA_ALG_ECDSA_BASE ((psa_algorithm_t)0x10060000)
1061/** ECDSA signature with hashing.
1062 *
1063 * This is the ECDSA signature scheme defined by ANSI X9.62,
1064 * with a random per-message secret number (*k*).
1065 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001066 * The representation of the signature as a byte string consists of
1067 * the concatentation of the signature values *r* and *s*. Each of
1068 * *r* and *s* is encoded as an *N*-octet string, where *N* is the length
1069 * of the base point of the curve in octets. Each value is represented
1070 * in big-endian order (most significant octet first).
1071 *
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001072 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001073 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001074 *
1075 * \return The corresponding ECDSA signature algorithm.
1076 * \return Unspecified if \p alg is not a supported
1077 * hash algorithm.
1078 */
1079#define PSA_ALG_ECDSA(hash_alg) \
1080 (PSA_ALG_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1081/** ECDSA signature without hashing.
1082 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001083 * This is the same signature scheme as #PSA_ALG_ECDSA(), but
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001084 * without specifying a hash algorithm. This algorithm may only be
1085 * used to sign or verify a sequence of bytes that should be an
1086 * already-calculated hash. Note that the input is padded with
1087 * zeros on the left or truncated on the left as required to fit
1088 * the curve size.
1089 */
1090#define PSA_ALG_ECDSA_ANY PSA_ALG_ECDSA_BASE
1091#define PSA_ALG_DETERMINISTIC_ECDSA_BASE ((psa_algorithm_t)0x10070000)
1092/** Deterministic ECDSA signature with hashing.
1093 *
1094 * This is the deterministic ECDSA signature scheme defined by RFC 6979.
1095 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001096 * The representation of a signature is the same as with #PSA_ALG_ECDSA().
1097 *
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001098 * Note that when this algorithm is used for verification, signatures
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001099 * made with randomized ECDSA (#PSA_ALG_ECDSA(\p hash_alg)) with the
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001100 * same private key are accepted. In other words,
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001101 * #PSA_ALG_DETERMINISTIC_ECDSA(\p hash_alg) differs from
1102 * #PSA_ALG_ECDSA(\p hash_alg) only for signature, not for verification.
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001103 *
1104 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001105 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001106 *
1107 * \return The corresponding deterministic ECDSA signature
1108 * algorithm.
1109 * \return Unspecified if \p alg is not a supported
1110 * hash algorithm.
1111 */
1112#define PSA_ALG_DETERMINISTIC_ECDSA(hash_alg) \
1113 (PSA_ALG_DETERMINISTIC_ECDSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1114#define PSA_ALG_IS_ECDSA(alg) \
1115 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
1116 PSA_ALG_ECDSA_BASE)
1117#define PSA_ALG_ECDSA_IS_DETERMINISTIC(alg) \
1118 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
Gilles Peskine55728b02018-07-16 23:08:16 +02001119#define PSA_ALG_IS_DETERMINISTIC_ECDSA(alg) \
1120 (PSA_ALG_IS_ECDSA(alg) && PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
1121#define PSA_ALG_IS_RANDOMIZED_ECDSA(alg) \
1122 (PSA_ALG_IS_ECDSA(alg) && !PSA_ALG_ECDSA_IS_DETERMINISTIC(alg))
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001123
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001124/** Get the hash used by a hash-and-sign signature algorithm.
1125 *
1126 * A hash-and-sign algorithm is a signature algorithm which is
1127 * composed of two phases: first a hashing phase which does not use
1128 * the key and produces a hash of the input message, then a signing
1129 * phase which only uses the hash and the key and not the message
1130 * itself.
1131 *
1132 * \param alg A signature algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001133 * #PSA_ALG_IS_SIGN(\p alg) is true).
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001134 *
1135 * \return The underlying hash algorithm if \p alg is a hash-and-sign
1136 * algorithm.
1137 * \return 0 if \p alg is a signature algorithm that does not
1138 * follow the hash-and-sign structure.
1139 * \return Unspecified if \p alg is not a signature algorithm or
1140 * if it is not supported by the implementation.
1141 */
1142#define PSA_ALG_SIGN_GET_HASH(alg) \
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001143 (PSA_ALG_IS_RSA_PSS(alg) || PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) || \
1144 PSA_ALG_IS_DSA(alg) || PSA_ALG_IS_ECDSA(alg) ? \
Gilles Peskine54622ae2018-06-29 22:24:24 +02001145 ((alg) & PSA_ALG_HASH_MASK) == 0 ? /*"raw" algorithm*/ 0 : \
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001146 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1147 0)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001148
Gilles Peskinedcd14942018-07-12 00:30:52 +02001149/** RSA PKCS#1 v1.5 encryption.
1150 */
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001151#define PSA_ALG_RSA_PKCS1V15_CRYPT ((psa_algorithm_t)0x12020000)
Gilles Peskinedcd14942018-07-12 00:30:52 +02001152
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001153#define PSA_ALG_RSA_OAEP_BASE ((psa_algorithm_t)0x12030000)
Gilles Peskinedcd14942018-07-12 00:30:52 +02001154/** RSA OAEP encryption.
1155 *
1156 * This is the encryption scheme defined by RFC 8017
1157 * (PKCS#1: RSA Cryptography Specifications) under the name
1158 * RSAES-OAEP, with the message generation function MGF1.
1159 *
1160 * \param hash_alg The hash algorithm (\c PSA_ALG_XXX value such that
1161 * #PSA_ALG_IS_HASH(\p hash_alg) is true) to use
1162 * for MGF1.
1163 *
1164 * \return The corresponding RSA OAEP signature algorithm.
1165 * \return Unspecified if \p alg is not a supported
1166 * hash algorithm.
1167 */
Gilles Peskine55bf3d12018-06-26 15:53:48 +02001168#define PSA_ALG_RSA_OAEP(hash_alg) \
1169 (PSA_ALG_RSA_OAEP_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1170#define PSA_ALG_IS_RSA_OAEP(alg) \
1171 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_RSA_OAEP_BASE)
Gilles Peskine072ac562018-06-30 00:21:29 +02001172#define PSA_ALG_RSA_OAEP_GET_HASH(alg) \
1173 (PSA_ALG_IS_RSA_OAEP(alg) ? \
1174 ((alg) & PSA_ALG_HASH_MASK) | PSA_ALG_CATEGORY_HASH : \
1175 0)
Gilles Peskined1e8e412018-06-07 09:49:39 +02001176
Gilles Peskinebef7f142018-07-12 17:22:21 +02001177#define PSA_ALG_HKDF_BASE ((psa_algorithm_t)0x30000100)
1178/** Macro to build an HKDF algorithm.
1179 *
1180 * For example, `PSA_ALG_HKDF(PSA_ALG_SHA256)` is HKDF using HMAC-SHA-256.
1181 *
1182 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1183 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1184 *
1185 * \return The corresponding HKDF algorithm.
1186 * \return Unspecified if \p alg is not a supported
1187 * hash algorithm.
1188 */
1189#define PSA_ALG_HKDF(hash_alg) \
1190 (PSA_ALG_HKDF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1191/** Whether the specified algorithm is an HKDF algorithm.
1192 *
1193 * HKDF is a family of key derivation algorithms that are based on a hash
1194 * function and the HMAC construction.
1195 *
1196 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1197 *
1198 * \return 1 if \c alg is an HKDF algorithm, 0 otherwise.
1199 * This macro may return either 0 or 1 if \c alg is not a supported
1200 * key derivation algorithm identifier.
1201 */
1202#define PSA_ALG_IS_HKDF(alg) \
1203 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_HKDF_BASE)
1204#define PSA_ALG_HKDF_GET_HASH(hkdf_alg) \
1205 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1206
Hanno Becker79250c22018-10-09 17:32:46 +01001207#define PSA_ALG_TLS12_PRF_BASE ((psa_algorithm_t)0x30000200)
1208/** Macro to build a TLS-1.2 PRF algorithm.
1209 *
Hanno Becker2255a362018-11-16 16:05:13 +00001210 * TLS 1.2 uses a custom pseudorandom function (PRF) for key schedule,
1211 * specified in Section 5 of RFC 5246. It is based on HMAC and can be
1212 * used with either SHA-256 or SHA-384.
1213 *
1214 * For the application to TLS-1.2, the salt and label arguments passed
1215 * to psa_key_derivation() are what's called 'seed' and 'label' in RFC 5246,
1216 * respectively. For example, for TLS key expansion, the salt is the
1217 * concatenation of ServerHello.Random + ClientHello.Random,
1218 * while the label is "key expansion".
1219 *
Hanno Becker79250c22018-10-09 17:32:46 +01001220 * For example, `PSA_ALG_TLS12_PRF(PSA_ALG_SHA256)` represents the
1221 * TLS 1.2 PRF using HMAC-SHA-256.
1222 *
1223 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1224 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1225 *
1226 * \return The corresponding TLS-1.2 PRF algorithm.
1227 * \return Unspecified if \p alg is not a supported
1228 * hash algorithm.
1229 */
1230#define PSA_ALG_TLS12_PRF(hash_alg) \
1231 (PSA_ALG_TLS12_PRF_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1232
1233/** Whether the specified algorithm is a TLS-1.2 PRF algorithm.
1234 *
Hanno Becker79250c22018-10-09 17:32:46 +01001235 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1236 *
1237 * \return 1 if \c alg is a TLS-1.2 PRF algorithm, 0 otherwise.
1238 * This macro may return either 0 or 1 if \c alg is not a supported
1239 * key derivation algorithm identifier.
1240 */
1241#define PSA_ALG_IS_TLS12_PRF(alg) \
1242 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PRF_BASE)
1243#define PSA_ALG_TLS12_PRF_GET_HASH(hkdf_alg) \
1244 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1245
Hanno Becker8dbfca42018-10-12 11:56:55 +01001246#define PSA_ALG_TLS12_PSK_TO_MS_BASE ((psa_algorithm_t)0x30000300)
1247/** Macro to build a TLS-1.2 PSK-to-MasterSecret algorithm.
1248 *
Hanno Becker2255a362018-11-16 16:05:13 +00001249 * In a pure-PSK handshake in TLS 1.2, the master secret is derived
1250 * from the PreSharedKey (PSK) through the application of padding
1251 * (RFC 4279, Section 2) and the TLS-1.2 PRF (RFC 5246, Section 5).
1252 * The latter is based on HMAC and can be used with either SHA-256
1253 * or SHA-384.
1254 *
1255 * For the application to TLS-1.2, the salt passed to psa_key_derivation()
1256 * (and forwarded to the TLS-1.2 PRF) is the concatenation of the
1257 * ClientHello.Random + ServerHello.Random, while the label is "master secret"
1258 * or "extended master secret".
1259 *
Hanno Becker8dbfca42018-10-12 11:56:55 +01001260 * For example, `PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA256)` represents the
1261 * TLS-1.2 PSK to MasterSecret derivation PRF using HMAC-SHA-256.
1262 *
1263 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
1264 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
1265 *
1266 * \return The corresponding TLS-1.2 PSK to MS algorithm.
1267 * \return Unspecified if \p alg is not a supported
1268 * hash algorithm.
1269 */
1270#define PSA_ALG_TLS12_PSK_TO_MS(hash_alg) \
1271 (PSA_ALG_TLS12_PSK_TO_MS_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
1272
1273/** Whether the specified algorithm is a TLS-1.2 PSK to MS algorithm.
1274 *
Hanno Becker8dbfca42018-10-12 11:56:55 +01001275 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1276 *
1277 * \return 1 if \c alg is a TLS-1.2 PSK to MS algorithm, 0 otherwise.
1278 * This macro may return either 0 or 1 if \c alg is not a supported
1279 * key derivation algorithm identifier.
1280 */
1281#define PSA_ALG_IS_TLS12_PSK_TO_MS(alg) \
1282 (((alg) & ~PSA_ALG_HASH_MASK) == PSA_ALG_TLS12_PSK_TO_MS_BASE)
1283#define PSA_ALG_TLS12_PSK_TO_MS_GET_HASH(hkdf_alg) \
1284 (PSA_ALG_CATEGORY_HASH | ((hkdf_alg) & PSA_ALG_HASH_MASK))
1285
Gilles Peskinee8f0e3d2018-09-18 11:52:10 +02001286#define PSA_ALG_KEY_DERIVATION_MASK ((psa_algorithm_t)0x010fffff)
1287
1288/** Use a shared secret as is.
1289 *
1290 * Specify this algorithm as the selection component of a key agreement
1291 * to use the raw result of the key agreement as key material.
1292 *
1293 * \warning The raw result of a key agreement algorithm such as finite-field
1294 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
1295 * not be used directly as key material. It can however be used as the secret
1296 * input in a key derivation algorithm.
1297 */
1298#define PSA_ALG_SELECT_RAW ((psa_algorithm_t)0x31000001)
1299
1300#define PSA_ALG_KEY_AGREEMENT_GET_KDF(alg) \
1301 (((alg) & PSA_ALG_KEY_DERIVATION_MASK) | PSA_ALG_CATEGORY_KEY_DERIVATION)
1302
1303#define PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) \
1304 ((alg) & ~PSA_ALG_KEY_DERIVATION_MASK)
Gilles Peskine93098fd2018-09-18 11:54:43 +02001305
1306#define PSA_ALG_FFDH_BASE ((psa_algorithm_t)0x22100000)
1307/** The Diffie-Hellman key agreement algorithm.
1308 *
Gilles Peskine2607bca2018-10-25 22:21:03 +02001309 * This algorithm combines the finite-field Diffie-Hellman (DH) key
1310 * agreement, also known as Diffie-Hellman-Merkle (DHM) key agreement,
1311 * to produce a shared secret from a private key and the peer's
Gilles Peskine93098fd2018-09-18 11:54:43 +02001312 * public key, with a key selection or key derivation algorithm to produce
1313 * one or more shared keys and other shared cryptographic material.
1314 *
Gilles Peskine99d02592018-11-15 17:47:25 +01001315 * The shared secret produced by key agreement and passed as input to the
1316 * derivation or selection algorithm \p kdf_alg is the shared secret
1317 * `g^{ab}` in big-endian format.
1318 * It is `ceiling(m / 8)` bytes long where `m` is the size of the prime `p`
1319 * in bits.
Gilles Peskine79dd6222018-10-25 22:22:11 +02001320 *
Gilles Peskine93098fd2018-09-18 11:54:43 +02001321 * \param kdf_alg A key derivation algorithm (\c PSA_ALG_XXX value such
1322 * that #PSA_ALG_IS_KEY_DERIVATION(\p hash_alg) is true)
1323 * or a key selection algorithm (\c PSA_ALG_XXX value such
Gilles Peskine19643c52018-11-16 16:45:02 +01001324 * that #PSA_ALG_IS_KEY_SELECTION(\p hash_alg) is true).
Gilles Peskine93098fd2018-09-18 11:54:43 +02001325 *
1326 * \return The Diffie-Hellman algorithm with the specified
1327 * selection or derivation algorithm.
1328 */
1329#define PSA_ALG_FFDH(kdf_alg) \
1330 (PSA_ALG_FFDH_BASE | ((kdf_alg) & PSA_ALG_KEY_DERIVATION_MASK))
1331/** Whether the specified algorithm is a finite field Diffie-Hellman algorithm.
1332 *
1333 * This includes every supported key selection or key agreement algorithm
1334 * for the output of the Diffie-Hellman calculation.
1335 *
1336 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1337 *
1338 * \return 1 if \c alg is a finite field Diffie-Hellman algorithm, 0 otherwise.
1339 * This macro may return either 0 or 1 if \c alg is not a supported
1340 * key agreement algorithm identifier.
1341 */
1342#define PSA_ALG_IS_FFDH(alg) \
1343 (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_FFDH_BASE)
1344
1345#define PSA_ALG_ECDH_BASE ((psa_algorithm_t)0x22200000)
Gilles Peskine2607bca2018-10-25 22:21:03 +02001346/** The elliptic curve Diffie-Hellman (ECDH) key agreement algorithm.
Gilles Peskine93098fd2018-09-18 11:54:43 +02001347 *
1348 * This algorithm combines the elliptic curve Diffie-Hellman key
1349 * agreement to produce a shared secret from a private key and the peer's
1350 * public key, with a key selection or key derivation algorithm to produce
1351 * one or more shared keys and other shared cryptographic material.
1352 *
Gilles Peskine7b5b4a02018-11-14 21:05:10 +01001353 * The shared secret produced by key agreement and passed as input to the
1354 * derivation or selection algorithm \p kdf_alg is the x-coordinate of
Gilles Peskine6c6a0232018-11-15 17:44:43 +01001355 * the shared secret point. It is always `ceiling(m / 8)` bytes long where
1356 * `m` is the bit size associated with the curve, i.e. the bit size of the
1357 * order of the curve's coordinate field. When `m` is not a multiple of 8,
Gilles Peskine7b5b4a02018-11-14 21:05:10 +01001358 * the byte containing the most significant bit of the shared secret
1359 * is padded with zero bits. The byte order is either little-endian
1360 * or big-endian depending on the curve type.
1361 *
1362 * - For Montgomery curves (curve types `PSA_ECC_CURVE_CURVEXXX`),
1363 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1364 * in little-endian byte order.
1365 * The bit size is 448 for Curve448 and 255 for Curve25519.
1366 * - For Weierstrass curves over prime fields (curve types
1367 * `PSA_ECC_CURVE_SECPXXX` and `PSA_ECC_CURVE_BRAINPOOL_PXXX`),
1368 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1369 * in big-endian byte order.
Gilles Peskine6c6a0232018-11-15 17:44:43 +01001370 * The bit size is `m = ceiling(log_2(p))` for the field `F_p`.
Gilles Peskine7b5b4a02018-11-14 21:05:10 +01001371 * - For Weierstrass curves over binary fields (curve types
1372 * `PSA_ECC_CURVE_SECTXXX`),
1373 * the shared secret is the x-coordinate of `d_A Q_B = d_B Q_A`
1374 * in big-endian byte order.
Gilles Peskine6c6a0232018-11-15 17:44:43 +01001375 * The bit size is `m` for the field `F_{2^m}`.
Gilles Peskine79dd6222018-10-25 22:22:11 +02001376 *
Gilles Peskine93098fd2018-09-18 11:54:43 +02001377 * \param kdf_alg A key derivation algorithm (\c PSA_ALG_XXX value such
1378 * that #PSA_ALG_IS_KEY_DERIVATION(\p hash_alg) is true)
1379 * or a selection algorithm (\c PSA_ALG_XXX value such
1380 * that #PSA_ALG_IS_KEY_SELECTION(\p hash_alg) is true).
1381 *
1382 * \return The Diffie-Hellman algorithm with the specified
1383 * selection or derivation algorithm.
1384 */
1385#define PSA_ALG_ECDH(kdf_alg) \
1386 (PSA_ALG_ECDH_BASE | ((kdf_alg) & PSA_ALG_KEY_DERIVATION_MASK))
1387/** Whether the specified algorithm is an elliptic curve Diffie-Hellman
1388 * algorithm.
1389 *
1390 * This includes every supported key selection or key agreement algorithm
1391 * for the output of the Diffie-Hellman calculation.
1392 *
1393 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
1394 *
1395 * \return 1 if \c alg is an elliptic curve Diffie-Hellman algorithm,
1396 * 0 otherwise.
1397 * This macro may return either 0 or 1 if \c alg is not a supported
1398 * key agreement algorithm identifier.
1399 */
1400#define PSA_ALG_IS_ECDH(alg) \
1401 (PSA_ALG_KEY_AGREEMENT_GET_BASE(alg) == PSA_ALG_ECDH_BASE)
1402
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001403/**@}*/
1404
1405/** \defgroup key_management Key management
1406 * @{
1407 */
1408
Gilles Peskine3cac8c42018-11-30 14:07:45 +01001409/** Encoding of key lifetimes.
1410 */
1411typedef uint32_t psa_key_lifetime_t;
1412
1413/** Encoding of identifiers of persistent keys.
1414 */
1415typedef uint32_t psa_key_id_t;
1416
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01001417/** A volatile key only exists as long as the handle to it is not closed.
1418 * The key material is guaranteed to be erased on a power reset.
Gilles Peskine3cac8c42018-11-30 14:07:45 +01001419 */
1420#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
1421
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01001422/** The default storage area for persistent keys.
1423 *
1424 * A persistent key remains in storage until it is explicitly destroyed or
1425 * until the corresponding storage area is wiped. This specification does
1426 * not define any mechanism to wipe a storage area, but implementations may
1427 * provide their own mechanism (for example to perform a factory reset,
1428 * to prepare for device refurbishment, or to uninstall an application).
1429 *
1430 * This lifetime value is the default storage area for the calling
1431 * application. Implementations may offer other storage areas designated
1432 * by other lifetime values as implementation-specific extensions.
Gilles Peskine3cac8c42018-11-30 14:07:45 +01001433 */
1434#define PSA_KEY_LIFETIME_PERSISTENT ((psa_key_lifetime_t)0x00000001)
1435
Gilles Peskineae32aac2018-11-30 14:39:32 +01001436/** \brief Retrieve the lifetime of an open key.
Gilles Peskine3cac8c42018-11-30 14:07:45 +01001437 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001438 * \param handle Handle to query.
Gilles Peskine3cac8c42018-11-30 14:07:45 +01001439 * \param[out] lifetime On success, the lifetime value.
1440 *
1441 * \retval #PSA_SUCCESS
1442 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001443 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine3cac8c42018-11-30 14:07:45 +01001444 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1445 * \retval #PSA_ERROR_HARDWARE_FAILURE
1446 * \retval #PSA_ERROR_TAMPERING_DETECTED
1447 * \retval #PSA_ERROR_BAD_STATE
1448 * The library has not been previously initialized by psa_crypto_init().
1449 * It is implementation-dependent whether a failure to initialize
1450 * results in this error code.
1451 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001452psa_status_t psa_get_key_lifetime(psa_key_handle_t handle,
Gilles Peskine3cac8c42018-11-30 14:07:45 +01001453 psa_key_lifetime_t *lifetime);
1454
Gilles Peskine3cac8c42018-11-30 14:07:45 +01001455
Gilles Peskinef535eb22018-11-30 14:08:36 +01001456/** Allocate a key slot for a transient key, i.e. a key which is only stored
1457 * in volatile memory.
1458 *
1459 * The allocated key slot and its handle remain valid until the
1460 * application calls psa_close_key() or psa_destroy_key() or until the
1461 * application terminates.
1462 *
1463 * This function takes a key type and maximum size as arguments so that
1464 * the implementation can reserve a corresponding amount of memory.
1465 * Implementations are not required to enforce this limit: if the application
1466 * later tries to create a larger key or a key of a different type, it
1467 * is implementation-defined whether this may succeed.
1468 *
1469 * \param type The type of key that the slot will contain.
1470 * \param max_bits The maximum key size that the slot will contain.
1471 * \param[out] handle On success, a handle to a volatile key slot.
1472 *
1473 * \retval #PSA_SUCCESS
1474 * Success. The application can now use the value of `*handle`
1475 * to access the newly allocated key slot.
1476 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1477 * There was not enough memory, or the maximum number of key slots
1478 * has been reached.
1479 * \retval #PSA_ERROR_INVALID_ARGUMENT
1480 * This implementation does not support this key type.
1481 */
1482
1483psa_status_t psa_allocate_key(psa_key_type_t type,
1484 size_t max_bits,
1485 psa_key_handle_t *handle);
1486
1487/** Open a handle to an existing persistent key.
1488 *
1489 * Open a handle to a key which was previously created with psa_create_key().
1490 *
1491 * \param lifetime The lifetime of the key. This designates a storage
1492 * area where the key material is stored. This must not
1493 * be #PSA_KEY_LIFETIME_VOLATILE.
1494 * \param id The persistent identifier of the key.
1495 * \param[out] handle On success, a handle to a key slot which contains
1496 * the data and metadata loaded from the specified
1497 * persistent location.
1498 *
1499 * \retval #PSA_SUCCESS
1500 * Success. The application can now use the value of `*handle`
1501 * to access the newly allocated key slot.
1502 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1503 * \retval #PSA_ERROR_EMPTY_SLOT
1504 * \retval #PSA_ERROR_INVALID_ARGUMENT
1505 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
1506 * \retval #PSA_ERROR_INVALID_ARGUMENT
1507 * \p id is invalid for the specified lifetime.
1508 * \retval #PSA_ERROR_NOT_SUPPORTED
1509 * \p lifetime is not supported.
1510 * \retval #PSA_ERROR_NOT_PERMITTED
1511 * The specified key exists, but the application does not have the
1512 * permission to access it. Note that this specification does not
1513 * define any way to create such a key, but it may be possible
1514 * through implementation-specific means.
1515 */
1516psa_status_t psa_open_key(psa_key_lifetime_t lifetime,
1517 psa_key_id_t id,
1518 psa_key_handle_t *handle);
1519
1520/** Create a new persistent key slot.
1521 *
1522 * Create a new persistent key slot and return a handle to it. The handle
1523 * remains valid until the application calls psa_close_key() or terminates.
1524 * The application can open the key again with psa_open_key() until it
1525 * removes the key by calling psa_destroy_key().
1526 *
1527 * \param lifetime The lifetime of the key. This designates a storage
1528 * area where the key material is stored. This must not
1529 * be #PSA_KEY_LIFETIME_VOLATILE.
1530 * \param id The persistent identifier of the key.
1531 * \param type The type of key that the slot will contain.
1532 * \param max_bits The maximum key size that the slot will contain.
1533 * \param[out] handle On success, a handle to the newly created key slot.
1534 * When key material is later created in this key slot,
1535 * it will be saved to the specified persistent location.
1536 *
1537 * \retval #PSA_SUCCESS
1538 * Success. The application can now use the value of `*handle`
1539 * to access the newly allocated key slot.
1540 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1541 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1542 * \retval #PSA_ERROR_OCCUPIED_SLOT
1543 * There is already a key with the identifier \p id in the storage
1544 * area designated by \p lifetime.
1545 * \retval #PSA_ERROR_INVALID_ARGUMENT
1546 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
1547 * \retval #PSA_ERROR_INVALID_ARGUMENT
1548 * \p id is invalid for the specified lifetime.
1549 * \retval #PSA_ERROR_NOT_SUPPORTED
1550 * \p lifetime is not supported.
1551 * \retval #PSA_ERROR_NOT_PERMITTED
1552 * \p lifetime is valid, but the application does not have the
1553 * permission to create a key there.
1554 */
1555psa_status_t psa_create_key(psa_key_lifetime_t lifetime,
1556 psa_key_id_t id,
1557 psa_key_type_t type,
1558 size_t max_bits,
1559 psa_key_handle_t *handle);
1560
1561/** Close a key handle.
1562 *
1563 * If the handle designates a volatile key, destroy the key material and
1564 * free all associated resources, just like psa_destroy_key().
1565 *
1566 * If the handle designates a persistent key, free all resources associated
1567 * with the key in volatile memory. The key slot in persistent storage is
1568 * not affected and can be opened again later with psa_open_key().
1569 *
1570 * \param handle The key handle to close.
1571 *
1572 * \retval #PSA_SUCCESS
1573 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +01001574 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +01001575 */
1576psa_status_t psa_close_key(psa_key_handle_t handle);
1577
Gilles Peskine3cac8c42018-11-30 14:07:45 +01001578/**@}*/
1579
1580/** \defgroup import_export Key import and export
1581 * @{
1582 */
1583
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001584/**
1585 * \brief Import a key in binary format.
1586 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +01001587 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +01001588 * documentation of psa_export_public_key() for the format of public keys
1589 * and to the documentation of psa_export_key() for the format for
1590 * other key types.
1591 *
1592 * This specification supports a single format for each key type.
1593 * Implementations may support other formats as long as the standard
1594 * format is supported. Implementations that support other formats
1595 * should ensure that the formats are clearly unambiguous so as to
1596 * minimize the risk that an invalid input is accidentally interpreted
1597 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001598 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001599 * \param handle Handle to the slot where the key will be stored.
1600 * This must be a valid slot for a key of the chosen
1601 * type: it must have been obtained by calling
1602 * psa_allocate_key() or psa_create_key() with the
1603 * correct \p type and with a maximum size that is
1604 * compatible with \p data.
Gilles Peskinef7933932018-10-31 14:07:52 +01001605 * \param type Key type (a \c PSA_KEY_TYPE_XXX value). On a successful
1606 * import, the key slot will contain a key of this type.
1607 * \param[in] data Buffer containing the key data. The content of this
1608 * buffer is interpreted according to \p type. It must
1609 * contain the format described in the documentation
1610 * of psa_export_key() or psa_export_public_key() for
1611 * the chosen type.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001612 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001613 *
Gilles Peskine28538492018-07-11 17:34:00 +02001614 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001615 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01001616 * If the key is persistent, the key material and the key's metadata
1617 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001618 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001619 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001620 * The key type or key size is not supported, either by the
1621 * implementation in general or in this particular slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001622 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine308b91d2018-02-08 09:47:44 +01001623 * The key slot is invalid,
1624 * or the key data is not correctly formatted.
Gilles Peskine28538492018-07-11 17:34:00 +02001625 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskine65eb8582018-04-19 08:28:58 +02001626 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +02001627 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1628 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1629 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +01001630 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +02001631 * \retval #PSA_ERROR_HARDWARE_FAILURE
1632 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001633 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001634 * The library has not been previously initialized by psa_crypto_init().
1635 * It is implementation-dependent whether a failure to initialize
1636 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001637 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001638psa_status_t psa_import_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001639 psa_key_type_t type,
1640 const uint8_t *data,
1641 size_t data_length);
1642
1643/**
Gilles Peskineae32aac2018-11-30 14:39:32 +01001644 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +02001645 *
1646 * This function destroys the content of the key slot from both volatile
1647 * memory and, if applicable, non-volatile storage. Implementations shall
1648 * make a best effort to ensure that any previous content of the slot is
1649 * unrecoverable.
1650 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001651 * This function also erases any metadata such as policies and frees all
1652 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +02001653 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001654 * \param handle Handle to the key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001655 *
Gilles Peskine28538492018-07-11 17:34:00 +02001656 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +02001657 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +02001658 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001659 * The slot holds content and cannot be erased because it is
1660 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001661 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001662 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +02001663 * There was an failure in communication with the cryptoprocessor.
1664 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +02001665 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +02001666 * The storage is corrupted. Implementations shall make a best effort
1667 * to erase key material even in this stage, however applications
1668 * should be aware that it may be impossible to guarantee that the
1669 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +02001670 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +02001671 * An unexpected condition which is not a storage corruption or
1672 * a communication failure occurred. The cryptoprocessor may have
1673 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +03001674 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001675 * The library has not been previously initialized by psa_crypto_init().
1676 * It is implementation-dependent whether a failure to initialize
1677 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001678 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001679psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001680
1681/**
1682 * \brief Get basic metadata about a key.
1683 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001684 * \param handle Handle to the key slot to query.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001685 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001686 * This may be a null pointer, in which case the key type
1687 * is not written.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001688 * \param[out] bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +01001689 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +01001690 * is not written.
1691 *
Gilles Peskine28538492018-07-11 17:34:00 +02001692 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +01001693 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001694 * \retval #PSA_ERROR_EMPTY_SLOT
Gilles Peskineae32aac2018-11-30 14:39:32 +01001695 * The handle is to a key slot which does not contain key material yet.
Gilles Peskine28538492018-07-11 17:34:00 +02001696 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1697 * \retval #PSA_ERROR_HARDWARE_FAILURE
1698 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001699 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001700 * The library has not been previously initialized by psa_crypto_init().
1701 * It is implementation-dependent whether a failure to initialize
1702 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001703 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001704psa_status_t psa_get_key_information(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001705 psa_key_type_t *type,
1706 size_t *bits);
1707
1708/**
1709 * \brief Export a key in binary format.
1710 *
1711 * The output of this function can be passed to psa_import_key() to
1712 * create an equivalent object.
1713 *
Gilles Peskinef7933932018-10-31 14:07:52 +01001714 * If the implementation of psa_import_key() supports other formats
1715 * beyond the format specified here, the output from psa_export_key()
1716 * must use the representation specified here, not the original
1717 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001718 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001719 * For standard key types, the output format is as follows:
1720 *
1721 * - For symmetric keys (including MAC keys), the format is the
1722 * raw bytes of the key.
1723 * - For DES, the key data consists of 8 bytes. The parity bits must be
1724 * correct.
1725 * - For Triple-DES, the format is the concatenation of the
1726 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +01001727 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001728 * is the non-encrypted DER encoding of the representation defined by
1729 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
1730 * ```
1731 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001732 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001733 * modulus INTEGER, -- n
1734 * publicExponent INTEGER, -- e
1735 * privateExponent INTEGER, -- d
1736 * prime1 INTEGER, -- p
1737 * prime2 INTEGER, -- q
1738 * exponent1 INTEGER, -- d mod (p-1)
1739 * exponent2 INTEGER, -- d mod (q-1)
1740 * coefficient INTEGER, -- (inverse of q) mod p
1741 * }
1742 * ```
1743 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format
1744 * is the non-encrypted DER encoding of the representation used by
Gilles Peskinec6290c02018-08-13 17:24:59 +02001745 * OpenSSL and OpenSSH, whose structure is described in ASN.1 as follows:
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001746 * ```
1747 * DSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001748 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001749 * prime INTEGER, -- p
1750 * subprime INTEGER, -- q
1751 * generator INTEGER, -- g
1752 * public INTEGER, -- y
1753 * private INTEGER, -- x
1754 * }
1755 * ```
1756 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +01001757 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +01001758 * a representation of the private value as a `ceiling(m/8)`-byte string
1759 * where `m` is the bit size associated with the curve, i.e. the bit size
1760 * of the order of the curve's coordinate field. This byte string is
1761 * in little-endian order for Montgomery curves (curve types
1762 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
1763 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
1764 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +01001765 * This is the content of the `privateKey` field of the `ECPrivateKey`
1766 * format defined by RFC 5915.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001767 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
1768 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +01001769 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001770 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001771 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001772 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001773 * \param[out] data_length On success, the number of bytes
1774 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001775 *
Gilles Peskine28538492018-07-11 17:34:00 +02001776 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +01001777 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001778 * \retval #PSA_ERROR_EMPTY_SLOT
1779 * \retval #PSA_ERROR_NOT_PERMITTED
Darryl Green9e2d7a02018-07-24 16:33:30 +01001780 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +02001781 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1782 * The size of the \p data buffer is too small. You can determine a
1783 * sufficient buffer size by calling
1784 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
1785 * where \c type is the key type
1786 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +02001787 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1788 * \retval #PSA_ERROR_HARDWARE_FAILURE
1789 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001790 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001791 * The library has not been previously initialized by psa_crypto_init().
1792 * It is implementation-dependent whether a failure to initialize
1793 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001794 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001795psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001796 uint8_t *data,
1797 size_t data_size,
1798 size_t *data_length);
1799
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001800/**
1801 * \brief Export a public key or the public part of a key pair in binary format.
1802 *
1803 * The output of this function can be passed to psa_import_key() to
1804 * create an object that is equivalent to the public key.
1805 *
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001806 * The format is the DER representation defined by RFC 5280 as
1807 * `SubjectPublicKeyInfo`, with the `subjectPublicKey` format
1808 * specified below.
1809 * ```
1810 * SubjectPublicKeyInfo ::= SEQUENCE {
1811 * algorithm AlgorithmIdentifier,
1812 * subjectPublicKey BIT STRING }
1813 * AlgorithmIdentifier ::= SEQUENCE {
1814 * algorithm OBJECT IDENTIFIER,
1815 * parameters ANY DEFINED BY algorithm OPTIONAL }
1816 * ```
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001817 *
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001818 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY),
1819 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.1 as
1820 * `RSAPublicKey`,
1821 * with the OID `rsaEncryption`,
1822 * and with the parameters `NULL`.
1823 * ```
1824 * pkcs-1 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840)
1825 * rsadsi(113549) pkcs(1) 1 }
1826 * rsaEncryption OBJECT IDENTIFIER ::= { pkcs-1 1 }
1827 *
1828 * RSAPublicKey ::= SEQUENCE {
1829 * modulus INTEGER, -- n
1830 * publicExponent INTEGER } -- e
1831 * ```
1832 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY),
1833 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.2 as
1834 * `DSAPublicKey`,
1835 * with the OID `id-dsa`,
1836 * and with the parameters `DSS-Parms`.
1837 * ```
1838 * id-dsa OBJECT IDENTIFIER ::= {
1839 * iso(1) member-body(2) us(840) x9-57(10040) x9cm(4) 1 }
1840 *
1841 * Dss-Parms ::= SEQUENCE {
1842 * p INTEGER,
1843 * q INTEGER,
1844 * g INTEGER }
1845 * DSAPublicKey ::= INTEGER -- public key, Y
1846 * ```
1847 * - For elliptic curve public keys (key types for which
1848 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true),
1849 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.5 as
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001850 * `ECPoint`, which contains the uncompressed
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001851 * representation defined by SEC1 &sect;2.3.3.
1852 * The OID is `id-ecPublicKey`,
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001853 * and the parameters must be given as a `namedCurve` OID as specified in
Gilles Peskinec6290c02018-08-13 17:24:59 +02001854 * RFC 5480 &sect;2.1.1.1 or other applicable standards.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001855 * ```
1856 * ansi-X9-62 OBJECT IDENTIFIER ::=
1857 * { iso(1) member-body(2) us(840) 10045 }
1858 * id-public-key-type OBJECT IDENTIFIER ::= { ansi-X9.62 2 }
1859 * id-ecPublicKey OBJECT IDENTIFIER ::= { id-publicKeyType 1 }
1860 *
Gilles Peskine4f6c77b2018-08-11 01:17:53 +02001861 * ECPoint ::= ...
1862 * -- first 8 bits: 0x04;
Gilles Peskine6c6a0232018-11-15 17:44:43 +01001863 * -- then x_P as a `ceiling(m/8)`-byte string, big endian;
1864 * -- then y_P as a `ceiling(m/8)`-byte string, big endian;
1865 * -- where `m` is the bit size associated with the curve,
Gilles Peskine7b5b4a02018-11-14 21:05:10 +01001866 * -- i.e. the bit size of `q` for a curve over `F_q`.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +02001867 *
1868 * EcpkParameters ::= CHOICE { -- other choices are not allowed
1869 * namedCurve OBJECT IDENTIFIER }
1870 * ```
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001871 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001872 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001873 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001874 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001875 * \param[out] data_length On success, the number of bytes
1876 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001877 *
Gilles Peskine28538492018-07-11 17:34:00 +02001878 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +01001879 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001880 * \retval #PSA_ERROR_EMPTY_SLOT
1881 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +02001882 * The key is neither a public key nor a key pair.
1883 * \retval #PSA_ERROR_NOT_SUPPORTED
1884 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1885 * The size of the \p data buffer is too small. You can determine a
1886 * sufficient buffer size by calling
1887 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
1888 * where \c type is the key type
1889 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +02001890 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1891 * \retval #PSA_ERROR_HARDWARE_FAILURE
1892 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001893 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001894 * The library has not been previously initialized by psa_crypto_init().
1895 * It is implementation-dependent whether a failure to initialize
1896 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001897 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001898psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001899 uint8_t *data,
1900 size_t data_size,
1901 size_t *data_length);
1902
1903/**@}*/
1904
1905/** \defgroup policy Key policies
1906 * @{
1907 */
1908
1909/** \brief Encoding of permitted usage on a key. */
1910typedef uint32_t psa_key_usage_t;
1911
Gilles Peskine7e198532018-03-08 07:50:30 +01001912/** Whether the key may be exported.
1913 *
1914 * A public key or the public part of a key pair may always be exported
1915 * regardless of the value of this permission flag.
1916 *
1917 * If a key does not have export permission, implementations shall not
1918 * allow the key to be exported in plain form from the cryptoprocessor,
1919 * whether through psa_export_key() or through a proprietary interface.
1920 * The key may however be exportable in a wrapped form, i.e. in a form
1921 * where it is encrypted by another key.
1922 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001923#define PSA_KEY_USAGE_EXPORT ((psa_key_usage_t)0x00000001)
1924
Gilles Peskine7e198532018-03-08 07:50:30 +01001925/** Whether the key may be used to encrypt a message.
1926 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001927 * This flag allows the key to be used for a symmetric encryption operation,
1928 * for an AEAD encryption-and-authentication operation,
1929 * or for an asymmetric encryption operation,
1930 * if otherwise permitted by the key's type and policy.
1931 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001932 * For a key pair, this concerns the public key.
1933 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001934#define PSA_KEY_USAGE_ENCRYPT ((psa_key_usage_t)0x00000100)
Gilles Peskine7e198532018-03-08 07:50:30 +01001935
1936/** Whether the key may be used to decrypt a message.
1937 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001938 * This flag allows the key to be used for a symmetric decryption operation,
1939 * for an AEAD decryption-and-verification operation,
1940 * or for an asymmetric decryption operation,
1941 * if otherwise permitted by the key's type and policy.
1942 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001943 * For a key pair, this concerns the private key.
1944 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001945#define PSA_KEY_USAGE_DECRYPT ((psa_key_usage_t)0x00000200)
Gilles Peskine7e198532018-03-08 07:50:30 +01001946
1947/** Whether the key may be used to sign a message.
1948 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001949 * This flag allows the key to be used for a MAC calculation operation
1950 * or for an asymmetric signature operation,
1951 * if otherwise permitted by the key's type and policy.
1952 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001953 * For a key pair, this concerns the private key.
1954 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001955#define PSA_KEY_USAGE_SIGN ((psa_key_usage_t)0x00000400)
Gilles Peskine7e198532018-03-08 07:50:30 +01001956
1957/** Whether the key may be used to verify a message signature.
1958 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001959 * This flag allows the key to be used for a MAC verification operation
1960 * or for an asymmetric signature verification operation,
1961 * if otherwise permitted by by the key's type and policy.
1962 *
Gilles Peskine7e198532018-03-08 07:50:30 +01001963 * For a key pair, this concerns the public key.
1964 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001965#define PSA_KEY_USAGE_VERIFY ((psa_key_usage_t)0x00000800)
1966
Gilles Peskineea0fb492018-07-12 17:17:20 +02001967/** Whether the key may be used to derive other keys.
1968 */
1969#define PSA_KEY_USAGE_DERIVE ((psa_key_usage_t)0x00001000)
1970
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001971/** The type of the key policy data structure.
1972 *
1973 * This is an implementation-defined \c struct. Applications should not
1974 * make any assumptions about the content of this structure except
1975 * as directed by the documentation of a specific implementation. */
1976typedef struct psa_key_policy_s psa_key_policy_t;
1977
1978/** \brief Initialize a key policy structure to a default that forbids all
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001979 * usage of the key.
1980 *
1981 * \param[out] policy The policy object to initialize.
1982 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001983void psa_key_policy_init(psa_key_policy_t *policy);
1984
Gilles Peskine7e198532018-03-08 07:50:30 +01001985/** \brief Set the standard fields of a policy structure.
1986 *
1987 * Note that this function does not make any consistency check of the
1988 * parameters. The values are only checked when applying the policy to
1989 * a key slot with psa_set_key_policy().
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001990 *
1991 * \param[out] policy The policy object to modify.
1992 * \param usage The permitted uses for the key.
1993 * \param alg The algorithm that the key may be used for.
Gilles Peskine7e198532018-03-08 07:50:30 +01001994 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +01001995void psa_key_policy_set_usage(psa_key_policy_t *policy,
1996 psa_key_usage_t usage,
1997 psa_algorithm_t alg);
1998
Gilles Peskine6ac73a92018-07-12 19:47:19 +02001999/** \brief Retrieve the usage field of a policy structure.
2000 *
2001 * \param[in] policy The policy object to query.
2002 *
2003 * \return The permitted uses for a key with this policy.
2004 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002005psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +01002006
Gilles Peskine6ac73a92018-07-12 19:47:19 +02002007/** \brief Retrieve the algorithm field of a policy structure.
2008 *
2009 * \param[in] policy The policy object to query.
2010 *
2011 * \return The permitted algorithm for a key with this policy.
2012 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002013psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +01002014
2015/** \brief Set the usage policy on a key slot.
2016 *
2017 * This function must be called on an empty key slot, before importing,
2018 * generating or creating a key in the slot. Changing the policy of an
2019 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +01002020 *
2021 * Implementations may set restrictions on supported key policies
2022 * depending on the key type and the key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +02002023 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002024 * \param handle Handle to the key whose policy is to be changed.
Gilles Peskine6ac73a92018-07-12 19:47:19 +02002025 * \param[in] policy The policy object to query.
2026 *
2027 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01002028 * Success.
2029 * If the key is persistent, it is implementation-defined whether
2030 * the policy has been saved to persistent storage. Implementations
2031 * may defer saving the policy until the key material is created.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002032 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +02002033 * \retval #PSA_ERROR_OCCUPIED_SLOT
2034 * \retval #PSA_ERROR_NOT_SUPPORTED
2035 * \retval #PSA_ERROR_INVALID_ARGUMENT
2036 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2037 * \retval #PSA_ERROR_HARDWARE_FAILURE
2038 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002039 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002040 * The library has not been previously initialized by psa_crypto_init().
2041 * It is implementation-dependent whether a failure to initialize
2042 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +01002043 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002044psa_status_t psa_set_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +01002045 const psa_key_policy_t *policy);
2046
Gilles Peskine7e198532018-03-08 07:50:30 +01002047/** \brief Get the usage policy for a key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +02002048 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002049 * \param handle Handle to the key slot whose policy is being queried.
Gilles Peskine6ac73a92018-07-12 19:47:19 +02002050 * \param[out] policy On success, the key's policy.
2051 *
2052 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +01002053 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +02002054 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2055 * \retval #PSA_ERROR_HARDWARE_FAILURE
2056 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002057 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002058 * The library has not been previously initialized by psa_crypto_init().
2059 * It is implementation-dependent whether a failure to initialize
2060 * results in this error code.
Gilles Peskine7e198532018-03-08 07:50:30 +01002061 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002062psa_status_t psa_get_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +01002063 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +01002064
2065/**@}*/
2066
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002067/** \defgroup hash Message digests
2068 * @{
2069 */
2070
Gilles Peskine308b91d2018-02-08 09:47:44 +01002071/** The type of the state data structure for multipart hash operations.
2072 *
Gilles Peskine92b30732018-03-03 21:29:30 +01002073 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +01002074 * make any assumptions about the content of this structure except
2075 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002076typedef struct psa_hash_operation_s psa_hash_operation_t;
2077
Gilles Peskine308b91d2018-02-08 09:47:44 +01002078/** The size of the output of psa_hash_finish(), in bytes.
2079 *
2080 * This is also the hash size that psa_hash_verify() expects.
2081 *
2082 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002083 * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm
Gilles Peskinebe42f312018-07-13 14:38:15 +02002084 * (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a
Gilles Peskine35855962018-04-19 08:39:16 +02002085 * hash algorithm).
Gilles Peskine308b91d2018-02-08 09:47:44 +01002086 *
2087 * \return The hash size for the specified hash algorithm.
2088 * If the hash algorithm is not recognized, return 0.
2089 * An implementation may return either 0 or the correct size
2090 * for a hash algorithm that it recognizes, but does not support.
2091 */
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002092#define PSA_HASH_SIZE(alg) \
2093 ( \
Gilles Peskine00709fa2018-08-22 18:25:41 +02002094 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD2 ? 16 : \
2095 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD4 ? 16 : \
2096 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 16 : \
2097 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
2098 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
2099 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
2100 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
2101 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
2102 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
2103 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
2104 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
2105 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
2106 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
2107 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
2108 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002109 0)
2110
Gilles Peskine308b91d2018-02-08 09:47:44 +01002111/** Start a multipart hash operation.
2112 *
2113 * The sequence of operations to calculate a hash (message digest)
2114 * is as follows:
2115 * -# Allocate an operation object which will be passed to all the functions
2116 * listed here.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002117 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002118 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +01002119 * of the message each time. The hash that is calculated is the hash
2120 * of the concatenation of these messages in order.
2121 * -# To calculate the hash, call psa_hash_finish().
2122 * To compare the hash with an expected value, call psa_hash_verify().
2123 *
2124 * The application may call psa_hash_abort() at any time after the operation
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002125 * has been initialized with psa_hash_setup().
Gilles Peskine308b91d2018-02-08 09:47:44 +01002126 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002127 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01002128 * eventually terminate the operation. The following events terminate an
2129 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +01002130 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +01002131 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +01002132 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002133 * \param[out] operation The operation object to use.
2134 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
2135 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +01002136 *
Gilles Peskine28538492018-07-11 17:34:00 +02002137 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002138 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002139 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002140 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002141 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2142 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2143 * \retval #PSA_ERROR_HARDWARE_FAILURE
2144 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01002145 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002146psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002147 psa_algorithm_t alg);
2148
Gilles Peskine308b91d2018-02-08 09:47:44 +01002149/** Add a message fragment to a multipart hash operation.
2150 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002151 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002152 *
2153 * If this function returns an error status, the operation becomes inactive.
2154 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002155 * \param[in,out] operation Active hash operation.
2156 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002157 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002158 *
Gilles Peskine28538492018-07-11 17:34:00 +02002159 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002160 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002161 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002162 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02002163 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2164 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2165 * \retval #PSA_ERROR_HARDWARE_FAILURE
2166 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01002167 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002168psa_status_t psa_hash_update(psa_hash_operation_t *operation,
2169 const uint8_t *input,
2170 size_t input_length);
2171
Gilles Peskine308b91d2018-02-08 09:47:44 +01002172/** Finish the calculation of the hash of a message.
2173 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002174 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002175 * This function calculates the hash of the message formed by concatenating
2176 * the inputs passed to preceding calls to psa_hash_update().
2177 *
2178 * When this function returns, the operation becomes inactive.
2179 *
2180 * \warning Applications should not call this function if they expect
2181 * a specific value for the hash. Call psa_hash_verify() instead.
2182 * Beware that comparing integrity or authenticity data such as
2183 * hash values with a function such as \c memcmp is risky
2184 * because the time taken by the comparison may leak information
2185 * about the hashed data which could allow an attacker to guess
2186 * a valid hash and thereby bypass security controls.
2187 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002188 * \param[in,out] operation Active hash operation.
2189 * \param[out] hash Buffer where the hash is to be written.
2190 * \param hash_size Size of the \p hash buffer in bytes.
2191 * \param[out] hash_length On success, the number of bytes
2192 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02002193 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02002194 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002195 *
Gilles Peskine28538492018-07-11 17:34:00 +02002196 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002197 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02002198 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002199 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02002200 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002201 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002202 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002203 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02002204 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2205 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2206 * \retval #PSA_ERROR_HARDWARE_FAILURE
2207 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01002208 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002209psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
2210 uint8_t *hash,
2211 size_t hash_size,
2212 size_t *hash_length);
2213
Gilles Peskine308b91d2018-02-08 09:47:44 +01002214/** Finish the calculation of the hash of a message and compare it with
2215 * an expected value.
2216 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002217 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002218 * This function calculates the hash of the message formed by concatenating
2219 * the inputs passed to preceding calls to psa_hash_update(). It then
2220 * compares the calculated hash with the expected hash passed as a
2221 * parameter to this function.
2222 *
2223 * When this function returns, the operation becomes inactive.
2224 *
Gilles Peskine19067982018-03-20 17:54:53 +01002225 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01002226 * comparison between the actual hash and the expected hash is performed
2227 * in constant time.
2228 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002229 * \param[in,out] operation Active hash operation.
2230 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002231 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002232 *
Gilles Peskine28538492018-07-11 17:34:00 +02002233 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002234 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02002235 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002236 * The hash of the message was calculated successfully, but it
2237 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02002238 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002239 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02002240 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2241 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2242 * \retval #PSA_ERROR_HARDWARE_FAILURE
2243 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01002244 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002245psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
2246 const uint8_t *hash,
2247 size_t hash_length);
2248
Gilles Peskine308b91d2018-02-08 09:47:44 +01002249/** Abort a hash operation.
2250 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01002251 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002252 * \p operation structure itself. Once aborted, the operation object
2253 * can be reused for another operation by calling
2254 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002255 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002256 * You may call this function any time after the operation object has
2257 * been initialized by any of the following methods:
2258 * - A call to psa_hash_setup(), whether it succeeds or not.
2259 * - Initializing the \c struct to all-bits-zero.
2260 * - Initializing the \c struct to logical zeros, e.g.
2261 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002262 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002263 * In particular, calling psa_hash_abort() after the operation has been
2264 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
2265 * psa_hash_verify() is safe and has no effect.
2266 *
2267 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002268 *
Gilles Peskine28538492018-07-11 17:34:00 +02002269 * \retval #PSA_SUCCESS
2270 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002271 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02002272 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2273 * \retval #PSA_ERROR_HARDWARE_FAILURE
2274 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01002275 */
2276psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002277
2278/**@}*/
2279
Gilles Peskine8c9def32018-02-08 10:02:12 +01002280/** \defgroup MAC Message authentication codes
2281 * @{
2282 */
2283
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002284/** The type of the state data structure for multipart MAC operations.
2285 *
Gilles Peskine92b30732018-03-03 21:29:30 +01002286 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002287 * make any assumptions about the content of this structure except
2288 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002289typedef struct psa_mac_operation_s psa_mac_operation_t;
2290
Gilles Peskine89167cb2018-07-08 20:12:23 +02002291/** Start a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002292 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02002293 * This function sets up the calculation of the MAC
2294 * (message authentication code) of a byte string.
2295 * To verify the MAC of a message against an
2296 * expected value, use psa_mac_verify_setup() instead.
2297 *
2298 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002299 * -# Allocate an operation object which will be passed to all the functions
2300 * listed here.
Gilles Peskine89167cb2018-07-08 20:12:23 +02002301 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002302 * The key remains associated with the operation even if the content
2303 * of the key slot changes.
2304 * -# Call psa_mac_update() zero, one or more times, passing a fragment
2305 * of the message each time. The MAC that is calculated is the MAC
2306 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02002307 * -# At the end of the message, call psa_mac_sign_finish() to finish
2308 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002309 *
2310 * The application may call psa_mac_abort() at any time after the operation
Gilles Peskine89167cb2018-07-08 20:12:23 +02002311 * has been initialized with psa_mac_sign_setup().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002312 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02002313 * After a successful call to psa_mac_sign_setup(), the application must
2314 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002315 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02002316 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002317 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002318 * \param[out] operation The operation object to use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002319 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002320 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
2321 * such that #PSA_ALG_IS_MAC(alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002322 *
Gilles Peskine28538492018-07-11 17:34:00 +02002323 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002324 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002325 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002326 * \retval #PSA_ERROR_EMPTY_SLOT
2327 * \retval #PSA_ERROR_NOT_PERMITTED
2328 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002329 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002330 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002331 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002332 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2333 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2334 * \retval #PSA_ERROR_HARDWARE_FAILURE
2335 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002336 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002337 * The library has not been previously initialized by psa_crypto_init().
2338 * It is implementation-dependent whether a failure to initialize
2339 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01002340 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02002341psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002342 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002343 psa_algorithm_t alg);
2344
2345/** Start a multipart MAC verification operation.
2346 *
2347 * This function sets up the verification of the MAC
2348 * (message authentication code) of a byte string against an expected value.
2349 *
2350 * The sequence of operations to verify a MAC is as follows:
2351 * -# Allocate an operation object which will be passed to all the functions
2352 * listed here.
2353 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
2354 * The key remains associated with the operation even if the content
2355 * of the key slot changes.
2356 * -# Call psa_mac_update() zero, one or more times, passing a fragment
2357 * of the message each time. The MAC that is calculated is the MAC
2358 * of the concatenation of these messages in order.
2359 * -# At the end of the message, call psa_mac_verify_finish() to finish
2360 * calculating the actual MAC of the message and verify it against
2361 * the expected value.
2362 *
2363 * The application may call psa_mac_abort() at any time after the operation
2364 * has been initialized with psa_mac_verify_setup().
2365 *
2366 * After a successful call to psa_mac_verify_setup(), the application must
2367 * eventually terminate the operation through one of the following methods:
2368 * - A failed call to psa_mac_update().
2369 * - A call to psa_mac_verify_finish() or psa_mac_abort().
2370 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002371 * \param[out] operation The operation object to use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002372 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002373 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
2374 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02002375 *
Gilles Peskine28538492018-07-11 17:34:00 +02002376 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02002377 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002378 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002379 * \retval #PSA_ERROR_EMPTY_SLOT
2380 * \retval #PSA_ERROR_NOT_PERMITTED
2381 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02002382 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002383 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02002384 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002385 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2386 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2387 * \retval #PSA_ERROR_HARDWARE_FAILURE
2388 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002389 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002390 * The library has not been previously initialized by psa_crypto_init().
2391 * It is implementation-dependent whether a failure to initialize
2392 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02002393 */
2394psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002395 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002396 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002397
Gilles Peskinedcd14942018-07-12 00:30:52 +02002398/** Add a message fragment to a multipart MAC operation.
2399 *
2400 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
2401 * before calling this function.
2402 *
2403 * If this function returns an error status, the operation becomes inactive.
2404 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002405 * \param[in,out] operation Active MAC operation.
2406 * \param[in] input Buffer containing the message fragment to add to
2407 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002408 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002409 *
2410 * \retval #PSA_SUCCESS
2411 * Success.
2412 * \retval #PSA_ERROR_BAD_STATE
2413 * The operation state is not valid (not started, or already completed).
2414 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2415 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2416 * \retval #PSA_ERROR_HARDWARE_FAILURE
2417 * \retval #PSA_ERROR_TAMPERING_DETECTED
2418 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002419psa_status_t psa_mac_update(psa_mac_operation_t *operation,
2420 const uint8_t *input,
2421 size_t input_length);
2422
Gilles Peskinedcd14942018-07-12 00:30:52 +02002423/** Finish the calculation of the MAC of a message.
2424 *
2425 * The application must call psa_mac_sign_setup() before calling this function.
2426 * This function calculates the MAC of the message formed by concatenating
2427 * the inputs passed to preceding calls to psa_mac_update().
2428 *
2429 * When this function returns, the operation becomes inactive.
2430 *
2431 * \warning Applications should not call this function if they expect
2432 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
2433 * Beware that comparing integrity or authenticity data such as
2434 * MAC values with a function such as \c memcmp is risky
2435 * because the time taken by the comparison may leak information
2436 * about the MAC value which could allow an attacker to guess
2437 * a valid MAC and thereby bypass security controls.
2438 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002439 * \param[in,out] operation Active MAC operation.
2440 * \param[out] mac Buffer where the MAC value is to be written.
2441 * \param mac_size Size of the \p mac buffer in bytes.
2442 * \param[out] mac_length On success, the number of bytes
2443 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002444 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02002445 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002446 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02002447 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002448 *
2449 * \retval #PSA_SUCCESS
2450 * Success.
2451 * \retval #PSA_ERROR_BAD_STATE
2452 * The operation state is not valid (not started, or already completed).
2453 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002454 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02002455 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
2456 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2457 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2458 * \retval #PSA_ERROR_HARDWARE_FAILURE
2459 * \retval #PSA_ERROR_TAMPERING_DETECTED
2460 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02002461psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
2462 uint8_t *mac,
2463 size_t mac_size,
2464 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002465
Gilles Peskinedcd14942018-07-12 00:30:52 +02002466/** Finish the calculation of the MAC of a message and compare it with
2467 * an expected value.
2468 *
2469 * The application must call psa_mac_verify_setup() before calling this function.
2470 * This function calculates the MAC of the message formed by concatenating
2471 * the inputs passed to preceding calls to psa_mac_update(). It then
2472 * compares the calculated MAC with the expected MAC passed as a
2473 * parameter to this function.
2474 *
2475 * When this function returns, the operation becomes inactive.
2476 *
2477 * \note Implementations shall make the best effort to ensure that the
2478 * comparison between the actual MAC and the expected MAC is performed
2479 * in constant time.
2480 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002481 * \param[in,out] operation Active MAC operation.
2482 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002483 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002484 *
2485 * \retval #PSA_SUCCESS
2486 * The expected MAC is identical to the actual MAC of the message.
2487 * \retval #PSA_ERROR_INVALID_SIGNATURE
2488 * The MAC of the message was calculated successfully, but it
2489 * differs from the expected MAC.
2490 * \retval #PSA_ERROR_BAD_STATE
2491 * The operation state is not valid (not started, or already completed).
2492 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2493 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2494 * \retval #PSA_ERROR_HARDWARE_FAILURE
2495 * \retval #PSA_ERROR_TAMPERING_DETECTED
2496 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02002497psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
2498 const uint8_t *mac,
2499 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01002500
Gilles Peskinedcd14942018-07-12 00:30:52 +02002501/** Abort a MAC operation.
2502 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002503 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002504 * \p operation structure itself. Once aborted, the operation object
2505 * can be reused for another operation by calling
2506 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002507 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002508 * You may call this function any time after the operation object has
2509 * been initialized by any of the following methods:
2510 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
2511 * it succeeds or not.
2512 * - Initializing the \c struct to all-bits-zero.
2513 * - Initializing the \c struct to logical zeros, e.g.
2514 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002515 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002516 * In particular, calling psa_mac_abort() after the operation has been
2517 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
2518 * psa_mac_verify_finish() is safe and has no effect.
2519 *
2520 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002521 *
2522 * \retval #PSA_SUCCESS
2523 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002524 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002525 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2526 * \retval #PSA_ERROR_HARDWARE_FAILURE
2527 * \retval #PSA_ERROR_TAMPERING_DETECTED
2528 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002529psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
2530
2531/**@}*/
2532
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002533/** \defgroup cipher Symmetric ciphers
2534 * @{
2535 */
2536
2537/** The type of the state data structure for multipart cipher operations.
2538 *
2539 * This is an implementation-defined \c struct. Applications should not
2540 * make any assumptions about the content of this structure except
2541 * as directed by the documentation of a specific implementation. */
2542typedef struct psa_cipher_operation_s psa_cipher_operation_t;
2543
2544/** Set the key for a multipart symmetric encryption operation.
2545 *
2546 * The sequence of operations to encrypt a message with a symmetric cipher
2547 * is as follows:
2548 * -# Allocate an operation object which will be passed to all the functions
2549 * listed here.
Gilles Peskinefe119512018-07-08 21:39:34 +02002550 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002551 * The key remains associated with the operation even if the content
2552 * of the key slot changes.
itayzafrired7382f2018-08-02 14:19:33 +03002553 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002554 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03002555 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002556 * requires a specific IV value.
2557 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
2558 * of the message each time.
2559 * -# Call psa_cipher_finish().
2560 *
2561 * The application may call psa_cipher_abort() at any time after the operation
Gilles Peskinefe119512018-07-08 21:39:34 +02002562 * has been initialized with psa_cipher_encrypt_setup().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002563 *
Gilles Peskinefe119512018-07-08 21:39:34 +02002564 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01002565 * eventually terminate the operation. The following events terminate an
2566 * operation:
itayzafrired7382f2018-08-02 14:19:33 +03002567 * - A failed call to psa_cipher_generate_iv(), psa_cipher_set_iv()
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002568 * or psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01002569 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002570 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002571 * \param[out] operation The operation object to use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002572 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002573 * \param alg The cipher algorithm to compute
2574 * (\c PSA_ALG_XXX value such that
2575 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002576 *
Gilles Peskine28538492018-07-11 17:34:00 +02002577 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002578 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002579 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002580 * \retval #PSA_ERROR_EMPTY_SLOT
2581 * \retval #PSA_ERROR_NOT_PERMITTED
2582 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002583 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002584 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002585 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002586 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2587 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2588 * \retval #PSA_ERROR_HARDWARE_FAILURE
2589 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002590 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002591 * The library has not been previously initialized by psa_crypto_init().
2592 * It is implementation-dependent whether a failure to initialize
2593 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002594 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002595psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002596 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002597 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002598
2599/** Set the key for a multipart symmetric decryption operation.
2600 *
2601 * The sequence of operations to decrypt a message with a symmetric cipher
2602 * is as follows:
2603 * -# Allocate an operation object which will be passed to all the functions
2604 * listed here.
Gilles Peskinefe119512018-07-08 21:39:34 +02002605 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002606 * The key remains associated with the operation even if the content
2607 * of the key slot changes.
2608 * -# Call psa_cipher_update() with the IV (initialization vector) for the
2609 * decryption. If the IV is prepended to the ciphertext, you can call
2610 * psa_cipher_update() on a buffer containing the IV followed by the
2611 * beginning of the message.
2612 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
2613 * of the message each time.
2614 * -# Call psa_cipher_finish().
2615 *
2616 * The application may call psa_cipher_abort() at any time after the operation
Gilles Peskinefe119512018-07-08 21:39:34 +02002617 * has been initialized with psa_cipher_decrypt_setup().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002618 *
Gilles Peskinefe119512018-07-08 21:39:34 +02002619 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01002620 * eventually terminate the operation. The following events terminate an
2621 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002622 * - A failed call to psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01002623 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002624 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002625 * \param[out] operation The operation object to use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002626 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002627 * \param alg The cipher algorithm to compute
2628 * (\c PSA_ALG_XXX value such that
2629 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002630 *
Gilles Peskine28538492018-07-11 17:34:00 +02002631 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002632 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002633 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002634 * \retval #PSA_ERROR_EMPTY_SLOT
2635 * \retval #PSA_ERROR_NOT_PERMITTED
2636 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002637 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002638 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002639 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002640 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2641 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2642 * \retval #PSA_ERROR_HARDWARE_FAILURE
2643 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002644 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002645 * The library has not been previously initialized by psa_crypto_init().
2646 * It is implementation-dependent whether a failure to initialize
2647 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002648 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002649psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002650 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002651 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002652
Gilles Peskinedcd14942018-07-12 00:30:52 +02002653/** Generate an IV for a symmetric encryption operation.
2654 *
2655 * This function generates a random IV (initialization vector), nonce
2656 * or initial counter value for the encryption operation as appropriate
2657 * for the chosen algorithm, key type and key size.
2658 *
2659 * The application must call psa_cipher_encrypt_setup() before
2660 * calling this function.
2661 *
2662 * If this function returns an error status, the operation becomes inactive.
2663 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002664 * \param[in,out] operation Active cipher operation.
2665 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002666 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002667 * \param[out] iv_length On success, the number of bytes of the
2668 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002669 *
2670 * \retval #PSA_SUCCESS
2671 * Success.
2672 * \retval #PSA_ERROR_BAD_STATE
2673 * The operation state is not valid (not started, or IV already set).
2674 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002675 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002676 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2677 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2678 * \retval #PSA_ERROR_HARDWARE_FAILURE
2679 * \retval #PSA_ERROR_TAMPERING_DETECTED
2680 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002681psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
2682 unsigned char *iv,
2683 size_t iv_size,
2684 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002685
Gilles Peskinedcd14942018-07-12 00:30:52 +02002686/** Set the IV for a symmetric encryption or decryption operation.
2687 *
2688 * This function sets the random IV (initialization vector), nonce
2689 * or initial counter value for the encryption or decryption operation.
2690 *
2691 * The application must call psa_cipher_encrypt_setup() before
2692 * calling this function.
2693 *
2694 * If this function returns an error status, the operation becomes inactive.
2695 *
2696 * \note When encrypting, applications should use psa_cipher_generate_iv()
2697 * instead of this function, unless implementing a protocol that requires
2698 * a non-random IV.
2699 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002700 * \param[in,out] operation Active cipher operation.
2701 * \param[in] iv Buffer containing the IV to use.
2702 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002703 *
2704 * \retval #PSA_SUCCESS
2705 * Success.
2706 * \retval #PSA_ERROR_BAD_STATE
2707 * The operation state is not valid (not started, or IV already set).
2708 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002709 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02002710 * or the chosen algorithm does not use an IV.
2711 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2712 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2713 * \retval #PSA_ERROR_HARDWARE_FAILURE
2714 * \retval #PSA_ERROR_TAMPERING_DETECTED
2715 */
Gilles Peskinefe119512018-07-08 21:39:34 +02002716psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
2717 const unsigned char *iv,
2718 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002719
Gilles Peskinedcd14942018-07-12 00:30:52 +02002720/** Encrypt or decrypt a message fragment in an active cipher operation.
2721 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02002722 * Before calling this function, you must:
2723 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
2724 * The choice of setup function determines whether this function
2725 * encrypts or decrypts its input.
2726 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
2727 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02002728 *
2729 * If this function returns an error status, the operation becomes inactive.
2730 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002731 * \param[in,out] operation Active cipher operation.
2732 * \param[in] input Buffer containing the message fragment to
2733 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002734 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002735 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002736 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002737 * \param[out] output_length On success, the number of bytes
2738 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002739 *
2740 * \retval #PSA_SUCCESS
2741 * Success.
2742 * \retval #PSA_ERROR_BAD_STATE
2743 * The operation state is not valid (not started, IV required but
2744 * not set, or already completed).
2745 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2746 * The size of the \p output buffer is too small.
2747 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2748 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2749 * \retval #PSA_ERROR_HARDWARE_FAILURE
2750 * \retval #PSA_ERROR_TAMPERING_DETECTED
2751 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002752psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
2753 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02002754 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02002755 unsigned char *output,
2756 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002757 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002758
Gilles Peskinedcd14942018-07-12 00:30:52 +02002759/** Finish encrypting or decrypting a message in a cipher operation.
2760 *
2761 * The application must call psa_cipher_encrypt_setup() or
2762 * psa_cipher_decrypt_setup() before calling this function. The choice
2763 * of setup function determines whether this function encrypts or
2764 * decrypts its input.
2765 *
2766 * This function finishes the encryption or decryption of the message
2767 * formed by concatenating the inputs passed to preceding calls to
2768 * psa_cipher_update().
2769 *
2770 * When this function returns, the operation becomes inactive.
2771 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002772 * \param[in,out] operation Active cipher operation.
2773 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002774 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002775 * \param[out] output_length On success, the number of bytes
2776 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002777 *
2778 * \retval #PSA_SUCCESS
2779 * Success.
2780 * \retval #PSA_ERROR_BAD_STATE
2781 * The operation state is not valid (not started, IV required but
2782 * not set, or already completed).
2783 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2784 * The size of the \p output buffer is too small.
2785 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2786 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2787 * \retval #PSA_ERROR_HARDWARE_FAILURE
2788 * \retval #PSA_ERROR_TAMPERING_DETECTED
2789 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002790psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02002791 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03002792 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02002793 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002794
Gilles Peskinedcd14942018-07-12 00:30:52 +02002795/** Abort a cipher operation.
2796 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02002797 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002798 * \p operation structure itself. Once aborted, the operation object
2799 * can be reused for another operation by calling
2800 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002801 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002802 * You may call this function any time after the operation object has
2803 * been initialized by any of the following methods:
2804 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
2805 * whether it succeeds or not.
2806 * - Initializing the \c struct to all-bits-zero.
2807 * - Initializing the \c struct to logical zeros, e.g.
2808 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002809 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02002810 * In particular, calling psa_cipher_abort() after the operation has been
2811 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
2812 * is safe and has no effect.
2813 *
2814 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002815 *
2816 * \retval #PSA_SUCCESS
2817 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002818 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02002819 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2820 * \retval #PSA_ERROR_HARDWARE_FAILURE
2821 * \retval #PSA_ERROR_TAMPERING_DETECTED
2822 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01002823psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
2824
2825/**@}*/
2826
Gilles Peskine3b555712018-03-03 21:27:57 +01002827/** \defgroup aead Authenticated encryption with associated data (AEAD)
2828 * @{
2829 */
2830
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002831/** The tag size for an AEAD algorithm, in bytes.
Gilles Peskine3b555712018-03-03 21:27:57 +01002832 *
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002833 * \param alg An AEAD algorithm
2834 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002835 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002836 *
2837 * \return The tag size for the specified algorithm.
2838 * If the AEAD algorithm does not have an identified
2839 * tag that can be distinguished from the rest of
2840 * the ciphertext, return 0.
2841 * If the AEAD algorithm is not recognized, return 0.
2842 * An implementation may return either 0 or a
2843 * correct size for an AEAD algorithm that it
2844 * recognizes, but does not support.
2845 */
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002846#define PSA_AEAD_TAG_LENGTH(alg) \
2847 (PSA_ALG_IS_AEAD(alg) ? \
2848 (((alg) & PSA_ALG_AEAD_TAG_LENGTH_MASK) >> PSA_AEAD_TAG_LENGTH_OFFSET) : \
Gilles Peskine5e39dc92018-06-08 11:41:57 +02002849 0)
Gilles Peskine3b555712018-03-03 21:27:57 +01002850
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002851/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002852 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002853 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002854 * \param alg The AEAD algorithm to compute
2855 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002856 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002857 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002858 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002859 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002860 * but not encrypted.
2861 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002862 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002863 * encrypted.
2864 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002865 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002866 * encrypted data. The additional data is not
2867 * part of this output. For algorithms where the
2868 * encrypted data and the authentication tag
2869 * are defined as separate outputs, the
2870 * authentication tag is appended to the
2871 * encrypted data.
2872 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2873 * This must be at least
2874 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2875 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002876 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002877 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002878 *
Gilles Peskine28538492018-07-11 17:34:00 +02002879 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002880 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002881 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002882 * \retval #PSA_ERROR_EMPTY_SLOT
2883 * \retval #PSA_ERROR_NOT_PERMITTED
2884 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002885 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002886 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002887 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002888 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2889 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2890 * \retval #PSA_ERROR_HARDWARE_FAILURE
2891 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002892 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002893 * The library has not been previously initialized by psa_crypto_init().
2894 * It is implementation-dependent whether a failure to initialize
2895 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002896 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002897psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002898 psa_algorithm_t alg,
2899 const uint8_t *nonce,
2900 size_t nonce_length,
2901 const uint8_t *additional_data,
2902 size_t additional_data_length,
2903 const uint8_t *plaintext,
2904 size_t plaintext_length,
2905 uint8_t *ciphertext,
2906 size_t ciphertext_size,
2907 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002908
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002909/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002910 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002911 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002912 * \param alg The AEAD algorithm to compute
2913 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002914 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002915 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002916 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002917 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002918 * but not encrypted.
2919 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002920 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002921 * encrypted. For algorithms where the
2922 * encrypted data and the authentication tag
2923 * are defined as separate inputs, the buffer
2924 * must contain the encrypted data followed
2925 * by the authentication tag.
2926 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002927 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002928 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2929 * This must be at least
2930 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2931 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002932 * \param[out] plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03002933 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002934 *
Gilles Peskine28538492018-07-11 17:34:00 +02002935 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002936 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002937 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02002938 * \retval #PSA_ERROR_EMPTY_SLOT
2939 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002940 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002941 * \retval #PSA_ERROR_NOT_PERMITTED
2942 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002943 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002944 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002945 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002946 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2947 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2948 * \retval #PSA_ERROR_HARDWARE_FAILURE
2949 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002950 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002951 * The library has not been previously initialized by psa_crypto_init().
2952 * It is implementation-dependent whether a failure to initialize
2953 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002954 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002955psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002956 psa_algorithm_t alg,
2957 const uint8_t *nonce,
2958 size_t nonce_length,
2959 const uint8_t *additional_data,
2960 size_t additional_data_length,
2961 const uint8_t *ciphertext,
2962 size_t ciphertext_length,
2963 uint8_t *plaintext,
2964 size_t plaintext_size,
2965 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002966
2967/**@}*/
2968
Gilles Peskine20035e32018-02-03 22:44:14 +01002969/** \defgroup asymmetric Asymmetric cryptography
2970 * @{
2971 */
2972
2973/**
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002974 * \brief ECDSA signature size for a given curve bit size
Gilles Peskine0189e752018-02-03 23:57:22 +01002975 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002976 * \param curve_bits Curve size in bits.
2977 * \return Signature size in bytes.
Gilles Peskine0189e752018-02-03 23:57:22 +01002978 *
2979 * \note This macro returns a compile-time constant if its argument is one.
Gilles Peskine0189e752018-02-03 23:57:22 +01002980 */
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002981#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
2982 (PSA_BITS_TO_BYTES(curve_bits) * 2)
Gilles Peskine0189e752018-02-03 23:57:22 +01002983
Gilles Peskine0189e752018-02-03 23:57:22 +01002984/**
Gilles Peskine20035e32018-02-03 22:44:14 +01002985 * \brief Sign a hash or short message with a private key.
2986 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002987 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002988 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002989 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2990 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2991 * to determine the hash algorithm to use.
2992 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002993 * \param handle Handle to the key to use for the operation.
2994 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002995 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002996 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002997 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002998 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002999 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003000 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003001 * \param[out] signature_length On success, the number of bytes
3002 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01003003 *
Gilles Peskine28538492018-07-11 17:34:00 +02003004 * \retval #PSA_SUCCESS
3005 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003006 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01003007 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02003008 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01003009 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003010 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02003011 * \retval #PSA_ERROR_NOT_SUPPORTED
3012 * \retval #PSA_ERROR_INVALID_ARGUMENT
3013 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3014 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3015 * \retval #PSA_ERROR_HARDWARE_FAILURE
3016 * \retval #PSA_ERROR_TAMPERING_DETECTED
3017 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03003018 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003019 * The library has not been previously initialized by psa_crypto_init().
3020 * It is implementation-dependent whether a failure to initialize
3021 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01003022 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01003023psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01003024 psa_algorithm_t alg,
3025 const uint8_t *hash,
3026 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01003027 uint8_t *signature,
3028 size_t signature_size,
3029 size_t *signature_length);
3030
3031/**
3032 * \brief Verify the signature a hash or short message using a public key.
3033 *
Gilles Peskine08bac712018-06-26 16:14:46 +02003034 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02003035 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02003036 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
3037 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
3038 * to determine the hash algorithm to use.
3039 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01003040 * \param handle Handle to the key to use for the operation.
3041 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01003042 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003043 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003044 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02003045 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003046 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003047 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003048 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01003049 *
Gilles Peskine28538492018-07-11 17:34:00 +02003050 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01003051 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02003052 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01003053 * The calculation was perfomed successfully, but the passed
3054 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02003055 * \retval #PSA_ERROR_NOT_SUPPORTED
3056 * \retval #PSA_ERROR_INVALID_ARGUMENT
3057 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3058 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3059 * \retval #PSA_ERROR_HARDWARE_FAILURE
3060 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003061 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003062 * The library has not been previously initialized by psa_crypto_init().
3063 * It is implementation-dependent whether a failure to initialize
3064 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01003065 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01003066psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01003067 psa_algorithm_t alg,
3068 const uint8_t *hash,
3069 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02003070 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02003071 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01003072
Gilles Peskine723feff2018-05-31 20:08:13 +02003073#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
Gilles Peskine072ac562018-06-30 00:21:29 +02003074 (PSA_ALG_IS_RSA_OAEP(alg) ? \
3075 2 * PSA_HASH_FINAL_SIZE(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
Gilles Peskine723feff2018-05-31 20:08:13 +02003076 11 /*PKCS#1v1.5*/)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003077
3078/**
3079 * \brief Encrypt a short message with a public key.
3080 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01003081 * \param handle Handle to the key to use for the operation.
3082 * It must be a public key or an asymmetric
3083 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003084 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003085 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003086 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003087 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003088 * \param[in] salt A salt or label, if supported by the
3089 * encryption algorithm.
3090 * If the algorithm does not support a
3091 * salt, pass \c NULL.
3092 * If the algorithm supports an optional
3093 * salt and you do not want to pass a salt,
3094 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003095 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003096 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3097 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003098 * \param salt_length Size of the \p salt buffer in bytes.
3099 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003100 * \param[out] output Buffer where the encrypted message is to
3101 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003102 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003103 * \param[out] output_length On success, the number of bytes
3104 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003105 *
Gilles Peskine28538492018-07-11 17:34:00 +02003106 * \retval #PSA_SUCCESS
3107 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003108 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003109 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02003110 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003111 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003112 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02003113 * \retval #PSA_ERROR_NOT_SUPPORTED
3114 * \retval #PSA_ERROR_INVALID_ARGUMENT
3115 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3116 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3117 * \retval #PSA_ERROR_HARDWARE_FAILURE
3118 * \retval #PSA_ERROR_TAMPERING_DETECTED
3119 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03003120 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003121 * The library has not been previously initialized by psa_crypto_init().
3122 * It is implementation-dependent whether a failure to initialize
3123 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003124 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01003125psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003126 psa_algorithm_t alg,
3127 const uint8_t *input,
3128 size_t input_length,
3129 const uint8_t *salt,
3130 size_t salt_length,
3131 uint8_t *output,
3132 size_t output_size,
3133 size_t *output_length);
3134
3135/**
3136 * \brief Decrypt a short message with a private key.
3137 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01003138 * \param handle Handle to the key to use for the operation.
3139 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003140 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003141 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003142 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003143 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003144 * \param[in] salt A salt or label, if supported by the
3145 * encryption algorithm.
3146 * If the algorithm does not support a
3147 * salt, pass \c NULL.
3148 * If the algorithm supports an optional
3149 * salt and you do not want to pass a salt,
3150 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003151 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003152 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
3153 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003154 * \param salt_length Size of the \p salt buffer in bytes.
3155 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02003156 * \param[out] output Buffer where the decrypted message is to
3157 * be written.
3158 * \param output_size Size of the \c output buffer in bytes.
3159 * \param[out] output_length On success, the number of bytes
3160 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003161 *
Gilles Peskine28538492018-07-11 17:34:00 +02003162 * \retval #PSA_SUCCESS
3163 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003164 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003165 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003166 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003167 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003168 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02003169 * \retval #PSA_ERROR_NOT_SUPPORTED
3170 * \retval #PSA_ERROR_INVALID_ARGUMENT
3171 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3172 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3173 * \retval #PSA_ERROR_HARDWARE_FAILURE
3174 * \retval #PSA_ERROR_TAMPERING_DETECTED
3175 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3176 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03003177 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003178 * The library has not been previously initialized by psa_crypto_init().
3179 * It is implementation-dependent whether a failure to initialize
3180 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003181 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01003182psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02003183 psa_algorithm_t alg,
3184 const uint8_t *input,
3185 size_t input_length,
3186 const uint8_t *salt,
3187 size_t salt_length,
3188 uint8_t *output,
3189 size_t output_size,
3190 size_t *output_length);
3191
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003192/**@}*/
3193
Gilles Peskineedd76872018-07-20 17:42:05 +02003194/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02003195 * @{
3196 */
3197
3198/** The type of the state data structure for generators.
3199 *
3200 * Before calling any function on a generator, the application must
3201 * initialize it by any of the following means:
3202 * - Set the structure to all-bits-zero, for example:
3203 * \code
3204 * psa_crypto_generator_t generator;
3205 * memset(&generator, 0, sizeof(generator));
3206 * \endcode
3207 * - Initialize the structure to logical zero values, for example:
3208 * \code
3209 * psa_crypto_generator_t generator = {0};
3210 * \endcode
3211 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
3212 * for example:
3213 * \code
3214 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
3215 * \endcode
3216 * - Assign the result of the function psa_crypto_generator_init()
3217 * to the structure, for example:
3218 * \code
3219 * psa_crypto_generator_t generator;
3220 * generator = psa_crypto_generator_init();
3221 * \endcode
3222 *
3223 * This is an implementation-defined \c struct. Applications should not
3224 * make any assumptions about the content of this structure except
3225 * as directed by the documentation of a specific implementation.
3226 */
3227typedef struct psa_crypto_generator_s psa_crypto_generator_t;
3228
3229/** \def PSA_CRYPTO_GENERATOR_INIT
3230 *
3231 * This macro returns a suitable initializer for a generator object
3232 * of type #psa_crypto_generator_t.
3233 */
3234#ifdef __DOXYGEN_ONLY__
3235/* This is an example definition for documentation purposes.
3236 * Implementations should define a suitable value in `crypto_struct.h`.
3237 */
3238#define PSA_CRYPTO_GENERATOR_INIT {0}
3239#endif
3240
3241/** Return an initial value for a generator object.
3242 */
3243static psa_crypto_generator_t psa_crypto_generator_init(void);
3244
3245/** Retrieve the current capacity of a generator.
3246 *
3247 * The capacity of a generator is the maximum number of bytes that it can
3248 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
3249 *
3250 * \param[in] generator The generator to query.
3251 * \param[out] capacity On success, the capacity of the generator.
3252 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003253 * \retval #PSA_SUCCESS
3254 * \retval #PSA_ERROR_BAD_STATE
3255 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02003256 */
3257psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3258 size_t *capacity);
3259
3260/** Read some data from a generator.
3261 *
3262 * This function reads and returns a sequence of bytes from a generator.
3263 * The data that is read is discarded from the generator. The generator's
3264 * capacity is decreased by the number of bytes read.
3265 *
3266 * \param[in,out] generator The generator object to read from.
3267 * \param[out] output Buffer where the generator output will be
3268 * written.
3269 * \param output_length Number of bytes to output.
3270 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003271 * \retval #PSA_SUCCESS
3272 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02003273 * There were fewer than \p output_length bytes
3274 * in the generator. Note that in this case, no
3275 * output is written to the output buffer.
3276 * The generator's capacity is set to 0, thus
3277 * subsequent calls to this function will not
3278 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003279 * \retval #PSA_ERROR_BAD_STATE
3280 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3281 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3282 * \retval #PSA_ERROR_HARDWARE_FAILURE
3283 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003284 */
3285psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
3286 uint8_t *output,
3287 size_t output_length);
3288
3289/** Create a symmetric key from data read from a generator.
3290 *
3291 * This function reads a sequence of bytes from a generator and imports
3292 * these bytes as a key.
3293 * The data that is read is discarded from the generator. The generator's
3294 * capacity is decreased by the number of bytes read.
3295 *
3296 * This function is equivalent to calling #psa_generator_read and
3297 * passing the resulting output to #psa_import_key, but
3298 * if the implementation provides an isolation boundary then
3299 * the key material is not exposed outside the isolation boundary.
3300 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01003301 * \param handle Handle to the slot where the key will be stored.
3302 * This must be a valid slot for a key of the chosen
3303 * type: it must have been obtained by calling
3304 * psa_allocate_key() or psa_create_key() with the
3305 * correct \p type and with a maximum size that is
3306 * compatible with \p bits.
3307 * It must not contain any key material yet.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003308 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
3309 * This must be a symmetric key type.
3310 * \param bits Key size in bits.
3311 * \param[in,out] generator The generator object to read from.
3312 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003313 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003314 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003315 * If the key is persistent, the key material and the key's metadata
3316 * have been saved to persistent storage.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003317 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02003318 * There were fewer than \p output_length bytes
3319 * in the generator. Note that in this case, no
3320 * output is written to the output buffer.
3321 * The generator's capacity is set to 0, thus
3322 * subsequent calls to this function will not
3323 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003324 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003325 * The key type or key size is not supported, either by the
3326 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003327 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineae32aac2018-11-30 14:39:32 +01003328 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003329 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskineeab56e42018-07-12 17:12:33 +02003330 * There is already a key in the specified slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003331 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3332 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3333 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3334 * \retval #PSA_ERROR_HARDWARE_FAILURE
3335 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003336 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003337 * The library has not been previously initialized by psa_crypto_init().
3338 * It is implementation-dependent whether a failure to initialize
3339 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003340 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01003341psa_status_t psa_generator_import_key(psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003342 psa_key_type_t type,
3343 size_t bits,
3344 psa_crypto_generator_t *generator);
3345
3346/** Abort a generator.
3347 *
3348 * Once a generator has been aborted, its capacity is zero.
3349 * Aborting a generator frees all associated resources except for the
3350 * \c generator structure itself.
3351 *
3352 * This function may be called at any time as long as the generator
3353 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
3354 * psa_crypto_generator_init() or a zero value. In particular, it is valid
3355 * to call psa_generator_abort() twice, or to call psa_generator_abort()
3356 * on a generator that has not been set up.
3357 *
3358 * Once aborted, the generator object may be called.
3359 *
3360 * \param[in,out] generator The generator to abort.
3361 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003362 * \retval #PSA_SUCCESS
3363 * \retval #PSA_ERROR_BAD_STATE
3364 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3365 * \retval #PSA_ERROR_HARDWARE_FAILURE
3366 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003367 */
3368psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
3369
Gilles Peskine8feb3a82018-09-18 12:06:11 +02003370/** Use the maximum possible capacity for a generator.
3371 *
3372 * Use this value as the capacity argument when setting up a generator
3373 * to indicate that the generator should have the maximum possible capacity.
3374 * The value of the maximum possible capacity depends on the generator
3375 * algorithm.
3376 */
3377#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
3378
Gilles Peskineeab56e42018-07-12 17:12:33 +02003379/**@}*/
3380
Gilles Peskineea0fb492018-07-12 17:17:20 +02003381/** \defgroup derivation Key derivation
3382 * @{
3383 */
3384
3385/** Set up a key derivation operation.
3386 *
3387 * A key derivation algorithm takes three inputs: a secret input \p key and
3388 * two non-secret inputs \p label and p salt.
3389 * The result of this function is a byte generator which can
3390 * be used to produce keys and other cryptographic material.
3391 *
3392 * The role of \p label and \p salt is as follows:
Gilles Peskinebef7f142018-07-12 17:22:21 +02003393 * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step
3394 * and \p label is the info string used in the "expand" step.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003395 *
3396 * \param[in,out] generator The generator object to set up. It must
Gilles Peskine92587db2018-09-18 12:12:42 +02003397 * have been initialized to all-bits-zero,
3398 * a logical zero (`{0}`),
3399 * \c PSA_CRYPTO_GENERATOR_INIT or
3400 * psa_crypto_generator_init().
Gilles Peskineae32aac2018-11-30 14:39:32 +01003401 * \param handle Handle to the secret key.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003402 * \param alg The key derivation algorithm to compute
3403 * (\c PSA_ALG_XXX value such that
3404 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
3405 * \param[in] salt Salt to use.
3406 * \param salt_length Size of the \p salt buffer in bytes.
3407 * \param[in] label Label to use.
3408 * \param label_length Size of the \p label buffer in bytes.
3409 * \param capacity The maximum number of bytes that the
3410 * generator will be able to provide.
3411 *
3412 * \retval #PSA_SUCCESS
3413 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003414 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineea0fb492018-07-12 17:17:20 +02003415 * \retval #PSA_ERROR_EMPTY_SLOT
3416 * \retval #PSA_ERROR_NOT_PERMITTED
3417 * \retval #PSA_ERROR_INVALID_ARGUMENT
3418 * \c key is not compatible with \c alg,
3419 * or \p capacity is too large for the specified algorithm and key.
3420 * \retval #PSA_ERROR_NOT_SUPPORTED
3421 * \c alg is not supported or is not a key derivation algorithm.
3422 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3423 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3424 * \retval #PSA_ERROR_HARDWARE_FAILURE
3425 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003426 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003427 * The library has not been previously initialized by psa_crypto_init().
3428 * It is implementation-dependent whether a failure to initialize
3429 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003430 */
3431psa_status_t psa_key_derivation(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01003432 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02003433 psa_algorithm_t alg,
3434 const uint8_t *salt,
3435 size_t salt_length,
3436 const uint8_t *label,
3437 size_t label_length,
3438 size_t capacity);
3439
Gilles Peskine01d718c2018-09-18 12:01:02 +02003440/** Set up a key agreement operation.
3441 *
3442 * A key agreement algorithm takes two inputs: a private key \p private_key
3443 * a public key \p peer_key.
3444 * The result of this function is a byte generator which can
3445 * be used to produce keys and other cryptographic material.
3446 *
Gilles Peskine211a4362018-10-25 22:22:31 +02003447 * The resulting generator always has the maximum capacity permitted by
3448 * the algorithm.
3449 *
Gilles Peskine01d718c2018-09-18 12:01:02 +02003450 * \param[in,out] generator The generator object to set up. It must
3451 * have been initialized to all-bits-zero,
3452 * a logical zero (`{0}`),
3453 * \c PSA_CRYPTO_GENERATOR_INIT or
3454 * psa_crypto_generator_init().
Gilles Peskineae32aac2018-11-30 14:39:32 +01003455 * \param private_key Handle to the private key to use.
Gilles Peskined171e782018-11-15 17:46:21 +01003456 * \param[in] peer_key Public key of the peer. It must be
3457 * in the same format that psa_import_key()
3458 * accepts. The standard formats for public
3459 * keys are documented in the documentation
3460 * of psa_export_public_key().
Gilles Peskine01d718c2018-09-18 12:01:02 +02003461 * \param peer_key_length Size of \p peer_key in bytes.
3462 * \param alg The key agreement algorithm to compute
3463 * (\c PSA_ALG_XXX value such that
3464 * #PSA_ALG_IS_KEY_AGREEMENT(\p alg) is true).
3465 *
3466 * \retval #PSA_SUCCESS
3467 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003468 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine01d718c2018-09-18 12:01:02 +02003469 * \retval #PSA_ERROR_EMPTY_SLOT
3470 * \retval #PSA_ERROR_NOT_PERMITTED
3471 * \retval #PSA_ERROR_INVALID_ARGUMENT
3472 * \c private_key is not compatible with \c alg,
3473 * or \p peer_key is not valid for \c alg or not compatible with
3474 * \c private_key.
3475 * \retval #PSA_ERROR_NOT_SUPPORTED
3476 * \c alg is not supported or is not a key derivation algorithm.
3477 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3478 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3479 * \retval #PSA_ERROR_HARDWARE_FAILURE
3480 * \retval #PSA_ERROR_TAMPERING_DETECTED
3481 */
3482psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01003483 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02003484 const uint8_t *peer_key,
3485 size_t peer_key_length,
3486 psa_algorithm_t alg);
3487
Gilles Peskineea0fb492018-07-12 17:17:20 +02003488/**@}*/
3489
Gilles Peskineedd76872018-07-20 17:42:05 +02003490/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003491 * @{
3492 */
3493
3494/**
3495 * \brief Generate random bytes.
3496 *
3497 * \warning This function **can** fail! Callers MUST check the return status
3498 * and MUST NOT use the content of the output buffer if the return
3499 * status is not #PSA_SUCCESS.
3500 *
3501 * \note To generate a key, use psa_generate_key() instead.
3502 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02003503 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003504 * \param output_size Number of bytes to generate and output.
3505 *
Gilles Peskine28538492018-07-11 17:34:00 +02003506 * \retval #PSA_SUCCESS
3507 * \retval #PSA_ERROR_NOT_SUPPORTED
3508 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3509 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3510 * \retval #PSA_ERROR_HARDWARE_FAILURE
3511 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03003512 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003513 * The library has not been previously initialized by psa_crypto_init().
3514 * It is implementation-dependent whether a failure to initialize
3515 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003516 */
3517psa_status_t psa_generate_random(uint8_t *output,
3518 size_t output_size);
3519
Gilles Peskine4c317f42018-07-12 01:24:09 +02003520/** Extra parameters for RSA key generation.
3521 *
Gilles Peskinebe42f312018-07-13 14:38:15 +02003522 * You may pass a pointer to a structure of this type as the \c extra
Gilles Peskine4c317f42018-07-12 01:24:09 +02003523 * parameter to psa_generate_key().
3524 */
3525typedef struct {
Gilles Peskineedd76872018-07-20 17:42:05 +02003526 uint32_t e; /**< Public exponent value. Default: 65537. */
Gilles Peskine4c317f42018-07-12 01:24:09 +02003527} psa_generate_key_extra_rsa;
3528
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003529/**
3530 * \brief Generate a key or key pair.
3531 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01003532 * \param handle Handle to the slot where the key will be stored.
3533 * This must be a valid slot for a key of the chosen
3534 * type: it must have been obtained by calling
3535 * psa_allocate_key() or psa_create_key() with the
3536 * correct \p type and with a maximum size that is
3537 * compatible with \p bits.
3538 * It must not contain any key material yet.
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003539 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
3540 * \param bits Key size in bits.
Gilles Peskine53d991e2018-07-12 01:14:59 +02003541 * \param[in] extra Extra parameters for key generation. The
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003542 * interpretation of this parameter depends on
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003543 * \p type. All types support \c NULL to use
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003544 * default parameters. Implementation that support
3545 * the generation of vendor-specific key types
3546 * that allow extra parameters shall document
3547 * the format of these extra parameters and
3548 * the default values. For standard parameters,
3549 * the meaning of \p extra is as follows:
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003550 * - For a symmetric key type (a type such
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003551 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
3552 * false), \p extra must be \c NULL.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003553 * - For an elliptic curve key type (a type
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003554 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
3555 * false), \p extra must be \c NULL.
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003556 * - For an RSA key (\p type is
3557 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
3558 * optional #psa_generate_key_extra_rsa structure
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003559 * specifying the public exponent. The
3560 * default public exponent used when \p extra
3561 * is \c NULL is 65537.
Gilles Peskine53d991e2018-07-12 01:14:59 +02003562 * \param extra_size Size of the buffer that \p extra
3563 * points to, in bytes. Note that if \p extra is
3564 * \c NULL then \p extra_size must be zero.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003565 *
Gilles Peskine28538492018-07-11 17:34:00 +02003566 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003567 * Success.
3568 * If the key is persistent, the key material and the key's metadata
3569 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003570 * \retval #PSA_ERROR_INVALID_HANDLE
3571 * \retval #PSA_ERROR_OCCUPIED_SLOT
3572 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +02003573 * \retval #PSA_ERROR_NOT_SUPPORTED
3574 * \retval #PSA_ERROR_INVALID_ARGUMENT
3575 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3576 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3577 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3578 * \retval #PSA_ERROR_HARDWARE_FAILURE
3579 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003580 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003581 * The library has not been previously initialized by psa_crypto_init().
3582 * It is implementation-dependent whether a failure to initialize
3583 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003584 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01003585psa_status_t psa_generate_key(psa_key_handle_t handle,
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003586 psa_key_type_t type,
3587 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003588 const void *extra,
3589 size_t extra_size);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003590
3591/**@}*/
3592
Gilles Peskinee59236f2018-01-27 23:32:46 +01003593#ifdef __cplusplus
3594}
3595#endif
3596
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003597/* The file "crypto_sizes.h" contains definitions for size calculation
3598 * macros whose definitions are implementation-specific. */
3599#include "crypto_sizes.h"
3600
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003601/* The file "crypto_struct.h" contains definitions for
3602 * implementation-specific structs that are declared above. */
3603#include "crypto_struct.h"
3604
3605/* The file "crypto_extra.h" contains vendor-specific definitions. This
3606 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01003607#include "crypto_extra.h"
3608
3609#endif /* PSA_CRYPTO_H */