blob: fa8045cf404f0df851d36d3ee293d0a78d4e340e [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 *
575 * This is an implementation-defined \c struct. Applications should not
576 * make any assumptions about the content of this structure except
577 * as directed by the documentation of a specific implementation. */
578typedef struct psa_key_policy_s psa_key_policy_t;
579
580/** \brief Initialize a key policy structure to a default that forbids all
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200581 * usage of the key.
582 *
583 * \param[out] policy The policy object to initialize.
584 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100585void psa_key_policy_init(psa_key_policy_t *policy);
586
Gilles Peskine7e198532018-03-08 07:50:30 +0100587/** \brief Set the standard fields of a policy structure.
588 *
589 * Note that this function does not make any consistency check of the
590 * parameters. The values are only checked when applying the policy to
591 * a key slot with psa_set_key_policy().
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200592 *
593 * \param[out] policy The policy object to modify.
594 * \param usage The permitted uses for the key.
595 * \param alg The algorithm that the key may be used for.
Gilles Peskine7e198532018-03-08 07:50:30 +0100596 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100597void psa_key_policy_set_usage(psa_key_policy_t *policy,
598 psa_key_usage_t usage,
599 psa_algorithm_t alg);
600
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200601/** \brief Retrieve the usage field of a policy structure.
602 *
603 * \param[in] policy The policy object to query.
604 *
605 * \return The permitted uses for a key with this policy.
606 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +0200607psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100608
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200609/** \brief Retrieve the algorithm field of a policy structure.
610 *
611 * \param[in] policy The policy object to query.
612 *
613 * \return The permitted algorithm for a key with this policy.
614 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +0200615psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100616
617/** \brief Set the usage policy on a key slot.
618 *
619 * This function must be called on an empty key slot, before importing,
620 * generating or creating a key in the slot. Changing the policy of an
621 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +0100622 *
623 * Implementations may set restrictions on supported key policies
624 * depending on the key type and the key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200625 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100626 * \param handle Handle to the key whose policy is to be changed.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200627 * \param[in] policy The policy object to query.
628 *
629 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100630 * Success.
631 * If the key is persistent, it is implementation-defined whether
632 * the policy has been saved to persistent storage. Implementations
633 * may defer saving the policy until the key material is created.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100634 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200635 * \retval #PSA_ERROR_OCCUPIED_SLOT
636 * \retval #PSA_ERROR_NOT_SUPPORTED
637 * \retval #PSA_ERROR_INVALID_ARGUMENT
638 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
639 * \retval #PSA_ERROR_HARDWARE_FAILURE
640 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300641 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300642 * The library has not been previously initialized by psa_crypto_init().
643 * It is implementation-dependent whether a failure to initialize
644 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100645 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100646psa_status_t psa_set_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100647 const psa_key_policy_t *policy);
648
Gilles Peskine7e198532018-03-08 07:50:30 +0100649/** \brief Get the usage policy for a key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200650 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100651 * \param handle Handle to the key slot whose policy is being queried.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200652 * \param[out] policy On success, the key's policy.
653 *
654 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100655 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200656 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
657 * \retval #PSA_ERROR_HARDWARE_FAILURE
658 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300659 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300660 * The library has not been previously initialized by psa_crypto_init().
661 * It is implementation-dependent whether a failure to initialize
662 * results in this error code.
Gilles Peskine7e198532018-03-08 07:50:30 +0100663 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100664psa_status_t psa_get_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100665 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +0100666
667/**@}*/
668
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100669/** \defgroup hash Message digests
670 * @{
671 */
672
Gilles Peskine308b91d2018-02-08 09:47:44 +0100673/** The type of the state data structure for multipart hash operations.
674 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100675 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100676 * make any assumptions about the content of this structure except
677 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100678typedef struct psa_hash_operation_s psa_hash_operation_t;
679
Gilles Peskine308b91d2018-02-08 09:47:44 +0100680/** Start a multipart hash operation.
681 *
682 * The sequence of operations to calculate a hash (message digest)
683 * is as follows:
684 * -# Allocate an operation object which will be passed to all the functions
685 * listed here.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200686 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100687 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100688 * of the message each time. The hash that is calculated is the hash
689 * of the concatenation of these messages in order.
690 * -# To calculate the hash, call psa_hash_finish().
691 * To compare the hash with an expected value, call psa_hash_verify().
692 *
693 * The application may call psa_hash_abort() at any time after the operation
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200694 * has been initialized with psa_hash_setup().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100695 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200696 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100697 * eventually terminate the operation. The following events terminate an
698 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100699 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100700 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100701 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200702 * \param[out] operation The operation object to use.
703 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
704 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100705 *
Gilles Peskine28538492018-07-11 17:34:00 +0200706 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100707 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200708 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200709 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +0200710 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
711 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
712 * \retval #PSA_ERROR_HARDWARE_FAILURE
713 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100714 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200715psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100716 psa_algorithm_t alg);
717
Gilles Peskine308b91d2018-02-08 09:47:44 +0100718/** Add a message fragment to a multipart hash operation.
719 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200720 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100721 *
722 * If this function returns an error status, the operation becomes inactive.
723 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200724 * \param[in,out] operation Active hash operation.
725 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200726 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100727 *
Gilles Peskine28538492018-07-11 17:34:00 +0200728 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100729 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200730 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100731 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200732 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
733 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
734 * \retval #PSA_ERROR_HARDWARE_FAILURE
735 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100736 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100737psa_status_t psa_hash_update(psa_hash_operation_t *operation,
738 const uint8_t *input,
739 size_t input_length);
740
Gilles Peskine308b91d2018-02-08 09:47:44 +0100741/** Finish the calculation of the hash of a message.
742 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200743 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100744 * This function calculates the hash of the message formed by concatenating
745 * the inputs passed to preceding calls to psa_hash_update().
746 *
747 * When this function returns, the operation becomes inactive.
748 *
749 * \warning Applications should not call this function if they expect
750 * a specific value for the hash. Call psa_hash_verify() instead.
751 * Beware that comparing integrity or authenticity data such as
752 * hash values with a function such as \c memcmp is risky
753 * because the time taken by the comparison may leak information
754 * about the hashed data which could allow an attacker to guess
755 * a valid hash and thereby bypass security controls.
756 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200757 * \param[in,out] operation Active hash operation.
758 * \param[out] hash Buffer where the hash is to be written.
759 * \param hash_size Size of the \p hash buffer in bytes.
760 * \param[out] hash_length On success, the number of bytes
761 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +0200762 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +0200763 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100764 *
Gilles Peskine28538492018-07-11 17:34:00 +0200765 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100766 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200767 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100768 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200769 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200770 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200771 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100772 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +0200773 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
774 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
775 * \retval #PSA_ERROR_HARDWARE_FAILURE
776 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100777 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100778psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
779 uint8_t *hash,
780 size_t hash_size,
781 size_t *hash_length);
782
Gilles Peskine308b91d2018-02-08 09:47:44 +0100783/** Finish the calculation of the hash of a message and compare it with
784 * an expected value.
785 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200786 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100787 * This function calculates the hash of the message formed by concatenating
788 * the inputs passed to preceding calls to psa_hash_update(). It then
789 * compares the calculated hash with the expected hash passed as a
790 * parameter to this function.
791 *
792 * When this function returns, the operation becomes inactive.
793 *
Gilles Peskine19067982018-03-20 17:54:53 +0100794 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +0100795 * comparison between the actual hash and the expected hash is performed
796 * in constant time.
797 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200798 * \param[in,out] operation Active hash operation.
799 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200800 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100801 *
Gilles Peskine28538492018-07-11 17:34:00 +0200802 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100803 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +0200804 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100805 * The hash of the message was calculated successfully, but it
806 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +0200807 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100808 * The operation state is not valid (not started, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200809 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
810 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
811 * \retval #PSA_ERROR_HARDWARE_FAILURE
812 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100813 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100814psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
815 const uint8_t *hash,
816 size_t hash_length);
817
Gilles Peskine308b91d2018-02-08 09:47:44 +0100818/** Abort a hash operation.
819 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100820 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200821 * \p operation structure itself. Once aborted, the operation object
822 * can be reused for another operation by calling
823 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100824 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200825 * You may call this function any time after the operation object has
826 * been initialized by any of the following methods:
827 * - A call to psa_hash_setup(), whether it succeeds or not.
828 * - Initializing the \c struct to all-bits-zero.
829 * - Initializing the \c struct to logical zeros, e.g.
830 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100831 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200832 * In particular, calling psa_hash_abort() after the operation has been
833 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
834 * psa_hash_verify() is safe and has no effect.
835 *
836 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100837 *
Gilles Peskine28538492018-07-11 17:34:00 +0200838 * \retval #PSA_SUCCESS
839 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200840 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +0200841 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
842 * \retval #PSA_ERROR_HARDWARE_FAILURE
843 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100844 */
845psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100846
847/**@}*/
848
Gilles Peskine8c9def32018-02-08 10:02:12 +0100849/** \defgroup MAC Message authentication codes
850 * @{
851 */
852
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100853/** The type of the state data structure for multipart MAC operations.
854 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100855 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100856 * make any assumptions about the content of this structure except
857 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100858typedef struct psa_mac_operation_s psa_mac_operation_t;
859
Gilles Peskine89167cb2018-07-08 20:12:23 +0200860/** Start a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100861 *
Gilles Peskine89167cb2018-07-08 20:12:23 +0200862 * This function sets up the calculation of the MAC
863 * (message authentication code) of a byte string.
864 * To verify the MAC of a message against an
865 * expected value, use psa_mac_verify_setup() instead.
866 *
867 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100868 * -# Allocate an operation object which will be passed to all the functions
869 * listed here.
Gilles Peskine89167cb2018-07-08 20:12:23 +0200870 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100871 * The key remains associated with the operation even if the content
872 * of the key slot changes.
873 * -# Call psa_mac_update() zero, one or more times, passing a fragment
874 * of the message each time. The MAC that is calculated is the MAC
875 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +0200876 * -# At the end of the message, call psa_mac_sign_finish() to finish
877 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100878 *
879 * The application may call psa_mac_abort() at any time after the operation
Gilles Peskine89167cb2018-07-08 20:12:23 +0200880 * has been initialized with psa_mac_sign_setup().
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100881 *
Gilles Peskine89167cb2018-07-08 20:12:23 +0200882 * After a successful call to psa_mac_sign_setup(), the application must
883 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100884 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +0200885 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100886 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200887 * \param[out] operation The operation object to use.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100888 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200889 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
890 * such that #PSA_ALG_IS_MAC(alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100891 *
Gilles Peskine28538492018-07-11 17:34:00 +0200892 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100893 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100894 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200895 * \retval #PSA_ERROR_EMPTY_SLOT
896 * \retval #PSA_ERROR_NOT_PERMITTED
897 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200898 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +0200899 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200900 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +0200901 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
902 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
903 * \retval #PSA_ERROR_HARDWARE_FAILURE
904 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300905 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300906 * The library has not been previously initialized by psa_crypto_init().
907 * It is implementation-dependent whether a failure to initialize
908 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100909 */
Gilles Peskine89167cb2018-07-08 20:12:23 +0200910psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +0100911 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +0200912 psa_algorithm_t alg);
913
914/** Start a multipart MAC verification operation.
915 *
916 * This function sets up the verification of the MAC
917 * (message authentication code) of a byte string against an expected value.
918 *
919 * The sequence of operations to verify a MAC is as follows:
920 * -# Allocate an operation object which will be passed to all the functions
921 * listed here.
922 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
923 * The key remains associated with the operation even if the content
924 * of the key slot changes.
925 * -# Call psa_mac_update() zero, one or more times, passing a fragment
926 * of the message each time. The MAC that is calculated is the MAC
927 * of the concatenation of these messages in order.
928 * -# At the end of the message, call psa_mac_verify_finish() to finish
929 * calculating the actual MAC of the message and verify it against
930 * the expected value.
931 *
932 * The application may call psa_mac_abort() at any time after the operation
933 * has been initialized with psa_mac_verify_setup().
934 *
935 * After a successful call to psa_mac_verify_setup(), the application must
936 * eventually terminate the operation through one of the following methods:
937 * - A failed call to psa_mac_update().
938 * - A call to psa_mac_verify_finish() or psa_mac_abort().
939 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200940 * \param[out] operation The operation object to use.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100941 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200942 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
943 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +0200944 *
Gilles Peskine28538492018-07-11 17:34:00 +0200945 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +0200946 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100947 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200948 * \retval #PSA_ERROR_EMPTY_SLOT
949 * \retval #PSA_ERROR_NOT_PERMITTED
950 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +0200951 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +0200952 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +0200953 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +0200954 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
955 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
956 * \retval #PSA_ERROR_HARDWARE_FAILURE
957 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300958 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300959 * The library has not been previously initialized by psa_crypto_init().
960 * It is implementation-dependent whether a failure to initialize
961 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +0200962 */
963psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +0100964 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +0200965 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +0100966
Gilles Peskinedcd14942018-07-12 00:30:52 +0200967/** Add a message fragment to a multipart MAC operation.
968 *
969 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
970 * before calling this function.
971 *
972 * If this function returns an error status, the operation becomes inactive.
973 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200974 * \param[in,out] operation Active MAC operation.
975 * \param[in] input Buffer containing the message fragment to add to
976 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200977 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +0200978 *
979 * \retval #PSA_SUCCESS
980 * Success.
981 * \retval #PSA_ERROR_BAD_STATE
982 * The operation state is not valid (not started, or already completed).
983 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
984 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
985 * \retval #PSA_ERROR_HARDWARE_FAILURE
986 * \retval #PSA_ERROR_TAMPERING_DETECTED
987 */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100988psa_status_t psa_mac_update(psa_mac_operation_t *operation,
989 const uint8_t *input,
990 size_t input_length);
991
Gilles Peskinedcd14942018-07-12 00:30:52 +0200992/** Finish the calculation of the MAC of a message.
993 *
994 * The application must call psa_mac_sign_setup() before calling this function.
995 * This function calculates the MAC of the message formed by concatenating
996 * the inputs passed to preceding calls to psa_mac_update().
997 *
998 * When this function returns, the operation becomes inactive.
999 *
1000 * \warning Applications should not call this function if they expect
1001 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1002 * Beware that comparing integrity or authenticity data such as
1003 * MAC values with a function such as \c memcmp is risky
1004 * because the time taken by the comparison may leak information
1005 * about the MAC value which could allow an attacker to guess
1006 * a valid MAC and thereby bypass security controls.
1007 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001008 * \param[in,out] operation Active MAC operation.
1009 * \param[out] mac Buffer where the MAC value is to be written.
1010 * \param mac_size Size of the \p mac buffer in bytes.
1011 * \param[out] mac_length On success, the number of bytes
1012 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001013 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001014 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001015 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001016 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001017 *
1018 * \retval #PSA_SUCCESS
1019 * Success.
1020 * \retval #PSA_ERROR_BAD_STATE
1021 * The operation state is not valid (not started, or already completed).
1022 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001023 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001024 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1025 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1026 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1027 * \retval #PSA_ERROR_HARDWARE_FAILURE
1028 * \retval #PSA_ERROR_TAMPERING_DETECTED
1029 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001030psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1031 uint8_t *mac,
1032 size_t mac_size,
1033 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001034
Gilles Peskinedcd14942018-07-12 00:30:52 +02001035/** Finish the calculation of the MAC of a message and compare it with
1036 * an expected value.
1037 *
1038 * The application must call psa_mac_verify_setup() before calling this function.
1039 * This function calculates the MAC of the message formed by concatenating
1040 * the inputs passed to preceding calls to psa_mac_update(). It then
1041 * compares the calculated MAC with the expected MAC passed as a
1042 * parameter to this function.
1043 *
1044 * When this function returns, the operation becomes inactive.
1045 *
1046 * \note Implementations shall make the best effort to ensure that the
1047 * comparison between the actual MAC and the expected MAC is performed
1048 * in constant time.
1049 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001050 * \param[in,out] operation Active MAC operation.
1051 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001052 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001053 *
1054 * \retval #PSA_SUCCESS
1055 * The expected MAC is identical to the actual MAC of the message.
1056 * \retval #PSA_ERROR_INVALID_SIGNATURE
1057 * The MAC of the message was calculated successfully, but it
1058 * differs from the expected MAC.
1059 * \retval #PSA_ERROR_BAD_STATE
1060 * The operation state is not valid (not started, or already completed).
1061 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1062 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1063 * \retval #PSA_ERROR_HARDWARE_FAILURE
1064 * \retval #PSA_ERROR_TAMPERING_DETECTED
1065 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001066psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1067 const uint8_t *mac,
1068 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001069
Gilles Peskinedcd14942018-07-12 00:30:52 +02001070/** Abort a MAC operation.
1071 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001072 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001073 * \p operation structure itself. Once aborted, the operation object
1074 * can be reused for another operation by calling
1075 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001076 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001077 * You may call this function any time after the operation object has
1078 * been initialized by any of the following methods:
1079 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1080 * it succeeds or not.
1081 * - Initializing the \c struct to all-bits-zero.
1082 * - Initializing the \c struct to logical zeros, e.g.
1083 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001084 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001085 * In particular, calling psa_mac_abort() after the operation has been
1086 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1087 * psa_mac_verify_finish() is safe and has no effect.
1088 *
1089 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001090 *
1091 * \retval #PSA_SUCCESS
1092 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001093 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001094 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1095 * \retval #PSA_ERROR_HARDWARE_FAILURE
1096 * \retval #PSA_ERROR_TAMPERING_DETECTED
1097 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001098psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1099
1100/**@}*/
1101
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001102/** \defgroup cipher Symmetric ciphers
1103 * @{
1104 */
1105
1106/** The type of the state data structure for multipart cipher operations.
1107 *
1108 * This is an implementation-defined \c struct. Applications should not
1109 * make any assumptions about the content of this structure except
1110 * as directed by the documentation of a specific implementation. */
1111typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1112
1113/** Set the key for a multipart symmetric encryption operation.
1114 *
1115 * The sequence of operations to encrypt a message with a symmetric cipher
1116 * is as follows:
1117 * -# Allocate an operation object which will be passed to all the functions
1118 * listed here.
Gilles Peskinefe119512018-07-08 21:39:34 +02001119 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001120 * The key remains associated with the operation even if the content
1121 * of the key slot changes.
itayzafrired7382f2018-08-02 14:19:33 +03001122 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001123 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001124 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001125 * requires a specific IV value.
1126 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1127 * of the message each time.
1128 * -# Call psa_cipher_finish().
1129 *
1130 * The application may call psa_cipher_abort() at any time after the operation
Gilles Peskinefe119512018-07-08 21:39:34 +02001131 * has been initialized with psa_cipher_encrypt_setup().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001132 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001133 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001134 * eventually terminate the operation. The following events terminate an
1135 * operation:
itayzafrired7382f2018-08-02 14:19:33 +03001136 * - A failed call to psa_cipher_generate_iv(), psa_cipher_set_iv()
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001137 * or psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001138 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001139 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001140 * \param[out] operation The operation object to use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001141 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001142 * \param alg The cipher algorithm to compute
1143 * (\c PSA_ALG_XXX value such that
1144 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001145 *
Gilles Peskine28538492018-07-11 17:34:00 +02001146 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001147 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001148 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001149 * \retval #PSA_ERROR_EMPTY_SLOT
1150 * \retval #PSA_ERROR_NOT_PERMITTED
1151 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001152 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001153 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001154 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001155 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1156 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1157 * \retval #PSA_ERROR_HARDWARE_FAILURE
1158 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001159 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001160 * The library has not been previously initialized by psa_crypto_init().
1161 * It is implementation-dependent whether a failure to initialize
1162 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001163 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001164psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001165 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001166 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001167
1168/** Set the key for a multipart symmetric decryption operation.
1169 *
1170 * The sequence of operations to decrypt a message with a symmetric cipher
1171 * is as follows:
1172 * -# Allocate an operation object which will be passed to all the functions
1173 * listed here.
Gilles Peskinefe119512018-07-08 21:39:34 +02001174 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001175 * The key remains associated with the operation even if the content
1176 * of the key slot changes.
1177 * -# Call psa_cipher_update() with the IV (initialization vector) for the
1178 * decryption. If the IV is prepended to the ciphertext, you can call
1179 * psa_cipher_update() on a buffer containing the IV followed by the
1180 * beginning of the message.
1181 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1182 * of the message each time.
1183 * -# Call psa_cipher_finish().
1184 *
1185 * The application may call psa_cipher_abort() at any time after the operation
Gilles Peskinefe119512018-07-08 21:39:34 +02001186 * has been initialized with psa_cipher_decrypt_setup().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001187 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001188 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001189 * eventually terminate the operation. The following events terminate an
1190 * operation:
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001191 * - A failed call to psa_cipher_update().
Gilles Peskine19067982018-03-20 17:54:53 +01001192 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001193 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001194 * \param[out] operation The operation object to use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001195 * \param handle Handle to the key to use for the operation.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001196 * \param alg The cipher algorithm to compute
1197 * (\c PSA_ALG_XXX value such that
1198 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001199 *
Gilles Peskine28538492018-07-11 17:34:00 +02001200 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001201 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001202 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001203 * \retval #PSA_ERROR_EMPTY_SLOT
1204 * \retval #PSA_ERROR_NOT_PERMITTED
1205 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001206 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001207 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001208 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001209 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1210 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1211 * \retval #PSA_ERROR_HARDWARE_FAILURE
1212 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001213 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001214 * The library has not been previously initialized by psa_crypto_init().
1215 * It is implementation-dependent whether a failure to initialize
1216 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001217 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001218psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001219 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001220 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001221
Gilles Peskinedcd14942018-07-12 00:30:52 +02001222/** Generate an IV for a symmetric encryption operation.
1223 *
1224 * This function generates a random IV (initialization vector), nonce
1225 * or initial counter value for the encryption operation as appropriate
1226 * for the chosen algorithm, key type and key size.
1227 *
1228 * The application must call psa_cipher_encrypt_setup() before
1229 * calling this function.
1230 *
1231 * If this function returns an error status, the operation becomes inactive.
1232 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001233 * \param[in,out] operation Active cipher operation.
1234 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001235 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001236 * \param[out] iv_length On success, the number of bytes of the
1237 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001238 *
1239 * \retval #PSA_SUCCESS
1240 * Success.
1241 * \retval #PSA_ERROR_BAD_STATE
1242 * The operation state is not valid (not started, or IV already set).
1243 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001244 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001245 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1246 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1247 * \retval #PSA_ERROR_HARDWARE_FAILURE
1248 * \retval #PSA_ERROR_TAMPERING_DETECTED
1249 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001250psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1251 unsigned char *iv,
1252 size_t iv_size,
1253 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001254
Gilles Peskinedcd14942018-07-12 00:30:52 +02001255/** Set the IV for a symmetric encryption or decryption operation.
1256 *
1257 * This function sets the random IV (initialization vector), nonce
1258 * or initial counter value for the encryption or decryption operation.
1259 *
1260 * The application must call psa_cipher_encrypt_setup() before
1261 * calling this function.
1262 *
1263 * If this function returns an error status, the operation becomes inactive.
1264 *
1265 * \note When encrypting, applications should use psa_cipher_generate_iv()
1266 * instead of this function, unless implementing a protocol that requires
1267 * a non-random IV.
1268 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001269 * \param[in,out] operation Active cipher operation.
1270 * \param[in] iv Buffer containing the IV to use.
1271 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001272 *
1273 * \retval #PSA_SUCCESS
1274 * Success.
1275 * \retval #PSA_ERROR_BAD_STATE
1276 * The operation state is not valid (not started, or IV already set).
1277 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001278 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001279 * or the chosen algorithm does not use an IV.
1280 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1281 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1282 * \retval #PSA_ERROR_HARDWARE_FAILURE
1283 * \retval #PSA_ERROR_TAMPERING_DETECTED
1284 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001285psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1286 const unsigned char *iv,
1287 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001288
Gilles Peskinedcd14942018-07-12 00:30:52 +02001289/** Encrypt or decrypt a message fragment in an active cipher operation.
1290 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001291 * Before calling this function, you must:
1292 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1293 * The choice of setup function determines whether this function
1294 * encrypts or decrypts its input.
1295 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1296 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001297 *
1298 * If this function returns an error status, the operation becomes inactive.
1299 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001300 * \param[in,out] operation Active cipher operation.
1301 * \param[in] input Buffer containing the message fragment to
1302 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001303 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001304 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001305 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001306 * \param[out] output_length On success, the number of bytes
1307 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001308 *
1309 * \retval #PSA_SUCCESS
1310 * Success.
1311 * \retval #PSA_ERROR_BAD_STATE
1312 * The operation state is not valid (not started, IV required but
1313 * not set, or already completed).
1314 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1315 * The size of the \p output buffer is too small.
1316 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1317 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1318 * \retval #PSA_ERROR_HARDWARE_FAILURE
1319 * \retval #PSA_ERROR_TAMPERING_DETECTED
1320 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001321psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1322 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001323 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001324 unsigned char *output,
1325 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001326 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001327
Gilles Peskinedcd14942018-07-12 00:30:52 +02001328/** Finish encrypting or decrypting a message in a cipher operation.
1329 *
1330 * The application must call psa_cipher_encrypt_setup() or
1331 * psa_cipher_decrypt_setup() before calling this function. The choice
1332 * of setup function determines whether this function encrypts or
1333 * decrypts its input.
1334 *
1335 * This function finishes the encryption or decryption of the message
1336 * formed by concatenating the inputs passed to preceding calls to
1337 * psa_cipher_update().
1338 *
1339 * When this function returns, the operation becomes inactive.
1340 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001341 * \param[in,out] operation Active cipher operation.
1342 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001343 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001344 * \param[out] output_length On success, the number of bytes
1345 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001346 *
1347 * \retval #PSA_SUCCESS
1348 * Success.
1349 * \retval #PSA_ERROR_BAD_STATE
1350 * The operation state is not valid (not started, IV required but
1351 * not set, or already completed).
1352 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1353 * The size of the \p output buffer is too small.
1354 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1355 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1356 * \retval #PSA_ERROR_HARDWARE_FAILURE
1357 * \retval #PSA_ERROR_TAMPERING_DETECTED
1358 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001359psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001360 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001361 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001362 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001363
Gilles Peskinedcd14942018-07-12 00:30:52 +02001364/** Abort a cipher operation.
1365 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001366 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001367 * \p operation structure itself. Once aborted, the operation object
1368 * can be reused for another operation by calling
1369 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001370 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001371 * You may call this function any time after the operation object has
1372 * been initialized by any of the following methods:
1373 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1374 * whether it succeeds or not.
1375 * - Initializing the \c struct to all-bits-zero.
1376 * - Initializing the \c struct to logical zeros, e.g.
1377 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001378 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001379 * In particular, calling psa_cipher_abort() after the operation has been
1380 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1381 * is safe and has no effect.
1382 *
1383 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001384 *
1385 * \retval #PSA_SUCCESS
1386 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001387 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001388 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1389 * \retval #PSA_ERROR_HARDWARE_FAILURE
1390 * \retval #PSA_ERROR_TAMPERING_DETECTED
1391 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001392psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1393
1394/**@}*/
1395
Gilles Peskine3b555712018-03-03 21:27:57 +01001396/** \defgroup aead Authenticated encryption with associated data (AEAD)
1397 * @{
1398 */
1399
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001400/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001401 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001402 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001403 * \param alg The AEAD algorithm to compute
1404 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001405 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001406 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001407 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001408 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001409 * but not encrypted.
1410 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001411 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001412 * encrypted.
1413 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001414 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001415 * encrypted data. The additional data is not
1416 * part of this output. For algorithms where the
1417 * encrypted data and the authentication tag
1418 * are defined as separate outputs, the
1419 * authentication tag is appended to the
1420 * encrypted data.
1421 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1422 * This must be at least
1423 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1424 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001425 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001426 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001427 *
Gilles Peskine28538492018-07-11 17:34:00 +02001428 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001429 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001430 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001431 * \retval #PSA_ERROR_EMPTY_SLOT
1432 * \retval #PSA_ERROR_NOT_PERMITTED
1433 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001434 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001435 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001436 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001437 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1438 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1439 * \retval #PSA_ERROR_HARDWARE_FAILURE
1440 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001441 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001442 * The library has not been previously initialized by psa_crypto_init().
1443 * It is implementation-dependent whether a failure to initialize
1444 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001445 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001446psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001447 psa_algorithm_t alg,
1448 const uint8_t *nonce,
1449 size_t nonce_length,
1450 const uint8_t *additional_data,
1451 size_t additional_data_length,
1452 const uint8_t *plaintext,
1453 size_t plaintext_length,
1454 uint8_t *ciphertext,
1455 size_t ciphertext_size,
1456 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001457
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001458/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001459 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001460 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001461 * \param alg The AEAD algorithm to compute
1462 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001463 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001464 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001465 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001466 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001467 * but not encrypted.
1468 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001469 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001470 * encrypted. For algorithms where the
1471 * encrypted data and the authentication tag
1472 * are defined as separate inputs, the buffer
1473 * must contain the encrypted data followed
1474 * by the authentication tag.
1475 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001476 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001477 * \param plaintext_size Size of the \p plaintext buffer in bytes.
1478 * This must be at least
1479 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
1480 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001481 * \param[out] plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03001482 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001483 *
Gilles Peskine28538492018-07-11 17:34:00 +02001484 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001485 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001486 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001487 * \retval #PSA_ERROR_EMPTY_SLOT
1488 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001489 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02001490 * \retval #PSA_ERROR_NOT_PERMITTED
1491 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001492 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001493 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001494 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001495 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1496 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1497 * \retval #PSA_ERROR_HARDWARE_FAILURE
1498 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001499 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001500 * The library has not been previously initialized by psa_crypto_init().
1501 * It is implementation-dependent whether a failure to initialize
1502 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001503 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001504psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001505 psa_algorithm_t alg,
1506 const uint8_t *nonce,
1507 size_t nonce_length,
1508 const uint8_t *additional_data,
1509 size_t additional_data_length,
1510 const uint8_t *ciphertext,
1511 size_t ciphertext_length,
1512 uint8_t *plaintext,
1513 size_t plaintext_size,
1514 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001515
1516/**@}*/
1517
Gilles Peskine20035e32018-02-03 22:44:14 +01001518/** \defgroup asymmetric Asymmetric cryptography
1519 * @{
1520 */
1521
1522/**
1523 * \brief Sign a hash or short message with a private key.
1524 *
Gilles Peskine08bac712018-06-26 16:14:46 +02001525 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001526 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02001527 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
1528 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
1529 * to determine the hash algorithm to use.
1530 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001531 * \param handle Handle to the key to use for the operation.
1532 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001533 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001534 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001535 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001536 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001537 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001538 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001539 * \param[out] signature_length On success, the number of bytes
1540 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001541 *
Gilles Peskine28538492018-07-11 17:34:00 +02001542 * \retval #PSA_SUCCESS
1543 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001544 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01001545 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001546 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01001547 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001548 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001549 * \retval #PSA_ERROR_NOT_SUPPORTED
1550 * \retval #PSA_ERROR_INVALID_ARGUMENT
1551 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1552 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1553 * \retval #PSA_ERROR_HARDWARE_FAILURE
1554 * \retval #PSA_ERROR_TAMPERING_DETECTED
1555 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03001556 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001557 * The library has not been previously initialized by psa_crypto_init().
1558 * It is implementation-dependent whether a failure to initialize
1559 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01001560 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001561psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01001562 psa_algorithm_t alg,
1563 const uint8_t *hash,
1564 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01001565 uint8_t *signature,
1566 size_t signature_size,
1567 size_t *signature_length);
1568
1569/**
1570 * \brief Verify the signature a hash or short message using a public key.
1571 *
Gilles Peskine08bac712018-06-26 16:14:46 +02001572 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001573 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02001574 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
1575 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
1576 * to determine the hash algorithm to use.
1577 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001578 * \param handle Handle to the key to use for the operation.
1579 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001580 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001581 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001582 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02001583 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001584 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001585 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001586 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01001587 *
Gilles Peskine28538492018-07-11 17:34:00 +02001588 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01001589 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02001590 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01001591 * The calculation was perfomed successfully, but the passed
1592 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02001593 * \retval #PSA_ERROR_NOT_SUPPORTED
1594 * \retval #PSA_ERROR_INVALID_ARGUMENT
1595 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1596 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1597 * \retval #PSA_ERROR_HARDWARE_FAILURE
1598 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001599 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001600 * The library has not been previously initialized by psa_crypto_init().
1601 * It is implementation-dependent whether a failure to initialize
1602 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01001603 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001604psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01001605 psa_algorithm_t alg,
1606 const uint8_t *hash,
1607 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02001608 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02001609 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01001610
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001611/**
1612 * \brief Encrypt a short message with a public key.
1613 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001614 * \param handle Handle to the key to use for the operation.
1615 * It must be a public key or an asymmetric
1616 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001617 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001618 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001619 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001620 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001621 * \param[in] salt A salt or label, if supported by the
1622 * encryption algorithm.
1623 * If the algorithm does not support a
1624 * salt, pass \c NULL.
1625 * If the algorithm supports an optional
1626 * salt and you do not want to pass a salt,
1627 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001628 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001629 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1630 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001631 * \param salt_length Size of the \p salt buffer in bytes.
1632 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001633 * \param[out] output Buffer where the encrypted message is to
1634 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001635 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001636 * \param[out] output_length On success, the number of bytes
1637 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001638 *
Gilles Peskine28538492018-07-11 17:34:00 +02001639 * \retval #PSA_SUCCESS
1640 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001641 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001642 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001643 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001644 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001645 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001646 * \retval #PSA_ERROR_NOT_SUPPORTED
1647 * \retval #PSA_ERROR_INVALID_ARGUMENT
1648 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1649 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1650 * \retval #PSA_ERROR_HARDWARE_FAILURE
1651 * \retval #PSA_ERROR_TAMPERING_DETECTED
1652 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03001653 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001654 * The library has not been previously initialized by psa_crypto_init().
1655 * It is implementation-dependent whether a failure to initialize
1656 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001657 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001658psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001659 psa_algorithm_t alg,
1660 const uint8_t *input,
1661 size_t input_length,
1662 const uint8_t *salt,
1663 size_t salt_length,
1664 uint8_t *output,
1665 size_t output_size,
1666 size_t *output_length);
1667
1668/**
1669 * \brief Decrypt a short message with a private key.
1670 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001671 * \param handle Handle to the key to use for the operation.
1672 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001673 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001674 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001675 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001676 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001677 * \param[in] salt A salt or label, if supported by the
1678 * encryption algorithm.
1679 * If the algorithm does not support a
1680 * salt, pass \c NULL.
1681 * If the algorithm supports an optional
1682 * salt and you do not want to pass a salt,
1683 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001684 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001685 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
1686 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001687 * \param salt_length Size of the \p salt buffer in bytes.
1688 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001689 * \param[out] output Buffer where the decrypted message is to
1690 * be written.
1691 * \param output_size Size of the \c output buffer in bytes.
1692 * \param[out] output_length On success, the number of bytes
1693 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001694 *
Gilles Peskine28538492018-07-11 17:34:00 +02001695 * \retval #PSA_SUCCESS
1696 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001697 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001698 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001699 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001700 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001701 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02001702 * \retval #PSA_ERROR_NOT_SUPPORTED
1703 * \retval #PSA_ERROR_INVALID_ARGUMENT
1704 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1705 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1706 * \retval #PSA_ERROR_HARDWARE_FAILURE
1707 * \retval #PSA_ERROR_TAMPERING_DETECTED
1708 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
1709 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03001710 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001711 * The library has not been previously initialized by psa_crypto_init().
1712 * It is implementation-dependent whether a failure to initialize
1713 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001714 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001715psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02001716 psa_algorithm_t alg,
1717 const uint8_t *input,
1718 size_t input_length,
1719 const uint8_t *salt,
1720 size_t salt_length,
1721 uint8_t *output,
1722 size_t output_size,
1723 size_t *output_length);
1724
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001725/**@}*/
1726
Gilles Peskineedd76872018-07-20 17:42:05 +02001727/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02001728 * @{
1729 */
1730
1731/** The type of the state data structure for generators.
1732 *
1733 * Before calling any function on a generator, the application must
1734 * initialize it by any of the following means:
1735 * - Set the structure to all-bits-zero, for example:
1736 * \code
1737 * psa_crypto_generator_t generator;
1738 * memset(&generator, 0, sizeof(generator));
1739 * \endcode
1740 * - Initialize the structure to logical zero values, for example:
1741 * \code
1742 * psa_crypto_generator_t generator = {0};
1743 * \endcode
1744 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
1745 * for example:
1746 * \code
1747 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1748 * \endcode
1749 * - Assign the result of the function psa_crypto_generator_init()
1750 * to the structure, for example:
1751 * \code
1752 * psa_crypto_generator_t generator;
1753 * generator = psa_crypto_generator_init();
1754 * \endcode
1755 *
1756 * This is an implementation-defined \c struct. Applications should not
1757 * make any assumptions about the content of this structure except
1758 * as directed by the documentation of a specific implementation.
1759 */
1760typedef struct psa_crypto_generator_s psa_crypto_generator_t;
1761
1762/** \def PSA_CRYPTO_GENERATOR_INIT
1763 *
1764 * This macro returns a suitable initializer for a generator object
1765 * of type #psa_crypto_generator_t.
1766 */
1767#ifdef __DOXYGEN_ONLY__
1768/* This is an example definition for documentation purposes.
1769 * Implementations should define a suitable value in `crypto_struct.h`.
1770 */
1771#define PSA_CRYPTO_GENERATOR_INIT {0}
1772#endif
1773
1774/** Return an initial value for a generator object.
1775 */
1776static psa_crypto_generator_t psa_crypto_generator_init(void);
1777
1778/** Retrieve the current capacity of a generator.
1779 *
1780 * The capacity of a generator is the maximum number of bytes that it can
1781 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
1782 *
1783 * \param[in] generator The generator to query.
1784 * \param[out] capacity On success, the capacity of the generator.
1785 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001786 * \retval #PSA_SUCCESS
1787 * \retval #PSA_ERROR_BAD_STATE
1788 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02001789 */
1790psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
1791 size_t *capacity);
1792
1793/** Read some data from a generator.
1794 *
1795 * This function reads and returns a sequence of bytes from a generator.
1796 * The data that is read is discarded from the generator. The generator's
1797 * capacity is decreased by the number of bytes read.
1798 *
1799 * \param[in,out] generator The generator object to read from.
1800 * \param[out] output Buffer where the generator output will be
1801 * written.
1802 * \param output_length Number of bytes to output.
1803 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001804 * \retval #PSA_SUCCESS
1805 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02001806 * There were fewer than \p output_length bytes
1807 * in the generator. Note that in this case, no
1808 * output is written to the output buffer.
1809 * The generator's capacity is set to 0, thus
1810 * subsequent calls to this function will not
1811 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001812 * \retval #PSA_ERROR_BAD_STATE
1813 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1814 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1815 * \retval #PSA_ERROR_HARDWARE_FAILURE
1816 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02001817 */
1818psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
1819 uint8_t *output,
1820 size_t output_length);
1821
1822/** Create a symmetric key from data read from a generator.
1823 *
1824 * This function reads a sequence of bytes from a generator and imports
1825 * these bytes as a key.
1826 * The data that is read is discarded from the generator. The generator's
1827 * capacity is decreased by the number of bytes read.
1828 *
1829 * This function is equivalent to calling #psa_generator_read and
1830 * passing the resulting output to #psa_import_key, but
1831 * if the implementation provides an isolation boundary then
1832 * the key material is not exposed outside the isolation boundary.
1833 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001834 * \param handle Handle to the slot where the key will be stored.
1835 * This must be a valid slot for a key of the chosen
1836 * type: it must have been obtained by calling
1837 * psa_allocate_key() or psa_create_key() with the
1838 * correct \p type and with a maximum size that is
1839 * compatible with \p bits.
1840 * It must not contain any key material yet.
Gilles Peskineeab56e42018-07-12 17:12:33 +02001841 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
1842 * This must be a symmetric key type.
1843 * \param bits Key size in bits.
1844 * \param[in,out] generator The generator object to read from.
1845 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001846 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02001847 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01001848 * If the key is persistent, the key material and the key's metadata
1849 * have been saved to persistent storage.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001850 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02001851 * There were fewer than \p output_length bytes
1852 * in the generator. Note that in this case, no
1853 * output is written to the output buffer.
1854 * The generator's capacity is set to 0, thus
1855 * subsequent calls to this function will not
1856 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001857 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02001858 * The key type or key size is not supported, either by the
1859 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001860 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineae32aac2018-11-30 14:39:32 +01001861 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001862 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskineeab56e42018-07-12 17:12:33 +02001863 * There is already a key in the specified slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001864 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1865 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1866 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1867 * \retval #PSA_ERROR_HARDWARE_FAILURE
1868 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001869 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001870 * The library has not been previously initialized by psa_crypto_init().
1871 * It is implementation-dependent whether a failure to initialize
1872 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02001873 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001874psa_status_t psa_generator_import_key(psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02001875 psa_key_type_t type,
1876 size_t bits,
1877 psa_crypto_generator_t *generator);
1878
1879/** Abort a generator.
1880 *
1881 * Once a generator has been aborted, its capacity is zero.
1882 * Aborting a generator frees all associated resources except for the
1883 * \c generator structure itself.
1884 *
1885 * This function may be called at any time as long as the generator
1886 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
1887 * psa_crypto_generator_init() or a zero value. In particular, it is valid
1888 * to call psa_generator_abort() twice, or to call psa_generator_abort()
1889 * on a generator that has not been set up.
1890 *
1891 * Once aborted, the generator object may be called.
1892 *
1893 * \param[in,out] generator The generator to abort.
1894 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01001895 * \retval #PSA_SUCCESS
1896 * \retval #PSA_ERROR_BAD_STATE
1897 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1898 * \retval #PSA_ERROR_HARDWARE_FAILURE
1899 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02001900 */
1901psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
1902
Gilles Peskine8feb3a82018-09-18 12:06:11 +02001903/** Use the maximum possible capacity for a generator.
1904 *
1905 * Use this value as the capacity argument when setting up a generator
1906 * to indicate that the generator should have the maximum possible capacity.
1907 * The value of the maximum possible capacity depends on the generator
1908 * algorithm.
1909 */
1910#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
1911
Gilles Peskineeab56e42018-07-12 17:12:33 +02001912/**@}*/
1913
Gilles Peskineea0fb492018-07-12 17:17:20 +02001914/** \defgroup derivation Key derivation
1915 * @{
1916 */
1917
1918/** Set up a key derivation operation.
1919 *
1920 * A key derivation algorithm takes three inputs: a secret input \p key and
1921 * two non-secret inputs \p label and p salt.
1922 * The result of this function is a byte generator which can
1923 * be used to produce keys and other cryptographic material.
1924 *
1925 * The role of \p label and \p salt is as follows:
Gilles Peskinebef7f142018-07-12 17:22:21 +02001926 * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step
1927 * and \p label is the info string used in the "expand" step.
Gilles Peskineea0fb492018-07-12 17:17:20 +02001928 *
1929 * \param[in,out] generator The generator object to set up. It must
Gilles Peskine92587db2018-09-18 12:12:42 +02001930 * have been initialized to all-bits-zero,
1931 * a logical zero (`{0}`),
1932 * \c PSA_CRYPTO_GENERATOR_INIT or
1933 * psa_crypto_generator_init().
Gilles Peskineae32aac2018-11-30 14:39:32 +01001934 * \param handle Handle to the secret key.
Gilles Peskineea0fb492018-07-12 17:17:20 +02001935 * \param alg The key derivation algorithm to compute
1936 * (\c PSA_ALG_XXX value such that
1937 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
1938 * \param[in] salt Salt to use.
1939 * \param salt_length Size of the \p salt buffer in bytes.
1940 * \param[in] label Label to use.
1941 * \param label_length Size of the \p label buffer in bytes.
1942 * \param capacity The maximum number of bytes that the
1943 * generator will be able to provide.
1944 *
1945 * \retval #PSA_SUCCESS
1946 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001947 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineea0fb492018-07-12 17:17:20 +02001948 * \retval #PSA_ERROR_EMPTY_SLOT
1949 * \retval #PSA_ERROR_NOT_PERMITTED
1950 * \retval #PSA_ERROR_INVALID_ARGUMENT
1951 * \c key is not compatible with \c alg,
1952 * or \p capacity is too large for the specified algorithm and key.
1953 * \retval #PSA_ERROR_NOT_SUPPORTED
1954 * \c alg is not supported or is not a key derivation algorithm.
1955 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1956 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1957 * \retval #PSA_ERROR_HARDWARE_FAILURE
1958 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001959 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001960 * The library has not been previously initialized by psa_crypto_init().
1961 * It is implementation-dependent whether a failure to initialize
1962 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02001963 */
1964psa_status_t psa_key_derivation(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001965 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02001966 psa_algorithm_t alg,
1967 const uint8_t *salt,
1968 size_t salt_length,
1969 const uint8_t *label,
1970 size_t label_length,
1971 size_t capacity);
1972
Gilles Peskine01d718c2018-09-18 12:01:02 +02001973/** Set up a key agreement operation.
1974 *
1975 * A key agreement algorithm takes two inputs: a private key \p private_key
1976 * a public key \p peer_key.
1977 * The result of this function is a byte generator which can
1978 * be used to produce keys and other cryptographic material.
1979 *
Gilles Peskine211a4362018-10-25 22:22:31 +02001980 * The resulting generator always has the maximum capacity permitted by
1981 * the algorithm.
1982 *
Gilles Peskine01d718c2018-09-18 12:01:02 +02001983 * \param[in,out] generator The generator object to set up. It must
1984 * have been initialized to all-bits-zero,
1985 * a logical zero (`{0}`),
1986 * \c PSA_CRYPTO_GENERATOR_INIT or
1987 * psa_crypto_generator_init().
Gilles Peskineae32aac2018-11-30 14:39:32 +01001988 * \param private_key Handle to the private key to use.
Gilles Peskined171e782018-11-15 17:46:21 +01001989 * \param[in] peer_key Public key of the peer. It must be
1990 * in the same format that psa_import_key()
1991 * accepts. The standard formats for public
1992 * keys are documented in the documentation
1993 * of psa_export_public_key().
Gilles Peskine01d718c2018-09-18 12:01:02 +02001994 * \param peer_key_length Size of \p peer_key in bytes.
1995 * \param alg The key agreement algorithm to compute
1996 * (\c PSA_ALG_XXX value such that
1997 * #PSA_ALG_IS_KEY_AGREEMENT(\p alg) is true).
1998 *
1999 * \retval #PSA_SUCCESS
2000 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002001 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine01d718c2018-09-18 12:01:02 +02002002 * \retval #PSA_ERROR_EMPTY_SLOT
2003 * \retval #PSA_ERROR_NOT_PERMITTED
2004 * \retval #PSA_ERROR_INVALID_ARGUMENT
2005 * \c private_key is not compatible with \c alg,
2006 * or \p peer_key is not valid for \c alg or not compatible with
2007 * \c private_key.
2008 * \retval #PSA_ERROR_NOT_SUPPORTED
2009 * \c alg is not supported or is not a key derivation algorithm.
2010 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2011 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2012 * \retval #PSA_ERROR_HARDWARE_FAILURE
2013 * \retval #PSA_ERROR_TAMPERING_DETECTED
2014 */
2015psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002016 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02002017 const uint8_t *peer_key,
2018 size_t peer_key_length,
2019 psa_algorithm_t alg);
2020
Gilles Peskineea0fb492018-07-12 17:17:20 +02002021/**@}*/
2022
Gilles Peskineedd76872018-07-20 17:42:05 +02002023/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002024 * @{
2025 */
2026
2027/**
2028 * \brief Generate random bytes.
2029 *
2030 * \warning This function **can** fail! Callers MUST check the return status
2031 * and MUST NOT use the content of the output buffer if the return
2032 * status is not #PSA_SUCCESS.
2033 *
2034 * \note To generate a key, use psa_generate_key() instead.
2035 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002036 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002037 * \param output_size Number of bytes to generate and output.
2038 *
Gilles Peskine28538492018-07-11 17:34:00 +02002039 * \retval #PSA_SUCCESS
2040 * \retval #PSA_ERROR_NOT_SUPPORTED
2041 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2042 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2043 * \retval #PSA_ERROR_HARDWARE_FAILURE
2044 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03002045 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002046 * The library has not been previously initialized by psa_crypto_init().
2047 * It is implementation-dependent whether a failure to initialize
2048 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002049 */
2050psa_status_t psa_generate_random(uint8_t *output,
2051 size_t output_size);
2052
Gilles Peskine4c317f42018-07-12 01:24:09 +02002053/** Extra parameters for RSA key generation.
2054 *
Gilles Peskinebe42f312018-07-13 14:38:15 +02002055 * You may pass a pointer to a structure of this type as the \c extra
Gilles Peskine4c317f42018-07-12 01:24:09 +02002056 * parameter to psa_generate_key().
2057 */
2058typedef struct {
Gilles Peskineedd76872018-07-20 17:42:05 +02002059 uint32_t e; /**< Public exponent value. Default: 65537. */
Gilles Peskine4c317f42018-07-12 01:24:09 +02002060} psa_generate_key_extra_rsa;
2061
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002062/**
2063 * \brief Generate a key or key pair.
2064 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002065 * \param handle Handle to the slot where the key will be stored.
2066 * This must be a valid slot for a key of the chosen
2067 * type: it must have been obtained by calling
2068 * psa_allocate_key() or psa_create_key() with the
2069 * correct \p type and with a maximum size that is
2070 * compatible with \p bits.
2071 * It must not contain any key material yet.
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002072 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2073 * \param bits Key size in bits.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002074 * \param[in] extra Extra parameters for key generation. The
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002075 * interpretation of this parameter depends on
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002076 * \p type. All types support \c NULL to use
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002077 * default parameters. Implementation that support
2078 * the generation of vendor-specific key types
2079 * that allow extra parameters shall document
2080 * the format of these extra parameters and
2081 * the default values. For standard parameters,
2082 * the meaning of \p extra is as follows:
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002083 * - For a symmetric key type (a type such
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002084 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
2085 * false), \p extra must be \c NULL.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002086 * - For an elliptic curve key type (a type
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002087 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
2088 * false), \p extra must be \c NULL.
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002089 * - For an RSA key (\p type is
2090 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
2091 * optional #psa_generate_key_extra_rsa structure
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002092 * specifying the public exponent. The
2093 * default public exponent used when \p extra
2094 * is \c NULL is 65537.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002095 * \param extra_size Size of the buffer that \p extra
2096 * points to, in bytes. Note that if \p extra is
2097 * \c NULL then \p extra_size must be zero.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002098 *
Gilles Peskine28538492018-07-11 17:34:00 +02002099 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01002100 * Success.
2101 * If the key is persistent, the key material and the key's metadata
2102 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002103 * \retval #PSA_ERROR_INVALID_HANDLE
2104 * \retval #PSA_ERROR_OCCUPIED_SLOT
2105 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +02002106 * \retval #PSA_ERROR_NOT_SUPPORTED
2107 * \retval #PSA_ERROR_INVALID_ARGUMENT
2108 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2109 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2110 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2111 * \retval #PSA_ERROR_HARDWARE_FAILURE
2112 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002113 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002114 * The library has not been previously initialized by psa_crypto_init().
2115 * It is implementation-dependent whether a failure to initialize
2116 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002117 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002118psa_status_t psa_generate_key(psa_key_handle_t handle,
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002119 psa_key_type_t type,
2120 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02002121 const void *extra,
2122 size_t extra_size);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002123
2124/**@}*/
2125
Gilles Peskinee59236f2018-01-27 23:32:46 +01002126#ifdef __cplusplus
2127}
2128#endif
2129
Gilles Peskine0cad07c2018-06-27 19:49:02 +02002130/* The file "crypto_sizes.h" contains definitions for size calculation
2131 * macros whose definitions are implementation-specific. */
2132#include "crypto_sizes.h"
2133
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002134/* The file "crypto_struct.h" contains definitions for
2135 * implementation-specific structs that are declared above. */
2136#include "crypto_struct.h"
2137
2138/* The file "crypto_extra.h" contains vendor-specific definitions. This
2139 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01002140#include "crypto_extra.h"
2141
2142#endif /* PSA_CRYPTO_H */