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