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