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