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