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