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