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