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