blob: 933092dddd6e050bc472fb0502cb62764f1f79e0 [file] [log] [blame]
Ronald Cron0ff57952021-03-08 16:46:35 +01001/*
Dave Rodgman0877dc82022-11-02 09:29:35 +00002 * PSA cipher driver entry points and associated auxiliary functions
Ronald Cron0ff57952021-03-08 16:46:35 +01003 */
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
Valerio Setti2c2aded2023-08-25 09:22:19 +020027#if defined(MBEDTLS_CIPHER_C)
Dave Rodgman16304472022-11-02 09:25:38 +000028/** Get Mbed TLS cipher information given the cipher algorithm PSA identifier
29 * as well as the PSA type and size of the key to be used with the cipher
30 * algorithm.
31 *
32 * \param alg PSA cipher algorithm identifier
33 * \param key_type PSA key type
34 * \param key_bits Size of the key in bits
35 * \param[out] cipher_id Mbed TLS cipher algorithm identifier
36 *
37 * \return The Mbed TLS cipher information of the cipher algorithm.
38 * \c NULL if the PSA cipher algorithm is not supported.
39 */
40const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
41 psa_algorithm_t alg, psa_key_type_t key_type, size_t key_bits,
Gilles Peskine449bd832023-01-11 14:50:10 +010042 mbedtls_cipher_id_t *cipher_id);
Valerio Setti2c2aded2023-08-25 09:22:19 +020043#endif /* MBEDTLS_CIPHER_C */
Dave Rodgman16304472022-11-02 09:25:38 +000044
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 *
Gilles Peskineed733552023-02-14 19:21:09 +010064 * \retval #PSA_SUCCESS \emptydescription
65 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
66 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
67 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Ronald Crond6d28882020-12-14 14:56:02 +010068 */
69psa_status_t mbedtls_psa_cipher_encrypt_setup(
Ronald Cron6e412a72021-03-10 09:58:47 +010070 mbedtls_psa_cipher_operation_t *operation,
Ronald Crond6d28882020-12-14 14:56:02 +010071 const psa_key_attributes_t *attributes,
72 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +010073 psa_algorithm_t alg);
Ronald Crond6d28882020-12-14 14:56:02 +010074
75/**
76 * \brief Set the key for a multipart symmetric decryption operation.
77 *
78 * \note The signature of this function is that of a PSA driver
79 * cipher_decrypt_setup entry point. This function behaves as a
80 * cipher_decrypt_setup entry point as defined in the PSA driver
81 * interface specification for transparent drivers.
82 *
83 * \param[in,out] operation The operation object to set up. It has been
84 * initialized as per the documentation for
85 * #psa_cipher_operation_t and not yet in use.
86 * \param[in] attributes The attributes of the key to use for the
87 * operation.
88 * \param[in] key_buffer The buffer containing the key context.
89 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
90 * \param[in] alg The cipher algorithm to compute
91 * (\c PSA_ALG_XXX value such that
92 * #PSA_ALG_IS_CIPHER(\p alg) is true).
93 *
Gilles Peskineed733552023-02-14 19:21:09 +010094 * \retval #PSA_SUCCESS \emptydescription
95 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
96 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
97 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Ronald Crond6d28882020-12-14 14:56:02 +010098 */
99psa_status_t mbedtls_psa_cipher_decrypt_setup(
Ronald Cron6e412a72021-03-10 09:58:47 +0100100 mbedtls_psa_cipher_operation_t *operation,
Ronald Crond6d28882020-12-14 14:56:02 +0100101 const psa_key_attributes_t *attributes,
102 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 psa_algorithm_t alg);
Ronald Crond6d28882020-12-14 14:56:02 +0100104
Ronald Cron6d051732020-10-01 14:10:20 +0200105/** Set the IV for a symmetric encryption or decryption operation.
106 *
107 * This function sets the IV (initialization vector), nonce
108 * or initial counter value for the encryption or decryption operation.
109 *
110 * \note The signature of this function is that of a PSA driver
111 * cipher_set_iv entry point. This function behaves as a
112 * cipher_set_iv entry point as defined in the PSA driver
113 * interface specification for transparent drivers.
114 *
115 * \param[in,out] operation Active cipher operation.
116 * \param[in] iv Buffer containing the IV to use.
Ronald Crona0d68172021-03-26 10:15:08 +0100117 * \param[in] iv_length Size of the IV in bytes. It is guaranteed by
118 * the core to be less or equal to
119 * PSA_CIPHER_IV_MAX_SIZE.
Ronald Cron6d051732020-10-01 14:10:20 +0200120 *
Gilles Peskineed733552023-02-14 19:21:09 +0100121 * \retval #PSA_SUCCESS \emptydescription
Ronald Cron6d051732020-10-01 14:10:20 +0200122 * \retval #PSA_ERROR_INVALID_ARGUMENT
123 * The size of \p iv is not acceptable for the chosen algorithm,
124 * or the chosen algorithm does not use an IV.
Gilles Peskineed733552023-02-14 19:21:09 +0100125 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
Ronald Cron6d051732020-10-01 14:10:20 +0200126 */
127psa_status_t mbedtls_psa_cipher_set_iv(
Ronald Cron6e412a72021-03-10 09:58:47 +0100128 mbedtls_psa_cipher_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 const uint8_t *iv, size_t iv_length);
Ronald Cron6d051732020-10-01 14:10:20 +0200130
131/** Encrypt or decrypt a message fragment in an active cipher operation.
132 *
133 * \note The signature of this function is that of a PSA driver
134 * cipher_update entry point. This function behaves as a
135 * cipher_update entry point as defined in the PSA driver
136 * interface specification for transparent drivers.
137 *
138 * \param[in,out] operation Active cipher operation.
139 * \param[in] input Buffer containing the message fragment to
140 * encrypt or decrypt.
141 * \param[in] input_length Size of the \p input buffer in bytes.
142 * \param[out] output Buffer where the output is to be written.
143 * \param[in] output_size Size of the \p output buffer in bytes.
144 * \param[out] output_length On success, the number of bytes
145 * that make up the returned output.
146 *
Gilles Peskineed733552023-02-14 19:21:09 +0100147 * \retval #PSA_SUCCESS \emptydescription
Ronald Cron6d051732020-10-01 14:10:20 +0200148 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
149 * The size of the \p output buffer is too small.
Gilles Peskineed733552023-02-14 19:21:09 +0100150 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
Ronald Cron6d051732020-10-01 14:10:20 +0200151 */
152psa_status_t mbedtls_psa_cipher_update(
Ronald Cron6e412a72021-03-10 09:58:47 +0100153 mbedtls_psa_cipher_operation_t *operation,
Ronald Cron6d051732020-10-01 14:10:20 +0200154 const uint8_t *input, size_t input_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 uint8_t *output, size_t output_size, size_t *output_length);
Ronald Cron6d051732020-10-01 14:10:20 +0200156
157/** Finish encrypting or decrypting a message in a cipher operation.
158 *
159 * \note The signature of this function is that of a PSA driver
160 * cipher_finish entry point. This function behaves as a
161 * cipher_finish entry point as defined in the PSA driver
162 * interface specification for transparent drivers.
163 *
164 * \param[in,out] operation Active cipher operation.
165 * \param[out] output Buffer where the output is to be written.
166 * \param[in] output_size Size of the \p output buffer in bytes.
167 * \param[out] output_length On success, the number of bytes
168 * that make up the returned output.
169 *
Gilles Peskineed733552023-02-14 19:21:09 +0100170 * \retval #PSA_SUCCESS \emptydescription
Ronald Cron6d051732020-10-01 14:10:20 +0200171 * \retval #PSA_ERROR_INVALID_ARGUMENT
172 * The total input size passed to this operation is not valid for
173 * this particular algorithm. For example, the algorithm is a based
174 * on block cipher and requires a whole number of blocks, but the
175 * total input size is not a multiple of the block size.
176 * \retval #PSA_ERROR_INVALID_PADDING
177 * This is a decryption operation for an algorithm that includes
178 * padding, and the ciphertext does not contain valid padding.
179 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
180 * The size of the \p output buffer is too small.
Gilles Peskineed733552023-02-14 19:21:09 +0100181 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
Ronald Cron6d051732020-10-01 14:10:20 +0200182 */
183psa_status_t mbedtls_psa_cipher_finish(
Ronald Cron6e412a72021-03-10 09:58:47 +0100184 mbedtls_psa_cipher_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 uint8_t *output, size_t output_size, size_t *output_length);
Ronald Cron6d051732020-10-01 14:10:20 +0200186
187/** Abort a cipher operation.
188 *
189 * Aborting an operation frees all associated resources except for the
190 * \p operation structure itself. Once aborted, the operation object
191 * can be reused for another operation.
192 *
193 * \note The signature of this function is that of a PSA driver
194 * cipher_abort entry point. This function behaves as a
195 * cipher_abort entry point as defined in the PSA driver
196 * interface specification for transparent drivers.
197 *
198 * \param[in,out] operation Initialized cipher operation.
199 *
Gilles Peskineed733552023-02-14 19:21:09 +0100200 * \retval #PSA_SUCCESS \emptydescription
Ronald Cron6d051732020-10-01 14:10:20 +0200201 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100202psa_status_t mbedtls_psa_cipher_abort(mbedtls_psa_cipher_operation_t *operation);
Ronald Cron6d051732020-10-01 14:10:20 +0200203
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100204/** Encrypt a message using a symmetric cipher.
205 *
206 * \note The signature of this function is that of a PSA driver
207 * cipher_encrypt entry point. This function behaves as a
208 * cipher_encrypt entry point as defined in the PSA driver
209 * interface specification for transparent drivers.
210 *
211 * \param[in] attributes The attributes of the key to use for the
212 * operation.
213 * \param[in] key_buffer The buffer containing the key context.
214 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
215 * \param[in] alg The cipher algorithm to compute
216 * (\c PSA_ALG_XXX value such that
217 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Ronald Cron9b674282021-07-09 09:19:35 +0200218 * \param[in] iv Buffer containing the IV for encryption. The
219 * IV has been generated by the core.
220 * \param[in] iv_length Size of the \p iv in bytes.
221 * \param[in] input Buffer containing the message to encrypt.
222 * \param[in] input_length Size of the \p input buffer in bytes.
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100223 * \param[in,out] output Buffer where the output is to be written.
gabor-mezei-arm3f860e42021-06-29 16:39:49 +0200224 * \param[in] output_size Size of the \p output buffer in bytes.
gabor-mezei-arm90fceea2021-06-25 15:43:07 +0200225 * \param[out] output_length On success, the number of bytes that make up
226 * the returned output. Initialized to zero
227 * by the core.
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100228 *
Gilles Peskineed733552023-02-14 19:21:09 +0100229 * \retval #PSA_SUCCESS \emptydescription
230 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
231 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
232 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100233 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
234 * The size of the \p output buffer is too small.
235 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Cron9b674282021-07-09 09:19:35 +0200236 * The size \p iv_length is not acceptable for the chosen algorithm,
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100237 * or the chosen algorithm does not use an IV.
238 * The total input size passed to this operation is not valid for
239 * this particular algorithm. For example, the algorithm is a based
240 * on block cipher and requires a whole number of blocks, but the
241 * total input size is not a multiple of the block size.
242 * \retval #PSA_ERROR_INVALID_PADDING
243 * This is a decryption operation for an algorithm that includes
244 * padding, and the ciphertext does not contain valid padding.
245 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100246psa_status_t mbedtls_psa_cipher_encrypt(const psa_key_attributes_t *attributes,
247 const uint8_t *key_buffer,
248 size_t key_buffer_size,
249 psa_algorithm_t alg,
250 const uint8_t *iv,
251 size_t iv_length,
252 const uint8_t *input,
253 size_t input_length,
254 uint8_t *output,
255 size_t output_size,
256 size_t *output_length);
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100257
258/** Decrypt a message using a symmetric cipher.
259 *
260 * \note The signature of this function is that of a PSA driver
261 * cipher_decrypt entry point. This function behaves as a
262 * cipher_decrypt entry point as defined in the PSA driver
263 * interface specification for transparent drivers.
264 *
265 * \param[in] attributes The attributes of the key to use for the
266 * operation.
267 * \param[in] key_buffer The buffer containing the key context.
268 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
269 * \param[in] alg The cipher algorithm to compute
270 * (\c PSA_ALG_XXX value such that
271 * #PSA_ALG_IS_CIPHER(\p alg) is true).
gabor-mezei-arm90fceea2021-06-25 15:43:07 +0200272 * \param[in] input Buffer containing the iv and the ciphertext.
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100273 * \param[in] input_length Size of the \p input buffer in bytes.
274 * \param[out] output Buffer where the output is to be written.
275 * \param[in] output_size Size of the \p output buffer in bytes.
gabor-mezei-arm90fceea2021-06-25 15:43:07 +0200276 * \param[out] output_length On success, the number of bytes that make up
277 * the returned output. Initialized to zero
278 * by the core.
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100279 *
Gilles Peskineed733552023-02-14 19:21:09 +0100280 * \retval #PSA_SUCCESS \emptydescription
281 * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
282 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
283 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100284 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
285 * The size of the \p output buffer is too small.
286 * \retval #PSA_ERROR_INVALID_ARGUMENT
287 * The size of \p iv is not acceptable for the chosen algorithm,
288 * or the chosen algorithm does not use an IV.
289 * The total input size passed to this operation is not valid for
290 * this particular algorithm. For example, the algorithm is a based
291 * on block cipher and requires a whole number of blocks, but the
292 * total input size is not a multiple of the block size.
293 * \retval #PSA_ERROR_INVALID_PADDING
294 * This is a decryption operation for an algorithm that includes
295 * padding, and the ciphertext does not contain valid padding.
296 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100297psa_status_t mbedtls_psa_cipher_decrypt(const psa_key_attributes_t *attributes,
298 const uint8_t *key_buffer,
299 size_t key_buffer_size,
300 psa_algorithm_t alg,
301 const uint8_t *input,
302 size_t input_length,
303 uint8_t *output,
304 size_t output_size,
305 size_t *output_length);
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100306
Ronald Cron0ff57952021-03-08 16:46:35 +0100307#endif /* PSA_CRYPTO_CIPHER_H */