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