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