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