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