blob: 25c3cb4dbd70d6295f2be755fde55a7160c8d130 [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 Peskinef603c712019-01-19 13:40:11 +010096/** \defgroup policy Key policies
97 * @{
98 */
99
100/** The type of the key policy data structure.
101 *
102 * Before calling any function on a key policy, the application must initialize
103 * it by any of the following means:
104 * - Set the structure to all-bits-zero, for example:
105 * \code
106 * psa_key_policy_t policy;
107 * memset(&policy, 0, sizeof(policy));
108 * \endcode
109 * - Initialize the structure to logical zero values, for example:
110 * \code
111 * psa_key_policy_t policy = {0};
112 * \endcode
113 * - Initialize the structure to the initializer #PSA_KEY_POLICY_INIT,
114 * for example:
115 * \code
116 * psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
117 * \endcode
118 * - Assign the result of the function psa_key_policy_init()
119 * to the structure, for example:
120 * \code
121 * psa_key_policy_t policy;
122 * policy = psa_key_policy_init();
123 * \endcode
124 *
125 * This is an implementation-defined \c struct. Applications should not
126 * make any assumptions about the content of this structure except
127 * as directed by the documentation of a specific implementation. */
128typedef struct psa_key_policy_s psa_key_policy_t;
129
130/** \def PSA_KEY_POLICY_INIT
131 *
132 * This macro returns a suitable initializer for a key policy object of type
133 * #psa_key_policy_t.
134 */
135#ifdef __DOXYGEN_ONLY__
136/* This is an example definition for documentation purposes.
137 * Implementations should define a suitable value in `crypto_struct.h`.
138 */
139#define PSA_KEY_POLICY_INIT {0}
140#endif
141
142/** Return an initial value for a key policy that forbids all usage of the key.
143 */
144static psa_key_policy_t psa_key_policy_init(void);
145
146/** \brief Set the standard fields of a policy structure.
147 *
148 * Note that this function does not make any consistency check of the
149 * parameters. The values are only checked when applying the policy to
150 * a key slot with psa_set_key_policy().
151 *
152 * \param[in,out] policy The key policy to modify. It must have been
153 * initialized as per the documentation for
154 * #psa_key_policy_t.
155 * \param usage The permitted uses for the key.
156 * \param alg The algorithm that the key may be used for.
157 */
158void psa_key_policy_set_usage(psa_key_policy_t *policy,
159 psa_key_usage_t usage,
160 psa_algorithm_t alg);
161
162/** \brief Retrieve the usage field of a policy structure.
163 *
164 * \param[in] policy The policy object to query.
165 *
166 * \return The permitted uses for a key with this policy.
167 */
168psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
169
170/** \brief Retrieve the algorithm field of a policy structure.
171 *
172 * \param[in] policy The policy object to query.
173 *
174 * \return The permitted algorithm for a key with this policy.
175 */
176psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
177
178/** \brief Set the usage policy on a key slot.
179 *
180 * This function must be called on an empty key slot, before importing,
181 * generating or creating a key in the slot. Changing the policy of an
182 * existing key is not permitted.
183 *
184 * Implementations may set restrictions on supported key policies
185 * depending on the key type and the key slot.
186 *
187 * \param handle Handle to the key whose policy is to be changed.
188 * \param[in] policy The policy object to query.
189 *
190 * \retval #PSA_SUCCESS
191 * Success.
192 * If the key is persistent, it is implementation-defined whether
193 * the policy has been saved to persistent storage. Implementations
194 * may defer saving the policy until the key material is created.
195 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200196 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskinef603c712019-01-19 13:40:11 +0100197 * \retval #PSA_ERROR_NOT_SUPPORTED
198 * \retval #PSA_ERROR_INVALID_ARGUMENT
199 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
200 * \retval #PSA_ERROR_HARDWARE_FAILURE
201 * \retval #PSA_ERROR_TAMPERING_DETECTED
202 * \retval #PSA_ERROR_BAD_STATE
203 * The library has not been previously initialized by psa_crypto_init().
204 * It is implementation-dependent whether a failure to initialize
205 * results in this error code.
206 */
207psa_status_t psa_set_key_policy(psa_key_handle_t handle,
208 const psa_key_policy_t *policy);
209
210/** \brief Get the usage policy for a key slot.
211 *
212 * \param handle Handle to the key slot whose policy is being queried.
213 * \param[out] policy On success, the key's policy.
214 *
215 * \retval #PSA_SUCCESS
216 * \retval #PSA_ERROR_INVALID_HANDLE
217 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
218 * \retval #PSA_ERROR_HARDWARE_FAILURE
219 * \retval #PSA_ERROR_TAMPERING_DETECTED
220 * \retval #PSA_ERROR_BAD_STATE
221 * The library has not been previously initialized by psa_crypto_init().
222 * It is implementation-dependent whether a failure to initialize
223 * results in this error code.
224 */
225psa_status_t psa_get_key_policy(psa_key_handle_t handle,
226 psa_key_policy_t *policy);
227
228/**@}*/
229
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100230/** \defgroup key_management Key management
231 * @{
232 */
233
Gilles Peskineae32aac2018-11-30 14:39:32 +0100234/** \brief Retrieve the lifetime of an open key.
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100235 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100236 * \param handle Handle to query.
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100237 * \param[out] lifetime On success, the lifetime value.
238 *
239 * \retval #PSA_SUCCESS
240 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100241 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100242 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
243 * \retval #PSA_ERROR_HARDWARE_FAILURE
244 * \retval #PSA_ERROR_TAMPERING_DETECTED
245 * \retval #PSA_ERROR_BAD_STATE
246 * The library has not been previously initialized by psa_crypto_init().
247 * It is implementation-dependent whether a failure to initialize
248 * results in this error code.
249 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100250psa_status_t psa_get_key_lifetime(psa_key_handle_t handle,
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100251 psa_key_lifetime_t *lifetime);
252
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100253
Gilles Peskinef535eb22018-11-30 14:08:36 +0100254/** Allocate a key slot for a transient key, i.e. a key which is only stored
255 * in volatile memory.
256 *
257 * The allocated key slot and its handle remain valid until the
258 * application calls psa_close_key() or psa_destroy_key() or until the
259 * application terminates.
260 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100261 * \param[out] handle On success, a handle to a volatile key slot.
262 *
263 * \retval #PSA_SUCCESS
264 * Success. The application can now use the value of `*handle`
265 * to access the newly allocated key slot.
266 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
267 * There was not enough memory, or the maximum number of key slots
268 * has been reached.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100269 */
Gilles Peskined40c1fb2019-01-19 12:20:52 +0100270psa_status_t psa_allocate_key(psa_key_handle_t *handle);
Gilles Peskinef535eb22018-11-30 14:08:36 +0100271
272/** Open a handle to an existing persistent key.
273 *
274 * Open a handle to a key which was previously created with psa_create_key().
275 *
276 * \param lifetime The lifetime of the key. This designates a storage
277 * area where the key material is stored. This must not
278 * be #PSA_KEY_LIFETIME_VOLATILE.
279 * \param id The persistent identifier of the key.
280 * \param[out] handle On success, a handle to a key slot which contains
281 * the data and metadata loaded from the specified
282 * persistent location.
283 *
284 * \retval #PSA_SUCCESS
285 * Success. The application can now use the value of `*handle`
286 * to access the newly allocated key slot.
287 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
David Saadab4ecc272019-02-14 13:48:10 +0200288 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskinef535eb22018-11-30 14:08:36 +0100289 * \retval #PSA_ERROR_INVALID_ARGUMENT
290 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
291 * \retval #PSA_ERROR_INVALID_ARGUMENT
292 * \p id is invalid for the specified lifetime.
293 * \retval #PSA_ERROR_NOT_SUPPORTED
294 * \p lifetime is not supported.
295 * \retval #PSA_ERROR_NOT_PERMITTED
296 * The specified key exists, but the application does not have the
297 * permission to access it. Note that this specification does not
298 * define any way to create such a key, but it may be possible
299 * through implementation-specific means.
300 */
301psa_status_t psa_open_key(psa_key_lifetime_t lifetime,
302 psa_key_id_t id,
303 psa_key_handle_t *handle);
304
305/** Create a new persistent key slot.
306 *
307 * Create a new persistent key slot and return a handle to it. The handle
308 * remains valid until the application calls psa_close_key() or terminates.
309 * The application can open the key again with psa_open_key() until it
310 * removes the key by calling psa_destroy_key().
311 *
312 * \param lifetime The lifetime of the key. This designates a storage
313 * area where the key material is stored. This must not
314 * be #PSA_KEY_LIFETIME_VOLATILE.
315 * \param id The persistent identifier of the key.
Gilles Peskinef535eb22018-11-30 14:08:36 +0100316 * \param[out] handle On success, a handle to the newly created key slot.
317 * When key material is later created in this key slot,
318 * it will be saved to the specified persistent location.
319 *
320 * \retval #PSA_SUCCESS
321 * Success. The application can now use the value of `*handle`
322 * to access the newly allocated key slot.
323 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
324 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
David Saadab4ecc272019-02-14 13:48:10 +0200325 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskinef535eb22018-11-30 14:08:36 +0100326 * There is already a key with the identifier \p id in the storage
327 * area designated by \p lifetime.
328 * \retval #PSA_ERROR_INVALID_ARGUMENT
329 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
330 * \retval #PSA_ERROR_INVALID_ARGUMENT
331 * \p id is invalid for the specified lifetime.
332 * \retval #PSA_ERROR_NOT_SUPPORTED
333 * \p lifetime is not supported.
334 * \retval #PSA_ERROR_NOT_PERMITTED
335 * \p lifetime is valid, but the application does not have the
336 * permission to create a key there.
337 */
338psa_status_t psa_create_key(psa_key_lifetime_t lifetime,
339 psa_key_id_t id,
Gilles Peskinef535eb22018-11-30 14:08:36 +0100340 psa_key_handle_t *handle);
341
342/** Close a key handle.
343 *
344 * If the handle designates a volatile key, destroy the key material and
345 * free all associated resources, just like psa_destroy_key().
346 *
347 * If the handle designates a persistent key, free all resources associated
348 * with the key in volatile memory. The key slot in persistent storage is
349 * not affected and can be opened again later with psa_open_key().
350 *
351 * \param handle The key handle to close.
352 *
353 * \retval #PSA_SUCCESS
354 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100355 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100356 */
357psa_status_t psa_close_key(psa_key_handle_t handle);
358
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100359/**@}*/
360
361/** \defgroup import_export Key import and export
362 * @{
363 */
364
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100365/**
366 * \brief Import a key in binary format.
367 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100368 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100369 * documentation of psa_export_public_key() for the format of public keys
370 * and to the documentation of psa_export_key() for the format for
371 * other key types.
372 *
373 * This specification supports a single format for each key type.
374 * Implementations may support other formats as long as the standard
375 * format is supported. Implementations that support other formats
376 * should ensure that the formats are clearly unambiguous so as to
377 * minimize the risk that an invalid input is accidentally interpreted
378 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100379 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100380 * \param handle Handle to the slot where the key will be stored.
Gilles Peskined40c1fb2019-01-19 12:20:52 +0100381 * It must have been obtained by calling
382 * psa_allocate_key() or psa_create_key() and must
383 * not contain key material yet.
Gilles Peskinef7933932018-10-31 14:07:52 +0100384 * \param type Key type (a \c PSA_KEY_TYPE_XXX value). On a successful
385 * import, the key slot will contain a key of this type.
386 * \param[in] data Buffer containing the key data. The content of this
387 * buffer is interpreted according to \p type. It must
388 * contain the format described in the documentation
389 * of psa_export_key() or psa_export_public_key() for
390 * the chosen type.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200391 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100392 *
Gilles Peskine28538492018-07-11 17:34:00 +0200393 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100394 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100395 * If the key is persistent, the key material and the key's metadata
396 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100397 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200398 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200399 * The key type or key size is not supported, either by the
400 * implementation in general or in this particular slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200401 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine308b91d2018-02-08 09:47:44 +0100402 * The key slot is invalid,
403 * or the key data is not correctly formatted.
David Saadab4ecc272019-02-14 13:48:10 +0200404 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200405 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200406 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
407 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
408 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100409 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200410 * \retval #PSA_ERROR_HARDWARE_FAILURE
411 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300412 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300413 * The library has not been previously initialized by psa_crypto_init().
414 * It is implementation-dependent whether a failure to initialize
415 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100416 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100417psa_status_t psa_import_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100418 psa_key_type_t type,
419 const uint8_t *data,
420 size_t data_length);
421
422/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100423 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200424 *
425 * This function destroys the content of the key slot from both volatile
426 * memory and, if applicable, non-volatile storage. Implementations shall
427 * make a best effort to ensure that any previous content of the slot is
428 * unrecoverable.
429 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100430 * This function also erases any metadata such as policies and frees all
431 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200432 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100433 * \param handle Handle to the key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100434 *
Gilles Peskine28538492018-07-11 17:34:00 +0200435 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200436 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200437 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200438 * The slot holds content and cannot be erased because it is
439 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100440 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200441 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200442 * There was an failure in communication with the cryptoprocessor.
443 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200444 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200445 * The storage is corrupted. Implementations shall make a best effort
446 * to erase key material even in this stage, however applications
447 * should be aware that it may be impossible to guarantee that the
448 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +0200449 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200450 * An unexpected condition which is not a storage corruption or
451 * a communication failure occurred. The cryptoprocessor may have
452 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300453 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300454 * The library has not been previously initialized by psa_crypto_init().
455 * It is implementation-dependent whether a failure to initialize
456 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100457 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100458psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100459
460/**
461 * \brief Get basic metadata about a key.
462 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100463 * \param handle Handle to the key slot to query.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200464 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100465 * This may be a null pointer, in which case the key type
466 * is not written.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200467 * \param[out] bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +0100468 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +0100469 * is not written.
470 *
Gilles Peskine28538492018-07-11 17:34:00 +0200471 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100472 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200473 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskineae32aac2018-11-30 14:39:32 +0100474 * The handle is to a key slot which does not contain key material yet.
Gilles Peskine28538492018-07-11 17:34:00 +0200475 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
476 * \retval #PSA_ERROR_HARDWARE_FAILURE
477 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300478 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300479 * The library has not been previously initialized by psa_crypto_init().
480 * It is implementation-dependent whether a failure to initialize
481 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100482 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100483psa_status_t psa_get_key_information(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100484 psa_key_type_t *type,
485 size_t *bits);
486
487/**
488 * \brief Export a key in binary format.
489 *
490 * The output of this function can be passed to psa_import_key() to
491 * create an equivalent object.
492 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100493 * If the implementation of psa_import_key() supports other formats
494 * beyond the format specified here, the output from psa_export_key()
495 * must use the representation specified here, not the original
496 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100497 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100498 * For standard key types, the output format is as follows:
499 *
500 * - For symmetric keys (including MAC keys), the format is the
501 * raw bytes of the key.
502 * - For DES, the key data consists of 8 bytes. The parity bits must be
503 * correct.
504 * - For Triple-DES, the format is the concatenation of the
505 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100506 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200507 * is the non-encrypted DER encoding of the representation defined by
508 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
509 * ```
510 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200511 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200512 * modulus INTEGER, -- n
513 * publicExponent INTEGER, -- e
514 * privateExponent INTEGER, -- d
515 * prime1 INTEGER, -- p
516 * prime2 INTEGER, -- q
517 * exponent1 INTEGER, -- d mod (p-1)
518 * exponent2 INTEGER, -- d mod (q-1)
519 * coefficient INTEGER, -- (inverse of q) mod p
520 * }
521 * ```
522 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format
523 * is the non-encrypted DER encoding of the representation used by
Gilles Peskinec6290c02018-08-13 17:24:59 +0200524 * OpenSSL and OpenSSH, whose structure is described in ASN.1 as follows:
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200525 * ```
526 * DSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200527 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200528 * prime INTEGER, -- p
529 * subprime INTEGER, -- q
530 * generator INTEGER, -- g
531 * public INTEGER, -- y
532 * private INTEGER, -- x
533 * }
534 * ```
535 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +0100536 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100537 * a representation of the private value as a `ceiling(m/8)`-byte string
538 * where `m` is the bit size associated with the curve, i.e. the bit size
539 * of the order of the curve's coordinate field. This byte string is
540 * in little-endian order for Montgomery curves (curve types
541 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
542 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
543 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100544 * This is the content of the `privateKey` field of the `ECPrivateKey`
545 * format defined by RFC 5915.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200546 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
547 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100548 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100549 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200550 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200551 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200552 * \param[out] data_length On success, the number of bytes
553 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100554 *
Gilles Peskine28538492018-07-11 17:34:00 +0200555 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100556 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200557 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200558 * \retval #PSA_ERROR_NOT_PERMITTED
Darryl Green9e2d7a02018-07-24 16:33:30 +0100559 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200560 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
561 * The size of the \p data buffer is too small. You can determine a
562 * sufficient buffer size by calling
563 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
564 * where \c type is the key type
565 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200566 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
567 * \retval #PSA_ERROR_HARDWARE_FAILURE
568 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300569 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300570 * The library has not been previously initialized by psa_crypto_init().
571 * It is implementation-dependent whether a failure to initialize
572 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100573 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100574psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100575 uint8_t *data,
576 size_t data_size,
577 size_t *data_length);
578
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100579/**
580 * \brief Export a public key or the public part of a key pair in binary format.
581 *
582 * The output of this function can be passed to psa_import_key() to
583 * create an object that is equivalent to the public key.
584 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000585 * This specification supports a single format for each key type.
586 * Implementations may support other formats as long as the standard
587 * format is supported. Implementations that support other formats
588 * should ensure that the formats are clearly unambiguous so as to
589 * minimize the risk that an invalid input is accidentally interpreted
590 * according to a different format.
591 *
Jaeden Amero25384a22019-01-10 10:23:21 +0000592 * For standard key types, the output format is as follows:
593 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
594 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
595 * ```
596 * RSAPublicKey ::= SEQUENCE {
597 * modulus INTEGER, -- n
598 * publicExponent INTEGER } -- e
599 * ```
Jaeden Ameroccdce902019-01-10 11:42:27 +0000600 * - For elliptic curve public keys (key types for which
601 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
602 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint:
603 * Let `m` be the bit size associated with the curve, i.e. the bit size of
604 * `q` for a curve over `F_q`. The representation consists of:
605 * - The byte 0x04;
606 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
607 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Jaeden Amero25384a22019-01-10 10:23:21 +0000608 *
609 * For other public key types, the format is the DER representation defined by
610 * RFC 5280 as `SubjectPublicKeyInfo`, with the `subjectPublicKey` format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200611 * specified below.
612 * ```
613 * SubjectPublicKeyInfo ::= SEQUENCE {
614 * algorithm AlgorithmIdentifier,
615 * subjectPublicKey BIT STRING }
616 * AlgorithmIdentifier ::= SEQUENCE {
617 * algorithm OBJECT IDENTIFIER,
618 * parameters ANY DEFINED BY algorithm OPTIONAL }
619 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200620 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY),
621 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.2 as
622 * `DSAPublicKey`,
623 * with the OID `id-dsa`,
624 * and with the parameters `DSS-Parms`.
625 * ```
626 * id-dsa OBJECT IDENTIFIER ::= {
627 * iso(1) member-body(2) us(840) x9-57(10040) x9cm(4) 1 }
628 *
629 * Dss-Parms ::= SEQUENCE {
630 * p INTEGER,
631 * q INTEGER,
632 * g INTEGER }
633 * DSAPublicKey ::= INTEGER -- public key, Y
634 * ```
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100635 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100636 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200637 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200638 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200639 * \param[out] data_length On success, the number of bytes
640 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100641 *
Gilles Peskine28538492018-07-11 17:34:00 +0200642 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100643 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200644 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +0200645 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200646 * The key is neither a public key nor a key pair.
647 * \retval #PSA_ERROR_NOT_SUPPORTED
648 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
649 * The size of the \p data buffer is too small. You can determine a
650 * sufficient buffer size by calling
651 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
652 * where \c type is the key type
653 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200654 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
655 * \retval #PSA_ERROR_HARDWARE_FAILURE
656 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300657 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300658 * The library has not been previously initialized by psa_crypto_init().
659 * It is implementation-dependent whether a failure to initialize
660 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100661 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100662psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100663 uint8_t *data,
664 size_t data_size,
665 size_t *data_length);
666
Gilles Peskinef603c712019-01-19 13:40:11 +0100667/** Make a copy of a key.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100668 *
Gilles Peskinef603c712019-01-19 13:40:11 +0100669 * Copy key material from one location to another.
Jaeden Amero70261c52019-01-04 11:47:20 +0000670 *
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100671 * This function is primarily useful to copy a key from one location
672 * to another, since it populates a key using the material from
673 * another key which may have a different lifetime.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200674 *
Gilles Peskinef603c712019-01-19 13:40:11 +0100675 * In an implementation where slots have different ownerships,
Gilles Peskineaec5a7f2019-02-05 20:26:09 +0100676 * this function may be used to share a key with a different party,
Gilles Peskinef603c712019-01-19 13:40:11 +0100677 * subject to implementation-defined restrictions on key sharing.
678 * In this case \p constraint would typically prevent the recipient
679 * from exporting the key.
Gilles Peskine7e198532018-03-08 07:50:30 +0100680 *
Gilles Peskinef603c712019-01-19 13:40:11 +0100681 * The resulting key may only be used in a way that conforms to all
682 * three of: the policy of the source key, the policy previously set
683 * on the target, and the \p constraint parameter passed when calling
684 * this function.
685 * - The usage flags on the resulting key are the bitwise-and of the
686 * usage flags on the source policy, the previously-set target policy
687 * and the policy constraint.
688 * - If all three policies allow the same algorithm or wildcard-based
689 * algorithm policy, the resulting key has the same algorithm policy.
690 * - If one of the policies allows an algorithm and all the other policies
691 * either allow the same algorithm or a wildcard-based algorithm policy
692 * that includes this algorithm, the resulting key allows the same
693 * algorithm.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200694 *
Gilles Peskinef603c712019-01-19 13:40:11 +0100695 * The effect of this function on implementation-defined metadata is
696 * implementation-defined.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200697 *
Gilles Peskinef603c712019-01-19 13:40:11 +0100698 * \param source_handle The key to copy. It must be a handle to an
699 * occupied slot.
700 * \param target_handle A handle to the target slot. It must not contain
701 * key material yet.
702 * \param[in] constraint An optional policy constraint. If this parameter
703 * is non-null then the resulting key will conform
704 * to this policy in addition to the source policy
705 * and the policy already present on the target
706 * slot. If this parameter is null then the
707 * function behaves in the same way as if it was
708 * the target policy, i.e. only the source and
709 * target policies apply.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200710 *
711 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100712 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +0200713 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskinef603c712019-01-19 13:40:11 +0100714 * \p target already contains key material.
David Saadab4ecc272019-02-14 13:48:10 +0200715 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskinef603c712019-01-19 13:40:11 +0100716 * \p source does not contain key material.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200717 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinef603c712019-01-19 13:40:11 +0100718 * The policy constraints on the source, on the target and
719 * \p constraints are incompatible.
720 * \retval #PSA_ERROR_NOT_PERMITTED
721 * The source key is not exportable and its lifetime does not
722 * allow copying it to the target's lifetime.
723 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
724 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200725 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
726 * \retval #PSA_ERROR_HARDWARE_FAILURE
727 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100728 */
Gilles Peskinef603c712019-01-19 13:40:11 +0100729psa_status_t psa_copy_key(psa_key_handle_t source_handle,
730 psa_key_handle_t target_handle,
731 const psa_key_policy_t *constraint);
Gilles Peskine20035e32018-02-03 22:44:14 +0100732
733/**@}*/
734
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100735/** \defgroup hash Message digests
736 * @{
737 */
738
Gilles Peskine308b91d2018-02-08 09:47:44 +0100739/** The type of the state data structure for multipart hash operations.
740 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000741 * Before calling any function on a hash operation object, the application must
742 * initialize it by any of the following means:
743 * - Set the structure to all-bits-zero, for example:
744 * \code
745 * psa_hash_operation_t operation;
746 * memset(&operation, 0, sizeof(operation));
747 * \endcode
748 * - Initialize the structure to logical zero values, for example:
749 * \code
750 * psa_hash_operation_t operation = {0};
751 * \endcode
752 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
753 * for example:
754 * \code
755 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
756 * \endcode
757 * - Assign the result of the function psa_hash_operation_init()
758 * to the structure, for example:
759 * \code
760 * psa_hash_operation_t operation;
761 * operation = psa_hash_operation_init();
762 * \endcode
763 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100764 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100765 * make any assumptions about the content of this structure except
766 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100767typedef struct psa_hash_operation_s psa_hash_operation_t;
768
Jaeden Amero6a25b412019-01-04 11:47:44 +0000769/** \def PSA_HASH_OPERATION_INIT
770 *
771 * This macro returns a suitable initializer for a hash operation object
772 * of type #psa_hash_operation_t.
773 */
774#ifdef __DOXYGEN_ONLY__
775/* This is an example definition for documentation purposes.
776 * Implementations should define a suitable value in `crypto_struct.h`.
777 */
778#define PSA_HASH_OPERATION_INIT {0}
779#endif
780
781/** Return an initial value for a hash operation object.
782 */
783static psa_hash_operation_t psa_hash_operation_init(void);
784
Gilles Peskine308b91d2018-02-08 09:47:44 +0100785/** Start a multipart hash operation.
786 *
787 * The sequence of operations to calculate a hash (message digest)
788 * is as follows:
789 * -# Allocate an operation object which will be passed to all the functions
790 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000791 * -# Initialize the operation object with one of the methods described in the
792 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200793 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100794 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100795 * of the message each time. The hash that is calculated is the hash
796 * of the concatenation of these messages in order.
797 * -# To calculate the hash, call psa_hash_finish().
798 * To compare the hash with an expected value, call psa_hash_verify().
799 *
800 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000801 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100802 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200803 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100804 * eventually terminate the operation. The following events terminate an
805 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100806 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100807 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100808 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000809 * \param[in,out] operation The operation object to set up. It must have
810 * been initialized as per the documentation for
811 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200812 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
813 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100814 *
Gilles Peskine28538492018-07-11 17:34:00 +0200815 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100816 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200817 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200818 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +0200819 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
820 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
821 * \retval #PSA_ERROR_HARDWARE_FAILURE
822 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100823 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200824psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100825 psa_algorithm_t alg);
826
Gilles Peskine308b91d2018-02-08 09:47:44 +0100827/** Add a message fragment to a multipart hash operation.
828 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200829 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100830 *
831 * If this function returns an error status, the operation becomes inactive.
832 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200833 * \param[in,out] operation Active hash operation.
834 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200835 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100836 *
Gilles Peskine28538492018-07-11 17:34:00 +0200837 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100838 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200839 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100840 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200841 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
842 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
843 * \retval #PSA_ERROR_HARDWARE_FAILURE
844 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100845 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100846psa_status_t psa_hash_update(psa_hash_operation_t *operation,
847 const uint8_t *input,
848 size_t input_length);
849
Gilles Peskine308b91d2018-02-08 09:47:44 +0100850/** Finish the calculation of the hash of a message.
851 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200852 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100853 * This function calculates the hash of the message formed by concatenating
854 * the inputs passed to preceding calls to psa_hash_update().
855 *
856 * When this function returns, the operation becomes inactive.
857 *
858 * \warning Applications should not call this function if they expect
859 * a specific value for the hash. Call psa_hash_verify() instead.
860 * Beware that comparing integrity or authenticity data such as
861 * hash values with a function such as \c memcmp is risky
862 * because the time taken by the comparison may leak information
863 * about the hashed data which could allow an attacker to guess
864 * a valid hash and thereby bypass security controls.
865 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200866 * \param[in,out] operation Active hash operation.
867 * \param[out] hash Buffer where the hash is to be written.
868 * \param hash_size Size of the \p hash buffer in bytes.
869 * \param[out] hash_length On success, the number of bytes
870 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +0200871 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +0200872 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100873 *
Gilles Peskine28538492018-07-11 17:34:00 +0200874 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100875 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200876 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100877 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200878 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200879 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200880 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100881 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +0200882 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
883 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
884 * \retval #PSA_ERROR_HARDWARE_FAILURE
885 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100886 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100887psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
888 uint8_t *hash,
889 size_t hash_size,
890 size_t *hash_length);
891
Gilles Peskine308b91d2018-02-08 09:47:44 +0100892/** Finish the calculation of the hash of a message and compare it with
893 * an expected value.
894 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200895 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100896 * This function calculates the hash of the message formed by concatenating
897 * the inputs passed to preceding calls to psa_hash_update(). It then
898 * compares the calculated hash with the expected hash passed as a
899 * parameter to this function.
900 *
901 * When this function returns, the operation becomes inactive.
902 *
Gilles Peskine19067982018-03-20 17:54:53 +0100903 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +0100904 * comparison between the actual hash and the expected hash is performed
905 * in constant time.
906 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200907 * \param[in,out] operation Active hash operation.
908 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200909 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100910 *
Gilles Peskine28538492018-07-11 17:34:00 +0200911 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100912 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +0200913 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100914 * The hash of the message was calculated successfully, but it
915 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +0200916 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100917 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200918 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
919 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
920 * \retval #PSA_ERROR_HARDWARE_FAILURE
921 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100922 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100923psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
924 const uint8_t *hash,
925 size_t hash_length);
926
Gilles Peskine308b91d2018-02-08 09:47:44 +0100927/** Abort a hash operation.
928 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100929 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200930 * \p operation structure itself. Once aborted, the operation object
931 * can be reused for another operation by calling
932 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100933 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200934 * You may call this function any time after the operation object has
935 * been initialized by any of the following methods:
936 * - A call to psa_hash_setup(), whether it succeeds or not.
937 * - Initializing the \c struct to all-bits-zero.
938 * - Initializing the \c struct to logical zeros, e.g.
939 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100940 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200941 * In particular, calling psa_hash_abort() after the operation has been
942 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
943 * psa_hash_verify() is safe and has no effect.
944 *
945 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100946 *
Gilles Peskine28538492018-07-11 17:34:00 +0200947 * \retval #PSA_SUCCESS
948 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200949 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +0200950 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
951 * \retval #PSA_ERROR_HARDWARE_FAILURE
952 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100953 */
954psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100955
Gilles Peskineebb2c3e2019-01-19 12:03:41 +0100956/** Clone a hash operation.
957 *
Gilles Peskinee43aa392019-01-21 14:50:37 +0100958 * This function copies the state of an ongoing hash operation to
959 * a new operation object. In other words, this function is equivalent
960 * to calling psa_hash_setup() on \p target_operation with the same
961 * algorithm that \p source_operation was set up for, then
962 * psa_hash_update() on \p target_operation with the same input that
963 * that was passed to \p source_operation. After this function returns, the
964 * two objects are independent, i.e. subsequent calls involving one of
965 * the objects do not affect the other object.
966 *
Gilles Peskineebb2c3e2019-01-19 12:03:41 +0100967 * \param[in] source_operation The active hash operation to clone.
968 * \param[in,out] target_operation The operation object to set up.
969 * It must be initialized but not active.
970 *
971 * \retval #PSA_SUCCESS
972 * \retval #PSA_ERROR_BAD_STATE
973 * \p source_operation is not an active hash operation.
974 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinee43aa392019-01-21 14:50:37 +0100975 * \p target_operation is active.
Gilles Peskineebb2c3e2019-01-19 12:03:41 +0100976 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
977 * \retval #PSA_ERROR_HARDWARE_FAILURE
978 * \retval #PSA_ERROR_TAMPERING_DETECTED
979 */
980psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
981 psa_hash_operation_t *target_operation);
982
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100983/**@}*/
984
Gilles Peskine8c9def32018-02-08 10:02:12 +0100985/** \defgroup MAC Message authentication codes
986 * @{
987 */
988
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100989/** The type of the state data structure for multipart MAC operations.
990 *
Jaeden Amero769ce272019-01-04 11:48:03 +0000991 * Before calling any function on a MAC operation object, the application must
992 * initialize it by any of the following means:
993 * - Set the structure to all-bits-zero, for example:
994 * \code
995 * psa_mac_operation_t operation;
996 * memset(&operation, 0, sizeof(operation));
997 * \endcode
998 * - Initialize the structure to logical zero values, for example:
999 * \code
1000 * psa_mac_operation_t operation = {0};
1001 * \endcode
1002 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1003 * for example:
1004 * \code
1005 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1006 * \endcode
1007 * - Assign the result of the function psa_mac_operation_init()
1008 * to the structure, for example:
1009 * \code
1010 * psa_mac_operation_t operation;
1011 * operation = psa_mac_operation_init();
1012 * \endcode
1013 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001014 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001015 * make any assumptions about the content of this structure except
1016 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001017typedef struct psa_mac_operation_s psa_mac_operation_t;
1018
Jaeden Amero769ce272019-01-04 11:48:03 +00001019/** \def PSA_MAC_OPERATION_INIT
1020 *
1021 * This macro returns a suitable initializer for a MAC operation object of type
1022 * #psa_mac_operation_t.
1023 */
1024#ifdef __DOXYGEN_ONLY__
1025/* This is an example definition for documentation purposes.
1026 * Implementations should define a suitable value in `crypto_struct.h`.
1027 */
1028#define PSA_MAC_OPERATION_INIT {0}
1029#endif
1030
1031/** Return an initial value for a MAC operation object.
1032 */
1033static psa_mac_operation_t psa_mac_operation_init(void);
1034
Gilles Peskine89167cb2018-07-08 20:12:23 +02001035/** Start a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001036 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001037 * This function sets up the calculation of the MAC
1038 * (message authentication code) of a byte string.
1039 * To verify the MAC of a message against an
1040 * expected value, use psa_mac_verify_setup() instead.
1041 *
1042 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001043 * -# Allocate an operation object which will be passed to all the functions
1044 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001045 * -# Initialize the operation object with one of the methods described in the
1046 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001047 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001048 * The key remains associated with the operation even if the content
1049 * of the key slot changes.
1050 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1051 * of the message each time. The MAC that is calculated is the MAC
1052 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001053 * -# At the end of the message, call psa_mac_sign_finish() to finish
1054 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001055 *
1056 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001057 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001058 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001059 * After a successful call to psa_mac_sign_setup(), the application must
1060 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001061 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001062 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001063 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001064 * \param[in,out] operation The operation object to set up. It must have
1065 * been initialized as per the documentation for
1066 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001067 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001068 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1069 * such that #PSA_ALG_IS_MAC(alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001070 *
Gilles Peskine28538492018-07-11 17:34:00 +02001071 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001072 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001073 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001074 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001075 * \retval #PSA_ERROR_NOT_PERMITTED
1076 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001077 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001078 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001079 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001080 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1081 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1082 * \retval #PSA_ERROR_HARDWARE_FAILURE
1083 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001084 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001085 * The library has not been previously initialized by psa_crypto_init().
1086 * It is implementation-dependent whether a failure to initialize
1087 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001088 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001089psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001090 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001091 psa_algorithm_t alg);
1092
1093/** Start a multipart MAC verification operation.
1094 *
1095 * This function sets up the verification of the MAC
1096 * (message authentication code) of a byte string against an expected value.
1097 *
1098 * The sequence of operations to verify a MAC is as follows:
1099 * -# Allocate an operation object which will be passed to all the functions
1100 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001101 * -# Initialize the operation object with one of the methods described in the
1102 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001103 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
1104 * The key remains associated with the operation even if the content
1105 * of the key slot changes.
1106 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1107 * of the message each time. The MAC that is calculated is the MAC
1108 * of the concatenation of these messages in order.
1109 * -# At the end of the message, call psa_mac_verify_finish() to finish
1110 * calculating the actual MAC of the message and verify it against
1111 * the expected value.
1112 *
1113 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001114 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001115 *
1116 * After a successful call to psa_mac_verify_setup(), the application must
1117 * eventually terminate the operation through one of the following methods:
1118 * - A failed call to psa_mac_update().
1119 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1120 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001121 * \param[in,out] operation The operation object to set up. It must have
1122 * been initialized as per the documentation for
1123 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001124 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001125 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1126 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001127 *
Gilles Peskine28538492018-07-11 17:34:00 +02001128 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001129 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001130 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001131 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001132 * \retval #PSA_ERROR_NOT_PERMITTED
1133 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001134 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001135 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001136 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001137 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1138 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1139 * \retval #PSA_ERROR_HARDWARE_FAILURE
1140 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001141 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001142 * The library has not been previously initialized by psa_crypto_init().
1143 * It is implementation-dependent whether a failure to initialize
1144 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001145 */
1146psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001147 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001148 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001149
Gilles Peskinedcd14942018-07-12 00:30:52 +02001150/** Add a message fragment to a multipart MAC operation.
1151 *
1152 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1153 * before calling this function.
1154 *
1155 * If this function returns an error status, the operation becomes inactive.
1156 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001157 * \param[in,out] operation Active MAC operation.
1158 * \param[in] input Buffer containing the message fragment to add to
1159 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001160 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001161 *
1162 * \retval #PSA_SUCCESS
1163 * Success.
1164 * \retval #PSA_ERROR_BAD_STATE
1165 * The operation state is not valid (not started, or already completed).
1166 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1167 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1168 * \retval #PSA_ERROR_HARDWARE_FAILURE
1169 * \retval #PSA_ERROR_TAMPERING_DETECTED
1170 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001171psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1172 const uint8_t *input,
1173 size_t input_length);
1174
Gilles Peskinedcd14942018-07-12 00:30:52 +02001175/** Finish the calculation of the MAC of a message.
1176 *
1177 * The application must call psa_mac_sign_setup() before calling this function.
1178 * This function calculates the MAC of the message formed by concatenating
1179 * the inputs passed to preceding calls to psa_mac_update().
1180 *
1181 * When this function returns, the operation becomes inactive.
1182 *
1183 * \warning Applications should not call this function if they expect
1184 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1185 * Beware that comparing integrity or authenticity data such as
1186 * MAC values with a function such as \c memcmp is risky
1187 * because the time taken by the comparison may leak information
1188 * about the MAC value which could allow an attacker to guess
1189 * a valid MAC and thereby bypass security controls.
1190 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001191 * \param[in,out] operation Active MAC operation.
1192 * \param[out] mac Buffer where the MAC value is to be written.
1193 * \param mac_size Size of the \p mac buffer in bytes.
1194 * \param[out] mac_length On success, the number of bytes
1195 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001196 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001197 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001198 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001199 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001200 *
1201 * \retval #PSA_SUCCESS
1202 * Success.
1203 * \retval #PSA_ERROR_BAD_STATE
1204 * The operation state is not valid (not started, or already completed).
1205 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001206 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001207 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
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 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001213psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1214 uint8_t *mac,
1215 size_t mac_size,
1216 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001217
Gilles Peskinedcd14942018-07-12 00:30:52 +02001218/** Finish the calculation of the MAC of a message and compare it with
1219 * an expected value.
1220 *
1221 * The application must call psa_mac_verify_setup() before calling this function.
1222 * This function calculates the MAC of the message formed by concatenating
1223 * the inputs passed to preceding calls to psa_mac_update(). It then
1224 * compares the calculated MAC with the expected MAC passed as a
1225 * parameter to this function.
1226 *
1227 * When this function returns, the operation becomes inactive.
1228 *
1229 * \note Implementations shall make the best effort to ensure that the
1230 * comparison between the actual MAC and the expected MAC is performed
1231 * in constant time.
1232 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001233 * \param[in,out] operation Active MAC operation.
1234 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001235 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001236 *
1237 * \retval #PSA_SUCCESS
1238 * The expected MAC is identical to the actual MAC of the message.
1239 * \retval #PSA_ERROR_INVALID_SIGNATURE
1240 * The MAC of the message was calculated successfully, but it
1241 * differs from the expected MAC.
1242 * \retval #PSA_ERROR_BAD_STATE
1243 * The operation state is not valid (not started, or already completed).
1244 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1245 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1246 * \retval #PSA_ERROR_HARDWARE_FAILURE
1247 * \retval #PSA_ERROR_TAMPERING_DETECTED
1248 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001249psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1250 const uint8_t *mac,
1251 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001252
Gilles Peskinedcd14942018-07-12 00:30:52 +02001253/** Abort a MAC operation.
1254 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001255 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001256 * \p operation structure itself. Once aborted, the operation object
1257 * can be reused for another operation by calling
1258 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001259 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001260 * You may call this function any time after the operation object has
1261 * been initialized by any of the following methods:
1262 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1263 * it succeeds or not.
1264 * - Initializing the \c struct to all-bits-zero.
1265 * - Initializing the \c struct to logical zeros, e.g.
1266 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001267 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001268 * In particular, calling psa_mac_abort() after the operation has been
1269 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1270 * psa_mac_verify_finish() is safe and has no effect.
1271 *
1272 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001273 *
1274 * \retval #PSA_SUCCESS
1275 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001276 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001277 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1278 * \retval #PSA_ERROR_HARDWARE_FAILURE
1279 * \retval #PSA_ERROR_TAMPERING_DETECTED
1280 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001281psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1282
1283/**@}*/
1284
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001285/** \defgroup cipher Symmetric ciphers
1286 * @{
1287 */
1288
1289/** The type of the state data structure for multipart cipher operations.
1290 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001291 * Before calling any function on a cipher operation object, the application
1292 * must initialize it by any of the following means:
1293 * - Set the structure to all-bits-zero, for example:
1294 * \code
1295 * psa_cipher_operation_t operation;
1296 * memset(&operation, 0, sizeof(operation));
1297 * \endcode
1298 * - Initialize the structure to logical zero values, for example:
1299 * \code
1300 * psa_cipher_operation_t operation = {0};
1301 * \endcode
1302 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1303 * for example:
1304 * \code
1305 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1306 * \endcode
1307 * - Assign the result of the function psa_cipher_operation_init()
1308 * to the structure, for example:
1309 * \code
1310 * psa_cipher_operation_t operation;
1311 * operation = psa_cipher_operation_init();
1312 * \endcode
1313 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001314 * This is an implementation-defined \c struct. Applications should not
1315 * make any assumptions about the content of this structure except
1316 * as directed by the documentation of a specific implementation. */
1317typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1318
Jaeden Amero5bae2272019-01-04 11:48:27 +00001319/** \def PSA_CIPHER_OPERATION_INIT
1320 *
1321 * This macro returns a suitable initializer for a cipher operation object of
1322 * type #psa_cipher_operation_t.
1323 */
1324#ifdef __DOXYGEN_ONLY__
1325/* This is an example definition for documentation purposes.
1326 * Implementations should define a suitable value in `crypto_struct.h`.
1327 */
1328#define PSA_CIPHER_OPERATION_INIT {0}
1329#endif
1330
1331/** Return an initial value for a cipher operation object.
1332 */
1333static psa_cipher_operation_t psa_cipher_operation_init(void);
1334
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001335/** Set the key for a multipart symmetric encryption operation.
1336 *
1337 * The sequence of operations to encrypt a message with a symmetric cipher
1338 * is as follows:
1339 * -# Allocate an operation object which will be passed to all the functions
1340 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001341 * -# Initialize the operation object with one of the methods described in the
1342 * documentation for #psa_cipher_operation_t, e.g.
1343 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001344 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001345 * The key remains associated with the operation even if the content
1346 * of the key slot changes.
itayzafrired7382f2018-08-02 14:19:33 +03001347 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001348 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001349 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001350 * requires a specific IV value.
1351 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1352 * of the message each time.
1353 * -# Call psa_cipher_finish().
1354 *
1355 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001356 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001357 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001358 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001359 * eventually terminate the operation. The following events terminate an
1360 * operation:
itayzafrired7382f2018-08-02 14:19:33 +03001361 * - A failed call to psa_cipher_generate_iv(), psa_cipher_set_iv()
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001362 * or psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001363 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001364 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001365 * \param[in,out] operation The operation object to set up. It must have
1366 * been initialized as per the documentation for
1367 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001368 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001369 * \param alg The cipher algorithm to compute
1370 * (\c PSA_ALG_XXX value such that
1371 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001372 *
Gilles Peskine28538492018-07-11 17:34:00 +02001373 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001374 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001375 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001376 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001377 * \retval #PSA_ERROR_NOT_PERMITTED
1378 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001379 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001380 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001381 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001382 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1383 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1384 * \retval #PSA_ERROR_HARDWARE_FAILURE
1385 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001386 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001387 * The library has not been previously initialized by psa_crypto_init().
1388 * It is implementation-dependent whether a failure to initialize
1389 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001390 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001391psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001392 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001393 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001394
1395/** Set the key for a multipart symmetric decryption operation.
1396 *
1397 * The sequence of operations to decrypt a message with a symmetric cipher
1398 * is as follows:
1399 * -# Allocate an operation object which will be passed to all the functions
1400 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001401 * -# Initialize the operation object with one of the methods described in the
1402 * documentation for #psa_cipher_operation_t, e.g.
1403 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001404 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001405 * The key remains associated with the operation even if the content
1406 * of the key slot changes.
1407 * -# Call psa_cipher_update() with the IV (initialization vector) for the
1408 * decryption. If the IV is prepended to the ciphertext, you can call
1409 * psa_cipher_update() on a buffer containing the IV followed by the
1410 * beginning of the message.
1411 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1412 * of the message each time.
1413 * -# Call psa_cipher_finish().
1414 *
1415 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001416 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001417 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001418 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001419 * eventually terminate the operation. The following events terminate an
1420 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001421 * - A failed call to psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001422 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001423 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001424 * \param[in,out] operation The operation object to set up. It must have
1425 * been initialized as per the documentation for
1426 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001427 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001428 * \param alg The cipher algorithm to compute
1429 * (\c PSA_ALG_XXX value such that
1430 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001431 *
Gilles Peskine28538492018-07-11 17:34:00 +02001432 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001433 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001434 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001435 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001436 * \retval #PSA_ERROR_NOT_PERMITTED
1437 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001438 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001439 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001440 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001441 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1442 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1443 * \retval #PSA_ERROR_HARDWARE_FAILURE
1444 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001445 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001446 * The library has not been previously initialized by psa_crypto_init().
1447 * It is implementation-dependent whether a failure to initialize
1448 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001449 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001450psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001451 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001452 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001453
Gilles Peskinedcd14942018-07-12 00:30:52 +02001454/** Generate an IV for a symmetric encryption operation.
1455 *
1456 * This function generates a random IV (initialization vector), nonce
1457 * or initial counter value for the encryption operation as appropriate
1458 * for the chosen algorithm, key type and key size.
1459 *
1460 * The application must call psa_cipher_encrypt_setup() before
1461 * calling this function.
1462 *
1463 * If this function returns an error status, the operation becomes inactive.
1464 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001465 * \param[in,out] operation Active cipher operation.
1466 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001467 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001468 * \param[out] iv_length On success, the number of bytes of the
1469 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001470 *
1471 * \retval #PSA_SUCCESS
1472 * Success.
1473 * \retval #PSA_ERROR_BAD_STATE
1474 * The operation state is not valid (not started, or IV already set).
1475 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001476 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001477 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1478 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1479 * \retval #PSA_ERROR_HARDWARE_FAILURE
1480 * \retval #PSA_ERROR_TAMPERING_DETECTED
1481 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001482psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1483 unsigned char *iv,
1484 size_t iv_size,
1485 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001486
Gilles Peskinedcd14942018-07-12 00:30:52 +02001487/** Set the IV for a symmetric encryption or decryption operation.
1488 *
1489 * This function sets the random IV (initialization vector), nonce
1490 * or initial counter value for the encryption or decryption operation.
1491 *
1492 * The application must call psa_cipher_encrypt_setup() before
1493 * calling this function.
1494 *
1495 * If this function returns an error status, the operation becomes inactive.
1496 *
1497 * \note When encrypting, applications should use psa_cipher_generate_iv()
1498 * instead of this function, unless implementing a protocol that requires
1499 * a non-random IV.
1500 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001501 * \param[in,out] operation Active cipher operation.
1502 * \param[in] iv Buffer containing the IV to use.
1503 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001504 *
1505 * \retval #PSA_SUCCESS
1506 * Success.
1507 * \retval #PSA_ERROR_BAD_STATE
1508 * The operation state is not valid (not started, or IV already set).
1509 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001510 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001511 * or the chosen algorithm does not use an IV.
1512 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1513 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1514 * \retval #PSA_ERROR_HARDWARE_FAILURE
1515 * \retval #PSA_ERROR_TAMPERING_DETECTED
1516 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001517psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1518 const unsigned char *iv,
1519 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001520
Gilles Peskinedcd14942018-07-12 00:30:52 +02001521/** Encrypt or decrypt a message fragment in an active cipher operation.
1522 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001523 * Before calling this function, you must:
1524 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1525 * The choice of setup function determines whether this function
1526 * encrypts or decrypts its input.
1527 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1528 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001529 *
1530 * If this function returns an error status, the operation becomes inactive.
1531 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001532 * \param[in,out] operation Active cipher operation.
1533 * \param[in] input Buffer containing the message fragment to
1534 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001535 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001536 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001537 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001538 * \param[out] output_length On success, the number of bytes
1539 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001540 *
1541 * \retval #PSA_SUCCESS
1542 * Success.
1543 * \retval #PSA_ERROR_BAD_STATE
1544 * The operation state is not valid (not started, IV required but
1545 * not set, or already completed).
1546 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1547 * The size of the \p output buffer is too small.
1548 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1549 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1550 * \retval #PSA_ERROR_HARDWARE_FAILURE
1551 * \retval #PSA_ERROR_TAMPERING_DETECTED
1552 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001553psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1554 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001555 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001556 unsigned char *output,
1557 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001558 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001559
Gilles Peskinedcd14942018-07-12 00:30:52 +02001560/** Finish encrypting or decrypting a message in a cipher operation.
1561 *
1562 * The application must call psa_cipher_encrypt_setup() or
1563 * psa_cipher_decrypt_setup() before calling this function. The choice
1564 * of setup function determines whether this function encrypts or
1565 * decrypts its input.
1566 *
1567 * This function finishes the encryption or decryption of the message
1568 * formed by concatenating the inputs passed to preceding calls to
1569 * psa_cipher_update().
1570 *
1571 * When this function returns, the operation becomes inactive.
1572 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001573 * \param[in,out] operation Active cipher operation.
1574 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001575 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001576 * \param[out] output_length On success, the number of bytes
1577 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001578 *
1579 * \retval #PSA_SUCCESS
1580 * Success.
1581 * \retval #PSA_ERROR_BAD_STATE
1582 * The operation state is not valid (not started, IV required but
1583 * not set, or already completed).
1584 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1585 * The size of the \p output buffer is too small.
1586 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1587 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1588 * \retval #PSA_ERROR_HARDWARE_FAILURE
1589 * \retval #PSA_ERROR_TAMPERING_DETECTED
1590 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001591psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001592 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001593 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001594 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001595
Gilles Peskinedcd14942018-07-12 00:30:52 +02001596/** Abort a cipher operation.
1597 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001598 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001599 * \p operation structure itself. Once aborted, the operation object
1600 * can be reused for another operation by calling
1601 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001602 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001603 * You may call this function any time after the operation object has
1604 * been initialized by any of the following methods:
1605 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1606 * whether it succeeds or not.
1607 * - Initializing the \c struct to all-bits-zero.
1608 * - Initializing the \c struct to logical zeros, e.g.
1609 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001610 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001611 * In particular, calling psa_cipher_abort() after the operation has been
1612 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1613 * is safe and has no effect.
1614 *
1615 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001616 *
1617 * \retval #PSA_SUCCESS
1618 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001619 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001620 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1621 * \retval #PSA_ERROR_HARDWARE_FAILURE
1622 * \retval #PSA_ERROR_TAMPERING_DETECTED
1623 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001624psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1625
1626/**@}*/
1627
Gilles Peskine3b555712018-03-03 21:27:57 +01001628/** \defgroup aead Authenticated encryption with associated data (AEAD)
1629 * @{
1630 */
1631
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001632/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001633 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001634 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001635 * \param alg The AEAD algorithm to compute
1636 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001637 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001638 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001639 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001640 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001641 * but not encrypted.
1642 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001643 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001644 * encrypted.
1645 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001646 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001647 * encrypted data. The additional data is not
1648 * part of this output. For algorithms where the
1649 * encrypted data and the authentication tag
1650 * are defined as separate outputs, the
1651 * authentication tag is appended to the
1652 * encrypted data.
1653 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1654 * This must be at least
1655 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1656 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001657 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001658 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001659 *
Gilles Peskine28538492018-07-11 17:34:00 +02001660 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001661 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001662 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001663 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001664 * \retval #PSA_ERROR_NOT_PERMITTED
1665 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001666 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001667 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001668 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001669 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1670 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1671 * \retval #PSA_ERROR_HARDWARE_FAILURE
1672 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001673 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001674 * The library has not been previously initialized by psa_crypto_init().
1675 * It is implementation-dependent whether a failure to initialize
1676 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001677 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001678psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001679 psa_algorithm_t alg,
1680 const uint8_t *nonce,
1681 size_t nonce_length,
1682 const uint8_t *additional_data,
1683 size_t additional_data_length,
1684 const uint8_t *plaintext,
1685 size_t plaintext_length,
1686 uint8_t *ciphertext,
1687 size_t ciphertext_size,
1688 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001689
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001690/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001691 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001692 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001693 * \param alg The AEAD algorithm to compute
1694 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001695 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001696 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001697 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001698 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001699 * but not encrypted.
1700 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001701 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001702 * encrypted. For algorithms where the
1703 * encrypted data and the authentication tag
1704 * are defined as separate inputs, the buffer
1705 * must contain the encrypted data followed
1706 * by the authentication tag.
1707 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001708 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001709 * \param plaintext_size Size of the \p plaintext buffer in bytes.
1710 * This must be at least
1711 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
1712 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001713 * \param[out] plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03001714 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001715 *
Gilles Peskine28538492018-07-11 17:34:00 +02001716 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001717 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001718 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02001719 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine28538492018-07-11 17:34:00 +02001720 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001721 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02001722 * \retval #PSA_ERROR_NOT_PERMITTED
1723 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001724 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001725 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001726 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001727 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1728 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1729 * \retval #PSA_ERROR_HARDWARE_FAILURE
1730 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001731 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001732 * The library has not been previously initialized by psa_crypto_init().
1733 * It is implementation-dependent whether a failure to initialize
1734 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001735 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001736psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001737 psa_algorithm_t alg,
1738 const uint8_t *nonce,
1739 size_t nonce_length,
1740 const uint8_t *additional_data,
1741 size_t additional_data_length,
1742 const uint8_t *ciphertext,
1743 size_t ciphertext_length,
1744 uint8_t *plaintext,
1745 size_t plaintext_size,
1746 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001747
1748/**@}*/
1749
Gilles Peskine20035e32018-02-03 22:44:14 +01001750/** \defgroup asymmetric Asymmetric cryptography
1751 * @{
1752 */
1753
1754/**
1755 * \brief Sign a hash or short message with a private key.
1756 *
Gilles Peskine08bac712018-06-26 16:14:46 +02001757 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001758 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02001759 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
1760 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
1761 * to determine the hash algorithm to use.
1762 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001763 * \param handle Handle to the key to use for the operation.
1764 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001765 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001766 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001767 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001768 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001769 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001770 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001771 * \param[out] signature_length On success, the number of bytes
1772 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001773 *
Gilles Peskine28538492018-07-11 17:34:00 +02001774 * \retval #PSA_SUCCESS
1775 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001776 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01001777 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001778 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001779 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001780 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001781 * \retval #PSA_ERROR_NOT_SUPPORTED
1782 * \retval #PSA_ERROR_INVALID_ARGUMENT
1783 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1784 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1785 * \retval #PSA_ERROR_HARDWARE_FAILURE
1786 * \retval #PSA_ERROR_TAMPERING_DETECTED
1787 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03001788 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001789 * The library has not been previously initialized by psa_crypto_init().
1790 * It is implementation-dependent whether a failure to initialize
1791 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01001792 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001793psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01001794 psa_algorithm_t alg,
1795 const uint8_t *hash,
1796 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01001797 uint8_t *signature,
1798 size_t signature_size,
1799 size_t *signature_length);
1800
1801/**
1802 * \brief Verify the signature a hash or short message using a public key.
1803 *
Gilles Peskine08bac712018-06-26 16:14:46 +02001804 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001805 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02001806 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
1807 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
1808 * to determine the hash algorithm to use.
1809 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001810 * \param handle Handle to the key to use for the operation.
1811 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001812 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001813 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001814 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02001815 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001816 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001817 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001818 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001819 *
Gilles Peskine28538492018-07-11 17:34:00 +02001820 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001821 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02001822 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001823 * The calculation was perfomed successfully, but the passed
1824 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02001825 * \retval #PSA_ERROR_NOT_SUPPORTED
1826 * \retval #PSA_ERROR_INVALID_ARGUMENT
1827 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1828 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1829 * \retval #PSA_ERROR_HARDWARE_FAILURE
1830 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001831 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001832 * The library has not been previously initialized by psa_crypto_init().
1833 * It is implementation-dependent whether a failure to initialize
1834 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01001835 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001836psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01001837 psa_algorithm_t alg,
1838 const uint8_t *hash,
1839 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02001840 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02001841 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01001842
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001843/**
1844 * \brief Encrypt a short message with a public key.
1845 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001846 * \param handle Handle to the key to use for the operation.
1847 * It must be a public key or an asymmetric
1848 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001849 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001850 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001851 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001852 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001853 * \param[in] salt A salt or label, if supported by the
1854 * encryption algorithm.
1855 * If the algorithm does not support a
1856 * salt, pass \c NULL.
1857 * If the algorithm supports an optional
1858 * salt and you do not want to pass a salt,
1859 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001860 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001861 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1862 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001863 * \param salt_length Size of the \p salt buffer in bytes.
1864 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001865 * \param[out] output Buffer where the encrypted message is to
1866 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001867 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001868 * \param[out] output_length On success, the number of bytes
1869 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001870 *
Gilles Peskine28538492018-07-11 17:34:00 +02001871 * \retval #PSA_SUCCESS
1872 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001873 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001874 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001875 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001876 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001877 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001878 * \retval #PSA_ERROR_NOT_SUPPORTED
1879 * \retval #PSA_ERROR_INVALID_ARGUMENT
1880 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1881 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1882 * \retval #PSA_ERROR_HARDWARE_FAILURE
1883 * \retval #PSA_ERROR_TAMPERING_DETECTED
1884 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03001885 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001886 * The library has not been previously initialized by psa_crypto_init().
1887 * It is implementation-dependent whether a failure to initialize
1888 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001889 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001890psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001891 psa_algorithm_t alg,
1892 const uint8_t *input,
1893 size_t input_length,
1894 const uint8_t *salt,
1895 size_t salt_length,
1896 uint8_t *output,
1897 size_t output_size,
1898 size_t *output_length);
1899
1900/**
1901 * \brief Decrypt a short message with a private key.
1902 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001903 * \param handle Handle to the key to use for the operation.
1904 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001905 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001906 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001907 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001908 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001909 * \param[in] salt A salt or label, if supported by the
1910 * encryption algorithm.
1911 * If the algorithm does not support a
1912 * salt, pass \c NULL.
1913 * If the algorithm supports an optional
1914 * salt and you do not want to pass a salt,
1915 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001916 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001917 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1918 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001919 * \param salt_length Size of the \p salt buffer in bytes.
1920 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001921 * \param[out] output Buffer where the decrypted message is to
1922 * be written.
1923 * \param output_size Size of the \c output buffer in bytes.
1924 * \param[out] output_length On success, the number of bytes
1925 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001926 *
Gilles Peskine28538492018-07-11 17:34:00 +02001927 * \retval #PSA_SUCCESS
1928 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001929 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001930 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001931 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001932 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001933 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001934 * \retval #PSA_ERROR_NOT_SUPPORTED
1935 * \retval #PSA_ERROR_INVALID_ARGUMENT
1936 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1937 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1938 * \retval #PSA_ERROR_HARDWARE_FAILURE
1939 * \retval #PSA_ERROR_TAMPERING_DETECTED
1940 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
1941 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03001942 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001943 * The library has not been previously initialized by psa_crypto_init().
1944 * It is implementation-dependent whether a failure to initialize
1945 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001946 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001947psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001948 psa_algorithm_t alg,
1949 const uint8_t *input,
1950 size_t input_length,
1951 const uint8_t *salt,
1952 size_t salt_length,
1953 uint8_t *output,
1954 size_t output_size,
1955 size_t *output_length);
1956
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001957/**@}*/
1958
Gilles Peskineedd76872018-07-20 17:42:05 +02001959/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02001960 * @{
1961 */
1962
1963/** The type of the state data structure for generators.
1964 *
1965 * Before calling any function on a generator, the application must
1966 * initialize it by any of the following means:
1967 * - Set the structure to all-bits-zero, for example:
1968 * \code
1969 * psa_crypto_generator_t generator;
1970 * memset(&generator, 0, sizeof(generator));
1971 * \endcode
1972 * - Initialize the structure to logical zero values, for example:
1973 * \code
1974 * psa_crypto_generator_t generator = {0};
1975 * \endcode
1976 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
1977 * for example:
1978 * \code
1979 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1980 * \endcode
1981 * - Assign the result of the function psa_crypto_generator_init()
1982 * to the structure, for example:
1983 * \code
1984 * psa_crypto_generator_t generator;
1985 * generator = psa_crypto_generator_init();
1986 * \endcode
1987 *
1988 * This is an implementation-defined \c struct. Applications should not
1989 * make any assumptions about the content of this structure except
1990 * as directed by the documentation of a specific implementation.
1991 */
1992typedef struct psa_crypto_generator_s psa_crypto_generator_t;
1993
1994/** \def PSA_CRYPTO_GENERATOR_INIT
1995 *
1996 * This macro returns a suitable initializer for a generator object
1997 * of type #psa_crypto_generator_t.
1998 */
1999#ifdef __DOXYGEN_ONLY__
2000/* This is an example definition for documentation purposes.
2001 * Implementations should define a suitable value in `crypto_struct.h`.
2002 */
2003#define PSA_CRYPTO_GENERATOR_INIT {0}
2004#endif
2005
2006/** Return an initial value for a generator object.
2007 */
2008static psa_crypto_generator_t psa_crypto_generator_init(void);
2009
2010/** Retrieve the current capacity of a generator.
2011 *
2012 * The capacity of a generator is the maximum number of bytes that it can
2013 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
2014 *
2015 * \param[in] generator The generator to query.
2016 * \param[out] capacity On success, the capacity of the generator.
2017 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002018 * \retval #PSA_SUCCESS
2019 * \retval #PSA_ERROR_BAD_STATE
2020 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002021 */
2022psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
2023 size_t *capacity);
2024
2025/** Read some data from a generator.
2026 *
2027 * This function reads and returns a sequence of bytes from a generator.
2028 * The data that is read is discarded from the generator. The generator's
2029 * capacity is decreased by the number of bytes read.
2030 *
2031 * \param[in,out] generator The generator object to read from.
2032 * \param[out] output Buffer where the generator output will be
2033 * written.
2034 * \param output_length Number of bytes to output.
2035 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002036 * \retval #PSA_SUCCESS
David Saadab4ecc272019-02-14 13:48:10 +02002037 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskineeab56e42018-07-12 17:12:33 +02002038 * There were fewer than \p output_length bytes
2039 * in the generator. Note that in this case, no
2040 * output is written to the output buffer.
2041 * The generator's capacity is set to 0, thus
2042 * subsequent calls to this function will not
2043 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002044 * \retval #PSA_ERROR_BAD_STATE
2045 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2046 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2047 * \retval #PSA_ERROR_HARDWARE_FAILURE
2048 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002049 */
2050psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
2051 uint8_t *output,
2052 size_t output_length);
2053
2054/** Create a symmetric key from data read from a generator.
2055 *
2056 * This function reads a sequence of bytes from a generator and imports
2057 * these bytes as a key.
2058 * The data that is read is discarded from the generator. The generator's
2059 * capacity is decreased by the number of bytes read.
2060 *
2061 * This function is equivalent to calling #psa_generator_read and
2062 * passing the resulting output to #psa_import_key, but
2063 * if the implementation provides an isolation boundary then
2064 * the key material is not exposed outside the isolation boundary.
2065 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002066 * \param handle Handle to the slot where the key will be stored.
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002067 * It must have been obtained by calling
2068 * psa_allocate_key() or psa_create_key() and must
2069 * not contain key material yet.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002070 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2071 * This must be a symmetric key type.
2072 * \param bits Key size in bits.
2073 * \param[in,out] generator The generator object to read from.
2074 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002075 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02002076 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01002077 * If the key is persistent, the key material and the key's metadata
2078 * have been saved to persistent storage.
David Saadab4ecc272019-02-14 13:48:10 +02002079 * \retval #PSA_ERROR_INSUFFICIENT_DATA
Gilles Peskineeab56e42018-07-12 17:12:33 +02002080 * There were fewer than \p output_length bytes
2081 * in the generator. Note that in this case, no
2082 * output is written to the output buffer.
2083 * The generator's capacity is set to 0, thus
2084 * subsequent calls to this function will not
2085 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002086 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002087 * The key type or key size is not supported, either by the
2088 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002089 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineae32aac2018-11-30 14:39:32 +01002090 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002091 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskineeab56e42018-07-12 17:12:33 +02002092 * There is already a key in the specified slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002093 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2094 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
2095 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2096 * \retval #PSA_ERROR_HARDWARE_FAILURE
2097 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002098 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002099 * The library has not been previously initialized by psa_crypto_init().
2100 * It is implementation-dependent whether a failure to initialize
2101 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002102 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002103psa_status_t psa_generator_import_key(psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002104 psa_key_type_t type,
2105 size_t bits,
2106 psa_crypto_generator_t *generator);
2107
2108/** Abort a generator.
2109 *
2110 * Once a generator has been aborted, its capacity is zero.
2111 * Aborting a generator frees all associated resources except for the
2112 * \c generator structure itself.
2113 *
2114 * This function may be called at any time as long as the generator
2115 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
2116 * psa_crypto_generator_init() or a zero value. In particular, it is valid
2117 * to call psa_generator_abort() twice, or to call psa_generator_abort()
2118 * on a generator that has not been set up.
2119 *
2120 * Once aborted, the generator object may be called.
2121 *
2122 * \param[in,out] generator The generator to abort.
2123 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002124 * \retval #PSA_SUCCESS
2125 * \retval #PSA_ERROR_BAD_STATE
2126 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2127 * \retval #PSA_ERROR_HARDWARE_FAILURE
2128 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002129 */
2130psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
2131
Gilles Peskine8feb3a82018-09-18 12:06:11 +02002132/** Use the maximum possible capacity for a generator.
2133 *
2134 * Use this value as the capacity argument when setting up a generator
2135 * to indicate that the generator should have the maximum possible capacity.
2136 * The value of the maximum possible capacity depends on the generator
2137 * algorithm.
2138 */
2139#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
2140
Gilles Peskineeab56e42018-07-12 17:12:33 +02002141/**@}*/
2142
Gilles Peskineea0fb492018-07-12 17:17:20 +02002143/** \defgroup derivation Key derivation
2144 * @{
2145 */
2146
2147/** Set up a key derivation operation.
2148 *
2149 * A key derivation algorithm takes three inputs: a secret input \p key and
2150 * two non-secret inputs \p label and p salt.
2151 * The result of this function is a byte generator which can
2152 * be used to produce keys and other cryptographic material.
2153 *
2154 * The role of \p label and \p salt is as follows:
Gilles Peskinebef7f142018-07-12 17:22:21 +02002155 * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step
2156 * and \p label is the info string used in the "expand" step.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002157 *
Jaeden Amero9e919c62019-01-07 15:41:50 +00002158 * \param[in,out] generator The generator object to set up. It must have
2159 * been initialized as per the documentation for
2160 * #psa_crypto_generator_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002161 * \param handle Handle to the secret key.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002162 * \param alg The key derivation algorithm to compute
2163 * (\c PSA_ALG_XXX value such that
2164 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2165 * \param[in] salt Salt to use.
2166 * \param salt_length Size of the \p salt buffer in bytes.
2167 * \param[in] label Label to use.
2168 * \param label_length Size of the \p label buffer in bytes.
2169 * \param capacity The maximum number of bytes that the
2170 * generator will be able to provide.
2171 *
2172 * \retval #PSA_SUCCESS
2173 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002174 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002175 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskineea0fb492018-07-12 17:17:20 +02002176 * \retval #PSA_ERROR_NOT_PERMITTED
2177 * \retval #PSA_ERROR_INVALID_ARGUMENT
2178 * \c key is not compatible with \c alg,
2179 * or \p capacity is too large for the specified algorithm and key.
2180 * \retval #PSA_ERROR_NOT_SUPPORTED
2181 * \c alg is not supported or is not a key derivation algorithm.
2182 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2183 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2184 * \retval #PSA_ERROR_HARDWARE_FAILURE
2185 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002186 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002187 * The library has not been previously initialized by psa_crypto_init().
2188 * It is implementation-dependent whether a failure to initialize
2189 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002190 */
2191psa_status_t psa_key_derivation(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002192 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02002193 psa_algorithm_t alg,
2194 const uint8_t *salt,
2195 size_t salt_length,
2196 const uint8_t *label,
2197 size_t label_length,
2198 size_t capacity);
2199
Gilles Peskine01d718c2018-09-18 12:01:02 +02002200/** Set up a key agreement operation.
2201 *
2202 * A key agreement algorithm takes two inputs: a private key \p private_key
2203 * a public key \p peer_key.
2204 * The result of this function is a byte generator which can
2205 * be used to produce keys and other cryptographic material.
2206 *
Gilles Peskine211a4362018-10-25 22:22:31 +02002207 * The resulting generator always has the maximum capacity permitted by
2208 * the algorithm.
2209 *
Jaeden Amero21fec0c2019-01-14 16:56:20 +00002210 * \param[in,out] generator The generator object to set up. It must have been
2211 * initialized as per the documentation for
2212 * #psa_crypto_generator_t and not yet in use.
2213 * \param private_key Handle to the private key to use.
2214 * \param[in] peer_key Public key of the peer. The peer key must be in the
2215 * same format that psa_import_key() accepts for the
2216 * public key type corresponding to the type of
2217 * \p private_key. That is, this function performs the
2218 * equivalent of
2219 * `psa_import_key(internal_public_key_handle,
2220 * PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(private_key_type),
2221 * peer_key, peer_key_length)` where
2222 * `private_key_type` is the type of \p private_key.
2223 * For example, for EC keys, this means that \p
2224 * peer_key is interpreted as a point on the curve
2225 * that the private key is associated with. The
2226 * standard formats for public keys are documented in
2227 * the documentation of psa_export_public_key().
2228 * \param peer_key_length Size of \p peer_key in bytes.
2229 * \param alg The key agreement algorithm to compute
2230 * (\c PSA_ALG_XXX value such that
2231 * #PSA_ALG_IS_KEY_AGREEMENT(\p alg) is true).
Gilles Peskine01d718c2018-09-18 12:01:02 +02002232 *
2233 * \retval #PSA_SUCCESS
2234 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002235 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002236 * \retval #PSA_ERROR_DOES_NOT_EXIST
Gilles Peskine01d718c2018-09-18 12:01:02 +02002237 * \retval #PSA_ERROR_NOT_PERMITTED
2238 * \retval #PSA_ERROR_INVALID_ARGUMENT
2239 * \c private_key is not compatible with \c alg,
2240 * or \p peer_key is not valid for \c alg or not compatible with
2241 * \c private_key.
2242 * \retval #PSA_ERROR_NOT_SUPPORTED
2243 * \c alg is not supported or is not a key derivation algorithm.
2244 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2245 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2246 * \retval #PSA_ERROR_HARDWARE_FAILURE
2247 * \retval #PSA_ERROR_TAMPERING_DETECTED
2248 */
2249psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002250 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02002251 const uint8_t *peer_key,
2252 size_t peer_key_length,
2253 psa_algorithm_t alg);
2254
Gilles Peskineea0fb492018-07-12 17:17:20 +02002255/**@}*/
2256
Gilles Peskineedd76872018-07-20 17:42:05 +02002257/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002258 * @{
2259 */
2260
2261/**
2262 * \brief Generate random bytes.
2263 *
2264 * \warning This function **can** fail! Callers MUST check the return status
2265 * and MUST NOT use the content of the output buffer if the return
2266 * status is not #PSA_SUCCESS.
2267 *
2268 * \note To generate a key, use psa_generate_key() instead.
2269 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002270 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002271 * \param output_size Number of bytes to generate and output.
2272 *
Gilles Peskine28538492018-07-11 17:34:00 +02002273 * \retval #PSA_SUCCESS
2274 * \retval #PSA_ERROR_NOT_SUPPORTED
2275 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2276 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2277 * \retval #PSA_ERROR_HARDWARE_FAILURE
2278 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03002279 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002280 * The library has not been previously initialized by psa_crypto_init().
2281 * It is implementation-dependent whether a failure to initialize
2282 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002283 */
2284psa_status_t psa_generate_random(uint8_t *output,
2285 size_t output_size);
2286
Gilles Peskine4c317f42018-07-12 01:24:09 +02002287/** Extra parameters for RSA key generation.
2288 *
Gilles Peskinebe42f312018-07-13 14:38:15 +02002289 * You may pass a pointer to a structure of this type as the \c extra
Gilles Peskine4c317f42018-07-12 01:24:09 +02002290 * parameter to psa_generate_key().
2291 */
2292typedef struct {
Gilles Peskineedd76872018-07-20 17:42:05 +02002293 uint32_t e; /**< Public exponent value. Default: 65537. */
Gilles Peskine4c317f42018-07-12 01:24:09 +02002294} psa_generate_key_extra_rsa;
2295
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002296/**
2297 * \brief Generate a key or key pair.
2298 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002299 * \param handle Handle to the slot where the key will be stored.
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002300 * It must have been obtained by calling
2301 * psa_allocate_key() or psa_create_key() and must
2302 * not contain key material yet.
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002303 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2304 * \param bits Key size in bits.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002305 * \param[in] extra Extra parameters for key generation. The
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002306 * interpretation of this parameter depends on
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002307 * \p type. All types support \c NULL to use
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002308 * default parameters. Implementation that support
2309 * the generation of vendor-specific key types
2310 * that allow extra parameters shall document
2311 * the format of these extra parameters and
2312 * the default values. For standard parameters,
2313 * the meaning of \p extra is as follows:
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002314 * - For a symmetric key type (a type such
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002315 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
2316 * false), \p extra must be \c NULL.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002317 * - For an elliptic curve key type (a type
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002318 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
2319 * false), \p extra must be \c NULL.
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002320 * - For an RSA key (\p type is
2321 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
2322 * optional #psa_generate_key_extra_rsa structure
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002323 * specifying the public exponent. The
2324 * default public exponent used when \p extra
2325 * is \c NULL is 65537.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002326 * \param extra_size Size of the buffer that \p extra
2327 * points to, in bytes. Note that if \p extra is
2328 * \c NULL then \p extra_size must be zero.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002329 *
Gilles Peskine28538492018-07-11 17:34:00 +02002330 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01002331 * Success.
2332 * If the key is persistent, the key material and the key's metadata
2333 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002334 * \retval #PSA_ERROR_INVALID_HANDLE
David Saadab4ecc272019-02-14 13:48:10 +02002335 * \retval #PSA_ERROR_ALREADY_EXISTS
Gilles Peskineae32aac2018-11-30 14:39:32 +01002336 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +02002337 * \retval #PSA_ERROR_NOT_SUPPORTED
2338 * \retval #PSA_ERROR_INVALID_ARGUMENT
2339 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2340 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2341 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2342 * \retval #PSA_ERROR_HARDWARE_FAILURE
2343 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002344 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002345 * The library has not been previously initialized by psa_crypto_init().
2346 * It is implementation-dependent whether a failure to initialize
2347 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002348 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002349psa_status_t psa_generate_key(psa_key_handle_t handle,
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002350 psa_key_type_t type,
2351 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02002352 const void *extra,
2353 size_t extra_size);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002354
2355/**@}*/
2356
Gilles Peskinee59236f2018-01-27 23:32:46 +01002357#ifdef __cplusplus
2358}
2359#endif
2360
Gilles Peskine0cad07c2018-06-27 19:49:02 +02002361/* The file "crypto_sizes.h" contains definitions for size calculation
2362 * macros whose definitions are implementation-specific. */
2363#include "crypto_sizes.h"
2364
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002365/* The file "crypto_struct.h" contains definitions for
2366 * implementation-specific structs that are declared above. */
2367#include "crypto_struct.h"
2368
2369/* The file "crypto_extra.h" contains vendor-specific definitions. This
2370 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01002371#include "crypto_extra.h"
2372
2373#endif /* PSA_CRYPTO_H */