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