blob: ed3f56369d31a029435589da41bd768c0f9bc3b2 [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 *
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200477 * The format is the DER representation defined by RFC 5280 as
478 * `SubjectPublicKeyInfo`, with the `subjectPublicKey` format
479 * specified below.
480 * ```
481 * SubjectPublicKeyInfo ::= SEQUENCE {
482 * algorithm AlgorithmIdentifier,
483 * subjectPublicKey BIT STRING }
484 * AlgorithmIdentifier ::= SEQUENCE {
485 * algorithm OBJECT IDENTIFIER,
486 * parameters ANY DEFINED BY algorithm OPTIONAL }
487 * ```
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100488 *
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200489 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY),
490 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.1 as
491 * `RSAPublicKey`,
492 * with the OID `rsaEncryption`,
493 * and with the parameters `NULL`.
494 * ```
495 * pkcs-1 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840)
496 * rsadsi(113549) pkcs(1) 1 }
497 * rsaEncryption OBJECT IDENTIFIER ::= { pkcs-1 1 }
498 *
499 * RSAPublicKey ::= SEQUENCE {
500 * modulus INTEGER, -- n
501 * publicExponent INTEGER } -- e
502 * ```
503 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY),
504 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.2 as
505 * `DSAPublicKey`,
506 * with the OID `id-dsa`,
507 * and with the parameters `DSS-Parms`.
508 * ```
509 * id-dsa OBJECT IDENTIFIER ::= {
510 * iso(1) member-body(2) us(840) x9-57(10040) x9cm(4) 1 }
511 *
512 * Dss-Parms ::= SEQUENCE {
513 * p INTEGER,
514 * q INTEGER,
515 * g INTEGER }
516 * DSAPublicKey ::= INTEGER -- public key, Y
517 * ```
518 * - For elliptic curve public keys (key types for which
519 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true),
520 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.5 as
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200521 * `ECPoint`, which contains the uncompressed
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200522 * representation defined by SEC1 &sect;2.3.3.
523 * The OID is `id-ecPublicKey`,
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200524 * and the parameters must be given as a `namedCurve` OID as specified in
Gilles Peskinec6290c02018-08-13 17:24:59 +0200525 * RFC 5480 &sect;2.1.1.1 or other applicable standards.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200526 * ```
527 * ansi-X9-62 OBJECT IDENTIFIER ::=
528 * { iso(1) member-body(2) us(840) 10045 }
529 * id-public-key-type OBJECT IDENTIFIER ::= { ansi-X9.62 2 }
530 * id-ecPublicKey OBJECT IDENTIFIER ::= { id-publicKeyType 1 }
531 *
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200532 * ECPoint ::= ...
533 * -- first 8 bits: 0x04;
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100534 * -- then x_P as a `ceiling(m/8)`-byte string, big endian;
535 * -- then y_P as a `ceiling(m/8)`-byte string, big endian;
536 * -- where `m` is the bit size associated with the curve,
Gilles Peskine7b5b4a02018-11-14 21:05:10 +0100537 * -- i.e. the bit size of `q` for a curve over `F_q`.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200538 *
539 * EcpkParameters ::= CHOICE { -- other choices are not allowed
540 * namedCurve OBJECT IDENTIFIER }
541 * ```
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100542 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100543 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200544 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200545 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200546 * \param[out] data_length On success, the number of bytes
547 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100548 *
Gilles Peskine28538492018-07-11 17:34:00 +0200549 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100550 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200551 * \retval #PSA_ERROR_EMPTY_SLOT
552 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200553 * The key is neither a public key nor a key pair.
554 * \retval #PSA_ERROR_NOT_SUPPORTED
555 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
556 * The size of the \p data buffer is too small. You can determine a
557 * sufficient buffer size by calling
558 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
559 * where \c type is the key type
560 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200561 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
562 * \retval #PSA_ERROR_HARDWARE_FAILURE
563 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300564 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300565 * The library has not been previously initialized by psa_crypto_init().
566 * It is implementation-dependent whether a failure to initialize
567 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100568 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100569psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100570 uint8_t *data,
571 size_t data_size,
572 size_t *data_length);
573
574/**@}*/
575
576/** \defgroup policy Key policies
577 * @{
578 */
579
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100580/** The type of the key policy data structure.
581 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000582 * Before calling any function on a key policy, the application must initialize
583 * it by any of the following means:
584 * - Set the structure to all-bits-zero, for example:
585 * \code
586 * psa_key_policy_t policy;
587 * memset(&policy, 0, sizeof(policy));
588 * \endcode
589 * - Initialize the structure to logical zero values, for example:
590 * \code
591 * psa_key_policy_t policy = {0};
592 * \endcode
593 * - Initialize the structure to the initializer #PSA_KEY_POLICY_INIT,
594 * for example:
595 * \code
596 * psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
597 * \endcode
598 * - Assign the result of the function psa_key_policy_init()
599 * to the structure, for example:
600 * \code
601 * psa_key_policy_t policy;
602 * policy = psa_key_policy_init();
603 * \endcode
604 *
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100605 * This is an implementation-defined \c struct. Applications should not
606 * make any assumptions about the content of this structure except
607 * as directed by the documentation of a specific implementation. */
608typedef struct psa_key_policy_s psa_key_policy_t;
609
Jaeden Amero70261c52019-01-04 11:47:20 +0000610/** \def PSA_KEY_POLICY_INIT
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200611 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000612 * This macro returns a suitable initializer for a key policy object of type
613 * #psa_key_policy_t.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200614 */
Jaeden Amero70261c52019-01-04 11:47:20 +0000615#ifdef __DOXYGEN_ONLY__
616/* This is an example definition for documentation purposes.
617 * Implementations should define a suitable value in `crypto_struct.h`.
618 */
619#define PSA_KEY_POLICY_INIT {0}
620#endif
621
622/** Return an initial value for a key policy that forbids all usage of the key.
623 */
624static psa_key_policy_t psa_key_policy_init(void);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100625
Gilles Peskine7e198532018-03-08 07:50:30 +0100626/** \brief Set the standard fields of a policy structure.
627 *
628 * Note that this function does not make any consistency check of the
629 * parameters. The values are only checked when applying the policy to
630 * a key slot with psa_set_key_policy().
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200631 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000632 * \param[in,out] policy The key policy to modify. It must have been
633 * initialized as per the documentation for
634 * #psa_key_policy_t.
635 * \param usage The permitted uses for the key.
636 * \param alg The algorithm that the key may be used for.
Gilles Peskine7e198532018-03-08 07:50:30 +0100637 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100638void psa_key_policy_set_usage(psa_key_policy_t *policy,
639 psa_key_usage_t usage,
640 psa_algorithm_t alg);
641
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200642/** \brief Retrieve the usage field of a policy structure.
643 *
644 * \param[in] policy The policy object to query.
645 *
646 * \return The permitted uses for a key with this policy.
647 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +0200648psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100649
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200650/** \brief Retrieve the algorithm field of a policy structure.
651 *
652 * \param[in] policy The policy object to query.
653 *
654 * \return The permitted algorithm for a key with this policy.
655 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +0200656psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100657
658/** \brief Set the usage policy on a key slot.
659 *
660 * This function must be called on an empty key slot, before importing,
661 * generating or creating a key in the slot. Changing the policy of an
662 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +0100663 *
664 * Implementations may set restrictions on supported key policies
665 * depending on the key type and the key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200666 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100667 * \param handle Handle to the key whose policy is to be changed.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200668 * \param[in] policy The policy object to query.
669 *
670 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100671 * Success.
672 * If the key is persistent, it is implementation-defined whether
673 * the policy has been saved to persistent storage. Implementations
674 * may defer saving the policy until the key material is created.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100675 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200676 * \retval #PSA_ERROR_OCCUPIED_SLOT
677 * \retval #PSA_ERROR_NOT_SUPPORTED
678 * \retval #PSA_ERROR_INVALID_ARGUMENT
679 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
680 * \retval #PSA_ERROR_HARDWARE_FAILURE
681 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300682 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300683 * The library has not been previously initialized by psa_crypto_init().
684 * It is implementation-dependent whether a failure to initialize
685 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100686 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100687psa_status_t psa_set_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100688 const psa_key_policy_t *policy);
689
Gilles Peskine7e198532018-03-08 07:50:30 +0100690/** \brief Get the usage policy for a key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200691 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100692 * \param handle Handle to the key slot whose policy is being queried.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200693 * \param[out] policy On success, the key's policy.
694 *
695 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100696 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200697 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
698 * \retval #PSA_ERROR_HARDWARE_FAILURE
699 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300700 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300701 * The library has not been previously initialized by psa_crypto_init().
702 * It is implementation-dependent whether a failure to initialize
703 * results in this error code.
Gilles Peskine7e198532018-03-08 07:50:30 +0100704 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100705psa_status_t psa_get_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100706 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +0100707
708/**@}*/
709
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100710/** \defgroup hash Message digests
711 * @{
712 */
713
Gilles Peskine308b91d2018-02-08 09:47:44 +0100714/** The type of the state data structure for multipart hash operations.
715 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000716 * Before calling any function on a hash operation object, the application must
717 * initialize it by any of the following means:
718 * - Set the structure to all-bits-zero, for example:
719 * \code
720 * psa_hash_operation_t operation;
721 * memset(&operation, 0, sizeof(operation));
722 * \endcode
723 * - Initialize the structure to logical zero values, for example:
724 * \code
725 * psa_hash_operation_t operation = {0};
726 * \endcode
727 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
728 * for example:
729 * \code
730 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
731 * \endcode
732 * - Assign the result of the function psa_hash_operation_init()
733 * to the structure, for example:
734 * \code
735 * psa_hash_operation_t operation;
736 * operation = psa_hash_operation_init();
737 * \endcode
738 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100739 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100740 * make any assumptions about the content of this structure except
741 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100742typedef struct psa_hash_operation_s psa_hash_operation_t;
743
Jaeden Amero6a25b412019-01-04 11:47:44 +0000744/** \def PSA_HASH_OPERATION_INIT
745 *
746 * This macro returns a suitable initializer for a hash operation object
747 * of type #psa_hash_operation_t.
748 */
749#ifdef __DOXYGEN_ONLY__
750/* This is an example definition for documentation purposes.
751 * Implementations should define a suitable value in `crypto_struct.h`.
752 */
753#define PSA_HASH_OPERATION_INIT {0}
754#endif
755
756/** Return an initial value for a hash operation object.
757 */
758static psa_hash_operation_t psa_hash_operation_init(void);
759
Gilles Peskine308b91d2018-02-08 09:47:44 +0100760/** Start a multipart hash operation.
761 *
762 * The sequence of operations to calculate a hash (message digest)
763 * is as follows:
764 * -# Allocate an operation object which will be passed to all the functions
765 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000766 * -# Initialize the operation object with one of the methods described in the
767 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200768 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100769 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100770 * of the message each time. The hash that is calculated is the hash
771 * of the concatenation of these messages in order.
772 * -# To calculate the hash, call psa_hash_finish().
773 * To compare the hash with an expected value, call psa_hash_verify().
774 *
775 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000776 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100777 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200778 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100779 * eventually terminate the operation. The following events terminate an
780 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100781 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100782 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100783 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000784 * \param[in,out] operation The operation object to set up. It must have
785 * been initialized as per the documentation for
786 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200787 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
788 * such that #PSA_ALG_IS_HASH(\p alg) is true).
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_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200793 * \p alg is not supported or is not a hash algorithm.
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 Peskineda8191d1c2018-07-08 19:46:38 +0200799psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100800 psa_algorithm_t alg);
801
Gilles Peskine308b91d2018-02-08 09:47:44 +0100802/** Add a message fragment to a multipart hash operation.
803 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200804 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100805 *
806 * If this function returns an error status, the operation becomes inactive.
807 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200808 * \param[in,out] operation Active hash operation.
809 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200810 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100811 *
Gilles Peskine28538492018-07-11 17:34:00 +0200812 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100813 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200814 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100815 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200816 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
817 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
818 * \retval #PSA_ERROR_HARDWARE_FAILURE
819 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100820 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100821psa_status_t psa_hash_update(psa_hash_operation_t *operation,
822 const uint8_t *input,
823 size_t input_length);
824
Gilles Peskine308b91d2018-02-08 09:47:44 +0100825/** Finish the calculation of the hash of a message.
826 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200827 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100828 * This function calculates the hash of the message formed by concatenating
829 * the inputs passed to preceding calls to psa_hash_update().
830 *
831 * When this function returns, the operation becomes inactive.
832 *
833 * \warning Applications should not call this function if they expect
834 * a specific value for the hash. Call psa_hash_verify() instead.
835 * Beware that comparing integrity or authenticity data such as
836 * hash values with a function such as \c memcmp is risky
837 * because the time taken by the comparison may leak information
838 * about the hashed data which could allow an attacker to guess
839 * a valid hash and thereby bypass security controls.
840 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200841 * \param[in,out] operation Active hash operation.
842 * \param[out] hash Buffer where the hash is to be written.
843 * \param hash_size Size of the \p hash buffer in bytes.
844 * \param[out] hash_length On success, the number of bytes
845 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +0200846 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +0200847 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100848 *
Gilles Peskine28538492018-07-11 17:34:00 +0200849 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100850 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200851 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100852 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200853 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200854 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200855 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100856 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +0200857 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
858 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
859 * \retval #PSA_ERROR_HARDWARE_FAILURE
860 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100861 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100862psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
863 uint8_t *hash,
864 size_t hash_size,
865 size_t *hash_length);
866
Gilles Peskine308b91d2018-02-08 09:47:44 +0100867/** Finish the calculation of the hash of a message and compare it with
868 * an expected value.
869 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200870 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100871 * This function calculates the hash of the message formed by concatenating
872 * the inputs passed to preceding calls to psa_hash_update(). It then
873 * compares the calculated hash with the expected hash passed as a
874 * parameter to this function.
875 *
876 * When this function returns, the operation becomes inactive.
877 *
Gilles Peskine19067982018-03-20 17:54:53 +0100878 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +0100879 * comparison between the actual hash and the expected hash is performed
880 * in constant time.
881 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200882 * \param[in,out] operation Active hash operation.
883 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200884 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100885 *
Gilles Peskine28538492018-07-11 17:34:00 +0200886 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100887 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +0200888 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100889 * The hash of the message was calculated successfully, but it
890 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +0200891 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100892 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200893 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
894 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
895 * \retval #PSA_ERROR_HARDWARE_FAILURE
896 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100897 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100898psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
899 const uint8_t *hash,
900 size_t hash_length);
901
Gilles Peskine308b91d2018-02-08 09:47:44 +0100902/** Abort a hash operation.
903 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100904 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200905 * \p operation structure itself. Once aborted, the operation object
906 * can be reused for another operation by calling
907 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100908 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200909 * You may call this function any time after the operation object has
910 * been initialized by any of the following methods:
911 * - A call to psa_hash_setup(), whether it succeeds or not.
912 * - Initializing the \c struct to all-bits-zero.
913 * - Initializing the \c struct to logical zeros, e.g.
914 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100915 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200916 * In particular, calling psa_hash_abort() after the operation has been
917 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
918 * psa_hash_verify() is safe and has no effect.
919 *
920 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100921 *
Gilles Peskine28538492018-07-11 17:34:00 +0200922 * \retval #PSA_SUCCESS
923 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200924 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +0200925 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
926 * \retval #PSA_ERROR_HARDWARE_FAILURE
927 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100928 */
929psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100930
931/**@}*/
932
Gilles Peskine8c9def32018-02-08 10:02:12 +0100933/** \defgroup MAC Message authentication codes
934 * @{
935 */
936
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100937/** The type of the state data structure for multipart MAC operations.
938 *
Jaeden Amero769ce272019-01-04 11:48:03 +0000939 * Before calling any function on a MAC operation object, the application must
940 * initialize it by any of the following means:
941 * - Set the structure to all-bits-zero, for example:
942 * \code
943 * psa_mac_operation_t operation;
944 * memset(&operation, 0, sizeof(operation));
945 * \endcode
946 * - Initialize the structure to logical zero values, for example:
947 * \code
948 * psa_mac_operation_t operation = {0};
949 * \endcode
950 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
951 * for example:
952 * \code
953 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
954 * \endcode
955 * - Assign the result of the function psa_mac_operation_init()
956 * to the structure, for example:
957 * \code
958 * psa_mac_operation_t operation;
959 * operation = psa_mac_operation_init();
960 * \endcode
961 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100962 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100963 * make any assumptions about the content of this structure except
964 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100965typedef struct psa_mac_operation_s psa_mac_operation_t;
966
Jaeden Amero769ce272019-01-04 11:48:03 +0000967/** \def PSA_MAC_OPERATION_INIT
968 *
969 * This macro returns a suitable initializer for a MAC operation object of type
970 * #psa_mac_operation_t.
971 */
972#ifdef __DOXYGEN_ONLY__
973/* This is an example definition for documentation purposes.
974 * Implementations should define a suitable value in `crypto_struct.h`.
975 */
976#define PSA_MAC_OPERATION_INIT {0}
977#endif
978
979/** Return an initial value for a MAC operation object.
980 */
981static psa_mac_operation_t psa_mac_operation_init(void);
982
Gilles Peskine89167cb2018-07-08 20:12:23 +0200983/** Start a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100984 *
Gilles Peskine89167cb2018-07-08 20:12:23 +0200985 * This function sets up the calculation of the MAC
986 * (message authentication code) of a byte string.
987 * To verify the MAC of a message against an
988 * expected value, use psa_mac_verify_setup() instead.
989 *
990 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100991 * -# Allocate an operation object which will be passed to all the functions
992 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +0000993 * -# Initialize the operation object with one of the methods described in the
994 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +0200995 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100996 * The key remains associated with the operation even if the content
997 * of the key slot changes.
998 * -# Call psa_mac_update() zero, one or more times, passing a fragment
999 * of the message each time. The MAC that is calculated is the MAC
1000 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001001 * -# At the end of the message, call psa_mac_sign_finish() to finish
1002 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001003 *
1004 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001005 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001006 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001007 * After a successful call to psa_mac_sign_setup(), the application must
1008 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001009 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001010 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001011 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001012 * \param[in,out] operation The operation object to set up. It must have
1013 * been initialized as per the documentation for
1014 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001015 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001016 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1017 * such that #PSA_ALG_IS_MAC(alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001018 *
Gilles Peskine28538492018-07-11 17:34:00 +02001019 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001020 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001021 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001022 * \retval #PSA_ERROR_EMPTY_SLOT
1023 * \retval #PSA_ERROR_NOT_PERMITTED
1024 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001025 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001026 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001027 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001028 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1029 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1030 * \retval #PSA_ERROR_HARDWARE_FAILURE
1031 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001032 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001033 * The library has not been previously initialized by psa_crypto_init().
1034 * It is implementation-dependent whether a failure to initialize
1035 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001036 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001037psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001038 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001039 psa_algorithm_t alg);
1040
1041/** Start a multipart MAC verification operation.
1042 *
1043 * This function sets up the verification of the MAC
1044 * (message authentication code) of a byte string against an expected value.
1045 *
1046 * The sequence of operations to verify a MAC is as follows:
1047 * -# Allocate an operation object which will be passed to all the functions
1048 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001049 * -# Initialize the operation object with one of the methods described in the
1050 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001051 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
1052 * The key remains associated with the operation even if the content
1053 * of the key slot changes.
1054 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1055 * of the message each time. The MAC that is calculated is the MAC
1056 * of the concatenation of these messages in order.
1057 * -# At the end of the message, call psa_mac_verify_finish() to finish
1058 * calculating the actual MAC of the message and verify it against
1059 * the expected value.
1060 *
1061 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001062 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001063 *
1064 * After a successful call to psa_mac_verify_setup(), the application must
1065 * eventually terminate the operation through one of the following methods:
1066 * - A failed call to psa_mac_update().
1067 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1068 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001069 * \param[in,out] operation The operation object to set up. It must have
1070 * been initialized as per the documentation for
1071 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001072 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001073 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1074 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001075 *
Gilles Peskine28538492018-07-11 17:34:00 +02001076 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001077 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001078 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001079 * \retval #PSA_ERROR_EMPTY_SLOT
1080 * \retval #PSA_ERROR_NOT_PERMITTED
1081 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001082 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001083 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001084 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001085 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1086 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1087 * \retval #PSA_ERROR_HARDWARE_FAILURE
1088 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001089 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001090 * The library has not been previously initialized by psa_crypto_init().
1091 * It is implementation-dependent whether a failure to initialize
1092 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001093 */
1094psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001095 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001096 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001097
Gilles Peskinedcd14942018-07-12 00:30:52 +02001098/** Add a message fragment to a multipart MAC operation.
1099 *
1100 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1101 * before calling this function.
1102 *
1103 * If this function returns an error status, the operation becomes inactive.
1104 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001105 * \param[in,out] operation Active MAC operation.
1106 * \param[in] input Buffer containing the message fragment to add to
1107 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001108 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001109 *
1110 * \retval #PSA_SUCCESS
1111 * Success.
1112 * \retval #PSA_ERROR_BAD_STATE
1113 * The operation state is not valid (not started, or already completed).
1114 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1115 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1116 * \retval #PSA_ERROR_HARDWARE_FAILURE
1117 * \retval #PSA_ERROR_TAMPERING_DETECTED
1118 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001119psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1120 const uint8_t *input,
1121 size_t input_length);
1122
Gilles Peskinedcd14942018-07-12 00:30:52 +02001123/** Finish the calculation of the MAC of a message.
1124 *
1125 * The application must call psa_mac_sign_setup() before calling this function.
1126 * This function calculates the MAC of the message formed by concatenating
1127 * the inputs passed to preceding calls to psa_mac_update().
1128 *
1129 * When this function returns, the operation becomes inactive.
1130 *
1131 * \warning Applications should not call this function if they expect
1132 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1133 * Beware that comparing integrity or authenticity data such as
1134 * MAC values with a function such as \c memcmp is risky
1135 * because the time taken by the comparison may leak information
1136 * about the MAC value which could allow an attacker to guess
1137 * a valid MAC and thereby bypass security controls.
1138 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001139 * \param[in,out] operation Active MAC operation.
1140 * \param[out] mac Buffer where the MAC value is to be written.
1141 * \param mac_size Size of the \p mac buffer in bytes.
1142 * \param[out] mac_length On success, the number of bytes
1143 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001144 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001145 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001146 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001147 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001148 *
1149 * \retval #PSA_SUCCESS
1150 * Success.
1151 * \retval #PSA_ERROR_BAD_STATE
1152 * The operation state is not valid (not started, or already completed).
1153 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001154 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001155 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1156 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1157 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1158 * \retval #PSA_ERROR_HARDWARE_FAILURE
1159 * \retval #PSA_ERROR_TAMPERING_DETECTED
1160 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001161psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1162 uint8_t *mac,
1163 size_t mac_size,
1164 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001165
Gilles Peskinedcd14942018-07-12 00:30:52 +02001166/** Finish the calculation of the MAC of a message and compare it with
1167 * an expected value.
1168 *
1169 * The application must call psa_mac_verify_setup() before calling this function.
1170 * This function calculates the MAC of the message formed by concatenating
1171 * the inputs passed to preceding calls to psa_mac_update(). It then
1172 * compares the calculated MAC with the expected MAC passed as a
1173 * parameter to this function.
1174 *
1175 * When this function returns, the operation becomes inactive.
1176 *
1177 * \note Implementations shall make the best effort to ensure that the
1178 * comparison between the actual MAC and the expected MAC is performed
1179 * in constant time.
1180 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001181 * \param[in,out] operation Active MAC operation.
1182 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001183 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001184 *
1185 * \retval #PSA_SUCCESS
1186 * The expected MAC is identical to the actual MAC of the message.
1187 * \retval #PSA_ERROR_INVALID_SIGNATURE
1188 * The MAC of the message was calculated successfully, but it
1189 * differs from the expected MAC.
1190 * \retval #PSA_ERROR_BAD_STATE
1191 * The operation state is not valid (not started, or already completed).
1192 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1193 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1194 * \retval #PSA_ERROR_HARDWARE_FAILURE
1195 * \retval #PSA_ERROR_TAMPERING_DETECTED
1196 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001197psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1198 const uint8_t *mac,
1199 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001200
Gilles Peskinedcd14942018-07-12 00:30:52 +02001201/** Abort a MAC operation.
1202 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001203 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001204 * \p operation structure itself. Once aborted, the operation object
1205 * can be reused for another operation by calling
1206 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001207 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001208 * You may call this function any time after the operation object has
1209 * been initialized by any of the following methods:
1210 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1211 * it succeeds or not.
1212 * - Initializing the \c struct to all-bits-zero.
1213 * - Initializing the \c struct to logical zeros, e.g.
1214 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001215 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001216 * In particular, calling psa_mac_abort() after the operation has been
1217 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1218 * psa_mac_verify_finish() is safe and has no effect.
1219 *
1220 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001221 *
1222 * \retval #PSA_SUCCESS
1223 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001224 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001225 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1226 * \retval #PSA_ERROR_HARDWARE_FAILURE
1227 * \retval #PSA_ERROR_TAMPERING_DETECTED
1228 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001229psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1230
1231/**@}*/
1232
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001233/** \defgroup cipher Symmetric ciphers
1234 * @{
1235 */
1236
1237/** The type of the state data structure for multipart cipher operations.
1238 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001239 * Before calling any function on a cipher operation object, the application
1240 * must initialize it by any of the following means:
1241 * - Set the structure to all-bits-zero, for example:
1242 * \code
1243 * psa_cipher_operation_t operation;
1244 * memset(&operation, 0, sizeof(operation));
1245 * \endcode
1246 * - Initialize the structure to logical zero values, for example:
1247 * \code
1248 * psa_cipher_operation_t operation = {0};
1249 * \endcode
1250 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1251 * for example:
1252 * \code
1253 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1254 * \endcode
1255 * - Assign the result of the function psa_cipher_operation_init()
1256 * to the structure, for example:
1257 * \code
1258 * psa_cipher_operation_t operation;
1259 * operation = psa_cipher_operation_init();
1260 * \endcode
1261 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001262 * This is an implementation-defined \c struct. Applications should not
1263 * make any assumptions about the content of this structure except
1264 * as directed by the documentation of a specific implementation. */
1265typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1266
Jaeden Amero5bae2272019-01-04 11:48:27 +00001267/** \def PSA_CIPHER_OPERATION_INIT
1268 *
1269 * This macro returns a suitable initializer for a cipher operation object of
1270 * type #psa_cipher_operation_t.
1271 */
1272#ifdef __DOXYGEN_ONLY__
1273/* This is an example definition for documentation purposes.
1274 * Implementations should define a suitable value in `crypto_struct.h`.
1275 */
1276#define PSA_CIPHER_OPERATION_INIT {0}
1277#endif
1278
1279/** Return an initial value for a cipher operation object.
1280 */
1281static psa_cipher_operation_t psa_cipher_operation_init(void);
1282
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001283/** Set the key for a multipart symmetric encryption operation.
1284 *
1285 * The sequence of operations to encrypt a message with a symmetric cipher
1286 * is as follows:
1287 * -# Allocate an operation object which will be passed to all the functions
1288 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001289 * -# Initialize the operation object with one of the methods described in the
1290 * documentation for #psa_cipher_operation_t, e.g.
1291 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001292 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001293 * The key remains associated with the operation even if the content
1294 * of the key slot changes.
itayzafrired7382f2018-08-02 14:19:33 +03001295 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001296 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001297 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001298 * requires a specific IV value.
1299 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1300 * of the message each time.
1301 * -# Call psa_cipher_finish().
1302 *
1303 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001304 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001305 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001306 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001307 * eventually terminate the operation. The following events terminate an
1308 * operation:
itayzafrired7382f2018-08-02 14:19:33 +03001309 * - A failed call to psa_cipher_generate_iv(), psa_cipher_set_iv()
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001310 * or psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001311 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001312 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001313 * \param[in,out] operation The operation object to set up. It must have
1314 * been initialized as per the documentation for
1315 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001316 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001317 * \param alg The cipher algorithm to compute
1318 * (\c PSA_ALG_XXX value such that
1319 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001320 *
Gilles Peskine28538492018-07-11 17:34:00 +02001321 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001322 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001323 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001324 * \retval #PSA_ERROR_EMPTY_SLOT
1325 * \retval #PSA_ERROR_NOT_PERMITTED
1326 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001327 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001328 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001329 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001330 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1331 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1332 * \retval #PSA_ERROR_HARDWARE_FAILURE
1333 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001334 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001335 * The library has not been previously initialized by psa_crypto_init().
1336 * It is implementation-dependent whether a failure to initialize
1337 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001338 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001339psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001340 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001341 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001342
1343/** Set the key for a multipart symmetric decryption operation.
1344 *
1345 * The sequence of operations to decrypt a message with a symmetric cipher
1346 * is as follows:
1347 * -# Allocate an operation object which will be passed to all the functions
1348 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001349 * -# Initialize the operation object with one of the methods described in the
1350 * documentation for #psa_cipher_operation_t, e.g.
1351 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001352 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001353 * The key remains associated with the operation even if the content
1354 * of the key slot changes.
1355 * -# Call psa_cipher_update() with the IV (initialization vector) for the
1356 * decryption. If the IV is prepended to the ciphertext, you can call
1357 * psa_cipher_update() on a buffer containing the IV followed by the
1358 * beginning of the message.
1359 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1360 * of the message each time.
1361 * -# Call psa_cipher_finish().
1362 *
1363 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001364 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001365 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001366 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001367 * eventually terminate the operation. The following events terminate an
1368 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001369 * - A failed call to psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001370 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001371 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001372 * \param[in,out] operation The operation object to set up. It must have
1373 * been initialized as per the documentation for
1374 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001375 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001376 * \param alg The cipher algorithm to compute
1377 * (\c PSA_ALG_XXX value such that
1378 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001379 *
Gilles Peskine28538492018-07-11 17:34:00 +02001380 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001381 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001382 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001383 * \retval #PSA_ERROR_EMPTY_SLOT
1384 * \retval #PSA_ERROR_NOT_PERMITTED
1385 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001386 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001387 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001388 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001389 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1390 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1391 * \retval #PSA_ERROR_HARDWARE_FAILURE
1392 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001393 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001394 * The library has not been previously initialized by psa_crypto_init().
1395 * It is implementation-dependent whether a failure to initialize
1396 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001397 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001398psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001399 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001400 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001401
Gilles Peskinedcd14942018-07-12 00:30:52 +02001402/** Generate an IV for a symmetric encryption operation.
1403 *
1404 * This function generates a random IV (initialization vector), nonce
1405 * or initial counter value for the encryption operation as appropriate
1406 * for the chosen algorithm, key type and key size.
1407 *
1408 * The application must call psa_cipher_encrypt_setup() before
1409 * calling this function.
1410 *
1411 * If this function returns an error status, the operation becomes inactive.
1412 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001413 * \param[in,out] operation Active cipher operation.
1414 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001415 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001416 * \param[out] iv_length On success, the number of bytes of the
1417 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001418 *
1419 * \retval #PSA_SUCCESS
1420 * Success.
1421 * \retval #PSA_ERROR_BAD_STATE
1422 * The operation state is not valid (not started, or IV already set).
1423 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001424 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001425 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1426 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1427 * \retval #PSA_ERROR_HARDWARE_FAILURE
1428 * \retval #PSA_ERROR_TAMPERING_DETECTED
1429 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001430psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1431 unsigned char *iv,
1432 size_t iv_size,
1433 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001434
Gilles Peskinedcd14942018-07-12 00:30:52 +02001435/** Set the IV for a symmetric encryption or decryption operation.
1436 *
1437 * This function sets the random IV (initialization vector), nonce
1438 * or initial counter value for the encryption or decryption operation.
1439 *
1440 * The application must call psa_cipher_encrypt_setup() before
1441 * calling this function.
1442 *
1443 * If this function returns an error status, the operation becomes inactive.
1444 *
1445 * \note When encrypting, applications should use psa_cipher_generate_iv()
1446 * instead of this function, unless implementing a protocol that requires
1447 * a non-random IV.
1448 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001449 * \param[in,out] operation Active cipher operation.
1450 * \param[in] iv Buffer containing the IV to use.
1451 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001452 *
1453 * \retval #PSA_SUCCESS
1454 * Success.
1455 * \retval #PSA_ERROR_BAD_STATE
1456 * The operation state is not valid (not started, or IV already set).
1457 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001458 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001459 * or the chosen algorithm does not use an IV.
1460 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1461 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1462 * \retval #PSA_ERROR_HARDWARE_FAILURE
1463 * \retval #PSA_ERROR_TAMPERING_DETECTED
1464 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001465psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1466 const unsigned char *iv,
1467 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001468
Gilles Peskinedcd14942018-07-12 00:30:52 +02001469/** Encrypt or decrypt a message fragment in an active cipher operation.
1470 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001471 * Before calling this function, you must:
1472 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1473 * The choice of setup function determines whether this function
1474 * encrypts or decrypts its input.
1475 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1476 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001477 *
1478 * If this function returns an error status, the operation becomes inactive.
1479 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001480 * \param[in,out] operation Active cipher operation.
1481 * \param[in] input Buffer containing the message fragment to
1482 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001483 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001484 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001485 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001486 * \param[out] output_length On success, the number of bytes
1487 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001488 *
1489 * \retval #PSA_SUCCESS
1490 * Success.
1491 * \retval #PSA_ERROR_BAD_STATE
1492 * The operation state is not valid (not started, IV required but
1493 * not set, or already completed).
1494 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1495 * The size of the \p output buffer is too small.
1496 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1497 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1498 * \retval #PSA_ERROR_HARDWARE_FAILURE
1499 * \retval #PSA_ERROR_TAMPERING_DETECTED
1500 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001501psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1502 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001503 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001504 unsigned char *output,
1505 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001506 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001507
Gilles Peskinedcd14942018-07-12 00:30:52 +02001508/** Finish encrypting or decrypting a message in a cipher operation.
1509 *
1510 * The application must call psa_cipher_encrypt_setup() or
1511 * psa_cipher_decrypt_setup() before calling this function. The choice
1512 * of setup function determines whether this function encrypts or
1513 * decrypts its input.
1514 *
1515 * This function finishes the encryption or decryption of the message
1516 * formed by concatenating the inputs passed to preceding calls to
1517 * psa_cipher_update().
1518 *
1519 * When this function returns, the operation becomes inactive.
1520 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001521 * \param[in,out] operation Active cipher operation.
1522 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001523 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001524 * \param[out] output_length On success, the number of bytes
1525 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001526 *
1527 * \retval #PSA_SUCCESS
1528 * Success.
1529 * \retval #PSA_ERROR_BAD_STATE
1530 * The operation state is not valid (not started, IV required but
1531 * not set, or already completed).
1532 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1533 * The size of the \p output buffer is too small.
1534 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1535 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1536 * \retval #PSA_ERROR_HARDWARE_FAILURE
1537 * \retval #PSA_ERROR_TAMPERING_DETECTED
1538 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001539psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001540 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001541 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001542 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001543
Gilles Peskinedcd14942018-07-12 00:30:52 +02001544/** Abort a cipher operation.
1545 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001546 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001547 * \p operation structure itself. Once aborted, the operation object
1548 * can be reused for another operation by calling
1549 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001550 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001551 * You may call this function any time after the operation object has
1552 * been initialized by any of the following methods:
1553 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1554 * whether it succeeds or not.
1555 * - Initializing the \c struct to all-bits-zero.
1556 * - Initializing the \c struct to logical zeros, e.g.
1557 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001558 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001559 * In particular, calling psa_cipher_abort() after the operation has been
1560 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1561 * is safe and has no effect.
1562 *
1563 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001564 *
1565 * \retval #PSA_SUCCESS
1566 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001567 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001568 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1569 * \retval #PSA_ERROR_HARDWARE_FAILURE
1570 * \retval #PSA_ERROR_TAMPERING_DETECTED
1571 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001572psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1573
1574/**@}*/
1575
Gilles Peskine3b555712018-03-03 21:27:57 +01001576/** \defgroup aead Authenticated encryption with associated data (AEAD)
1577 * @{
1578 */
1579
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001580/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001581 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001582 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001583 * \param alg The AEAD algorithm to compute
1584 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001585 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001586 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001587 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001588 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001589 * but not encrypted.
1590 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001591 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001592 * encrypted.
1593 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001594 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001595 * encrypted data. The additional data is not
1596 * part of this output. For algorithms where the
1597 * encrypted data and the authentication tag
1598 * are defined as separate outputs, the
1599 * authentication tag is appended to the
1600 * encrypted data.
1601 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1602 * This must be at least
1603 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1604 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001605 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001606 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001607 *
Gilles Peskine28538492018-07-11 17:34:00 +02001608 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001609 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001610 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001611 * \retval #PSA_ERROR_EMPTY_SLOT
1612 * \retval #PSA_ERROR_NOT_PERMITTED
1613 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001614 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001615 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001616 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001617 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1618 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1619 * \retval #PSA_ERROR_HARDWARE_FAILURE
1620 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001621 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001622 * The library has not been previously initialized by psa_crypto_init().
1623 * It is implementation-dependent whether a failure to initialize
1624 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001625 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001626psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001627 psa_algorithm_t alg,
1628 const uint8_t *nonce,
1629 size_t nonce_length,
1630 const uint8_t *additional_data,
1631 size_t additional_data_length,
1632 const uint8_t *plaintext,
1633 size_t plaintext_length,
1634 uint8_t *ciphertext,
1635 size_t ciphertext_size,
1636 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001637
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001638/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001639 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001640 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001641 * \param alg The AEAD algorithm to compute
1642 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001643 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001644 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001645 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001646 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001647 * but not encrypted.
1648 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001649 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001650 * encrypted. For algorithms where the
1651 * encrypted data and the authentication tag
1652 * are defined as separate inputs, the buffer
1653 * must contain the encrypted data followed
1654 * by the authentication tag.
1655 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001656 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001657 * \param plaintext_size Size of the \p plaintext buffer in bytes.
1658 * This must be at least
1659 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
1660 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001661 * \param[out] plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03001662 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001663 *
Gilles Peskine28538492018-07-11 17:34:00 +02001664 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001665 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001666 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001667 * \retval #PSA_ERROR_EMPTY_SLOT
1668 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001669 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02001670 * \retval #PSA_ERROR_NOT_PERMITTED
1671 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001672 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001673 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001674 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001675 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1676 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1677 * \retval #PSA_ERROR_HARDWARE_FAILURE
1678 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001679 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001680 * The library has not been previously initialized by psa_crypto_init().
1681 * It is implementation-dependent whether a failure to initialize
1682 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001683 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001684psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001685 psa_algorithm_t alg,
1686 const uint8_t *nonce,
1687 size_t nonce_length,
1688 const uint8_t *additional_data,
1689 size_t additional_data_length,
1690 const uint8_t *ciphertext,
1691 size_t ciphertext_length,
1692 uint8_t *plaintext,
1693 size_t plaintext_size,
1694 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001695
1696/**@}*/
1697
Gilles Peskine20035e32018-02-03 22:44:14 +01001698/** \defgroup asymmetric Asymmetric cryptography
1699 * @{
1700 */
1701
1702/**
1703 * \brief Sign a hash or short message with a private key.
1704 *
Gilles Peskine08bac712018-06-26 16:14:46 +02001705 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001706 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02001707 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
1708 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
1709 * to determine the hash algorithm to use.
1710 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001711 * \param handle Handle to the key to use for the operation.
1712 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001713 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001714 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001715 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001716 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001717 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001718 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001719 * \param[out] signature_length On success, the number of bytes
1720 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001721 *
Gilles Peskine28538492018-07-11 17:34:00 +02001722 * \retval #PSA_SUCCESS
1723 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001724 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01001725 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001726 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001727 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001728 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001729 * \retval #PSA_ERROR_NOT_SUPPORTED
1730 * \retval #PSA_ERROR_INVALID_ARGUMENT
1731 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1732 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1733 * \retval #PSA_ERROR_HARDWARE_FAILURE
1734 * \retval #PSA_ERROR_TAMPERING_DETECTED
1735 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03001736 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001737 * The library has not been previously initialized by psa_crypto_init().
1738 * It is implementation-dependent whether a failure to initialize
1739 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01001740 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001741psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01001742 psa_algorithm_t alg,
1743 const uint8_t *hash,
1744 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01001745 uint8_t *signature,
1746 size_t signature_size,
1747 size_t *signature_length);
1748
1749/**
1750 * \brief Verify the signature a hash or short message using a public key.
1751 *
Gilles Peskine08bac712018-06-26 16:14:46 +02001752 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001753 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02001754 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
1755 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
1756 * to determine the hash algorithm to use.
1757 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001758 * \param handle Handle to the key to use for the operation.
1759 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001760 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001761 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001762 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02001763 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001764 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001765 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001766 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001767 *
Gilles Peskine28538492018-07-11 17:34:00 +02001768 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001769 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02001770 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001771 * The calculation was perfomed successfully, but the passed
1772 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02001773 * \retval #PSA_ERROR_NOT_SUPPORTED
1774 * \retval #PSA_ERROR_INVALID_ARGUMENT
1775 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1776 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1777 * \retval #PSA_ERROR_HARDWARE_FAILURE
1778 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001779 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001780 * The library has not been previously initialized by psa_crypto_init().
1781 * It is implementation-dependent whether a failure to initialize
1782 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01001783 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001784psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01001785 psa_algorithm_t alg,
1786 const uint8_t *hash,
1787 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02001788 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02001789 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01001790
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001791/**
1792 * \brief Encrypt a short message with a public key.
1793 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001794 * \param handle Handle to the key to use for the operation.
1795 * It must be a public key or an asymmetric
1796 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001797 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001798 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001799 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001800 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001801 * \param[in] salt A salt or label, if supported by the
1802 * encryption algorithm.
1803 * If the algorithm does not support a
1804 * salt, pass \c NULL.
1805 * If the algorithm supports an optional
1806 * salt and you do not want to pass a salt,
1807 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001808 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001809 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1810 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001811 * \param salt_length Size of the \p salt buffer in bytes.
1812 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001813 * \param[out] output Buffer where the encrypted message is to
1814 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001815 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001816 * \param[out] output_length On success, the number of bytes
1817 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001818 *
Gilles Peskine28538492018-07-11 17:34:00 +02001819 * \retval #PSA_SUCCESS
1820 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001821 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001822 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001823 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001824 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001825 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001826 * \retval #PSA_ERROR_NOT_SUPPORTED
1827 * \retval #PSA_ERROR_INVALID_ARGUMENT
1828 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1829 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1830 * \retval #PSA_ERROR_HARDWARE_FAILURE
1831 * \retval #PSA_ERROR_TAMPERING_DETECTED
1832 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03001833 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001834 * The library has not been previously initialized by psa_crypto_init().
1835 * It is implementation-dependent whether a failure to initialize
1836 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001837 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001838psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001839 psa_algorithm_t alg,
1840 const uint8_t *input,
1841 size_t input_length,
1842 const uint8_t *salt,
1843 size_t salt_length,
1844 uint8_t *output,
1845 size_t output_size,
1846 size_t *output_length);
1847
1848/**
1849 * \brief Decrypt a short message with a private key.
1850 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001851 * \param handle Handle to the key to use for the operation.
1852 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001853 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001854 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001855 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001856 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001857 * \param[in] salt A salt or label, if supported by the
1858 * encryption algorithm.
1859 * If the algorithm does not support a
1860 * salt, pass \c NULL.
1861 * If the algorithm supports an optional
1862 * salt and you do not want to pass a salt,
1863 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001864 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001865 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1866 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001867 * \param salt_length Size of the \p salt buffer in bytes.
1868 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001869 * \param[out] output Buffer where the decrypted message is to
1870 * be written.
1871 * \param output_size Size of the \c output buffer in bytes.
1872 * \param[out] output_length On success, the number of bytes
1873 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001874 *
Gilles Peskine28538492018-07-11 17:34:00 +02001875 * \retval #PSA_SUCCESS
1876 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001877 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001878 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001879 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001880 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001881 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001882 * \retval #PSA_ERROR_NOT_SUPPORTED
1883 * \retval #PSA_ERROR_INVALID_ARGUMENT
1884 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1885 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1886 * \retval #PSA_ERROR_HARDWARE_FAILURE
1887 * \retval #PSA_ERROR_TAMPERING_DETECTED
1888 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
1889 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03001890 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001891 * The library has not been previously initialized by psa_crypto_init().
1892 * It is implementation-dependent whether a failure to initialize
1893 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001894 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001895psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001896 psa_algorithm_t alg,
1897 const uint8_t *input,
1898 size_t input_length,
1899 const uint8_t *salt,
1900 size_t salt_length,
1901 uint8_t *output,
1902 size_t output_size,
1903 size_t *output_length);
1904
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001905/**@}*/
1906
Gilles Peskineedd76872018-07-20 17:42:05 +02001907/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02001908 * @{
1909 */
1910
1911/** The type of the state data structure for generators.
1912 *
1913 * Before calling any function on a generator, the application must
1914 * initialize it by any of the following means:
1915 * - Set the structure to all-bits-zero, for example:
1916 * \code
1917 * psa_crypto_generator_t generator;
1918 * memset(&generator, 0, sizeof(generator));
1919 * \endcode
1920 * - Initialize the structure to logical zero values, for example:
1921 * \code
1922 * psa_crypto_generator_t generator = {0};
1923 * \endcode
1924 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
1925 * for example:
1926 * \code
1927 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1928 * \endcode
1929 * - Assign the result of the function psa_crypto_generator_init()
1930 * to the structure, for example:
1931 * \code
1932 * psa_crypto_generator_t generator;
1933 * generator = psa_crypto_generator_init();
1934 * \endcode
1935 *
1936 * This is an implementation-defined \c struct. Applications should not
1937 * make any assumptions about the content of this structure except
1938 * as directed by the documentation of a specific implementation.
1939 */
1940typedef struct psa_crypto_generator_s psa_crypto_generator_t;
1941
1942/** \def PSA_CRYPTO_GENERATOR_INIT
1943 *
1944 * This macro returns a suitable initializer for a generator object
1945 * of type #psa_crypto_generator_t.
1946 */
1947#ifdef __DOXYGEN_ONLY__
1948/* This is an example definition for documentation purposes.
1949 * Implementations should define a suitable value in `crypto_struct.h`.
1950 */
1951#define PSA_CRYPTO_GENERATOR_INIT {0}
1952#endif
1953
1954/** Return an initial value for a generator object.
1955 */
1956static psa_crypto_generator_t psa_crypto_generator_init(void);
1957
1958/** Retrieve the current capacity of a generator.
1959 *
1960 * The capacity of a generator is the maximum number of bytes that it can
1961 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
1962 *
1963 * \param[in] generator The generator to query.
1964 * \param[out] capacity On success, the capacity of the generator.
1965 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001966 * \retval #PSA_SUCCESS
1967 * \retval #PSA_ERROR_BAD_STATE
1968 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02001969 */
1970psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
1971 size_t *capacity);
1972
1973/** Read some data from a generator.
1974 *
1975 * This function reads and returns a sequence of bytes from a generator.
1976 * The data that is read is discarded from the generator. The generator's
1977 * capacity is decreased by the number of bytes read.
1978 *
1979 * \param[in,out] generator The generator object to read from.
1980 * \param[out] output Buffer where the generator output will be
1981 * written.
1982 * \param output_length Number of bytes to output.
1983 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001984 * \retval #PSA_SUCCESS
1985 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02001986 * There were fewer than \p output_length bytes
1987 * in the generator. Note that in this case, no
1988 * output is written to the output buffer.
1989 * The generator's capacity is set to 0, thus
1990 * subsequent calls to this function will not
1991 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001992 * \retval #PSA_ERROR_BAD_STATE
1993 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1994 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1995 * \retval #PSA_ERROR_HARDWARE_FAILURE
1996 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02001997 */
1998psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
1999 uint8_t *output,
2000 size_t output_length);
2001
2002/** Create a symmetric key from data read from a generator.
2003 *
2004 * This function reads a sequence of bytes from a generator and imports
2005 * these bytes as a key.
2006 * The data that is read is discarded from the generator. The generator's
2007 * capacity is decreased by the number of bytes read.
2008 *
2009 * This function is equivalent to calling #psa_generator_read and
2010 * passing the resulting output to #psa_import_key, but
2011 * if the implementation provides an isolation boundary then
2012 * the key material is not exposed outside the isolation boundary.
2013 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002014 * \param handle Handle to the slot where the key will be stored.
2015 * This must be a valid slot for a key of the chosen
2016 * type: it must have been obtained by calling
2017 * psa_allocate_key() or psa_create_key() with the
2018 * correct \p type and with a maximum size that is
2019 * compatible with \p bits.
2020 * It must not contain any key material yet.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002021 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2022 * This must be a symmetric key type.
2023 * \param bits Key size in bits.
2024 * \param[in,out] generator The generator object to read from.
2025 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002026 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02002027 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01002028 * If the key is persistent, the key material and the key's metadata
2029 * have been saved to persistent storage.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002030 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02002031 * There were fewer than \p output_length bytes
2032 * in the generator. Note that in this case, no
2033 * output is written to the output buffer.
2034 * The generator's capacity is set to 0, thus
2035 * subsequent calls to this function will not
2036 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002037 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002038 * The key type or key size is not supported, either by the
2039 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002040 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineae32aac2018-11-30 14:39:32 +01002041 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002042 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskineeab56e42018-07-12 17:12:33 +02002043 * There is already a key in the specified slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002044 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2045 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
2046 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2047 * \retval #PSA_ERROR_HARDWARE_FAILURE
2048 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002049 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002050 * The library has not been previously initialized by psa_crypto_init().
2051 * It is implementation-dependent whether a failure to initialize
2052 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002053 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002054psa_status_t psa_generator_import_key(psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002055 psa_key_type_t type,
2056 size_t bits,
2057 psa_crypto_generator_t *generator);
2058
2059/** Abort a generator.
2060 *
2061 * Once a generator has been aborted, its capacity is zero.
2062 * Aborting a generator frees all associated resources except for the
2063 * \c generator structure itself.
2064 *
2065 * This function may be called at any time as long as the generator
2066 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
2067 * psa_crypto_generator_init() or a zero value. In particular, it is valid
2068 * to call psa_generator_abort() twice, or to call psa_generator_abort()
2069 * on a generator that has not been set up.
2070 *
2071 * Once aborted, the generator object may be called.
2072 *
2073 * \param[in,out] generator The generator to abort.
2074 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002075 * \retval #PSA_SUCCESS
2076 * \retval #PSA_ERROR_BAD_STATE
2077 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2078 * \retval #PSA_ERROR_HARDWARE_FAILURE
2079 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002080 */
2081psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
2082
Gilles Peskine8feb3a82018-09-18 12:06:11 +02002083/** Use the maximum possible capacity for a generator.
2084 *
2085 * Use this value as the capacity argument when setting up a generator
2086 * to indicate that the generator should have the maximum possible capacity.
2087 * The value of the maximum possible capacity depends on the generator
2088 * algorithm.
2089 */
2090#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
2091
Gilles Peskineeab56e42018-07-12 17:12:33 +02002092/**@}*/
2093
Gilles Peskineea0fb492018-07-12 17:17:20 +02002094/** \defgroup derivation Key derivation
2095 * @{
2096 */
2097
2098/** Set up a key derivation operation.
2099 *
2100 * A key derivation algorithm takes three inputs: a secret input \p key and
2101 * two non-secret inputs \p label and p salt.
2102 * The result of this function is a byte generator which can
2103 * be used to produce keys and other cryptographic material.
2104 *
2105 * The role of \p label and \p salt is as follows:
Gilles Peskinebef7f142018-07-12 17:22:21 +02002106 * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step
2107 * and \p label is the info string used in the "expand" step.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002108 *
Jaeden Amero9e919c62019-01-07 15:41:50 +00002109 * \param[in,out] generator The generator object to set up. It must have
2110 * been initialized as per the documentation for
2111 * #psa_crypto_generator_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002112 * \param handle Handle to the secret key.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002113 * \param alg The key derivation algorithm to compute
2114 * (\c PSA_ALG_XXX value such that
2115 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2116 * \param[in] salt Salt to use.
2117 * \param salt_length Size of the \p salt buffer in bytes.
2118 * \param[in] label Label to use.
2119 * \param label_length Size of the \p label buffer in bytes.
2120 * \param capacity The maximum number of bytes that the
2121 * generator will be able to provide.
2122 *
2123 * \retval #PSA_SUCCESS
2124 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002125 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineea0fb492018-07-12 17:17:20 +02002126 * \retval #PSA_ERROR_EMPTY_SLOT
2127 * \retval #PSA_ERROR_NOT_PERMITTED
2128 * \retval #PSA_ERROR_INVALID_ARGUMENT
2129 * \c key is not compatible with \c alg,
2130 * or \p capacity is too large for the specified algorithm and key.
2131 * \retval #PSA_ERROR_NOT_SUPPORTED
2132 * \c alg is not supported or is not a key derivation algorithm.
2133 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2134 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2135 * \retval #PSA_ERROR_HARDWARE_FAILURE
2136 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002137 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002138 * The library has not been previously initialized by psa_crypto_init().
2139 * It is implementation-dependent whether a failure to initialize
2140 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002141 */
2142psa_status_t psa_key_derivation(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002143 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02002144 psa_algorithm_t alg,
2145 const uint8_t *salt,
2146 size_t salt_length,
2147 const uint8_t *label,
2148 size_t label_length,
2149 size_t capacity);
2150
Gilles Peskine01d718c2018-09-18 12:01:02 +02002151/** Set up a key agreement operation.
2152 *
2153 * A key agreement algorithm takes two inputs: a private key \p private_key
2154 * a public key \p peer_key.
2155 * The result of this function is a byte generator which can
2156 * be used to produce keys and other cryptographic material.
2157 *
Gilles Peskine211a4362018-10-25 22:22:31 +02002158 * The resulting generator always has the maximum capacity permitted by
2159 * the algorithm.
2160 *
Jaeden Amero9e919c62019-01-07 15:41:50 +00002161 * \param[in,out] generator The generator object to set up. It must have
2162 * been initialized as per the documentation for
2163 * #psa_crypto_generator_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002164 * \param private_key Handle to the private key to use.
Gilles Peskined171e782018-11-15 17:46:21 +01002165 * \param[in] peer_key Public key of the peer. It must be
2166 * in the same format that psa_import_key()
2167 * accepts. The standard formats for public
2168 * keys are documented in the documentation
2169 * of psa_export_public_key().
Gilles Peskine01d718c2018-09-18 12:01:02 +02002170 * \param peer_key_length Size of \p peer_key in bytes.
2171 * \param alg The key agreement algorithm to compute
2172 * (\c PSA_ALG_XXX value such that
2173 * #PSA_ALG_IS_KEY_AGREEMENT(\p alg) is true).
2174 *
2175 * \retval #PSA_SUCCESS
2176 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002177 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine01d718c2018-09-18 12:01:02 +02002178 * \retval #PSA_ERROR_EMPTY_SLOT
2179 * \retval #PSA_ERROR_NOT_PERMITTED
2180 * \retval #PSA_ERROR_INVALID_ARGUMENT
2181 * \c private_key is not compatible with \c alg,
2182 * or \p peer_key is not valid for \c alg or not compatible with
2183 * \c private_key.
2184 * \retval #PSA_ERROR_NOT_SUPPORTED
2185 * \c alg is not supported or is not a key derivation algorithm.
2186 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2187 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2188 * \retval #PSA_ERROR_HARDWARE_FAILURE
2189 * \retval #PSA_ERROR_TAMPERING_DETECTED
2190 */
2191psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002192 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02002193 const uint8_t *peer_key,
2194 size_t peer_key_length,
2195 psa_algorithm_t alg);
2196
Gilles Peskineea0fb492018-07-12 17:17:20 +02002197/**@}*/
2198
Gilles Peskineedd76872018-07-20 17:42:05 +02002199/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002200 * @{
2201 */
2202
2203/**
2204 * \brief Generate random bytes.
2205 *
2206 * \warning This function **can** fail! Callers MUST check the return status
2207 * and MUST NOT use the content of the output buffer if the return
2208 * status is not #PSA_SUCCESS.
2209 *
2210 * \note To generate a key, use psa_generate_key() instead.
2211 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002212 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002213 * \param output_size Number of bytes to generate and output.
2214 *
Gilles Peskine28538492018-07-11 17:34:00 +02002215 * \retval #PSA_SUCCESS
2216 * \retval #PSA_ERROR_NOT_SUPPORTED
2217 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2218 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2219 * \retval #PSA_ERROR_HARDWARE_FAILURE
2220 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03002221 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002222 * The library has not been previously initialized by psa_crypto_init().
2223 * It is implementation-dependent whether a failure to initialize
2224 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002225 */
2226psa_status_t psa_generate_random(uint8_t *output,
2227 size_t output_size);
2228
Gilles Peskine4c317f42018-07-12 01:24:09 +02002229/** Extra parameters for RSA key generation.
2230 *
Gilles Peskinebe42f312018-07-13 14:38:15 +02002231 * You may pass a pointer to a structure of this type as the \c extra
Gilles Peskine4c317f42018-07-12 01:24:09 +02002232 * parameter to psa_generate_key().
2233 */
2234typedef struct {
Gilles Peskineedd76872018-07-20 17:42:05 +02002235 uint32_t e; /**< Public exponent value. Default: 65537. */
Gilles Peskine4c317f42018-07-12 01:24:09 +02002236} psa_generate_key_extra_rsa;
2237
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002238/**
2239 * \brief Generate a key or key pair.
2240 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002241 * \param handle Handle to the slot where the key will be stored.
2242 * This must be a valid slot for a key of the chosen
2243 * type: it must have been obtained by calling
2244 * psa_allocate_key() or psa_create_key() with the
2245 * correct \p type and with a maximum size that is
2246 * compatible with \p bits.
2247 * It must not contain any key material yet.
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002248 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2249 * \param bits Key size in bits.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002250 * \param[in] extra Extra parameters for key generation. The
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002251 * interpretation of this parameter depends on
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002252 * \p type. All types support \c NULL to use
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002253 * default parameters. Implementation that support
2254 * the generation of vendor-specific key types
2255 * that allow extra parameters shall document
2256 * the format of these extra parameters and
2257 * the default values. For standard parameters,
2258 * the meaning of \p extra is as follows:
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002259 * - For a symmetric key type (a type such
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002260 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
2261 * false), \p extra must be \c NULL.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002262 * - For an elliptic curve key type (a type
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002263 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
2264 * false), \p extra must be \c NULL.
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002265 * - For an RSA key (\p type is
2266 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
2267 * optional #psa_generate_key_extra_rsa structure
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002268 * specifying the public exponent. The
2269 * default public exponent used when \p extra
2270 * is \c NULL is 65537.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002271 * \param extra_size Size of the buffer that \p extra
2272 * points to, in bytes. Note that if \p extra is
2273 * \c NULL then \p extra_size must be zero.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002274 *
Gilles Peskine28538492018-07-11 17:34:00 +02002275 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01002276 * Success.
2277 * If the key is persistent, the key material and the key's metadata
2278 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002279 * \retval #PSA_ERROR_INVALID_HANDLE
2280 * \retval #PSA_ERROR_OCCUPIED_SLOT
2281 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +02002282 * \retval #PSA_ERROR_NOT_SUPPORTED
2283 * \retval #PSA_ERROR_INVALID_ARGUMENT
2284 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2285 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2286 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2287 * \retval #PSA_ERROR_HARDWARE_FAILURE
2288 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002289 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002290 * The library has not been previously initialized by psa_crypto_init().
2291 * It is implementation-dependent whether a failure to initialize
2292 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002293 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002294psa_status_t psa_generate_key(psa_key_handle_t handle,
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002295 psa_key_type_t type,
2296 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02002297 const void *extra,
2298 size_t extra_size);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002299
2300/**@}*/
2301
Gilles Peskinee59236f2018-01-27 23:32:46 +01002302#ifdef __cplusplus
2303}
2304#endif
2305
Gilles Peskine0cad07c2018-06-27 19:49:02 +02002306/* The file "crypto_sizes.h" contains definitions for size calculation
2307 * macros whose definitions are implementation-specific. */
2308#include "crypto_sizes.h"
2309
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002310/* The file "crypto_struct.h" contains definitions for
2311 * implementation-specific structs that are declared above. */
2312#include "crypto_struct.h"
2313
2314/* The file "crypto_extra.h" contains vendor-specific definitions. This
2315 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01002316#include "crypto_extra.h"
2317
2318#endif /* PSA_CRYPTO_H */