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 | |
| 58 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) || defined(PSA_CRYPTO_DRIVER_TEST) |
| 59 | |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 60 | static psa_status_t cipher_setup( |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 61 | mbedtls_psa_cipher_operation_t *operation, |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 62 | const psa_key_attributes_t *attributes, |
| 63 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 64 | psa_algorithm_t alg, |
| 65 | mbedtls_operation_t cipher_operation ) |
| 66 | { |
| 67 | int ret = 0; |
| 68 | size_t key_bits; |
| 69 | const mbedtls_cipher_info_t *cipher_info = NULL; |
| 70 | psa_key_type_t key_type = attributes->core.type; |
| 71 | |
| 72 | (void)key_buffer_size; |
| 73 | |
| 74 | /* Proceed with initializing an mbed TLS cipher context if no driver is |
| 75 | * available for the given algorithm & key. */ |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 76 | mbedtls_cipher_init( &operation->cipher ); |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 77 | |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 78 | operation->alg = alg; |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 79 | key_bits = attributes->core.bits; |
| 80 | cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, |
| 81 | key_bits, NULL ); |
| 82 | if( cipher_info == NULL ) |
| 83 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 84 | |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 85 | ret = mbedtls_cipher_setup( &operation->cipher, cipher_info ); |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 86 | if( ret != 0 ) |
| 87 | goto exit; |
| 88 | |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame^] | 89 | #if defined(BUILTIN_KEY_TYPE_DES) |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 90 | if( key_type == PSA_KEY_TYPE_DES && key_bits == 128 ) |
| 91 | { |
| 92 | /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */ |
| 93 | uint8_t keys[24]; |
| 94 | memcpy( keys, key_buffer, 16 ); |
| 95 | memcpy( keys + 16, key_buffer, 8 ); |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 96 | ret = mbedtls_cipher_setkey( &operation->cipher, |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 97 | keys, |
| 98 | 192, cipher_operation ); |
| 99 | } |
| 100 | else |
| 101 | #endif |
| 102 | { |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 103 | ret = mbedtls_cipher_setkey( &operation->cipher, key_buffer, |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 104 | (int) key_bits, cipher_operation ); |
| 105 | } |
| 106 | if( ret != 0 ) |
| 107 | goto exit; |
| 108 | |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame^] | 109 | #if defined(BUILTIN_ALG_CBC_NO_PADDING) || \ |
| 110 | defined(BUILTIN_ALG_CBC_PKCS7) |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 111 | switch( alg ) |
| 112 | { |
| 113 | case PSA_ALG_CBC_NO_PADDING: |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 114 | ret = mbedtls_cipher_set_padding_mode( &operation->cipher, |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 115 | MBEDTLS_PADDING_NONE ); |
| 116 | break; |
| 117 | case PSA_ALG_CBC_PKCS7: |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 118 | ret = mbedtls_cipher_set_padding_mode( &operation->cipher, |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 119 | MBEDTLS_PADDING_PKCS7 ); |
| 120 | break; |
| 121 | default: |
| 122 | /* The algorithm doesn't involve padding. */ |
| 123 | ret = 0; |
| 124 | break; |
| 125 | } |
| 126 | if( ret != 0 ) |
| 127 | goto exit; |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame^] | 128 | #endif /* BUILTIN_ALG_CBC_NO_PADDING || BUILTIN_ALG_CBC_PKCS7 */ |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 129 | |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 130 | operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 : |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 131 | PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) ); |
| 132 | if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 && |
| 133 | alg != PSA_ALG_ECB_NO_PADDING ) |
| 134 | { |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 135 | operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ); |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 136 | } |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame^] | 137 | #if defined(BUILTIN_KEY_TYPE_CHACHA20) |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 138 | else |
| 139 | if( ( alg == PSA_ALG_STREAM_CIPHER ) && |
| 140 | ( key_type == PSA_KEY_TYPE_CHACHA20 ) ) |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 141 | operation->iv_size = 12; |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 142 | #endif |
| 143 | |
| 144 | exit: |
| 145 | return( mbedtls_to_psa_error( ret ) ); |
| 146 | } |
| 147 | |
Ronald Cron | 8287e6b | 2021-03-12 10:35:18 +0100 | [diff] [blame] | 148 | static psa_status_t cipher_encrypt_setup( |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 149 | mbedtls_psa_cipher_operation_t *operation, |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 150 | const psa_key_attributes_t *attributes, |
| 151 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 152 | psa_algorithm_t alg ) |
| 153 | { |
| 154 | return( cipher_setup( operation, attributes, |
| 155 | key_buffer, key_buffer_size, |
| 156 | alg, MBEDTLS_ENCRYPT ) ); |
| 157 | } |
| 158 | |
Ronald Cron | 8287e6b | 2021-03-12 10:35:18 +0100 | [diff] [blame] | 159 | static psa_status_t cipher_decrypt_setup( |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 160 | mbedtls_psa_cipher_operation_t *operation, |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 161 | const psa_key_attributes_t *attributes, |
| 162 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 163 | psa_algorithm_t alg ) |
| 164 | { |
| 165 | return( cipher_setup( operation, attributes, |
| 166 | key_buffer, key_buffer_size, |
| 167 | alg, MBEDTLS_DECRYPT ) ); |
| 168 | } |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 169 | |
Ronald Cron | 8287e6b | 2021-03-12 10:35:18 +0100 | [diff] [blame] | 170 | static psa_status_t cipher_set_iv( mbedtls_psa_cipher_operation_t *operation, |
| 171 | const uint8_t *iv, size_t iv_length ) |
| 172 | { |
| 173 | if( iv_length != operation->iv_size ) |
| 174 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 175 | |
| 176 | return( mbedtls_to_psa_error( |
| 177 | mbedtls_cipher_set_iv( &operation->cipher, |
| 178 | iv, iv_length ) ) ); |
| 179 | } |
| 180 | |
| 181 | static psa_status_t cipher_generate_iv( |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 182 | mbedtls_psa_cipher_operation_t *operation, |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 183 | uint8_t *iv, size_t iv_size, size_t *iv_length ) |
| 184 | { |
| 185 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 186 | |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 187 | if( iv_size < operation->iv_size ) |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 188 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 189 | |
| 190 | ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE, |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 191 | iv, operation->iv_size ); |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 192 | if( ret != 0 ) |
| 193 | return( mbedtls_to_psa_error( ret ) ); |
| 194 | |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 195 | *iv_length = operation->iv_size; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 196 | |
Ronald Cron | 8287e6b | 2021-03-12 10:35:18 +0100 | [diff] [blame] | 197 | return( cipher_set_iv( operation, iv, *iv_length ) ); |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /* Process input for which the algorithm is set to ECB mode. This requires |
| 201 | * manual processing, since the PSA API is defined as being able to process |
| 202 | * arbitrary-length calls to psa_cipher_update() with ECB mode, but the |
| 203 | * underlying mbedtls_cipher_update only takes full blocks. */ |
| 204 | static psa_status_t psa_cipher_update_ecb( |
| 205 | mbedtls_cipher_context_t *ctx, |
| 206 | const uint8_t *input, |
| 207 | size_t input_length, |
| 208 | uint8_t *output, |
| 209 | size_t output_size, |
| 210 | size_t *output_length ) |
| 211 | { |
| 212 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 213 | size_t block_size = ctx->cipher_info->block_size; |
| 214 | size_t internal_output_length = 0; |
| 215 | *output_length = 0; |
| 216 | |
| 217 | if( input_length == 0 ) |
| 218 | { |
| 219 | status = PSA_SUCCESS; |
| 220 | goto exit; |
| 221 | } |
| 222 | |
| 223 | if( ctx->unprocessed_len > 0 ) |
| 224 | { |
| 225 | /* Fill up to block size, and run the block if there's a full one. */ |
| 226 | size_t bytes_to_copy = block_size - ctx->unprocessed_len; |
| 227 | |
| 228 | if( input_length < bytes_to_copy ) |
| 229 | bytes_to_copy = input_length; |
| 230 | |
| 231 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), |
| 232 | input, bytes_to_copy ); |
| 233 | input_length -= bytes_to_copy; |
| 234 | input += bytes_to_copy; |
| 235 | ctx->unprocessed_len += bytes_to_copy; |
| 236 | |
| 237 | if( ctx->unprocessed_len == block_size ) |
| 238 | { |
| 239 | status = mbedtls_to_psa_error( |
| 240 | mbedtls_cipher_update( ctx, |
| 241 | ctx->unprocessed_data, |
| 242 | block_size, |
| 243 | output, &internal_output_length ) ); |
| 244 | |
| 245 | if( status != PSA_SUCCESS ) |
| 246 | goto exit; |
| 247 | |
| 248 | output += internal_output_length; |
| 249 | output_size -= internal_output_length; |
| 250 | *output_length += internal_output_length; |
| 251 | ctx->unprocessed_len = 0; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | while( input_length >= block_size ) |
| 256 | { |
| 257 | /* Run all full blocks we have, one by one */ |
| 258 | status = mbedtls_to_psa_error( |
| 259 | mbedtls_cipher_update( ctx, input, |
| 260 | block_size, |
| 261 | output, &internal_output_length ) ); |
| 262 | |
| 263 | if( status != PSA_SUCCESS ) |
| 264 | goto exit; |
| 265 | |
| 266 | input_length -= block_size; |
| 267 | input += block_size; |
| 268 | |
| 269 | output += internal_output_length; |
| 270 | output_size -= internal_output_length; |
| 271 | *output_length += internal_output_length; |
| 272 | } |
| 273 | |
| 274 | if( input_length > 0 ) |
| 275 | { |
| 276 | /* Save unprocessed bytes for later processing */ |
| 277 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), |
| 278 | input, input_length ); |
| 279 | ctx->unprocessed_len += input_length; |
| 280 | } |
| 281 | |
| 282 | status = PSA_SUCCESS; |
| 283 | |
| 284 | exit: |
| 285 | return( status ); |
| 286 | } |
| 287 | |
Ronald Cron | 8287e6b | 2021-03-12 10:35:18 +0100 | [diff] [blame] | 288 | static psa_status_t cipher_update( mbedtls_psa_cipher_operation_t *operation, |
| 289 | const uint8_t *input, |
| 290 | size_t input_length, |
| 291 | uint8_t *output, |
| 292 | size_t output_size, |
| 293 | size_t *output_length ) |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 294 | { |
| 295 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 296 | size_t expected_output_size; |
| 297 | |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 298 | if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) ) |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 299 | { |
| 300 | /* Take the unprocessed partial block left over from previous |
| 301 | * update calls, if any, plus the input to this call. Remove |
| 302 | * the last partial block, if any. You get the data that will be |
| 303 | * output in this call. */ |
| 304 | expected_output_size = |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 305 | ( operation->cipher.unprocessed_len + input_length ) |
| 306 | / operation->block_size * operation->block_size; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 307 | } |
| 308 | else |
| 309 | { |
| 310 | expected_output_size = input_length; |
| 311 | } |
| 312 | |
| 313 | if( output_size < expected_output_size ) |
| 314 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 315 | |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 316 | if( operation->alg == PSA_ALG_ECB_NO_PADDING ) |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 317 | { |
| 318 | /* mbedtls_cipher_update has an API inconsistency: it will only |
| 319 | * process a single block at a time in ECB mode. Abstract away that |
| 320 | * inconsistency here to match the PSA API behaviour. */ |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 321 | status = psa_cipher_update_ecb( &operation->cipher, |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 322 | input, |
| 323 | input_length, |
| 324 | output, |
| 325 | output_size, |
| 326 | output_length ); |
| 327 | } |
| 328 | else |
| 329 | { |
| 330 | status = mbedtls_to_psa_error( |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 331 | mbedtls_cipher_update( &operation->cipher, input, |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 332 | input_length, output, output_length ) ); |
| 333 | } |
| 334 | |
| 335 | return( status ); |
| 336 | } |
| 337 | |
Ronald Cron | 8287e6b | 2021-03-12 10:35:18 +0100 | [diff] [blame] | 338 | static psa_status_t cipher_finish( mbedtls_psa_cipher_operation_t *operation, |
| 339 | uint8_t *output, |
| 340 | size_t output_size, |
| 341 | size_t *output_length ) |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 342 | { |
| 343 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 344 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; |
| 345 | |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 346 | if( operation->cipher.unprocessed_len != 0 ) |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 347 | { |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 348 | if( operation->alg == PSA_ALG_ECB_NO_PADDING || |
| 349 | operation->alg == PSA_ALG_CBC_NO_PADDING ) |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 350 | { |
| 351 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 352 | goto exit; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | status = mbedtls_to_psa_error( |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 357 | mbedtls_cipher_finish( &operation->cipher, |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 358 | temp_output_buffer, |
| 359 | output_length ) ); |
| 360 | if( status != PSA_SUCCESS ) |
| 361 | goto exit; |
| 362 | |
| 363 | if( *output_length == 0 ) |
| 364 | ; /* Nothing to copy. Note that output may be NULL in this case. */ |
| 365 | else if( output_size >= *output_length ) |
| 366 | memcpy( output, temp_output_buffer, *output_length ); |
| 367 | else |
| 368 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 369 | |
| 370 | exit: |
| 371 | mbedtls_platform_zeroize( temp_output_buffer, |
| 372 | sizeof( temp_output_buffer ) ); |
| 373 | |
| 374 | return( status ); |
| 375 | } |
| 376 | |
Ronald Cron | 8287e6b | 2021-03-12 10:35:18 +0100 | [diff] [blame] | 377 | static psa_status_t cipher_abort( mbedtls_psa_cipher_operation_t *operation ) |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 378 | { |
Ronald Cron | 937dfee | 2021-03-10 09:17:32 +0100 | [diff] [blame] | 379 | /* Sanity check (shouldn't happen: operation->alg should |
| 380 | * always have been initialized to a valid value). */ |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 381 | if( ! PSA_ALG_IS_CIPHER( operation->alg ) ) |
Ronald Cron | 937dfee | 2021-03-10 09:17:32 +0100 | [diff] [blame] | 382 | return( PSA_ERROR_BAD_STATE ); |
| 383 | |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 384 | mbedtls_cipher_free( &operation->cipher ); |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 385 | |
| 386 | return( PSA_SUCCESS ); |
| 387 | } |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame^] | 388 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER || PSA_CRYPTO_DRIVER_TEST */ |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 389 | |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame^] | 390 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
Ronald Cron | 8287e6b | 2021-03-12 10:35:18 +0100 | [diff] [blame] | 391 | psa_status_t mbedtls_psa_cipher_encrypt_setup( |
| 392 | mbedtls_psa_cipher_operation_t *operation, |
| 393 | const psa_key_attributes_t *attributes, |
| 394 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 395 | psa_algorithm_t alg ) |
| 396 | { |
| 397 | return( cipher_encrypt_setup( |
| 398 | operation, attributes, key_buffer, key_buffer_size, alg ) ); |
| 399 | } |
| 400 | |
| 401 | psa_status_t mbedtls_psa_cipher_decrypt_setup( |
| 402 | mbedtls_psa_cipher_operation_t *operation, |
| 403 | const psa_key_attributes_t *attributes, |
| 404 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 405 | psa_algorithm_t alg ) |
| 406 | { |
| 407 | return( cipher_decrypt_setup( |
| 408 | operation, attributes, key_buffer, key_buffer_size, alg ) ); |
| 409 | } |
| 410 | |
| 411 | psa_status_t mbedtls_psa_cipher_generate_iv( |
| 412 | mbedtls_psa_cipher_operation_t *operation, |
| 413 | uint8_t *iv, size_t iv_size, size_t *iv_length ) |
| 414 | { |
| 415 | return( cipher_generate_iv( operation, iv, iv_size, iv_length ) ); |
| 416 | } |
| 417 | |
| 418 | psa_status_t mbedtls_psa_cipher_set_iv( mbedtls_psa_cipher_operation_t *operation, |
| 419 | const uint8_t *iv, |
| 420 | size_t iv_length ) |
| 421 | { |
| 422 | return( cipher_set_iv( operation, iv, iv_length ) ); |
| 423 | } |
| 424 | |
| 425 | psa_status_t mbedtls_psa_cipher_update( mbedtls_psa_cipher_operation_t *operation, |
| 426 | const uint8_t *input, |
| 427 | size_t input_length, |
| 428 | uint8_t *output, |
| 429 | size_t output_size, |
| 430 | size_t *output_length ) |
| 431 | { |
| 432 | return( cipher_update( operation, input, input_length, |
| 433 | output, output_size, output_length ) ); |
| 434 | } |
| 435 | |
| 436 | psa_status_t mbedtls_psa_cipher_finish( mbedtls_psa_cipher_operation_t *operation, |
| 437 | uint8_t *output, |
| 438 | size_t output_size, |
| 439 | size_t *output_length ) |
| 440 | { |
| 441 | return( cipher_finish( operation, output, output_size, output_length ) ); |
| 442 | } |
| 443 | |
| 444 | psa_status_t mbedtls_psa_cipher_abort( mbedtls_psa_cipher_operation_t *operation ) |
| 445 | { |
| 446 | return( cipher_abort( operation ) ); |
| 447 | } |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame^] | 448 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ |
Ronald Cron | 8287e6b | 2021-03-12 10:35:18 +0100 | [diff] [blame] | 449 | |
Ronald Cron | 3522e32 | 2021-03-12 11:08:49 +0100 | [diff] [blame] | 450 | /* |
| 451 | * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY. |
| 452 | */ |
| 453 | |
| 454 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 455 | psa_status_t mbedtls_transparent_test_driver_cipher_encrypt_setup( |
| 456 | mbedtls_psa_cipher_operation_t *operation, |
| 457 | const psa_key_attributes_t *attributes, |
| 458 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 459 | psa_algorithm_t alg ) |
| 460 | { |
| 461 | return( cipher_encrypt_setup( |
| 462 | operation, attributes, key_buffer, key_buffer_size, alg ) ); |
| 463 | } |
| 464 | |
| 465 | psa_status_t mbedtls_transparent_test_driver_cipher_decrypt_setup( |
| 466 | mbedtls_psa_cipher_operation_t *operation, |
| 467 | const psa_key_attributes_t *attributes, |
| 468 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 469 | psa_algorithm_t alg ) |
| 470 | { |
| 471 | return( cipher_decrypt_setup( |
| 472 | operation, attributes, key_buffer, key_buffer_size, alg ) ); |
| 473 | } |
| 474 | |
| 475 | psa_status_t mbedtls_transparent_test_driver_cipher_generate_iv( |
| 476 | mbedtls_psa_cipher_operation_t *operation, |
| 477 | uint8_t *iv, size_t iv_size, size_t *iv_length ) |
| 478 | { |
| 479 | return( cipher_generate_iv( operation, iv, iv_size, iv_length ) ); |
| 480 | } |
| 481 | |
| 482 | psa_status_t mbedtls_transparent_test_driver_cipher_set_iv( |
| 483 | mbedtls_psa_cipher_operation_t *operation, |
| 484 | const uint8_t *iv, size_t iv_length ) |
| 485 | { |
| 486 | return( cipher_set_iv( operation, iv, iv_length ) ); |
| 487 | } |
| 488 | |
| 489 | psa_status_t mbedtls_transparent_test_driver_cipher_update( |
| 490 | mbedtls_psa_cipher_operation_t *operation, |
| 491 | const uint8_t *input, size_t input_length, |
| 492 | uint8_t *output, size_t output_size, size_t *output_length ) |
| 493 | { |
| 494 | return( cipher_update( operation, input, input_length, |
| 495 | output, output_size, output_length ) ); |
| 496 | } |
| 497 | |
| 498 | psa_status_t mbedtls_transparent_test_driver_cipher_finish( |
| 499 | mbedtls_psa_cipher_operation_t *operation, |
| 500 | uint8_t *output, size_t output_size, size_t *output_length ) |
| 501 | { |
| 502 | return( cipher_finish( operation, output, output_size, output_length ) ); |
| 503 | } |
| 504 | |
| 505 | psa_status_t mbedtls_transparent_test_driver_cipher_abort( |
| 506 | mbedtls_psa_cipher_operation_t *operation ) |
| 507 | { |
| 508 | return( cipher_abort( operation ) ); |
| 509 | } |
| 510 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 511 | |
Ronald Cron | 0ff5795 | 2021-03-08 16:46:35 +0100 | [diff] [blame] | 512 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |