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