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 | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 34 | const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( |
| 35 | psa_algorithm_t alg, |
| 36 | psa_key_type_t key_type, |
| 37 | size_t key_bits, |
| 38 | mbedtls_cipher_id_t* cipher_id ) |
| 39 | { |
| 40 | mbedtls_cipher_mode_t mode; |
| 41 | mbedtls_cipher_id_t cipher_id_tmp; |
| 42 | |
| 43 | if( PSA_ALG_IS_AEAD( alg ) ) |
| 44 | alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ); |
| 45 | |
| 46 | if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ) |
| 47 | { |
| 48 | switch( alg ) |
| 49 | { |
| 50 | case PSA_ALG_STREAM_CIPHER: |
| 51 | mode = MBEDTLS_MODE_STREAM; |
| 52 | break; |
| 53 | case PSA_ALG_CTR: |
| 54 | mode = MBEDTLS_MODE_CTR; |
| 55 | break; |
| 56 | case PSA_ALG_CFB: |
| 57 | mode = MBEDTLS_MODE_CFB; |
| 58 | break; |
| 59 | case PSA_ALG_OFB: |
| 60 | mode = MBEDTLS_MODE_OFB; |
| 61 | break; |
| 62 | case PSA_ALG_ECB_NO_PADDING: |
| 63 | mode = MBEDTLS_MODE_ECB; |
| 64 | break; |
| 65 | case PSA_ALG_CBC_NO_PADDING: |
| 66 | mode = MBEDTLS_MODE_CBC; |
| 67 | break; |
| 68 | case PSA_ALG_CBC_PKCS7: |
| 69 | mode = MBEDTLS_MODE_CBC; |
| 70 | break; |
Mateusz Starzyk | 594215b | 2021-10-14 12:23:06 +0200 | [diff] [blame] | 71 | case PSA_ALG_CCM_STAR_NO_TAG: |
Mateusz Starzyk | 4cb9739 | 2021-10-27 10:42:31 +0200 | [diff] [blame] | 72 | mode = MBEDTLS_MODE_CCM_STAR_NO_TAG; |
Mateusz Starzyk | 594215b | 2021-10-14 12:23:06 +0200 | [diff] [blame] | 73 | break; |
Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 74 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ): |
| 75 | mode = MBEDTLS_MODE_CCM; |
| 76 | break; |
| 77 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ): |
| 78 | mode = MBEDTLS_MODE_GCM; |
| 79 | break; |
| 80 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ): |
| 81 | mode = MBEDTLS_MODE_CHACHAPOLY; |
| 82 | break; |
| 83 | default: |
| 84 | return( NULL ); |
| 85 | } |
| 86 | } |
| 87 | else if( alg == PSA_ALG_CMAC ) |
| 88 | mode = MBEDTLS_MODE_ECB; |
| 89 | else |
| 90 | return( NULL ); |
| 91 | |
| 92 | switch( key_type ) |
| 93 | { |
| 94 | case PSA_KEY_TYPE_AES: |
| 95 | cipher_id_tmp = MBEDTLS_CIPHER_ID_AES; |
| 96 | break; |
Gilles Peskine | 6c12a1e | 2021-09-21 11:59:39 +0200 | [diff] [blame] | 97 | case PSA_KEY_TYPE_ARIA: |
| 98 | cipher_id_tmp = MBEDTLS_CIPHER_ID_ARIA; |
| 99 | break; |
Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 100 | case PSA_KEY_TYPE_DES: |
| 101 | /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES, |
| 102 | * and 192 for three-key Triple-DES. */ |
| 103 | if( key_bits == 64 ) |
| 104 | cipher_id_tmp = MBEDTLS_CIPHER_ID_DES; |
| 105 | else |
| 106 | cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES; |
| 107 | /* mbedtls doesn't recognize two-key Triple-DES as an algorithm, |
| 108 | * but two-key Triple-DES is functionally three-key Triple-DES |
| 109 | * with K1=K3, so that's how we present it to mbedtls. */ |
| 110 | if( key_bits == 128 ) |
| 111 | key_bits = 192; |
| 112 | break; |
| 113 | case PSA_KEY_TYPE_CAMELLIA: |
| 114 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA; |
| 115 | break; |
Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 116 | case PSA_KEY_TYPE_CHACHA20: |
| 117 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20; |
| 118 | break; |
| 119 | default: |
| 120 | return( NULL ); |
| 121 | } |
| 122 | if( cipher_id != NULL ) |
| 123 | *cipher_id = cipher_id_tmp; |
| 124 | |
| 125 | return( mbedtls_cipher_info_from_values( cipher_id_tmp, |
| 126 | (int) key_bits, mode ) ); |
| 127 | } |
| 128 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 129 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 130 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 131 | static psa_status_t psa_cipher_setup( |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 132 | mbedtls_psa_cipher_operation_t *operation, |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 133 | const psa_key_attributes_t *attributes, |
| 134 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 135 | psa_algorithm_t alg, |
| 136 | mbedtls_operation_t cipher_operation ) |
| 137 | { |
| 138 | int ret = 0; |
| 139 | size_t key_bits; |
| 140 | const mbedtls_cipher_info_t *cipher_info = NULL; |
| 141 | psa_key_type_t key_type = attributes->core.type; |
| 142 | |
| 143 | (void)key_buffer_size; |
| 144 | |
gabor-mezei-arm | 42cdb2a | 2021-04-12 15:47:35 +0200 | [diff] [blame] | 145 | mbedtls_cipher_init( &operation->ctx.cipher ); |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 146 | |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 147 | operation->alg = alg; |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 148 | key_bits = attributes->core.bits; |
| 149 | cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, |
| 150 | key_bits, NULL ); |
| 151 | if( cipher_info == NULL ) |
| 152 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 153 | |
gabor-mezei-arm | 42cdb2a | 2021-04-12 15:47:35 +0200 | [diff] [blame] | 154 | ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info ); |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 155 | if( ret != 0 ) |
| 156 | goto exit; |
| 157 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 158 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 159 | if( key_type == PSA_KEY_TYPE_DES && key_bits == 128 ) |
| 160 | { |
| 161 | /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */ |
| 162 | uint8_t keys[24]; |
| 163 | memcpy( keys, key_buffer, 16 ); |
| 164 | memcpy( keys + 16, key_buffer, 8 ); |
gabor-mezei-arm | 42cdb2a | 2021-04-12 15:47:35 +0200 | [diff] [blame] | 165 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 166 | keys, |
| 167 | 192, cipher_operation ); |
| 168 | } |
| 169 | else |
| 170 | #endif |
| 171 | { |
gabor-mezei-arm | 42cdb2a | 2021-04-12 15:47:35 +0200 | [diff] [blame] | 172 | ret = mbedtls_cipher_setkey( &operation->ctx.cipher, key_buffer, |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 173 | (int) key_bits, cipher_operation ); |
| 174 | } |
| 175 | if( ret != 0 ) |
| 176 | goto exit; |
| 177 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 178 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING) || \ |
| 179 | defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7) |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 180 | switch( alg ) |
| 181 | { |
| 182 | case PSA_ALG_CBC_NO_PADDING: |
gabor-mezei-arm | 42cdb2a | 2021-04-12 15:47:35 +0200 | [diff] [blame] | 183 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 184 | MBEDTLS_PADDING_NONE ); |
| 185 | break; |
| 186 | case PSA_ALG_CBC_PKCS7: |
gabor-mezei-arm | 42cdb2a | 2021-04-12 15:47:35 +0200 | [diff] [blame] | 187 | ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 188 | MBEDTLS_PADDING_PKCS7 ); |
| 189 | break; |
| 190 | default: |
| 191 | /* The algorithm doesn't involve padding. */ |
| 192 | ret = 0; |
| 193 | break; |
| 194 | } |
| 195 | if( ret != 0 ) |
| 196 | goto exit; |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 197 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING || |
| 198 | MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7 */ |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 199 | |
Ronald Cron | 6ad554c | 2021-03-26 09:29:09 +0100 | [diff] [blame] | 200 | operation->block_length = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 : |
| 201 | PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) ); |
Ronald Cron | c17e8a9 | 2021-03-19 14:12:26 +0100 | [diff] [blame] | 202 | operation->iv_length = PSA_CIPHER_IV_LENGTH( key_type, alg ); |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 203 | |
| 204 | exit: |
| 205 | return( mbedtls_to_psa_error( ret ) ); |
| 206 | } |
| 207 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 208 | psa_status_t mbedtls_psa_cipher_encrypt_setup( |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 209 | mbedtls_psa_cipher_operation_t *operation, |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 210 | const psa_key_attributes_t *attributes, |
| 211 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 212 | psa_algorithm_t alg ) |
| 213 | { |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 214 | return( psa_cipher_setup( operation, attributes, |
| 215 | key_buffer, key_buffer_size, |
| 216 | alg, MBEDTLS_ENCRYPT ) ); |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 217 | } |
| 218 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 219 | psa_status_t mbedtls_psa_cipher_decrypt_setup( |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 220 | mbedtls_psa_cipher_operation_t *operation, |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 221 | const psa_key_attributes_t *attributes, |
| 222 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 223 | psa_algorithm_t alg ) |
| 224 | { |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 225 | return( psa_cipher_setup( operation, attributes, |
| 226 | key_buffer, key_buffer_size, |
| 227 | alg, MBEDTLS_DECRYPT ) ); |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 228 | } |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 229 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 230 | psa_status_t mbedtls_psa_cipher_set_iv( |
| 231 | mbedtls_psa_cipher_operation_t *operation, |
| 232 | const uint8_t *iv, size_t iv_length ) |
Ronald Cron | 8287e6b | 2021-03-12 10:35:18 +0100 | [diff] [blame] | 233 | { |
Ronald Cron | 6ad554c | 2021-03-26 09:29:09 +0100 | [diff] [blame] | 234 | if( iv_length != operation->iv_length ) |
Ronald Cron | 8287e6b | 2021-03-12 10:35:18 +0100 | [diff] [blame] | 235 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 236 | |
| 237 | return( mbedtls_to_psa_error( |
gabor-mezei-arm | 42cdb2a | 2021-04-12 15:47:35 +0200 | [diff] [blame] | 238 | mbedtls_cipher_set_iv( &operation->ctx.cipher, |
Ronald Cron | 8287e6b | 2021-03-12 10:35:18 +0100 | [diff] [blame] | 239 | iv, iv_length ) ) ); |
| 240 | } |
| 241 | |
Gilles Peskine | 55dffe5 | 2021-09-13 09:33:28 +0200 | [diff] [blame] | 242 | /** Process input for which the algorithm is set to ECB mode. |
| 243 | * |
| 244 | * This requires manual processing, since the PSA API is defined as being |
| 245 | * able to process arbitrary-length calls to psa_cipher_update() with ECB mode, |
| 246 | * but the underlying mbedtls_cipher_update only takes full blocks. |
| 247 | * |
| 248 | * \param ctx The mbedtls cipher context to use. It must have been |
| 249 | * set up for ECB. |
| 250 | * \param[in] input The input plaintext or ciphertext to process. |
| 251 | * \param input_length The number of bytes to process from \p input. |
| 252 | * This does not need to be aligned to a block boundary. |
| 253 | * If there is a partial block at the end of the input, |
| 254 | * it is stored in \p ctx for future processing. |
Gilles Peskine | d87d873 | 2021-09-13 12:20:51 +0200 | [diff] [blame] | 255 | * \param output The buffer where the output is written. It must be |
| 256 | * at least `BS * floor((p + input_length) / BS)` bytes |
| 257 | * long, where `p` is the number of bytes in the |
| 258 | * unprocessed partial block in \p ctx (with |
| 259 | * `0 <= p <= BS - 1`) and `BS` is the block size. |
Gilles Peskine | 55dffe5 | 2021-09-13 09:33:28 +0200 | [diff] [blame] | 260 | * \param output_length On success, the number of bytes written to \p output. |
| 261 | * \c 0 on error. |
| 262 | * |
| 263 | * \return #PSA_SUCCESS or an error from a hardware accelerator |
| 264 | */ |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 265 | static psa_status_t psa_cipher_update_ecb( |
| 266 | mbedtls_cipher_context_t *ctx, |
| 267 | const uint8_t *input, |
| 268 | size_t input_length, |
| 269 | uint8_t *output, |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 270 | size_t *output_length ) |
| 271 | { |
| 272 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 273 | size_t block_size = ctx->cipher_info->block_size; |
| 274 | size_t internal_output_length = 0; |
| 275 | *output_length = 0; |
| 276 | |
| 277 | if( input_length == 0 ) |
| 278 | { |
| 279 | status = PSA_SUCCESS; |
| 280 | goto exit; |
| 281 | } |
| 282 | |
| 283 | if( ctx->unprocessed_len > 0 ) |
| 284 | { |
| 285 | /* Fill up to block size, and run the block if there's a full one. */ |
| 286 | size_t bytes_to_copy = block_size - ctx->unprocessed_len; |
| 287 | |
| 288 | if( input_length < bytes_to_copy ) |
| 289 | bytes_to_copy = input_length; |
| 290 | |
| 291 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), |
| 292 | input, bytes_to_copy ); |
| 293 | input_length -= bytes_to_copy; |
| 294 | input += bytes_to_copy; |
| 295 | ctx->unprocessed_len += bytes_to_copy; |
| 296 | |
| 297 | if( ctx->unprocessed_len == block_size ) |
| 298 | { |
| 299 | status = mbedtls_to_psa_error( |
| 300 | mbedtls_cipher_update( ctx, |
| 301 | ctx->unprocessed_data, |
| 302 | block_size, |
| 303 | output, &internal_output_length ) ); |
| 304 | |
| 305 | if( status != PSA_SUCCESS ) |
| 306 | goto exit; |
| 307 | |
| 308 | output += internal_output_length; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 309 | *output_length += internal_output_length; |
| 310 | ctx->unprocessed_len = 0; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | while( input_length >= block_size ) |
| 315 | { |
| 316 | /* Run all full blocks we have, one by one */ |
| 317 | status = mbedtls_to_psa_error( |
| 318 | mbedtls_cipher_update( ctx, input, |
| 319 | block_size, |
| 320 | output, &internal_output_length ) ); |
| 321 | |
| 322 | if( status != PSA_SUCCESS ) |
| 323 | goto exit; |
| 324 | |
| 325 | input_length -= block_size; |
| 326 | input += block_size; |
| 327 | |
| 328 | output += internal_output_length; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 329 | *output_length += internal_output_length; |
| 330 | } |
| 331 | |
| 332 | if( input_length > 0 ) |
| 333 | { |
| 334 | /* Save unprocessed bytes for later processing */ |
| 335 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), |
| 336 | input, input_length ); |
| 337 | ctx->unprocessed_len += input_length; |
| 338 | } |
| 339 | |
| 340 | status = PSA_SUCCESS; |
| 341 | |
| 342 | exit: |
| 343 | return( status ); |
| 344 | } |
| 345 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 346 | psa_status_t mbedtls_psa_cipher_update( |
| 347 | mbedtls_psa_cipher_operation_t *operation, |
| 348 | const uint8_t *input, size_t input_length, |
| 349 | uint8_t *output, size_t output_size, size_t *output_length ) |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 350 | { |
| 351 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 352 | size_t expected_output_size; |
| 353 | |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 354 | if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 355 | { |
| 356 | /* Take the unprocessed partial block left over from previous |
| 357 | * update calls, if any, plus the input to this call. Remove |
| 358 | * the last partial block, if any. You get the data that will be |
| 359 | * output in this call. */ |
| 360 | expected_output_size = |
gabor-mezei-arm | 42cdb2a | 2021-04-12 15:47:35 +0200 | [diff] [blame] | 361 | ( operation->ctx.cipher.unprocessed_len + input_length ) |
Ronald Cron | 6ad554c | 2021-03-26 09:29:09 +0100 | [diff] [blame] | 362 | / operation->block_length * operation->block_length; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 363 | } |
| 364 | else |
| 365 | { |
| 366 | expected_output_size = input_length; |
| 367 | } |
| 368 | |
| 369 | if( output_size < expected_output_size ) |
| 370 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 371 | |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 372 | if( operation->alg == PSA_ALG_ECB_NO_PADDING ) |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 373 | { |
| 374 | /* mbedtls_cipher_update has an API inconsistency: it will only |
| 375 | * process a single block at a time in ECB mode. Abstract away that |
| 376 | * inconsistency here to match the PSA API behaviour. */ |
gabor-mezei-arm | 42cdb2a | 2021-04-12 15:47:35 +0200 | [diff] [blame] | 377 | status = psa_cipher_update_ecb( &operation->ctx.cipher, |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 378 | input, |
| 379 | input_length, |
| 380 | output, |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 381 | output_length ); |
| 382 | } |
| 383 | else |
| 384 | { |
| 385 | status = mbedtls_to_psa_error( |
gabor-mezei-arm | 42cdb2a | 2021-04-12 15:47:35 +0200 | [diff] [blame] | 386 | mbedtls_cipher_update( &operation->ctx.cipher, input, |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 387 | input_length, output, output_length ) ); |
gabor-mezei-arm | 58c1727 | 2021-06-29 16:41:25 +0200 | [diff] [blame] | 388 | |
| 389 | if( *output_length > output_size ) |
gabor-mezei-arm | 00e54f1 | 2021-06-29 19:06:30 +0200 | [diff] [blame] | 390 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | return( status ); |
| 394 | } |
| 395 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 396 | psa_status_t mbedtls_psa_cipher_finish( |
| 397 | mbedtls_psa_cipher_operation_t *operation, |
| 398 | uint8_t *output, size_t output_size, size_t *output_length ) |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 399 | { |
| 400 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 401 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; |
| 402 | |
gabor-mezei-arm | 42cdb2a | 2021-04-12 15:47:35 +0200 | [diff] [blame] | 403 | if( operation->ctx.cipher.unprocessed_len != 0 ) |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 404 | { |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 405 | if( operation->alg == PSA_ALG_ECB_NO_PADDING || |
| 406 | operation->alg == PSA_ALG_CBC_NO_PADDING ) |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 407 | { |
| 408 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 409 | goto exit; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | status = mbedtls_to_psa_error( |
gabor-mezei-arm | 42cdb2a | 2021-04-12 15:47:35 +0200 | [diff] [blame] | 414 | mbedtls_cipher_finish( &operation->ctx.cipher, |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 415 | temp_output_buffer, |
| 416 | output_length ) ); |
| 417 | if( status != PSA_SUCCESS ) |
| 418 | goto exit; |
| 419 | |
| 420 | if( *output_length == 0 ) |
| 421 | ; /* Nothing to copy. Note that output may be NULL in this case. */ |
| 422 | else if( output_size >= *output_length ) |
| 423 | memcpy( output, temp_output_buffer, *output_length ); |
| 424 | else |
| 425 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 426 | |
| 427 | exit: |
| 428 | mbedtls_platform_zeroize( temp_output_buffer, |
| 429 | sizeof( temp_output_buffer ) ); |
| 430 | |
| 431 | return( status ); |
| 432 | } |
| 433 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 434 | psa_status_t mbedtls_psa_cipher_abort( |
| 435 | mbedtls_psa_cipher_operation_t *operation ) |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 436 | { |
Ronald Cron | 937dfee | 2021-03-10 09:17:32 +0100 | [diff] [blame] | 437 | /* Sanity check (shouldn't happen: operation->alg should |
| 438 | * always have been initialized to a valid value). */ |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 439 | if( ! PSA_ALG_IS_CIPHER( operation->alg ) ) |
Ronald Cron | 937dfee | 2021-03-10 09:17:32 +0100 | [diff] [blame] | 440 | return( PSA_ERROR_BAD_STATE ); |
| 441 | |
gabor-mezei-arm | 42cdb2a | 2021-04-12 15:47:35 +0200 | [diff] [blame] | 442 | mbedtls_cipher_free( &operation->ctx.cipher ); |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 443 | |
| 444 | return( PSA_SUCCESS ); |
| 445 | } |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 446 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 447 | psa_status_t mbedtls_psa_cipher_encrypt( |
| 448 | const psa_key_attributes_t *attributes, |
| 449 | const uint8_t *key_buffer, |
| 450 | size_t key_buffer_size, |
| 451 | psa_algorithm_t alg, |
| 452 | const uint8_t *input, |
| 453 | size_t input_length, |
| 454 | uint8_t *output, |
| 455 | size_t output_size, |
| 456 | size_t *output_length ) |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 457 | { |
| 458 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 459 | mbedtls_psa_cipher_operation_t operation = MBEDTLS_PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | e5ff8f4 | 2021-06-25 15:23:05 +0200 | [diff] [blame] | 460 | size_t olength, accumulated_length; |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 461 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 462 | status = mbedtls_psa_cipher_encrypt_setup( &operation, attributes, |
| 463 | key_buffer, key_buffer_size, |
| 464 | alg ); |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 465 | if( status != PSA_SUCCESS ) |
| 466 | goto exit; |
| 467 | |
gabor-mezei-arm | e5ff8f4 | 2021-06-25 15:23:05 +0200 | [diff] [blame] | 468 | accumulated_length = 0; |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 469 | if( operation.iv_length > 0 ) |
| 470 | { |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 471 | status = mbedtls_psa_cipher_set_iv( &operation, |
| 472 | output, operation.iv_length ); |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 473 | if( status != PSA_SUCCESS ) |
| 474 | goto exit; |
| 475 | |
gabor-mezei-arm | e5ff8f4 | 2021-06-25 15:23:05 +0200 | [diff] [blame] | 476 | accumulated_length = operation.iv_length; |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 477 | } |
| 478 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 479 | status = mbedtls_psa_cipher_update( &operation, input, input_length, |
| 480 | output + operation.iv_length, |
| 481 | output_size - operation.iv_length, |
| 482 | &olength ); |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 483 | if( status != PSA_SUCCESS ) |
| 484 | goto exit; |
| 485 | |
gabor-mezei-arm | 6158e28 | 2021-06-29 16:42:13 +0200 | [diff] [blame] | 486 | accumulated_length += olength; |
| 487 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 488 | status = mbedtls_psa_cipher_finish( &operation, output + accumulated_length, |
| 489 | output_size - accumulated_length, |
| 490 | &olength ); |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 491 | if( status != PSA_SUCCESS ) |
| 492 | goto exit; |
| 493 | |
gabor-mezei-arm | 00e54f1 | 2021-06-29 19:06:30 +0200 | [diff] [blame] | 494 | *output_length = accumulated_length + olength; |
gabor-mezei-arm | e5ff8f4 | 2021-06-25 15:23:05 +0200 | [diff] [blame] | 495 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 496 | exit: |
| 497 | if( status == PSA_SUCCESS ) |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 498 | status = mbedtls_psa_cipher_abort( &operation ); |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 499 | else |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 500 | mbedtls_psa_cipher_abort( &operation ); |
| 501 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 502 | return( status ); |
| 503 | } |
| 504 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 505 | psa_status_t mbedtls_psa_cipher_decrypt( |
| 506 | const psa_key_attributes_t *attributes, |
| 507 | const uint8_t *key_buffer, |
| 508 | size_t key_buffer_size, |
| 509 | psa_algorithm_t alg, |
| 510 | const uint8_t *input, |
| 511 | size_t input_length, |
| 512 | uint8_t *output, |
| 513 | size_t output_size, |
| 514 | size_t *output_length ) |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 515 | { |
| 516 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 517 | mbedtls_psa_cipher_operation_t operation = MBEDTLS_PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | e5ff8f4 | 2021-06-25 15:23:05 +0200 | [diff] [blame] | 518 | size_t olength, accumulated_length; |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 519 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 520 | status = mbedtls_psa_cipher_decrypt_setup( &operation, attributes, |
| 521 | key_buffer, key_buffer_size, |
| 522 | alg ); |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 523 | if( status != PSA_SUCCESS ) |
| 524 | goto exit; |
| 525 | |
| 526 | if( operation.iv_length > 0 ) |
| 527 | { |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 528 | status = mbedtls_psa_cipher_set_iv( &operation, |
| 529 | input, operation.iv_length ); |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 530 | if( status != PSA_SUCCESS ) |
| 531 | goto exit; |
| 532 | } |
| 533 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 534 | status = mbedtls_psa_cipher_update( &operation, input + operation.iv_length, |
| 535 | input_length - operation.iv_length, |
| 536 | output, output_size, &olength ); |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 537 | if( status != PSA_SUCCESS ) |
| 538 | goto exit; |
| 539 | |
gabor-mezei-arm | 6158e28 | 2021-06-29 16:42:13 +0200 | [diff] [blame] | 540 | accumulated_length = olength; |
gabor-mezei-arm | 258ae07 | 2021-06-25 15:25:38 +0200 | [diff] [blame] | 541 | |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 542 | status = mbedtls_psa_cipher_finish( &operation, output + accumulated_length, |
| 543 | output_size - accumulated_length, |
| 544 | &olength ); |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 545 | if( status != PSA_SUCCESS ) |
| 546 | goto exit; |
| 547 | |
gabor-mezei-arm | 00e54f1 | 2021-06-29 19:06:30 +0200 | [diff] [blame] | 548 | *output_length = accumulated_length + olength; |
gabor-mezei-arm | e5ff8f4 | 2021-06-25 15:23:05 +0200 | [diff] [blame] | 549 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 550 | exit: |
| 551 | if ( status == PSA_SUCCESS ) |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 552 | status = mbedtls_psa_cipher_abort( &operation ); |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 553 | else |
Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 554 | mbedtls_psa_cipher_abort( &operation ); |
| 555 | |
gabor-mezei-arm | a9449a0 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 556 | return( status ); |
| 557 | } |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 558 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ |
Ronald Cron | 8287e6b | 2021-03-12 10:35:18 +0100 | [diff] [blame] | 559 | |
Ronald Cron | 0ff5795 | 2021-03-08 16:46:35 +0100 | [diff] [blame] | 560 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |