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