blob: 931a768a4c33b73a80a6d70c60c5d4fcda15b216 [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 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100234 * If the key is currently in use in a multipart operation,
235 * the multipart operation is aborted.
236 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100237 * \param handle The key handle to close.
238 *
239 * \retval #PSA_SUCCESS
240 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100241 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100242 */
243psa_status_t psa_close_key(psa_key_handle_t handle);
244
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100245/**@}*/
246
247/** \defgroup import_export Key import and export
248 * @{
249 */
250
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100251/**
252 * \brief Import a key in binary format.
253 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100254 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100255 * documentation of psa_export_public_key() for the format of public keys
256 * and to the documentation of psa_export_key() for the format for
257 * other key types.
258 *
259 * This specification supports a single format for each key type.
260 * Implementations may support other formats as long as the standard
261 * format is supported. Implementations that support other formats
262 * should ensure that the formats are clearly unambiguous so as to
263 * minimize the risk that an invalid input is accidentally interpreted
264 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100265 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100266 * \param handle Handle to the slot where the key will be stored.
267 * This must be a valid slot for a key of the chosen
268 * type: it must have been obtained by calling
269 * psa_allocate_key() or psa_create_key() with the
270 * correct \p type and with a maximum size that is
271 * compatible with \p data.
Gilles Peskinef7933932018-10-31 14:07:52 +0100272 * \param type Key type (a \c PSA_KEY_TYPE_XXX value). On a successful
273 * import, the key slot will contain a key of this type.
274 * \param[in] data Buffer containing the key data. The content of this
275 * buffer is interpreted according to \p type. It must
276 * contain the format described in the documentation
277 * of psa_export_key() or psa_export_public_key() for
278 * the chosen type.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200279 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100280 *
Gilles Peskine28538492018-07-11 17:34:00 +0200281 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100282 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100283 * If the key is persistent, the key material and the key's metadata
284 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100285 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200286 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200287 * The key type or key size is not supported, either by the
288 * implementation in general or in this particular slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200289 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine308b91d2018-02-08 09:47:44 +0100290 * The key slot is invalid,
291 * or the key data is not correctly formatted.
Gilles Peskine28538492018-07-11 17:34:00 +0200292 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskine65eb8582018-04-19 08:28:58 +0200293 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200294 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
295 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
296 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100297 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200298 * \retval #PSA_ERROR_HARDWARE_FAILURE
299 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300300 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300301 * The library has not been previously initialized by psa_crypto_init().
302 * It is implementation-dependent whether a failure to initialize
303 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100304 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100305psa_status_t psa_import_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100306 psa_key_type_t type,
307 const uint8_t *data,
308 size_t data_length);
309
310/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100311 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200312 *
313 * This function destroys the content of the key slot from both volatile
314 * memory and, if applicable, non-volatile storage. Implementations shall
315 * make a best effort to ensure that any previous content of the slot is
316 * unrecoverable.
317 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100318 * This function also erases any metadata such as policies and frees all
319 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200320 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100321 * If the key is currently in use in a multipart operation,
322 * the multipart operation is aborted.
323 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100324 * \param handle Handle to the key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100325 *
Gilles Peskine28538492018-07-11 17:34:00 +0200326 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200327 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200328 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200329 * The slot holds content and cannot be erased because it is
330 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100331 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200332 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200333 * There was an failure in communication with the cryptoprocessor.
334 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200335 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200336 * The storage is corrupted. Implementations shall make a best effort
337 * to erase key material even in this stage, however applications
338 * should be aware that it may be impossible to guarantee that the
339 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +0200340 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200341 * An unexpected condition which is not a storage corruption or
342 * a communication failure occurred. The cryptoprocessor may have
343 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300344 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300345 * The library has not been previously initialized by psa_crypto_init().
346 * It is implementation-dependent whether a failure to initialize
347 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100348 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100349psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100350
351/**
352 * \brief Get basic metadata about a key.
353 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100354 * \param handle Handle to the key slot to query.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200355 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100356 * This may be a null pointer, in which case the key type
357 * is not written.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200358 * \param[out] bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +0100359 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +0100360 * is not written.
361 *
Gilles Peskine28538492018-07-11 17:34:00 +0200362 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100363 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200364 * \retval #PSA_ERROR_EMPTY_SLOT
Gilles Peskineae32aac2018-11-30 14:39:32 +0100365 * The handle is to a key slot which does not contain key material yet.
Gilles Peskine28538492018-07-11 17:34:00 +0200366 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
367 * \retval #PSA_ERROR_HARDWARE_FAILURE
368 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300369 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300370 * The library has not been previously initialized by psa_crypto_init().
371 * It is implementation-dependent whether a failure to initialize
372 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100373 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100374psa_status_t psa_get_key_information(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100375 psa_key_type_t *type,
376 size_t *bits);
377
378/**
379 * \brief Export a key in binary format.
380 *
381 * The output of this function can be passed to psa_import_key() to
382 * create an equivalent object.
383 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100384 * If the implementation of psa_import_key() supports other formats
385 * beyond the format specified here, the output from psa_export_key()
386 * must use the representation specified here, not the original
387 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100388 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100389 * For standard key types, the output format is as follows:
390 *
391 * - For symmetric keys (including MAC keys), the format is the
392 * raw bytes of the key.
393 * - For DES, the key data consists of 8 bytes. The parity bits must be
394 * correct.
395 * - For Triple-DES, the format is the concatenation of the
396 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100397 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200398 * is the non-encrypted DER encoding of the representation defined by
399 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
400 * ```
401 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200402 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200403 * modulus INTEGER, -- n
404 * publicExponent INTEGER, -- e
405 * privateExponent INTEGER, -- d
406 * prime1 INTEGER, -- p
407 * prime2 INTEGER, -- q
408 * exponent1 INTEGER, -- d mod (p-1)
409 * exponent2 INTEGER, -- d mod (q-1)
410 * coefficient INTEGER, -- (inverse of q) mod p
411 * }
412 * ```
413 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format
414 * is the non-encrypted DER encoding of the representation used by
Gilles Peskinec6290c02018-08-13 17:24:59 +0200415 * OpenSSL and OpenSSH, whose structure is described in ASN.1 as follows:
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200416 * ```
417 * DSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200418 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200419 * prime INTEGER, -- p
420 * subprime INTEGER, -- q
421 * generator INTEGER, -- g
422 * public INTEGER, -- y
423 * private INTEGER, -- x
424 * }
425 * ```
426 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +0100427 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100428 * a representation of the private value as a `ceiling(m/8)`-byte string
429 * where `m` is the bit size associated with the curve, i.e. the bit size
430 * of the order of the curve's coordinate field. This byte string is
431 * in little-endian order for Montgomery curves (curve types
432 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
433 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
434 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100435 * This is the content of the `privateKey` field of the `ECPrivateKey`
436 * format defined by RFC 5915.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200437 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
438 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100439 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100440 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200441 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200442 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200443 * \param[out] data_length On success, the number of bytes
444 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100445 *
Gilles Peskine28538492018-07-11 17:34:00 +0200446 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100447 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200448 * \retval #PSA_ERROR_EMPTY_SLOT
449 * \retval #PSA_ERROR_NOT_PERMITTED
Darryl Green9e2d7a02018-07-24 16:33:30 +0100450 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200451 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
452 * The size of the \p data buffer is too small. You can determine a
453 * sufficient buffer size by calling
454 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
455 * where \c type is the key type
456 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200457 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
458 * \retval #PSA_ERROR_HARDWARE_FAILURE
459 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300460 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300461 * The library has not been previously initialized by psa_crypto_init().
462 * It is implementation-dependent whether a failure to initialize
463 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100464 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100465psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100466 uint8_t *data,
467 size_t data_size,
468 size_t *data_length);
469
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100470/**
471 * \brief Export a public key or the public part of a key pair in binary format.
472 *
473 * The output of this function can be passed to psa_import_key() to
474 * create an object that is equivalent to the public key.
475 *
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200476 * The format is the DER representation defined by RFC 5280 as
477 * `SubjectPublicKeyInfo`, with the `subjectPublicKey` format
478 * specified below.
479 * ```
480 * SubjectPublicKeyInfo ::= SEQUENCE {
481 * algorithm AlgorithmIdentifier,
482 * subjectPublicKey BIT STRING }
483 * AlgorithmIdentifier ::= SEQUENCE {
484 * algorithm OBJECT IDENTIFIER,
485 * parameters ANY DEFINED BY algorithm OPTIONAL }
486 * ```
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100487 *
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200488 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY),
489 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.1 as
490 * `RSAPublicKey`,
491 * with the OID `rsaEncryption`,
492 * and with the parameters `NULL`.
493 * ```
494 * pkcs-1 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840)
495 * rsadsi(113549) pkcs(1) 1 }
496 * rsaEncryption OBJECT IDENTIFIER ::= { pkcs-1 1 }
497 *
498 * RSAPublicKey ::= SEQUENCE {
499 * modulus INTEGER, -- n
500 * publicExponent INTEGER } -- e
501 * ```
502 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY),
503 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.2 as
504 * `DSAPublicKey`,
505 * with the OID `id-dsa`,
506 * and with the parameters `DSS-Parms`.
507 * ```
508 * id-dsa OBJECT IDENTIFIER ::= {
509 * iso(1) member-body(2) us(840) x9-57(10040) x9cm(4) 1 }
510 *
511 * Dss-Parms ::= SEQUENCE {
512 * p INTEGER,
513 * q INTEGER,
514 * g INTEGER }
515 * DSAPublicKey ::= INTEGER -- public key, Y
516 * ```
517 * - For elliptic curve public keys (key types for which
518 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true),
519 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.5 as
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200520 * `ECPoint`, which contains the uncompressed
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200521 * representation defined by SEC1 &sect;2.3.3.
522 * The OID is `id-ecPublicKey`,
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200523 * and the parameters must be given as a `namedCurve` OID as specified in
Gilles Peskinec6290c02018-08-13 17:24:59 +0200524 * RFC 5480 &sect;2.1.1.1 or other applicable standards.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200525 * ```
526 * ansi-X9-62 OBJECT IDENTIFIER ::=
527 * { iso(1) member-body(2) us(840) 10045 }
528 * id-public-key-type OBJECT IDENTIFIER ::= { ansi-X9.62 2 }
529 * id-ecPublicKey OBJECT IDENTIFIER ::= { id-publicKeyType 1 }
530 *
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200531 * ECPoint ::= ...
532 * -- first 8 bits: 0x04;
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100533 * -- then x_P as a `ceiling(m/8)`-byte string, big endian;
534 * -- then y_P as a `ceiling(m/8)`-byte string, big endian;
535 * -- where `m` is the bit size associated with the curve,
Gilles Peskine7b5b4a02018-11-14 21:05:10 +0100536 * -- i.e. the bit size of `q` for a curve over `F_q`.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200537 *
538 * EcpkParameters ::= CHOICE { -- other choices are not allowed
539 * namedCurve OBJECT IDENTIFIER }
540 * ```
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100541 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100542 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200543 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200544 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200545 * \param[out] data_length On success, the number of bytes
546 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100547 *
Gilles Peskine28538492018-07-11 17:34:00 +0200548 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100549 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200550 * \retval #PSA_ERROR_EMPTY_SLOT
551 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200552 * The key is neither a public key nor a key pair.
553 * \retval #PSA_ERROR_NOT_SUPPORTED
554 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
555 * The size of the \p data buffer is too small. You can determine a
556 * sufficient buffer size by calling
557 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
558 * where \c type is the key type
559 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200560 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
561 * \retval #PSA_ERROR_HARDWARE_FAILURE
562 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300563 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300564 * The library has not been previously initialized by psa_crypto_init().
565 * It is implementation-dependent whether a failure to initialize
566 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100567 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100568psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100569 uint8_t *data,
570 size_t data_size,
571 size_t *data_length);
572
573/**@}*/
574
575/** \defgroup policy Key policies
576 * @{
577 */
578
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100579/** The type of the key policy data structure.
580 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000581 * Before calling any function on a key policy, the application must initialize
582 * it by any of the following means:
583 * - Set the structure to all-bits-zero, for example:
584 * \code
585 * psa_key_policy_t policy;
586 * memset(&policy, 0, sizeof(policy));
587 * \endcode
588 * - Initialize the structure to logical zero values, for example:
589 * \code
590 * psa_key_policy_t policy = {0};
591 * \endcode
592 * - Initialize the structure to the initializer #PSA_KEY_POLICY_INIT,
593 * for example:
594 * \code
595 * psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
596 * \endcode
597 * - Assign the result of the function psa_key_policy_init()
598 * to the structure, for example:
599 * \code
600 * psa_key_policy_t policy;
601 * policy = psa_key_policy_init();
602 * \endcode
603 *
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100604 * This is an implementation-defined \c struct. Applications should not
605 * make any assumptions about the content of this structure except
606 * as directed by the documentation of a specific implementation. */
607typedef struct psa_key_policy_s psa_key_policy_t;
608
Jaeden Amero70261c52019-01-04 11:47:20 +0000609/** \def PSA_KEY_POLICY_INIT
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200610 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000611 * This macro returns a suitable initializer for a key policy object of type
612 * #psa_key_policy_t.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200613 */
Jaeden Amero70261c52019-01-04 11:47:20 +0000614#ifdef __DOXYGEN_ONLY__
615/* This is an example definition for documentation purposes.
616 * Implementations should define a suitable value in `crypto_struct.h`.
617 */
618#define PSA_KEY_POLICY_INIT {0}
619#endif
620
621/** Return an initial value for a key policy that forbids all usage of the key.
622 */
623static psa_key_policy_t psa_key_policy_init(void);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100624
Gilles Peskine7e198532018-03-08 07:50:30 +0100625/** \brief Set the standard fields of a policy structure.
626 *
627 * Note that this function does not make any consistency check of the
628 * parameters. The values are only checked when applying the policy to
629 * a key slot with psa_set_key_policy().
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200630 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000631 * \param[in,out] policy The key policy to modify. It must have been
632 * initialized as per the documentation for
633 * #psa_key_policy_t.
634 * \param usage The permitted uses for the key.
635 * \param alg The algorithm that the key may be used for.
Gilles Peskine7e198532018-03-08 07:50:30 +0100636 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100637void psa_key_policy_set_usage(psa_key_policy_t *policy,
638 psa_key_usage_t usage,
639 psa_algorithm_t alg);
640
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200641/** \brief Retrieve the usage field of a policy structure.
642 *
643 * \param[in] policy The policy object to query.
644 *
645 * \return The permitted uses for a key with this policy.
646 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +0200647psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100648
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200649/** \brief Retrieve the algorithm field of a policy structure.
650 *
651 * \param[in] policy The policy object to query.
652 *
653 * \return The permitted algorithm for a key with this policy.
654 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +0200655psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100656
657/** \brief Set the usage policy on a key slot.
658 *
659 * This function must be called on an empty key slot, before importing,
660 * generating or creating a key in the slot. Changing the policy of an
661 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +0100662 *
663 * Implementations may set restrictions on supported key policies
664 * depending on the key type and the key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200665 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100666 * \param handle Handle to the key whose policy is to be changed.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200667 * \param[in] policy The policy object to query.
668 *
669 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100670 * Success.
671 * If the key is persistent, it is implementation-defined whether
672 * the policy has been saved to persistent storage. Implementations
673 * may defer saving the policy until the key material is created.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100674 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200675 * \retval #PSA_ERROR_OCCUPIED_SLOT
676 * \retval #PSA_ERROR_NOT_SUPPORTED
677 * \retval #PSA_ERROR_INVALID_ARGUMENT
678 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
679 * \retval #PSA_ERROR_HARDWARE_FAILURE
680 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300681 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300682 * The library has not been previously initialized by psa_crypto_init().
683 * It is implementation-dependent whether a failure to initialize
684 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100685 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100686psa_status_t psa_set_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100687 const psa_key_policy_t *policy);
688
Gilles Peskine7e198532018-03-08 07:50:30 +0100689/** \brief Get the usage policy for a key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200690 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100691 * \param handle Handle to the key slot whose policy is being queried.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200692 * \param[out] policy On success, the key's policy.
693 *
694 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100695 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200696 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
697 * \retval #PSA_ERROR_HARDWARE_FAILURE
698 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300699 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300700 * The library has not been previously initialized by psa_crypto_init().
701 * It is implementation-dependent whether a failure to initialize
702 * results in this error code.
Gilles Peskine7e198532018-03-08 07:50:30 +0100703 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100704psa_status_t psa_get_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100705 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +0100706
707/**@}*/
708
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100709/** \defgroup hash Message digests
710 * @{
711 */
712
Gilles Peskine308b91d2018-02-08 09:47:44 +0100713/** The type of the state data structure for multipart hash operations.
714 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000715 * Before calling any function on a hash operation object, the application must
716 * initialize it by any of the following means:
717 * - Set the structure to all-bits-zero, for example:
718 * \code
719 * psa_hash_operation_t operation;
720 * memset(&operation, 0, sizeof(operation));
721 * \endcode
722 * - Initialize the structure to logical zero values, for example:
723 * \code
724 * psa_hash_operation_t operation = {0};
725 * \endcode
726 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
727 * for example:
728 * \code
729 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
730 * \endcode
731 * - Assign the result of the function psa_hash_operation_init()
732 * to the structure, for example:
733 * \code
734 * psa_hash_operation_t operation;
735 * operation = psa_hash_operation_init();
736 * \endcode
737 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100738 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100739 * make any assumptions about the content of this structure except
740 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100741typedef struct psa_hash_operation_s psa_hash_operation_t;
742
Jaeden Amero6a25b412019-01-04 11:47:44 +0000743/** \def PSA_HASH_OPERATION_INIT
744 *
745 * This macro returns a suitable initializer for a hash operation object
746 * of type #psa_hash_operation_t.
747 */
748#ifdef __DOXYGEN_ONLY__
749/* This is an example definition for documentation purposes.
750 * Implementations should define a suitable value in `crypto_struct.h`.
751 */
752#define PSA_HASH_OPERATION_INIT {0}
753#endif
754
755/** Return an initial value for a hash operation object.
756 */
757static psa_hash_operation_t psa_hash_operation_init(void);
758
Gilles Peskine308b91d2018-02-08 09:47:44 +0100759/** Start a multipart hash operation.
760 *
761 * The sequence of operations to calculate a hash (message digest)
762 * is as follows:
763 * -# Allocate an operation object which will be passed to all the functions
764 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000765 * -# Initialize the operation object with one of the methods described in the
766 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200767 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100768 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100769 * of the message each time. The hash that is calculated is the hash
770 * of the concatenation of these messages in order.
771 * -# To calculate the hash, call psa_hash_finish().
772 * To compare the hash with an expected value, call psa_hash_verify().
773 *
774 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000775 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100776 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200777 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100778 * eventually terminate the operation. The following events terminate an
779 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100780 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100781 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100782 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000783 * \param[in,out] operation The operation object to set up. It must have
784 * been initialized as per the documentation for
785 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200786 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
787 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100788 *
Gilles Peskine28538492018-07-11 17:34:00 +0200789 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100790 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200791 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200792 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +0200793 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
794 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
795 * \retval #PSA_ERROR_HARDWARE_FAILURE
796 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100797 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200798psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100799 psa_algorithm_t alg);
800
Gilles Peskine308b91d2018-02-08 09:47:44 +0100801/** Add a message fragment to a multipart hash operation.
802 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200803 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100804 *
805 * If this function returns an error status, the operation becomes inactive.
806 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200807 * \param[in,out] operation Active hash operation.
808 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200809 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100810 *
Gilles Peskine28538492018-07-11 17:34:00 +0200811 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100812 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200813 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100814 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200815 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
816 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
817 * \retval #PSA_ERROR_HARDWARE_FAILURE
818 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100819 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100820psa_status_t psa_hash_update(psa_hash_operation_t *operation,
821 const uint8_t *input,
822 size_t input_length);
823
Gilles Peskine308b91d2018-02-08 09:47:44 +0100824/** Finish the calculation of the hash of a message.
825 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200826 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100827 * This function calculates the hash of the message formed by concatenating
828 * the inputs passed to preceding calls to psa_hash_update().
829 *
830 * When this function returns, the operation becomes inactive.
831 *
832 * \warning Applications should not call this function if they expect
833 * a specific value for the hash. Call psa_hash_verify() instead.
834 * Beware that comparing integrity or authenticity data such as
835 * hash values with a function such as \c memcmp is risky
836 * because the time taken by the comparison may leak information
837 * about the hashed data which could allow an attacker to guess
838 * a valid hash and thereby bypass security controls.
839 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200840 * \param[in,out] operation Active hash operation.
841 * \param[out] hash Buffer where the hash is to be written.
842 * \param hash_size Size of the \p hash buffer in bytes.
843 * \param[out] hash_length On success, the number of bytes
844 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +0200845 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +0200846 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100847 *
Gilles Peskine28538492018-07-11 17:34:00 +0200848 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100849 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200850 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100851 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200852 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200853 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200854 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100855 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +0200856 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
857 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
858 * \retval #PSA_ERROR_HARDWARE_FAILURE
859 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100860 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100861psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
862 uint8_t *hash,
863 size_t hash_size,
864 size_t *hash_length);
865
Gilles Peskine308b91d2018-02-08 09:47:44 +0100866/** Finish the calculation of the hash of a message and compare it with
867 * an expected value.
868 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200869 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100870 * This function calculates the hash of the message formed by concatenating
871 * the inputs passed to preceding calls to psa_hash_update(). It then
872 * compares the calculated hash with the expected hash passed as a
873 * parameter to this function.
874 *
875 * When this function returns, the operation becomes inactive.
876 *
Gilles Peskine19067982018-03-20 17:54:53 +0100877 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +0100878 * comparison between the actual hash and the expected hash is performed
879 * in constant time.
880 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200881 * \param[in,out] operation Active hash operation.
882 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200883 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100884 *
Gilles Peskine28538492018-07-11 17:34:00 +0200885 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100886 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +0200887 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100888 * The hash of the message was calculated successfully, but it
889 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +0200890 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100891 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200892 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
893 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
894 * \retval #PSA_ERROR_HARDWARE_FAILURE
895 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100896 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100897psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
898 const uint8_t *hash,
899 size_t hash_length);
900
Gilles Peskine308b91d2018-02-08 09:47:44 +0100901/** Abort a hash operation.
902 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100903 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200904 * \p operation structure itself. Once aborted, the operation object
905 * can be reused for another operation by calling
906 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100907 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200908 * You may call this function any time after the operation object has
909 * been initialized by any of the following methods:
910 * - A call to psa_hash_setup(), whether it succeeds or not.
911 * - Initializing the \c struct to all-bits-zero.
912 * - Initializing the \c struct to logical zeros, e.g.
913 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100914 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200915 * In particular, calling psa_hash_abort() after the operation has been
916 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
917 * psa_hash_verify() is safe and has no effect.
918 *
919 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100920 *
Gilles Peskine28538492018-07-11 17:34:00 +0200921 * \retval #PSA_SUCCESS
922 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200923 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +0200924 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
925 * \retval #PSA_ERROR_HARDWARE_FAILURE
926 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100927 */
928psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100929
930/**@}*/
931
Gilles Peskine8c9def32018-02-08 10:02:12 +0100932/** \defgroup MAC Message authentication codes
933 * @{
934 */
935
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100936/** The type of the state data structure for multipart MAC operations.
937 *
Jaeden Amero769ce272019-01-04 11:48:03 +0000938 * Before calling any function on a MAC operation object, the application must
939 * initialize it by any of the following means:
940 * - Set the structure to all-bits-zero, for example:
941 * \code
942 * psa_mac_operation_t operation;
943 * memset(&operation, 0, sizeof(operation));
944 * \endcode
945 * - Initialize the structure to logical zero values, for example:
946 * \code
947 * psa_mac_operation_t operation = {0};
948 * \endcode
949 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
950 * for example:
951 * \code
952 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
953 * \endcode
954 * - Assign the result of the function psa_mac_operation_init()
955 * to the structure, for example:
956 * \code
957 * psa_mac_operation_t operation;
958 * operation = psa_mac_operation_init();
959 * \endcode
960 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100961 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100962 * make any assumptions about the content of this structure except
963 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100964typedef struct psa_mac_operation_s psa_mac_operation_t;
965
Jaeden Amero769ce272019-01-04 11:48:03 +0000966/** \def PSA_MAC_OPERATION_INIT
967 *
968 * This macro returns a suitable initializer for a MAC operation object of type
969 * #psa_mac_operation_t.
970 */
971#ifdef __DOXYGEN_ONLY__
972/* This is an example definition for documentation purposes.
973 * Implementations should define a suitable value in `crypto_struct.h`.
974 */
975#define PSA_MAC_OPERATION_INIT {0}
976#endif
977
978/** Return an initial value for a MAC operation object.
979 */
980static psa_mac_operation_t psa_mac_operation_init(void);
981
Gilles Peskine89167cb2018-07-08 20:12:23 +0200982/** Start a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100983 *
Gilles Peskine89167cb2018-07-08 20:12:23 +0200984 * This function sets up the calculation of the MAC
985 * (message authentication code) of a byte string.
986 * To verify the MAC of a message against an
987 * expected value, use psa_mac_verify_setup() instead.
988 *
989 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100990 * -# Allocate an operation object which will be passed to all the functions
991 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +0000992 * -# Initialize the operation object with one of the methods described in the
993 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +0200994 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100995 * -# Call psa_mac_update() zero, one or more times, passing a fragment
996 * of the message each time. The MAC that is calculated is the MAC
997 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +0200998 * -# At the end of the message, call psa_mac_sign_finish() to finish
999 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001000 *
1001 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001002 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001003 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001004 * After a successful call to psa_mac_sign_setup(), the application must
1005 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001006 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001007 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001008 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001009 * \param[in,out] operation The operation object to set up. It must have
1010 * been initialized as per the documentation for
1011 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001012 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001013 * It must remain valid until the operation
1014 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001015 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1016 * such that #PSA_ALG_IS_MAC(alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001017 *
Gilles Peskine28538492018-07-11 17:34:00 +02001018 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001019 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001020 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001021 * \retval #PSA_ERROR_EMPTY_SLOT
1022 * \retval #PSA_ERROR_NOT_PERMITTED
1023 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001024 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001025 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001026 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001027 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1028 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1029 * \retval #PSA_ERROR_HARDWARE_FAILURE
1030 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001031 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001032 * The library has not been previously initialized by psa_crypto_init().
1033 * It is implementation-dependent whether a failure to initialize
1034 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001035 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001036psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001037 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001038 psa_algorithm_t alg);
1039
1040/** Start a multipart MAC verification operation.
1041 *
1042 * This function sets up the verification of the MAC
1043 * (message authentication code) of a byte string against an expected value.
1044 *
1045 * The sequence of operations to verify a MAC is as follows:
1046 * -# Allocate an operation object which will be passed to all the functions
1047 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001048 * -# Initialize the operation object with one of the methods described in the
1049 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001050 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001051 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1052 * of the message each time. The MAC that is calculated is the MAC
1053 * of the concatenation of these messages in order.
1054 * -# At the end of the message, call psa_mac_verify_finish() to finish
1055 * calculating the actual MAC of the message and verify it against
1056 * the expected value.
1057 *
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 Peskine89167cb2018-07-08 20:12:23 +02001060 *
1061 * After a successful call to psa_mac_verify_setup(), the application must
1062 * eventually terminate the operation through one of the following methods:
1063 * - A failed call to psa_mac_update().
1064 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1065 *
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 Peskine5f25dd02019-01-14 18:24:53 +01001070 * It must remain valid until the operation
1071 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001072 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1073 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001074 *
Gilles Peskine28538492018-07-11 17:34:00 +02001075 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001076 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001077 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001078 * \retval #PSA_ERROR_EMPTY_SLOT
1079 * \retval #PSA_ERROR_NOT_PERMITTED
1080 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001081 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001082 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001083 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001084 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1085 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1086 * \retval #PSA_ERROR_HARDWARE_FAILURE
1087 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001088 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001089 * The library has not been previously initialized by psa_crypto_init().
1090 * It is implementation-dependent whether a failure to initialize
1091 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001092 */
1093psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001094 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001095 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001096
Gilles Peskinedcd14942018-07-12 00:30:52 +02001097/** Add a message fragment to a multipart MAC operation.
1098 *
1099 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1100 * before calling this function.
1101 *
1102 * If this function returns an error status, the operation becomes inactive.
1103 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001104 * \param[in,out] operation Active MAC operation.
1105 * \param[in] input Buffer containing the message fragment to add to
1106 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001107 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001108 *
1109 * \retval #PSA_SUCCESS
1110 * Success.
1111 * \retval #PSA_ERROR_BAD_STATE
1112 * The operation state is not valid (not started, or already completed).
1113 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1114 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1115 * \retval #PSA_ERROR_HARDWARE_FAILURE
1116 * \retval #PSA_ERROR_TAMPERING_DETECTED
1117 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001118psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1119 const uint8_t *input,
1120 size_t input_length);
1121
Gilles Peskinedcd14942018-07-12 00:30:52 +02001122/** Finish the calculation of the MAC of a message.
1123 *
1124 * The application must call psa_mac_sign_setup() before calling this function.
1125 * This function calculates the MAC of the message formed by concatenating
1126 * the inputs passed to preceding calls to psa_mac_update().
1127 *
1128 * When this function returns, the operation becomes inactive.
1129 *
1130 * \warning Applications should not call this function if they expect
1131 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1132 * Beware that comparing integrity or authenticity data such as
1133 * MAC values with a function such as \c memcmp is risky
1134 * because the time taken by the comparison may leak information
1135 * about the MAC value which could allow an attacker to guess
1136 * a valid MAC and thereby bypass security controls.
1137 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001138 * \param[in,out] operation Active MAC operation.
1139 * \param[out] mac Buffer where the MAC value is to be written.
1140 * \param mac_size Size of the \p mac buffer in bytes.
1141 * \param[out] mac_length On success, the number of bytes
1142 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001143 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001144 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001145 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001146 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001147 *
1148 * \retval #PSA_SUCCESS
1149 * Success.
1150 * \retval #PSA_ERROR_BAD_STATE
1151 * The operation state is not valid (not started, or already completed).
1152 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001153 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001154 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1155 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1156 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1157 * \retval #PSA_ERROR_HARDWARE_FAILURE
1158 * \retval #PSA_ERROR_TAMPERING_DETECTED
1159 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001160psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1161 uint8_t *mac,
1162 size_t mac_size,
1163 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001164
Gilles Peskinedcd14942018-07-12 00:30:52 +02001165/** Finish the calculation of the MAC of a message and compare it with
1166 * an expected value.
1167 *
1168 * The application must call psa_mac_verify_setup() before calling this function.
1169 * This function calculates the MAC of the message formed by concatenating
1170 * the inputs passed to preceding calls to psa_mac_update(). It then
1171 * compares the calculated MAC with the expected MAC passed as a
1172 * parameter to this function.
1173 *
1174 * When this function returns, the operation becomes inactive.
1175 *
1176 * \note Implementations shall make the best effort to ensure that the
1177 * comparison between the actual MAC and the expected MAC is performed
1178 * in constant time.
1179 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001180 * \param[in,out] operation Active MAC operation.
1181 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001182 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001183 *
1184 * \retval #PSA_SUCCESS
1185 * The expected MAC is identical to the actual MAC of the message.
1186 * \retval #PSA_ERROR_INVALID_SIGNATURE
1187 * The MAC of the message was calculated successfully, but it
1188 * differs from the expected MAC.
1189 * \retval #PSA_ERROR_BAD_STATE
1190 * The operation state is not valid (not started, or already completed).
1191 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1192 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1193 * \retval #PSA_ERROR_HARDWARE_FAILURE
1194 * \retval #PSA_ERROR_TAMPERING_DETECTED
1195 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001196psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1197 const uint8_t *mac,
1198 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001199
Gilles Peskinedcd14942018-07-12 00:30:52 +02001200/** Abort a MAC operation.
1201 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001202 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001203 * \p operation structure itself. Once aborted, the operation object
1204 * can be reused for another operation by calling
1205 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001206 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001207 * You may call this function any time after the operation object has
1208 * been initialized by any of the following methods:
1209 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1210 * it succeeds or not.
1211 * - Initializing the \c struct to all-bits-zero.
1212 * - Initializing the \c struct to logical zeros, e.g.
1213 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001214 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001215 * In particular, calling psa_mac_abort() after the operation has been
1216 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1217 * psa_mac_verify_finish() is safe and has no effect.
1218 *
1219 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001220 *
1221 * \retval #PSA_SUCCESS
1222 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001223 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001224 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1225 * \retval #PSA_ERROR_HARDWARE_FAILURE
1226 * \retval #PSA_ERROR_TAMPERING_DETECTED
1227 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001228psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1229
1230/**@}*/
1231
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001232/** \defgroup cipher Symmetric ciphers
1233 * @{
1234 */
1235
1236/** The type of the state data structure for multipart cipher operations.
1237 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001238 * Before calling any function on a cipher operation object, the application
1239 * must initialize it by any of the following means:
1240 * - Set the structure to all-bits-zero, for example:
1241 * \code
1242 * psa_cipher_operation_t operation;
1243 * memset(&operation, 0, sizeof(operation));
1244 * \endcode
1245 * - Initialize the structure to logical zero values, for example:
1246 * \code
1247 * psa_cipher_operation_t operation = {0};
1248 * \endcode
1249 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1250 * for example:
1251 * \code
1252 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1253 * \endcode
1254 * - Assign the result of the function psa_cipher_operation_init()
1255 * to the structure, for example:
1256 * \code
1257 * psa_cipher_operation_t operation;
1258 * operation = psa_cipher_operation_init();
1259 * \endcode
1260 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001261 * This is an implementation-defined \c struct. Applications should not
1262 * make any assumptions about the content of this structure except
1263 * as directed by the documentation of a specific implementation. */
1264typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1265
Jaeden Amero5bae2272019-01-04 11:48:27 +00001266/** \def PSA_CIPHER_OPERATION_INIT
1267 *
1268 * This macro returns a suitable initializer for a cipher operation object of
1269 * type #psa_cipher_operation_t.
1270 */
1271#ifdef __DOXYGEN_ONLY__
1272/* This is an example definition for documentation purposes.
1273 * Implementations should define a suitable value in `crypto_struct.h`.
1274 */
1275#define PSA_CIPHER_OPERATION_INIT {0}
1276#endif
1277
1278/** Return an initial value for a cipher operation object.
1279 */
1280static psa_cipher_operation_t psa_cipher_operation_init(void);
1281
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001282/** Set the key for a multipart symmetric encryption operation.
1283 *
1284 * The sequence of operations to encrypt a message with a symmetric cipher
1285 * is as follows:
1286 * -# Allocate an operation object which will be passed to all the functions
1287 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001288 * -# Initialize the operation object with one of the methods described in the
1289 * documentation for #psa_cipher_operation_t, e.g.
1290 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001291 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001292 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001293 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001294 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001295 * requires a specific IV value.
1296 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1297 * of the message each time.
1298 * -# Call psa_cipher_finish().
1299 *
1300 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001301 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001302 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001303 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001304 * eventually terminate the operation. The following events terminate an
1305 * operation:
itayzafrired7382f2018-08-02 14:19:33 +03001306 * - A failed call to psa_cipher_generate_iv(), psa_cipher_set_iv()
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001307 * or psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001308 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001309 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001310 * \param[in,out] operation The operation object to set up. It must have
1311 * been initialized as per the documentation for
1312 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001313 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001314 * It must remain valid until the operation
1315 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001316 * \param alg The cipher algorithm to compute
1317 * (\c PSA_ALG_XXX value such that
1318 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001319 *
Gilles Peskine28538492018-07-11 17:34:00 +02001320 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001321 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001322 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001323 * \retval #PSA_ERROR_EMPTY_SLOT
1324 * \retval #PSA_ERROR_NOT_PERMITTED
1325 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001326 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001327 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001328 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001329 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1330 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1331 * \retval #PSA_ERROR_HARDWARE_FAILURE
1332 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001333 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001334 * The library has not been previously initialized by psa_crypto_init().
1335 * It is implementation-dependent whether a failure to initialize
1336 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001337 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001338psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001339 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001340 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001341
1342/** Set the key for a multipart symmetric decryption operation.
1343 *
1344 * The sequence of operations to decrypt a message with a symmetric cipher
1345 * is as follows:
1346 * -# Allocate an operation object which will be passed to all the functions
1347 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001348 * -# Initialize the operation object with one of the methods described in the
1349 * documentation for #psa_cipher_operation_t, e.g.
1350 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001351 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001352 * -# Call psa_cipher_update() with the IV (initialization vector) for the
1353 * decryption. If the IV is prepended to the ciphertext, you can call
1354 * psa_cipher_update() on a buffer containing the IV followed by the
1355 * beginning of the message.
1356 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1357 * of the message each time.
1358 * -# Call psa_cipher_finish().
1359 *
1360 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001361 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001362 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001363 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001364 * eventually terminate the operation. The following events terminate an
1365 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001366 * - A failed call to psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001367 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001368 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001369 * \param[in,out] operation The operation object to set up. It must have
1370 * been initialized as per the documentation for
1371 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001372 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001373 * It must remain valid until the operation
1374 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001375 * \param alg The cipher algorithm to compute
1376 * (\c PSA_ALG_XXX value such that
1377 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001378 *
Gilles Peskine28538492018-07-11 17:34:00 +02001379 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001380 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001381 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001382 * \retval #PSA_ERROR_EMPTY_SLOT
1383 * \retval #PSA_ERROR_NOT_PERMITTED
1384 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001385 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001386 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001387 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001388 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1389 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1390 * \retval #PSA_ERROR_HARDWARE_FAILURE
1391 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001392 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001393 * The library has not been previously initialized by psa_crypto_init().
1394 * It is implementation-dependent whether a failure to initialize
1395 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001396 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001397psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001398 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001399 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001400
Gilles Peskinedcd14942018-07-12 00:30:52 +02001401/** Generate an IV for a symmetric encryption operation.
1402 *
1403 * This function generates a random IV (initialization vector), nonce
1404 * or initial counter value for the encryption operation as appropriate
1405 * for the chosen algorithm, key type and key size.
1406 *
1407 * The application must call psa_cipher_encrypt_setup() before
1408 * calling this function.
1409 *
1410 * If this function returns an error status, the operation becomes inactive.
1411 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001412 * \param[in,out] operation Active cipher operation.
1413 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001414 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001415 * \param[out] iv_length On success, the number of bytes of the
1416 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001417 *
1418 * \retval #PSA_SUCCESS
1419 * Success.
1420 * \retval #PSA_ERROR_BAD_STATE
1421 * The operation state is not valid (not started, or IV already set).
1422 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001423 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001424 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1425 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1426 * \retval #PSA_ERROR_HARDWARE_FAILURE
1427 * \retval #PSA_ERROR_TAMPERING_DETECTED
1428 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001429psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1430 unsigned char *iv,
1431 size_t iv_size,
1432 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001433
Gilles Peskinedcd14942018-07-12 00:30:52 +02001434/** Set the IV for a symmetric encryption or decryption operation.
1435 *
1436 * This function sets the random IV (initialization vector), nonce
1437 * or initial counter value for the encryption or decryption operation.
1438 *
1439 * The application must call psa_cipher_encrypt_setup() before
1440 * calling this function.
1441 *
1442 * If this function returns an error status, the operation becomes inactive.
1443 *
1444 * \note When encrypting, applications should use psa_cipher_generate_iv()
1445 * instead of this function, unless implementing a protocol that requires
1446 * a non-random IV.
1447 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001448 * \param[in,out] operation Active cipher operation.
1449 * \param[in] iv Buffer containing the IV to use.
1450 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001451 *
1452 * \retval #PSA_SUCCESS
1453 * Success.
1454 * \retval #PSA_ERROR_BAD_STATE
1455 * The operation state is not valid (not started, or IV already set).
1456 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001457 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001458 * or the chosen algorithm does not use an IV.
1459 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1460 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1461 * \retval #PSA_ERROR_HARDWARE_FAILURE
1462 * \retval #PSA_ERROR_TAMPERING_DETECTED
1463 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001464psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1465 const unsigned char *iv,
1466 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001467
Gilles Peskinedcd14942018-07-12 00:30:52 +02001468/** Encrypt or decrypt a message fragment in an active cipher operation.
1469 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001470 * Before calling this function, you must:
1471 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1472 * The choice of setup function determines whether this function
1473 * encrypts or decrypts its input.
1474 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1475 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001476 *
1477 * If this function returns an error status, the operation becomes inactive.
1478 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001479 * \param[in,out] operation Active cipher operation.
1480 * \param[in] input Buffer containing the message fragment to
1481 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001482 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001483 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001484 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001485 * \param[out] output_length On success, the number of bytes
1486 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001487 *
1488 * \retval #PSA_SUCCESS
1489 * Success.
1490 * \retval #PSA_ERROR_BAD_STATE
1491 * The operation state is not valid (not started, IV required but
1492 * not set, or already completed).
1493 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1494 * The size of the \p output buffer is too small.
1495 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1496 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1497 * \retval #PSA_ERROR_HARDWARE_FAILURE
1498 * \retval #PSA_ERROR_TAMPERING_DETECTED
1499 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001500psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1501 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001502 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001503 unsigned char *output,
1504 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001505 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001506
Gilles Peskinedcd14942018-07-12 00:30:52 +02001507/** Finish encrypting or decrypting a message in a cipher operation.
1508 *
1509 * The application must call psa_cipher_encrypt_setup() or
1510 * psa_cipher_decrypt_setup() before calling this function. The choice
1511 * of setup function determines whether this function encrypts or
1512 * decrypts its input.
1513 *
1514 * This function finishes the encryption or decryption of the message
1515 * formed by concatenating the inputs passed to preceding calls to
1516 * psa_cipher_update().
1517 *
1518 * When this function returns, the operation becomes inactive.
1519 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001520 * \param[in,out] operation Active cipher operation.
1521 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001522 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001523 * \param[out] output_length On success, the number of bytes
1524 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001525 *
1526 * \retval #PSA_SUCCESS
1527 * Success.
1528 * \retval #PSA_ERROR_BAD_STATE
1529 * The operation state is not valid (not started, IV required but
1530 * not set, or already completed).
1531 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1532 * The size of the \p output buffer is too small.
1533 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1534 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1535 * \retval #PSA_ERROR_HARDWARE_FAILURE
1536 * \retval #PSA_ERROR_TAMPERING_DETECTED
1537 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001538psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001539 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001540 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001541 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001542
Gilles Peskinedcd14942018-07-12 00:30:52 +02001543/** Abort a cipher operation.
1544 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001545 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001546 * \p operation structure itself. Once aborted, the operation object
1547 * can be reused for another operation by calling
1548 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001549 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001550 * You may call this function any time after the operation object has
1551 * been initialized by any of the following methods:
1552 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1553 * whether it succeeds or not.
1554 * - Initializing the \c struct to all-bits-zero.
1555 * - Initializing the \c struct to logical zeros, e.g.
1556 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001557 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001558 * In particular, calling psa_cipher_abort() after the operation has been
1559 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1560 * is safe and has no effect.
1561 *
1562 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001563 *
1564 * \retval #PSA_SUCCESS
1565 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001566 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001567 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1568 * \retval #PSA_ERROR_HARDWARE_FAILURE
1569 * \retval #PSA_ERROR_TAMPERING_DETECTED
1570 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001571psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1572
1573/**@}*/
1574
Gilles Peskine3b555712018-03-03 21:27:57 +01001575/** \defgroup aead Authenticated encryption with associated data (AEAD)
1576 * @{
1577 */
1578
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001579/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001580 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001581 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001582 * \param alg The AEAD algorithm to compute
1583 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001584 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001585 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001586 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001587 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001588 * but not encrypted.
1589 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001590 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001591 * encrypted.
1592 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001593 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001594 * encrypted data. The additional data is not
1595 * part of this output. For algorithms where the
1596 * encrypted data and the authentication tag
1597 * are defined as separate outputs, the
1598 * authentication tag is appended to the
1599 * encrypted data.
1600 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1601 * This must be at least
1602 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1603 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001604 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001605 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001606 *
Gilles Peskine28538492018-07-11 17:34:00 +02001607 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001608 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001609 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001610 * \retval #PSA_ERROR_EMPTY_SLOT
1611 * \retval #PSA_ERROR_NOT_PERMITTED
1612 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001613 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001614 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001615 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001616 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1617 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1618 * \retval #PSA_ERROR_HARDWARE_FAILURE
1619 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001620 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001621 * The library has not been previously initialized by psa_crypto_init().
1622 * It is implementation-dependent whether a failure to initialize
1623 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001624 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001625psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001626 psa_algorithm_t alg,
1627 const uint8_t *nonce,
1628 size_t nonce_length,
1629 const uint8_t *additional_data,
1630 size_t additional_data_length,
1631 const uint8_t *plaintext,
1632 size_t plaintext_length,
1633 uint8_t *ciphertext,
1634 size_t ciphertext_size,
1635 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001636
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001637/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001638 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001639 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001640 * \param alg The AEAD algorithm to compute
1641 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001642 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001643 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001644 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001645 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001646 * but not encrypted.
1647 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001648 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001649 * encrypted. For algorithms where the
1650 * encrypted data and the authentication tag
1651 * are defined as separate inputs, the buffer
1652 * must contain the encrypted data followed
1653 * by the authentication tag.
1654 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001655 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001656 * \param plaintext_size Size of the \p plaintext buffer in bytes.
1657 * This must be at least
1658 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
1659 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001660 * \param[out] plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03001661 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001662 *
Gilles Peskine28538492018-07-11 17:34:00 +02001663 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001664 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001665 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001666 * \retval #PSA_ERROR_EMPTY_SLOT
1667 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001668 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02001669 * \retval #PSA_ERROR_NOT_PERMITTED
1670 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001671 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001672 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001673 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001674 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1675 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1676 * \retval #PSA_ERROR_HARDWARE_FAILURE
1677 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001678 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001679 * The library has not been previously initialized by psa_crypto_init().
1680 * It is implementation-dependent whether a failure to initialize
1681 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001682 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001683psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001684 psa_algorithm_t alg,
1685 const uint8_t *nonce,
1686 size_t nonce_length,
1687 const uint8_t *additional_data,
1688 size_t additional_data_length,
1689 const uint8_t *ciphertext,
1690 size_t ciphertext_length,
1691 uint8_t *plaintext,
1692 size_t plaintext_size,
1693 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001694
1695/**@}*/
1696
Gilles Peskine20035e32018-02-03 22:44:14 +01001697/** \defgroup asymmetric Asymmetric cryptography
1698 * @{
1699 */
1700
1701/**
1702 * \brief Sign a hash or short message with a private key.
1703 *
Gilles Peskine08bac712018-06-26 16:14:46 +02001704 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001705 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02001706 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
1707 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
1708 * to determine the hash algorithm to use.
1709 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001710 * \param handle Handle to the key to use for the operation.
1711 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001712 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001713 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001714 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001715 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001716 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001717 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001718 * \param[out] signature_length On success, the number of bytes
1719 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001720 *
Gilles Peskine28538492018-07-11 17:34:00 +02001721 * \retval #PSA_SUCCESS
1722 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001723 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01001724 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001725 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001726 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001727 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001728 * \retval #PSA_ERROR_NOT_SUPPORTED
1729 * \retval #PSA_ERROR_INVALID_ARGUMENT
1730 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1731 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1732 * \retval #PSA_ERROR_HARDWARE_FAILURE
1733 * \retval #PSA_ERROR_TAMPERING_DETECTED
1734 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03001735 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001736 * The library has not been previously initialized by psa_crypto_init().
1737 * It is implementation-dependent whether a failure to initialize
1738 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01001739 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001740psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01001741 psa_algorithm_t alg,
1742 const uint8_t *hash,
1743 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01001744 uint8_t *signature,
1745 size_t signature_size,
1746 size_t *signature_length);
1747
1748/**
1749 * \brief Verify the signature a hash or short message using a public key.
1750 *
Gilles Peskine08bac712018-06-26 16:14:46 +02001751 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001752 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02001753 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
1754 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
1755 * to determine the hash algorithm to use.
1756 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001757 * \param handle Handle to the key to use for the operation.
1758 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001759 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001760 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001761 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02001762 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001763 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001764 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001765 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001766 *
Gilles Peskine28538492018-07-11 17:34:00 +02001767 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001768 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02001769 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001770 * The calculation was perfomed successfully, but the passed
1771 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02001772 * \retval #PSA_ERROR_NOT_SUPPORTED
1773 * \retval #PSA_ERROR_INVALID_ARGUMENT
1774 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1775 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1776 * \retval #PSA_ERROR_HARDWARE_FAILURE
1777 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001778 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001779 * The library has not been previously initialized by psa_crypto_init().
1780 * It is implementation-dependent whether a failure to initialize
1781 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01001782 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001783psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01001784 psa_algorithm_t alg,
1785 const uint8_t *hash,
1786 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02001787 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02001788 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01001789
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001790/**
1791 * \brief Encrypt a short message with a public key.
1792 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001793 * \param handle Handle to the key to use for the operation.
1794 * It must be a public key or an asymmetric
1795 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001796 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001797 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001798 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001799 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001800 * \param[in] salt A salt or label, if supported by the
1801 * encryption algorithm.
1802 * If the algorithm does not support a
1803 * salt, pass \c NULL.
1804 * If the algorithm supports an optional
1805 * salt and you do not want to pass a salt,
1806 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001807 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001808 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1809 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001810 * \param salt_length Size of the \p salt buffer in bytes.
1811 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001812 * \param[out] output Buffer where the encrypted message is to
1813 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001814 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001815 * \param[out] output_length On success, the number of bytes
1816 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001817 *
Gilles Peskine28538492018-07-11 17:34:00 +02001818 * \retval #PSA_SUCCESS
1819 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001820 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001821 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001822 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001823 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001824 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001825 * \retval #PSA_ERROR_NOT_SUPPORTED
1826 * \retval #PSA_ERROR_INVALID_ARGUMENT
1827 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1828 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1829 * \retval #PSA_ERROR_HARDWARE_FAILURE
1830 * \retval #PSA_ERROR_TAMPERING_DETECTED
1831 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03001832 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001833 * The library has not been previously initialized by psa_crypto_init().
1834 * It is implementation-dependent whether a failure to initialize
1835 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001836 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001837psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001838 psa_algorithm_t alg,
1839 const uint8_t *input,
1840 size_t input_length,
1841 const uint8_t *salt,
1842 size_t salt_length,
1843 uint8_t *output,
1844 size_t output_size,
1845 size_t *output_length);
1846
1847/**
1848 * \brief Decrypt a short message with a private key.
1849 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001850 * \param handle Handle to the key to use for the operation.
1851 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001852 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001853 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001854 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001855 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001856 * \param[in] salt A salt or label, if supported by the
1857 * encryption algorithm.
1858 * If the algorithm does not support a
1859 * salt, pass \c NULL.
1860 * If the algorithm supports an optional
1861 * salt and you do not want to pass a salt,
1862 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001863 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001864 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1865 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001866 * \param salt_length Size of the \p salt buffer in bytes.
1867 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001868 * \param[out] output Buffer where the decrypted message is to
1869 * be written.
1870 * \param output_size Size of the \c output buffer in bytes.
1871 * \param[out] output_length On success, the number of bytes
1872 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001873 *
Gilles Peskine28538492018-07-11 17:34:00 +02001874 * \retval #PSA_SUCCESS
1875 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001876 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001877 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001878 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001879 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001880 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001881 * \retval #PSA_ERROR_NOT_SUPPORTED
1882 * \retval #PSA_ERROR_INVALID_ARGUMENT
1883 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1884 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1885 * \retval #PSA_ERROR_HARDWARE_FAILURE
1886 * \retval #PSA_ERROR_TAMPERING_DETECTED
1887 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
1888 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03001889 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001890 * The library has not been previously initialized by psa_crypto_init().
1891 * It is implementation-dependent whether a failure to initialize
1892 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001893 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001894psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001895 psa_algorithm_t alg,
1896 const uint8_t *input,
1897 size_t input_length,
1898 const uint8_t *salt,
1899 size_t salt_length,
1900 uint8_t *output,
1901 size_t output_size,
1902 size_t *output_length);
1903
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001904/**@}*/
1905
Gilles Peskineedd76872018-07-20 17:42:05 +02001906/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02001907 * @{
1908 */
1909
1910/** The type of the state data structure for generators.
1911 *
1912 * Before calling any function on a generator, the application must
1913 * initialize it by any of the following means:
1914 * - Set the structure to all-bits-zero, for example:
1915 * \code
1916 * psa_crypto_generator_t generator;
1917 * memset(&generator, 0, sizeof(generator));
1918 * \endcode
1919 * - Initialize the structure to logical zero values, for example:
1920 * \code
1921 * psa_crypto_generator_t generator = {0};
1922 * \endcode
1923 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
1924 * for example:
1925 * \code
1926 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1927 * \endcode
1928 * - Assign the result of the function psa_crypto_generator_init()
1929 * to the structure, for example:
1930 * \code
1931 * psa_crypto_generator_t generator;
1932 * generator = psa_crypto_generator_init();
1933 * \endcode
1934 *
1935 * This is an implementation-defined \c struct. Applications should not
1936 * make any assumptions about the content of this structure except
1937 * as directed by the documentation of a specific implementation.
1938 */
1939typedef struct psa_crypto_generator_s psa_crypto_generator_t;
1940
1941/** \def PSA_CRYPTO_GENERATOR_INIT
1942 *
1943 * This macro returns a suitable initializer for a generator object
1944 * of type #psa_crypto_generator_t.
1945 */
1946#ifdef __DOXYGEN_ONLY__
1947/* This is an example definition for documentation purposes.
1948 * Implementations should define a suitable value in `crypto_struct.h`.
1949 */
1950#define PSA_CRYPTO_GENERATOR_INIT {0}
1951#endif
1952
1953/** Return an initial value for a generator object.
1954 */
1955static psa_crypto_generator_t psa_crypto_generator_init(void);
1956
1957/** Retrieve the current capacity of a generator.
1958 *
1959 * The capacity of a generator is the maximum number of bytes that it can
1960 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
1961 *
1962 * \param[in] generator The generator to query.
1963 * \param[out] capacity On success, the capacity of the generator.
1964 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001965 * \retval #PSA_SUCCESS
1966 * \retval #PSA_ERROR_BAD_STATE
1967 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02001968 */
1969psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
1970 size_t *capacity);
1971
1972/** Read some data from a generator.
1973 *
1974 * This function reads and returns a sequence of bytes from a generator.
1975 * The data that is read is discarded from the generator. The generator's
1976 * capacity is decreased by the number of bytes read.
1977 *
1978 * \param[in,out] generator The generator object to read from.
1979 * \param[out] output Buffer where the generator output will be
1980 * written.
1981 * \param output_length Number of bytes to output.
1982 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001983 * \retval #PSA_SUCCESS
1984 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02001985 * There were fewer than \p output_length bytes
1986 * in the generator. Note that in this case, no
1987 * output is written to the output buffer.
1988 * The generator's capacity is set to 0, thus
1989 * subsequent calls to this function will not
1990 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001991 * \retval #PSA_ERROR_BAD_STATE
1992 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1993 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1994 * \retval #PSA_ERROR_HARDWARE_FAILURE
1995 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02001996 */
1997psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
1998 uint8_t *output,
1999 size_t output_length);
2000
2001/** Create a symmetric key from data read from a generator.
2002 *
2003 * This function reads a sequence of bytes from a generator and imports
2004 * these bytes as a key.
2005 * The data that is read is discarded from the generator. The generator's
2006 * capacity is decreased by the number of bytes read.
2007 *
2008 * This function is equivalent to calling #psa_generator_read and
2009 * passing the resulting output to #psa_import_key, but
2010 * if the implementation provides an isolation boundary then
2011 * the key material is not exposed outside the isolation boundary.
2012 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002013 * \param handle Handle to the slot where the key will be stored.
2014 * This must be a valid slot for a key of the chosen
2015 * type: it must have been obtained by calling
2016 * psa_allocate_key() or psa_create_key() with the
2017 * correct \p type and with a maximum size that is
2018 * compatible with \p bits.
2019 * It must not contain any key material yet.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002020 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2021 * This must be a symmetric key type.
2022 * \param bits Key size in bits.
2023 * \param[in,out] generator The generator object to read from.
2024 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002025 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02002026 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01002027 * If the key is persistent, the key material and the key's metadata
2028 * have been saved to persistent storage.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002029 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02002030 * There were fewer than \p output_length bytes
2031 * in the generator. Note that in this case, no
2032 * output is written to the output buffer.
2033 * The generator's capacity is set to 0, thus
2034 * subsequent calls to this function will not
2035 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002036 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002037 * The key type or key size is not supported, either by the
2038 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002039 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineae32aac2018-11-30 14:39:32 +01002040 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002041 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskineeab56e42018-07-12 17:12:33 +02002042 * There is already a key in the specified slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002043 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2044 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
2045 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2046 * \retval #PSA_ERROR_HARDWARE_FAILURE
2047 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002048 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002049 * The library has not been previously initialized by psa_crypto_init().
2050 * It is implementation-dependent whether a failure to initialize
2051 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002052 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002053psa_status_t psa_generator_import_key(psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002054 psa_key_type_t type,
2055 size_t bits,
2056 psa_crypto_generator_t *generator);
2057
2058/** Abort a generator.
2059 *
2060 * Once a generator has been aborted, its capacity is zero.
2061 * Aborting a generator frees all associated resources except for the
2062 * \c generator structure itself.
2063 *
2064 * This function may be called at any time as long as the generator
2065 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
2066 * psa_crypto_generator_init() or a zero value. In particular, it is valid
2067 * to call psa_generator_abort() twice, or to call psa_generator_abort()
2068 * on a generator that has not been set up.
2069 *
2070 * Once aborted, the generator object may be called.
2071 *
2072 * \param[in,out] generator The generator to abort.
2073 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002074 * \retval #PSA_SUCCESS
2075 * \retval #PSA_ERROR_BAD_STATE
2076 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2077 * \retval #PSA_ERROR_HARDWARE_FAILURE
2078 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002079 */
2080psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
2081
Gilles Peskine8feb3a82018-09-18 12:06:11 +02002082/** Use the maximum possible capacity for a generator.
2083 *
2084 * Use this value as the capacity argument when setting up a generator
2085 * to indicate that the generator should have the maximum possible capacity.
2086 * The value of the maximum possible capacity depends on the generator
2087 * algorithm.
2088 */
2089#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
2090
Gilles Peskineeab56e42018-07-12 17:12:33 +02002091/**@}*/
2092
Gilles Peskineea0fb492018-07-12 17:17:20 +02002093/** \defgroup derivation Key derivation
2094 * @{
2095 */
2096
2097/** Set up a key derivation operation.
2098 *
2099 * A key derivation algorithm takes three inputs: a secret input \p key and
2100 * two non-secret inputs \p label and p salt.
2101 * The result of this function is a byte generator which can
2102 * be used to produce keys and other cryptographic material.
2103 *
2104 * The role of \p label and \p salt is as follows:
Gilles Peskinebef7f142018-07-12 17:22:21 +02002105 * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step
2106 * and \p label is the info string used in the "expand" step.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002107 *
Jaeden Amero9e919c62019-01-07 15:41:50 +00002108 * \param[in,out] generator The generator object to set up. It must have
2109 * been initialized as per the documentation for
2110 * #psa_crypto_generator_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002111 * \param handle Handle to the secret key.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002112 * \param alg The key derivation algorithm to compute
2113 * (\c PSA_ALG_XXX value such that
2114 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2115 * \param[in] salt Salt to use.
2116 * \param salt_length Size of the \p salt buffer in bytes.
2117 * \param[in] label Label to use.
2118 * \param label_length Size of the \p label buffer in bytes.
2119 * \param capacity The maximum number of bytes that the
2120 * generator will be able to provide.
2121 *
2122 * \retval #PSA_SUCCESS
2123 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002124 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineea0fb492018-07-12 17:17:20 +02002125 * \retval #PSA_ERROR_EMPTY_SLOT
2126 * \retval #PSA_ERROR_NOT_PERMITTED
2127 * \retval #PSA_ERROR_INVALID_ARGUMENT
2128 * \c key is not compatible with \c alg,
2129 * or \p capacity is too large for the specified algorithm and key.
2130 * \retval #PSA_ERROR_NOT_SUPPORTED
2131 * \c alg is not supported or is not a key derivation algorithm.
2132 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2133 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2134 * \retval #PSA_ERROR_HARDWARE_FAILURE
2135 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002136 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002137 * The library has not been previously initialized by psa_crypto_init().
2138 * It is implementation-dependent whether a failure to initialize
2139 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002140 */
2141psa_status_t psa_key_derivation(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002142 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02002143 psa_algorithm_t alg,
2144 const uint8_t *salt,
2145 size_t salt_length,
2146 const uint8_t *label,
2147 size_t label_length,
2148 size_t capacity);
2149
Gilles Peskine01d718c2018-09-18 12:01:02 +02002150/** Set up a key agreement operation.
2151 *
2152 * A key agreement algorithm takes two inputs: a private key \p private_key
2153 * a public key \p peer_key.
2154 * The result of this function is a byte generator which can
2155 * be used to produce keys and other cryptographic material.
2156 *
Gilles Peskine211a4362018-10-25 22:22:31 +02002157 * The resulting generator always has the maximum capacity permitted by
2158 * the algorithm.
2159 *
Jaeden Amero9e919c62019-01-07 15:41:50 +00002160 * \param[in,out] generator The generator object to set up. It must have
2161 * been initialized as per the documentation for
2162 * #psa_crypto_generator_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002163 * \param private_key Handle to the private key to use.
Gilles Peskined171e782018-11-15 17:46:21 +01002164 * \param[in] peer_key Public key of the peer. It must be
2165 * in the same format that psa_import_key()
2166 * accepts. The standard formats for public
2167 * keys are documented in the documentation
2168 * of psa_export_public_key().
Gilles Peskine01d718c2018-09-18 12:01:02 +02002169 * \param peer_key_length Size of \p peer_key in bytes.
2170 * \param alg The key agreement algorithm to compute
2171 * (\c PSA_ALG_XXX value such that
2172 * #PSA_ALG_IS_KEY_AGREEMENT(\p alg) is true).
2173 *
2174 * \retval #PSA_SUCCESS
2175 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002176 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine01d718c2018-09-18 12:01:02 +02002177 * \retval #PSA_ERROR_EMPTY_SLOT
2178 * \retval #PSA_ERROR_NOT_PERMITTED
2179 * \retval #PSA_ERROR_INVALID_ARGUMENT
2180 * \c private_key is not compatible with \c alg,
2181 * or \p peer_key is not valid for \c alg or not compatible with
2182 * \c private_key.
2183 * \retval #PSA_ERROR_NOT_SUPPORTED
2184 * \c alg is not supported or is not a key derivation algorithm.
2185 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2186 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2187 * \retval #PSA_ERROR_HARDWARE_FAILURE
2188 * \retval #PSA_ERROR_TAMPERING_DETECTED
2189 */
2190psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002191 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02002192 const uint8_t *peer_key,
2193 size_t peer_key_length,
2194 psa_algorithm_t alg);
2195
Gilles Peskineea0fb492018-07-12 17:17:20 +02002196/**@}*/
2197
Gilles Peskineedd76872018-07-20 17:42:05 +02002198/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002199 * @{
2200 */
2201
2202/**
2203 * \brief Generate random bytes.
2204 *
2205 * \warning This function **can** fail! Callers MUST check the return status
2206 * and MUST NOT use the content of the output buffer if the return
2207 * status is not #PSA_SUCCESS.
2208 *
2209 * \note To generate a key, use psa_generate_key() instead.
2210 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002211 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002212 * \param output_size Number of bytes to generate and output.
2213 *
Gilles Peskine28538492018-07-11 17:34:00 +02002214 * \retval #PSA_SUCCESS
2215 * \retval #PSA_ERROR_NOT_SUPPORTED
2216 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2217 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2218 * \retval #PSA_ERROR_HARDWARE_FAILURE
2219 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03002220 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002221 * The library has not been previously initialized by psa_crypto_init().
2222 * It is implementation-dependent whether a failure to initialize
2223 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002224 */
2225psa_status_t psa_generate_random(uint8_t *output,
2226 size_t output_size);
2227
Gilles Peskine4c317f42018-07-12 01:24:09 +02002228/** Extra parameters for RSA key generation.
2229 *
Gilles Peskinebe42f312018-07-13 14:38:15 +02002230 * You may pass a pointer to a structure of this type as the \c extra
Gilles Peskine4c317f42018-07-12 01:24:09 +02002231 * parameter to psa_generate_key().
2232 */
2233typedef struct {
Gilles Peskineedd76872018-07-20 17:42:05 +02002234 uint32_t e; /**< Public exponent value. Default: 65537. */
Gilles Peskine4c317f42018-07-12 01:24:09 +02002235} psa_generate_key_extra_rsa;
2236
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002237/**
2238 * \brief Generate a key or key pair.
2239 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002240 * \param handle Handle to the slot where the key will be stored.
2241 * This must be a valid slot for a key of the chosen
2242 * type: it must have been obtained by calling
2243 * psa_allocate_key() or psa_create_key() with the
2244 * correct \p type and with a maximum size that is
2245 * compatible with \p bits.
2246 * It must not contain any key material yet.
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002247 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2248 * \param bits Key size in bits.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002249 * \param[in] extra Extra parameters for key generation. The
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002250 * interpretation of this parameter depends on
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002251 * \p type. All types support \c NULL to use
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002252 * default parameters. Implementation that support
2253 * the generation of vendor-specific key types
2254 * that allow extra parameters shall document
2255 * the format of these extra parameters and
2256 * the default values. For standard parameters,
2257 * the meaning of \p extra is as follows:
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002258 * - For a symmetric key type (a type such
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002259 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
2260 * false), \p extra must be \c NULL.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002261 * - For an elliptic curve key type (a type
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002262 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
2263 * false), \p extra must be \c NULL.
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002264 * - For an RSA key (\p type is
2265 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
2266 * optional #psa_generate_key_extra_rsa structure
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002267 * specifying the public exponent. The
2268 * default public exponent used when \p extra
2269 * is \c NULL is 65537.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002270 * \param extra_size Size of the buffer that \p extra
2271 * points to, in bytes. Note that if \p extra is
2272 * \c NULL then \p extra_size must be zero.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002273 *
Gilles Peskine28538492018-07-11 17:34:00 +02002274 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01002275 * Success.
2276 * If the key is persistent, the key material and the key's metadata
2277 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002278 * \retval #PSA_ERROR_INVALID_HANDLE
2279 * \retval #PSA_ERROR_OCCUPIED_SLOT
2280 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +02002281 * \retval #PSA_ERROR_NOT_SUPPORTED
2282 * \retval #PSA_ERROR_INVALID_ARGUMENT
2283 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2284 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2285 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2286 * \retval #PSA_ERROR_HARDWARE_FAILURE
2287 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002288 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002289 * The library has not been previously initialized by psa_crypto_init().
2290 * It is implementation-dependent whether a failure to initialize
2291 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002292 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002293psa_status_t psa_generate_key(psa_key_handle_t handle,
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002294 psa_key_type_t type,
2295 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02002296 const void *extra,
2297 size_t extra_size);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002298
2299/**@}*/
2300
Gilles Peskinee59236f2018-01-27 23:32:46 +01002301#ifdef __cplusplus
2302}
2303#endif
2304
Gilles Peskine0cad07c2018-06-27 19:49:02 +02002305/* The file "crypto_sizes.h" contains definitions for size calculation
2306 * macros whose definitions are implementation-specific. */
2307#include "crypto_sizes.h"
2308
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002309/* The file "crypto_struct.h" contains definitions for
2310 * implementation-specific structs that are declared above. */
2311#include "crypto_struct.h"
2312
2313/* The file "crypto_extra.h" contains vendor-specific definitions. This
2314 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01002315#include "crypto_extra.h"
2316
2317#endif /* PSA_CRYPTO_H */