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