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