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