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