blob: ce35e07c78bb94e2ceb20e4ee54fc2980f3989b1 [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 *
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200470 * The format is the DER representation defined by RFC 5280 as
471 * `SubjectPublicKeyInfo`, with the `subjectPublicKey` format
472 * specified below.
473 * ```
474 * SubjectPublicKeyInfo ::= SEQUENCE {
475 * algorithm AlgorithmIdentifier,
476 * subjectPublicKey BIT STRING }
477 * AlgorithmIdentifier ::= SEQUENCE {
478 * algorithm OBJECT IDENTIFIER,
479 * parameters ANY DEFINED BY algorithm OPTIONAL }
480 * ```
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100481 *
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200482 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY),
483 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.1 as
484 * `RSAPublicKey`,
485 * with the OID `rsaEncryption`,
486 * and with the parameters `NULL`.
487 * ```
488 * pkcs-1 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840)
489 * rsadsi(113549) pkcs(1) 1 }
490 * rsaEncryption OBJECT IDENTIFIER ::= { pkcs-1 1 }
491 *
492 * RSAPublicKey ::= SEQUENCE {
493 * modulus INTEGER, -- n
494 * publicExponent INTEGER } -- e
495 * ```
496 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY),
497 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.2 as
498 * `DSAPublicKey`,
499 * with the OID `id-dsa`,
500 * and with the parameters `DSS-Parms`.
501 * ```
502 * id-dsa OBJECT IDENTIFIER ::= {
503 * iso(1) member-body(2) us(840) x9-57(10040) x9cm(4) 1 }
504 *
505 * Dss-Parms ::= SEQUENCE {
506 * p INTEGER,
507 * q INTEGER,
508 * g INTEGER }
509 * DSAPublicKey ::= INTEGER -- public key, Y
510 * ```
511 * - For elliptic curve public keys (key types for which
512 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true),
513 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.5 as
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200514 * `ECPoint`, which contains the uncompressed
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200515 * representation defined by SEC1 &sect;2.3.3.
516 * The OID is `id-ecPublicKey`,
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200517 * and the parameters must be given as a `namedCurve` OID as specified in
Gilles Peskinec6290c02018-08-13 17:24:59 +0200518 * RFC 5480 &sect;2.1.1.1 or other applicable standards.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200519 * ```
520 * ansi-X9-62 OBJECT IDENTIFIER ::=
521 * { iso(1) member-body(2) us(840) 10045 }
522 * id-public-key-type OBJECT IDENTIFIER ::= { ansi-X9.62 2 }
523 * id-ecPublicKey OBJECT IDENTIFIER ::= { id-publicKeyType 1 }
524 *
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200525 * ECPoint ::= ...
526 * -- first 8 bits: 0x04;
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100527 * -- then x_P as a `ceiling(m/8)`-byte string, big endian;
528 * -- then y_P as a `ceiling(m/8)`-byte string, big endian;
529 * -- where `m` is the bit size associated with the curve,
Gilles Peskine7b5b4a02018-11-14 21:05:10 +0100530 * -- i.e. the bit size of `q` for a curve over `F_q`.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200531 *
532 * EcpkParameters ::= CHOICE { -- other choices are not allowed
533 * namedCurve OBJECT IDENTIFIER }
534 * ```
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100535 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100536 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200537 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200538 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200539 * \param[out] data_length On success, the number of bytes
540 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100541 *
Gilles Peskine28538492018-07-11 17:34:00 +0200542 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100543 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200544 * \retval #PSA_ERROR_EMPTY_SLOT
545 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200546 * The key is neither a public key nor a key pair.
547 * \retval #PSA_ERROR_NOT_SUPPORTED
548 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
549 * The size of the \p data buffer is too small. You can determine a
550 * sufficient buffer size by calling
551 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
552 * where \c type is the key type
553 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200554 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
555 * \retval #PSA_ERROR_HARDWARE_FAILURE
556 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300557 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300558 * The library has not been previously initialized by psa_crypto_init().
559 * It is implementation-dependent whether a failure to initialize
560 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100561 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100562psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100563 uint8_t *data,
564 size_t data_size,
565 size_t *data_length);
566
567/**@}*/
568
569/** \defgroup policy Key policies
570 * @{
571 */
572
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100573/** The type of the key policy data structure.
574 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000575 * Before calling any function on a key policy, the application must initialize
576 * it by any of the following means:
577 * - Set the structure to all-bits-zero, for example:
578 * \code
579 * psa_key_policy_t policy;
580 * memset(&policy, 0, sizeof(policy));
581 * \endcode
582 * - Initialize the structure to logical zero values, for example:
583 * \code
584 * psa_key_policy_t policy = {0};
585 * \endcode
586 * - Initialize the structure to the initializer #PSA_KEY_POLICY_INIT,
587 * for example:
588 * \code
589 * psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
590 * \endcode
591 * - Assign the result of the function psa_key_policy_init()
592 * to the structure, for example:
593 * \code
594 * psa_key_policy_t policy;
595 * policy = psa_key_policy_init();
596 * \endcode
597 *
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100598 * This is an implementation-defined \c struct. Applications should not
599 * make any assumptions about the content of this structure except
600 * as directed by the documentation of a specific implementation. */
601typedef struct psa_key_policy_s psa_key_policy_t;
602
Jaeden Amero70261c52019-01-04 11:47:20 +0000603/** \def PSA_KEY_POLICY_INIT
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200604 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000605 * This macro returns a suitable initializer for a key policy object of type
606 * #psa_key_policy_t.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200607 */
Jaeden Amero70261c52019-01-04 11:47:20 +0000608#ifdef __DOXYGEN_ONLY__
609/* This is an example definition for documentation purposes.
610 * Implementations should define a suitable value in `crypto_struct.h`.
611 */
612#define PSA_KEY_POLICY_INIT {0}
613#endif
614
615/** Return an initial value for a key policy that forbids all usage of the key.
616 */
617static psa_key_policy_t psa_key_policy_init(void);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100618
Gilles Peskine7e198532018-03-08 07:50:30 +0100619/** \brief Set the standard fields of a policy structure.
620 *
621 * Note that this function does not make any consistency check of the
622 * parameters. The values are only checked when applying the policy to
623 * a key slot with psa_set_key_policy().
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200624 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000625 * \param[in,out] policy The key policy to modify. It must have been
626 * initialized as per the documentation for
627 * #psa_key_policy_t.
628 * \param usage The permitted uses for the key.
629 * \param alg The algorithm that the key may be used for.
Gilles Peskine7e198532018-03-08 07:50:30 +0100630 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100631void psa_key_policy_set_usage(psa_key_policy_t *policy,
632 psa_key_usage_t usage,
633 psa_algorithm_t alg);
634
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200635/** \brief Retrieve the usage field of a policy structure.
636 *
637 * \param[in] policy The policy object to query.
638 *
639 * \return The permitted uses for a key with this policy.
640 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +0200641psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100642
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200643/** \brief Retrieve the algorithm field of a policy structure.
644 *
645 * \param[in] policy The policy object to query.
646 *
647 * \return The permitted algorithm for a key with this policy.
648 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +0200649psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100650
651/** \brief Set the usage policy on a key slot.
652 *
653 * This function must be called on an empty key slot, before importing,
654 * generating or creating a key in the slot. Changing the policy of an
655 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +0100656 *
657 * Implementations may set restrictions on supported key policies
658 * depending on the key type and the key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200659 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100660 * \param handle Handle to the key whose policy is to be changed.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200661 * \param[in] policy The policy object to query.
662 *
663 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100664 * Success.
665 * If the key is persistent, it is implementation-defined whether
666 * the policy has been saved to persistent storage. Implementations
667 * may defer saving the policy until the key material is created.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100668 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200669 * \retval #PSA_ERROR_OCCUPIED_SLOT
670 * \retval #PSA_ERROR_NOT_SUPPORTED
671 * \retval #PSA_ERROR_INVALID_ARGUMENT
672 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
673 * \retval #PSA_ERROR_HARDWARE_FAILURE
674 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300675 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300676 * The library has not been previously initialized by psa_crypto_init().
677 * It is implementation-dependent whether a failure to initialize
678 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100679 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100680psa_status_t psa_set_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100681 const psa_key_policy_t *policy);
682
Gilles Peskine7e198532018-03-08 07:50:30 +0100683/** \brief Get the usage policy for a key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200684 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100685 * \param handle Handle to the key slot whose policy is being queried.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200686 * \param[out] policy On success, the key's policy.
687 *
688 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100689 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200690 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
691 * \retval #PSA_ERROR_HARDWARE_FAILURE
692 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300693 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300694 * The library has not been previously initialized by psa_crypto_init().
695 * It is implementation-dependent whether a failure to initialize
696 * results in this error code.
Gilles Peskine7e198532018-03-08 07:50:30 +0100697 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100698psa_status_t psa_get_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100699 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +0100700
701/**@}*/
702
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100703/** \defgroup hash Message digests
704 * @{
705 */
706
Gilles Peskine308b91d2018-02-08 09:47:44 +0100707/** The type of the state data structure for multipart hash operations.
708 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000709 * Before calling any function on a hash operation object, the application must
710 * initialize it by any of the following means:
711 * - Set the structure to all-bits-zero, for example:
712 * \code
713 * psa_hash_operation_t operation;
714 * memset(&operation, 0, sizeof(operation));
715 * \endcode
716 * - Initialize the structure to logical zero values, for example:
717 * \code
718 * psa_hash_operation_t operation = {0};
719 * \endcode
720 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
721 * for example:
722 * \code
723 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
724 * \endcode
725 * - Assign the result of the function psa_hash_operation_init()
726 * to the structure, for example:
727 * \code
728 * psa_hash_operation_t operation;
729 * operation = psa_hash_operation_init();
730 * \endcode
731 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100732 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100733 * make any assumptions about the content of this structure except
734 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100735typedef struct psa_hash_operation_s psa_hash_operation_t;
736
Jaeden Amero6a25b412019-01-04 11:47:44 +0000737/** \def PSA_HASH_OPERATION_INIT
738 *
739 * This macro returns a suitable initializer for a hash operation object
740 * of type #psa_hash_operation_t.
741 */
742#ifdef __DOXYGEN_ONLY__
743/* This is an example definition for documentation purposes.
744 * Implementations should define a suitable value in `crypto_struct.h`.
745 */
746#define PSA_HASH_OPERATION_INIT {0}
747#endif
748
749/** Return an initial value for a hash operation object.
750 */
751static psa_hash_operation_t psa_hash_operation_init(void);
752
Gilles Peskine76d7bfe2019-01-10 11:47:49 +0100753/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100754 *
755 * The sequence of operations to calculate a hash (message digest)
756 * is as follows:
757 * -# Allocate an operation object which will be passed to all the functions
758 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000759 * -# Initialize the operation object with one of the methods described in the
760 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200761 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100762 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100763 * of the message each time. The hash that is calculated is the hash
764 * of the concatenation of these messages in order.
765 * -# To calculate the hash, call psa_hash_finish().
766 * To compare the hash with an expected value, call psa_hash_verify().
767 *
768 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000769 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100770 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200771 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100772 * eventually terminate the operation. The following events terminate an
773 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100774 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100775 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100776 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000777 * \param[in,out] operation The operation object to set up. It must have
778 * been initialized as per the documentation for
779 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200780 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
781 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100782 *
Gilles Peskine28538492018-07-11 17:34:00 +0200783 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100784 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200785 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200786 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine8e1addc2019-01-10 11:51:17 +0100787 * \retval #PSA_ERROR_BAD_STATE
788 * The operation state is not valid (already set up and not
789 * subsequently completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200790 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
791 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
792 * \retval #PSA_ERROR_HARDWARE_FAILURE
793 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100794 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200795psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100796 psa_algorithm_t alg);
797
Gilles Peskine308b91d2018-02-08 09:47:44 +0100798/** Add a message fragment to a multipart hash operation.
799 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200800 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100801 *
802 * If this function returns an error status, the operation becomes inactive.
803 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200804 * \param[in,out] operation Active hash operation.
805 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200806 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100807 *
Gilles Peskine28538492018-07-11 17:34:00 +0200808 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100809 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200810 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine76d7bfe2019-01-10 11:47:49 +0100811 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200812 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
813 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
814 * \retval #PSA_ERROR_HARDWARE_FAILURE
815 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100816 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100817psa_status_t psa_hash_update(psa_hash_operation_t *operation,
818 const uint8_t *input,
819 size_t input_length);
820
Gilles Peskine308b91d2018-02-08 09:47:44 +0100821/** Finish the calculation of the hash of a message.
822 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200823 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100824 * This function calculates the hash of the message formed by concatenating
825 * the inputs passed to preceding calls to psa_hash_update().
826 *
827 * When this function returns, the operation becomes inactive.
828 *
829 * \warning Applications should not call this function if they expect
830 * a specific value for the hash. Call psa_hash_verify() instead.
831 * Beware that comparing integrity or authenticity data such as
832 * hash values with a function such as \c memcmp is risky
833 * because the time taken by the comparison may leak information
834 * about the hashed data which could allow an attacker to guess
835 * a valid hash and thereby bypass security controls.
836 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200837 * \param[in,out] operation Active hash operation.
838 * \param[out] hash Buffer where the hash is to be written.
839 * \param hash_size Size of the \p hash buffer in bytes.
840 * \param[out] hash_length On success, the number of bytes
841 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +0200842 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +0200843 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100844 *
Gilles Peskine28538492018-07-11 17:34:00 +0200845 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100846 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200847 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine76d7bfe2019-01-10 11:47:49 +0100848 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200849 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200850 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200851 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100852 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +0200853 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
854 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
855 * \retval #PSA_ERROR_HARDWARE_FAILURE
856 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100857 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100858psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
859 uint8_t *hash,
860 size_t hash_size,
861 size_t *hash_length);
862
Gilles Peskine308b91d2018-02-08 09:47:44 +0100863/** Finish the calculation of the hash of a message and compare it with
864 * an expected value.
865 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200866 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100867 * This function calculates the hash of the message formed by concatenating
868 * the inputs passed to preceding calls to psa_hash_update(). It then
869 * compares the calculated hash with the expected hash passed as a
870 * parameter to this function.
871 *
872 * When this function returns, the operation becomes inactive.
873 *
Gilles Peskine19067982018-03-20 17:54:53 +0100874 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +0100875 * comparison between the actual hash and the expected hash is performed
876 * in constant time.
877 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200878 * \param[in,out] operation Active hash operation.
879 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200880 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100881 *
Gilles Peskine28538492018-07-11 17:34:00 +0200882 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100883 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +0200884 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100885 * The hash of the message was calculated successfully, but it
886 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +0200887 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine76d7bfe2019-01-10 11:47:49 +0100888 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200889 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
890 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
891 * \retval #PSA_ERROR_HARDWARE_FAILURE
892 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100893 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100894psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
895 const uint8_t *hash,
896 size_t hash_length);
897
Gilles Peskine308b91d2018-02-08 09:47:44 +0100898/** Abort a hash operation.
899 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100900 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200901 * \p operation structure itself. Once aborted, the operation object
902 * can be reused for another operation by calling
903 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100904 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200905 * You may call this function any time after the operation object has
906 * been initialized by any of the following methods:
907 * - A call to psa_hash_setup(), whether it succeeds or not.
908 * - Initializing the \c struct to all-bits-zero.
909 * - Initializing the \c struct to logical zeros, e.g.
910 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100911 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200912 * In particular, calling psa_hash_abort() after the operation has been
913 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
914 * psa_hash_verify() is safe and has no effect.
915 *
916 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100917 *
Gilles Peskine28538492018-07-11 17:34:00 +0200918 * \retval #PSA_SUCCESS
919 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200920 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +0200921 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
922 * \retval #PSA_ERROR_HARDWARE_FAILURE
923 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100924 */
925psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100926
927/**@}*/
928
Gilles Peskine8c9def32018-02-08 10:02:12 +0100929/** \defgroup MAC Message authentication codes
930 * @{
931 */
932
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100933/** The type of the state data structure for multipart MAC operations.
934 *
Jaeden Amero769ce272019-01-04 11:48:03 +0000935 * Before calling any function on a MAC operation object, the application must
936 * initialize it by any of the following means:
937 * - Set the structure to all-bits-zero, for example:
938 * \code
939 * psa_mac_operation_t operation;
940 * memset(&operation, 0, sizeof(operation));
941 * \endcode
942 * - Initialize the structure to logical zero values, for example:
943 * \code
944 * psa_mac_operation_t operation = {0};
945 * \endcode
946 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
947 * for example:
948 * \code
949 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
950 * \endcode
951 * - Assign the result of the function psa_mac_operation_init()
952 * to the structure, for example:
953 * \code
954 * psa_mac_operation_t operation;
955 * operation = psa_mac_operation_init();
956 * \endcode
957 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100958 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100959 * make any assumptions about the content of this structure except
960 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100961typedef struct psa_mac_operation_s psa_mac_operation_t;
962
Jaeden Amero769ce272019-01-04 11:48:03 +0000963/** \def PSA_MAC_OPERATION_INIT
964 *
965 * This macro returns a suitable initializer for a MAC operation object of type
966 * #psa_mac_operation_t.
967 */
968#ifdef __DOXYGEN_ONLY__
969/* This is an example definition for documentation purposes.
970 * Implementations should define a suitable value in `crypto_struct.h`.
971 */
972#define PSA_MAC_OPERATION_INIT {0}
973#endif
974
975/** Return an initial value for a MAC operation object.
976 */
977static psa_mac_operation_t psa_mac_operation_init(void);
978
Gilles Peskine76d7bfe2019-01-10 11:47:49 +0100979/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100980 *
Gilles Peskine89167cb2018-07-08 20:12:23 +0200981 * This function sets up the calculation of the MAC
982 * (message authentication code) of a byte string.
983 * To verify the MAC of a message against an
984 * expected value, use psa_mac_verify_setup() instead.
985 *
986 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100987 * -# Allocate an operation object which will be passed to all the functions
988 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +0000989 * -# Initialize the operation object with one of the methods described in the
990 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +0200991 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100992 * The key remains associated with the operation even if the content
993 * of the key slot changes.
994 * -# Call psa_mac_update() zero, one or more times, passing a fragment
995 * of the message each time. The MAC that is calculated is the MAC
996 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +0200997 * -# At the end of the message, call psa_mac_sign_finish() to finish
998 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100999 *
1000 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001001 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001002 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001003 * After a successful call to psa_mac_sign_setup(), the application must
1004 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001005 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001006 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001007 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001008 * \param[in,out] operation The operation object to set up. It must have
1009 * been initialized as per the documentation for
1010 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001011 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001012 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1013 * such that #PSA_ALG_IS_MAC(alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001014 *
Gilles Peskine28538492018-07-11 17:34:00 +02001015 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001016 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001017 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001018 * \retval #PSA_ERROR_EMPTY_SLOT
1019 * \retval #PSA_ERROR_NOT_PERMITTED
1020 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001021 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001022 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001023 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001024 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1025 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1026 * \retval #PSA_ERROR_HARDWARE_FAILURE
1027 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001028 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001029 * The operation state is not valid (already set up and not
1030 * subsequently completed).
1031 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001032 * The library has not been previously initialized by psa_crypto_init().
1033 * It is implementation-dependent whether a failure to initialize
1034 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001035 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001036psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001037 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001038 psa_algorithm_t alg);
1039
Gilles Peskine76d7bfe2019-01-10 11:47:49 +01001040/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001041 *
1042 * This function sets up the verification of the MAC
1043 * (message authentication code) of a byte string against an expected value.
1044 *
1045 * The sequence of operations to verify a MAC is as follows:
1046 * -# Allocate an operation object which will be passed to all the functions
1047 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001048 * -# Initialize the operation object with one of the methods described in the
1049 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001050 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
1051 * The key remains associated with the operation even if the content
1052 * of the key slot changes.
1053 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1054 * of the message each time. The MAC that is calculated is the MAC
1055 * of the concatenation of these messages in order.
1056 * -# At the end of the message, call psa_mac_verify_finish() to finish
1057 * calculating the actual MAC of the message and verify it against
1058 * the expected value.
1059 *
1060 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001061 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001062 *
1063 * After a successful call to psa_mac_verify_setup(), the application must
1064 * eventually terminate the operation through one of the following methods:
1065 * - A failed call to psa_mac_update().
1066 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1067 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001068 * \param[in,out] operation The operation object to set up. It must have
1069 * been initialized as per the documentation for
1070 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001071 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001072 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1073 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001074 *
Gilles Peskine28538492018-07-11 17:34:00 +02001075 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001076 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001077 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001078 * \retval #PSA_ERROR_EMPTY_SLOT
1079 * \retval #PSA_ERROR_NOT_PERMITTED
1080 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001081 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001082 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001083 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001084 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1085 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1086 * \retval #PSA_ERROR_HARDWARE_FAILURE
1087 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001088 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001089 * The operation state is not valid (already set up and not
1090 * subsequently completed).
1091 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001092 * The library has not been previously initialized by psa_crypto_init().
1093 * It is implementation-dependent whether a failure to initialize
1094 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001095 */
1096psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001097 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001098 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001099
Gilles Peskinedcd14942018-07-12 00:30:52 +02001100/** Add a message fragment to a multipart MAC operation.
1101 *
1102 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1103 * before calling this function.
1104 *
1105 * If this function returns an error status, the operation becomes inactive.
1106 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001107 * \param[in,out] operation Active MAC operation.
1108 * \param[in] input Buffer containing the message fragment to add to
1109 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001110 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001111 *
1112 * \retval #PSA_SUCCESS
1113 * Success.
1114 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine76d7bfe2019-01-10 11:47:49 +01001115 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001116 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1117 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1118 * \retval #PSA_ERROR_HARDWARE_FAILURE
1119 * \retval #PSA_ERROR_TAMPERING_DETECTED
1120 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001121psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1122 const uint8_t *input,
1123 size_t input_length);
1124
Gilles Peskinedcd14942018-07-12 00:30:52 +02001125/** Finish the calculation of the MAC of a message.
1126 *
1127 * The application must call psa_mac_sign_setup() before calling this function.
1128 * This function calculates the MAC of the message formed by concatenating
1129 * the inputs passed to preceding calls to psa_mac_update().
1130 *
1131 * When this function returns, the operation becomes inactive.
1132 *
1133 * \warning Applications should not call this function if they expect
1134 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1135 * Beware that comparing integrity or authenticity data such as
1136 * MAC values with a function such as \c memcmp is risky
1137 * because the time taken by the comparison may leak information
1138 * about the MAC value which could allow an attacker to guess
1139 * a valid MAC and thereby bypass security controls.
1140 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001141 * \param[in,out] operation Active MAC operation.
1142 * \param[out] mac Buffer where the MAC value is to be written.
1143 * \param mac_size Size of the \p mac buffer in bytes.
1144 * \param[out] mac_length On success, the number of bytes
1145 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001146 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001147 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001148 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001149 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001150 *
1151 * \retval #PSA_SUCCESS
1152 * Success.
1153 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine76d7bfe2019-01-10 11:47:49 +01001154 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001155 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001156 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001157 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1158 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1159 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1160 * \retval #PSA_ERROR_HARDWARE_FAILURE
1161 * \retval #PSA_ERROR_TAMPERING_DETECTED
1162 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001163psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1164 uint8_t *mac,
1165 size_t mac_size,
1166 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001167
Gilles Peskinedcd14942018-07-12 00:30:52 +02001168/** Finish the calculation of the MAC of a message and compare it with
1169 * an expected value.
1170 *
1171 * The application must call psa_mac_verify_setup() before calling this function.
1172 * This function calculates the MAC of the message formed by concatenating
1173 * the inputs passed to preceding calls to psa_mac_update(). It then
1174 * compares the calculated MAC with the expected MAC passed as a
1175 * parameter to this function.
1176 *
1177 * When this function returns, the operation becomes inactive.
1178 *
1179 * \note Implementations shall make the best effort to ensure that the
1180 * comparison between the actual MAC and the expected MAC is performed
1181 * in constant time.
1182 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001183 * \param[in,out] operation Active MAC operation.
1184 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001185 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001186 *
1187 * \retval #PSA_SUCCESS
1188 * The expected MAC is identical to the actual MAC of the message.
1189 * \retval #PSA_ERROR_INVALID_SIGNATURE
1190 * The MAC of the message was calculated successfully, but it
1191 * differs from the expected MAC.
1192 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine76d7bfe2019-01-10 11:47:49 +01001193 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001194 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1195 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1196 * \retval #PSA_ERROR_HARDWARE_FAILURE
1197 * \retval #PSA_ERROR_TAMPERING_DETECTED
1198 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001199psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1200 const uint8_t *mac,
1201 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001202
Gilles Peskinedcd14942018-07-12 00:30:52 +02001203/** Abort a MAC operation.
1204 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001205 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001206 * \p operation structure itself. Once aborted, the operation object
1207 * can be reused for another operation by calling
1208 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001209 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001210 * You may call this function any time after the operation object has
1211 * been initialized by any of the following methods:
1212 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1213 * it succeeds or not.
1214 * - Initializing the \c struct to all-bits-zero.
1215 * - Initializing the \c struct to logical zeros, e.g.
1216 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001217 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001218 * In particular, calling psa_mac_abort() after the operation has been
1219 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1220 * psa_mac_verify_finish() is safe and has no effect.
1221 *
1222 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001223 *
1224 * \retval #PSA_SUCCESS
1225 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001226 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001227 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1228 * \retval #PSA_ERROR_HARDWARE_FAILURE
1229 * \retval #PSA_ERROR_TAMPERING_DETECTED
1230 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001231psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1232
1233/**@}*/
1234
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001235/** \defgroup cipher Symmetric ciphers
1236 * @{
1237 */
1238
1239/** The type of the state data structure for multipart cipher operations.
1240 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001241 * Before calling any function on a cipher operation object, the application
1242 * must initialize it by any of the following means:
1243 * - Set the structure to all-bits-zero, for example:
1244 * \code
1245 * psa_cipher_operation_t operation;
1246 * memset(&operation, 0, sizeof(operation));
1247 * \endcode
1248 * - Initialize the structure to logical zero values, for example:
1249 * \code
1250 * psa_cipher_operation_t operation = {0};
1251 * \endcode
1252 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1253 * for example:
1254 * \code
1255 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1256 * \endcode
1257 * - Assign the result of the function psa_cipher_operation_init()
1258 * to the structure, for example:
1259 * \code
1260 * psa_cipher_operation_t operation;
1261 * operation = psa_cipher_operation_init();
1262 * \endcode
1263 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001264 * This is an implementation-defined \c struct. Applications should not
1265 * make any assumptions about the content of this structure except
1266 * as directed by the documentation of a specific implementation. */
1267typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1268
Jaeden Amero5bae2272019-01-04 11:48:27 +00001269/** \def PSA_CIPHER_OPERATION_INIT
1270 *
1271 * This macro returns a suitable initializer for a cipher operation object of
1272 * type #psa_cipher_operation_t.
1273 */
1274#ifdef __DOXYGEN_ONLY__
1275/* This is an example definition for documentation purposes.
1276 * Implementations should define a suitable value in `crypto_struct.h`.
1277 */
1278#define PSA_CIPHER_OPERATION_INIT {0}
1279#endif
1280
1281/** Return an initial value for a cipher operation object.
1282 */
1283static psa_cipher_operation_t psa_cipher_operation_init(void);
1284
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001285/** Set the key for a multipart symmetric encryption operation.
1286 *
1287 * The sequence of operations to encrypt a message with a symmetric cipher
1288 * is as follows:
1289 * -# Allocate an operation object which will be passed to all the functions
1290 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001291 * -# Initialize the operation object with one of the methods described in the
1292 * documentation for #psa_cipher_operation_t, e.g.
1293 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001294 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001295 * The key remains associated with the operation even if the content
1296 * of the key slot changes.
itayzafrired7382f2018-08-02 14:19:33 +03001297 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001298 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001299 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001300 * requires a specific IV value.
1301 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1302 * of the message each time.
1303 * -# Call psa_cipher_finish().
1304 *
1305 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001306 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001307 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001308 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001309 * eventually terminate the operation. The following events terminate an
1310 * operation:
itayzafrired7382f2018-08-02 14:19:33 +03001311 * - A failed call to psa_cipher_generate_iv(), psa_cipher_set_iv()
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001312 * or psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001313 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001314 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001315 * \param[in,out] operation The operation object to set up. It must have
1316 * been initialized as per the documentation for
1317 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001318 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001319 * \param alg The cipher algorithm to compute
1320 * (\c PSA_ALG_XXX value such that
1321 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001322 *
Gilles Peskine28538492018-07-11 17:34:00 +02001323 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001324 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001325 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001326 * \retval #PSA_ERROR_EMPTY_SLOT
1327 * \retval #PSA_ERROR_NOT_PERMITTED
1328 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001329 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001330 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001331 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001332 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1333 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1334 * \retval #PSA_ERROR_HARDWARE_FAILURE
1335 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001336 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001337 * The operation state is not valid (already set up and not
1338 * subsequently completed).
1339 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001340 * The library has not been previously initialized by psa_crypto_init().
1341 * It is implementation-dependent whether a failure to initialize
1342 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001343 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001344psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001345 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001346 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001347
1348/** Set the key for a multipart symmetric decryption operation.
1349 *
1350 * The sequence of operations to decrypt a message with a symmetric cipher
1351 * is as follows:
1352 * -# Allocate an operation object which will be passed to all the functions
1353 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001354 * -# Initialize the operation object with one of the methods described in the
1355 * documentation for #psa_cipher_operation_t, e.g.
1356 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001357 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001358 * The key remains associated with the operation even if the content
1359 * of the key slot changes.
1360 * -# Call psa_cipher_update() with the IV (initialization vector) for the
1361 * decryption. If the IV is prepended to the ciphertext, you can call
1362 * psa_cipher_update() on a buffer containing the IV followed by the
1363 * beginning of the message.
1364 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1365 * of the message each time.
1366 * -# Call psa_cipher_finish().
1367 *
1368 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001369 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001370 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001371 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001372 * eventually terminate the operation. The following events terminate an
1373 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001374 * - A failed call to psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001375 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001376 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001377 * \param[in,out] operation The operation object to set up. It must have
1378 * been initialized as per the documentation for
1379 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001380 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001381 * \param alg The cipher algorithm to compute
1382 * (\c PSA_ALG_XXX value such that
1383 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001384 *
Gilles Peskine28538492018-07-11 17:34:00 +02001385 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001386 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001387 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001388 * \retval #PSA_ERROR_EMPTY_SLOT
1389 * \retval #PSA_ERROR_NOT_PERMITTED
1390 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001391 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001392 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001393 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001394 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1395 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1396 * \retval #PSA_ERROR_HARDWARE_FAILURE
1397 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001398 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine8e1addc2019-01-10 11:51:17 +01001399 * The operation state is not valid (already set up and not
1400 * subsequently completed).
1401 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001402 * The library has not been previously initialized by psa_crypto_init().
1403 * It is implementation-dependent whether a failure to initialize
1404 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001405 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001406psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001407 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001408 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001409
Gilles Peskinedcd14942018-07-12 00:30:52 +02001410/** Generate an IV for a symmetric encryption operation.
1411 *
1412 * This function generates a random IV (initialization vector), nonce
1413 * or initial counter value for the encryption operation as appropriate
1414 * for the chosen algorithm, key type and key size.
1415 *
1416 * The application must call psa_cipher_encrypt_setup() before
1417 * calling this function.
1418 *
1419 * If this function returns an error status, the operation becomes inactive.
1420 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001421 * \param[in,out] operation Active cipher operation.
1422 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001423 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001424 * \param[out] iv_length On success, the number of bytes of the
1425 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001426 *
1427 * \retval #PSA_SUCCESS
1428 * Success.
1429 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine76d7bfe2019-01-10 11:47:49 +01001430 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001431 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001432 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001433 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1434 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1435 * \retval #PSA_ERROR_HARDWARE_FAILURE
1436 * \retval #PSA_ERROR_TAMPERING_DETECTED
1437 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001438psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1439 unsigned char *iv,
1440 size_t iv_size,
1441 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001442
Gilles Peskinedcd14942018-07-12 00:30:52 +02001443/** Set the IV for a symmetric encryption or decryption operation.
1444 *
1445 * This function sets the random IV (initialization vector), nonce
1446 * or initial counter value for the encryption or decryption operation.
1447 *
1448 * The application must call psa_cipher_encrypt_setup() before
1449 * calling this function.
1450 *
1451 * If this function returns an error status, the operation becomes inactive.
1452 *
1453 * \note When encrypting, applications should use psa_cipher_generate_iv()
1454 * instead of this function, unless implementing a protocol that requires
1455 * a non-random IV.
1456 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001457 * \param[in,out] operation Active cipher operation.
1458 * \param[in] iv Buffer containing the IV to use.
1459 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001460 *
1461 * \retval #PSA_SUCCESS
1462 * Success.
1463 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine76d7bfe2019-01-10 11:47:49 +01001464 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001465 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001466 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001467 * or the chosen algorithm does not use an IV.
1468 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1469 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1470 * \retval #PSA_ERROR_HARDWARE_FAILURE
1471 * \retval #PSA_ERROR_TAMPERING_DETECTED
1472 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001473psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1474 const unsigned char *iv,
1475 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001476
Gilles Peskinedcd14942018-07-12 00:30:52 +02001477/** Encrypt or decrypt a message fragment in an active cipher operation.
1478 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001479 * Before calling this function, you must:
1480 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1481 * The choice of setup function determines whether this function
1482 * encrypts or decrypts its input.
1483 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1484 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001485 *
1486 * If this function returns an error status, the operation becomes inactive.
1487 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001488 * \param[in,out] operation Active cipher operation.
1489 * \param[in] input Buffer containing the message fragment to
1490 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001491 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001492 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001493 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001494 * \param[out] output_length On success, the number of bytes
1495 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001496 *
1497 * \retval #PSA_SUCCESS
1498 * Success.
1499 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine76d7bfe2019-01-10 11:47:49 +01001500 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001501 * not set, or already completed).
1502 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1503 * The size of the \p output buffer is too small.
1504 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1505 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1506 * \retval #PSA_ERROR_HARDWARE_FAILURE
1507 * \retval #PSA_ERROR_TAMPERING_DETECTED
1508 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001509psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1510 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001511 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001512 unsigned char *output,
1513 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001514 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001515
Gilles Peskinedcd14942018-07-12 00:30:52 +02001516/** Finish encrypting or decrypting a message in a cipher operation.
1517 *
1518 * The application must call psa_cipher_encrypt_setup() or
1519 * psa_cipher_decrypt_setup() before calling this function. The choice
1520 * of setup function determines whether this function encrypts or
1521 * decrypts its input.
1522 *
1523 * This function finishes the encryption or decryption of the message
1524 * formed by concatenating the inputs passed to preceding calls to
1525 * psa_cipher_update().
1526 *
1527 * When this function returns, the operation becomes inactive.
1528 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001529 * \param[in,out] operation Active cipher operation.
1530 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001531 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001532 * \param[out] output_length On success, the number of bytes
1533 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001534 *
1535 * \retval #PSA_SUCCESS
1536 * Success.
1537 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine76d7bfe2019-01-10 11:47:49 +01001538 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001539 * not set, or already completed).
1540 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1541 * The size of the \p output buffer is too small.
1542 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1543 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1544 * \retval #PSA_ERROR_HARDWARE_FAILURE
1545 * \retval #PSA_ERROR_TAMPERING_DETECTED
1546 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001547psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001548 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001549 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001550 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001551
Gilles Peskinedcd14942018-07-12 00:30:52 +02001552/** Abort a cipher operation.
1553 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001554 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001555 * \p operation structure itself. Once aborted, the operation object
1556 * can be reused for another operation by calling
1557 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001558 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001559 * You may call this function any time after the operation object has
1560 * been initialized by any of the following methods:
1561 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1562 * whether it succeeds or not.
1563 * - Initializing the \c struct to all-bits-zero.
1564 * - Initializing the \c struct to logical zeros, e.g.
1565 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001566 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001567 * In particular, calling psa_cipher_abort() after the operation has been
1568 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1569 * is safe and has no effect.
1570 *
1571 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001572 *
1573 * \retval #PSA_SUCCESS
1574 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001575 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001576 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1577 * \retval #PSA_ERROR_HARDWARE_FAILURE
1578 * \retval #PSA_ERROR_TAMPERING_DETECTED
1579 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001580psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1581
1582/**@}*/
1583
Gilles Peskine3b555712018-03-03 21:27:57 +01001584/** \defgroup aead Authenticated encryption with associated data (AEAD)
1585 * @{
1586 */
1587
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001588/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001589 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001590 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001591 * \param alg The AEAD algorithm to compute
1592 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001593 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001594 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001595 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001596 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001597 * but not encrypted.
1598 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001599 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001600 * encrypted.
1601 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001602 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001603 * encrypted data. The additional data is not
1604 * part of this output. For algorithms where the
1605 * encrypted data and the authentication tag
1606 * are defined as separate outputs, the
1607 * authentication tag is appended to the
1608 * encrypted data.
1609 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1610 * This must be at least
1611 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1612 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001613 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001614 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001615 *
Gilles Peskine28538492018-07-11 17:34:00 +02001616 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001617 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001618 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001619 * \retval #PSA_ERROR_EMPTY_SLOT
1620 * \retval #PSA_ERROR_NOT_PERMITTED
1621 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001622 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001623 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001624 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001625 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1626 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1627 * \retval #PSA_ERROR_HARDWARE_FAILURE
1628 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001629 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001630 * The library has not been previously initialized by psa_crypto_init().
1631 * It is implementation-dependent whether a failure to initialize
1632 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001633 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001634psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001635 psa_algorithm_t alg,
1636 const uint8_t *nonce,
1637 size_t nonce_length,
1638 const uint8_t *additional_data,
1639 size_t additional_data_length,
1640 const uint8_t *plaintext,
1641 size_t plaintext_length,
1642 uint8_t *ciphertext,
1643 size_t ciphertext_size,
1644 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001645
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001646/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001647 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001648 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001649 * \param alg The AEAD algorithm to compute
1650 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001651 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001652 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001653 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001654 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001655 * but not encrypted.
1656 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001657 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001658 * encrypted. For algorithms where the
1659 * encrypted data and the authentication tag
1660 * are defined as separate inputs, the buffer
1661 * must contain the encrypted data followed
1662 * by the authentication tag.
1663 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001664 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001665 * \param plaintext_size Size of the \p plaintext buffer in bytes.
1666 * This must be at least
1667 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
1668 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001669 * \param[out] plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03001670 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001671 *
Gilles Peskine28538492018-07-11 17:34:00 +02001672 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001673 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001674 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001675 * \retval #PSA_ERROR_EMPTY_SLOT
1676 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001677 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02001678 * \retval #PSA_ERROR_NOT_PERMITTED
1679 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001680 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001681 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001682 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001683 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1684 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1685 * \retval #PSA_ERROR_HARDWARE_FAILURE
1686 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001687 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001688 * The library has not been previously initialized by psa_crypto_init().
1689 * It is implementation-dependent whether a failure to initialize
1690 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001691 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001692psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001693 psa_algorithm_t alg,
1694 const uint8_t *nonce,
1695 size_t nonce_length,
1696 const uint8_t *additional_data,
1697 size_t additional_data_length,
1698 const uint8_t *ciphertext,
1699 size_t ciphertext_length,
1700 uint8_t *plaintext,
1701 size_t plaintext_size,
1702 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001703
1704/**@}*/
1705
Gilles Peskine20035e32018-02-03 22:44:14 +01001706/** \defgroup asymmetric Asymmetric cryptography
1707 * @{
1708 */
1709
1710/**
1711 * \brief Sign a hash or short message with a private key.
1712 *
Gilles Peskine08bac712018-06-26 16:14:46 +02001713 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001714 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02001715 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
1716 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
1717 * to determine the hash algorithm to use.
1718 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001719 * \param handle Handle to the key to use for the operation.
1720 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001721 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001722 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001723 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001724 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001725 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001726 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001727 * \param[out] signature_length On success, the number of bytes
1728 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001729 *
Gilles Peskine28538492018-07-11 17:34:00 +02001730 * \retval #PSA_SUCCESS
1731 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001732 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01001733 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001734 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001735 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001736 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001737 * \retval #PSA_ERROR_NOT_SUPPORTED
1738 * \retval #PSA_ERROR_INVALID_ARGUMENT
1739 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1740 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1741 * \retval #PSA_ERROR_HARDWARE_FAILURE
1742 * \retval #PSA_ERROR_TAMPERING_DETECTED
1743 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03001744 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001745 * The library has not been previously initialized by psa_crypto_init().
1746 * It is implementation-dependent whether a failure to initialize
1747 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01001748 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001749psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01001750 psa_algorithm_t alg,
1751 const uint8_t *hash,
1752 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01001753 uint8_t *signature,
1754 size_t signature_size,
1755 size_t *signature_length);
1756
1757/**
1758 * \brief Verify the signature a hash or short message using a public key.
1759 *
Gilles Peskine08bac712018-06-26 16:14:46 +02001760 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001761 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02001762 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
1763 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
1764 * to determine the hash algorithm to use.
1765 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001766 * \param handle Handle to the key to use for the operation.
1767 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001768 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001769 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001770 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02001771 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001772 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001773 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001774 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001775 *
Gilles Peskine28538492018-07-11 17:34:00 +02001776 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001777 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02001778 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001779 * The calculation was perfomed successfully, but the passed
1780 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02001781 * \retval #PSA_ERROR_NOT_SUPPORTED
1782 * \retval #PSA_ERROR_INVALID_ARGUMENT
1783 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1784 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1785 * \retval #PSA_ERROR_HARDWARE_FAILURE
1786 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001787 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001788 * The library has not been previously initialized by psa_crypto_init().
1789 * It is implementation-dependent whether a failure to initialize
1790 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01001791 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001792psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01001793 psa_algorithm_t alg,
1794 const uint8_t *hash,
1795 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02001796 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02001797 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01001798
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001799/**
1800 * \brief Encrypt a short message with a public key.
1801 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001802 * \param handle Handle to the key to use for the operation.
1803 * It must be a public key or an asymmetric
1804 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001805 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001806 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001807 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001808 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001809 * \param[in] salt A salt or label, if supported by the
1810 * encryption algorithm.
1811 * If the algorithm does not support a
1812 * salt, pass \c NULL.
1813 * If the algorithm supports an optional
1814 * salt and you do not want to pass a salt,
1815 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001816 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001817 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1818 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001819 * \param salt_length Size of the \p salt buffer in bytes.
1820 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001821 * \param[out] output Buffer where the encrypted message is to
1822 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001823 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001824 * \param[out] output_length On success, the number of bytes
1825 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001826 *
Gilles Peskine28538492018-07-11 17:34:00 +02001827 * \retval #PSA_SUCCESS
1828 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001829 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001830 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001831 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001832 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001833 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001834 * \retval #PSA_ERROR_NOT_SUPPORTED
1835 * \retval #PSA_ERROR_INVALID_ARGUMENT
1836 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1837 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1838 * \retval #PSA_ERROR_HARDWARE_FAILURE
1839 * \retval #PSA_ERROR_TAMPERING_DETECTED
1840 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03001841 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001842 * The library has not been previously initialized by psa_crypto_init().
1843 * It is implementation-dependent whether a failure to initialize
1844 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001845 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001846psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001847 psa_algorithm_t alg,
1848 const uint8_t *input,
1849 size_t input_length,
1850 const uint8_t *salt,
1851 size_t salt_length,
1852 uint8_t *output,
1853 size_t output_size,
1854 size_t *output_length);
1855
1856/**
1857 * \brief Decrypt a short message with a private key.
1858 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001859 * \param handle Handle to the key to use for the operation.
1860 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001861 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001862 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001863 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001864 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001865 * \param[in] salt A salt or label, if supported by the
1866 * encryption algorithm.
1867 * If the algorithm does not support a
1868 * salt, pass \c NULL.
1869 * If the algorithm supports an optional
1870 * salt and you do not want to pass a salt,
1871 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001872 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001873 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1874 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001875 * \param salt_length Size of the \p salt buffer in bytes.
1876 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001877 * \param[out] output Buffer where the decrypted message is to
1878 * be written.
1879 * \param output_size Size of the \c output buffer in bytes.
1880 * \param[out] output_length On success, the number of bytes
1881 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001882 *
Gilles Peskine28538492018-07-11 17:34:00 +02001883 * \retval #PSA_SUCCESS
1884 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001885 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001886 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001887 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001888 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001889 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001890 * \retval #PSA_ERROR_NOT_SUPPORTED
1891 * \retval #PSA_ERROR_INVALID_ARGUMENT
1892 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1893 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1894 * \retval #PSA_ERROR_HARDWARE_FAILURE
1895 * \retval #PSA_ERROR_TAMPERING_DETECTED
1896 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
1897 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03001898 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001899 * The library has not been previously initialized by psa_crypto_init().
1900 * It is implementation-dependent whether a failure to initialize
1901 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001902 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001903psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001904 psa_algorithm_t alg,
1905 const uint8_t *input,
1906 size_t input_length,
1907 const uint8_t *salt,
1908 size_t salt_length,
1909 uint8_t *output,
1910 size_t output_size,
1911 size_t *output_length);
1912
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001913/**@}*/
1914
Gilles Peskineedd76872018-07-20 17:42:05 +02001915/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02001916 * @{
1917 */
1918
1919/** The type of the state data structure for generators.
1920 *
1921 * Before calling any function on a generator, the application must
1922 * initialize it by any of the following means:
1923 * - Set the structure to all-bits-zero, for example:
1924 * \code
1925 * psa_crypto_generator_t generator;
1926 * memset(&generator, 0, sizeof(generator));
1927 * \endcode
1928 * - Initialize the structure to logical zero values, for example:
1929 * \code
1930 * psa_crypto_generator_t generator = {0};
1931 * \endcode
1932 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
1933 * for example:
1934 * \code
1935 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1936 * \endcode
1937 * - Assign the result of the function psa_crypto_generator_init()
1938 * to the structure, for example:
1939 * \code
1940 * psa_crypto_generator_t generator;
1941 * generator = psa_crypto_generator_init();
1942 * \endcode
1943 *
1944 * This is an implementation-defined \c struct. Applications should not
1945 * make any assumptions about the content of this structure except
1946 * as directed by the documentation of a specific implementation.
1947 */
1948typedef struct psa_crypto_generator_s psa_crypto_generator_t;
1949
1950/** \def PSA_CRYPTO_GENERATOR_INIT
1951 *
1952 * This macro returns a suitable initializer for a generator object
1953 * of type #psa_crypto_generator_t.
1954 */
1955#ifdef __DOXYGEN_ONLY__
1956/* This is an example definition for documentation purposes.
1957 * Implementations should define a suitable value in `crypto_struct.h`.
1958 */
1959#define PSA_CRYPTO_GENERATOR_INIT {0}
1960#endif
1961
1962/** Return an initial value for a generator object.
1963 */
1964static psa_crypto_generator_t psa_crypto_generator_init(void);
1965
1966/** Retrieve the current capacity of a generator.
1967 *
1968 * The capacity of a generator is the maximum number of bytes that it can
1969 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
1970 *
1971 * \param[in] generator The generator to query.
1972 * \param[out] capacity On success, the capacity of the generator.
1973 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001974 * \retval #PSA_SUCCESS
1975 * \retval #PSA_ERROR_BAD_STATE
1976 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02001977 */
1978psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
1979 size_t *capacity);
1980
1981/** Read some data from a generator.
1982 *
1983 * This function reads and returns a sequence of bytes from a generator.
1984 * The data that is read is discarded from the generator. The generator's
1985 * capacity is decreased by the number of bytes read.
1986 *
1987 * \param[in,out] generator The generator object to read from.
1988 * \param[out] output Buffer where the generator output will be
1989 * written.
1990 * \param output_length Number of bytes to output.
1991 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001992 * \retval #PSA_SUCCESS
1993 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02001994 * There were fewer than \p output_length bytes
1995 * in the generator. Note that in this case, no
1996 * output is written to the output buffer.
1997 * The generator's capacity is set to 0, thus
1998 * subsequent calls to this function will not
1999 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002000 * \retval #PSA_ERROR_BAD_STATE
2001 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2002 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2003 * \retval #PSA_ERROR_HARDWARE_FAILURE
2004 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002005 */
2006psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
2007 uint8_t *output,
2008 size_t output_length);
2009
2010/** Create a symmetric key from data read from a generator.
2011 *
2012 * This function reads a sequence of bytes from a generator and imports
2013 * these bytes as a key.
2014 * The data that is read is discarded from the generator. The generator's
2015 * capacity is decreased by the number of bytes read.
2016 *
2017 * This function is equivalent to calling #psa_generator_read and
2018 * passing the resulting output to #psa_import_key, but
2019 * if the implementation provides an isolation boundary then
2020 * the key material is not exposed outside the isolation boundary.
2021 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002022 * \param handle Handle to the slot where the key will be stored.
2023 * This must be a valid slot for a key of the chosen
2024 * type: it must have been obtained by calling
2025 * psa_allocate_key() or psa_create_key() with the
2026 * correct \p type and with a maximum size that is
2027 * compatible with \p bits.
2028 * It must not contain any key material yet.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002029 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2030 * This must be a symmetric key type.
2031 * \param bits Key size in bits.
2032 * \param[in,out] generator The generator object to read from.
2033 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002034 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02002035 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01002036 * If the key is persistent, the key material and the key's metadata
2037 * have been saved to persistent storage.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002038 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02002039 * There were fewer than \p output_length bytes
2040 * in the generator. Note that in this case, no
2041 * output is written to the output buffer.
2042 * The generator's capacity is set to 0, thus
2043 * subsequent calls to this function will not
2044 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002045 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002046 * The key type or key size is not supported, either by the
2047 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002048 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineae32aac2018-11-30 14:39:32 +01002049 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002050 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskineeab56e42018-07-12 17:12:33 +02002051 * There is already a key in the specified slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002052 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2053 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
2054 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2055 * \retval #PSA_ERROR_HARDWARE_FAILURE
2056 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002057 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002058 * The library has not been previously initialized by psa_crypto_init().
2059 * It is implementation-dependent whether a failure to initialize
2060 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002061 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002062psa_status_t psa_generator_import_key(psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002063 psa_key_type_t type,
2064 size_t bits,
2065 psa_crypto_generator_t *generator);
2066
2067/** Abort a generator.
2068 *
2069 * Once a generator has been aborted, its capacity is zero.
2070 * Aborting a generator frees all associated resources except for the
2071 * \c generator structure itself.
2072 *
2073 * This function may be called at any time as long as the generator
2074 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
2075 * psa_crypto_generator_init() or a zero value. In particular, it is valid
2076 * to call psa_generator_abort() twice, or to call psa_generator_abort()
2077 * on a generator that has not been set up.
2078 *
2079 * Once aborted, the generator object may be called.
2080 *
2081 * \param[in,out] generator The generator to abort.
2082 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002083 * \retval #PSA_SUCCESS
2084 * \retval #PSA_ERROR_BAD_STATE
2085 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2086 * \retval #PSA_ERROR_HARDWARE_FAILURE
2087 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002088 */
2089psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
2090
Gilles Peskine8feb3a82018-09-18 12:06:11 +02002091/** Use the maximum possible capacity for a generator.
2092 *
2093 * Use this value as the capacity argument when setting up a generator
2094 * to indicate that the generator should have the maximum possible capacity.
2095 * The value of the maximum possible capacity depends on the generator
2096 * algorithm.
2097 */
2098#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
2099
Gilles Peskineeab56e42018-07-12 17:12:33 +02002100/**@}*/
2101
Gilles Peskineea0fb492018-07-12 17:17:20 +02002102/** \defgroup derivation Key derivation
2103 * @{
2104 */
2105
2106/** Set up a key derivation operation.
2107 *
2108 * A key derivation algorithm takes three inputs: a secret input \p key and
2109 * two non-secret inputs \p label and p salt.
2110 * The result of this function is a byte generator which can
2111 * be used to produce keys and other cryptographic material.
2112 *
2113 * The role of \p label and \p salt is as follows:
Gilles Peskinebef7f142018-07-12 17:22:21 +02002114 * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step
2115 * and \p label is the info string used in the "expand" step.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002116 *
Jaeden Amero9e919c62019-01-07 15:41:50 +00002117 * \param[in,out] generator The generator object to set up. It must have
2118 * been initialized as per the documentation for
2119 * #psa_crypto_generator_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002120 * \param handle Handle to the secret key.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002121 * \param alg The key derivation algorithm to compute
2122 * (\c PSA_ALG_XXX value such that
2123 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2124 * \param[in] salt Salt to use.
2125 * \param salt_length Size of the \p salt buffer in bytes.
2126 * \param[in] label Label to use.
2127 * \param label_length Size of the \p label buffer in bytes.
2128 * \param capacity The maximum number of bytes that the
2129 * generator will be able to provide.
2130 *
2131 * \retval #PSA_SUCCESS
2132 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002133 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineea0fb492018-07-12 17:17:20 +02002134 * \retval #PSA_ERROR_EMPTY_SLOT
2135 * \retval #PSA_ERROR_NOT_PERMITTED
2136 * \retval #PSA_ERROR_INVALID_ARGUMENT
2137 * \c key is not compatible with \c alg,
2138 * or \p capacity is too large for the specified algorithm and key.
2139 * \retval #PSA_ERROR_NOT_SUPPORTED
2140 * \c alg is not supported or is not a key derivation algorithm.
2141 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2142 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2143 * \retval #PSA_ERROR_HARDWARE_FAILURE
2144 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002145 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002146 * The library has not been previously initialized by psa_crypto_init().
2147 * It is implementation-dependent whether a failure to initialize
2148 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002149 */
2150psa_status_t psa_key_derivation(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002151 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02002152 psa_algorithm_t alg,
2153 const uint8_t *salt,
2154 size_t salt_length,
2155 const uint8_t *label,
2156 size_t label_length,
2157 size_t capacity);
2158
Gilles Peskine01d718c2018-09-18 12:01:02 +02002159/** Set up a key agreement operation.
2160 *
2161 * A key agreement algorithm takes two inputs: a private key \p private_key
2162 * a public key \p peer_key.
2163 * The result of this function is a byte generator which can
2164 * be used to produce keys and other cryptographic material.
2165 *
Gilles Peskine211a4362018-10-25 22:22:31 +02002166 * The resulting generator always has the maximum capacity permitted by
2167 * the algorithm.
2168 *
Jaeden Amero9e919c62019-01-07 15:41:50 +00002169 * \param[in,out] generator The generator object to set up. It must have
2170 * been initialized as per the documentation for
2171 * #psa_crypto_generator_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002172 * \param private_key Handle to the private key to use.
Gilles Peskined171e782018-11-15 17:46:21 +01002173 * \param[in] peer_key Public key of the peer. It must be
2174 * in the same format that psa_import_key()
2175 * accepts. The standard formats for public
2176 * keys are documented in the documentation
2177 * of psa_export_public_key().
Gilles Peskine01d718c2018-09-18 12:01:02 +02002178 * \param peer_key_length Size of \p peer_key in bytes.
2179 * \param alg The key agreement algorithm to compute
2180 * (\c PSA_ALG_XXX value such that
2181 * #PSA_ALG_IS_KEY_AGREEMENT(\p alg) is true).
2182 *
2183 * \retval #PSA_SUCCESS
2184 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002185 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine01d718c2018-09-18 12:01:02 +02002186 * \retval #PSA_ERROR_EMPTY_SLOT
2187 * \retval #PSA_ERROR_NOT_PERMITTED
2188 * \retval #PSA_ERROR_INVALID_ARGUMENT
2189 * \c private_key is not compatible with \c alg,
2190 * or \p peer_key is not valid for \c alg or not compatible with
2191 * \c private_key.
2192 * \retval #PSA_ERROR_NOT_SUPPORTED
2193 * \c alg is not supported or is not a key derivation algorithm.
2194 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2195 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2196 * \retval #PSA_ERROR_HARDWARE_FAILURE
2197 * \retval #PSA_ERROR_TAMPERING_DETECTED
2198 */
2199psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002200 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02002201 const uint8_t *peer_key,
2202 size_t peer_key_length,
2203 psa_algorithm_t alg);
2204
Gilles Peskineea0fb492018-07-12 17:17:20 +02002205/**@}*/
2206
Gilles Peskineedd76872018-07-20 17:42:05 +02002207/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002208 * @{
2209 */
2210
2211/**
2212 * \brief Generate random bytes.
2213 *
2214 * \warning This function **can** fail! Callers MUST check the return status
2215 * and MUST NOT use the content of the output buffer if the return
2216 * status is not #PSA_SUCCESS.
2217 *
2218 * \note To generate a key, use psa_generate_key() instead.
2219 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002220 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002221 * \param output_size Number of bytes to generate and output.
2222 *
Gilles Peskine28538492018-07-11 17:34:00 +02002223 * \retval #PSA_SUCCESS
2224 * \retval #PSA_ERROR_NOT_SUPPORTED
2225 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2226 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2227 * \retval #PSA_ERROR_HARDWARE_FAILURE
2228 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03002229 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002230 * The library has not been previously initialized by psa_crypto_init().
2231 * It is implementation-dependent whether a failure to initialize
2232 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002233 */
2234psa_status_t psa_generate_random(uint8_t *output,
2235 size_t output_size);
2236
Gilles Peskine4c317f42018-07-12 01:24:09 +02002237/** Extra parameters for RSA key generation.
2238 *
Gilles Peskinebe42f312018-07-13 14:38:15 +02002239 * You may pass a pointer to a structure of this type as the \c extra
Gilles Peskine4c317f42018-07-12 01:24:09 +02002240 * parameter to psa_generate_key().
2241 */
2242typedef struct {
Gilles Peskineedd76872018-07-20 17:42:05 +02002243 uint32_t e; /**< Public exponent value. Default: 65537. */
Gilles Peskine4c317f42018-07-12 01:24:09 +02002244} psa_generate_key_extra_rsa;
2245
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002246/**
2247 * \brief Generate a key or key pair.
2248 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002249 * \param handle Handle to the slot where the key will be stored.
2250 * This must be a valid slot for a key of the chosen
2251 * type: it must have been obtained by calling
2252 * psa_allocate_key() or psa_create_key() with the
2253 * correct \p type and with a maximum size that is
2254 * compatible with \p bits.
2255 * It must not contain any key material yet.
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002256 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2257 * \param bits Key size in bits.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002258 * \param[in] extra Extra parameters for key generation. The
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002259 * interpretation of this parameter depends on
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002260 * \p type. All types support \c NULL to use
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002261 * default parameters. Implementation that support
2262 * the generation of vendor-specific key types
2263 * that allow extra parameters shall document
2264 * the format of these extra parameters and
2265 * the default values. For standard parameters,
2266 * the meaning of \p extra is as follows:
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002267 * - For a symmetric key type (a type such
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002268 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
2269 * false), \p extra must be \c NULL.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002270 * - For an elliptic curve key type (a type
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002271 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
2272 * false), \p extra must be \c NULL.
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002273 * - For an RSA key (\p type is
2274 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
2275 * optional #psa_generate_key_extra_rsa structure
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002276 * specifying the public exponent. The
2277 * default public exponent used when \p extra
2278 * is \c NULL is 65537.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002279 * \param extra_size Size of the buffer that \p extra
2280 * points to, in bytes. Note that if \p extra is
2281 * \c NULL then \p extra_size must be zero.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002282 *
Gilles Peskine28538492018-07-11 17:34:00 +02002283 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01002284 * Success.
2285 * If the key is persistent, the key material and the key's metadata
2286 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002287 * \retval #PSA_ERROR_INVALID_HANDLE
2288 * \retval #PSA_ERROR_OCCUPIED_SLOT
2289 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +02002290 * \retval #PSA_ERROR_NOT_SUPPORTED
2291 * \retval #PSA_ERROR_INVALID_ARGUMENT
2292 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2293 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2294 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2295 * \retval #PSA_ERROR_HARDWARE_FAILURE
2296 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002297 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002298 * The library has not been previously initialized by psa_crypto_init().
2299 * It is implementation-dependent whether a failure to initialize
2300 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002301 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002302psa_status_t psa_generate_key(psa_key_handle_t handle,
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002303 psa_key_type_t type,
2304 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02002305 const void *extra,
2306 size_t extra_size);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002307
2308/**@}*/
2309
Gilles Peskinee59236f2018-01-27 23:32:46 +01002310#ifdef __cplusplus
2311}
2312#endif
2313
Gilles Peskine0cad07c2018-06-27 19:49:02 +02002314/* The file "crypto_sizes.h" contains definitions for size calculation
2315 * macros whose definitions are implementation-specific. */
2316#include "crypto_sizes.h"
2317
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002318/* The file "crypto_struct.h" contains definitions for
2319 * implementation-specific structs that are declared above. */
2320#include "crypto_struct.h"
2321
2322/* The file "crypto_extra.h" contains vendor-specific definitions. This
2323 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01002324#include "crypto_extra.h"
2325
2326#endif /* PSA_CRYPTO_H */