blob: 903ef99a4b3ce99d1727750d3f65e450aa1dfe26 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto.h
3 * \brief Platform Security Architecture cryptography module
4 */
Jaeden Amerocab54942018-07-25 13:26:13 +01005/*
6 * Copyright (C) 2018, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
Gilles Peskinee59236f2018-01-27 23:32:46 +010021
22#ifndef PSA_CRYPTO_H
23#define PSA_CRYPTO_H
24
25#include "crypto_platform.h"
26
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027#include <stddef.h>
28
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010029#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010030/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
31 * must be defined in the crypto_platform.h header. These mock definitions
32 * are present in this file as a convenience to generate pretty-printed
33 * documentation that includes those definitions. */
34
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010035/** \defgroup platform Implementation-specific definitions
36 * @{
37 */
38
Gilles Peskineae32aac2018-11-30 14:39:32 +010039/** \brief Key handle.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040 *
Gilles Peskineae32aac2018-11-30 14:39:32 +010041 * This type represents open handles to keys. It must be an unsigned integral
Gilles Peskine308b91d2018-02-08 09:47:44 +010042 * type. The choice of type is implementation-dependent.
Gilles Peskineae32aac2018-11-30 14:39:32 +010043 *
Gilles Peskine23fd2bd2018-12-11 15:51:32 +010044 * 0 is not a valid key handle. How other handle values are assigned is
45 * implementation-dependent.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046 */
Gilles Peskineae32aac2018-11-30 14:39:32 +010047typedef _unsigned_integral_type_ psa_key_handle_t;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010049/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010050#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010051
Gilles Peskinee59236f2018-01-27 23:32:46 +010052#ifdef __cplusplus
53extern "C" {
54#endif
55
Gilles Peskinef3b731e2018-12-12 13:38:31 +010056/* The file "crypto_types.h" declares types that encode errors,
57 * algorithms, key types, policies, etc. */
58#include "crypto_types.h"
59
60/* The file "crypto_values.h" declares macros to build and analyze values
61 * of integral types defined in "crypto_types.h". */
62#include "crypto_values.h"
63
64/** \defgroup initialization Library initialization
Gilles Peskinee59236f2018-01-27 23:32:46 +010065 * @{
66 */
67
68/**
Gilles Peskinee59236f2018-01-27 23:32:46 +010069 * \brief Library initialization.
70 *
71 * Applications must call this function before calling any other
72 * function in this module.
73 *
74 * Applications may call this function more than once. Once a call
75 * succeeds, subsequent calls are guaranteed to succeed.
76 *
itayzafrir18617092018-09-16 12:22:41 +030077 * If the application calls other functions before calling psa_crypto_init(),
78 * the behavior is undefined. Implementations are encouraged to either perform
79 * the operation as if the library had been initialized or to return
80 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
81 * implementations should not return a success status if the lack of
82 * initialization may have security implications, for example due to improper
83 * seeding of the random number generator.
84 *
Gilles Peskine28538492018-07-11 17:34:00 +020085 * \retval #PSA_SUCCESS
86 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
87 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
88 * \retval #PSA_ERROR_HARDWARE_FAILURE
89 * \retval #PSA_ERROR_TAMPERING_DETECTED
90 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +010091 */
92psa_status_t psa_crypto_init(void);
93
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010094/**@}*/
95
96/** \defgroup key_management Key management
97 * @{
98 */
99
Gilles Peskineae32aac2018-11-30 14:39:32 +0100100/** \brief Retrieve the lifetime of an open key.
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100101 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100102 * \param handle Handle to query.
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100103 * \param[out] lifetime On success, the lifetime value.
104 *
105 * \retval #PSA_SUCCESS
106 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100107 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100108 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
109 * \retval #PSA_ERROR_HARDWARE_FAILURE
110 * \retval #PSA_ERROR_TAMPERING_DETECTED
111 * \retval #PSA_ERROR_BAD_STATE
112 * The library has not been previously initialized by psa_crypto_init().
113 * It is implementation-dependent whether a failure to initialize
114 * results in this error code.
115 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100116psa_status_t psa_get_key_lifetime(psa_key_handle_t handle,
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100117 psa_key_lifetime_t *lifetime);
118
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100119
Gilles Peskinef535eb22018-11-30 14:08:36 +0100120/** Allocate a key slot for a transient key, i.e. a key which is only stored
121 * in volatile memory.
122 *
123 * The allocated key slot and its handle remain valid until the
124 * application calls psa_close_key() or psa_destroy_key() or until the
125 * application terminates.
126 *
127 * This function takes a key type and maximum size as arguments so that
128 * the implementation can reserve a corresponding amount of memory.
129 * Implementations are not required to enforce this limit: if the application
130 * later tries to create a larger key or a key of a different type, it
131 * is implementation-defined whether this may succeed.
132 *
133 * \param type The type of key that the slot will contain.
134 * \param max_bits The maximum key size that the slot will contain.
135 * \param[out] handle On success, a handle to a volatile key slot.
136 *
137 * \retval #PSA_SUCCESS
138 * Success. The application can now use the value of `*handle`
139 * to access the newly allocated key slot.
140 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
141 * There was not enough memory, or the maximum number of key slots
142 * has been reached.
143 * \retval #PSA_ERROR_INVALID_ARGUMENT
144 * This implementation does not support this key type.
145 */
146
147psa_status_t psa_allocate_key(psa_key_type_t type,
148 size_t max_bits,
149 psa_key_handle_t *handle);
150
151/** Open a handle to an existing persistent key.
152 *
153 * Open a handle to a key which was previously created with psa_create_key().
154 *
155 * \param lifetime The lifetime of the key. This designates a storage
156 * area where the key material is stored. This must not
157 * be #PSA_KEY_LIFETIME_VOLATILE.
158 * \param id The persistent identifier of the key.
159 * \param[out] handle On success, a handle to a key slot which contains
160 * the data and metadata loaded from the specified
161 * persistent location.
162 *
163 * \retval #PSA_SUCCESS
164 * Success. The application can now use the value of `*handle`
165 * to access the newly allocated key slot.
166 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
167 * \retval #PSA_ERROR_EMPTY_SLOT
168 * \retval #PSA_ERROR_INVALID_ARGUMENT
169 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
170 * \retval #PSA_ERROR_INVALID_ARGUMENT
171 * \p id is invalid for the specified lifetime.
172 * \retval #PSA_ERROR_NOT_SUPPORTED
173 * \p lifetime is not supported.
174 * \retval #PSA_ERROR_NOT_PERMITTED
175 * The specified key exists, but the application does not have the
176 * permission to access it. Note that this specification does not
177 * define any way to create such a key, but it may be possible
178 * through implementation-specific means.
179 */
180psa_status_t psa_open_key(psa_key_lifetime_t lifetime,
181 psa_key_id_t id,
182 psa_key_handle_t *handle);
183
184/** Create a new persistent key slot.
185 *
186 * Create a new persistent key slot and return a handle to it. The handle
187 * remains valid until the application calls psa_close_key() or terminates.
188 * The application can open the key again with psa_open_key() until it
189 * removes the key by calling psa_destroy_key().
190 *
191 * \param lifetime The lifetime of the key. This designates a storage
192 * area where the key material is stored. This must not
193 * be #PSA_KEY_LIFETIME_VOLATILE.
194 * \param id The persistent identifier of the key.
195 * \param type The type of key that the slot will contain.
196 * \param max_bits The maximum key size that the slot will contain.
197 * \param[out] handle On success, a handle to the newly created key slot.
198 * When key material is later created in this key slot,
199 * it will be saved to the specified persistent location.
200 *
201 * \retval #PSA_SUCCESS
202 * Success. The application can now use the value of `*handle`
203 * to access the newly allocated key slot.
204 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
205 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
206 * \retval #PSA_ERROR_OCCUPIED_SLOT
207 * There is already a key with the identifier \p id in the storage
208 * area designated by \p lifetime.
209 * \retval #PSA_ERROR_INVALID_ARGUMENT
210 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
211 * \retval #PSA_ERROR_INVALID_ARGUMENT
212 * \p id is invalid for the specified lifetime.
213 * \retval #PSA_ERROR_NOT_SUPPORTED
214 * \p lifetime is not supported.
215 * \retval #PSA_ERROR_NOT_PERMITTED
216 * \p lifetime is valid, but the application does not have the
217 * permission to create a key there.
218 */
219psa_status_t psa_create_key(psa_key_lifetime_t lifetime,
220 psa_key_id_t id,
221 psa_key_type_t type,
222 size_t max_bits,
223 psa_key_handle_t *handle);
224
225/** Close a key handle.
226 *
227 * If the handle designates a volatile key, destroy the key material and
228 * free all associated resources, just like psa_destroy_key().
229 *
230 * If the handle designates a persistent key, free all resources associated
231 * with the key in volatile memory. The key slot in persistent storage is
232 * not affected and can be opened again later with psa_open_key().
233 *
234 * \param handle The key handle to close.
235 *
236 * \retval #PSA_SUCCESS
237 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100238 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100239 */
240psa_status_t psa_close_key(psa_key_handle_t handle);
241
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100242/**@}*/
243
244/** \defgroup import_export Key import and export
245 * @{
246 */
247
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100248/**
249 * \brief Import a key in binary format.
250 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100251 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100252 * documentation of psa_export_public_key() for the format of public keys
253 * and to the documentation of psa_export_key() for the format for
254 * other key types.
255 *
256 * This specification supports a single format for each key type.
257 * Implementations may support other formats as long as the standard
258 * format is supported. Implementations that support other formats
259 * should ensure that the formats are clearly unambiguous so as to
260 * minimize the risk that an invalid input is accidentally interpreted
261 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100262 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100263 * \param handle Handle to the slot where the key will be stored.
264 * This must be a valid slot for a key of the chosen
265 * type: it must have been obtained by calling
266 * psa_allocate_key() or psa_create_key() with the
267 * correct \p type and with a maximum size that is
268 * compatible with \p data.
Gilles Peskinef7933932018-10-31 14:07:52 +0100269 * \param type Key type (a \c PSA_KEY_TYPE_XXX value). On a successful
270 * import, the key slot will contain a key of this type.
271 * \param[in] data Buffer containing the key data. The content of this
272 * buffer is interpreted according to \p type. It must
273 * contain the format described in the documentation
274 * of psa_export_key() or psa_export_public_key() for
275 * the chosen type.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200276 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100277 *
Gilles Peskine28538492018-07-11 17:34:00 +0200278 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100279 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100280 * If the key is persistent, the key material and the key's metadata
281 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100282 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200283 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200284 * The key type or key size is not supported, either by the
285 * implementation in general or in this particular slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200286 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine308b91d2018-02-08 09:47:44 +0100287 * The key slot is invalid,
288 * or the key data is not correctly formatted.
Gilles Peskine28538492018-07-11 17:34:00 +0200289 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskine65eb8582018-04-19 08:28:58 +0200290 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200291 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
292 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
293 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100294 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200295 * \retval #PSA_ERROR_HARDWARE_FAILURE
296 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300297 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300298 * The library has not been previously initialized by psa_crypto_init().
299 * It is implementation-dependent whether a failure to initialize
300 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100301 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100302psa_status_t psa_import_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100303 psa_key_type_t type,
304 const uint8_t *data,
305 size_t data_length);
306
307/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100308 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200309 *
310 * This function destroys the content of the key slot from both volatile
311 * memory and, if applicable, non-volatile storage. Implementations shall
312 * make a best effort to ensure that any previous content of the slot is
313 * unrecoverable.
314 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100315 * This function also erases any metadata such as policies and frees all
316 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200317 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100318 * \param handle Handle to the key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100319 *
Gilles Peskine28538492018-07-11 17:34:00 +0200320 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200321 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200322 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200323 * The slot holds content and cannot be erased because it is
324 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100325 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200326 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200327 * There was an failure in communication with the cryptoprocessor.
328 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200329 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200330 * The storage is corrupted. Implementations shall make a best effort
331 * to erase key material even in this stage, however applications
332 * should be aware that it may be impossible to guarantee that the
333 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +0200334 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200335 * An unexpected condition which is not a storage corruption or
336 * a communication failure occurred. The cryptoprocessor may have
337 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300338 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300339 * The library has not been previously initialized by psa_crypto_init().
340 * It is implementation-dependent whether a failure to initialize
341 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100342 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100343psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100344
345/**
346 * \brief Get basic metadata about a key.
347 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100348 * \param handle Handle to the key slot to query.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200349 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100350 * This may be a null pointer, in which case the key type
351 * is not written.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200352 * \param[out] bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +0100353 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +0100354 * is not written.
355 *
Gilles Peskine28538492018-07-11 17:34:00 +0200356 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100357 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200358 * \retval #PSA_ERROR_EMPTY_SLOT
Gilles Peskineae32aac2018-11-30 14:39:32 +0100359 * The handle is to a key slot which does not contain key material yet.
Gilles Peskine28538492018-07-11 17:34:00 +0200360 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
361 * \retval #PSA_ERROR_HARDWARE_FAILURE
362 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300363 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300364 * The library has not been previously initialized by psa_crypto_init().
365 * It is implementation-dependent whether a failure to initialize
366 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100367 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100368psa_status_t psa_get_key_information(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100369 psa_key_type_t *type,
370 size_t *bits);
371
372/**
373 * \brief Export a key in binary format.
374 *
375 * The output of this function can be passed to psa_import_key() to
376 * create an equivalent object.
377 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100378 * If the implementation of psa_import_key() supports other formats
379 * beyond the format specified here, the output from psa_export_key()
380 * must use the representation specified here, not the original
381 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100382 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100383 * For standard key types, the output format is as follows:
384 *
385 * - For symmetric keys (including MAC keys), the format is the
386 * raw bytes of the key.
387 * - For DES, the key data consists of 8 bytes. The parity bits must be
388 * correct.
389 * - For Triple-DES, the format is the concatenation of the
390 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100391 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200392 * is the non-encrypted DER encoding of the representation defined by
393 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
394 * ```
395 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200396 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200397 * modulus INTEGER, -- n
398 * publicExponent INTEGER, -- e
399 * privateExponent INTEGER, -- d
400 * prime1 INTEGER, -- p
401 * prime2 INTEGER, -- q
402 * exponent1 INTEGER, -- d mod (p-1)
403 * exponent2 INTEGER, -- d mod (q-1)
404 * coefficient INTEGER, -- (inverse of q) mod p
405 * }
406 * ```
407 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format
408 * is the non-encrypted DER encoding of the representation used by
Gilles Peskinec6290c02018-08-13 17:24:59 +0200409 * OpenSSL and OpenSSH, whose structure is described in ASN.1 as follows:
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200410 * ```
411 * DSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200412 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200413 * prime INTEGER, -- p
414 * subprime INTEGER, -- q
415 * generator INTEGER, -- g
416 * public INTEGER, -- y
417 * private INTEGER, -- x
418 * }
419 * ```
420 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +0100421 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100422 * a representation of the private value as a `ceiling(m/8)`-byte string
423 * where `m` is the bit size associated with the curve, i.e. the bit size
424 * of the order of the curve's coordinate field. This byte string is
425 * in little-endian order for Montgomery curves (curve types
426 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
427 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
428 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100429 * This is the content of the `privateKey` field of the `ECPrivateKey`
430 * format defined by RFC 5915.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200431 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
432 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100433 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100434 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200435 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200436 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200437 * \param[out] data_length On success, the number of bytes
438 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100439 *
Gilles Peskine28538492018-07-11 17:34:00 +0200440 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100441 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200442 * \retval #PSA_ERROR_EMPTY_SLOT
443 * \retval #PSA_ERROR_NOT_PERMITTED
Darryl Green9e2d7a02018-07-24 16:33:30 +0100444 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200445 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
446 * The size of the \p data buffer is too small. You can determine a
447 * sufficient buffer size by calling
448 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
449 * where \c type is the key type
450 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200451 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
452 * \retval #PSA_ERROR_HARDWARE_FAILURE
453 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300454 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300455 * The library has not been previously initialized by psa_crypto_init().
456 * It is implementation-dependent whether a failure to initialize
457 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100458 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100459psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100460 uint8_t *data,
461 size_t data_size,
462 size_t *data_length);
463
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100464/**
465 * \brief Export a public key or the public part of a key pair in binary format.
466 *
467 * The output of this function can be passed to psa_import_key() to
468 * create an object that is equivalent to the public key.
469 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000470 * This specification supports a single format for each key type.
471 * Implementations may support other formats as long as the standard
472 * format is supported. Implementations that support other formats
473 * should ensure that the formats are clearly unambiguous so as to
474 * minimize the risk that an invalid input is accidentally interpreted
475 * according to a different format.
476 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000477 * For standard key types, the output format is as follows:
478 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
479 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
480 * ```
481 * RSAPublicKey ::= SEQUENCE {
482 * modulus INTEGER, -- n
483 * publicExponent INTEGER } -- e
484 * ```
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000485 * - For elliptic curve public keys (key types for which
486 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true), the format is the uncompressed
487 * representation defined by SEC1 &sect;2.3.3 as the content of an ECPoint.
488 * Let `m` be the bit size associated with the curve, i.e. the bit size of
489 * `q` for a curve over `F_q`. The representation consists of:
490 * - The byte 0x04;
491 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
492 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Jaeden Amero6b196002019-01-10 10:23:21 +0000493 *
494 * For other public key types, the format is the DER representation defined by
495 * RFC 5280 as `SubjectPublicKeyInfo`, with the `subjectPublicKey` format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200496 * specified below.
497 * ```
498 * SubjectPublicKeyInfo ::= SEQUENCE {
499 * algorithm AlgorithmIdentifier,
500 * subjectPublicKey BIT STRING }
501 * AlgorithmIdentifier ::= SEQUENCE {
502 * algorithm OBJECT IDENTIFIER,
503 * parameters ANY DEFINED BY algorithm OPTIONAL }
504 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200505 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY),
506 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.2 as
507 * `DSAPublicKey`,
508 * with the OID `id-dsa`,
509 * and with the parameters `DSS-Parms`.
510 * ```
511 * id-dsa OBJECT IDENTIFIER ::= {
512 * iso(1) member-body(2) us(840) x9-57(10040) x9cm(4) 1 }
513 *
514 * Dss-Parms ::= SEQUENCE {
515 * p INTEGER,
516 * q INTEGER,
517 * g INTEGER }
518 * DSAPublicKey ::= INTEGER -- public key, Y
519 * ```
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100520 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100521 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200522 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200523 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200524 * \param[out] data_length On success, the number of bytes
525 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100526 *
Gilles Peskine28538492018-07-11 17:34:00 +0200527 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100528 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200529 * \retval #PSA_ERROR_EMPTY_SLOT
530 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200531 * The key is neither a public key nor a key pair.
532 * \retval #PSA_ERROR_NOT_SUPPORTED
533 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
534 * The size of the \p data buffer is too small. You can determine a
535 * sufficient buffer size by calling
536 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
537 * where \c type is the key type
538 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200539 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
540 * \retval #PSA_ERROR_HARDWARE_FAILURE
541 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300542 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300543 * The library has not been previously initialized by psa_crypto_init().
544 * It is implementation-dependent whether a failure to initialize
545 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100546 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100547psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100548 uint8_t *data,
549 size_t data_size,
550 size_t *data_length);
551
552/**@}*/
553
554/** \defgroup policy Key policies
555 * @{
556 */
557
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100558/** The type of the key policy data structure.
559 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000560 * Before calling any function on a key policy, the application must initialize
561 * it by any of the following means:
562 * - Set the structure to all-bits-zero, for example:
563 * \code
564 * psa_key_policy_t policy;
565 * memset(&policy, 0, sizeof(policy));
566 * \endcode
567 * - Initialize the structure to logical zero values, for example:
568 * \code
569 * psa_key_policy_t policy = {0};
570 * \endcode
571 * - Initialize the structure to the initializer #PSA_KEY_POLICY_INIT,
572 * for example:
573 * \code
574 * psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
575 * \endcode
576 * - Assign the result of the function psa_key_policy_init()
577 * to the structure, for example:
578 * \code
579 * psa_key_policy_t policy;
580 * policy = psa_key_policy_init();
581 * \endcode
582 *
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100583 * This is an implementation-defined \c struct. Applications should not
584 * make any assumptions about the content of this structure except
585 * as directed by the documentation of a specific implementation. */
586typedef struct psa_key_policy_s psa_key_policy_t;
587
Jaeden Amero70261c52019-01-04 11:47:20 +0000588/** \def PSA_KEY_POLICY_INIT
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200589 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000590 * This macro returns a suitable initializer for a key policy object of type
591 * #psa_key_policy_t.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200592 */
Jaeden Amero70261c52019-01-04 11:47:20 +0000593#ifdef __DOXYGEN_ONLY__
594/* This is an example definition for documentation purposes.
595 * Implementations should define a suitable value in `crypto_struct.h`.
596 */
597#define PSA_KEY_POLICY_INIT {0}
598#endif
599
600/** Return an initial value for a key policy that forbids all usage of the key.
601 */
602static psa_key_policy_t psa_key_policy_init(void);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100603
Gilles Peskine7e198532018-03-08 07:50:30 +0100604/** \brief Set the standard fields of a policy structure.
605 *
606 * Note that this function does not make any consistency check of the
607 * parameters. The values are only checked when applying the policy to
608 * a key slot with psa_set_key_policy().
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200609 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000610 * \param[in,out] policy The key policy to modify. It must have been
611 * initialized as per the documentation for
612 * #psa_key_policy_t.
613 * \param usage The permitted uses for the key.
614 * \param alg The algorithm that the key may be used for.
Gilles Peskine7e198532018-03-08 07:50:30 +0100615 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100616void psa_key_policy_set_usage(psa_key_policy_t *policy,
617 psa_key_usage_t usage,
618 psa_algorithm_t alg);
619
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200620/** \brief Retrieve the usage field of a policy structure.
621 *
622 * \param[in] policy The policy object to query.
623 *
624 * \return The permitted uses for a key with this policy.
625 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +0200626psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100627
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200628/** \brief Retrieve the algorithm field of a policy structure.
629 *
630 * \param[in] policy The policy object to query.
631 *
632 * \return The permitted algorithm for a key with this policy.
633 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +0200634psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100635
636/** \brief Set the usage policy on a key slot.
637 *
638 * This function must be called on an empty key slot, before importing,
639 * generating or creating a key in the slot. Changing the policy of an
640 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +0100641 *
642 * Implementations may set restrictions on supported key policies
643 * depending on the key type and the key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200644 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100645 * \param handle Handle to the key whose policy is to be changed.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200646 * \param[in] policy The policy object to query.
647 *
648 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100649 * Success.
650 * If the key is persistent, it is implementation-defined whether
651 * the policy has been saved to persistent storage. Implementations
652 * may defer saving the policy until the key material is created.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100653 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200654 * \retval #PSA_ERROR_OCCUPIED_SLOT
655 * \retval #PSA_ERROR_NOT_SUPPORTED
656 * \retval #PSA_ERROR_INVALID_ARGUMENT
657 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
658 * \retval #PSA_ERROR_HARDWARE_FAILURE
659 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300660 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300661 * The library has not been previously initialized by psa_crypto_init().
662 * It is implementation-dependent whether a failure to initialize
663 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100664 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100665psa_status_t psa_set_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100666 const psa_key_policy_t *policy);
667
Gilles Peskine7e198532018-03-08 07:50:30 +0100668/** \brief Get the usage policy for a key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200669 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100670 * \param handle Handle to the key slot whose policy is being queried.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200671 * \param[out] policy On success, the key's policy.
672 *
673 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100674 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200675 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
676 * \retval #PSA_ERROR_HARDWARE_FAILURE
677 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300678 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300679 * The library has not been previously initialized by psa_crypto_init().
680 * It is implementation-dependent whether a failure to initialize
681 * results in this error code.
Gilles Peskine7e198532018-03-08 07:50:30 +0100682 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100683psa_status_t psa_get_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100684 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +0100685
686/**@}*/
687
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100688/** \defgroup hash Message digests
689 * @{
690 */
691
Gilles Peskine308b91d2018-02-08 09:47:44 +0100692/** The type of the state data structure for multipart hash operations.
693 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000694 * Before calling any function on a hash operation object, the application must
695 * initialize it by any of the following means:
696 * - Set the structure to all-bits-zero, for example:
697 * \code
698 * psa_hash_operation_t operation;
699 * memset(&operation, 0, sizeof(operation));
700 * \endcode
701 * - Initialize the structure to logical zero values, for example:
702 * \code
703 * psa_hash_operation_t operation = {0};
704 * \endcode
705 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
706 * for example:
707 * \code
708 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
709 * \endcode
710 * - Assign the result of the function psa_hash_operation_init()
711 * to the structure, for example:
712 * \code
713 * psa_hash_operation_t operation;
714 * operation = psa_hash_operation_init();
715 * \endcode
716 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100717 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100718 * make any assumptions about the content of this structure except
719 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100720typedef struct psa_hash_operation_s psa_hash_operation_t;
721
Jaeden Amero6a25b412019-01-04 11:47:44 +0000722/** \def PSA_HASH_OPERATION_INIT
723 *
724 * This macro returns a suitable initializer for a hash operation object
725 * of type #psa_hash_operation_t.
726 */
727#ifdef __DOXYGEN_ONLY__
728/* This is an example definition for documentation purposes.
729 * Implementations should define a suitable value in `crypto_struct.h`.
730 */
731#define PSA_HASH_OPERATION_INIT {0}
732#endif
733
734/** Return an initial value for a hash operation object.
735 */
736static psa_hash_operation_t psa_hash_operation_init(void);
737
Gilles Peskine308b91d2018-02-08 09:47:44 +0100738/** Start a multipart hash operation.
739 *
740 * The sequence of operations to calculate a hash (message digest)
741 * is as follows:
742 * -# Allocate an operation object which will be passed to all the functions
743 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000744 * -# Initialize the operation object with one of the methods described in the
745 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200746 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100747 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100748 * of the message each time. The hash that is calculated is the hash
749 * of the concatenation of these messages in order.
750 * -# To calculate the hash, call psa_hash_finish().
751 * To compare the hash with an expected value, call psa_hash_verify().
752 *
753 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000754 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100755 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200756 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100757 * eventually terminate the operation. The following events terminate an
758 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100759 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100760 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100761 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000762 * \param[in,out] operation The operation object to set up. It must have
763 * been initialized as per the documentation for
764 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200765 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
766 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100767 *
Gilles Peskine28538492018-07-11 17:34:00 +0200768 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100769 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200770 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200771 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +0200772 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
773 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
774 * \retval #PSA_ERROR_HARDWARE_FAILURE
775 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100776 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200777psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100778 psa_algorithm_t alg);
779
Gilles Peskine308b91d2018-02-08 09:47:44 +0100780/** Add a message fragment to a multipart hash operation.
781 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200782 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100783 *
784 * If this function returns an error status, the operation becomes inactive.
785 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200786 * \param[in,out] operation Active hash operation.
787 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200788 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100789 *
Gilles Peskine28538492018-07-11 17:34:00 +0200790 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100791 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200792 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100793 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200794 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
795 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
796 * \retval #PSA_ERROR_HARDWARE_FAILURE
797 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100798 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100799psa_status_t psa_hash_update(psa_hash_operation_t *operation,
800 const uint8_t *input,
801 size_t input_length);
802
Gilles Peskine308b91d2018-02-08 09:47:44 +0100803/** Finish the calculation of the hash of a message.
804 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200805 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100806 * This function calculates the hash of the message formed by concatenating
807 * the inputs passed to preceding calls to psa_hash_update().
808 *
809 * When this function returns, the operation becomes inactive.
810 *
811 * \warning Applications should not call this function if they expect
812 * a specific value for the hash. Call psa_hash_verify() instead.
813 * Beware that comparing integrity or authenticity data such as
814 * hash values with a function such as \c memcmp is risky
815 * because the time taken by the comparison may leak information
816 * about the hashed data which could allow an attacker to guess
817 * a valid hash and thereby bypass security controls.
818 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200819 * \param[in,out] operation Active hash operation.
820 * \param[out] hash Buffer where the hash is to be written.
821 * \param hash_size Size of the \p hash buffer in bytes.
822 * \param[out] hash_length On success, the number of bytes
823 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +0200824 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +0200825 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100826 *
Gilles Peskine28538492018-07-11 17:34:00 +0200827 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100828 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200829 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100830 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200831 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200832 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200833 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100834 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +0200835 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
836 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
837 * \retval #PSA_ERROR_HARDWARE_FAILURE
838 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100839 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100840psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
841 uint8_t *hash,
842 size_t hash_size,
843 size_t *hash_length);
844
Gilles Peskine308b91d2018-02-08 09:47:44 +0100845/** Finish the calculation of the hash of a message and compare it with
846 * an expected value.
847 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200848 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100849 * This function calculates the hash of the message formed by concatenating
850 * the inputs passed to preceding calls to psa_hash_update(). It then
851 * compares the calculated hash with the expected hash passed as a
852 * parameter to this function.
853 *
854 * When this function returns, the operation becomes inactive.
855 *
Gilles Peskine19067982018-03-20 17:54:53 +0100856 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +0100857 * comparison between the actual hash and the expected hash is performed
858 * in constant time.
859 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200860 * \param[in,out] operation Active hash operation.
861 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200862 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100863 *
Gilles Peskine28538492018-07-11 17:34:00 +0200864 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100865 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +0200866 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100867 * The hash of the message was calculated successfully, but it
868 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +0200869 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100870 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200871 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
872 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
873 * \retval #PSA_ERROR_HARDWARE_FAILURE
874 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100875 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100876psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
877 const uint8_t *hash,
878 size_t hash_length);
879
Gilles Peskine308b91d2018-02-08 09:47:44 +0100880/** Abort a hash operation.
881 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100882 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200883 * \p operation structure itself. Once aborted, the operation object
884 * can be reused for another operation by calling
885 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100886 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200887 * You may call this function any time after the operation object has
888 * been initialized by any of the following methods:
889 * - A call to psa_hash_setup(), whether it succeeds or not.
890 * - Initializing the \c struct to all-bits-zero.
891 * - Initializing the \c struct to logical zeros, e.g.
892 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100893 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200894 * In particular, calling psa_hash_abort() after the operation has been
895 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
896 * psa_hash_verify() is safe and has no effect.
897 *
898 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100899 *
Gilles Peskine28538492018-07-11 17:34:00 +0200900 * \retval #PSA_SUCCESS
901 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200902 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +0200903 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
904 * \retval #PSA_ERROR_HARDWARE_FAILURE
905 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100906 */
907psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100908
909/**@}*/
910
Gilles Peskine8c9def32018-02-08 10:02:12 +0100911/** \defgroup MAC Message authentication codes
912 * @{
913 */
914
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100915/** The type of the state data structure for multipart MAC operations.
916 *
Jaeden Amero769ce272019-01-04 11:48:03 +0000917 * Before calling any function on a MAC operation object, the application must
918 * initialize it by any of the following means:
919 * - Set the structure to all-bits-zero, for example:
920 * \code
921 * psa_mac_operation_t operation;
922 * memset(&operation, 0, sizeof(operation));
923 * \endcode
924 * - Initialize the structure to logical zero values, for example:
925 * \code
926 * psa_mac_operation_t operation = {0};
927 * \endcode
928 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
929 * for example:
930 * \code
931 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
932 * \endcode
933 * - Assign the result of the function psa_mac_operation_init()
934 * to the structure, for example:
935 * \code
936 * psa_mac_operation_t operation;
937 * operation = psa_mac_operation_init();
938 * \endcode
939 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100940 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100941 * make any assumptions about the content of this structure except
942 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100943typedef struct psa_mac_operation_s psa_mac_operation_t;
944
Jaeden Amero769ce272019-01-04 11:48:03 +0000945/** \def PSA_MAC_OPERATION_INIT
946 *
947 * This macro returns a suitable initializer for a MAC operation object of type
948 * #psa_mac_operation_t.
949 */
950#ifdef __DOXYGEN_ONLY__
951/* This is an example definition for documentation purposes.
952 * Implementations should define a suitable value in `crypto_struct.h`.
953 */
954#define PSA_MAC_OPERATION_INIT {0}
955#endif
956
957/** Return an initial value for a MAC operation object.
958 */
959static psa_mac_operation_t psa_mac_operation_init(void);
960
Gilles Peskine89167cb2018-07-08 20:12:23 +0200961/** Start a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100962 *
Gilles Peskine89167cb2018-07-08 20:12:23 +0200963 * This function sets up the calculation of the MAC
964 * (message authentication code) of a byte string.
965 * To verify the MAC of a message against an
966 * expected value, use psa_mac_verify_setup() instead.
967 *
968 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100969 * -# Allocate an operation object which will be passed to all the functions
970 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +0000971 * -# Initialize the operation object with one of the methods described in the
972 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +0200973 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100974 * The key remains associated with the operation even if the content
975 * of the key slot changes.
976 * -# Call psa_mac_update() zero, one or more times, passing a fragment
977 * of the message each time. The MAC that is calculated is the MAC
978 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +0200979 * -# At the end of the message, call psa_mac_sign_finish() to finish
980 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100981 *
982 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +0000983 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100984 *
Gilles Peskine89167cb2018-07-08 20:12:23 +0200985 * After a successful call to psa_mac_sign_setup(), the application must
986 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100987 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +0200988 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100989 *
Jaeden Amero769ce272019-01-04 11:48:03 +0000990 * \param[in,out] operation The operation object to set up. It must have
991 * been initialized as per the documentation for
992 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100993 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200994 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
995 * such that #PSA_ALG_IS_MAC(alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100996 *
Gilles Peskine28538492018-07-11 17:34:00 +0200997 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100998 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100999 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001000 * \retval #PSA_ERROR_EMPTY_SLOT
1001 * \retval #PSA_ERROR_NOT_PERMITTED
1002 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001003 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001004 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001005 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001006 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1007 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1008 * \retval #PSA_ERROR_HARDWARE_FAILURE
1009 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001010 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001011 * The library has not been previously initialized by psa_crypto_init().
1012 * It is implementation-dependent whether a failure to initialize
1013 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001014 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001015psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001016 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001017 psa_algorithm_t alg);
1018
1019/** Start a multipart MAC verification operation.
1020 *
1021 * This function sets up the verification of the MAC
1022 * (message authentication code) of a byte string against an expected value.
1023 *
1024 * The sequence of operations to verify a MAC is as follows:
1025 * -# Allocate an operation object which will be passed to all the functions
1026 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001027 * -# Initialize the operation object with one of the methods described in the
1028 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001029 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
1030 * The key remains associated with the operation even if the content
1031 * of the key slot changes.
1032 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1033 * of the message each time. The MAC that is calculated is the MAC
1034 * of the concatenation of these messages in order.
1035 * -# At the end of the message, call psa_mac_verify_finish() to finish
1036 * calculating the actual MAC of the message and verify it against
1037 * the expected value.
1038 *
1039 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001040 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001041 *
1042 * After a successful call to psa_mac_verify_setup(), the application must
1043 * eventually terminate the operation through one of the following methods:
1044 * - A failed call to psa_mac_update().
1045 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1046 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001047 * \param[in,out] operation The operation object to set up. It must have
1048 * been initialized as per the documentation for
1049 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001050 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001051 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1052 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001053 *
Gilles Peskine28538492018-07-11 17:34:00 +02001054 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001055 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001056 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001057 * \retval #PSA_ERROR_EMPTY_SLOT
1058 * \retval #PSA_ERROR_NOT_PERMITTED
1059 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001060 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001061 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001062 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001063 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1064 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1065 * \retval #PSA_ERROR_HARDWARE_FAILURE
1066 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001067 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001068 * The library has not been previously initialized by psa_crypto_init().
1069 * It is implementation-dependent whether a failure to initialize
1070 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001071 */
1072psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001073 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001074 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001075
Gilles Peskinedcd14942018-07-12 00:30:52 +02001076/** Add a message fragment to a multipart MAC operation.
1077 *
1078 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1079 * before calling this function.
1080 *
1081 * If this function returns an error status, the operation becomes inactive.
1082 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001083 * \param[in,out] operation Active MAC operation.
1084 * \param[in] input Buffer containing the message fragment to add to
1085 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001086 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001087 *
1088 * \retval #PSA_SUCCESS
1089 * Success.
1090 * \retval #PSA_ERROR_BAD_STATE
1091 * The operation state is not valid (not started, or already completed).
1092 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1093 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1094 * \retval #PSA_ERROR_HARDWARE_FAILURE
1095 * \retval #PSA_ERROR_TAMPERING_DETECTED
1096 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001097psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1098 const uint8_t *input,
1099 size_t input_length);
1100
Gilles Peskinedcd14942018-07-12 00:30:52 +02001101/** Finish the calculation of the MAC of a message.
1102 *
1103 * The application must call psa_mac_sign_setup() before calling this function.
1104 * This function calculates the MAC of the message formed by concatenating
1105 * the inputs passed to preceding calls to psa_mac_update().
1106 *
1107 * When this function returns, the operation becomes inactive.
1108 *
1109 * \warning Applications should not call this function if they expect
1110 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1111 * Beware that comparing integrity or authenticity data such as
1112 * MAC values with a function such as \c memcmp is risky
1113 * because the time taken by the comparison may leak information
1114 * about the MAC value which could allow an attacker to guess
1115 * a valid MAC and thereby bypass security controls.
1116 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001117 * \param[in,out] operation Active MAC operation.
1118 * \param[out] mac Buffer where the MAC value is to be written.
1119 * \param mac_size Size of the \p mac buffer in bytes.
1120 * \param[out] mac_length On success, the number of bytes
1121 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001122 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001123 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001124 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001125 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001126 *
1127 * \retval #PSA_SUCCESS
1128 * Success.
1129 * \retval #PSA_ERROR_BAD_STATE
1130 * The operation state is not valid (not started, or already completed).
1131 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001132 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001133 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1134 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1135 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1136 * \retval #PSA_ERROR_HARDWARE_FAILURE
1137 * \retval #PSA_ERROR_TAMPERING_DETECTED
1138 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001139psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1140 uint8_t *mac,
1141 size_t mac_size,
1142 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001143
Gilles Peskinedcd14942018-07-12 00:30:52 +02001144/** Finish the calculation of the MAC of a message and compare it with
1145 * an expected value.
1146 *
1147 * The application must call psa_mac_verify_setup() before calling this function.
1148 * This function calculates the MAC of the message formed by concatenating
1149 * the inputs passed to preceding calls to psa_mac_update(). It then
1150 * compares the calculated MAC with the expected MAC passed as a
1151 * parameter to this function.
1152 *
1153 * When this function returns, the operation becomes inactive.
1154 *
1155 * \note Implementations shall make the best effort to ensure that the
1156 * comparison between the actual MAC and the expected MAC is performed
1157 * in constant time.
1158 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001159 * \param[in,out] operation Active MAC operation.
1160 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001161 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001162 *
1163 * \retval #PSA_SUCCESS
1164 * The expected MAC is identical to the actual MAC of the message.
1165 * \retval #PSA_ERROR_INVALID_SIGNATURE
1166 * The MAC of the message was calculated successfully, but it
1167 * differs from the expected MAC.
1168 * \retval #PSA_ERROR_BAD_STATE
1169 * The operation state is not valid (not started, or already completed).
1170 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1171 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1172 * \retval #PSA_ERROR_HARDWARE_FAILURE
1173 * \retval #PSA_ERROR_TAMPERING_DETECTED
1174 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001175psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1176 const uint8_t *mac,
1177 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001178
Gilles Peskinedcd14942018-07-12 00:30:52 +02001179/** Abort a MAC operation.
1180 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001181 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001182 * \p operation structure itself. Once aborted, the operation object
1183 * can be reused for another operation by calling
1184 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001185 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001186 * You may call this function any time after the operation object has
1187 * been initialized by any of the following methods:
1188 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1189 * it succeeds or not.
1190 * - Initializing the \c struct to all-bits-zero.
1191 * - Initializing the \c struct to logical zeros, e.g.
1192 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001193 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001194 * In particular, calling psa_mac_abort() after the operation has been
1195 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1196 * psa_mac_verify_finish() is safe and has no effect.
1197 *
1198 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001199 *
1200 * \retval #PSA_SUCCESS
1201 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001202 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001203 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1204 * \retval #PSA_ERROR_HARDWARE_FAILURE
1205 * \retval #PSA_ERROR_TAMPERING_DETECTED
1206 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001207psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1208
1209/**@}*/
1210
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001211/** \defgroup cipher Symmetric ciphers
1212 * @{
1213 */
1214
1215/** The type of the state data structure for multipart cipher operations.
1216 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001217 * Before calling any function on a cipher operation object, the application
1218 * must initialize it by any of the following means:
1219 * - Set the structure to all-bits-zero, for example:
1220 * \code
1221 * psa_cipher_operation_t operation;
1222 * memset(&operation, 0, sizeof(operation));
1223 * \endcode
1224 * - Initialize the structure to logical zero values, for example:
1225 * \code
1226 * psa_cipher_operation_t operation = {0};
1227 * \endcode
1228 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1229 * for example:
1230 * \code
1231 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1232 * \endcode
1233 * - Assign the result of the function psa_cipher_operation_init()
1234 * to the structure, for example:
1235 * \code
1236 * psa_cipher_operation_t operation;
1237 * operation = psa_cipher_operation_init();
1238 * \endcode
1239 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001240 * This is an implementation-defined \c struct. Applications should not
1241 * make any assumptions about the content of this structure except
1242 * as directed by the documentation of a specific implementation. */
1243typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1244
Jaeden Amero5bae2272019-01-04 11:48:27 +00001245/** \def PSA_CIPHER_OPERATION_INIT
1246 *
1247 * This macro returns a suitable initializer for a cipher operation object of
1248 * type #psa_cipher_operation_t.
1249 */
1250#ifdef __DOXYGEN_ONLY__
1251/* This is an example definition for documentation purposes.
1252 * Implementations should define a suitable value in `crypto_struct.h`.
1253 */
1254#define PSA_CIPHER_OPERATION_INIT {0}
1255#endif
1256
1257/** Return an initial value for a cipher operation object.
1258 */
1259static psa_cipher_operation_t psa_cipher_operation_init(void);
1260
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001261/** Set the key for a multipart symmetric encryption operation.
1262 *
1263 * The sequence of operations to encrypt a message with a symmetric cipher
1264 * is as follows:
1265 * -# Allocate an operation object which will be passed to all the functions
1266 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001267 * -# Initialize the operation object with one of the methods described in the
1268 * documentation for #psa_cipher_operation_t, e.g.
1269 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001270 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001271 * The key remains associated with the operation even if the content
1272 * of the key slot changes.
itayzafrired7382f2018-08-02 14:19:33 +03001273 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001274 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001275 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001276 * requires a specific IV value.
1277 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1278 * of the message each time.
1279 * -# Call psa_cipher_finish().
1280 *
1281 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001282 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001283 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001284 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001285 * eventually terminate the operation. The following events terminate an
1286 * operation:
itayzafrired7382f2018-08-02 14:19:33 +03001287 * - A failed call to psa_cipher_generate_iv(), psa_cipher_set_iv()
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001288 * or psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001289 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001290 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001291 * \param[in,out] operation The operation object to set up. It must have
1292 * been initialized as per the documentation for
1293 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001294 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001295 * \param alg The cipher algorithm to compute
1296 * (\c PSA_ALG_XXX value such that
1297 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001298 *
Gilles Peskine28538492018-07-11 17:34:00 +02001299 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001300 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001301 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001302 * \retval #PSA_ERROR_EMPTY_SLOT
1303 * \retval #PSA_ERROR_NOT_PERMITTED
1304 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001305 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001306 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001307 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001308 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1309 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1310 * \retval #PSA_ERROR_HARDWARE_FAILURE
1311 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001312 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001313 * The library has not been previously initialized by psa_crypto_init().
1314 * It is implementation-dependent whether a failure to initialize
1315 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001316 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001317psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001318 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001319 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001320
1321/** Set the key for a multipart symmetric decryption operation.
1322 *
1323 * The sequence of operations to decrypt a message with a symmetric cipher
1324 * is as follows:
1325 * -# Allocate an operation object which will be passed to all the functions
1326 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001327 * -# Initialize the operation object with one of the methods described in the
1328 * documentation for #psa_cipher_operation_t, e.g.
1329 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001330 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001331 * The key remains associated with the operation even if the content
1332 * of the key slot changes.
1333 * -# Call psa_cipher_update() with the IV (initialization vector) for the
1334 * decryption. If the IV is prepended to the ciphertext, you can call
1335 * psa_cipher_update() on a buffer containing the IV followed by the
1336 * beginning of the message.
1337 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1338 * of the message each time.
1339 * -# Call psa_cipher_finish().
1340 *
1341 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001342 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001343 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001344 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001345 * eventually terminate the operation. The following events terminate an
1346 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001347 * - A failed call to psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001348 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001349 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001350 * \param[in,out] operation The operation object to set up. It must have
1351 * been initialized as per the documentation for
1352 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001353 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001354 * \param alg The cipher algorithm to compute
1355 * (\c PSA_ALG_XXX value such that
1356 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001357 *
Gilles Peskine28538492018-07-11 17:34:00 +02001358 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001359 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001360 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001361 * \retval #PSA_ERROR_EMPTY_SLOT
1362 * \retval #PSA_ERROR_NOT_PERMITTED
1363 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001364 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001365 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001366 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001367 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1368 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1369 * \retval #PSA_ERROR_HARDWARE_FAILURE
1370 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001371 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001372 * The library has not been previously initialized by psa_crypto_init().
1373 * It is implementation-dependent whether a failure to initialize
1374 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001375 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001376psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001377 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001378 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001379
Gilles Peskinedcd14942018-07-12 00:30:52 +02001380/** Generate an IV for a symmetric encryption operation.
1381 *
1382 * This function generates a random IV (initialization vector), nonce
1383 * or initial counter value for the encryption operation as appropriate
1384 * for the chosen algorithm, key type and key size.
1385 *
1386 * The application must call psa_cipher_encrypt_setup() before
1387 * calling this function.
1388 *
1389 * If this function returns an error status, the operation becomes inactive.
1390 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001391 * \param[in,out] operation Active cipher operation.
1392 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001393 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001394 * \param[out] iv_length On success, the number of bytes of the
1395 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001396 *
1397 * \retval #PSA_SUCCESS
1398 * Success.
1399 * \retval #PSA_ERROR_BAD_STATE
1400 * The operation state is not valid (not started, or IV already set).
1401 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001402 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001403 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1404 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1405 * \retval #PSA_ERROR_HARDWARE_FAILURE
1406 * \retval #PSA_ERROR_TAMPERING_DETECTED
1407 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001408psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1409 unsigned char *iv,
1410 size_t iv_size,
1411 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001412
Gilles Peskinedcd14942018-07-12 00:30:52 +02001413/** Set the IV for a symmetric encryption or decryption operation.
1414 *
1415 * This function sets the random IV (initialization vector), nonce
1416 * or initial counter value for the encryption or decryption operation.
1417 *
1418 * The application must call psa_cipher_encrypt_setup() before
1419 * calling this function.
1420 *
1421 * If this function returns an error status, the operation becomes inactive.
1422 *
1423 * \note When encrypting, applications should use psa_cipher_generate_iv()
1424 * instead of this function, unless implementing a protocol that requires
1425 * a non-random IV.
1426 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001427 * \param[in,out] operation Active cipher operation.
1428 * \param[in] iv Buffer containing the IV to use.
1429 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001430 *
1431 * \retval #PSA_SUCCESS
1432 * Success.
1433 * \retval #PSA_ERROR_BAD_STATE
1434 * The operation state is not valid (not started, or IV already set).
1435 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001436 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001437 * or the chosen algorithm does not use an IV.
1438 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1439 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1440 * \retval #PSA_ERROR_HARDWARE_FAILURE
1441 * \retval #PSA_ERROR_TAMPERING_DETECTED
1442 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001443psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1444 const unsigned char *iv,
1445 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001446
Gilles Peskinedcd14942018-07-12 00:30:52 +02001447/** Encrypt or decrypt a message fragment in an active cipher operation.
1448 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001449 * Before calling this function, you must:
1450 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1451 * The choice of setup function determines whether this function
1452 * encrypts or decrypts its input.
1453 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1454 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001455 *
1456 * If this function returns an error status, the operation becomes inactive.
1457 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001458 * \param[in,out] operation Active cipher operation.
1459 * \param[in] input Buffer containing the message fragment to
1460 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001461 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001462 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001463 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001464 * \param[out] output_length On success, the number of bytes
1465 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001466 *
1467 * \retval #PSA_SUCCESS
1468 * Success.
1469 * \retval #PSA_ERROR_BAD_STATE
1470 * The operation state is not valid (not started, IV required but
1471 * not set, or already completed).
1472 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1473 * The size of the \p output buffer is too small.
1474 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1475 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1476 * \retval #PSA_ERROR_HARDWARE_FAILURE
1477 * \retval #PSA_ERROR_TAMPERING_DETECTED
1478 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001479psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1480 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001481 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001482 unsigned char *output,
1483 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001484 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001485
Gilles Peskinedcd14942018-07-12 00:30:52 +02001486/** Finish encrypting or decrypting a message in a cipher operation.
1487 *
1488 * The application must call psa_cipher_encrypt_setup() or
1489 * psa_cipher_decrypt_setup() before calling this function. The choice
1490 * of setup function determines whether this function encrypts or
1491 * decrypts its input.
1492 *
1493 * This function finishes the encryption or decryption of the message
1494 * formed by concatenating the inputs passed to preceding calls to
1495 * psa_cipher_update().
1496 *
1497 * When this function returns, the operation becomes inactive.
1498 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001499 * \param[in,out] operation Active cipher operation.
1500 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001501 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001502 * \param[out] output_length On success, the number of bytes
1503 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001504 *
1505 * \retval #PSA_SUCCESS
1506 * Success.
1507 * \retval #PSA_ERROR_BAD_STATE
1508 * The operation state is not valid (not started, IV required but
1509 * not set, or already completed).
1510 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1511 * The size of the \p output buffer is too small.
1512 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1513 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1514 * \retval #PSA_ERROR_HARDWARE_FAILURE
1515 * \retval #PSA_ERROR_TAMPERING_DETECTED
1516 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001517psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001518 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001519 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001520 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001521
Gilles Peskinedcd14942018-07-12 00:30:52 +02001522/** Abort a cipher operation.
1523 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001524 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001525 * \p operation structure itself. Once aborted, the operation object
1526 * can be reused for another operation by calling
1527 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001528 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001529 * You may call this function any time after the operation object has
1530 * been initialized by any of the following methods:
1531 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1532 * whether it succeeds or not.
1533 * - Initializing the \c struct to all-bits-zero.
1534 * - Initializing the \c struct to logical zeros, e.g.
1535 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001536 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001537 * In particular, calling psa_cipher_abort() after the operation has been
1538 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1539 * is safe and has no effect.
1540 *
1541 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001542 *
1543 * \retval #PSA_SUCCESS
1544 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001545 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001546 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1547 * \retval #PSA_ERROR_HARDWARE_FAILURE
1548 * \retval #PSA_ERROR_TAMPERING_DETECTED
1549 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001550psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1551
1552/**@}*/
1553
Gilles Peskine3b555712018-03-03 21:27:57 +01001554/** \defgroup aead Authenticated encryption with associated data (AEAD)
1555 * @{
1556 */
1557
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001558/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001559 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001560 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001561 * \param alg The AEAD algorithm to compute
1562 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001563 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001564 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001565 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001566 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001567 * but not encrypted.
1568 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001569 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001570 * encrypted.
1571 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001572 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001573 * encrypted data. The additional data is not
1574 * part of this output. For algorithms where the
1575 * encrypted data and the authentication tag
1576 * are defined as separate outputs, the
1577 * authentication tag is appended to the
1578 * encrypted data.
1579 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1580 * This must be at least
1581 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1582 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001583 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001584 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001585 *
Gilles Peskine28538492018-07-11 17:34:00 +02001586 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001587 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001588 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001589 * \retval #PSA_ERROR_EMPTY_SLOT
1590 * \retval #PSA_ERROR_NOT_PERMITTED
1591 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001592 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001593 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001594 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001595 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1596 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1597 * \retval #PSA_ERROR_HARDWARE_FAILURE
1598 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001599 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001600 * The library has not been previously initialized by psa_crypto_init().
1601 * It is implementation-dependent whether a failure to initialize
1602 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001603 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001604psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001605 psa_algorithm_t alg,
1606 const uint8_t *nonce,
1607 size_t nonce_length,
1608 const uint8_t *additional_data,
1609 size_t additional_data_length,
1610 const uint8_t *plaintext,
1611 size_t plaintext_length,
1612 uint8_t *ciphertext,
1613 size_t ciphertext_size,
1614 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001615
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001616/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001617 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001618 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001619 * \param alg The AEAD algorithm to compute
1620 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001621 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001622 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001623 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001624 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001625 * but not encrypted.
1626 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001627 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001628 * encrypted. For algorithms where the
1629 * encrypted data and the authentication tag
1630 * are defined as separate inputs, the buffer
1631 * must contain the encrypted data followed
1632 * by the authentication tag.
1633 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001634 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001635 * \param plaintext_size Size of the \p plaintext buffer in bytes.
1636 * This must be at least
1637 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
1638 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001639 * \param[out] plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03001640 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001641 *
Gilles Peskine28538492018-07-11 17:34:00 +02001642 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001643 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001644 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001645 * \retval #PSA_ERROR_EMPTY_SLOT
1646 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001647 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02001648 * \retval #PSA_ERROR_NOT_PERMITTED
1649 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001650 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001651 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001652 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001653 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1654 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1655 * \retval #PSA_ERROR_HARDWARE_FAILURE
1656 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001657 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001658 * The library has not been previously initialized by psa_crypto_init().
1659 * It is implementation-dependent whether a failure to initialize
1660 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001661 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001662psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001663 psa_algorithm_t alg,
1664 const uint8_t *nonce,
1665 size_t nonce_length,
1666 const uint8_t *additional_data,
1667 size_t additional_data_length,
1668 const uint8_t *ciphertext,
1669 size_t ciphertext_length,
1670 uint8_t *plaintext,
1671 size_t plaintext_size,
1672 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001673
1674/**@}*/
1675
Gilles Peskine20035e32018-02-03 22:44:14 +01001676/** \defgroup asymmetric Asymmetric cryptography
1677 * @{
1678 */
1679
1680/**
1681 * \brief Sign a hash or short message with a private key.
1682 *
Gilles Peskine08bac712018-06-26 16:14:46 +02001683 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001684 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02001685 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
1686 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
1687 * to determine the hash algorithm to use.
1688 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001689 * \param handle Handle to the key to use for the operation.
1690 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001691 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001692 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001693 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001694 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001695 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001696 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001697 * \param[out] signature_length On success, the number of bytes
1698 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001699 *
Gilles Peskine28538492018-07-11 17:34:00 +02001700 * \retval #PSA_SUCCESS
1701 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001702 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01001703 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001704 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001705 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001706 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001707 * \retval #PSA_ERROR_NOT_SUPPORTED
1708 * \retval #PSA_ERROR_INVALID_ARGUMENT
1709 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1710 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1711 * \retval #PSA_ERROR_HARDWARE_FAILURE
1712 * \retval #PSA_ERROR_TAMPERING_DETECTED
1713 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03001714 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001715 * The library has not been previously initialized by psa_crypto_init().
1716 * It is implementation-dependent whether a failure to initialize
1717 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01001718 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001719psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01001720 psa_algorithm_t alg,
1721 const uint8_t *hash,
1722 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01001723 uint8_t *signature,
1724 size_t signature_size,
1725 size_t *signature_length);
1726
1727/**
1728 * \brief Verify the signature a hash or short message using a public key.
1729 *
Gilles Peskine08bac712018-06-26 16:14:46 +02001730 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001731 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02001732 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
1733 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
1734 * to determine the hash algorithm to use.
1735 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001736 * \param handle Handle to the key to use for the operation.
1737 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001738 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001739 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001740 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02001741 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001742 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001743 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001744 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001745 *
Gilles Peskine28538492018-07-11 17:34:00 +02001746 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001747 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02001748 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001749 * The calculation was perfomed successfully, but the passed
1750 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02001751 * \retval #PSA_ERROR_NOT_SUPPORTED
1752 * \retval #PSA_ERROR_INVALID_ARGUMENT
1753 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1754 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1755 * \retval #PSA_ERROR_HARDWARE_FAILURE
1756 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001757 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001758 * The library has not been previously initialized by psa_crypto_init().
1759 * It is implementation-dependent whether a failure to initialize
1760 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01001761 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001762psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01001763 psa_algorithm_t alg,
1764 const uint8_t *hash,
1765 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02001766 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02001767 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01001768
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001769/**
1770 * \brief Encrypt a short message with a public key.
1771 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001772 * \param handle Handle to the key to use for the operation.
1773 * It must be a public key or an asymmetric
1774 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001775 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001776 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001777 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001778 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001779 * \param[in] salt A salt or label, if supported by the
1780 * encryption algorithm.
1781 * If the algorithm does not support a
1782 * salt, pass \c NULL.
1783 * If the algorithm supports an optional
1784 * salt and you do not want to pass a salt,
1785 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001786 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001787 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1788 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001789 * \param salt_length Size of the \p salt buffer in bytes.
1790 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001791 * \param[out] output Buffer where the encrypted message is to
1792 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001793 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001794 * \param[out] output_length On success, the number of bytes
1795 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001796 *
Gilles Peskine28538492018-07-11 17:34:00 +02001797 * \retval #PSA_SUCCESS
1798 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001799 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001800 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001801 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001802 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001803 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001804 * \retval #PSA_ERROR_NOT_SUPPORTED
1805 * \retval #PSA_ERROR_INVALID_ARGUMENT
1806 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1807 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1808 * \retval #PSA_ERROR_HARDWARE_FAILURE
1809 * \retval #PSA_ERROR_TAMPERING_DETECTED
1810 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03001811 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001812 * The library has not been previously initialized by psa_crypto_init().
1813 * It is implementation-dependent whether a failure to initialize
1814 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001815 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001816psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001817 psa_algorithm_t alg,
1818 const uint8_t *input,
1819 size_t input_length,
1820 const uint8_t *salt,
1821 size_t salt_length,
1822 uint8_t *output,
1823 size_t output_size,
1824 size_t *output_length);
1825
1826/**
1827 * \brief Decrypt a short message with a private key.
1828 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001829 * \param handle Handle to the key to use for the operation.
1830 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001831 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001832 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001833 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001834 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001835 * \param[in] salt A salt or label, if supported by the
1836 * encryption algorithm.
1837 * If the algorithm does not support a
1838 * salt, pass \c NULL.
1839 * If the algorithm supports an optional
1840 * salt and you do not want to pass a salt,
1841 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001842 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001843 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1844 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001845 * \param salt_length Size of the \p salt buffer in bytes.
1846 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001847 * \param[out] output Buffer where the decrypted message is to
1848 * be written.
1849 * \param output_size Size of the \c output buffer in bytes.
1850 * \param[out] output_length On success, the number of bytes
1851 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001852 *
Gilles Peskine28538492018-07-11 17:34:00 +02001853 * \retval #PSA_SUCCESS
1854 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001855 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001856 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001857 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001858 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001859 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001860 * \retval #PSA_ERROR_NOT_SUPPORTED
1861 * \retval #PSA_ERROR_INVALID_ARGUMENT
1862 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1863 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1864 * \retval #PSA_ERROR_HARDWARE_FAILURE
1865 * \retval #PSA_ERROR_TAMPERING_DETECTED
1866 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
1867 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03001868 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001869 * The library has not been previously initialized by psa_crypto_init().
1870 * It is implementation-dependent whether a failure to initialize
1871 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001872 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001873psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001874 psa_algorithm_t alg,
1875 const uint8_t *input,
1876 size_t input_length,
1877 const uint8_t *salt,
1878 size_t salt_length,
1879 uint8_t *output,
1880 size_t output_size,
1881 size_t *output_length);
1882
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001883/**@}*/
1884
Gilles Peskineedd76872018-07-20 17:42:05 +02001885/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02001886 * @{
1887 */
1888
1889/** The type of the state data structure for generators.
1890 *
1891 * Before calling any function on a generator, the application must
1892 * initialize it by any of the following means:
1893 * - Set the structure to all-bits-zero, for example:
1894 * \code
1895 * psa_crypto_generator_t generator;
1896 * memset(&generator, 0, sizeof(generator));
1897 * \endcode
1898 * - Initialize the structure to logical zero values, for example:
1899 * \code
1900 * psa_crypto_generator_t generator = {0};
1901 * \endcode
1902 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
1903 * for example:
1904 * \code
1905 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1906 * \endcode
1907 * - Assign the result of the function psa_crypto_generator_init()
1908 * to the structure, for example:
1909 * \code
1910 * psa_crypto_generator_t generator;
1911 * generator = psa_crypto_generator_init();
1912 * \endcode
1913 *
1914 * This is an implementation-defined \c struct. Applications should not
1915 * make any assumptions about the content of this structure except
1916 * as directed by the documentation of a specific implementation.
1917 */
1918typedef struct psa_crypto_generator_s psa_crypto_generator_t;
1919
1920/** \def PSA_CRYPTO_GENERATOR_INIT
1921 *
1922 * This macro returns a suitable initializer for a generator object
1923 * of type #psa_crypto_generator_t.
1924 */
1925#ifdef __DOXYGEN_ONLY__
1926/* This is an example definition for documentation purposes.
1927 * Implementations should define a suitable value in `crypto_struct.h`.
1928 */
1929#define PSA_CRYPTO_GENERATOR_INIT {0}
1930#endif
1931
1932/** Return an initial value for a generator object.
1933 */
1934static psa_crypto_generator_t psa_crypto_generator_init(void);
1935
1936/** Retrieve the current capacity of a generator.
1937 *
1938 * The capacity of a generator is the maximum number of bytes that it can
1939 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
1940 *
1941 * \param[in] generator The generator to query.
1942 * \param[out] capacity On success, the capacity of the generator.
1943 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001944 * \retval #PSA_SUCCESS
1945 * \retval #PSA_ERROR_BAD_STATE
1946 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02001947 */
1948psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
1949 size_t *capacity);
1950
1951/** Read some data from a generator.
1952 *
1953 * This function reads and returns a sequence of bytes from a generator.
1954 * The data that is read is discarded from the generator. The generator's
1955 * capacity is decreased by the number of bytes read.
1956 *
1957 * \param[in,out] generator The generator object to read from.
1958 * \param[out] output Buffer where the generator output will be
1959 * written.
1960 * \param output_length Number of bytes to output.
1961 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001962 * \retval #PSA_SUCCESS
1963 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02001964 * There were fewer than \p output_length bytes
1965 * in the generator. Note that in this case, no
1966 * output is written to the output buffer.
1967 * The generator's capacity is set to 0, thus
1968 * subsequent calls to this function will not
1969 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001970 * \retval #PSA_ERROR_BAD_STATE
1971 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1972 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1973 * \retval #PSA_ERROR_HARDWARE_FAILURE
1974 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02001975 */
1976psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
1977 uint8_t *output,
1978 size_t output_length);
1979
1980/** Create a symmetric key from data read from a generator.
1981 *
1982 * This function reads a sequence of bytes from a generator and imports
1983 * these bytes as a key.
1984 * The data that is read is discarded from the generator. The generator's
1985 * capacity is decreased by the number of bytes read.
1986 *
1987 * This function is equivalent to calling #psa_generator_read and
1988 * passing the resulting output to #psa_import_key, but
1989 * if the implementation provides an isolation boundary then
1990 * the key material is not exposed outside the isolation boundary.
1991 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001992 * \param handle Handle to the slot where the key will be stored.
1993 * This must be a valid slot for a key of the chosen
1994 * type: it must have been obtained by calling
1995 * psa_allocate_key() or psa_create_key() with the
1996 * correct \p type and with a maximum size that is
1997 * compatible with \p bits.
1998 * It must not contain any key material yet.
Gilles Peskineeab56e42018-07-12 17:12:33 +02001999 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2000 * This must be a symmetric key type.
2001 * \param bits Key size in bits.
2002 * \param[in,out] generator The generator object to read from.
2003 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002004 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02002005 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01002006 * If the key is persistent, the key material and the key's metadata
2007 * have been saved to persistent storage.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002008 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02002009 * There were fewer than \p output_length bytes
2010 * in the generator. Note that in this case, no
2011 * output is written to the output buffer.
2012 * The generator's capacity is set to 0, thus
2013 * subsequent calls to this function will not
2014 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002015 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002016 * The key type or key size is not supported, either by the
2017 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002018 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineae32aac2018-11-30 14:39:32 +01002019 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002020 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskineeab56e42018-07-12 17:12:33 +02002021 * There is already a key in the specified slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002022 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2023 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
2024 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2025 * \retval #PSA_ERROR_HARDWARE_FAILURE
2026 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002027 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002028 * The library has not been previously initialized by psa_crypto_init().
2029 * It is implementation-dependent whether a failure to initialize
2030 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002031 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002032psa_status_t psa_generator_import_key(psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002033 psa_key_type_t type,
2034 size_t bits,
2035 psa_crypto_generator_t *generator);
2036
2037/** Abort a generator.
2038 *
2039 * Once a generator has been aborted, its capacity is zero.
2040 * Aborting a generator frees all associated resources except for the
2041 * \c generator structure itself.
2042 *
2043 * This function may be called at any time as long as the generator
2044 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
2045 * psa_crypto_generator_init() or a zero value. In particular, it is valid
2046 * to call psa_generator_abort() twice, or to call psa_generator_abort()
2047 * on a generator that has not been set up.
2048 *
2049 * Once aborted, the generator object may be called.
2050 *
2051 * \param[in,out] generator The generator to abort.
2052 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002053 * \retval #PSA_SUCCESS
2054 * \retval #PSA_ERROR_BAD_STATE
2055 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2056 * \retval #PSA_ERROR_HARDWARE_FAILURE
2057 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002058 */
2059psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
2060
Gilles Peskine8feb3a82018-09-18 12:06:11 +02002061/** Use the maximum possible capacity for a generator.
2062 *
2063 * Use this value as the capacity argument when setting up a generator
2064 * to indicate that the generator should have the maximum possible capacity.
2065 * The value of the maximum possible capacity depends on the generator
2066 * algorithm.
2067 */
2068#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
2069
Gilles Peskineeab56e42018-07-12 17:12:33 +02002070/**@}*/
2071
Gilles Peskineea0fb492018-07-12 17:17:20 +02002072/** \defgroup derivation Key derivation
2073 * @{
2074 */
2075
2076/** Set up a key derivation operation.
2077 *
2078 * A key derivation algorithm takes three inputs: a secret input \p key and
2079 * two non-secret inputs \p label and p salt.
2080 * The result of this function is a byte generator which can
2081 * be used to produce keys and other cryptographic material.
2082 *
2083 * The role of \p label and \p salt is as follows:
Gilles Peskinebef7f142018-07-12 17:22:21 +02002084 * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step
2085 * and \p label is the info string used in the "expand" step.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002086 *
Jaeden Amero9e919c62019-01-07 15:41:50 +00002087 * \param[in,out] generator The generator object to set up. It must have
2088 * been initialized as per the documentation for
2089 * #psa_crypto_generator_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002090 * \param handle Handle to the secret key.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002091 * \param alg The key derivation algorithm to compute
2092 * (\c PSA_ALG_XXX value such that
2093 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2094 * \param[in] salt Salt to use.
2095 * \param salt_length Size of the \p salt buffer in bytes.
2096 * \param[in] label Label to use.
2097 * \param label_length Size of the \p label buffer in bytes.
2098 * \param capacity The maximum number of bytes that the
2099 * generator will be able to provide.
2100 *
2101 * \retval #PSA_SUCCESS
2102 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002103 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineea0fb492018-07-12 17:17:20 +02002104 * \retval #PSA_ERROR_EMPTY_SLOT
2105 * \retval #PSA_ERROR_NOT_PERMITTED
2106 * \retval #PSA_ERROR_INVALID_ARGUMENT
2107 * \c key is not compatible with \c alg,
2108 * or \p capacity is too large for the specified algorithm and key.
2109 * \retval #PSA_ERROR_NOT_SUPPORTED
2110 * \c alg is not supported or is not a key derivation algorithm.
2111 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2112 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2113 * \retval #PSA_ERROR_HARDWARE_FAILURE
2114 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002115 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002116 * The library has not been previously initialized by psa_crypto_init().
2117 * It is implementation-dependent whether a failure to initialize
2118 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002119 */
2120psa_status_t psa_key_derivation(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002121 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02002122 psa_algorithm_t alg,
2123 const uint8_t *salt,
2124 size_t salt_length,
2125 const uint8_t *label,
2126 size_t label_length,
2127 size_t capacity);
2128
Gilles Peskine01d718c2018-09-18 12:01:02 +02002129/** Set up a key agreement operation.
2130 *
2131 * A key agreement algorithm takes two inputs: a private key \p private_key
2132 * a public key \p peer_key.
2133 * The result of this function is a byte generator which can
2134 * be used to produce keys and other cryptographic material.
2135 *
Gilles Peskine211a4362018-10-25 22:22:31 +02002136 * The resulting generator always has the maximum capacity permitted by
2137 * the algorithm.
2138 *
Jaeden Amero8afbff82019-01-14 16:56:20 +00002139 * \param[in,out] generator The generator object to set up. It must have been
2140 * initialized as per the documentation for
2141 * #psa_crypto_generator_t and not yet in use.
2142 * \param private_key Handle to the private key to use.
2143 * \param[in] peer_key Public key of the peer. The peer key must be in the
2144 * same format that psa_import_key() accepts for the
2145 * public key type corresponding to the type of
2146 * private_key. That is, this function performs the
2147 * equivalent of
2148 * `psa_import_key(internal_public_key_handle,
2149 * PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(private_key_type),
2150 * peer_key, peer_key_length)` where
2151 * `private_key_type` is the type of `private_key`.
2152 * For example, for EC keys, this means that peer_key
2153 * is interpreted as a point on the curve that the
2154 * private key is on. The standard formats for public
2155 * keys are documented in the documentation of
2156 * psa_export_public_key().
2157 * \param peer_key_length Size of \p peer_key in bytes.
2158 * \param alg The key agreement algorithm to compute
2159 * (\c PSA_ALG_XXX value such that
2160 * #PSA_ALG_IS_KEY_AGREEMENT(\p alg) is true).
Gilles Peskine01d718c2018-09-18 12:01:02 +02002161 *
2162 * \retval #PSA_SUCCESS
2163 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002164 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine01d718c2018-09-18 12:01:02 +02002165 * \retval #PSA_ERROR_EMPTY_SLOT
2166 * \retval #PSA_ERROR_NOT_PERMITTED
2167 * \retval #PSA_ERROR_INVALID_ARGUMENT
2168 * \c private_key is not compatible with \c alg,
2169 * or \p peer_key is not valid for \c alg or not compatible with
2170 * \c private_key.
2171 * \retval #PSA_ERROR_NOT_SUPPORTED
2172 * \c alg is not supported or is not a key derivation algorithm.
2173 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2174 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2175 * \retval #PSA_ERROR_HARDWARE_FAILURE
2176 * \retval #PSA_ERROR_TAMPERING_DETECTED
2177 */
2178psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002179 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02002180 const uint8_t *peer_key,
2181 size_t peer_key_length,
2182 psa_algorithm_t alg);
2183
Gilles Peskineea0fb492018-07-12 17:17:20 +02002184/**@}*/
2185
Gilles Peskineedd76872018-07-20 17:42:05 +02002186/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002187 * @{
2188 */
2189
2190/**
2191 * \brief Generate random bytes.
2192 *
2193 * \warning This function **can** fail! Callers MUST check the return status
2194 * and MUST NOT use the content of the output buffer if the return
2195 * status is not #PSA_SUCCESS.
2196 *
2197 * \note To generate a key, use psa_generate_key() instead.
2198 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002199 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002200 * \param output_size Number of bytes to generate and output.
2201 *
Gilles Peskine28538492018-07-11 17:34:00 +02002202 * \retval #PSA_SUCCESS
2203 * \retval #PSA_ERROR_NOT_SUPPORTED
2204 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2205 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2206 * \retval #PSA_ERROR_HARDWARE_FAILURE
2207 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03002208 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002209 * The library has not been previously initialized by psa_crypto_init().
2210 * It is implementation-dependent whether a failure to initialize
2211 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002212 */
2213psa_status_t psa_generate_random(uint8_t *output,
2214 size_t output_size);
2215
Gilles Peskine4c317f42018-07-12 01:24:09 +02002216/** Extra parameters for RSA key generation.
2217 *
Gilles Peskinebe42f312018-07-13 14:38:15 +02002218 * You may pass a pointer to a structure of this type as the \c extra
Gilles Peskine4c317f42018-07-12 01:24:09 +02002219 * parameter to psa_generate_key().
2220 */
2221typedef struct {
Gilles Peskineedd76872018-07-20 17:42:05 +02002222 uint32_t e; /**< Public exponent value. Default: 65537. */
Gilles Peskine4c317f42018-07-12 01:24:09 +02002223} psa_generate_key_extra_rsa;
2224
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002225/**
2226 * \brief Generate a key or key pair.
2227 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002228 * \param handle Handle to the slot where the key will be stored.
2229 * This must be a valid slot for a key of the chosen
2230 * type: it must have been obtained by calling
2231 * psa_allocate_key() or psa_create_key() with the
2232 * correct \p type and with a maximum size that is
2233 * compatible with \p bits.
2234 * It must not contain any key material yet.
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002235 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2236 * \param bits Key size in bits.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002237 * \param[in] extra Extra parameters for key generation. The
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002238 * interpretation of this parameter depends on
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002239 * \p type. All types support \c NULL to use
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002240 * default parameters. Implementation that support
2241 * the generation of vendor-specific key types
2242 * that allow extra parameters shall document
2243 * the format of these extra parameters and
2244 * the default values. For standard parameters,
2245 * the meaning of \p extra is as follows:
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002246 * - For a symmetric key type (a type such
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002247 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
2248 * false), \p extra must be \c NULL.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002249 * - For an elliptic curve key type (a type
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002250 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
2251 * false), \p extra must be \c NULL.
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002252 * - For an RSA key (\p type is
2253 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
2254 * optional #psa_generate_key_extra_rsa structure
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002255 * specifying the public exponent. The
2256 * default public exponent used when \p extra
2257 * is \c NULL is 65537.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002258 * \param extra_size Size of the buffer that \p extra
2259 * points to, in bytes. Note that if \p extra is
2260 * \c NULL then \p extra_size must be zero.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002261 *
Gilles Peskine28538492018-07-11 17:34:00 +02002262 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01002263 * Success.
2264 * If the key is persistent, the key material and the key's metadata
2265 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002266 * \retval #PSA_ERROR_INVALID_HANDLE
2267 * \retval #PSA_ERROR_OCCUPIED_SLOT
2268 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +02002269 * \retval #PSA_ERROR_NOT_SUPPORTED
2270 * \retval #PSA_ERROR_INVALID_ARGUMENT
2271 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2272 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2273 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2274 * \retval #PSA_ERROR_HARDWARE_FAILURE
2275 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002276 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002277 * The library has not been previously initialized by psa_crypto_init().
2278 * It is implementation-dependent whether a failure to initialize
2279 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002280 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002281psa_status_t psa_generate_key(psa_key_handle_t handle,
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002282 psa_key_type_t type,
2283 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02002284 const void *extra,
2285 size_t extra_size);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002286
2287/**@}*/
2288
Gilles Peskinee59236f2018-01-27 23:32:46 +01002289#ifdef __cplusplus
2290}
2291#endif
2292
Gilles Peskine0cad07c2018-06-27 19:49:02 +02002293/* The file "crypto_sizes.h" contains definitions for size calculation
2294 * macros whose definitions are implementation-specific. */
2295#include "crypto_sizes.h"
2296
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002297/* The file "crypto_struct.h" contains definitions for
2298 * implementation-specific structs that are declared above. */
2299#include "crypto_struct.h"
2300
2301/* The file "crypto_extra.h" contains vendor-specific definitions. This
2302 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01002303#include "crypto_extra.h"
2304
2305#endif /* PSA_CRYPTO_H */