blob: 3168026790a537eb2f6f574dbbd085e316a0ab88 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto.h
3 * \brief Platform Security Architecture cryptography module
4 */
Jaeden Amerocab54942018-07-25 13:26:13 +01005/*
6 * Copyright (C) 2018, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
Gilles Peskinee59236f2018-01-27 23:32:46 +010021
22#ifndef PSA_CRYPTO_H
23#define PSA_CRYPTO_H
24
25#include "crypto_platform.h"
26
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027#include <stddef.h>
28
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010029#ifdef __DOXYGEN_ONLY__
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010030/* This __DOXYGEN_ONLY__ block contains mock definitions for things that
31 * must be defined in the crypto_platform.h header. These mock definitions
32 * are present in this file as a convenience to generate pretty-printed
33 * documentation that includes those definitions. */
34
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010035/** \defgroup platform Implementation-specific definitions
36 * @{
37 */
38
Gilles Peskineae32aac2018-11-30 14:39:32 +010039/** \brief Key handle.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010040 *
Gilles Peskineae32aac2018-11-30 14:39:32 +010041 * This type represents open handles to keys. It must be an unsigned integral
Gilles Peskine308b91d2018-02-08 09:47:44 +010042 * type. The choice of type is implementation-dependent.
Gilles Peskineae32aac2018-11-30 14:39:32 +010043 *
Gilles Peskine23fd2bd2018-12-11 15:51:32 +010044 * 0 is not a valid key handle. How other handle values are assigned is
45 * implementation-dependent.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046 */
Gilles Peskineae32aac2018-11-30 14:39:32 +010047typedef _unsigned_integral_type_ psa_key_handle_t;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010049/**@}*/
Gilles Peskinef5b9fa12018-03-07 16:40:18 +010050#endif /* __DOXYGEN_ONLY__ */
Gilles Peskine62a7e7e2018-02-07 21:54:47 +010051
Gilles Peskinee59236f2018-01-27 23:32:46 +010052#ifdef __cplusplus
53extern "C" {
54#endif
55
Gilles Peskinef3b731e2018-12-12 13:38:31 +010056/* The file "crypto_types.h" declares types that encode errors,
57 * algorithms, key types, policies, etc. */
58#include "crypto_types.h"
59
60/* The file "crypto_values.h" declares macros to build and analyze values
61 * of integral types defined in "crypto_types.h". */
62#include "crypto_values.h"
63
64/** \defgroup initialization Library initialization
Gilles Peskinee59236f2018-01-27 23:32:46 +010065 * @{
66 */
67
68/**
Gilles Peskinee59236f2018-01-27 23:32:46 +010069 * \brief Library initialization.
70 *
71 * Applications must call this function before calling any other
72 * function in this module.
73 *
74 * Applications may call this function more than once. Once a call
75 * succeeds, subsequent calls are guaranteed to succeed.
76 *
itayzafrir18617092018-09-16 12:22:41 +030077 * If the application calls other functions before calling psa_crypto_init(),
78 * the behavior is undefined. Implementations are encouraged to either perform
79 * the operation as if the library had been initialized or to return
80 * #PSA_ERROR_BAD_STATE or some other applicable error. In particular,
81 * implementations should not return a success status if the lack of
82 * initialization may have security implications, for example due to improper
83 * seeding of the random number generator.
84 *
Gilles Peskine28538492018-07-11 17:34:00 +020085 * \retval #PSA_SUCCESS
86 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
87 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
88 * \retval #PSA_ERROR_HARDWARE_FAILURE
89 * \retval #PSA_ERROR_TAMPERING_DETECTED
90 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
Gilles Peskinee59236f2018-01-27 23:32:46 +010091 */
92psa_status_t psa_crypto_init(void);
93
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010094/**@}*/
95
96/** \defgroup key_management Key management
97 * @{
98 */
99
Gilles Peskineae32aac2018-11-30 14:39:32 +0100100/** \brief Retrieve the lifetime of an open key.
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100101 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100102 * \param handle Handle to query.
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100103 * \param[out] lifetime On success, the lifetime value.
104 *
105 * \retval #PSA_SUCCESS
106 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100107 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100108 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
109 * \retval #PSA_ERROR_HARDWARE_FAILURE
110 * \retval #PSA_ERROR_TAMPERING_DETECTED
111 * \retval #PSA_ERROR_BAD_STATE
112 * The library has not been previously initialized by psa_crypto_init().
113 * It is implementation-dependent whether a failure to initialize
114 * results in this error code.
115 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100116psa_status_t psa_get_key_lifetime(psa_key_handle_t handle,
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100117 psa_key_lifetime_t *lifetime);
118
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100119
Gilles Peskinef535eb22018-11-30 14:08:36 +0100120/** Allocate a key slot for a transient key, i.e. a key which is only stored
121 * in volatile memory.
122 *
123 * The allocated key slot and its handle remain valid until the
124 * application calls psa_close_key() or psa_destroy_key() or until the
125 * application terminates.
126 *
127 * This function takes a key type and maximum size as arguments so that
128 * the implementation can reserve a corresponding amount of memory.
129 * Implementations are not required to enforce this limit: if the application
130 * later tries to create a larger key or a key of a different type, it
131 * is implementation-defined whether this may succeed.
132 *
133 * \param type The type of key that the slot will contain.
134 * \param max_bits The maximum key size that the slot will contain.
135 * \param[out] handle On success, a handle to a volatile key slot.
136 *
137 * \retval #PSA_SUCCESS
138 * Success. The application can now use the value of `*handle`
139 * to access the newly allocated key slot.
140 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
141 * There was not enough memory, or the maximum number of key slots
142 * has been reached.
143 * \retval #PSA_ERROR_INVALID_ARGUMENT
144 * This implementation does not support this key type.
145 */
146
147psa_status_t psa_allocate_key(psa_key_type_t type,
148 size_t max_bits,
149 psa_key_handle_t *handle);
150
151/** Open a handle to an existing persistent key.
152 *
153 * Open a handle to a key which was previously created with psa_create_key().
154 *
155 * \param lifetime The lifetime of the key. This designates a storage
156 * area where the key material is stored. This must not
157 * be #PSA_KEY_LIFETIME_VOLATILE.
158 * \param id The persistent identifier of the key.
159 * \param[out] handle On success, a handle to a key slot which contains
160 * the data and metadata loaded from the specified
161 * persistent location.
162 *
163 * \retval #PSA_SUCCESS
164 * Success. The application can now use the value of `*handle`
165 * to access the newly allocated key slot.
166 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
167 * \retval #PSA_ERROR_EMPTY_SLOT
168 * \retval #PSA_ERROR_INVALID_ARGUMENT
169 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
170 * \retval #PSA_ERROR_INVALID_ARGUMENT
171 * \p id is invalid for the specified lifetime.
172 * \retval #PSA_ERROR_NOT_SUPPORTED
173 * \p lifetime is not supported.
174 * \retval #PSA_ERROR_NOT_PERMITTED
175 * The specified key exists, but the application does not have the
176 * permission to access it. Note that this specification does not
177 * define any way to create such a key, but it may be possible
178 * through implementation-specific means.
179 */
180psa_status_t psa_open_key(psa_key_lifetime_t lifetime,
181 psa_key_id_t id,
182 psa_key_handle_t *handle);
183
184/** Create a new persistent key slot.
185 *
186 * Create a new persistent key slot and return a handle to it. The handle
187 * remains valid until the application calls psa_close_key() or terminates.
188 * The application can open the key again with psa_open_key() until it
189 * removes the key by calling psa_destroy_key().
190 *
191 * \param lifetime The lifetime of the key. This designates a storage
192 * area where the key material is stored. This must not
193 * be #PSA_KEY_LIFETIME_VOLATILE.
194 * \param id The persistent identifier of the key.
195 * \param type The type of key that the slot will contain.
196 * \param max_bits The maximum key size that the slot will contain.
197 * \param[out] handle On success, a handle to the newly created key slot.
198 * When key material is later created in this key slot,
199 * it will be saved to the specified persistent location.
200 *
201 * \retval #PSA_SUCCESS
202 * Success. The application can now use the value of `*handle`
203 * to access the newly allocated key slot.
204 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
205 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
206 * \retval #PSA_ERROR_OCCUPIED_SLOT
207 * There is already a key with the identifier \p id in the storage
208 * area designated by \p lifetime.
209 * \retval #PSA_ERROR_INVALID_ARGUMENT
210 * \p lifetime is invalid, for example #PSA_KEY_LIFETIME_VOLATILE.
211 * \retval #PSA_ERROR_INVALID_ARGUMENT
212 * \p id is invalid for the specified lifetime.
213 * \retval #PSA_ERROR_NOT_SUPPORTED
214 * \p lifetime is not supported.
215 * \retval #PSA_ERROR_NOT_PERMITTED
216 * \p lifetime is valid, but the application does not have the
217 * permission to create a key there.
218 */
219psa_status_t psa_create_key(psa_key_lifetime_t lifetime,
220 psa_key_id_t id,
221 psa_key_type_t type,
222 size_t max_bits,
223 psa_key_handle_t *handle);
224
225/** Close a key handle.
226 *
227 * If the handle designates a volatile key, destroy the key material and
228 * free all associated resources, just like psa_destroy_key().
229 *
230 * If the handle designates a persistent key, free all resources associated
231 * with the key in volatile memory. The key slot in persistent storage is
232 * not affected and can be opened again later with psa_open_key().
233 *
234 * \param handle The key handle to close.
235 *
236 * \retval #PSA_SUCCESS
237 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100238 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100239 */
240psa_status_t psa_close_key(psa_key_handle_t handle);
241
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100242/**@}*/
243
244/** \defgroup import_export Key import and export
245 * @{
246 */
247
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100248/**
249 * \brief Import a key in binary format.
250 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100251 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100252 * documentation of psa_export_public_key() for the format of public keys
253 * and to the documentation of psa_export_key() for the format for
254 * other key types.
255 *
256 * This specification supports a single format for each key type.
257 * Implementations may support other formats as long as the standard
258 * format is supported. Implementations that support other formats
259 * should ensure that the formats are clearly unambiguous so as to
260 * minimize the risk that an invalid input is accidentally interpreted
261 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100262 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100263 * \param handle Handle to the slot where the key will be stored.
264 * This must be a valid slot for a key of the chosen
265 * type: it must have been obtained by calling
266 * psa_allocate_key() or psa_create_key() with the
267 * correct \p type and with a maximum size that is
268 * compatible with \p data.
Gilles Peskinef7933932018-10-31 14:07:52 +0100269 * \param type Key type (a \c PSA_KEY_TYPE_XXX value). On a successful
270 * import, the key slot will contain a key of this type.
271 * \param[in] data Buffer containing the key data. The content of this
272 * buffer is interpreted according to \p type. It must
273 * contain the format described in the documentation
274 * of psa_export_key() or psa_export_public_key() for
275 * the chosen type.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200276 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100277 *
Gilles Peskine28538492018-07-11 17:34:00 +0200278 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100279 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100280 * If the key is persistent, the key material and the key's metadata
281 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100282 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200283 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200284 * The key type or key size is not supported, either by the
285 * implementation in general or in this particular slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200286 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine308b91d2018-02-08 09:47:44 +0100287 * The key slot is invalid,
288 * or the key data is not correctly formatted.
Gilles Peskine28538492018-07-11 17:34:00 +0200289 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskine65eb8582018-04-19 08:28:58 +0200290 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200291 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
292 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
293 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100294 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200295 * \retval #PSA_ERROR_HARDWARE_FAILURE
296 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300297 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300298 * The library has not been previously initialized by psa_crypto_init().
299 * It is implementation-dependent whether a failure to initialize
300 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100301 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100302psa_status_t psa_import_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100303 psa_key_type_t type,
304 const uint8_t *data,
305 size_t data_length);
306
307/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100308 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200309 *
310 * This function destroys the content of the key slot from both volatile
311 * memory and, if applicable, non-volatile storage. Implementations shall
312 * make a best effort to ensure that any previous content of the slot is
313 * unrecoverable.
314 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100315 * This function also erases any metadata such as policies and frees all
316 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200317 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100318 * \param handle Handle to the key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100319 *
Gilles Peskine28538492018-07-11 17:34:00 +0200320 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200321 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200322 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200323 * The slot holds content and cannot be erased because it is
324 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100325 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200326 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200327 * There was an failure in communication with the cryptoprocessor.
328 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200329 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200330 * The storage is corrupted. Implementations shall make a best effort
331 * to erase key material even in this stage, however applications
332 * should be aware that it may be impossible to guarantee that the
333 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +0200334 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200335 * An unexpected condition which is not a storage corruption or
336 * a communication failure occurred. The cryptoprocessor may have
337 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300338 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300339 * The library has not been previously initialized by psa_crypto_init().
340 * It is implementation-dependent whether a failure to initialize
341 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100342 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100343psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100344
345/**
346 * \brief Get basic metadata about a key.
347 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100348 * \param handle Handle to the key slot to query.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200349 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100350 * This may be a null pointer, in which case the key type
351 * is not written.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200352 * \param[out] bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +0100353 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +0100354 * is not written.
355 *
Gilles Peskine28538492018-07-11 17:34:00 +0200356 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100357 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200358 * \retval #PSA_ERROR_EMPTY_SLOT
Gilles Peskineae32aac2018-11-30 14:39:32 +0100359 * The handle is to a key slot which does not contain key material yet.
Gilles Peskine28538492018-07-11 17:34:00 +0200360 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
361 * \retval #PSA_ERROR_HARDWARE_FAILURE
362 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300363 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300364 * The library has not been previously initialized by psa_crypto_init().
365 * It is implementation-dependent whether a failure to initialize
366 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100367 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100368psa_status_t psa_get_key_information(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100369 psa_key_type_t *type,
370 size_t *bits);
371
372/**
373 * \brief Export a key in binary format.
374 *
375 * The output of this function can be passed to psa_import_key() to
376 * create an equivalent object.
377 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100378 * If the implementation of psa_import_key() supports other formats
379 * beyond the format specified here, the output from psa_export_key()
380 * must use the representation specified here, not the original
381 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100382 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100383 * For standard key types, the output format is as follows:
384 *
385 * - For symmetric keys (including MAC keys), the format is the
386 * raw bytes of the key.
387 * - For DES, the key data consists of 8 bytes. The parity bits must be
388 * correct.
389 * - For Triple-DES, the format is the concatenation of the
390 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100391 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200392 * is the non-encrypted DER encoding of the representation defined by
393 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
394 * ```
395 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200396 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200397 * modulus INTEGER, -- n
398 * publicExponent INTEGER, -- e
399 * privateExponent INTEGER, -- d
400 * prime1 INTEGER, -- p
401 * prime2 INTEGER, -- q
402 * exponent1 INTEGER, -- d mod (p-1)
403 * exponent2 INTEGER, -- d mod (q-1)
404 * coefficient INTEGER, -- (inverse of q) mod p
405 * }
406 * ```
407 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format
408 * is the non-encrypted DER encoding of the representation used by
Gilles Peskinec6290c02018-08-13 17:24:59 +0200409 * OpenSSL and OpenSSH, whose structure is described in ASN.1 as follows:
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200410 * ```
411 * DSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200412 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200413 * prime INTEGER, -- p
414 * subprime INTEGER, -- q
415 * generator INTEGER, -- g
416 * public INTEGER, -- y
417 * private INTEGER, -- x
418 * }
419 * ```
420 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +0100421 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100422 * a representation of the private value as a `ceiling(m/8)`-byte string
423 * where `m` is the bit size associated with the curve, i.e. the bit size
424 * of the order of the curve's coordinate field. This byte string is
425 * in little-endian order for Montgomery curves (curve types
426 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
427 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
428 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100429 * This is the content of the `privateKey` field of the `ECPrivateKey`
430 * format defined by RFC 5915.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200431 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
432 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100433 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100434 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200435 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200436 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200437 * \param[out] data_length On success, the number of bytes
438 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100439 *
Gilles Peskine28538492018-07-11 17:34:00 +0200440 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100441 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200442 * \retval #PSA_ERROR_EMPTY_SLOT
443 * \retval #PSA_ERROR_NOT_PERMITTED
Darryl Green9e2d7a02018-07-24 16:33:30 +0100444 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200445 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
446 * The size of the \p data buffer is too small. You can determine a
447 * sufficient buffer size by calling
448 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
449 * where \c type is the key type
450 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200451 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
452 * \retval #PSA_ERROR_HARDWARE_FAILURE
453 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300454 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300455 * The library has not been previously initialized by psa_crypto_init().
456 * It is implementation-dependent whether a failure to initialize
457 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100458 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100459psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100460 uint8_t *data,
461 size_t data_size,
462 size_t *data_length);
463
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100464/**
465 * \brief Export a public key or the public part of a key pair in binary format.
466 *
467 * The output of this function can be passed to psa_import_key() to
468 * create an object that is equivalent to the public key.
469 *
Jaeden Amerod3a0c2c2019-01-11 17:15:56 +0000470 * This specification supports a single format for each key type.
471 * Implementations may support other formats as long as the standard
472 * format is supported. Implementations that support other formats
473 * should ensure that the formats are clearly unambiguous so as to
474 * minimize the risk that an invalid input is accidentally interpreted
475 * according to a different format.
476 *
Jaeden Amero6b196002019-01-10 10:23:21 +0000477 * For standard key types, the output format is as follows:
478 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY), the DER encoding of
479 * the representation defined by RFC 3279 &sect;2.3.1 as `RSAPublicKey`.
480 * ```
481 * RSAPublicKey ::= SEQUENCE {
482 * modulus INTEGER, -- n
483 * publicExponent INTEGER } -- e
484 * ```
485 *
486 * For other public key types, the format is the DER representation defined by
487 * RFC 5280 as `SubjectPublicKeyInfo`, with the `subjectPublicKey` format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200488 * specified below.
489 * ```
490 * SubjectPublicKeyInfo ::= SEQUENCE {
491 * algorithm AlgorithmIdentifier,
492 * subjectPublicKey BIT STRING }
493 * AlgorithmIdentifier ::= SEQUENCE {
494 * algorithm OBJECT IDENTIFIER,
495 * parameters ANY DEFINED BY algorithm OPTIONAL }
496 * ```
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200497 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY),
498 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.2 as
499 * `DSAPublicKey`,
500 * with the OID `id-dsa`,
501 * and with the parameters `DSS-Parms`.
502 * ```
503 * id-dsa OBJECT IDENTIFIER ::= {
504 * iso(1) member-body(2) us(840) x9-57(10040) x9cm(4) 1 }
505 *
506 * Dss-Parms ::= SEQUENCE {
507 * p INTEGER,
508 * q INTEGER,
509 * g INTEGER }
510 * DSAPublicKey ::= INTEGER -- public key, Y
511 * ```
512 * - For elliptic curve public keys (key types for which
513 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true),
514 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.5 as
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200515 * `ECPoint`, which contains the uncompressed
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200516 * representation defined by SEC1 &sect;2.3.3.
517 * The OID is `id-ecPublicKey`,
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200518 * and the parameters must be given as a `namedCurve` OID as specified in
Gilles Peskinec6290c02018-08-13 17:24:59 +0200519 * RFC 5480 &sect;2.1.1.1 or other applicable standards.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200520 * ```
521 * ansi-X9-62 OBJECT IDENTIFIER ::=
522 * { iso(1) member-body(2) us(840) 10045 }
523 * id-public-key-type OBJECT IDENTIFIER ::= { ansi-X9.62 2 }
524 * id-ecPublicKey OBJECT IDENTIFIER ::= { id-publicKeyType 1 }
525 *
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200526 * ECPoint ::= ...
527 * -- first 8 bits: 0x04;
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100528 * -- then x_P as a `ceiling(m/8)`-byte string, big endian;
529 * -- then y_P as a `ceiling(m/8)`-byte string, big endian;
530 * -- where `m` is the bit size associated with the curve,
Gilles Peskine7b5b4a02018-11-14 21:05:10 +0100531 * -- i.e. the bit size of `q` for a curve over `F_q`.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200532 *
533 * EcpkParameters ::= CHOICE { -- other choices are not allowed
534 * namedCurve OBJECT IDENTIFIER }
535 * ```
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100536 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100537 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200538 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200539 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200540 * \param[out] data_length On success, the number of bytes
541 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100542 *
Gilles Peskine28538492018-07-11 17:34:00 +0200543 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100544 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200545 * \retval #PSA_ERROR_EMPTY_SLOT
546 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200547 * The key is neither a public key nor a key pair.
548 * \retval #PSA_ERROR_NOT_SUPPORTED
549 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
550 * The size of the \p data buffer is too small. You can determine a
551 * sufficient buffer size by calling
552 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
553 * where \c type is the key type
554 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200555 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
556 * \retval #PSA_ERROR_HARDWARE_FAILURE
557 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300558 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300559 * The library has not been previously initialized by psa_crypto_init().
560 * It is implementation-dependent whether a failure to initialize
561 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100562 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100563psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100564 uint8_t *data,
565 size_t data_size,
566 size_t *data_length);
567
568/**@}*/
569
570/** \defgroup policy Key policies
571 * @{
572 */
573
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100574/** The type of the key policy data structure.
575 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000576 * Before calling any function on a key policy, the application must initialize
577 * it by any of the following means:
578 * - Set the structure to all-bits-zero, for example:
579 * \code
580 * psa_key_policy_t policy;
581 * memset(&policy, 0, sizeof(policy));
582 * \endcode
583 * - Initialize the structure to logical zero values, for example:
584 * \code
585 * psa_key_policy_t policy = {0};
586 * \endcode
587 * - Initialize the structure to the initializer #PSA_KEY_POLICY_INIT,
588 * for example:
589 * \code
590 * psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
591 * \endcode
592 * - Assign the result of the function psa_key_policy_init()
593 * to the structure, for example:
594 * \code
595 * psa_key_policy_t policy;
596 * policy = psa_key_policy_init();
597 * \endcode
598 *
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100599 * This is an implementation-defined \c struct. Applications should not
600 * make any assumptions about the content of this structure except
601 * as directed by the documentation of a specific implementation. */
602typedef struct psa_key_policy_s psa_key_policy_t;
603
Jaeden Amero70261c52019-01-04 11:47:20 +0000604/** \def PSA_KEY_POLICY_INIT
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200605 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000606 * This macro returns a suitable initializer for a key policy object of type
607 * #psa_key_policy_t.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200608 */
Jaeden Amero70261c52019-01-04 11:47:20 +0000609#ifdef __DOXYGEN_ONLY__
610/* This is an example definition for documentation purposes.
611 * Implementations should define a suitable value in `crypto_struct.h`.
612 */
613#define PSA_KEY_POLICY_INIT {0}
614#endif
615
616/** Return an initial value for a key policy that forbids all usage of the key.
617 */
618static psa_key_policy_t psa_key_policy_init(void);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100619
Gilles Peskine7e198532018-03-08 07:50:30 +0100620/** \brief Set the standard fields of a policy structure.
621 *
622 * Note that this function does not make any consistency check of the
623 * parameters. The values are only checked when applying the policy to
624 * a key slot with psa_set_key_policy().
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200625 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000626 * \param[in,out] policy The key policy to modify. It must have been
627 * initialized as per the documentation for
628 * #psa_key_policy_t.
629 * \param usage The permitted uses for the key.
630 * \param alg The algorithm that the key may be used for.
Gilles Peskine7e198532018-03-08 07:50:30 +0100631 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100632void psa_key_policy_set_usage(psa_key_policy_t *policy,
633 psa_key_usage_t usage,
634 psa_algorithm_t alg);
635
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200636/** \brief Retrieve the usage field of a policy structure.
637 *
638 * \param[in] policy The policy object to query.
639 *
640 * \return The permitted uses for a key with this policy.
641 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +0200642psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100643
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200644/** \brief Retrieve the algorithm field of a policy structure.
645 *
646 * \param[in] policy The policy object to query.
647 *
648 * \return The permitted algorithm for a key with this policy.
649 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +0200650psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100651
652/** \brief Set the usage policy on a key slot.
653 *
654 * This function must be called on an empty key slot, before importing,
655 * generating or creating a key in the slot. Changing the policy of an
656 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +0100657 *
658 * Implementations may set restrictions on supported key policies
659 * depending on the key type and the key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200660 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100661 * \param handle Handle to the key whose policy is to be changed.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200662 * \param[in] policy The policy object to query.
663 *
664 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100665 * Success.
666 * If the key is persistent, it is implementation-defined whether
667 * the policy has been saved to persistent storage. Implementations
668 * may defer saving the policy until the key material is created.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100669 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200670 * \retval #PSA_ERROR_OCCUPIED_SLOT
671 * \retval #PSA_ERROR_NOT_SUPPORTED
672 * \retval #PSA_ERROR_INVALID_ARGUMENT
673 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
674 * \retval #PSA_ERROR_HARDWARE_FAILURE
675 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300676 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300677 * The library has not been previously initialized by psa_crypto_init().
678 * It is implementation-dependent whether a failure to initialize
679 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100680 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100681psa_status_t psa_set_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100682 const psa_key_policy_t *policy);
683
Gilles Peskine7e198532018-03-08 07:50:30 +0100684/** \brief Get the usage policy for a key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200685 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100686 * \param handle Handle to the key slot whose policy is being queried.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200687 * \param[out] policy On success, the key's policy.
688 *
689 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100690 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200691 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
692 * \retval #PSA_ERROR_HARDWARE_FAILURE
693 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300694 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300695 * The library has not been previously initialized by psa_crypto_init().
696 * It is implementation-dependent whether a failure to initialize
697 * results in this error code.
Gilles Peskine7e198532018-03-08 07:50:30 +0100698 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100699psa_status_t psa_get_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100700 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +0100701
702/**@}*/
703
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100704/** \defgroup hash Message digests
705 * @{
706 */
707
Gilles Peskine308b91d2018-02-08 09:47:44 +0100708/** The type of the state data structure for multipart hash operations.
709 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000710 * Before calling any function on a hash operation object, the application must
711 * initialize it by any of the following means:
712 * - Set the structure to all-bits-zero, for example:
713 * \code
714 * psa_hash_operation_t operation;
715 * memset(&operation, 0, sizeof(operation));
716 * \endcode
717 * - Initialize the structure to logical zero values, for example:
718 * \code
719 * psa_hash_operation_t operation = {0};
720 * \endcode
721 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
722 * for example:
723 * \code
724 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
725 * \endcode
726 * - Assign the result of the function psa_hash_operation_init()
727 * to the structure, for example:
728 * \code
729 * psa_hash_operation_t operation;
730 * operation = psa_hash_operation_init();
731 * \endcode
732 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100733 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100734 * make any assumptions about the content of this structure except
735 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100736typedef struct psa_hash_operation_s psa_hash_operation_t;
737
Jaeden Amero6a25b412019-01-04 11:47:44 +0000738/** \def PSA_HASH_OPERATION_INIT
739 *
740 * This macro returns a suitable initializer for a hash operation object
741 * of type #psa_hash_operation_t.
742 */
743#ifdef __DOXYGEN_ONLY__
744/* This is an example definition for documentation purposes.
745 * Implementations should define a suitable value in `crypto_struct.h`.
746 */
747#define PSA_HASH_OPERATION_INIT {0}
748#endif
749
750/** Return an initial value for a hash operation object.
751 */
752static psa_hash_operation_t psa_hash_operation_init(void);
753
Gilles Peskine308b91d2018-02-08 09:47:44 +0100754/** Start a multipart hash operation.
755 *
756 * The sequence of operations to calculate a hash (message digest)
757 * is as follows:
758 * -# Allocate an operation object which will be passed to all the functions
759 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000760 * -# Initialize the operation object with one of the methods described in the
761 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200762 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100763 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100764 * of the message each time. The hash that is calculated is the hash
765 * of the concatenation of these messages in order.
766 * -# To calculate the hash, call psa_hash_finish().
767 * To compare the hash with an expected value, call psa_hash_verify().
768 *
769 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000770 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100771 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200772 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100773 * eventually terminate the operation. The following events terminate an
774 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100775 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100776 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100777 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000778 * \param[in,out] operation The operation object to set up. It must have
779 * been initialized as per the documentation for
780 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200781 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
782 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100783 *
Gilles Peskine28538492018-07-11 17:34:00 +0200784 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100785 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200786 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200787 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +0200788 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
789 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
790 * \retval #PSA_ERROR_HARDWARE_FAILURE
791 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100792 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200793psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100794 psa_algorithm_t alg);
795
Gilles Peskine308b91d2018-02-08 09:47:44 +0100796/** Add a message fragment to a multipart hash operation.
797 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200798 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100799 *
800 * If this function returns an error status, the operation becomes inactive.
801 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200802 * \param[in,out] operation Active hash operation.
803 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200804 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100805 *
Gilles Peskine28538492018-07-11 17:34:00 +0200806 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100807 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200808 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100809 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200810 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
811 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
812 * \retval #PSA_ERROR_HARDWARE_FAILURE
813 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100814 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100815psa_status_t psa_hash_update(psa_hash_operation_t *operation,
816 const uint8_t *input,
817 size_t input_length);
818
Gilles Peskine308b91d2018-02-08 09:47:44 +0100819/** Finish the calculation of the hash of a message.
820 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200821 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100822 * This function calculates the hash of the message formed by concatenating
823 * the inputs passed to preceding calls to psa_hash_update().
824 *
825 * When this function returns, the operation becomes inactive.
826 *
827 * \warning Applications should not call this function if they expect
828 * a specific value for the hash. Call psa_hash_verify() instead.
829 * Beware that comparing integrity or authenticity data such as
830 * hash values with a function such as \c memcmp is risky
831 * because the time taken by the comparison may leak information
832 * about the hashed data which could allow an attacker to guess
833 * a valid hash and thereby bypass security controls.
834 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200835 * \param[in,out] operation Active hash operation.
836 * \param[out] hash Buffer where the hash is to be written.
837 * \param hash_size Size of the \p hash buffer in bytes.
838 * \param[out] hash_length On success, the number of bytes
839 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +0200840 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +0200841 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100842 *
Gilles Peskine28538492018-07-11 17:34:00 +0200843 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100844 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200845 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100846 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200847 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200848 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200849 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100850 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +0200851 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
852 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
853 * \retval #PSA_ERROR_HARDWARE_FAILURE
854 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100855 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100856psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
857 uint8_t *hash,
858 size_t hash_size,
859 size_t *hash_length);
860
Gilles Peskine308b91d2018-02-08 09:47:44 +0100861/** Finish the calculation of the hash of a message and compare it with
862 * an expected value.
863 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200864 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100865 * This function calculates the hash of the message formed by concatenating
866 * the inputs passed to preceding calls to psa_hash_update(). It then
867 * compares the calculated hash with the expected hash passed as a
868 * parameter to this function.
869 *
870 * When this function returns, the operation becomes inactive.
871 *
Gilles Peskine19067982018-03-20 17:54:53 +0100872 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +0100873 * comparison between the actual hash and the expected hash is performed
874 * in constant time.
875 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200876 * \param[in,out] operation Active hash operation.
877 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200878 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100879 *
Gilles Peskine28538492018-07-11 17:34:00 +0200880 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100881 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +0200882 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100883 * The hash of the message was calculated successfully, but it
884 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +0200885 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100886 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200887 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
888 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
889 * \retval #PSA_ERROR_HARDWARE_FAILURE
890 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100891 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100892psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
893 const uint8_t *hash,
894 size_t hash_length);
895
Gilles Peskine308b91d2018-02-08 09:47:44 +0100896/** Abort a hash operation.
897 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100898 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200899 * \p operation structure itself. Once aborted, the operation object
900 * can be reused for another operation by calling
901 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100902 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200903 * You may call this function any time after the operation object has
904 * been initialized by any of the following methods:
905 * - A call to psa_hash_setup(), whether it succeeds or not.
906 * - Initializing the \c struct to all-bits-zero.
907 * - Initializing the \c struct to logical zeros, e.g.
908 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100909 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200910 * In particular, calling psa_hash_abort() after the operation has been
911 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
912 * psa_hash_verify() is safe and has no effect.
913 *
914 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100915 *
Gilles Peskine28538492018-07-11 17:34:00 +0200916 * \retval #PSA_SUCCESS
917 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200918 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +0200919 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
920 * \retval #PSA_ERROR_HARDWARE_FAILURE
921 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100922 */
923psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100924
925/**@}*/
926
Gilles Peskine8c9def32018-02-08 10:02:12 +0100927/** \defgroup MAC Message authentication codes
928 * @{
929 */
930
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100931/** The type of the state data structure for multipart MAC operations.
932 *
Jaeden Amero769ce272019-01-04 11:48:03 +0000933 * Before calling any function on a MAC operation object, the application must
934 * initialize it by any of the following means:
935 * - Set the structure to all-bits-zero, for example:
936 * \code
937 * psa_mac_operation_t operation;
938 * memset(&operation, 0, sizeof(operation));
939 * \endcode
940 * - Initialize the structure to logical zero values, for example:
941 * \code
942 * psa_mac_operation_t operation = {0};
943 * \endcode
944 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
945 * for example:
946 * \code
947 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
948 * \endcode
949 * - Assign the result of the function psa_mac_operation_init()
950 * to the structure, for example:
951 * \code
952 * psa_mac_operation_t operation;
953 * operation = psa_mac_operation_init();
954 * \endcode
955 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100956 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100957 * make any assumptions about the content of this structure except
958 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100959typedef struct psa_mac_operation_s psa_mac_operation_t;
960
Jaeden Amero769ce272019-01-04 11:48:03 +0000961/** \def PSA_MAC_OPERATION_INIT
962 *
963 * This macro returns a suitable initializer for a MAC operation object of type
964 * #psa_mac_operation_t.
965 */
966#ifdef __DOXYGEN_ONLY__
967/* This is an example definition for documentation purposes.
968 * Implementations should define a suitable value in `crypto_struct.h`.
969 */
970#define PSA_MAC_OPERATION_INIT {0}
971#endif
972
973/** Return an initial value for a MAC operation object.
974 */
975static psa_mac_operation_t psa_mac_operation_init(void);
976
Gilles Peskine89167cb2018-07-08 20:12:23 +0200977/** Start a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100978 *
Gilles Peskine89167cb2018-07-08 20:12:23 +0200979 * This function sets up the calculation of the MAC
980 * (message authentication code) of a byte string.
981 * To verify the MAC of a message against an
982 * expected value, use psa_mac_verify_setup() instead.
983 *
984 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100985 * -# Allocate an operation object which will be passed to all the functions
986 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +0000987 * -# Initialize the operation object with one of the methods described in the
988 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +0200989 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100990 * The key remains associated with the operation even if the content
991 * of the key slot changes.
992 * -# Call psa_mac_update() zero, one or more times, passing a fragment
993 * of the message each time. The MAC that is calculated is the MAC
994 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +0200995 * -# At the end of the message, call psa_mac_sign_finish() to finish
996 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100997 *
998 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +0000999 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001000 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001001 * After a successful call to psa_mac_sign_setup(), the application must
1002 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001003 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001004 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001005 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001006 * \param[in,out] operation The operation object to set up. It must have
1007 * been initialized as per the documentation for
1008 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001009 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001010 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1011 * such that #PSA_ALG_IS_MAC(alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001012 *
Gilles Peskine28538492018-07-11 17:34:00 +02001013 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001014 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001015 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001016 * \retval #PSA_ERROR_EMPTY_SLOT
1017 * \retval #PSA_ERROR_NOT_PERMITTED
1018 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001019 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001020 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001021 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001022 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1023 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1024 * \retval #PSA_ERROR_HARDWARE_FAILURE
1025 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001026 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001027 * The library has not been previously initialized by psa_crypto_init().
1028 * It is implementation-dependent whether a failure to initialize
1029 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001030 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001031psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001032 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001033 psa_algorithm_t alg);
1034
1035/** Start a multipart MAC verification operation.
1036 *
1037 * This function sets up the verification of the MAC
1038 * (message authentication code) of a byte string against an expected value.
1039 *
1040 * The sequence of operations to verify a MAC is as follows:
1041 * -# Allocate an operation object which will be passed to all the functions
1042 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001043 * -# Initialize the operation object with one of the methods described in the
1044 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001045 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
1046 * The key remains associated with the operation even if the content
1047 * of the key slot changes.
1048 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1049 * of the message each time. The MAC that is calculated is the MAC
1050 * of the concatenation of these messages in order.
1051 * -# At the end of the message, call psa_mac_verify_finish() to finish
1052 * calculating the actual MAC of the message and verify it against
1053 * the expected value.
1054 *
1055 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001056 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001057 *
1058 * After a successful call to psa_mac_verify_setup(), the application must
1059 * eventually terminate the operation through one of the following methods:
1060 * - A failed call to psa_mac_update().
1061 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1062 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001063 * \param[in,out] operation The operation object to set up. It must have
1064 * been initialized as per the documentation for
1065 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001066 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001067 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1068 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001069 *
Gilles Peskine28538492018-07-11 17:34:00 +02001070 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001071 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001072 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001073 * \retval #PSA_ERROR_EMPTY_SLOT
1074 * \retval #PSA_ERROR_NOT_PERMITTED
1075 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001076 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001077 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001078 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001079 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1080 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1081 * \retval #PSA_ERROR_HARDWARE_FAILURE
1082 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001083 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001084 * The library has not been previously initialized by psa_crypto_init().
1085 * It is implementation-dependent whether a failure to initialize
1086 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001087 */
1088psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001089 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001090 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001091
Gilles Peskinedcd14942018-07-12 00:30:52 +02001092/** Add a message fragment to a multipart MAC operation.
1093 *
1094 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1095 * before calling this function.
1096 *
1097 * If this function returns an error status, the operation becomes inactive.
1098 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001099 * \param[in,out] operation Active MAC operation.
1100 * \param[in] input Buffer containing the message fragment to add to
1101 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001102 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001103 *
1104 * \retval #PSA_SUCCESS
1105 * Success.
1106 * \retval #PSA_ERROR_BAD_STATE
1107 * The operation state is not valid (not started, or already completed).
1108 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1109 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1110 * \retval #PSA_ERROR_HARDWARE_FAILURE
1111 * \retval #PSA_ERROR_TAMPERING_DETECTED
1112 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001113psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1114 const uint8_t *input,
1115 size_t input_length);
1116
Gilles Peskinedcd14942018-07-12 00:30:52 +02001117/** Finish the calculation of the MAC of a message.
1118 *
1119 * The application must call psa_mac_sign_setup() before calling this function.
1120 * This function calculates the MAC of the message formed by concatenating
1121 * the inputs passed to preceding calls to psa_mac_update().
1122 *
1123 * When this function returns, the operation becomes inactive.
1124 *
1125 * \warning Applications should not call this function if they expect
1126 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1127 * Beware that comparing integrity or authenticity data such as
1128 * MAC values with a function such as \c memcmp is risky
1129 * because the time taken by the comparison may leak information
1130 * about the MAC value which could allow an attacker to guess
1131 * a valid MAC and thereby bypass security controls.
1132 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001133 * \param[in,out] operation Active MAC operation.
1134 * \param[out] mac Buffer where the MAC value is to be written.
1135 * \param mac_size Size of the \p mac buffer in bytes.
1136 * \param[out] mac_length On success, the number of bytes
1137 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001138 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001139 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001140 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001141 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001142 *
1143 * \retval #PSA_SUCCESS
1144 * Success.
1145 * \retval #PSA_ERROR_BAD_STATE
1146 * The operation state is not valid (not started, or already completed).
1147 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001148 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001149 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1150 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1151 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1152 * \retval #PSA_ERROR_HARDWARE_FAILURE
1153 * \retval #PSA_ERROR_TAMPERING_DETECTED
1154 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001155psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1156 uint8_t *mac,
1157 size_t mac_size,
1158 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001159
Gilles Peskinedcd14942018-07-12 00:30:52 +02001160/** Finish the calculation of the MAC of a message and compare it with
1161 * an expected value.
1162 *
1163 * The application must call psa_mac_verify_setup() before calling this function.
1164 * This function calculates the MAC of the message formed by concatenating
1165 * the inputs passed to preceding calls to psa_mac_update(). It then
1166 * compares the calculated MAC with the expected MAC passed as a
1167 * parameter to this function.
1168 *
1169 * When this function returns, the operation becomes inactive.
1170 *
1171 * \note Implementations shall make the best effort to ensure that the
1172 * comparison between the actual MAC and the expected MAC is performed
1173 * in constant time.
1174 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001175 * \param[in,out] operation Active MAC operation.
1176 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001177 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001178 *
1179 * \retval #PSA_SUCCESS
1180 * The expected MAC is identical to the actual MAC of the message.
1181 * \retval #PSA_ERROR_INVALID_SIGNATURE
1182 * The MAC of the message was calculated successfully, but it
1183 * differs from the expected MAC.
1184 * \retval #PSA_ERROR_BAD_STATE
1185 * The operation state is not valid (not started, or already completed).
1186 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1187 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1188 * \retval #PSA_ERROR_HARDWARE_FAILURE
1189 * \retval #PSA_ERROR_TAMPERING_DETECTED
1190 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001191psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1192 const uint8_t *mac,
1193 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001194
Gilles Peskinedcd14942018-07-12 00:30:52 +02001195/** Abort a MAC operation.
1196 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001197 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001198 * \p operation structure itself. Once aborted, the operation object
1199 * can be reused for another operation by calling
1200 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001201 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001202 * You may call this function any time after the operation object has
1203 * been initialized by any of the following methods:
1204 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1205 * it succeeds or not.
1206 * - Initializing the \c struct to all-bits-zero.
1207 * - Initializing the \c struct to logical zeros, e.g.
1208 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001209 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001210 * In particular, calling psa_mac_abort() after the operation has been
1211 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1212 * psa_mac_verify_finish() is safe and has no effect.
1213 *
1214 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001215 *
1216 * \retval #PSA_SUCCESS
1217 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001218 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001219 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1220 * \retval #PSA_ERROR_HARDWARE_FAILURE
1221 * \retval #PSA_ERROR_TAMPERING_DETECTED
1222 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001223psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1224
1225/**@}*/
1226
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001227/** \defgroup cipher Symmetric ciphers
1228 * @{
1229 */
1230
1231/** The type of the state data structure for multipart cipher operations.
1232 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001233 * Before calling any function on a cipher operation object, the application
1234 * must initialize it by any of the following means:
1235 * - Set the structure to all-bits-zero, for example:
1236 * \code
1237 * psa_cipher_operation_t operation;
1238 * memset(&operation, 0, sizeof(operation));
1239 * \endcode
1240 * - Initialize the structure to logical zero values, for example:
1241 * \code
1242 * psa_cipher_operation_t operation = {0};
1243 * \endcode
1244 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1245 * for example:
1246 * \code
1247 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1248 * \endcode
1249 * - Assign the result of the function psa_cipher_operation_init()
1250 * to the structure, for example:
1251 * \code
1252 * psa_cipher_operation_t operation;
1253 * operation = psa_cipher_operation_init();
1254 * \endcode
1255 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001256 * This is an implementation-defined \c struct. Applications should not
1257 * make any assumptions about the content of this structure except
1258 * as directed by the documentation of a specific implementation. */
1259typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1260
Jaeden Amero5bae2272019-01-04 11:48:27 +00001261/** \def PSA_CIPHER_OPERATION_INIT
1262 *
1263 * This macro returns a suitable initializer for a cipher operation object of
1264 * type #psa_cipher_operation_t.
1265 */
1266#ifdef __DOXYGEN_ONLY__
1267/* This is an example definition for documentation purposes.
1268 * Implementations should define a suitable value in `crypto_struct.h`.
1269 */
1270#define PSA_CIPHER_OPERATION_INIT {0}
1271#endif
1272
1273/** Return an initial value for a cipher operation object.
1274 */
1275static psa_cipher_operation_t psa_cipher_operation_init(void);
1276
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001277/** Set the key for a multipart symmetric encryption operation.
1278 *
1279 * The sequence of operations to encrypt a message with a symmetric cipher
1280 * is as follows:
1281 * -# Allocate an operation object which will be passed to all the functions
1282 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001283 * -# Initialize the operation object with one of the methods described in the
1284 * documentation for #psa_cipher_operation_t, e.g.
1285 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001286 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001287 * The key remains associated with the operation even if the content
1288 * of the key slot changes.
itayzafrired7382f2018-08-02 14:19:33 +03001289 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001290 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001291 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001292 * requires a specific IV value.
1293 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1294 * of the message each time.
1295 * -# Call psa_cipher_finish().
1296 *
1297 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001298 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001299 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001300 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001301 * eventually terminate the operation. The following events terminate an
1302 * operation:
itayzafrired7382f2018-08-02 14:19:33 +03001303 * - A failed call to psa_cipher_generate_iv(), psa_cipher_set_iv()
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001304 * or psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001305 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001306 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001307 * \param[in,out] operation The operation object to set up. It must have
1308 * been initialized as per the documentation for
1309 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001310 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001311 * \param alg The cipher algorithm to compute
1312 * (\c PSA_ALG_XXX value such that
1313 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001314 *
Gilles Peskine28538492018-07-11 17:34:00 +02001315 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001316 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001317 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001318 * \retval #PSA_ERROR_EMPTY_SLOT
1319 * \retval #PSA_ERROR_NOT_PERMITTED
1320 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001321 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001322 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001323 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001324 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1325 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1326 * \retval #PSA_ERROR_HARDWARE_FAILURE
1327 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001328 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001329 * The library has not been previously initialized by psa_crypto_init().
1330 * It is implementation-dependent whether a failure to initialize
1331 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001332 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001333psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001334 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001335 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001336
1337/** Set the key for a multipart symmetric decryption operation.
1338 *
1339 * The sequence of operations to decrypt a message with a symmetric cipher
1340 * is as follows:
1341 * -# Allocate an operation object which will be passed to all the functions
1342 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001343 * -# Initialize the operation object with one of the methods described in the
1344 * documentation for #psa_cipher_operation_t, e.g.
1345 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001346 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001347 * The key remains associated with the operation even if the content
1348 * of the key slot changes.
1349 * -# Call psa_cipher_update() with the IV (initialization vector) for the
1350 * decryption. If the IV is prepended to the ciphertext, you can call
1351 * psa_cipher_update() on a buffer containing the IV followed by the
1352 * beginning of the message.
1353 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1354 * of the message each time.
1355 * -# Call psa_cipher_finish().
1356 *
1357 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001358 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001359 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001360 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001361 * eventually terminate the operation. The following events terminate an
1362 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001363 * - A failed call to psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001364 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001365 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001366 * \param[in,out] operation The operation object to set up. It must have
1367 * been initialized as per the documentation for
1368 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001369 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001370 * \param alg The cipher algorithm to compute
1371 * (\c PSA_ALG_XXX value such that
1372 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001373 *
Gilles Peskine28538492018-07-11 17:34:00 +02001374 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001375 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001376 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001377 * \retval #PSA_ERROR_EMPTY_SLOT
1378 * \retval #PSA_ERROR_NOT_PERMITTED
1379 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001380 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001381 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001382 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001383 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1384 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1385 * \retval #PSA_ERROR_HARDWARE_FAILURE
1386 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001387 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001388 * The library has not been previously initialized by psa_crypto_init().
1389 * It is implementation-dependent whether a failure to initialize
1390 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001391 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001392psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001393 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001394 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001395
Gilles Peskinedcd14942018-07-12 00:30:52 +02001396/** Generate an IV for a symmetric encryption operation.
1397 *
1398 * This function generates a random IV (initialization vector), nonce
1399 * or initial counter value for the encryption operation as appropriate
1400 * for the chosen algorithm, key type and key size.
1401 *
1402 * The application must call psa_cipher_encrypt_setup() before
1403 * calling this function.
1404 *
1405 * If this function returns an error status, the operation becomes inactive.
1406 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001407 * \param[in,out] operation Active cipher operation.
1408 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001409 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001410 * \param[out] iv_length On success, the number of bytes of the
1411 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001412 *
1413 * \retval #PSA_SUCCESS
1414 * Success.
1415 * \retval #PSA_ERROR_BAD_STATE
1416 * The operation state is not valid (not started, or IV already set).
1417 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001418 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001419 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1420 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1421 * \retval #PSA_ERROR_HARDWARE_FAILURE
1422 * \retval #PSA_ERROR_TAMPERING_DETECTED
1423 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001424psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1425 unsigned char *iv,
1426 size_t iv_size,
1427 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001428
Gilles Peskinedcd14942018-07-12 00:30:52 +02001429/** Set the IV for a symmetric encryption or decryption operation.
1430 *
1431 * This function sets the random IV (initialization vector), nonce
1432 * or initial counter value for the encryption or decryption operation.
1433 *
1434 * The application must call psa_cipher_encrypt_setup() before
1435 * calling this function.
1436 *
1437 * If this function returns an error status, the operation becomes inactive.
1438 *
1439 * \note When encrypting, applications should use psa_cipher_generate_iv()
1440 * instead of this function, unless implementing a protocol that requires
1441 * a non-random IV.
1442 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001443 * \param[in,out] operation Active cipher operation.
1444 * \param[in] iv Buffer containing the IV to use.
1445 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001446 *
1447 * \retval #PSA_SUCCESS
1448 * Success.
1449 * \retval #PSA_ERROR_BAD_STATE
1450 * The operation state is not valid (not started, or IV already set).
1451 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001452 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001453 * or the chosen algorithm does not use an IV.
1454 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1455 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1456 * \retval #PSA_ERROR_HARDWARE_FAILURE
1457 * \retval #PSA_ERROR_TAMPERING_DETECTED
1458 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001459psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1460 const unsigned char *iv,
1461 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001462
Gilles Peskinedcd14942018-07-12 00:30:52 +02001463/** Encrypt or decrypt a message fragment in an active cipher operation.
1464 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001465 * Before calling this function, you must:
1466 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1467 * The choice of setup function determines whether this function
1468 * encrypts or decrypts its input.
1469 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1470 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001471 *
1472 * If this function returns an error status, the operation becomes inactive.
1473 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001474 * \param[in,out] operation Active cipher operation.
1475 * \param[in] input Buffer containing the message fragment to
1476 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001477 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001478 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001479 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001480 * \param[out] output_length On success, the number of bytes
1481 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001482 *
1483 * \retval #PSA_SUCCESS
1484 * Success.
1485 * \retval #PSA_ERROR_BAD_STATE
1486 * The operation state is not valid (not started, IV required but
1487 * not set, or already completed).
1488 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1489 * The size of the \p output buffer is too small.
1490 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1491 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1492 * \retval #PSA_ERROR_HARDWARE_FAILURE
1493 * \retval #PSA_ERROR_TAMPERING_DETECTED
1494 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001495psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1496 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001497 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001498 unsigned char *output,
1499 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001500 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001501
Gilles Peskinedcd14942018-07-12 00:30:52 +02001502/** Finish encrypting or decrypting a message in a cipher operation.
1503 *
1504 * The application must call psa_cipher_encrypt_setup() or
1505 * psa_cipher_decrypt_setup() before calling this function. The choice
1506 * of setup function determines whether this function encrypts or
1507 * decrypts its input.
1508 *
1509 * This function finishes the encryption or decryption of the message
1510 * formed by concatenating the inputs passed to preceding calls to
1511 * psa_cipher_update().
1512 *
1513 * When this function returns, the operation becomes inactive.
1514 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001515 * \param[in,out] operation Active cipher operation.
1516 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001517 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001518 * \param[out] output_length On success, the number of bytes
1519 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001520 *
1521 * \retval #PSA_SUCCESS
1522 * Success.
1523 * \retval #PSA_ERROR_BAD_STATE
1524 * The operation state is not valid (not started, IV required but
1525 * not set, or already completed).
1526 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1527 * The size of the \p output buffer is too small.
1528 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1529 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1530 * \retval #PSA_ERROR_HARDWARE_FAILURE
1531 * \retval #PSA_ERROR_TAMPERING_DETECTED
1532 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001533psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001534 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001535 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001536 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001537
Gilles Peskinedcd14942018-07-12 00:30:52 +02001538/** Abort a cipher operation.
1539 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001540 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001541 * \p operation structure itself. Once aborted, the operation object
1542 * can be reused for another operation by calling
1543 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001544 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001545 * You may call this function any time after the operation object has
1546 * been initialized by any of the following methods:
1547 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1548 * whether it succeeds or not.
1549 * - Initializing the \c struct to all-bits-zero.
1550 * - Initializing the \c struct to logical zeros, e.g.
1551 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001552 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001553 * In particular, calling psa_cipher_abort() after the operation has been
1554 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1555 * is safe and has no effect.
1556 *
1557 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001558 *
1559 * \retval #PSA_SUCCESS
1560 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001561 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001562 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1563 * \retval #PSA_ERROR_HARDWARE_FAILURE
1564 * \retval #PSA_ERROR_TAMPERING_DETECTED
1565 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001566psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1567
1568/**@}*/
1569
Gilles Peskine3b555712018-03-03 21:27:57 +01001570/** \defgroup aead Authenticated encryption with associated data (AEAD)
1571 * @{
1572 */
1573
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001574/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001575 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001576 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001577 * \param alg The AEAD algorithm to compute
1578 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001579 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001580 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001581 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001582 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001583 * but not encrypted.
1584 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001585 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001586 * encrypted.
1587 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001588 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001589 * encrypted data. The additional data is not
1590 * part of this output. For algorithms where the
1591 * encrypted data and the authentication tag
1592 * are defined as separate outputs, the
1593 * authentication tag is appended to the
1594 * encrypted data.
1595 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1596 * This must be at least
1597 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1598 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001599 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001600 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001601 *
Gilles Peskine28538492018-07-11 17:34:00 +02001602 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001603 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001604 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001605 * \retval #PSA_ERROR_EMPTY_SLOT
1606 * \retval #PSA_ERROR_NOT_PERMITTED
1607 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001608 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001609 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001610 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001611 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1612 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1613 * \retval #PSA_ERROR_HARDWARE_FAILURE
1614 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001615 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001616 * The library has not been previously initialized by psa_crypto_init().
1617 * It is implementation-dependent whether a failure to initialize
1618 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001619 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001620psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001621 psa_algorithm_t alg,
1622 const uint8_t *nonce,
1623 size_t nonce_length,
1624 const uint8_t *additional_data,
1625 size_t additional_data_length,
1626 const uint8_t *plaintext,
1627 size_t plaintext_length,
1628 uint8_t *ciphertext,
1629 size_t ciphertext_size,
1630 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001631
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001632/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001633 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001634 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001635 * \param alg The AEAD algorithm to compute
1636 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001637 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001638 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001639 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001640 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001641 * but not encrypted.
1642 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001643 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001644 * encrypted. For algorithms where the
1645 * encrypted data and the authentication tag
1646 * are defined as separate inputs, the buffer
1647 * must contain the encrypted data followed
1648 * by the authentication tag.
1649 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001650 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001651 * \param plaintext_size Size of the \p plaintext buffer in bytes.
1652 * This must be at least
1653 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
1654 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001655 * \param[out] plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03001656 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001657 *
Gilles Peskine28538492018-07-11 17:34:00 +02001658 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001659 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001660 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001661 * \retval #PSA_ERROR_EMPTY_SLOT
1662 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001663 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02001664 * \retval #PSA_ERROR_NOT_PERMITTED
1665 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001666 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001667 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001668 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001669 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1670 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1671 * \retval #PSA_ERROR_HARDWARE_FAILURE
1672 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001673 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001674 * The library has not been previously initialized by psa_crypto_init().
1675 * It is implementation-dependent whether a failure to initialize
1676 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001677 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001678psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001679 psa_algorithm_t alg,
1680 const uint8_t *nonce,
1681 size_t nonce_length,
1682 const uint8_t *additional_data,
1683 size_t additional_data_length,
1684 const uint8_t *ciphertext,
1685 size_t ciphertext_length,
1686 uint8_t *plaintext,
1687 size_t plaintext_size,
1688 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001689
1690/**@}*/
1691
Gilles Peskine20035e32018-02-03 22:44:14 +01001692/** \defgroup asymmetric Asymmetric cryptography
1693 * @{
1694 */
1695
1696/**
1697 * \brief Sign a hash or short message with a private key.
1698 *
Gilles Peskine08bac712018-06-26 16:14:46 +02001699 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001700 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02001701 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
1702 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
1703 * to determine the hash algorithm to use.
1704 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001705 * \param handle Handle to the key to use for the operation.
1706 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001707 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001708 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001709 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001710 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001711 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001712 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001713 * \param[out] signature_length On success, the number of bytes
1714 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001715 *
Gilles Peskine28538492018-07-11 17:34:00 +02001716 * \retval #PSA_SUCCESS
1717 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001718 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01001719 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001720 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001721 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001722 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001723 * \retval #PSA_ERROR_NOT_SUPPORTED
1724 * \retval #PSA_ERROR_INVALID_ARGUMENT
1725 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1726 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1727 * \retval #PSA_ERROR_HARDWARE_FAILURE
1728 * \retval #PSA_ERROR_TAMPERING_DETECTED
1729 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03001730 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001731 * The library has not been previously initialized by psa_crypto_init().
1732 * It is implementation-dependent whether a failure to initialize
1733 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01001734 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001735psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01001736 psa_algorithm_t alg,
1737 const uint8_t *hash,
1738 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01001739 uint8_t *signature,
1740 size_t signature_size,
1741 size_t *signature_length);
1742
1743/**
1744 * \brief Verify the signature a hash or short message using a public key.
1745 *
Gilles Peskine08bac712018-06-26 16:14:46 +02001746 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001747 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02001748 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
1749 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
1750 * to determine the hash algorithm to use.
1751 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001752 * \param handle Handle to the key to use for the operation.
1753 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001754 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001755 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001756 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02001757 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001758 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001759 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001760 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001761 *
Gilles Peskine28538492018-07-11 17:34:00 +02001762 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001763 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02001764 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001765 * The calculation was perfomed successfully, but the passed
1766 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02001767 * \retval #PSA_ERROR_NOT_SUPPORTED
1768 * \retval #PSA_ERROR_INVALID_ARGUMENT
1769 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1770 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1771 * \retval #PSA_ERROR_HARDWARE_FAILURE
1772 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001773 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001774 * The library has not been previously initialized by psa_crypto_init().
1775 * It is implementation-dependent whether a failure to initialize
1776 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01001777 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001778psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01001779 psa_algorithm_t alg,
1780 const uint8_t *hash,
1781 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02001782 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02001783 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01001784
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001785/**
1786 * \brief Encrypt a short message with a public key.
1787 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001788 * \param handle Handle to the key to use for the operation.
1789 * It must be a public key or an asymmetric
1790 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001791 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001792 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001793 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001794 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001795 * \param[in] salt A salt or label, if supported by the
1796 * encryption algorithm.
1797 * If the algorithm does not support a
1798 * salt, pass \c NULL.
1799 * If the algorithm supports an optional
1800 * salt and you do not want to pass a salt,
1801 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001802 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001803 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1804 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001805 * \param salt_length Size of the \p salt buffer in bytes.
1806 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001807 * \param[out] output Buffer where the encrypted message is to
1808 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001809 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001810 * \param[out] output_length On success, the number of bytes
1811 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001812 *
Gilles Peskine28538492018-07-11 17:34:00 +02001813 * \retval #PSA_SUCCESS
1814 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001815 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001816 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001817 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001818 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001819 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001820 * \retval #PSA_ERROR_NOT_SUPPORTED
1821 * \retval #PSA_ERROR_INVALID_ARGUMENT
1822 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1823 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1824 * \retval #PSA_ERROR_HARDWARE_FAILURE
1825 * \retval #PSA_ERROR_TAMPERING_DETECTED
1826 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03001827 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001828 * The library has not been previously initialized by psa_crypto_init().
1829 * It is implementation-dependent whether a failure to initialize
1830 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001831 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001832psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001833 psa_algorithm_t alg,
1834 const uint8_t *input,
1835 size_t input_length,
1836 const uint8_t *salt,
1837 size_t salt_length,
1838 uint8_t *output,
1839 size_t output_size,
1840 size_t *output_length);
1841
1842/**
1843 * \brief Decrypt a short message with a private key.
1844 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001845 * \param handle Handle to the key to use for the operation.
1846 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001847 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001848 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001849 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001850 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001851 * \param[in] salt A salt or label, if supported by the
1852 * encryption algorithm.
1853 * If the algorithm does not support a
1854 * salt, pass \c NULL.
1855 * If the algorithm supports an optional
1856 * salt and you do not want to pass a salt,
1857 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001858 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001859 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1860 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001861 * \param salt_length Size of the \p salt buffer in bytes.
1862 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001863 * \param[out] output Buffer where the decrypted message is to
1864 * be written.
1865 * \param output_size Size of the \c output buffer in bytes.
1866 * \param[out] output_length On success, the number of bytes
1867 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001868 *
Gilles Peskine28538492018-07-11 17:34:00 +02001869 * \retval #PSA_SUCCESS
1870 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001871 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001872 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001873 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001874 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001875 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001876 * \retval #PSA_ERROR_NOT_SUPPORTED
1877 * \retval #PSA_ERROR_INVALID_ARGUMENT
1878 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1879 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1880 * \retval #PSA_ERROR_HARDWARE_FAILURE
1881 * \retval #PSA_ERROR_TAMPERING_DETECTED
1882 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
1883 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03001884 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001885 * The library has not been previously initialized by psa_crypto_init().
1886 * It is implementation-dependent whether a failure to initialize
1887 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001888 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001889psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001890 psa_algorithm_t alg,
1891 const uint8_t *input,
1892 size_t input_length,
1893 const uint8_t *salt,
1894 size_t salt_length,
1895 uint8_t *output,
1896 size_t output_size,
1897 size_t *output_length);
1898
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001899/**@}*/
1900
Gilles Peskineedd76872018-07-20 17:42:05 +02001901/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02001902 * @{
1903 */
1904
1905/** The type of the state data structure for generators.
1906 *
1907 * Before calling any function on a generator, the application must
1908 * initialize it by any of the following means:
1909 * - Set the structure to all-bits-zero, for example:
1910 * \code
1911 * psa_crypto_generator_t generator;
1912 * memset(&generator, 0, sizeof(generator));
1913 * \endcode
1914 * - Initialize the structure to logical zero values, for example:
1915 * \code
1916 * psa_crypto_generator_t generator = {0};
1917 * \endcode
1918 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
1919 * for example:
1920 * \code
1921 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1922 * \endcode
1923 * - Assign the result of the function psa_crypto_generator_init()
1924 * to the structure, for example:
1925 * \code
1926 * psa_crypto_generator_t generator;
1927 * generator = psa_crypto_generator_init();
1928 * \endcode
1929 *
1930 * This is an implementation-defined \c struct. Applications should not
1931 * make any assumptions about the content of this structure except
1932 * as directed by the documentation of a specific implementation.
1933 */
1934typedef struct psa_crypto_generator_s psa_crypto_generator_t;
1935
1936/** \def PSA_CRYPTO_GENERATOR_INIT
1937 *
1938 * This macro returns a suitable initializer for a generator object
1939 * of type #psa_crypto_generator_t.
1940 */
1941#ifdef __DOXYGEN_ONLY__
1942/* This is an example definition for documentation purposes.
1943 * Implementations should define a suitable value in `crypto_struct.h`.
1944 */
1945#define PSA_CRYPTO_GENERATOR_INIT {0}
1946#endif
1947
1948/** Return an initial value for a generator object.
1949 */
1950static psa_crypto_generator_t psa_crypto_generator_init(void);
1951
1952/** Retrieve the current capacity of a generator.
1953 *
1954 * The capacity of a generator is the maximum number of bytes that it can
1955 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
1956 *
1957 * \param[in] generator The generator to query.
1958 * \param[out] capacity On success, the capacity of the generator.
1959 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001960 * \retval #PSA_SUCCESS
1961 * \retval #PSA_ERROR_BAD_STATE
1962 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02001963 */
1964psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
1965 size_t *capacity);
1966
1967/** Read some data from a generator.
1968 *
1969 * This function reads and returns a sequence of bytes from a generator.
1970 * The data that is read is discarded from the generator. The generator's
1971 * capacity is decreased by the number of bytes read.
1972 *
1973 * \param[in,out] generator The generator object to read from.
1974 * \param[out] output Buffer where the generator output will be
1975 * written.
1976 * \param output_length Number of bytes to output.
1977 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001978 * \retval #PSA_SUCCESS
1979 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02001980 * There were fewer than \p output_length bytes
1981 * in the generator. Note that in this case, no
1982 * output is written to the output buffer.
1983 * The generator's capacity is set to 0, thus
1984 * subsequent calls to this function will not
1985 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001986 * \retval #PSA_ERROR_BAD_STATE
1987 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1988 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1989 * \retval #PSA_ERROR_HARDWARE_FAILURE
1990 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02001991 */
1992psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
1993 uint8_t *output,
1994 size_t output_length);
1995
1996/** Create a symmetric key from data read from a generator.
1997 *
1998 * This function reads a sequence of bytes from a generator and imports
1999 * these bytes as a key.
2000 * The data that is read is discarded from the generator. The generator's
2001 * capacity is decreased by the number of bytes read.
2002 *
2003 * This function is equivalent to calling #psa_generator_read and
2004 * passing the resulting output to #psa_import_key, but
2005 * if the implementation provides an isolation boundary then
2006 * the key material is not exposed outside the isolation boundary.
2007 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002008 * \param handle Handle to the slot where the key will be stored.
2009 * This must be a valid slot for a key of the chosen
2010 * type: it must have been obtained by calling
2011 * psa_allocate_key() or psa_create_key() with the
2012 * correct \p type and with a maximum size that is
2013 * compatible with \p bits.
2014 * It must not contain any key material yet.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002015 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2016 * This must be a symmetric key type.
2017 * \param bits Key size in bits.
2018 * \param[in,out] generator The generator object to read from.
2019 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002020 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02002021 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01002022 * If the key is persistent, the key material and the key's metadata
2023 * have been saved to persistent storage.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002024 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02002025 * There were fewer than \p output_length bytes
2026 * in the generator. Note that in this case, no
2027 * output is written to the output buffer.
2028 * The generator's capacity is set to 0, thus
2029 * subsequent calls to this function will not
2030 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002031 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002032 * The key type or key size is not supported, either by the
2033 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002034 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineae32aac2018-11-30 14:39:32 +01002035 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002036 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskineeab56e42018-07-12 17:12:33 +02002037 * There is already a key in the specified slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002038 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2039 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
2040 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2041 * \retval #PSA_ERROR_HARDWARE_FAILURE
2042 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002043 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002044 * The library has not been previously initialized by psa_crypto_init().
2045 * It is implementation-dependent whether a failure to initialize
2046 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002047 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002048psa_status_t psa_generator_import_key(psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002049 psa_key_type_t type,
2050 size_t bits,
2051 psa_crypto_generator_t *generator);
2052
2053/** Abort a generator.
2054 *
2055 * Once a generator has been aborted, its capacity is zero.
2056 * Aborting a generator frees all associated resources except for the
2057 * \c generator structure itself.
2058 *
2059 * This function may be called at any time as long as the generator
2060 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
2061 * psa_crypto_generator_init() or a zero value. In particular, it is valid
2062 * to call psa_generator_abort() twice, or to call psa_generator_abort()
2063 * on a generator that has not been set up.
2064 *
2065 * Once aborted, the generator object may be called.
2066 *
2067 * \param[in,out] generator The generator to abort.
2068 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002069 * \retval #PSA_SUCCESS
2070 * \retval #PSA_ERROR_BAD_STATE
2071 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2072 * \retval #PSA_ERROR_HARDWARE_FAILURE
2073 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002074 */
2075psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
2076
Gilles Peskine8feb3a82018-09-18 12:06:11 +02002077/** Use the maximum possible capacity for a generator.
2078 *
2079 * Use this value as the capacity argument when setting up a generator
2080 * to indicate that the generator should have the maximum possible capacity.
2081 * The value of the maximum possible capacity depends on the generator
2082 * algorithm.
2083 */
2084#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
2085
Gilles Peskineeab56e42018-07-12 17:12:33 +02002086/**@}*/
2087
Gilles Peskineea0fb492018-07-12 17:17:20 +02002088/** \defgroup derivation Key derivation
2089 * @{
2090 */
2091
2092/** Set up a key derivation operation.
2093 *
2094 * A key derivation algorithm takes three inputs: a secret input \p key and
2095 * two non-secret inputs \p label and p salt.
2096 * The result of this function is a byte generator which can
2097 * be used to produce keys and other cryptographic material.
2098 *
2099 * The role of \p label and \p salt is as follows:
Gilles Peskinebef7f142018-07-12 17:22:21 +02002100 * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step
2101 * and \p label is the info string used in the "expand" step.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002102 *
Jaeden Amero9e919c62019-01-07 15:41:50 +00002103 * \param[in,out] generator The generator object to set up. It must have
2104 * been initialized as per the documentation for
2105 * #psa_crypto_generator_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002106 * \param handle Handle to the secret key.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002107 * \param alg The key derivation algorithm to compute
2108 * (\c PSA_ALG_XXX value such that
2109 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2110 * \param[in] salt Salt to use.
2111 * \param salt_length Size of the \p salt buffer in bytes.
2112 * \param[in] label Label to use.
2113 * \param label_length Size of the \p label buffer in bytes.
2114 * \param capacity The maximum number of bytes that the
2115 * generator will be able to provide.
2116 *
2117 * \retval #PSA_SUCCESS
2118 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002119 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineea0fb492018-07-12 17:17:20 +02002120 * \retval #PSA_ERROR_EMPTY_SLOT
2121 * \retval #PSA_ERROR_NOT_PERMITTED
2122 * \retval #PSA_ERROR_INVALID_ARGUMENT
2123 * \c key is not compatible with \c alg,
2124 * or \p capacity is too large for the specified algorithm and key.
2125 * \retval #PSA_ERROR_NOT_SUPPORTED
2126 * \c alg is not supported or is not a key derivation algorithm.
2127 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2128 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2129 * \retval #PSA_ERROR_HARDWARE_FAILURE
2130 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002131 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002132 * The library has not been previously initialized by psa_crypto_init().
2133 * It is implementation-dependent whether a failure to initialize
2134 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002135 */
2136psa_status_t psa_key_derivation(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002137 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02002138 psa_algorithm_t alg,
2139 const uint8_t *salt,
2140 size_t salt_length,
2141 const uint8_t *label,
2142 size_t label_length,
2143 size_t capacity);
2144
Gilles Peskine01d718c2018-09-18 12:01:02 +02002145/** Set up a key agreement operation.
2146 *
2147 * A key agreement algorithm takes two inputs: a private key \p private_key
2148 * a public key \p peer_key.
2149 * The result of this function is a byte generator which can
2150 * be used to produce keys and other cryptographic material.
2151 *
Gilles Peskine211a4362018-10-25 22:22:31 +02002152 * The resulting generator always has the maximum capacity permitted by
2153 * the algorithm.
2154 *
Jaeden Amero9e919c62019-01-07 15:41:50 +00002155 * \param[in,out] generator The generator object to set up. It must have
2156 * been initialized as per the documentation for
2157 * #psa_crypto_generator_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002158 * \param private_key Handle to the private key to use.
Gilles Peskined171e782018-11-15 17:46:21 +01002159 * \param[in] peer_key Public key of the peer. It must be
2160 * in the same format that psa_import_key()
2161 * accepts. The standard formats for public
2162 * keys are documented in the documentation
2163 * of psa_export_public_key().
Gilles Peskine01d718c2018-09-18 12:01:02 +02002164 * \param peer_key_length Size of \p peer_key in bytes.
2165 * \param alg The key agreement algorithm to compute
2166 * (\c PSA_ALG_XXX value such that
2167 * #PSA_ALG_IS_KEY_AGREEMENT(\p alg) is true).
2168 *
2169 * \retval #PSA_SUCCESS
2170 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002171 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine01d718c2018-09-18 12:01:02 +02002172 * \retval #PSA_ERROR_EMPTY_SLOT
2173 * \retval #PSA_ERROR_NOT_PERMITTED
2174 * \retval #PSA_ERROR_INVALID_ARGUMENT
2175 * \c private_key is not compatible with \c alg,
2176 * or \p peer_key is not valid for \c alg or not compatible with
2177 * \c private_key.
2178 * \retval #PSA_ERROR_NOT_SUPPORTED
2179 * \c alg is not supported or is not a key derivation algorithm.
2180 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2181 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2182 * \retval #PSA_ERROR_HARDWARE_FAILURE
2183 * \retval #PSA_ERROR_TAMPERING_DETECTED
2184 */
2185psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002186 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02002187 const uint8_t *peer_key,
2188 size_t peer_key_length,
2189 psa_algorithm_t alg);
2190
Gilles Peskineea0fb492018-07-12 17:17:20 +02002191/**@}*/
2192
Gilles Peskineedd76872018-07-20 17:42:05 +02002193/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002194 * @{
2195 */
2196
2197/**
2198 * \brief Generate random bytes.
2199 *
2200 * \warning This function **can** fail! Callers MUST check the return status
2201 * and MUST NOT use the content of the output buffer if the return
2202 * status is not #PSA_SUCCESS.
2203 *
2204 * \note To generate a key, use psa_generate_key() instead.
2205 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002206 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002207 * \param output_size Number of bytes to generate and output.
2208 *
Gilles Peskine28538492018-07-11 17:34:00 +02002209 * \retval #PSA_SUCCESS
2210 * \retval #PSA_ERROR_NOT_SUPPORTED
2211 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2212 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2213 * \retval #PSA_ERROR_HARDWARE_FAILURE
2214 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03002215 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002216 * The library has not been previously initialized by psa_crypto_init().
2217 * It is implementation-dependent whether a failure to initialize
2218 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002219 */
2220psa_status_t psa_generate_random(uint8_t *output,
2221 size_t output_size);
2222
Gilles Peskine4c317f42018-07-12 01:24:09 +02002223/** Extra parameters for RSA key generation.
2224 *
Gilles Peskinebe42f312018-07-13 14:38:15 +02002225 * You may pass a pointer to a structure of this type as the \c extra
Gilles Peskine4c317f42018-07-12 01:24:09 +02002226 * parameter to psa_generate_key().
2227 */
2228typedef struct {
Gilles Peskineedd76872018-07-20 17:42:05 +02002229 uint32_t e; /**< Public exponent value. Default: 65537. */
Gilles Peskine4c317f42018-07-12 01:24:09 +02002230} psa_generate_key_extra_rsa;
2231
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002232/**
2233 * \brief Generate a key or key pair.
2234 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002235 * \param handle Handle to the slot where the key will be stored.
2236 * This must be a valid slot for a key of the chosen
2237 * type: it must have been obtained by calling
2238 * psa_allocate_key() or psa_create_key() with the
2239 * correct \p type and with a maximum size that is
2240 * compatible with \p bits.
2241 * It must not contain any key material yet.
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002242 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2243 * \param bits Key size in bits.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002244 * \param[in] extra Extra parameters for key generation. The
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002245 * interpretation of this parameter depends on
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002246 * \p type. All types support \c NULL to use
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002247 * default parameters. Implementation that support
2248 * the generation of vendor-specific key types
2249 * that allow extra parameters shall document
2250 * the format of these extra parameters and
2251 * the default values. For standard parameters,
2252 * the meaning of \p extra is as follows:
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002253 * - For a symmetric key type (a type such
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002254 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
2255 * false), \p extra must be \c NULL.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002256 * - For an elliptic curve key type (a type
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002257 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
2258 * false), \p extra must be \c NULL.
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002259 * - For an RSA key (\p type is
2260 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
2261 * optional #psa_generate_key_extra_rsa structure
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002262 * specifying the public exponent. The
2263 * default public exponent used when \p extra
2264 * is \c NULL is 65537.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002265 * \param extra_size Size of the buffer that \p extra
2266 * points to, in bytes. Note that if \p extra is
2267 * \c NULL then \p extra_size must be zero.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002268 *
Gilles Peskine28538492018-07-11 17:34:00 +02002269 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01002270 * Success.
2271 * If the key is persistent, the key material and the key's metadata
2272 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002273 * \retval #PSA_ERROR_INVALID_HANDLE
2274 * \retval #PSA_ERROR_OCCUPIED_SLOT
2275 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +02002276 * \retval #PSA_ERROR_NOT_SUPPORTED
2277 * \retval #PSA_ERROR_INVALID_ARGUMENT
2278 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2279 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2280 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2281 * \retval #PSA_ERROR_HARDWARE_FAILURE
2282 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002283 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002284 * The library has not been previously initialized by psa_crypto_init().
2285 * It is implementation-dependent whether a failure to initialize
2286 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002287 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002288psa_status_t psa_generate_key(psa_key_handle_t handle,
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002289 psa_key_type_t type,
2290 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02002291 const void *extra,
2292 size_t extra_size);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002293
2294/**@}*/
2295
Gilles Peskinee59236f2018-01-27 23:32:46 +01002296#ifdef __cplusplus
2297}
2298#endif
2299
Gilles Peskine0cad07c2018-06-27 19:49:02 +02002300/* The file "crypto_sizes.h" contains definitions for size calculation
2301 * macros whose definitions are implementation-specific. */
2302#include "crypto_sizes.h"
2303
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002304/* The file "crypto_struct.h" contains definitions for
2305 * implementation-specific structs that are declared above. */
2306#include "crypto_struct.h"
2307
2308/* The file "crypto_extra.h" contains vendor-specific definitions. This
2309 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01002310#include "crypto_extra.h"
2311
2312#endif /* PSA_CRYPTO_H */