blob: 4554158516c7800092e7c899c67ce9426d34d9a1 [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 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100234 * If the key is currently in use in a multipart operation,
235 * the multipart operation is aborted.
236 *
Gilles Peskinef535eb22018-11-30 14:08:36 +0100237 * \param handle The key handle to close.
238 *
239 * \retval #PSA_SUCCESS
240 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineae32aac2018-11-30 14:39:32 +0100241 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskinef535eb22018-11-30 14:08:36 +0100242 */
243psa_status_t psa_close_key(psa_key_handle_t handle);
244
Gilles Peskine3cac8c42018-11-30 14:07:45 +0100245/**@}*/
246
247/** \defgroup import_export Key import and export
248 * @{
249 */
250
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100251/**
252 * \brief Import a key in binary format.
253 *
Gilles Peskinef5b9fa12018-03-07 16:40:18 +0100254 * This function supports any output from psa_export_key(). Refer to the
Gilles Peskinef7933932018-10-31 14:07:52 +0100255 * documentation of psa_export_public_key() for the format of public keys
256 * and to the documentation of psa_export_key() for the format for
257 * other key types.
258 *
259 * This specification supports a single format for each key type.
260 * Implementations may support other formats as long as the standard
261 * format is supported. Implementations that support other formats
262 * should ensure that the formats are clearly unambiguous so as to
263 * minimize the risk that an invalid input is accidentally interpreted
264 * according to a different format.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100265 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100266 * \param handle Handle to the slot where the key will be stored.
267 * This must be a valid slot for a key of the chosen
268 * type: it must have been obtained by calling
269 * psa_allocate_key() or psa_create_key() with the
270 * correct \p type and with a maximum size that is
271 * compatible with \p data.
Gilles Peskinef7933932018-10-31 14:07:52 +0100272 * \param type Key type (a \c PSA_KEY_TYPE_XXX value). On a successful
273 * import, the key slot will contain a key of this type.
274 * \param[in] data Buffer containing the key data. The content of this
275 * buffer is interpreted according to \p type. It must
276 * contain the format described in the documentation
277 * of psa_export_key() or psa_export_public_key() for
278 * the chosen type.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200279 * \param data_length Size of the \p data buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100280 *
Gilles Peskine28538492018-07-11 17:34:00 +0200281 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100282 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100283 * If the key is persistent, the key material and the key's metadata
284 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100285 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200286 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200287 * The key type or key size is not supported, either by the
288 * implementation in general or in this particular slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200289 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine308b91d2018-02-08 09:47:44 +0100290 * The key slot is invalid,
291 * or the key data is not correctly formatted.
Gilles Peskine28538492018-07-11 17:34:00 +0200292 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskine65eb8582018-04-19 08:28:58 +0200293 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +0200294 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
295 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
296 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Darryl Greend49a4992018-06-18 17:27:26 +0100297 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine28538492018-07-11 17:34:00 +0200298 * \retval #PSA_ERROR_HARDWARE_FAILURE
299 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300300 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300301 * The library has not been previously initialized by psa_crypto_init().
302 * It is implementation-dependent whether a failure to initialize
303 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100304 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100305psa_status_t psa_import_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100306 psa_key_type_t type,
307 const uint8_t *data,
308 size_t data_length);
309
310/**
Gilles Peskineae32aac2018-11-30 14:39:32 +0100311 * \brief Destroy a key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200312 *
313 * This function destroys the content of the key slot from both volatile
314 * memory and, if applicable, non-volatile storage. Implementations shall
315 * make a best effort to ensure that any previous content of the slot is
316 * unrecoverable.
317 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100318 * This function also erases any metadata such as policies and frees all
319 * resources associated with the key.
Gilles Peskine154bd952018-04-19 08:38:16 +0200320 *
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100321 * If the key is currently in use in a multipart operation,
322 * the multipart operation is aborted.
323 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100324 * \param handle Handle to the key slot to erase.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100325 *
Gilles Peskine28538492018-07-11 17:34:00 +0200326 * \retval #PSA_SUCCESS
Gilles Peskine65eb8582018-04-19 08:28:58 +0200327 * The slot's content, if any, has been erased.
Gilles Peskine28538492018-07-11 17:34:00 +0200328 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200329 * The slot holds content and cannot be erased because it is
330 * read-only, either due to a policy or due to physical restrictions.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100331 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200332 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200333 * There was an failure in communication with the cryptoprocessor.
334 * The key material may still be present in the cryptoprocessor.
Gilles Peskine28538492018-07-11 17:34:00 +0200335 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine65eb8582018-04-19 08:28:58 +0200336 * The storage is corrupted. Implementations shall make a best effort
337 * to erase key material even in this stage, however applications
338 * should be aware that it may be impossible to guarantee that the
339 * key material is not recoverable in such cases.
Gilles Peskine28538492018-07-11 17:34:00 +0200340 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine65eb8582018-04-19 08:28:58 +0200341 * An unexpected condition which is not a storage corruption or
342 * a communication failure occurred. The cryptoprocessor may have
343 * been compromised.
itayzafrir90d8c7a2018-09-12 11:44:52 +0300344 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300345 * The library has not been previously initialized by psa_crypto_init().
346 * It is implementation-dependent whether a failure to initialize
347 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100348 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100349psa_status_t psa_destroy_key(psa_key_handle_t handle);
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100350
351/**
352 * \brief Get basic metadata about a key.
353 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100354 * \param handle Handle to the key slot to query.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200355 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX value).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100356 * This may be a null pointer, in which case the key type
357 * is not written.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200358 * \param[out] bits On success, the key size in bits.
Gilles Peskine9a1ba0d2018-03-21 20:49:16 +0100359 * This may be a null pointer, in which case the key size
Gilles Peskine308b91d2018-02-08 09:47:44 +0100360 * is not written.
361 *
Gilles Peskine28538492018-07-11 17:34:00 +0200362 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100363 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200364 * \retval #PSA_ERROR_EMPTY_SLOT
Gilles Peskineae32aac2018-11-30 14:39:32 +0100365 * The handle is to a key slot which does not contain key material yet.
Gilles Peskine28538492018-07-11 17:34:00 +0200366 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
367 * \retval #PSA_ERROR_HARDWARE_FAILURE
368 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300369 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300370 * The library has not been previously initialized by psa_crypto_init().
371 * It is implementation-dependent whether a failure to initialize
372 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100373 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100374psa_status_t psa_get_key_information(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100375 psa_key_type_t *type,
376 size_t *bits);
377
378/**
379 * \brief Export a key in binary format.
380 *
381 * The output of this function can be passed to psa_import_key() to
382 * create an equivalent object.
383 *
Gilles Peskinef7933932018-10-31 14:07:52 +0100384 * If the implementation of psa_import_key() supports other formats
385 * beyond the format specified here, the output from psa_export_key()
386 * must use the representation specified here, not the original
387 * representation.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100388 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100389 * For standard key types, the output format is as follows:
390 *
391 * - For symmetric keys (including MAC keys), the format is the
392 * raw bytes of the key.
393 * - For DES, the key data consists of 8 bytes. The parity bits must be
394 * correct.
395 * - For Triple-DES, the format is the concatenation of the
396 * two or three DES keys.
Gilles Peskine92b30732018-03-03 21:29:30 +0100397 * - For RSA key pairs (#PSA_KEY_TYPE_RSA_KEYPAIR), the format
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200398 * is the non-encrypted DER encoding of the representation defined by
399 * PKCS\#1 (RFC 8017) as `RSAPrivateKey`, version 0.
400 * ```
401 * RSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200402 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200403 * modulus INTEGER, -- n
404 * publicExponent INTEGER, -- e
405 * privateExponent INTEGER, -- d
406 * prime1 INTEGER, -- p
407 * prime2 INTEGER, -- q
408 * exponent1 INTEGER, -- d mod (p-1)
409 * exponent2 INTEGER, -- d mod (q-1)
410 * coefficient INTEGER, -- (inverse of q) mod p
411 * }
412 * ```
413 * - For DSA private keys (#PSA_KEY_TYPE_DSA_KEYPAIR), the format
414 * is the non-encrypted DER encoding of the representation used by
Gilles Peskinec6290c02018-08-13 17:24:59 +0200415 * OpenSSL and OpenSSH, whose structure is described in ASN.1 as follows:
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200416 * ```
417 * DSAPrivateKey ::= SEQUENCE {
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200418 * version INTEGER, -- must be 0
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200419 * prime INTEGER, -- p
420 * subprime INTEGER, -- q
421 * generator INTEGER, -- g
422 * public INTEGER, -- y
423 * private INTEGER, -- x
424 * }
425 * ```
426 * - For elliptic curve key pairs (key types for which
Gilles Peskinef76aa772018-10-29 19:24:33 +0100427 * #PSA_KEY_TYPE_IS_ECC_KEYPAIR is true), the format is
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100428 * a representation of the private value as a `ceiling(m/8)`-byte string
429 * where `m` is the bit size associated with the curve, i.e. the bit size
430 * of the order of the curve's coordinate field. This byte string is
431 * in little-endian order for Montgomery curves (curve types
432 * `PSA_ECC_CURVE_CURVEXXX`), and in big-endian order for Weierstrass
433 * curves (curve types `PSA_ECC_CURVE_SECTXXX`, `PSA_ECC_CURVE_SECPXXX`
434 * and `PSA_ECC_CURVE_BRAINPOOL_PXXX`).
Gilles Peskinef76aa772018-10-29 19:24:33 +0100435 * This is the content of the `privateKey` field of the `ECPrivateKey`
436 * format defined by RFC 5915.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200437 * - For public keys (key types for which #PSA_KEY_TYPE_IS_PUBLIC_KEY is
438 * true), the format is the same as for psa_export_public_key().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100439 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100440 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200441 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200442 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200443 * \param[out] data_length On success, the number of bytes
444 * that make up the key data.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100445 *
Gilles Peskine28538492018-07-11 17:34:00 +0200446 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100447 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200448 * \retval #PSA_ERROR_EMPTY_SLOT
449 * \retval #PSA_ERROR_NOT_PERMITTED
Darryl Green9e2d7a02018-07-24 16:33:30 +0100450 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine1be949b2018-08-10 19:06:59 +0200451 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
452 * The size of the \p data buffer is too small. You can determine a
453 * sufficient buffer size by calling
454 * #PSA_KEY_EXPORT_MAX_SIZE(\c type, \c bits)
455 * where \c type is the key type
456 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200457 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
458 * \retval #PSA_ERROR_HARDWARE_FAILURE
459 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300460 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300461 * The library has not been previously initialized by psa_crypto_init().
462 * It is implementation-dependent whether a failure to initialize
463 * results in this error code.
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100464 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100465psa_status_t psa_export_key(psa_key_handle_t handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100466 uint8_t *data,
467 size_t data_size,
468 size_t *data_length);
469
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100470/**
471 * \brief Export a public key or the public part of a key pair in binary format.
472 *
473 * The output of this function can be passed to psa_import_key() to
474 * create an object that is equivalent to the public key.
475 *
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200476 * The format is the DER representation defined by RFC 5280 as
477 * `SubjectPublicKeyInfo`, with the `subjectPublicKey` format
478 * specified below.
479 * ```
480 * SubjectPublicKeyInfo ::= SEQUENCE {
481 * algorithm AlgorithmIdentifier,
482 * subjectPublicKey BIT STRING }
483 * AlgorithmIdentifier ::= SEQUENCE {
484 * algorithm OBJECT IDENTIFIER,
485 * parameters ANY DEFINED BY algorithm OPTIONAL }
486 * ```
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100487 *
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200488 * - For RSA public keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY),
489 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.1 as
490 * `RSAPublicKey`,
491 * with the OID `rsaEncryption`,
492 * and with the parameters `NULL`.
493 * ```
494 * pkcs-1 OBJECT IDENTIFIER ::= { iso(1) member-body(2) us(840)
495 * rsadsi(113549) pkcs(1) 1 }
496 * rsaEncryption OBJECT IDENTIFIER ::= { pkcs-1 1 }
497 *
498 * RSAPublicKey ::= SEQUENCE {
499 * modulus INTEGER, -- n
500 * publicExponent INTEGER } -- e
501 * ```
502 * - For DSA public keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY),
503 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.2 as
504 * `DSAPublicKey`,
505 * with the OID `id-dsa`,
506 * and with the parameters `DSS-Parms`.
507 * ```
508 * id-dsa OBJECT IDENTIFIER ::= {
509 * iso(1) member-body(2) us(840) x9-57(10040) x9cm(4) 1 }
510 *
511 * Dss-Parms ::= SEQUENCE {
512 * p INTEGER,
513 * q INTEGER,
514 * g INTEGER }
515 * DSAPublicKey ::= INTEGER -- public key, Y
516 * ```
517 * - For elliptic curve public keys (key types for which
518 * #PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY is true),
519 * the `subjectPublicKey` format is defined by RFC 3279 &sect;2.3.5 as
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200520 * `ECPoint`, which contains the uncompressed
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200521 * representation defined by SEC1 &sect;2.3.3.
522 * The OID is `id-ecPublicKey`,
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200523 * and the parameters must be given as a `namedCurve` OID as specified in
Gilles Peskinec6290c02018-08-13 17:24:59 +0200524 * RFC 5480 &sect;2.1.1.1 or other applicable standards.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200525 * ```
526 * ansi-X9-62 OBJECT IDENTIFIER ::=
527 * { iso(1) member-body(2) us(840) 10045 }
528 * id-public-key-type OBJECT IDENTIFIER ::= { ansi-X9.62 2 }
529 * id-ecPublicKey OBJECT IDENTIFIER ::= { id-publicKeyType 1 }
530 *
Gilles Peskine4f6c77b2018-08-11 01:17:53 +0200531 * ECPoint ::= ...
532 * -- first 8 bits: 0x04;
Gilles Peskine6c6a0232018-11-15 17:44:43 +0100533 * -- then x_P as a `ceiling(m/8)`-byte string, big endian;
534 * -- then y_P as a `ceiling(m/8)`-byte string, big endian;
535 * -- where `m` is the bit size associated with the curve,
Gilles Peskine7b5b4a02018-11-14 21:05:10 +0100536 * -- i.e. the bit size of `q` for a curve over `F_q`.
Gilles Peskine4e1e9be2018-08-10 18:57:40 +0200537 *
538 * EcpkParameters ::= CHOICE { -- other choices are not allowed
539 * namedCurve OBJECT IDENTIFIER }
540 * ```
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100541 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100542 * \param handle Handle to the key to export.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200543 * \param[out] data Buffer where the key data is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200544 * \param data_size Size of the \p data buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200545 * \param[out] data_length On success, the number of bytes
546 * that make up the key data.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100547 *
Gilles Peskine28538492018-07-11 17:34:00 +0200548 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100549 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +0200550 * \retval #PSA_ERROR_EMPTY_SLOT
551 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine1be949b2018-08-10 19:06:59 +0200552 * The key is neither a public key nor a key pair.
553 * \retval #PSA_ERROR_NOT_SUPPORTED
554 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
555 * The size of the \p data buffer is too small. You can determine a
556 * sufficient buffer size by calling
557 * #PSA_KEY_EXPORT_MAX_SIZE(#PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(\c type), \c bits)
558 * where \c type is the key type
559 * and \c bits is the key size in bits.
Gilles Peskine28538492018-07-11 17:34:00 +0200560 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
561 * \retval #PSA_ERROR_HARDWARE_FAILURE
562 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300563 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300564 * The library has not been previously initialized by psa_crypto_init().
565 * It is implementation-dependent whether a failure to initialize
566 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100567 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100568psa_status_t psa_export_public_key(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100569 uint8_t *data,
570 size_t data_size,
571 size_t *data_length);
572
573/**@}*/
574
575/** \defgroup policy Key policies
576 * @{
577 */
578
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100579/** The type of the key policy data structure.
580 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000581 * Before calling any function on a key policy, the application must initialize
582 * it by any of the following means:
583 * - Set the structure to all-bits-zero, for example:
584 * \code
585 * psa_key_policy_t policy;
586 * memset(&policy, 0, sizeof(policy));
587 * \endcode
588 * - Initialize the structure to logical zero values, for example:
589 * \code
590 * psa_key_policy_t policy = {0};
591 * \endcode
592 * - Initialize the structure to the initializer #PSA_KEY_POLICY_INIT,
593 * for example:
594 * \code
595 * psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
596 * \endcode
597 * - Assign the result of the function psa_key_policy_init()
598 * to the structure, for example:
599 * \code
600 * psa_key_policy_t policy;
601 * policy = psa_key_policy_init();
602 * \endcode
603 *
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100604 * This is an implementation-defined \c struct. Applications should not
605 * make any assumptions about the content of this structure except
606 * as directed by the documentation of a specific implementation. */
607typedef struct psa_key_policy_s psa_key_policy_t;
608
Jaeden Amero70261c52019-01-04 11:47:20 +0000609/** \def PSA_KEY_POLICY_INIT
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200610 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000611 * This macro returns a suitable initializer for a key policy object of type
612 * #psa_key_policy_t.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200613 */
Jaeden Amero70261c52019-01-04 11:47:20 +0000614#ifdef __DOXYGEN_ONLY__
615/* This is an example definition for documentation purposes.
616 * Implementations should define a suitable value in `crypto_struct.h`.
617 */
618#define PSA_KEY_POLICY_INIT {0}
619#endif
620
621/** Return an initial value for a key policy that forbids all usage of the key.
622 */
623static psa_key_policy_t psa_key_policy_init(void);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100624
Gilles Peskine7e198532018-03-08 07:50:30 +0100625/** \brief Set the standard fields of a policy structure.
626 *
627 * Note that this function does not make any consistency check of the
628 * parameters. The values are only checked when applying the policy to
629 * a key slot with psa_set_key_policy().
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200630 *
Jaeden Amero70261c52019-01-04 11:47:20 +0000631 * \param[in,out] policy The key policy to modify. It must have been
632 * initialized as per the documentation for
633 * #psa_key_policy_t.
634 * \param usage The permitted uses for the key.
635 * \param alg The algorithm that the key may be used for.
Gilles Peskine7e198532018-03-08 07:50:30 +0100636 */
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100637void psa_key_policy_set_usage(psa_key_policy_t *policy,
638 psa_key_usage_t usage,
639 psa_algorithm_t alg);
640
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200641/** \brief Retrieve the usage field of a policy structure.
642 *
643 * \param[in] policy The policy object to query.
644 *
645 * \return The permitted uses for a key with this policy.
646 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +0200647psa_key_usage_t psa_key_policy_get_usage(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100648
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200649/** \brief Retrieve the algorithm field of a policy structure.
650 *
651 * \param[in] policy The policy object to query.
652 *
653 * \return The permitted algorithm for a key with this policy.
654 */
Gilles Peskineaa7bc472018-07-12 00:54:56 +0200655psa_algorithm_t psa_key_policy_get_algorithm(const psa_key_policy_t *policy);
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100656
657/** \brief Set the usage policy on a key slot.
658 *
659 * This function must be called on an empty key slot, before importing,
660 * generating or creating a key in the slot. Changing the policy of an
661 * existing key is not permitted.
Gilles Peskine7e198532018-03-08 07:50:30 +0100662 *
663 * Implementations may set restrictions on supported key policies
664 * depending on the key type and the key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200665 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100666 * \param handle Handle to the key whose policy is to be changed.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200667 * \param[in] policy The policy object to query.
668 *
669 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +0100670 * Success.
671 * If the key is persistent, it is implementation-defined whether
672 * the policy has been saved to persistent storage. Implementations
673 * may defer saving the policy until the key material is created.
Gilles Peskineae32aac2018-11-30 14:39:32 +0100674 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200675 * \retval #PSA_ERROR_OCCUPIED_SLOT
676 * \retval #PSA_ERROR_NOT_SUPPORTED
677 * \retval #PSA_ERROR_INVALID_ARGUMENT
678 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
679 * \retval #PSA_ERROR_HARDWARE_FAILURE
680 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300681 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300682 * The library has not been previously initialized by psa_crypto_init().
683 * It is implementation-dependent whether a failure to initialize
684 * results in this error code.
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100685 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100686psa_status_t psa_set_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100687 const psa_key_policy_t *policy);
688
Gilles Peskine7e198532018-03-08 07:50:30 +0100689/** \brief Get the usage policy for a key slot.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200690 *
Gilles Peskineae32aac2018-11-30 14:39:32 +0100691 * \param handle Handle to the key slot whose policy is being queried.
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200692 * \param[out] policy On success, the key's policy.
693 *
694 * \retval #PSA_SUCCESS
Gilles Peskineae32aac2018-11-30 14:39:32 +0100695 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine6ac73a92018-07-12 19:47:19 +0200696 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
697 * \retval #PSA_ERROR_HARDWARE_FAILURE
698 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +0300699 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +0300700 * The library has not been previously initialized by psa_crypto_init().
701 * It is implementation-dependent whether a failure to initialize
702 * results in this error code.
Gilles Peskine7e198532018-03-08 07:50:30 +0100703 */
Gilles Peskineae32aac2018-11-30 14:39:32 +0100704psa_status_t psa_get_key_policy(psa_key_handle_t handle,
Gilles Peskine7698bcf2018-03-03 21:30:44 +0100705 psa_key_policy_t *policy);
Gilles Peskine20035e32018-02-03 22:44:14 +0100706
707/**@}*/
708
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100709/** \defgroup hash Message digests
710 * @{
711 */
712
Gilles Peskine69647a42019-01-14 20:18:12 +0100713/** Calculate the hash (digest) of a message.
714 *
715 * \note To verify the hash of a message against an
716 * expected value, use psa_hash_compare() instead.
717 *
718 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
719 * such that #PSA_ALG_IS_HASH(\p alg) is true).
720 * \param[in] input Buffer containing the message to hash.
721 * \param input_length Size of the \p input buffer in bytes.
722 * \param[out] hash Buffer where the hash is to be written.
723 * \param hash_size Size of the \p hash buffer in bytes.
724 * \param[out] hash_length On success, the number of bytes
725 * that make up the hash value. This is always
726 * #PSA_HASH_SIZE(\c alg) where \c alg is the
727 * hash algorithm that is calculated.
728 *
729 * \retval #PSA_SUCCESS
730 * Success.
731 * \retval #PSA_ERROR_NOT_SUPPORTED
732 * \p alg is not supported or is not a hash algorithm.
733 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
734 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
735 * \retval #PSA_ERROR_HARDWARE_FAILURE
736 * \retval #PSA_ERROR_TAMPERING_DETECTED
737 */
738psa_status_t psa_hash_compute(psa_algorithm_t alg,
739 const uint8_t *input,
740 size_t input_length,
741 uint8_t *hash,
742 size_t hash_size,
743 size_t *hash_length);
744
745/** Calculate the hash (digest) of a message and compare it with a
746 * reference value.
747 *
748 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
749 * such that #PSA_ALG_IS_HASH(\p alg) is true).
750 * \param[in] input Buffer containing the message to hash.
751 * \param input_length Size of the \p input buffer in bytes.
752 * \param[out] hash Buffer containing the expected hash value.
Gilles Peskinea05602d2019-01-17 15:25:52 +0100753 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine69647a42019-01-14 20:18:12 +0100754 *
755 * \retval #PSA_SUCCESS
756 * The expected hash is identical to the actual hash of the input.
757 * \retval #PSA_ERROR_INVALID_SIGNATURE
758 * The hash of the message was calculated successfully, but it
759 * differs from the expected hash.
760 * \retval #PSA_ERROR_NOT_SUPPORTED
761 * \p alg is not supported or is not a hash algorithm.
762 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
763 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
764 * \retval #PSA_ERROR_HARDWARE_FAILURE
765 * \retval #PSA_ERROR_TAMPERING_DETECTED
766 */
767psa_status_t psa_hash_compare(psa_algorithm_t alg,
768 const uint8_t *input,
769 size_t input_length,
770 const uint8_t *hash,
771 const size_t hash_length);
772
Gilles Peskine308b91d2018-02-08 09:47:44 +0100773/** The type of the state data structure for multipart hash operations.
774 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000775 * Before calling any function on a hash operation object, the application must
776 * initialize it by any of the following means:
777 * - Set the structure to all-bits-zero, for example:
778 * \code
779 * psa_hash_operation_t operation;
780 * memset(&operation, 0, sizeof(operation));
781 * \endcode
782 * - Initialize the structure to logical zero values, for example:
783 * \code
784 * psa_hash_operation_t operation = {0};
785 * \endcode
786 * - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
787 * for example:
788 * \code
789 * psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
790 * \endcode
791 * - Assign the result of the function psa_hash_operation_init()
792 * to the structure, for example:
793 * \code
794 * psa_hash_operation_t operation;
795 * operation = psa_hash_operation_init();
796 * \endcode
797 *
Gilles Peskine92b30732018-03-03 21:29:30 +0100798 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine308b91d2018-02-08 09:47:44 +0100799 * make any assumptions about the content of this structure except
800 * as directed by the documentation of a specific implementation. */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100801typedef struct psa_hash_operation_s psa_hash_operation_t;
802
Jaeden Amero6a25b412019-01-04 11:47:44 +0000803/** \def PSA_HASH_OPERATION_INIT
804 *
805 * This macro returns a suitable initializer for a hash operation object
806 * of type #psa_hash_operation_t.
807 */
808#ifdef __DOXYGEN_ONLY__
809/* This is an example definition for documentation purposes.
810 * Implementations should define a suitable value in `crypto_struct.h`.
811 */
812#define PSA_HASH_OPERATION_INIT {0}
813#endif
814
815/** Return an initial value for a hash operation object.
816 */
817static psa_hash_operation_t psa_hash_operation_init(void);
818
Gilles Peskinef45adda2019-01-14 18:29:18 +0100819/** Set up a multipart hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100820 *
821 * The sequence of operations to calculate a hash (message digest)
822 * is as follows:
823 * -# Allocate an operation object which will be passed to all the functions
824 * listed here.
Jaeden Amero6a25b412019-01-04 11:47:44 +0000825 * -# Initialize the operation object with one of the methods described in the
826 * documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200827 * -# Call psa_hash_setup() to specify the algorithm.
Gilles Peskine7e4acc52018-02-16 21:24:11 +0100828 * -# Call psa_hash_update() zero, one or more times, passing a fragment
Gilles Peskine308b91d2018-02-08 09:47:44 +0100829 * of the message each time. The hash that is calculated is the hash
830 * of the concatenation of these messages in order.
831 * -# To calculate the hash, call psa_hash_finish().
832 * To compare the hash with an expected value, call psa_hash_verify().
833 *
834 * The application may call psa_hash_abort() at any time after the operation
Jaeden Amero6a25b412019-01-04 11:47:44 +0000835 * has been initialized.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100836 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200837 * After a successful call to psa_hash_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +0100838 * eventually terminate the operation. The following events terminate an
839 * operation:
Gilles Peskine308b91d2018-02-08 09:47:44 +0100840 * - A failed call to psa_hash_update().
Gilles Peskine19067982018-03-20 17:54:53 +0100841 * - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
Gilles Peskine308b91d2018-02-08 09:47:44 +0100842 *
Jaeden Amero6a25b412019-01-04 11:47:44 +0000843 * \param[in,out] operation The operation object to set up. It must have
844 * been initialized as per the documentation for
845 * #psa_hash_operation_t and not yet in use.
Gilles Peskineedd11a12018-07-12 01:08:58 +0200846 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
847 * such that #PSA_ALG_IS_HASH(\p alg) is true).
Gilles Peskine308b91d2018-02-08 09:47:44 +0100848 *
Gilles Peskine28538492018-07-11 17:34:00 +0200849 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100850 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200851 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200852 * \p alg is not supported or is not a hash algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +0200853 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
854 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
855 * \retval #PSA_ERROR_HARDWARE_FAILURE
856 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100857 */
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200858psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100859 psa_algorithm_t alg);
860
Gilles Peskine308b91d2018-02-08 09:47:44 +0100861/** Add a message fragment to a multipart hash operation.
862 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200863 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100864 *
865 * If this function returns an error status, the operation becomes inactive.
866 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200867 * \param[in,out] operation Active hash operation.
868 * \param[in] input Buffer containing the message fragment to hash.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200869 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100870 *
Gilles Peskine28538492018-07-11 17:34:00 +0200871 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100872 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200873 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100874 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200875 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
876 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
877 * \retval #PSA_ERROR_HARDWARE_FAILURE
878 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100879 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100880psa_status_t psa_hash_update(psa_hash_operation_t *operation,
881 const uint8_t *input,
882 size_t input_length);
883
Gilles Peskine308b91d2018-02-08 09:47:44 +0100884/** Finish the calculation of the hash of a message.
885 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200886 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100887 * This function calculates the hash of the message formed by concatenating
888 * the inputs passed to preceding calls to psa_hash_update().
889 *
890 * When this function returns, the operation becomes inactive.
891 *
892 * \warning Applications should not call this function if they expect
893 * a specific value for the hash. Call psa_hash_verify() instead.
894 * Beware that comparing integrity or authenticity data such as
895 * hash values with a function such as \c memcmp is risky
896 * because the time taken by the comparison may leak information
897 * about the hashed data which could allow an attacker to guess
898 * a valid hash and thereby bypass security controls.
899 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200900 * \param[in,out] operation Active hash operation.
901 * \param[out] hash Buffer where the hash is to be written.
902 * \param hash_size Size of the \p hash buffer in bytes.
903 * \param[out] hash_length On success, the number of bytes
904 * that make up the hash value. This is always
Gilles Peskinebe42f312018-07-13 14:38:15 +0200905 * #PSA_HASH_SIZE(\c alg) where \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +0200906 * hash algorithm that is calculated.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100907 *
Gilles Peskine28538492018-07-11 17:34:00 +0200908 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100909 * Success.
Gilles Peskine28538492018-07-11 17:34:00 +0200910 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100911 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200912 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200913 * The size of the \p hash buffer is too small. You can determine a
Gilles Peskine7256e6c2018-07-12 00:34:26 +0200914 * sufficient buffer size by calling #PSA_HASH_SIZE(\c alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +0100915 * where \c alg is the hash algorithm that is calculated.
Gilles Peskine28538492018-07-11 17:34:00 +0200916 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
917 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
918 * \retval #PSA_ERROR_HARDWARE_FAILURE
919 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100920 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100921psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
922 uint8_t *hash,
923 size_t hash_size,
924 size_t *hash_length);
925
Gilles Peskine308b91d2018-02-08 09:47:44 +0100926/** Finish the calculation of the hash of a message and compare it with
927 * an expected value.
928 *
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200929 * The application must call psa_hash_setup() before calling this function.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100930 * This function calculates the hash of the message formed by concatenating
931 * the inputs passed to preceding calls to psa_hash_update(). It then
932 * compares the calculated hash with the expected hash passed as a
933 * parameter to this function.
934 *
935 * When this function returns, the operation becomes inactive.
936 *
Gilles Peskine19067982018-03-20 17:54:53 +0100937 * \note Implementations shall make the best effort to ensure that the
Gilles Peskine308b91d2018-02-08 09:47:44 +0100938 * comparison between the actual hash and the expected hash is performed
939 * in constant time.
940 *
Gilles Peskineedd11a12018-07-12 01:08:58 +0200941 * \param[in,out] operation Active hash operation.
942 * \param[in] hash Buffer containing the expected hash value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200943 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100944 *
Gilles Peskine28538492018-07-11 17:34:00 +0200945 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +0100946 * The expected hash is identical to the actual hash of the message.
Gilles Peskine28538492018-07-11 17:34:00 +0200947 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +0100948 * The hash of the message was calculated successfully, but it
949 * differs from the expected hash.
Gilles Peskine28538492018-07-11 17:34:00 +0200950 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +0100951 * The operation state is not valid (not set up, or already completed).
Gilles Peskine28538492018-07-11 17:34:00 +0200952 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
953 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
954 * \retval #PSA_ERROR_HARDWARE_FAILURE
955 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100956 */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100957psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
958 const uint8_t *hash,
959 size_t hash_length);
960
Gilles Peskine308b91d2018-02-08 09:47:44 +0100961/** Abort a hash operation.
962 *
Gilles Peskine308b91d2018-02-08 09:47:44 +0100963 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200964 * \p operation structure itself. Once aborted, the operation object
965 * can be reused for another operation by calling
966 * psa_hash_setup() again.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100967 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200968 * You may call this function any time after the operation object has
969 * been initialized by any of the following methods:
970 * - A call to psa_hash_setup(), whether it succeeds or not.
971 * - Initializing the \c struct to all-bits-zero.
972 * - Initializing the \c struct to logical zeros, e.g.
973 * `psa_hash_operation_t operation = {0}`.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100974 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +0200975 * In particular, calling psa_hash_abort() after the operation has been
976 * terminated by a call to psa_hash_abort(), psa_hash_finish() or
977 * psa_hash_verify() is safe and has no effect.
978 *
979 * \param[in,out] operation Initialized hash operation.
Gilles Peskine308b91d2018-02-08 09:47:44 +0100980 *
Gilles Peskine28538492018-07-11 17:34:00 +0200981 * \retval #PSA_SUCCESS
982 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +0200983 * \p operation is not an active hash operation.
Gilles Peskine28538492018-07-11 17:34:00 +0200984 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
985 * \retval #PSA_ERROR_HARDWARE_FAILURE
986 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskine308b91d2018-02-08 09:47:44 +0100987 */
988psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100989
990/**@}*/
991
Gilles Peskine8c9def32018-02-08 10:02:12 +0100992/** \defgroup MAC Message authentication codes
993 * @{
994 */
995
Gilles Peskine69647a42019-01-14 20:18:12 +0100996/** Calculate the MAC (message authentication code) of a message.
997 *
998 * \note To verify the MAC of a message against an
999 * expected value, use psa_mac_verify() instead.
1000 * Beware that comparing integrity or authenticity data such as
1001 * MAC values with a function such as \c memcmp is risky
1002 * because the time taken by the comparison may leak information
1003 * about the MAC value which could allow an attacker to guess
1004 * a valid MAC and thereby bypass security controls.
1005 *
1006 * \param handle Handle to the key to use for the operation.
1007 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1008 * such that #PSA_ALG_IS_MAC(alg) is true).
1009 * \param[in] input Buffer containing the input message.
1010 * \param input_length Size of the \p input buffer in bytes.
1011 * \param[out] mac Buffer where the MAC value is to be written.
1012 * \param mac_size Size of the \p mac buffer in bytes.
1013 * \param[out] mac_length On success, the number of bytes
1014 * that make up the mac value. This is always
1015 * #PSA_HASH_SIZE(\c alg) where \c alg is the
1016 * hash algorithm that is calculated.
1017 *
1018 * \retval #PSA_SUCCESS
1019 * Success.
1020 * \retval #PSA_ERROR_INVALID_HANDLE
1021 * \retval #PSA_ERROR_EMPTY_SLOT
1022 * \retval #PSA_ERROR_NOT_PERMITTED
1023 * \retval #PSA_ERROR_INVALID_ARGUMENT
1024 * \p key is not compatible with \p alg.
1025 * \retval #PSA_ERROR_NOT_SUPPORTED
1026 * \p alg is not supported or is not a MAC algorithm.
1027 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1028 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1029 * \retval #PSA_ERROR_HARDWARE_FAILURE
1030 * \retval #PSA_ERROR_TAMPERING_DETECTED
1031 * \retval #PSA_ERROR_BAD_STATE
1032 * The library has not been previously initialized by psa_crypto_init().
1033 * It is implementation-dependent whether a failure to initialize
1034 * results in this error code.
1035 */
1036psa_status_t psa_mac_compute(psa_key_handle_t handle,
1037 psa_algorithm_t alg,
1038 const uint8_t *input,
1039 size_t input_length,
1040 uint8_t *mac,
1041 size_t mac_size,
1042 size_t *mac_length);
1043
1044/** Calculate the MAC of a message and compare it with a reference value.
1045 *
1046 * \param handle Handle to the key to use for the operation.
1047 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1048 * such that #PSA_ALG_IS_MAC(alg) is true).
1049 * \param[in] input Buffer containing the input message.
1050 * \param input_length Size of the \p input buffer in bytes.
1051 * \param[out] mac Buffer containing the expected MAC value.
1052 * \param mac_length Size of the \p mac buffer in bytes.
1053 *
1054 * \retval #PSA_SUCCESS
1055 * The expected MAC is identical to the actual MAC of the input.
1056 * \retval #PSA_ERROR_INVALID_SIGNATURE
1057 * The MAC of the message was calculated successfully, but it
1058 * differs from the expected value.
1059 * \retval #PSA_ERROR_INVALID_HANDLE
1060 * \retval #PSA_ERROR_EMPTY_SLOT
1061 * \retval #PSA_ERROR_NOT_PERMITTED
1062 * \retval #PSA_ERROR_INVALID_ARGUMENT
1063 * \p key is not compatible with \p alg.
1064 * \retval #PSA_ERROR_NOT_SUPPORTED
1065 * \p alg is not supported or is not a MAC algorithm.
1066 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1067 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1068 * \retval #PSA_ERROR_HARDWARE_FAILURE
1069 * \retval #PSA_ERROR_TAMPERING_DETECTED
1070 */
Gilles Peskinea05602d2019-01-17 15:25:52 +01001071psa_status_t psa_mac_verify(psa_key_handle_t handle,
1072 psa_algorithm_t alg,
Gilles Peskine69647a42019-01-14 20:18:12 +01001073 const uint8_t *input,
1074 size_t input_length,
1075 const uint8_t *mac,
1076 const size_t mac_length);
1077
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001078/** The type of the state data structure for multipart MAC operations.
1079 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001080 * Before calling any function on a MAC operation object, the application must
1081 * initialize it by any of the following means:
1082 * - Set the structure to all-bits-zero, for example:
1083 * \code
1084 * psa_mac_operation_t operation;
1085 * memset(&operation, 0, sizeof(operation));
1086 * \endcode
1087 * - Initialize the structure to logical zero values, for example:
1088 * \code
1089 * psa_mac_operation_t operation = {0};
1090 * \endcode
1091 * - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
1092 * for example:
1093 * \code
1094 * psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1095 * \endcode
1096 * - Assign the result of the function psa_mac_operation_init()
1097 * to the structure, for example:
1098 * \code
1099 * psa_mac_operation_t operation;
1100 * operation = psa_mac_operation_init();
1101 * \endcode
1102 *
Gilles Peskine92b30732018-03-03 21:29:30 +01001103 * This is an implementation-defined \c struct. Applications should not
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001104 * make any assumptions about the content of this structure except
1105 * as directed by the documentation of a specific implementation. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001106typedef struct psa_mac_operation_s psa_mac_operation_t;
1107
Jaeden Amero769ce272019-01-04 11:48:03 +00001108/** \def PSA_MAC_OPERATION_INIT
1109 *
1110 * This macro returns a suitable initializer for a MAC operation object of type
1111 * #psa_mac_operation_t.
1112 */
1113#ifdef __DOXYGEN_ONLY__
1114/* This is an example definition for documentation purposes.
1115 * Implementations should define a suitable value in `crypto_struct.h`.
1116 */
1117#define PSA_MAC_OPERATION_INIT {0}
1118#endif
1119
1120/** Return an initial value for a MAC operation object.
1121 */
1122static psa_mac_operation_t psa_mac_operation_init(void);
1123
Gilles Peskinef45adda2019-01-14 18:29:18 +01001124/** Set up a multipart MAC calculation operation.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001125 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001126 * This function sets up the calculation of the MAC
1127 * (message authentication code) of a byte string.
1128 * To verify the MAC of a message against an
1129 * expected value, use psa_mac_verify_setup() instead.
1130 *
1131 * The sequence of operations to calculate a MAC is as follows:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001132 * -# Allocate an operation object which will be passed to all the functions
1133 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001134 * -# Initialize the operation object with one of the methods described in the
1135 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001136 * -# Call psa_mac_sign_setup() to specify the algorithm and key.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001137 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1138 * of the message each time. The MAC that is calculated is the MAC
1139 * of the concatenation of these messages in order.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001140 * -# At the end of the message, call psa_mac_sign_finish() to finish
1141 * calculating the MAC value and retrieve it.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001142 *
1143 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001144 * has been initialized.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001145 *
Gilles Peskine89167cb2018-07-08 20:12:23 +02001146 * After a successful call to psa_mac_sign_setup(), the application must
1147 * eventually terminate the operation through one of the following methods:
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001148 * - A failed call to psa_mac_update().
Gilles Peskine89167cb2018-07-08 20:12:23 +02001149 * - A call to psa_mac_sign_finish() or psa_mac_abort().
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001150 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001151 * \param[in,out] operation The operation object to set up. It must have
1152 * been initialized as per the documentation for
1153 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001154 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001155 * It must remain valid until the operation
1156 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001157 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1158 * such that #PSA_ALG_IS_MAC(alg) is true).
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001159 *
Gilles Peskine28538492018-07-11 17:34:00 +02001160 * \retval #PSA_SUCCESS
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001161 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001162 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001163 * \retval #PSA_ERROR_EMPTY_SLOT
1164 * \retval #PSA_ERROR_NOT_PERMITTED
1165 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001166 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001167 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001168 * \p alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001169 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1170 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1171 * \retval #PSA_ERROR_HARDWARE_FAILURE
1172 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001173 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001174 * The library has not been previously initialized by psa_crypto_init().
1175 * It is implementation-dependent whether a failure to initialize
1176 * results in this error code.
Gilles Peskine7e4acc52018-02-16 21:24:11 +01001177 */
Gilles Peskine89167cb2018-07-08 20:12:23 +02001178psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001179 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001180 psa_algorithm_t alg);
1181
Gilles Peskinef45adda2019-01-14 18:29:18 +01001182/** Set up a multipart MAC verification operation.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001183 *
1184 * This function sets up the verification of the MAC
1185 * (message authentication code) of a byte string against an expected value.
1186 *
1187 * The sequence of operations to verify a MAC is as follows:
1188 * -# Allocate an operation object which will be passed to all the functions
1189 * listed here.
Jaeden Amero769ce272019-01-04 11:48:03 +00001190 * -# Initialize the operation object with one of the methods described in the
1191 * documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001192 * -# Call psa_mac_verify_setup() to specify the algorithm and key.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001193 * -# Call psa_mac_update() zero, one or more times, passing a fragment
1194 * of the message each time. The MAC that is calculated is the MAC
1195 * of the concatenation of these messages in order.
1196 * -# At the end of the message, call psa_mac_verify_finish() to finish
1197 * calculating the actual MAC of the message and verify it against
1198 * the expected value.
1199 *
1200 * The application may call psa_mac_abort() at any time after the operation
Jaeden Amero769ce272019-01-04 11:48:03 +00001201 * has been initialized.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001202 *
1203 * After a successful call to psa_mac_verify_setup(), the application must
1204 * eventually terminate the operation through one of the following methods:
1205 * - A failed call to psa_mac_update().
1206 * - A call to psa_mac_verify_finish() or psa_mac_abort().
1207 *
Jaeden Amero769ce272019-01-04 11:48:03 +00001208 * \param[in,out] operation The operation object to set up. It must have
1209 * been initialized as per the documentation for
1210 * #psa_mac_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001211 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001212 * It must remain valid until the operation
1213 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001214 * \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
1215 * such that #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine89167cb2018-07-08 20:12:23 +02001216 *
Gilles Peskine28538492018-07-11 17:34:00 +02001217 * \retval #PSA_SUCCESS
Gilles Peskine89167cb2018-07-08 20:12:23 +02001218 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001219 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001220 * \retval #PSA_ERROR_EMPTY_SLOT
1221 * \retval #PSA_ERROR_NOT_PERMITTED
1222 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskine89167cb2018-07-08 20:12:23 +02001223 * \c key is not compatible with \c alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001224 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskine89167cb2018-07-08 20:12:23 +02001225 * \c alg is not supported or is not a MAC algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001226 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1227 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1228 * \retval #PSA_ERROR_HARDWARE_FAILURE
1229 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001230 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001231 * The library has not been previously initialized by psa_crypto_init().
1232 * It is implementation-dependent whether a failure to initialize
1233 * results in this error code.
Gilles Peskine89167cb2018-07-08 20:12:23 +02001234 */
1235psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001236 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001237 psa_algorithm_t alg);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001238
Gilles Peskinedcd14942018-07-12 00:30:52 +02001239/** Add a message fragment to a multipart MAC operation.
1240 *
1241 * The application must call psa_mac_sign_setup() or psa_mac_verify_setup()
1242 * before calling this function.
1243 *
1244 * If this function returns an error status, the operation becomes inactive.
1245 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001246 * \param[in,out] operation Active MAC operation.
1247 * \param[in] input Buffer containing the message fragment to add to
1248 * the MAC calculation.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001249 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001250 *
1251 * \retval #PSA_SUCCESS
1252 * Success.
1253 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001254 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001255 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1256 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1257 * \retval #PSA_ERROR_HARDWARE_FAILURE
1258 * \retval #PSA_ERROR_TAMPERING_DETECTED
1259 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001260psa_status_t psa_mac_update(psa_mac_operation_t *operation,
1261 const uint8_t *input,
1262 size_t input_length);
1263
Gilles Peskinedcd14942018-07-12 00:30:52 +02001264/** Finish the calculation of the MAC of a message.
1265 *
1266 * The application must call psa_mac_sign_setup() before calling this function.
1267 * This function calculates the MAC of the message formed by concatenating
1268 * the inputs passed to preceding calls to psa_mac_update().
1269 *
1270 * When this function returns, the operation becomes inactive.
1271 *
1272 * \warning Applications should not call this function if they expect
1273 * a specific value for the MAC. Call psa_mac_verify_finish() instead.
1274 * Beware that comparing integrity or authenticity data such as
1275 * MAC values with a function such as \c memcmp is risky
1276 * because the time taken by the comparison may leak information
1277 * about the MAC value which could allow an attacker to guess
1278 * a valid MAC and thereby bypass security controls.
1279 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001280 * \param[in,out] operation Active MAC operation.
1281 * \param[out] mac Buffer where the MAC value is to be written.
1282 * \param mac_size Size of the \p mac buffer in bytes.
1283 * \param[out] mac_length On success, the number of bytes
1284 * that make up the MAC value. This is always
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001285 * #PSA_MAC_FINAL_SIZE(\c key_type, \c key_bits, \c alg)
Gilles Peskineedd11a12018-07-12 01:08:58 +02001286 * where \c key_type and \c key_bits are the type and
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001287 * bit-size respectively of the key and \c alg is the
Gilles Peskineedd11a12018-07-12 01:08:58 +02001288 * MAC algorithm that is calculated.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001289 *
1290 * \retval #PSA_SUCCESS
1291 * Success.
1292 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001293 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001294 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001295 * The size of the \p mac buffer is too small. You can determine a
Gilles Peskinedcd14942018-07-12 00:30:52 +02001296 * sufficient buffer size by calling PSA_MAC_FINAL_SIZE().
1297 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1298 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1299 * \retval #PSA_ERROR_HARDWARE_FAILURE
1300 * \retval #PSA_ERROR_TAMPERING_DETECTED
1301 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001302psa_status_t psa_mac_sign_finish(psa_mac_operation_t *operation,
1303 uint8_t *mac,
1304 size_t mac_size,
1305 size_t *mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001306
Gilles Peskinedcd14942018-07-12 00:30:52 +02001307/** Finish the calculation of the MAC of a message and compare it with
1308 * an expected value.
1309 *
1310 * The application must call psa_mac_verify_setup() before calling this function.
1311 * This function calculates the MAC of the message formed by concatenating
1312 * the inputs passed to preceding calls to psa_mac_update(). It then
1313 * compares the calculated MAC with the expected MAC passed as a
1314 * parameter to this function.
1315 *
1316 * When this function returns, the operation becomes inactive.
1317 *
1318 * \note Implementations shall make the best effort to ensure that the
1319 * comparison between the actual MAC and the expected MAC is performed
1320 * in constant time.
1321 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001322 * \param[in,out] operation Active MAC operation.
1323 * \param[in] mac Buffer containing the expected MAC value.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001324 * \param mac_length Size of the \p mac buffer in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001325 *
1326 * \retval #PSA_SUCCESS
1327 * The expected MAC is identical to the actual MAC of the message.
1328 * \retval #PSA_ERROR_INVALID_SIGNATURE
1329 * The MAC of the message was calculated successfully, but it
1330 * differs from the expected MAC.
1331 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001332 * The operation state is not valid (not set up, or already completed).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001333 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1334 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1335 * \retval #PSA_ERROR_HARDWARE_FAILURE
1336 * \retval #PSA_ERROR_TAMPERING_DETECTED
1337 */
Gilles Peskineacd4be32018-07-08 19:56:25 +02001338psa_status_t psa_mac_verify_finish(psa_mac_operation_t *operation,
1339 const uint8_t *mac,
1340 size_t mac_length);
Gilles Peskine8c9def32018-02-08 10:02:12 +01001341
Gilles Peskinedcd14942018-07-12 00:30:52 +02001342/** Abort a MAC operation.
1343 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001344 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001345 * \p operation structure itself. Once aborted, the operation object
1346 * can be reused for another operation by calling
1347 * psa_mac_sign_setup() or psa_mac_verify_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001348 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001349 * You may call this function any time after the operation object has
1350 * been initialized by any of the following methods:
1351 * - A call to psa_mac_sign_setup() or psa_mac_verify_setup(), whether
1352 * it succeeds or not.
1353 * - Initializing the \c struct to all-bits-zero.
1354 * - Initializing the \c struct to logical zeros, e.g.
1355 * `psa_mac_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001356 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001357 * In particular, calling psa_mac_abort() after the operation has been
1358 * terminated by a call to psa_mac_abort(), psa_mac_sign_finish() or
1359 * psa_mac_verify_finish() is safe and has no effect.
1360 *
1361 * \param[in,out] operation Initialized MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001362 *
1363 * \retval #PSA_SUCCESS
1364 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001365 * \p operation is not an active MAC operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001366 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1367 * \retval #PSA_ERROR_HARDWARE_FAILURE
1368 * \retval #PSA_ERROR_TAMPERING_DETECTED
1369 */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001370psa_status_t psa_mac_abort(psa_mac_operation_t *operation);
1371
1372/**@}*/
1373
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001374/** \defgroup cipher Symmetric ciphers
1375 * @{
1376 */
1377
Gilles Peskine69647a42019-01-14 20:18:12 +01001378/** Encrypt a message using a symmetric cipher.
1379 *
1380 * This function encrypts a message with a random IV (initialization
1381 * vector).
1382 *
1383 * \param handle Handle to the key to use for the operation.
1384 * It must remain valid until the operation
1385 * terminates.
1386 * \param alg The cipher algorithm to compute
1387 * (\c PSA_ALG_XXX value such that
1388 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1389 * \param[in] input Buffer containing the message to encrypt.
1390 * \param input_length Size of the \p input buffer in bytes.
1391 * \param[out] output Buffer where the output is to be written.
1392 * The output contains the IV followed by
1393 * the ciphertext proper.
1394 * \param output_size Size of the \p output buffer in bytes.
1395 * \param[out] output_length On success, the number of bytes
1396 * that make up the output.
1397 *
1398 * \retval #PSA_SUCCESS
1399 * Success.
1400 * \retval #PSA_ERROR_INVALID_HANDLE
1401 * \retval #PSA_ERROR_EMPTY_SLOT
1402 * \retval #PSA_ERROR_NOT_PERMITTED
1403 * \retval #PSA_ERROR_INVALID_ARGUMENT
1404 * \p key is not compatible with \p alg.
1405 * \retval #PSA_ERROR_NOT_SUPPORTED
1406 * \p alg is not supported or is not a cipher algorithm.
1407 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1408 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1409 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1410 * \retval #PSA_ERROR_HARDWARE_FAILURE
1411 * \retval #PSA_ERROR_TAMPERING_DETECTED
1412 */
1413psa_status_t psa_cipher_encrypt(psa_key_handle_t handle,
1414 psa_algorithm_t alg,
1415 const uint8_t *input,
1416 size_t input_length,
1417 uint8_t *output,
1418 size_t output_size,
1419 size_t *output_length);
1420
1421/** Decrypt a message using a symmetric cipher.
1422 *
1423 * This function decrypts a message encrypted with a symmetric cipher.
1424 *
1425 * \param handle Handle to the key to use for the operation.
1426 * It must remain valid until the operation
1427 * terminates.
1428 * \param alg The cipher algorithm to compute
1429 * (\c PSA_ALG_XXX value such that
1430 * #PSA_ALG_IS_CIPHER(\p alg) is true).
1431 * \param[in] input Buffer containing the message to decrypt.
1432 * This consists of the IV followed by the
1433 * ciphertext proper.
1434 * \param input_length Size of the \p input buffer in bytes.
1435 * \param[out] output Buffer where the plaintext is to be written.
1436 * \param output_size Size of the \p output buffer in bytes.
1437 * \param[out] output_length On success, the number of bytes
1438 * that make up the output.
1439 *
1440 * \retval #PSA_SUCCESS
1441 * Success.
1442 * \retval #PSA_ERROR_INVALID_HANDLE
1443 * \retval #PSA_ERROR_EMPTY_SLOT
1444 * \retval #PSA_ERROR_NOT_PERMITTED
1445 * \retval #PSA_ERROR_INVALID_ARGUMENT
1446 * \p key is not compatible with \p alg.
1447 * \retval #PSA_ERROR_NOT_SUPPORTED
1448 * \p alg is not supported or is not a cipher algorithm.
1449 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1450 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1451 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1452 * \retval #PSA_ERROR_HARDWARE_FAILURE
1453 * \retval #PSA_ERROR_TAMPERING_DETECTED
1454 */
1455psa_status_t psa_cipher_decrypt(psa_key_handle_t handle,
1456 psa_algorithm_t alg,
1457 const uint8_t *input,
1458 size_t input_length,
1459 uint8_t *output,
1460 size_t output_size,
1461 size_t *output_length);
1462
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001463/** The type of the state data structure for multipart cipher operations.
1464 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001465 * Before calling any function on a cipher operation object, the application
1466 * must initialize it by any of the following means:
1467 * - Set the structure to all-bits-zero, for example:
1468 * \code
1469 * psa_cipher_operation_t operation;
1470 * memset(&operation, 0, sizeof(operation));
1471 * \endcode
1472 * - Initialize the structure to logical zero values, for example:
1473 * \code
1474 * psa_cipher_operation_t operation = {0};
1475 * \endcode
1476 * - Initialize the structure to the initializer #PSA_CIPHER_OPERATION_INIT,
1477 * for example:
1478 * \code
1479 * psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
1480 * \endcode
1481 * - Assign the result of the function psa_cipher_operation_init()
1482 * to the structure, for example:
1483 * \code
1484 * psa_cipher_operation_t operation;
1485 * operation = psa_cipher_operation_init();
1486 * \endcode
1487 *
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001488 * This is an implementation-defined \c struct. Applications should not
1489 * make any assumptions about the content of this structure except
1490 * as directed by the documentation of a specific implementation. */
1491typedef struct psa_cipher_operation_s psa_cipher_operation_t;
1492
Jaeden Amero5bae2272019-01-04 11:48:27 +00001493/** \def PSA_CIPHER_OPERATION_INIT
1494 *
1495 * This macro returns a suitable initializer for a cipher operation object of
1496 * type #psa_cipher_operation_t.
1497 */
1498#ifdef __DOXYGEN_ONLY__
1499/* This is an example definition for documentation purposes.
1500 * Implementations should define a suitable value in `crypto_struct.h`.
1501 */
1502#define PSA_CIPHER_OPERATION_INIT {0}
1503#endif
1504
1505/** Return an initial value for a cipher operation object.
1506 */
1507static psa_cipher_operation_t psa_cipher_operation_init(void);
1508
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001509/** Set the key for a multipart symmetric encryption operation.
1510 *
1511 * The sequence of operations to encrypt a message with a symmetric cipher
1512 * is as follows:
1513 * -# Allocate an operation object which will be passed to all the functions
1514 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001515 * -# Initialize the operation object with one of the methods described in the
1516 * documentation for #psa_cipher_operation_t, e.g.
1517 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001518 * -# Call psa_cipher_encrypt_setup() to specify the algorithm and key.
itayzafrired7382f2018-08-02 14:19:33 +03001519 * -# Call either psa_cipher_generate_iv() or psa_cipher_set_iv() to
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001520 * generate or set the IV (initialization vector). You should use
itayzafrired7382f2018-08-02 14:19:33 +03001521 * psa_cipher_generate_iv() unless the protocol you are implementing
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001522 * requires a specific IV value.
1523 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1524 * of the message each time.
1525 * -# Call psa_cipher_finish().
1526 *
1527 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001528 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001529 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001530 * After a successful call to psa_cipher_encrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001531 * eventually terminate the operation. The following events terminate an
1532 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001533 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001534 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001535 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001536 * \param[in,out] operation The operation object to set up. It must have
1537 * been initialized as per the documentation for
1538 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001539 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001540 * It must remain valid until the operation
1541 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001542 * \param alg The cipher algorithm to compute
1543 * (\c PSA_ALG_XXX value such that
1544 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001545 *
Gilles Peskine28538492018-07-11 17:34:00 +02001546 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001547 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001548 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001549 * \retval #PSA_ERROR_EMPTY_SLOT
1550 * \retval #PSA_ERROR_NOT_PERMITTED
1551 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001552 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001553 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001554 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001555 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1556 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1557 * \retval #PSA_ERROR_HARDWARE_FAILURE
1558 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001559 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001560 * The library has not been previously initialized by psa_crypto_init().
1561 * It is implementation-dependent whether a failure to initialize
1562 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001563 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001564psa_status_t psa_cipher_encrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001565 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001566 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001567
1568/** Set the key for a multipart symmetric decryption operation.
1569 *
1570 * The sequence of operations to decrypt a message with a symmetric cipher
1571 * is as follows:
1572 * -# Allocate an operation object which will be passed to all the functions
1573 * listed here.
Jaeden Amero5bae2272019-01-04 11:48:27 +00001574 * -# Initialize the operation object with one of the methods described in the
1575 * documentation for #psa_cipher_operation_t, e.g.
1576 * PSA_CIPHER_OPERATION_INIT.
Gilles Peskinefe119512018-07-08 21:39:34 +02001577 * -# Call psa_cipher_decrypt_setup() to specify the algorithm and key.
Gilles Peskinef45adda2019-01-14 18:29:18 +01001578 * -# Call psa_cipher_set_iv() with the IV (initialization vector) for the
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001579 * decryption. If the IV is prepended to the ciphertext, you can call
1580 * psa_cipher_update() on a buffer containing the IV followed by the
1581 * beginning of the message.
1582 * -# Call psa_cipher_update() zero, one or more times, passing a fragment
1583 * of the message each time.
1584 * -# Call psa_cipher_finish().
1585 *
1586 * The application may call psa_cipher_abort() at any time after the operation
Jaeden Amero5bae2272019-01-04 11:48:27 +00001587 * has been initialized.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001588 *
Gilles Peskinefe119512018-07-08 21:39:34 +02001589 * After a successful call to psa_cipher_decrypt_setup(), the application must
Gilles Peskineed522972018-03-20 17:54:15 +01001590 * eventually terminate the operation. The following events terminate an
1591 * operation:
Gilles Peskinef45adda2019-01-14 18:29:18 +01001592 * - A failed call to any of the \c psa_cipher_xxx functions.
Gilles Peskine19067982018-03-20 17:54:53 +01001593 * - A call to psa_cipher_finish() or psa_cipher_abort().
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001594 *
Jaeden Amero5bae2272019-01-04 11:48:27 +00001595 * \param[in,out] operation The operation object to set up. It must have
1596 * been initialized as per the documentation for
1597 * #psa_cipher_operation_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001598 * \param handle Handle to the key to use for the operation.
Gilles Peskine5f25dd02019-01-14 18:24:53 +01001599 * It must remain valid until the operation
1600 * terminates.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001601 * \param alg The cipher algorithm to compute
1602 * (\c PSA_ALG_XXX value such that
1603 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001604 *
Gilles Peskine28538492018-07-11 17:34:00 +02001605 * \retval #PSA_SUCCESS
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001606 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001607 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001608 * \retval #PSA_ERROR_EMPTY_SLOT
1609 * \retval #PSA_ERROR_NOT_PERMITTED
1610 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001611 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001612 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001613 * \p alg is not supported or is not a cipher algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001614 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1615 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1616 * \retval #PSA_ERROR_HARDWARE_FAILURE
1617 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001618 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001619 * The library has not been previously initialized by psa_crypto_init().
1620 * It is implementation-dependent whether a failure to initialize
1621 * results in this error code.
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001622 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001623psa_status_t psa_cipher_decrypt_setup(psa_cipher_operation_t *operation,
Gilles Peskineae32aac2018-11-30 14:39:32 +01001624 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02001625 psa_algorithm_t alg);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001626
Gilles Peskinedcd14942018-07-12 00:30:52 +02001627/** Generate an IV for a symmetric encryption operation.
1628 *
1629 * This function generates a random IV (initialization vector), nonce
1630 * or initial counter value for the encryption operation as appropriate
1631 * for the chosen algorithm, key type and key size.
1632 *
1633 * The application must call psa_cipher_encrypt_setup() before
1634 * calling this function.
1635 *
1636 * If this function returns an error status, the operation becomes inactive.
1637 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001638 * \param[in,out] operation Active cipher operation.
1639 * \param[out] iv Buffer where the generated IV is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001640 * \param iv_size Size of the \p iv buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001641 * \param[out] iv_length On success, the number of bytes of the
1642 * generated IV.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001643 *
1644 * \retval #PSA_SUCCESS
1645 * Success.
1646 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001647 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001648 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinedda3bd32018-07-12 19:40:46 +02001649 * The size of the \p iv buffer is too small.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001650 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1651 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1652 * \retval #PSA_ERROR_HARDWARE_FAILURE
1653 * \retval #PSA_ERROR_TAMPERING_DETECTED
1654 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001655psa_status_t psa_cipher_generate_iv(psa_cipher_operation_t *operation,
1656 unsigned char *iv,
1657 size_t iv_size,
1658 size_t *iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001659
Gilles Peskinedcd14942018-07-12 00:30:52 +02001660/** Set the IV for a symmetric encryption or decryption operation.
1661 *
Gilles Peskinef45adda2019-01-14 18:29:18 +01001662 * This function sets the IV (initialization vector), nonce
Gilles Peskinedcd14942018-07-12 00:30:52 +02001663 * or initial counter value for the encryption or decryption operation.
1664 *
1665 * The application must call psa_cipher_encrypt_setup() before
1666 * calling this function.
1667 *
1668 * If this function returns an error status, the operation becomes inactive.
1669 *
1670 * \note When encrypting, applications should use psa_cipher_generate_iv()
1671 * instead of this function, unless implementing a protocol that requires
1672 * a non-random IV.
1673 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001674 * \param[in,out] operation Active cipher operation.
1675 * \param[in] iv Buffer containing the IV to use.
1676 * \param iv_length Size of the IV in bytes.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001677 *
1678 * \retval #PSA_SUCCESS
1679 * Success.
1680 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001681 * The operation state is not valid (not set up, or IV already set).
Gilles Peskinedcd14942018-07-12 00:30:52 +02001682 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001683 * The size of \p iv is not acceptable for the chosen algorithm,
Gilles Peskinedcd14942018-07-12 00:30:52 +02001684 * or the chosen algorithm does not use an IV.
1685 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1686 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1687 * \retval #PSA_ERROR_HARDWARE_FAILURE
1688 * \retval #PSA_ERROR_TAMPERING_DETECTED
1689 */
Gilles Peskinefe119512018-07-08 21:39:34 +02001690psa_status_t psa_cipher_set_iv(psa_cipher_operation_t *operation,
1691 const unsigned char *iv,
1692 size_t iv_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001693
Gilles Peskinedcd14942018-07-12 00:30:52 +02001694/** Encrypt or decrypt a message fragment in an active cipher operation.
1695 *
Gilles Peskine9ac94262018-07-12 20:15:32 +02001696 * Before calling this function, you must:
1697 * 1. Call either psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup().
1698 * The choice of setup function determines whether this function
1699 * encrypts or decrypts its input.
1700 * 2. If the algorithm requires an IV, call psa_cipher_generate_iv()
1701 * (recommended when encrypting) or psa_cipher_set_iv().
Gilles Peskinedcd14942018-07-12 00:30:52 +02001702 *
1703 * If this function returns an error status, the operation becomes inactive.
1704 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001705 * \param[in,out] operation Active cipher operation.
1706 * \param[in] input Buffer containing the message fragment to
1707 * encrypt or decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001708 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001709 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001710 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001711 * \param[out] output_length On success, the number of bytes
1712 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001713 *
1714 * \retval #PSA_SUCCESS
1715 * Success.
1716 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001717 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001718 * not set, or already completed).
1719 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1720 * The size of the \p output buffer is too small.
1721 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1722 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1723 * \retval #PSA_ERROR_HARDWARE_FAILURE
1724 * \retval #PSA_ERROR_TAMPERING_DETECTED
1725 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001726psa_status_t psa_cipher_update(psa_cipher_operation_t *operation,
1727 const uint8_t *input,
mohammad1603503973b2018-03-12 15:59:30 +02001728 size_t input_length,
Gilles Peskine2d277862018-06-18 15:41:12 +02001729 unsigned char *output,
1730 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001731 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001732
Gilles Peskinedcd14942018-07-12 00:30:52 +02001733/** Finish encrypting or decrypting a message in a cipher operation.
1734 *
1735 * The application must call psa_cipher_encrypt_setup() or
1736 * psa_cipher_decrypt_setup() before calling this function. The choice
1737 * of setup function determines whether this function encrypts or
1738 * decrypts its input.
1739 *
1740 * This function finishes the encryption or decryption of the message
1741 * formed by concatenating the inputs passed to preceding calls to
1742 * psa_cipher_update().
1743 *
1744 * When this function returns, the operation becomes inactive.
1745 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02001746 * \param[in,out] operation Active cipher operation.
1747 * \param[out] output Buffer where the output is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001748 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001749 * \param[out] output_length On success, the number of bytes
1750 * that make up the returned output.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001751 *
1752 * \retval #PSA_SUCCESS
1753 * Success.
1754 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinef45adda2019-01-14 18:29:18 +01001755 * The operation state is not valid (not set up, IV required but
Gilles Peskinedcd14942018-07-12 00:30:52 +02001756 * not set, or already completed).
1757 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1758 * The size of the \p output buffer is too small.
1759 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1760 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1761 * \retval #PSA_ERROR_HARDWARE_FAILURE
1762 * \retval #PSA_ERROR_TAMPERING_DETECTED
1763 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001764psa_status_t psa_cipher_finish(psa_cipher_operation_t *operation,
mohammad1603503973b2018-03-12 15:59:30 +02001765 uint8_t *output,
Moran Peker0071b872018-04-22 20:16:58 +03001766 size_t output_size,
mohammad1603503973b2018-03-12 15:59:30 +02001767 size_t *output_length);
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001768
Gilles Peskinedcd14942018-07-12 00:30:52 +02001769/** Abort a cipher operation.
1770 *
Gilles Peskinedcd14942018-07-12 00:30:52 +02001771 * Aborting an operation frees all associated resources except for the
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001772 * \p operation structure itself. Once aborted, the operation object
1773 * can be reused for another operation by calling
1774 * psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup() again.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001775 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001776 * You may call this function any time after the operation object has
1777 * been initialized by any of the following methods:
1778 * - A call to psa_cipher_encrypt_setup() or psa_cipher_decrypt_setup(),
1779 * whether it succeeds or not.
1780 * - Initializing the \c struct to all-bits-zero.
1781 * - Initializing the \c struct to logical zeros, e.g.
1782 * `psa_cipher_operation_t operation = {0}`.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001783 *
Gilles Peskineb82ab6f2018-07-13 15:33:43 +02001784 * In particular, calling psa_cipher_abort() after the operation has been
1785 * terminated by a call to psa_cipher_abort() or psa_cipher_finish()
1786 * is safe and has no effect.
1787 *
1788 * \param[in,out] operation Initialized cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001789 *
1790 * \retval #PSA_SUCCESS
1791 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001792 * \p operation is not an active cipher operation.
Gilles Peskinedcd14942018-07-12 00:30:52 +02001793 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1794 * \retval #PSA_ERROR_HARDWARE_FAILURE
1795 * \retval #PSA_ERROR_TAMPERING_DETECTED
1796 */
Gilles Peskine428dc5a2018-03-03 21:27:18 +01001797psa_status_t psa_cipher_abort(psa_cipher_operation_t *operation);
1798
1799/**@}*/
1800
Gilles Peskine3b555712018-03-03 21:27:57 +01001801/** \defgroup aead Authenticated encryption with associated data (AEAD)
1802 * @{
1803 */
1804
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001805/** Process an authenticated encryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001806 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001807 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001808 * \param alg The AEAD algorithm to compute
1809 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001810 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001811 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001812 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001813 * \param[in] additional_data Additional data that will be authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001814 * but not encrypted.
1815 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001816 * \param[in] plaintext Data that will be authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001817 * encrypted.
1818 * \param plaintext_length Size of \p plaintext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001819 * \param[out] ciphertext Output buffer for the authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001820 * encrypted data. The additional data is not
1821 * part of this output. For algorithms where the
1822 * encrypted data and the authentication tag
1823 * are defined as separate outputs, the
1824 * authentication tag is appended to the
1825 * encrypted data.
1826 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
1827 * This must be at least
1828 * #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg,
1829 * \p plaintext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001830 * \param[out] ciphertext_length On success, the size of the output
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001831 * in the \b ciphertext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001832 *
Gilles Peskine28538492018-07-11 17:34:00 +02001833 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001834 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001835 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001836 * \retval #PSA_ERROR_EMPTY_SLOT
1837 * \retval #PSA_ERROR_NOT_PERMITTED
1838 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001839 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001840 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001841 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001842 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1843 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1844 * \retval #PSA_ERROR_HARDWARE_FAILURE
1845 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001846 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001847 * The library has not been previously initialized by psa_crypto_init().
1848 * It is implementation-dependent whether a failure to initialize
1849 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001850 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001851psa_status_t psa_aead_encrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001852 psa_algorithm_t alg,
1853 const uint8_t *nonce,
1854 size_t nonce_length,
1855 const uint8_t *additional_data,
1856 size_t additional_data_length,
1857 const uint8_t *plaintext,
1858 size_t plaintext_length,
1859 uint8_t *ciphertext,
1860 size_t ciphertext_size,
1861 size_t *ciphertext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001862
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001863/** Process an authenticated decryption operation.
Gilles Peskine3b555712018-03-03 21:27:57 +01001864 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01001865 * \param handle Handle to the key to use for the operation.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001866 * \param alg The AEAD algorithm to compute
1867 * (\c PSA_ALG_XXX value such that
Gilles Peskine7256e6c2018-07-12 00:34:26 +02001868 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001869 * \param[in] nonce Nonce or IV to use.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001870 * \param nonce_length Size of the \p nonce buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001871 * \param[in] additional_data Additional data that has been authenticated
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001872 * but not encrypted.
1873 * \param additional_data_length Size of \p additional_data in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001874 * \param[in] ciphertext Data that has been authenticated and
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001875 * encrypted. For algorithms where the
1876 * encrypted data and the authentication tag
1877 * are defined as separate inputs, the buffer
1878 * must contain the encrypted data followed
1879 * by the authentication tag.
1880 * \param ciphertext_length Size of \p ciphertext in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02001881 * \param[out] plaintext Output buffer for the decrypted data.
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001882 * \param plaintext_size Size of the \p plaintext buffer in bytes.
1883 * This must be at least
1884 * #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg,
1885 * \p ciphertext_length).
Gilles Peskineedd11a12018-07-12 01:08:58 +02001886 * \param[out] plaintext_length On success, the size of the output
mohammad1603fb5b9cb2018-06-06 13:44:27 +03001887 * in the \b plaintext buffer.
Gilles Peskine3b555712018-03-03 21:27:57 +01001888 *
Gilles Peskine28538492018-07-11 17:34:00 +02001889 * \retval #PSA_SUCCESS
Gilles Peskine3b555712018-03-03 21:27:57 +01001890 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01001891 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine28538492018-07-11 17:34:00 +02001892 * \retval #PSA_ERROR_EMPTY_SLOT
1893 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine1e7d8f12018-06-01 16:29:38 +02001894 * The ciphertext is not authentic.
Gilles Peskine28538492018-07-11 17:34:00 +02001895 * \retval #PSA_ERROR_NOT_PERMITTED
1896 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001897 * \p key is not compatible with \p alg.
Gilles Peskine28538492018-07-11 17:34:00 +02001898 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskinefa4070c2018-07-12 19:23:03 +02001899 * \p alg is not supported or is not an AEAD algorithm.
Gilles Peskine28538492018-07-11 17:34:00 +02001900 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1901 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1902 * \retval #PSA_ERROR_HARDWARE_FAILURE
1903 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03001904 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03001905 * The library has not been previously initialized by psa_crypto_init().
1906 * It is implementation-dependent whether a failure to initialize
1907 * results in this error code.
Gilles Peskine3b555712018-03-03 21:27:57 +01001908 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01001909psa_status_t psa_aead_decrypt(psa_key_handle_t handle,
Gilles Peskine9fb0e012018-07-19 15:51:49 +02001910 psa_algorithm_t alg,
1911 const uint8_t *nonce,
1912 size_t nonce_length,
1913 const uint8_t *additional_data,
1914 size_t additional_data_length,
1915 const uint8_t *ciphertext,
1916 size_t ciphertext_length,
1917 uint8_t *plaintext,
1918 size_t plaintext_size,
1919 size_t *plaintext_length);
Gilles Peskine3b555712018-03-03 21:27:57 +01001920
Gilles Peskine30a9e412019-01-14 18:36:12 +01001921/** The type of the state data structure for multipart AEAD operations.
1922 *
1923 * Before calling any function on an AEAD operation object, the application
1924 * must initialize it by any of the following means:
1925 * - Set the structure to all-bits-zero, for example:
1926 * \code
1927 * psa_aead_operation_t operation;
1928 * memset(&operation, 0, sizeof(operation));
1929 * \endcode
1930 * - Initialize the structure to logical zero values, for example:
1931 * \code
1932 * psa_aead_operation_t operation = {0};
1933 * \endcode
1934 * - Initialize the structure to the initializer #PSA_AEAD_OPERATION_INIT,
1935 * for example:
1936 * \code
1937 * psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT;
1938 * \endcode
1939 * - Assign the result of the function psa_aead_operation_init()
1940 * to the structure, for example:
1941 * \code
1942 * psa_aead_operation_t operation;
1943 * operation = psa_aead_operation_init();
1944 * \endcode
1945 *
1946 * This is an implementation-defined \c struct. Applications should not
1947 * make any assumptions about the content of this structure except
1948 * as directed by the documentation of a specific implementation. */
1949typedef struct psa_aead_operation_s psa_aead_operation_t;
1950
1951/** \def PSA_AEAD_OPERATION_INIT
1952 *
1953 * This macro returns a suitable initializer for an AEAD operation object of
1954 * type #psa_aead_operation_t.
1955 */
1956#ifdef __DOXYGEN_ONLY__
1957/* This is an example definition for documentation purposes.
1958 * Implementations should define a suitable value in `crypto_struct.h`.
1959 */
1960#define PSA_AEAD_OPERATION_INIT {0}
1961#endif
1962
1963/** Return an initial value for an AEAD operation object.
1964 */
1965static psa_aead_operation_t psa_aead_operation_init(void);
1966
1967/** Set the key for a multipart authenticated encryption operation.
1968 *
1969 * The sequence of operations to encrypt a message with authentication
1970 * is as follows:
1971 * -# Allocate an operation object which will be passed to all the functions
1972 * listed here.
1973 * -# Initialize the operation object with one of the methods described in the
1974 * documentation for #psa_aead_operation_t, e.g.
1975 * PSA_AEAD_OPERATION_INIT.
1976 * -# Call psa_aead_encrypt_setup() to specify the algorithm and key.
1977 * -# Call either psa_aead_generate_nonce() or psa_aead_set_nonce() to
1978 * generate or set the nonce. You should use
1979 * psa_aead_generate_nonce() unless the protocol you are implementing
1980 * requires a specific nonce value.
1981 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
1982 * of the non-encrypted additional authenticated data each time.
1983 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01001984 * of the message to encrypt each time.
Gilles Peskine30a9e412019-01-14 18:36:12 +01001985 * -# Call psa_aead_finish().
1986 *
1987 * The application may call psa_aead_abort() at any time after the operation
1988 * has been initialized.
1989 *
1990 * After a successful call to psa_aead_encrypt_setup(), the application must
1991 * eventually terminate the operation. The following events terminate an
1992 * operation:
1993 * - A failed call to any of the \c psa_aead_xxx functions.
1994 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
1995 *
1996 * \param[in,out] operation The operation object to set up. It must have
1997 * been initialized as per the documentation for
1998 * #psa_aead_operation_t and not yet in use.
1999 * \param handle Handle to the key to use for the operation.
2000 * It must remain valid until the operation
2001 * terminates.
2002 * \param alg The AEAD algorithm to compute
2003 * (\c PSA_ALG_XXX value such that
2004 * #PSA_ALG_IS_AEAD(\p alg) is true).
2005 *
2006 * \retval #PSA_SUCCESS
2007 * Success.
2008 * \retval #PSA_ERROR_INVALID_HANDLE
2009 * \retval #PSA_ERROR_EMPTY_SLOT
2010 * \retval #PSA_ERROR_NOT_PERMITTED
2011 * \retval #PSA_ERROR_INVALID_ARGUMENT
2012 * \p key is not compatible with \p alg.
2013 * \retval #PSA_ERROR_NOT_SUPPORTED
2014 * \p alg is not supported or is not an AEAD algorithm.
2015 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2016 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2017 * \retval #PSA_ERROR_HARDWARE_FAILURE
2018 * \retval #PSA_ERROR_TAMPERING_DETECTED
2019 * \retval #PSA_ERROR_BAD_STATE
2020 * The library has not been previously initialized by psa_crypto_init().
2021 * It is implementation-dependent whether a failure to initialize
2022 * results in this error code.
2023 */
2024psa_status_t psa_aead_encrypt_setup(psa_aead_operation_t *operation,
2025 psa_key_handle_t handle,
2026 psa_algorithm_t alg);
2027
2028/** Set the key for a multipart authenticated decryption operation.
2029 *
2030 * The sequence of operations to decrypt a message with authentication
2031 * is as follows:
2032 * -# Allocate an operation object which will be passed to all the functions
2033 * listed here.
2034 * -# Initialize the operation object with one of the methods described in the
2035 * documentation for #psa_aead_operation_t, e.g.
2036 * PSA_AEAD_OPERATION_INIT.
2037 * -# Call psa_aead_decrypt_setup() to specify the algorithm and key.
2038 * -# Call psa_aead_set_nonce() with the nonce for the decryption.
2039 * -# Call psa_aead_update_ad() zero, one or more times, passing a fragment
2040 * of the non-encrypted additional authenticated data each time.
2041 * -# Call psa_aead_update() zero, one or more times, passing a fragment
Gilles Peskinea05602d2019-01-17 15:25:52 +01002042 * of the ciphertext to decrypt each time.
2043 * -# Call psa_aead_verify().
Gilles Peskine30a9e412019-01-14 18:36:12 +01002044 *
2045 * The application may call psa_aead_abort() at any time after the operation
2046 * has been initialized.
2047 *
2048 * After a successful call to psa_aead_decrypt_setup(), the application must
2049 * eventually terminate the operation. The following events terminate an
2050 * operation:
2051 * - A failed call to any of the \c psa_aead_xxx functions.
2052 * - A call to psa_aead_finish(), psa_aead_verify() or psa_aead_abort().
2053 *
2054 * \param[in,out] operation The operation object to set up. It must have
2055 * been initialized as per the documentation for
2056 * #psa_aead_operation_t and not yet in use.
2057 * \param handle Handle to the key to use for the operation.
2058 * It must remain valid until the operation
2059 * terminates.
2060 * \param alg The AEAD algorithm to compute
2061 * (\c PSA_ALG_XXX value such that
2062 * #PSA_ALG_IS_AEAD(\p alg) is true).
2063 *
2064 * \retval #PSA_SUCCESS
2065 * Success.
2066 * \retval #PSA_ERROR_INVALID_HANDLE
2067 * \retval #PSA_ERROR_EMPTY_SLOT
2068 * \retval #PSA_ERROR_NOT_PERMITTED
2069 * \retval #PSA_ERROR_INVALID_ARGUMENT
2070 * \p key is not compatible with \p alg.
2071 * \retval #PSA_ERROR_NOT_SUPPORTED
2072 * \p alg is not supported or is not an AEAD algorithm.
2073 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2074 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2075 * \retval #PSA_ERROR_HARDWARE_FAILURE
2076 * \retval #PSA_ERROR_TAMPERING_DETECTED
2077 * \retval #PSA_ERROR_BAD_STATE
2078 * The library has not been previously initialized by psa_crypto_init().
2079 * It is implementation-dependent whether a failure to initialize
2080 * results in this error code.
2081 */
2082psa_status_t psa_aead_decrypt_setup(psa_aead_operation_t *operation,
2083 psa_key_handle_t handle,
2084 psa_algorithm_t alg);
2085
2086/** Generate a random nonce for an authenticated encryption operation.
2087 *
2088 * This function generates a random nonce for the authenticated encryption
2089 * operation with an appropriate size for the chosen algorithm, key type
2090 * and key size.
2091 *
2092 * The application must call psa_aead_encrypt_setup() before
2093 * calling this function.
2094 *
2095 * If this function returns an error status, the operation becomes inactive.
2096 *
2097 * \param[in,out] operation Active AEAD operation.
2098 * \param[out] nonce Buffer where the generated nonce is to be
2099 * written.
2100 * \param nonce_size Size of the \p nonce buffer in bytes.
2101 * \param[out] nonce_length On success, the number of bytes of the
2102 * generated nonce.
2103 *
2104 * \retval #PSA_SUCCESS
2105 * Success.
2106 * \retval #PSA_ERROR_BAD_STATE
2107 * The operation state is not valid (not set up, or nonce already set).
2108 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2109 * The size of the \p nonce buffer is too small.
2110 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2111 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2112 * \retval #PSA_ERROR_HARDWARE_FAILURE
2113 * \retval #PSA_ERROR_TAMPERING_DETECTED
2114 */
2115psa_status_t psa_aead_generate_nonce(psa_aead_operation_t *operation,
2116 unsigned char *nonce,
2117 size_t nonce_size,
2118 size_t *nonce_length);
2119
2120/** Set the nonce for an authenticated encryption or decryption operation.
2121 *
2122 * This function sets the nonce for the authenticated
2123 * encryption or decryption operation.
2124 *
2125 * The application must call psa_aead_encrypt_setup() before
2126 * calling this function.
2127 *
2128 * If this function returns an error status, the operation becomes inactive.
2129 *
Gilles Peskinea05602d2019-01-17 15:25:52 +01002130 * \note When encrypting, applications should use psa_aead_generate_nonce()
Gilles Peskine30a9e412019-01-14 18:36:12 +01002131 * instead of this function, unless implementing a protocol that requires
2132 * a non-random IV.
2133 *
2134 * \param[in,out] operation Active AEAD operation.
Gilles Peskinea05602d2019-01-17 15:25:52 +01002135 * \param[in] nonce Buffer containing the nonce to use.
2136 * \param nonce_length Size of the nonce in bytes.
Gilles Peskine30a9e412019-01-14 18:36:12 +01002137 *
2138 * \retval #PSA_SUCCESS
2139 * Success.
2140 * \retval #PSA_ERROR_BAD_STATE
2141 * The operation state is not valid (not set up, or nonce already set).
2142 * \retval #PSA_ERROR_INVALID_ARGUMENT
2143 * The size of \p nonce is not acceptable for the chosen algorithm.
2144 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2145 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2146 * \retval #PSA_ERROR_HARDWARE_FAILURE
2147 * \retval #PSA_ERROR_TAMPERING_DETECTED
2148 */
2149psa_status_t psa_aead_set_nonce(psa_aead_operation_t *operation,
2150 const unsigned char *nonce,
2151 size_t nonce_length);
2152
2153/** Pass additional data to an active AEAD operation.
2154 *
2155 * Additional data is authenticated, but not encrypted.
2156 *
2157 * You may call this function multiple times to pass successive fragments
2158 * of the additional data. You may not call this function after passing
2159 * data to encrypt or decrypt with psa_aead_update().
2160 *
2161 * Before calling this function, you must:
2162 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2163 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2164 *
2165 * If this function returns an error status, the operation becomes inactive.
2166 *
2167 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2168 * there is no guarantee that the input is valid. Therefore, until
2169 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS,
2170 * treat the input as untrusted and prepare to undo any action that
2171 * depends on the input if psa_aead_verify() returns an error status.
2172 *
2173 * \param[in,out] operation Active AEAD operation.
2174 * \param[in] input Buffer containing the fragment of
2175 * additional data.
2176 * \param input_length Size of the \p input buffer in bytes.
2177 *
2178 * \retval #PSA_SUCCESS
2179 * Success.
2180 * \retval #PSA_ERROR_BAD_STATE
2181 * The operation state is not valid (not set up, nonce not set,
2182 * psa_aead_update() already called, or operation already completed).
2183 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2184 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2185 * \retval #PSA_ERROR_HARDWARE_FAILURE
2186 * \retval #PSA_ERROR_TAMPERING_DETECTED
2187 */
2188psa_status_t psa_aead_update_ad(psa_aead_operation_t *operation,
2189 const uint8_t *input,
2190 size_t input_length);
2191
2192/** Encrypt or decrypt a message fragment in an active AEAD operation.
2193 *
2194 * Before calling this function, you must:
2195 * 1. Call either psa_aead_encrypt_setup() or psa_aead_decrypt_setup().
2196 * The choice of setup function determines whether this function
2197 * encrypts or decrypts its input.
2198 * 2. Set the nonce with psa_aead_generate_nonce() or psa_aead_set_nonce().
2199 * 3. Call psa_aead_update_ad() to pass all the additional data.
2200 *
2201 * If this function returns an error status, the operation becomes inactive.
2202 *
2203 * \warning When decrypting, until psa_aead_verify() has returned #PSA_SUCCESS,
2204 * there is no guarantee that the input is valid. Therefore, until
2205 * you have called psa_aead_verify() and it has returned #PSA_SUCCESS:
2206 * - Do not use the output in any way other than storing it in a
2207 * confidential location. If you take any action that depends
2208 * on the tentative decrypted data, this action will need to be
2209 * undone if the input turns out not to be valid. Furthermore,
2210 * if an adversary can observe that this action took place
2211 * (for example through timing), they may be able to use this
2212 * fact as an oracle to decrypt any message encrypted with the
2213 * same key.
2214 * - In particular, do not copy the output anywhere but to a
2215 * memory or storage space that you have exclusive access to.
2216 *
2217 * \param[in,out] operation Active AEAD operation.
2218 * \param[in] input Buffer containing the message fragment to
2219 * encrypt or decrypt.
2220 * \param input_length Size of the \p input buffer in bytes.
2221 * \param[out] output Buffer where the output is to be written.
2222 * \param output_size Size of the \p output buffer in bytes.
2223 * \param[out] output_length On success, the number of bytes
2224 * that make up the returned output.
2225 *
2226 * \retval #PSA_SUCCESS
2227 * Success.
2228 * \retval #PSA_ERROR_BAD_STATE
2229 * The operation state is not valid (not set up, nonce not set
2230 * or already completed).
2231 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2232 * The size of the \p output buffer is too small.
2233 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2234 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2235 * \retval #PSA_ERROR_HARDWARE_FAILURE
2236 * \retval #PSA_ERROR_TAMPERING_DETECTED
2237 */
2238psa_status_t psa_aead_update(psa_aead_operation_t *operation,
2239 const uint8_t *input,
2240 size_t input_length,
2241 unsigned char *output,
2242 size_t output_size,
2243 size_t *output_length);
2244
2245/** Finish encrypting a message in an AEAD operation.
2246 *
2247 * The operation must have been set up with psa_aead_encrypt_setup().
2248 *
2249 * This function finishes the authentication of the additional data
2250 * formed by concatenating the inputs passed to preceding calls to
2251 * psa_aead_update_ad() with the plaintext formed by concatenating the
2252 * inputs passed to preceding calls to psa_aead_update().
2253 *
2254 * This function has two output buffers:
2255 * - \p ciphertext contains trailing ciphertext that was buffered from
2256 * preceding calls to psa_aead_update(). For all standard AEAD algorithms,
2257 * psa_aead_update() does not buffer any output and therefore \p ciphertext
2258 * will not contain any output and can be a 0-sized buffer.
2259 * - \p tag contains the authentication tag. Its length is always
2260 * #PSA_AEAD_TAG_LENGTH(\p alg) where \p alg is the AEAD algorithm
2261 * that the operation performs.
2262 *
2263 * When this function returns, the operation becomes inactive.
2264 *
2265 * \param[in,out] operation Active AEAD operation.
2266 * \param[out] ciphertext Buffer where the last part of the ciphertext
2267 * is to be written.
2268 * \param ciphertext_size Size of the \p ciphertext buffer in bytes.
2269 * \param[out] ciphertext_length On success, the number of bytes of
2270 * returned ciphertext.
2271 * \param[out] tag Buffer where the authentication tag is
2272 * to be written.
2273 * \param tag_size Size of the \p tag buffer in bytes.
2274 * \param[out] tag_length On success, the number of bytes
2275 * that make up the returned tag.
2276 *
2277 * \retval #PSA_SUCCESS
2278 * Success.
2279 * \retval #PSA_ERROR_BAD_STATE
2280 * The operation state is not valid (not set up, nonce not set,
2281 * decryption, or already completed).
2282 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2283 * The size of the \p output buffer is too small.
2284 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2285 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2286 * \retval #PSA_ERROR_HARDWARE_FAILURE
2287 * \retval #PSA_ERROR_TAMPERING_DETECTED
2288 */
2289psa_status_t psa_aead_finish(psa_aead_operation_t *operation,
Gilles Peskinea05602d2019-01-17 15:25:52 +01002290 uint8_t *ciphertext,
2291 size_t ciphertext_size,
2292 size_t *ciphertext_length,
Gilles Peskine30a9e412019-01-14 18:36:12 +01002293 uint8_t *tag,
2294 size_t tag_size,
2295 size_t *tag_length);
2296
2297/** Finish authenticating and decrypting a message in an AEAD operation.
2298 *
2299 * The operation must have been set up with psa_aead_decrypt_setup().
2300 *
2301 * This function finishes the authentication of the additional data
2302 * formed by concatenating the inputs passed to preceding calls to
2303 * psa_aead_update_ad() with the ciphertext formed by concatenating the
2304 * inputs passed to preceding calls to psa_aead_update().
2305 *
2306 * When this function returns, the operation becomes inactive.
2307 *
2308 * \param[in,out] operation Active AEAD operation.
2309 * \param[in] tag Buffer containing the authentication tag.
2310 * \param tag_length Size of the \p tag buffer in bytes.
2311 *
2312 * \retval #PSA_SUCCESS
2313 * Success.
2314 * \retval #PSA_ERROR_BAD_STATE
2315 * The operation state is not valid (not set up, nonce not set,
2316 * encryption, or already completed).
2317 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
2318 * The size of the \p output buffer is too small.
2319 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2320 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2321 * \retval #PSA_ERROR_HARDWARE_FAILURE
2322 * \retval #PSA_ERROR_TAMPERING_DETECTED
2323 */
2324psa_status_t psa_aead_verify(psa_aead_operation_t *operation,
2325 const uint8_t *tag,
2326 size_t tag_length);
2327
2328/** Abort an AEAD operation.
2329 *
2330 * Aborting an operation frees all associated resources except for the
2331 * \p operation structure itself. Once aborted, the operation object
2332 * can be reused for another operation by calling
2333 * psa_aead_encrypt_setup() or psa_aead_decrypt_setup() again.
2334 *
2335 * You may call this function any time after the operation object has
2336 * been initialized by any of the following methods:
2337 * - A call to psa_aead_encrypt_setup() or psa_aead_decrypt_setup(),
2338 * whether it succeeds or not.
2339 * - Initializing the \c struct to all-bits-zero.
2340 * - Initializing the \c struct to logical zeros, e.g.
2341 * `psa_aead_operation_t operation = {0}`.
2342 *
2343 * In particular, calling psa_aead_abort() after the operation has been
2344 * terminated by a call to psa_aead_abort() or psa_aead_finish()
2345 * is safe and has no effect.
2346 *
2347 * \param[in,out] operation Initialized AEAD operation.
2348 *
2349 * \retval #PSA_SUCCESS
2350 * \retval #PSA_ERROR_BAD_STATE
2351 * \p operation is not an active AEAD operation.
2352 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2353 * \retval #PSA_ERROR_HARDWARE_FAILURE
2354 * \retval #PSA_ERROR_TAMPERING_DETECTED
2355 */
2356psa_status_t psa_aead_abort(psa_aead_operation_t *operation);
2357
Gilles Peskine3b555712018-03-03 21:27:57 +01002358/**@}*/
2359
Gilles Peskine20035e32018-02-03 22:44:14 +01002360/** \defgroup asymmetric Asymmetric cryptography
2361 * @{
2362 */
2363
2364/**
2365 * \brief Sign a hash or short message with a private key.
2366 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002367 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002368 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002369 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2370 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2371 * to determine the hash algorithm to use.
2372 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002373 * \param handle Handle to the key to use for the operation.
2374 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002375 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002376 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002377 * \param[in] hash The hash or message to sign.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002378 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002379 * \param[out] signature Buffer where the signature is to be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002380 * \param signature_size Size of the \p signature buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002381 * \param[out] signature_length On success, the number of bytes
2382 * that make up the returned signature value.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002383 *
Gilles Peskine28538492018-07-11 17:34:00 +02002384 * \retval #PSA_SUCCESS
2385 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002386 * The size of the \p signature buffer is too small. You can
Gilles Peskine308b91d2018-02-08 09:47:44 +01002387 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002388 * #PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine308b91d2018-02-08 09:47:44 +01002389 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002390 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002391 * \retval #PSA_ERROR_NOT_SUPPORTED
2392 * \retval #PSA_ERROR_INVALID_ARGUMENT
2393 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2394 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2395 * \retval #PSA_ERROR_HARDWARE_FAILURE
2396 * \retval #PSA_ERROR_TAMPERING_DETECTED
2397 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002398 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002399 * The library has not been previously initialized by psa_crypto_init().
2400 * It is implementation-dependent whether a failure to initialize
2401 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002402 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002403psa_status_t psa_asymmetric_sign(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002404 psa_algorithm_t alg,
2405 const uint8_t *hash,
2406 size_t hash_length,
Gilles Peskine20035e32018-02-03 22:44:14 +01002407 uint8_t *signature,
2408 size_t signature_size,
2409 size_t *signature_length);
2410
2411/**
2412 * \brief Verify the signature a hash or short message using a public key.
2413 *
Gilles Peskine08bac712018-06-26 16:14:46 +02002414 * Note that to perform a hash-and-sign signature algorithm, you must
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002415 * first calculate the hash by calling psa_hash_setup(), psa_hash_update()
Gilles Peskine08bac712018-06-26 16:14:46 +02002416 * and psa_hash_finish(). Then pass the resulting hash as the \p hash
2417 * parameter to this function. You can use #PSA_ALG_SIGN_GET_HASH(\p alg)
2418 * to determine the hash algorithm to use.
2419 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002420 * \param handle Handle to the key to use for the operation.
2421 * It must be a public key or an asymmetric key pair.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002422 * \param alg A signature algorithm that is compatible with
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002423 * the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002424 * \param[in] hash The hash or message whose signature is to be
Gilles Peskine08bac712018-06-26 16:14:46 +02002425 * verified.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002426 * \param hash_length Size of the \p hash buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002427 * \param[in] signature Buffer containing the signature to verify.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002428 * \param signature_length Size of the \p signature buffer in bytes.
Gilles Peskine308b91d2018-02-08 09:47:44 +01002429 *
Gilles Peskine28538492018-07-11 17:34:00 +02002430 * \retval #PSA_SUCCESS
Gilles Peskine308b91d2018-02-08 09:47:44 +01002431 * The signature is valid.
Gilles Peskine28538492018-07-11 17:34:00 +02002432 * \retval #PSA_ERROR_INVALID_SIGNATURE
Gilles Peskine308b91d2018-02-08 09:47:44 +01002433 * The calculation was perfomed successfully, but the passed
2434 * signature is not a valid signature.
Gilles Peskine28538492018-07-11 17:34:00 +02002435 * \retval #PSA_ERROR_NOT_SUPPORTED
2436 * \retval #PSA_ERROR_INVALID_ARGUMENT
2437 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2438 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2439 * \retval #PSA_ERROR_HARDWARE_FAILURE
2440 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002441 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002442 * The library has not been previously initialized by psa_crypto_init().
2443 * It is implementation-dependent whether a failure to initialize
2444 * results in this error code.
Gilles Peskine20035e32018-02-03 22:44:14 +01002445 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002446psa_status_t psa_asymmetric_verify(psa_key_handle_t handle,
Gilles Peskine20035e32018-02-03 22:44:14 +01002447 psa_algorithm_t alg,
2448 const uint8_t *hash,
2449 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002450 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002451 size_t signature_length);
Gilles Peskine20035e32018-02-03 22:44:14 +01002452
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002453/**
2454 * \brief Encrypt a short message with a public key.
2455 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002456 * \param handle Handle to the key to use for the operation.
2457 * It must be a public key or an asymmetric
2458 * key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002459 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002460 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002461 * \param[in] input The message to encrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002462 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002463 * \param[in] salt A salt or label, if supported by the
2464 * encryption algorithm.
2465 * If the algorithm does not support a
2466 * salt, pass \c NULL.
2467 * If the algorithm supports an optional
2468 * salt and you do not want to pass a salt,
2469 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002470 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002471 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2472 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002473 * \param salt_length Size of the \p salt buffer in bytes.
2474 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002475 * \param[out] output Buffer where the encrypted message is to
2476 * be written.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002477 * \param output_size Size of the \p output buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002478 * \param[out] output_length On success, the number of bytes
2479 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002480 *
Gilles Peskine28538492018-07-11 17:34:00 +02002481 * \retval #PSA_SUCCESS
2482 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002483 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002484 * determine a sufficient buffer size by calling
Gilles Peskine7256e6c2018-07-12 00:34:26 +02002485 * #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002486 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002487 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002488 * \retval #PSA_ERROR_NOT_SUPPORTED
2489 * \retval #PSA_ERROR_INVALID_ARGUMENT
2490 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2491 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2492 * \retval #PSA_ERROR_HARDWARE_FAILURE
2493 * \retval #PSA_ERROR_TAMPERING_DETECTED
2494 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
itayzafrir90d8c7a2018-09-12 11:44:52 +03002495 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002496 * The library has not been previously initialized by psa_crypto_init().
2497 * It is implementation-dependent whether a failure to initialize
2498 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002499 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002500psa_status_t psa_asymmetric_encrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002501 psa_algorithm_t alg,
2502 const uint8_t *input,
2503 size_t input_length,
2504 const uint8_t *salt,
2505 size_t salt_length,
2506 uint8_t *output,
2507 size_t output_size,
2508 size_t *output_length);
2509
2510/**
2511 * \brief Decrypt a short message with a private key.
2512 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002513 * \param handle Handle to the key to use for the operation.
2514 * It must be an asymmetric key pair.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002515 * \param alg An asymmetric encryption algorithm that is
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002516 * compatible with the type of \p key.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002517 * \param[in] input The message to decrypt.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002518 * \param input_length Size of the \p input buffer in bytes.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002519 * \param[in] salt A salt or label, if supported by the
2520 * encryption algorithm.
2521 * If the algorithm does not support a
2522 * salt, pass \c NULL.
2523 * If the algorithm supports an optional
2524 * salt and you do not want to pass a salt,
2525 * pass \c NULL.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002526 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002527 * - For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
2528 * supported.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002529 * \param salt_length Size of the \p salt buffer in bytes.
2530 * If \p salt is \c NULL, pass 0.
Gilles Peskineedd11a12018-07-12 01:08:58 +02002531 * \param[out] output Buffer where the decrypted message is to
2532 * be written.
2533 * \param output_size Size of the \c output buffer in bytes.
2534 * \param[out] output_length On success, the number of bytes
2535 * that make up the returned output.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002536 *
Gilles Peskine28538492018-07-11 17:34:00 +02002537 * \retval #PSA_SUCCESS
2538 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002539 * The size of the \p output buffer is too small. You can
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002540 * determine a sufficient buffer size by calling
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002541 * #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002542 * where \c key_type and \c key_bits are the type and bit-size
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002543 * respectively of \p key.
Gilles Peskine28538492018-07-11 17:34:00 +02002544 * \retval #PSA_ERROR_NOT_SUPPORTED
2545 * \retval #PSA_ERROR_INVALID_ARGUMENT
2546 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2547 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2548 * \retval #PSA_ERROR_HARDWARE_FAILURE
2549 * \retval #PSA_ERROR_TAMPERING_DETECTED
2550 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2551 * \retval #PSA_ERROR_INVALID_PADDING
itayzafrir90d8c7a2018-09-12 11:44:52 +03002552 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002553 * The library has not been previously initialized by psa_crypto_init().
2554 * It is implementation-dependent whether a failure to initialize
2555 * results in this error code.
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002556 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002557psa_status_t psa_asymmetric_decrypt(psa_key_handle_t handle,
Gilles Peskine6944f9a2018-03-28 14:18:39 +02002558 psa_algorithm_t alg,
2559 const uint8_t *input,
2560 size_t input_length,
2561 const uint8_t *salt,
2562 size_t salt_length,
2563 uint8_t *output,
2564 size_t output_size,
2565 size_t *output_length);
2566
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002567/**@}*/
2568
Gilles Peskineedd76872018-07-20 17:42:05 +02002569/** \defgroup generators Generators
Gilles Peskineeab56e42018-07-12 17:12:33 +02002570 * @{
2571 */
2572
2573/** The type of the state data structure for generators.
2574 *
2575 * Before calling any function on a generator, the application must
2576 * initialize it by any of the following means:
2577 * - Set the structure to all-bits-zero, for example:
2578 * \code
2579 * psa_crypto_generator_t generator;
2580 * memset(&generator, 0, sizeof(generator));
2581 * \endcode
2582 * - Initialize the structure to logical zero values, for example:
2583 * \code
2584 * psa_crypto_generator_t generator = {0};
2585 * \endcode
2586 * - Initialize the structure to the initializer #PSA_CRYPTO_GENERATOR_INIT,
2587 * for example:
2588 * \code
2589 * psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2590 * \endcode
2591 * - Assign the result of the function psa_crypto_generator_init()
2592 * to the structure, for example:
2593 * \code
2594 * psa_crypto_generator_t generator;
2595 * generator = psa_crypto_generator_init();
2596 * \endcode
2597 *
2598 * This is an implementation-defined \c struct. Applications should not
2599 * make any assumptions about the content of this structure except
2600 * as directed by the documentation of a specific implementation.
2601 */
2602typedef struct psa_crypto_generator_s psa_crypto_generator_t;
2603
2604/** \def PSA_CRYPTO_GENERATOR_INIT
2605 *
2606 * This macro returns a suitable initializer for a generator object
2607 * of type #psa_crypto_generator_t.
2608 */
2609#ifdef __DOXYGEN_ONLY__
2610/* This is an example definition for documentation purposes.
2611 * Implementations should define a suitable value in `crypto_struct.h`.
2612 */
2613#define PSA_CRYPTO_GENERATOR_INIT {0}
2614#endif
2615
2616/** Return an initial value for a generator object.
2617 */
2618static psa_crypto_generator_t psa_crypto_generator_init(void);
2619
2620/** Retrieve the current capacity of a generator.
2621 *
2622 * The capacity of a generator is the maximum number of bytes that it can
2623 * return. Reading *N* bytes from a generator reduces its capacity by *N*.
2624 *
2625 * \param[in] generator The generator to query.
2626 * \param[out] capacity On success, the capacity of the generator.
2627 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002628 * \retval #PSA_SUCCESS
2629 * \retval #PSA_ERROR_BAD_STATE
2630 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
Gilles Peskineeab56e42018-07-12 17:12:33 +02002631 */
2632psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
2633 size_t *capacity);
2634
2635/** Read some data from a generator.
2636 *
2637 * This function reads and returns a sequence of bytes from a generator.
2638 * The data that is read is discarded from the generator. The generator's
2639 * capacity is decreased by the number of bytes read.
2640 *
2641 * \param[in,out] generator The generator object to read from.
2642 * \param[out] output Buffer where the generator output will be
2643 * written.
2644 * \param output_length Number of bytes to output.
2645 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002646 * \retval #PSA_SUCCESS
2647 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02002648 * There were fewer than \p output_length bytes
2649 * in the generator. Note that in this case, no
2650 * output is written to the output buffer.
2651 * The generator's capacity is set to 0, thus
2652 * subsequent calls to this function will not
2653 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002654 * \retval #PSA_ERROR_BAD_STATE
2655 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2656 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2657 * \retval #PSA_ERROR_HARDWARE_FAILURE
2658 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002659 */
2660psa_status_t psa_generator_read(psa_crypto_generator_t *generator,
2661 uint8_t *output,
2662 size_t output_length);
2663
2664/** Create a symmetric key from data read from a generator.
2665 *
2666 * This function reads a sequence of bytes from a generator and imports
2667 * these bytes as a key.
2668 * The data that is read is discarded from the generator. The generator's
2669 * capacity is decreased by the number of bytes read.
2670 *
2671 * This function is equivalent to calling #psa_generator_read and
2672 * passing the resulting output to #psa_import_key, but
2673 * if the implementation provides an isolation boundary then
2674 * the key material is not exposed outside the isolation boundary.
2675 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002676 * \param handle Handle to the slot where the key will be stored.
2677 * This must be a valid slot for a key of the chosen
2678 * type: it must have been obtained by calling
2679 * psa_allocate_key() or psa_create_key() with the
2680 * correct \p type and with a maximum size that is
2681 * compatible with \p bits.
2682 * It must not contain any key material yet.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002683 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2684 * This must be a symmetric key type.
2685 * \param bits Key size in bits.
2686 * \param[in,out] generator The generator object to read from.
2687 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002688 * \retval #PSA_SUCCESS
Gilles Peskineeab56e42018-07-12 17:12:33 +02002689 * Success.
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01002690 * If the key is persistent, the key material and the key's metadata
2691 * have been saved to persistent storage.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002692 * \retval #PSA_ERROR_INSUFFICIENT_CAPACITY
Gilles Peskineeab56e42018-07-12 17:12:33 +02002693 * There were fewer than \p output_length bytes
2694 * in the generator. Note that in this case, no
2695 * output is written to the output buffer.
2696 * The generator's capacity is set to 0, thus
2697 * subsequent calls to this function will not
2698 * succeed, even with a smaller output buffer.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002699 * \retval #PSA_ERROR_NOT_SUPPORTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002700 * The key type or key size is not supported, either by the
2701 * implementation in general or in this particular slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002702 * \retval #PSA_ERROR_BAD_STATE
Gilles Peskineae32aac2018-11-30 14:39:32 +01002703 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002704 * \retval #PSA_ERROR_OCCUPIED_SLOT
Gilles Peskineeab56e42018-07-12 17:12:33 +02002705 * There is already a key in the specified slot.
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002706 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2707 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
2708 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2709 * \retval #PSA_ERROR_HARDWARE_FAILURE
2710 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002711 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002712 * The library has not been previously initialized by psa_crypto_init().
2713 * It is implementation-dependent whether a failure to initialize
2714 * results in this error code.
Gilles Peskineeab56e42018-07-12 17:12:33 +02002715 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002716psa_status_t psa_generator_import_key(psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02002717 psa_key_type_t type,
2718 size_t bits,
2719 psa_crypto_generator_t *generator);
2720
2721/** Abort a generator.
2722 *
2723 * Once a generator has been aborted, its capacity is zero.
2724 * Aborting a generator frees all associated resources except for the
2725 * \c generator structure itself.
2726 *
2727 * This function may be called at any time as long as the generator
2728 * object has been initialized to #PSA_CRYPTO_GENERATOR_INIT, to
2729 * psa_crypto_generator_init() or a zero value. In particular, it is valid
2730 * to call psa_generator_abort() twice, or to call psa_generator_abort()
2731 * on a generator that has not been set up.
2732 *
2733 * Once aborted, the generator object may be called.
2734 *
2735 * \param[in,out] generator The generator to abort.
2736 *
Gilles Peskine644cd5f2018-12-11 16:47:35 +01002737 * \retval #PSA_SUCCESS
2738 * \retval #PSA_ERROR_BAD_STATE
2739 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2740 * \retval #PSA_ERROR_HARDWARE_FAILURE
2741 * \retval #PSA_ERROR_TAMPERING_DETECTED
Gilles Peskineeab56e42018-07-12 17:12:33 +02002742 */
2743psa_status_t psa_generator_abort(psa_crypto_generator_t *generator);
2744
Gilles Peskine8feb3a82018-09-18 12:06:11 +02002745/** Use the maximum possible capacity for a generator.
2746 *
2747 * Use this value as the capacity argument when setting up a generator
2748 * to indicate that the generator should have the maximum possible capacity.
2749 * The value of the maximum possible capacity depends on the generator
2750 * algorithm.
2751 */
2752#define PSA_GENERATOR_UNBRIDLED_CAPACITY ((size_t)(-1))
2753
Gilles Peskineeab56e42018-07-12 17:12:33 +02002754/**@}*/
2755
Gilles Peskineea0fb492018-07-12 17:17:20 +02002756/** \defgroup derivation Key derivation
2757 * @{
2758 */
2759
2760/** Set up a key derivation operation.
2761 *
2762 * A key derivation algorithm takes three inputs: a secret input \p key and
2763 * two non-secret inputs \p label and p salt.
2764 * The result of this function is a byte generator which can
2765 * be used to produce keys and other cryptographic material.
2766 *
2767 * The role of \p label and \p salt is as follows:
Gilles Peskinebef7f142018-07-12 17:22:21 +02002768 * - For HKDF (#PSA_ALG_HKDF), \p salt is the salt used in the "extract" step
2769 * and \p label is the info string used in the "expand" step.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002770 *
Jaeden Amero9e919c62019-01-07 15:41:50 +00002771 * \param[in,out] generator The generator object to set up. It must have
2772 * been initialized as per the documentation for
2773 * #psa_crypto_generator_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002774 * \param handle Handle to the secret key.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002775 * \param alg The key derivation algorithm to compute
2776 * (\c PSA_ALG_XXX value such that
2777 * #PSA_ALG_IS_KEY_DERIVATION(\p alg) is true).
2778 * \param[in] salt Salt to use.
2779 * \param salt_length Size of the \p salt buffer in bytes.
2780 * \param[in] label Label to use.
2781 * \param label_length Size of the \p label buffer in bytes.
2782 * \param capacity The maximum number of bytes that the
2783 * generator will be able to provide.
2784 *
2785 * \retval #PSA_SUCCESS
2786 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002787 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskineea0fb492018-07-12 17:17:20 +02002788 * \retval #PSA_ERROR_EMPTY_SLOT
2789 * \retval #PSA_ERROR_NOT_PERMITTED
2790 * \retval #PSA_ERROR_INVALID_ARGUMENT
2791 * \c key is not compatible with \c alg,
2792 * or \p capacity is too large for the specified algorithm and key.
2793 * \retval #PSA_ERROR_NOT_SUPPORTED
2794 * \c alg is not supported or is not a key derivation algorithm.
2795 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2796 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2797 * \retval #PSA_ERROR_HARDWARE_FAILURE
2798 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002799 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002800 * The library has not been previously initialized by psa_crypto_init().
2801 * It is implementation-dependent whether a failure to initialize
2802 * results in this error code.
Gilles Peskineea0fb492018-07-12 17:17:20 +02002803 */
2804psa_status_t psa_key_derivation(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002805 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02002806 psa_algorithm_t alg,
2807 const uint8_t *salt,
2808 size_t salt_length,
2809 const uint8_t *label,
2810 size_t label_length,
2811 size_t capacity);
2812
Gilles Peskine01d718c2018-09-18 12:01:02 +02002813/** Set up a key agreement operation.
2814 *
2815 * A key agreement algorithm takes two inputs: a private key \p private_key
2816 * a public key \p peer_key.
2817 * The result of this function is a byte generator which can
2818 * be used to produce keys and other cryptographic material.
2819 *
Gilles Peskine211a4362018-10-25 22:22:31 +02002820 * The resulting generator always has the maximum capacity permitted by
2821 * the algorithm.
2822 *
Jaeden Amero9e919c62019-01-07 15:41:50 +00002823 * \param[in,out] generator The generator object to set up. It must have
2824 * been initialized as per the documentation for
2825 * #psa_crypto_generator_t and not yet in use.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002826 * \param private_key Handle to the private key to use.
Gilles Peskined171e782018-11-15 17:46:21 +01002827 * \param[in] peer_key Public key of the peer. It must be
2828 * in the same format that psa_import_key()
2829 * accepts. The standard formats for public
2830 * keys are documented in the documentation
2831 * of psa_export_public_key().
Gilles Peskine01d718c2018-09-18 12:01:02 +02002832 * \param peer_key_length Size of \p peer_key in bytes.
2833 * \param alg The key agreement algorithm to compute
2834 * (\c PSA_ALG_XXX value such that
2835 * #PSA_ALG_IS_KEY_AGREEMENT(\p alg) is true).
2836 *
2837 * \retval #PSA_SUCCESS
2838 * Success.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002839 * \retval #PSA_ERROR_INVALID_HANDLE
Gilles Peskine01d718c2018-09-18 12:01:02 +02002840 * \retval #PSA_ERROR_EMPTY_SLOT
2841 * \retval #PSA_ERROR_NOT_PERMITTED
2842 * \retval #PSA_ERROR_INVALID_ARGUMENT
2843 * \c private_key is not compatible with \c alg,
2844 * or \p peer_key is not valid for \c alg or not compatible with
2845 * \c private_key.
2846 * \retval #PSA_ERROR_NOT_SUPPORTED
2847 * \c alg is not supported or is not a key derivation algorithm.
2848 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2849 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2850 * \retval #PSA_ERROR_HARDWARE_FAILURE
2851 * \retval #PSA_ERROR_TAMPERING_DETECTED
2852 */
2853psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
Gilles Peskineae32aac2018-11-30 14:39:32 +01002854 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02002855 const uint8_t *peer_key,
2856 size_t peer_key_length,
2857 psa_algorithm_t alg);
2858
Gilles Peskineea0fb492018-07-12 17:17:20 +02002859/**@}*/
2860
Gilles Peskineedd76872018-07-20 17:42:05 +02002861/** \defgroup random Random generation
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002862 * @{
2863 */
2864
2865/**
2866 * \brief Generate random bytes.
2867 *
2868 * \warning This function **can** fail! Callers MUST check the return status
2869 * and MUST NOT use the content of the output buffer if the return
2870 * status is not #PSA_SUCCESS.
2871 *
2872 * \note To generate a key, use psa_generate_key() instead.
2873 *
Gilles Peskineedd11a12018-07-12 01:08:58 +02002874 * \param[out] output Output buffer for the generated data.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002875 * \param output_size Number of bytes to generate and output.
2876 *
Gilles Peskine28538492018-07-11 17:34:00 +02002877 * \retval #PSA_SUCCESS
2878 * \retval #PSA_ERROR_NOT_SUPPORTED
2879 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2880 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2881 * \retval #PSA_ERROR_HARDWARE_FAILURE
2882 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir0adf0fc2018-09-06 16:24:41 +03002883 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002884 * The library has not been previously initialized by psa_crypto_init().
2885 * It is implementation-dependent whether a failure to initialize
2886 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002887 */
2888psa_status_t psa_generate_random(uint8_t *output,
2889 size_t output_size);
2890
Gilles Peskine4c317f42018-07-12 01:24:09 +02002891/** Extra parameters for RSA key generation.
2892 *
Gilles Peskinebe42f312018-07-13 14:38:15 +02002893 * You may pass a pointer to a structure of this type as the \c extra
Gilles Peskine4c317f42018-07-12 01:24:09 +02002894 * parameter to psa_generate_key().
2895 */
2896typedef struct {
Gilles Peskineedd76872018-07-20 17:42:05 +02002897 uint32_t e; /**< Public exponent value. Default: 65537. */
Gilles Peskine4c317f42018-07-12 01:24:09 +02002898} psa_generate_key_extra_rsa;
2899
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002900/**
2901 * \brief Generate a key or key pair.
2902 *
Gilles Peskineae32aac2018-11-30 14:39:32 +01002903 * \param handle Handle to the slot where the key will be stored.
2904 * This must be a valid slot for a key of the chosen
2905 * type: it must have been obtained by calling
2906 * psa_allocate_key() or psa_create_key() with the
2907 * correct \p type and with a maximum size that is
2908 * compatible with \p bits.
2909 * It must not contain any key material yet.
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002910 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
2911 * \param bits Key size in bits.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002912 * \param[in] extra Extra parameters for key generation. The
Gilles Peskine4e69d7a2018-06-19 20:19:14 +02002913 * interpretation of this parameter depends on
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002914 * \p type. All types support \c NULL to use
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002915 * default parameters. Implementation that support
2916 * the generation of vendor-specific key types
2917 * that allow extra parameters shall document
2918 * the format of these extra parameters and
2919 * the default values. For standard parameters,
2920 * the meaning of \p extra is as follows:
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002921 * - For a symmetric key type (a type such
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002922 * that #PSA_KEY_TYPE_IS_ASYMMETRIC(\p type) is
2923 * false), \p extra must be \c NULL.
Gilles Peskinefa4070c2018-07-12 19:23:03 +02002924 * - For an elliptic curve key type (a type
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002925 * such that #PSA_KEY_TYPE_IS_ECC(\p type) is
2926 * false), \p extra must be \c NULL.
Gilles Peskinedda3bd32018-07-12 19:40:46 +02002927 * - For an RSA key (\p type is
2928 * #PSA_KEY_TYPE_RSA_KEYPAIR), \p extra is an
2929 * optional #psa_generate_key_extra_rsa structure
Gilles Peskine3fa675c2018-07-12 01:31:03 +02002930 * specifying the public exponent. The
2931 * default public exponent used when \p extra
2932 * is \c NULL is 65537.
Gilles Peskine53d991e2018-07-12 01:14:59 +02002933 * \param extra_size Size of the buffer that \p extra
2934 * points to, in bytes. Note that if \p extra is
2935 * \c NULL then \p extra_size must be zero.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002936 *
Gilles Peskine28538492018-07-11 17:34:00 +02002937 * \retval #PSA_SUCCESS
Gilles Peskine23fd2bd2018-12-11 15:51:32 +01002938 * Success.
2939 * If the key is persistent, the key material and the key's metadata
2940 * have been saved to persistent storage.
Gilles Peskineae32aac2018-11-30 14:39:32 +01002941 * \retval #PSA_ERROR_INVALID_HANDLE
2942 * \retval #PSA_ERROR_OCCUPIED_SLOT
2943 * There is already a key in the specified slot.
Gilles Peskine28538492018-07-11 17:34:00 +02002944 * \retval #PSA_ERROR_NOT_SUPPORTED
2945 * \retval #PSA_ERROR_INVALID_ARGUMENT
2946 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
2947 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
2948 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
2949 * \retval #PSA_ERROR_HARDWARE_FAILURE
2950 * \retval #PSA_ERROR_TAMPERING_DETECTED
itayzafrir90d8c7a2018-09-12 11:44:52 +03002951 * \retval #PSA_ERROR_BAD_STATE
itayzafrir18617092018-09-16 12:22:41 +03002952 * The library has not been previously initialized by psa_crypto_init().
2953 * It is implementation-dependent whether a failure to initialize
2954 * results in this error code.
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002955 */
Gilles Peskineae32aac2018-11-30 14:39:32 +01002956psa_status_t psa_generate_key(psa_key_handle_t handle,
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002957 psa_key_type_t type,
2958 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02002959 const void *extra,
2960 size_t extra_size);
Gilles Peskine9e7dc712018-03-28 14:18:50 +02002961
2962/**@}*/
2963
Gilles Peskinee59236f2018-01-27 23:32:46 +01002964#ifdef __cplusplus
2965}
2966#endif
2967
Gilles Peskine0cad07c2018-06-27 19:49:02 +02002968/* The file "crypto_sizes.h" contains definitions for size calculation
2969 * macros whose definitions are implementation-specific. */
2970#include "crypto_sizes.h"
2971
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002972/* The file "crypto_struct.h" contains definitions for
2973 * implementation-specific structs that are declared above. */
2974#include "crypto_struct.h"
2975
2976/* The file "crypto_extra.h" contains vendor-specific definitions. This
2977 * can include vendor-defined algorithms, extra functions, etc. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01002978#include "crypto_extra.h"
2979
2980#endif /* PSA_CRYPTO_H */