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