blob: 3130e8b1809bdccdb718845d1914753473938af9 [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/** Generate an IV for a symmetric encryption operation.
104 *
105 * This function generates a random IV (initialization vector), nonce
106 * or initial counter value for the encryption operation as appropriate
107 * for the chosen algorithm, key type and key size.
108 *
109 * \note The signature of this function is that of a PSA driver
110 * cipher_generate_iv entry point. This function behaves as a
111 * cipher_generate_iv entry point as defined in the PSA driver
112 * interface specification for transparent drivers.
113 *
114 * \param[in,out] operation Active cipher operation.
115 * \param[out] iv Buffer where the generated IV is to be written.
116 * \param[in] iv_size Size of the \p iv buffer in bytes.
117 * \param[out] iv_length On success, the number of bytes of the
118 * generated IV.
119 *
120 * \retval #PSA_SUCCESS
121 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
122 * The size of the \p iv buffer is too small.
123 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
124 */
125psa_status_t mbedtls_psa_cipher_generate_iv(
Ronald Cron6e412a72021-03-10 09:58:47 +0100126 mbedtls_psa_cipher_operation_t *operation,
Ronald Cron6d051732020-10-01 14:10:20 +0200127 uint8_t *iv, size_t iv_size, size_t *iv_length );
128
129/** Set the IV for a symmetric encryption or decryption operation.
130 *
131 * This function sets the IV (initialization vector), nonce
132 * or initial counter value for the encryption or decryption operation.
133 *
134 * \note The signature of this function is that of a PSA driver
135 * cipher_set_iv entry point. This function behaves as a
136 * cipher_set_iv entry point as defined in the PSA driver
137 * interface specification for transparent drivers.
138 *
139 * \param[in,out] operation Active cipher operation.
140 * \param[in] iv Buffer containing the IV to use.
141 * \param[in] iv_length Size of the IV in bytes.
142 *
143 * \retval #PSA_SUCCESS
144 * \retval #PSA_ERROR_INVALID_ARGUMENT
145 * The size of \p iv is not acceptable for the chosen algorithm,
146 * or the chosen algorithm does not use an IV.
147 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
148 */
149psa_status_t mbedtls_psa_cipher_set_iv(
Ronald Cron6e412a72021-03-10 09:58:47 +0100150 mbedtls_psa_cipher_operation_t *operation,
Ronald Cron6d051732020-10-01 14:10:20 +0200151 const uint8_t *iv, size_t iv_length );
152
153/** Encrypt or decrypt a message fragment in an active cipher operation.
154 *
155 * \note The signature of this function is that of a PSA driver
156 * cipher_update entry point. This function behaves as a
157 * cipher_update entry point as defined in the PSA driver
158 * interface specification for transparent drivers.
159 *
160 * \param[in,out] operation Active cipher operation.
161 * \param[in] input Buffer containing the message fragment to
162 * encrypt or decrypt.
163 * \param[in] input_length Size of the \p input buffer in bytes.
164 * \param[out] output Buffer where the output is to be written.
165 * \param[in] output_size Size of the \p output buffer in bytes.
166 * \param[out] output_length On success, the number of bytes
167 * that make up the returned output.
168 *
169 * \retval #PSA_SUCCESS
170 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
171 * The size of the \p output buffer is too small.
172 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
173 */
174psa_status_t mbedtls_psa_cipher_update(
Ronald Cron6e412a72021-03-10 09:58:47 +0100175 mbedtls_psa_cipher_operation_t *operation,
Ronald Cron6d051732020-10-01 14:10:20 +0200176 const uint8_t *input, size_t input_length,
177 uint8_t *output, size_t output_size, size_t *output_length );
178
179/** Finish encrypting or decrypting a message in a cipher operation.
180 *
181 * \note The signature of this function is that of a PSA driver
182 * cipher_finish entry point. This function behaves as a
183 * cipher_finish entry point as defined in the PSA driver
184 * interface specification for transparent drivers.
185 *
186 * \param[in,out] operation Active cipher operation.
187 * \param[out] output Buffer where the output is to be written.
188 * \param[in] output_size Size of the \p output buffer in bytes.
189 * \param[out] output_length On success, the number of bytes
190 * that make up the returned output.
191 *
192 * \retval #PSA_SUCCESS
193 * \retval #PSA_ERROR_INVALID_ARGUMENT
194 * The total input size passed to this operation is not valid for
195 * this particular algorithm. For example, the algorithm is a based
196 * on block cipher and requires a whole number of blocks, but the
197 * total input size is not a multiple of the block size.
198 * \retval #PSA_ERROR_INVALID_PADDING
199 * This is a decryption operation for an algorithm that includes
200 * padding, and the ciphertext does not contain valid padding.
201 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
202 * The size of the \p output buffer is too small.
203 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
204 */
205psa_status_t mbedtls_psa_cipher_finish(
Ronald Cron6e412a72021-03-10 09:58:47 +0100206 mbedtls_psa_cipher_operation_t *operation,
Ronald Cron6d051732020-10-01 14:10:20 +0200207 uint8_t *output, size_t output_size, size_t *output_length );
208
209/** Abort a cipher operation.
210 *
211 * Aborting an operation frees all associated resources except for the
212 * \p operation structure itself. Once aborted, the operation object
213 * can be reused for another operation.
214 *
215 * \note The signature of this function is that of a PSA driver
216 * cipher_abort entry point. This function behaves as a
217 * cipher_abort entry point as defined in the PSA driver
218 * interface specification for transparent drivers.
219 *
220 * \param[in,out] operation Initialized cipher operation.
221 *
222 * \retval #PSA_SUCCESS
223 */
Ronald Cron6e412a72021-03-10 09:58:47 +0100224psa_status_t mbedtls_psa_cipher_abort( mbedtls_psa_cipher_operation_t *operation );
Ronald Cron6d051732020-10-01 14:10:20 +0200225
Ronald Cron3522e322021-03-12 11:08:49 +0100226/*
227 * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY.
228 */
229
230#if defined(PSA_CRYPTO_DRIVER_TEST)
231psa_status_t mbedtls_transparent_test_driver_cipher_encrypt_setup(
232 mbedtls_psa_cipher_operation_t *operation,
233 const psa_key_attributes_t *attributes,
234 const uint8_t *key_buffer, size_t key_buffer_size,
235 psa_algorithm_t alg );
236
237psa_status_t mbedtls_transparent_test_driver_cipher_decrypt_setup(
238 mbedtls_psa_cipher_operation_t *operation,
239 const psa_key_attributes_t *attributes,
240 const uint8_t *key_buffer, size_t key_buffer_size,
241 psa_algorithm_t alg );
242
243psa_status_t mbedtls_transparent_test_driver_cipher_generate_iv(
244 mbedtls_psa_cipher_operation_t *operation,
245 uint8_t *iv, size_t iv_size, size_t *iv_length );
246
247psa_status_t mbedtls_transparent_test_driver_cipher_set_iv(
248 mbedtls_psa_cipher_operation_t *operation,
249 const uint8_t *iv, size_t iv_length );
250
251psa_status_t mbedtls_transparent_test_driver_cipher_update(
252 mbedtls_psa_cipher_operation_t *operation,
253 const uint8_t *input, size_t input_length,
254 uint8_t *output, size_t output_size, size_t *output_length );
255
256psa_status_t mbedtls_transparent_test_driver_cipher_finish(
257 mbedtls_psa_cipher_operation_t *operation,
258 uint8_t *output, size_t output_size, size_t *output_length );
259
260psa_status_t mbedtls_transparent_test_driver_cipher_abort(
261 mbedtls_psa_cipher_operation_t *operation );
262#endif /* PSA_CRYPTO_DRIVER_TEST */
263
Ronald Cron0ff57952021-03-08 16:46:35 +0100264#endif /* PSA_CRYPTO_CIPHER_H */