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