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