Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file cipher.c |
Paul Bakker | 7dc4c44 | 2014-02-01 22:50:26 +0100 | [diff] [blame] | 3 | * |
Gilles Peskine | e820c0a | 2023-08-03 17:45:20 +0200 | [diff] [blame] | 4 | * \brief Generic cipher wrapper for Mbed TLS |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 5 | * |
| 6 | * \author Adriaan de Jong <dejong@fox-it.com> |
| 7 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 8 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 9 | * SPDX-License-Identifier: Apache-2.0 |
| 10 | * |
| 11 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 12 | * not use this file except in compliance with the License. |
| 13 | * You may obtain a copy of the License at |
| 14 | * |
| 15 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 16 | * |
| 17 | * Unless required by applicable law or agreed to in writing, software |
| 18 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 19 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 | * See the License for the specific language governing permissions and |
| 21 | * limitations under the License. |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 24 | #include "common.h" |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 25 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 26 | #if defined(MBEDTLS_CIPHER_C) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 27 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 28 | #include "mbedtls/cipher.h" |
Chris Jones | daacb59 | 2021-03-09 17:03:29 +0000 | [diff] [blame] | 29 | #include "cipher_wrap.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 30 | #include "mbedtls/platform_util.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 31 | #include "mbedtls/error.h" |
Gabor Mezei | 765862c | 2021-10-19 12:22:25 +0200 | [diff] [blame] | 32 | #include "mbedtls/constant_time.h" |
Dave Rodgman | 6b7e2a5 | 2023-09-18 19:00:44 +0100 | [diff] [blame] | 33 | #include "constant_time_internal.h" |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 34 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 35 | #include <stdlib.h> |
| 36 | #include <string.h> |
| 37 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 38 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 39 | #include "mbedtls/chachapoly.h" |
Daniel King | 8fe4701 | 2016-05-17 20:33:28 -0300 | [diff] [blame] | 40 | #endif |
| 41 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 42 | #if defined(MBEDTLS_GCM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 43 | #include "mbedtls/gcm.h" |
Manuel Pégourié-Gonnard | 07f8fa5 | 2013-08-30 18:34:08 +0200 | [diff] [blame] | 44 | #endif |
| 45 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 46 | #if defined(MBEDTLS_CCM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 47 | #include "mbedtls/ccm.h" |
Manuel Pégourié-Gonnard | 4193695 | 2014-05-13 13:18:17 +0200 | [diff] [blame] | 48 | #endif |
| 49 | |
Daniel King | bd92062 | 2016-05-15 19:56:20 -0300 | [diff] [blame] | 50 | #if defined(MBEDTLS_CHACHA20_C) |
| 51 | #include "mbedtls/chacha20.h" |
| 52 | #endif |
| 53 | |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 54 | #if defined(MBEDTLS_CMAC_C) |
| 55 | #include "mbedtls/cmac.h" |
| 56 | #endif |
| 57 | |
Manuel Pégourié-Gonnard | 5c731b0 | 2023-06-21 10:37:30 +0200 | [diff] [blame] | 58 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
Hanno Becker | 4ccfc40 | 2018-11-09 16:10:57 +0000 | [diff] [blame] | 59 | #include "psa/crypto.h" |
Manuel Pégourié-Gonnard | 5c731b0 | 2023-06-21 10:37:30 +0200 | [diff] [blame] | 60 | #endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | 4ccfc40 | 2018-11-09 16:10:57 +0000 | [diff] [blame] | 61 | |
Jack Lloyd | ffdf288 | 2019-03-07 17:00:32 -0500 | [diff] [blame] | 62 | #if defined(MBEDTLS_NIST_KW_C) |
| 63 | #include "mbedtls/nist_kw.h" |
| 64 | #endif |
| 65 | |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 66 | #include "mbedtls/platform.h" |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 67 | |
Manuel Pégourié-Gonnard | dace82f | 2013-09-18 15:12:07 +0200 | [diff] [blame] | 68 | static int supported_init = 0; |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 69 | |
Dave Rodgman | 3b46b77 | 2023-06-24 13:25:06 +0100 | [diff] [blame] | 70 | static inline const mbedtls_cipher_base_t *mbedtls_cipher_get_base( |
| 71 | const mbedtls_cipher_info_t *info) |
| 72 | { |
Dave Rodgman | de3de77 | 2023-06-24 12:51:06 +0100 | [diff] [blame] | 73 | return mbedtls_cipher_base_lookup_table[info->base_idx]; |
| 74 | } |
| 75 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 76 | const int *mbedtls_cipher_list(void) |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 77 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 78 | const mbedtls_cipher_definition_t *def; |
Manuel Pégourié-Gonnard | dace82f | 2013-09-18 15:12:07 +0200 | [diff] [blame] | 79 | int *type; |
| 80 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 81 | if (!supported_init) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 82 | def = mbedtls_cipher_definitions; |
| 83 | type = mbedtls_cipher_supported; |
Manuel Pégourié-Gonnard | dace82f | 2013-09-18 15:12:07 +0200 | [diff] [blame] | 84 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 85 | while (def->type != 0) { |
Manuel Pégourié-Gonnard | dace82f | 2013-09-18 15:12:07 +0200 | [diff] [blame] | 86 | *type++ = (*def++).type; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 87 | } |
Manuel Pégourié-Gonnard | dace82f | 2013-09-18 15:12:07 +0200 | [diff] [blame] | 88 | |
| 89 | *type = 0; |
| 90 | |
| 91 | supported_init = 1; |
| 92 | } |
| 93 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 94 | return mbedtls_cipher_supported; |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Hanno Becker | 18597cd | 2018-11-09 16:36:33 +0000 | [diff] [blame] | 97 | const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 98 | const mbedtls_cipher_type_t cipher_type) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 99 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 100 | const mbedtls_cipher_definition_t *def; |
Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 101 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 102 | for (def = mbedtls_cipher_definitions; def->info != NULL; def++) { |
| 103 | if (def->type == cipher_type) { |
| 104 | return def->info; |
| 105 | } |
| 106 | } |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 107 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 108 | return NULL; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Hanno Becker | 18597cd | 2018-11-09 16:36:33 +0000 | [diff] [blame] | 111 | const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 112 | const char *cipher_name) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 113 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 114 | const mbedtls_cipher_definition_t *def; |
Manuel Pégourié-Gonnard | dace82f | 2013-09-18 15:12:07 +0200 | [diff] [blame] | 115 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 116 | if (NULL == cipher_name) { |
| 117 | return NULL; |
| 118 | } |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 119 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 120 | for (def = mbedtls_cipher_definitions; def->info != NULL; def++) { |
| 121 | if (!strcmp(def->info->name, cipher_name)) { |
| 122 | return def->info; |
| 123 | } |
| 124 | } |
Paul Bakker | fab5c82 | 2012-02-06 16:45:10 +0000 | [diff] [blame] | 125 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 126 | return NULL; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Hanno Becker | 18597cd | 2018-11-09 16:36:33 +0000 | [diff] [blame] | 129 | const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values( |
| 130 | const mbedtls_cipher_id_t cipher_id, |
| 131 | int key_bitlen, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 132 | const mbedtls_cipher_mode_t mode) |
Paul Bakker | f46b695 | 2013-09-09 00:08:26 +0200 | [diff] [blame] | 133 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 134 | const mbedtls_cipher_definition_t *def; |
Paul Bakker | f46b695 | 2013-09-09 00:08:26 +0200 | [diff] [blame] | 135 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 136 | for (def = mbedtls_cipher_definitions; def->info != NULL; def++) { |
Dave Rodgman | de3de77 | 2023-06-24 12:51:06 +0100 | [diff] [blame] | 137 | if (mbedtls_cipher_get_base(def->info)->cipher == cipher_id && |
Dave Rodgman | 9282d4f | 2023-06-24 11:03:04 +0100 | [diff] [blame] | 138 | mbedtls_cipher_info_get_key_bitlen(def->info) == (unsigned) key_bitlen && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 139 | def->info->mode == mode) { |
| 140 | return def->info; |
| 141 | } |
| 142 | } |
Paul Bakker | f46b695 | 2013-09-09 00:08:26 +0200 | [diff] [blame] | 143 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 144 | return NULL; |
Paul Bakker | f46b695 | 2013-09-09 00:08:26 +0200 | [diff] [blame] | 145 | } |
| 146 | |
Manuel Pégourié-Gonnard | efcc1f2 | 2023-06-07 13:20:24 +0200 | [diff] [blame] | 147 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
| 148 | static inline psa_key_type_t mbedtls_psa_translate_cipher_type( |
| 149 | mbedtls_cipher_type_t cipher) |
| 150 | { |
| 151 | switch (cipher) { |
| 152 | case MBEDTLS_CIPHER_AES_128_CCM: |
| 153 | case MBEDTLS_CIPHER_AES_192_CCM: |
| 154 | case MBEDTLS_CIPHER_AES_256_CCM: |
| 155 | case MBEDTLS_CIPHER_AES_128_CCM_STAR_NO_TAG: |
| 156 | case MBEDTLS_CIPHER_AES_192_CCM_STAR_NO_TAG: |
| 157 | case MBEDTLS_CIPHER_AES_256_CCM_STAR_NO_TAG: |
| 158 | case MBEDTLS_CIPHER_AES_128_GCM: |
| 159 | case MBEDTLS_CIPHER_AES_192_GCM: |
| 160 | case MBEDTLS_CIPHER_AES_256_GCM: |
| 161 | case MBEDTLS_CIPHER_AES_128_CBC: |
| 162 | case MBEDTLS_CIPHER_AES_192_CBC: |
| 163 | case MBEDTLS_CIPHER_AES_256_CBC: |
| 164 | case MBEDTLS_CIPHER_AES_128_ECB: |
| 165 | case MBEDTLS_CIPHER_AES_192_ECB: |
| 166 | case MBEDTLS_CIPHER_AES_256_ECB: |
| 167 | return PSA_KEY_TYPE_AES; |
| 168 | |
| 169 | /* ARIA not yet supported in PSA. */ |
| 170 | /* case MBEDTLS_CIPHER_ARIA_128_CCM: |
| 171 | case MBEDTLS_CIPHER_ARIA_192_CCM: |
| 172 | case MBEDTLS_CIPHER_ARIA_256_CCM: |
| 173 | case MBEDTLS_CIPHER_ARIA_128_CCM_STAR_NO_TAG: |
| 174 | case MBEDTLS_CIPHER_ARIA_192_CCM_STAR_NO_TAG: |
| 175 | case MBEDTLS_CIPHER_ARIA_256_CCM_STAR_NO_TAG: |
| 176 | case MBEDTLS_CIPHER_ARIA_128_GCM: |
| 177 | case MBEDTLS_CIPHER_ARIA_192_GCM: |
| 178 | case MBEDTLS_CIPHER_ARIA_256_GCM: |
| 179 | case MBEDTLS_CIPHER_ARIA_128_CBC: |
| 180 | case MBEDTLS_CIPHER_ARIA_192_CBC: |
| 181 | case MBEDTLS_CIPHER_ARIA_256_CBC: |
| 182 | return( PSA_KEY_TYPE_ARIA ); */ |
| 183 | |
| 184 | default: |
| 185 | return 0; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | static inline psa_algorithm_t mbedtls_psa_translate_cipher_mode( |
| 190 | mbedtls_cipher_mode_t mode, size_t taglen) |
| 191 | { |
| 192 | switch (mode) { |
| 193 | case MBEDTLS_MODE_ECB: |
| 194 | return PSA_ALG_ECB_NO_PADDING; |
| 195 | case MBEDTLS_MODE_GCM: |
| 196 | return PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, taglen); |
| 197 | case MBEDTLS_MODE_CCM: |
| 198 | return PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen); |
| 199 | case MBEDTLS_MODE_CCM_STAR_NO_TAG: |
| 200 | return PSA_ALG_CCM_STAR_NO_TAG; |
| 201 | case MBEDTLS_MODE_CBC: |
| 202 | if (taglen == 0) { |
| 203 | return PSA_ALG_CBC_NO_PADDING; |
| 204 | } else { |
| 205 | return 0; |
| 206 | } |
| 207 | default: |
| 208 | return 0; |
| 209 | } |
| 210 | } |
Manuel Pégourié-Gonnard | efcc1f2 | 2023-06-07 13:20:24 +0200 | [diff] [blame] | 211 | #endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */ |
| 212 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 213 | void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx) |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 214 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 215 | memset(ctx, 0, sizeof(mbedtls_cipher_context_t)); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 216 | } |
| 217 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 218 | void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx) |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 219 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 220 | if (ctx == NULL) { |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 221 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 222 | } |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 223 | |
Manuel Pégourié-Gonnard | 5c731b0 | 2023-06-21 10:37:30 +0200 | [diff] [blame] | 224 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 225 | if (ctx->psa_enabled == 1) { |
| 226 | if (ctx->cipher_ctx != NULL) { |
Hanno Becker | 6118e43 | 2018-11-09 16:47:20 +0000 | [diff] [blame] | 227 | mbedtls_cipher_context_psa * const cipher_psa = |
| 228 | (mbedtls_cipher_context_psa *) ctx->cipher_ctx; |
| 229 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 230 | if (cipher_psa->slot_state == MBEDTLS_CIPHER_PSA_KEY_OWNED) { |
Hanno Becker | edda8b8 | 2018-11-12 11:59:30 +0000 | [diff] [blame] | 231 | /* xxx_free() doesn't allow to return failures. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 232 | (void) psa_destroy_key(cipher_psa->slot); |
Hanno Becker | 6118e43 | 2018-11-09 16:47:20 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 235 | mbedtls_zeroize_and_free(cipher_psa, sizeof(*cipher_psa)); |
Hanno Becker | 6118e43 | 2018-11-09 16:47:20 +0000 | [diff] [blame] | 236 | } |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 237 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 238 | mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t)); |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 239 | return; |
| 240 | } |
Manuel Pégourié-Gonnard | 5c731b0 | 2023-06-21 10:37:30 +0200 | [diff] [blame] | 241 | #endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 242 | |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 243 | #if defined(MBEDTLS_CMAC_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 244 | if (ctx->cmac_ctx) { |
Tom Cosgrove | ca8c61b | 2023-07-17 15:17:40 +0100 | [diff] [blame] | 245 | mbedtls_zeroize_and_free(ctx->cmac_ctx, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 246 | sizeof(mbedtls_cmac_context_t)); |
Simon Butcher | 327398a | 2016-10-05 14:09:11 +0100 | [diff] [blame] | 247 | } |
| 248 | #endif |
| 249 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 250 | if (ctx->cipher_ctx) { |
Dave Rodgman | de3de77 | 2023-06-24 12:51:06 +0100 | [diff] [blame] | 251 | mbedtls_cipher_get_base(ctx->cipher_info)->ctx_free_func(ctx->cipher_ctx); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 252 | } |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 253 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 254 | mbedtls_platform_zeroize(ctx, sizeof(mbedtls_cipher_context_t)); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 255 | } |
| 256 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 257 | int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx, |
| 258 | const mbedtls_cipher_info_t *cipher_info) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 259 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 260 | if (cipher_info == NULL) { |
| 261 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
| 262 | } |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 263 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 264 | memset(ctx, 0, sizeof(mbedtls_cipher_context_t)); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 265 | |
Valerio Setti | bbc46b4 | 2023-10-26 09:00:21 +0200 | [diff] [blame^] | 266 | if (mbedtls_cipher_get_base(cipher_info)->ctx_alloc_func != NULL) { |
| 267 | ctx->cipher_ctx = mbedtls_cipher_get_base(cipher_info)->ctx_alloc_func(); |
| 268 | if (ctx->cipher_ctx == NULL) { |
| 269 | return MBEDTLS_ERR_CIPHER_ALLOC_FAILED; |
| 270 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 271 | } |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 272 | |
| 273 | ctx->cipher_info = cipher_info; |
| 274 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 275 | return 0; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Manuel Pégourié-Gonnard | 5c731b0 | 2023-06-21 10:37:30 +0200 | [diff] [blame] | 278 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 279 | int mbedtls_cipher_setup_psa(mbedtls_cipher_context_t *ctx, |
| 280 | const mbedtls_cipher_info_t *cipher_info, |
| 281 | size_t taglen) |
Hanno Becker | 4ccfc40 | 2018-11-09 16:10:57 +0000 | [diff] [blame] | 282 | { |
Hanno Becker | edda8b8 | 2018-11-12 11:59:30 +0000 | [diff] [blame] | 283 | psa_algorithm_t alg; |
| 284 | mbedtls_cipher_context_psa *cipher_psa; |
| 285 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 286 | if (NULL == cipher_info || NULL == ctx) { |
| 287 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
| 288 | } |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 289 | |
Hanno Becker | 4ee7e76 | 2018-11-17 22:00:38 +0000 | [diff] [blame] | 290 | /* Check that the underlying cipher mode and cipher type are |
| 291 | * supported by the underlying PSA Crypto implementation. */ |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 292 | alg = mbedtls_psa_translate_cipher_mode(((mbedtls_cipher_mode_t) cipher_info->mode), taglen); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 293 | if (alg == 0) { |
| 294 | return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
| 295 | } |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 296 | if (mbedtls_psa_translate_cipher_type(((mbedtls_cipher_type_t) cipher_info->type)) == 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 297 | return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
| 298 | } |
Hanno Becker | 6118e43 | 2018-11-09 16:47:20 +0000 | [diff] [blame] | 299 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 300 | memset(ctx, 0, sizeof(mbedtls_cipher_context_t)); |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 301 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 302 | cipher_psa = mbedtls_calloc(1, sizeof(mbedtls_cipher_context_psa)); |
| 303 | if (cipher_psa == NULL) { |
| 304 | return MBEDTLS_ERR_CIPHER_ALLOC_FAILED; |
| 305 | } |
Hanno Becker | edda8b8 | 2018-11-12 11:59:30 +0000 | [diff] [blame] | 306 | cipher_psa->alg = alg; |
| 307 | ctx->cipher_ctx = cipher_psa; |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 308 | ctx->cipher_info = cipher_info; |
| 309 | ctx->psa_enabled = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 310 | return 0; |
Hanno Becker | 4ccfc40 | 2018-11-09 16:10:57 +0000 | [diff] [blame] | 311 | } |
Manuel Pégourié-Gonnard | 5c731b0 | 2023-06-21 10:37:30 +0200 | [diff] [blame] | 312 | #endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | 4ccfc40 | 2018-11-09 16:10:57 +0000 | [diff] [blame] | 313 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 314 | int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx, |
| 315 | const unsigned char *key, |
| 316 | int key_bitlen, |
| 317 | const mbedtls_operation_t operation) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 318 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 319 | if (operation != MBEDTLS_ENCRYPT && operation != MBEDTLS_DECRYPT) { |
Tuvshinzaya Erdenekhuu | 80a6af6 | 2022-08-05 15:31:57 +0100 | [diff] [blame] | 320 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 321 | } |
| 322 | if (ctx->cipher_info == NULL) { |
| 323 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
| 324 | } |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 325 | |
Manuel Pégourié-Gonnard | 5c731b0 | 2023-06-21 10:37:30 +0200 | [diff] [blame] | 326 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 327 | if (ctx->psa_enabled == 1) { |
Hanno Becker | edda8b8 | 2018-11-12 11:59:30 +0000 | [diff] [blame] | 328 | mbedtls_cipher_context_psa * const cipher_psa = |
| 329 | (mbedtls_cipher_context_psa *) ctx->cipher_ctx; |
| 330 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 331 | size_t const key_bytelen = ((size_t) key_bitlen + 7) / 8; |
Hanno Becker | edda8b8 | 2018-11-12 11:59:30 +0000 | [diff] [blame] | 332 | |
| 333 | psa_status_t status; |
| 334 | psa_key_type_t key_type; |
Gilles Peskine | d2d45c1 | 2019-05-27 14:53:13 +0200 | [diff] [blame] | 335 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Hanno Becker | edda8b8 | 2018-11-12 11:59:30 +0000 | [diff] [blame] | 336 | |
| 337 | /* PSA Crypto API only accepts byte-aligned keys. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 338 | if (key_bitlen % 8 != 0) { |
| 339 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
| 340 | } |
Hanno Becker | edda8b8 | 2018-11-12 11:59:30 +0000 | [diff] [blame] | 341 | |
| 342 | /* Don't allow keys to be set multiple times. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 343 | if (cipher_psa->slot_state != MBEDTLS_CIPHER_PSA_KEY_UNSET) { |
| 344 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
| 345 | } |
Hanno Becker | edda8b8 | 2018-11-12 11:59:30 +0000 | [diff] [blame] | 346 | |
Andrzej Kurek | c750932 | 2019-01-08 09:36:01 -0500 | [diff] [blame] | 347 | key_type = mbedtls_psa_translate_cipher_type( |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 348 | ((mbedtls_cipher_type_t) ctx->cipher_info->type)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 349 | if (key_type == 0) { |
| 350 | return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
| 351 | } |
| 352 | psa_set_key_type(&attributes, key_type); |
Hanno Becker | a395d8f | 2018-11-12 13:33:16 +0000 | [diff] [blame] | 353 | |
| 354 | /* Mbed TLS' cipher layer doesn't enforce the mode of operation |
Andrzej Kurek | f410a5c | 2019-01-15 03:33:35 -0500 | [diff] [blame] | 355 | * (encrypt vs. decrypt): it is possible to setup a key for encryption |
| 356 | * and use it for AEAD decryption. Until tests relying on this |
| 357 | * are changed, allow any usage in PSA. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 358 | psa_set_key_usage_flags(&attributes, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 359 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT); |
| 360 | psa_set_key_algorithm(&attributes, cipher_psa->alg); |
Hanno Becker | edda8b8 | 2018-11-12 11:59:30 +0000 | [diff] [blame] | 361 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 362 | status = psa_import_key(&attributes, key, key_bytelen, |
| 363 | &cipher_psa->slot); |
| 364 | switch (status) { |
Gilles Peskine | d2d45c1 | 2019-05-27 14:53:13 +0200 | [diff] [blame] | 365 | case PSA_SUCCESS: |
| 366 | break; |
| 367 | case PSA_ERROR_INSUFFICIENT_MEMORY: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 368 | return MBEDTLS_ERR_CIPHER_ALLOC_FAILED; |
Gilles Peskine | d2d45c1 | 2019-05-27 14:53:13 +0200 | [diff] [blame] | 369 | case PSA_ERROR_NOT_SUPPORTED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 370 | return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Gilles Peskine | d2d45c1 | 2019-05-27 14:53:13 +0200 | [diff] [blame] | 371 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 372 | return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
Gilles Peskine | d2d45c1 | 2019-05-27 14:53:13 +0200 | [diff] [blame] | 373 | } |
| 374 | /* Indicate that we own the key slot and need to |
| 375 | * destroy it in mbedtls_cipher_free(). */ |
| 376 | cipher_psa->slot_state = MBEDTLS_CIPHER_PSA_KEY_OWNED; |
Hanno Becker | edda8b8 | 2018-11-12 11:59:30 +0000 | [diff] [blame] | 377 | |
| 378 | ctx->key_bitlen = key_bitlen; |
| 379 | ctx->operation = operation; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 380 | return 0; |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 381 | } |
Manuel Pégourié-Gonnard | 5c731b0 | 2023-06-21 10:37:30 +0200 | [diff] [blame] | 382 | #endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 383 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 384 | if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN) == 0 && |
Dave Rodgman | 9282d4f | 2023-06-24 11:03:04 +0100 | [diff] [blame] | 385 | (int) mbedtls_cipher_info_get_key_bitlen(ctx->cipher_info) != key_bitlen) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 386 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 398c57b | 2014-06-23 12:10:59 +0200 | [diff] [blame] | 387 | } |
Manuel Pégourié-Gonnard | dd0f57f | 2013-09-16 11:47:43 +0200 | [diff] [blame] | 388 | |
Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 389 | ctx->key_bitlen = key_bitlen; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 390 | ctx->operation = operation; |
| 391 | |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 392 | /* |
Simon Butcher | 8c0fd1e | 2018-04-22 22:58:07 +0100 | [diff] [blame] | 393 | * For OFB, CFB and CTR mode always use the encryption key schedule |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 394 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 395 | if (MBEDTLS_ENCRYPT == operation || |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 396 | MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) || |
| 397 | MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) || |
| 398 | MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) { |
Dave Rodgman | de3de77 | 2023-06-24 12:51:06 +0100 | [diff] [blame] | 399 | return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_enc_func(ctx->cipher_ctx, key, |
Dave Rodgman | 3b46b77 | 2023-06-24 13:25:06 +0100 | [diff] [blame] | 400 | ctx->key_bitlen); |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 401 | } |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 402 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 403 | if (MBEDTLS_DECRYPT == operation) { |
Dave Rodgman | de3de77 | 2023-06-24 12:51:06 +0100 | [diff] [blame] | 404 | return mbedtls_cipher_get_base(ctx->cipher_info)->setkey_dec_func(ctx->cipher_ctx, key, |
Dave Rodgman | 3b46b77 | 2023-06-24 13:25:06 +0100 | [diff] [blame] | 405 | ctx->key_bitlen); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 406 | } |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 407 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 408 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 411 | int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx, |
| 412 | const unsigned char *iv, |
| 413 | size_t iv_len) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 414 | { |
Manuel Pégourié-Gonnard | a235b5b | 2013-09-03 13:25:52 +0200 | [diff] [blame] | 415 | size_t actual_iv_size; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 416 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 417 | if (ctx->cipher_info == NULL) { |
| 418 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
| 419 | } |
Manuel Pégourié-Gonnard | 5c731b0 | 2023-06-21 10:37:30 +0200 | [diff] [blame] | 420 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 421 | if (ctx->psa_enabled == 1) { |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 422 | /* While PSA Crypto has an API for multipart |
| 423 | * operations, we currently don't make it |
| 424 | * accessible through the cipher layer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 425 | return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 426 | } |
Manuel Pégourié-Gonnard | 5c731b0 | 2023-06-21 10:37:30 +0200 | [diff] [blame] | 427 | #endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 428 | |
Manuel Pégourié-Gonnard | e0dca4a | 2013-10-24 16:54:25 +0200 | [diff] [blame] | 429 | /* avoid buffer overflow in ctx->iv */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 430 | if (iv_len > MBEDTLS_MAX_IV_LENGTH) { |
| 431 | return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
| 432 | } |
Manuel Pégourié-Gonnard | e0dca4a | 2013-10-24 16:54:25 +0200 | [diff] [blame] | 433 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 434 | if ((ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN) != 0) { |
Manuel Pégourié-Gonnard | a235b5b | 2013-09-03 13:25:52 +0200 | [diff] [blame] | 435 | actual_iv_size = iv_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 436 | } else { |
Dave Rodgman | bb521fd | 2023-06-24 11:21:25 +0100 | [diff] [blame] | 437 | actual_iv_size = mbedtls_cipher_info_get_iv_size(ctx->cipher_info); |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 438 | |
Manuel Pégourié-Gonnard | e0dca4a | 2013-10-24 16:54:25 +0200 | [diff] [blame] | 439 | /* avoid reading past the end of input buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 440 | if (actual_iv_size > iv_len) { |
| 441 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
| 442 | } |
Manuel Pégourié-Gonnard | e0dca4a | 2013-10-24 16:54:25 +0200 | [diff] [blame] | 443 | } |
| 444 | |
Daniel King | bd92062 | 2016-05-15 19:56:20 -0300 | [diff] [blame] | 445 | #if defined(MBEDTLS_CHACHA20_C) |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 446 | if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20) { |
Andrzej Kurek | 33ca6af | 2021-12-01 21:58:05 +0100 | [diff] [blame] | 447 | /* Even though the actual_iv_size is overwritten with a correct value |
| 448 | * of 12 from the cipher info, return an error to indicate that |
| 449 | * the input iv_len is wrong. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 450 | if (iv_len != 12) { |
| 451 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
| 452 | } |
Andrzej Kurek | 33ca6af | 2021-12-01 21:58:05 +0100 | [diff] [blame] | 453 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 454 | if (0 != mbedtls_chacha20_starts((mbedtls_chacha20_context *) ctx->cipher_ctx, |
| 455 | iv, |
| 456 | 0U)) { /* Initial counter value */ |
| 457 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
Daniel King | bd92062 | 2016-05-15 19:56:20 -0300 | [diff] [blame] | 458 | } |
| 459 | } |
Andrzej Kurek | 63439ed | 2021-12-01 22:19:33 +0100 | [diff] [blame] | 460 | #if defined(MBEDTLS_CHACHAPOLY_C) |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 461 | if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20_POLY1305 && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 462 | iv_len != 12) { |
| 463 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
| 464 | } |
Andrzej Kurek | 63439ed | 2021-12-01 22:19:33 +0100 | [diff] [blame] | 465 | #endif |
Daniel King | bd92062 | 2016-05-15 19:56:20 -0300 | [diff] [blame] | 466 | #endif |
| 467 | |
Gilles Peskine | 295fc13 | 2021-04-15 18:32:23 +0200 | [diff] [blame] | 468 | #if defined(MBEDTLS_GCM_C) |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 469 | if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 470 | return mbedtls_gcm_starts((mbedtls_gcm_context *) ctx->cipher_ctx, |
| 471 | ctx->operation, |
| 472 | iv, iv_len); |
Gilles Peskine | 295fc13 | 2021-04-15 18:32:23 +0200 | [diff] [blame] | 473 | } |
| 474 | #endif |
| 475 | |
Mateusz Starzyk | 594215b | 2021-10-14 12:23:06 +0200 | [diff] [blame] | 476 | #if defined(MBEDTLS_CCM_C) |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 477 | if (MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) { |
Mateusz Starzyk | 594215b | 2021-10-14 12:23:06 +0200 | [diff] [blame] | 478 | int set_lengths_result; |
| 479 | int ccm_star_mode; |
| 480 | |
| 481 | set_lengths_result = mbedtls_ccm_set_lengths( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 482 | (mbedtls_ccm_context *) ctx->cipher_ctx, |
| 483 | 0, 0, 0); |
| 484 | if (set_lengths_result != 0) { |
Mateusz Starzyk | 594215b | 2021-10-14 12:23:06 +0200 | [diff] [blame] | 485 | return set_lengths_result; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 486 | } |
Mateusz Starzyk | 594215b | 2021-10-14 12:23:06 +0200 | [diff] [blame] | 487 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 488 | if (ctx->operation == MBEDTLS_DECRYPT) { |
Mateusz Starzyk | 594215b | 2021-10-14 12:23:06 +0200 | [diff] [blame] | 489 | ccm_star_mode = MBEDTLS_CCM_STAR_DECRYPT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 490 | } else if (ctx->operation == MBEDTLS_ENCRYPT) { |
Mateusz Starzyk | 594215b | 2021-10-14 12:23:06 +0200 | [diff] [blame] | 491 | ccm_star_mode = MBEDTLS_CCM_STAR_ENCRYPT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 492 | } else { |
Mateusz Starzyk | 594215b | 2021-10-14 12:23:06 +0200 | [diff] [blame] | 493 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 494 | } |
Mateusz Starzyk | 594215b | 2021-10-14 12:23:06 +0200 | [diff] [blame] | 495 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 496 | return mbedtls_ccm_starts((mbedtls_ccm_context *) ctx->cipher_ctx, |
| 497 | ccm_star_mode, |
| 498 | iv, iv_len); |
Mateusz Starzyk | 594215b | 2021-10-14 12:23:06 +0200 | [diff] [blame] | 499 | } |
| 500 | #endif |
| 501 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 502 | if (actual_iv_size != 0) { |
| 503 | memcpy(ctx->iv, iv, actual_iv_size); |
Ron Eldor | 4e64e0b | 2017-09-25 18:22:32 +0300 | [diff] [blame] | 504 | ctx->iv_size = actual_iv_size; |
| 505 | } |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 506 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 507 | return 0; |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 508 | } |
| 509 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 510 | int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx) |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 511 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 512 | if (ctx->cipher_info == NULL) { |
| 513 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
| 514 | } |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 515 | |
Manuel Pégourié-Gonnard | 5c731b0 | 2023-06-21 10:37:30 +0200 | [diff] [blame] | 516 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 517 | if (ctx->psa_enabled == 1) { |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 518 | /* We don't support resetting PSA-based |
| 519 | * cipher contexts, yet. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 520 | return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 521 | } |
Manuel Pégourié-Gonnard | 5c731b0 | 2023-06-21 10:37:30 +0200 | [diff] [blame] | 522 | #endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 523 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 524 | ctx->unprocessed_len = 0; |
| 525 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 526 | return 0; |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 527 | } |
| 528 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 529 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 530 | int mbedtls_cipher_update_ad(mbedtls_cipher_context_t *ctx, |
| 531 | const unsigned char *ad, size_t ad_len) |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 532 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 533 | if (ctx->cipher_info == NULL) { |
| 534 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
| 535 | } |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 536 | |
Manuel Pégourié-Gonnard | 5c731b0 | 2023-06-21 10:37:30 +0200 | [diff] [blame] | 537 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 538 | if (ctx->psa_enabled == 1) { |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 539 | /* While PSA Crypto has an API for multipart |
| 540 | * operations, we currently don't make it |
| 541 | * accessible through the cipher layer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 542 | return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 543 | } |
Manuel Pégourié-Gonnard | 5c731b0 | 2023-06-21 10:37:30 +0200 | [diff] [blame] | 544 | #endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 545 | |
Daniel King | 8fe4701 | 2016-05-17 20:33:28 -0300 | [diff] [blame] | 546 | #if defined(MBEDTLS_GCM_C) |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 547 | if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 548 | return mbedtls_gcm_update_ad((mbedtls_gcm_context *) ctx->cipher_ctx, |
| 549 | ad, ad_len); |
Manuel Pégourié-Gonnard | 07f8fa5 | 2013-08-30 18:34:08 +0200 | [diff] [blame] | 550 | } |
Daniel King | 8fe4701 | 2016-05-17 20:33:28 -0300 | [diff] [blame] | 551 | #endif |
| 552 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 553 | #if defined(MBEDTLS_CHACHAPOLY_C) |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 554 | if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) { |
Daniel King | 8fe4701 | 2016-05-17 20:33:28 -0300 | [diff] [blame] | 555 | int result; |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 556 | mbedtls_chachapoly_mode_t mode; |
Daniel King | 8fe4701 | 2016-05-17 20:33:28 -0300 | [diff] [blame] | 557 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 558 | mode = (ctx->operation == MBEDTLS_ENCRYPT) |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 559 | ? MBEDTLS_CHACHAPOLY_ENCRYPT |
| 560 | : MBEDTLS_CHACHAPOLY_DECRYPT; |
Daniel King | 8fe4701 | 2016-05-17 20:33:28 -0300 | [diff] [blame] | 561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 562 | result = mbedtls_chachapoly_starts((mbedtls_chachapoly_context *) ctx->cipher_ctx, |
| 563 | ctx->iv, |
| 564 | mode); |
| 565 | if (result != 0) { |
| 566 | return result; |
| 567 | } |
Daniel King | 8fe4701 | 2016-05-17 20:33:28 -0300 | [diff] [blame] | 568 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 569 | return mbedtls_chachapoly_update_aad((mbedtls_chachapoly_context *) ctx->cipher_ctx, |
| 570 | ad, ad_len); |
Daniel King | 8fe4701 | 2016-05-17 20:33:28 -0300 | [diff] [blame] | 571 | } |
| 572 | #endif |
Manuel Pégourié-Gonnard | 07f8fa5 | 2013-08-30 18:34:08 +0200 | [diff] [blame] | 573 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 574 | return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 575 | } |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 576 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 577 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 578 | int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input, |
| 579 | size_t ilen, unsigned char *output, size_t *olen) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 580 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 581 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 582 | size_t block_size; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 583 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 584 | if (ctx->cipher_info == NULL) { |
| 585 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
| 586 | } |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 587 | |
Manuel Pégourié-Gonnard | 5c731b0 | 2023-06-21 10:37:30 +0200 | [diff] [blame] | 588 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 589 | if (ctx->psa_enabled == 1) { |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 590 | /* While PSA Crypto has an API for multipart |
| 591 | * operations, we currently don't make it |
| 592 | * accessible through the cipher layer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 593 | return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 594 | } |
Manuel Pégourié-Gonnard | 5c731b0 | 2023-06-21 10:37:30 +0200 | [diff] [blame] | 595 | #endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 596 | |
Paul Bakker | 6c21276 | 2013-12-16 15:24:50 +0100 | [diff] [blame] | 597 | *olen = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 598 | block_size = mbedtls_cipher_get_block_size(ctx); |
| 599 | if (0 == block_size) { |
| 600 | return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT; |
Gilles Peskine | a2bdcb9 | 2020-01-21 15:02:14 +0100 | [diff] [blame] | 601 | } |
Paul Bakker | 6c21276 | 2013-12-16 15:24:50 +0100 | [diff] [blame] | 602 | |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 603 | if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_ECB) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 604 | if (ilen != block_size) { |
| 605 | return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED; |
| 606 | } |
Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 607 | |
| 608 | *olen = ilen; |
| 609 | |
Dave Rodgman | de3de77 | 2023-06-24 12:51:06 +0100 | [diff] [blame] | 610 | if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ecb_func(ctx->cipher_ctx, |
Dave Rodgman | 3b46b77 | 2023-06-24 13:25:06 +0100 | [diff] [blame] | 611 | ctx->operation, input, |
| 612 | output))) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 613 | return ret; |
Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 614 | } |
| 615 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 616 | return 0; |
Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 617 | } |
| 618 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 619 | #if defined(MBEDTLS_GCM_C) |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 620 | if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_GCM) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 621 | return mbedtls_gcm_update((mbedtls_gcm_context *) ctx->cipher_ctx, |
| 622 | input, ilen, |
| 623 | output, ilen, olen); |
Manuel Pégourié-Gonnard | b8bd593 | 2013-09-05 13:38:15 +0200 | [diff] [blame] | 624 | } |
| 625 | #endif |
| 626 | |
Mateusz Starzyk | 594215b | 2021-10-14 12:23:06 +0200 | [diff] [blame] | 627 | #if defined(MBEDTLS_CCM_C) |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 628 | if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CCM_STAR_NO_TAG) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 629 | return mbedtls_ccm_update((mbedtls_ccm_context *) ctx->cipher_ctx, |
| 630 | input, ilen, |
| 631 | output, ilen, olen); |
Mateusz Starzyk | 594215b | 2021-10-14 12:23:06 +0200 | [diff] [blame] | 632 | } |
| 633 | #endif |
| 634 | |
Manuel Pégourié-Gonnard | 32902e6 | 2018-05-10 12:30:19 +0200 | [diff] [blame] | 635 | #if defined(MBEDTLS_CHACHAPOLY_C) |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 636 | if (((mbedtls_cipher_type_t) ctx->cipher_info->type) == MBEDTLS_CIPHER_CHACHA20_POLY1305) { |
Manuel Pégourié-Gonnard | 32902e6 | 2018-05-10 12:30:19 +0200 | [diff] [blame] | 637 | *olen = ilen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 638 | return mbedtls_chachapoly_update((mbedtls_chachapoly_context *) ctx->cipher_ctx, |
| 639 | ilen, input, output); |
Manuel Pégourié-Gonnard | 32902e6 | 2018-05-10 12:30:19 +0200 | [diff] [blame] | 640 | } |
| 641 | #endif |
| 642 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 643 | if (input == output && |
| 644 | (ctx->unprocessed_len != 0 || ilen % block_size)) { |
| 645 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 646 | } |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 647 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 648 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 649 | if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CBC) { |
Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 650 | size_t copy_len = 0; |
| 651 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 652 | /* |
| 653 | * If there is not enough data for a full block, cache it. |
| 654 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 655 | if ((ctx->operation == MBEDTLS_DECRYPT && NULL != ctx->add_padding && |
| 656 | ilen <= block_size - ctx->unprocessed_len) || |
| 657 | (ctx->operation == MBEDTLS_DECRYPT && NULL == ctx->add_padding && |
| 658 | ilen < block_size - ctx->unprocessed_len) || |
| 659 | (ctx->operation == MBEDTLS_ENCRYPT && |
| 660 | ilen < block_size - ctx->unprocessed_len)) { |
| 661 | memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input, |
| 662 | ilen); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 663 | |
| 664 | ctx->unprocessed_len += ilen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 665 | return 0; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | /* |
| 669 | * Process cached data first |
| 670 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 671 | if (0 != ctx->unprocessed_len) { |
Janos Follath | 98e28a7 | 2016-05-31 14:03:54 +0100 | [diff] [blame] | 672 | copy_len = block_size - ctx->unprocessed_len; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 673 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 674 | memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), input, |
| 675 | copy_len); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 676 | |
Dave Rodgman | de3de77 | 2023-06-24 12:51:06 +0100 | [diff] [blame] | 677 | if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx, |
Dave Rodgman | 3b46b77 | 2023-06-24 13:25:06 +0100 | [diff] [blame] | 678 | ctx->operation, |
| 679 | block_size, ctx->iv, |
| 680 | ctx-> |
| 681 | unprocessed_data, |
| 682 | output))) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 683 | return ret; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Janos Follath | 98e28a7 | 2016-05-31 14:03:54 +0100 | [diff] [blame] | 686 | *olen += block_size; |
| 687 | output += block_size; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 688 | ctx->unprocessed_len = 0; |
| 689 | |
| 690 | input += copy_len; |
| 691 | ilen -= copy_len; |
| 692 | } |
| 693 | |
| 694 | /* |
| 695 | * Cache final, incomplete block |
| 696 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 697 | if (0 != ilen) { |
Andy Leiserson | 79e7789 | 2017-04-28 20:01:49 -0700 | [diff] [blame] | 698 | /* Encryption: only cache partial blocks |
| 699 | * Decryption w/ padding: always keep at least one whole block |
| 700 | * Decryption w/o padding: only cache partial blocks |
| 701 | */ |
Janos Follath | 98e28a7 | 2016-05-31 14:03:54 +0100 | [diff] [blame] | 702 | copy_len = ilen % block_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 703 | if (copy_len == 0 && |
Andy Leiserson | 79e7789 | 2017-04-28 20:01:49 -0700 | [diff] [blame] | 704 | ctx->operation == MBEDTLS_DECRYPT && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 705 | NULL != ctx->add_padding) { |
Janos Follath | 98e28a7 | 2016-05-31 14:03:54 +0100 | [diff] [blame] | 706 | copy_len = block_size; |
Andy Leiserson | 79e7789 | 2017-04-28 20:01:49 -0700 | [diff] [blame] | 707 | } |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 708 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 709 | memcpy(ctx->unprocessed_data, &(input[ilen - copy_len]), |
| 710 | copy_len); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 711 | |
| 712 | ctx->unprocessed_len += copy_len; |
| 713 | ilen -= copy_len; |
| 714 | } |
| 715 | |
| 716 | /* |
| 717 | * Process remaining full blocks |
| 718 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 719 | if (ilen) { |
Dave Rodgman | de3de77 | 2023-06-24 12:51:06 +0100 | [diff] [blame] | 720 | if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx, |
Dave Rodgman | 3b46b77 | 2023-06-24 13:25:06 +0100 | [diff] [blame] | 721 | ctx->operation, |
| 722 | ilen, ctx->iv, |
| 723 | input, |
| 724 | output))) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 725 | return ret; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 726 | } |
Manuel Pégourié-Gonnard | 07f8fa5 | 2013-08-30 18:34:08 +0200 | [diff] [blame] | 727 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 728 | *olen += ilen; |
| 729 | } |
| 730 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 731 | return 0; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 732 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 733 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 734 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 735 | #if defined(MBEDTLS_CIPHER_MODE_CFB) |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 736 | if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CFB) { |
Dave Rodgman | de3de77 | 2023-06-24 12:51:06 +0100 | [diff] [blame] | 737 | if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cfb_func(ctx->cipher_ctx, |
Dave Rodgman | 3b46b77 | 2023-06-24 13:25:06 +0100 | [diff] [blame] | 738 | ctx->operation, ilen, |
| 739 | &ctx->unprocessed_len, |
| 740 | ctx->iv, |
| 741 | input, output))) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 742 | return ret; |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | *olen = ilen; |
| 746 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 747 | return 0; |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 748 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 749 | #endif /* MBEDTLS_CIPHER_MODE_CFB */ |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 750 | |
Simon Butcher | 8c0fd1e | 2018-04-22 22:58:07 +0100 | [diff] [blame] | 751 | #if defined(MBEDTLS_CIPHER_MODE_OFB) |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 752 | if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_OFB) { |
Dave Rodgman | de3de77 | 2023-06-24 12:51:06 +0100 | [diff] [blame] | 753 | if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ofb_func(ctx->cipher_ctx, |
Dave Rodgman | 3b46b77 | 2023-06-24 13:25:06 +0100 | [diff] [blame] | 754 | ilen, |
| 755 | &ctx->unprocessed_len, |
| 756 | ctx->iv, |
| 757 | input, output))) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 758 | return ret; |
Simon Butcher | 8c0fd1e | 2018-04-22 22:58:07 +0100 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | *olen = ilen; |
| 762 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 763 | return 0; |
Simon Butcher | 8c0fd1e | 2018-04-22 22:58:07 +0100 | [diff] [blame] | 764 | } |
| 765 | #endif /* MBEDTLS_CIPHER_MODE_OFB */ |
| 766 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 767 | #if defined(MBEDTLS_CIPHER_MODE_CTR) |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 768 | if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_CTR) { |
Dave Rodgman | de3de77 | 2023-06-24 12:51:06 +0100 | [diff] [blame] | 769 | if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->ctr_func(ctx->cipher_ctx, |
Dave Rodgman | 3b46b77 | 2023-06-24 13:25:06 +0100 | [diff] [blame] | 770 | ilen, |
| 771 | &ctx->unprocessed_len, |
| 772 | ctx->iv, |
| 773 | ctx->unprocessed_data, |
| 774 | input, output))) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 775 | return ret; |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | *olen = ilen; |
| 779 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 780 | return 0; |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 781 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 782 | #endif /* MBEDTLS_CIPHER_MODE_CTR */ |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 783 | |
Jaeden Amero | c653990 | 2018-04-30 17:17:41 +0100 | [diff] [blame] | 784 | #if defined(MBEDTLS_CIPHER_MODE_XTS) |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 785 | if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_XTS) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 786 | if (ctx->unprocessed_len > 0) { |
Jaeden Amero | c653990 | 2018-04-30 17:17:41 +0100 | [diff] [blame] | 787 | /* We can only process an entire data unit at a time. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 788 | return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Jaeden Amero | c653990 | 2018-04-30 17:17:41 +0100 | [diff] [blame] | 789 | } |
| 790 | |
Dave Rodgman | de3de77 | 2023-06-24 12:51:06 +0100 | [diff] [blame] | 791 | ret = mbedtls_cipher_get_base(ctx->cipher_info)->xts_func(ctx->cipher_ctx, |
Dave Rodgman | 3b46b77 | 2023-06-24 13:25:06 +0100 | [diff] [blame] | 792 | ctx->operation, |
| 793 | ilen, |
| 794 | ctx->iv, |
| 795 | input, |
| 796 | output); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 797 | if (ret != 0) { |
| 798 | return ret; |
Jaeden Amero | c653990 | 2018-04-30 17:17:41 +0100 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | *olen = ilen; |
| 802 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 803 | return 0; |
Jaeden Amero | c653990 | 2018-04-30 17:17:41 +0100 | [diff] [blame] | 804 | } |
| 805 | #endif /* MBEDTLS_CIPHER_MODE_XTS */ |
| 806 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 807 | #if defined(MBEDTLS_CIPHER_MODE_STREAM) |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 808 | if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) == MBEDTLS_MODE_STREAM) { |
Dave Rodgman | de3de77 | 2023-06-24 12:51:06 +0100 | [diff] [blame] | 809 | if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->stream_func(ctx->cipher_ctx, |
Dave Rodgman | 3b46b77 | 2023-06-24 13:25:06 +0100 | [diff] [blame] | 810 | ilen, input, |
| 811 | output))) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 812 | return ret; |
Manuel Pégourié-Gonnard | 37e230c | 2013-08-28 13:50:42 +0200 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | *olen = ilen; |
| 816 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 817 | return 0; |
Manuel Pégourié-Gonnard | 37e230c | 2013-08-28 13:50:42 +0200 | [diff] [blame] | 818 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 819 | #endif /* MBEDTLS_CIPHER_MODE_STREAM */ |
Manuel Pégourié-Gonnard | 37e230c | 2013-08-28 13:50:42 +0200 | [diff] [blame] | 820 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 821 | return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 824 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
| 825 | #if defined(MBEDTLS_CIPHER_PADDING_PKCS7) |
Manuel Pégourié-Gonnard | 679f9e9 | 2013-07-26 12:46:02 +0200 | [diff] [blame] | 826 | /* |
| 827 | * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len |
| 828 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 829 | static void add_pkcs_padding(unsigned char *output, size_t output_len, |
| 830 | size_t data_len) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 831 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 832 | size_t padding_len = output_len - data_len; |
Manuel Pégourié-Gonnard | f8ab069 | 2013-10-27 17:21:14 +0100 | [diff] [blame] | 833 | unsigned char i; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 834 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 835 | for (i = 0; i < padding_len; i++) { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 836 | output[data_len + i] = (unsigned char) padding_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 837 | } |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 838 | } |
| 839 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 840 | static int get_pkcs_padding(unsigned char *input, size_t input_len, |
| 841 | size_t *data_len) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 842 | { |
Manuel Pégourié-Gonnard | f8ab069 | 2013-10-27 17:21:14 +0100 | [diff] [blame] | 843 | size_t i, pad_idx; |
Dave Rodgman | 6b7e2a5 | 2023-09-18 19:00:44 +0100 | [diff] [blame] | 844 | unsigned char padding_len; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 845 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 846 | if (NULL == input || NULL == data_len) { |
| 847 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
| 848 | } |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 849 | |
| 850 | padding_len = input[input_len - 1]; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 851 | *data_len = input_len - padding_len; |
| 852 | |
Dave Rodgman | e834d6c | 2023-09-20 19:06:53 +0100 | [diff] [blame] | 853 | mbedtls_ct_condition_t bad = mbedtls_ct_uint_gt(padding_len, input_len); |
Dave Rodgman | 6b7e2a5 | 2023-09-18 19:00:44 +0100 | [diff] [blame] | 854 | bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_eq(padding_len, 0)); |
Manuel Pégourié-Gonnard | f8ab069 | 2013-10-27 17:21:14 +0100 | [diff] [blame] | 855 | |
| 856 | /* The number of bytes checked must be independent of padding_len, |
| 857 | * so pick input_len, which is usually 8 or 16 (one block) */ |
| 858 | pad_idx = input_len - padding_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 859 | for (i = 0; i < input_len; i++) { |
Dave Rodgman | c43a0a4 | 2023-09-20 19:07:22 +0100 | [diff] [blame] | 860 | mbedtls_ct_condition_t in_padding = mbedtls_ct_uint_ge(i, pad_idx); |
| 861 | mbedtls_ct_condition_t different = mbedtls_ct_uint_ne(input[i], padding_len); |
| 862 | bad = mbedtls_ct_bool_or(bad, mbedtls_ct_bool_and(in_padding, different)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 863 | } |
Manuel Pégourié-Gonnard | f8ab069 | 2013-10-27 17:21:14 +0100 | [diff] [blame] | 864 | |
Dave Rodgman | d03f483 | 2023-09-22 09:52:15 +0100 | [diff] [blame] | 865 | return mbedtls_ct_error_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 866 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 867 | #endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 868 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 869 | #if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS) |
Manuel Pégourié-Gonnard | 679f9e9 | 2013-07-26 12:46:02 +0200 | [diff] [blame] | 870 | /* |
| 871 | * One and zeros padding: fill with 80 00 ... 00 |
| 872 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 873 | static void add_one_and_zeros_padding(unsigned char *output, |
| 874 | size_t output_len, size_t data_len) |
Manuel Pégourié-Gonnard | 679f9e9 | 2013-07-26 12:46:02 +0200 | [diff] [blame] | 875 | { |
| 876 | size_t padding_len = output_len - data_len; |
| 877 | unsigned char i = 0; |
| 878 | |
| 879 | output[data_len] = 0x80; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 880 | for (i = 1; i < padding_len; i++) { |
Manuel Pégourié-Gonnard | 679f9e9 | 2013-07-26 12:46:02 +0200 | [diff] [blame] | 881 | output[data_len + i] = 0x00; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 882 | } |
Manuel Pégourié-Gonnard | 679f9e9 | 2013-07-26 12:46:02 +0200 | [diff] [blame] | 883 | } |
| 884 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 885 | static int get_one_and_zeros_padding(unsigned char *input, size_t input_len, |
| 886 | size_t *data_len) |
Manuel Pégourié-Gonnard | 679f9e9 | 2013-07-26 12:46:02 +0200 | [diff] [blame] | 887 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 888 | if (NULL == input || NULL == data_len) { |
| 889 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
| 890 | } |
Manuel Pégourié-Gonnard | 679f9e9 | 2013-07-26 12:46:02 +0200 | [diff] [blame] | 891 | |
Dave Rodgman | 89a9bd5 | 2023-09-19 14:37:50 +0100 | [diff] [blame] | 892 | mbedtls_ct_condition_t in_padding = MBEDTLS_CT_TRUE; |
| 893 | mbedtls_ct_condition_t bad = MBEDTLS_CT_TRUE; |
| 894 | |
Manuel Pégourié-Gonnard | 6c32990 | 2013-10-27 18:25:03 +0100 | [diff] [blame] | 895 | *data_len = 0; |
Dave Rodgman | 89a9bd5 | 2023-09-19 14:37:50 +0100 | [diff] [blame] | 896 | |
Dave Rodgman | 437500c | 2023-09-19 21:36:43 +0100 | [diff] [blame] | 897 | for (ptrdiff_t i = (ptrdiff_t) (input_len) - 1; i >= 0; i--) { |
Dave Rodgman | 89a9bd5 | 2023-09-19 14:37:50 +0100 | [diff] [blame] | 898 | mbedtls_ct_condition_t is_nonzero = mbedtls_ct_bool(input[i]); |
| 899 | |
| 900 | mbedtls_ct_condition_t hit_first_nonzero = mbedtls_ct_bool_and(is_nonzero, in_padding); |
| 901 | |
| 902 | *data_len = mbedtls_ct_size_if(hit_first_nonzero, i, *data_len); |
| 903 | |
Dave Rodgman | fd96579 | 2023-09-19 21:51:50 +0100 | [diff] [blame] | 904 | bad = mbedtls_ct_bool_if(hit_first_nonzero, mbedtls_ct_uint_ne(input[i], 0x80), bad); |
Dave Rodgman | 89a9bd5 | 2023-09-19 14:37:50 +0100 | [diff] [blame] | 905 | |
| 906 | in_padding = mbedtls_ct_bool_and(in_padding, mbedtls_ct_bool_not(is_nonzero)); |
Manuel Pégourié-Gonnard | 6c32990 | 2013-10-27 18:25:03 +0100 | [diff] [blame] | 907 | } |
Manuel Pégourié-Gonnard | 679f9e9 | 2013-07-26 12:46:02 +0200 | [diff] [blame] | 908 | |
Dave Rodgman | d03f483 | 2023-09-22 09:52:15 +0100 | [diff] [blame] | 909 | return mbedtls_ct_error_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING); |
Manuel Pégourié-Gonnard | 679f9e9 | 2013-07-26 12:46:02 +0200 | [diff] [blame] | 910 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 911 | #endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */ |
Manuel Pégourié-Gonnard | 679f9e9 | 2013-07-26 12:46:02 +0200 | [diff] [blame] | 912 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 913 | #if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN) |
Manuel Pégourié-Gonnard | 8d4291b | 2013-07-26 14:55:18 +0200 | [diff] [blame] | 914 | /* |
| 915 | * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length |
| 916 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 917 | static void add_zeros_and_len_padding(unsigned char *output, |
| 918 | size_t output_len, size_t data_len) |
Manuel Pégourié-Gonnard | 8d4291b | 2013-07-26 14:55:18 +0200 | [diff] [blame] | 919 | { |
| 920 | size_t padding_len = output_len - data_len; |
| 921 | unsigned char i = 0; |
| 922 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 923 | for (i = 1; i < padding_len; i++) { |
Manuel Pégourié-Gonnard | 8d4291b | 2013-07-26 14:55:18 +0200 | [diff] [blame] | 924 | output[data_len + i - 1] = 0x00; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 925 | } |
Manuel Pégourié-Gonnard | 8d4291b | 2013-07-26 14:55:18 +0200 | [diff] [blame] | 926 | output[output_len - 1] = (unsigned char) padding_len; |
| 927 | } |
| 928 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 929 | static int get_zeros_and_len_padding(unsigned char *input, size_t input_len, |
| 930 | size_t *data_len) |
Manuel Pégourié-Gonnard | 8d4291b | 2013-07-26 14:55:18 +0200 | [diff] [blame] | 931 | { |
Manuel Pégourié-Gonnard | d17df51 | 2013-10-27 17:32:43 +0100 | [diff] [blame] | 932 | size_t i, pad_idx; |
Dave Rodgman | 6cec41c | 2023-09-18 21:51:55 +0100 | [diff] [blame] | 933 | unsigned char padding_len; |
| 934 | mbedtls_ct_condition_t bad; |
Manuel Pégourié-Gonnard | 8d4291b | 2013-07-26 14:55:18 +0200 | [diff] [blame] | 935 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 936 | if (NULL == input || NULL == data_len) { |
| 937 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
| 938 | } |
Manuel Pégourié-Gonnard | 8d4291b | 2013-07-26 14:55:18 +0200 | [diff] [blame] | 939 | |
| 940 | padding_len = input[input_len - 1]; |
Manuel Pégourié-Gonnard | 8d4291b | 2013-07-26 14:55:18 +0200 | [diff] [blame] | 941 | *data_len = input_len - padding_len; |
| 942 | |
Manuel Pégourié-Gonnard | d17df51 | 2013-10-27 17:32:43 +0100 | [diff] [blame] | 943 | /* Avoid logical || since it results in a branch */ |
Dave Rodgman | 6cec41c | 2023-09-18 21:51:55 +0100 | [diff] [blame] | 944 | bad = mbedtls_ct_uint_gt(padding_len, input_len); |
| 945 | bad = mbedtls_ct_bool_or(bad, mbedtls_ct_uint_eq(padding_len, 0)); |
Manuel Pégourié-Gonnard | d17df51 | 2013-10-27 17:32:43 +0100 | [diff] [blame] | 946 | |
| 947 | /* The number of bytes checked must be independent of padding_len */ |
| 948 | pad_idx = input_len - padding_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 949 | for (i = 0; i < input_len - 1; i++) { |
Dave Rodgman | 6cec41c | 2023-09-18 21:51:55 +0100 | [diff] [blame] | 950 | mbedtls_ct_condition_t is_padding = mbedtls_ct_uint_ge(i, pad_idx); |
Dave Rodgman | 6be4bcf | 2023-09-19 19:47:51 +0100 | [diff] [blame] | 951 | mbedtls_ct_condition_t nonzero_pad_byte; |
Dave Rodgman | fd96579 | 2023-09-19 21:51:50 +0100 | [diff] [blame] | 952 | nonzero_pad_byte = mbedtls_ct_bool_if_else_0(is_padding, mbedtls_ct_bool(input[i])); |
Dave Rodgman | 6cec41c | 2023-09-18 21:51:55 +0100 | [diff] [blame] | 953 | bad = mbedtls_ct_bool_or(bad, nonzero_pad_byte); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 954 | } |
Manuel Pégourié-Gonnard | d17df51 | 2013-10-27 17:32:43 +0100 | [diff] [blame] | 955 | |
Dave Rodgman | d03f483 | 2023-09-22 09:52:15 +0100 | [diff] [blame] | 956 | return mbedtls_ct_error_if_else_0(bad, MBEDTLS_ERR_CIPHER_INVALID_PADDING); |
Manuel Pégourié-Gonnard | 8d4291b | 2013-07-26 14:55:18 +0200 | [diff] [blame] | 957 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 958 | #endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */ |
Manuel Pégourié-Gonnard | 8d4291b | 2013-07-26 14:55:18 +0200 | [diff] [blame] | 959 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 960 | #if defined(MBEDTLS_CIPHER_PADDING_ZEROS) |
Manuel Pégourié-Gonnard | 0e7d2c0 | 2013-07-26 16:05:14 +0200 | [diff] [blame] | 961 | /* |
| 962 | * Zero padding: fill with 00 ... 00 |
| 963 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 964 | static void add_zeros_padding(unsigned char *output, |
| 965 | size_t output_len, size_t data_len) |
Manuel Pégourié-Gonnard | 0e7d2c0 | 2013-07-26 16:05:14 +0200 | [diff] [blame] | 966 | { |
Dave Rodgman | f8182d9 | 2023-09-19 16:25:17 +0100 | [diff] [blame] | 967 | memset(output + data_len, 0, output_len - data_len); |
Manuel Pégourié-Gonnard | 0e7d2c0 | 2013-07-26 16:05:14 +0200 | [diff] [blame] | 968 | } |
| 969 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 970 | static int get_zeros_padding(unsigned char *input, size_t input_len, |
| 971 | size_t *data_len) |
Manuel Pégourié-Gonnard | 0e7d2c0 | 2013-07-26 16:05:14 +0200 | [diff] [blame] | 972 | { |
Manuel Pégourié-Gonnard | e68bf17 | 2013-10-27 18:26:39 +0100 | [diff] [blame] | 973 | size_t i; |
Dave Rodgman | d8c68a9 | 2023-09-19 16:19:38 +0100 | [diff] [blame] | 974 | mbedtls_ct_condition_t done = MBEDTLS_CT_FALSE, prev_done; |
Manuel Pégourié-Gonnard | e68bf17 | 2013-10-27 18:26:39 +0100 | [diff] [blame] | 975 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 976 | if (NULL == input || NULL == data_len) { |
| 977 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | e68bf17 | 2013-10-27 18:26:39 +0100 | [diff] [blame] | 978 | } |
Manuel Pégourié-Gonnard | 0e7d2c0 | 2013-07-26 16:05:14 +0200 | [diff] [blame] | 979 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 980 | *data_len = 0; |
| 981 | for (i = input_len; i > 0; i--) { |
| 982 | prev_done = done; |
Dave Rodgman | d8c68a9 | 2023-09-19 16:19:38 +0100 | [diff] [blame] | 983 | done = mbedtls_ct_bool_or(done, mbedtls_ct_uint_ne(input[i-1], 0)); |
| 984 | *data_len = mbedtls_ct_size_if(mbedtls_ct_bool_ne(done, prev_done), i, *data_len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | return 0; |
Manuel Pégourié-Gonnard | 0e7d2c0 | 2013-07-26 16:05:14 +0200 | [diff] [blame] | 988 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 989 | #endif /* MBEDTLS_CIPHER_PADDING_ZEROS */ |
Manuel Pégourié-Gonnard | 0e7d2c0 | 2013-07-26 16:05:14 +0200 | [diff] [blame] | 990 | |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 991 | /* |
| 992 | * No padding: don't pad :) |
| 993 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 994 | * There is no add_padding function (check for NULL in mbedtls_cipher_finish) |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 995 | * but a trivial get_padding function |
| 996 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 997 | static int get_no_padding(unsigned char *input, size_t input_len, |
| 998 | size_t *data_len) |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 999 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1000 | if (NULL == input || NULL == data_len) { |
| 1001 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
| 1002 | } |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 1003 | |
| 1004 | *data_len = input_len; |
| 1005 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1006 | return 0; |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 1007 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1008 | #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */ |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 1009 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1010 | int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx, |
| 1011 | unsigned char *output, size_t *olen) |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 1012 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1013 | if (ctx->cipher_info == NULL) { |
| 1014 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
| 1015 | } |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 1016 | |
Manuel Pégourié-Gonnard | 5c731b0 | 2023-06-21 10:37:30 +0200 | [diff] [blame] | 1017 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1018 | if (ctx->psa_enabled == 1) { |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 1019 | /* While PSA Crypto has an API for multipart |
| 1020 | * operations, we currently don't make it |
| 1021 | * accessible through the cipher layer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1022 | return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 1023 | } |
Manuel Pégourié-Gonnard | 5c731b0 | 2023-06-21 10:37:30 +0200 | [diff] [blame] | 1024 | #endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 1025 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 1026 | *olen = 0; |
| 1027 | |
Waleed Elmelegy | a7d206f | 2023-09-07 17:54:46 +0100 | [diff] [blame] | 1028 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
| 1029 | /* CBC mode requires padding so we make sure a call to |
| 1030 | * mbedtls_cipher_set_padding_mode has been done successfully. */ |
| 1031 | if (MBEDTLS_MODE_CBC == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) { |
| 1032 | if (ctx->get_padding == NULL) { |
| 1033 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
| 1034 | } |
| 1035 | } |
| 1036 | #endif |
| 1037 | |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 1038 | if (MBEDTLS_MODE_CFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) || |
| 1039 | MBEDTLS_MODE_OFB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) || |
| 1040 | MBEDTLS_MODE_CTR == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) || |
| 1041 | MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) || |
| 1042 | MBEDTLS_MODE_CCM_STAR_NO_TAG == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) || |
| 1043 | MBEDTLS_MODE_XTS == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) || |
| 1044 | MBEDTLS_MODE_STREAM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1045 | return 0; |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 1048 | if ((MBEDTLS_CIPHER_CHACHA20 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) || |
| 1049 | (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type))) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1050 | return 0; |
Daniel King | bd92062 | 2016-05-15 19:56:20 -0300 | [diff] [blame] | 1051 | } |
| 1052 | |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 1053 | if (MBEDTLS_MODE_ECB == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1054 | if (ctx->unprocessed_len != 0) { |
| 1055 | return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED; |
| 1056 | } |
Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 1057 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1058 | return 0; |
Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 1059 | } |
| 1060 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1061 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 1062 | if (MBEDTLS_MODE_CBC == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) { |
Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 1063 | int ret = 0; |
| 1064 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1065 | if (MBEDTLS_ENCRYPT == ctx->operation) { |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 1066 | /* check for 'no padding' mode */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1067 | if (NULL == ctx->add_padding) { |
| 1068 | if (0 != ctx->unprocessed_len) { |
| 1069 | return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED; |
| 1070 | } |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 1071 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1072 | return 0; |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 1073 | } |
| 1074 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1075 | ctx->add_padding(ctx->unprocessed_data, mbedtls_cipher_get_iv_size(ctx), |
| 1076 | ctx->unprocessed_len); |
| 1077 | } else if (mbedtls_cipher_get_block_size(ctx) != ctx->unprocessed_len) { |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 1078 | /* |
| 1079 | * For decrypt operations, expect a full block, |
| 1080 | * or an empty block if no padding |
| 1081 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1082 | if (NULL == ctx->add_padding && 0 == ctx->unprocessed_len) { |
| 1083 | return 0; |
| 1084 | } |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 1085 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1086 | return MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | /* cipher block */ |
Dave Rodgman | de3de77 | 2023-06-24 12:51:06 +0100 | [diff] [blame] | 1090 | if (0 != (ret = mbedtls_cipher_get_base(ctx->cipher_info)->cbc_func(ctx->cipher_ctx, |
Dave Rodgman | 3b46b77 | 2023-06-24 13:25:06 +0100 | [diff] [blame] | 1091 | ctx->operation, |
| 1092 | mbedtls_cipher_get_block_size( |
| 1093 | ctx), |
| 1094 | ctx->iv, |
| 1095 | ctx->unprocessed_data, |
| 1096 | output))) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1097 | return ret; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | /* Set output size for decryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1101 | if (MBEDTLS_DECRYPT == ctx->operation) { |
| 1102 | return ctx->get_padding(output, mbedtls_cipher_get_block_size(ctx), |
| 1103 | olen); |
| 1104 | } |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 1105 | |
| 1106 | /* Set output size for encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1107 | *olen = mbedtls_cipher_get_block_size(ctx); |
| 1108 | return 0; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 1109 | } |
Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 1110 | #else |
| 1111 | ((void) output); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1112 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 1113 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1114 | return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1117 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1118 | int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t *ctx, |
| 1119 | mbedtls_cipher_padding_t mode) |
Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 1120 | { |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 1121 | if (NULL == ctx->cipher_info || |
| 1122 | MBEDTLS_MODE_CBC != ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1123 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 1124 | } |
| 1125 | |
Manuel Pégourié-Gonnard | 5c731b0 | 2023-06-21 10:37:30 +0200 | [diff] [blame] | 1126 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1127 | if (ctx->psa_enabled == 1) { |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 1128 | /* While PSA Crypto knows about CBC padding |
| 1129 | * schemes, we currently don't make them |
| 1130 | * accessible through the cipher layer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1131 | if (mode != MBEDTLS_PADDING_NONE) { |
| 1132 | return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
| 1133 | } |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 1134 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1135 | return 0; |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 1136 | } |
Manuel Pégourié-Gonnard | 5c731b0 | 2023-06-21 10:37:30 +0200 | [diff] [blame] | 1137 | #endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 1138 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1139 | switch (mode) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1140 | #if defined(MBEDTLS_CIPHER_PADDING_PKCS7) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1141 | case MBEDTLS_PADDING_PKCS7: |
| 1142 | ctx->add_padding = add_pkcs_padding; |
| 1143 | ctx->get_padding = get_pkcs_padding; |
| 1144 | break; |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 1145 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1146 | #if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1147 | case MBEDTLS_PADDING_ONE_AND_ZEROS: |
| 1148 | ctx->add_padding = add_one_and_zeros_padding; |
| 1149 | ctx->get_padding = get_one_and_zeros_padding; |
| 1150 | break; |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 1151 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1152 | #if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1153 | case MBEDTLS_PADDING_ZEROS_AND_LEN: |
| 1154 | ctx->add_padding = add_zeros_and_len_padding; |
| 1155 | ctx->get_padding = get_zeros_and_len_padding; |
| 1156 | break; |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 1157 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1158 | #if defined(MBEDTLS_CIPHER_PADDING_ZEROS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1159 | case MBEDTLS_PADDING_ZEROS: |
| 1160 | ctx->add_padding = add_zeros_padding; |
| 1161 | ctx->get_padding = get_zeros_padding; |
| 1162 | break; |
Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 1163 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1164 | case MBEDTLS_PADDING_NONE: |
| 1165 | ctx->add_padding = NULL; |
| 1166 | ctx->get_padding = get_no_padding; |
| 1167 | break; |
Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 1168 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1169 | default: |
| 1170 | return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 1171 | } |
| 1172 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1173 | return 0; |
Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 1174 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1175 | #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */ |
Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 1176 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 1177 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1178 | int mbedtls_cipher_write_tag(mbedtls_cipher_context_t *ctx, |
| 1179 | unsigned char *tag, size_t tag_len) |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 1180 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1181 | if (ctx->cipher_info == NULL) { |
| 1182 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
| 1183 | } |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 1184 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1185 | if (MBEDTLS_ENCRYPT != ctx->operation) { |
| 1186 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
| 1187 | } |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 1188 | |
Manuel Pégourié-Gonnard | 5c731b0 | 2023-06-21 10:37:30 +0200 | [diff] [blame] | 1189 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1190 | if (ctx->psa_enabled == 1) { |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 1191 | /* While PSA Crypto has an API for multipart |
| 1192 | * operations, we currently don't make it |
| 1193 | * accessible through the cipher layer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1194 | return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 1195 | } |
Manuel Pégourié-Gonnard | 5c731b0 | 2023-06-21 10:37:30 +0200 | [diff] [blame] | 1196 | #endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 1197 | |
Daniel King | 8fe4701 | 2016-05-17 20:33:28 -0300 | [diff] [blame] | 1198 | #if defined(MBEDTLS_GCM_C) |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 1199 | if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) { |
Gilles Peskine | 5a7be10 | 2021-06-23 21:51:32 +0200 | [diff] [blame] | 1200 | size_t output_length; |
| 1201 | /* The code here doesn't yet support alternative implementations |
| 1202 | * that can delay up to a block of output. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1203 | return mbedtls_gcm_finish((mbedtls_gcm_context *) ctx->cipher_ctx, |
| 1204 | NULL, 0, &output_length, |
| 1205 | tag, tag_len); |
Gilles Peskine | 5a7be10 | 2021-06-23 21:51:32 +0200 | [diff] [blame] | 1206 | } |
Daniel King | 8fe4701 | 2016-05-17 20:33:28 -0300 | [diff] [blame] | 1207 | #endif |
| 1208 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 1209 | #if defined(MBEDTLS_CHACHAPOLY_C) |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 1210 | if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) { |
Daniel King | 8fe4701 | 2016-05-17 20:33:28 -0300 | [diff] [blame] | 1211 | /* Don't allow truncated MAC for Poly1305 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1212 | if (tag_len != 16U) { |
| 1213 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
| 1214 | } |
Daniel King | 8fe4701 | 2016-05-17 20:33:28 -0300 | [diff] [blame] | 1215 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1216 | return mbedtls_chachapoly_finish( |
| 1217 | (mbedtls_chachapoly_context *) ctx->cipher_ctx, tag); |
Daniel King | 8fe4701 | 2016-05-17 20:33:28 -0300 | [diff] [blame] | 1218 | } |
| 1219 | #endif |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 1220 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1221 | return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 1222 | } |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 1223 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1224 | int mbedtls_cipher_check_tag(mbedtls_cipher_context_t *ctx, |
| 1225 | const unsigned char *tag, size_t tag_len) |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 1226 | { |
Daniel King | 8fe4701 | 2016-05-17 20:33:28 -0300 | [diff] [blame] | 1227 | unsigned char check_tag[16]; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1228 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 1229 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1230 | if (ctx->cipher_info == NULL) { |
| 1231 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
| 1232 | } |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 1233 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1234 | if (MBEDTLS_DECRYPT != ctx->operation) { |
| 1235 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 1236 | } |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 1237 | |
Manuel Pégourié-Gonnard | 5c731b0 | 2023-06-21 10:37:30 +0200 | [diff] [blame] | 1238 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1239 | if (ctx->psa_enabled == 1) { |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 1240 | /* While PSA Crypto has an API for multipart |
| 1241 | * operations, we currently don't make it |
| 1242 | * accessible through the cipher layer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1243 | return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 1244 | } |
Manuel Pégourié-Gonnard | 5c731b0 | 2023-06-21 10:37:30 +0200 | [diff] [blame] | 1245 | #endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 1246 | |
Denis V. Lunev | 2df73ae | 2018-11-01 12:22:27 +0300 | [diff] [blame] | 1247 | /* Status to return on a non-authenticated algorithm. */ |
| 1248 | ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Gilles Peskine | e7835d9 | 2021-12-13 12:32:43 +0100 | [diff] [blame] | 1249 | |
Daniel King | 8fe4701 | 2016-05-17 20:33:28 -0300 | [diff] [blame] | 1250 | #if defined(MBEDTLS_GCM_C) |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 1251 | if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) { |
Gilles Peskine | 5a7be10 | 2021-06-23 21:51:32 +0200 | [diff] [blame] | 1252 | size_t output_length; |
| 1253 | /* The code here doesn't yet support alternative implementations |
| 1254 | * that can delay up to a block of output. */ |
| 1255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1256 | if (tag_len > sizeof(check_tag)) { |
| 1257 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
| 1258 | } |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 1259 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1260 | if (0 != (ret = mbedtls_gcm_finish( |
| 1261 | (mbedtls_gcm_context *) ctx->cipher_ctx, |
| 1262 | NULL, 0, &output_length, |
| 1263 | check_tag, tag_len))) { |
| 1264 | return ret; |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 1265 | } |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 1266 | |
| 1267 | /* Check the tag in "constant-time" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1268 | if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) { |
Gilles Peskine | e7835d9 | 2021-12-13 12:32:43 +0100 | [diff] [blame] | 1269 | ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED; |
Gilles Peskine | cd74298 | 2021-12-13 16:57:47 +0100 | [diff] [blame] | 1270 | goto exit; |
| 1271 | } |
Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 1272 | } |
Daniel King | 8fe4701 | 2016-05-17 20:33:28 -0300 | [diff] [blame] | 1273 | #endif /* MBEDTLS_GCM_C */ |
| 1274 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 1275 | #if defined(MBEDTLS_CHACHAPOLY_C) |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 1276 | if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) { |
Daniel King | 8fe4701 | 2016-05-17 20:33:28 -0300 | [diff] [blame] | 1277 | /* Don't allow truncated MAC for Poly1305 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1278 | if (tag_len != sizeof(check_tag)) { |
| 1279 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
| 1280 | } |
Daniel King | 8fe4701 | 2016-05-17 20:33:28 -0300 | [diff] [blame] | 1281 | |
Hanno Becker | 18597cd | 2018-11-09 16:36:33 +0000 | [diff] [blame] | 1282 | ret = mbedtls_chachapoly_finish( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1283 | (mbedtls_chachapoly_context *) ctx->cipher_ctx, check_tag); |
| 1284 | if (ret != 0) { |
| 1285 | return ret; |
Daniel King | 8fe4701 | 2016-05-17 20:33:28 -0300 | [diff] [blame] | 1286 | } |
| 1287 | |
| 1288 | /* Check the tag in "constant-time" */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1289 | if (mbedtls_ct_memcmp(tag, check_tag, tag_len) != 0) { |
Gilles Peskine | e7835d9 | 2021-12-13 12:32:43 +0100 | [diff] [blame] | 1290 | ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED; |
Gilles Peskine | cd74298 | 2021-12-13 16:57:47 +0100 | [diff] [blame] | 1291 | goto exit; |
| 1292 | } |
Daniel King | 8fe4701 | 2016-05-17 20:33:28 -0300 | [diff] [blame] | 1293 | } |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 1294 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 1295 | |
Gilles Peskine | cd74298 | 2021-12-13 16:57:47 +0100 | [diff] [blame] | 1296 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1297 | mbedtls_platform_zeroize(check_tag, tag_len); |
| 1298 | return ret; |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 1299 | } |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 1300 | #endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */ |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 1301 | |
Manuel Pégourié-Gonnard | 3c1d150 | 2014-05-12 13:46:08 +0200 | [diff] [blame] | 1302 | /* |
| 1303 | * Packet-oriented wrapper for non-AEAD modes |
| 1304 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1305 | int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx, |
| 1306 | const unsigned char *iv, size_t iv_len, |
| 1307 | const unsigned char *input, size_t ilen, |
| 1308 | unsigned char *output, size_t *olen) |
Manuel Pégourié-Gonnard | 3c1d150 | 2014-05-12 13:46:08 +0200 | [diff] [blame] | 1309 | { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1310 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 3c1d150 | 2014-05-12 13:46:08 +0200 | [diff] [blame] | 1311 | size_t finish_olen; |
| 1312 | |
Manuel Pégourié-Gonnard | 5c731b0 | 2023-06-21 10:37:30 +0200 | [diff] [blame] | 1313 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1314 | if (ctx->psa_enabled == 1) { |
Hanno Becker | 55e2e3d | 2018-11-12 12:36:17 +0000 | [diff] [blame] | 1315 | /* As in the non-PSA case, we don't check that |
| 1316 | * a key has been set. If not, the key slot will |
| 1317 | * still be in its default state of 0, which is |
| 1318 | * guaranteed to be invalid, hence the PSA-call |
| 1319 | * below will gracefully fail. */ |
| 1320 | mbedtls_cipher_context_psa * const cipher_psa = |
| 1321 | (mbedtls_cipher_context_psa *) ctx->cipher_ctx; |
| 1322 | |
| 1323 | psa_status_t status; |
Jaeden Amero | fe96fbe | 2019-02-20 10:32:28 +0000 | [diff] [blame] | 1324 | psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT; |
Hanno Becker | 55e2e3d | 2018-11-12 12:36:17 +0000 | [diff] [blame] | 1325 | size_t part_len; |
| 1326 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1327 | if (ctx->operation == MBEDTLS_DECRYPT) { |
| 1328 | status = psa_cipher_decrypt_setup(&cipher_op, |
| 1329 | cipher_psa->slot, |
| 1330 | cipher_psa->alg); |
| 1331 | } else if (ctx->operation == MBEDTLS_ENCRYPT) { |
| 1332 | status = psa_cipher_encrypt_setup(&cipher_op, |
| 1333 | cipher_psa->slot, |
| 1334 | cipher_psa->alg); |
| 1335 | } else { |
| 1336 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
Hanno Becker | 55e2e3d | 2018-11-12 12:36:17 +0000 | [diff] [blame] | 1337 | } |
Hanno Becker | 55e2e3d | 2018-11-12 12:36:17 +0000 | [diff] [blame] | 1338 | |
| 1339 | /* In the following, we can immediately return on an error, |
| 1340 | * because the PSA Crypto API guarantees that cipher operations |
| 1341 | * are terminated by unsuccessful calls to psa_cipher_update(), |
| 1342 | * and by any call to psa_cipher_finish(). */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1343 | if (status != PSA_SUCCESS) { |
| 1344 | return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
Przemyslaw Stekiel | 80c6a8e | 2021-09-29 12:13:11 +0200 | [diff] [blame] | 1345 | } |
Hanno Becker | 55e2e3d | 2018-11-12 12:36:17 +0000 | [diff] [blame] | 1346 | |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 1347 | if (((mbedtls_cipher_mode_t) ctx->cipher_info->mode) != MBEDTLS_MODE_ECB) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1348 | status = psa_cipher_set_iv(&cipher_op, iv, iv_len); |
| 1349 | if (status != PSA_SUCCESS) { |
| 1350 | return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
| 1351 | } |
| 1352 | } |
Hanno Becker | 55e2e3d | 2018-11-12 12:36:17 +0000 | [diff] [blame] | 1353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1354 | status = psa_cipher_update(&cipher_op, |
| 1355 | input, ilen, |
| 1356 | output, ilen, olen); |
| 1357 | if (status != PSA_SUCCESS) { |
| 1358 | return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
| 1359 | } |
| 1360 | |
| 1361 | status = psa_cipher_finish(&cipher_op, |
| 1362 | output + *olen, ilen - *olen, |
| 1363 | &part_len); |
| 1364 | if (status != PSA_SUCCESS) { |
| 1365 | return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
| 1366 | } |
Hanno Becker | 55e2e3d | 2018-11-12 12:36:17 +0000 | [diff] [blame] | 1367 | |
| 1368 | *olen += part_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1369 | return 0; |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 1370 | } |
Manuel Pégourié-Gonnard | 5c731b0 | 2023-06-21 10:37:30 +0200 | [diff] [blame] | 1371 | #endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 1372 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1373 | if ((ret = mbedtls_cipher_set_iv(ctx, iv, iv_len)) != 0) { |
| 1374 | return ret; |
| 1375 | } |
Manuel Pégourié-Gonnard | 3c1d150 | 2014-05-12 13:46:08 +0200 | [diff] [blame] | 1376 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1377 | if ((ret = mbedtls_cipher_reset(ctx)) != 0) { |
| 1378 | return ret; |
| 1379 | } |
Manuel Pégourié-Gonnard | 3c1d150 | 2014-05-12 13:46:08 +0200 | [diff] [blame] | 1380 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1381 | if ((ret = mbedtls_cipher_update(ctx, input, ilen, |
| 1382 | output, olen)) != 0) { |
| 1383 | return ret; |
| 1384 | } |
Manuel Pégourié-Gonnard | 3c1d150 | 2014-05-12 13:46:08 +0200 | [diff] [blame] | 1385 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1386 | if ((ret = mbedtls_cipher_finish(ctx, output + *olen, |
| 1387 | &finish_olen)) != 0) { |
| 1388 | return ret; |
| 1389 | } |
Manuel Pégourié-Gonnard | 3c1d150 | 2014-05-12 13:46:08 +0200 | [diff] [blame] | 1390 | |
| 1391 | *olen += finish_olen; |
| 1392 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1393 | return 0; |
Manuel Pégourié-Gonnard | 3c1d150 | 2014-05-12 13:46:08 +0200 | [diff] [blame] | 1394 | } |
| 1395 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1396 | #if defined(MBEDTLS_CIPHER_MODE_AEAD) |
Manuel Pégourié-Gonnard | 4562ffe | 2014-05-13 12:19:29 +0200 | [diff] [blame] | 1397 | /* |
TRodziewicz | 18efb73 | 2021-04-29 23:12:19 +0200 | [diff] [blame] | 1398 | * Packet-oriented encryption for AEAD modes: internal function used by |
| 1399 | * mbedtls_cipher_auth_encrypt_ext(). |
Manuel Pégourié-Gonnard | 4562ffe | 2014-05-13 12:19:29 +0200 | [diff] [blame] | 1400 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1401 | static int mbedtls_cipher_aead_encrypt(mbedtls_cipher_context_t *ctx, |
| 1402 | const unsigned char *iv, size_t iv_len, |
| 1403 | const unsigned char *ad, size_t ad_len, |
| 1404 | const unsigned char *input, size_t ilen, |
| 1405 | unsigned char *output, size_t *olen, |
| 1406 | unsigned char *tag, size_t tag_len) |
Manuel Pégourié-Gonnard | 4562ffe | 2014-05-13 12:19:29 +0200 | [diff] [blame] | 1407 | { |
Manuel Pégourié-Gonnard | 5c731b0 | 2023-06-21 10:37:30 +0200 | [diff] [blame] | 1408 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1409 | if (ctx->psa_enabled == 1) { |
Hanno Becker | fe73ade | 2018-11-12 16:26:46 +0000 | [diff] [blame] | 1410 | /* As in the non-PSA case, we don't check that |
| 1411 | * a key has been set. If not, the key slot will |
| 1412 | * still be in its default state of 0, which is |
| 1413 | * guaranteed to be invalid, hence the PSA-call |
| 1414 | * below will gracefully fail. */ |
| 1415 | mbedtls_cipher_context_psa * const cipher_psa = |
| 1416 | (mbedtls_cipher_context_psa *) ctx->cipher_ctx; |
| 1417 | |
| 1418 | psa_status_t status; |
| 1419 | |
| 1420 | /* PSA Crypto API always writes the authentication tag |
| 1421 | * at the end of the encrypted message. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1422 | if (output == NULL || tag != output + ilen) { |
| 1423 | return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
| 1424 | } |
Hanno Becker | fe73ade | 2018-11-12 16:26:46 +0000 | [diff] [blame] | 1425 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1426 | status = psa_aead_encrypt(cipher_psa->slot, |
| 1427 | cipher_psa->alg, |
| 1428 | iv, iv_len, |
| 1429 | ad, ad_len, |
| 1430 | input, ilen, |
| 1431 | output, ilen + tag_len, olen); |
| 1432 | if (status != PSA_SUCCESS) { |
| 1433 | return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
| 1434 | } |
Hanno Becker | fe73ade | 2018-11-12 16:26:46 +0000 | [diff] [blame] | 1435 | |
| 1436 | *olen -= tag_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1437 | return 0; |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 1438 | } |
Manuel Pégourié-Gonnard | 5c731b0 | 2023-06-21 10:37:30 +0200 | [diff] [blame] | 1439 | #endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 1440 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1441 | #if defined(MBEDTLS_GCM_C) |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 1442 | if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) { |
Manuel Pégourié-Gonnard | 4562ffe | 2014-05-13 12:19:29 +0200 | [diff] [blame] | 1443 | *olen = ilen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1444 | return mbedtls_gcm_crypt_and_tag(ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, |
| 1445 | ilen, iv, iv_len, ad, ad_len, |
| 1446 | input, output, tag_len, tag); |
Manuel Pégourié-Gonnard | 4562ffe | 2014-05-13 12:19:29 +0200 | [diff] [blame] | 1447 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1448 | #endif /* MBEDTLS_GCM_C */ |
| 1449 | #if defined(MBEDTLS_CCM_C) |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 1450 | if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) { |
Manuel Pégourié-Gonnard | 4193695 | 2014-05-13 13:18:17 +0200 | [diff] [blame] | 1451 | *olen = ilen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1452 | return mbedtls_ccm_encrypt_and_tag(ctx->cipher_ctx, ilen, |
| 1453 | iv, iv_len, ad, ad_len, input, output, |
| 1454 | tag, tag_len); |
Manuel Pégourié-Gonnard | 4193695 | 2014-05-13 13:18:17 +0200 | [diff] [blame] | 1455 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1456 | #endif /* MBEDTLS_CCM_C */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 1457 | #if defined(MBEDTLS_CHACHAPOLY_C) |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 1458 | if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) { |
Manuel Pégourié-Gonnard | fe725de | 2018-05-08 09:38:09 +0200 | [diff] [blame] | 1459 | /* ChachaPoly has fixed length nonce and MAC (tag) */ |
Dave Rodgman | bb521fd | 2023-06-24 11:21:25 +0100 | [diff] [blame] | 1460 | if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1461 | (tag_len != 16U)) { |
| 1462 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
Daniel King | 8fe4701 | 2016-05-17 20:33:28 -0300 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | *olen = ilen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1466 | return mbedtls_chachapoly_encrypt_and_tag(ctx->cipher_ctx, |
| 1467 | ilen, iv, ad, ad_len, input, output, tag); |
Daniel King | 8fe4701 | 2016-05-17 20:33:28 -0300 | [diff] [blame] | 1468 | } |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 1469 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
Manuel Pégourié-Gonnard | 4562ffe | 2014-05-13 12:19:29 +0200 | [diff] [blame] | 1470 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1471 | return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | 4562ffe | 2014-05-13 12:19:29 +0200 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | /* |
TRodziewicz | 18efb73 | 2021-04-29 23:12:19 +0200 | [diff] [blame] | 1475 | * Packet-oriented encryption for AEAD modes: internal function used by |
| 1476 | * mbedtls_cipher_auth_encrypt_ext(). |
Manuel Pégourié-Gonnard | 4562ffe | 2014-05-13 12:19:29 +0200 | [diff] [blame] | 1477 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1478 | static int mbedtls_cipher_aead_decrypt(mbedtls_cipher_context_t *ctx, |
| 1479 | const unsigned char *iv, size_t iv_len, |
| 1480 | const unsigned char *ad, size_t ad_len, |
| 1481 | const unsigned char *input, size_t ilen, |
| 1482 | unsigned char *output, size_t *olen, |
| 1483 | const unsigned char *tag, size_t tag_len) |
Manuel Pégourié-Gonnard | 4562ffe | 2014-05-13 12:19:29 +0200 | [diff] [blame] | 1484 | { |
Manuel Pégourié-Gonnard | 5c731b0 | 2023-06-21 10:37:30 +0200 | [diff] [blame] | 1485 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1486 | if (ctx->psa_enabled == 1) { |
Hanno Becker | fe73ade | 2018-11-12 16:26:46 +0000 | [diff] [blame] | 1487 | /* As in the non-PSA case, we don't check that |
| 1488 | * a key has been set. If not, the key slot will |
| 1489 | * still be in its default state of 0, which is |
| 1490 | * guaranteed to be invalid, hence the PSA-call |
| 1491 | * below will gracefully fail. */ |
| 1492 | mbedtls_cipher_context_psa * const cipher_psa = |
| 1493 | (mbedtls_cipher_context_psa *) ctx->cipher_ctx; |
| 1494 | |
| 1495 | psa_status_t status; |
| 1496 | |
| 1497 | /* PSA Crypto API always writes the authentication tag |
| 1498 | * at the end of the encrypted message. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1499 | if (input == NULL || tag != input + ilen) { |
| 1500 | return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
| 1501 | } |
Hanno Becker | fe73ade | 2018-11-12 16:26:46 +0000 | [diff] [blame] | 1502 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1503 | status = psa_aead_decrypt(cipher_psa->slot, |
| 1504 | cipher_psa->alg, |
| 1505 | iv, iv_len, |
| 1506 | ad, ad_len, |
| 1507 | input, ilen + tag_len, |
| 1508 | output, ilen, olen); |
| 1509 | if (status == PSA_ERROR_INVALID_SIGNATURE) { |
| 1510 | return MBEDTLS_ERR_CIPHER_AUTH_FAILED; |
| 1511 | } else if (status != PSA_SUCCESS) { |
| 1512 | return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
| 1513 | } |
Hanno Becker | fe73ade | 2018-11-12 16:26:46 +0000 | [diff] [blame] | 1514 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1515 | return 0; |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 1516 | } |
Manuel Pégourié-Gonnard | 5c731b0 | 2023-06-21 10:37:30 +0200 | [diff] [blame] | 1517 | #endif /* MBEDTLS_USE_PSA_CRYPTO && !MBEDTLS_DEPRECATED_REMOVED */ |
Hanno Becker | ce1ddee | 2018-11-09 16:20:29 +0000 | [diff] [blame] | 1518 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1519 | #if defined(MBEDTLS_GCM_C) |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 1520 | if (MBEDTLS_MODE_GCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1521 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 4562ffe | 2014-05-13 12:19:29 +0200 | [diff] [blame] | 1522 | |
| 1523 | *olen = ilen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1524 | ret = mbedtls_gcm_auth_decrypt(ctx->cipher_ctx, ilen, |
| 1525 | iv, iv_len, ad, ad_len, |
| 1526 | tag, tag_len, input, output); |
Manuel Pégourié-Gonnard | 4562ffe | 2014-05-13 12:19:29 +0200 | [diff] [blame] | 1527 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1528 | if (ret == MBEDTLS_ERR_GCM_AUTH_FAILED) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1529 | ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1530 | } |
Manuel Pégourié-Gonnard | 4562ffe | 2014-05-13 12:19:29 +0200 | [diff] [blame] | 1531 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1532 | return ret; |
Manuel Pégourié-Gonnard | 4562ffe | 2014-05-13 12:19:29 +0200 | [diff] [blame] | 1533 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1534 | #endif /* MBEDTLS_GCM_C */ |
| 1535 | #if defined(MBEDTLS_CCM_C) |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 1536 | if (MBEDTLS_MODE_CCM == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1537 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 4193695 | 2014-05-13 13:18:17 +0200 | [diff] [blame] | 1538 | |
| 1539 | *olen = ilen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1540 | ret = mbedtls_ccm_auth_decrypt(ctx->cipher_ctx, ilen, |
| 1541 | iv, iv_len, ad, ad_len, |
| 1542 | input, output, tag, tag_len); |
Manuel Pégourié-Gonnard | 4193695 | 2014-05-13 13:18:17 +0200 | [diff] [blame] | 1543 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1544 | if (ret == MBEDTLS_ERR_CCM_AUTH_FAILED) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1545 | ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1546 | } |
Manuel Pégourié-Gonnard | 4193695 | 2014-05-13 13:18:17 +0200 | [diff] [blame] | 1547 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1548 | return ret; |
Manuel Pégourié-Gonnard | 4193695 | 2014-05-13 13:18:17 +0200 | [diff] [blame] | 1549 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1550 | #endif /* MBEDTLS_CCM_C */ |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 1551 | #if defined(MBEDTLS_CHACHAPOLY_C) |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 1552 | if (MBEDTLS_CIPHER_CHACHA20_POLY1305 == ((mbedtls_cipher_type_t) ctx->cipher_info->type)) { |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 1553 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Daniel King | 8fe4701 | 2016-05-17 20:33:28 -0300 | [diff] [blame] | 1554 | |
Manuel Pégourié-Gonnard | fe725de | 2018-05-08 09:38:09 +0200 | [diff] [blame] | 1555 | /* ChachaPoly has fixed length nonce and MAC (tag) */ |
Dave Rodgman | bb521fd | 2023-06-24 11:21:25 +0100 | [diff] [blame] | 1556 | if ((iv_len != mbedtls_cipher_info_get_iv_size(ctx->cipher_info)) || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1557 | (tag_len != 16U)) { |
| 1558 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
Daniel King | 8fe4701 | 2016-05-17 20:33:28 -0300 | [diff] [blame] | 1559 | } |
| 1560 | |
| 1561 | *olen = ilen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1562 | ret = mbedtls_chachapoly_auth_decrypt(ctx->cipher_ctx, ilen, |
| 1563 | iv, ad, ad_len, tag, input, output); |
Daniel King | 8fe4701 | 2016-05-17 20:33:28 -0300 | [diff] [blame] | 1564 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1565 | if (ret == MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED) { |
Manuel Pégourié-Gonnard | fe725de | 2018-05-08 09:38:09 +0200 | [diff] [blame] | 1566 | ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1567 | } |
Daniel King | 8fe4701 | 2016-05-17 20:33:28 -0300 | [diff] [blame] | 1568 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1569 | return ret; |
Daniel King | 8fe4701 | 2016-05-17 20:33:28 -0300 | [diff] [blame] | 1570 | } |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 1571 | #endif /* MBEDTLS_CHACHAPOLY_C */ |
Manuel Pégourié-Gonnard | faddf98 | 2020-11-25 13:39:47 +0100 | [diff] [blame] | 1572 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1573 | return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | faddf98 | 2020-11-25 13:39:47 +0100 | [diff] [blame] | 1574 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1575 | #endif /* MBEDTLS_CIPHER_MODE_AEAD */ |
Manuel Pégourié-Gonnard | 4562ffe | 2014-05-13 12:19:29 +0200 | [diff] [blame] | 1576 | |
Manuel Pégourié-Gonnard | faddf98 | 2020-11-25 13:39:47 +0100 | [diff] [blame] | 1577 | #if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C) |
| 1578 | /* |
| 1579 | * Packet-oriented encryption for AEAD/NIST_KW: public function. |
| 1580 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1581 | int mbedtls_cipher_auth_encrypt_ext(mbedtls_cipher_context_t *ctx, |
| 1582 | const unsigned char *iv, size_t iv_len, |
| 1583 | const unsigned char *ad, size_t ad_len, |
| 1584 | const unsigned char *input, size_t ilen, |
| 1585 | unsigned char *output, size_t output_len, |
| 1586 | size_t *olen, size_t tag_len) |
Manuel Pégourié-Gonnard | faddf98 | 2020-11-25 13:39:47 +0100 | [diff] [blame] | 1587 | { |
Manuel Pégourié-Gonnard | faddf98 | 2020-11-25 13:39:47 +0100 | [diff] [blame] | 1588 | #if defined(MBEDTLS_NIST_KW_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1589 | if ( |
Manuel Pégourié-Gonnard | 5c731b0 | 2023-06-21 10:37:30 +0200 | [diff] [blame] | 1590 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | a56d3d9 | 2020-12-04 00:47:07 +0100 | [diff] [blame] | 1591 | ctx->psa_enabled == 0 && |
| 1592 | #endif |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 1593 | (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) || |
| 1594 | MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) { |
| 1595 | mbedtls_nist_kw_mode_t mode = |
| 1596 | (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) ? |
| 1597 | MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP; |
Manuel Pégourié-Gonnard | faddf98 | 2020-11-25 13:39:47 +0100 | [diff] [blame] | 1598 | |
| 1599 | /* There is no iv, tag or ad associated with KW and KWP, |
| 1600 | * so these length should be 0 as documented. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1601 | if (iv_len != 0 || tag_len != 0 || ad_len != 0) { |
| 1602 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
| 1603 | } |
Manuel Pégourié-Gonnard | faddf98 | 2020-11-25 13:39:47 +0100 | [diff] [blame] | 1604 | |
Manuel Pégourié-Gonnard | 841b6fa | 2020-12-07 10:42:21 +0100 | [diff] [blame] | 1605 | (void) iv; |
| 1606 | (void) ad; |
| 1607 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1608 | return mbedtls_nist_kw_wrap(ctx->cipher_ctx, mode, input, ilen, |
| 1609 | output, olen, output_len); |
Manuel Pégourié-Gonnard | faddf98 | 2020-11-25 13:39:47 +0100 | [diff] [blame] | 1610 | } |
| 1611 | #endif /* MBEDTLS_NIST_KW_C */ |
| 1612 | |
| 1613 | #if defined(MBEDTLS_CIPHER_MODE_AEAD) |
| 1614 | /* AEAD case: check length before passing on to shared function */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1615 | if (output_len < ilen + tag_len) { |
| 1616 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
| 1617 | } |
Manuel Pégourié-Gonnard | faddf98 | 2020-11-25 13:39:47 +0100 | [diff] [blame] | 1618 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1619 | int ret = mbedtls_cipher_aead_encrypt(ctx, iv, iv_len, ad, ad_len, |
| 1620 | input, ilen, output, olen, |
| 1621 | output + ilen, tag_len); |
Manuel Pégourié-Gonnard | faddf98 | 2020-11-25 13:39:47 +0100 | [diff] [blame] | 1622 | *olen += tag_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1623 | return ret; |
Manuel Pégourié-Gonnard | faddf98 | 2020-11-25 13:39:47 +0100 | [diff] [blame] | 1624 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1625 | return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | faddf98 | 2020-11-25 13:39:47 +0100 | [diff] [blame] | 1626 | #endif /* MBEDTLS_CIPHER_MODE_AEAD */ |
| 1627 | } |
| 1628 | |
| 1629 | /* |
| 1630 | * Packet-oriented decryption for AEAD/NIST_KW: public function. |
| 1631 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1632 | int mbedtls_cipher_auth_decrypt_ext(mbedtls_cipher_context_t *ctx, |
| 1633 | const unsigned char *iv, size_t iv_len, |
| 1634 | const unsigned char *ad, size_t ad_len, |
| 1635 | const unsigned char *input, size_t ilen, |
| 1636 | unsigned char *output, size_t output_len, |
| 1637 | size_t *olen, size_t tag_len) |
Manuel Pégourié-Gonnard | faddf98 | 2020-11-25 13:39:47 +0100 | [diff] [blame] | 1638 | { |
Manuel Pégourié-Gonnard | faddf98 | 2020-11-25 13:39:47 +0100 | [diff] [blame] | 1639 | #if defined(MBEDTLS_NIST_KW_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1640 | if ( |
Manuel Pégourié-Gonnard | 5c731b0 | 2023-06-21 10:37:30 +0200 | [diff] [blame] | 1641 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | a56d3d9 | 2020-12-04 00:47:07 +0100 | [diff] [blame] | 1642 | ctx->psa_enabled == 0 && |
| 1643 | #endif |
Dave Rodgman | 1b8a3b1 | 2023-06-24 17:32:43 +0100 | [diff] [blame] | 1644 | (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode) || |
| 1645 | MBEDTLS_MODE_KWP == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode))) { |
| 1646 | mbedtls_nist_kw_mode_t mode = |
| 1647 | (MBEDTLS_MODE_KW == ((mbedtls_cipher_mode_t) ctx->cipher_info->mode)) ? |
| 1648 | MBEDTLS_KW_MODE_KW : MBEDTLS_KW_MODE_KWP; |
Manuel Pégourié-Gonnard | faddf98 | 2020-11-25 13:39:47 +0100 | [diff] [blame] | 1649 | |
| 1650 | /* There is no iv, tag or ad associated with KW and KWP, |
| 1651 | * so these length should be 0 as documented. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1652 | if (iv_len != 0 || tag_len != 0 || ad_len != 0) { |
| 1653 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
| 1654 | } |
Manuel Pégourié-Gonnard | faddf98 | 2020-11-25 13:39:47 +0100 | [diff] [blame] | 1655 | |
Manuel Pégourié-Gonnard | 841b6fa | 2020-12-07 10:42:21 +0100 | [diff] [blame] | 1656 | (void) iv; |
| 1657 | (void) ad; |
| 1658 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1659 | return mbedtls_nist_kw_unwrap(ctx->cipher_ctx, mode, input, ilen, |
| 1660 | output, olen, output_len); |
Manuel Pégourié-Gonnard | faddf98 | 2020-11-25 13:39:47 +0100 | [diff] [blame] | 1661 | } |
| 1662 | #endif /* MBEDTLS_NIST_KW_C */ |
| 1663 | |
| 1664 | #if defined(MBEDTLS_CIPHER_MODE_AEAD) |
| 1665 | /* AEAD case: check length before passing on to shared function */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1666 | if (ilen < tag_len || output_len < ilen - tag_len) { |
| 1667 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
| 1668 | } |
Manuel Pégourié-Gonnard | faddf98 | 2020-11-25 13:39:47 +0100 | [diff] [blame] | 1669 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1670 | return mbedtls_cipher_aead_decrypt(ctx, iv, iv_len, ad, ad_len, |
| 1671 | input, ilen - tag_len, output, olen, |
| 1672 | input + ilen - tag_len, tag_len); |
Manuel Pégourié-Gonnard | faddf98 | 2020-11-25 13:39:47 +0100 | [diff] [blame] | 1673 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1674 | return MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | faddf98 | 2020-11-25 13:39:47 +0100 | [diff] [blame] | 1675 | #endif /* MBEDTLS_CIPHER_MODE_AEAD */ |
| 1676 | } |
| 1677 | #endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */ |
| 1678 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1679 | #endif /* MBEDTLS_CIPHER_C */ |