blob: 2046947ddc7045976ba2200c6e448b8e3a6f52b1 [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/* The file "crypto_types.h" declares types that encode errors,
57 * algorithms, key types, policies, etc. */
58#include "crypto_types.h"
59
60/* The file "crypto_values.h" declares macros to build and analyze values
61 * of integral types defined in "crypto_types.h". */
62#include "crypto_values.h"
63
64/** \defgroup initialization Library initialization
Gilles Peskinee59236f2018-01-27 23:32:46 +010065 * @{
66 */
67
68/**
Gilles Peskinee59236f2018-01-27 23:32:46 +010069 * \brief Library initialization.
70 *
71 * Applications must call this function before calling any other
72 * function in this module.
73 *
74 * Applications may call this function more than once. Once a call
75 * succeeds, subsequent calls are guaranteed to succeed.
76 *
itayzafrir18617092018-09-16 12:22:41 +030077 * If the application calls other functions before calling psa_crypto_init(),
78 * the behavior is undefined. Implementations are encouraged to either perform
79 * the operation as if the library had been initialized or to return
80 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
81 * implementations should not return a success status if the lack of
82 * initialization may have security implications, for example due to improper
83 * seeding of the random number generator.
84 *
Gilles Peskine28538492018-07-11 17:34:00 +020085 * \retval #PSA_SUCCESS
86 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
87 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
88 * \retval #PSA_ERROR_HARDWARE_FAILURE
89 * \retval #PSA_ERROR_TAMPERING_DETECTED
90 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +010091 */
92psa_status_t psa_crypto_init(void);
93
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010094/**@}*/
95
Gilles Peskine87a5e562019-04-17 12:28:25 +020096/** \defgroup attributes Key attributes
97 * @{
98 */
99
100/** The type of a structure containing key attributes.
101 *
102 * This is an opaque structure that can represent the metadata of a key
103 * object, including the key type and size, domain parameters, usage policies,
104 * location in storage, and any other similar information.
105 *
106 * The actual key material is not considered an attribute of a key.
107 * Key attributes do not contain information that is generally considered
108 * highly confidential.
Gilles Peskine20628592019-04-19 19:29:50 +0200109 *
110 * Before calling any function on a key attribute structure, the application
111 * must initialize it by any of the following means:
112 * - Set the structure to all-bits-zero, for example:
113 * \code
114 * psa_key_attributes_t attributes;
115 * memset(&attributes, 0, sizeof(attributes));
116 * \endcode
117 * - Initialize the structure to logical zero values, for example:
118 * \code
119 * psa_key_attributes_t attributes = {0};
120 * \endcode
121 * - Initialize the structure to the initializer #PSA_KEY_ATTRIBUTES_INIT,
122 * for example:
123 * \code
124 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
125 * \endcode
126 * - Assign the result of the function psa_key_attributes_init()
127 * to the structure, for example:
128 * \code
129 * psa_key_attributes_t attributes;
130 * attributes = psa_key_attributes_init();
131 * \endcode
132 *
133 * A freshly initialized attribute structure contains the following
134 * values:
135 *
136 * - lifetime: #PSA_KEY_LIFETIME_VOLATILE.
137 * - key identifier: unspecified.
138 * - type: \c 0, with no domain parameters.
139 * - key size: \c 0.
140 * - usage flags: \c 0.
141 * - algorithm: \c 0.
142 *
143 * A freshly initialized attribute structure does not own any auxiliary
144 * resources such as pointers to allocated memory, and therefore can be
145 * freed simply by freeing the memory allocated for the structure itself.
146 * This property still holds if the structure has only been modified
147 * by the following functions:
148 * - psa_make_key_persistent()
149 * - psa_set_key_type()
150 * - psa_set_key_usage_flags()
151 * - psa_set_key_algorithm()
152 * - psa_reset_key_attributes()
153 * - psa_get_key_attributes() on a key which has been created with
154 * attribute structure that itself did not contain auxiliary resources
155 *
156 * If the attribute structure has been modified with other functions,
157 * you must free auxiliary resources by calling psa_reset_key_attributes().
158 * The following functions may create auxiliary resouces:
159 * - psa_set_key_domain_parameters()
Gilles Peskine87a5e562019-04-17 12:28:25 +0200160 */
161typedef struct psa_key_attributes_s psa_key_attributes_t;
162
Gilles Peskine20628592019-04-19 19:29:50 +0200163/** Declare a key as persistent.
164 *
165 * This function does not access storage, it merely fills the attribute
166 * structure with given values. The persistent key will be written to
167 * storage when the attribute structure is passed to a key creation
168 * function such as psa_import_key(), psa_generate_key(),
169 * psa_generator_import_key() or psa_copy_key().
170 *
171 * This function overwrites any identifier and lifetime values
172 * previously set in \p attributes.
173 *
174 * This function may be declared as `static` (i.e. without external
175 * linkage). This function may be provided as a function-like macro,
176 * but in this case it must evaluate each of its arguments exactly once.
177 *
178 * \param[out] attributes The attribute structure to write to.
179 * \param id The persistent identifier for the key.
180 * \param lifetime The lifetime for the key.
181 * If this is #PSA_KEY_LIFETIME_VOLATILE, the
182 * key will be volatile, and \p id is ignored.
183 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200184static void psa_make_key_persistent(psa_key_attributes_t *attributes,
185 psa_key_id_t id,
186 psa_key_lifetime_t lifetime);
187
Gilles Peskine20628592019-04-19 19:29:50 +0200188/** Retrieve the key identifier from key attributes.
189 *
190 * This function may be declared as `static` (i.e. without external
191 * linkage). This function may be provided as a function-like macro,
192 * but in this case it must evaluate its argument exactly once.
193 *
194 * \param[in] attributes The key attribute structure to query.
195 *
196 * \return The persistent identifier stored in the attribute structure.
197 * This value is unspecified if the attribute structure declares
198 * the key as volatile.
199 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200200static psa_key_id_t psa_get_key_id(const psa_key_attributes_t *attributes);
201
Gilles Peskine20628592019-04-19 19:29:50 +0200202/** Retrieve the lifetime from key attributes.
203 *
204 * This function may be declared as `static` (i.e. without external
205 * linkage). This function may be provided as a function-like macro,
206 * but in this case it must evaluate its argument exactly once.
207 *
208 * \param[in] attributes The key attribute structure to query.
209 *
210 * \return The lifetime value stored in the attribute structure.
211 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200212static psa_key_lifetime_t psa_get_key_lifetime(
213 const psa_key_attributes_t *attributes);
214
Gilles Peskine20628592019-04-19 19:29:50 +0200215/** Declare usage flags for a key.
216 *
217 * Usage flags are part of a key's usage policy. They encode what
218 * kind of operations are permitted on the key. For more details,
219 * refer to the documentation of the type #psa_key_usage_t.
220 *
221 * This function overwrites any usage flags
222 * previously set in \p attributes.
223 *
224 * This function may be declared as `static` (i.e. without external
225 * linkage). This function may be provided as a function-like macro,
226 * but in this case it must evaluate each of its arguments exactly once.
227 *
228 * \param[out] attributes The attribute structure to write to.
229 * \param usage_flags The usage flags to write.
230 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200231static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
232 psa_key_usage_t usage_flags);
233
Gilles Peskine20628592019-04-19 19:29:50 +0200234/** Retrieve the usage flags from key attributes.
235 *
236 * This function may be declared as `static` (i.e. without external
237 * linkage). This function may be provided as a function-like macro,
238 * but in this case it must evaluate its argument exactly once.
239 *
240 * \param[in] attributes The key attribute structure to query.
241 *
242 * \return The usage flags stored in the attribute structure.
243 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200244static psa_key_usage_t psa_get_key_usage_flags(
245 const psa_key_attributes_t *attributes);
246
Gilles Peskine20628592019-04-19 19:29:50 +0200247/** Declare the permitted algorithm policy for a key.
248 *
249 * The permitted algorithm policy of a key encodes which algorithm or
250 * algorithms are permitted to be used with this key.
251 *
252 * This function overwrites any algorithm policy
253 * previously set in \p attributes.
254 *
255 * This function may be declared as `static` (i.e. without external
256 * linkage). This function may be provided as a function-like macro,
257 * but in this case it must evaluate each of its arguments exactly once.
258 *
259 * \param[out] attributes The attribute structure to write to.
260 * \param alg The permitted algorithm policy to write.
261 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200262static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
263 psa_algorithm_t alg);
264
Gilles Peskine20628592019-04-19 19:29:50 +0200265/** Retrieve the algorithm policy from key attributes.
266 *
267 * This function may be declared as `static` (i.e. without external
268 * linkage). This function may be provided as a function-like macro,
269 * but in this case it must evaluate its argument exactly once.
270 *
271 * \param[in] attributes The key attribute structure to query.
272 *
273 * \return The algorithm stored in the attribute structure.
274 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200275static psa_algorithm_t psa_get_key_algorithm(
276 const psa_key_attributes_t *attributes);
277
Gilles Peskine20628592019-04-19 19:29:50 +0200278/** Declare the type of a key.
279 *
280 * If a type requires domain parameters, you must call
281 * psa_set_key_domain_parameters() instead of this function.
282 *
283 * This function overwrites any key type and domain parameters
284 * previously set in \p attributes.
285 *
286 * This function may be declared as `static` (i.e. without external
287 * linkage). This function may be provided as a function-like macro,
288 * but in this case it must evaluate each of its arguments exactly once.
289 *
290 * \param[out] attributes The attribute structure to write to.
291 * \param type The key type to write.
292 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200293static void psa_set_key_type(psa_key_attributes_t *attributes,
294 psa_key_type_t type);
295
Gilles Peskine20628592019-04-19 19:29:50 +0200296/** Retrieve the key type from key attributes.
297 *
298 * This function may be declared as `static` (i.e. without external
299 * linkage). This function may be provided as a function-like macro,
300 * but in this case it must evaluate its argument exactly once.
301 *
302 * \param[in] attributes The key attribute structure to query.
303 *
304 * \return The key type stored in the attribute structure.
305 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200306static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
307
Gilles Peskine20628592019-04-19 19:29:50 +0200308/** Retrieve the key size from key attributes.
309 *
310 * This function may be declared as `static` (i.e. without external
311 * linkage). This function may be provided as a function-like macro,
312 * but in this case it must evaluate its argument exactly once.
313 *
314 * \param[in] attributes The key attribute structure to query.
315 *
316 * \return The key size stored in the attribute structure, in bits.
317 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200318static size_t psa_get_key_bits(const psa_key_attributes_t *attributes);
319
Gilles Peskine20628592019-04-19 19:29:50 +0200320/** Retrieve the attributes of a key.
321 *
322 * This function first resets the attribute structure as with
323 * psa_reset_key_attributes(). It then populates the attribute
324 * structure with the attributes of the given key.
325 *
326 * The attributes that were set when creating the key are reported in a
327 * semantically equivalent manner, not necessarily with the same
328 * numerical value or the same bit pattern. In this specification,
329 * all key types, usage flags, algorithms and lifetime values are
330 * equivalent only if they have the same numerical encoding, but this
331 * property may not hold in future versions of this specification or
332 * for implementation-specific values.
333 *
334 * In addition to the attributes that were set when creating the key,
335 * this function reports the following data:
336 * - The key size in bits, which can be retrieved with
337 * psa_get_key_bits().
338 *
339 * \param[in] handle Handle to the key to query.
340 * \param[in,out] attributes On success, the attributes of the key.
341 * On failure, equivalent to a
342 * freshly-initialized structure.
343 *
344 * \retval #PSA_SUCCESS
345 * \retval #PSA_ERROR_INVALID_HANDLE
346 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
347 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
348 */
Gilles Peskine4747d192019-04-17 15:05:45 +0200349psa_status_t psa_get_key_attributes(psa_key_handle_t handle,
350 psa_key_attributes_t *attributes);
351
Gilles Peskine20628592019-04-19 19:29:50 +0200352/** Reset a key attribute structure to a freshly initialized state.
353 *
354 * You must initialize the attribute structure as described in the
355 * documentation of the type #psa_key_attributes_t before calling this
356 * function. Once the structure has been initialized, you may call this
357 * function at any time.
358 *
359 * This function frees any auxiliary resources that the structure
360 * may contain.
361 *
362 * \param[in,out] attributes The attribute structure to reset.
363 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200364void psa_reset_key_attributes(psa_key_attributes_t *attributes);
Gilles Peskine4747d192019-04-17 15:05:45 +0200365
Gilles Peskine87a5e562019-04-17 12:28:25 +0200366/**@}*/
367
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100368/** \defgroup key_management Key management
369 * @{
370 */
371
Gilles Peskinef535eb22018-11-30 14:08:36 +0100372/** Open a handle to an existing persistent key.
373 *
374 * Open a handle to a key which was previously created with psa_create_key().
375 *
376 * \param lifetime The lifetime of the key. This designates a storage
377 * area where the key material is stored. This must not
378 * be #PSA_KEY_LIFETIME_VOLATILE.
379 * \param id The persistent identifier of the key.
380 * \param[out] handle On success, a handle to a key slot which contains
381 * the data and metadata loaded from the specified
382 * persistent location.
383 *
384 * \retval #PSA_SUCCESS
385 * Success. The application can now use the value of `*handle`
386 * to access the newly allocated key slot.
387 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
David Saadab4ecc272019-02-14 13:48:10 +0200388 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskinef535eb22018-11-30 14:08:36 +0100389 * \retval #PSA_ERROR_INVALID_ARGUMENT
390 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
391 * \retval #PSA_ERROR_INVALID_ARGUMENT
392 * \p id is invalid for the specified lifetime.
393 * \retval #PSA_ERROR_NOT_SUPPORTED
394 * \p lifetime is not supported.
395 * \retval #PSA_ERROR_NOT_PERMITTED
396 * The specified key exists, but the application does not have the
397 * permission to access it. Note that this specification does not
398 * define any way to create such a key, but it may be possible
399 * through implementation-specific means.
400 */
401psa_status_t psa_open_key(psa_key_lifetime_t lifetime,
402 psa_key_id_t id,
403 psa_key_handle_t *handle);
404
Gilles Peskinef535eb22018-11-30 14:08:36 +0100405/** Close a key handle.
406 *
407 * If the handle designates a volatile key, destroy the key material and
408 * free all associated resources, just like psa_destroy_key().
409 *
410 * If the handle designates a persistent key, free all resources associated
411 * with the key in volatile memory. The key slot in persistent storage is
412 * not affected and can be opened again later with psa_open_key().
413 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100414 * If the key is currently in use in a multipart operation,
415 * the multipart operation is aborted.
416 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100417 * \param handle The key handle to close.
418 *
419 * \retval #PSA_SUCCESS
420 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100421 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100422 */
423psa_status_t psa_close_key(psa_key_handle_t handle);
424
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100425/**@}*/
426
427/** \defgroup import_export Key import and export
428 * @{
429 */
430
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100431/**
432 * \brief Import a key in binary format.
433 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100434 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100435 * documentation of psa_export_public_key() for the format of public keys
436 * and to the documentation of psa_export_key() for the format for
437 * other key types.
438 *
439 * This specification supports a single format for each key type.
440 * Implementations may support other formats as long as the standard
441 * format is supported. Implementations that support other formats
442 * should ensure that the formats are clearly unambiguous so as to
443 * minimize the risk that an invalid input is accidentally interpreted
444 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100445 *
Gilles Peskine20628592019-04-19 19:29:50 +0200446 * \param[in] attributes The attributes for the new key.
447 * The key size field in \p attributes is
448 * ignored; the actual key size is determined
449 * from the \p data buffer.
450 * \param[out] handle On success, a handle to the newly created key.
451 * \c 0 on failure.
Gilles Peskinef7933932018-10-31 14:07:52 +0100452 * \param[in] data Buffer containing the key data. The content of this
Gilles Peskine20628592019-04-19 19:29:50 +0200453 * buffer is interpreted according to the type and,
454 * if applicable, domain parameters declared in
455 * \p attributes.
456 * All implementations must support at least the format
457 * described in the documentation
Gilles Peskinef7933932018-10-31 14:07:52 +0100458 * of psa_export_key() or psa_export_public_key() for
Gilles Peskine20628592019-04-19 19:29:50 +0200459 * the chosen type. Implementations may allow other
460 * formats, but should be conservative: implementations
461 * should err on the side of rejecting content if it
462 * may be erroneous (e.g. wrong type or truncated data).
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200463 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100464 *
Gilles Peskine28538492018-07-11 17:34:00 +0200465 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100466 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100467 * If the key is persistent, the key material and the key's metadata
468 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +0200469 * \retval #PSA_ERROR_ALREADY_EXISTS
470 * This is an attempt to create a persistent key, and there is
471 * already a persistent key with the given identifier.
Gilles Peskine28538492018-07-11 17:34:00 +0200472 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200473 * The key type or key size is not supported, either by the
Gilles Peskine20628592019-04-19 19:29:50 +0200474 * implementation in general or in this particular persistent location.
Gilles Peskine28538492018-07-11 17:34:00 +0200475 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200476 * The key attributes, as a whole, are invalid,
Gilles Peskine308b91d2018-02-08 09:47:44 +0100477 * or the key data is not correctly formatted.
Gilles Peskine28538492018-07-11 17:34:00 +0200478 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
479 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
480 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100481 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200482 * \retval #PSA_ERROR_HARDWARE_FAILURE
483 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300484 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300485 * The library has not been previously initialized by psa_crypto_init().
486 * It is implementation-dependent whether a failure to initialize
487 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100488 */
Gilles Peskine87a5e562019-04-17 12:28:25 +0200489psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
490 psa_key_handle_t *handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100491 const uint8_t *data,
492 size_t data_length);
493
494/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100495 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200496 *
497 * This function destroys the content of the key slot from both volatile
498 * memory and, if applicable, non-volatile storage. Implementations shall
499 * make a best effort to ensure that any previous content of the slot is
500 * unrecoverable.
501 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100502 * This function also erases any metadata such as policies and frees all
503 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200504 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100505 * If the key is currently in use in a multipart operation,
506 * the multipart operation is aborted.
507 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100508 * \param handle Handle to the key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100509 *
Gilles Peskine28538492018-07-11 17:34:00 +0200510 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200511 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200512 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200513 * The slot holds content and cannot be erased because it is
514 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100515 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200516 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200517 * There was an failure in communication with the cryptoprocessor.
518 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200519 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200520 * The storage is corrupted. Implementations shall make a best effort
521 * to erase key material even in this stage, however applications
522 * should be aware that it may be impossible to guarantee that the
523 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +0200524 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200525 * An unexpected condition which is not a storage corruption or
526 * a communication failure occurred. The cryptoprocessor may have
527 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300528 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300529 * The library has not been previously initialized by psa_crypto_init().
530 * It is implementation-dependent whether a failure to initialize
531 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100532 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100533psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100534
535/**
Jaeden Amero283dfd12019-01-11 12:06:22 +0000536 * \brief Set domain parameters for a key.
537 *
538 * Some key types require additional domain parameters to be set before import
539 * or generation of the key. The domain parameters can be set with this
540 * function or, for key generation, through the \c extra parameter of
541 * psa_generate_key().
542 *
543 * The format for the required domain parameters varies by the key type.
Jaeden Amero1308fb52019-01-11 13:50:43 +0000544 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY),
545 * the `Dss-Parms` format as defined by RFC 3279 &sect;2.3.2.
546 * ```
547 * Dss-Parms ::= SEQUENCE {
548 * p INTEGER,
549 * q INTEGER,
550 * g INTEGER
551 * }
552 * ```
Jaeden Amero8851c402019-01-11 14:20:03 +0000553 * - For Diffie-Hellman key exchange keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY), the
554 * `DomainParameters` format as defined by RFC 3279 &sect;2.3.3.
555 * ```
556 * DomainParameters ::= SEQUENCE {
557 * p INTEGER, -- odd prime, p=jq +1
558 * g INTEGER, -- generator, g
559 * q INTEGER, -- factor of p-1
560 * j INTEGER OPTIONAL, -- subgroup factor
561 * validationParms ValidationParms OPTIONAL
562 * }
563 * ValidationParms ::= SEQUENCE {
564 * seed BIT STRING,
565 * pgenCounter INTEGER
566 * }
567 * ```
Jaeden Amero283dfd12019-01-11 12:06:22 +0000568 *
Gilles Peskine3a74e002019-01-18 17:11:25 +0100569 * \param handle Handle to the slot where the key will be stored.
570 * This must be a valid slot for a key of the chosen
571 * type: it must have been obtained by calling
572 * psa_allocate_key() or psa_create_key() with the
573 * correct \p type and with a maximum size that is
574 * compatible with \p data. It must not contain
575 * key material yet.
576 * \param type Key type (a \c PSA_KEY_TYPE_XXX value). When
577 * subsequently creating key material into \p handle,
578 * the type must be compatible.
Jaeden Amero283dfd12019-01-11 12:06:22 +0000579 * \param[in] data Buffer containing the key domain parameters. The content
580 * of this buffer is interpreted according to \p type. of
581 * psa_export_key() or psa_export_public_key() for the
582 * chosen type.
583 * \param data_length Size of the \p data buffer in bytes.
584 *
585 * \retval #PSA_SUCCESS
586 * \retval #PSA_ERROR_INVALID_HANDLE
587 * \retval #PSA_ERROR_OCCUPIED_SLOT
588 * There is already a key in the specified slot.
589 * \retval #PSA_ERROR_INVALID_ARGUMENT
590 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
591 * \retval #PSA_ERROR_HARDWARE_FAILURE
592 * \retval #PSA_ERROR_TAMPERING_DETECTED
593 * \retval #PSA_ERROR_BAD_STATE
594 * The library has not been previously initialized by psa_crypto_init().
595 * It is implementation-dependent whether a failure to initialize
596 * results in this error code.
597 */
598psa_status_t psa_set_key_domain_parameters(psa_key_handle_t handle,
Gilles Peskine3a74e002019-01-18 17:11:25 +0100599 psa_key_type_t type,
Jaeden Amero283dfd12019-01-11 12:06:22 +0000600 const uint8_t *data,
601 size_t data_length);
602
603/**
604 * \brief Get domain parameters for a key.
605 *
606 * Get the domain parameters for a key with this function, if any. The format
607 * of the domain parameters written to \p data is specified in the
608 * documentation for psa_set_key_domain_parameters().
609 *
610 * \param handle Handle to the key to get domain parameters from.
611 * \param[out] data On success, the key domain parameters.
612 * \param data_size Size of the \p data buffer in bytes.
613 * \param[out] data_length On success, the number of bytes
614 * that make up the key domain parameters data.
615 *
616 * \retval #PSA_SUCCESS
617 * \retval #PSA_ERROR_INVALID_HANDLE
618 * \retval #PSA_ERROR_EMPTY_SLOT
619 * There is no key in the specified slot.
620 * \retval #PSA_ERROR_INVALID_ARGUMENT
621 * \retval #PSA_ERROR_NOT_SUPPORTED
622 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
623 * \retval #PSA_ERROR_HARDWARE_FAILURE
624 * \retval #PSA_ERROR_TAMPERING_DETECTED
625 * \retval #PSA_ERROR_BAD_STATE
626 * The library has not been previously initialized by psa_crypto_init().
627 * It is implementation-dependent whether a failure to initialize
628 * results in this error code.
629 */
630psa_status_t psa_get_key_domain_parameters(psa_key_handle_t handle,
631 uint8_t *data,
632 size_t data_size,
633 size_t *data_length);
634
635/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100636 * \brief Export a key in binary format.
637 *
638 * The output of this function can be passed to psa_import_key() to
639 * create an equivalent object.
640 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100641 * If the implementation of psa_import_key() supports other formats
642 * beyond the format specified here, the output from psa_export_key()
643 * must use the representation specified here, not the original
644 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100645 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100646 * For standard key types, the output format is as follows:
647 *
648 * - For symmetric keys (including MAC keys), the format is the
649 * raw bytes of the key.
650 * - For DES, the key data consists of 8 bytes. The parity bits must be
651 * correct.
652 * - For Triple-DES, the format is the concatenation of the
653 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100654 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200655 * is the non-encrypted DER encoding of the representation defined by
656 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
657 * ```
658 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200659 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200660 * modulus INTEGER, -- n
661 * publicExponent INTEGER, -- e
662 * privateExponent INTEGER, -- d
663 * prime1 INTEGER, -- p
664 * prime2 INTEGER, -- q
665 * exponent1 INTEGER, -- d mod (p-1)
666 * exponent2 INTEGER, -- d mod (q-1)
667 * coefficient INTEGER, -- (inverse of q) mod p
668 * }
669 * ```
Jaeden Amero1308fb52019-01-11 13:50:43 +0000670 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format is the
671 * representation of the private key `x` as a big-endian byte string. The
672 * length of the byte string is the private key size in bytes (leading zeroes
673 * are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200674 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +0100675 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100676 * a representation of the private value as a `ceiling(m/8)`-byte string
677 * where `m` is the bit size associated with the curve, i.e. the bit size
678 * of the order of the curve's coordinate field. This byte string is
679 * in little-endian order for Montgomery curves (curve types
680 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
681 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
682 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100683 * This is the content of the `privateKey` field of the `ECPrivateKey`
684 * format defined by RFC 5915.
Jaeden Amero8851c402019-01-11 14:20:03 +0000685 * - For Diffie-Hellman key exchange key pairs (#PSA_KEY_TYPE_DH_KEYPAIR), the
686 * format is the representation of the private key `x` as a big-endian byte
687 * string. The length of the byte string is the private key size in bytes
688 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200689 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
690 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100691 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100692 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200693 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200694 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200695 * \param[out] data_length On success, the number of bytes
696 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100697 *
Gilles Peskine28538492018-07-11 17:34:00 +0200698 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100699 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200700 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200701 * \retval #PSA_ERROR_NOT_PERMITTED
Darryl Green9e2d7a02018-07-24 16:33:30 +0100702 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200703 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
704 * The size of the \p data buffer is too small. You can determine a
705 * sufficient buffer size by calling
706 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
707 * where \c type is the key type
708 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200709 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
710 * \retval #PSA_ERROR_HARDWARE_FAILURE
711 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300712 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300713 * The library has not been previously initialized by psa_crypto_init().
714 * It is implementation-dependent whether a failure to initialize
715 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100716 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100717psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100718 uint8_t *data,
719 size_t data_size,
720 size_t *data_length);
721
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100722/**
723 * \brief Export a public key or the public part of a key pair in binary format.
724 *
725 * The output of this function can be passed to psa_import_key() to
726 * create an object that is equivalent to the public key.
727 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000728 * This specification supports a single format for each key type.
729 * Implementations may support other formats as long as the standard
730 * format is supported. Implementations that support other formats
731 * should ensure that the formats are clearly unambiguous so as to
732 * minimize the risk that an invalid input is accidentally interpreted
733 * according to a different format.
734 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000735 * For standard key types, the output format is as follows:
736 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
737 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
738 * ```
739 * RSAPublicKey ::= SEQUENCE {
740 * modulus INTEGER, -- n
741 * publicExponent INTEGER } -- e
742 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000743 * - For elliptic curve public keys (key types for which
744 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
745 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
746 * Let `m` be the bit size associated with the curve, i.e. the bit size of
747 * `q` for a curve over `F_q`. The representation consists of:
748 * - The byte 0x04;
749 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
750 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Jaeden Amero1308fb52019-01-11 13:50:43 +0000751 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY), the format is the
752 * representation of the public key `y = g^x mod p` as a big-endian byte
753 * string. The length of the byte string is the length of the base prime `p`
754 * in bytes.
Jaeden Amero8851c402019-01-11 14:20:03 +0000755 * - For Diffie-Hellman key exchange public keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY),
756 * the format is the representation of the public key `y = g^x mod p` as a
757 * big-endian byte string. The length of the byte string is the length of the
758 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100759 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100760 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200761 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200762 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200763 * \param[out] data_length On success, the number of bytes
764 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100765 *
Gilles Peskine28538492018-07-11 17:34:00 +0200766 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100767 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200768 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200769 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200770 * The key is neither a public key nor a key pair.
771 * \retval #PSA_ERROR_NOT_SUPPORTED
772 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
773 * The size of the \p data buffer is too small. You can determine a
774 * sufficient buffer size by calling
775 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
776 * where \c type is the key type
777 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200778 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
779 * \retval #PSA_ERROR_HARDWARE_FAILURE
780 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300781 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300782 * The library has not been previously initialized by psa_crypto_init().
783 * It is implementation-dependent whether a failure to initialize
784 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100785 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100786psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100787 uint8_t *data,
788 size_t data_size,
789 size_t *data_length);
790
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100791/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100792 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100793 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000794 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100795 * This function is primarily useful to copy a key from one location
796 * to another, since it populates a key using the material from
797 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200798 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100799 * In an implementation where slots have different ownerships,
Gilles Peskinebf7a98b2019-02-22 16:42:11 +0100800 * this function may be used to share a key with a different party,
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100801 * subject to implementation-defined restrictions on key sharing.
Gilles Peskine7e198532018-03-08 07:50:30 +0100802 *
Gilles Peskine20628592019-04-19 19:29:50 +0200803 * The resulting key may only be used in a way that conforms to
804 * both the policy of the original key and the policy specified in
805 * the \p attributes parameter:
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100806 * - The usage flags on the resulting key are the bitwise-and of the
Gilles Peskine20628592019-04-19 19:29:50 +0200807 * usage flags on the source policy and the usage flags in \p attributes.
808 * - If both allow the same algorithm or wildcard-based
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100809 * algorithm policy, the resulting key has the same algorithm policy.
Gilles Peskine20628592019-04-19 19:29:50 +0200810 * - If either of the policies allows an algorithm and the other policy
811 * allows a wildcard-based algorithm policy that includes this algorithm,
812 * the resulting key allows the same algorithm.
813 * - If the policies do not allow any algorithm in common, this function
814 * fails with the status #PSA_ERROR_INVALID_ARGUMENT.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200815 *
Gilles Peskine20628592019-04-19 19:29:50 +0200816 * The effect of this function on implementation-defined attributes is
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100817 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200818 *
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100819 * \param source_handle The key to copy. It must be a handle to an
820 * occupied slot.
Gilles Peskine20628592019-04-19 19:29:50 +0200821 * \param[in] attributes The attributes for the new key.
822 * They are used as follows:
823 * - The key type, key size and domain parameters
824 * are ignored. This information is copied
825 * from the source key.
826 * - The key location (the lifetime and, for
827 * persistent keys, the key identifier) is
828 * used directly.
829 * - The policy constraints (usage flags and
830 * algorithm policy) are combined from
831 * the source key and \p attributes so that
832 * both sets of restrictions apply, as
833 * described in the documentation of this function.
834 * \param[out] target_handle On success, a handle to the newly created key.
835 * \c 0 on failure.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200836 *
837 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100838 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine20628592019-04-19 19:29:50 +0200839 * \p source_handle is invalid.
David Saadab4ecc272019-02-14 13:48:10 +0200840 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +0200841 * This is an attempt to create a persistent key, and there is
842 * already a persistent key with the given identifier.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200843 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine20628592019-04-19 19:29:50 +0200844 * The lifetime or identifier in \p attributes are invalid.
845 * \retval #PSA_ERROR_INVALID_ARGUMENT
846 * The policy constraints on the source and specified in
847 * \p attributes are incompatible.
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100848 * \retval #PSA_ERROR_NOT_PERMITTED
849 * The source key is not exportable and its lifetime does not
850 * allow copying it to the target's lifetime.
851 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
852 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200853 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
854 * \retval #PSA_ERROR_HARDWARE_FAILURE
855 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100856 */
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100857psa_status_t psa_copy_key(psa_key_handle_t source_handle,
Gilles Peskine87a5e562019-04-17 12:28:25 +0200858 const psa_key_attributes_t *attributes,
859 psa_key_handle_t *target_handle);
Gilles Peskine20035e32018-02-03 22:44:14 +0100860
861/**@}*/
862
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100863/** \defgroup hash Message digests
864 * @{
865 */
866
Gilles Peskine69647a42019-01-14 20:18:12 +0100867/** Calculate the hash (digest) of a message.
868 *
869 * \note To verify the hash of a message against an
870 * expected value, use psa_hash_compare() instead.
871 *
872 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
873 * such that #PSA_ALG_IS_HASH(\p alg) is true).
874 * \param[in] input Buffer containing the message to hash.
875 * \param input_length Size of the \p input buffer in bytes.
876 * \param[out] hash Buffer where the hash is to be written.
877 * \param hash_size Size of the \p hash buffer in bytes.
878 * \param[out] hash_length On success, the number of bytes
879 * that make up the hash value. This is always
Gilles Peskined338b912019-02-15 13:01:41 +0100880 * #PSA_HASH_SIZE(\p alg).
Gilles Peskine69647a42019-01-14 20:18:12 +0100881 *
882 * \retval #PSA_SUCCESS
883 * Success.
884 * \retval #PSA_ERROR_NOT_SUPPORTED
885 * \p alg is not supported or is not a hash algorithm.
886 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
887 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
888 * \retval #PSA_ERROR_HARDWARE_FAILURE
889 * \retval #PSA_ERROR_TAMPERING_DETECTED
890 */
891psa_status_t psa_hash_compute(psa_algorithm_t alg,
892 const uint8_t *input,
893 size_t input_length,
894 uint8_t *hash,
895 size_t hash_size,
896 size_t *hash_length);
897
898/** Calculate the hash (digest) of a message and compare it with a
899 * reference value.
900 *
901 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
902 * such that #PSA_ALG_IS_HASH(\p alg) is true).
903 * \param[in] input Buffer containing the message to hash.
904 * \param input_length Size of the \p input buffer in bytes.
905 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100906 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100907 *
908 * \retval #PSA_SUCCESS
909 * The expected hash is identical to the actual hash of the input.
910 * \retval #PSA_ERROR_INVALID_SIGNATURE
911 * The hash of the message was calculated successfully, but it
912 * differs from the expected hash.
913 * \retval #PSA_ERROR_NOT_SUPPORTED
914 * \p alg is not supported or is not a hash algorithm.
915 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
916 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
917 * \retval #PSA_ERROR_HARDWARE_FAILURE
918 * \retval #PSA_ERROR_TAMPERING_DETECTED
919 */
920psa_status_t psa_hash_compare(psa_algorithm_t alg,
921 const uint8_t *input,
922 size_t input_length,
923 const uint8_t *hash,
924 const size_t hash_length);
925
Gilles Peskine308b91d2018-02-08 09:47:44 +0100926/** The type of the state data structure for multipart hash operations.
927 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000928 * Before calling any function on a hash operation object, the application must
929 * initialize it by any of the following means:
930 * - Set the structure to all-bits-zero, for example:
931 * \code
932 * psa_hash_operation_t operation;
933 * memset(&operation, 0, sizeof(operation));
934 * \endcode
935 * - Initialize the structure to logical zero values, for example:
936 * \code
937 * psa_hash_operation_t operation = {0};
938 * \endcode
939 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
940 * for example:
941 * \code
942 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
943 * \endcode
944 * - Assign the result of the function psa_hash_operation_init()
945 * to the structure, for example:
946 * \code
947 * psa_hash_operation_t operation;
948 * operation = psa_hash_operation_init();
949 * \endcode
950 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100951 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100952 * make any assumptions about the content of this structure except
953 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100954typedef struct psa_hash_operation_s psa_hash_operation_t;
955
Jaeden Amero6a25b412019-01-04 11:47:44 +0000956/** \def PSA_HASH_OPERATION_INIT
957 *
958 * This macro returns a suitable initializer for a hash operation object
959 * of type #psa_hash_operation_t.
960 */
961#ifdef __DOXYGEN_ONLY__
962/* This is an example definition for documentation purposes.
963 * Implementations should define a suitable value in `crypto_struct.h`.
964 */
965#define PSA_HASH_OPERATION_INIT {0}
966#endif
967
968/** Return an initial value for a hash operation object.
969 */
970static psa_hash_operation_t psa_hash_operation_init(void);
971
Gilles Peskinef45adda2019-01-14 18:29:18 +0100972/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100973 *
974 * The sequence of operations to calculate a hash (message digest)
975 * is as follows:
976 * -# Allocate an operation object which will be passed to all the functions
977 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000978 * -# Initialize the operation object with one of the methods described in the
979 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200980 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100981 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100982 * of the message each time. The hash that is calculated is the hash
983 * of the concatenation of these messages in order.
984 * -# To calculate the hash, call psa_hash_finish().
985 * To compare the hash with an expected value, call psa_hash_verify().
986 *
987 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000988 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100989 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200990 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100991 * eventually terminate the operation. The following events terminate an
992 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100993 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100994 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100995 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000996 * \param[in,out] operation The operation object to set up. It must have
997 * been initialized as per the documentation for
998 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200999 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
1000 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +01001001 *
Gilles Peskine28538492018-07-11 17:34:00 +02001002 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001003 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001004 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001005 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001006 * \retval #PSA_ERROR_BAD_STATE
1007 * The operation state is not valid (already set up and not
1008 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001009 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1010 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1011 * \retval #PSA_ERROR_HARDWARE_FAILURE
1012 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001013 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001014psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001015 psa_algorithm_t alg);
1016
Gilles Peskine308b91d2018-02-08 09:47:44 +01001017/** Add a message fragment to a multipart hash operation.
1018 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001019 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001020 *
1021 * If this function returns an error status, the operation becomes inactive.
1022 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001023 * \param[in,out] operation Active hash operation.
1024 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001025 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001026 *
Gilles Peskine28538492018-07-11 17:34:00 +02001027 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001028 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001029 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001030 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001031 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1032 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1033 * \retval #PSA_ERROR_HARDWARE_FAILURE
1034 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001035 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001036psa_status_t psa_hash_update(psa_hash_operation_t *operation,
1037 const uint8_t *input,
1038 size_t input_length);
1039
Gilles Peskine308b91d2018-02-08 09:47:44 +01001040/** Finish the calculation of the hash of a message.
1041 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001042 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001043 * This function calculates the hash of the message formed by concatenating
1044 * the inputs passed to preceding calls to psa_hash_update().
1045 *
1046 * When this function returns, the operation becomes inactive.
1047 *
1048 * \warning Applications should not call this function if they expect
1049 * a specific value for the hash. Call psa_hash_verify() instead.
1050 * Beware that comparing integrity or authenticity data such as
1051 * hash values with a function such as \c memcmp is risky
1052 * because the time taken by the comparison may leak information
1053 * about the hashed data which could allow an attacker to guess
1054 * a valid hash and thereby bypass security controls.
1055 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001056 * \param[in,out] operation Active hash operation.
1057 * \param[out] hash Buffer where the hash is to be written.
1058 * \param hash_size Size of the \p hash buffer in bytes.
1059 * \param[out] hash_length On success, the number of bytes
1060 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +02001061 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001062 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001063 *
Gilles Peskine28538492018-07-11 17:34:00 +02001064 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001065 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +02001066 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001067 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001068 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001069 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001070 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001071 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +02001072 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1073 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1074 * \retval #PSA_ERROR_HARDWARE_FAILURE
1075 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001076 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001077psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
1078 uint8_t *hash,
1079 size_t hash_size,
1080 size_t *hash_length);
1081
Gilles Peskine308b91d2018-02-08 09:47:44 +01001082/** Finish the calculation of the hash of a message and compare it with
1083 * an expected value.
1084 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001085 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001086 * This function calculates the hash of the message formed by concatenating
1087 * the inputs passed to preceding calls to psa_hash_update(). It then
1088 * compares the calculated hash with the expected hash passed as a
1089 * parameter to this function.
1090 *
1091 * When this function returns, the operation becomes inactive.
1092 *
Gilles Peskine19067982018-03-20 17:54:53 +01001093 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +01001094 * comparison between the actual hash and the expected hash is performed
1095 * in constant time.
1096 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001097 * \param[in,out] operation Active hash operation.
1098 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001099 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001100 *
Gilles Peskine28538492018-07-11 17:34:00 +02001101 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001102 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +02001103 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001104 * The hash of the message was calculated successfully, but it
1105 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +02001106 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001107 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +02001108 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1109 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1110 * \retval #PSA_ERROR_HARDWARE_FAILURE
1111 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001112 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001113psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
1114 const uint8_t *hash,
1115 size_t hash_length);
1116
Gilles Peskine308b91d2018-02-08 09:47:44 +01001117/** Abort a hash operation.
1118 *
Gilles Peskine308b91d2018-02-08 09:47:44 +01001119 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001120 * \p operation structure itself. Once aborted, the operation object
1121 * can be reused for another operation by calling
1122 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001123 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001124 * You may call this function any time after the operation object has
1125 * been initialized by any of the following methods:
1126 * - A call to psa_hash_setup(), whether it succeeds or not.
1127 * - Initializing the \c struct to all-bits-zero.
1128 * - Initializing the \c struct to logical zeros, e.g.
1129 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001130 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001131 * In particular, calling psa_hash_abort() after the operation has been
1132 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
1133 * psa_hash_verify() is safe and has no effect.
1134 *
1135 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001136 *
Gilles Peskine28538492018-07-11 17:34:00 +02001137 * \retval #PSA_SUCCESS
1138 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001139 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +02001140 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1141 * \retval #PSA_ERROR_HARDWARE_FAILURE
1142 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +01001143 */
1144psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001145
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001146/** Clone a hash operation.
1147 *
Gilles Peskinee43aa392019-01-21 14:50:37 +01001148 * This function copies the state of an ongoing hash operation to
1149 * a new operation object. In other words, this function is equivalent
1150 * to calling psa_hash_setup() on \p target_operation with the same
1151 * algorithm that \p source_operation was set up for, then
1152 * psa_hash_update() on \p target_operation with the same input that
1153 * that was passed to \p source_operation. After this function returns, the
1154 * two objects are independent, i.e. subsequent calls involving one of
1155 * the objects do not affect the other object.
1156 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001157 * \param[in] source_operation The active hash operation to clone.
1158 * \param[in,out] target_operation The operation object to set up.
1159 * It must be initialized but not active.
1160 *
1161 * \retval #PSA_SUCCESS
1162 * \retval #PSA_ERROR_BAD_STATE
1163 * \p source_operation is not an active hash operation.
1164 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +01001165 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001166 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1167 * \retval #PSA_ERROR_HARDWARE_FAILURE
1168 * \retval #PSA_ERROR_TAMPERING_DETECTED
1169 */
1170psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1171 psa_hash_operation_t *target_operation);
1172
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001173/**@}*/
1174
Gilles Peskine8c9def32018-02-08 10:02:12 +01001175/** \defgroup MAC Message authentication codes
1176 * @{
1177 */
1178
Gilles Peskine69647a42019-01-14 20:18:12 +01001179/** Calculate the MAC (message authentication code) of a message.
1180 *
1181 * \note To verify the MAC of a message against an
1182 * expected value, use psa_mac_verify() instead.
1183 * Beware that comparing integrity or authenticity data such as
1184 * MAC values with a function such as \c memcmp is risky
1185 * because the time taken by the comparison may leak information
1186 * about the MAC value which could allow an attacker to guess
1187 * a valid MAC and thereby bypass security controls.
1188 *
1189 * \param handle Handle to the key to use for the operation.
1190 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001191 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001192 * \param[in] input Buffer containing the input message.
1193 * \param input_length Size of the \p input buffer in bytes.
1194 * \param[out] mac Buffer where the MAC value is to be written.
1195 * \param mac_size Size of the \p mac buffer in bytes.
1196 * \param[out] mac_length On success, the number of bytes
Gilles Peskined338b912019-02-15 13:01:41 +01001197 * that make up the MAC value.
Gilles Peskine69647a42019-01-14 20:18:12 +01001198 *
1199 * \retval #PSA_SUCCESS
1200 * Success.
1201 * \retval #PSA_ERROR_INVALID_HANDLE
1202 * \retval #PSA_ERROR_EMPTY_SLOT
1203 * \retval #PSA_ERROR_NOT_PERMITTED
1204 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001205 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001206 * \retval #PSA_ERROR_NOT_SUPPORTED
1207 * \p alg is not supported or is not a MAC algorithm.
1208 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1209 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1210 * \retval #PSA_ERROR_HARDWARE_FAILURE
1211 * \retval #PSA_ERROR_TAMPERING_DETECTED
1212 * \retval #PSA_ERROR_BAD_STATE
1213 * The library has not been previously initialized by psa_crypto_init().
1214 * It is implementation-dependent whether a failure to initialize
1215 * results in this error code.
1216 */
1217psa_status_t psa_mac_compute(psa_key_handle_t handle,
1218 psa_algorithm_t alg,
1219 const uint8_t *input,
1220 size_t input_length,
1221 uint8_t *mac,
1222 size_t mac_size,
1223 size_t *mac_length);
1224
1225/** Calculate the MAC of a message and compare it with a reference value.
1226 *
1227 * \param handle Handle to the key to use for the operation.
1228 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001229 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine69647a42019-01-14 20:18:12 +01001230 * \param[in] input Buffer containing the input message.
1231 * \param input_length Size of the \p input buffer in bytes.
1232 * \param[out] mac Buffer containing the expected MAC value.
1233 * \param mac_length Size of the \p mac buffer in bytes.
1234 *
1235 * \retval #PSA_SUCCESS
1236 * The expected MAC is identical to the actual MAC of the input.
1237 * \retval #PSA_ERROR_INVALID_SIGNATURE
1238 * The MAC of the message was calculated successfully, but it
1239 * differs from the expected value.
1240 * \retval #PSA_ERROR_INVALID_HANDLE
1241 * \retval #PSA_ERROR_EMPTY_SLOT
1242 * \retval #PSA_ERROR_NOT_PERMITTED
1243 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001244 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001245 * \retval #PSA_ERROR_NOT_SUPPORTED
1246 * \p alg is not supported or is not a MAC algorithm.
1247 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1248 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1249 * \retval #PSA_ERROR_HARDWARE_FAILURE
1250 * \retval #PSA_ERROR_TAMPERING_DETECTED
1251 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001252psa_status_t psa_mac_verify(psa_key_handle_t handle,
1253 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001254 const uint8_t *input,
1255 size_t input_length,
1256 const uint8_t *mac,
1257 const size_t mac_length);
1258
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001259/** The type of the state data structure for multipart MAC operations.
1260 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001261 * Before calling any function on a MAC operation object, the application must
1262 * initialize it by any of the following means:
1263 * - Set the structure to all-bits-zero, for example:
1264 * \code
1265 * psa_mac_operation_t operation;
1266 * memset(&operation, 0, sizeof(operation));
1267 * \endcode
1268 * - Initialize the structure to logical zero values, for example:
1269 * \code
1270 * psa_mac_operation_t operation = {0};
1271 * \endcode
1272 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1273 * for example:
1274 * \code
1275 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1276 * \endcode
1277 * - Assign the result of the function psa_mac_operation_init()
1278 * to the structure, for example:
1279 * \code
1280 * psa_mac_operation_t operation;
1281 * operation = psa_mac_operation_init();
1282 * \endcode
1283 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001284 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001285 * make any assumptions about the content of this structure except
1286 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001287typedef struct psa_mac_operation_s psa_mac_operation_t;
1288
Jaeden Amero769ce272019-01-04 11:48:03 +00001289/** \def PSA_MAC_OPERATION_INIT
1290 *
1291 * This macro returns a suitable initializer for a MAC operation object of type
1292 * #psa_mac_operation_t.
1293 */
1294#ifdef __DOXYGEN_ONLY__
1295/* This is an example definition for documentation purposes.
1296 * Implementations should define a suitable value in `crypto_struct.h`.
1297 */
1298#define PSA_MAC_OPERATION_INIT {0}
1299#endif
1300
1301/** Return an initial value for a MAC operation object.
1302 */
1303static psa_mac_operation_t psa_mac_operation_init(void);
1304
Gilles Peskinef45adda2019-01-14 18:29:18 +01001305/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001306 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001307 * This function sets up the calculation of the MAC
1308 * (message authentication code) of a byte string.
1309 * To verify the MAC of a message against an
1310 * expected value, use psa_mac_verify_setup() instead.
1311 *
1312 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001313 * -# Allocate an operation object which will be passed to all the functions
1314 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001315 * -# Initialize the operation object with one of the methods described in the
1316 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001317 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001318 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1319 * of the message each time. The MAC that is calculated is the MAC
1320 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001321 * -# At the end of the message, call psa_mac_sign_finish() to finish
1322 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001323 *
1324 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001325 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001326 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001327 * After a successful call to psa_mac_sign_setup(), the application must
1328 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001329 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001330 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001331 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001332 * \param[in,out] operation The operation object to set up. It must have
1333 * been initialized as per the documentation for
1334 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001335 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001336 * It must remain valid until the operation
1337 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001338 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
Gilles Peskine63f79302019-02-15 13:01:17 +01001339 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001340 *
Gilles Peskine28538492018-07-11 17:34:00 +02001341 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001342 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001343 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001344 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001345 * \retval #PSA_ERROR_NOT_PERMITTED
1346 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001347 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001348 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001349 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001350 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1351 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1352 * \retval #PSA_ERROR_HARDWARE_FAILURE
1353 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001354 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001355 * The operation state is not valid (already set up and not
1356 * subsequently completed).
1357 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001358 * The library has not been previously initialized by psa_crypto_init().
1359 * It is implementation-dependent whether a failure to initialize
1360 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001361 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001362psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001363 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001364 psa_algorithm_t alg);
1365
Gilles Peskinef45adda2019-01-14 18:29:18 +01001366/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001367 *
1368 * This function sets up the verification of the MAC
1369 * (message authentication code) of a byte string against an expected value.
1370 *
1371 * The sequence of operations to verify a MAC is as follows:
1372 * -# Allocate an operation object which will be passed to all the functions
1373 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001374 * -# Initialize the operation object with one of the methods described in the
1375 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001376 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001377 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1378 * of the message each time. The MAC that is calculated is the MAC
1379 * of the concatenation of these messages in order.
1380 * -# At the end of the message, call psa_mac_verify_finish() to finish
1381 * calculating the actual MAC of the message and verify it against
1382 * the expected value.
1383 *
1384 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001385 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001386 *
1387 * After a successful call to psa_mac_verify_setup(), the application must
1388 * eventually terminate the operation through one of the following methods:
1389 * - A failed call to psa_mac_update().
1390 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1391 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001392 * \param[in,out] operation The operation object to set up. It must have
1393 * been initialized as per the documentation for
1394 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001395 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001396 * It must remain valid until the operation
1397 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001398 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1399 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001400 *
Gilles Peskine28538492018-07-11 17:34:00 +02001401 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001402 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001403 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001404 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001405 * \retval #PSA_ERROR_NOT_PERMITTED
1406 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001407 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001408 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001409 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001410 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1411 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1412 * \retval #PSA_ERROR_HARDWARE_FAILURE
1413 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001414 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001415 * The operation state is not valid (already set up and not
1416 * subsequently completed).
1417 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001418 * The library has not been previously initialized by psa_crypto_init().
1419 * It is implementation-dependent whether a failure to initialize
1420 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001421 */
1422psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001423 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001424 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001425
Gilles Peskinedcd14942018-07-12 00:30:52 +02001426/** Add a message fragment to a multipart MAC operation.
1427 *
1428 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1429 * before calling this function.
1430 *
1431 * If this function returns an error status, the operation becomes inactive.
1432 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001433 * \param[in,out] operation Active MAC operation.
1434 * \param[in] input Buffer containing the message fragment to add to
1435 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001436 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001437 *
1438 * \retval #PSA_SUCCESS
1439 * Success.
1440 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001441 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001442 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1443 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1444 * \retval #PSA_ERROR_HARDWARE_FAILURE
1445 * \retval #PSA_ERROR_TAMPERING_DETECTED
1446 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001447psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1448 const uint8_t *input,
1449 size_t input_length);
1450
Gilles Peskinedcd14942018-07-12 00:30:52 +02001451/** Finish the calculation of the MAC of a message.
1452 *
1453 * The application must call psa_mac_sign_setup() before calling this function.
1454 * This function calculates the MAC of the message formed by concatenating
1455 * the inputs passed to preceding calls to psa_mac_update().
1456 *
1457 * When this function returns, the operation becomes inactive.
1458 *
1459 * \warning Applications should not call this function if they expect
1460 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1461 * Beware that comparing integrity or authenticity data such as
1462 * MAC values with a function such as \c memcmp is risky
1463 * because the time taken by the comparison may leak information
1464 * about the MAC value which could allow an attacker to guess
1465 * a valid MAC and thereby bypass security controls.
1466 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001467 * \param[in,out] operation Active MAC operation.
1468 * \param[out] mac Buffer where the MAC value is to be written.
1469 * \param mac_size Size of the \p mac buffer in bytes.
1470 * \param[out] mac_length On success, the number of bytes
1471 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001472 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001473 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001474 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001475 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001476 *
1477 * \retval #PSA_SUCCESS
1478 * Success.
1479 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001480 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001481 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001482 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001483 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1484 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1485 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1486 * \retval #PSA_ERROR_HARDWARE_FAILURE
1487 * \retval #PSA_ERROR_TAMPERING_DETECTED
1488 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001489psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1490 uint8_t *mac,
1491 size_t mac_size,
1492 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001493
Gilles Peskinedcd14942018-07-12 00:30:52 +02001494/** Finish the calculation of the MAC of a message and compare it with
1495 * an expected value.
1496 *
1497 * The application must call psa_mac_verify_setup() before calling this function.
1498 * This function calculates the MAC of the message formed by concatenating
1499 * the inputs passed to preceding calls to psa_mac_update(). It then
1500 * compares the calculated MAC with the expected MAC passed as a
1501 * parameter to this function.
1502 *
1503 * When this function returns, the operation becomes inactive.
1504 *
1505 * \note Implementations shall make the best effort to ensure that the
1506 * comparison between the actual MAC and the expected MAC is performed
1507 * in constant time.
1508 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001509 * \param[in,out] operation Active MAC operation.
1510 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001511 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001512 *
1513 * \retval #PSA_SUCCESS
1514 * The expected MAC is identical to the actual MAC of the message.
1515 * \retval #PSA_ERROR_INVALID_SIGNATURE
1516 * The MAC of the message was calculated successfully, but it
1517 * differs from the expected MAC.
1518 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001519 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001520 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1521 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1522 * \retval #PSA_ERROR_HARDWARE_FAILURE
1523 * \retval #PSA_ERROR_TAMPERING_DETECTED
1524 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001525psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1526 const uint8_t *mac,
1527 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001528
Gilles Peskinedcd14942018-07-12 00:30:52 +02001529/** Abort a MAC operation.
1530 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001531 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001532 * \p operation structure itself. Once aborted, the operation object
1533 * can be reused for another operation by calling
1534 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001535 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001536 * You may call this function any time after the operation object has
1537 * been initialized by any of the following methods:
1538 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1539 * it succeeds or not.
1540 * - Initializing the \c struct to all-bits-zero.
1541 * - Initializing the \c struct to logical zeros, e.g.
1542 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001543 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001544 * In particular, calling psa_mac_abort() after the operation has been
1545 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1546 * psa_mac_verify_finish() is safe and has no effect.
1547 *
1548 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001549 *
1550 * \retval #PSA_SUCCESS
1551 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001552 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001553 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1554 * \retval #PSA_ERROR_HARDWARE_FAILURE
1555 * \retval #PSA_ERROR_TAMPERING_DETECTED
1556 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001557psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1558
1559/**@}*/
1560
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001561/** \defgroup cipher Symmetric ciphers
1562 * @{
1563 */
1564
Gilles Peskine69647a42019-01-14 20:18:12 +01001565/** Encrypt a message using a symmetric cipher.
1566 *
1567 * This function encrypts a message with a random IV (initialization
1568 * vector).
1569 *
1570 * \param handle Handle to the key to use for the operation.
1571 * It must remain valid until the operation
1572 * terminates.
1573 * \param alg The cipher algorithm to compute
1574 * (\c PSA_ALG_XXX value such that
1575 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1576 * \param[in] input Buffer containing the message to encrypt.
1577 * \param input_length Size of the \p input buffer in bytes.
1578 * \param[out] output Buffer where the output is to be written.
1579 * The output contains the IV followed by
1580 * the ciphertext proper.
1581 * \param output_size Size of the \p output buffer in bytes.
1582 * \param[out] output_length On success, the number of bytes
1583 * that make up the output.
1584 *
1585 * \retval #PSA_SUCCESS
1586 * Success.
1587 * \retval #PSA_ERROR_INVALID_HANDLE
1588 * \retval #PSA_ERROR_EMPTY_SLOT
1589 * \retval #PSA_ERROR_NOT_PERMITTED
1590 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001591 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001592 * \retval #PSA_ERROR_NOT_SUPPORTED
1593 * \p alg is not supported or is not a cipher algorithm.
1594 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1595 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1596 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1597 * \retval #PSA_ERROR_HARDWARE_FAILURE
1598 * \retval #PSA_ERROR_TAMPERING_DETECTED
1599 */
1600psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1601 psa_algorithm_t alg,
1602 const uint8_t *input,
1603 size_t input_length,
1604 uint8_t *output,
1605 size_t output_size,
1606 size_t *output_length);
1607
1608/** Decrypt a message using a symmetric cipher.
1609 *
1610 * This function decrypts a message encrypted with a symmetric cipher.
1611 *
1612 * \param handle Handle to the key to use for the operation.
1613 * It must remain valid until the operation
1614 * terminates.
1615 * \param alg The cipher algorithm to compute
1616 * (\c PSA_ALG_XXX value such that
1617 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1618 * \param[in] input Buffer containing the message to decrypt.
1619 * This consists of the IV followed by the
1620 * ciphertext proper.
1621 * \param input_length Size of the \p input buffer in bytes.
1622 * \param[out] output Buffer where the plaintext is to be written.
1623 * \param output_size Size of the \p output buffer in bytes.
1624 * \param[out] output_length On success, the number of bytes
1625 * that make up the output.
1626 *
1627 * \retval #PSA_SUCCESS
1628 * Success.
1629 * \retval #PSA_ERROR_INVALID_HANDLE
1630 * \retval #PSA_ERROR_EMPTY_SLOT
1631 * \retval #PSA_ERROR_NOT_PERMITTED
1632 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001633 * \p handle is not compatible with \p alg.
Gilles Peskine69647a42019-01-14 20:18:12 +01001634 * \retval #PSA_ERROR_NOT_SUPPORTED
1635 * \p alg is not supported or is not a cipher algorithm.
1636 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1637 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1638 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1639 * \retval #PSA_ERROR_HARDWARE_FAILURE
1640 * \retval #PSA_ERROR_TAMPERING_DETECTED
1641 */
1642psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1643 psa_algorithm_t alg,
1644 const uint8_t *input,
1645 size_t input_length,
1646 uint8_t *output,
1647 size_t output_size,
1648 size_t *output_length);
1649
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001650/** The type of the state data structure for multipart cipher operations.
1651 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001652 * Before calling any function on a cipher operation object, the application
1653 * must initialize it by any of the following means:
1654 * - Set the structure to all-bits-zero, for example:
1655 * \code
1656 * psa_cipher_operation_t operation;
1657 * memset(&operation, 0, sizeof(operation));
1658 * \endcode
1659 * - Initialize the structure to logical zero values, for example:
1660 * \code
1661 * psa_cipher_operation_t operation = {0};
1662 * \endcode
1663 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1664 * for example:
1665 * \code
1666 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1667 * \endcode
1668 * - Assign the result of the function psa_cipher_operation_init()
1669 * to the structure, for example:
1670 * \code
1671 * psa_cipher_operation_t operation;
1672 * operation = psa_cipher_operation_init();
1673 * \endcode
1674 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001675 * This is an implementation-defined \c struct. Applications should not
1676 * make any assumptions about the content of this structure except
1677 * as directed by the documentation of a specific implementation. */
1678typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1679
Jaeden Amero5bae2272019-01-04 11:48:27 +00001680/** \def PSA_CIPHER_OPERATION_INIT
1681 *
1682 * This macro returns a suitable initializer for a cipher operation object of
1683 * type #psa_cipher_operation_t.
1684 */
1685#ifdef __DOXYGEN_ONLY__
1686/* This is an example definition for documentation purposes.
1687 * Implementations should define a suitable value in `crypto_struct.h`.
1688 */
1689#define PSA_CIPHER_OPERATION_INIT {0}
1690#endif
1691
1692/** Return an initial value for a cipher operation object.
1693 */
1694static psa_cipher_operation_t psa_cipher_operation_init(void);
1695
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001696/** Set the key for a multipart symmetric encryption operation.
1697 *
1698 * The sequence of operations to encrypt a message with a symmetric cipher
1699 * is as follows:
1700 * -# Allocate an operation object which will be passed to all the functions
1701 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001702 * -# Initialize the operation object with one of the methods described in the
1703 * documentation for #psa_cipher_operation_t, e.g.
1704 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001705 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001706 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001707 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001708 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001709 * requires a specific IV value.
1710 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1711 * of the message each time.
1712 * -# Call psa_cipher_finish().
1713 *
1714 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001715 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001716 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001717 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001718 * eventually terminate the operation. The following events terminate an
1719 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001720 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001721 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001722 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001723 * \param[in,out] operation The operation object to set up. It must have
1724 * been initialized as per the documentation for
1725 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001726 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001727 * It must remain valid until the operation
1728 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001729 * \param alg The cipher algorithm to compute
1730 * (\c PSA_ALG_XXX value such that
1731 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001732 *
Gilles Peskine28538492018-07-11 17:34:00 +02001733 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001734 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001735 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001736 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001737 * \retval #PSA_ERROR_NOT_PERMITTED
1738 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001739 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001740 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001741 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001742 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1743 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1744 * \retval #PSA_ERROR_HARDWARE_FAILURE
1745 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001746 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001747 * The operation state is not valid (already set up and not
1748 * subsequently completed).
1749 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001750 * The library has not been previously initialized by psa_crypto_init().
1751 * It is implementation-dependent whether a failure to initialize
1752 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001753 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001754psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001755 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001756 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001757
1758/** Set the key for a multipart symmetric decryption operation.
1759 *
1760 * The sequence of operations to decrypt a message with a symmetric cipher
1761 * is as follows:
1762 * -# Allocate an operation object which will be passed to all the functions
1763 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001764 * -# Initialize the operation object with one of the methods described in the
1765 * documentation for #psa_cipher_operation_t, e.g.
1766 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001767 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001768 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001769 * decryption. If the IV is prepended to the ciphertext, you can call
1770 * psa_cipher_update() on a buffer containing the IV followed by the
1771 * beginning of the message.
1772 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1773 * of the message each time.
1774 * -# Call psa_cipher_finish().
1775 *
1776 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001777 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001778 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001779 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001780 * eventually terminate the operation. The following events terminate an
1781 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001782 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001783 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001784 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001785 * \param[in,out] operation The operation object to set up. It must have
1786 * been initialized as per the documentation for
1787 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001788 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001789 * It must remain valid until the operation
1790 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001791 * \param alg The cipher algorithm to compute
1792 * (\c PSA_ALG_XXX value such that
1793 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001794 *
Gilles Peskine28538492018-07-11 17:34:00 +02001795 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001796 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001797 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001798 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001799 * \retval #PSA_ERROR_NOT_PERMITTED
1800 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01001801 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001802 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001803 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001804 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1805 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1806 * \retval #PSA_ERROR_HARDWARE_FAILURE
1807 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001808 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001809 * The operation state is not valid (already set up and not
1810 * subsequently completed).
1811 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001812 * The library has not been previously initialized by psa_crypto_init().
1813 * It is implementation-dependent whether a failure to initialize
1814 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001815 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001816psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001817 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001818 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001819
Gilles Peskinedcd14942018-07-12 00:30:52 +02001820/** Generate an IV for a symmetric encryption operation.
1821 *
1822 * This function generates a random IV (initialization vector), nonce
1823 * or initial counter value for the encryption operation as appropriate
1824 * for the chosen algorithm, key type and key size.
1825 *
1826 * The application must call psa_cipher_encrypt_setup() before
1827 * calling this function.
1828 *
1829 * If this function returns an error status, the operation becomes inactive.
1830 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001831 * \param[in,out] operation Active cipher operation.
1832 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001833 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001834 * \param[out] iv_length On success, the number of bytes of the
1835 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001836 *
1837 * \retval #PSA_SUCCESS
1838 * Success.
1839 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001840 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001841 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001842 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001843 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1844 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1845 * \retval #PSA_ERROR_HARDWARE_FAILURE
1846 * \retval #PSA_ERROR_TAMPERING_DETECTED
1847 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001848psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1849 unsigned char *iv,
1850 size_t iv_size,
1851 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001852
Gilles Peskinedcd14942018-07-12 00:30:52 +02001853/** Set the IV for a symmetric encryption or decryption operation.
1854 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001855 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001856 * or initial counter value for the encryption or decryption operation.
1857 *
1858 * The application must call psa_cipher_encrypt_setup() before
1859 * calling this function.
1860 *
1861 * If this function returns an error status, the operation becomes inactive.
1862 *
1863 * \note When encrypting, applications should use psa_cipher_generate_iv()
1864 * instead of this function, unless implementing a protocol that requires
1865 * a non-random IV.
1866 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001867 * \param[in,out] operation Active cipher operation.
1868 * \param[in] iv Buffer containing the IV to use.
1869 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001870 *
1871 * \retval #PSA_SUCCESS
1872 * Success.
1873 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001874 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001875 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001876 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001877 * or the chosen algorithm does not use an IV.
1878 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1879 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1880 * \retval #PSA_ERROR_HARDWARE_FAILURE
1881 * \retval #PSA_ERROR_TAMPERING_DETECTED
1882 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001883psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1884 const unsigned char *iv,
1885 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001886
Gilles Peskinedcd14942018-07-12 00:30:52 +02001887/** Encrypt or decrypt a message fragment in an active cipher operation.
1888 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001889 * Before calling this function, you must:
1890 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1891 * The choice of setup function determines whether this function
1892 * encrypts or decrypts its input.
1893 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1894 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001895 *
1896 * If this function returns an error status, the operation becomes inactive.
1897 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001898 * \param[in,out] operation Active cipher operation.
1899 * \param[in] input Buffer containing the message fragment to
1900 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001901 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001902 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001903 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001904 * \param[out] output_length On success, the number of bytes
1905 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001906 *
1907 * \retval #PSA_SUCCESS
1908 * Success.
1909 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001910 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001911 * not set, or already completed).
1912 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1913 * The size of the \p output buffer is too small.
1914 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1915 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1916 * \retval #PSA_ERROR_HARDWARE_FAILURE
1917 * \retval #PSA_ERROR_TAMPERING_DETECTED
1918 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001919psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1920 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001921 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001922 unsigned char *output,
1923 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001924 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001925
Gilles Peskinedcd14942018-07-12 00:30:52 +02001926/** Finish encrypting or decrypting a message in a cipher operation.
1927 *
1928 * The application must call psa_cipher_encrypt_setup() or
1929 * psa_cipher_decrypt_setup() before calling this function. The choice
1930 * of setup function determines whether this function encrypts or
1931 * decrypts its input.
1932 *
1933 * This function finishes the encryption or decryption of the message
1934 * formed by concatenating the inputs passed to preceding calls to
1935 * psa_cipher_update().
1936 *
1937 * When this function returns, the operation becomes inactive.
1938 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001939 * \param[in,out] operation Active cipher operation.
1940 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001941 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001942 * \param[out] output_length On success, the number of bytes
1943 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001944 *
1945 * \retval #PSA_SUCCESS
1946 * Success.
1947 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001948 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001949 * not set, or already completed).
1950 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1951 * The size of the \p output buffer is too small.
1952 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1953 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1954 * \retval #PSA_ERROR_HARDWARE_FAILURE
1955 * \retval #PSA_ERROR_TAMPERING_DETECTED
1956 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001957psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001958 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001959 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001960 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001961
Gilles Peskinedcd14942018-07-12 00:30:52 +02001962/** Abort a cipher operation.
1963 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001964 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001965 * \p operation structure itself. Once aborted, the operation object
1966 * can be reused for another operation by calling
1967 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001968 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001969 * You may call this function any time after the operation object has
1970 * been initialized by any of the following methods:
1971 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1972 * whether it succeeds or not.
1973 * - Initializing the \c struct to all-bits-zero.
1974 * - Initializing the \c struct to logical zeros, e.g.
1975 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001976 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001977 * In particular, calling psa_cipher_abort() after the operation has been
1978 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1979 * is safe and has no effect.
1980 *
1981 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001982 *
1983 * \retval #PSA_SUCCESS
1984 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001985 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001986 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1987 * \retval #PSA_ERROR_HARDWARE_FAILURE
1988 * \retval #PSA_ERROR_TAMPERING_DETECTED
1989 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001990psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1991
1992/**@}*/
1993
Gilles Peskine3b555712018-03-03 21:27:57 +01001994/** \defgroup aead Authenticated encryption with associated data (AEAD)
1995 * @{
1996 */
1997
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001998/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001999 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002000 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002001 * \param alg The AEAD algorithm to compute
2002 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002003 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002004 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002005 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002006 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002007 * but not encrypted.
2008 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002009 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002010 * encrypted.
2011 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002012 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002013 * encrypted data. The additional data is not
2014 * part of this output. For algorithms where the
2015 * encrypted data and the authentication tag
2016 * are defined as separate outputs, the
2017 * authentication tag is appended to the
2018 * encrypted data.
2019 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2020 * This must be at least
2021 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
2022 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002023 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002024 * in the \p ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002025 *
Gilles Peskine28538492018-07-11 17:34:00 +02002026 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002027 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002028 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002029 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002030 * \retval #PSA_ERROR_NOT_PERMITTED
2031 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002032 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002033 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002034 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002035 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2036 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2037 * \retval #PSA_ERROR_HARDWARE_FAILURE
2038 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002039 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002040 * The library has not been previously initialized by psa_crypto_init().
2041 * It is implementation-dependent whether a failure to initialize
2042 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002043 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002044psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002045 psa_algorithm_t alg,
2046 const uint8_t *nonce,
2047 size_t nonce_length,
2048 const uint8_t *additional_data,
2049 size_t additional_data_length,
2050 const uint8_t *plaintext,
2051 size_t plaintext_length,
2052 uint8_t *ciphertext,
2053 size_t ciphertext_size,
2054 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002055
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002056/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01002057 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002058 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002059 * \param alg The AEAD algorithm to compute
2060 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002061 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002062 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002063 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002064 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002065 * but not encrypted.
2066 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002067 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002068 * encrypted. For algorithms where the
2069 * encrypted data and the authentication tag
2070 * are defined as separate inputs, the buffer
2071 * must contain the encrypted data followed
2072 * by the authentication tag.
2073 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002074 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002075 * \param plaintext_size Size of the \p plaintext buffer in bytes.
2076 * This must be at least
2077 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
2078 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02002079 * \param[out] plaintext_length On success, the size of the output
Gilles Peskine4c6fdbb2019-02-08 11:22:39 +01002080 * in the \p plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01002081 *
Gilles Peskine28538492018-07-11 17:34:00 +02002082 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01002083 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002084 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002085 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02002086 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02002087 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02002088 * \retval #PSA_ERROR_NOT_PERMITTED
2089 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002090 * \p handle is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02002091 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002092 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02002093 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2094 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2095 * \retval #PSA_ERROR_HARDWARE_FAILURE
2096 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002097 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002098 * The library has not been previously initialized by psa_crypto_init().
2099 * It is implementation-dependent whether a failure to initialize
2100 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01002101 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002102psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02002103 psa_algorithm_t alg,
2104 const uint8_t *nonce,
2105 size_t nonce_length,
2106 const uint8_t *additional_data,
2107 size_t additional_data_length,
2108 const uint8_t *ciphertext,
2109 size_t ciphertext_length,
2110 uint8_t *plaintext,
2111 size_t plaintext_size,
2112 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01002113
Gilles Peskine30a9e412019-01-14 18:36:12 +01002114/** The type of the state data structure for multipart AEAD operations.
2115 *
2116 * Before calling any function on an AEAD operation object, the application
2117 * must initialize it by any of the following means:
2118 * - Set the structure to all-bits-zero, for example:
2119 * \code
2120 * psa_aead_operation_t operation;
2121 * memset(&operation, 0, sizeof(operation));
2122 * \endcode
2123 * - Initialize the structure to logical zero values, for example:
2124 * \code
2125 * psa_aead_operation_t operation = {0};
2126 * \endcode
2127 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
2128 * for example:
2129 * \code
2130 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
2131 * \endcode
2132 * - Assign the result of the function psa_aead_operation_init()
2133 * to the structure, for example:
2134 * \code
2135 * psa_aead_operation_t operation;
2136 * operation = psa_aead_operation_init();
2137 * \endcode
2138 *
2139 * This is an implementation-defined \c struct. Applications should not
2140 * make any assumptions about the content of this structure except
2141 * as directed by the documentation of a specific implementation. */
2142typedef struct psa_aead_operation_s psa_aead_operation_t;
2143
2144/** \def PSA_AEAD_OPERATION_INIT
2145 *
2146 * This macro returns a suitable initializer for an AEAD operation object of
2147 * type #psa_aead_operation_t.
2148 */
2149#ifdef __DOXYGEN_ONLY__
2150/* This is an example definition for documentation purposes.
2151 * Implementations should define a suitable value in `crypto_struct.h`.
2152 */
2153#define PSA_AEAD_OPERATION_INIT {0}
2154#endif
2155
2156/** Return an initial value for an AEAD operation object.
2157 */
2158static psa_aead_operation_t psa_aead_operation_init(void);
2159
2160/** Set the key for a multipart authenticated encryption operation.
2161 *
2162 * The sequence of operations to encrypt a message with authentication
2163 * is as follows:
2164 * -# Allocate an operation object which will be passed to all the functions
2165 * listed here.
2166 * -# Initialize the operation object with one of the methods described in the
2167 * documentation for #psa_aead_operation_t, e.g.
2168 * PSA_AEAD_OPERATION_INIT.
2169 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002170 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2171 * inputs to the subsequent calls to psa_aead_update_ad() and
2172 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2173 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002174 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
2175 * generate or set the nonce. You should use
2176 * psa_aead_generate_nonce() unless the protocol you are implementing
2177 * requires a specific nonce value.
2178 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2179 * of the non-encrypted additional authenticated data each time.
2180 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002181 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002182 * -# Call psa_aead_finish().
2183 *
2184 * The application may call psa_aead_abort() at any time after the operation
2185 * has been initialized.
2186 *
2187 * After a successful call to psa_aead_encrypt_setup(), the application must
2188 * eventually terminate the operation. The following events terminate an
2189 * operation:
2190 * - A failed call to any of the \c psa_aead_xxx functions.
2191 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2192 *
2193 * \param[in,out] operation The operation object to set up. It must have
2194 * been initialized as per the documentation for
2195 * #psa_aead_operation_t and not yet in use.
2196 * \param handle Handle to the key to use for the operation.
2197 * It must remain valid until the operation
2198 * terminates.
2199 * \param alg The AEAD algorithm to compute
2200 * (\c PSA_ALG_XXX value such that
2201 * #PSA_ALG_IS_AEAD(\p alg) is true).
2202 *
2203 * \retval #PSA_SUCCESS
2204 * Success.
2205 * \retval #PSA_ERROR_INVALID_HANDLE
2206 * \retval #PSA_ERROR_EMPTY_SLOT
2207 * \retval #PSA_ERROR_NOT_PERMITTED
2208 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002209 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002210 * \retval #PSA_ERROR_NOT_SUPPORTED
2211 * \p alg is not supported or is not an AEAD algorithm.
2212 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2213 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2214 * \retval #PSA_ERROR_HARDWARE_FAILURE
2215 * \retval #PSA_ERROR_TAMPERING_DETECTED
2216 * \retval #PSA_ERROR_BAD_STATE
2217 * The library has not been previously initialized by psa_crypto_init().
2218 * It is implementation-dependent whether a failure to initialize
2219 * results in this error code.
2220 */
2221psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2222 psa_key_handle_t handle,
2223 psa_algorithm_t alg);
2224
2225/** Set the key for a multipart authenticated decryption operation.
2226 *
2227 * The sequence of operations to decrypt a message with authentication
2228 * is as follows:
2229 * -# Allocate an operation object which will be passed to all the functions
2230 * listed here.
2231 * -# Initialize the operation object with one of the methods described in the
2232 * documentation for #psa_aead_operation_t, e.g.
2233 * PSA_AEAD_OPERATION_INIT.
2234 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002235 * -# If needed, call psa_aead_set_lengths() to specify the length of the
2236 * inputs to the subsequent calls to psa_aead_update_ad() and
2237 * psa_aead_update(). See the documentation of psa_aead_set_lengths()
2238 * for details.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002239 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2240 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2241 * of the non-encrypted additional authenticated data each time.
2242 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002243 * of the ciphertext to decrypt each time.
2244 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002245 *
2246 * The application may call psa_aead_abort() at any time after the operation
2247 * has been initialized.
2248 *
2249 * After a successful call to psa_aead_decrypt_setup(), the application must
2250 * eventually terminate the operation. The following events terminate an
2251 * operation:
2252 * - A failed call to any of the \c psa_aead_xxx functions.
2253 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2254 *
2255 * \param[in,out] operation The operation object to set up. It must have
2256 * been initialized as per the documentation for
2257 * #psa_aead_operation_t and not yet in use.
2258 * \param handle Handle to the key to use for the operation.
2259 * It must remain valid until the operation
2260 * terminates.
2261 * \param alg The AEAD algorithm to compute
2262 * (\c PSA_ALG_XXX value such that
2263 * #PSA_ALG_IS_AEAD(\p alg) is true).
2264 *
2265 * \retval #PSA_SUCCESS
2266 * Success.
2267 * \retval #PSA_ERROR_INVALID_HANDLE
2268 * \retval #PSA_ERROR_EMPTY_SLOT
2269 * \retval #PSA_ERROR_NOT_PERMITTED
2270 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002271 * \p handle is not compatible with \p alg.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002272 * \retval #PSA_ERROR_NOT_SUPPORTED
2273 * \p alg is not supported or is not an AEAD algorithm.
2274 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2275 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2276 * \retval #PSA_ERROR_HARDWARE_FAILURE
2277 * \retval #PSA_ERROR_TAMPERING_DETECTED
2278 * \retval #PSA_ERROR_BAD_STATE
2279 * The library has not been previously initialized by psa_crypto_init().
2280 * It is implementation-dependent whether a failure to initialize
2281 * results in this error code.
2282 */
2283psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2284 psa_key_handle_t handle,
2285 psa_algorithm_t alg);
2286
2287/** Generate a random nonce for an authenticated encryption operation.
2288 *
2289 * This function generates a random nonce for the authenticated encryption
2290 * operation with an appropriate size for the chosen algorithm, key type
2291 * and key size.
2292 *
2293 * The application must call psa_aead_encrypt_setup() before
2294 * calling this function.
2295 *
2296 * If this function returns an error status, the operation becomes inactive.
2297 *
2298 * \param[in,out] operation Active AEAD operation.
2299 * \param[out] nonce Buffer where the generated nonce is to be
2300 * written.
2301 * \param nonce_size Size of the \p nonce buffer in bytes.
2302 * \param[out] nonce_length On success, the number of bytes of the
2303 * generated nonce.
2304 *
2305 * \retval #PSA_SUCCESS
2306 * Success.
2307 * \retval #PSA_ERROR_BAD_STATE
2308 * The operation state is not valid (not set up, or nonce already set).
2309 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2310 * The size of the \p nonce buffer is too small.
2311 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2312 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2313 * \retval #PSA_ERROR_HARDWARE_FAILURE
2314 * \retval #PSA_ERROR_TAMPERING_DETECTED
2315 */
2316psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2317 unsigned char *nonce,
2318 size_t nonce_size,
2319 size_t *nonce_length);
2320
2321/** Set the nonce for an authenticated encryption or decryption operation.
2322 *
2323 * This function sets the nonce for the authenticated
2324 * encryption or decryption operation.
2325 *
2326 * The application must call psa_aead_encrypt_setup() before
2327 * calling this function.
2328 *
2329 * If this function returns an error status, the operation becomes inactive.
2330 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002331 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002332 * instead of this function, unless implementing a protocol that requires
2333 * a non-random IV.
2334 *
2335 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002336 * \param[in] nonce Buffer containing the nonce to use.
2337 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002338 *
2339 * \retval #PSA_SUCCESS
2340 * Success.
2341 * \retval #PSA_ERROR_BAD_STATE
2342 * The operation state is not valid (not set up, or nonce already set).
2343 * \retval #PSA_ERROR_INVALID_ARGUMENT
2344 * The size of \p nonce is not acceptable for the chosen algorithm.
2345 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2346 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2347 * \retval #PSA_ERROR_HARDWARE_FAILURE
2348 * \retval #PSA_ERROR_TAMPERING_DETECTED
2349 */
2350psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2351 const unsigned char *nonce,
2352 size_t nonce_length);
2353
Gilles Peskinebc59c852019-01-17 15:26:08 +01002354/** Declare the lengths of the message and additional data for AEAD.
2355 *
2356 * The application must call this function before calling
2357 * psa_aead_update_ad() or psa_aead_update() if the algorithm for
2358 * the operation requires it. If the algorithm does not require it,
2359 * calling this function is optional, but if this function is called
2360 * then the implementation must enforce the lengths.
2361 *
2362 * You may call this function before or after setting the nonce with
2363 * psa_aead_set_nonce() or psa_aead_generate_nonce().
2364 *
2365 * - For #PSA_ALG_CCM, calling this function is required.
2366 * - For the other AEAD algorithms defined in this specification, calling
2367 * this function is not required.
2368 * - For vendor-defined algorithm, refer to the vendor documentation.
2369 *
2370 * \param[in,out] operation Active AEAD operation.
2371 * \param ad_length Size of the non-encrypted additional
2372 * authenticated data in bytes.
2373 * \param plaintext_length Size of the plaintext to encrypt in bytes.
2374 *
2375 * \retval #PSA_SUCCESS
2376 * Success.
2377 * \retval #PSA_ERROR_BAD_STATE
2378 * The operation state is not valid (not set up, already completed,
2379 * or psa_aead_update_ad() or psa_aead_update() already called).
2380 * \retval #PSA_ERROR_INVALID_ARGUMENT
2381 * At least one of the lengths is not acceptable for the chosen
2382 * algorithm.
2383 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2384 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2385 * \retval #PSA_ERROR_HARDWARE_FAILURE
2386 * \retval #PSA_ERROR_TAMPERING_DETECTED
2387 */
2388psa_status_t psa_aead_set_lengths(psa_aead_operation_t *operation,
2389 size_t ad_length,
2390 size_t plaintext_length);
2391
Gilles Peskine30a9e412019-01-14 18:36:12 +01002392/** Pass additional data to an active AEAD operation.
2393 *
2394 * Additional data is authenticated, but not encrypted.
2395 *
2396 * You may call this function multiple times to pass successive fragments
2397 * of the additional data. You may not call this function after passing
2398 * data to encrypt or decrypt with psa_aead_update().
2399 *
2400 * Before calling this function, you must:
2401 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2402 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2403 *
2404 * If this function returns an error status, the operation becomes inactive.
2405 *
2406 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2407 * there is no guarantee that the input is valid. Therefore, until
2408 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2409 * treat the input as untrusted and prepare to undo any action that
2410 * depends on the input if psa_aead_verify() returns an error status.
2411 *
2412 * \param[in,out] operation Active AEAD operation.
2413 * \param[in] input Buffer containing the fragment of
2414 * additional data.
2415 * \param input_length Size of the \p input buffer in bytes.
2416 *
2417 * \retval #PSA_SUCCESS
2418 * Success.
2419 * \retval #PSA_ERROR_BAD_STATE
2420 * The operation state is not valid (not set up, nonce not set,
2421 * psa_aead_update() already called, or operation already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002422 * \retval #PSA_ERROR_INVALID_ARGUMENT
2423 * The total input length overflows the additional data length that
2424 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002425 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2426 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2427 * \retval #PSA_ERROR_HARDWARE_FAILURE
2428 * \retval #PSA_ERROR_TAMPERING_DETECTED
2429 */
2430psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2431 const uint8_t *input,
2432 size_t input_length);
2433
2434/** Encrypt or decrypt a message fragment in an active AEAD operation.
2435 *
2436 * Before calling this function, you must:
2437 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2438 * The choice of setup function determines whether this function
2439 * encrypts or decrypts its input.
2440 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2441 * 3. Call psa_aead_update_ad() to pass all the additional data.
2442 *
2443 * If this function returns an error status, the operation becomes inactive.
2444 *
2445 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2446 * there is no guarantee that the input is valid. Therefore, until
2447 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2448 * - Do not use the output in any way other than storing it in a
2449 * confidential location. If you take any action that depends
2450 * on the tentative decrypted data, this action will need to be
2451 * undone if the input turns out not to be valid. Furthermore,
2452 * if an adversary can observe that this action took place
2453 * (for example through timing), they may be able to use this
2454 * fact as an oracle to decrypt any message encrypted with the
2455 * same key.
2456 * - In particular, do not copy the output anywhere but to a
2457 * memory or storage space that you have exclusive access to.
2458 *
2459 * \param[in,out] operation Active AEAD operation.
2460 * \param[in] input Buffer containing the message fragment to
2461 * encrypt or decrypt.
2462 * \param input_length Size of the \p input buffer in bytes.
2463 * \param[out] output Buffer where the output is to be written.
2464 * \param output_size Size of the \p output buffer in bytes.
2465 * \param[out] output_length On success, the number of bytes
2466 * that make up the returned output.
2467 *
2468 * \retval #PSA_SUCCESS
2469 * Success.
2470 * \retval #PSA_ERROR_BAD_STATE
2471 * The operation state is not valid (not set up, nonce not set
2472 * or already completed).
2473 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2474 * The size of the \p output buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002475 * \retval #PSA_ERROR_INVALID_ARGUMENT
2476 * The total length of input to psa_aead_update_ad() so far is
2477 * less than the additional data length that was previously
2478 * specified with psa_aead_set_lengths().
2479 * \retval #PSA_ERROR_INVALID_ARGUMENT
2480 * The total input length overflows the plaintext length that
2481 * was previously specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002482 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2483 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2484 * \retval #PSA_ERROR_HARDWARE_FAILURE
2485 * \retval #PSA_ERROR_TAMPERING_DETECTED
2486 */
2487psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2488 const uint8_t *input,
2489 size_t input_length,
2490 unsigned char *output,
2491 size_t output_size,
2492 size_t *output_length);
2493
2494/** Finish encrypting a message in an AEAD operation.
2495 *
2496 * The operation must have been set up with psa_aead_encrypt_setup().
2497 *
2498 * This function finishes the authentication of the additional data
2499 * formed by concatenating the inputs passed to preceding calls to
2500 * psa_aead_update_ad() with the plaintext formed by concatenating the
2501 * inputs passed to preceding calls to psa_aead_update().
2502 *
2503 * This function has two output buffers:
2504 * - \p ciphertext contains trailing ciphertext that was buffered from
2505 * preceding calls to psa_aead_update(). For all standard AEAD algorithms,
2506 * psa_aead_update() does not buffer any output and therefore \p ciphertext
2507 * will not contain any output and can be a 0-sized buffer.
2508 * - \p tag contains the authentication tag. Its length is always
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002509 * #PSA_AEAD_TAG_LENGTH(\c alg) where \c alg is the AEAD algorithm
Gilles Peskine30a9e412019-01-14 18:36:12 +01002510 * that the operation performs.
2511 *
2512 * When this function returns, the operation becomes inactive.
2513 *
2514 * \param[in,out] operation Active AEAD operation.
2515 * \param[out] ciphertext Buffer where the last part of the ciphertext
2516 * is to be written.
2517 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2518 * \param[out] ciphertext_length On success, the number of bytes of
2519 * returned ciphertext.
2520 * \param[out] tag Buffer where the authentication tag is
2521 * to be written.
2522 * \param tag_size Size of the \p tag buffer in bytes.
2523 * \param[out] tag_length On success, the number of bytes
2524 * that make up the returned tag.
2525 *
2526 * \retval #PSA_SUCCESS
2527 * Success.
2528 * \retval #PSA_ERROR_BAD_STATE
2529 * The operation state is not valid (not set up, nonce not set,
2530 * decryption, or already completed).
2531 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002532 * The size of the \p ciphertext or \p tag buffer is too small.
Gilles Peskinebc59c852019-01-17 15:26:08 +01002533 * \retval #PSA_ERROR_INVALID_ARGUMENT
2534 * The total length of input to psa_aead_update_ad() so far is
2535 * less than the additional data length that was previously
2536 * specified with psa_aead_set_lengths().
2537 * \retval #PSA_ERROR_INVALID_ARGUMENT
2538 * The total length of input to psa_aead_update() so far is
2539 * less than the plaintext length that was previously
2540 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002541 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2542 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2543 * \retval #PSA_ERROR_HARDWARE_FAILURE
2544 * \retval #PSA_ERROR_TAMPERING_DETECTED
2545 */
2546psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002547 uint8_t *ciphertext,
2548 size_t ciphertext_size,
2549 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002550 uint8_t *tag,
2551 size_t tag_size,
2552 size_t *tag_length);
2553
2554/** Finish authenticating and decrypting a message in an AEAD operation.
2555 *
2556 * The operation must have been set up with psa_aead_decrypt_setup().
2557 *
2558 * This function finishes the authentication of the additional data
2559 * formed by concatenating the inputs passed to preceding calls to
2560 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2561 * inputs passed to preceding calls to psa_aead_update().
2562 *
2563 * When this function returns, the operation becomes inactive.
2564 *
2565 * \param[in,out] operation Active AEAD operation.
2566 * \param[in] tag Buffer containing the authentication tag.
2567 * \param tag_length Size of the \p tag buffer in bytes.
2568 *
2569 * \retval #PSA_SUCCESS
2570 * Success.
2571 * \retval #PSA_ERROR_BAD_STATE
2572 * The operation state is not valid (not set up, nonce not set,
2573 * encryption, or already completed).
Gilles Peskinebc59c852019-01-17 15:26:08 +01002574 * \retval #PSA_ERROR_INVALID_ARGUMENT
2575 * The total length of input to psa_aead_update_ad() so far is
2576 * less than the additional data length that was previously
2577 * specified with psa_aead_set_lengths().
2578 * \retval #PSA_ERROR_INVALID_ARGUMENT
2579 * The total length of input to psa_aead_update() so far is
2580 * less than the plaintext length that was previously
2581 * specified with psa_aead_set_lengths().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002582 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2583 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2584 * \retval #PSA_ERROR_HARDWARE_FAILURE
2585 * \retval #PSA_ERROR_TAMPERING_DETECTED
2586 */
2587psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
2588 const uint8_t *tag,
2589 size_t tag_length);
2590
2591/** Abort an AEAD operation.
2592 *
2593 * Aborting an operation frees all associated resources except for the
2594 * \p operation structure itself. Once aborted, the operation object
2595 * can be reused for another operation by calling
2596 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2597 *
2598 * You may call this function any time after the operation object has
2599 * been initialized by any of the following methods:
2600 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2601 * whether it succeeds or not.
2602 * - Initializing the \c struct to all-bits-zero.
2603 * - Initializing the \c struct to logical zeros, e.g.
2604 * `psa_aead_operation_t operation = {0}`.
2605 *
2606 * In particular, calling psa_aead_abort() after the operation has been
2607 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2608 * is safe and has no effect.
2609 *
2610 * \param[in,out] operation Initialized AEAD operation.
2611 *
2612 * \retval #PSA_SUCCESS
2613 * \retval #PSA_ERROR_BAD_STATE
2614 * \p operation is not an active AEAD operation.
2615 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2616 * \retval #PSA_ERROR_HARDWARE_FAILURE
2617 * \retval #PSA_ERROR_TAMPERING_DETECTED
2618 */
2619psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2620
Gilles Peskine3b555712018-03-03 21:27:57 +01002621/**@}*/
2622
Gilles Peskine20035e32018-02-03 22:44:14 +01002623/** \defgroup asymmetric Asymmetric cryptography
2624 * @{
2625 */
2626
2627/**
2628 * \brief Sign a hash or short message with a private key.
2629 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002630 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002631 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002632 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2633 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2634 * to determine the hash algorithm to use.
2635 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002636 * \param handle Handle to the key to use for the operation.
2637 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002638 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002639 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002640 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002641 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002642 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002643 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002644 * \param[out] signature_length On success, the number of bytes
2645 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002646 *
Gilles Peskine28538492018-07-11 17:34:00 +02002647 * \retval #PSA_SUCCESS
2648 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002649 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002650 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002651 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002652 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002653 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002654 * \retval #PSA_ERROR_NOT_SUPPORTED
2655 * \retval #PSA_ERROR_INVALID_ARGUMENT
2656 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2657 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2658 * \retval #PSA_ERROR_HARDWARE_FAILURE
2659 * \retval #PSA_ERROR_TAMPERING_DETECTED
2660 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002661 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002662 * The library has not been previously initialized by psa_crypto_init().
2663 * It is implementation-dependent whether a failure to initialize
2664 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002665 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002666psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002667 psa_algorithm_t alg,
2668 const uint8_t *hash,
2669 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002670 uint8_t *signature,
2671 size_t signature_size,
2672 size_t *signature_length);
2673
2674/**
2675 * \brief Verify the signature a hash or short message using a public key.
2676 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002677 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002678 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002679 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2680 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2681 * to determine the hash algorithm to use.
2682 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002683 * \param handle Handle to the key to use for the operation.
2684 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002685 * \param alg A signature algorithm that is compatible with
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002686 * the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002687 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002688 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002689 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002690 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002691 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002692 *
Gilles Peskine28538492018-07-11 17:34:00 +02002693 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002694 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002695 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002696 * The calculation was perfomed successfully, but the passed
2697 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002698 * \retval #PSA_ERROR_NOT_SUPPORTED
2699 * \retval #PSA_ERROR_INVALID_ARGUMENT
2700 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2701 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2702 * \retval #PSA_ERROR_HARDWARE_FAILURE
2703 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002704 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002705 * The library has not been previously initialized by psa_crypto_init().
2706 * It is implementation-dependent whether a failure to initialize
2707 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002708 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002709psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002710 psa_algorithm_t alg,
2711 const uint8_t *hash,
2712 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002713 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002714 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002715
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002716/**
2717 * \brief Encrypt a short message with a public key.
2718 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002719 * \param handle Handle to the key to use for the operation.
2720 * It must be a public key or an asymmetric
2721 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002722 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002723 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002724 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002725 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002726 * \param[in] salt A salt or label, if supported by the
2727 * encryption algorithm.
2728 * If the algorithm does not support a
2729 * salt, pass \c NULL.
2730 * If the algorithm supports an optional
2731 * salt and you do not want to pass a salt,
2732 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002733 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002734 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2735 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002736 * \param salt_length Size of the \p salt buffer in bytes.
2737 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002738 * \param[out] output Buffer where the encrypted message is to
2739 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002740 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002741 * \param[out] output_length On success, the number of bytes
2742 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002743 *
Gilles Peskine28538492018-07-11 17:34:00 +02002744 * \retval #PSA_SUCCESS
2745 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002746 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002747 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002748 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002749 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002750 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002751 * \retval #PSA_ERROR_NOT_SUPPORTED
2752 * \retval #PSA_ERROR_INVALID_ARGUMENT
2753 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2754 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2755 * \retval #PSA_ERROR_HARDWARE_FAILURE
2756 * \retval #PSA_ERROR_TAMPERING_DETECTED
2757 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002758 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002759 * The library has not been previously initialized by psa_crypto_init().
2760 * It is implementation-dependent whether a failure to initialize
2761 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002762 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002763psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002764 psa_algorithm_t alg,
2765 const uint8_t *input,
2766 size_t input_length,
2767 const uint8_t *salt,
2768 size_t salt_length,
2769 uint8_t *output,
2770 size_t output_size,
2771 size_t *output_length);
2772
2773/**
2774 * \brief Decrypt a short message with a private key.
2775 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002776 * \param handle Handle to the key to use for the operation.
2777 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002778 * \param alg An asymmetric encryption algorithm that is
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002779 * compatible with the type of \p handle.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002780 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002781 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002782 * \param[in] salt A salt or label, if supported by the
2783 * encryption algorithm.
2784 * If the algorithm does not support a
2785 * salt, pass \c NULL.
2786 * If the algorithm supports an optional
2787 * salt and you do not want to pass a salt,
2788 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002789 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002790 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2791 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002792 * \param salt_length Size of the \p salt buffer in bytes.
2793 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002794 * \param[out] output Buffer where the decrypted message is to
2795 * be written.
2796 * \param output_size Size of the \c output buffer in bytes.
2797 * \param[out] output_length On success, the number of bytes
2798 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002799 *
Gilles Peskine28538492018-07-11 17:34:00 +02002800 * \retval #PSA_SUCCESS
2801 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002802 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002803 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002804 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002805 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01002806 * respectively of \p handle.
Gilles Peskine28538492018-07-11 17:34:00 +02002807 * \retval #PSA_ERROR_NOT_SUPPORTED
2808 * \retval #PSA_ERROR_INVALID_ARGUMENT
2809 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2810 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2811 * \retval #PSA_ERROR_HARDWARE_FAILURE
2812 * \retval #PSA_ERROR_TAMPERING_DETECTED
2813 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2814 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002815 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002816 * The library has not been previously initialized by psa_crypto_init().
2817 * It is implementation-dependent whether a failure to initialize
2818 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002819 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002820psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002821 psa_algorithm_t alg,
2822 const uint8_t *input,
2823 size_t input_length,
2824 const uint8_t *salt,
2825 size_t salt_length,
2826 uint8_t *output,
2827 size_t output_size,
2828 size_t *output_length);
2829
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002830/**@}*/
2831
Gilles Peskineedd76872018-07-20 17:42:05 +02002832/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02002833 * @{
2834 */
2835
2836/** The type of the state data structure for generators.
2837 *
2838 * Before calling any function on a generator, the application must
2839 * initialize it by any of the following means:
2840 * - Set the structure to all-bits-zero, for example:
2841 * \code
2842 * psa_crypto_generator_t generator;
2843 * memset(&generator, 0, sizeof(generator));
2844 * \endcode
2845 * - Initialize the structure to logical zero values, for example:
2846 * \code
2847 * psa_crypto_generator_t generator = {0};
2848 * \endcode
2849 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
2850 * for example:
2851 * \code
2852 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2853 * \endcode
2854 * - Assign the result of the function psa_crypto_generator_init()
2855 * to the structure, for example:
2856 * \code
2857 * psa_crypto_generator_t generator;
2858 * generator = psa_crypto_generator_init();
2859 * \endcode
2860 *
2861 * This is an implementation-defined \c struct. Applications should not
2862 * make any assumptions about the content of this structure except
2863 * as directed by the documentation of a specific implementation.
2864 */
2865typedef struct psa_crypto_generator_s psa_crypto_generator_t;
2866
2867/** \def PSA_CRYPTO_GENERATOR_INIT
2868 *
2869 * This macro returns a suitable initializer for a generator object
2870 * of type #psa_crypto_generator_t.
2871 */
2872#ifdef __DOXYGEN_ONLY__
2873/* This is an example definition for documentation purposes.
2874 * Implementations should define a suitable value in `crypto_struct.h`.
2875 */
2876#define PSA_CRYPTO_GENERATOR_INIT {0}
2877#endif
2878
2879/** Return an initial value for a generator object.
2880 */
2881static psa_crypto_generator_t psa_crypto_generator_init(void);
2882
2883/** Retrieve the current capacity of a generator.
2884 *
2885 * The capacity of a generator is the maximum number of bytes that it can
2886 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
2887 *
2888 * \param[in] generator The generator to query.
2889 * \param[out] capacity On success, the capacity of the generator.
2890 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002891 * \retval #PSA_SUCCESS
2892 * \retval #PSA_ERROR_BAD_STATE
2893 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002894 */
2895psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
2896 size_t *capacity);
2897
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01002898/** Set the maximum capacity of a generator.
2899 *
2900 * \param[in,out] generator The generator object to modify.
2901 * \param capacity The new capacity of the generator.
2902 * It must be less or equal to the generator's
2903 * current capacity.
2904 *
2905 * \retval #PSA_SUCCESS
2906 * \retval #PSA_ERROR_INVALID_ARGUMENT
2907 * \p capacity is larger than the generator's current capacity.
2908 * \retval #PSA_ERROR_BAD_STATE
2909 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2910 */
2911psa_status_t psa_set_generator_capacity(psa_crypto_generator_t *generator,
2912 size_t capacity);
2913
Gilles Peskineeab56e42018-07-12 17:12:33 +02002914/** Read some data from a generator.
2915 *
2916 * This function reads and returns a sequence of bytes from a generator.
2917 * The data that is read is discarded from the generator. The generator's
2918 * capacity is decreased by the number of bytes read.
2919 *
2920 * \param[in,out] generator The generator object to read from.
2921 * \param[out] output Buffer where the generator output will be
2922 * written.
2923 * \param output_length Number of bytes to output.
2924 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002925 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02002926 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskineeab56e42018-07-12 17:12:33 +02002927 * There were fewer than \p output_length bytes
2928 * in the generator. Note that in this case, no
2929 * output is written to the output buffer.
2930 * The generator's capacity is set to 0, thus
2931 * subsequent calls to this function will not
2932 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002933 * \retval #PSA_ERROR_BAD_STATE
2934 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2935 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2936 * \retval #PSA_ERROR_HARDWARE_FAILURE
2937 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002938 */
2939psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
2940 uint8_t *output,
2941 size_t output_length);
2942
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002943/** Generate a key deterministically from data read from a generator.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002944 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002945 * This function uses the output of a generator to derive a key.
2946 * How much output it consumes and how the key is derived depends on the
2947 * key type.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002948 *
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002949 * - For key types for which the key is an arbitrary sequence of bytes
2950 * of a given size,
2951 * this function is functionally equivalent to calling #psa_generator_read
2952 * and passing the resulting output to #psa_import_key.
2953 * However, this function has a security benefit:
2954 * if the implementation provides an isolation boundary then
2955 * the key material is not exposed outside the isolation boundary.
2956 * As a consequence, for these key types, this function always consumes
2957 * exactly (\p bits / 8) bytes from the generator.
2958 * The following key types defined in this specification follow this scheme:
2959 *
2960 * - #PSA_KEY_TYPE_AES;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002961 * - #PSA_KEY_TYPE_ARC4;
2962 * - #PSA_KEY_TYPE_CAMELLIA;
Gilles Peskinefa4486d2019-03-11 17:30:31 +01002963 * - #PSA_KEY_TYPE_DERIVE;
2964 * - #PSA_KEY_TYPE_HMAC.
2965 *
2966 * - For ECC keys on a Montgomery elliptic curve
2967 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
2968 * Montgomery curve), this function always draws a byte string whose
2969 * length is determined by the curve, and sets the mandatory bits
2970 * accordingly. That is:
2971 *
2972 * - #PSA_ECC_CURVE_CURVE25519: draw a 32-byte string
2973 * and process it as specified in RFC 7748 &sect;5.
2974 * - #PSA_ECC_CURVE_CURVE448: draw a 56-byte string
2975 * and process it as specified in RFC 7748 &sect;5.
2976 *
2977 * - For key types for which the key is represented by a single sequence of
2978 * \p bits bits with constraints as to which bit sequences are acceptable,
2979 * this function draws a byte string of length (\p bits / 8) bytes rounded
2980 * up to the nearest whole number of bytes. If the resulting byte string
2981 * is acceptable, it becomes the key, otherwise the drawn bytes are discarded.
2982 * This process is repeated until an acceptable byte string is drawn.
2983 * The byte string drawn from the generator is interpreted as specified
2984 * for the output produced by psa_export_key().
2985 * The following key types defined in this specification follow this scheme:
2986 *
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01002987 * - #PSA_KEY_TYPE_DES.
2988 * Force-set the parity bits, but discard forbidden weak keys.
2989 * For 2-key and 3-key triple-DES, the three keys are generated
2990 * successively (for example, for 3-key triple-DES,
2991 * if the first 8 bytes specify a weak key and the next 8 bytes do not,
2992 * discard the first 8 bytes, use the next 8 bytes as the first key,
2993 * and continue reading output from the generator to derive the other
2994 * two keys).
2995 * - Finite-field Diffie-Hellman keys (#PSA_KEY_TYPE_DH_KEYPAIR),
2996 * DSA keys (#PSA_KEY_TYPE_DSA_KEYPAIR), and
2997 * ECC keys on a Weierstrass elliptic curve
2998 * (#PSA_KEY_TYPE_ECC_KEYPAIR(\c curve) where \c curve designates a
2999 * Weierstrass curve).
3000 * For these key types, interpret the byte string as integer
3001 * in big-endian order. Discard it if it is not in the range
3002 * [0, *N* - 2] where *N* is the boundary of the private key domain
3003 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
Gilles Peskine55799712019-03-12 11:50:26 +01003004 * or the order of the curve's base point for ECC).
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003005 * Add 1 to the resulting integer and use this as the private key *x*.
Gilles Peskine55799712019-03-12 11:50:26 +01003006 * This method allows compliance to NIST standards, specifically
3007 * the methods titled "key-pair generation by testing candidates"
Gilles Peskine2de2c0d2019-03-11 17:59:16 +01003008 * in NIST SP 800-56A &sect;5.6.1.1.4 for Diffie-Hellman,
3009 * in FIPS 186-4 &sect;B.1.2 for DSA, and
3010 * in NIST SP 800-56A &sect;5.6.1.2.2 or
3011 * FIPS 186-4 &sect;B.4.2 for elliptic curve keys.
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003012 *
3013 * - For other key types, including #PSA_KEY_TYPE_RSA_KEYPAIR,
3014 * the way in which the generator output is consumed is
3015 * implementation-defined.
3016 *
3017 * In all cases, the data that is read is discarded from the generator.
3018 * The generator's capacity is decreased by the number of bytes read.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003019 *
Gilles Peskine20628592019-04-19 19:29:50 +02003020 * \param[in] attributes The attributes for the new key.
3021 * The key size field in \p attributes is
3022 * ignored; the actual key size is taken
3023 * from the \p bits parameter instead.
3024 * \param[out] handle On success, a handle to the newly created key.
3025 * \c 0 on failure.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003026 * \param bits Key size in bits.
3027 * \param[in,out] generator The generator object to read from.
3028 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003029 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02003030 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003031 * If the key is persistent, the key material and the key's metadata
3032 * have been saved to persistent storage.
Gilles Peskine20628592019-04-19 19:29:50 +02003033 * \retval #PSA_ERROR_ALREADY_EXISTS
3034 * This is an attempt to create a persistent key, and there is
3035 * already a persistent key with the given identifier.
David Saadab4ecc272019-02-14 13:48:10 +02003036 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskinefa4486d2019-03-11 17:30:31 +01003037 * There was not enough data to create the desired key.
3038 * Note that in this case, no output is written to the output buffer.
3039 * The generator's capacity is set to 0, thus subsequent calls to
3040 * this function will not succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003041 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003042 * The key type or key size is not supported, either by the
3043 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003044 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003045 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3046 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
3047 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3048 * \retval #PSA_ERROR_HARDWARE_FAILURE
3049 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003050 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003051 * The library has not been previously initialized by psa_crypto_init().
3052 * It is implementation-dependent whether a failure to initialize
3053 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02003054 */
Gilles Peskine87a5e562019-04-17 12:28:25 +02003055psa_status_t psa_generator_import_key(const psa_key_attributes_t *attributes,
3056 psa_key_handle_t *handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003057 size_t bits,
3058 psa_crypto_generator_t *generator);
3059
3060/** Abort a generator.
3061 *
3062 * Once a generator has been aborted, its capacity is zero.
3063 * Aborting a generator frees all associated resources except for the
3064 * \c generator structure itself.
3065 *
3066 * This function may be called at any time as long as the generator
3067 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
3068 * psa_crypto_generator_init() or a zero value. In particular, it is valid
3069 * to call psa_generator_abort() twice, or to call psa_generator_abort()
3070 * on a generator that has not been set up.
3071 *
3072 * Once aborted, the generator object may be called.
3073 *
3074 * \param[in,out] generator The generator to abort.
3075 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01003076 * \retval #PSA_SUCCESS
3077 * \retval #PSA_ERROR_BAD_STATE
3078 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3079 * \retval #PSA_ERROR_HARDWARE_FAILURE
3080 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02003081 */
3082psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
3083
Gilles Peskine8feb3a82018-09-18 12:06:11 +02003084/** Use the maximum possible capacity for a generator.
3085 *
3086 * Use this value as the capacity argument when setting up a generator
3087 * to indicate that the generator should have the maximum possible capacity.
3088 * The value of the maximum possible capacity depends on the generator
3089 * algorithm.
3090 */
3091#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
3092
Gilles Peskineeab56e42018-07-12 17:12:33 +02003093/**@}*/
3094
Gilles Peskineea0fb492018-07-12 17:17:20 +02003095/** \defgroup derivation Key derivation
3096 * @{
3097 */
3098
3099/** Set up a key derivation operation.
3100 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003101 * A key derivation algorithm takes some inputs and uses them to create
3102 * a byte generator which can be used to produce keys and other
3103 * cryptographic material.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003104 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003105 * To use a generator for key derivation:
3106 * - Start with an initialized object of type #psa_crypto_generator_t.
3107 * - Call psa_key_derivation_setup() to select the algorithm.
3108 * - Provide the inputs for the key derivation by calling
3109 * psa_key_derivation_input_bytes() or psa_key_derivation_input_key()
3110 * as appropriate. Which inputs are needed, in what order, and whether
3111 * they may be keys and if so of what type depends on the algorithm.
3112 * - Optionally set the generator's maximum capacity with
3113 * psa_set_generator_capacity(). You may do this before, in the middle of
3114 * or after providing inputs. For some algorithms, this step is mandatory
3115 * because the output depends on the maximum capacity.
3116 * - Generate output with psa_generator_read() or
3117 * psa_generator_import_key(). Successive calls to these functions
3118 * use successive output bytes from the generator.
3119 * - Clean up the generator object with psa_generator_abort().
Gilles Peskineea0fb492018-07-12 17:17:20 +02003120 *
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003121 * \param[in,out] generator The generator object to set up. It must
3122 * have been initialized but not set up yet.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003123 * \param alg The key derivation algorithm to compute
3124 * (\c PSA_ALG_XXX value such that
3125 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
Gilles Peskineea0fb492018-07-12 17:17:20 +02003126 *
3127 * \retval #PSA_SUCCESS
3128 * Success.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003129 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003130 * \c alg is not a key derivation algorithm.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003131 * \retval #PSA_ERROR_NOT_SUPPORTED
3132 * \c alg is not supported or is not a key derivation algorithm.
3133 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3134 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3135 * \retval #PSA_ERROR_HARDWARE_FAILURE
3136 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03003137 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003138 */
3139psa_status_t psa_key_derivation_setup(psa_crypto_generator_t *generator,
3140 psa_algorithm_t alg);
3141
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003142/** Provide an input for key derivation or key agreement.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003143 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003144 * Which inputs are required and in what order depends on the algorithm.
3145 * Refer to the documentation of each key derivation or key agreement
3146 * algorithm for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003147 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003148 * This function passes direct inputs. Some inputs must be passed as keys
3149 * using psa_key_derivation_input_key() instead of this function. Refer to
3150 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003151 *
3152 * \param[in,out] generator The generator object to use. It must
3153 * have been set up with
3154 * psa_key_derivation_setup() and must not
3155 * have produced any output yet.
3156 * \param step Which step the input data is for.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003157 * \param[in] data Input data to use.
3158 * \param data_length Size of the \p data buffer in bytes.
3159 *
3160 * \retval #PSA_SUCCESS
3161 * Success.
3162 * \retval #PSA_ERROR_INVALID_ARGUMENT
3163 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003164 * \retval #PSA_ERROR_INVALID_ARGUMENT
3165 * \c step does not allow direct inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003166 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3167 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3168 * \retval #PSA_ERROR_HARDWARE_FAILURE
3169 * \retval #PSA_ERROR_TAMPERING_DETECTED
3170 * \retval #PSA_ERROR_BAD_STATE
3171 * The value of \p step is not valid given the state of \p generator.
3172 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003173 * The library has not been previously initialized by psa_crypto_init().
3174 * It is implementation-dependent whether a failure to initialize
3175 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02003176 */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003177psa_status_t psa_key_derivation_input_bytes(psa_crypto_generator_t *generator,
3178 psa_key_derivation_step_t step,
3179 const uint8_t *data,
3180 size_t data_length);
Gilles Peskineea0fb492018-07-12 17:17:20 +02003181
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003182/** Provide an input for key derivation in the form of a key.
3183 *
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003184 * Which inputs are required and in what order depends on the algorithm.
3185 * Refer to the documentation of each key derivation or key agreement
3186 * algorithm for information.
3187 *
3188 * This function passes key inputs. Some inputs must be passed as keys
3189 * of the appropriate type using this function, while others must be
3190 * passed as direct inputs using psa_key_derivation_input_bytes(). Refer to
3191 * the documentation of individual step types for information.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003192 *
3193 * \param[in,out] generator The generator object to use. It must
3194 * have been set up with
3195 * psa_key_derivation_setup() and must not
3196 * have produced any output yet.
3197 * \param step Which step the input data is for.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003198 * \param handle Handle to the key. It must have an
3199 * appropriate type for \p step and must
3200 * allow the usage #PSA_KEY_USAGE_DERIVE.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003201 *
3202 * \retval #PSA_SUCCESS
3203 * Success.
3204 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine31351842019-04-09 12:00:00 +02003205 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003206 * \retval #PSA_ERROR_NOT_PERMITTED
3207 * \retval #PSA_ERROR_INVALID_ARGUMENT
3208 * \c step is not compatible with the generator's algorithm.
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01003209 * \retval #PSA_ERROR_INVALID_ARGUMENT
3210 * \c step does not allow key inputs.
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003211 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3212 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3213 * \retval #PSA_ERROR_HARDWARE_FAILURE
3214 * \retval #PSA_ERROR_TAMPERING_DETECTED
3215 * \retval #PSA_ERROR_BAD_STATE
3216 * The value of \p step is not valid given the state of \p generator.
3217 * \retval #PSA_ERROR_BAD_STATE
3218 * The library has not been previously initialized by psa_crypto_init().
3219 * It is implementation-dependent whether a failure to initialize
3220 * results in this error code.
3221 */
3222psa_status_t psa_key_derivation_input_key(psa_crypto_generator_t *generator,
3223 psa_key_derivation_step_t step,
3224 psa_key_handle_t handle);
3225
Gilles Peskine969c5d62019-01-16 15:53:06 +01003226/** Perform a key agreement and use the shared secret as input to a key
3227 * derivation.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003228 *
3229 * A key agreement algorithm takes two inputs: a private key \p private_key
3230 * a public key \p peer_key.
Gilles Peskine969c5d62019-01-16 15:53:06 +01003231 * The result of this function is passed as input to a key derivation.
3232 * The output of this key derivation can be extracted by reading from the
3233 * resulting generator to produce keys and other cryptographic material.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003234 *
Gilles Peskine969c5d62019-01-16 15:53:06 +01003235 * \param[in,out] generator The generator object to use. It must
3236 * have been set up with
3237 * psa_key_derivation_setup() with a
Gilles Peskine6843c292019-01-18 16:44:49 +01003238 * key agreement and derivation algorithm
3239 * \c alg (\c PSA_ALG_XXX value such that
Gilles Peskine3be6b7f2019-03-05 19:32:26 +01003240 * #PSA_ALG_IS_KEY_AGREEMENT(\c alg) is true
3241 * and #PSA_ALG_IS_RAW_KEY_AGREEMENT(\c alg)
Gilles Peskine6843c292019-01-18 16:44:49 +01003242 * is false).
Gilles Peskine969c5d62019-01-16 15:53:06 +01003243 * The generator must be ready for an
3244 * input of the type given by \p step.
3245 * \param step Which step the input data is for.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003246 * \param private_key Handle to the private key to use.
Jaeden Amero8afbff82019-01-14 16:56:20 +00003247 * \param[in] peer_key Public key of the peer. The peer key must be in the
3248 * same format that psa_import_key() accepts for the
3249 * public key type corresponding to the type of
3250 * private_key. That is, this function performs the
3251 * equivalent of
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003252 * #psa_import_key(`internal_public_key_handle`,
3253 * #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(`private_key_type`),
3254 * `peer_key`, `peer_key_length`) where
Jaeden Amero8afbff82019-01-14 16:56:20 +00003255 * `private_key_type` is the type of `private_key`.
3256 * For example, for EC keys, this means that peer_key
3257 * is interpreted as a point on the curve that the
3258 * private key is on. The standard formats for public
3259 * keys are documented in the documentation of
3260 * psa_export_public_key().
Gilles Peskine01d718c2018-09-18 12:01:02 +02003261 * \param peer_key_length Size of \p peer_key in bytes.
Gilles Peskine01d718c2018-09-18 12:01:02 +02003262 *
3263 * \retval #PSA_SUCCESS
3264 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01003265 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02003266 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine01d718c2018-09-18 12:01:02 +02003267 * \retval #PSA_ERROR_NOT_PERMITTED
3268 * \retval #PSA_ERROR_INVALID_ARGUMENT
3269 * \c private_key is not compatible with \c alg,
3270 * or \p peer_key is not valid for \c alg or not compatible with
3271 * \c private_key.
3272 * \retval #PSA_ERROR_NOT_SUPPORTED
3273 * \c alg is not supported or is not a key derivation algorithm.
3274 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3275 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3276 * \retval #PSA_ERROR_HARDWARE_FAILURE
3277 * \retval #PSA_ERROR_TAMPERING_DETECTED
3278 */
3279psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003280 psa_key_derivation_step_t step,
Gilles Peskineae32aac2018-11-30 14:39:32 +01003281 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02003282 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003283 size_t peer_key_length);
Gilles Peskine01d718c2018-09-18 12:01:02 +02003284
Gilles Peskine769c7a62019-01-18 16:42:29 +01003285/** Perform a key agreement and use the shared secret as input to a key
3286 * derivation.
3287 *
3288 * A key agreement algorithm takes two inputs: a private key \p private_key
3289 * a public key \p peer_key.
3290 *
3291 * \warning The raw result of a key agreement algorithm such as finite-field
3292 * Diffie-Hellman or elliptic curve Diffie-Hellman has biases and should
3293 * not be used directly as key material. It should instead be passed as
3294 * input to a key derivation algorithm. To chain a key agreement with
3295 * a key derivation, use psa_key_agreement() and other functions from
3296 * the key derivation and generator interface.
3297 *
Gilles Peskine47e79fb2019-02-08 11:24:59 +01003298 * \param alg The key agreement algorithm to compute
3299 * (\c PSA_ALG_XXX value such that
3300 * #PSA_ALG_IS_RAW_KEY_AGREEMENT(\p alg)
3301 * is true).
Gilles Peskine769c7a62019-01-18 16:42:29 +01003302 * \param private_key Handle to the private key to use.
3303 * \param[in] peer_key Public key of the peer. It must be
3304 * in the same format that psa_import_key()
3305 * accepts. The standard formats for public
3306 * keys are documented in the documentation
3307 * of psa_export_public_key().
3308 * \param peer_key_length Size of \p peer_key in bytes.
3309 * \param[out] output Buffer where the decrypted message is to
3310 * be written.
3311 * \param output_size Size of the \c output buffer in bytes.
3312 * \param[out] output_length On success, the number of bytes
3313 * that make up the returned output.
3314 *
3315 * \retval #PSA_SUCCESS
3316 * Success.
3317 * \retval #PSA_ERROR_INVALID_HANDLE
3318 * \retval #PSA_ERROR_EMPTY_SLOT
3319 * \retval #PSA_ERROR_NOT_PERMITTED
3320 * \retval #PSA_ERROR_INVALID_ARGUMENT
3321 * \p alg is not a key agreement algorithm
3322 * \retval #PSA_ERROR_INVALID_ARGUMENT
3323 * \p private_key is not compatible with \p alg,
3324 * or \p peer_key is not valid for \p alg or not compatible with
3325 * \p private_key.
3326 * \retval #PSA_ERROR_NOT_SUPPORTED
3327 * \p alg is not a supported key agreement algorithm.
3328 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
3329 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3330 * \retval #PSA_ERROR_HARDWARE_FAILURE
3331 * \retval #PSA_ERROR_TAMPERING_DETECTED
3332 */
3333psa_status_t psa_key_agreement_raw_shared_secret(psa_algorithm_t alg,
3334 psa_key_handle_t private_key,
3335 const uint8_t *peer_key,
3336 size_t peer_key_length,
3337 uint8_t *output,
3338 size_t output_size,
3339 size_t *output_length);
Gilles Peskine4c317f42018-07-12 01:24:09 +02003340
3341/**@}*/
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003342
3343/** \defgroup random Random generation
3344 * @{
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003345 */
3346
3347/**
3348 * \brief Generate random bytes.
3349 *
Gilles Peskine53d991e2018-07-12 01:14:59 +02003350 * \warning This function **can** fail! Callers MUST check the return status
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02003351 * and MUST NOT use the content of the output buffer if the return
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003352 * status is not #PSA_SUCCESS.
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003353 *
3354 * \note To generate a key, use psa_generate_key() instead.
3355 *
3356 * \param[out] output Output buffer for the generated data.
3357 * \param output_size Number of bytes to generate and output.
3358 *
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003359 * \retval #PSA_SUCCESS
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003360 * \retval #PSA_ERROR_NOT_SUPPORTED
3361 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinefa4070c2018-07-12 19:23:03 +02003362 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003363 * \retval #PSA_ERROR_HARDWARE_FAILURE
3364 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskinedda3bd32018-07-12 19:40:46 +02003365 * \retval #PSA_ERROR_BAD_STATE
3366 * The library has not been previously initialized by psa_crypto_init().
3367 * It is implementation-dependent whether a failure to initialize
Gilles Peskine3fa675c2018-07-12 01:31:03 +02003368 * results in this error code.
3369 */
3370psa_status_t psa_generate_random(uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003371 size_t output_size);
3372
3373/** Extra parameters for RSA key generation.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003374 *
Gilles Peskine28538492018-07-11 17:34:00 +02003375 * You may pass a pointer to a structure of this type as the \c extra
3376 * parameter to psa_generate_key().
3377 */
3378typedef struct {
3379 uint32_t e; /**< Public exponent value. Default: 65537. */
3380} psa_generate_key_extra_rsa;
3381
3382/**
itayzafrir90d8c7a2018-09-12 11:44:52 +03003383 * \brief Generate a key or key pair.
itayzafrir18617092018-09-16 12:22:41 +03003384 *
Gilles Peskine20628592019-04-19 19:29:50 +02003385 * \param[in] attributes The attributes for the new key.
3386 * The key size field in \p attributes is
3387 * ignored; the actual key size is taken
3388 * from the \p bits parameter instead.
3389 * \param[out] handle On success, a handle to the newly created key.
3390 * \c 0 on failure.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003391 * \param bits Key size in bits.
3392 * \param[in] extra Extra parameters for key generation. The
3393 * interpretation of this parameter depends on
Gilles Peskine20628592019-04-19 19:29:50 +02003394 * the key type \c type. All types support \c NULL to
3395 * use default parameters. Implementation that support
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003396 * the generation of vendor-specific key types
3397 * that allow extra parameters shall document
3398 * the format of these extra parameters and
3399 * the default values. For standard parameters,
3400 * the meaning of \p extra is as follows:
3401 * - For a symmetric key type (a type such
Gilles Peskine20628592019-04-19 19:29:50 +02003402 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\c type) is
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003403 * false), \p extra must be \c NULL.
3404 * - For an elliptic curve key type (a type
Gilles Peskine20628592019-04-19 19:29:50 +02003405 * such that #PSA_KEY_TYPE_IS_ECC(\c type) is
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003406 * false), \p extra must be \c NULL.
Gilles Peskine20628592019-04-19 19:29:50 +02003407 * - For an RSA key (\c type is
Gilles Peskine53d991e2018-07-12 01:14:59 +02003408 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
3409 * optional #psa_generate_key_extra_rsa structure
Gilles Peskine9e7dc712018-03-28 14:18:50 +02003410 * specifying the public exponent. The
3411 * default public exponent used when \p extra
3412 * is \c NULL is 65537.
Gilles Peskine20628592019-04-19 19:29:50 +02003413 * - For an DSA key (\c type is
Jaeden Amero1308fb52019-01-11 13:50:43 +00003414 * #PSA_KEY_TYPE_DSA_KEYPAIR), \p extra is an
3415 * optional structure specifying the key domain
3416 * parameters. The key domain parameters can also be
3417 * provided by psa_set_key_domain_parameters(),
3418 * which documents the format of the structure.
Gilles Peskine20628592019-04-19 19:29:50 +02003419 * - For a DH key (\c type is
Jaeden Amero8851c402019-01-11 14:20:03 +00003420 * #PSA_KEY_TYPE_DH_KEYPAIR), the \p extra is an
3421 * optional structure specifying the key domain
3422 * parameters. The key domain parameters can also be
3423 * provided by psa_set_key_domain_parameters(),
3424 * which documents the format of the structure.
Gilles Peskinee59236f2018-01-27 23:32:46 +01003425 * \param extra_size Size of the buffer that \p extra
3426 * points to, in bytes. Note that if \p extra is
3427 * \c NULL then \p extra_size must be zero.
3428 *
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003429 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01003430 * Success.
3431 * If the key is persistent, the key material and the key's metadata
3432 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02003433 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine20628592019-04-19 19:29:50 +02003434 * This is an attempt to create a persistent key, and there is
3435 * already a persistent key with the given identifier.
Gilles Peskine0cad07c2018-06-27 19:49:02 +02003436 * \retval #PSA_ERROR_NOT_SUPPORTED
3437 * \retval #PSA_ERROR_INVALID_ARGUMENT
3438 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003439 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
3440 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
3441 * \retval #PSA_ERROR_HARDWARE_FAILURE
3442 * \retval #PSA_ERROR_TAMPERING_DETECTED
3443 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03003444 * The library has not been previously initialized by psa_crypto_init().
3445 * It is implementation-dependent whether a failure to initialize
3446 * results in this error code.
Gilles Peskinee59236f2018-01-27 23:32:46 +01003447 */
Gilles Peskine87a5e562019-04-17 12:28:25 +02003448psa_status_t psa_generate_key(const psa_key_attributes_t *attributes,
3449 psa_key_handle_t *handle,
Gilles Peskinee59236f2018-01-27 23:32:46 +01003450 size_t bits,
3451 const void *extra,
3452 size_t extra_size);
3453
3454/**@}*/
3455
3456#ifdef __cplusplus
3457}
3458#endif
3459
3460/* The file "crypto_sizes.h" contains definitions for size calculation
3461 * macros whose definitions are implementation-specific. */
3462#include "crypto_sizes.h"
3463
3464/* The file "crypto_struct.h" contains definitions for
3465 * implementation-specific structs that are declared above. */
3466#include "crypto_struct.h"
3467
3468/* The file "crypto_extra.h" contains vendor-specific definitions. This
3469 * can include vendor-defined algorithms, extra functions, etc. */
3470#include "crypto_extra.h"
3471
3472#endif /* PSA_CRYPTO_H */