| Ronald Cron | 0ff5795 | 2021-03-08 16:46:35 +0100 | [diff] [blame] | 1 | /* | 
|  | 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 | #include "common.h" | 
|  | 22 |  | 
|  | 23 | #if defined(MBEDTLS_PSA_CRYPTO_C) | 
|  | 24 |  | 
|  | 25 | #include <psa_crypto_cipher.h> | 
| Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 26 | #include "psa_crypto_core.h" | 
| Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 27 | #include "psa_crypto_random_impl.h" | 
|  | 28 |  | 
| Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 29 | #include "mbedtls/cipher.h" | 
| Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 30 | #include "mbedtls/error.h" | 
| Ronald Cron | 0ff5795 | 2021-03-08 16:46:35 +0100 | [diff] [blame] | 31 |  | 
| Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 32 | #include <string.h> | 
|  | 33 |  | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 34 | #if ( defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) || \ | 
|  | 35 | ( defined(PSA_CRYPTO_DRIVER_TEST) && \ | 
|  | 36 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_DES) ) ) | 
|  | 37 | #define BUILTIN_KEY_TYPE_DES  1 | 
|  | 38 | #endif | 
|  | 39 |  | 
|  | 40 | #if ( defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING) || \ | 
|  | 41 | ( defined(PSA_CRYPTO_DRIVER_TEST) && \ | 
|  | 42 | defined(MBEDTLS_PSA_ACCEL_ALG_CBC_NO_PADDING) ) ) | 
|  | 43 | #define BUILTIN_ALG_CBC_NO_PADDING  1 | 
|  | 44 | #endif | 
|  | 45 |  | 
|  | 46 | #if ( defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7) || \ | 
|  | 47 | ( defined(PSA_CRYPTO_DRIVER_TEST) && \ | 
|  | 48 | defined(MBEDTLS_PSA_ACCEL_ALG_CBC_PKCS7) ) ) | 
|  | 49 | #define BUILTIN_ALG_CBC_PKCS7  1 | 
|  | 50 | #endif | 
|  | 51 |  | 
|  | 52 | #if ( defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CHACHA20) || \ | 
|  | 53 | ( defined(PSA_CRYPTO_DRIVER_TEST) && \ | 
|  | 54 | defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_CHACHA20) ) ) | 
|  | 55 | #define BUILTIN_KEY_TYPE_CHACHA20  1 | 
|  | 56 | #endif | 
|  | 57 |  | 
| Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 58 | const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( | 
|  | 59 | psa_algorithm_t alg, | 
|  | 60 | psa_key_type_t key_type, | 
|  | 61 | size_t key_bits, | 
|  | 62 | mbedtls_cipher_id_t* cipher_id ) | 
|  | 63 | { | 
|  | 64 | mbedtls_cipher_mode_t mode; | 
|  | 65 | mbedtls_cipher_id_t cipher_id_tmp; | 
|  | 66 |  | 
|  | 67 | if( PSA_ALG_IS_AEAD( alg ) ) | 
|  | 68 | alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ); | 
|  | 69 |  | 
|  | 70 | if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ) | 
|  | 71 | { | 
|  | 72 | switch( alg ) | 
|  | 73 | { | 
|  | 74 | case PSA_ALG_STREAM_CIPHER: | 
|  | 75 | mode = MBEDTLS_MODE_STREAM; | 
|  | 76 | break; | 
|  | 77 | case PSA_ALG_CTR: | 
|  | 78 | mode = MBEDTLS_MODE_CTR; | 
|  | 79 | break; | 
|  | 80 | case PSA_ALG_CFB: | 
|  | 81 | mode = MBEDTLS_MODE_CFB; | 
|  | 82 | break; | 
|  | 83 | case PSA_ALG_OFB: | 
|  | 84 | mode = MBEDTLS_MODE_OFB; | 
|  | 85 | break; | 
|  | 86 | case PSA_ALG_ECB_NO_PADDING: | 
|  | 87 | mode = MBEDTLS_MODE_ECB; | 
|  | 88 | break; | 
|  | 89 | case PSA_ALG_CBC_NO_PADDING: | 
|  | 90 | mode = MBEDTLS_MODE_CBC; | 
|  | 91 | break; | 
|  | 92 | case PSA_ALG_CBC_PKCS7: | 
|  | 93 | mode = MBEDTLS_MODE_CBC; | 
|  | 94 | break; | 
| Mateusz Starzyk | 594215b | 2021-10-14 12:23:06 +0200 | [diff] [blame] | 95 | case PSA_ALG_CCM_STAR_NO_TAG: | 
| Mateusz Starzyk | 4cb9739 | 2021-10-27 10:42:31 +0200 | [diff] [blame] | 96 | mode = MBEDTLS_MODE_CCM_STAR_NO_TAG; | 
| Mateusz Starzyk | 594215b | 2021-10-14 12:23:06 +0200 | [diff] [blame] | 97 | break; | 
| Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 98 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ): | 
|  | 99 | mode = MBEDTLS_MODE_CCM; | 
|  | 100 | break; | 
|  | 101 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ): | 
|  | 102 | mode = MBEDTLS_MODE_GCM; | 
|  | 103 | break; | 
|  | 104 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ): | 
|  | 105 | mode = MBEDTLS_MODE_CHACHAPOLY; | 
|  | 106 | break; | 
|  | 107 | default: | 
|  | 108 | return( NULL ); | 
|  | 109 | } | 
|  | 110 | } | 
|  | 111 | else if( alg == PSA_ALG_CMAC ) | 
|  | 112 | mode = MBEDTLS_MODE_ECB; | 
|  | 113 | else | 
|  | 114 | return( NULL ); | 
|  | 115 |  | 
|  | 116 | switch( key_type ) | 
|  | 117 | { | 
|  | 118 | case PSA_KEY_TYPE_AES: | 
|  | 119 | cipher_id_tmp = MBEDTLS_CIPHER_ID_AES; | 
|  | 120 | break; | 
| Gilles Peskine | 6c12a1e | 2021-09-21 11:59:39 +0200 | [diff] [blame] | 121 | case PSA_KEY_TYPE_ARIA: | 
|  | 122 | cipher_id_tmp = MBEDTLS_CIPHER_ID_ARIA; | 
|  | 123 | break; | 
| Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 124 | case PSA_KEY_TYPE_DES: | 
|  | 125 | /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES, | 
|  | 126 | * and 192 for three-key Triple-DES. */ | 
|  | 127 | if( key_bits == 64 ) | 
|  | 128 | cipher_id_tmp = MBEDTLS_CIPHER_ID_DES; | 
|  | 129 | else | 
|  | 130 | cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES; | 
|  | 131 | /* mbedtls doesn't recognize two-key Triple-DES as an algorithm, | 
|  | 132 | * but two-key Triple-DES is functionally three-key Triple-DES | 
|  | 133 | * with K1=K3, so that's how we present it to mbedtls. */ | 
|  | 134 | if( key_bits == 128 ) | 
|  | 135 | key_bits = 192; | 
|  | 136 | break; | 
|  | 137 | case PSA_KEY_TYPE_CAMELLIA: | 
|  | 138 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA; | 
|  | 139 | break; | 
| Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 140 | case PSA_KEY_TYPE_CHACHA20: | 
|  | 141 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20; | 
|  | 142 | break; | 
|  | 143 | default: | 
|  | 144 | return( NULL ); | 
|  | 145 | } | 
|  | 146 | if( cipher_id != NULL ) | 
|  | 147 | *cipher_id = cipher_id_tmp; | 
|  | 148 |  | 
|  | 149 | return( mbedtls_cipher_info_from_values( cipher_id_tmp, | 
|  | 150 | (int) key_bits, mode ) ); | 
|  | 151 | } | 
|  | 152 |  | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 153 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) || defined(PSA_CRYPTO_DRIVER_TEST) | 
|  | 154 |  | 
| Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 155 | static psa_status_t cipher_setup( | 
| Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 156 | mbedtls_psa_cipher_operation_t *operation, | 
| Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 157 | const psa_key_attributes_t *attributes, | 
|  | 158 | const uint8_t *key_buffer, size_t key_buffer_size, | 
|  | 159 | psa_algorithm_t alg, | 
|  | 160 | mbedtls_operation_t cipher_operation ) | 
|  | 161 | { | 
|  | 162 | int ret = 0; | 
|  | 163 | size_t key_bits; | 
|  | 164 | const mbedtls_cipher_info_t *cipher_info = NULL; | 
|  | 165 | psa_key_type_t key_type = attributes->core.type; | 
|  | 166 |  | 
|  | 167 | (void)key_buffer_size; | 
|  | 168 |  | 
| gabor-mezei-arm | 42cdb2a | 2021-04-12 15:47:35 +0200 | [diff] [blame] | 169 | mbedtls_cipher_init( &operation->ctx.cipher ); | 
| Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 170 |  | 
| Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 171 | operation->alg = alg; | 
| Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 172 | key_bits = attributes->core.bits; | 
|  | 173 | cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, | 
|  | 174 | key_bits, NULL ); | 
|  | 175 | if( cipher_info == NULL ) | 
|  | 176 | return( PSA_ERROR_NOT_SUPPORTED ); | 
|  | 177 |  | 
| gabor-mezei-arm | 42cdb2a | 2021-04-12 15:47:35 +0200 | [diff] [blame] | 178 | ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info ); | 
| Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 179 | if( ret != 0 ) | 
|  | 180 | goto exit; | 
|  | 181 |  | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 182 | #if defined(BUILTIN_KEY_TYPE_DES) | 
| Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 183 | if( key_type == PSA_KEY_TYPE_DES && key_bits == 128 ) | 
|  | 184 | { | 
|  | 185 | /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */ | 
|  | 186 | uint8_t keys[24]; | 
|  | 187 | memcpy( keys, key_buffer, 16 ); | 
|  | 188 | memcpy( keys + 16, key_buffer, 8 ); | 
| gabor-mezei-arm | 42cdb2a | 2021-04-12 15:47:35 +0200 | [diff] [blame] | 189 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, | 
| Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 190 | keys, | 
|  | 191 | 192, cipher_operation ); | 
|  | 192 | } | 
|  | 193 | else | 
|  | 194 | #endif | 
|  | 195 | { | 
| gabor-mezei-arm | 42cdb2a | 2021-04-12 15:47:35 +0200 | [diff] [blame] | 196 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, key_buffer, | 
| Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 197 | (int) key_bits, cipher_operation ); | 
|  | 198 | } | 
|  | 199 | if( ret != 0 ) | 
|  | 200 | goto exit; | 
|  | 201 |  | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 202 | #if defined(BUILTIN_ALG_CBC_NO_PADDING) || \ | 
|  | 203 | defined(BUILTIN_ALG_CBC_PKCS7) | 
| Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 204 | switch( alg ) | 
|  | 205 | { | 
|  | 206 | case PSA_ALG_CBC_NO_PADDING: | 
| gabor-mezei-arm | 42cdb2a | 2021-04-12 15:47:35 +0200 | [diff] [blame] | 207 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, | 
| Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 208 | MBEDTLS_PADDING_NONE ); | 
|  | 209 | break; | 
|  | 210 | case PSA_ALG_CBC_PKCS7: | 
| gabor-mezei-arm | 42cdb2a | 2021-04-12 15:47:35 +0200 | [diff] [blame] | 211 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, | 
| Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 212 | MBEDTLS_PADDING_PKCS7 ); | 
|  | 213 | break; | 
|  | 214 | default: | 
|  | 215 | /* The algorithm doesn't involve padding. */ | 
|  | 216 | ret = 0; | 
|  | 217 | break; | 
|  | 218 | } | 
|  | 219 | if( ret != 0 ) | 
|  | 220 | goto exit; | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 221 | #endif /* BUILTIN_ALG_CBC_NO_PADDING || BUILTIN_ALG_CBC_PKCS7 */ | 
| Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 222 |  | 
| Ronald Cron | 6ad554c | 2021-03-26 09:29:09 +0100 | [diff] [blame] | 223 | operation->block_length = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 : | 
|  | 224 | PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) ); | 
| Ronald Cron | c17e8a9 | 2021-03-19 14:12:26 +0100 | [diff] [blame] | 225 | operation->iv_length = PSA_CIPHER_IV_LENGTH( key_type, alg ); | 
| Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 226 |  | 
|  | 227 | exit: | 
|  | 228 | return( mbedtls_to_psa_error( ret ) ); | 
|  | 229 | } | 
|  | 230 |  | 
| Ronald Cron | 8287e6b | 2021-03-12 10:35:18 +0100 | [diff] [blame] | 231 | static psa_status_t cipher_encrypt_setup( | 
| Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 232 | mbedtls_psa_cipher_operation_t *operation, | 
| Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 233 | const psa_key_attributes_t *attributes, | 
|  | 234 | const uint8_t *key_buffer, size_t key_buffer_size, | 
|  | 235 | psa_algorithm_t alg ) | 
|  | 236 | { | 
|  | 237 | return( cipher_setup( operation, attributes, | 
|  | 238 | key_buffer, key_buffer_size, | 
|  | 239 | alg, MBEDTLS_ENCRYPT ) ); | 
|  | 240 | } | 
|  | 241 |  | 
| Ronald Cron | 8287e6b | 2021-03-12 10:35:18 +0100 | [diff] [blame] | 242 | static psa_status_t cipher_decrypt_setup( | 
| Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 243 | mbedtls_psa_cipher_operation_t *operation, | 
| Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 244 | const psa_key_attributes_t *attributes, | 
|  | 245 | const uint8_t *key_buffer, size_t key_buffer_size, | 
|  | 246 | psa_algorithm_t alg ) | 
|  | 247 | { | 
|  | 248 | return( cipher_setup( operation, attributes, | 
|  | 249 | key_buffer, key_buffer_size, | 
|  | 250 | alg, MBEDTLS_DECRYPT ) ); | 
|  | 251 | } | 
| Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 252 |  | 
| Ronald Cron | 8287e6b | 2021-03-12 10:35:18 +0100 | [diff] [blame] | 253 | static psa_status_t cipher_set_iv( mbedtls_psa_cipher_operation_t *operation, | 
|  | 254 | const uint8_t *iv, size_t iv_length ) | 
|  | 255 | { | 
| Ronald Cron | 6ad554c | 2021-03-26 09:29:09 +0100 | [diff] [blame] | 256 | if( iv_length != operation->iv_length ) | 
| Ronald Cron | 8287e6b | 2021-03-12 10:35:18 +0100 | [diff] [blame] | 257 | return( PSA_ERROR_INVALID_ARGUMENT ); | 
|  | 258 |  | 
|  | 259 | return( mbedtls_to_psa_error( | 
| gabor-mezei-arm | 42cdb2a | 2021-04-12 15:47:35 +0200 | [diff] [blame] | 260 | mbedtls_cipher_set_iv( &operation->ctx.cipher, | 
| Ronald Cron | 8287e6b | 2021-03-12 10:35:18 +0100 | [diff] [blame] | 261 | iv, iv_length ) ) ); | 
|  | 262 | } | 
|  | 263 |  | 
| Gilles Peskine | 55dffe5 | 2021-09-13 09:33:28 +0200 | [diff] [blame] | 264 | /** Process input for which the algorithm is set to ECB mode. | 
|  | 265 | * | 
|  | 266 | * This requires manual processing, since the PSA API is defined as being | 
|  | 267 | * able to process arbitrary-length calls to psa_cipher_update() with ECB mode, | 
|  | 268 | * but the underlying mbedtls_cipher_update only takes full blocks. | 
|  | 269 | * | 
|  | 270 | * \param ctx           The mbedtls cipher context to use. It must have been | 
|  | 271 | *                      set up for ECB. | 
|  | 272 | * \param[in] input     The input plaintext or ciphertext to process. | 
|  | 273 | * \param input_length  The number of bytes to process from \p input. | 
|  | 274 | *                      This does not need to be aligned to a block boundary. | 
|  | 275 | *                      If there is a partial block at the end of the input, | 
|  | 276 | *                      it is stored in \p ctx for future processing. | 
| Gilles Peskine | d87d873 | 2021-09-13 12:20:51 +0200 | [diff] [blame] | 277 | * \param output        The buffer where the output is written. It must be | 
|  | 278 | *                      at least `BS * floor((p + input_length) / BS)` bytes | 
|  | 279 | *                      long, where `p` is the number of bytes in the | 
|  | 280 | *                      unprocessed partial block in \p ctx (with | 
|  | 281 | *                      `0 <= p <= BS - 1`) and `BS` is the block size. | 
| Gilles Peskine | 55dffe5 | 2021-09-13 09:33:28 +0200 | [diff] [blame] | 282 | * \param output_length On success, the number of bytes written to \p output. | 
|  | 283 | *                      \c 0 on error. | 
|  | 284 | * | 
|  | 285 | * \return #PSA_SUCCESS or an error from a hardware accelerator | 
|  | 286 | */ | 
| Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 287 | static psa_status_t psa_cipher_update_ecb( | 
|  | 288 | mbedtls_cipher_context_t *ctx, | 
|  | 289 | const uint8_t *input, | 
|  | 290 | size_t input_length, | 
|  | 291 | uint8_t *output, | 
| Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 292 | size_t *output_length ) | 
|  | 293 | { | 
|  | 294 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 295 | size_t block_size = ctx->cipher_info->block_size; | 
|  | 296 | size_t internal_output_length = 0; | 
|  | 297 | *output_length = 0; | 
|  | 298 |  | 
|  | 299 | if( input_length == 0 ) | 
|  | 300 | { | 
|  | 301 | status = PSA_SUCCESS; | 
|  | 302 | goto exit; | 
|  | 303 | } | 
|  | 304 |  | 
|  | 305 | if( ctx->unprocessed_len > 0 ) | 
|  | 306 | { | 
|  | 307 | /* Fill up to block size, and run the block if there's a full one. */ | 
|  | 308 | size_t bytes_to_copy = block_size - ctx->unprocessed_len; | 
|  | 309 |  | 
|  | 310 | if( input_length < bytes_to_copy ) | 
|  | 311 | bytes_to_copy = input_length; | 
|  | 312 |  | 
|  | 313 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), | 
|  | 314 | input, bytes_to_copy ); | 
|  | 315 | input_length -= bytes_to_copy; | 
|  | 316 | input += bytes_to_copy; | 
|  | 317 | ctx->unprocessed_len += bytes_to_copy; | 
|  | 318 |  | 
|  | 319 | if( ctx->unprocessed_len == block_size ) | 
|  | 320 | { | 
|  | 321 | status = mbedtls_to_psa_error( | 
|  | 322 | mbedtls_cipher_update( ctx, | 
|  | 323 | ctx->unprocessed_data, | 
|  | 324 | block_size, | 
|  | 325 | output, &internal_output_length ) ); | 
|  | 326 |  | 
|  | 327 | if( status != PSA_SUCCESS ) | 
|  | 328 | goto exit; | 
|  | 329 |  | 
|  | 330 | output += internal_output_length; | 
| Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 331 | *output_length += internal_output_length; | 
|  | 332 | ctx->unprocessed_len = 0; | 
|  | 333 | } | 
|  | 334 | } | 
|  | 335 |  | 
|  | 336 | while( input_length >= block_size ) | 
|  | 337 | { | 
|  | 338 | /* Run all full blocks we have, one by one */ | 
|  | 339 | status = mbedtls_to_psa_error( | 
|  | 340 | mbedtls_cipher_update( ctx, input, | 
|  | 341 | block_size, | 
|  | 342 | output, &internal_output_length ) ); | 
|  | 343 |  | 
|  | 344 | if( status != PSA_SUCCESS ) | 
|  | 345 | goto exit; | 
|  | 346 |  | 
|  | 347 | input_length -= block_size; | 
|  | 348 | input += block_size; | 
|  | 349 |  | 
|  | 350 | output += internal_output_length; | 
| Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 351 | *output_length += internal_output_length; | 
|  | 352 | } | 
|  | 353 |  | 
|  | 354 | if( input_length > 0 ) | 
|  | 355 | { | 
|  | 356 | /* Save unprocessed bytes for later processing */ | 
|  | 357 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), | 
|  | 358 | input, input_length ); | 
|  | 359 | ctx->unprocessed_len += input_length; | 
|  | 360 | } | 
|  | 361 |  | 
|  | 362 | status = PSA_SUCCESS; | 
|  | 363 |  | 
|  | 364 | exit: | 
|  | 365 | return( status ); | 
|  | 366 | } | 
|  | 367 |  | 
| Ronald Cron | 8287e6b | 2021-03-12 10:35:18 +0100 | [diff] [blame] | 368 | static psa_status_t cipher_update( mbedtls_psa_cipher_operation_t *operation, | 
|  | 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 Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 374 | { | 
|  | 375 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 376 | size_t expected_output_size; | 
|  | 377 |  | 
| Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 378 | if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) | 
| Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 379 | { | 
|  | 380 | /* Take the unprocessed partial block left over from previous | 
|  | 381 | * update calls, if any, plus the input to this call. Remove | 
|  | 382 | * the last partial block, if any. You get the data that will be | 
|  | 383 | * output in this call. */ | 
|  | 384 | expected_output_size = | 
| gabor-mezei-arm | 42cdb2a | 2021-04-12 15:47:35 +0200 | [diff] [blame] | 385 | ( operation->ctx.cipher.unprocessed_len + input_length ) | 
| Ronald Cron | 6ad554c | 2021-03-26 09:29:09 +0100 | [diff] [blame] | 386 | / operation->block_length * operation->block_length; | 
| Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 387 | } | 
|  | 388 | else | 
|  | 389 | { | 
|  | 390 | expected_output_size = input_length; | 
|  | 391 | } | 
|  | 392 |  | 
|  | 393 | if( output_size < expected_output_size ) | 
|  | 394 | return( PSA_ERROR_BUFFER_TOO_SMALL ); | 
|  | 395 |  | 
| Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 396 | if( operation->alg == PSA_ALG_ECB_NO_PADDING ) | 
| Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 397 | { | 
|  | 398 | /* mbedtls_cipher_update has an API inconsistency: it will only | 
|  | 399 | * process a single block at a time in ECB mode. Abstract away that | 
|  | 400 | * inconsistency here to match the PSA API behaviour. */ | 
| gabor-mezei-arm | 42cdb2a | 2021-04-12 15:47:35 +0200 | [diff] [blame] | 401 | status = psa_cipher_update_ecb( &operation->ctx.cipher, | 
| Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 402 | input, | 
|  | 403 | input_length, | 
|  | 404 | output, | 
| Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 405 | output_length ); | 
|  | 406 | } | 
|  | 407 | else | 
|  | 408 | { | 
|  | 409 | status = mbedtls_to_psa_error( | 
| gabor-mezei-arm | 42cdb2a | 2021-04-12 15:47:35 +0200 | [diff] [blame] | 410 | mbedtls_cipher_update( &operation->ctx.cipher, input, | 
| Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 411 | input_length, output, output_length ) ); | 
| gabor-mezei-arm | 58c1727 | 2021-06-29 16:41:25 +0200 | [diff] [blame] | 412 |  | 
|  | 413 | if( *output_length > output_size ) | 
| gabor-mezei-arm | 00e54f1 | 2021-06-29 19:06:30 +0200 | [diff] [blame] | 414 | return( PSA_ERROR_CORRUPTION_DETECTED ); | 
| Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 415 | } | 
|  | 416 |  | 
|  | 417 | return( status ); | 
|  | 418 | } | 
|  | 419 |  | 
| Ronald Cron | 8287e6b | 2021-03-12 10:35:18 +0100 | [diff] [blame] | 420 | static psa_status_t cipher_finish( mbedtls_psa_cipher_operation_t *operation, | 
|  | 421 | uint8_t *output, | 
|  | 422 | size_t output_size, | 
|  | 423 | size_t *output_length ) | 
| Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 424 | { | 
|  | 425 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; | 
|  | 426 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; | 
|  | 427 |  | 
| gabor-mezei-arm | 42cdb2a | 2021-04-12 15:47:35 +0200 | [diff] [blame] | 428 | if( operation->ctx.cipher.unprocessed_len != 0 ) | 
| Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 429 | { | 
| Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 430 | if( operation->alg == PSA_ALG_ECB_NO_PADDING || | 
|  | 431 | operation->alg == PSA_ALG_CBC_NO_PADDING ) | 
| Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 432 | { | 
|  | 433 | status = PSA_ERROR_INVALID_ARGUMENT; | 
|  | 434 | goto exit; | 
|  | 435 | } | 
|  | 436 | } | 
|  | 437 |  | 
|  | 438 | status = mbedtls_to_psa_error( | 
| gabor-mezei-arm | 42cdb2a | 2021-04-12 15:47:35 +0200 | [diff] [blame] | 439 | mbedtls_cipher_finish( &operation->ctx.cipher, | 
| Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 440 | temp_output_buffer, | 
|  | 441 | output_length ) ); | 
|  | 442 | if( status != PSA_SUCCESS ) | 
|  | 443 | goto exit; | 
|  | 444 |  | 
|  | 445 | if( *output_length == 0 ) | 
|  | 446 | ; /* Nothing to copy. Note that output may be NULL in this case. */ | 
|  | 447 | else if( output_size >= *output_length ) | 
|  | 448 | memcpy( output, temp_output_buffer, *output_length ); | 
|  | 449 | else | 
|  | 450 | status = PSA_ERROR_BUFFER_TOO_SMALL; | 
|  | 451 |  | 
|  | 452 | exit: | 
|  | 453 | mbedtls_platform_zeroize( temp_output_buffer, | 
|  | 454 | sizeof( temp_output_buffer ) ); | 
|  | 455 |  | 
|  | 456 | return( status ); | 
|  | 457 | } | 
|  | 458 |  | 
| Ronald Cron | 8287e6b | 2021-03-12 10:35:18 +0100 | [diff] [blame] | 459 | static psa_status_t cipher_abort( mbedtls_psa_cipher_operation_t *operation ) | 
| Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 460 | { | 
| Ronald Cron | 937dfee | 2021-03-10 09:17:32 +0100 | [diff] [blame] | 461 | /* Sanity check (shouldn't happen: operation->alg should | 
|  | 462 | * always have been initialized to a valid value). */ | 
| Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 463 | if( ! PSA_ALG_IS_CIPHER( operation->alg ) ) | 
| Ronald Cron | 937dfee | 2021-03-10 09:17:32 +0100 | [diff] [blame] | 464 | return( PSA_ERROR_BAD_STATE ); | 
|  | 465 |  | 
| gabor-mezei-arm | 42cdb2a | 2021-04-12 15:47:35 +0200 | [diff] [blame] | 466 | mbedtls_cipher_free( &operation->ctx.cipher ); | 
| Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 467 |  | 
|  | 468 | return( PSA_SUCCESS ); | 
|  | 469 | } | 
| gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 470 |  | 
|  | 471 | static psa_status_t cipher_encrypt( const psa_key_attributes_t *attributes, | 
|  | 472 | const uint8_t *key_buffer, | 
|  | 473 | size_t key_buffer_size, | 
|  | 474 | psa_algorithm_t alg, | 
|  | 475 | const uint8_t *input, | 
|  | 476 | size_t input_length, | 
|  | 477 | uint8_t *output, | 
|  | 478 | size_t output_size, | 
|  | 479 | size_t *output_length ) | 
|  | 480 | { | 
|  | 481 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 482 | mbedtls_psa_cipher_operation_t operation = MBEDTLS_PSA_CIPHER_OPERATION_INIT; | 
| gabor-mezei-arm | e5ff8f4 | 2021-06-25 15:23:05 +0200 | [diff] [blame] | 483 | size_t olength, accumulated_length; | 
| gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 484 |  | 
| gabor-mezei-arm | 47a8e14 | 2021-06-25 15:44:47 +0200 | [diff] [blame] | 485 | status = cipher_encrypt_setup( &operation, attributes, | 
|  | 486 | key_buffer, key_buffer_size, alg ); | 
| gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 487 | if( status != PSA_SUCCESS ) | 
|  | 488 | goto exit; | 
|  | 489 |  | 
| gabor-mezei-arm | e5ff8f4 | 2021-06-25 15:23:05 +0200 | [diff] [blame] | 490 | accumulated_length = 0; | 
| gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 491 | if( operation.iv_length > 0 ) | 
|  | 492 | { | 
|  | 493 | status = cipher_set_iv( &operation, output, operation.iv_length ); | 
|  | 494 | if( status != PSA_SUCCESS ) | 
|  | 495 | goto exit; | 
|  | 496 |  | 
| gabor-mezei-arm | e5ff8f4 | 2021-06-25 15:23:05 +0200 | [diff] [blame] | 497 | accumulated_length = operation.iv_length; | 
| gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 498 | } | 
|  | 499 |  | 
| gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 500 | status = cipher_update( &operation, input, input_length, | 
| gabor-mezei-arm | e5ff8f4 | 2021-06-25 15:23:05 +0200 | [diff] [blame] | 501 | output + operation.iv_length, | 
|  | 502 | output_size - operation.iv_length, | 
| gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 503 | &olength ); | 
| gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 504 | if( status != PSA_SUCCESS ) | 
|  | 505 | goto exit; | 
|  | 506 |  | 
| gabor-mezei-arm | 6158e28 | 2021-06-29 16:42:13 +0200 | [diff] [blame] | 507 | accumulated_length += olength; | 
|  | 508 |  | 
| gabor-mezei-arm | e5ff8f4 | 2021-06-25 15:23:05 +0200 | [diff] [blame] | 509 | status = cipher_finish( &operation, output + accumulated_length, | 
|  | 510 | output_size - accumulated_length, &olength ); | 
| gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 511 | if( status != PSA_SUCCESS ) | 
|  | 512 | goto exit; | 
|  | 513 |  | 
| gabor-mezei-arm | 00e54f1 | 2021-06-29 19:06:30 +0200 | [diff] [blame] | 514 | *output_length = accumulated_length + olength; | 
| gabor-mezei-arm | e5ff8f4 | 2021-06-25 15:23:05 +0200 | [diff] [blame] | 515 |  | 
| gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 516 | exit: | 
|  | 517 | if( status == PSA_SUCCESS ) | 
|  | 518 | status = cipher_abort( &operation ); | 
|  | 519 | else | 
|  | 520 | cipher_abort( &operation ); | 
|  | 521 | return( status ); | 
|  | 522 | } | 
|  | 523 |  | 
|  | 524 | static psa_status_t cipher_decrypt( const psa_key_attributes_t *attributes, | 
|  | 525 | const uint8_t *key_buffer, | 
|  | 526 | size_t key_buffer_size, | 
|  | 527 | psa_algorithm_t alg, | 
|  | 528 | const uint8_t *input, | 
|  | 529 | size_t input_length, | 
|  | 530 | uint8_t *output, | 
|  | 531 | size_t output_size, | 
|  | 532 | size_t *output_length ) | 
|  | 533 | { | 
|  | 534 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 535 | mbedtls_psa_cipher_operation_t operation = MBEDTLS_PSA_CIPHER_OPERATION_INIT; | 
| gabor-mezei-arm | e5ff8f4 | 2021-06-25 15:23:05 +0200 | [diff] [blame] | 536 | size_t olength, accumulated_length; | 
| gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 537 |  | 
|  | 538 | status = cipher_decrypt_setup( &operation, attributes, | 
|  | 539 | key_buffer, key_buffer_size, alg ); | 
|  | 540 | if( status != PSA_SUCCESS ) | 
|  | 541 | goto exit; | 
|  | 542 |  | 
|  | 543 | if( operation.iv_length > 0 ) | 
|  | 544 | { | 
|  | 545 | status = cipher_set_iv( &operation, input, operation.iv_length ); | 
|  | 546 | if( status != PSA_SUCCESS ) | 
|  | 547 | goto exit; | 
|  | 548 | } | 
|  | 549 |  | 
| gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 550 | status = cipher_update( &operation, input + operation.iv_length, | 
|  | 551 | input_length - operation.iv_length, | 
|  | 552 | output, output_size, &olength ); | 
| gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 553 | if( status != PSA_SUCCESS ) | 
|  | 554 | goto exit; | 
|  | 555 |  | 
| gabor-mezei-arm | 6158e28 | 2021-06-29 16:42:13 +0200 | [diff] [blame] | 556 | accumulated_length = olength; | 
| gabor-mezei-arm | 258ae07 | 2021-06-25 15:25:38 +0200 | [diff] [blame] | 557 |  | 
| gabor-mezei-arm | e5ff8f4 | 2021-06-25 15:23:05 +0200 | [diff] [blame] | 558 | status = cipher_finish( &operation, output + accumulated_length, | 
|  | 559 | output_size - accumulated_length, &olength ); | 
| gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 560 | if( status != PSA_SUCCESS ) | 
|  | 561 | goto exit; | 
|  | 562 |  | 
| gabor-mezei-arm | 00e54f1 | 2021-06-29 19:06:30 +0200 | [diff] [blame] | 563 | *output_length = accumulated_length + olength; | 
| gabor-mezei-arm | e5ff8f4 | 2021-06-25 15:23:05 +0200 | [diff] [blame] | 564 |  | 
| gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 565 | exit: | 
|  | 566 | if ( status == PSA_SUCCESS ) | 
|  | 567 | status = cipher_abort( &operation ); | 
|  | 568 | else | 
|  | 569 | cipher_abort( &operation ); | 
|  | 570 | return( status ); | 
|  | 571 | } | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 572 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER || PSA_CRYPTO_DRIVER_TEST */ | 
| Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 573 |  | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 574 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) | 
| Ronald Cron | 8287e6b | 2021-03-12 10:35:18 +0100 | [diff] [blame] | 575 | psa_status_t mbedtls_psa_cipher_encrypt_setup( | 
|  | 576 | mbedtls_psa_cipher_operation_t *operation, | 
|  | 577 | const psa_key_attributes_t *attributes, | 
|  | 578 | const uint8_t *key_buffer, size_t key_buffer_size, | 
|  | 579 | psa_algorithm_t alg ) | 
|  | 580 | { | 
|  | 581 | return( cipher_encrypt_setup( | 
|  | 582 | operation, attributes, key_buffer, key_buffer_size, alg ) ); | 
|  | 583 | } | 
|  | 584 |  | 
|  | 585 | psa_status_t mbedtls_psa_cipher_decrypt_setup( | 
|  | 586 | mbedtls_psa_cipher_operation_t *operation, | 
|  | 587 | const psa_key_attributes_t *attributes, | 
|  | 588 | const uint8_t *key_buffer, size_t key_buffer_size, | 
|  | 589 | psa_algorithm_t alg ) | 
|  | 590 | { | 
|  | 591 | return( cipher_decrypt_setup( | 
|  | 592 | operation, attributes, key_buffer, key_buffer_size, alg ) ); | 
|  | 593 | } | 
|  | 594 |  | 
| Ronald Cron | 8287e6b | 2021-03-12 10:35:18 +0100 | [diff] [blame] | 595 | psa_status_t mbedtls_psa_cipher_set_iv( mbedtls_psa_cipher_operation_t *operation, | 
|  | 596 | const uint8_t *iv, | 
|  | 597 | size_t iv_length ) | 
|  | 598 | { | 
|  | 599 | return( cipher_set_iv( operation, iv, iv_length ) ); | 
|  | 600 | } | 
|  | 601 |  | 
|  | 602 | psa_status_t mbedtls_psa_cipher_update( mbedtls_psa_cipher_operation_t *operation, | 
|  | 603 | const uint8_t *input, | 
|  | 604 | size_t input_length, | 
|  | 605 | uint8_t *output, | 
|  | 606 | size_t output_size, | 
|  | 607 | size_t *output_length ) | 
|  | 608 | { | 
|  | 609 | return( cipher_update( operation, input, input_length, | 
|  | 610 | output, output_size, output_length ) ); | 
|  | 611 | } | 
|  | 612 |  | 
|  | 613 | psa_status_t mbedtls_psa_cipher_finish( mbedtls_psa_cipher_operation_t *operation, | 
|  | 614 | uint8_t *output, | 
|  | 615 | size_t output_size, | 
|  | 616 | size_t *output_length ) | 
|  | 617 | { | 
|  | 618 | return( cipher_finish( operation, output, output_size, output_length ) ); | 
|  | 619 | } | 
|  | 620 |  | 
|  | 621 | psa_status_t mbedtls_psa_cipher_abort( mbedtls_psa_cipher_operation_t *operation ) | 
|  | 622 | { | 
|  | 623 | return( cipher_abort( operation ) ); | 
|  | 624 | } | 
| gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 625 |  | 
|  | 626 | psa_status_t mbedtls_psa_cipher_encrypt( const psa_key_attributes_t *attributes, | 
|  | 627 | const uint8_t *key_buffer, | 
|  | 628 | size_t key_buffer_size, | 
|  | 629 | psa_algorithm_t alg, | 
|  | 630 | const uint8_t *input, | 
|  | 631 | size_t input_length, | 
|  | 632 | uint8_t *output, | 
|  | 633 | size_t output_size, | 
|  | 634 | size_t *output_length ) | 
|  | 635 | { | 
|  | 636 | return( cipher_encrypt( attributes, key_buffer, key_buffer_size, | 
|  | 637 | alg, input, input_length, | 
|  | 638 | output, output_size, output_length ) ); | 
|  | 639 | } | 
|  | 640 |  | 
|  | 641 | psa_status_t mbedtls_psa_cipher_decrypt( const psa_key_attributes_t *attributes, | 
|  | 642 | const uint8_t *key_buffer, | 
|  | 643 | size_t key_buffer_size, | 
|  | 644 | psa_algorithm_t alg, | 
|  | 645 | const uint8_t *input, | 
|  | 646 | size_t input_length, | 
|  | 647 | uint8_t *output, | 
|  | 648 | size_t output_size, | 
|  | 649 | size_t *output_length ) | 
|  | 650 | { | 
|  | 651 | return( cipher_decrypt( attributes, key_buffer, key_buffer_size, | 
|  | 652 | alg, input, input_length, | 
|  | 653 | output, output_size, output_length ) ); | 
|  | 654 | } | 
| Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 655 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ | 
| Ronald Cron | 8287e6b | 2021-03-12 10:35:18 +0100 | [diff] [blame] | 656 |  | 
| Ronald Cron | 3522e32 | 2021-03-12 11:08:49 +0100 | [diff] [blame] | 657 | /* | 
|  | 658 | * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY. | 
|  | 659 | */ | 
|  | 660 |  | 
|  | 661 | #if defined(PSA_CRYPTO_DRIVER_TEST) | 
|  | 662 | psa_status_t mbedtls_transparent_test_driver_cipher_encrypt_setup( | 
|  | 663 | mbedtls_psa_cipher_operation_t *operation, | 
|  | 664 | const psa_key_attributes_t *attributes, | 
|  | 665 | const uint8_t *key_buffer, size_t key_buffer_size, | 
|  | 666 | psa_algorithm_t alg ) | 
|  | 667 | { | 
|  | 668 | return( cipher_encrypt_setup( | 
|  | 669 | operation, attributes, key_buffer, key_buffer_size, alg ) ); | 
|  | 670 | } | 
|  | 671 |  | 
|  | 672 | psa_status_t mbedtls_transparent_test_driver_cipher_decrypt_setup( | 
|  | 673 | mbedtls_psa_cipher_operation_t *operation, | 
|  | 674 | const psa_key_attributes_t *attributes, | 
|  | 675 | const uint8_t *key_buffer, size_t key_buffer_size, | 
|  | 676 | psa_algorithm_t alg ) | 
|  | 677 | { | 
|  | 678 | return( cipher_decrypt_setup( | 
|  | 679 | operation, attributes, key_buffer, key_buffer_size, alg ) ); | 
|  | 680 | } | 
|  | 681 |  | 
| Ronald Cron | 3522e32 | 2021-03-12 11:08:49 +0100 | [diff] [blame] | 682 | psa_status_t mbedtls_transparent_test_driver_cipher_set_iv( | 
|  | 683 | mbedtls_psa_cipher_operation_t *operation, | 
|  | 684 | const uint8_t *iv, size_t iv_length ) | 
|  | 685 | { | 
|  | 686 | return( cipher_set_iv( operation, iv, iv_length ) ); | 
|  | 687 | } | 
|  | 688 |  | 
|  | 689 | psa_status_t mbedtls_transparent_test_driver_cipher_update( | 
|  | 690 | mbedtls_psa_cipher_operation_t *operation, | 
|  | 691 | const uint8_t *input, size_t input_length, | 
|  | 692 | uint8_t *output, size_t output_size, size_t *output_length ) | 
|  | 693 | { | 
|  | 694 | return( cipher_update( operation, input, input_length, | 
|  | 695 | output, output_size, output_length ) ); | 
|  | 696 | } | 
|  | 697 |  | 
|  | 698 | psa_status_t mbedtls_transparent_test_driver_cipher_finish( | 
|  | 699 | mbedtls_psa_cipher_operation_t *operation, | 
|  | 700 | uint8_t *output, size_t output_size, size_t *output_length ) | 
|  | 701 | { | 
|  | 702 | return( cipher_finish( operation, output, output_size, output_length ) ); | 
|  | 703 | } | 
|  | 704 |  | 
|  | 705 | psa_status_t mbedtls_transparent_test_driver_cipher_abort( | 
|  | 706 | mbedtls_psa_cipher_operation_t *operation ) | 
|  | 707 | { | 
|  | 708 | return( cipher_abort( operation ) ); | 
|  | 709 | } | 
| gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 710 |  | 
|  | 711 | psa_status_t mbedtls_transparent_test_driver_cipher_encrypt( | 
|  | 712 | const psa_key_attributes_t *attributes, | 
|  | 713 | const uint8_t *key_buffer, | 
|  | 714 | size_t key_buffer_size, | 
|  | 715 | psa_algorithm_t alg, | 
|  | 716 | const uint8_t *input, | 
|  | 717 | size_t input_length, | 
|  | 718 | uint8_t *output, | 
|  | 719 | size_t output_size, | 
|  | 720 | size_t *output_length ) | 
|  | 721 | { | 
|  | 722 | return( cipher_encrypt( attributes, key_buffer, key_buffer_size, | 
|  | 723 | alg, input, input_length, | 
|  | 724 | output, output_size, output_length ) ); | 
|  | 725 | } | 
|  | 726 |  | 
|  | 727 | psa_status_t mbedtls_transparent_test_driver_cipher_decrypt( | 
|  | 728 | const psa_key_attributes_t *attributes, | 
|  | 729 | const uint8_t *key_buffer, | 
|  | 730 | size_t key_buffer_size, | 
|  | 731 | psa_algorithm_t alg, | 
|  | 732 | const uint8_t *input, | 
|  | 733 | size_t input_length, | 
|  | 734 | uint8_t *output, | 
|  | 735 | size_t output_size, | 
|  | 736 | size_t *output_length ) | 
|  | 737 | { | 
|  | 738 | return( cipher_decrypt( attributes, key_buffer, key_buffer_size, | 
|  | 739 | alg, input, input_length, | 
|  | 740 | output, output_size, output_length ) ); | 
|  | 741 | } | 
| Ronald Cron | 3522e32 | 2021-03-12 11:08:49 +0100 | [diff] [blame] | 742 | #endif /* PSA_CRYPTO_DRIVER_TEST */ | 
|  | 743 |  | 
| Ronald Cron | 0ff5795 | 2021-03-08 16:46:35 +0100 | [diff] [blame] | 744 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |