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