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