blob: 39e5688c82638c80819f6d05838116bb2a62fc5f [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 */
39const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
40 psa_algorithm_t alg, psa_key_type_t key_type, size_t key_bits,
41 mbedtls_cipher_id_t *cipher_id );
42
Ronald Crond6d28882020-12-14 14:56:02 +010043/**
44 * \brief Set the key for a multipart symmetric encryption operation.
45 *
46 * \note The signature of this function is that of a PSA driver
47 * cipher_encrypt_setup entry point. This function behaves as a
48 * cipher_encrypt_setup entry point as defined in the PSA driver
49 * interface specification for transparent drivers.
50 *
51 * \param[in,out] operation The operation object to set up. It has been
52 * initialized as per the documentation for
53 * #psa_cipher_operation_t and not yet in use.
54 * \param[in] attributes The attributes of the key to use for the
55 * operation.
56 * \param[in] key_buffer The buffer containing the key context.
57 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
58 * \param[in] alg The cipher algorithm to compute
59 * (\c PSA_ALG_XXX value such that
60 * #PSA_ALG_IS_CIPHER(\p alg) is true).
61 *
62 * \retval #PSA_SUCCESS
63 * \retval #PSA_ERROR_NOT_SUPPORTED
64 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
65 * \retval #PSA_ERROR_CORRUPTION_DETECTED
66 */
67psa_status_t mbedtls_psa_cipher_encrypt_setup(
Ronald Cron6e412a72021-03-10 09:58:47 +010068 mbedtls_psa_cipher_operation_t *operation,
Ronald Crond6d28882020-12-14 14:56:02 +010069 const psa_key_attributes_t *attributes,
70 const uint8_t *key_buffer, size_t key_buffer_size,
71 psa_algorithm_t alg );
72
73/**
74 * \brief Set the key for a multipart symmetric decryption operation.
75 *
76 * \note The signature of this function is that of a PSA driver
77 * cipher_decrypt_setup entry point. This function behaves as a
78 * cipher_decrypt_setup entry point as defined in the PSA driver
79 * interface specification for transparent drivers.
80 *
81 * \param[in,out] operation The operation object to set up. It has been
82 * initialized as per the documentation for
83 * #psa_cipher_operation_t and not yet in use.
84 * \param[in] attributes The attributes of the key to use for the
85 * operation.
86 * \param[in] key_buffer The buffer containing the key context.
87 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
88 * \param[in] alg The cipher algorithm to compute
89 * (\c PSA_ALG_XXX value such that
90 * #PSA_ALG_IS_CIPHER(\p alg) is true).
91 *
92 * \retval #PSA_SUCCESS
93 * \retval #PSA_ERROR_NOT_SUPPORTED
94 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
95 * \retval #PSA_ERROR_CORRUPTION_DETECTED
96 */
97psa_status_t mbedtls_psa_cipher_decrypt_setup(
Ronald Cron6e412a72021-03-10 09:58:47 +010098 mbedtls_psa_cipher_operation_t *operation,
Ronald Crond6d28882020-12-14 14:56:02 +010099 const psa_key_attributes_t *attributes,
100 const uint8_t *key_buffer, size_t key_buffer_size,
101 psa_algorithm_t alg );
102
Ronald Cron6d051732020-10-01 14:10:20 +0200103/** Set the IV for a symmetric encryption or decryption operation.
104 *
105 * This function sets the IV (initialization vector), nonce
106 * or initial counter value for the encryption or decryption operation.
107 *
108 * \note The signature of this function is that of a PSA driver
109 * cipher_set_iv entry point. This function behaves as a
110 * cipher_set_iv entry point as defined in the PSA driver
111 * interface specification for transparent drivers.
112 *
113 * \param[in,out] operation Active cipher operation.
114 * \param[in] iv Buffer containing the IV to use.
Ronald Crona0d68172021-03-26 10:15:08 +0100115 * \param[in] iv_length Size of the IV in bytes. It is guaranteed by
116 * the core to be less or equal to
117 * PSA_CIPHER_IV_MAX_SIZE.
Ronald Cron6d051732020-10-01 14:10:20 +0200118 *
119 * \retval #PSA_SUCCESS
120 * \retval #PSA_ERROR_INVALID_ARGUMENT
121 * The size of \p iv is not acceptable for the chosen algorithm,
122 * or the chosen algorithm does not use an IV.
123 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
124 */
125psa_status_t mbedtls_psa_cipher_set_iv(
Ronald Cron6e412a72021-03-10 09:58:47 +0100126 mbedtls_psa_cipher_operation_t *operation,
Ronald Cron6d051732020-10-01 14:10:20 +0200127 const uint8_t *iv, size_t iv_length );
128
129/** Encrypt or decrypt a message fragment in an active cipher operation.
130 *
131 * \note The signature of this function is that of a PSA driver
132 * cipher_update entry point. This function behaves as a
133 * cipher_update entry point as defined in the PSA driver
134 * interface specification for transparent drivers.
135 *
136 * \param[in,out] operation Active cipher operation.
137 * \param[in] input Buffer containing the message fragment to
138 * encrypt or decrypt.
139 * \param[in] input_length Size of the \p input buffer in bytes.
140 * \param[out] output Buffer where the output is to be written.
141 * \param[in] output_size Size of the \p output buffer in bytes.
142 * \param[out] output_length On success, the number of bytes
143 * that make up the returned output.
144 *
145 * \retval #PSA_SUCCESS
146 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
147 * The size of the \p output buffer is too small.
148 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
149 */
150psa_status_t mbedtls_psa_cipher_update(
Ronald Cron6e412a72021-03-10 09:58:47 +0100151 mbedtls_psa_cipher_operation_t *operation,
Ronald Cron6d051732020-10-01 14:10:20 +0200152 const uint8_t *input, size_t input_length,
153 uint8_t *output, size_t output_size, size_t *output_length );
154
155/** Finish encrypting or decrypting a message in a cipher operation.
156 *
157 * \note The signature of this function is that of a PSA driver
158 * cipher_finish entry point. This function behaves as a
159 * cipher_finish entry point as defined in the PSA driver
160 * interface specification for transparent drivers.
161 *
162 * \param[in,out] operation Active cipher operation.
163 * \param[out] output Buffer where the output is to be written.
164 * \param[in] output_size Size of the \p output buffer in bytes.
165 * \param[out] output_length On success, the number of bytes
166 * that make up the returned output.
167 *
168 * \retval #PSA_SUCCESS
169 * \retval #PSA_ERROR_INVALID_ARGUMENT
170 * The total input size passed to this operation is not valid for
171 * this particular algorithm. For example, the algorithm is a based
172 * on block cipher and requires a whole number of blocks, but the
173 * total input size is not a multiple of the block size.
174 * \retval #PSA_ERROR_INVALID_PADDING
175 * This is a decryption operation for an algorithm that includes
176 * padding, and the ciphertext does not contain valid padding.
177 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
178 * The size of the \p output buffer is too small.
179 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
180 */
181psa_status_t mbedtls_psa_cipher_finish(
Ronald Cron6e412a72021-03-10 09:58:47 +0100182 mbedtls_psa_cipher_operation_t *operation,
Ronald Cron6d051732020-10-01 14:10:20 +0200183 uint8_t *output, size_t output_size, size_t *output_length );
184
185/** Abort a cipher operation.
186 *
187 * Aborting an operation frees all associated resources except for the
188 * \p operation structure itself. Once aborted, the operation object
189 * can be reused for another operation.
190 *
191 * \note The signature of this function is that of a PSA driver
192 * cipher_abort entry point. This function behaves as a
193 * cipher_abort entry point as defined in the PSA driver
194 * interface specification for transparent drivers.
195 *
196 * \param[in,out] operation Initialized cipher operation.
197 *
198 * \retval #PSA_SUCCESS
199 */
Ronald Cron6e412a72021-03-10 09:58:47 +0100200psa_status_t mbedtls_psa_cipher_abort( mbedtls_psa_cipher_operation_t *operation );
Ronald Cron6d051732020-10-01 14:10:20 +0200201
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100202/** Encrypt a message using a symmetric cipher.
203 *
204 * \note The signature of this function is that of a PSA driver
205 * cipher_encrypt entry point. This function behaves as a
206 * cipher_encrypt entry point as defined in the PSA driver
207 * interface specification for transparent drivers.
208 *
209 * \param[in] attributes The attributes of the key to use for the
210 * operation.
211 * \param[in] key_buffer The buffer containing the key context.
212 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
213 * \param[in] alg The cipher algorithm to compute
214 * (\c PSA_ALG_XXX value such that
215 * #PSA_ALG_IS_CIPHER(\p alg) is true).
gabor-mezei-arm90fceea2021-06-25 15:43:07 +0200216 * \param[in] input Buffer containing the message to encrypt.
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100217 * \param[in] input_length Size of the \p input buffer in bytes.
218 * \param[in,out] output Buffer where the output is to be written.
gabor-mezei-arm90fceea2021-06-25 15:43:07 +0200219 * The core has generated and written the IV
220 * at the beginning of this buffer before
221 * this function is called. The size of the IV
222 * is PSA_CIPHER_IV_LENGTH( key_type, alg ) where
223 * \c key_type is the type of the key identified
224 * by \p key and \p alg is the cipher algorithm
225 * to compute.
226 * \param[in] output_size Size of the \p output buffer in bytes. The core
227 * has checked that this size is greater or equal to
228 * PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_length )
229 * where \c key_type is the type of the key
230 * identified by \p key, \p alg is the cipher
231 * algorithm to compute and \p input_length is the
232 * size of the \p input buffer.
233 * \param[out] output_length On success, the number of bytes that make up
234 * the returned output. Initialized to zero
235 * by the core.
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100236 *
237 * \retval #PSA_SUCCESS
238 * \retval #PSA_ERROR_NOT_SUPPORTED
239 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
240 * \retval #PSA_ERROR_CORRUPTION_DETECTED
241 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
242 * The size of the \p output buffer is too small.
243 * \retval #PSA_ERROR_INVALID_ARGUMENT
244 * The size of \p iv is not acceptable for the chosen algorithm,
245 * or the chosen algorithm does not use an IV.
246 * The total input size passed to this operation is not valid for
247 * this particular algorithm. For example, the algorithm is a based
248 * on block cipher and requires a whole number of blocks, but the
249 * total input size is not a multiple of the block size.
250 * \retval #PSA_ERROR_INVALID_PADDING
251 * This is a decryption operation for an algorithm that includes
252 * padding, and the ciphertext does not contain valid padding.
253 */
254psa_status_t mbedtls_psa_cipher_encrypt( const psa_key_attributes_t *attributes,
255 const uint8_t *key_buffer,
256 size_t key_buffer_size,
257 psa_algorithm_t alg,
258 const uint8_t *input,
259 size_t input_length,
260 uint8_t *output,
261 size_t output_size,
262 size_t *output_length );
263
264/** Decrypt a message using a symmetric cipher.
265 *
266 * \note The signature of this function is that of a PSA driver
267 * cipher_decrypt entry point. This function behaves as a
268 * cipher_decrypt entry point as defined in the PSA driver
269 * interface specification for transparent drivers.
270 *
271 * \param[in] attributes The attributes of the key to use for the
272 * operation.
273 * \param[in] key_buffer The buffer containing the key context.
274 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
275 * \param[in] alg The cipher algorithm to compute
276 * (\c PSA_ALG_XXX value such that
277 * #PSA_ALG_IS_CIPHER(\p alg) is true).
gabor-mezei-arm90fceea2021-06-25 15:43:07 +0200278 * \param[in] input Buffer containing the iv and the ciphertext.
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100279 * \param[in] input_length Size of the \p input buffer in bytes.
280 * \param[out] output Buffer where the output is to be written.
281 * \param[in] output_size Size of the \p output buffer in bytes.
gabor-mezei-arm90fceea2021-06-25 15:43:07 +0200282 * The core has checked that this size is
283 * greater or equal to
284 * PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_length )
285 * where \c key_type is the type of the key
286 * identified by \p key, \p alg is the cipher
287 * algorithm to compute and \p input_length is the
288 * size of the \p input buffer.
289 * \param[out] output_length On success, the number of bytes that make up
290 * the returned output. Initialized to zero
291 * by the core.
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100292 *
293 * \retval #PSA_SUCCESS
294 * \retval #PSA_ERROR_NOT_SUPPORTED
295 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
296 * \retval #PSA_ERROR_CORRUPTION_DETECTED
297 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
298 * The size of the \p output buffer is too small.
299 * \retval #PSA_ERROR_INVALID_ARGUMENT
300 * The size of \p iv is not acceptable for the chosen algorithm,
301 * or the chosen algorithm does not use an IV.
302 * The total input size passed to this operation is not valid for
303 * this particular algorithm. For example, the algorithm is a based
304 * on block cipher and requires a whole number of blocks, but the
305 * total input size is not a multiple of the block size.
306 * \retval #PSA_ERROR_INVALID_PADDING
307 * This is a decryption operation for an algorithm that includes
308 * padding, and the ciphertext does not contain valid padding.
309 */
310psa_status_t mbedtls_psa_cipher_decrypt( const psa_key_attributes_t *attributes,
311 const uint8_t *key_buffer,
312 size_t key_buffer_size,
313 psa_algorithm_t alg,
314 const uint8_t *input,
315 size_t input_length,
316 uint8_t *output,
317 size_t output_size,
318 size_t *output_length );
319
Ronald Cron3522e322021-03-12 11:08:49 +0100320/*
321 * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY.
322 */
323
324#if defined(PSA_CRYPTO_DRIVER_TEST)
325psa_status_t mbedtls_transparent_test_driver_cipher_encrypt_setup(
326 mbedtls_psa_cipher_operation_t *operation,
327 const psa_key_attributes_t *attributes,
328 const uint8_t *key_buffer, size_t key_buffer_size,
329 psa_algorithm_t alg );
330
331psa_status_t mbedtls_transparent_test_driver_cipher_decrypt_setup(
332 mbedtls_psa_cipher_operation_t *operation,
333 const psa_key_attributes_t *attributes,
334 const uint8_t *key_buffer, size_t key_buffer_size,
335 psa_algorithm_t alg );
336
Ronald Cron3522e322021-03-12 11:08:49 +0100337psa_status_t mbedtls_transparent_test_driver_cipher_set_iv(
338 mbedtls_psa_cipher_operation_t *operation,
339 const uint8_t *iv, size_t iv_length );
340
341psa_status_t mbedtls_transparent_test_driver_cipher_update(
342 mbedtls_psa_cipher_operation_t *operation,
343 const uint8_t *input, size_t input_length,
344 uint8_t *output, size_t output_size, size_t *output_length );
345
346psa_status_t mbedtls_transparent_test_driver_cipher_finish(
347 mbedtls_psa_cipher_operation_t *operation,
348 uint8_t *output, size_t output_size, size_t *output_length );
349
350psa_status_t mbedtls_transparent_test_driver_cipher_abort(
351 mbedtls_psa_cipher_operation_t *operation );
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100352
353psa_status_t mbedtls_transparent_test_driver_cipher_encrypt(
354 const psa_key_attributes_t *attributes,
355 const uint8_t *key_buffer,
356 size_t key_buffer_size,
357 psa_algorithm_t alg,
358 const uint8_t *input,
359 size_t input_length,
360 uint8_t *output,
361 size_t output_size,
362 size_t *output_length );
363
364psa_status_t mbedtls_transparent_test_driver_cipher_decrypt(
365 const psa_key_attributes_t *attributes,
366 const uint8_t *key_buffer,
367 size_t key_buffer_size,
368 psa_algorithm_t alg,
369 const uint8_t *input,
370 size_t input_length,
371 uint8_t *output,
372 size_t output_size,
373 size_t *output_length );
Ronald Cron3522e322021-03-12 11:08:49 +0100374#endif /* PSA_CRYPTO_DRIVER_TEST */
375
Ronald Cron0ff57952021-03-08 16:46:35 +0100376#endif /* PSA_CRYPTO_CIPHER_H */