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