| 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 | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 8 | *  Copyright (C) 2006-2014, ARM Limited, All Rights Reserved | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 9 | * | 
| Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 10 | *  This file is part of mbed TLS (https://tls.mbed.org) | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 11 | * | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 12 | *  This program is free software; you can redistribute it and/or modify | 
|  | 13 | *  it under the terms of the GNU General Public License as published by | 
|  | 14 | *  the Free Software Foundation; either version 2 of the License, or | 
|  | 15 | *  (at your option) any later version. | 
|  | 16 | * | 
|  | 17 | *  This program is distributed in the hope that it will be useful, | 
|  | 18 | *  but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 19 | *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 20 | *  GNU General Public License for more details. | 
|  | 21 | * | 
|  | 22 | *  You should have received a copy of the GNU General Public License along | 
|  | 23 | *  with this program; if not, write to the Free Software Foundation, Inc., | 
|  | 24 | *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | 
|  | 25 | */ | 
|  | 26 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 27 | #if !defined(MBEDTLS_CONFIG_FILE) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 28 | #include "mbedtls/config.h" | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 29 | #else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 30 | #include MBEDTLS_CONFIG_FILE | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 31 | #endif | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 32 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 33 | #if defined(MBEDTLS_CIPHER_C) | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 34 |  | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 35 | #include "mbedtls/cipher.h" | 
| Manuel Pégourié-Gonnard | 50518f4 | 2015-05-26 11:04:15 +0200 | [diff] [blame] | 36 | #include "mbedtls/cipher_internal.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 | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 41 | #if defined(MBEDTLS_GCM_C) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 42 | #include "mbedtls/gcm.h" | 
| Manuel Pégourié-Gonnard | 07f8fa5 | 2013-08-30 18:34:08 +0200 | [diff] [blame] | 43 | #endif | 
|  | 44 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 45 | #if defined(MBEDTLS_CCM_C) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 46 | #include "mbedtls/ccm.h" | 
| Manuel Pégourié-Gonnard | 4193695 | 2014-05-13 13:18:17 +0200 | [diff] [blame] | 47 | #endif | 
|  | 48 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 49 | #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) | 
|  | 50 | #define MBEDTLS_CIPHER_MODE_STREAM | 
| Manuel Pégourié-Gonnard | 37e230c | 2013-08-28 13:50:42 +0200 | [diff] [blame] | 51 | #endif | 
|  | 52 |  | 
| Paul Bakker | 6edcd41 | 2013-10-29 15:22:54 +0100 | [diff] [blame] | 53 | #if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \ | 
|  | 54 | !defined(EFI32) | 
| Paul Bakker | af5c85f | 2011-04-18 03:47:52 +0000 | [diff] [blame] | 55 | #define strcasecmp _stricmp | 
|  | 56 | #endif | 
|  | 57 |  | 
| Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 58 | /* Implementation that should never be optimized out by the compiler */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 59 | static void mbedtls_zeroize( void *v, size_t n ) { | 
| Paul Bakker | 3461772 | 2014-06-13 17:20:13 +0200 | [diff] [blame] | 60 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; | 
|  | 61 | } | 
|  | 62 |  | 
| Manuel Pégourié-Gonnard | dace82f | 2013-09-18 15:12:07 +0200 | [diff] [blame] | 63 | static int supported_init = 0; | 
| Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 64 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 65 | const int *mbedtls_cipher_list( void ) | 
| Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 66 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 67 | const mbedtls_cipher_definition_t *def; | 
| Manuel Pégourié-Gonnard | dace82f | 2013-09-18 15:12:07 +0200 | [diff] [blame] | 68 | int *type; | 
|  | 69 |  | 
|  | 70 | if( ! supported_init ) | 
|  | 71 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 72 | def = mbedtls_cipher_definitions; | 
|  | 73 | type = mbedtls_cipher_supported; | 
| Manuel Pégourié-Gonnard | dace82f | 2013-09-18 15:12:07 +0200 | [diff] [blame] | 74 |  | 
|  | 75 | while( def->type != 0 ) | 
|  | 76 | *type++ = (*def++).type; | 
|  | 77 |  | 
|  | 78 | *type = 0; | 
|  | 79 |  | 
|  | 80 | supported_init = 1; | 
|  | 81 | } | 
|  | 82 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 83 | return( mbedtls_cipher_supported ); | 
| Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 84 | } | 
|  | 85 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 86 | 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] | 87 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 88 | const mbedtls_cipher_definition_t *def; | 
| Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 89 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 90 | for( def = mbedtls_cipher_definitions; def->info != NULL; def++ ) | 
| Manuel Pégourié-Gonnard | dace82f | 2013-09-18 15:12:07 +0200 | [diff] [blame] | 91 | if( def->type == cipher_type ) | 
|  | 92 | return( def->info ); | 
| Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 93 |  | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 94 | return( NULL ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 95 | } | 
|  | 96 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 97 | 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] | 98 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 99 | const mbedtls_cipher_definition_t *def; | 
| Manuel Pégourié-Gonnard | dace82f | 2013-09-18 15:12:07 +0200 | [diff] [blame] | 100 |  | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 101 | if( NULL == cipher_name ) | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 102 | return( NULL ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 103 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 104 | for( def = mbedtls_cipher_definitions; def->info != NULL; def++ ) | 
| Manuel Pégourié-Gonnard | dace82f | 2013-09-18 15:12:07 +0200 | [diff] [blame] | 105 | if( !  strcasecmp( def->info->name, cipher_name ) ) | 
|  | 106 | return( def->info ); | 
| Paul Bakker | fab5c82 | 2012-02-06 16:45:10 +0000 | [diff] [blame] | 107 |  | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 108 | return( NULL ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 109 | } | 
|  | 110 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 111 | const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values( const mbedtls_cipher_id_t cipher_id, | 
| Paul Bakker | f46b695 | 2013-09-09 00:08:26 +0200 | [diff] [blame] | 112 | int key_length, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 113 | const mbedtls_cipher_mode_t mode ) | 
| Paul Bakker | f46b695 | 2013-09-09 00:08:26 +0200 | [diff] [blame] | 114 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 115 | const mbedtls_cipher_definition_t *def; | 
| Paul Bakker | f46b695 | 2013-09-09 00:08:26 +0200 | [diff] [blame] | 116 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 117 | for( def = mbedtls_cipher_definitions; def->info != NULL; def++ ) | 
| Manuel Pégourié-Gonnard | dace82f | 2013-09-18 15:12:07 +0200 | [diff] [blame] | 118 | if( def->info->base->cipher == cipher_id && | 
|  | 119 | def->info->key_length == (unsigned) key_length && | 
|  | 120 | def->info->mode == mode ) | 
|  | 121 | return( def->info ); | 
| Paul Bakker | f46b695 | 2013-09-09 00:08:26 +0200 | [diff] [blame] | 122 |  | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 123 | return( NULL ); | 
| Paul Bakker | f46b695 | 2013-09-09 00:08:26 +0200 | [diff] [blame] | 124 | } | 
|  | 125 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 126 | void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx ) | 
| Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 127 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 128 | memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) ); | 
| Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 129 | } | 
|  | 130 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 131 | void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx ) | 
| Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 132 | { | 
|  | 133 | if( ctx == NULL ) | 
|  | 134 | return; | 
|  | 135 |  | 
|  | 136 | if( ctx->cipher_ctx ) | 
|  | 137 | ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx ); | 
|  | 138 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 139 | mbedtls_zeroize( ctx, sizeof(mbedtls_cipher_context_t) ); | 
| Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 140 | } | 
|  | 141 |  | 
| Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 142 | 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] | 143 | { | 
|  | 144 | if( NULL == cipher_info || NULL == ctx ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 145 | return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 146 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 147 | memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 148 |  | 
| Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 149 | 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] | 150 | return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 151 |  | 
|  | 152 | ctx->cipher_info = cipher_info; | 
|  | 153 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 154 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) | 
| Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 155 | /* | 
|  | 156 | * Ignore possible errors caused by a cipher mode that doesn't use padding | 
|  | 157 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 158 | #if defined(MBEDTLS_CIPHER_PADDING_PKCS7) | 
|  | 159 | (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 ); | 
| Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 160 | #else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 161 | (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE ); | 
| Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 162 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 163 | #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */ | 
| Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 164 |  | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 165 | return( 0 ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 166 | } | 
|  | 167 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 168 | int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key, | 
|  | 169 | int key_length, const mbedtls_operation_t operation ) | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 170 | { | 
|  | 171 | if( NULL == ctx || NULL == ctx->cipher_info ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 172 | return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 173 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 174 | if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 && | 
| Manuel Pégourié-Gonnard | 398c57b | 2014-06-23 12:10:59 +0200 | [diff] [blame] | 175 | (int) ctx->cipher_info->key_length != key_length ) | 
|  | 176 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 177 | return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 398c57b | 2014-06-23 12:10:59 +0200 | [diff] [blame] | 178 | } | 
| Manuel Pégourié-Gonnard | dd0f57f | 2013-09-16 11:47:43 +0200 | [diff] [blame] | 179 |  | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 180 | ctx->key_length = key_length; | 
|  | 181 | ctx->operation = operation; | 
|  | 182 |  | 
| Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 183 | /* | 
| Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 184 | * For CFB and CTR mode always use the encryption key schedule | 
| Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 185 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 186 | if( MBEDTLS_ENCRYPT == operation || | 
|  | 187 | MBEDTLS_MODE_CFB == ctx->cipher_info->mode || | 
|  | 188 | MBEDTLS_MODE_CTR == ctx->cipher_info->mode ) | 
| Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 189 | { | 
|  | 190 | return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key, | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 191 | ctx->key_length ); | 
| Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 192 | } | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 193 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 194 | if( MBEDTLS_DECRYPT == operation ) | 
| Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 195 | return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key, | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 196 | ctx->key_length ); | 
|  | 197 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 198 | return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 199 | } | 
|  | 200 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 201 | int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx, | 
| Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 202 | const unsigned char *iv, size_t iv_len ) | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 203 | { | 
| Manuel Pégourié-Gonnard | a235b5b | 2013-09-03 13:25:52 +0200 | [diff] [blame] | 204 | size_t actual_iv_size; | 
| Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 205 |  | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 206 | if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 207 | return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 208 |  | 
| Manuel Pégourié-Gonnard | e0dca4a | 2013-10-24 16:54:25 +0200 | [diff] [blame] | 209 | /* avoid buffer overflow in ctx->iv */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 210 | if( iv_len > MBEDTLS_MAX_IV_LENGTH ) | 
|  | 211 | return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE ); | 
| Manuel Pégourié-Gonnard | e0dca4a | 2013-10-24 16:54:25 +0200 | [diff] [blame] | 212 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 213 | 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] | 214 | actual_iv_size = iv_len; | 
|  | 215 | else | 
| Manuel Pégourié-Gonnard | e0dca4a | 2013-10-24 16:54:25 +0200 | [diff] [blame] | 216 | { | 
| Manuel Pégourié-Gonnard | a235b5b | 2013-09-03 13:25:52 +0200 | [diff] [blame] | 217 | actual_iv_size = ctx->cipher_info->iv_size; | 
| Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 218 |  | 
| Manuel Pégourié-Gonnard | e0dca4a | 2013-10-24 16:54:25 +0200 | [diff] [blame] | 219 | /* avoid reading past the end of input buffer */ | 
|  | 220 | if( actual_iv_size > iv_len ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 221 | return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | e0dca4a | 2013-10-24 16:54:25 +0200 | [diff] [blame] | 222 | } | 
|  | 223 |  | 
| Manuel Pégourié-Gonnard | a235b5b | 2013-09-03 13:25:52 +0200 | [diff] [blame] | 224 | memcpy( ctx->iv, iv, actual_iv_size ); | 
|  | 225 | ctx->iv_size = actual_iv_size; | 
| Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 226 |  | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 227 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 228 | } | 
|  | 229 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 230 | int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx ) | 
| Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 231 | { | 
| Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 232 | if( NULL == ctx || NULL == ctx->cipher_info ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 233 | return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 234 |  | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 235 | ctx->unprocessed_len = 0; | 
|  | 236 |  | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 237 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 238 | } | 
|  | 239 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 240 | #if defined(MBEDTLS_GCM_C) | 
|  | 241 | int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx, | 
| Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 242 | const unsigned char *ad, size_t ad_len ) | 
|  | 243 | { | 
|  | 244 | if( NULL == ctx || NULL == ctx->cipher_info ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 245 | return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 246 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 247 | if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode ) | 
| Manuel Pégourié-Gonnard | 07f8fa5 | 2013-08-30 18:34:08 +0200 | [diff] [blame] | 248 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 249 | return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation, | 
| Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 250 | ctx->iv, ctx->iv_size, ad, ad_len ); | 
| Manuel Pégourié-Gonnard | 07f8fa5 | 2013-08-30 18:34:08 +0200 | [diff] [blame] | 251 | } | 
| Manuel Pégourié-Gonnard | 07f8fa5 | 2013-08-30 18:34:08 +0200 | [diff] [blame] | 252 |  | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 253 | return( 0 ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 254 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 255 | #endif /* MBEDTLS_GCM_C */ | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 256 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 257 | 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] | 258 | size_t ilen, unsigned char *output, size_t *olen ) | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 259 | { | 
| Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 260 | int ret; | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 261 |  | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 262 | if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen ) | 
| Paul Bakker | a885d68 | 2011-01-20 16:35:05 +0000 | [diff] [blame] | 263 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 264 | return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); | 
| Paul Bakker | a885d68 | 2011-01-20 16:35:05 +0000 | [diff] [blame] | 265 | } | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 266 |  | 
| Paul Bakker | 6c21276 | 2013-12-16 15:24:50 +0100 | [diff] [blame] | 267 | *olen = 0; | 
|  | 268 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 269 | if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB ) | 
| Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 270 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 271 | if( ilen != mbedtls_cipher_get_block_size( ctx ) ) | 
|  | 272 | return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED ); | 
| Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 273 |  | 
|  | 274 | *olen = ilen; | 
|  | 275 |  | 
|  | 276 | if( 0 != ( ret = ctx->cipher_info->base->ecb_func( ctx->cipher_ctx, | 
|  | 277 | ctx->operation, input, output ) ) ) | 
|  | 278 | { | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 279 | return( ret ); | 
| Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 280 | } | 
|  | 281 |  | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 282 | return( 0 ); | 
| Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 283 | } | 
|  | 284 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 285 | #if defined(MBEDTLS_GCM_C) | 
|  | 286 | if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM ) | 
| Manuel Pégourié-Gonnard | b8bd593 | 2013-09-05 13:38:15 +0200 | [diff] [blame] | 287 | { | 
|  | 288 | *olen = ilen; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 289 | return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input, | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 290 | output ); | 
| Manuel Pégourié-Gonnard | b8bd593 | 2013-09-05 13:38:15 +0200 | [diff] [blame] | 291 | } | 
|  | 292 | #endif | 
|  | 293 |  | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 294 | if( input == output && | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 295 | ( ctx->unprocessed_len != 0 || ilen % mbedtls_cipher_get_block_size( ctx ) ) ) | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 296 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 297 | return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); | 
| Paul Bakker | 68884e3 | 2013-01-07 18:20:04 +0100 | [diff] [blame] | 298 | } | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 299 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 300 | #if defined(MBEDTLS_CIPHER_MODE_CBC) | 
|  | 301 | if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC ) | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 302 | { | 
| Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 303 | size_t copy_len = 0; | 
|  | 304 |  | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 305 | /* | 
|  | 306 | * If there is not enough data for a full block, cache it. | 
|  | 307 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 308 | if( ( ctx->operation == MBEDTLS_DECRYPT && | 
|  | 309 | ilen + ctx->unprocessed_len <= mbedtls_cipher_get_block_size( ctx ) ) || | 
|  | 310 | ( ctx->operation == MBEDTLS_ENCRYPT && | 
|  | 311 | ilen + ctx->unprocessed_len < mbedtls_cipher_get_block_size( ctx ) ) ) | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 312 | { | 
|  | 313 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input, | 
|  | 314 | ilen ); | 
|  | 315 |  | 
|  | 316 | ctx->unprocessed_len += ilen; | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 317 | return( 0 ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 318 | } | 
|  | 319 |  | 
|  | 320 | /* | 
|  | 321 | * Process cached data first | 
|  | 322 | */ | 
|  | 323 | if( ctx->unprocessed_len != 0 ) | 
|  | 324 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 325 | copy_len = mbedtls_cipher_get_block_size( ctx ) - ctx->unprocessed_len; | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 326 |  | 
|  | 327 | memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input, | 
|  | 328 | copy_len ); | 
|  | 329 |  | 
| Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 330 | 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] | 331 | ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv, | 
| Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 332 | ctx->unprocessed_data, output ) ) ) | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 333 | { | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 334 | return( ret ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 335 | } | 
|  | 336 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 337 | *olen += mbedtls_cipher_get_block_size( ctx ); | 
|  | 338 | output += mbedtls_cipher_get_block_size( ctx ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 339 | ctx->unprocessed_len = 0; | 
|  | 340 |  | 
|  | 341 | input += copy_len; | 
|  | 342 | ilen -= copy_len; | 
|  | 343 | } | 
|  | 344 |  | 
|  | 345 | /* | 
|  | 346 | * Cache final, incomplete block | 
|  | 347 | */ | 
|  | 348 | if( 0 != ilen ) | 
|  | 349 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 350 | copy_len = ilen % mbedtls_cipher_get_block_size( ctx ); | 
|  | 351 | if( copy_len == 0 && ctx->operation == MBEDTLS_DECRYPT ) | 
|  | 352 | copy_len = mbedtls_cipher_get_block_size( ctx ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 353 |  | 
|  | 354 | memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ), | 
|  | 355 | copy_len ); | 
|  | 356 |  | 
|  | 357 | ctx->unprocessed_len += copy_len; | 
|  | 358 | ilen -= copy_len; | 
|  | 359 | } | 
|  | 360 |  | 
|  | 361 | /* | 
|  | 362 | * Process remaining full blocks | 
|  | 363 | */ | 
|  | 364 | if( ilen ) | 
|  | 365 | { | 
| Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 366 | if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx, | 
|  | 367 | ctx->operation, ilen, ctx->iv, input, output ) ) ) | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 368 | { | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 369 | return( ret ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 370 | } | 
| Manuel Pégourié-Gonnard | 07f8fa5 | 2013-08-30 18:34:08 +0200 | [diff] [blame] | 371 |  | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 372 | *olen += ilen; | 
|  | 373 | } | 
|  | 374 |  | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 375 | return( 0 ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 376 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 377 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 378 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 379 | #if defined(MBEDTLS_CIPHER_MODE_CFB) | 
|  | 380 | if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB ) | 
| Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 381 | { | 
| Paul Bakker | 6132d0a | 2012-07-04 17:10:40 +0000 | [diff] [blame] | 382 | if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx, | 
| Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 383 | ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv, | 
| Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 384 | input, output ) ) ) | 
| Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 385 | { | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 386 | return( ret ); | 
| Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 387 | } | 
|  | 388 |  | 
|  | 389 | *olen = ilen; | 
|  | 390 |  | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 391 | return( 0 ); | 
| Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 392 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 393 | #endif /* MBEDTLS_CIPHER_MODE_CFB */ | 
| Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 394 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 395 | #if defined(MBEDTLS_CIPHER_MODE_CTR) | 
|  | 396 | if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR ) | 
| Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 397 | { | 
| Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 398 | if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx, | 
| Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 399 | ilen, &ctx->unprocessed_len, ctx->iv, | 
| Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 400 | ctx->unprocessed_data, input, output ) ) ) | 
| Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 401 | { | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 402 | return( ret ); | 
| Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 403 | } | 
|  | 404 |  | 
|  | 405 | *olen = ilen; | 
|  | 406 |  | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 407 | return( 0 ); | 
| Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 408 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 409 | #endif /* MBEDTLS_CIPHER_MODE_CTR */ | 
| Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 410 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 411 | #if defined(MBEDTLS_CIPHER_MODE_STREAM) | 
|  | 412 | if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM ) | 
| Manuel Pégourié-Gonnard | 37e230c | 2013-08-28 13:50:42 +0200 | [diff] [blame] | 413 | { | 
|  | 414 | if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx, | 
|  | 415 | ilen, input, output ) ) ) | 
|  | 416 | { | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 417 | return( ret ); | 
| Manuel Pégourié-Gonnard | 37e230c | 2013-08-28 13:50:42 +0200 | [diff] [blame] | 418 | } | 
|  | 419 |  | 
|  | 420 | *olen = ilen; | 
|  | 421 |  | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 422 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 37e230c | 2013-08-28 13:50:42 +0200 | [diff] [blame] | 423 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 424 | #endif /* MBEDTLS_CIPHER_MODE_STREAM */ | 
| Manuel Pégourié-Gonnard | 37e230c | 2013-08-28 13:50:42 +0200 | [diff] [blame] | 425 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 426 | return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 427 | } | 
|  | 428 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 429 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) | 
|  | 430 | #if defined(MBEDTLS_CIPHER_PADDING_PKCS7) | 
| Manuel Pégourié-Gonnard | 679f9e9 | 2013-07-26 12:46:02 +0200 | [diff] [blame] | 431 | /* | 
|  | 432 | * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len | 
|  | 433 | */ | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 434 | static void add_pkcs_padding( unsigned char *output, size_t output_len, | 
|  | 435 | size_t data_len ) | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 436 | { | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 437 | size_t padding_len = output_len - data_len; | 
| Manuel Pégourié-Gonnard | f8ab069 | 2013-10-27 17:21:14 +0100 | [diff] [blame] | 438 | unsigned char i; | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 439 |  | 
|  | 440 | for( i = 0; i < padding_len; i++ ) | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 441 | output[data_len + i] = (unsigned char) padding_len; | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 442 | } | 
|  | 443 |  | 
| Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 444 | static int get_pkcs_padding( unsigned char *input, size_t input_len, | 
|  | 445 | size_t *data_len ) | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 446 | { | 
| Manuel Pégourié-Gonnard | f8ab069 | 2013-10-27 17:21:14 +0100 | [diff] [blame] | 447 | size_t i, pad_idx; | 
|  | 448 | unsigned char padding_len, bad = 0; | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 449 |  | 
| Paul Bakker | a885d68 | 2011-01-20 16:35:05 +0000 | [diff] [blame] | 450 | if( NULL == input || NULL == data_len ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 451 | return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 452 |  | 
|  | 453 | padding_len = input[input_len - 1]; | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 454 | *data_len = input_len - padding_len; | 
|  | 455 |  | 
| Manuel Pégourié-Gonnard | f8ab069 | 2013-10-27 17:21:14 +0100 | [diff] [blame] | 456 | /* Avoid logical || since it results in a branch */ | 
|  | 457 | bad |= padding_len > input_len; | 
|  | 458 | bad |= padding_len == 0; | 
|  | 459 |  | 
|  | 460 | /* The number of bytes checked must be independent of padding_len, | 
|  | 461 | * so pick input_len, which is usually 8 or 16 (one block) */ | 
|  | 462 | pad_idx = input_len - padding_len; | 
|  | 463 | for( i = 0; i < input_len; i++ ) | 
|  | 464 | bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx ); | 
|  | 465 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 466 | return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 467 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 468 | #endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */ | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 469 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 470 | #if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS) | 
| Manuel Pégourié-Gonnard | 679f9e9 | 2013-07-26 12:46:02 +0200 | [diff] [blame] | 471 | /* | 
|  | 472 | * One and zeros padding: fill with 80 00 ... 00 | 
|  | 473 | */ | 
|  | 474 | static void add_one_and_zeros_padding( unsigned char *output, | 
|  | 475 | size_t output_len, size_t data_len ) | 
|  | 476 | { | 
|  | 477 | size_t padding_len = output_len - data_len; | 
|  | 478 | unsigned char i = 0; | 
|  | 479 |  | 
|  | 480 | output[data_len] = 0x80; | 
|  | 481 | for( i = 1; i < padding_len; i++ ) | 
|  | 482 | output[data_len + i] = 0x00; | 
|  | 483 | } | 
|  | 484 |  | 
|  | 485 | static int get_one_and_zeros_padding( unsigned char *input, size_t input_len, | 
|  | 486 | size_t *data_len ) | 
|  | 487 | { | 
| Manuel Pégourié-Gonnard | 6c32990 | 2013-10-27 18:25:03 +0100 | [diff] [blame] | 488 | size_t i; | 
|  | 489 | unsigned char done = 0, prev_done, bad; | 
| Manuel Pégourié-Gonnard | 679f9e9 | 2013-07-26 12:46:02 +0200 | [diff] [blame] | 490 |  | 
|  | 491 | if( NULL == input || NULL == data_len ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 492 | return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 679f9e9 | 2013-07-26 12:46:02 +0200 | [diff] [blame] | 493 |  | 
| Manuel Pégourié-Gonnard | 6c32990 | 2013-10-27 18:25:03 +0100 | [diff] [blame] | 494 | bad = 0xFF; | 
|  | 495 | *data_len = 0; | 
|  | 496 | for( i = input_len; i > 0; i-- ) | 
|  | 497 | { | 
|  | 498 | prev_done = done; | 
|  | 499 | done |= ( input[i-1] != 0 ); | 
|  | 500 | *data_len |= ( i - 1 ) * ( done != prev_done ); | 
|  | 501 | bad &= ( input[i-1] ^ 0x80 ) | ( done == prev_done ); | 
|  | 502 | } | 
| Manuel Pégourié-Gonnard | 679f9e9 | 2013-07-26 12:46:02 +0200 | [diff] [blame] | 503 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 504 | return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) ); | 
| Manuel Pégourié-Gonnard | 679f9e9 | 2013-07-26 12:46:02 +0200 | [diff] [blame] | 505 |  | 
| Manuel Pégourié-Gonnard | 679f9e9 | 2013-07-26 12:46:02 +0200 | [diff] [blame] | 506 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 507 | #endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */ | 
| Manuel Pégourié-Gonnard | 679f9e9 | 2013-07-26 12:46:02 +0200 | [diff] [blame] | 508 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 509 | #if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN) | 
| Manuel Pégourié-Gonnard | 8d4291b | 2013-07-26 14:55:18 +0200 | [diff] [blame] | 510 | /* | 
|  | 511 | * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length | 
|  | 512 | */ | 
|  | 513 | static void add_zeros_and_len_padding( unsigned char *output, | 
|  | 514 | size_t output_len, size_t data_len ) | 
|  | 515 | { | 
|  | 516 | size_t padding_len = output_len - data_len; | 
|  | 517 | unsigned char i = 0; | 
|  | 518 |  | 
|  | 519 | for( i = 1; i < padding_len; i++ ) | 
|  | 520 | output[data_len + i - 1] = 0x00; | 
|  | 521 | output[output_len - 1] = (unsigned char) padding_len; | 
|  | 522 | } | 
|  | 523 |  | 
|  | 524 | static int get_zeros_and_len_padding( unsigned char *input, size_t input_len, | 
|  | 525 | size_t *data_len ) | 
|  | 526 | { | 
| Manuel Pégourié-Gonnard | d17df51 | 2013-10-27 17:32:43 +0100 | [diff] [blame] | 527 | size_t i, pad_idx; | 
|  | 528 | unsigned char padding_len, bad = 0; | 
| Manuel Pégourié-Gonnard | 8d4291b | 2013-07-26 14:55:18 +0200 | [diff] [blame] | 529 |  | 
|  | 530 | if( NULL == input || NULL == data_len ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 531 | return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 8d4291b | 2013-07-26 14:55:18 +0200 | [diff] [blame] | 532 |  | 
|  | 533 | padding_len = input[input_len - 1]; | 
| Manuel Pégourié-Gonnard | 8d4291b | 2013-07-26 14:55:18 +0200 | [diff] [blame] | 534 | *data_len = input_len - padding_len; | 
|  | 535 |  | 
| Manuel Pégourié-Gonnard | d17df51 | 2013-10-27 17:32:43 +0100 | [diff] [blame] | 536 | /* Avoid logical || since it results in a branch */ | 
|  | 537 | bad |= padding_len > input_len; | 
|  | 538 | bad |= padding_len == 0; | 
|  | 539 |  | 
|  | 540 | /* The number of bytes checked must be independent of padding_len */ | 
|  | 541 | pad_idx = input_len - padding_len; | 
|  | 542 | for( i = 0; i < input_len - 1; i++ ) | 
|  | 543 | bad |= input[i] * ( i >= pad_idx ); | 
|  | 544 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 545 | return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) ); | 
| Manuel Pégourié-Gonnard | 8d4291b | 2013-07-26 14:55:18 +0200 | [diff] [blame] | 546 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 547 | #endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */ | 
| Manuel Pégourié-Gonnard | 8d4291b | 2013-07-26 14:55:18 +0200 | [diff] [blame] | 548 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 549 | #if defined(MBEDTLS_CIPHER_PADDING_ZEROS) | 
| Manuel Pégourié-Gonnard | 0e7d2c0 | 2013-07-26 16:05:14 +0200 | [diff] [blame] | 550 | /* | 
|  | 551 | * Zero padding: fill with 00 ... 00 | 
|  | 552 | */ | 
|  | 553 | static void add_zeros_padding( unsigned char *output, | 
|  | 554 | size_t output_len, size_t data_len ) | 
|  | 555 | { | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 556 | size_t i; | 
| Manuel Pégourié-Gonnard | 0e7d2c0 | 2013-07-26 16:05:14 +0200 | [diff] [blame] | 557 |  | 
|  | 558 | for( i = data_len; i < output_len; i++ ) | 
|  | 559 | output[i] = 0x00; | 
|  | 560 | } | 
|  | 561 |  | 
|  | 562 | static int get_zeros_padding( unsigned char *input, size_t input_len, | 
|  | 563 | size_t *data_len ) | 
|  | 564 | { | 
| Manuel Pégourié-Gonnard | e68bf17 | 2013-10-27 18:26:39 +0100 | [diff] [blame] | 565 | size_t i; | 
|  | 566 | unsigned char done = 0, prev_done; | 
|  | 567 |  | 
| Manuel Pégourié-Gonnard | 0e7d2c0 | 2013-07-26 16:05:14 +0200 | [diff] [blame] | 568 | if( NULL == input || NULL == data_len ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 569 | return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 0e7d2c0 | 2013-07-26 16:05:14 +0200 | [diff] [blame] | 570 |  | 
| Manuel Pégourié-Gonnard | e68bf17 | 2013-10-27 18:26:39 +0100 | [diff] [blame] | 571 | *data_len = 0; | 
|  | 572 | for( i = input_len; i > 0; i-- ) | 
|  | 573 | { | 
|  | 574 | prev_done = done; | 
|  | 575 | done |= ( input[i-1] != 0 ); | 
|  | 576 | *data_len |= i * ( done != prev_done ); | 
|  | 577 | } | 
| Manuel Pégourié-Gonnard | 0e7d2c0 | 2013-07-26 16:05:14 +0200 | [diff] [blame] | 578 |  | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 579 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 0e7d2c0 | 2013-07-26 16:05:14 +0200 | [diff] [blame] | 580 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 581 | #endif /* MBEDTLS_CIPHER_PADDING_ZEROS */ | 
| Manuel Pégourié-Gonnard | 0e7d2c0 | 2013-07-26 16:05:14 +0200 | [diff] [blame] | 582 |  | 
| Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 583 | /* | 
|  | 584 | * No padding: don't pad :) | 
|  | 585 | * | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 586 | * 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] | 587 | * but a trivial get_padding function | 
|  | 588 | */ | 
|  | 589 | static int get_no_padding( unsigned char *input, size_t input_len, | 
|  | 590 | size_t *data_len ) | 
|  | 591 | { | 
|  | 592 | if( NULL == input || NULL == data_len ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 593 | return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 594 |  | 
|  | 595 | *data_len = input_len; | 
|  | 596 |  | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 597 | return( 0 ); | 
| Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 598 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 599 | #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */ | 
| Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 600 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 601 | int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx, | 
| Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 602 | unsigned char *output, size_t *olen ) | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 603 | { | 
|  | 604 | if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 605 | return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 606 |  | 
|  | 607 | *olen = 0; | 
|  | 608 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 609 | if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode || | 
|  | 610 | MBEDTLS_MODE_CTR == ctx->cipher_info->mode || | 
|  | 611 | MBEDTLS_MODE_GCM == ctx->cipher_info->mode || | 
|  | 612 | MBEDTLS_MODE_STREAM == ctx->cipher_info->mode ) | 
| Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 613 | { | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 614 | return( 0 ); | 
| Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 615 | } | 
|  | 616 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 617 | if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode ) | 
| Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 618 | { | 
|  | 619 | if( ctx->unprocessed_len != 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 620 | return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED ); | 
| Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 621 |  | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 622 | return( 0 ); | 
| Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 623 | } | 
|  | 624 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 625 | #if defined(MBEDTLS_CIPHER_MODE_CBC) | 
|  | 626 | if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode ) | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 627 | { | 
| Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 628 | int ret = 0; | 
|  | 629 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 630 | if( MBEDTLS_ENCRYPT == ctx->operation ) | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 631 | { | 
| Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 632 | /* check for 'no padding' mode */ | 
|  | 633 | if( NULL == ctx->add_padding ) | 
|  | 634 | { | 
|  | 635 | if( 0 != ctx->unprocessed_len ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 636 | return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED ); | 
| Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 637 |  | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 638 | return( 0 ); | 
| Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 639 | } | 
|  | 640 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 641 | ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ), | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 642 | ctx->unprocessed_len ); | 
|  | 643 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 644 | else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len ) | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 645 | { | 
| Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 646 | /* | 
|  | 647 | * For decrypt operations, expect a full block, | 
|  | 648 | * or an empty block if no padding | 
|  | 649 | */ | 
|  | 650 | if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len ) | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 651 | return( 0 ); | 
| Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 652 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 653 | return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 654 | } | 
|  | 655 |  | 
|  | 656 | /* cipher block */ | 
| Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 657 | if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 658 | ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv, | 
| Paul Bakker | ff61a78 | 2011-06-09 15:42:02 +0000 | [diff] [blame] | 659 | ctx->unprocessed_data, output ) ) ) | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 660 | { | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 661 | return( ret ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 662 | } | 
|  | 663 |  | 
|  | 664 | /* Set output size for decryption */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 665 | if( MBEDTLS_DECRYPT == ctx->operation ) | 
|  | 666 | return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ), | 
| Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 667 | olen ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 668 |  | 
|  | 669 | /* Set output size for encryption */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 670 | *olen = mbedtls_cipher_get_block_size( ctx ); | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 671 | return( 0 ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 672 | } | 
| Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 673 | #else | 
|  | 674 | ((void) output); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 675 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 676 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 677 | return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE ); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 678 | } | 
|  | 679 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 680 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) | 
|  | 681 | int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode ) | 
| Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 682 | { | 
|  | 683 | if( NULL == ctx || | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 684 | MBEDTLS_MODE_CBC != ctx->cipher_info->mode ) | 
| Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 685 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 686 | return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 687 | } | 
|  | 688 |  | 
| Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 689 | switch( mode ) | 
| Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 690 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 691 | #if defined(MBEDTLS_CIPHER_PADDING_PKCS7) | 
|  | 692 | case MBEDTLS_PADDING_PKCS7: | 
| Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 693 | ctx->add_padding = add_pkcs_padding; | 
|  | 694 | ctx->get_padding = get_pkcs_padding; | 
| Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 695 | break; | 
| Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 696 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 697 | #if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS) | 
|  | 698 | case MBEDTLS_PADDING_ONE_AND_ZEROS: | 
| Manuel Pégourié-Gonnard | 679f9e9 | 2013-07-26 12:46:02 +0200 | [diff] [blame] | 699 | ctx->add_padding = add_one_and_zeros_padding; | 
|  | 700 | ctx->get_padding = get_one_and_zeros_padding; | 
| Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 701 | break; | 
| Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 702 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 703 | #if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN) | 
|  | 704 | case MBEDTLS_PADDING_ZEROS_AND_LEN: | 
| Manuel Pégourié-Gonnard | 8d4291b | 2013-07-26 14:55:18 +0200 | [diff] [blame] | 705 | ctx->add_padding = add_zeros_and_len_padding; | 
|  | 706 | ctx->get_padding = get_zeros_and_len_padding; | 
| Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 707 | break; | 
| Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 708 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 709 | #if defined(MBEDTLS_CIPHER_PADDING_ZEROS) | 
|  | 710 | case MBEDTLS_PADDING_ZEROS: | 
| Manuel Pégourié-Gonnard | 0e7d2c0 | 2013-07-26 16:05:14 +0200 | [diff] [blame] | 711 | ctx->add_padding = add_zeros_padding; | 
|  | 712 | ctx->get_padding = get_zeros_padding; | 
| Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 713 | break; | 
| Paul Bakker | 48e93c8 | 2013-08-14 12:21:18 +0200 | [diff] [blame] | 714 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 715 | case MBEDTLS_PADDING_NONE: | 
| Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 716 | ctx->add_padding = NULL; | 
|  | 717 | ctx->get_padding = get_no_padding; | 
| Paul Bakker | 1a45d91 | 2013-08-14 12:04:26 +0200 | [diff] [blame] | 718 | break; | 
|  | 719 |  | 
|  | 720 | default: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 721 | return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE ); | 
| Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 722 | } | 
|  | 723 |  | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 724 | return( 0 ); | 
| Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 725 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 726 | #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */ | 
| Manuel Pégourié-Gonnard | ac56a1a | 2013-07-25 12:31:10 +0200 | [diff] [blame] | 727 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 728 | #if defined(MBEDTLS_GCM_C) | 
|  | 729 | int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx, | 
| Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 730 | unsigned char *tag, size_t tag_len ) | 
|  | 731 | { | 
|  | 732 | if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 733 | return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 734 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 735 | if( MBEDTLS_ENCRYPT != ctx->operation ) | 
|  | 736 | return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 737 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 738 | if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode ) | 
|  | 739 | return mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx, tag, tag_len ); | 
| Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 740 |  | 
| Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 741 | return( 0 ); | 
| Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 742 | } | 
| Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 743 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 744 | int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx, | 
| Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 745 | const unsigned char *tag, size_t tag_len ) | 
|  | 746 | { | 
|  | 747 | int ret; | 
| Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 748 |  | 
| Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 749 | if( NULL == ctx || NULL == ctx->cipher_info || | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 750 | MBEDTLS_DECRYPT != ctx->operation ) | 
| Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 751 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 752 | return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 753 | } | 
| Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 754 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 755 | if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode ) | 
| Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 756 | { | 
|  | 757 | unsigned char check_tag[16]; | 
|  | 758 | size_t i; | 
|  | 759 | int diff; | 
|  | 760 |  | 
|  | 761 | if( tag_len > sizeof( check_tag ) ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 762 | return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); | 
| Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 763 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 764 | if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx, | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 765 | check_tag, tag_len ) ) ) | 
|  | 766 | { | 
| Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 767 | return( ret ); | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 768 | } | 
| Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 769 |  | 
|  | 770 | /* Check the tag in "constant-time" */ | 
|  | 771 | for( diff = 0, i = 0; i < tag_len; i++ ) | 
|  | 772 | diff |= tag[i] ^ check_tag[i]; | 
|  | 773 |  | 
|  | 774 | if( diff != 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 775 | return( MBEDTLS_ERR_CIPHER_AUTH_FAILED ); | 
| Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 776 |  | 
| Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 777 | return( 0 ); | 
| Manuel Pégourié-Gonnard | 43a4780 | 2013-09-03 16:35:53 +0200 | [diff] [blame] | 778 | } | 
| Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 779 |  | 
|  | 780 | return( 0 ); | 
|  | 781 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 782 | #endif /* MBEDTLS_GCM_C */ | 
| Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 783 |  | 
| Manuel Pégourié-Gonnard | 3c1d150 | 2014-05-12 13:46:08 +0200 | [diff] [blame] | 784 | /* | 
|  | 785 | * Packet-oriented wrapper for non-AEAD modes | 
|  | 786 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 787 | int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx, | 
| Manuel Pégourié-Gonnard | 3c1d150 | 2014-05-12 13:46:08 +0200 | [diff] [blame] | 788 | const unsigned char *iv, size_t iv_len, | 
|  | 789 | const unsigned char *input, size_t ilen, | 
|  | 790 | unsigned char *output, size_t *olen ) | 
|  | 791 | { | 
|  | 792 | int ret; | 
|  | 793 | size_t finish_olen; | 
|  | 794 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 795 | 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] | 796 | return( ret ); | 
|  | 797 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 798 | if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | 3c1d150 | 2014-05-12 13:46:08 +0200 | [diff] [blame] | 799 | return( ret ); | 
|  | 800 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 801 | 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] | 802 | return( ret ); | 
|  | 803 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 804 | 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] | 805 | return( ret ); | 
|  | 806 |  | 
|  | 807 | *olen += finish_olen; | 
|  | 808 |  | 
|  | 809 | return( 0 ); | 
|  | 810 | } | 
|  | 811 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 812 | #if defined(MBEDTLS_CIPHER_MODE_AEAD) | 
| Manuel Pégourié-Gonnard | 4562ffe | 2014-05-13 12:19:29 +0200 | [diff] [blame] | 813 | /* | 
|  | 814 | * Packet-oriented encryption for AEAD modes | 
|  | 815 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 816 | int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx, | 
| Manuel Pégourié-Gonnard | 4562ffe | 2014-05-13 12:19:29 +0200 | [diff] [blame] | 817 | const unsigned char *iv, size_t iv_len, | 
|  | 818 | const unsigned char *ad, size_t ad_len, | 
|  | 819 | const unsigned char *input, size_t ilen, | 
|  | 820 | unsigned char *output, size_t *olen, | 
|  | 821 | unsigned char *tag, size_t tag_len ) | 
|  | 822 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 823 | #if defined(MBEDTLS_GCM_C) | 
|  | 824 | if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode ) | 
| Manuel Pégourié-Gonnard | 4562ffe | 2014-05-13 12:19:29 +0200 | [diff] [blame] | 825 | { | 
|  | 826 | *olen = ilen; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 827 | 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] | 828 | iv, iv_len, ad, ad_len, input, output, | 
|  | 829 | tag_len, tag ) ); | 
|  | 830 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 831 | #endif /* MBEDTLS_GCM_C */ | 
|  | 832 | #if defined(MBEDTLS_CCM_C) | 
|  | 833 | if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode ) | 
| Manuel Pégourié-Gonnard | 4193695 | 2014-05-13 13:18:17 +0200 | [diff] [blame] | 834 | { | 
|  | 835 | *olen = ilen; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 836 | return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen, | 
| Manuel Pégourié-Gonnard | 4193695 | 2014-05-13 13:18:17 +0200 | [diff] [blame] | 837 | iv, iv_len, ad, ad_len, input, output, | 
|  | 838 | tag, tag_len ) ); | 
|  | 839 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 840 | #endif /* MBEDTLS_CCM_C */ | 
| Manuel Pégourié-Gonnard | 4562ffe | 2014-05-13 12:19:29 +0200 | [diff] [blame] | 841 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 842 | return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE ); | 
| Manuel Pégourié-Gonnard | 4562ffe | 2014-05-13 12:19:29 +0200 | [diff] [blame] | 843 | } | 
|  | 844 |  | 
|  | 845 | /* | 
|  | 846 | * Packet-oriented decryption for AEAD modes | 
|  | 847 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 848 | int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx, | 
| Manuel Pégourié-Gonnard | 4562ffe | 2014-05-13 12:19:29 +0200 | [diff] [blame] | 849 | const unsigned char *iv, size_t iv_len, | 
|  | 850 | const unsigned char *ad, size_t ad_len, | 
|  | 851 | const unsigned char *input, size_t ilen, | 
|  | 852 | unsigned char *output, size_t *olen, | 
|  | 853 | const unsigned char *tag, size_t tag_len ) | 
|  | 854 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 855 | #if defined(MBEDTLS_GCM_C) | 
|  | 856 | if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode ) | 
| Manuel Pégourié-Gonnard | 4562ffe | 2014-05-13 12:19:29 +0200 | [diff] [blame] | 857 | { | 
|  | 858 | int ret; | 
|  | 859 |  | 
|  | 860 | *olen = ilen; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 861 | ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen, | 
| Manuel Pégourié-Gonnard | 4562ffe | 2014-05-13 12:19:29 +0200 | [diff] [blame] | 862 | iv, iv_len, ad, ad_len, | 
|  | 863 | tag, tag_len, input, output ); | 
|  | 864 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 865 | if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED ) | 
|  | 866 | ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED; | 
| Manuel Pégourié-Gonnard | 4562ffe | 2014-05-13 12:19:29 +0200 | [diff] [blame] | 867 |  | 
|  | 868 | return( ret ); | 
|  | 869 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 870 | #endif /* MBEDTLS_GCM_C */ | 
|  | 871 | #if defined(MBEDTLS_CCM_C) | 
|  | 872 | if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode ) | 
| Manuel Pégourié-Gonnard | 4193695 | 2014-05-13 13:18:17 +0200 | [diff] [blame] | 873 | { | 
|  | 874 | int ret; | 
|  | 875 |  | 
|  | 876 | *olen = ilen; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 877 | ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen, | 
| Manuel Pégourié-Gonnard | 4193695 | 2014-05-13 13:18:17 +0200 | [diff] [blame] | 878 | iv, iv_len, ad, ad_len, | 
|  | 879 | input, output, tag, tag_len ); | 
|  | 880 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 881 | if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED ) | 
|  | 882 | ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED; | 
| Manuel Pégourié-Gonnard | 4193695 | 2014-05-13 13:18:17 +0200 | [diff] [blame] | 883 |  | 
|  | 884 | return( ret ); | 
|  | 885 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 886 | #endif /* MBEDTLS_CCM_C */ | 
| Manuel Pégourié-Gonnard | 4562ffe | 2014-05-13 12:19:29 +0200 | [diff] [blame] | 887 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 888 | return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE ); | 
| Manuel Pégourié-Gonnard | 4562ffe | 2014-05-13 12:19:29 +0200 | [diff] [blame] | 889 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 890 | #endif /* MBEDTLS_CIPHER_MODE_AEAD */ | 
| Manuel Pégourié-Gonnard | 4562ffe | 2014-05-13 12:19:29 +0200 | [diff] [blame] | 891 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 892 | #endif /* MBEDTLS_CIPHER_C */ |