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