blob: 8c41cabe703bc34f54acd269943ad4d6a1140b70 [file] [log] [blame]
Ronald Cron0ff57952021-03-08 16:46:35 +01001/*
2 * PSA cipher driver entry points
3 */
4/*
5 * Copyright The Mbed TLS Contributors
6 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21#ifndef PSA_CRYPTO_CIPHER_H
22#define PSA_CRYPTO_CIPHER_H
23
Ronald Cron75e6ae22021-03-17 14:46:05 +010024#include <mbedtls/cipher.h>
Ronald Cron0ff57952021-03-08 16:46:35 +010025#include <psa/crypto.h>
26
Ronald Cron75e6ae22021-03-17 14:46:05 +010027/** Get Mbed TLS cipher information given the cipher algorithm PSA identifier
28 * as well as the PSA type and size of the key to be used with the cipher
29 * algorithm.
30 *
31 * \param alg PSA cipher algorithm identifier
32 * \param key_type PSA key type
33 * \param key_bits Size of the key in bits
34 * \param[out] cipher_id Mbed TLS cipher algorithm identifier
35 *
36 * \return The Mbed TLS cipher information of the cipher algorithm.
37 * \c NULL if the PSA cipher algorithm is not supported.
38 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020039const mbedtls_cipher_info_t *
40mbedtls_cipher_info_from_psa(psa_algorithm_t alg,
41 psa_key_type_t key_type,
42 size_t key_bits,
43 mbedtls_cipher_id_t *cipher_id);
Ronald Cron75e6ae22021-03-17 14:46:05 +010044
Ronald Crond6d28882020-12-14 14:56:02 +010045/**
46 * \brief Set the key for a multipart symmetric encryption operation.
47 *
48 * \note The signature of this function is that of a PSA driver
49 * cipher_encrypt_setup entry point. This function behaves as a
50 * cipher_encrypt_setup entry point as defined in the PSA driver
51 * interface specification for transparent drivers.
52 *
53 * \param[in,out] operation The operation object to set up. It has been
54 * initialized as per the documentation for
55 * #psa_cipher_operation_t and not yet in use.
56 * \param[in] attributes The attributes of the key to use for the
57 * operation.
58 * \param[in] key_buffer The buffer containing the key context.
59 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
60 * \param[in] alg The cipher algorithm to compute
61 * (\c PSA_ALG_XXX value such that
62 * #PSA_ALG_IS_CIPHER(\p alg) is true).
63 *
64 * \retval #PSA_SUCCESS
65 * \retval #PSA_ERROR_NOT_SUPPORTED
66 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
67 * \retval #PSA_ERROR_CORRUPTION_DETECTED
68 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020069psa_status_t
70mbedtls_psa_cipher_encrypt_setup(mbedtls_psa_cipher_operation_t *operation,
71 const psa_key_attributes_t *attributes,
72 const uint8_t *key_buffer,
73 size_t key_buffer_size,
74 psa_algorithm_t alg);
Ronald Crond6d28882020-12-14 14:56:02 +010075
76/**
77 * \brief Set the key for a multipart symmetric decryption operation.
78 *
79 * \note The signature of this function is that of a PSA driver
80 * cipher_decrypt_setup entry point. This function behaves as a
81 * cipher_decrypt_setup entry point as defined in the PSA driver
82 * interface specification for transparent drivers.
83 *
84 * \param[in,out] operation The operation object to set up. It has been
85 * initialized as per the documentation for
86 * #psa_cipher_operation_t and not yet in use.
87 * \param[in] attributes The attributes of the key to use for the
88 * operation.
89 * \param[in] key_buffer The buffer containing the key context.
90 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
91 * \param[in] alg The cipher algorithm to compute
92 * (\c PSA_ALG_XXX value such that
93 * #PSA_ALG_IS_CIPHER(\p alg) is true).
94 *
95 * \retval #PSA_SUCCESS
96 * \retval #PSA_ERROR_NOT_SUPPORTED
97 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
98 * \retval #PSA_ERROR_CORRUPTION_DETECTED
99 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200100psa_status_t
101mbedtls_psa_cipher_decrypt_setup(mbedtls_psa_cipher_operation_t *operation,
102 const psa_key_attributes_t *attributes,
103 const uint8_t *key_buffer,
104 size_t key_buffer_size,
105 psa_algorithm_t alg);
Ronald Crond6d28882020-12-14 14:56:02 +0100106
Ronald Cron6d051732020-10-01 14:10:20 +0200107/** Set the IV for a symmetric encryption or decryption operation.
108 *
109 * This function sets the IV (initialization vector), nonce
110 * or initial counter value for the encryption or decryption operation.
111 *
112 * \note The signature of this function is that of a PSA driver
113 * cipher_set_iv entry point. This function behaves as a
114 * cipher_set_iv entry point as defined in the PSA driver
115 * interface specification for transparent drivers.
116 *
117 * \param[in,out] operation Active cipher operation.
118 * \param[in] iv Buffer containing the IV to use.
Ronald Crona0d68172021-03-26 10:15:08 +0100119 * \param[in] iv_length Size of the IV in bytes. It is guaranteed by
120 * the core to be less or equal to
121 * PSA_CIPHER_IV_MAX_SIZE.
Ronald Cron6d051732020-10-01 14:10:20 +0200122 *
123 * \retval #PSA_SUCCESS
124 * \retval #PSA_ERROR_INVALID_ARGUMENT
125 * The size of \p iv is not acceptable for the chosen algorithm,
126 * or the chosen algorithm does not use an IV.
127 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
128 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200129psa_status_t
130mbedtls_psa_cipher_set_iv(mbedtls_psa_cipher_operation_t *operation,
131 const uint8_t *iv,
132 size_t iv_length);
Ronald Cron6d051732020-10-01 14:10:20 +0200133
134/** Encrypt or decrypt a message fragment in an active cipher operation.
135 *
136 * \note The signature of this function is that of a PSA driver
137 * cipher_update entry point. This function behaves as a
138 * cipher_update entry point as defined in the PSA driver
139 * interface specification for transparent drivers.
140 *
141 * \param[in,out] operation Active cipher operation.
142 * \param[in] input Buffer containing the message fragment to
143 * encrypt or decrypt.
144 * \param[in] input_length Size of the \p input buffer in bytes.
145 * \param[out] output Buffer where the output is to be written.
146 * \param[in] output_size Size of the \p output buffer in bytes.
147 * \param[out] output_length On success, the number of bytes
148 * that make up the returned output.
149 *
150 * \retval #PSA_SUCCESS
151 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
152 * The size of the \p output buffer is too small.
153 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
154 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200155psa_status_t
156mbedtls_psa_cipher_update(mbedtls_psa_cipher_operation_t *operation,
157 const uint8_t *input,
158 size_t input_length,
159 uint8_t *output,
160 size_t output_size,
161 size_t *output_length);
Ronald Cron6d051732020-10-01 14:10:20 +0200162
163/** Finish encrypting or decrypting a message in a cipher operation.
164 *
165 * \note The signature of this function is that of a PSA driver
166 * cipher_finish entry point. This function behaves as a
167 * cipher_finish entry point as defined in the PSA driver
168 * interface specification for transparent drivers.
169 *
170 * \param[in,out] operation Active cipher operation.
171 * \param[out] output Buffer where the output is to be written.
172 * \param[in] output_size Size of the \p output buffer in bytes.
173 * \param[out] output_length On success, the number of bytes
174 * that make up the returned output.
175 *
176 * \retval #PSA_SUCCESS
177 * \retval #PSA_ERROR_INVALID_ARGUMENT
178 * The total input size passed to this operation is not valid for
179 * this particular algorithm. For example, the algorithm is a based
180 * on block cipher and requires a whole number of blocks, but the
181 * total input size is not a multiple of the block size.
182 * \retval #PSA_ERROR_INVALID_PADDING
183 * This is a decryption operation for an algorithm that includes
184 * padding, and the ciphertext does not contain valid padding.
185 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
186 * The size of the \p output buffer is too small.
187 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
188 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200189psa_status_t
190mbedtls_psa_cipher_finish(mbedtls_psa_cipher_operation_t *operation,
191 uint8_t *output,
192 size_t output_size,
193 size_t *output_length);
Ronald Cron6d051732020-10-01 14:10:20 +0200194
195/** Abort a cipher operation.
196 *
197 * Aborting an operation frees all associated resources except for the
198 * \p operation structure itself. Once aborted, the operation object
199 * can be reused for another operation.
200 *
201 * \note The signature of this function is that of a PSA driver
202 * cipher_abort entry point. This function behaves as a
203 * cipher_abort entry point as defined in the PSA driver
204 * interface specification for transparent drivers.
205 *
206 * \param[in,out] operation Initialized cipher operation.
207 *
208 * \retval #PSA_SUCCESS
209 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200210psa_status_t
211mbedtls_psa_cipher_abort(mbedtls_psa_cipher_operation_t *operation);
Ronald Cron6d051732020-10-01 14:10:20 +0200212
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100213/** Encrypt a message using a symmetric cipher.
214 *
215 * \note The signature of this function is that of a PSA driver
216 * cipher_encrypt entry point. This function behaves as a
217 * cipher_encrypt entry point as defined in the PSA driver
218 * interface specification for transparent drivers.
219 *
220 * \param[in] attributes The attributes of the key to use for the
221 * operation.
222 * \param[in] key_buffer The buffer containing the key context.
223 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
224 * \param[in] alg The cipher algorithm to compute
225 * (\c PSA_ALG_XXX value such that
226 * #PSA_ALG_IS_CIPHER(\p alg) is true).
gabor-mezei-arm90fceea2021-06-25 15:43:07 +0200227 * \param[in] input Buffer containing the message to encrypt.
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100228 * \param[in] input_length Size of the \p input buffer in bytes.
229 * \param[in,out] output Buffer where the output is to be written.
gabor-mezei-arm90fceea2021-06-25 15:43:07 +0200230 * The core has generated and written the IV
231 * at the beginning of this buffer before
232 * this function is called. The size of the IV
233 * is PSA_CIPHER_IV_LENGTH( key_type, alg ) where
234 * \c key_type is the type of the key identified
235 * by \p key and \p alg is the cipher algorithm
236 * to compute.
gabor-mezei-arm3f860e42021-06-29 16:39:49 +0200237 * \param[in] output_size Size of the \p output buffer in bytes.
gabor-mezei-arm90fceea2021-06-25 15:43:07 +0200238 * \param[out] output_length On success, the number of bytes that make up
239 * the returned output. Initialized to zero
240 * by the core.
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100241 *
242 * \retval #PSA_SUCCESS
243 * \retval #PSA_ERROR_NOT_SUPPORTED
244 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
245 * \retval #PSA_ERROR_CORRUPTION_DETECTED
246 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
247 * The size of the \p output buffer is too small.
248 * \retval #PSA_ERROR_INVALID_ARGUMENT
249 * The size of \p iv is not acceptable for the chosen algorithm,
250 * or the chosen algorithm does not use an IV.
251 * The total input size passed to this operation is not valid for
252 * this particular algorithm. For example, the algorithm is a based
253 * on block cipher and requires a whole number of blocks, but the
254 * total input size is not a multiple of the block size.
255 * \retval #PSA_ERROR_INVALID_PADDING
256 * This is a decryption operation for an algorithm that includes
257 * padding, and the ciphertext does not contain valid padding.
258 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200259psa_status_t mbedtls_psa_cipher_encrypt(const psa_key_attributes_t *attributes,
260 const uint8_t *key_buffer,
261 size_t key_buffer_size,
262 psa_algorithm_t alg,
263 const uint8_t *input,
264 size_t input_length,
265 uint8_t *output,
266 size_t output_size,
267 size_t *output_length);
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100268
269/** Decrypt a message using a symmetric cipher.
270 *
271 * \note The signature of this function is that of a PSA driver
272 * cipher_decrypt entry point. This function behaves as a
273 * cipher_decrypt entry point as defined in the PSA driver
274 * interface specification for transparent drivers.
275 *
276 * \param[in] attributes The attributes of the key to use for the
277 * operation.
278 * \param[in] key_buffer The buffer containing the key context.
279 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
280 * \param[in] alg The cipher algorithm to compute
281 * (\c PSA_ALG_XXX value such that
282 * #PSA_ALG_IS_CIPHER(\p alg) is true).
gabor-mezei-arm90fceea2021-06-25 15:43:07 +0200283 * \param[in] input Buffer containing the iv and the ciphertext.
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100284 * \param[in] input_length Size of the \p input buffer in bytes.
285 * \param[out] output Buffer where the output is to be written.
286 * \param[in] output_size Size of the \p output buffer in bytes.
gabor-mezei-arm90fceea2021-06-25 15:43:07 +0200287 * \param[out] output_length On success, the number of bytes that make up
288 * the returned output. Initialized to zero
289 * by the core.
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100290 *
291 * \retval #PSA_SUCCESS
292 * \retval #PSA_ERROR_NOT_SUPPORTED
293 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
294 * \retval #PSA_ERROR_CORRUPTION_DETECTED
295 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
296 * The size of the \p output buffer is too small.
297 * \retval #PSA_ERROR_INVALID_ARGUMENT
298 * The size of \p iv is not acceptable for the chosen algorithm,
299 * or the chosen algorithm does not use an IV.
300 * The total input size passed to this operation is not valid for
301 * this particular algorithm. For example, the algorithm is a based
302 * on block cipher and requires a whole number of blocks, but the
303 * total input size is not a multiple of the block size.
304 * \retval #PSA_ERROR_INVALID_PADDING
305 * This is a decryption operation for an algorithm that includes
306 * padding, and the ciphertext does not contain valid padding.
307 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200308psa_status_t mbedtls_psa_cipher_decrypt(const psa_key_attributes_t *attributes,
309 const uint8_t *key_buffer,
310 size_t key_buffer_size,
311 psa_algorithm_t alg,
312 const uint8_t *input,
313 size_t input_length,
314 uint8_t *output,
315 size_t output_size,
316 size_t *output_length);
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100317
Ronald Cron3522e322021-03-12 11:08:49 +0100318/*
319 * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY.
320 */
321
322#if defined(PSA_CRYPTO_DRIVER_TEST)
323psa_status_t mbedtls_transparent_test_driver_cipher_encrypt_setup(
324 mbedtls_psa_cipher_operation_t *operation,
325 const psa_key_attributes_t *attributes,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200326 const uint8_t *key_buffer,
327 size_t key_buffer_size,
328 psa_algorithm_t alg);
Ronald Cron3522e322021-03-12 11:08:49 +0100329
330psa_status_t mbedtls_transparent_test_driver_cipher_decrypt_setup(
331 mbedtls_psa_cipher_operation_t *operation,
332 const psa_key_attributes_t *attributes,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200333 const uint8_t *key_buffer,
334 size_t key_buffer_size,
335 psa_algorithm_t alg);
Ronald Cron3522e322021-03-12 11:08:49 +0100336
Ronald Cron3522e322021-03-12 11:08:49 +0100337psa_status_t mbedtls_transparent_test_driver_cipher_set_iv(
338 mbedtls_psa_cipher_operation_t *operation,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200339 const uint8_t *iv,
340 size_t iv_length);
Ronald Cron3522e322021-03-12 11:08:49 +0100341
342psa_status_t mbedtls_transparent_test_driver_cipher_update(
343 mbedtls_psa_cipher_operation_t *operation,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200344 const uint8_t *input,
345 size_t input_length,
346 uint8_t *output,
347 size_t output_size,
348 size_t *output_length);
Ronald Cron3522e322021-03-12 11:08:49 +0100349
350psa_status_t mbedtls_transparent_test_driver_cipher_finish(
351 mbedtls_psa_cipher_operation_t *operation,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200352 uint8_t *output,
353 size_t output_size,
354 size_t *output_length);
Ronald Cron3522e322021-03-12 11:08:49 +0100355
356psa_status_t mbedtls_transparent_test_driver_cipher_abort(
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200357 mbedtls_psa_cipher_operation_t *operation);
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100358
359psa_status_t mbedtls_transparent_test_driver_cipher_encrypt(
360 const psa_key_attributes_t *attributes,
361 const uint8_t *key_buffer,
362 size_t key_buffer_size,
363 psa_algorithm_t alg,
364 const uint8_t *input,
365 size_t input_length,
366 uint8_t *output,
367 size_t output_size,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200368 size_t *output_length);
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100369
370psa_status_t mbedtls_transparent_test_driver_cipher_decrypt(
371 const psa_key_attributes_t *attributes,
372 const uint8_t *key_buffer,
373 size_t key_buffer_size,
374 psa_algorithm_t alg,
375 const uint8_t *input,
376 size_t input_length,
377 uint8_t *output,
378 size_t output_size,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200379 size_t *output_length);
Ronald Cron3522e322021-03-12 11:08:49 +0100380#endif /* PSA_CRYPTO_DRIVER_TEST */
381
Ronald Cron0ff57952021-03-08 16:46:35 +0100382#endif /* PSA_CRYPTO_CIPHER_H */