blob: 7f9daa90457437fd0c5f2356a7de0949d54a08a6 [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
96/** \defgroup key_management Key management
97 * @{
98 */
99
Gilles Peskineae32aac2018-11-30 14:39:32 +0100100/** \brief Retrieve the lifetime of an open key.
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100101 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100102 * \param handle Handle to query.
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100103 * \param[out] lifetime On success, the lifetime value.
104 *
105 * \retval #PSA_SUCCESS
106 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100107 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100108 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
109 * \retval #PSA_ERROR_HARDWARE_FAILURE
110 * \retval #PSA_ERROR_TAMPERING_DETECTED
111 * \retval #PSA_ERROR_BAD_STATE
112 * The library has not been previously initialized by psa_crypto_init().
113 * It is implementation-dependent whether a failure to initialize
114 * results in this error code.
115 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100116psa_status_t psa_get_key_lifetime(psa_key_handle_t handle,
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100117 psa_key_lifetime_t *lifetime);
118
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100119
Gilles Peskinef535eb22018-11-30 14:08:36 +0100120/** Allocate a key slot for a transient key, i.e. a key which is only stored
121 * in volatile memory.
122 *
123 * The allocated key slot and its handle remain valid until the
124 * application calls psa_close_key() or psa_destroy_key() or until the
125 * application terminates.
126 *
127 * This function takes a key type and maximum size as arguments so that
128 * the implementation can reserve a corresponding amount of memory.
129 * Implementations are not required to enforce this limit: if the application
130 * later tries to create a larger key or a key of a different type, it
131 * is implementation-defined whether this may succeed.
132 *
133 * \param type The type of key that the slot will contain.
134 * \param max_bits The maximum key size that the slot will contain.
135 * \param[out] handle On success, a handle to a volatile key slot.
136 *
137 * \retval #PSA_SUCCESS
138 * Success. The application can now use the value of `*handle`
139 * to access the newly allocated key slot.
140 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
141 * There was not enough memory, or the maximum number of key slots
142 * has been reached.
143 * \retval #PSA_ERROR_INVALID_ARGUMENT
144 * This implementation does not support this key type.
145 */
146
147psa_status_t psa_allocate_key(psa_key_type_t type,
148 size_t max_bits,
149 psa_key_handle_t *handle);
150
151/** Open a handle to an existing persistent key.
152 *
153 * Open a handle to a key which was previously created with psa_create_key().
154 *
155 * \param lifetime The lifetime of the key. This designates a storage
156 * area where the key material is stored. This must not
157 * be #PSA_KEY_LIFETIME_VOLATILE.
158 * \param id The persistent identifier of the key.
159 * \param[out] handle On success, a handle to a key slot which contains
160 * the data and metadata loaded from the specified
161 * persistent location.
162 *
163 * \retval #PSA_SUCCESS
164 * Success. The application can now use the value of `*handle`
165 * to access the newly allocated key slot.
166 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
167 * \retval #PSA_ERROR_EMPTY_SLOT
168 * \retval #PSA_ERROR_INVALID_ARGUMENT
169 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
170 * \retval #PSA_ERROR_INVALID_ARGUMENT
171 * \p id is invalid for the specified lifetime.
172 * \retval #PSA_ERROR_NOT_SUPPORTED
173 * \p lifetime is not supported.
174 * \retval #PSA_ERROR_NOT_PERMITTED
175 * The specified key exists, but the application does not have the
176 * permission to access it. Note that this specification does not
177 * define any way to create such a key, but it may be possible
178 * through implementation-specific means.
179 */
180psa_status_t psa_open_key(psa_key_lifetime_t lifetime,
181 psa_key_id_t id,
182 psa_key_handle_t *handle);
183
184/** Create a new persistent key slot.
185 *
186 * Create a new persistent key slot and return a handle to it. The handle
187 * remains valid until the application calls psa_close_key() or terminates.
188 * The application can open the key again with psa_open_key() until it
189 * removes the key by calling psa_destroy_key().
190 *
191 * \param lifetime The lifetime of the key. This designates a storage
192 * area where the key material is stored. This must not
193 * be #PSA_KEY_LIFETIME_VOLATILE.
194 * \param id The persistent identifier of the key.
195 * \param type The type of key that the slot will contain.
196 * \param max_bits The maximum key size that the slot will contain.
197 * \param[out] handle On success, a handle to the newly created key slot.
198 * When key material is later created in this key slot,
199 * it will be saved to the specified persistent location.
200 *
201 * \retval #PSA_SUCCESS
202 * Success. The application can now use the value of `*handle`
203 * to access the newly allocated key slot.
204 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
205 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
206 * \retval #PSA_ERROR_OCCUPIED_SLOT
207 * There is already a key with the identifier \p id in the storage
208 * area designated by \p lifetime.
209 * \retval #PSA_ERROR_INVALID_ARGUMENT
210 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
211 * \retval #PSA_ERROR_INVALID_ARGUMENT
212 * \p id is invalid for the specified lifetime.
213 * \retval #PSA_ERROR_NOT_SUPPORTED
214 * \p lifetime is not supported.
215 * \retval #PSA_ERROR_NOT_PERMITTED
216 * \p lifetime is valid, but the application does not have the
217 * permission to create a key there.
218 */
219psa_status_t psa_create_key(psa_key_lifetime_t lifetime,
220 psa_key_id_t id,
221 psa_key_type_t type,
222 size_t max_bits,
223 psa_key_handle_t *handle);
224
225/** Close a key handle.
226 *
227 * If the handle designates a volatile key, destroy the key material and
228 * free all associated resources, just like psa_destroy_key().
229 *
230 * If the handle designates a persistent key, free all resources associated
231 * with the key in volatile memory. The key slot in persistent storage is
232 * not affected and can be opened again later with psa_open_key().
233 *
234 * \param handle The key handle to close.
235 *
236 * \retval #PSA_SUCCESS
237 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100238 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100239 */
240psa_status_t psa_close_key(psa_key_handle_t handle);
241
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100242/**@}*/
243
244/** \defgroup import_export Key import and export
245 * @{
246 */
247
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100248/**
249 * \brief Import a key in binary format.
250 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100251 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100252 * documentation of psa_export_public_key() for the format of public keys
253 * and to the documentation of psa_export_key() for the format for
254 * other key types.
255 *
256 * This specification supports a single format for each key type.
257 * Implementations may support other formats as long as the standard
258 * format is supported. Implementations that support other formats
259 * should ensure that the formats are clearly unambiguous so as to
260 * minimize the risk that an invalid input is accidentally interpreted
261 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100262 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100263 * \param handle Handle to the slot where the key will be stored.
264 * This must be a valid slot for a key of the chosen
265 * type: it must have been obtained by calling
266 * psa_allocate_key() or psa_create_key() with the
267 * correct \p type and with a maximum size that is
268 * compatible with \p data.
Gilles Peskinef7933932018-10-31 14:07:52 +0100269 * \param type Key type (a \c PSA_KEY_TYPE_XXX value). On a successful
270 * import, the key slot will contain a key of this type.
271 * \param[in] data Buffer containing the key data. The content of this
272 * buffer is interpreted according to \p type. It must
273 * contain the format described in the documentation
274 * of psa_export_key() or psa_export_public_key() for
275 * the chosen type.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200276 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100277 *
Gilles Peskine28538492018-07-11 17:34:00 +0200278 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100279 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100280 * If the key is persistent, the key material and the key's metadata
281 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100282 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200283 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200284 * The key type or key size is not supported, either by the
285 * implementation in general or in this particular slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200286 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine308b91d2018-02-08 09:47:44 +0100287 * The key slot is invalid,
288 * or the key data is not correctly formatted.
Gilles Peskine28538492018-07-11 17:34:00 +0200289 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskine65eb8582018-04-19 08:28:58 +0200290 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200291 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
292 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
293 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100294 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200295 * \retval #PSA_ERROR_HARDWARE_FAILURE
296 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300297 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300298 * The library has not been previously initialized by psa_crypto_init().
299 * It is implementation-dependent whether a failure to initialize
300 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100301 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100302psa_status_t psa_import_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100303 psa_key_type_t type,
304 const uint8_t *data,
305 size_t data_length);
306
307/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100308 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200309 *
310 * This function destroys the content of the key slot from both volatile
311 * memory and, if applicable, non-volatile storage. Implementations shall
312 * make a best effort to ensure that any previous content of the slot is
313 * unrecoverable.
314 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100315 * This function also erases any metadata such as policies and frees all
316 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200317 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100318 * \param handle Handle to the key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100319 *
Gilles Peskine28538492018-07-11 17:34:00 +0200320 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200321 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200322 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200323 * The slot holds content and cannot be erased because it is
324 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100325 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200326 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200327 * There was an failure in communication with the cryptoprocessor.
328 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200329 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200330 * The storage is corrupted. Implementations shall make a best effort
331 * to erase key material even in this stage, however applications
332 * should be aware that it may be impossible to guarantee that the
333 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +0200334 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200335 * An unexpected condition which is not a storage corruption or
336 * a communication failure occurred. The cryptoprocessor may have
337 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300338 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300339 * The library has not been previously initialized by psa_crypto_init().
340 * It is implementation-dependent whether a failure to initialize
341 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100342 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100343psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100344
345/**
346 * \brief Get basic metadata about a key.
347 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100348 * \param handle Handle to the key slot to query.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200349 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100350 * This may be a null pointer, in which case the key type
351 * is not written.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200352 * \param[out] bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +0100353 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +0100354 * is not written.
355 *
Gilles Peskine28538492018-07-11 17:34:00 +0200356 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100357 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200358 * \retval #PSA_ERROR_EMPTY_SLOT
Gilles Peskineae32aac2018-11-30 14:39:32 +0100359 * The handle is to a key slot which does not contain key material yet.
Gilles Peskine28538492018-07-11 17:34:00 +0200360 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
361 * \retval #PSA_ERROR_HARDWARE_FAILURE
362 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300363 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300364 * The library has not been previously initialized by psa_crypto_init().
365 * It is implementation-dependent whether a failure to initialize
366 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100367 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100368psa_status_t psa_get_key_information(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100369 psa_key_type_t *type,
370 size_t *bits);
371
372/**
Jaeden Amero283dfd12019-01-11 12:06:22 +0000373 * \brief Set domain parameters for a key.
374 *
375 * Some key types require additional domain parameters to be set before import
376 * or generation of the key. The domain parameters can be set with this
377 * function or, for key generation, through the \c extra parameter of
378 * psa_generate_key().
379 *
380 * The format for the required domain parameters varies by the key type.
Jaeden Amero1308fb52019-01-11 13:50:43 +0000381 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY),
382 * the `Dss-Parms` format as defined by RFC 3279 &sect;2.3.2.
383 * ```
384 * Dss-Parms ::= SEQUENCE {
385 * p INTEGER,
386 * q INTEGER,
387 * g INTEGER
388 * }
389 * ```
Jaeden Amero8851c402019-01-11 14:20:03 +0000390 * - For Diffie-Hellman key exchange keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY), the
391 * `DomainParameters` format as defined by RFC 3279 &sect;2.3.3.
392 * ```
393 * DomainParameters ::= SEQUENCE {
394 * p INTEGER, -- odd prime, p=jq +1
395 * g INTEGER, -- generator, g
396 * q INTEGER, -- factor of p-1
397 * j INTEGER OPTIONAL, -- subgroup factor
398 * validationParms ValidationParms OPTIONAL
399 * }
400 * ValidationParms ::= SEQUENCE {
401 * seed BIT STRING,
402 * pgenCounter INTEGER
403 * }
404 * ```
Jaeden Amero283dfd12019-01-11 12:06:22 +0000405 *
406 * \param handle Handle to the key to set domain parameters for.
407 * \param[in] data Buffer containing the key domain parameters. The content
408 * of this buffer is interpreted according to \p type. of
409 * psa_export_key() or psa_export_public_key() for the
410 * chosen type.
411 * \param data_length Size of the \p data buffer in bytes.
412 *
413 * \retval #PSA_SUCCESS
414 * \retval #PSA_ERROR_INVALID_HANDLE
415 * \retval #PSA_ERROR_OCCUPIED_SLOT
416 * There is already a key in the specified slot.
417 * \retval #PSA_ERROR_INVALID_ARGUMENT
418 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
419 * \retval #PSA_ERROR_HARDWARE_FAILURE
420 * \retval #PSA_ERROR_TAMPERING_DETECTED
421 * \retval #PSA_ERROR_BAD_STATE
422 * The library has not been previously initialized by psa_crypto_init().
423 * It is implementation-dependent whether a failure to initialize
424 * results in this error code.
425 */
426psa_status_t psa_set_key_domain_parameters(psa_key_handle_t handle,
427 const uint8_t *data,
428 size_t data_length);
429
430/**
431 * \brief Get domain parameters for a key.
432 *
433 * Get the domain parameters for a key with this function, if any. The format
434 * of the domain parameters written to \p data is specified in the
435 * documentation for psa_set_key_domain_parameters().
436 *
437 * \param handle Handle to the key to get domain parameters from.
438 * \param[out] data On success, the key domain parameters.
439 * \param data_size Size of the \p data buffer in bytes.
440 * \param[out] data_length On success, the number of bytes
441 * that make up the key domain parameters data.
442 *
443 * \retval #PSA_SUCCESS
444 * \retval #PSA_ERROR_INVALID_HANDLE
445 * \retval #PSA_ERROR_EMPTY_SLOT
446 * There is no key in the specified slot.
447 * \retval #PSA_ERROR_INVALID_ARGUMENT
448 * \retval #PSA_ERROR_NOT_SUPPORTED
449 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
450 * \retval #PSA_ERROR_HARDWARE_FAILURE
451 * \retval #PSA_ERROR_TAMPERING_DETECTED
452 * \retval #PSA_ERROR_BAD_STATE
453 * The library has not been previously initialized by psa_crypto_init().
454 * It is implementation-dependent whether a failure to initialize
455 * results in this error code.
456 */
457psa_status_t psa_get_key_domain_parameters(psa_key_handle_t handle,
458 uint8_t *data,
459 size_t data_size,
460 size_t *data_length);
461
462/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100463 * \brief Export a key in binary format.
464 *
465 * The output of this function can be passed to psa_import_key() to
466 * create an equivalent object.
467 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100468 * If the implementation of psa_import_key() supports other formats
469 * beyond the format specified here, the output from psa_export_key()
470 * must use the representation specified here, not the original
471 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100472 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100473 * For standard key types, the output format is as follows:
474 *
475 * - For symmetric keys (including MAC keys), the format is the
476 * raw bytes of the key.
477 * - For DES, the key data consists of 8 bytes. The parity bits must be
478 * correct.
479 * - For Triple-DES, the format is the concatenation of the
480 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100481 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200482 * is the non-encrypted DER encoding of the representation defined by
483 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
484 * ```
485 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200486 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200487 * modulus INTEGER, -- n
488 * publicExponent INTEGER, -- e
489 * privateExponent INTEGER, -- d
490 * prime1 INTEGER, -- p
491 * prime2 INTEGER, -- q
492 * exponent1 INTEGER, -- d mod (p-1)
493 * exponent2 INTEGER, -- d mod (q-1)
494 * coefficient INTEGER, -- (inverse of q) mod p
495 * }
496 * ```
Jaeden Amero1308fb52019-01-11 13:50:43 +0000497 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format is the
498 * representation of the private key `x` as a big-endian byte string. The
499 * length of the byte string is the private key size in bytes (leading zeroes
500 * are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200501 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +0100502 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100503 * a representation of the private value as a `ceiling(m/8)`-byte string
504 * where `m` is the bit size associated with the curve, i.e. the bit size
505 * of the order of the curve's coordinate field. This byte string is
506 * in little-endian order for Montgomery curves (curve types
507 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
508 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
509 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100510 * This is the content of the `privateKey` field of the `ECPrivateKey`
511 * format defined by RFC 5915.
Jaeden Amero8851c402019-01-11 14:20:03 +0000512 * - For Diffie-Hellman key exchange key pairs (#PSA_KEY_TYPE_DH_KEYPAIR), the
513 * format is the representation of the private key `x` as a big-endian byte
514 * string. The length of the byte string is the private key size in bytes
515 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200516 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
517 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100518 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100519 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200520 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200521 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200522 * \param[out] data_length On success, the number of bytes
523 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100524 *
Gilles Peskine28538492018-07-11 17:34:00 +0200525 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100526 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200527 * \retval #PSA_ERROR_EMPTY_SLOT
528 * \retval #PSA_ERROR_NOT_PERMITTED
Darryl Green9e2d7a02018-07-24 16:33:30 +0100529 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200530 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
531 * The size of the \p data buffer is too small. You can determine a
532 * sufficient buffer size by calling
533 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
534 * where \c type is the key type
535 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200536 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
537 * \retval #PSA_ERROR_HARDWARE_FAILURE
538 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300539 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300540 * The library has not been previously initialized by psa_crypto_init().
541 * It is implementation-dependent whether a failure to initialize
542 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100543 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100544psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100545 uint8_t *data,
546 size_t data_size,
547 size_t *data_length);
548
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100549/**
550 * \brief Export a public key or the public part of a key pair in binary format.
551 *
552 * The output of this function can be passed to psa_import_key() to
553 * create an object that is equivalent to the public key.
554 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000555 * This specification supports a single format for each key type.
556 * Implementations may support other formats as long as the standard
557 * format is supported. Implementations that support other formats
558 * should ensure that the formats are clearly unambiguous so as to
559 * minimize the risk that an invalid input is accidentally interpreted
560 * according to a different format.
561 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000562 * For standard key types, the output format is as follows:
563 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
564 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
565 * ```
566 * RSAPublicKey ::= SEQUENCE {
567 * modulus INTEGER, -- n
568 * publicExponent INTEGER } -- e
569 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000570 * - For elliptic curve public keys (key types for which
571 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
572 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
573 * Let `m` be the bit size associated with the curve, i.e. the bit size of
574 * `q` for a curve over `F_q`. The representation consists of:
575 * - The byte 0x04;
576 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
577 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Jaeden Amero1308fb52019-01-11 13:50:43 +0000578 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY), the format is the
579 * representation of the public key `y = g^x mod p` as a big-endian byte
580 * string. The length of the byte string is the length of the base prime `p`
581 * in bytes.
Jaeden Amero8851c402019-01-11 14:20:03 +0000582 * - For Diffie-Hellman key exchange public keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY),
583 * the format is the representation of the public key `y = g^x mod p` as a
584 * big-endian byte string. The length of the byte string is the length of the
585 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100586 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100587 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200588 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200589 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200590 * \param[out] data_length On success, the number of bytes
591 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100592 *
Gilles Peskine28538492018-07-11 17:34:00 +0200593 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100594 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200595 * \retval #PSA_ERROR_EMPTY_SLOT
596 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200597 * The key is neither a public key nor a key pair.
598 * \retval #PSA_ERROR_NOT_SUPPORTED
599 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
600 * The size of the \p data buffer is too small. You can determine a
601 * sufficient buffer size by calling
602 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
603 * where \c type is the key type
604 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200605 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
606 * \retval #PSA_ERROR_HARDWARE_FAILURE
607 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300608 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300609 * The library has not been previously initialized by psa_crypto_init().
610 * It is implementation-dependent whether a failure to initialize
611 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100612 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100613psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100614 uint8_t *data,
615 size_t data_size,
616 size_t *data_length);
617
618/**@}*/
619
620/** \defgroup policy Key policies
621 * @{
622 */
623
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100624/** The type of the key policy data structure.
625 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000626 * Before calling any function on a key policy, the application must initialize
627 * it by any of the following means:
628 * - Set the structure to all-bits-zero, for example:
629 * \code
630 * psa_key_policy_t policy;
631 * memset(&policy, 0, sizeof(policy));
632 * \endcode
633 * - Initialize the structure to logical zero values, for example:
634 * \code
635 * psa_key_policy_t policy = {0};
636 * \endcode
637 * - Initialize the structure to the initializer #PSA_KEY_POLICY_INIT,
638 * for example:
639 * \code
640 * psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
641 * \endcode
642 * - Assign the result of the function psa_key_policy_init()
643 * to the structure, for example:
644 * \code
645 * psa_key_policy_t policy;
646 * policy = psa_key_policy_init();
647 * \endcode
648 *
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100649 * This is an implementation-defined \c struct. Applications should not
650 * make any assumptions about the content of this structure except
651 * as directed by the documentation of a specific implementation. */
652typedef struct psa_key_policy_s psa_key_policy_t;
653
Jaeden Amero70261c52019-01-04 11:47:20 +0000654/** \def PSA_KEY_POLICY_INIT
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200655 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000656 * This macro returns a suitable initializer for a key policy object of type
657 * #psa_key_policy_t.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200658 */
Jaeden Amero70261c52019-01-04 11:47:20 +0000659#ifdef __DOXYGEN_ONLY__
660/* This is an example definition for documentation purposes.
661 * Implementations should define a suitable value in `crypto_struct.h`.
662 */
663#define PSA_KEY_POLICY_INIT {0}
664#endif
665
666/** Return an initial value for a key policy that forbids all usage of the key.
667 */
668static psa_key_policy_t psa_key_policy_init(void);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100669
Gilles Peskine7e198532018-03-08 07:50:30 +0100670/** \brief Set the standard fields of a policy structure.
671 *
672 * Note that this function does not make any consistency check of the
673 * parameters. The values are only checked when applying the policy to
674 * a key slot with psa_set_key_policy().
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200675 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000676 * \param[in,out] policy The key policy to modify. It must have been
677 * initialized as per the documentation for
678 * #psa_key_policy_t.
679 * \param usage The permitted uses for the key.
680 * \param alg The algorithm that the key may be used for.
Gilles Peskine7e198532018-03-08 07:50:30 +0100681 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100682void psa_key_policy_set_usage(psa_key_policy_t *policy,
683 psa_key_usage_t usage,
684 psa_algorithm_t alg);
685
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200686/** \brief Retrieve the usage field of a policy structure.
687 *
688 * \param[in] policy The policy object to query.
689 *
690 * \return The permitted uses for a key with this policy.
691 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +0200692psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100693
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200694/** \brief Retrieve the algorithm field of a policy structure.
695 *
696 * \param[in] policy The policy object to query.
697 *
698 * \return The permitted algorithm for a key with this policy.
699 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +0200700psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100701
702/** \brief Set the usage policy on a key slot.
703 *
704 * This function must be called on an empty key slot, before importing,
705 * generating or creating a key in the slot. Changing the policy of an
706 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +0100707 *
708 * Implementations may set restrictions on supported key policies
709 * depending on the key type and the key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200710 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100711 * \param handle Handle to the key whose policy is to be changed.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200712 * \param[in] policy The policy object to query.
713 *
714 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100715 * Success.
716 * If the key is persistent, it is implementation-defined whether
717 * the policy has been saved to persistent storage. Implementations
718 * may defer saving the policy until the key material is created.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100719 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200720 * \retval #PSA_ERROR_OCCUPIED_SLOT
721 * \retval #PSA_ERROR_NOT_SUPPORTED
722 * \retval #PSA_ERROR_INVALID_ARGUMENT
723 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
724 * \retval #PSA_ERROR_HARDWARE_FAILURE
725 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300726 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300727 * The library has not been previously initialized by psa_crypto_init().
728 * It is implementation-dependent whether a failure to initialize
729 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100730 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100731psa_status_t psa_set_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100732 const psa_key_policy_t *policy);
733
Gilles Peskine7e198532018-03-08 07:50:30 +0100734/** \brief Get the usage policy for a key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200735 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100736 * \param handle Handle to the key slot whose policy is being queried.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200737 * \param[out] policy On success, the key's policy.
738 *
739 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100740 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200741 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
742 * \retval #PSA_ERROR_HARDWARE_FAILURE
743 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300744 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300745 * The library has not been previously initialized by psa_crypto_init().
746 * It is implementation-dependent whether a failure to initialize
747 * results in this error code.
Gilles Peskine7e198532018-03-08 07:50:30 +0100748 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100749psa_status_t psa_get_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100750 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +0100751
752/**@}*/
753
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100754/** \defgroup hash Message digests
755 * @{
756 */
757
Gilles Peskine308b91d2018-02-08 09:47:44 +0100758/** The type of the state data structure for multipart hash operations.
759 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000760 * Before calling any function on a hash operation object, the application must
761 * initialize it by any of the following means:
762 * - Set the structure to all-bits-zero, for example:
763 * \code
764 * psa_hash_operation_t operation;
765 * memset(&operation, 0, sizeof(operation));
766 * \endcode
767 * - Initialize the structure to logical zero values, for example:
768 * \code
769 * psa_hash_operation_t operation = {0};
770 * \endcode
771 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
772 * for example:
773 * \code
774 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
775 * \endcode
776 * - Assign the result of the function psa_hash_operation_init()
777 * to the structure, for example:
778 * \code
779 * psa_hash_operation_t operation;
780 * operation = psa_hash_operation_init();
781 * \endcode
782 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100783 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100784 * make any assumptions about the content of this structure except
785 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100786typedef struct psa_hash_operation_s psa_hash_operation_t;
787
Jaeden Amero6a25b412019-01-04 11:47:44 +0000788/** \def PSA_HASH_OPERATION_INIT
789 *
790 * This macro returns a suitable initializer for a hash operation object
791 * of type #psa_hash_operation_t.
792 */
793#ifdef __DOXYGEN_ONLY__
794/* This is an example definition for documentation purposes.
795 * Implementations should define a suitable value in `crypto_struct.h`.
796 */
797#define PSA_HASH_OPERATION_INIT {0}
798#endif
799
800/** Return an initial value for a hash operation object.
801 */
802static psa_hash_operation_t psa_hash_operation_init(void);
803
Gilles Peskine308b91d2018-02-08 09:47:44 +0100804/** Start a multipart hash operation.
805 *
806 * The sequence of operations to calculate a hash (message digest)
807 * is as follows:
808 * -# Allocate an operation object which will be passed to all the functions
809 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000810 * -# Initialize the operation object with one of the methods described in the
811 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200812 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100813 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100814 * of the message each time. The hash that is calculated is the hash
815 * of the concatenation of these messages in order.
816 * -# To calculate the hash, call psa_hash_finish().
817 * To compare the hash with an expected value, call psa_hash_verify().
818 *
819 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000820 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100821 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200822 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100823 * eventually terminate the operation. The following events terminate an
824 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100825 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100826 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100827 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000828 * \param[in,out] operation The operation object to set up. It must have
829 * been initialized as per the documentation for
830 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200831 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
832 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100833 *
Gilles Peskine28538492018-07-11 17:34:00 +0200834 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100835 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200836 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200837 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +0200838 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
839 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
840 * \retval #PSA_ERROR_HARDWARE_FAILURE
841 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100842 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200843psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100844 psa_algorithm_t alg);
845
Gilles Peskine308b91d2018-02-08 09:47:44 +0100846/** Add a message fragment to a multipart hash operation.
847 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200848 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100849 *
850 * If this function returns an error status, the operation becomes inactive.
851 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200852 * \param[in,out] operation Active hash operation.
853 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200854 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100855 *
Gilles Peskine28538492018-07-11 17:34:00 +0200856 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100857 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200858 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100859 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200860 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
861 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
862 * \retval #PSA_ERROR_HARDWARE_FAILURE
863 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100864 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100865psa_status_t psa_hash_update(psa_hash_operation_t *operation,
866 const uint8_t *input,
867 size_t input_length);
868
Gilles Peskine308b91d2018-02-08 09:47:44 +0100869/** Finish the calculation of the hash of a message.
870 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200871 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100872 * This function calculates the hash of the message formed by concatenating
873 * the inputs passed to preceding calls to psa_hash_update().
874 *
875 * When this function returns, the operation becomes inactive.
876 *
877 * \warning Applications should not call this function if they expect
878 * a specific value for the hash. Call psa_hash_verify() instead.
879 * Beware that comparing integrity or authenticity data such as
880 * hash values with a function such as \c memcmp is risky
881 * because the time taken by the comparison may leak information
882 * about the hashed data which could allow an attacker to guess
883 * a valid hash and thereby bypass security controls.
884 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200885 * \param[in,out] operation Active hash operation.
886 * \param[out] hash Buffer where the hash is to be written.
887 * \param hash_size Size of the \p hash buffer in bytes.
888 * \param[out] hash_length On success, the number of bytes
889 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +0200890 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +0200891 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100892 *
Gilles Peskine28538492018-07-11 17:34:00 +0200893 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100894 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200895 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100896 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200897 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200898 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200899 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100900 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +0200901 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
902 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
903 * \retval #PSA_ERROR_HARDWARE_FAILURE
904 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100905 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100906psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
907 uint8_t *hash,
908 size_t hash_size,
909 size_t *hash_length);
910
Gilles Peskine308b91d2018-02-08 09:47:44 +0100911/** Finish the calculation of the hash of a message and compare it with
912 * an expected value.
913 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200914 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100915 * This function calculates the hash of the message formed by concatenating
916 * the inputs passed to preceding calls to psa_hash_update(). It then
917 * compares the calculated hash with the expected hash passed as a
918 * parameter to this function.
919 *
920 * When this function returns, the operation becomes inactive.
921 *
Gilles Peskine19067982018-03-20 17:54:53 +0100922 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +0100923 * comparison between the actual hash and the expected hash is performed
924 * in constant time.
925 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200926 * \param[in,out] operation Active hash operation.
927 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200928 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100929 *
Gilles Peskine28538492018-07-11 17:34:00 +0200930 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100931 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +0200932 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100933 * The hash of the message was calculated successfully, but it
934 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +0200935 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100936 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200937 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
938 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
939 * \retval #PSA_ERROR_HARDWARE_FAILURE
940 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100941 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100942psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
943 const uint8_t *hash,
944 size_t hash_length);
945
Gilles Peskine308b91d2018-02-08 09:47:44 +0100946/** Abort a hash operation.
947 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100948 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200949 * \p operation structure itself. Once aborted, the operation object
950 * can be reused for another operation by calling
951 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100952 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200953 * You may call this function any time after the operation object has
954 * been initialized by any of the following methods:
955 * - A call to psa_hash_setup(), whether it succeeds or not.
956 * - Initializing the \c struct to all-bits-zero.
957 * - Initializing the \c struct to logical zeros, e.g.
958 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100959 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200960 * In particular, calling psa_hash_abort() after the operation has been
961 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
962 * psa_hash_verify() is safe and has no effect.
963 *
964 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100965 *
Gilles Peskine28538492018-07-11 17:34:00 +0200966 * \retval #PSA_SUCCESS
967 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200968 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +0200969 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
970 * \retval #PSA_ERROR_HARDWARE_FAILURE
971 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100972 */
973psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100974
975/**@}*/
976
Gilles Peskine8c9def32018-02-08 10:02:12 +0100977/** \defgroup MAC Message authentication codes
978 * @{
979 */
980
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100981/** The type of the state data structure for multipart MAC operations.
982 *
Jaeden Amero769ce272019-01-04 11:48:03 +0000983 * Before calling any function on a MAC operation object, the application must
984 * initialize it by any of the following means:
985 * - Set the structure to all-bits-zero, for example:
986 * \code
987 * psa_mac_operation_t operation;
988 * memset(&operation, 0, sizeof(operation));
989 * \endcode
990 * - Initialize the structure to logical zero values, for example:
991 * \code
992 * psa_mac_operation_t operation = {0};
993 * \endcode
994 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
995 * for example:
996 * \code
997 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
998 * \endcode
999 * - Assign the result of the function psa_mac_operation_init()
1000 * to the structure, for example:
1001 * \code
1002 * psa_mac_operation_t operation;
1003 * operation = psa_mac_operation_init();
1004 * \endcode
1005 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001006 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001007 * make any assumptions about the content of this structure except
1008 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001009typedef struct psa_mac_operation_s psa_mac_operation_t;
1010
Jaeden Amero769ce272019-01-04 11:48:03 +00001011/** \def PSA_MAC_OPERATION_INIT
1012 *
1013 * This macro returns a suitable initializer for a MAC operation object of type
1014 * #psa_mac_operation_t.
1015 */
1016#ifdef __DOXYGEN_ONLY__
1017/* This is an example definition for documentation purposes.
1018 * Implementations should define a suitable value in `crypto_struct.h`.
1019 */
1020#define PSA_MAC_OPERATION_INIT {0}
1021#endif
1022
1023/** Return an initial value for a MAC operation object.
1024 */
1025static psa_mac_operation_t psa_mac_operation_init(void);
1026
Gilles Peskine89167cb2018-07-08 20:12:23 +02001027/** Start a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001028 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001029 * This function sets up the calculation of the MAC
1030 * (message authentication code) of a byte string.
1031 * To verify the MAC of a message against an
1032 * expected value, use psa_mac_verify_setup() instead.
1033 *
1034 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001035 * -# Allocate an operation object which will be passed to all the functions
1036 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001037 * -# Initialize the operation object with one of the methods described in the
1038 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001039 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001040 * The key remains associated with the operation even if the content
1041 * of the key slot changes.
1042 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1043 * of the message each time. The MAC that is calculated is the MAC
1044 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001045 * -# At the end of the message, call psa_mac_sign_finish() to finish
1046 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001047 *
1048 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001049 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001050 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001051 * After a successful call to psa_mac_sign_setup(), the application must
1052 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001053 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001054 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001055 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001056 * \param[in,out] operation The operation object to set up. It must have
1057 * been initialized as per the documentation for
1058 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001059 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001060 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1061 * such that #PSA_ALG_IS_MAC(alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001062 *
Gilles Peskine28538492018-07-11 17:34:00 +02001063 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001064 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001065 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001066 * \retval #PSA_ERROR_EMPTY_SLOT
1067 * \retval #PSA_ERROR_NOT_PERMITTED
1068 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001069 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001070 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001071 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001072 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1073 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1074 * \retval #PSA_ERROR_HARDWARE_FAILURE
1075 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001076 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001077 * The library has not been previously initialized by psa_crypto_init().
1078 * It is implementation-dependent whether a failure to initialize
1079 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001080 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001081psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001082 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001083 psa_algorithm_t alg);
1084
1085/** Start a multipart MAC verification operation.
1086 *
1087 * This function sets up the verification of the MAC
1088 * (message authentication code) of a byte string against an expected value.
1089 *
1090 * The sequence of operations to verify a MAC is as follows:
1091 * -# Allocate an operation object which will be passed to all the functions
1092 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001093 * -# Initialize the operation object with one of the methods described in the
1094 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001095 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
1096 * The key remains associated with the operation even if the content
1097 * of the key slot changes.
1098 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1099 * of the message each time. The MAC that is calculated is the MAC
1100 * of the concatenation of these messages in order.
1101 * -# At the end of the message, call psa_mac_verify_finish() to finish
1102 * calculating the actual MAC of the message and verify it against
1103 * the expected value.
1104 *
1105 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001106 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001107 *
1108 * After a successful call to psa_mac_verify_setup(), the application must
1109 * eventually terminate the operation through one of the following methods:
1110 * - A failed call to psa_mac_update().
1111 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1112 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001113 * \param[in,out] operation The operation object to set up. It must have
1114 * been initialized as per the documentation for
1115 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001116 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001117 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1118 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001119 *
Gilles Peskine28538492018-07-11 17:34:00 +02001120 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001121 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001122 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001123 * \retval #PSA_ERROR_EMPTY_SLOT
1124 * \retval #PSA_ERROR_NOT_PERMITTED
1125 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001126 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001127 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001128 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001129 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1130 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1131 * \retval #PSA_ERROR_HARDWARE_FAILURE
1132 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001133 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001134 * The library has not been previously initialized by psa_crypto_init().
1135 * It is implementation-dependent whether a failure to initialize
1136 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001137 */
1138psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001139 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001140 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001141
Gilles Peskinedcd14942018-07-12 00:30:52 +02001142/** Add a message fragment to a multipart MAC operation.
1143 *
1144 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1145 * before calling this function.
1146 *
1147 * If this function returns an error status, the operation becomes inactive.
1148 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001149 * \param[in,out] operation Active MAC operation.
1150 * \param[in] input Buffer containing the message fragment to add to
1151 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001152 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001153 *
1154 * \retval #PSA_SUCCESS
1155 * Success.
1156 * \retval #PSA_ERROR_BAD_STATE
1157 * The operation state is not valid (not started, or already completed).
1158 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1159 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1160 * \retval #PSA_ERROR_HARDWARE_FAILURE
1161 * \retval #PSA_ERROR_TAMPERING_DETECTED
1162 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001163psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1164 const uint8_t *input,
1165 size_t input_length);
1166
Gilles Peskinedcd14942018-07-12 00:30:52 +02001167/** Finish the calculation of the MAC of a message.
1168 *
1169 * The application must call psa_mac_sign_setup() before calling this function.
1170 * This function calculates the MAC of the message formed by concatenating
1171 * the inputs passed to preceding calls to psa_mac_update().
1172 *
1173 * When this function returns, the operation becomes inactive.
1174 *
1175 * \warning Applications should not call this function if they expect
1176 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1177 * Beware that comparing integrity or authenticity data such as
1178 * MAC values with a function such as \c memcmp is risky
1179 * because the time taken by the comparison may leak information
1180 * about the MAC value which could allow an attacker to guess
1181 * a valid MAC and thereby bypass security controls.
1182 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001183 * \param[in,out] operation Active MAC operation.
1184 * \param[out] mac Buffer where the MAC value is to be written.
1185 * \param mac_size Size of the \p mac buffer in bytes.
1186 * \param[out] mac_length On success, the number of bytes
1187 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001188 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001189 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001190 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001191 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001192 *
1193 * \retval #PSA_SUCCESS
1194 * Success.
1195 * \retval #PSA_ERROR_BAD_STATE
1196 * The operation state is not valid (not started, or already completed).
1197 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001198 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001199 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1200 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1201 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1202 * \retval #PSA_ERROR_HARDWARE_FAILURE
1203 * \retval #PSA_ERROR_TAMPERING_DETECTED
1204 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001205psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1206 uint8_t *mac,
1207 size_t mac_size,
1208 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001209
Gilles Peskinedcd14942018-07-12 00:30:52 +02001210/** Finish the calculation of the MAC of a message and compare it with
1211 * an expected value.
1212 *
1213 * The application must call psa_mac_verify_setup() before calling this function.
1214 * This function calculates the MAC of the message formed by concatenating
1215 * the inputs passed to preceding calls to psa_mac_update(). It then
1216 * compares the calculated MAC with the expected MAC passed as a
1217 * parameter to this function.
1218 *
1219 * When this function returns, the operation becomes inactive.
1220 *
1221 * \note Implementations shall make the best effort to ensure that the
1222 * comparison between the actual MAC and the expected MAC is performed
1223 * in constant time.
1224 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001225 * \param[in,out] operation Active MAC operation.
1226 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001227 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001228 *
1229 * \retval #PSA_SUCCESS
1230 * The expected MAC is identical to the actual MAC of the message.
1231 * \retval #PSA_ERROR_INVALID_SIGNATURE
1232 * The MAC of the message was calculated successfully, but it
1233 * differs from the expected MAC.
1234 * \retval #PSA_ERROR_BAD_STATE
1235 * The operation state is not valid (not started, or already completed).
1236 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1237 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1238 * \retval #PSA_ERROR_HARDWARE_FAILURE
1239 * \retval #PSA_ERROR_TAMPERING_DETECTED
1240 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001241psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1242 const uint8_t *mac,
1243 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001244
Gilles Peskinedcd14942018-07-12 00:30:52 +02001245/** Abort a MAC operation.
1246 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001247 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001248 * \p operation structure itself. Once aborted, the operation object
1249 * can be reused for another operation by calling
1250 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001251 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001252 * You may call this function any time after the operation object has
1253 * been initialized by any of the following methods:
1254 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1255 * it succeeds or not.
1256 * - Initializing the \c struct to all-bits-zero.
1257 * - Initializing the \c struct to logical zeros, e.g.
1258 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001259 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001260 * In particular, calling psa_mac_abort() after the operation has been
1261 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1262 * psa_mac_verify_finish() is safe and has no effect.
1263 *
1264 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001265 *
1266 * \retval #PSA_SUCCESS
1267 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001268 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001269 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1270 * \retval #PSA_ERROR_HARDWARE_FAILURE
1271 * \retval #PSA_ERROR_TAMPERING_DETECTED
1272 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001273psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1274
1275/**@}*/
1276
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001277/** \defgroup cipher Symmetric ciphers
1278 * @{
1279 */
1280
1281/** The type of the state data structure for multipart cipher operations.
1282 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001283 * Before calling any function on a cipher operation object, the application
1284 * must initialize it by any of the following means:
1285 * - Set the structure to all-bits-zero, for example:
1286 * \code
1287 * psa_cipher_operation_t operation;
1288 * memset(&operation, 0, sizeof(operation));
1289 * \endcode
1290 * - Initialize the structure to logical zero values, for example:
1291 * \code
1292 * psa_cipher_operation_t operation = {0};
1293 * \endcode
1294 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1295 * for example:
1296 * \code
1297 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1298 * \endcode
1299 * - Assign the result of the function psa_cipher_operation_init()
1300 * to the structure, for example:
1301 * \code
1302 * psa_cipher_operation_t operation;
1303 * operation = psa_cipher_operation_init();
1304 * \endcode
1305 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001306 * This is an implementation-defined \c struct. Applications should not
1307 * make any assumptions about the content of this structure except
1308 * as directed by the documentation of a specific implementation. */
1309typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1310
Jaeden Amero5bae2272019-01-04 11:48:27 +00001311/** \def PSA_CIPHER_OPERATION_INIT
1312 *
1313 * This macro returns a suitable initializer for a cipher operation object of
1314 * type #psa_cipher_operation_t.
1315 */
1316#ifdef __DOXYGEN_ONLY__
1317/* This is an example definition for documentation purposes.
1318 * Implementations should define a suitable value in `crypto_struct.h`.
1319 */
1320#define PSA_CIPHER_OPERATION_INIT {0}
1321#endif
1322
1323/** Return an initial value for a cipher operation object.
1324 */
1325static psa_cipher_operation_t psa_cipher_operation_init(void);
1326
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001327/** Set the key for a multipart symmetric encryption operation.
1328 *
1329 * The sequence of operations to encrypt a message with a symmetric cipher
1330 * is as follows:
1331 * -# Allocate an operation object which will be passed to all the functions
1332 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001333 * -# Initialize the operation object with one of the methods described in the
1334 * documentation for #psa_cipher_operation_t, e.g.
1335 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001336 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001337 * The key remains associated with the operation even if the content
1338 * of the key slot changes.
itayzafrired7382f2018-08-02 14:19:33 +03001339 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001340 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001341 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001342 * requires a specific IV value.
1343 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1344 * of the message each time.
1345 * -# Call psa_cipher_finish().
1346 *
1347 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001348 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001349 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001350 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001351 * eventually terminate the operation. The following events terminate an
1352 * operation:
itayzafrired7382f2018-08-02 14:19:33 +03001353 * - A failed call to psa_cipher_generate_iv(), psa_cipher_set_iv()
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001354 * or psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001355 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001356 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001357 * \param[in,out] operation The operation object to set up. It must have
1358 * been initialized as per the documentation for
1359 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001360 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001361 * \param alg The cipher algorithm to compute
1362 * (\c PSA_ALG_XXX value such that
1363 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001364 *
Gilles Peskine28538492018-07-11 17:34:00 +02001365 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001366 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001367 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001368 * \retval #PSA_ERROR_EMPTY_SLOT
1369 * \retval #PSA_ERROR_NOT_PERMITTED
1370 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001371 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001372 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001373 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001374 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1375 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1376 * \retval #PSA_ERROR_HARDWARE_FAILURE
1377 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001378 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001379 * The library has not been previously initialized by psa_crypto_init().
1380 * It is implementation-dependent whether a failure to initialize
1381 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001382 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001383psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001384 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001385 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001386
1387/** Set the key for a multipart symmetric decryption operation.
1388 *
1389 * The sequence of operations to decrypt a message with a symmetric cipher
1390 * is as follows:
1391 * -# Allocate an operation object which will be passed to all the functions
1392 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001393 * -# Initialize the operation object with one of the methods described in the
1394 * documentation for #psa_cipher_operation_t, e.g.
1395 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001396 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001397 * The key remains associated with the operation even if the content
1398 * of the key slot changes.
1399 * -# Call psa_cipher_update() with the IV (initialization vector) for the
1400 * decryption. If the IV is prepended to the ciphertext, you can call
1401 * psa_cipher_update() on a buffer containing the IV followed by the
1402 * beginning of the message.
1403 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1404 * of the message each time.
1405 * -# Call psa_cipher_finish().
1406 *
1407 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001408 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001409 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001410 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001411 * eventually terminate the operation. The following events terminate an
1412 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001413 * - A failed call to psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001414 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001415 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001416 * \param[in,out] operation The operation object to set up. It must have
1417 * been initialized as per the documentation for
1418 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001419 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001420 * \param alg The cipher algorithm to compute
1421 * (\c PSA_ALG_XXX value such that
1422 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001423 *
Gilles Peskine28538492018-07-11 17:34:00 +02001424 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001425 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001426 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001427 * \retval #PSA_ERROR_EMPTY_SLOT
1428 * \retval #PSA_ERROR_NOT_PERMITTED
1429 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001430 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001431 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001432 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001433 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1434 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1435 * \retval #PSA_ERROR_HARDWARE_FAILURE
1436 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001437 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001438 * The library has not been previously initialized by psa_crypto_init().
1439 * It is implementation-dependent whether a failure to initialize
1440 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001441 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001442psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001443 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001444 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001445
Gilles Peskinedcd14942018-07-12 00:30:52 +02001446/** Generate an IV for a symmetric encryption operation.
1447 *
1448 * This function generates a random IV (initialization vector), nonce
1449 * or initial counter value for the encryption operation as appropriate
1450 * for the chosen algorithm, key type and key size.
1451 *
1452 * The application must call psa_cipher_encrypt_setup() before
1453 * calling this function.
1454 *
1455 * If this function returns an error status, the operation becomes inactive.
1456 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001457 * \param[in,out] operation Active cipher operation.
1458 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001459 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001460 * \param[out] iv_length On success, the number of bytes of the
1461 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001462 *
1463 * \retval #PSA_SUCCESS
1464 * Success.
1465 * \retval #PSA_ERROR_BAD_STATE
1466 * The operation state is not valid (not started, or IV already set).
1467 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001468 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001469 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1470 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1471 * \retval #PSA_ERROR_HARDWARE_FAILURE
1472 * \retval #PSA_ERROR_TAMPERING_DETECTED
1473 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001474psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1475 unsigned char *iv,
1476 size_t iv_size,
1477 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001478
Gilles Peskinedcd14942018-07-12 00:30:52 +02001479/** Set the IV for a symmetric encryption or decryption operation.
1480 *
1481 * This function sets the random IV (initialization vector), nonce
1482 * or initial counter value for the encryption or decryption operation.
1483 *
1484 * The application must call psa_cipher_encrypt_setup() before
1485 * calling this function.
1486 *
1487 * If this function returns an error status, the operation becomes inactive.
1488 *
1489 * \note When encrypting, applications should use psa_cipher_generate_iv()
1490 * instead of this function, unless implementing a protocol that requires
1491 * a non-random IV.
1492 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001493 * \param[in,out] operation Active cipher operation.
1494 * \param[in] iv Buffer containing the IV to use.
1495 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001496 *
1497 * \retval #PSA_SUCCESS
1498 * Success.
1499 * \retval #PSA_ERROR_BAD_STATE
1500 * The operation state is not valid (not started, or IV already set).
1501 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001502 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001503 * or the chosen algorithm does not use an IV.
1504 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1505 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1506 * \retval #PSA_ERROR_HARDWARE_FAILURE
1507 * \retval #PSA_ERROR_TAMPERING_DETECTED
1508 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001509psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1510 const unsigned char *iv,
1511 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001512
Gilles Peskinedcd14942018-07-12 00:30:52 +02001513/** Encrypt or decrypt a message fragment in an active cipher operation.
1514 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001515 * Before calling this function, you must:
1516 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1517 * The choice of setup function determines whether this function
1518 * encrypts or decrypts its input.
1519 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1520 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001521 *
1522 * If this function returns an error status, the operation becomes inactive.
1523 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001524 * \param[in,out] operation Active cipher operation.
1525 * \param[in] input Buffer containing the message fragment to
1526 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001527 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001528 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001529 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001530 * \param[out] output_length On success, the number of bytes
1531 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001532 *
1533 * \retval #PSA_SUCCESS
1534 * Success.
1535 * \retval #PSA_ERROR_BAD_STATE
1536 * The operation state is not valid (not started, IV required but
1537 * not set, or already completed).
1538 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1539 * The size of the \p output buffer is too small.
1540 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1541 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1542 * \retval #PSA_ERROR_HARDWARE_FAILURE
1543 * \retval #PSA_ERROR_TAMPERING_DETECTED
1544 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001545psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1546 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001547 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001548 unsigned char *output,
1549 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001550 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001551
Gilles Peskinedcd14942018-07-12 00:30:52 +02001552/** Finish encrypting or decrypting a message in a cipher operation.
1553 *
1554 * The application must call psa_cipher_encrypt_setup() or
1555 * psa_cipher_decrypt_setup() before calling this function. The choice
1556 * of setup function determines whether this function encrypts or
1557 * decrypts its input.
1558 *
1559 * This function finishes the encryption or decryption of the message
1560 * formed by concatenating the inputs passed to preceding calls to
1561 * psa_cipher_update().
1562 *
1563 * When this function returns, the operation becomes inactive.
1564 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001565 * \param[in,out] operation Active cipher operation.
1566 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001567 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001568 * \param[out] output_length On success, the number of bytes
1569 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001570 *
1571 * \retval #PSA_SUCCESS
1572 * Success.
1573 * \retval #PSA_ERROR_BAD_STATE
1574 * The operation state is not valid (not started, IV required but
1575 * not set, or already completed).
1576 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1577 * The size of the \p output buffer is too small.
1578 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1579 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1580 * \retval #PSA_ERROR_HARDWARE_FAILURE
1581 * \retval #PSA_ERROR_TAMPERING_DETECTED
1582 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001583psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001584 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001585 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001586 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001587
Gilles Peskinedcd14942018-07-12 00:30:52 +02001588/** Abort a cipher operation.
1589 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001590 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001591 * \p operation structure itself. Once aborted, the operation object
1592 * can be reused for another operation by calling
1593 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001594 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001595 * You may call this function any time after the operation object has
1596 * been initialized by any of the following methods:
1597 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1598 * whether it succeeds or not.
1599 * - Initializing the \c struct to all-bits-zero.
1600 * - Initializing the \c struct to logical zeros, e.g.
1601 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001602 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001603 * In particular, calling psa_cipher_abort() after the operation has been
1604 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1605 * is safe and has no effect.
1606 *
1607 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001608 *
1609 * \retval #PSA_SUCCESS
1610 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001611 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001612 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1613 * \retval #PSA_ERROR_HARDWARE_FAILURE
1614 * \retval #PSA_ERROR_TAMPERING_DETECTED
1615 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001616psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1617
1618/**@}*/
1619
Gilles Peskine3b555712018-03-03 21:27:57 +01001620/** \defgroup aead Authenticated encryption with associated data (AEAD)
1621 * @{
1622 */
1623
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001624/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001625 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001626 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001627 * \param alg The AEAD algorithm to compute
1628 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001629 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001630 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001631 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001632 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001633 * but not encrypted.
1634 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001635 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001636 * encrypted.
1637 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001638 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001639 * encrypted data. The additional data is not
1640 * part of this output. For algorithms where the
1641 * encrypted data and the authentication tag
1642 * are defined as separate outputs, the
1643 * authentication tag is appended to the
1644 * encrypted data.
1645 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1646 * This must be at least
1647 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1648 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001649 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001650 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001651 *
Gilles Peskine28538492018-07-11 17:34:00 +02001652 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001653 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001654 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001655 * \retval #PSA_ERROR_EMPTY_SLOT
1656 * \retval #PSA_ERROR_NOT_PERMITTED
1657 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001658 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001659 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001660 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001661 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1662 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1663 * \retval #PSA_ERROR_HARDWARE_FAILURE
1664 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001665 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001666 * The library has not been previously initialized by psa_crypto_init().
1667 * It is implementation-dependent whether a failure to initialize
1668 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001669 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001670psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001671 psa_algorithm_t alg,
1672 const uint8_t *nonce,
1673 size_t nonce_length,
1674 const uint8_t *additional_data,
1675 size_t additional_data_length,
1676 const uint8_t *plaintext,
1677 size_t plaintext_length,
1678 uint8_t *ciphertext,
1679 size_t ciphertext_size,
1680 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001681
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001682/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001683 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001684 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001685 * \param alg The AEAD algorithm to compute
1686 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001687 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001688 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001689 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001690 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001691 * but not encrypted.
1692 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001693 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001694 * encrypted. For algorithms where the
1695 * encrypted data and the authentication tag
1696 * are defined as separate inputs, the buffer
1697 * must contain the encrypted data followed
1698 * by the authentication tag.
1699 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001700 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001701 * \param plaintext_size Size of the \p plaintext buffer in bytes.
1702 * This must be at least
1703 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
1704 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001705 * \param[out] plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03001706 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001707 *
Gilles Peskine28538492018-07-11 17:34:00 +02001708 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001709 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001710 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001711 * \retval #PSA_ERROR_EMPTY_SLOT
1712 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001713 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02001714 * \retval #PSA_ERROR_NOT_PERMITTED
1715 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001716 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001717 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001718 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001719 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1720 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1721 * \retval #PSA_ERROR_HARDWARE_FAILURE
1722 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001723 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001724 * The library has not been previously initialized by psa_crypto_init().
1725 * It is implementation-dependent whether a failure to initialize
1726 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001727 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001728psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001729 psa_algorithm_t alg,
1730 const uint8_t *nonce,
1731 size_t nonce_length,
1732 const uint8_t *additional_data,
1733 size_t additional_data_length,
1734 const uint8_t *ciphertext,
1735 size_t ciphertext_length,
1736 uint8_t *plaintext,
1737 size_t plaintext_size,
1738 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001739
1740/**@}*/
1741
Gilles Peskine20035e32018-02-03 22:44:14 +01001742/** \defgroup asymmetric Asymmetric cryptography
1743 * @{
1744 */
1745
1746/**
1747 * \brief Sign a hash or short message with a private key.
1748 *
Gilles Peskine08bac712018-06-26 16:14:46 +02001749 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001750 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02001751 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
1752 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
1753 * to determine the hash algorithm to use.
1754 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001755 * \param handle Handle to the key to use for the operation.
1756 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001757 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001758 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001759 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001760 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001761 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001762 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001763 * \param[out] signature_length On success, the number of bytes
1764 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001765 *
Gilles Peskine28538492018-07-11 17:34:00 +02001766 * \retval #PSA_SUCCESS
1767 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001768 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01001769 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001770 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001771 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001772 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001773 * \retval #PSA_ERROR_NOT_SUPPORTED
1774 * \retval #PSA_ERROR_INVALID_ARGUMENT
1775 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1776 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1777 * \retval #PSA_ERROR_HARDWARE_FAILURE
1778 * \retval #PSA_ERROR_TAMPERING_DETECTED
1779 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03001780 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001781 * The library has not been previously initialized by psa_crypto_init().
1782 * It is implementation-dependent whether a failure to initialize
1783 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01001784 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001785psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01001786 psa_algorithm_t alg,
1787 const uint8_t *hash,
1788 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01001789 uint8_t *signature,
1790 size_t signature_size,
1791 size_t *signature_length);
1792
1793/**
1794 * \brief Verify the signature a hash or short message using a public key.
1795 *
Gilles Peskine08bac712018-06-26 16:14:46 +02001796 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001797 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02001798 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
1799 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
1800 * to determine the hash algorithm to use.
1801 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001802 * \param handle Handle to the key to use for the operation.
1803 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001804 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001805 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001806 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02001807 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001808 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001809 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001810 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001811 *
Gilles Peskine28538492018-07-11 17:34:00 +02001812 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001813 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02001814 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001815 * The calculation was perfomed successfully, but the passed
1816 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02001817 * \retval #PSA_ERROR_NOT_SUPPORTED
1818 * \retval #PSA_ERROR_INVALID_ARGUMENT
1819 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1820 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1821 * \retval #PSA_ERROR_HARDWARE_FAILURE
1822 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001823 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001824 * The library has not been previously initialized by psa_crypto_init().
1825 * It is implementation-dependent whether a failure to initialize
1826 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01001827 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001828psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01001829 psa_algorithm_t alg,
1830 const uint8_t *hash,
1831 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02001832 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02001833 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01001834
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001835/**
1836 * \brief Encrypt a short message with a public key.
1837 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001838 * \param handle Handle to the key to use for the operation.
1839 * It must be a public key or an asymmetric
1840 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001841 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001842 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001843 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001844 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001845 * \param[in] salt A salt or label, if supported by the
1846 * encryption algorithm.
1847 * If the algorithm does not support a
1848 * salt, pass \c NULL.
1849 * If the algorithm supports an optional
1850 * salt and you do not want to pass a salt,
1851 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001852 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001853 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1854 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001855 * \param salt_length Size of the \p salt buffer in bytes.
1856 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001857 * \param[out] output Buffer where the encrypted message is to
1858 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001859 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001860 * \param[out] output_length On success, the number of bytes
1861 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001862 *
Gilles Peskine28538492018-07-11 17:34:00 +02001863 * \retval #PSA_SUCCESS
1864 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001865 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001866 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001867 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001868 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001869 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001870 * \retval #PSA_ERROR_NOT_SUPPORTED
1871 * \retval #PSA_ERROR_INVALID_ARGUMENT
1872 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1873 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1874 * \retval #PSA_ERROR_HARDWARE_FAILURE
1875 * \retval #PSA_ERROR_TAMPERING_DETECTED
1876 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03001877 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001878 * The library has not been previously initialized by psa_crypto_init().
1879 * It is implementation-dependent whether a failure to initialize
1880 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001881 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001882psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001883 psa_algorithm_t alg,
1884 const uint8_t *input,
1885 size_t input_length,
1886 const uint8_t *salt,
1887 size_t salt_length,
1888 uint8_t *output,
1889 size_t output_size,
1890 size_t *output_length);
1891
1892/**
1893 * \brief Decrypt a short message with a private key.
1894 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001895 * \param handle Handle to the key to use for the operation.
1896 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001897 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001898 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001899 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001900 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001901 * \param[in] salt A salt or label, if supported by the
1902 * encryption algorithm.
1903 * If the algorithm does not support a
1904 * salt, pass \c NULL.
1905 * If the algorithm supports an optional
1906 * salt and you do not want to pass a salt,
1907 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001908 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001909 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1910 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001911 * \param salt_length Size of the \p salt buffer in bytes.
1912 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001913 * \param[out] output Buffer where the decrypted message is to
1914 * be written.
1915 * \param output_size Size of the \c output buffer in bytes.
1916 * \param[out] output_length On success, the number of bytes
1917 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001918 *
Gilles Peskine28538492018-07-11 17:34:00 +02001919 * \retval #PSA_SUCCESS
1920 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001921 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001922 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001923 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001924 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001925 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001926 * \retval #PSA_ERROR_NOT_SUPPORTED
1927 * \retval #PSA_ERROR_INVALID_ARGUMENT
1928 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1929 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1930 * \retval #PSA_ERROR_HARDWARE_FAILURE
1931 * \retval #PSA_ERROR_TAMPERING_DETECTED
1932 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
1933 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03001934 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001935 * The library has not been previously initialized by psa_crypto_init().
1936 * It is implementation-dependent whether a failure to initialize
1937 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001938 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001939psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001940 psa_algorithm_t alg,
1941 const uint8_t *input,
1942 size_t input_length,
1943 const uint8_t *salt,
1944 size_t salt_length,
1945 uint8_t *output,
1946 size_t output_size,
1947 size_t *output_length);
1948
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001949/**@}*/
1950
Gilles Peskineedd76872018-07-20 17:42:05 +02001951/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02001952 * @{
1953 */
1954
1955/** The type of the state data structure for generators.
1956 *
1957 * Before calling any function on a generator, the application must
1958 * initialize it by any of the following means:
1959 * - Set the structure to all-bits-zero, for example:
1960 * \code
1961 * psa_crypto_generator_t generator;
1962 * memset(&generator, 0, sizeof(generator));
1963 * \endcode
1964 * - Initialize the structure to logical zero values, for example:
1965 * \code
1966 * psa_crypto_generator_t generator = {0};
1967 * \endcode
1968 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
1969 * for example:
1970 * \code
1971 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1972 * \endcode
1973 * - Assign the result of the function psa_crypto_generator_init()
1974 * to the structure, for example:
1975 * \code
1976 * psa_crypto_generator_t generator;
1977 * generator = psa_crypto_generator_init();
1978 * \endcode
1979 *
1980 * This is an implementation-defined \c struct. Applications should not
1981 * make any assumptions about the content of this structure except
1982 * as directed by the documentation of a specific implementation.
1983 */
1984typedef struct psa_crypto_generator_s psa_crypto_generator_t;
1985
1986/** \def PSA_CRYPTO_GENERATOR_INIT
1987 *
1988 * This macro returns a suitable initializer for a generator object
1989 * of type #psa_crypto_generator_t.
1990 */
1991#ifdef __DOXYGEN_ONLY__
1992/* This is an example definition for documentation purposes.
1993 * Implementations should define a suitable value in `crypto_struct.h`.
1994 */
1995#define PSA_CRYPTO_GENERATOR_INIT {0}
1996#endif
1997
1998/** Return an initial value for a generator object.
1999 */
2000static psa_crypto_generator_t psa_crypto_generator_init(void);
2001
2002/** Retrieve the current capacity of a generator.
2003 *
2004 * The capacity of a generator is the maximum number of bytes that it can
2005 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
2006 *
2007 * \param[in] generator The generator to query.
2008 * \param[out] capacity On success, the capacity of the generator.
2009 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002010 * \retval #PSA_SUCCESS
2011 * \retval #PSA_ERROR_BAD_STATE
2012 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002013 */
2014psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
2015 size_t *capacity);
2016
2017/** Read some data from a generator.
2018 *
2019 * This function reads and returns a sequence of bytes from a generator.
2020 * The data that is read is discarded from the generator. The generator's
2021 * capacity is decreased by the number of bytes read.
2022 *
2023 * \param[in,out] generator The generator object to read from.
2024 * \param[out] output Buffer where the generator output will be
2025 * written.
2026 * \param output_length Number of bytes to output.
2027 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002028 * \retval #PSA_SUCCESS
2029 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02002030 * There were fewer than \p output_length bytes
2031 * in the generator. Note that in this case, no
2032 * output is written to the output buffer.
2033 * The generator's capacity is set to 0, thus
2034 * subsequent calls to this function will not
2035 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002036 * \retval #PSA_ERROR_BAD_STATE
2037 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2038 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2039 * \retval #PSA_ERROR_HARDWARE_FAILURE
2040 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002041 */
2042psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
2043 uint8_t *output,
2044 size_t output_length);
2045
2046/** Create a symmetric key from data read from a generator.
2047 *
2048 * This function reads a sequence of bytes from a generator and imports
2049 * these bytes as a key.
2050 * The data that is read is discarded from the generator. The generator's
2051 * capacity is decreased by the number of bytes read.
2052 *
2053 * This function is equivalent to calling #psa_generator_read and
2054 * passing the resulting output to #psa_import_key, but
2055 * if the implementation provides an isolation boundary then
2056 * the key material is not exposed outside the isolation boundary.
2057 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002058 * \param handle Handle to the slot where the key will be stored.
2059 * This must be a valid slot for a key of the chosen
2060 * type: it must have been obtained by calling
2061 * psa_allocate_key() or psa_create_key() with the
2062 * correct \p type and with a maximum size that is
2063 * compatible with \p bits.
2064 * It must not contain any key material yet.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002065 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2066 * This must be a symmetric key type.
2067 * \param bits Key size in bits.
2068 * \param[in,out] generator The generator object to read from.
2069 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002070 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02002071 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01002072 * If the key is persistent, the key material and the key's metadata
2073 * have been saved to persistent storage.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002074 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02002075 * There were fewer than \p output_length bytes
2076 * in the generator. Note that in this case, no
2077 * output is written to the output buffer.
2078 * The generator's capacity is set to 0, thus
2079 * subsequent calls to this function will not
2080 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002081 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002082 * The key type or key size is not supported, either by the
2083 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002084 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineae32aac2018-11-30 14:39:32 +01002085 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002086 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskineeab56e42018-07-12 17:12:33 +02002087 * There is already a key in the specified slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002088 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2089 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
2090 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2091 * \retval #PSA_ERROR_HARDWARE_FAILURE
2092 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002093 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002094 * The library has not been previously initialized by psa_crypto_init().
2095 * It is implementation-dependent whether a failure to initialize
2096 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002097 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002098psa_status_t psa_generator_import_key(psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002099 psa_key_type_t type,
2100 size_t bits,
2101 psa_crypto_generator_t *generator);
2102
2103/** Abort a generator.
2104 *
2105 * Once a generator has been aborted, its capacity is zero.
2106 * Aborting a generator frees all associated resources except for the
2107 * \c generator structure itself.
2108 *
2109 * This function may be called at any time as long as the generator
2110 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
2111 * psa_crypto_generator_init() or a zero value. In particular, it is valid
2112 * to call psa_generator_abort() twice, or to call psa_generator_abort()
2113 * on a generator that has not been set up.
2114 *
2115 * Once aborted, the generator object may be called.
2116 *
2117 * \param[in,out] generator The generator to abort.
2118 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002119 * \retval #PSA_SUCCESS
2120 * \retval #PSA_ERROR_BAD_STATE
2121 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2122 * \retval #PSA_ERROR_HARDWARE_FAILURE
2123 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002124 */
2125psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
2126
Gilles Peskine8feb3a82018-09-18 12:06:11 +02002127/** Use the maximum possible capacity for a generator.
2128 *
2129 * Use this value as the capacity argument when setting up a generator
2130 * to indicate that the generator should have the maximum possible capacity.
2131 * The value of the maximum possible capacity depends on the generator
2132 * algorithm.
2133 */
2134#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
2135
Gilles Peskineeab56e42018-07-12 17:12:33 +02002136/**@}*/
2137
Gilles Peskineea0fb492018-07-12 17:17:20 +02002138/** \defgroup derivation Key derivation
2139 * @{
2140 */
2141
2142/** Set up a key derivation operation.
2143 *
2144 * A key derivation algorithm takes three inputs: a secret input \p key and
2145 * two non-secret inputs \p label and p salt.
2146 * The result of this function is a byte generator which can
2147 * be used to produce keys and other cryptographic material.
2148 *
2149 * The role of \p label and \p salt is as follows:
Gilles Peskinebef7f142018-07-12 17:22:21 +02002150 * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step
2151 * and \p label is the info string used in the "expand" step.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002152 *
Jaeden Amero9e919c62019-01-07 15:41:50 +00002153 * \param[in,out] generator The generator object to set up. It must have
2154 * been initialized as per the documentation for
2155 * #psa_crypto_generator_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002156 * \param handle Handle to the secret key.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002157 * \param alg The key derivation algorithm to compute
2158 * (\c PSA_ALG_XXX value such that
2159 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2160 * \param[in] salt Salt to use.
2161 * \param salt_length Size of the \p salt buffer in bytes.
2162 * \param[in] label Label to use.
2163 * \param label_length Size of the \p label buffer in bytes.
2164 * \param capacity The maximum number of bytes that the
2165 * generator will be able to provide.
2166 *
2167 * \retval #PSA_SUCCESS
2168 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002169 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineea0fb492018-07-12 17:17:20 +02002170 * \retval #PSA_ERROR_EMPTY_SLOT
2171 * \retval #PSA_ERROR_NOT_PERMITTED
2172 * \retval #PSA_ERROR_INVALID_ARGUMENT
2173 * \c key is not compatible with \c alg,
2174 * or \p capacity is too large for the specified algorithm and key.
2175 * \retval #PSA_ERROR_NOT_SUPPORTED
2176 * \c alg is not supported or is not a key derivation algorithm.
2177 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2178 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2179 * \retval #PSA_ERROR_HARDWARE_FAILURE
2180 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002181 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002182 * The library has not been previously initialized by psa_crypto_init().
2183 * It is implementation-dependent whether a failure to initialize
2184 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002185 */
2186psa_status_t psa_key_derivation(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002187 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02002188 psa_algorithm_t alg,
2189 const uint8_t *salt,
2190 size_t salt_length,
2191 const uint8_t *label,
2192 size_t label_length,
2193 size_t capacity);
2194
Gilles Peskine01d718c2018-09-18 12:01:02 +02002195/** Set up a key agreement operation.
2196 *
2197 * A key agreement algorithm takes two inputs: a private key \p private_key
2198 * a public key \p peer_key.
2199 * The result of this function is a byte generator which can
2200 * be used to produce keys and other cryptographic material.
2201 *
Gilles Peskine211a4362018-10-25 22:22:31 +02002202 * The resulting generator always has the maximum capacity permitted by
2203 * the algorithm.
2204 *
Jaeden Amero8afbff82019-01-14 16:56:20 +00002205 * \param[in,out] generator The generator object to set up. It must have been
2206 * initialized as per the documentation for
2207 * #psa_crypto_generator_t and not yet in use.
2208 * \param private_key Handle to the private key to use.
2209 * \param[in] peer_key Public key of the peer. The peer key must be in the
2210 * same format that psa_import_key() accepts for the
2211 * public key type corresponding to the type of
2212 * private_key. That is, this function performs the
2213 * equivalent of
2214 * `psa_import_key(internal_public_key_handle,
2215 * PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(private_key_type),
2216 * peer_key, peer_key_length)` where
2217 * `private_key_type` is the type of `private_key`.
2218 * For example, for EC keys, this means that peer_key
2219 * is interpreted as a point on the curve that the
2220 * private key is on. The standard formats for public
2221 * keys are documented in the documentation of
2222 * psa_export_public_key().
2223 * \param peer_key_length Size of \p peer_key in bytes.
2224 * \param alg The key agreement algorithm to compute
2225 * (\c PSA_ALG_XXX value such that
2226 * #PSA_ALG_IS_KEY_AGREEMENT(\p alg) is true).
Gilles Peskine01d718c2018-09-18 12:01:02 +02002227 *
2228 * \retval #PSA_SUCCESS
2229 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002230 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine01d718c2018-09-18 12:01:02 +02002231 * \retval #PSA_ERROR_EMPTY_SLOT
2232 * \retval #PSA_ERROR_NOT_PERMITTED
2233 * \retval #PSA_ERROR_INVALID_ARGUMENT
2234 * \c private_key is not compatible with \c alg,
2235 * or \p peer_key is not valid for \c alg or not compatible with
2236 * \c private_key.
2237 * \retval #PSA_ERROR_NOT_SUPPORTED
2238 * \c alg is not supported or is not a key derivation algorithm.
2239 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2240 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2241 * \retval #PSA_ERROR_HARDWARE_FAILURE
2242 * \retval #PSA_ERROR_TAMPERING_DETECTED
2243 */
2244psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002245 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02002246 const uint8_t *peer_key,
2247 size_t peer_key_length,
2248 psa_algorithm_t alg);
2249
Gilles Peskineea0fb492018-07-12 17:17:20 +02002250/**@}*/
2251
Gilles Peskineedd76872018-07-20 17:42:05 +02002252/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002253 * @{
2254 */
2255
2256/**
2257 * \brief Generate random bytes.
2258 *
2259 * \warning This function **can** fail! Callers MUST check the return status
2260 * and MUST NOT use the content of the output buffer if the return
2261 * status is not #PSA_SUCCESS.
2262 *
2263 * \note To generate a key, use psa_generate_key() instead.
2264 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002265 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002266 * \param output_size Number of bytes to generate and output.
2267 *
Gilles Peskine28538492018-07-11 17:34:00 +02002268 * \retval #PSA_SUCCESS
2269 * \retval #PSA_ERROR_NOT_SUPPORTED
2270 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2271 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2272 * \retval #PSA_ERROR_HARDWARE_FAILURE
2273 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03002274 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002275 * The library has not been previously initialized by psa_crypto_init().
2276 * It is implementation-dependent whether a failure to initialize
2277 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002278 */
2279psa_status_t psa_generate_random(uint8_t *output,
2280 size_t output_size);
2281
Gilles Peskine4c317f42018-07-12 01:24:09 +02002282/** Extra parameters for RSA key generation.
2283 *
Gilles Peskinebe42f312018-07-13 14:38:15 +02002284 * You may pass a pointer to a structure of this type as the \c extra
Gilles Peskine4c317f42018-07-12 01:24:09 +02002285 * parameter to psa_generate_key().
2286 */
2287typedef struct {
Gilles Peskineedd76872018-07-20 17:42:05 +02002288 uint32_t e; /**< Public exponent value. Default: 65537. */
Gilles Peskine4c317f42018-07-12 01:24:09 +02002289} psa_generate_key_extra_rsa;
2290
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002291/**
2292 * \brief Generate a key or key pair.
2293 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002294 * \param handle Handle to the slot where the key will be stored.
2295 * This must be a valid slot for a key of the chosen
2296 * type: it must have been obtained by calling
2297 * psa_allocate_key() or psa_create_key() with the
2298 * correct \p type and with a maximum size that is
2299 * compatible with \p bits.
2300 * It must not contain any key material yet.
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002301 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2302 * \param bits Key size in bits.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002303 * \param[in] extra Extra parameters for key generation. The
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002304 * interpretation of this parameter depends on
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002305 * \p type. All types support \c NULL to use
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002306 * default parameters. Implementation that support
2307 * the generation of vendor-specific key types
2308 * that allow extra parameters shall document
2309 * the format of these extra parameters and
2310 * the default values. For standard parameters,
2311 * the meaning of \p extra is as follows:
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002312 * - For a symmetric key type (a type such
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002313 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
2314 * false), \p extra must be \c NULL.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002315 * - For an elliptic curve key type (a type
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002316 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
2317 * false), \p extra must be \c NULL.
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002318 * - For an RSA key (\p type is
2319 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
2320 * optional #psa_generate_key_extra_rsa structure
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002321 * specifying the public exponent. The
2322 * default public exponent used when \p extra
2323 * is \c NULL is 65537.
Jaeden Amero1308fb52019-01-11 13:50:43 +00002324 * - For an DSA key (\p type is
2325 * #PSA_KEY_TYPE_DSA_KEYPAIR), \p extra is an
2326 * optional structure specifying the key domain
2327 * parameters. The key domain parameters can also be
2328 * provided by psa_set_key_domain_parameters(),
2329 * which documents the format of the structure.
Jaeden Amero8851c402019-01-11 14:20:03 +00002330 * - For a DH key (\p type is
2331 * #PSA_KEY_TYPE_DH_KEYPAIR), the \p extra is an
2332 * optional structure specifying the key domain
2333 * parameters. The key domain parameters can also be
2334 * provided by psa_set_key_domain_parameters(),
2335 * which documents the format of the structure.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002336 * \param extra_size Size of the buffer that \p extra
2337 * points to, in bytes. Note that if \p extra is
2338 * \c NULL then \p extra_size must be zero.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002339 *
Gilles Peskine28538492018-07-11 17:34:00 +02002340 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01002341 * Success.
2342 * If the key is persistent, the key material and the key's metadata
2343 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002344 * \retval #PSA_ERROR_INVALID_HANDLE
2345 * \retval #PSA_ERROR_OCCUPIED_SLOT
2346 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +02002347 * \retval #PSA_ERROR_NOT_SUPPORTED
2348 * \retval #PSA_ERROR_INVALID_ARGUMENT
2349 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2350 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2351 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2352 * \retval #PSA_ERROR_HARDWARE_FAILURE
2353 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002354 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002355 * The library has not been previously initialized by psa_crypto_init().
2356 * It is implementation-dependent whether a failure to initialize
2357 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002358 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002359psa_status_t psa_generate_key(psa_key_handle_t handle,
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002360 psa_key_type_t type,
2361 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02002362 const void *extra,
2363 size_t extra_size);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002364
2365/**@}*/
2366
Gilles Peskinee59236f2018-01-27 23:32:46 +01002367#ifdef __cplusplus
2368}
2369#endif
2370
Gilles Peskine0cad07c2018-06-27 19:49:02 +02002371/* The file "crypto_sizes.h" contains definitions for size calculation
2372 * macros whose definitions are implementation-specific. */
2373#include "crypto_sizes.h"
2374
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002375/* The file "crypto_struct.h" contains definitions for
2376 * implementation-specific structs that are declared above. */
2377#include "crypto_struct.h"
2378
2379/* The file "crypto_extra.h" contains vendor-specific definitions. This
2380 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01002381#include "crypto_extra.h"
2382
2383#endif /* PSA_CRYPTO_H */