blob: e5fe23bb31b10bb04eac140addac66e144b2684d [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 Crond6d28882020-12-14 14:56:02 +010027/**
28 * \brief Set the key for a multipart symmetric encryption operation.
29 *
30 * \note The signature of this function is that of a PSA driver
31 * cipher_encrypt_setup entry point. This function behaves as a
32 * cipher_encrypt_setup entry point as defined in the PSA driver
33 * interface specification for transparent drivers.
34 *
35 * \param[in,out] operation The operation object to set up. It has been
36 * initialized as per the documentation for
37 * #psa_cipher_operation_t and not yet in use.
38 * \param[in] attributes The attributes of the key to use for the
39 * operation.
40 * \param[in] key_buffer The buffer containing the key context.
41 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
42 * \param[in] alg The cipher algorithm to compute
43 * (\c PSA_ALG_XXX value such that
44 * #PSA_ALG_IS_CIPHER(\p alg) is true).
45 *
46 * \retval #PSA_SUCCESS
47 * \retval #PSA_ERROR_NOT_SUPPORTED
48 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
49 * \retval #PSA_ERROR_CORRUPTION_DETECTED
50 */
51psa_status_t mbedtls_psa_cipher_encrypt_setup(
Ronald Cron6e412a72021-03-10 09:58:47 +010052 mbedtls_psa_cipher_operation_t *operation,
Ronald Crond6d28882020-12-14 14:56:02 +010053 const psa_key_attributes_t *attributes,
54 const uint8_t *key_buffer, size_t key_buffer_size,
55 psa_algorithm_t alg );
56
57/**
58 * \brief Set the key for a multipart symmetric decryption operation.
59 *
60 * \note The signature of this function is that of a PSA driver
61 * cipher_decrypt_setup entry point. This function behaves as a
62 * cipher_decrypt_setup entry point as defined in the PSA driver
63 * interface specification for transparent drivers.
64 *
65 * \param[in,out] operation The operation object to set up. It has been
66 * initialized as per the documentation for
67 * #psa_cipher_operation_t and not yet in use.
68 * \param[in] attributes The attributes of the key to use for the
69 * operation.
70 * \param[in] key_buffer The buffer containing the key context.
71 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
72 * \param[in] alg The cipher algorithm to compute
73 * (\c PSA_ALG_XXX value such that
74 * #PSA_ALG_IS_CIPHER(\p alg) is true).
75 *
76 * \retval #PSA_SUCCESS
77 * \retval #PSA_ERROR_NOT_SUPPORTED
78 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
79 * \retval #PSA_ERROR_CORRUPTION_DETECTED
80 */
81psa_status_t mbedtls_psa_cipher_decrypt_setup(
Ronald Cron6e412a72021-03-10 09:58:47 +010082 mbedtls_psa_cipher_operation_t *operation,
Ronald Crond6d28882020-12-14 14:56:02 +010083 const psa_key_attributes_t *attributes,
84 const uint8_t *key_buffer, size_t key_buffer_size,
85 psa_algorithm_t alg );
86
Ronald Cron6d051732020-10-01 14:10:20 +020087/** Set the IV for a symmetric encryption or decryption operation.
88 *
89 * This function sets the IV (initialization vector), nonce
90 * or initial counter value for the encryption or decryption operation.
91 *
92 * \note The signature of this function is that of a PSA driver
93 * cipher_set_iv entry point. This function behaves as a
94 * cipher_set_iv entry point as defined in the PSA driver
95 * interface specification for transparent drivers.
96 *
97 * \param[in,out] operation Active cipher operation.
98 * \param[in] iv Buffer containing the IV to use.
Ronald Crona0d68172021-03-26 10:15:08 +010099 * \param[in] iv_length Size of the IV in bytes. It is guaranteed by
100 * the core to be less or equal to
101 * PSA_CIPHER_IV_MAX_SIZE.
Ronald Cron6d051732020-10-01 14:10:20 +0200102 *
103 * \retval #PSA_SUCCESS
104 * \retval #PSA_ERROR_INVALID_ARGUMENT
105 * The size of \p iv is not acceptable for the chosen algorithm,
106 * or the chosen algorithm does not use an IV.
107 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
108 */
109psa_status_t mbedtls_psa_cipher_set_iv(
Ronald Cron6e412a72021-03-10 09:58:47 +0100110 mbedtls_psa_cipher_operation_t *operation,
Ronald Cron6d051732020-10-01 14:10:20 +0200111 const uint8_t *iv, size_t iv_length );
112
113/** Encrypt or decrypt a message fragment in an active cipher operation.
114 *
115 * \note The signature of this function is that of a PSA driver
116 * cipher_update entry point. This function behaves as a
117 * cipher_update entry point as defined in the PSA driver
118 * interface specification for transparent drivers.
119 *
120 * \param[in,out] operation Active cipher operation.
121 * \param[in] input Buffer containing the message fragment to
122 * encrypt or decrypt.
123 * \param[in] input_length Size of the \p input buffer in bytes.
124 * \param[out] output Buffer where the output is to be written.
125 * \param[in] output_size Size of the \p output buffer in bytes.
126 * \param[out] output_length On success, the number of bytes
127 * that make up the returned output.
128 *
129 * \retval #PSA_SUCCESS
130 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
131 * The size of the \p output buffer is too small.
132 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
133 */
134psa_status_t mbedtls_psa_cipher_update(
Ronald Cron6e412a72021-03-10 09:58:47 +0100135 mbedtls_psa_cipher_operation_t *operation,
Ronald Cron6d051732020-10-01 14:10:20 +0200136 const uint8_t *input, size_t input_length,
137 uint8_t *output, size_t output_size, size_t *output_length );
138
139/** Finish encrypting or decrypting a message in a cipher operation.
140 *
141 * \note The signature of this function is that of a PSA driver
142 * cipher_finish entry point. This function behaves as a
143 * cipher_finish entry point as defined in the PSA driver
144 * interface specification for transparent drivers.
145 *
146 * \param[in,out] operation Active cipher operation.
147 * \param[out] output Buffer where the output is to be written.
148 * \param[in] output_size Size of the \p output buffer in bytes.
149 * \param[out] output_length On success, the number of bytes
150 * that make up the returned output.
151 *
152 * \retval #PSA_SUCCESS
153 * \retval #PSA_ERROR_INVALID_ARGUMENT
154 * The total input size passed to this operation is not valid for
155 * this particular algorithm. For example, the algorithm is a based
156 * on block cipher and requires a whole number of blocks, but the
157 * total input size is not a multiple of the block size.
158 * \retval #PSA_ERROR_INVALID_PADDING
159 * This is a decryption operation for an algorithm that includes
160 * padding, and the ciphertext does not contain valid padding.
161 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
162 * The size of the \p output buffer is too small.
163 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
164 */
165psa_status_t mbedtls_psa_cipher_finish(
Ronald Cron6e412a72021-03-10 09:58:47 +0100166 mbedtls_psa_cipher_operation_t *operation,
Ronald Cron6d051732020-10-01 14:10:20 +0200167 uint8_t *output, size_t output_size, size_t *output_length );
168
169/** Abort a cipher operation.
170 *
171 * Aborting an operation frees all associated resources except for the
172 * \p operation structure itself. Once aborted, the operation object
173 * can be reused for another operation.
174 *
175 * \note The signature of this function is that of a PSA driver
176 * cipher_abort entry point. This function behaves as a
177 * cipher_abort entry point as defined in the PSA driver
178 * interface specification for transparent drivers.
179 *
180 * \param[in,out] operation Initialized cipher operation.
181 *
182 * \retval #PSA_SUCCESS
183 */
Ronald Cron6e412a72021-03-10 09:58:47 +0100184psa_status_t mbedtls_psa_cipher_abort( mbedtls_psa_cipher_operation_t *operation );
Ronald Cron6d051732020-10-01 14:10:20 +0200185
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100186/** Encrypt a message using a symmetric cipher.
187 *
188 * \note The signature of this function is that of a PSA driver
189 * cipher_encrypt entry point. This function behaves as a
190 * cipher_encrypt entry point as defined in the PSA driver
191 * interface specification for transparent drivers.
192 *
193 * \param[in] attributes The attributes of the key to use for the
194 * operation.
195 * \param[in] key_buffer The buffer containing the key context.
196 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
197 * \param[in] alg The cipher algorithm to compute
198 * (\c PSA_ALG_XXX value such that
199 * #PSA_ALG_IS_CIPHER(\p alg) is true).
Ronald Cron9b674282021-07-09 09:19:35 +0200200 * \param[in] iv Buffer containing the IV for encryption. The
201 * IV has been generated by the core.
202 * \param[in] iv_length Size of the \p iv in bytes.
203 * \param[in] input Buffer containing the message to encrypt.
204 * \param[in] input_length Size of the \p input buffer in bytes.
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100205 * \param[in,out] output Buffer where the output is to be written.
gabor-mezei-arm3f860e42021-06-29 16:39:49 +0200206 * \param[in] output_size Size of the \p output buffer in bytes.
gabor-mezei-arm90fceea2021-06-25 15:43:07 +0200207 * \param[out] output_length On success, the number of bytes that make up
208 * the returned output. Initialized to zero
209 * by the core.
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100210 *
211 * \retval #PSA_SUCCESS
212 * \retval #PSA_ERROR_NOT_SUPPORTED
213 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
214 * \retval #PSA_ERROR_CORRUPTION_DETECTED
215 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
216 * The size of the \p output buffer is too small.
217 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Cron9b674282021-07-09 09:19:35 +0200218 * The size \p iv_length is not acceptable for the chosen algorithm,
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100219 * or the chosen algorithm does not use an IV.
220 * The total input size passed to this operation is not valid for
221 * this particular algorithm. For example, the algorithm is a based
222 * on block cipher and requires a whole number of blocks, but the
223 * total input size is not a multiple of the block size.
224 * \retval #PSA_ERROR_INVALID_PADDING
225 * This is a decryption operation for an algorithm that includes
226 * padding, and the ciphertext does not contain valid padding.
227 */
228psa_status_t mbedtls_psa_cipher_encrypt( const psa_key_attributes_t *attributes,
229 const uint8_t *key_buffer,
230 size_t key_buffer_size,
231 psa_algorithm_t alg,
Ronald Cron9b674282021-07-09 09:19:35 +0200232 const uint8_t *iv,
233 size_t iv_length,
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100234 const uint8_t *input,
235 size_t input_length,
236 uint8_t *output,
237 size_t output_size,
238 size_t *output_length );
239
240/** Decrypt a message using a symmetric cipher.
241 *
242 * \note The signature of this function is that of a PSA driver
243 * cipher_decrypt entry point. This function behaves as a
244 * cipher_decrypt entry point as defined in the PSA driver
245 * interface specification for transparent drivers.
246 *
247 * \param[in] attributes The attributes of the key to use for the
248 * operation.
249 * \param[in] key_buffer The buffer containing the key context.
250 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
251 * \param[in] alg The cipher algorithm to compute
252 * (\c PSA_ALG_XXX value such that
253 * #PSA_ALG_IS_CIPHER(\p alg) is true).
gabor-mezei-arm90fceea2021-06-25 15:43:07 +0200254 * \param[in] input Buffer containing the iv and the ciphertext.
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100255 * \param[in] input_length Size of the \p input buffer in bytes.
256 * \param[out] output Buffer where the output is to be written.
257 * \param[in] output_size Size of the \p output buffer in bytes.
gabor-mezei-arm90fceea2021-06-25 15:43:07 +0200258 * \param[out] output_length On success, the number of bytes that make up
259 * the returned output. Initialized to zero
260 * by the core.
gabor-mezei-arma9449a02021-03-25 11:17:10 +0100261 *
262 * \retval #PSA_SUCCESS
263 * \retval #PSA_ERROR_NOT_SUPPORTED
264 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
265 * \retval #PSA_ERROR_CORRUPTION_DETECTED
266 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
267 * The size of the \p output buffer is too small.
268 * \retval #PSA_ERROR_INVALID_ARGUMENT
269 * The size of \p iv is not acceptable for the chosen algorithm,
270 * or the chosen algorithm does not use an IV.
271 * The total input size passed to this operation is not valid for
272 * this particular algorithm. For example, the algorithm is a based
273 * on block cipher and requires a whole number of blocks, but the
274 * total input size is not a multiple of the block size.
275 * \retval #PSA_ERROR_INVALID_PADDING
276 * This is a decryption operation for an algorithm that includes
277 * padding, and the ciphertext does not contain valid padding.
278 */
279psa_status_t mbedtls_psa_cipher_decrypt( const psa_key_attributes_t *attributes,
280 const uint8_t *key_buffer,
281 size_t key_buffer_size,
282 psa_algorithm_t alg,
283 const uint8_t *input,
284 size_t input_length,
285 uint8_t *output,
286 size_t output_size,
287 size_t *output_length );
288
Ronald Cron0ff57952021-03-08 16:46:35 +0100289#endif /* PSA_CRYPTO_CIPHER_H */