blob: 8fee6505ed1a51c74d8dc73c81c28aa5d849b518 [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 *
Gilles Peskine3a74e002019-01-18 17:11:25 +0100406 * \param handle Handle to the slot where the key will be stored.
407 * This must be a valid slot for a key of the chosen
408 * type: it must have been obtained by calling
409 * psa_allocate_key() or psa_create_key() with the
410 * correct \p type and with a maximum size that is
411 * compatible with \p data. It must not contain
412 * key material yet.
413 * \param type Key type (a \c PSA_KEY_TYPE_XXX value). When
414 * subsequently creating key material into \p handle,
415 * the type must be compatible.
Jaeden Amero283dfd12019-01-11 12:06:22 +0000416 * \param[in] data Buffer containing the key domain parameters. The content
417 * of this buffer is interpreted according to \p type. of
418 * psa_export_key() or psa_export_public_key() for the
419 * chosen type.
420 * \param data_length Size of the \p data buffer in bytes.
421 *
422 * \retval #PSA_SUCCESS
423 * \retval #PSA_ERROR_INVALID_HANDLE
424 * \retval #PSA_ERROR_OCCUPIED_SLOT
425 * There is already a key in the specified slot.
426 * \retval #PSA_ERROR_INVALID_ARGUMENT
427 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
428 * \retval #PSA_ERROR_HARDWARE_FAILURE
429 * \retval #PSA_ERROR_TAMPERING_DETECTED
430 * \retval #PSA_ERROR_BAD_STATE
431 * The library has not been previously initialized by psa_crypto_init().
432 * It is implementation-dependent whether a failure to initialize
433 * results in this error code.
434 */
435psa_status_t psa_set_key_domain_parameters(psa_key_handle_t handle,
Gilles Peskine3a74e002019-01-18 17:11:25 +0100436 psa_key_type_t type,
Jaeden Amero283dfd12019-01-11 12:06:22 +0000437 const uint8_t *data,
438 size_t data_length);
439
440/**
441 * \brief Get domain parameters for a key.
442 *
443 * Get the domain parameters for a key with this function, if any. The format
444 * of the domain parameters written to \p data is specified in the
445 * documentation for psa_set_key_domain_parameters().
446 *
447 * \param handle Handle to the key to get domain parameters from.
448 * \param[out] data On success, the key domain parameters.
449 * \param data_size Size of the \p data buffer in bytes.
450 * \param[out] data_length On success, the number of bytes
451 * that make up the key domain parameters data.
452 *
453 * \retval #PSA_SUCCESS
454 * \retval #PSA_ERROR_INVALID_HANDLE
455 * \retval #PSA_ERROR_EMPTY_SLOT
456 * There is no key in the specified slot.
457 * \retval #PSA_ERROR_INVALID_ARGUMENT
458 * \retval #PSA_ERROR_NOT_SUPPORTED
459 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
460 * \retval #PSA_ERROR_HARDWARE_FAILURE
461 * \retval #PSA_ERROR_TAMPERING_DETECTED
462 * \retval #PSA_ERROR_BAD_STATE
463 * The library has not been previously initialized by psa_crypto_init().
464 * It is implementation-dependent whether a failure to initialize
465 * results in this error code.
466 */
467psa_status_t psa_get_key_domain_parameters(psa_key_handle_t handle,
468 uint8_t *data,
469 size_t data_size,
470 size_t *data_length);
471
472/**
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100473 * \brief Export a key in binary format.
474 *
475 * The output of this function can be passed to psa_import_key() to
476 * create an equivalent object.
477 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100478 * If the implementation of psa_import_key() supports other formats
479 * beyond the format specified here, the output from psa_export_key()
480 * must use the representation specified here, not the original
481 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100482 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100483 * For standard key types, the output format is as follows:
484 *
485 * - For symmetric keys (including MAC keys), the format is the
486 * raw bytes of the key.
487 * - For DES, the key data consists of 8 bytes. The parity bits must be
488 * correct.
489 * - For Triple-DES, the format is the concatenation of the
490 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100491 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200492 * is the non-encrypted DER encoding of the representation defined by
493 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
494 * ```
495 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200496 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200497 * modulus INTEGER, -- n
498 * publicExponent INTEGER, -- e
499 * privateExponent INTEGER, -- d
500 * prime1 INTEGER, -- p
501 * prime2 INTEGER, -- q
502 * exponent1 INTEGER, -- d mod (p-1)
503 * exponent2 INTEGER, -- d mod (q-1)
504 * coefficient INTEGER, -- (inverse of q) mod p
505 * }
506 * ```
Jaeden Amero1308fb52019-01-11 13:50:43 +0000507 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format is the
508 * representation of the private key `x` as a big-endian byte string. The
509 * length of the byte string is the private key size in bytes (leading zeroes
510 * are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200511 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +0100512 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100513 * a representation of the private value as a `ceiling(m/8)`-byte string
514 * where `m` is the bit size associated with the curve, i.e. the bit size
515 * of the order of the curve's coordinate field. This byte string is
516 * in little-endian order for Montgomery curves (curve types
517 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
518 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
519 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100520 * This is the content of the `privateKey` field of the `ECPrivateKey`
521 * format defined by RFC 5915.
Jaeden Amero8851c402019-01-11 14:20:03 +0000522 * - For Diffie-Hellman key exchange key pairs (#PSA_KEY_TYPE_DH_KEYPAIR), the
523 * format is the representation of the private key `x` as a big-endian byte
524 * string. The length of the byte string is the private key size in bytes
525 * (leading zeroes are not stripped).
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200526 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
527 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100528 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100529 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200530 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200531 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200532 * \param[out] data_length On success, the number of bytes
533 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100534 *
Gilles Peskine28538492018-07-11 17:34:00 +0200535 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100536 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200537 * \retval #PSA_ERROR_EMPTY_SLOT
538 * \retval #PSA_ERROR_NOT_PERMITTED
Darryl Green9e2d7a02018-07-24 16:33:30 +0100539 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200540 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
541 * The size of the \p data buffer is too small. You can determine a
542 * sufficient buffer size by calling
543 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
544 * where \c type is the key type
545 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200546 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
547 * \retval #PSA_ERROR_HARDWARE_FAILURE
548 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300549 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300550 * The library has not been previously initialized by psa_crypto_init().
551 * It is implementation-dependent whether a failure to initialize
552 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100553 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100554psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100555 uint8_t *data,
556 size_t data_size,
557 size_t *data_length);
558
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100559/**
560 * \brief Export a public key or the public part of a key pair in binary format.
561 *
562 * The output of this function can be passed to psa_import_key() to
563 * create an object that is equivalent to the public key.
564 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000565 * This specification supports a single format for each key type.
566 * Implementations may support other formats as long as the standard
567 * format is supported. Implementations that support other formats
568 * should ensure that the formats are clearly unambiguous so as to
569 * minimize the risk that an invalid input is accidentally interpreted
570 * according to a different format.
571 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000572 * For standard key types, the output format is as follows:
573 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
574 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
575 * ```
576 * RSAPublicKey ::= SEQUENCE {
577 * modulus INTEGER, -- n
578 * publicExponent INTEGER } -- e
579 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000580 * - For elliptic curve public keys (key types for which
581 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
582 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
583 * Let `m` be the bit size associated with the curve, i.e. the bit size of
584 * `q` for a curve over `F_q`. The representation consists of:
585 * - The byte 0x04;
586 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
587 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Jaeden Amero1308fb52019-01-11 13:50:43 +0000588 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY), the format is the
589 * representation of the public key `y = g^x mod p` as a big-endian byte
590 * string. The length of the byte string is the length of the base prime `p`
591 * in bytes.
Jaeden Amero8851c402019-01-11 14:20:03 +0000592 * - For Diffie-Hellman key exchange public keys (#PSA_KEY_TYPE_DH_PUBLIC_KEY),
593 * the format is the representation of the public key `y = g^x mod p` as a
594 * big-endian byte string. The length of the byte string is the length of the
595 * base prime `p` in bytes.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100596 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100597 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200598 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200599 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200600 * \param[out] data_length On success, the number of bytes
601 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100602 *
Gilles Peskine28538492018-07-11 17:34:00 +0200603 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100604 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200605 * \retval #PSA_ERROR_EMPTY_SLOT
606 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200607 * The key is neither a public key nor a key pair.
608 * \retval #PSA_ERROR_NOT_SUPPORTED
609 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
610 * The size of the \p data buffer is too small. You can determine a
611 * sufficient buffer size by calling
612 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
613 * where \c type is the key type
614 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200615 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
616 * \retval #PSA_ERROR_HARDWARE_FAILURE
617 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300618 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300619 * The library has not been previously initialized by psa_crypto_init().
620 * It is implementation-dependent whether a failure to initialize
621 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100622 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100623psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100624 uint8_t *data,
625 size_t data_size,
626 size_t *data_length);
627
628/**@}*/
629
630/** \defgroup policy Key policies
631 * @{
632 */
633
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100634/** The type of the key policy data structure.
635 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000636 * Before calling any function on a key policy, the application must initialize
637 * it by any of the following means:
638 * - Set the structure to all-bits-zero, for example:
639 * \code
640 * psa_key_policy_t policy;
641 * memset(&policy, 0, sizeof(policy));
642 * \endcode
643 * - Initialize the structure to logical zero values, for example:
644 * \code
645 * psa_key_policy_t policy = {0};
646 * \endcode
647 * - Initialize the structure to the initializer #PSA_KEY_POLICY_INIT,
648 * for example:
649 * \code
650 * psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
651 * \endcode
652 * - Assign the result of the function psa_key_policy_init()
653 * to the structure, for example:
654 * \code
655 * psa_key_policy_t policy;
656 * policy = psa_key_policy_init();
657 * \endcode
658 *
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100659 * This is an implementation-defined \c struct. Applications should not
660 * make any assumptions about the content of this structure except
661 * as directed by the documentation of a specific implementation. */
662typedef struct psa_key_policy_s psa_key_policy_t;
663
Jaeden Amero70261c52019-01-04 11:47:20 +0000664/** \def PSA_KEY_POLICY_INIT
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200665 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000666 * This macro returns a suitable initializer for a key policy object of type
667 * #psa_key_policy_t.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200668 */
Jaeden Amero70261c52019-01-04 11:47:20 +0000669#ifdef __DOXYGEN_ONLY__
670/* This is an example definition for documentation purposes.
671 * Implementations should define a suitable value in `crypto_struct.h`.
672 */
673#define PSA_KEY_POLICY_INIT {0}
674#endif
675
676/** Return an initial value for a key policy that forbids all usage of the key.
677 */
678static psa_key_policy_t psa_key_policy_init(void);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100679
Gilles Peskine7e198532018-03-08 07:50:30 +0100680/** \brief Set the standard fields of a policy structure.
681 *
682 * Note that this function does not make any consistency check of the
683 * parameters. The values are only checked when applying the policy to
684 * a key slot with psa_set_key_policy().
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200685 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000686 * \param[in,out] policy The key policy to modify. It must have been
687 * initialized as per the documentation for
688 * #psa_key_policy_t.
689 * \param usage The permitted uses for the key.
690 * \param alg The algorithm that the key may be used for.
Gilles Peskine7e198532018-03-08 07:50:30 +0100691 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100692void psa_key_policy_set_usage(psa_key_policy_t *policy,
693 psa_key_usage_t usage,
694 psa_algorithm_t alg);
695
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200696/** \brief Retrieve the usage field of a policy structure.
697 *
698 * \param[in] policy The policy object to query.
699 *
700 * \return The permitted uses for a key with this policy.
701 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +0200702psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100703
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200704/** \brief Retrieve the algorithm field of a policy structure.
705 *
706 * \param[in] policy The policy object to query.
707 *
708 * \return The permitted algorithm for a key with this policy.
709 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +0200710psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100711
712/** \brief Set the usage policy on a key slot.
713 *
714 * This function must be called on an empty key slot, before importing,
715 * generating or creating a key in the slot. Changing the policy of an
716 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +0100717 *
718 * Implementations may set restrictions on supported key policies
719 * depending on the key type and the key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200720 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100721 * \param handle Handle to the key whose policy is to be changed.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200722 * \param[in] policy The policy object to query.
723 *
724 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100725 * Success.
726 * If the key is persistent, it is implementation-defined whether
727 * the policy has been saved to persistent storage. Implementations
728 * may defer saving the policy until the key material is created.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100729 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200730 * \retval #PSA_ERROR_OCCUPIED_SLOT
731 * \retval #PSA_ERROR_NOT_SUPPORTED
732 * \retval #PSA_ERROR_INVALID_ARGUMENT
733 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
734 * \retval #PSA_ERROR_HARDWARE_FAILURE
735 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300736 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300737 * The library has not been previously initialized by psa_crypto_init().
738 * It is implementation-dependent whether a failure to initialize
739 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100740 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100741psa_status_t psa_set_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100742 const psa_key_policy_t *policy);
743
Gilles Peskine7e198532018-03-08 07:50:30 +0100744/** \brief Get the usage policy for a key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200745 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100746 * \param handle Handle to the key slot whose policy is being queried.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200747 * \param[out] policy On success, the key's policy.
748 *
749 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100750 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200751 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
752 * \retval #PSA_ERROR_HARDWARE_FAILURE
753 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300754 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300755 * The library has not been previously initialized by psa_crypto_init().
756 * It is implementation-dependent whether a failure to initialize
757 * results in this error code.
Gilles Peskine7e198532018-03-08 07:50:30 +0100758 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100759psa_status_t psa_get_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100760 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +0100761
762/**@}*/
763
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100764/** \defgroup hash Message digests
765 * @{
766 */
767
Gilles Peskine308b91d2018-02-08 09:47:44 +0100768/** The type of the state data structure for multipart hash operations.
769 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000770 * Before calling any function on a hash operation object, the application must
771 * initialize it by any of the following means:
772 * - Set the structure to all-bits-zero, for example:
773 * \code
774 * psa_hash_operation_t operation;
775 * memset(&operation, 0, sizeof(operation));
776 * \endcode
777 * - Initialize the structure to logical zero values, for example:
778 * \code
779 * psa_hash_operation_t operation = {0};
780 * \endcode
781 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
782 * for example:
783 * \code
784 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
785 * \endcode
786 * - Assign the result of the function psa_hash_operation_init()
787 * to the structure, for example:
788 * \code
789 * psa_hash_operation_t operation;
790 * operation = psa_hash_operation_init();
791 * \endcode
792 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100793 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100794 * make any assumptions about the content of this structure except
795 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100796typedef struct psa_hash_operation_s psa_hash_operation_t;
797
Jaeden Amero6a25b412019-01-04 11:47:44 +0000798/** \def PSA_HASH_OPERATION_INIT
799 *
800 * This macro returns a suitable initializer for a hash operation object
801 * of type #psa_hash_operation_t.
802 */
803#ifdef __DOXYGEN_ONLY__
804/* This is an example definition for documentation purposes.
805 * Implementations should define a suitable value in `crypto_struct.h`.
806 */
807#define PSA_HASH_OPERATION_INIT {0}
808#endif
809
810/** Return an initial value for a hash operation object.
811 */
812static psa_hash_operation_t psa_hash_operation_init(void);
813
Gilles Peskine308b91d2018-02-08 09:47:44 +0100814/** Start a multipart hash operation.
815 *
816 * The sequence of operations to calculate a hash (message digest)
817 * is as follows:
818 * -# Allocate an operation object which will be passed to all the functions
819 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000820 * -# Initialize the operation object with one of the methods described in the
821 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200822 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100823 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100824 * of the message each time. The hash that is calculated is the hash
825 * of the concatenation of these messages in order.
826 * -# To calculate the hash, call psa_hash_finish().
827 * To compare the hash with an expected value, call psa_hash_verify().
828 *
829 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000830 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100831 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200832 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100833 * eventually terminate the operation. The following events terminate an
834 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100835 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100836 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100837 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000838 * \param[in,out] operation The operation object to set up. It must have
839 * been initialized as per the documentation for
840 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200841 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
842 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100843 *
Gilles Peskine28538492018-07-11 17:34:00 +0200844 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100845 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200846 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200847 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +0200848 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
849 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
850 * \retval #PSA_ERROR_HARDWARE_FAILURE
851 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100852 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200853psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100854 psa_algorithm_t alg);
855
Gilles Peskine308b91d2018-02-08 09:47:44 +0100856/** Add a message fragment to a multipart hash operation.
857 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200858 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100859 *
860 * If this function returns an error status, the operation becomes inactive.
861 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200862 * \param[in,out] operation Active hash operation.
863 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200864 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100865 *
Gilles Peskine28538492018-07-11 17:34:00 +0200866 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100867 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200868 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100869 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200870 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
871 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
872 * \retval #PSA_ERROR_HARDWARE_FAILURE
873 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100874 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100875psa_status_t psa_hash_update(psa_hash_operation_t *operation,
876 const uint8_t *input,
877 size_t input_length);
878
Gilles Peskine308b91d2018-02-08 09:47:44 +0100879/** Finish the calculation of the hash of a message.
880 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200881 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100882 * This function calculates the hash of the message formed by concatenating
883 * the inputs passed to preceding calls to psa_hash_update().
884 *
885 * When this function returns, the operation becomes inactive.
886 *
887 * \warning Applications should not call this function if they expect
888 * a specific value for the hash. Call psa_hash_verify() instead.
889 * Beware that comparing integrity or authenticity data such as
890 * hash values with a function such as \c memcmp is risky
891 * because the time taken by the comparison may leak information
892 * about the hashed data which could allow an attacker to guess
893 * a valid hash and thereby bypass security controls.
894 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200895 * \param[in,out] operation Active hash operation.
896 * \param[out] hash Buffer where the hash is to be written.
897 * \param hash_size Size of the \p hash buffer in bytes.
898 * \param[out] hash_length On success, the number of bytes
899 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +0200900 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +0200901 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100902 *
Gilles Peskine28538492018-07-11 17:34:00 +0200903 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100904 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200905 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100906 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200907 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200908 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200909 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100910 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +0200911 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
912 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
913 * \retval #PSA_ERROR_HARDWARE_FAILURE
914 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100915 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100916psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
917 uint8_t *hash,
918 size_t hash_size,
919 size_t *hash_length);
920
Gilles Peskine308b91d2018-02-08 09:47:44 +0100921/** Finish the calculation of the hash of a message and compare it with
922 * an expected value.
923 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200924 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100925 * This function calculates the hash of the message formed by concatenating
926 * the inputs passed to preceding calls to psa_hash_update(). It then
927 * compares the calculated hash with the expected hash passed as a
928 * parameter to this function.
929 *
930 * When this function returns, the operation becomes inactive.
931 *
Gilles Peskine19067982018-03-20 17:54:53 +0100932 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +0100933 * comparison between the actual hash and the expected hash is performed
934 * in constant time.
935 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200936 * \param[in,out] operation Active hash operation.
937 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200938 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100939 *
Gilles Peskine28538492018-07-11 17:34:00 +0200940 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100941 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +0200942 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100943 * The hash of the message was calculated successfully, but it
944 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +0200945 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100946 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200947 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
948 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
949 * \retval #PSA_ERROR_HARDWARE_FAILURE
950 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100951 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100952psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
953 const uint8_t *hash,
954 size_t hash_length);
955
Gilles Peskine308b91d2018-02-08 09:47:44 +0100956/** Abort a hash operation.
957 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100958 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200959 * \p operation structure itself. Once aborted, the operation object
960 * can be reused for another operation by calling
961 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100962 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200963 * You may call this function any time after the operation object has
964 * been initialized by any of the following methods:
965 * - A call to psa_hash_setup(), whether it succeeds or not.
966 * - Initializing the \c struct to all-bits-zero.
967 * - Initializing the \c struct to logical zeros, e.g.
968 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100969 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200970 * In particular, calling psa_hash_abort() after the operation has been
971 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
972 * psa_hash_verify() is safe and has no effect.
973 *
974 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100975 *
Gilles Peskine28538492018-07-11 17:34:00 +0200976 * \retval #PSA_SUCCESS
977 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200978 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +0200979 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
980 * \retval #PSA_ERROR_HARDWARE_FAILURE
981 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100982 */
983psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100984
985/**@}*/
986
Gilles Peskine8c9def32018-02-08 10:02:12 +0100987/** \defgroup MAC Message authentication codes
988 * @{
989 */
990
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100991/** The type of the state data structure for multipart MAC operations.
992 *
Jaeden Amero769ce272019-01-04 11:48:03 +0000993 * Before calling any function on a MAC operation object, the application must
994 * initialize it by any of the following means:
995 * - Set the structure to all-bits-zero, for example:
996 * \code
997 * psa_mac_operation_t operation;
998 * memset(&operation, 0, sizeof(operation));
999 * \endcode
1000 * - Initialize the structure to logical zero values, for example:
1001 * \code
1002 * psa_mac_operation_t operation = {0};
1003 * \endcode
1004 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1005 * for example:
1006 * \code
1007 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1008 * \endcode
1009 * - Assign the result of the function psa_mac_operation_init()
1010 * to the structure, for example:
1011 * \code
1012 * psa_mac_operation_t operation;
1013 * operation = psa_mac_operation_init();
1014 * \endcode
1015 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001016 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001017 * make any assumptions about the content of this structure except
1018 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001019typedef struct psa_mac_operation_s psa_mac_operation_t;
1020
Jaeden Amero769ce272019-01-04 11:48:03 +00001021/** \def PSA_MAC_OPERATION_INIT
1022 *
1023 * This macro returns a suitable initializer for a MAC operation object of type
1024 * #psa_mac_operation_t.
1025 */
1026#ifdef __DOXYGEN_ONLY__
1027/* This is an example definition for documentation purposes.
1028 * Implementations should define a suitable value in `crypto_struct.h`.
1029 */
1030#define PSA_MAC_OPERATION_INIT {0}
1031#endif
1032
1033/** Return an initial value for a MAC operation object.
1034 */
1035static psa_mac_operation_t psa_mac_operation_init(void);
1036
Gilles Peskine89167cb2018-07-08 20:12:23 +02001037/** Start a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001038 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001039 * This function sets up the calculation of the MAC
1040 * (message authentication code) of a byte string.
1041 * To verify the MAC of a message against an
1042 * expected value, use psa_mac_verify_setup() instead.
1043 *
1044 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001045 * -# Allocate an operation object which will be passed to all the functions
1046 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001047 * -# Initialize the operation object with one of the methods described in the
1048 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001049 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001050 * The key remains associated with the operation even if the content
1051 * of the key slot changes.
1052 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1053 * of the message each time. The MAC that is calculated is the MAC
1054 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001055 * -# At the end of the message, call psa_mac_sign_finish() to finish
1056 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001057 *
1058 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001059 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001060 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001061 * After a successful call to psa_mac_sign_setup(), the application must
1062 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001063 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001064 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001065 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001066 * \param[in,out] operation The operation object to set up. It must have
1067 * been initialized as per the documentation for
1068 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001069 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001070 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1071 * such that #PSA_ALG_IS_MAC(alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001072 *
Gilles Peskine28538492018-07-11 17:34:00 +02001073 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001074 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001075 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001076 * \retval #PSA_ERROR_EMPTY_SLOT
1077 * \retval #PSA_ERROR_NOT_PERMITTED
1078 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001079 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001080 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001081 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001082 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1083 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1084 * \retval #PSA_ERROR_HARDWARE_FAILURE
1085 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001086 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001087 * The library has not been previously initialized by psa_crypto_init().
1088 * It is implementation-dependent whether a failure to initialize
1089 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001090 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001091psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001092 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001093 psa_algorithm_t alg);
1094
1095/** Start a multipart MAC verification operation.
1096 *
1097 * This function sets up the verification of the MAC
1098 * (message authentication code) of a byte string against an expected value.
1099 *
1100 * The sequence of operations to verify a MAC is as follows:
1101 * -# Allocate an operation object which will be passed to all the functions
1102 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001103 * -# Initialize the operation object with one of the methods described in the
1104 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001105 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
1106 * The key remains associated with the operation even if the content
1107 * of the key slot changes.
1108 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1109 * of the message each time. The MAC that is calculated is the MAC
1110 * of the concatenation of these messages in order.
1111 * -# At the end of the message, call psa_mac_verify_finish() to finish
1112 * calculating the actual MAC of the message and verify it against
1113 * the expected value.
1114 *
1115 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001116 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001117 *
1118 * After a successful call to psa_mac_verify_setup(), the application must
1119 * eventually terminate the operation through one of the following methods:
1120 * - A failed call to psa_mac_update().
1121 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1122 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001123 * \param[in,out] operation The operation object to set up. It must have
1124 * been initialized as per the documentation for
1125 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001126 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001127 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1128 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001129 *
Gilles Peskine28538492018-07-11 17:34:00 +02001130 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001131 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001132 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001133 * \retval #PSA_ERROR_EMPTY_SLOT
1134 * \retval #PSA_ERROR_NOT_PERMITTED
1135 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001136 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001137 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001138 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001139 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1140 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1141 * \retval #PSA_ERROR_HARDWARE_FAILURE
1142 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001143 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001144 * The library has not been previously initialized by psa_crypto_init().
1145 * It is implementation-dependent whether a failure to initialize
1146 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001147 */
1148psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001149 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001150 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001151
Gilles Peskinedcd14942018-07-12 00:30:52 +02001152/** Add a message fragment to a multipart MAC operation.
1153 *
1154 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1155 * before calling this function.
1156 *
1157 * If this function returns an error status, the operation becomes inactive.
1158 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001159 * \param[in,out] operation Active MAC operation.
1160 * \param[in] input Buffer containing the message fragment to add to
1161 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001162 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001163 *
1164 * \retval #PSA_SUCCESS
1165 * Success.
1166 * \retval #PSA_ERROR_BAD_STATE
1167 * The operation state is not valid (not started, or already completed).
1168 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1169 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1170 * \retval #PSA_ERROR_HARDWARE_FAILURE
1171 * \retval #PSA_ERROR_TAMPERING_DETECTED
1172 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001173psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1174 const uint8_t *input,
1175 size_t input_length);
1176
Gilles Peskinedcd14942018-07-12 00:30:52 +02001177/** Finish the calculation of the MAC of a message.
1178 *
1179 * The application must call psa_mac_sign_setup() before calling this function.
1180 * This function calculates the MAC of the message formed by concatenating
1181 * the inputs passed to preceding calls to psa_mac_update().
1182 *
1183 * When this function returns, the operation becomes inactive.
1184 *
1185 * \warning Applications should not call this function if they expect
1186 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1187 * Beware that comparing integrity or authenticity data such as
1188 * MAC values with a function such as \c memcmp is risky
1189 * because the time taken by the comparison may leak information
1190 * about the MAC value which could allow an attacker to guess
1191 * a valid MAC and thereby bypass security controls.
1192 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001193 * \param[in,out] operation Active MAC operation.
1194 * \param[out] mac Buffer where the MAC value is to be written.
1195 * \param mac_size Size of the \p mac buffer in bytes.
1196 * \param[out] mac_length On success, the number of bytes
1197 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001198 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001199 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001200 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001201 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001202 *
1203 * \retval #PSA_SUCCESS
1204 * Success.
1205 * \retval #PSA_ERROR_BAD_STATE
1206 * The operation state is not valid (not started, or already completed).
1207 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001208 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001209 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1210 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1211 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1212 * \retval #PSA_ERROR_HARDWARE_FAILURE
1213 * \retval #PSA_ERROR_TAMPERING_DETECTED
1214 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001215psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1216 uint8_t *mac,
1217 size_t mac_size,
1218 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001219
Gilles Peskinedcd14942018-07-12 00:30:52 +02001220/** Finish the calculation of the MAC of a message and compare it with
1221 * an expected value.
1222 *
1223 * The application must call psa_mac_verify_setup() before calling this function.
1224 * This function calculates the MAC of the message formed by concatenating
1225 * the inputs passed to preceding calls to psa_mac_update(). It then
1226 * compares the calculated MAC with the expected MAC passed as a
1227 * parameter to this function.
1228 *
1229 * When this function returns, the operation becomes inactive.
1230 *
1231 * \note Implementations shall make the best effort to ensure that the
1232 * comparison between the actual MAC and the expected MAC is performed
1233 * in constant time.
1234 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001235 * \param[in,out] operation Active MAC operation.
1236 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001237 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001238 *
1239 * \retval #PSA_SUCCESS
1240 * The expected MAC is identical to the actual MAC of the message.
1241 * \retval #PSA_ERROR_INVALID_SIGNATURE
1242 * The MAC of the message was calculated successfully, but it
1243 * differs from the expected MAC.
1244 * \retval #PSA_ERROR_BAD_STATE
1245 * The operation state is not valid (not started, or already completed).
1246 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1247 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1248 * \retval #PSA_ERROR_HARDWARE_FAILURE
1249 * \retval #PSA_ERROR_TAMPERING_DETECTED
1250 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001251psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1252 const uint8_t *mac,
1253 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001254
Gilles Peskinedcd14942018-07-12 00:30:52 +02001255/** Abort a MAC operation.
1256 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001257 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001258 * \p operation structure itself. Once aborted, the operation object
1259 * can be reused for another operation by calling
1260 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001261 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001262 * You may call this function any time after the operation object has
1263 * been initialized by any of the following methods:
1264 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1265 * it succeeds or not.
1266 * - Initializing the \c struct to all-bits-zero.
1267 * - Initializing the \c struct to logical zeros, e.g.
1268 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001269 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001270 * In particular, calling psa_mac_abort() after the operation has been
1271 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1272 * psa_mac_verify_finish() is safe and has no effect.
1273 *
1274 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001275 *
1276 * \retval #PSA_SUCCESS
1277 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001278 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001279 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1280 * \retval #PSA_ERROR_HARDWARE_FAILURE
1281 * \retval #PSA_ERROR_TAMPERING_DETECTED
1282 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001283psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1284
1285/**@}*/
1286
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001287/** \defgroup cipher Symmetric ciphers
1288 * @{
1289 */
1290
1291/** The type of the state data structure for multipart cipher operations.
1292 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001293 * Before calling any function on a cipher operation object, the application
1294 * must initialize it by any of the following means:
1295 * - Set the structure to all-bits-zero, for example:
1296 * \code
1297 * psa_cipher_operation_t operation;
1298 * memset(&operation, 0, sizeof(operation));
1299 * \endcode
1300 * - Initialize the structure to logical zero values, for example:
1301 * \code
1302 * psa_cipher_operation_t operation = {0};
1303 * \endcode
1304 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1305 * for example:
1306 * \code
1307 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1308 * \endcode
1309 * - Assign the result of the function psa_cipher_operation_init()
1310 * to the structure, for example:
1311 * \code
1312 * psa_cipher_operation_t operation;
1313 * operation = psa_cipher_operation_init();
1314 * \endcode
1315 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001316 * This is an implementation-defined \c struct. Applications should not
1317 * make any assumptions about the content of this structure except
1318 * as directed by the documentation of a specific implementation. */
1319typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1320
Jaeden Amero5bae2272019-01-04 11:48:27 +00001321/** \def PSA_CIPHER_OPERATION_INIT
1322 *
1323 * This macro returns a suitable initializer for a cipher operation object of
1324 * type #psa_cipher_operation_t.
1325 */
1326#ifdef __DOXYGEN_ONLY__
1327/* This is an example definition for documentation purposes.
1328 * Implementations should define a suitable value in `crypto_struct.h`.
1329 */
1330#define PSA_CIPHER_OPERATION_INIT {0}
1331#endif
1332
1333/** Return an initial value for a cipher operation object.
1334 */
1335static psa_cipher_operation_t psa_cipher_operation_init(void);
1336
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001337/** Set the key for a multipart symmetric encryption operation.
1338 *
1339 * The sequence of operations to encrypt a message with a symmetric cipher
1340 * is as follows:
1341 * -# Allocate an operation object which will be passed to all the functions
1342 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001343 * -# Initialize the operation object with one of the methods described in the
1344 * documentation for #psa_cipher_operation_t, e.g.
1345 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001346 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001347 * The key remains associated with the operation even if the content
1348 * of the key slot changes.
itayzafrired7382f2018-08-02 14:19:33 +03001349 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001350 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001351 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001352 * requires a specific IV value.
1353 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1354 * of the message each time.
1355 * -# Call psa_cipher_finish().
1356 *
1357 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001358 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001359 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001360 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001361 * eventually terminate the operation. The following events terminate an
1362 * operation:
itayzafrired7382f2018-08-02 14:19:33 +03001363 * - A failed call to psa_cipher_generate_iv(), psa_cipher_set_iv()
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001364 * or psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001365 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001366 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001367 * \param[in,out] operation The operation object to set up. It must have
1368 * been initialized as per the documentation for
1369 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001370 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001371 * \param alg The cipher algorithm to compute
1372 * (\c PSA_ALG_XXX value such that
1373 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001374 *
Gilles Peskine28538492018-07-11 17:34:00 +02001375 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001376 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001377 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001378 * \retval #PSA_ERROR_EMPTY_SLOT
1379 * \retval #PSA_ERROR_NOT_PERMITTED
1380 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001381 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001382 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001383 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001384 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1385 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1386 * \retval #PSA_ERROR_HARDWARE_FAILURE
1387 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001388 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001389 * The library has not been previously initialized by psa_crypto_init().
1390 * It is implementation-dependent whether a failure to initialize
1391 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001392 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001393psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001394 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001395 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001396
1397/** Set the key for a multipart symmetric decryption operation.
1398 *
1399 * The sequence of operations to decrypt a message with a symmetric cipher
1400 * is as follows:
1401 * -# Allocate an operation object which will be passed to all the functions
1402 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001403 * -# Initialize the operation object with one of the methods described in the
1404 * documentation for #psa_cipher_operation_t, e.g.
1405 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001406 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001407 * The key remains associated with the operation even if the content
1408 * of the key slot changes.
1409 * -# Call psa_cipher_update() with the IV (initialization vector) for the
1410 * decryption. If the IV is prepended to the ciphertext, you can call
1411 * psa_cipher_update() on a buffer containing the IV followed by the
1412 * beginning of the message.
1413 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1414 * of the message each time.
1415 * -# Call psa_cipher_finish().
1416 *
1417 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001418 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001419 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001420 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001421 * eventually terminate the operation. The following events terminate an
1422 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001423 * - A failed call to psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001424 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001425 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001426 * \param[in,out] operation The operation object to set up. It must have
1427 * been initialized as per the documentation for
1428 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001429 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001430 * \param alg The cipher algorithm to compute
1431 * (\c PSA_ALG_XXX value such that
1432 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001433 *
Gilles Peskine28538492018-07-11 17:34:00 +02001434 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001435 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001436 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001437 * \retval #PSA_ERROR_EMPTY_SLOT
1438 * \retval #PSA_ERROR_NOT_PERMITTED
1439 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001440 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001441 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001442 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001443 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1444 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1445 * \retval #PSA_ERROR_HARDWARE_FAILURE
1446 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001447 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001448 * The library has not been previously initialized by psa_crypto_init().
1449 * It is implementation-dependent whether a failure to initialize
1450 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001451 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001452psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001453 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001454 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001455
Gilles Peskinedcd14942018-07-12 00:30:52 +02001456/** Generate an IV for a symmetric encryption operation.
1457 *
1458 * This function generates a random IV (initialization vector), nonce
1459 * or initial counter value for the encryption operation as appropriate
1460 * for the chosen algorithm, key type and key size.
1461 *
1462 * The application must call psa_cipher_encrypt_setup() before
1463 * calling this function.
1464 *
1465 * If this function returns an error status, the operation becomes inactive.
1466 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001467 * \param[in,out] operation Active cipher operation.
1468 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001469 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001470 * \param[out] iv_length On success, the number of bytes of the
1471 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001472 *
1473 * \retval #PSA_SUCCESS
1474 * Success.
1475 * \retval #PSA_ERROR_BAD_STATE
1476 * The operation state is not valid (not started, or IV already set).
1477 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001478 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001479 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1480 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1481 * \retval #PSA_ERROR_HARDWARE_FAILURE
1482 * \retval #PSA_ERROR_TAMPERING_DETECTED
1483 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001484psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1485 unsigned char *iv,
1486 size_t iv_size,
1487 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001488
Gilles Peskinedcd14942018-07-12 00:30:52 +02001489/** Set the IV for a symmetric encryption or decryption operation.
1490 *
1491 * This function sets the random IV (initialization vector), nonce
1492 * or initial counter value for the encryption or decryption operation.
1493 *
1494 * The application must call psa_cipher_encrypt_setup() before
1495 * calling this function.
1496 *
1497 * If this function returns an error status, the operation becomes inactive.
1498 *
1499 * \note When encrypting, applications should use psa_cipher_generate_iv()
1500 * instead of this function, unless implementing a protocol that requires
1501 * a non-random IV.
1502 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001503 * \param[in,out] operation Active cipher operation.
1504 * \param[in] iv Buffer containing the IV to use.
1505 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001506 *
1507 * \retval #PSA_SUCCESS
1508 * Success.
1509 * \retval #PSA_ERROR_BAD_STATE
1510 * The operation state is not valid (not started, or IV already set).
1511 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001512 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001513 * or the chosen algorithm does not use an IV.
1514 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1515 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1516 * \retval #PSA_ERROR_HARDWARE_FAILURE
1517 * \retval #PSA_ERROR_TAMPERING_DETECTED
1518 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001519psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1520 const unsigned char *iv,
1521 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001522
Gilles Peskinedcd14942018-07-12 00:30:52 +02001523/** Encrypt or decrypt a message fragment in an active cipher operation.
1524 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001525 * Before calling this function, you must:
1526 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1527 * The choice of setup function determines whether this function
1528 * encrypts or decrypts its input.
1529 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1530 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001531 *
1532 * If this function returns an error status, the operation becomes inactive.
1533 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001534 * \param[in,out] operation Active cipher operation.
1535 * \param[in] input Buffer containing the message fragment to
1536 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001537 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001538 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001539 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001540 * \param[out] output_length On success, the number of bytes
1541 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001542 *
1543 * \retval #PSA_SUCCESS
1544 * Success.
1545 * \retval #PSA_ERROR_BAD_STATE
1546 * The operation state is not valid (not started, IV required but
1547 * not set, or already completed).
1548 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1549 * The size of the \p output buffer is too small.
1550 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1551 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1552 * \retval #PSA_ERROR_HARDWARE_FAILURE
1553 * \retval #PSA_ERROR_TAMPERING_DETECTED
1554 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001555psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1556 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001557 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001558 unsigned char *output,
1559 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001560 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001561
Gilles Peskinedcd14942018-07-12 00:30:52 +02001562/** Finish encrypting or decrypting a message in a cipher operation.
1563 *
1564 * The application must call psa_cipher_encrypt_setup() or
1565 * psa_cipher_decrypt_setup() before calling this function. The choice
1566 * of setup function determines whether this function encrypts or
1567 * decrypts its input.
1568 *
1569 * This function finishes the encryption or decryption of the message
1570 * formed by concatenating the inputs passed to preceding calls to
1571 * psa_cipher_update().
1572 *
1573 * When this function returns, the operation becomes inactive.
1574 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001575 * \param[in,out] operation Active cipher operation.
1576 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001577 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001578 * \param[out] output_length On success, the number of bytes
1579 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001580 *
1581 * \retval #PSA_SUCCESS
1582 * Success.
1583 * \retval #PSA_ERROR_BAD_STATE
1584 * The operation state is not valid (not started, IV required but
1585 * not set, or already completed).
1586 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1587 * The size of the \p output buffer is too small.
1588 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1589 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1590 * \retval #PSA_ERROR_HARDWARE_FAILURE
1591 * \retval #PSA_ERROR_TAMPERING_DETECTED
1592 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001593psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001594 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001595 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001596 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001597
Gilles Peskinedcd14942018-07-12 00:30:52 +02001598/** Abort a cipher operation.
1599 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001600 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001601 * \p operation structure itself. Once aborted, the operation object
1602 * can be reused for another operation by calling
1603 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001604 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001605 * You may call this function any time after the operation object has
1606 * been initialized by any of the following methods:
1607 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1608 * whether it succeeds or not.
1609 * - Initializing the \c struct to all-bits-zero.
1610 * - Initializing the \c struct to logical zeros, e.g.
1611 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001612 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001613 * In particular, calling psa_cipher_abort() after the operation has been
1614 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1615 * is safe and has no effect.
1616 *
1617 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001618 *
1619 * \retval #PSA_SUCCESS
1620 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001621 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001622 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1623 * \retval #PSA_ERROR_HARDWARE_FAILURE
1624 * \retval #PSA_ERROR_TAMPERING_DETECTED
1625 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001626psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1627
1628/**@}*/
1629
Gilles Peskine3b555712018-03-03 21:27:57 +01001630/** \defgroup aead Authenticated encryption with associated data (AEAD)
1631 * @{
1632 */
1633
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001634/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001635 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001636 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001637 * \param alg The AEAD algorithm to compute
1638 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001639 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001640 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001641 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001642 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001643 * but not encrypted.
1644 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001645 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001646 * encrypted.
1647 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001648 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001649 * encrypted data. The additional data is not
1650 * part of this output. For algorithms where the
1651 * encrypted data and the authentication tag
1652 * are defined as separate outputs, the
1653 * authentication tag is appended to the
1654 * encrypted data.
1655 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1656 * This must be at least
1657 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1658 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001659 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001660 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001661 *
Gilles Peskine28538492018-07-11 17:34:00 +02001662 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001663 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001664 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001665 * \retval #PSA_ERROR_EMPTY_SLOT
1666 * \retval #PSA_ERROR_NOT_PERMITTED
1667 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001668 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001669 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001670 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001671 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1672 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1673 * \retval #PSA_ERROR_HARDWARE_FAILURE
1674 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001675 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001676 * The library has not been previously initialized by psa_crypto_init().
1677 * It is implementation-dependent whether a failure to initialize
1678 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001679 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001680psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001681 psa_algorithm_t alg,
1682 const uint8_t *nonce,
1683 size_t nonce_length,
1684 const uint8_t *additional_data,
1685 size_t additional_data_length,
1686 const uint8_t *plaintext,
1687 size_t plaintext_length,
1688 uint8_t *ciphertext,
1689 size_t ciphertext_size,
1690 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001691
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001692/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001693 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001694 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001695 * \param alg The AEAD algorithm to compute
1696 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001697 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001698 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001699 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001700 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001701 * but not encrypted.
1702 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001703 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001704 * encrypted. For algorithms where the
1705 * encrypted data and the authentication tag
1706 * are defined as separate inputs, the buffer
1707 * must contain the encrypted data followed
1708 * by the authentication tag.
1709 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001710 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001711 * \param plaintext_size Size of the \p plaintext buffer in bytes.
1712 * This must be at least
1713 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
1714 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001715 * \param[out] plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03001716 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001717 *
Gilles Peskine28538492018-07-11 17:34:00 +02001718 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001719 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001720 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001721 * \retval #PSA_ERROR_EMPTY_SLOT
1722 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001723 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02001724 * \retval #PSA_ERROR_NOT_PERMITTED
1725 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001726 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001727 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001728 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001729 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1730 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1731 * \retval #PSA_ERROR_HARDWARE_FAILURE
1732 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001733 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001734 * The library has not been previously initialized by psa_crypto_init().
1735 * It is implementation-dependent whether a failure to initialize
1736 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001737 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001738psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001739 psa_algorithm_t alg,
1740 const uint8_t *nonce,
1741 size_t nonce_length,
1742 const uint8_t *additional_data,
1743 size_t additional_data_length,
1744 const uint8_t *ciphertext,
1745 size_t ciphertext_length,
1746 uint8_t *plaintext,
1747 size_t plaintext_size,
1748 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001749
1750/**@}*/
1751
Gilles Peskine20035e32018-02-03 22:44:14 +01001752/** \defgroup asymmetric Asymmetric cryptography
1753 * @{
1754 */
1755
1756/**
1757 * \brief Sign a hash or short message with a private key.
1758 *
Gilles Peskine08bac712018-06-26 16:14:46 +02001759 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001760 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02001761 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
1762 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
1763 * to determine the hash algorithm to use.
1764 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001765 * \param handle Handle to the key to use for the operation.
1766 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001767 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001768 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001769 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001770 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001771 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001772 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001773 * \param[out] signature_length On success, the number of bytes
1774 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001775 *
Gilles Peskine28538492018-07-11 17:34:00 +02001776 * \retval #PSA_SUCCESS
1777 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001778 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01001779 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001780 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001781 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001782 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001783 * \retval #PSA_ERROR_NOT_SUPPORTED
1784 * \retval #PSA_ERROR_INVALID_ARGUMENT
1785 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1786 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1787 * \retval #PSA_ERROR_HARDWARE_FAILURE
1788 * \retval #PSA_ERROR_TAMPERING_DETECTED
1789 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03001790 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001791 * The library has not been previously initialized by psa_crypto_init().
1792 * It is implementation-dependent whether a failure to initialize
1793 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01001794 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001795psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01001796 psa_algorithm_t alg,
1797 const uint8_t *hash,
1798 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01001799 uint8_t *signature,
1800 size_t signature_size,
1801 size_t *signature_length);
1802
1803/**
1804 * \brief Verify the signature a hash or short message using a public key.
1805 *
Gilles Peskine08bac712018-06-26 16:14:46 +02001806 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001807 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02001808 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
1809 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
1810 * to determine the hash algorithm to use.
1811 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001812 * \param handle Handle to the key to use for the operation.
1813 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001814 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001815 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001816 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02001817 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001818 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001819 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001820 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001821 *
Gilles Peskine28538492018-07-11 17:34:00 +02001822 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001823 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02001824 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001825 * The calculation was perfomed successfully, but the passed
1826 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02001827 * \retval #PSA_ERROR_NOT_SUPPORTED
1828 * \retval #PSA_ERROR_INVALID_ARGUMENT
1829 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1830 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1831 * \retval #PSA_ERROR_HARDWARE_FAILURE
1832 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001833 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001834 * The library has not been previously initialized by psa_crypto_init().
1835 * It is implementation-dependent whether a failure to initialize
1836 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01001837 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001838psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01001839 psa_algorithm_t alg,
1840 const uint8_t *hash,
1841 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02001842 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02001843 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01001844
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001845/**
1846 * \brief Encrypt a short message with a public key.
1847 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001848 * \param handle Handle to the key to use for the operation.
1849 * It must be a public key or an asymmetric
1850 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001851 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001852 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001853 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001854 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001855 * \param[in] salt A salt or label, if supported by the
1856 * encryption algorithm.
1857 * If the algorithm does not support a
1858 * salt, pass \c NULL.
1859 * If the algorithm supports an optional
1860 * salt and you do not want to pass a salt,
1861 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001862 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001863 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1864 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001865 * \param salt_length Size of the \p salt buffer in bytes.
1866 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001867 * \param[out] output Buffer where the encrypted message is to
1868 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001869 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001870 * \param[out] output_length On success, the number of bytes
1871 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001872 *
Gilles Peskine28538492018-07-11 17:34:00 +02001873 * \retval #PSA_SUCCESS
1874 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001875 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001876 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001877 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001878 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001879 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001880 * \retval #PSA_ERROR_NOT_SUPPORTED
1881 * \retval #PSA_ERROR_INVALID_ARGUMENT
1882 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1883 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1884 * \retval #PSA_ERROR_HARDWARE_FAILURE
1885 * \retval #PSA_ERROR_TAMPERING_DETECTED
1886 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03001887 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001888 * The library has not been previously initialized by psa_crypto_init().
1889 * It is implementation-dependent whether a failure to initialize
1890 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001891 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001892psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001893 psa_algorithm_t alg,
1894 const uint8_t *input,
1895 size_t input_length,
1896 const uint8_t *salt,
1897 size_t salt_length,
1898 uint8_t *output,
1899 size_t output_size,
1900 size_t *output_length);
1901
1902/**
1903 * \brief Decrypt a short message with a private key.
1904 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001905 * \param handle Handle to the key to use for the operation.
1906 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001907 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001908 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001909 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001910 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001911 * \param[in] salt A salt or label, if supported by the
1912 * encryption algorithm.
1913 * If the algorithm does not support a
1914 * salt, pass \c NULL.
1915 * If the algorithm supports an optional
1916 * salt and you do not want to pass a salt,
1917 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001918 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001919 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1920 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001921 * \param salt_length Size of the \p salt buffer in bytes.
1922 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001923 * \param[out] output Buffer where the decrypted message is to
1924 * be written.
1925 * \param output_size Size of the \c output buffer in bytes.
1926 * \param[out] output_length On success, the number of bytes
1927 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001928 *
Gilles Peskine28538492018-07-11 17:34:00 +02001929 * \retval #PSA_SUCCESS
1930 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001931 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001932 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001933 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001934 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001935 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001936 * \retval #PSA_ERROR_NOT_SUPPORTED
1937 * \retval #PSA_ERROR_INVALID_ARGUMENT
1938 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1939 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1940 * \retval #PSA_ERROR_HARDWARE_FAILURE
1941 * \retval #PSA_ERROR_TAMPERING_DETECTED
1942 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
1943 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03001944 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001945 * The library has not been previously initialized by psa_crypto_init().
1946 * It is implementation-dependent whether a failure to initialize
1947 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001948 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001949psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001950 psa_algorithm_t alg,
1951 const uint8_t *input,
1952 size_t input_length,
1953 const uint8_t *salt,
1954 size_t salt_length,
1955 uint8_t *output,
1956 size_t output_size,
1957 size_t *output_length);
1958
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001959/**@}*/
1960
Gilles Peskineedd76872018-07-20 17:42:05 +02001961/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02001962 * @{
1963 */
1964
1965/** The type of the state data structure for generators.
1966 *
1967 * Before calling any function on a generator, the application must
1968 * initialize it by any of the following means:
1969 * - Set the structure to all-bits-zero, for example:
1970 * \code
1971 * psa_crypto_generator_t generator;
1972 * memset(&generator, 0, sizeof(generator));
1973 * \endcode
1974 * - Initialize the structure to logical zero values, for example:
1975 * \code
1976 * psa_crypto_generator_t generator = {0};
1977 * \endcode
1978 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
1979 * for example:
1980 * \code
1981 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1982 * \endcode
1983 * - Assign the result of the function psa_crypto_generator_init()
1984 * to the structure, for example:
1985 * \code
1986 * psa_crypto_generator_t generator;
1987 * generator = psa_crypto_generator_init();
1988 * \endcode
1989 *
1990 * This is an implementation-defined \c struct. Applications should not
1991 * make any assumptions about the content of this structure except
1992 * as directed by the documentation of a specific implementation.
1993 */
1994typedef struct psa_crypto_generator_s psa_crypto_generator_t;
1995
1996/** \def PSA_CRYPTO_GENERATOR_INIT
1997 *
1998 * This macro returns a suitable initializer for a generator object
1999 * of type #psa_crypto_generator_t.
2000 */
2001#ifdef __DOXYGEN_ONLY__
2002/* This is an example definition for documentation purposes.
2003 * Implementations should define a suitable value in `crypto_struct.h`.
2004 */
2005#define PSA_CRYPTO_GENERATOR_INIT {0}
2006#endif
2007
2008/** Return an initial value for a generator object.
2009 */
2010static psa_crypto_generator_t psa_crypto_generator_init(void);
2011
2012/** Retrieve the current capacity of a generator.
2013 *
2014 * The capacity of a generator is the maximum number of bytes that it can
2015 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
2016 *
2017 * \param[in] generator The generator to query.
2018 * \param[out] capacity On success, the capacity of the generator.
2019 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002020 * \retval #PSA_SUCCESS
2021 * \retval #PSA_ERROR_BAD_STATE
2022 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002023 */
2024psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
2025 size_t *capacity);
2026
2027/** Read some data from a generator.
2028 *
2029 * This function reads and returns a sequence of bytes from a generator.
2030 * The data that is read is discarded from the generator. The generator's
2031 * capacity is decreased by the number of bytes read.
2032 *
2033 * \param[in,out] generator The generator object to read from.
2034 * \param[out] output Buffer where the generator output will be
2035 * written.
2036 * \param output_length Number of bytes to output.
2037 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002038 * \retval #PSA_SUCCESS
2039 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02002040 * There were fewer than \p output_length bytes
2041 * in the generator. Note that in this case, no
2042 * output is written to the output buffer.
2043 * The generator's capacity is set to 0, thus
2044 * subsequent calls to this function will not
2045 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002046 * \retval #PSA_ERROR_BAD_STATE
2047 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2048 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2049 * \retval #PSA_ERROR_HARDWARE_FAILURE
2050 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002051 */
2052psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
2053 uint8_t *output,
2054 size_t output_length);
2055
2056/** Create a symmetric key from data read from a generator.
2057 *
2058 * This function reads a sequence of bytes from a generator and imports
2059 * these bytes as a key.
2060 * The data that is read is discarded from the generator. The generator's
2061 * capacity is decreased by the number of bytes read.
2062 *
2063 * This function is equivalent to calling #psa_generator_read and
2064 * passing the resulting output to #psa_import_key, but
2065 * if the implementation provides an isolation boundary then
2066 * the key material is not exposed outside the isolation boundary.
2067 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002068 * \param handle Handle to the slot where the key will be stored.
2069 * This must be a valid slot for a key of the chosen
2070 * type: it must have been obtained by calling
2071 * psa_allocate_key() or psa_create_key() with the
2072 * correct \p type and with a maximum size that is
2073 * compatible with \p bits.
2074 * It must not contain any key material yet.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002075 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2076 * This must be a symmetric key type.
2077 * \param bits Key size in bits.
2078 * \param[in,out] generator The generator object to read from.
2079 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002080 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02002081 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01002082 * If the key is persistent, the key material and the key's metadata
2083 * have been saved to persistent storage.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002084 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02002085 * There were fewer than \p output_length bytes
2086 * in the generator. Note that in this case, no
2087 * output is written to the output buffer.
2088 * The generator's capacity is set to 0, thus
2089 * subsequent calls to this function will not
2090 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002091 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002092 * The key type or key size is not supported, either by the
2093 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002094 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineae32aac2018-11-30 14:39:32 +01002095 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002096 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskineeab56e42018-07-12 17:12:33 +02002097 * There is already a key in the specified slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002098 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2099 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
2100 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2101 * \retval #PSA_ERROR_HARDWARE_FAILURE
2102 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002103 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002104 * The library has not been previously initialized by psa_crypto_init().
2105 * It is implementation-dependent whether a failure to initialize
2106 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002107 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002108psa_status_t psa_generator_import_key(psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002109 psa_key_type_t type,
2110 size_t bits,
2111 psa_crypto_generator_t *generator);
2112
2113/** Abort a generator.
2114 *
2115 * Once a generator has been aborted, its capacity is zero.
2116 * Aborting a generator frees all associated resources except for the
2117 * \c generator structure itself.
2118 *
2119 * This function may be called at any time as long as the generator
2120 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
2121 * psa_crypto_generator_init() or a zero value. In particular, it is valid
2122 * to call psa_generator_abort() twice, or to call psa_generator_abort()
2123 * on a generator that has not been set up.
2124 *
2125 * Once aborted, the generator object may be called.
2126 *
2127 * \param[in,out] generator The generator to abort.
2128 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002129 * \retval #PSA_SUCCESS
2130 * \retval #PSA_ERROR_BAD_STATE
2131 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2132 * \retval #PSA_ERROR_HARDWARE_FAILURE
2133 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002134 */
2135psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
2136
Gilles Peskine8feb3a82018-09-18 12:06:11 +02002137/** Use the maximum possible capacity for a generator.
2138 *
2139 * Use this value as the capacity argument when setting up a generator
2140 * to indicate that the generator should have the maximum possible capacity.
2141 * The value of the maximum possible capacity depends on the generator
2142 * algorithm.
2143 */
2144#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
2145
Gilles Peskineeab56e42018-07-12 17:12:33 +02002146/**@}*/
2147
Gilles Peskineea0fb492018-07-12 17:17:20 +02002148/** \defgroup derivation Key derivation
2149 * @{
2150 */
2151
2152/** Set up a key derivation operation.
2153 *
2154 * A key derivation algorithm takes three inputs: a secret input \p key and
2155 * two non-secret inputs \p label and p salt.
2156 * The result of this function is a byte generator which can
2157 * be used to produce keys and other cryptographic material.
2158 *
2159 * The role of \p label and \p salt is as follows:
Gilles Peskinebef7f142018-07-12 17:22:21 +02002160 * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step
2161 * and \p label is the info string used in the "expand" step.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002162 *
Jaeden Amero9e919c62019-01-07 15:41:50 +00002163 * \param[in,out] generator The generator object to set up. It must have
2164 * been initialized as per the documentation for
2165 * #psa_crypto_generator_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002166 * \param handle Handle to the secret key.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002167 * \param alg The key derivation algorithm to compute
2168 * (\c PSA_ALG_XXX value such that
2169 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2170 * \param[in] salt Salt to use.
2171 * \param salt_length Size of the \p salt buffer in bytes.
2172 * \param[in] label Label to use.
2173 * \param label_length Size of the \p label buffer in bytes.
2174 * \param capacity The maximum number of bytes that the
2175 * generator will be able to provide.
2176 *
2177 * \retval #PSA_SUCCESS
2178 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002179 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineea0fb492018-07-12 17:17:20 +02002180 * \retval #PSA_ERROR_EMPTY_SLOT
2181 * \retval #PSA_ERROR_NOT_PERMITTED
2182 * \retval #PSA_ERROR_INVALID_ARGUMENT
2183 * \c key is not compatible with \c alg,
2184 * or \p capacity is too large for the specified algorithm and key.
2185 * \retval #PSA_ERROR_NOT_SUPPORTED
2186 * \c alg is not supported or is not a key derivation algorithm.
2187 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2188 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2189 * \retval #PSA_ERROR_HARDWARE_FAILURE
2190 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002191 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002192 * The library has not been previously initialized by psa_crypto_init().
2193 * It is implementation-dependent whether a failure to initialize
2194 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002195 */
2196psa_status_t psa_key_derivation(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002197 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02002198 psa_algorithm_t alg,
2199 const uint8_t *salt,
2200 size_t salt_length,
2201 const uint8_t *label,
2202 size_t label_length,
2203 size_t capacity);
2204
Gilles Peskine01d718c2018-09-18 12:01:02 +02002205/** Set up a key agreement operation.
2206 *
2207 * A key agreement algorithm takes two inputs: a private key \p private_key
2208 * a public key \p peer_key.
2209 * The result of this function is a byte generator which can
2210 * be used to produce keys and other cryptographic material.
2211 *
Gilles Peskine211a4362018-10-25 22:22:31 +02002212 * The resulting generator always has the maximum capacity permitted by
2213 * the algorithm.
2214 *
Jaeden Amero8afbff82019-01-14 16:56:20 +00002215 * \param[in,out] generator The generator object to set up. It must have been
2216 * initialized as per the documentation for
2217 * #psa_crypto_generator_t and not yet in use.
2218 * \param private_key Handle to the private key to use.
2219 * \param[in] peer_key Public key of the peer. The peer key must be in the
2220 * same format that psa_import_key() accepts for the
2221 * public key type corresponding to the type of
2222 * private_key. That is, this function performs the
2223 * equivalent of
2224 * `psa_import_key(internal_public_key_handle,
2225 * PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(private_key_type),
2226 * peer_key, peer_key_length)` where
2227 * `private_key_type` is the type of `private_key`.
2228 * For example, for EC keys, this means that peer_key
2229 * is interpreted as a point on the curve that the
2230 * private key is on. The standard formats for public
2231 * keys are documented in the documentation of
2232 * psa_export_public_key().
2233 * \param peer_key_length Size of \p peer_key in bytes.
2234 * \param alg The key agreement algorithm to compute
2235 * (\c PSA_ALG_XXX value such that
2236 * #PSA_ALG_IS_KEY_AGREEMENT(\p alg) is true).
Gilles Peskine01d718c2018-09-18 12:01:02 +02002237 *
2238 * \retval #PSA_SUCCESS
2239 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002240 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine01d718c2018-09-18 12:01:02 +02002241 * \retval #PSA_ERROR_EMPTY_SLOT
2242 * \retval #PSA_ERROR_NOT_PERMITTED
2243 * \retval #PSA_ERROR_INVALID_ARGUMENT
2244 * \c private_key is not compatible with \c alg,
2245 * or \p peer_key is not valid for \c alg or not compatible with
2246 * \c private_key.
2247 * \retval #PSA_ERROR_NOT_SUPPORTED
2248 * \c alg is not supported or is not a key derivation algorithm.
2249 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2250 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2251 * \retval #PSA_ERROR_HARDWARE_FAILURE
2252 * \retval #PSA_ERROR_TAMPERING_DETECTED
2253 */
2254psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002255 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02002256 const uint8_t *peer_key,
2257 size_t peer_key_length,
2258 psa_algorithm_t alg);
2259
Gilles Peskineea0fb492018-07-12 17:17:20 +02002260/**@}*/
2261
Gilles Peskineedd76872018-07-20 17:42:05 +02002262/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002263 * @{
2264 */
2265
2266/**
2267 * \brief Generate random bytes.
2268 *
2269 * \warning This function **can** fail! Callers MUST check the return status
2270 * and MUST NOT use the content of the output buffer if the return
2271 * status is not #PSA_SUCCESS.
2272 *
2273 * \note To generate a key, use psa_generate_key() instead.
2274 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002275 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002276 * \param output_size Number of bytes to generate and output.
2277 *
Gilles Peskine28538492018-07-11 17:34:00 +02002278 * \retval #PSA_SUCCESS
2279 * \retval #PSA_ERROR_NOT_SUPPORTED
2280 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2281 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2282 * \retval #PSA_ERROR_HARDWARE_FAILURE
2283 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03002284 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002285 * The library has not been previously initialized by psa_crypto_init().
2286 * It is implementation-dependent whether a failure to initialize
2287 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002288 */
2289psa_status_t psa_generate_random(uint8_t *output,
2290 size_t output_size);
2291
Gilles Peskine4c317f42018-07-12 01:24:09 +02002292/** Extra parameters for RSA key generation.
2293 *
Gilles Peskinebe42f312018-07-13 14:38:15 +02002294 * You may pass a pointer to a structure of this type as the \c extra
Gilles Peskine4c317f42018-07-12 01:24:09 +02002295 * parameter to psa_generate_key().
2296 */
2297typedef struct {
Gilles Peskineedd76872018-07-20 17:42:05 +02002298 uint32_t e; /**< Public exponent value. Default: 65537. */
Gilles Peskine4c317f42018-07-12 01:24:09 +02002299} psa_generate_key_extra_rsa;
2300
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002301/**
2302 * \brief Generate a key or key pair.
2303 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002304 * \param handle Handle to the slot where the key will be stored.
2305 * This must be a valid slot for a key of the chosen
2306 * type: it must have been obtained by calling
2307 * psa_allocate_key() or psa_create_key() with the
2308 * correct \p type and with a maximum size that is
2309 * compatible with \p bits.
2310 * It must not contain any key material yet.
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002311 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2312 * \param bits Key size in bits.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002313 * \param[in] extra Extra parameters for key generation. The
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002314 * interpretation of this parameter depends on
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002315 * \p type. All types support \c NULL to use
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002316 * default parameters. Implementation that support
2317 * the generation of vendor-specific key types
2318 * that allow extra parameters shall document
2319 * the format of these extra parameters and
2320 * the default values. For standard parameters,
2321 * the meaning of \p extra is as follows:
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002322 * - For a symmetric key type (a type such
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002323 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
2324 * false), \p extra must be \c NULL.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002325 * - For an elliptic curve key type (a type
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002326 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
2327 * false), \p extra must be \c NULL.
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002328 * - For an RSA key (\p type is
2329 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
2330 * optional #psa_generate_key_extra_rsa structure
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002331 * specifying the public exponent. The
2332 * default public exponent used when \p extra
2333 * is \c NULL is 65537.
Jaeden Amero1308fb52019-01-11 13:50:43 +00002334 * - For an DSA key (\p type is
2335 * #PSA_KEY_TYPE_DSA_KEYPAIR), \p extra is an
2336 * optional structure specifying the key domain
2337 * parameters. The key domain parameters can also be
2338 * provided by psa_set_key_domain_parameters(),
2339 * which documents the format of the structure.
Jaeden Amero8851c402019-01-11 14:20:03 +00002340 * - For a DH key (\p type is
2341 * #PSA_KEY_TYPE_DH_KEYPAIR), the \p extra is an
2342 * optional structure specifying the key domain
2343 * parameters. The key domain parameters can also be
2344 * provided by psa_set_key_domain_parameters(),
2345 * which documents the format of the structure.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002346 * \param extra_size Size of the buffer that \p extra
2347 * points to, in bytes. Note that if \p extra is
2348 * \c NULL then \p extra_size must be zero.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002349 *
Gilles Peskine28538492018-07-11 17:34:00 +02002350 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01002351 * Success.
2352 * If the key is persistent, the key material and the key's metadata
2353 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002354 * \retval #PSA_ERROR_INVALID_HANDLE
2355 * \retval #PSA_ERROR_OCCUPIED_SLOT
2356 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +02002357 * \retval #PSA_ERROR_NOT_SUPPORTED
2358 * \retval #PSA_ERROR_INVALID_ARGUMENT
2359 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2360 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2361 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2362 * \retval #PSA_ERROR_HARDWARE_FAILURE
2363 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002364 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002365 * The library has not been previously initialized by psa_crypto_init().
2366 * It is implementation-dependent whether a failure to initialize
2367 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002368 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002369psa_status_t psa_generate_key(psa_key_handle_t handle,
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002370 psa_key_type_t type,
2371 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02002372 const void *extra,
2373 size_t extra_size);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002374
2375/**@}*/
2376
Gilles Peskinee59236f2018-01-27 23:32:46 +01002377#ifdef __cplusplus
2378}
2379#endif
2380
Gilles Peskine0cad07c2018-06-27 19:49:02 +02002381/* The file "crypto_sizes.h" contains definitions for size calculation
2382 * macros whose definitions are implementation-specific. */
2383#include "crypto_sizes.h"
2384
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002385/* The file "crypto_struct.h" contains definitions for
2386 * implementation-specific structs that are declared above. */
2387#include "crypto_struct.h"
2388
2389/* The file "crypto_extra.h" contains vendor-specific definitions. This
2390 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01002391#include "crypto_extra.h"
2392
2393#endif /* PSA_CRYPTO_H */