blob: 4669b2a536f91bcbabd952e39e03bd7187274534 [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
Gilles Peskinef3b731e2018-12-12 13:38:31 +010056#define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8)
57#define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8)
58
59/* The file "crypto_types.h" declares types that encode errors,
60 * algorithms, key types, policies, etc. */
61#include "crypto_types.h"
62
63/* The file "crypto_values.h" declares macros to build and analyze values
64 * of integral types defined in "crypto_types.h". */
65#include "crypto_values.h"
66
67/** \defgroup initialization Library initialization
Gilles Peskinee59236f2018-01-27 23:32:46 +010068 * @{
69 */
70
71/**
Gilles Peskinee59236f2018-01-27 23:32:46 +010072 * \brief Library initialization.
73 *
74 * Applications must call this function before calling any other
75 * function in this module.
76 *
77 * Applications may call this function more than once. Once a call
78 * succeeds, subsequent calls are guaranteed to succeed.
79 *
itayzafrir18617092018-09-16 12:22:41 +030080 * If the application calls other functions before calling psa_crypto_init(),
81 * the behavior is undefined. Implementations are encouraged to either perform
82 * the operation as if the library had been initialized or to return
83 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
84 * implementations should not return a success status if the lack of
85 * initialization may have security implications, for example due to improper
86 * seeding of the random number generator.
87 *
Gilles Peskine28538492018-07-11 17:34:00 +020088 * \retval #PSA_SUCCESS
89 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
90 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
91 * \retval #PSA_ERROR_HARDWARE_FAILURE
92 * \retval #PSA_ERROR_TAMPERING_DETECTED
93 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +010094 */
95psa_status_t psa_crypto_init(void);
96
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010097/**@}*/
98
99/** \defgroup key_management Key management
100 * @{
101 */
102
Gilles Peskineae32aac2018-11-30 14:39:32 +0100103/** \brief Retrieve the lifetime of an open key.
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100104 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100105 * \param handle Handle to query.
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100106 * \param[out] lifetime On success, the lifetime value.
107 *
108 * \retval #PSA_SUCCESS
109 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100110 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100111 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
112 * \retval #PSA_ERROR_HARDWARE_FAILURE
113 * \retval #PSA_ERROR_TAMPERING_DETECTED
114 * \retval #PSA_ERROR_BAD_STATE
115 * The library has not been previously initialized by psa_crypto_init().
116 * It is implementation-dependent whether a failure to initialize
117 * results in this error code.
118 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100119psa_status_t psa_get_key_lifetime(psa_key_handle_t handle,
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100120 psa_key_lifetime_t *lifetime);
121
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100122
Gilles Peskinef535eb22018-11-30 14:08:36 +0100123/** Allocate a key slot for a transient key, i.e. a key which is only stored
124 * in volatile memory.
125 *
126 * The allocated key slot and its handle remain valid until the
127 * application calls psa_close_key() or psa_destroy_key() or until the
128 * application terminates.
129 *
130 * This function takes a key type and maximum size as arguments so that
131 * the implementation can reserve a corresponding amount of memory.
132 * Implementations are not required to enforce this limit: if the application
133 * later tries to create a larger key or a key of a different type, it
134 * is implementation-defined whether this may succeed.
135 *
136 * \param type The type of key that the slot will contain.
137 * \param max_bits The maximum key size that the slot will contain.
138 * \param[out] handle On success, a handle to a volatile key slot.
139 *
140 * \retval #PSA_SUCCESS
141 * Success. The application can now use the value of `*handle`
142 * to access the newly allocated key slot.
143 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
144 * There was not enough memory, or the maximum number of key slots
145 * has been reached.
146 * \retval #PSA_ERROR_INVALID_ARGUMENT
147 * This implementation does not support this key type.
148 */
149
150psa_status_t psa_allocate_key(psa_key_type_t type,
151 size_t max_bits,
152 psa_key_handle_t *handle);
153
154/** Open a handle to an existing persistent key.
155 *
156 * Open a handle to a key which was previously created with psa_create_key().
157 *
158 * \param lifetime The lifetime of the key. This designates a storage
159 * area where the key material is stored. This must not
160 * be #PSA_KEY_LIFETIME_VOLATILE.
161 * \param id The persistent identifier of the key.
162 * \param[out] handle On success, a handle to a key slot which contains
163 * the data and metadata loaded from the specified
164 * persistent location.
165 *
166 * \retval #PSA_SUCCESS
167 * Success. The application can now use the value of `*handle`
168 * to access the newly allocated key slot.
169 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
170 * \retval #PSA_ERROR_EMPTY_SLOT
171 * \retval #PSA_ERROR_INVALID_ARGUMENT
172 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
173 * \retval #PSA_ERROR_INVALID_ARGUMENT
174 * \p id is invalid for the specified lifetime.
175 * \retval #PSA_ERROR_NOT_SUPPORTED
176 * \p lifetime is not supported.
177 * \retval #PSA_ERROR_NOT_PERMITTED
178 * The specified key exists, but the application does not have the
179 * permission to access it. Note that this specification does not
180 * define any way to create such a key, but it may be possible
181 * through implementation-specific means.
182 */
183psa_status_t psa_open_key(psa_key_lifetime_t lifetime,
184 psa_key_id_t id,
185 psa_key_handle_t *handle);
186
187/** Create a new persistent key slot.
188 *
189 * Create a new persistent key slot and return a handle to it. The handle
190 * remains valid until the application calls psa_close_key() or terminates.
191 * The application can open the key again with psa_open_key() until it
192 * removes the key by calling psa_destroy_key().
193 *
194 * \param lifetime The lifetime of the key. This designates a storage
195 * area where the key material is stored. This must not
196 * be #PSA_KEY_LIFETIME_VOLATILE.
197 * \param id The persistent identifier of the key.
198 * \param type The type of key that the slot will contain.
199 * \param max_bits The maximum key size that the slot will contain.
200 * \param[out] handle On success, a handle to the newly created key slot.
201 * When key material is later created in this key slot,
202 * it will be saved to the specified persistent location.
203 *
204 * \retval #PSA_SUCCESS
205 * Success. The application can now use the value of `*handle`
206 * to access the newly allocated key slot.
207 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
208 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
209 * \retval #PSA_ERROR_OCCUPIED_SLOT
210 * There is already a key with the identifier \p id in the storage
211 * area designated by \p lifetime.
212 * \retval #PSA_ERROR_INVALID_ARGUMENT
213 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
214 * \retval #PSA_ERROR_INVALID_ARGUMENT
215 * \p id is invalid for the specified lifetime.
216 * \retval #PSA_ERROR_NOT_SUPPORTED
217 * \p lifetime is not supported.
218 * \retval #PSA_ERROR_NOT_PERMITTED
219 * \p lifetime is valid, but the application does not have the
220 * permission to create a key there.
221 */
222psa_status_t psa_create_key(psa_key_lifetime_t lifetime,
223 psa_key_id_t id,
224 psa_key_type_t type,
225 size_t max_bits,
226 psa_key_handle_t *handle);
227
228/** Close a key handle.
229 *
230 * If the handle designates a volatile key, destroy the key material and
231 * free all associated resources, just like psa_destroy_key().
232 *
233 * If the handle designates a persistent key, free all resources associated
234 * with the key in volatile memory. The key slot in persistent storage is
235 * not affected and can be opened again later with psa_open_key().
236 *
237 * \param handle The key handle to close.
238 *
239 * \retval #PSA_SUCCESS
240 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100241 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100242 */
243psa_status_t psa_close_key(psa_key_handle_t handle);
244
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100245/**@}*/
246
247/** \defgroup import_export Key import and export
248 * @{
249 */
250
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100251/**
252 * \brief Import a key in binary format.
253 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100254 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100255 * documentation of psa_export_public_key() for the format of public keys
256 * and to the documentation of psa_export_key() for the format for
257 * other key types.
258 *
259 * This specification supports a single format for each key type.
260 * Implementations may support other formats as long as the standard
261 * format is supported. Implementations that support other formats
262 * should ensure that the formats are clearly unambiguous so as to
263 * minimize the risk that an invalid input is accidentally interpreted
264 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100265 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100266 * \param handle Handle to the slot where the key will be stored.
267 * This must be a valid slot for a key of the chosen
268 * type: it must have been obtained by calling
269 * psa_allocate_key() or psa_create_key() with the
270 * correct \p type and with a maximum size that is
271 * compatible with \p data.
Gilles Peskinef7933932018-10-31 14:07:52 +0100272 * \param type Key type (a \c PSA_KEY_TYPE_XXX value). On a successful
273 * import, the key slot will contain a key of this type.
274 * \param[in] data Buffer containing the key data. The content of this
275 * buffer is interpreted according to \p type. It must
276 * contain the format described in the documentation
277 * of psa_export_key() or psa_export_public_key() for
278 * the chosen type.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200279 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100280 *
Gilles Peskine28538492018-07-11 17:34:00 +0200281 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100282 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100283 * If the key is persistent, the key material and the key's metadata
284 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100285 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200286 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200287 * The key type or key size is not supported, either by the
288 * implementation in general or in this particular slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200289 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine308b91d2018-02-08 09:47:44 +0100290 * The key slot is invalid,
291 * or the key data is not correctly formatted.
Gilles Peskine28538492018-07-11 17:34:00 +0200292 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskine65eb8582018-04-19 08:28:58 +0200293 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200294 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
295 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
296 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100297 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200298 * \retval #PSA_ERROR_HARDWARE_FAILURE
299 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300300 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300301 * The library has not been previously initialized by psa_crypto_init().
302 * It is implementation-dependent whether a failure to initialize
303 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100304 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100305psa_status_t psa_import_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100306 psa_key_type_t type,
307 const uint8_t *data,
308 size_t data_length);
309
310/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100311 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200312 *
313 * This function destroys the content of the key slot from both volatile
314 * memory and, if applicable, non-volatile storage. Implementations shall
315 * make a best effort to ensure that any previous content of the slot is
316 * unrecoverable.
317 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100318 * This function also erases any metadata such as policies and frees all
319 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200320 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100321 * \param handle Handle to the key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100322 *
Gilles Peskine28538492018-07-11 17:34:00 +0200323 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200324 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200325 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200326 * The slot holds content and cannot be erased because it is
327 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100328 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200329 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200330 * There was an failure in communication with the cryptoprocessor.
331 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200332 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200333 * The storage is corrupted. Implementations shall make a best effort
334 * to erase key material even in this stage, however applications
335 * should be aware that it may be impossible to guarantee that the
336 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +0200337 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200338 * An unexpected condition which is not a storage corruption or
339 * a communication failure occurred. The cryptoprocessor may have
340 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300341 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300342 * The library has not been previously initialized by psa_crypto_init().
343 * It is implementation-dependent whether a failure to initialize
344 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100345 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100346psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100347
348/**
349 * \brief Get basic metadata about a key.
350 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100351 * \param handle Handle to the key slot to query.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200352 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100353 * This may be a null pointer, in which case the key type
354 * is not written.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200355 * \param[out] bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +0100356 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +0100357 * is not written.
358 *
Gilles Peskine28538492018-07-11 17:34:00 +0200359 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100360 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200361 * \retval #PSA_ERROR_EMPTY_SLOT
Gilles Peskineae32aac2018-11-30 14:39:32 +0100362 * The handle is to a key slot which does not contain key material yet.
Gilles Peskine28538492018-07-11 17:34:00 +0200363 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
364 * \retval #PSA_ERROR_HARDWARE_FAILURE
365 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300366 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300367 * The library has not been previously initialized by psa_crypto_init().
368 * It is implementation-dependent whether a failure to initialize
369 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100370 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100371psa_status_t psa_get_key_information(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100372 psa_key_type_t *type,
373 size_t *bits);
374
375/**
376 * \brief Export a key in binary format.
377 *
378 * The output of this function can be passed to psa_import_key() to
379 * create an equivalent object.
380 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100381 * If the implementation of psa_import_key() supports other formats
382 * beyond the format specified here, the output from psa_export_key()
383 * must use the representation specified here, not the original
384 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100385 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100386 * For standard key types, the output format is as follows:
387 *
388 * - For symmetric keys (including MAC keys), the format is the
389 * raw bytes of the key.
390 * - For DES, the key data consists of 8 bytes. The parity bits must be
391 * correct.
392 * - For Triple-DES, the format is the concatenation of the
393 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100394 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200395 * is the non-encrypted DER encoding of the representation defined by
396 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
397 * ```
398 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200399 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200400 * modulus INTEGER, -- n
401 * publicExponent INTEGER, -- e
402 * privateExponent INTEGER, -- d
403 * prime1 INTEGER, -- p
404 * prime2 INTEGER, -- q
405 * exponent1 INTEGER, -- d mod (p-1)
406 * exponent2 INTEGER, -- d mod (q-1)
407 * coefficient INTEGER, -- (inverse of q) mod p
408 * }
409 * ```
410 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format
411 * is the non-encrypted DER encoding of the representation used by
Gilles Peskinec6290c02018-08-13 17:24:59 +0200412 * OpenSSL and OpenSSH, whose structure is described in ASN.1 as follows:
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200413 * ```
414 * DSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200415 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200416 * prime INTEGER, -- p
417 * subprime INTEGER, -- q
418 * generator INTEGER, -- g
419 * public INTEGER, -- y
420 * private INTEGER, -- x
421 * }
422 * ```
423 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +0100424 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100425 * a representation of the private value as a `ceiling(m/8)`-byte string
426 * where `m` is the bit size associated with the curve, i.e. the bit size
427 * of the order of the curve's coordinate field. This byte string is
428 * in little-endian order for Montgomery curves (curve types
429 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
430 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
431 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100432 * This is the content of the `privateKey` field of the `ECPrivateKey`
433 * format defined by RFC 5915.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200434 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
435 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100436 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100437 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200438 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200439 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200440 * \param[out] data_length On success, the number of bytes
441 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100442 *
Gilles Peskine28538492018-07-11 17:34:00 +0200443 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100444 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200445 * \retval #PSA_ERROR_EMPTY_SLOT
446 * \retval #PSA_ERROR_NOT_PERMITTED
Darryl Green9e2d7a02018-07-24 16:33:30 +0100447 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200448 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
449 * The size of the \p data buffer is too small. You can determine a
450 * sufficient buffer size by calling
451 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
452 * where \c type is the key type
453 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200454 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
455 * \retval #PSA_ERROR_HARDWARE_FAILURE
456 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300457 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300458 * The library has not been previously initialized by psa_crypto_init().
459 * It is implementation-dependent whether a failure to initialize
460 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100461 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100462psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100463 uint8_t *data,
464 size_t data_size,
465 size_t *data_length);
466
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100467/**
468 * \brief Export a public key or the public part of a key pair in binary format.
469 *
470 * The output of this function can be passed to psa_import_key() to
471 * create an object that is equivalent to the public key.
472 *
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200473 * The format is the DER representation defined by RFC 5280 as
474 * `SubjectPublicKeyInfo`, with the `subjectPublicKey` format
475 * specified below.
476 * ```
477 * SubjectPublicKeyInfo ::= SEQUENCE {
478 * algorithm AlgorithmIdentifier,
479 * subjectPublicKey BIT STRING }
480 * AlgorithmIdentifier ::= SEQUENCE {
481 * algorithm OBJECT IDENTIFIER,
482 * parameters ANY DEFINED BY algorithm OPTIONAL }
483 * ```
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100484 *
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200485 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY),
486 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.1 as
487 * `RSAPublicKey`,
488 * with the OID `rsaEncryption`,
489 * and with the parameters `NULL`.
490 * ```
491 * pkcs-1 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840)
492 * rsadsi(113549) pkcs(1) 1 }
493 * rsaEncryption OBJECT IDENTIFIER ::= { pkcs-1 1 }
494 *
495 * RSAPublicKey ::= SEQUENCE {
496 * modulus INTEGER, -- n
497 * publicExponent INTEGER } -- e
498 * ```
499 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY),
500 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.2 as
501 * `DSAPublicKey`,
502 * with the OID `id-dsa`,
503 * and with the parameters `DSS-Parms`.
504 * ```
505 * id-dsa OBJECT IDENTIFIER ::= {
506 * iso(1) member-body(2) us(840) x9-57(10040) x9cm(4) 1 }
507 *
508 * Dss-Parms ::= SEQUENCE {
509 * p INTEGER,
510 * q INTEGER,
511 * g INTEGER }
512 * DSAPublicKey ::= INTEGER -- public key, Y
513 * ```
514 * - For elliptic curve public keys (key types for which
515 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true),
516 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.5 as
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200517 * `ECPoint`, which contains the uncompressed
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200518 * representation defined by SEC1 &sect;2.3.3.
519 * The OID is `id-ecPublicKey`,
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200520 * and the parameters must be given as a `namedCurve` OID as specified in
Gilles Peskinec6290c02018-08-13 17:24:59 +0200521 * RFC 5480 &sect;2.1.1.1 or other applicable standards.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200522 * ```
523 * ansi-X9-62 OBJECT IDENTIFIER ::=
524 * { iso(1) member-body(2) us(840) 10045 }
525 * id-public-key-type OBJECT IDENTIFIER ::= { ansi-X9.62 2 }
526 * id-ecPublicKey OBJECT IDENTIFIER ::= { id-publicKeyType 1 }
527 *
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200528 * ECPoint ::= ...
529 * -- first 8 bits: 0x04;
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100530 * -- then x_P as a `ceiling(m/8)`-byte string, big endian;
531 * -- then y_P as a `ceiling(m/8)`-byte string, big endian;
532 * -- where `m` is the bit size associated with the curve,
Gilles Peskine7b5b4a02018-11-14 21:05:10 +0100533 * -- i.e. the bit size of `q` for a curve over `F_q`.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200534 *
535 * EcpkParameters ::= CHOICE { -- other choices are not allowed
536 * namedCurve OBJECT IDENTIFIER }
537 * ```
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100538 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100539 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200540 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200541 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200542 * \param[out] data_length On success, the number of bytes
543 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100544 *
Gilles Peskine28538492018-07-11 17:34:00 +0200545 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100546 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200547 * \retval #PSA_ERROR_EMPTY_SLOT
548 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200549 * The key is neither a public key nor a key pair.
550 * \retval #PSA_ERROR_NOT_SUPPORTED
551 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
552 * The size of the \p data buffer is too small. You can determine a
553 * sufficient buffer size by calling
554 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
555 * where \c type is the key type
556 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200557 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
558 * \retval #PSA_ERROR_HARDWARE_FAILURE
559 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300560 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300561 * The library has not been previously initialized by psa_crypto_init().
562 * It is implementation-dependent whether a failure to initialize
563 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100564 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100565psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100566 uint8_t *data,
567 size_t data_size,
568 size_t *data_length);
569
570/**@}*/
571
572/** \defgroup policy Key policies
573 * @{
574 */
575
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100576/** The type of the key policy data structure.
577 *
578 * This is an implementation-defined \c struct. Applications should not
579 * make any assumptions about the content of this structure except
580 * as directed by the documentation of a specific implementation. */
581typedef struct psa_key_policy_s psa_key_policy_t;
582
583/** \brief Initialize a key policy structure to a default that forbids all
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200584 * usage of the key.
585 *
586 * \param[out] policy The policy object to initialize.
587 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100588void psa_key_policy_init(psa_key_policy_t *policy);
589
Gilles Peskine7e198532018-03-08 07:50:30 +0100590/** \brief Set the standard fields of a policy structure.
591 *
592 * Note that this function does not make any consistency check of the
593 * parameters. The values are only checked when applying the policy to
594 * a key slot with psa_set_key_policy().
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200595 *
596 * \param[out] policy The policy object to modify.
597 * \param usage The permitted uses for the key.
598 * \param alg The algorithm that the key may be used for.
Gilles Peskine7e198532018-03-08 07:50:30 +0100599 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100600void psa_key_policy_set_usage(psa_key_policy_t *policy,
601 psa_key_usage_t usage,
602 psa_algorithm_t alg);
603
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200604/** \brief Retrieve the usage field of a policy structure.
605 *
606 * \param[in] policy The policy object to query.
607 *
608 * \return The permitted uses for a key with this policy.
609 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +0200610psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100611
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200612/** \brief Retrieve the algorithm field of a policy structure.
613 *
614 * \param[in] policy The policy object to query.
615 *
616 * \return The permitted algorithm for a key with this policy.
617 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +0200618psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100619
620/** \brief Set the usage policy on a key slot.
621 *
622 * This function must be called on an empty key slot, before importing,
623 * generating or creating a key in the slot. Changing the policy of an
624 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +0100625 *
626 * Implementations may set restrictions on supported key policies
627 * depending on the key type and the key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200628 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100629 * \param handle Handle to the key whose policy is to be changed.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200630 * \param[in] policy The policy object to query.
631 *
632 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100633 * Success.
634 * If the key is persistent, it is implementation-defined whether
635 * the policy has been saved to persistent storage. Implementations
636 * may defer saving the policy until the key material is created.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100637 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200638 * \retval #PSA_ERROR_OCCUPIED_SLOT
639 * \retval #PSA_ERROR_NOT_SUPPORTED
640 * \retval #PSA_ERROR_INVALID_ARGUMENT
641 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
642 * \retval #PSA_ERROR_HARDWARE_FAILURE
643 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300644 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300645 * The library has not been previously initialized by psa_crypto_init().
646 * It is implementation-dependent whether a failure to initialize
647 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100648 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100649psa_status_t psa_set_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100650 const psa_key_policy_t *policy);
651
Gilles Peskine7e198532018-03-08 07:50:30 +0100652/** \brief Get the usage policy for a key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200653 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100654 * \param handle Handle to the key slot whose policy is being queried.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200655 * \param[out] policy On success, the key's policy.
656 *
657 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100658 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200659 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
660 * \retval #PSA_ERROR_HARDWARE_FAILURE
661 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300662 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300663 * The library has not been previously initialized by psa_crypto_init().
664 * It is implementation-dependent whether a failure to initialize
665 * results in this error code.
Gilles Peskine7e198532018-03-08 07:50:30 +0100666 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100667psa_status_t psa_get_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100668 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +0100669
670/**@}*/
671
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100672/** \defgroup hash Message digests
673 * @{
674 */
675
Gilles Peskine308b91d2018-02-08 09:47:44 +0100676/** The type of the state data structure for multipart hash operations.
677 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100678 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100679 * make any assumptions about the content of this structure except
680 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100681typedef struct psa_hash_operation_s psa_hash_operation_t;
682
Gilles Peskine308b91d2018-02-08 09:47:44 +0100683/** The size of the output of psa_hash_finish(), in bytes.
684 *
685 * This is also the hash size that psa_hash_verify() expects.
686 *
687 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200688 * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm
Gilles Peskinebe42f312018-07-13 14:38:15 +0200689 * (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a
Gilles Peskine35855962018-04-19 08:39:16 +0200690 * hash algorithm).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100691 *
692 * \return The hash size for the specified hash algorithm.
693 * If the hash algorithm is not recognized, return 0.
694 * An implementation may return either 0 or the correct size
695 * for a hash algorithm that it recognizes, but does not support.
696 */
Gilles Peskine7ed29c52018-06-26 15:50:08 +0200697#define PSA_HASH_SIZE(alg) \
698 ( \
Gilles Peskine00709fa2018-08-22 18:25:41 +0200699 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD2 ? 16 : \
700 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD4 ? 16 : \
701 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 16 : \
702 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
703 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
704 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
705 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
706 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
707 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
708 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
709 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
710 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
711 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
712 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
713 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100714 0)
715
Gilles Peskine308b91d2018-02-08 09:47:44 +0100716/** Start a multipart hash operation.
717 *
718 * The sequence of operations to calculate a hash (message digest)
719 * is as follows:
720 * -# Allocate an operation object which will be passed to all the functions
721 * listed here.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200722 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100723 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100724 * of the message each time. The hash that is calculated is the hash
725 * of the concatenation of these messages in order.
726 * -# To calculate the hash, call psa_hash_finish().
727 * To compare the hash with an expected value, call psa_hash_verify().
728 *
729 * The application may call psa_hash_abort() at any time after the operation
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200730 * has been initialized with psa_hash_setup().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100731 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200732 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100733 * eventually terminate the operation. The following events terminate an
734 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100735 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100736 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100737 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200738 * \param[out] operation The operation object to use.
739 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
740 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100741 *
Gilles Peskine28538492018-07-11 17:34:00 +0200742 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100743 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200744 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200745 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +0200746 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
747 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
748 * \retval #PSA_ERROR_HARDWARE_FAILURE
749 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100750 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200751psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100752 psa_algorithm_t alg);
753
Gilles Peskine308b91d2018-02-08 09:47:44 +0100754/** Add a message fragment to a multipart hash operation.
755 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200756 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100757 *
758 * If this function returns an error status, the operation becomes inactive.
759 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200760 * \param[in,out] operation Active hash operation.
761 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200762 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100763 *
Gilles Peskine28538492018-07-11 17:34:00 +0200764 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100765 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200766 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100767 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200768 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
769 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
770 * \retval #PSA_ERROR_HARDWARE_FAILURE
771 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100772 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100773psa_status_t psa_hash_update(psa_hash_operation_t *operation,
774 const uint8_t *input,
775 size_t input_length);
776
Gilles Peskine308b91d2018-02-08 09:47:44 +0100777/** Finish the calculation of the hash of a message.
778 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200779 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100780 * This function calculates the hash of the message formed by concatenating
781 * the inputs passed to preceding calls to psa_hash_update().
782 *
783 * When this function returns, the operation becomes inactive.
784 *
785 * \warning Applications should not call this function if they expect
786 * a specific value for the hash. Call psa_hash_verify() instead.
787 * Beware that comparing integrity or authenticity data such as
788 * hash values with a function such as \c memcmp is risky
789 * because the time taken by the comparison may leak information
790 * about the hashed data which could allow an attacker to guess
791 * a valid hash and thereby bypass security controls.
792 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200793 * \param[in,out] operation Active hash operation.
794 * \param[out] hash Buffer where the hash is to be written.
795 * \param hash_size Size of the \p hash buffer in bytes.
796 * \param[out] hash_length On success, the number of bytes
797 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +0200798 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +0200799 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100800 *
Gilles Peskine28538492018-07-11 17:34:00 +0200801 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100802 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200803 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100804 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200805 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200806 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200807 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100808 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +0200809 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
810 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
811 * \retval #PSA_ERROR_HARDWARE_FAILURE
812 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100813 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100814psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
815 uint8_t *hash,
816 size_t hash_size,
817 size_t *hash_length);
818
Gilles Peskine308b91d2018-02-08 09:47:44 +0100819/** Finish the calculation of the hash of a message and compare it with
820 * an expected value.
821 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200822 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100823 * This function calculates the hash of the message formed by concatenating
824 * the inputs passed to preceding calls to psa_hash_update(). It then
825 * compares the calculated hash with the expected hash passed as a
826 * parameter to this function.
827 *
828 * When this function returns, the operation becomes inactive.
829 *
Gilles Peskine19067982018-03-20 17:54:53 +0100830 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +0100831 * comparison between the actual hash and the expected hash is performed
832 * in constant time.
833 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200834 * \param[in,out] operation Active hash operation.
835 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200836 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100837 *
Gilles Peskine28538492018-07-11 17:34:00 +0200838 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100839 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +0200840 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100841 * The hash of the message was calculated successfully, but it
842 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +0200843 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100844 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200845 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
846 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
847 * \retval #PSA_ERROR_HARDWARE_FAILURE
848 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100849 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100850psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
851 const uint8_t *hash,
852 size_t hash_length);
853
Gilles Peskine308b91d2018-02-08 09:47:44 +0100854/** Abort a hash operation.
855 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100856 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200857 * \p operation structure itself. Once aborted, the operation object
858 * can be reused for another operation by calling
859 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100860 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200861 * You may call this function any time after the operation object has
862 * been initialized by any of the following methods:
863 * - A call to psa_hash_setup(), whether it succeeds or not.
864 * - Initializing the \c struct to all-bits-zero.
865 * - Initializing the \c struct to logical zeros, e.g.
866 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100867 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200868 * In particular, calling psa_hash_abort() after the operation has been
869 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
870 * psa_hash_verify() is safe and has no effect.
871 *
872 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100873 *
Gilles Peskine28538492018-07-11 17:34:00 +0200874 * \retval #PSA_SUCCESS
875 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200876 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +0200877 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
878 * \retval #PSA_ERROR_HARDWARE_FAILURE
879 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100880 */
881psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100882
883/**@}*/
884
Gilles Peskine8c9def32018-02-08 10:02:12 +0100885/** \defgroup MAC Message authentication codes
886 * @{
887 */
888
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100889/** The type of the state data structure for multipart MAC operations.
890 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100891 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100892 * make any assumptions about the content of this structure except
893 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100894typedef struct psa_mac_operation_s psa_mac_operation_t;
895
Gilles Peskine89167cb2018-07-08 20:12:23 +0200896/** Start a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100897 *
Gilles Peskine89167cb2018-07-08 20:12:23 +0200898 * This function sets up the calculation of the MAC
899 * (message authentication code) of a byte string.
900 * To verify the MAC of a message against an
901 * expected value, use psa_mac_verify_setup() instead.
902 *
903 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100904 * -# Allocate an operation object which will be passed to all the functions
905 * listed here.
Gilles Peskine89167cb2018-07-08 20:12:23 +0200906 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100907 * The key remains associated with the operation even if the content
908 * of the key slot changes.
909 * -# Call psa_mac_update() zero, one or more times, passing a fragment
910 * of the message each time. The MAC that is calculated is the MAC
911 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +0200912 * -# At the end of the message, call psa_mac_sign_finish() to finish
913 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100914 *
915 * The application may call psa_mac_abort() at any time after the operation
Gilles Peskine89167cb2018-07-08 20:12:23 +0200916 * has been initialized with psa_mac_sign_setup().
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100917 *
Gilles Peskine89167cb2018-07-08 20:12:23 +0200918 * After a successful call to psa_mac_sign_setup(), the application must
919 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100920 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +0200921 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100922 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200923 * \param[out] operation The operation object to use.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100924 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200925 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
926 * such that #PSA_ALG_IS_MAC(alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100927 *
Gilles Peskine28538492018-07-11 17:34:00 +0200928 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100929 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100930 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200931 * \retval #PSA_ERROR_EMPTY_SLOT
932 * \retval #PSA_ERROR_NOT_PERMITTED
933 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200934 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +0200935 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200936 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +0200937 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
938 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
939 * \retval #PSA_ERROR_HARDWARE_FAILURE
940 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300941 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300942 * The library has not been previously initialized by psa_crypto_init().
943 * It is implementation-dependent whether a failure to initialize
944 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100945 */
Gilles Peskine89167cb2018-07-08 20:12:23 +0200946psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +0100947 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +0200948 psa_algorithm_t alg);
949
950/** Start a multipart MAC verification operation.
951 *
952 * This function sets up the verification of the MAC
953 * (message authentication code) of a byte string against an expected value.
954 *
955 * The sequence of operations to verify a MAC is as follows:
956 * -# Allocate an operation object which will be passed to all the functions
957 * listed here.
958 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
959 * The key remains associated with the operation even if the content
960 * of the key slot changes.
961 * -# Call psa_mac_update() zero, one or more times, passing a fragment
962 * of the message each time. The MAC that is calculated is the MAC
963 * of the concatenation of these messages in order.
964 * -# At the end of the message, call psa_mac_verify_finish() to finish
965 * calculating the actual MAC of the message and verify it against
966 * the expected value.
967 *
968 * The application may call psa_mac_abort() at any time after the operation
969 * has been initialized with psa_mac_verify_setup().
970 *
971 * After a successful call to psa_mac_verify_setup(), the application must
972 * eventually terminate the operation through one of the following methods:
973 * - A failed call to psa_mac_update().
974 * - A call to psa_mac_verify_finish() or psa_mac_abort().
975 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200976 * \param[out] operation The operation object to use.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100977 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200978 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
979 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +0200980 *
Gilles Peskine28538492018-07-11 17:34:00 +0200981 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +0200982 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100983 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200984 * \retval #PSA_ERROR_EMPTY_SLOT
985 * \retval #PSA_ERROR_NOT_PERMITTED
986 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +0200987 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +0200988 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +0200989 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +0200990 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
991 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
992 * \retval #PSA_ERROR_HARDWARE_FAILURE
993 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300994 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300995 * The library has not been previously initialized by psa_crypto_init().
996 * It is implementation-dependent whether a failure to initialize
997 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +0200998 */
999psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001000 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001001 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001002
Gilles Peskinedcd14942018-07-12 00:30:52 +02001003/** Add a message fragment to a multipart MAC operation.
1004 *
1005 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1006 * before calling this function.
1007 *
1008 * If this function returns an error status, the operation becomes inactive.
1009 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001010 * \param[in,out] operation Active MAC operation.
1011 * \param[in] input Buffer containing the message fragment to add to
1012 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001013 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001014 *
1015 * \retval #PSA_SUCCESS
1016 * Success.
1017 * \retval #PSA_ERROR_BAD_STATE
1018 * The operation state is not valid (not started, or already completed).
1019 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1020 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1021 * \retval #PSA_ERROR_HARDWARE_FAILURE
1022 * \retval #PSA_ERROR_TAMPERING_DETECTED
1023 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001024psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1025 const uint8_t *input,
1026 size_t input_length);
1027
Gilles Peskinedcd14942018-07-12 00:30:52 +02001028/** Finish the calculation of the MAC of a message.
1029 *
1030 * The application must call psa_mac_sign_setup() before calling this function.
1031 * This function calculates the MAC of the message formed by concatenating
1032 * the inputs passed to preceding calls to psa_mac_update().
1033 *
1034 * When this function returns, the operation becomes inactive.
1035 *
1036 * \warning Applications should not call this function if they expect
1037 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1038 * Beware that comparing integrity or authenticity data such as
1039 * MAC values with a function such as \c memcmp is risky
1040 * because the time taken by the comparison may leak information
1041 * about the MAC value which could allow an attacker to guess
1042 * a valid MAC and thereby bypass security controls.
1043 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001044 * \param[in,out] operation Active MAC operation.
1045 * \param[out] mac Buffer where the MAC value is to be written.
1046 * \param mac_size Size of the \p mac buffer in bytes.
1047 * \param[out] mac_length On success, the number of bytes
1048 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001049 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001050 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001051 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001052 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001053 *
1054 * \retval #PSA_SUCCESS
1055 * Success.
1056 * \retval #PSA_ERROR_BAD_STATE
1057 * The operation state is not valid (not started, or already completed).
1058 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001059 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001060 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1061 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1062 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1063 * \retval #PSA_ERROR_HARDWARE_FAILURE
1064 * \retval #PSA_ERROR_TAMPERING_DETECTED
1065 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001066psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1067 uint8_t *mac,
1068 size_t mac_size,
1069 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001070
Gilles Peskinedcd14942018-07-12 00:30:52 +02001071/** Finish the calculation of the MAC of a message and compare it with
1072 * an expected value.
1073 *
1074 * The application must call psa_mac_verify_setup() before calling this function.
1075 * This function calculates the MAC of the message formed by concatenating
1076 * the inputs passed to preceding calls to psa_mac_update(). It then
1077 * compares the calculated MAC with the expected MAC passed as a
1078 * parameter to this function.
1079 *
1080 * When this function returns, the operation becomes inactive.
1081 *
1082 * \note Implementations shall make the best effort to ensure that the
1083 * comparison between the actual MAC and the expected MAC is performed
1084 * in constant time.
1085 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001086 * \param[in,out] operation Active MAC operation.
1087 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001088 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001089 *
1090 * \retval #PSA_SUCCESS
1091 * The expected MAC is identical to the actual MAC of the message.
1092 * \retval #PSA_ERROR_INVALID_SIGNATURE
1093 * The MAC of the message was calculated successfully, but it
1094 * differs from the expected MAC.
1095 * \retval #PSA_ERROR_BAD_STATE
1096 * The operation state is not valid (not started, or already completed).
1097 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1098 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1099 * \retval #PSA_ERROR_HARDWARE_FAILURE
1100 * \retval #PSA_ERROR_TAMPERING_DETECTED
1101 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001102psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1103 const uint8_t *mac,
1104 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001105
Gilles Peskinedcd14942018-07-12 00:30:52 +02001106/** Abort a MAC operation.
1107 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001108 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001109 * \p operation structure itself. Once aborted, the operation object
1110 * can be reused for another operation by calling
1111 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001112 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001113 * You may call this function any time after the operation object has
1114 * been initialized by any of the following methods:
1115 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1116 * it succeeds or not.
1117 * - Initializing the \c struct to all-bits-zero.
1118 * - Initializing the \c struct to logical zeros, e.g.
1119 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001120 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001121 * In particular, calling psa_mac_abort() after the operation has been
1122 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1123 * psa_mac_verify_finish() is safe and has no effect.
1124 *
1125 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001126 *
1127 * \retval #PSA_SUCCESS
1128 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001129 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001130 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1131 * \retval #PSA_ERROR_HARDWARE_FAILURE
1132 * \retval #PSA_ERROR_TAMPERING_DETECTED
1133 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001134psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1135
1136/**@}*/
1137
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001138/** \defgroup cipher Symmetric ciphers
1139 * @{
1140 */
1141
1142/** The type of the state data structure for multipart cipher operations.
1143 *
1144 * This is an implementation-defined \c struct. Applications should not
1145 * make any assumptions about the content of this structure except
1146 * as directed by the documentation of a specific implementation. */
1147typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1148
1149/** Set the key for a multipart symmetric encryption operation.
1150 *
1151 * The sequence of operations to encrypt a message with a symmetric cipher
1152 * is as follows:
1153 * -# Allocate an operation object which will be passed to all the functions
1154 * listed here.
Gilles Peskinefe119512018-07-08 21:39:34 +02001155 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001156 * The key remains associated with the operation even if the content
1157 * of the key slot changes.
itayzafrired7382f2018-08-02 14:19:33 +03001158 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001159 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001160 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001161 * requires a specific IV value.
1162 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1163 * of the message each time.
1164 * -# Call psa_cipher_finish().
1165 *
1166 * The application may call psa_cipher_abort() at any time after the operation
Gilles Peskinefe119512018-07-08 21:39:34 +02001167 * has been initialized with psa_cipher_encrypt_setup().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001168 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001169 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001170 * eventually terminate the operation. The following events terminate an
1171 * operation:
itayzafrired7382f2018-08-02 14:19:33 +03001172 * - A failed call to psa_cipher_generate_iv(), psa_cipher_set_iv()
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001173 * or psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001174 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001175 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001176 * \param[out] operation The operation object to use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001177 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001178 * \param alg The cipher algorithm to compute
1179 * (\c PSA_ALG_XXX value such that
1180 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001181 *
Gilles Peskine28538492018-07-11 17:34:00 +02001182 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001183 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001184 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001185 * \retval #PSA_ERROR_EMPTY_SLOT
1186 * \retval #PSA_ERROR_NOT_PERMITTED
1187 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001188 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001189 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001190 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001191 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1192 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1193 * \retval #PSA_ERROR_HARDWARE_FAILURE
1194 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001195 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001196 * The library has not been previously initialized by psa_crypto_init().
1197 * It is implementation-dependent whether a failure to initialize
1198 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001199 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001200psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001201 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001202 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001203
1204/** Set the key for a multipart symmetric decryption operation.
1205 *
1206 * The sequence of operations to decrypt a message with a symmetric cipher
1207 * is as follows:
1208 * -# Allocate an operation object which will be passed to all the functions
1209 * listed here.
Gilles Peskinefe119512018-07-08 21:39:34 +02001210 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001211 * The key remains associated with the operation even if the content
1212 * of the key slot changes.
1213 * -# Call psa_cipher_update() with the IV (initialization vector) for the
1214 * decryption. If the IV is prepended to the ciphertext, you can call
1215 * psa_cipher_update() on a buffer containing the IV followed by the
1216 * beginning of the message.
1217 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1218 * of the message each time.
1219 * -# Call psa_cipher_finish().
1220 *
1221 * The application may call psa_cipher_abort() at any time after the operation
Gilles Peskinefe119512018-07-08 21:39:34 +02001222 * has been initialized with psa_cipher_decrypt_setup().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001223 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001224 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001225 * eventually terminate the operation. The following events terminate an
1226 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001227 * - A failed call to psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001228 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001229 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001230 * \param[out] operation The operation object to use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001231 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001232 * \param alg The cipher algorithm to compute
1233 * (\c PSA_ALG_XXX value such that
1234 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001235 *
Gilles Peskine28538492018-07-11 17:34:00 +02001236 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001237 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001238 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001239 * \retval #PSA_ERROR_EMPTY_SLOT
1240 * \retval #PSA_ERROR_NOT_PERMITTED
1241 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001242 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001243 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001244 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001245 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1246 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1247 * \retval #PSA_ERROR_HARDWARE_FAILURE
1248 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001249 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001250 * The library has not been previously initialized by psa_crypto_init().
1251 * It is implementation-dependent whether a failure to initialize
1252 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001253 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001254psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001255 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001256 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001257
Gilles Peskinedcd14942018-07-12 00:30:52 +02001258/** Generate an IV for a symmetric encryption operation.
1259 *
1260 * This function generates a random IV (initialization vector), nonce
1261 * or initial counter value for the encryption operation as appropriate
1262 * for the chosen algorithm, key type and key size.
1263 *
1264 * The application must call psa_cipher_encrypt_setup() before
1265 * calling this function.
1266 *
1267 * If this function returns an error status, the operation becomes inactive.
1268 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001269 * \param[in,out] operation Active cipher operation.
1270 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001271 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001272 * \param[out] iv_length On success, the number of bytes of the
1273 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001274 *
1275 * \retval #PSA_SUCCESS
1276 * Success.
1277 * \retval #PSA_ERROR_BAD_STATE
1278 * The operation state is not valid (not started, or IV already set).
1279 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001280 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001281 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1282 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1283 * \retval #PSA_ERROR_HARDWARE_FAILURE
1284 * \retval #PSA_ERROR_TAMPERING_DETECTED
1285 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001286psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1287 unsigned char *iv,
1288 size_t iv_size,
1289 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001290
Gilles Peskinedcd14942018-07-12 00:30:52 +02001291/** Set the IV for a symmetric encryption or decryption operation.
1292 *
1293 * This function sets the random IV (initialization vector), nonce
1294 * or initial counter value for the encryption or decryption operation.
1295 *
1296 * The application must call psa_cipher_encrypt_setup() before
1297 * calling this function.
1298 *
1299 * If this function returns an error status, the operation becomes inactive.
1300 *
1301 * \note When encrypting, applications should use psa_cipher_generate_iv()
1302 * instead of this function, unless implementing a protocol that requires
1303 * a non-random IV.
1304 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001305 * \param[in,out] operation Active cipher operation.
1306 * \param[in] iv Buffer containing the IV to use.
1307 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001308 *
1309 * \retval #PSA_SUCCESS
1310 * Success.
1311 * \retval #PSA_ERROR_BAD_STATE
1312 * The operation state is not valid (not started, or IV already set).
1313 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001314 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001315 * or the chosen algorithm does not use an IV.
1316 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1317 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1318 * \retval #PSA_ERROR_HARDWARE_FAILURE
1319 * \retval #PSA_ERROR_TAMPERING_DETECTED
1320 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001321psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1322 const unsigned char *iv,
1323 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001324
Gilles Peskinedcd14942018-07-12 00:30:52 +02001325/** Encrypt or decrypt a message fragment in an active cipher operation.
1326 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001327 * Before calling this function, you must:
1328 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1329 * The choice of setup function determines whether this function
1330 * encrypts or decrypts its input.
1331 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1332 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001333 *
1334 * If this function returns an error status, the operation becomes inactive.
1335 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001336 * \param[in,out] operation Active cipher operation.
1337 * \param[in] input Buffer containing the message fragment to
1338 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001339 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001340 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001341 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001342 * \param[out] output_length On success, the number of bytes
1343 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001344 *
1345 * \retval #PSA_SUCCESS
1346 * Success.
1347 * \retval #PSA_ERROR_BAD_STATE
1348 * The operation state is not valid (not started, IV required but
1349 * not set, or already completed).
1350 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1351 * The size of the \p output buffer is too small.
1352 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1353 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1354 * \retval #PSA_ERROR_HARDWARE_FAILURE
1355 * \retval #PSA_ERROR_TAMPERING_DETECTED
1356 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001357psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1358 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001359 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001360 unsigned char *output,
1361 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001362 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001363
Gilles Peskinedcd14942018-07-12 00:30:52 +02001364/** Finish encrypting or decrypting a message in a cipher operation.
1365 *
1366 * The application must call psa_cipher_encrypt_setup() or
1367 * psa_cipher_decrypt_setup() before calling this function. The choice
1368 * of setup function determines whether this function encrypts or
1369 * decrypts its input.
1370 *
1371 * This function finishes the encryption or decryption of the message
1372 * formed by concatenating the inputs passed to preceding calls to
1373 * psa_cipher_update().
1374 *
1375 * When this function returns, the operation becomes inactive.
1376 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001377 * \param[in,out] operation Active cipher operation.
1378 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001379 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001380 * \param[out] output_length On success, the number of bytes
1381 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001382 *
1383 * \retval #PSA_SUCCESS
1384 * Success.
1385 * \retval #PSA_ERROR_BAD_STATE
1386 * The operation state is not valid (not started, IV required but
1387 * not set, or already completed).
1388 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1389 * The size of the \p output buffer is too small.
1390 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1391 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1392 * \retval #PSA_ERROR_HARDWARE_FAILURE
1393 * \retval #PSA_ERROR_TAMPERING_DETECTED
1394 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001395psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001396 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001397 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001398 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001399
Gilles Peskinedcd14942018-07-12 00:30:52 +02001400/** Abort a cipher operation.
1401 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001402 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001403 * \p operation structure itself. Once aborted, the operation object
1404 * can be reused for another operation by calling
1405 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001406 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001407 * You may call this function any time after the operation object has
1408 * been initialized by any of the following methods:
1409 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1410 * whether it succeeds or not.
1411 * - Initializing the \c struct to all-bits-zero.
1412 * - Initializing the \c struct to logical zeros, e.g.
1413 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001414 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001415 * In particular, calling psa_cipher_abort() after the operation has been
1416 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1417 * is safe and has no effect.
1418 *
1419 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001420 *
1421 * \retval #PSA_SUCCESS
1422 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001423 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001424 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1425 * \retval #PSA_ERROR_HARDWARE_FAILURE
1426 * \retval #PSA_ERROR_TAMPERING_DETECTED
1427 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001428psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1429
1430/**@}*/
1431
Gilles Peskine3b555712018-03-03 21:27:57 +01001432/** \defgroup aead Authenticated encryption with associated data (AEAD)
1433 * @{
1434 */
1435
Gilles Peskine5e39dc92018-06-08 11:41:57 +02001436/** The tag size for an AEAD algorithm, in bytes.
Gilles Peskine3b555712018-03-03 21:27:57 +01001437 *
Gilles Peskine5e39dc92018-06-08 11:41:57 +02001438 * \param alg An AEAD algorithm
1439 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001440 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine5e39dc92018-06-08 11:41:57 +02001441 *
1442 * \return The tag size for the specified algorithm.
1443 * If the AEAD algorithm does not have an identified
1444 * tag that can be distinguished from the rest of
1445 * the ciphertext, return 0.
1446 * If the AEAD algorithm is not recognized, return 0.
1447 * An implementation may return either 0 or a
1448 * correct size for an AEAD algorithm that it
1449 * recognizes, but does not support.
1450 */
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001451#define PSA_AEAD_TAG_LENGTH(alg) \
1452 (PSA_ALG_IS_AEAD(alg) ? \
1453 (((alg) & PSA_ALG_AEAD_TAG_LENGTH_MASK) >> PSA_AEAD_TAG_LENGTH_OFFSET) : \
Gilles Peskine5e39dc92018-06-08 11:41:57 +02001454 0)
Gilles Peskine3b555712018-03-03 21:27:57 +01001455
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001456/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001457 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001458 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001459 * \param alg The AEAD algorithm to compute
1460 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001461 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001462 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001463 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001464 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001465 * but not encrypted.
1466 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001467 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001468 * encrypted.
1469 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001470 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001471 * encrypted data. The additional data is not
1472 * part of this output. For algorithms where the
1473 * encrypted data and the authentication tag
1474 * are defined as separate outputs, the
1475 * authentication tag is appended to the
1476 * encrypted data.
1477 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1478 * This must be at least
1479 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1480 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001481 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001482 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001483 *
Gilles Peskine28538492018-07-11 17:34:00 +02001484 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001485 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001486 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001487 * \retval #PSA_ERROR_EMPTY_SLOT
1488 * \retval #PSA_ERROR_NOT_PERMITTED
1489 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001490 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001491 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001492 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001493 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1494 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1495 * \retval #PSA_ERROR_HARDWARE_FAILURE
1496 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001497 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001498 * The library has not been previously initialized by psa_crypto_init().
1499 * It is implementation-dependent whether a failure to initialize
1500 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001501 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001502psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001503 psa_algorithm_t alg,
1504 const uint8_t *nonce,
1505 size_t nonce_length,
1506 const uint8_t *additional_data,
1507 size_t additional_data_length,
1508 const uint8_t *plaintext,
1509 size_t plaintext_length,
1510 uint8_t *ciphertext,
1511 size_t ciphertext_size,
1512 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001513
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001514/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001515 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001516 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001517 * \param alg The AEAD algorithm to compute
1518 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001519 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001520 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001521 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001522 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001523 * but not encrypted.
1524 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001525 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001526 * encrypted. For algorithms where the
1527 * encrypted data and the authentication tag
1528 * are defined as separate inputs, the buffer
1529 * must contain the encrypted data followed
1530 * by the authentication tag.
1531 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001532 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001533 * \param plaintext_size Size of the \p plaintext buffer in bytes.
1534 * This must be at least
1535 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
1536 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001537 * \param[out] plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03001538 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001539 *
Gilles Peskine28538492018-07-11 17:34:00 +02001540 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001541 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001542 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001543 * \retval #PSA_ERROR_EMPTY_SLOT
1544 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001545 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02001546 * \retval #PSA_ERROR_NOT_PERMITTED
1547 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001548 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001549 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001550 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001551 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1552 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1553 * \retval #PSA_ERROR_HARDWARE_FAILURE
1554 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001555 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001556 * The library has not been previously initialized by psa_crypto_init().
1557 * It is implementation-dependent whether a failure to initialize
1558 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001559 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001560psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001561 psa_algorithm_t alg,
1562 const uint8_t *nonce,
1563 size_t nonce_length,
1564 const uint8_t *additional_data,
1565 size_t additional_data_length,
1566 const uint8_t *ciphertext,
1567 size_t ciphertext_length,
1568 uint8_t *plaintext,
1569 size_t plaintext_size,
1570 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001571
1572/**@}*/
1573
Gilles Peskine20035e32018-02-03 22:44:14 +01001574/** \defgroup asymmetric Asymmetric cryptography
1575 * @{
1576 */
1577
1578/**
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001579 * \brief ECDSA signature size for a given curve bit size
Gilles Peskine0189e752018-02-03 23:57:22 +01001580 *
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001581 * \param curve_bits Curve size in bits.
1582 * \return Signature size in bytes.
Gilles Peskine0189e752018-02-03 23:57:22 +01001583 *
1584 * \note This macro returns a compile-time constant if its argument is one.
Gilles Peskine0189e752018-02-03 23:57:22 +01001585 */
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001586#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
1587 (PSA_BITS_TO_BYTES(curve_bits) * 2)
Gilles Peskine0189e752018-02-03 23:57:22 +01001588
Gilles Peskine0189e752018-02-03 23:57:22 +01001589/**
Gilles Peskine20035e32018-02-03 22:44:14 +01001590 * \brief Sign a hash or short message with a private key.
1591 *
Gilles Peskine08bac712018-06-26 16:14:46 +02001592 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001593 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02001594 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
1595 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
1596 * to determine the hash algorithm to use.
1597 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001598 * \param handle Handle to the key to use for the operation.
1599 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001600 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001601 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001602 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001603 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001604 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001605 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001606 * \param[out] signature_length On success, the number of bytes
1607 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001608 *
Gilles Peskine28538492018-07-11 17:34:00 +02001609 * \retval #PSA_SUCCESS
1610 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001611 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01001612 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001613 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001614 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001615 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001616 * \retval #PSA_ERROR_NOT_SUPPORTED
1617 * \retval #PSA_ERROR_INVALID_ARGUMENT
1618 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1619 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1620 * \retval #PSA_ERROR_HARDWARE_FAILURE
1621 * \retval #PSA_ERROR_TAMPERING_DETECTED
1622 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03001623 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001624 * The library has not been previously initialized by psa_crypto_init().
1625 * It is implementation-dependent whether a failure to initialize
1626 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01001627 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001628psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01001629 psa_algorithm_t alg,
1630 const uint8_t *hash,
1631 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01001632 uint8_t *signature,
1633 size_t signature_size,
1634 size_t *signature_length);
1635
1636/**
1637 * \brief Verify the signature a hash or short message using a public key.
1638 *
Gilles Peskine08bac712018-06-26 16:14:46 +02001639 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001640 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02001641 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
1642 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
1643 * to determine the hash algorithm to use.
1644 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001645 * \param handle Handle to the key to use for the operation.
1646 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001647 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001648 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001649 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02001650 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001651 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001652 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001653 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001654 *
Gilles Peskine28538492018-07-11 17:34:00 +02001655 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001656 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02001657 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001658 * The calculation was perfomed successfully, but the passed
1659 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02001660 * \retval #PSA_ERROR_NOT_SUPPORTED
1661 * \retval #PSA_ERROR_INVALID_ARGUMENT
1662 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1663 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1664 * \retval #PSA_ERROR_HARDWARE_FAILURE
1665 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001666 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001667 * The library has not been previously initialized by psa_crypto_init().
1668 * It is implementation-dependent whether a failure to initialize
1669 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01001670 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001671psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01001672 psa_algorithm_t alg,
1673 const uint8_t *hash,
1674 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02001675 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02001676 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01001677
Gilles Peskine723feff2018-05-31 20:08:13 +02001678#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
Gilles Peskine072ac562018-06-30 00:21:29 +02001679 (PSA_ALG_IS_RSA_OAEP(alg) ? \
1680 2 * PSA_HASH_FINAL_SIZE(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
Gilles Peskine723feff2018-05-31 20:08:13 +02001681 11 /*PKCS#1v1.5*/)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001682
1683/**
1684 * \brief Encrypt a short message with a public key.
1685 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001686 * \param handle Handle to the key to use for the operation.
1687 * It must be a public key or an asymmetric
1688 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001689 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001690 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001691 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001692 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001693 * \param[in] salt A salt or label, if supported by the
1694 * encryption algorithm.
1695 * If the algorithm does not support a
1696 * salt, pass \c NULL.
1697 * If the algorithm supports an optional
1698 * salt and you do not want to pass a salt,
1699 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001700 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001701 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1702 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001703 * \param salt_length Size of the \p salt buffer in bytes.
1704 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001705 * \param[out] output Buffer where the encrypted message is to
1706 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001707 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001708 * \param[out] output_length On success, the number of bytes
1709 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001710 *
Gilles Peskine28538492018-07-11 17:34:00 +02001711 * \retval #PSA_SUCCESS
1712 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001713 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001714 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001715 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001716 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001717 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001718 * \retval #PSA_ERROR_NOT_SUPPORTED
1719 * \retval #PSA_ERROR_INVALID_ARGUMENT
1720 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1721 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1722 * \retval #PSA_ERROR_HARDWARE_FAILURE
1723 * \retval #PSA_ERROR_TAMPERING_DETECTED
1724 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03001725 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001726 * The library has not been previously initialized by psa_crypto_init().
1727 * It is implementation-dependent whether a failure to initialize
1728 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001729 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001730psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001731 psa_algorithm_t alg,
1732 const uint8_t *input,
1733 size_t input_length,
1734 const uint8_t *salt,
1735 size_t salt_length,
1736 uint8_t *output,
1737 size_t output_size,
1738 size_t *output_length);
1739
1740/**
1741 * \brief Decrypt a short message with a private key.
1742 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001743 * \param handle Handle to the key to use for the operation.
1744 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001745 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001746 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001747 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001748 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001749 * \param[in] salt A salt or label, if supported by the
1750 * encryption algorithm.
1751 * If the algorithm does not support a
1752 * salt, pass \c NULL.
1753 * If the algorithm supports an optional
1754 * salt and you do not want to pass a salt,
1755 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001756 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001757 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1758 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001759 * \param salt_length Size of the \p salt buffer in bytes.
1760 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001761 * \param[out] output Buffer where the decrypted message is to
1762 * be written.
1763 * \param output_size Size of the \c output buffer in bytes.
1764 * \param[out] output_length On success, the number of bytes
1765 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001766 *
Gilles Peskine28538492018-07-11 17:34:00 +02001767 * \retval #PSA_SUCCESS
1768 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001769 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001770 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001771 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001772 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001773 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001774 * \retval #PSA_ERROR_NOT_SUPPORTED
1775 * \retval #PSA_ERROR_INVALID_ARGUMENT
1776 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1777 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1778 * \retval #PSA_ERROR_HARDWARE_FAILURE
1779 * \retval #PSA_ERROR_TAMPERING_DETECTED
1780 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
1781 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03001782 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001783 * The library has not been previously initialized by psa_crypto_init().
1784 * It is implementation-dependent whether a failure to initialize
1785 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001786 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001787psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001788 psa_algorithm_t alg,
1789 const uint8_t *input,
1790 size_t input_length,
1791 const uint8_t *salt,
1792 size_t salt_length,
1793 uint8_t *output,
1794 size_t output_size,
1795 size_t *output_length);
1796
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001797/**@}*/
1798
Gilles Peskineedd76872018-07-20 17:42:05 +02001799/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02001800 * @{
1801 */
1802
1803/** The type of the state data structure for generators.
1804 *
1805 * Before calling any function on a generator, the application must
1806 * initialize it by any of the following means:
1807 * - Set the structure to all-bits-zero, for example:
1808 * \code
1809 * psa_crypto_generator_t generator;
1810 * memset(&generator, 0, sizeof(generator));
1811 * \endcode
1812 * - Initialize the structure to logical zero values, for example:
1813 * \code
1814 * psa_crypto_generator_t generator = {0};
1815 * \endcode
1816 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
1817 * for example:
1818 * \code
1819 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1820 * \endcode
1821 * - Assign the result of the function psa_crypto_generator_init()
1822 * to the structure, for example:
1823 * \code
1824 * psa_crypto_generator_t generator;
1825 * generator = psa_crypto_generator_init();
1826 * \endcode
1827 *
1828 * This is an implementation-defined \c struct. Applications should not
1829 * make any assumptions about the content of this structure except
1830 * as directed by the documentation of a specific implementation.
1831 */
1832typedef struct psa_crypto_generator_s psa_crypto_generator_t;
1833
1834/** \def PSA_CRYPTO_GENERATOR_INIT
1835 *
1836 * This macro returns a suitable initializer for a generator object
1837 * of type #psa_crypto_generator_t.
1838 */
1839#ifdef __DOXYGEN_ONLY__
1840/* This is an example definition for documentation purposes.
1841 * Implementations should define a suitable value in `crypto_struct.h`.
1842 */
1843#define PSA_CRYPTO_GENERATOR_INIT {0}
1844#endif
1845
1846/** Return an initial value for a generator object.
1847 */
1848static psa_crypto_generator_t psa_crypto_generator_init(void);
1849
1850/** Retrieve the current capacity of a generator.
1851 *
1852 * The capacity of a generator is the maximum number of bytes that it can
1853 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
1854 *
1855 * \param[in] generator The generator to query.
1856 * \param[out] capacity On success, the capacity of the generator.
1857 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001858 * \retval #PSA_SUCCESS
1859 * \retval #PSA_ERROR_BAD_STATE
1860 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02001861 */
1862psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
1863 size_t *capacity);
1864
1865/** Read some data from a generator.
1866 *
1867 * This function reads and returns a sequence of bytes from a generator.
1868 * The data that is read is discarded from the generator. The generator's
1869 * capacity is decreased by the number of bytes read.
1870 *
1871 * \param[in,out] generator The generator object to read from.
1872 * \param[out] output Buffer where the generator output will be
1873 * written.
1874 * \param output_length Number of bytes to output.
1875 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001876 * \retval #PSA_SUCCESS
1877 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02001878 * There were fewer than \p output_length bytes
1879 * in the generator. Note that in this case, no
1880 * output is written to the output buffer.
1881 * The generator's capacity is set to 0, thus
1882 * subsequent calls to this function will not
1883 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001884 * \retval #PSA_ERROR_BAD_STATE
1885 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1886 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1887 * \retval #PSA_ERROR_HARDWARE_FAILURE
1888 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02001889 */
1890psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
1891 uint8_t *output,
1892 size_t output_length);
1893
1894/** Create a symmetric key from data read from a generator.
1895 *
1896 * This function reads a sequence of bytes from a generator and imports
1897 * these bytes as a key.
1898 * The data that is read is discarded from the generator. The generator's
1899 * capacity is decreased by the number of bytes read.
1900 *
1901 * This function is equivalent to calling #psa_generator_read and
1902 * passing the resulting output to #psa_import_key, but
1903 * if the implementation provides an isolation boundary then
1904 * the key material is not exposed outside the isolation boundary.
1905 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001906 * \param handle Handle to the slot where the key will be stored.
1907 * This must be a valid slot for a key of the chosen
1908 * type: it must have been obtained by calling
1909 * psa_allocate_key() or psa_create_key() with the
1910 * correct \p type and with a maximum size that is
1911 * compatible with \p bits.
1912 * It must not contain any key material yet.
Gilles Peskineeab56e42018-07-12 17:12:33 +02001913 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
1914 * This must be a symmetric key type.
1915 * \param bits Key size in bits.
1916 * \param[in,out] generator The generator object to read from.
1917 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001918 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02001919 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01001920 * If the key is persistent, the key material and the key's metadata
1921 * have been saved to persistent storage.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001922 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02001923 * There were fewer than \p output_length bytes
1924 * in the generator. Note that in this case, no
1925 * output is written to the output buffer.
1926 * The generator's capacity is set to 0, thus
1927 * subsequent calls to this function will not
1928 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001929 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02001930 * The key type or key size is not supported, either by the
1931 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001932 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineae32aac2018-11-30 14:39:32 +01001933 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001934 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskineeab56e42018-07-12 17:12:33 +02001935 * There is already a key in the specified slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001936 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1937 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1938 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1939 * \retval #PSA_ERROR_HARDWARE_FAILURE
1940 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001941 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001942 * The library has not been previously initialized by psa_crypto_init().
1943 * It is implementation-dependent whether a failure to initialize
1944 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02001945 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001946psa_status_t psa_generator_import_key(psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02001947 psa_key_type_t type,
1948 size_t bits,
1949 psa_crypto_generator_t *generator);
1950
1951/** Abort a generator.
1952 *
1953 * Once a generator has been aborted, its capacity is zero.
1954 * Aborting a generator frees all associated resources except for the
1955 * \c generator structure itself.
1956 *
1957 * This function may be called at any time as long as the generator
1958 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
1959 * psa_crypto_generator_init() or a zero value. In particular, it is valid
1960 * to call psa_generator_abort() twice, or to call psa_generator_abort()
1961 * on a generator that has not been set up.
1962 *
1963 * Once aborted, the generator object may be called.
1964 *
1965 * \param[in,out] generator The generator to abort.
1966 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001967 * \retval #PSA_SUCCESS
1968 * \retval #PSA_ERROR_BAD_STATE
1969 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1970 * \retval #PSA_ERROR_HARDWARE_FAILURE
1971 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02001972 */
1973psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
1974
Gilles Peskine8feb3a82018-09-18 12:06:11 +02001975/** Use the maximum possible capacity for a generator.
1976 *
1977 * Use this value as the capacity argument when setting up a generator
1978 * to indicate that the generator should have the maximum possible capacity.
1979 * The value of the maximum possible capacity depends on the generator
1980 * algorithm.
1981 */
1982#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
1983
Gilles Peskineeab56e42018-07-12 17:12:33 +02001984/**@}*/
1985
Gilles Peskineea0fb492018-07-12 17:17:20 +02001986/** \defgroup derivation Key derivation
1987 * @{
1988 */
1989
1990/** Set up a key derivation operation.
1991 *
1992 * A key derivation algorithm takes three inputs: a secret input \p key and
1993 * two non-secret inputs \p label and p salt.
1994 * The result of this function is a byte generator which can
1995 * be used to produce keys and other cryptographic material.
1996 *
1997 * The role of \p label and \p salt is as follows:
Gilles Peskinebef7f142018-07-12 17:22:21 +02001998 * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step
1999 * and \p label is the info string used in the "expand" step.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002000 *
2001 * \param[in,out] generator The generator object to set up. It must
Gilles Peskine92587db2018-09-18 12:12:42 +02002002 * have been initialized to all-bits-zero,
2003 * a logical zero (`{0}`),
2004 * \c PSA_CRYPTO_GENERATOR_INIT or
2005 * psa_crypto_generator_init().
Gilles Peskineae32aac2018-11-30 14:39:32 +01002006 * \param handle Handle to the secret key.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002007 * \param alg The key derivation algorithm to compute
2008 * (\c PSA_ALG_XXX value such that
2009 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2010 * \param[in] salt Salt to use.
2011 * \param salt_length Size of the \p salt buffer in bytes.
2012 * \param[in] label Label to use.
2013 * \param label_length Size of the \p label buffer in bytes.
2014 * \param capacity The maximum number of bytes that the
2015 * generator will be able to provide.
2016 *
2017 * \retval #PSA_SUCCESS
2018 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002019 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineea0fb492018-07-12 17:17:20 +02002020 * \retval #PSA_ERROR_EMPTY_SLOT
2021 * \retval #PSA_ERROR_NOT_PERMITTED
2022 * \retval #PSA_ERROR_INVALID_ARGUMENT
2023 * \c key is not compatible with \c alg,
2024 * or \p capacity is too large for the specified algorithm and key.
2025 * \retval #PSA_ERROR_NOT_SUPPORTED
2026 * \c alg is not supported or is not a key derivation algorithm.
2027 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2028 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2029 * \retval #PSA_ERROR_HARDWARE_FAILURE
2030 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002031 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002032 * The library has not been previously initialized by psa_crypto_init().
2033 * It is implementation-dependent whether a failure to initialize
2034 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002035 */
2036psa_status_t psa_key_derivation(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002037 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02002038 psa_algorithm_t alg,
2039 const uint8_t *salt,
2040 size_t salt_length,
2041 const uint8_t *label,
2042 size_t label_length,
2043 size_t capacity);
2044
Gilles Peskine01d718c2018-09-18 12:01:02 +02002045/** Set up a key agreement operation.
2046 *
2047 * A key agreement algorithm takes two inputs: a private key \p private_key
2048 * a public key \p peer_key.
2049 * The result of this function is a byte generator which can
2050 * be used to produce keys and other cryptographic material.
2051 *
Gilles Peskine211a4362018-10-25 22:22:31 +02002052 * The resulting generator always has the maximum capacity permitted by
2053 * the algorithm.
2054 *
Gilles Peskine01d718c2018-09-18 12:01:02 +02002055 * \param[in,out] generator The generator object to set up. It must
2056 * have been initialized to all-bits-zero,
2057 * a logical zero (`{0}`),
2058 * \c PSA_CRYPTO_GENERATOR_INIT or
2059 * psa_crypto_generator_init().
Gilles Peskineae32aac2018-11-30 14:39:32 +01002060 * \param private_key Handle to the private key to use.
Gilles Peskined171e782018-11-15 17:46:21 +01002061 * \param[in] peer_key Public key of the peer. It must be
2062 * in the same format that psa_import_key()
2063 * accepts. The standard formats for public
2064 * keys are documented in the documentation
2065 * of psa_export_public_key().
Gilles Peskine01d718c2018-09-18 12:01:02 +02002066 * \param peer_key_length Size of \p peer_key in bytes.
2067 * \param alg The key agreement algorithm to compute
2068 * (\c PSA_ALG_XXX value such that
2069 * #PSA_ALG_IS_KEY_AGREEMENT(\p alg) is true).
2070 *
2071 * \retval #PSA_SUCCESS
2072 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002073 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine01d718c2018-09-18 12:01:02 +02002074 * \retval #PSA_ERROR_EMPTY_SLOT
2075 * \retval #PSA_ERROR_NOT_PERMITTED
2076 * \retval #PSA_ERROR_INVALID_ARGUMENT
2077 * \c private_key is not compatible with \c alg,
2078 * or \p peer_key is not valid for \c alg or not compatible with
2079 * \c private_key.
2080 * \retval #PSA_ERROR_NOT_SUPPORTED
2081 * \c alg is not supported or is not a key derivation algorithm.
2082 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2083 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2084 * \retval #PSA_ERROR_HARDWARE_FAILURE
2085 * \retval #PSA_ERROR_TAMPERING_DETECTED
2086 */
2087psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002088 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02002089 const uint8_t *peer_key,
2090 size_t peer_key_length,
2091 psa_algorithm_t alg);
2092
Gilles Peskineea0fb492018-07-12 17:17:20 +02002093/**@}*/
2094
Gilles Peskineedd76872018-07-20 17:42:05 +02002095/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002096 * @{
2097 */
2098
2099/**
2100 * \brief Generate random bytes.
2101 *
2102 * \warning This function **can** fail! Callers MUST check the return status
2103 * and MUST NOT use the content of the output buffer if the return
2104 * status is not #PSA_SUCCESS.
2105 *
2106 * \note To generate a key, use psa_generate_key() instead.
2107 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002108 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002109 * \param output_size Number of bytes to generate and output.
2110 *
Gilles Peskine28538492018-07-11 17:34:00 +02002111 * \retval #PSA_SUCCESS
2112 * \retval #PSA_ERROR_NOT_SUPPORTED
2113 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2114 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2115 * \retval #PSA_ERROR_HARDWARE_FAILURE
2116 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03002117 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002118 * The library has not been previously initialized by psa_crypto_init().
2119 * It is implementation-dependent whether a failure to initialize
2120 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002121 */
2122psa_status_t psa_generate_random(uint8_t *output,
2123 size_t output_size);
2124
Gilles Peskine4c317f42018-07-12 01:24:09 +02002125/** Extra parameters for RSA key generation.
2126 *
Gilles Peskinebe42f312018-07-13 14:38:15 +02002127 * You may pass a pointer to a structure of this type as the \c extra
Gilles Peskine4c317f42018-07-12 01:24:09 +02002128 * parameter to psa_generate_key().
2129 */
2130typedef struct {
Gilles Peskineedd76872018-07-20 17:42:05 +02002131 uint32_t e; /**< Public exponent value. Default: 65537. */
Gilles Peskine4c317f42018-07-12 01:24:09 +02002132} psa_generate_key_extra_rsa;
2133
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002134/**
2135 * \brief Generate a key or key pair.
2136 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002137 * \param handle Handle to the slot where the key will be stored.
2138 * This must be a valid slot for a key of the chosen
2139 * type: it must have been obtained by calling
2140 * psa_allocate_key() or psa_create_key() with the
2141 * correct \p type and with a maximum size that is
2142 * compatible with \p bits.
2143 * It must not contain any key material yet.
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002144 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2145 * \param bits Key size in bits.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002146 * \param[in] extra Extra parameters for key generation. The
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002147 * interpretation of this parameter depends on
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002148 * \p type. All types support \c NULL to use
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002149 * default parameters. Implementation that support
2150 * the generation of vendor-specific key types
2151 * that allow extra parameters shall document
2152 * the format of these extra parameters and
2153 * the default values. For standard parameters,
2154 * the meaning of \p extra is as follows:
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002155 * - For a symmetric key type (a type such
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002156 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
2157 * false), \p extra must be \c NULL.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002158 * - For an elliptic curve key type (a type
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002159 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
2160 * false), \p extra must be \c NULL.
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002161 * - For an RSA key (\p type is
2162 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
2163 * optional #psa_generate_key_extra_rsa structure
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002164 * specifying the public exponent. The
2165 * default public exponent used when \p extra
2166 * is \c NULL is 65537.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002167 * \param extra_size Size of the buffer that \p extra
2168 * points to, in bytes. Note that if \p extra is
2169 * \c NULL then \p extra_size must be zero.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002170 *
Gilles Peskine28538492018-07-11 17:34:00 +02002171 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01002172 * Success.
2173 * If the key is persistent, the key material and the key's metadata
2174 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002175 * \retval #PSA_ERROR_INVALID_HANDLE
2176 * \retval #PSA_ERROR_OCCUPIED_SLOT
2177 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +02002178 * \retval #PSA_ERROR_NOT_SUPPORTED
2179 * \retval #PSA_ERROR_INVALID_ARGUMENT
2180 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2181 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2182 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2183 * \retval #PSA_ERROR_HARDWARE_FAILURE
2184 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002185 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002186 * The library has not been previously initialized by psa_crypto_init().
2187 * It is implementation-dependent whether a failure to initialize
2188 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002189 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002190psa_status_t psa_generate_key(psa_key_handle_t handle,
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002191 psa_key_type_t type,
2192 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02002193 const void *extra,
2194 size_t extra_size);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002195
2196/**@}*/
2197
Gilles Peskinee59236f2018-01-27 23:32:46 +01002198#ifdef __cplusplus
2199}
2200#endif
2201
Gilles Peskine0cad07c2018-06-27 19:49:02 +02002202/* The file "crypto_sizes.h" contains definitions for size calculation
2203 * macros whose definitions are implementation-specific. */
2204#include "crypto_sizes.h"
2205
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002206/* The file "crypto_struct.h" contains definitions for
2207 * implementation-specific structs that are declared above. */
2208#include "crypto_struct.h"
2209
2210/* The file "crypto_extra.h" contains vendor-specific definitions. This
2211 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01002212#include "crypto_extra.h"
2213
2214#endif /* PSA_CRYPTO_H */