[MBEDTLS] Update mbedtls to v2.1.1. By Ismael Ferreras Morezuelas with a fix by Roy...
[reactos.git] / reactos / dll / 3rdparty / mbedtls / cipher.c
index 2f886d9..ccc0685 100644 (file)
@@ -5,71 +5,66 @@
  *
  * \author Adriaan de Jong <dejong@fox-it.com>
  *
- *  Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
+ *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
+ *  SPDX-License-Identifier: Apache-2.0
  *
- *  This file is part of mbed TLS (https://polarssl.org)
+ *  Licensed under the Apache License, Version 2.0 (the "License"); you may
+ *  not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
  *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
+ *  http://www.apache.org/licenses/LICENSE-2.0
  *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
  *
- *  You should have received a copy of the GNU General Public License along
- *  with this program; if not, write to the Free Software Foundation, Inc.,
- *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *  This file is part of mbed TLS (https://tls.mbed.org)
  */
 
-#if !defined(POLARSSL_CONFIG_FILE)
-#include "polarssl/config.h"
+#if !defined(MBEDTLS_CONFIG_FILE)
+#include "mbedtls/config.h"
 #else
-#include POLARSSL_CONFIG_FILE
+#include MBEDTLS_CONFIG_FILE
 #endif
 
-#if defined(POLARSSL_CIPHER_C)
+#if defined(MBEDTLS_CIPHER_C)
 
-#include "polarssl/cipher.h"
-#include "polarssl/cipher_wrap.h"
+#include "mbedtls/cipher.h"
+#include "mbedtls/cipher_internal.h"
 
-#if defined(POLARSSL_GCM_C)
-#include "polarssl/gcm.h"
-#endif
+#include <stdlib.h>
+#include <string.h>
 
-#if defined(POLARSSL_CCM_C)
-#include "polarssl/ccm.h"
+#if defined(MBEDTLS_GCM_C)
+#include "mbedtls/gcm.h"
 #endif
 
-#include <stdlib.h>
-
-#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
-#define POLARSSL_CIPHER_MODE_STREAM
+#if defined(MBEDTLS_CCM_C)
+#include "mbedtls/ccm.h"
 #endif
 
-#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
-    !defined(EFI32)
-#define strcasecmp _stricmp
+#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
+#define MBEDTLS_CIPHER_MODE_STREAM
 #endif
 
 /* Implementation that should never be optimized out by the compiler */
-static void polarssl_zeroize( void *v, size_t n ) {
+static void mbedtls_zeroize( void *v, size_t n ) {
     volatile unsigned char *p = v; while( n-- ) *p++ = 0;
 }
 
 static int supported_init = 0;
 
-const int *cipher_list( void )
+const int *mbedtls_cipher_list( void )
 {
-    const cipher_definition_t *def;
+    const mbedtls_cipher_definition_t *def;
     int *type;
 
     if( ! supported_init )
     {
-        def = cipher_definitions;
-        type = supported_ciphers;
+        def = mbedtls_cipher_definitions;
+        type = mbedtls_cipher_supported;
 
         while( def->type != 0 )
             *type++ = (*def++).type;
@@ -79,55 +74,55 @@ const int *cipher_list( void )
         supported_init = 1;
     }
 
-    return( supported_ciphers );
+    return( mbedtls_cipher_supported );
 }
 
-const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type )
+const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type )
 {
-    const cipher_definition_t *def;
+    const mbedtls_cipher_definition_t *def;
 
-    for( def = cipher_definitions; def->info != NULL; def++ )
+    for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
         if( def->type == cipher_type )
             return( def->info );
 
     return( NULL );
 }
 
-const cipher_info_t *cipher_info_from_string( const char *cipher_name )
+const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name )
 {
-    const cipher_definition_t *def;
+    const mbedtls_cipher_definition_t *def;
 
     if( NULL == cipher_name )
         return( NULL );
 
-    for( def = cipher_definitions; def->info != NULL; def++ )
-        if( !  strcasecmp( def->info->name, cipher_name ) )
+    for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
+        if( !  strcmp( def->info->name, cipher_name ) )
             return( def->info );
 
     return( NULL );
 }
 
-const cipher_info_t *cipher_info_from_values( const cipher_id_t cipher_id,
-                                              int key_length,
-                                              const cipher_mode_t mode )
+const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values( const mbedtls_cipher_id_t cipher_id,
+                                              int key_bitlen,
+                                              const mbedtls_cipher_mode_t mode )
 {
-    const cipher_definition_t *def;
+    const mbedtls_cipher_definition_t *def;
 
-    for( def = cipher_definitions; def->info != NULL; def++ )
+    for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
         if( def->info->base->cipher == cipher_id &&
-            def->info->key_length == (unsigned) key_length &&
+            def->info->key_bitlen == (unsigned) key_bitlen &&
             def->info->mode == mode )
             return( def->info );
 
     return( NULL );
 }
 
-void cipher_init( cipher_context_t *ctx )
+void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx )
 {
-    memset( ctx, 0, sizeof( cipher_context_t ) );
+    memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
 }
 
-void cipher_free( cipher_context_t *ctx )
+void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx )
 {
     if( ctx == NULL )
         return;
@@ -135,89 +130,81 @@ void cipher_free( cipher_context_t *ctx )
     if( ctx->cipher_ctx )
         ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
 
-    polarssl_zeroize( ctx, sizeof(cipher_context_t) );
+    mbedtls_zeroize( ctx, sizeof(mbedtls_cipher_context_t) );
 }
 
-int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info )
+int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info )
 {
     if( NULL == cipher_info || NULL == ctx )
-        return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
 
-    memset( ctx, 0, sizeof( cipher_context_t ) );
+    memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
 
     if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
-        return( POLARSSL_ERR_CIPHER_ALLOC_FAILED );
+        return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED );
 
     ctx->cipher_info = cipher_info;
 
-#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
+#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
     /*
      * Ignore possible errors caused by a cipher mode that doesn't use padding
      */
-#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
-    (void) cipher_set_padding_mode( ctx, POLARSSL_PADDING_PKCS7 );
+#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
+    (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_PKCS7 );
 #else
-    (void) cipher_set_padding_mode( ctx, POLARSSL_PADDING_NONE );
+    (void) mbedtls_cipher_set_padding_mode( ctx, MBEDTLS_PADDING_NONE );
 #endif
-#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
-
-    return( 0 );
-}
-
-/* Deprecated, redirects to cipher_free() */
-int cipher_free_ctx( cipher_context_t *ctx )
-{
-    cipher_free( ctx );
+#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
 
     return( 0 );
 }
 
-int cipher_setkey( cipher_context_t *ctx, const unsigned char *key,
-        int key_length, const operation_t operation )
+int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key,
+        int key_bitlen, const mbedtls_operation_t operation )
 {
     if( NULL == ctx || NULL == ctx->cipher_info )
-        return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
 
-    if( ( ctx->cipher_info->flags & POLARSSL_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
-        (int) ctx->cipher_info->key_length != key_length )
+    if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
+        (int) ctx->cipher_info->key_bitlen != key_bitlen )
     {
-        return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
     }
 
-    ctx->key_length = key_length;
+    ctx->key_bitlen = key_bitlen;
     ctx->operation = operation;
 
     /*
      * For CFB and CTR mode always use the encryption key schedule
      */
-    if( POLARSSL_ENCRYPT == operation ||
-        POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
-        POLARSSL_MODE_CTR == ctx->cipher_info->mode )
+    if( MBEDTLS_ENCRYPT == operation ||
+        MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
+        MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
     {
         return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
-                ctx->key_length );
+                ctx->key_bitlen );
     }
 
-    if( POLARSSL_DECRYPT == operation )
+    if( MBEDTLS_DECRYPT == operation )
         return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
-                ctx->key_length );
+                ctx->key_bitlen );
 
-    return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
+    return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
 }
 
-int cipher_set_iv( cipher_context_t *ctx,
+int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
                    const unsigned char *iv, size_t iv_len )
 {
     size_t actual_iv_size;
 
     if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
-        return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
 
     /* avoid buffer overflow in ctx->iv */
-    if( iv_len > POLARSSL_MAX_IV_LENGTH )
-        return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
+    if( iv_len > MBEDTLS_MAX_IV_LENGTH )
+        return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
 
-    if( ( ctx->cipher_info->flags & POLARSSL_CIPHER_VARIABLE_IV_LEN ) != 0 )
+    if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_IV_LEN ) != 0 )
         actual_iv_size = iv_len;
     else
     {
@@ -225,7 +212,7 @@ int cipher_set_iv( cipher_context_t *ctx,
 
         /* avoid reading past the end of input buffer */
         if( actual_iv_size > iv_len )
-            return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
+            return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
     }
 
     memcpy( ctx->iv, iv, actual_iv_size );
@@ -234,49 +221,49 @@ int cipher_set_iv( cipher_context_t *ctx,
     return( 0 );
 }
 
-int cipher_reset( cipher_context_t *ctx )
+int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
 {
     if( NULL == ctx || NULL == ctx->cipher_info )
-        return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
 
     ctx->unprocessed_len = 0;
 
     return( 0 );
 }
 
-#if defined(POLARSSL_GCM_C)
-int cipher_update_ad( cipher_context_t *ctx,
+#if defined(MBEDTLS_GCM_C)
+int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
                       const unsigned char *ad, size_t ad_len )
 {
     if( NULL == ctx || NULL == ctx->cipher_info )
-        return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
 
-    if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
+    if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
     {
-        return gcm_starts( (gcm_context *) ctx->cipher_ctx, ctx->operation,
+        return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
                            ctx->iv, ctx->iv_size, ad, ad_len );
     }
 
     return( 0 );
 }
-#endif /* POLARSSL_GCM_C */
+#endif /* MBEDTLS_GCM_C */
 
-int cipher_update( cipher_context_t *ctx, const unsigned char *input,
+int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
                    size_t ilen, unsigned char *output, size_t *olen )
 {
     int ret;
 
     if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
     {
-        return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
     }
 
     *olen = 0;
 
-    if( ctx->cipher_info->mode == POLARSSL_MODE_ECB )
+    if( ctx->cipher_info->mode == MBEDTLS_MODE_ECB )
     {
-        if( ilen != cipher_get_block_size( ctx ) )
-            return( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED );
+        if( ilen != mbedtls_cipher_get_block_size( ctx ) )
+            return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
 
         *olen = ilen;
 
@@ -289,33 +276,33 @@ int cipher_update( cipher_context_t *ctx, const unsigned char *input,
         return( 0 );
     }
 
-#if defined(POLARSSL_GCM_C)
-    if( ctx->cipher_info->mode == POLARSSL_MODE_GCM )
+#if defined(MBEDTLS_GCM_C)
+    if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
     {
         *olen = ilen;
-        return gcm_update( (gcm_context *) ctx->cipher_ctx, ilen, input,
+        return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
                            output );
     }
 #endif
 
     if( input == output &&
-       ( ctx->unprocessed_len != 0 || ilen % cipher_get_block_size( ctx ) ) )
+       ( ctx->unprocessed_len != 0 || ilen % mbedtls_cipher_get_block_size( ctx ) ) )
     {
-        return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
     }
 
-#if defined(POLARSSL_CIPHER_MODE_CBC)
-    if( ctx->cipher_info->mode == POLARSSL_MODE_CBC )
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
+    if( ctx->cipher_info->mode == MBEDTLS_MODE_CBC )
     {
         size_t copy_len = 0;
 
         /*
          * If there is not enough data for a full block, cache it.
          */
-        if( ( ctx->operation == POLARSSL_DECRYPT &&
-                ilen + ctx->unprocessed_len <= cipher_get_block_size( ctx ) ) ||
-             ( ctx->operation == POLARSSL_ENCRYPT &&
-                ilen + ctx->unprocessed_len < cipher_get_block_size( ctx ) ) )
+        if( ( ctx->operation == MBEDTLS_DECRYPT &&
+                ilen + ctx->unprocessed_len <= mbedtls_cipher_get_block_size( ctx ) ) ||
+             ( ctx->operation == MBEDTLS_ENCRYPT &&
+                ilen + ctx->unprocessed_len < mbedtls_cipher_get_block_size( ctx ) ) )
         {
             memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
                     ilen );
@@ -329,20 +316,20 @@ int cipher_update( cipher_context_t *ctx, const unsigned char *input,
          */
         if( ctx->unprocessed_len != 0 )
         {
-            copy_len = cipher_get_block_size( ctx ) - ctx->unprocessed_len;
+            copy_len = mbedtls_cipher_get_block_size( ctx ) - ctx->unprocessed_len;
 
             memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
                     copy_len );
 
             if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
-                    ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
+                    ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
                     ctx->unprocessed_data, output ) ) )
             {
                 return( ret );
             }
 
-            *olen += cipher_get_block_size( ctx );
-            output += cipher_get_block_size( ctx );
+            *olen += mbedtls_cipher_get_block_size( ctx );
+            output += mbedtls_cipher_get_block_size( ctx );
             ctx->unprocessed_len = 0;
 
             input += copy_len;
@@ -354,9 +341,9 @@ int cipher_update( cipher_context_t *ctx, const unsigned char *input,
          */
         if( 0 != ilen )
         {
-            copy_len = ilen % cipher_get_block_size( ctx );
-            if( copy_len == 0 && ctx->operation == POLARSSL_DECRYPT )
-                copy_len = cipher_get_block_size( ctx );
+            copy_len = ilen % mbedtls_cipher_get_block_size( ctx );
+            if( copy_len == 0 && ctx->operation == MBEDTLS_DECRYPT )
+                copy_len = mbedtls_cipher_get_block_size( ctx );
 
             memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
                     copy_len );
@@ -381,10 +368,10 @@ int cipher_update( cipher_context_t *ctx, const unsigned char *input,
 
         return( 0 );
     }
-#endif /* POLARSSL_CIPHER_MODE_CBC */
+#endif /* MBEDTLS_CIPHER_MODE_CBC */
 
-#if defined(POLARSSL_CIPHER_MODE_CFB)
-    if( ctx->cipher_info->mode == POLARSSL_MODE_CFB )
+#if defined(MBEDTLS_CIPHER_MODE_CFB)
+    if( ctx->cipher_info->mode == MBEDTLS_MODE_CFB )
     {
         if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
                 ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
@@ -397,10 +384,10 @@ int cipher_update( cipher_context_t *ctx, const unsigned char *input,
 
         return( 0 );
     }
-#endif /* POLARSSL_CIPHER_MODE_CFB */
+#endif /* MBEDTLS_CIPHER_MODE_CFB */
 
-#if defined(POLARSSL_CIPHER_MODE_CTR)
-    if( ctx->cipher_info->mode == POLARSSL_MODE_CTR )
+#if defined(MBEDTLS_CIPHER_MODE_CTR)
+    if( ctx->cipher_info->mode == MBEDTLS_MODE_CTR )
     {
         if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
                 ilen, &ctx->unprocessed_len, ctx->iv,
@@ -413,10 +400,10 @@ int cipher_update( cipher_context_t *ctx, const unsigned char *input,
 
         return( 0 );
     }
-#endif /* POLARSSL_CIPHER_MODE_CTR */
+#endif /* MBEDTLS_CIPHER_MODE_CTR */
 
-#if defined(POLARSSL_CIPHER_MODE_STREAM)
-    if( ctx->cipher_info->mode == POLARSSL_MODE_STREAM )
+#if defined(MBEDTLS_CIPHER_MODE_STREAM)
+    if( ctx->cipher_info->mode == MBEDTLS_MODE_STREAM )
     {
         if( 0 != ( ret = ctx->cipher_info->base->stream_func( ctx->cipher_ctx,
                                                     ilen, input, output ) ) )
@@ -428,13 +415,13 @@ int cipher_update( cipher_context_t *ctx, const unsigned char *input,
 
         return( 0 );
     }
-#endif /* POLARSSL_CIPHER_MODE_STREAM */
+#endif /* MBEDTLS_CIPHER_MODE_STREAM */
 
-    return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
+    return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
 }
 
-#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
-#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
+#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
+#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
 /*
  * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
  */
@@ -455,7 +442,7 @@ static int get_pkcs_padding( unsigned char *input, size_t input_len,
     unsigned char padding_len, bad = 0;
 
     if( NULL == input || NULL == data_len )
-        return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
 
     padding_len = input[input_len - 1];
     *data_len = input_len - padding_len;
@@ -470,11 +457,11 @@ static int get_pkcs_padding( unsigned char *input, size_t input_len,
     for( i = 0; i < input_len; i++ )
         bad |= ( input[i] ^ padding_len ) * ( i >= pad_idx );
 
-    return( POLARSSL_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
+    return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
 }
-#endif /* POLARSSL_CIPHER_PADDING_PKCS7 */
+#endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
 
-#if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
+#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
 /*
  * One and zeros padding: fill with 80 00 ... 00
  */
@@ -496,7 +483,7 @@ static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
     unsigned char done = 0, prev_done, bad;
 
     if( NULL == input || NULL == data_len )
-        return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
 
     bad = 0xFF;
     *data_len = 0;
@@ -508,12 +495,12 @@ static int get_one_and_zeros_padding( unsigned char *input, size_t input_len,
         bad &= ( input[i-1] ^ 0x80 ) | ( done == prev_done );
     }
 
-    return( POLARSSL_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
+    return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
 
 }
-#endif /* POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS */
+#endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
 
-#if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
+#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
 /*
  * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
  */
@@ -535,7 +522,7 @@ static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
     unsigned char padding_len, bad = 0;
 
     if( NULL == input || NULL == data_len )
-        return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
 
     padding_len = input[input_len - 1];
     *data_len = input_len - padding_len;
@@ -549,11 +536,11 @@ static int get_zeros_and_len_padding( unsigned char *input, size_t input_len,
     for( i = 0; i < input_len - 1; i++ )
         bad |= input[i] * ( i >= pad_idx );
 
-    return( POLARSSL_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
+    return( MBEDTLS_ERR_CIPHER_INVALID_PADDING * ( bad != 0 ) );
 }
-#endif /* POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN */
+#endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
 
-#if defined(POLARSSL_CIPHER_PADDING_ZEROS)
+#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
 /*
  * Zero padding: fill with 00 ... 00
  */
@@ -573,7 +560,7 @@ static int get_zeros_padding( unsigned char *input, size_t input_len,
     unsigned char done = 0, prev_done;
 
     if( NULL == input || NULL == data_len )
-        return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
 
     *data_len = 0;
     for( i = input_len; i > 0; i-- )
@@ -585,70 +572,70 @@ static int get_zeros_padding( unsigned char *input, size_t input_len,
 
     return( 0 );
 }
-#endif /* POLARSSL_CIPHER_PADDING_ZEROS */
+#endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
 
 /*
  * No padding: don't pad :)
  *
- * There is no add_padding function (check for NULL in cipher_finish)
+ * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
  * but a trivial get_padding function
  */
 static int get_no_padding( unsigned char *input, size_t input_len,
                               size_t *data_len )
 {
     if( NULL == input || NULL == data_len )
-        return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
 
     *data_len = input_len;
 
     return( 0 );
 }
-#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
+#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
 
-int cipher_finish( cipher_context_t *ctx,
+int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
                    unsigned char *output, size_t *olen )
 {
     if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
-        return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
 
     *olen = 0;
 
-    if( POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
-        POLARSSL_MODE_CTR == ctx->cipher_info->mode ||
-        POLARSSL_MODE_GCM == ctx->cipher_info->mode ||
-        POLARSSL_MODE_STREAM == ctx->cipher_info->mode )
+    if( MBEDTLS_MODE_CFB == ctx->cipher_info->mode ||
+        MBEDTLS_MODE_CTR == ctx->cipher_info->mode ||
+        MBEDTLS_MODE_GCM == ctx->cipher_info->mode ||
+        MBEDTLS_MODE_STREAM == ctx->cipher_info->mode )
     {
         return( 0 );
     }
 
-    if( POLARSSL_MODE_ECB == ctx->cipher_info->mode )
+    if( MBEDTLS_MODE_ECB == ctx->cipher_info->mode )
     {
         if( ctx->unprocessed_len != 0 )
-            return( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED );
+            return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
 
         return( 0 );
     }
 
-#if defined(POLARSSL_CIPHER_MODE_CBC)
-    if( POLARSSL_MODE_CBC == ctx->cipher_info->mode )
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
+    if( MBEDTLS_MODE_CBC == ctx->cipher_info->mode )
     {
         int ret = 0;
 
-        if( POLARSSL_ENCRYPT == ctx->operation )
+        if( MBEDTLS_ENCRYPT == ctx->operation )
         {
             /* check for 'no padding' mode */
             if( NULL == ctx->add_padding )
             {
                 if( 0 != ctx->unprocessed_len )
-                    return( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED );
+                    return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
 
                 return( 0 );
             }
 
-            ctx->add_padding( ctx->unprocessed_data, cipher_get_iv_size( ctx ),
+            ctx->add_padding( ctx->unprocessed_data, mbedtls_cipher_get_iv_size( ctx ),
                     ctx->unprocessed_len );
         }
-        else if( cipher_get_block_size( ctx ) != ctx->unprocessed_len )
+        else if( mbedtls_cipher_get_block_size( ctx ) != ctx->unprocessed_len )
         {
             /*
              * For decrypt operations, expect a full block,
@@ -657,118 +644,118 @@ int cipher_finish( cipher_context_t *ctx,
             if( NULL == ctx->add_padding && 0 == ctx->unprocessed_len )
                 return( 0 );
 
-            return( POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED );
+            return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
         }
 
         /* cipher block */
         if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
-                ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
+                ctx->operation, mbedtls_cipher_get_block_size( ctx ), ctx->iv,
                 ctx->unprocessed_data, output ) ) )
         {
             return( ret );
         }
 
         /* Set output size for decryption */
-        if( POLARSSL_DECRYPT == ctx->operation )
-            return ctx->get_padding( output, cipher_get_block_size( ctx ),
+        if( MBEDTLS_DECRYPT == ctx->operation )
+            return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
                                      olen );
 
         /* Set output size for encryption */
-        *olen = cipher_get_block_size( ctx );
+        *olen = mbedtls_cipher_get_block_size( ctx );
         return( 0 );
     }
 #else
     ((void) output);
-#endif /* POLARSSL_CIPHER_MODE_CBC */
+#endif /* MBEDTLS_CIPHER_MODE_CBC */
 
-    return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
+    return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
 }
 
-#if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
-int cipher_set_padding_mode( cipher_context_t *ctx, cipher_padding_t mode )
+#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
+int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode )
 {
     if( NULL == ctx ||
-        POLARSSL_MODE_CBC != ctx->cipher_info->mode )
+        MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
     {
-        return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
     }
 
     switch( mode )
     {
-#if defined(POLARSSL_CIPHER_PADDING_PKCS7)
-    case POLARSSL_PADDING_PKCS7:
+#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
+    case MBEDTLS_PADDING_PKCS7:
         ctx->add_padding = add_pkcs_padding;
         ctx->get_padding = get_pkcs_padding;
         break;
 #endif
-#if defined(POLARSSL_CIPHER_PADDING_ONE_AND_ZEROS)
-    case POLARSSL_PADDING_ONE_AND_ZEROS:
+#if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
+    case MBEDTLS_PADDING_ONE_AND_ZEROS:
         ctx->add_padding = add_one_and_zeros_padding;
         ctx->get_padding = get_one_and_zeros_padding;
         break;
 #endif
-#if defined(POLARSSL_CIPHER_PADDING_ZEROS_AND_LEN)
-    case POLARSSL_PADDING_ZEROS_AND_LEN:
+#if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
+    case MBEDTLS_PADDING_ZEROS_AND_LEN:
         ctx->add_padding = add_zeros_and_len_padding;
         ctx->get_padding = get_zeros_and_len_padding;
         break;
 #endif
-#if defined(POLARSSL_CIPHER_PADDING_ZEROS)
-    case POLARSSL_PADDING_ZEROS:
+#if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
+    case MBEDTLS_PADDING_ZEROS:
         ctx->add_padding = add_zeros_padding;
         ctx->get_padding = get_zeros_padding;
         break;
 #endif
-    case POLARSSL_PADDING_NONE:
+    case MBEDTLS_PADDING_NONE:
         ctx->add_padding = NULL;
         ctx->get_padding = get_no_padding;
         break;
 
     default:
-        return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
+        return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
     }
 
     return( 0 );
 }
-#endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
+#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
 
-#if defined(POLARSSL_GCM_C)
-int cipher_write_tag( cipher_context_t *ctx,
+#if defined(MBEDTLS_GCM_C)
+int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
                       unsigned char *tag, size_t tag_len )
 {
     if( NULL == ctx || NULL == ctx->cipher_info || NULL == tag )
-        return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
 
-    if( POLARSSL_ENCRYPT != ctx->operation )
-        return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
+    if( MBEDTLS_ENCRYPT != ctx->operation )
+        return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
 
-    if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
-        return gcm_finish( (gcm_context *) ctx->cipher_ctx, tag, tag_len );
+    if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
+        return mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx, tag, tag_len );
 
     return( 0 );
 }
 
-int cipher_check_tag( cipher_context_t *ctx,
+int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
                       const unsigned char *tag, size_t tag_len )
 {
     int ret;
 
     if( NULL == ctx || NULL == ctx->cipher_info ||
-        POLARSSL_DECRYPT != ctx->operation )
+        MBEDTLS_DECRYPT != ctx->operation )
     {
-        return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
     }
 
-    if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
+    if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
     {
         unsigned char check_tag[16];
         size_t i;
         int diff;
 
         if( tag_len > sizeof( check_tag ) )
-            return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
+            return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
 
-        if( 0 != ( ret = gcm_finish( (gcm_context *) ctx->cipher_ctx,
+        if( 0 != ( ret = mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
                                      check_tag, tag_len ) ) )
         {
             return( ret );
@@ -779,19 +766,19 @@ int cipher_check_tag( cipher_context_t *ctx,
             diff |= tag[i] ^ check_tag[i];
 
         if( diff != 0 )
-            return( POLARSSL_ERR_CIPHER_AUTH_FAILED );
+            return( MBEDTLS_ERR_CIPHER_AUTH_FAILED );
 
         return( 0 );
     }
 
     return( 0 );
 }
-#endif /* POLARSSL_GCM_C */
+#endif /* MBEDTLS_GCM_C */
 
 /*
  * Packet-oriented wrapper for non-AEAD modes
  */
-int cipher_crypt( cipher_context_t *ctx,
+int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
                   const unsigned char *iv, size_t iv_len,
                   const unsigned char *input, size_t ilen,
                   unsigned char *output, size_t *olen )
@@ -799,16 +786,16 @@ int cipher_crypt( cipher_context_t *ctx,
     int ret;
     size_t finish_olen;
 
-    if( ( ret = cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
+    if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
         return( ret );
 
-    if( ( ret = cipher_reset( ctx ) ) != 0 )
+    if( ( ret = mbedtls_cipher_reset( ctx ) ) != 0 )
         return( ret );
 
-    if( ( ret = cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
+    if( ( ret = mbedtls_cipher_update( ctx, input, ilen, output, olen ) ) != 0 )
         return( ret );
 
-    if( ( ret = cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
+    if( ( ret = mbedtls_cipher_finish( ctx, output + *olen, &finish_olen ) ) != 0 )
         return( ret );
 
     *olen += finish_olen;
@@ -816,99 +803,84 @@ int cipher_crypt( cipher_context_t *ctx,
     return( 0 );
 }
 
-#if defined(POLARSSL_CIPHER_MODE_AEAD)
+#if defined(MBEDTLS_CIPHER_MODE_AEAD)
 /*
  * Packet-oriented encryption for AEAD modes
  */
-int cipher_auth_encrypt( cipher_context_t *ctx,
+int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
                          const unsigned char *iv, size_t iv_len,
                          const unsigned char *ad, size_t ad_len,
                          const unsigned char *input, size_t ilen,
                          unsigned char *output, size_t *olen,
                          unsigned char *tag, size_t tag_len )
 {
-#if defined(POLARSSL_GCM_C)
-    if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
+#if defined(MBEDTLS_GCM_C)
+    if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
     {
         *olen = ilen;
-        return( gcm_crypt_and_tag( ctx->cipher_ctx, GCM_ENCRYPT, ilen,
+        return( mbedtls_gcm_crypt_and_tag( ctx->cipher_ctx, MBEDTLS_GCM_ENCRYPT, ilen,
                                    iv, iv_len, ad, ad_len, input, output,
                                    tag_len, tag ) );
     }
-#endif /* POLARSSL_GCM_C */
-#if defined(POLARSSL_CCM_C)
-    if( POLARSSL_MODE_CCM == ctx->cipher_info->mode )
+#endif /* MBEDTLS_GCM_C */
+#if defined(MBEDTLS_CCM_C)
+    if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
     {
         *olen = ilen;
-        return( ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
+        return( mbedtls_ccm_encrypt_and_tag( ctx->cipher_ctx, ilen,
                                      iv, iv_len, ad, ad_len, input, output,
                                      tag, tag_len ) );
     }
-#endif /* POLARSSL_CCM_C */
+#endif /* MBEDTLS_CCM_C */
 
-    return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
+    return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
 }
 
 /*
  * Packet-oriented decryption for AEAD modes
  */
-int cipher_auth_decrypt( cipher_context_t *ctx,
+int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
                          const unsigned char *iv, size_t iv_len,
                          const unsigned char *ad, size_t ad_len,
                          const unsigned char *input, size_t ilen,
                          unsigned char *output, size_t *olen,
                          const unsigned char *tag, size_t tag_len )
 {
-#if defined(POLARSSL_GCM_C)
-    if( POLARSSL_MODE_GCM == ctx->cipher_info->mode )
+#if defined(MBEDTLS_GCM_C)
+    if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
     {
         int ret;
 
         *olen = ilen;
-        ret = gcm_auth_decrypt( ctx->cipher_ctx, ilen,
+        ret = mbedtls_gcm_auth_decrypt( ctx->cipher_ctx, ilen,
                                 iv, iv_len, ad, ad_len,
                                 tag, tag_len, input, output );
 
-        if( ret == POLARSSL_ERR_GCM_AUTH_FAILED )
-            ret = POLARSSL_ERR_CIPHER_AUTH_FAILED;
+        if( ret == MBEDTLS_ERR_GCM_AUTH_FAILED )
+            ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
 
         return( ret );
     }
-#endif /* POLARSSL_GCM_C */
-#if defined(POLARSSL_CCM_C)
-    if( POLARSSL_MODE_CCM == ctx->cipher_info->mode )
+#endif /* MBEDTLS_GCM_C */
+#if defined(MBEDTLS_CCM_C)
+    if( MBEDTLS_MODE_CCM == ctx->cipher_info->mode )
     {
         int ret;
 
         *olen = ilen;
-        ret = ccm_auth_decrypt( ctx->cipher_ctx, ilen,
+        ret = mbedtls_ccm_auth_decrypt( ctx->cipher_ctx, ilen,
                                 iv, iv_len, ad, ad_len,
                                 input, output, tag, tag_len );
 
-        if( ret == POLARSSL_ERR_CCM_AUTH_FAILED )
-            ret = POLARSSL_ERR_CIPHER_AUTH_FAILED;
+        if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED )
+            ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED;
 
         return( ret );
     }
-#endif /* POLARSSL_CCM_C */
+#endif /* MBEDTLS_CCM_C */
 
-    return( POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE );
+    return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
 }
-#endif /* POLARSSL_CIPHER_MODE_AEAD */
-
-
-#if defined(POLARSSL_SELF_TEST)
-
-/*
- * Checkup routine
- */
-int cipher_self_test( int verbose )
-{
-    ((void) verbose);
-
-    return( 0 );
-}
-
-#endif /* POLARSSL_SELF_TEST */
+#endif /* MBEDTLS_CIPHER_MODE_AEAD */
 
-#endif /* POLARSSL_CIPHER_C */
+#endif /* MBEDTLS_CIPHER_C */