The Great Renaming

A simple execution of tmp/invoke-rename.pl
diff --git a/library/aes.c b/library/aes.c
index 4567534..9780e8f 100644
--- a/library/aes.c
+++ b/library/aes.c
@@ -26,37 +26,37 @@
  *  http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf
  */
 
-#if !defined(POLARSSL_CONFIG_FILE)
+#if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else
-#include POLARSSL_CONFIG_FILE
+#include MBEDTLS_CONFIG_FILE
 #endif
 
-#if defined(POLARSSL_AES_C)
+#if defined(MBEDTLS_AES_C)
 
 #include <string.h>
 
 #include "mbedtls/aes.h"
-#if defined(POLARSSL_PADLOCK_C)
+#if defined(MBEDTLS_PADLOCK_C)
 #include "mbedtls/padlock.h"
 #endif
-#if defined(POLARSSL_AESNI_C)
+#if defined(MBEDTLS_AESNI_C)
 #include "mbedtls/aesni.h"
 #endif
 
-#if defined(POLARSSL_SELF_TEST)
-#if defined(POLARSSL_PLATFORM_C)
+#if defined(MBEDTLS_SELF_TEST)
+#if defined(MBEDTLS_PLATFORM_C)
 #include "mbedtls/platform.h"
 #else
 #include <stdio.h>
-#define polarssl_printf printf
-#endif /* POLARSSL_PLATFORM_C */
-#endif /* POLARSSL_SELF_TEST */
+#define mbedtls_printf printf
+#endif /* MBEDTLS_PLATFORM_C */
+#endif /* MBEDTLS_SELF_TEST */
 
-#if !defined(POLARSSL_AES_ALT)
+#if !defined(MBEDTLS_AES_ALT)
 
 /* 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;
 }
 
@@ -83,12 +83,12 @@
 }
 #endif
 
-#if defined(POLARSSL_PADLOCK_C) &&                      \
-    ( defined(POLARSSL_HAVE_X86) || defined(PADLOCK_ALIGN16) )
+#if defined(MBEDTLS_PADLOCK_C) &&                      \
+    ( defined(MBEDTLS_HAVE_X86) || defined(MBEDTLS_PADLOCK_ALIGN16) )
 static int aes_padlock_ace = -1;
 #endif
 
-#if defined(POLARSSL_AES_ROM_TABLES)
+#if defined(MBEDTLS_AES_ROM_TABLES)
 /*
  * Forward S-box
  */
@@ -353,7 +353,7 @@
     0x0000001B, 0x00000036
 };
 
-#else /* POLARSSL_AES_ROM_TABLES */
+#else /* MBEDTLS_AES_ROM_TABLES */
 
 /*
  * Forward S-box & tables
@@ -463,31 +463,31 @@
     }
 }
 
-#endif /* POLARSSL_AES_ROM_TABLES */
+#endif /* MBEDTLS_AES_ROM_TABLES */
 
-void aes_init( aes_context *ctx )
+void mbedtls_aes_init( mbedtls_aes_context *ctx )
 {
-    memset( ctx, 0, sizeof( aes_context ) );
+    memset( ctx, 0, sizeof( mbedtls_aes_context ) );
 }
 
-void aes_free( aes_context *ctx )
+void mbedtls_aes_free( mbedtls_aes_context *ctx )
 {
     if( ctx == NULL )
         return;
 
-    polarssl_zeroize( ctx, sizeof( aes_context ) );
+    mbedtls_zeroize( ctx, sizeof( mbedtls_aes_context ) );
 }
 
 /*
  * AES key schedule (encryption)
  */
-int aes_setkey_enc( aes_context *ctx, const unsigned char *key,
+int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
                     unsigned int keysize )
 {
     unsigned int i;
     uint32_t *RK;
 
-#if !defined(POLARSSL_AES_ROM_TABLES)
+#if !defined(MBEDTLS_AES_ROM_TABLES)
     if( aes_init_done == 0 )
     {
         aes_gen_tables();
@@ -501,22 +501,22 @@
         case 128: ctx->nr = 10; break;
         case 192: ctx->nr = 12; break;
         case 256: ctx->nr = 14; break;
-        default : return( POLARSSL_ERR_AES_INVALID_KEY_LENGTH );
+        default : return( MBEDTLS_ERR_AES_INVALID_KEY_LENGTH );
     }
 
-#if defined(POLARSSL_PADLOCK_C) && defined(PADLOCK_ALIGN16)
+#if defined(MBEDTLS_PADLOCK_C) && defined(MBEDTLS_PADLOCK_ALIGN16)
     if( aes_padlock_ace == -1 )
-        aes_padlock_ace = padlock_supports( PADLOCK_ACE );
+        aes_padlock_ace = mbedtls_padlock_supports( MBEDTLS_PADLOCK_ACE );
 
     if( aes_padlock_ace )
-        ctx->rk = RK = PADLOCK_ALIGN16( ctx->buf );
+        ctx->rk = RK = MBEDTLS_PADLOCK_ALIGN16( ctx->buf );
     else
 #endif
     ctx->rk = RK = ctx->buf;
 
-#if defined(POLARSSL_AESNI_C) && defined(POLARSSL_HAVE_X86_64)
-    if( aesni_supports( POLARSSL_AESNI_AES ) )
-        return( aesni_setkey_enc( (unsigned char *) ctx->rk, key, keysize ) );
+#if defined(MBEDTLS_AESNI_C) && defined(MBEDTLS_HAVE_X86_64)
+    if( mbedtls_aesni_supports( MBEDTLS_AESNI_AES ) )
+        return( mbedtls_aesni_setkey_enc( (unsigned char *) ctx->rk, key, keysize ) );
 #endif
 
     for( i = 0; i < ( keysize >> 5 ); i++ )
@@ -593,36 +593,36 @@
 /*
  * AES key schedule (decryption)
  */
-int aes_setkey_dec( aes_context *ctx, const unsigned char *key,
+int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
                     unsigned int keysize )
 {
     int i, j, ret;
-    aes_context cty;
+    mbedtls_aes_context cty;
     uint32_t *RK;
     uint32_t *SK;
 
-    aes_init( &cty );
+    mbedtls_aes_init( &cty );
 
-#if defined(POLARSSL_PADLOCK_C) && defined(PADLOCK_ALIGN16)
+#if defined(MBEDTLS_PADLOCK_C) && defined(MBEDTLS_PADLOCK_ALIGN16)
     if( aes_padlock_ace == -1 )
-        aes_padlock_ace = padlock_supports( PADLOCK_ACE );
+        aes_padlock_ace = mbedtls_padlock_supports( MBEDTLS_PADLOCK_ACE );
 
     if( aes_padlock_ace )
-        ctx->rk = RK = PADLOCK_ALIGN16( ctx->buf );
+        ctx->rk = RK = MBEDTLS_PADLOCK_ALIGN16( ctx->buf );
     else
 #endif
     ctx->rk = RK = ctx->buf;
 
     /* Also checks keysize */
-    if( ( ret = aes_setkey_enc( &cty, key, keysize ) ) != 0 )
+    if( ( ret = mbedtls_aes_setkey_enc( &cty, key, keysize ) ) != 0 )
         goto exit;
 
     ctx->nr = cty.nr;
 
-#if defined(POLARSSL_AESNI_C) && defined(POLARSSL_HAVE_X86_64)
-    if( aesni_supports( POLARSSL_AESNI_AES ) )
+#if defined(MBEDTLS_AESNI_C) && defined(MBEDTLS_HAVE_X86_64)
+    if( mbedtls_aesni_supports( MBEDTLS_AESNI_AES ) )
     {
-        aesni_inverse_key( (unsigned char *) ctx->rk,
+        mbedtls_aesni_inverse_key( (unsigned char *) ctx->rk,
                            (const unsigned char *) cty.rk, ctx->nr );
         goto exit;
     }
@@ -652,7 +652,7 @@
     *RK++ = *SK++;
 
 exit:
-    aes_free( &cty );
+    mbedtls_aes_free( &cty );
 
     return( ret );
 }
@@ -706,7 +706,7 @@
 /*
  * AES-ECB block encryption/decryption
  */
-int aes_crypt_ecb( aes_context *ctx,
+int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
                     int mode,
                     const unsigned char input[16],
                     unsigned char output[16] )
@@ -714,15 +714,15 @@
     int i;
     uint32_t *RK, X0, X1, X2, X3, Y0, Y1, Y2, Y3;
 
-#if defined(POLARSSL_AESNI_C) && defined(POLARSSL_HAVE_X86_64)
-    if( aesni_supports( POLARSSL_AESNI_AES ) )
-        return( aesni_crypt_ecb( ctx, mode, input, output ) );
+#if defined(MBEDTLS_AESNI_C) && defined(MBEDTLS_HAVE_X86_64)
+    if( mbedtls_aesni_supports( MBEDTLS_AESNI_AES ) )
+        return( mbedtls_aesni_crypt_ecb( ctx, mode, input, output ) );
 #endif
 
-#if defined(POLARSSL_PADLOCK_C) && defined(POLARSSL_HAVE_X86)
+#if defined(MBEDTLS_PADLOCK_C) && defined(MBEDTLS_HAVE_X86)
     if( aes_padlock_ace )
     {
-        if( padlock_xcryptecb( ctx, mode, input, output ) == 0 )
+        if( mbedtls_padlock_xcryptecb( ctx, mode, input, output ) == 0 )
             return( 0 );
 
         // If padlock data misaligned, we just fall back to
@@ -738,7 +738,7 @@
     GET_UINT32_LE( X2, input,  8 ); X2 ^= *RK++;
     GET_UINT32_LE( X3, input, 12 ); X3 ^= *RK++;
 
-    if( mode == AES_DECRYPT )
+    if( mode == MBEDTLS_AES_DECRYPT )
     {
         for( i = ( ctx->nr >> 1 ) - 1; i > 0; i-- )
         {
@@ -772,7 +772,7 @@
                 ( (uint32_t) RSb[ ( Y1 >> 16 ) & 0xFF ] << 16 ) ^
                 ( (uint32_t) RSb[ ( Y0 >> 24 ) & 0xFF ] << 24 );
     }
-    else /* AES_ENCRYPT */
+    else /* MBEDTLS_AES_ENCRYPT */
     {
         for( i = ( ctx->nr >> 1 ) - 1; i > 0; i-- )
         {
@@ -815,11 +815,11 @@
     return( 0 );
 }
 
-#if defined(POLARSSL_CIPHER_MODE_CBC)
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
 /*
  * AES-CBC buffer encryption/decryption
  */
-int aes_crypt_cbc( aes_context *ctx,
+int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
                     int mode,
                     size_t length,
                     unsigned char iv[16],
@@ -830,12 +830,12 @@
     unsigned char temp[16];
 
     if( length % 16 )
-        return( POLARSSL_ERR_AES_INVALID_INPUT_LENGTH );
+        return( MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH );
 
-#if defined(POLARSSL_PADLOCK_C) && defined(POLARSSL_HAVE_X86)
+#if defined(MBEDTLS_PADLOCK_C) && defined(MBEDTLS_HAVE_X86)
     if( aes_padlock_ace )
     {
-        if( padlock_xcryptcbc( ctx, mode, length, iv, input, output ) == 0 )
+        if( mbedtls_padlock_xcryptcbc( ctx, mode, length, iv, input, output ) == 0 )
             return( 0 );
 
         // If padlock data misaligned, we just fall back to
@@ -844,12 +844,12 @@
     }
 #endif
 
-    if( mode == AES_DECRYPT )
+    if( mode == MBEDTLS_AES_DECRYPT )
     {
         while( length > 0 )
         {
             memcpy( temp, input, 16 );
-            aes_crypt_ecb( ctx, mode, input, output );
+            mbedtls_aes_crypt_ecb( ctx, mode, input, output );
 
             for( i = 0; i < 16; i++ )
                 output[i] = (unsigned char)( output[i] ^ iv[i] );
@@ -868,7 +868,7 @@
             for( i = 0; i < 16; i++ )
                 output[i] = (unsigned char)( input[i] ^ iv[i] );
 
-            aes_crypt_ecb( ctx, mode, output, output );
+            mbedtls_aes_crypt_ecb( ctx, mode, output, output );
             memcpy( iv, output, 16 );
 
             input  += 16;
@@ -879,13 +879,13 @@
 
     return( 0 );
 }
-#endif /* POLARSSL_CIPHER_MODE_CBC */
+#endif /* MBEDTLS_CIPHER_MODE_CBC */
 
-#if defined(POLARSSL_CIPHER_MODE_CFB)
+#if defined(MBEDTLS_CIPHER_MODE_CFB)
 /*
  * AES-CFB128 buffer encryption/decryption
  */
-int aes_crypt_cfb128( aes_context *ctx,
+int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
                        int mode,
                        size_t length,
                        size_t *iv_off,
@@ -896,12 +896,12 @@
     int c;
     size_t n = *iv_off;
 
-    if( mode == AES_DECRYPT )
+    if( mode == MBEDTLS_AES_DECRYPT )
     {
         while( length-- )
         {
             if( n == 0 )
-                aes_crypt_ecb( ctx, AES_ENCRYPT, iv, iv );
+                mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv );
 
             c = *input++;
             *output++ = (unsigned char)( c ^ iv[n] );
@@ -915,7 +915,7 @@
         while( length-- )
         {
             if( n == 0 )
-                aes_crypt_ecb( ctx, AES_ENCRYPT, iv, iv );
+                mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv );
 
             iv[n] = *output++ = (unsigned char)( iv[n] ^ *input++ );
 
@@ -931,7 +931,7 @@
 /*
  * AES-CFB8 buffer encryption/decryption
  */
-int aes_crypt_cfb8( aes_context *ctx,
+int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
                        int mode,
                        size_t length,
                        unsigned char iv[16],
@@ -944,14 +944,14 @@
     while( length-- )
     {
         memcpy( ov, iv, 16 );
-        aes_crypt_ecb( ctx, AES_ENCRYPT, iv, iv );
+        mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, iv, iv );
 
-        if( mode == AES_DECRYPT )
+        if( mode == MBEDTLS_AES_DECRYPT )
             ov[16] = *input;
 
         c = *output++ = (unsigned char)( iv[0] ^ *input++ );
 
-        if( mode == AES_ENCRYPT )
+        if( mode == MBEDTLS_AES_ENCRYPT )
             ov[16] = c;
 
         memcpy( iv, ov + 1, 16 );
@@ -959,13 +959,13 @@
 
     return( 0 );
 }
-#endif /*POLARSSL_CIPHER_MODE_CFB */
+#endif /*MBEDTLS_CIPHER_MODE_CFB */
 
-#if defined(POLARSSL_CIPHER_MODE_CTR)
+#if defined(MBEDTLS_CIPHER_MODE_CTR)
 /*
  * AES-CTR buffer encryption/decryption
  */
-int aes_crypt_ctr( aes_context *ctx,
+int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
                        size_t length,
                        size_t *nc_off,
                        unsigned char nonce_counter[16],
@@ -979,7 +979,7 @@
     while( length-- )
     {
         if( n == 0 ) {
-            aes_crypt_ecb( ctx, AES_ENCRYPT, nonce_counter, stream_block );
+            mbedtls_aes_crypt_ecb( ctx, MBEDTLS_AES_ENCRYPT, nonce_counter, stream_block );
 
             for( i = 16; i > 0; i-- )
                 if( ++nonce_counter[i - 1] != 0 )
@@ -995,11 +995,11 @@
 
     return( 0 );
 }
-#endif /* POLARSSL_CIPHER_MODE_CTR */
+#endif /* MBEDTLS_CIPHER_MODE_CTR */
 
-#endif /* !POLARSSL_AES_ALT */
+#endif /* !MBEDTLS_AES_ALT */
 
-#if defined(POLARSSL_SELF_TEST)
+#if defined(MBEDTLS_SELF_TEST)
 /*
  * AES test vectors from:
  *
@@ -1025,7 +1025,7 @@
       0xFF, 0x30, 0xB4, 0xEA, 0x21, 0x63, 0x6D, 0xA4 }
 };
 
-#if defined(POLARSSL_CIPHER_MODE_CBC)
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
 static const unsigned char aes_test_cbc_dec[3][16] =
 {
     { 0xFA, 0xCA, 0x37, 0xE0, 0xB0, 0xC8, 0x53, 0x73,
@@ -1045,9 +1045,9 @@
     { 0xFE, 0x3C, 0x53, 0x65, 0x3E, 0x2F, 0x45, 0xB5,
       0x6F, 0xCD, 0x88, 0xB2, 0xCC, 0x89, 0x8F, 0xF0 }
 };
-#endif /* POLARSSL_CIPHER_MODE_CBC */
+#endif /* MBEDTLS_CIPHER_MODE_CBC */
 
-#if defined(POLARSSL_CIPHER_MODE_CFB)
+#if defined(MBEDTLS_CIPHER_MODE_CFB)
 /*
  * AES-CFB128 test vectors from:
  *
@@ -1111,9 +1111,9 @@
       0x75, 0xA3, 0x85, 0x74, 0x1A, 0xB9, 0xCE, 0xF8,
       0x20, 0x31, 0x62, 0x3D, 0x55, 0xB1, 0xE4, 0x71 }
 };
-#endif /* POLARSSL_CIPHER_MODE_CFB */
+#endif /* MBEDTLS_CIPHER_MODE_CFB */
 
-#if defined(POLARSSL_CIPHER_MODE_CTR)
+#if defined(MBEDTLS_CIPHER_MODE_CTR)
 /*
  * AES-CTR test vectors from:
  *
@@ -1174,32 +1174,32 @@
 
 static const int aes_test_ctr_len[3] =
     { 16, 32, 36 };
-#endif /* POLARSSL_CIPHER_MODE_CTR */
+#endif /* MBEDTLS_CIPHER_MODE_CTR */
 
 /*
  * Checkup routine
  */
-int aes_self_test( int verbose )
+int mbedtls_aes_self_test( int verbose )
 {
     int ret = 0, i, j, u, v;
     unsigned char key[32];
     unsigned char buf[64];
     unsigned char iv[16];
-#if defined(POLARSSL_CIPHER_MODE_CBC)
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
     unsigned char prv[16];
 #endif
-#if defined(POLARSSL_CIPHER_MODE_CTR) || defined(POLARSSL_CIPHER_MODE_CFB)
+#if defined(MBEDTLS_CIPHER_MODE_CTR) || defined(MBEDTLS_CIPHER_MODE_CFB)
     size_t offset;
 #endif
-#if defined(POLARSSL_CIPHER_MODE_CTR)
+#if defined(MBEDTLS_CIPHER_MODE_CTR)
     int len;
     unsigned char nonce_counter[16];
     unsigned char stream_block[16];
 #endif
-    aes_context ctx;
+    mbedtls_aes_context ctx;
 
     memset( key, 0, 32 );
-    aes_init( &ctx );
+    mbedtls_aes_init( &ctx );
 
     /*
      * ECB mode
@@ -1210,22 +1210,22 @@
         v = i  & 1;
 
         if( verbose != 0 )
-            polarssl_printf( "  AES-ECB-%3d (%s): ", 128 + u * 64,
-                             ( v == AES_DECRYPT ) ? "dec" : "enc" );
+            mbedtls_printf( "  AES-ECB-%3d (%s): ", 128 + u * 64,
+                             ( v == MBEDTLS_AES_DECRYPT ) ? "dec" : "enc" );
 
         memset( buf, 0, 16 );
 
-        if( v == AES_DECRYPT )
+        if( v == MBEDTLS_AES_DECRYPT )
         {
-            aes_setkey_dec( &ctx, key, 128 + u * 64 );
+            mbedtls_aes_setkey_dec( &ctx, key, 128 + u * 64 );
 
             for( j = 0; j < 10000; j++ )
-                aes_crypt_ecb( &ctx, v, buf, buf );
+                mbedtls_aes_crypt_ecb( &ctx, v, buf, buf );
 
             if( memcmp( buf, aes_test_ecb_dec[u], 16 ) != 0 )
             {
                 if( verbose != 0 )
-                    polarssl_printf( "failed\n" );
+                    mbedtls_printf( "failed\n" );
 
                 ret = 1;
                 goto exit;
@@ -1233,15 +1233,15 @@
         }
         else
         {
-            aes_setkey_enc( &ctx, key, 128 + u * 64 );
+            mbedtls_aes_setkey_enc( &ctx, key, 128 + u * 64 );
 
             for( j = 0; j < 10000; j++ )
-                aes_crypt_ecb( &ctx, v, buf, buf );
+                mbedtls_aes_crypt_ecb( &ctx, v, buf, buf );
 
             if( memcmp( buf, aes_test_ecb_enc[u], 16 ) != 0 )
             {
                 if( verbose != 0 )
-                    polarssl_printf( "failed\n" );
+                    mbedtls_printf( "failed\n" );
 
                 ret = 1;
                 goto exit;
@@ -1249,13 +1249,13 @@
         }
 
         if( verbose != 0 )
-            polarssl_printf( "passed\n" );
+            mbedtls_printf( "passed\n" );
     }
 
     if( verbose != 0 )
-        polarssl_printf( "\n" );
+        mbedtls_printf( "\n" );
 
-#if defined(POLARSSL_CIPHER_MODE_CBC)
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
     /*
      * CBC mode
      */
@@ -1265,24 +1265,24 @@
         v = i  & 1;
 
         if( verbose != 0 )
-            polarssl_printf( "  AES-CBC-%3d (%s): ", 128 + u * 64,
-                             ( v == AES_DECRYPT ) ? "dec" : "enc" );
+            mbedtls_printf( "  AES-CBC-%3d (%s): ", 128 + u * 64,
+                             ( v == MBEDTLS_AES_DECRYPT ) ? "dec" : "enc" );
 
         memset( iv , 0, 16 );
         memset( prv, 0, 16 );
         memset( buf, 0, 16 );
 
-        if( v == AES_DECRYPT )
+        if( v == MBEDTLS_AES_DECRYPT )
         {
-            aes_setkey_dec( &ctx, key, 128 + u * 64 );
+            mbedtls_aes_setkey_dec( &ctx, key, 128 + u * 64 );
 
             for( j = 0; j < 10000; j++ )
-                aes_crypt_cbc( &ctx, v, 16, iv, buf, buf );
+                mbedtls_aes_crypt_cbc( &ctx, v, 16, iv, buf, buf );
 
             if( memcmp( buf, aes_test_cbc_dec[u], 16 ) != 0 )
             {
                 if( verbose != 0 )
-                    polarssl_printf( "failed\n" );
+                    mbedtls_printf( "failed\n" );
 
                 ret = 1;
                 goto exit;
@@ -1290,13 +1290,13 @@
         }
         else
         {
-            aes_setkey_enc( &ctx, key, 128 + u * 64 );
+            mbedtls_aes_setkey_enc( &ctx, key, 128 + u * 64 );
 
             for( j = 0; j < 10000; j++ )
             {
                 unsigned char tmp[16];
 
-                aes_crypt_cbc( &ctx, v, 16, iv, buf, buf );
+                mbedtls_aes_crypt_cbc( &ctx, v, 16, iv, buf, buf );
 
                 memcpy( tmp, prv, 16 );
                 memcpy( prv, buf, 16 );
@@ -1306,7 +1306,7 @@
             if( memcmp( prv, aes_test_cbc_enc[u], 16 ) != 0 )
             {
                 if( verbose != 0 )
-                    polarssl_printf( "failed\n" );
+                    mbedtls_printf( "failed\n" );
 
                 ret = 1;
                 goto exit;
@@ -1314,14 +1314,14 @@
         }
 
         if( verbose != 0 )
-            polarssl_printf( "passed\n" );
+            mbedtls_printf( "passed\n" );
     }
 
     if( verbose != 0 )
-        polarssl_printf( "\n" );
-#endif /* POLARSSL_CIPHER_MODE_CBC */
+        mbedtls_printf( "\n" );
+#endif /* MBEDTLS_CIPHER_MODE_CBC */
 
-#if defined(POLARSSL_CIPHER_MODE_CFB)
+#if defined(MBEDTLS_CIPHER_MODE_CFB)
     /*
      * CFB128 mode
      */
@@ -1331,24 +1331,24 @@
         v = i  & 1;
 
         if( verbose != 0 )
-            polarssl_printf( "  AES-CFB128-%3d (%s): ", 128 + u * 64,
-                             ( v == AES_DECRYPT ) ? "dec" : "enc" );
+            mbedtls_printf( "  AES-CFB128-%3d (%s): ", 128 + u * 64,
+                             ( v == MBEDTLS_AES_DECRYPT ) ? "dec" : "enc" );
 
         memcpy( iv,  aes_test_cfb128_iv, 16 );
         memcpy( key, aes_test_cfb128_key[u], 16 + u * 8 );
 
         offset = 0;
-        aes_setkey_enc( &ctx, key, 128 + u * 64 );
+        mbedtls_aes_setkey_enc( &ctx, key, 128 + u * 64 );
 
-        if( v == AES_DECRYPT )
+        if( v == MBEDTLS_AES_DECRYPT )
         {
             memcpy( buf, aes_test_cfb128_ct[u], 64 );
-            aes_crypt_cfb128( &ctx, v, 64, &offset, iv, buf, buf );
+            mbedtls_aes_crypt_cfb128( &ctx, v, 64, &offset, iv, buf, buf );
 
             if( memcmp( buf, aes_test_cfb128_pt, 64 ) != 0 )
             {
                 if( verbose != 0 )
-                    polarssl_printf( "failed\n" );
+                    mbedtls_printf( "failed\n" );
 
                 ret = 1;
                 goto exit;
@@ -1357,12 +1357,12 @@
         else
         {
             memcpy( buf, aes_test_cfb128_pt, 64 );
-            aes_crypt_cfb128( &ctx, v, 64, &offset, iv, buf, buf );
+            mbedtls_aes_crypt_cfb128( &ctx, v, 64, &offset, iv, buf, buf );
 
             if( memcmp( buf, aes_test_cfb128_ct[u], 64 ) != 0 )
             {
                 if( verbose != 0 )
-                    polarssl_printf( "failed\n" );
+                    mbedtls_printf( "failed\n" );
 
                 ret = 1;
                 goto exit;
@@ -1370,14 +1370,14 @@
         }
 
         if( verbose != 0 )
-            polarssl_printf( "passed\n" );
+            mbedtls_printf( "passed\n" );
     }
 
     if( verbose != 0 )
-        polarssl_printf( "\n" );
-#endif /* POLARSSL_CIPHER_MODE_CFB */
+        mbedtls_printf( "\n" );
+#endif /* MBEDTLS_CIPHER_MODE_CFB */
 
-#if defined(POLARSSL_CIPHER_MODE_CTR)
+#if defined(MBEDTLS_CIPHER_MODE_CTR)
     /*
      * CTR mode
      */
@@ -1387,27 +1387,27 @@
         v = i  & 1;
 
         if( verbose != 0 )
-            polarssl_printf( "  AES-CTR-128 (%s): ",
-                             ( v == AES_DECRYPT ) ? "dec" : "enc" );
+            mbedtls_printf( "  AES-CTR-128 (%s): ",
+                             ( v == MBEDTLS_AES_DECRYPT ) ? "dec" : "enc" );
 
         memcpy( nonce_counter, aes_test_ctr_nonce_counter[u], 16 );
         memcpy( key, aes_test_ctr_key[u], 16 );
 
         offset = 0;
-        aes_setkey_enc( &ctx, key, 128 );
+        mbedtls_aes_setkey_enc( &ctx, key, 128 );
 
-        if( v == AES_DECRYPT )
+        if( v == MBEDTLS_AES_DECRYPT )
         {
             len = aes_test_ctr_len[u];
             memcpy( buf, aes_test_ctr_ct[u], len );
 
-            aes_crypt_ctr( &ctx, len, &offset, nonce_counter, stream_block,
+            mbedtls_aes_crypt_ctr( &ctx, len, &offset, nonce_counter, stream_block,
                            buf, buf );
 
             if( memcmp( buf, aes_test_ctr_pt[u], len ) != 0 )
             {
                 if( verbose != 0 )
-                    polarssl_printf( "failed\n" );
+                    mbedtls_printf( "failed\n" );
 
                 ret = 1;
                 goto exit;
@@ -1418,13 +1418,13 @@
             len = aes_test_ctr_len[u];
             memcpy( buf, aes_test_ctr_pt[u], len );
 
-            aes_crypt_ctr( &ctx, len, &offset, nonce_counter, stream_block,
+            mbedtls_aes_crypt_ctr( &ctx, len, &offset, nonce_counter, stream_block,
                            buf, buf );
 
             if( memcmp( buf, aes_test_ctr_ct[u], len ) != 0 )
             {
                 if( verbose != 0 )
-                    polarssl_printf( "failed\n" );
+                    mbedtls_printf( "failed\n" );
 
                 ret = 1;
                 goto exit;
@@ -1432,21 +1432,21 @@
         }
 
         if( verbose != 0 )
-            polarssl_printf( "passed\n" );
+            mbedtls_printf( "passed\n" );
     }
 
     if( verbose != 0 )
-        polarssl_printf( "\n" );
-#endif /* POLARSSL_CIPHER_MODE_CTR */
+        mbedtls_printf( "\n" );
+#endif /* MBEDTLS_CIPHER_MODE_CTR */
 
     ret = 0;
 
 exit:
-    aes_free( &ctx );
+    mbedtls_aes_free( &ctx );
 
     return( ret );
 }
 
-#endif /* POLARSSL_SELF_TEST */
+#endif /* MBEDTLS_SELF_TEST */
 
-#endif /* POLARSSL_AES_C */
+#endif /* MBEDTLS_AES_C */
diff --git a/library/aesni.c b/library/aesni.c
index a6897b2..ff6c71d 100644
--- a/library/aesni.c
+++ b/library/aesni.c
@@ -25,24 +25,24 @@
  * [CLMUL-WP] http://software.intel.com/en-us/articles/intel-carry-less-multiplication-instruction-and-its-usage-for-computing-the-gcm-mode/
  */
 
-#if !defined(POLARSSL_CONFIG_FILE)
+#if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else
-#include POLARSSL_CONFIG_FILE
+#include MBEDTLS_CONFIG_FILE
 #endif
 
-#if defined(POLARSSL_AESNI_C)
+#if defined(MBEDTLS_AESNI_C)
 
 #include "mbedtls/aesni.h"
 
 #include <string.h>
 
-#if defined(POLARSSL_HAVE_X86_64)
+#if defined(MBEDTLS_HAVE_X86_64)
 
 /*
  * AES-NI support detection routine
  */
-int aesni_supports( unsigned int what )
+int mbedtls_aesni_supports( unsigned int what )
 {
     static int done = 0;
     static unsigned int c = 0;
@@ -89,7 +89,7 @@
 /*
  * AES-NI AES-ECB block en(de)cryption
  */
-int aesni_crypt_ecb( aes_context *ctx,
+int mbedtls_aesni_crypt_ecb( mbedtls_aes_context *ctx,
                      int mode,
                      const unsigned char input[16],
                      unsigned char output[16] )
@@ -135,7 +135,7 @@
  * GCM multiplication: c = a times b in GF(2^128)
  * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.
  */
-void aesni_gcm_mult( unsigned char c[16],
+void mbedtls_aesni_gcm_mult( unsigned char c[16],
                      const unsigned char a[16],
                      const unsigned char b[16] )
 {
@@ -246,7 +246,7 @@
 /*
  * Compute decryption round keys from encryption round keys
  */
-void aesni_inverse_key( unsigned char *invkey,
+void mbedtls_aesni_inverse_key( unsigned char *invkey,
                         const unsigned char *fwdkey, int nr )
 {
     unsigned char *ik = invkey;
@@ -423,7 +423,7 @@
 
          /*
           * Main "loop" - Generating one more key than necessary,
-          * see definition of aes_context.buf
+          * see definition of mbedtls_aes_context.buf
           */
          "2:                                \n\t"
          AESKEYGENA xmm1_xmm2 ",0x01        \n\tcall 1b \n\t"
@@ -441,7 +441,7 @@
 /*
  * Key expansion, wrapper
  */
-int aesni_setkey_enc( unsigned char *rk,
+int mbedtls_aesni_setkey_enc( unsigned char *rk,
                       const unsigned char *key,
                       size_t bits )
 {
@@ -450,12 +450,12 @@
         case 128: aesni_setkey_enc_128( rk, key ); break;
         case 192: aesni_setkey_enc_192( rk, key ); break;
         case 256: aesni_setkey_enc_256( rk, key ); break;
-        default : return( POLARSSL_ERR_AES_INVALID_KEY_LENGTH );
+        default : return( MBEDTLS_ERR_AES_INVALID_KEY_LENGTH );
     }
 
     return( 0 );
 }
 
-#endif /* POLARSSL_HAVE_X86_64 */
+#endif /* MBEDTLS_HAVE_X86_64 */
 
-#endif /* POLARSSL_AESNI_C */
+#endif /* MBEDTLS_AESNI_C */
diff --git a/library/arc4.c b/library/arc4.c
index e790579..f11b5d6 100644
--- a/library/arc4.c
+++ b/library/arc4.c
@@ -25,51 +25,51 @@
  *  http://groups.google.com/group/sci.crypt/msg/10a300c9d21afca0
  */
 
-#if !defined(POLARSSL_CONFIG_FILE)
+#if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else
-#include POLARSSL_CONFIG_FILE
+#include MBEDTLS_CONFIG_FILE
 #endif
 
-#if defined(POLARSSL_ARC4_C)
+#if defined(MBEDTLS_ARC4_C)
 
 #include "mbedtls/arc4.h"
 
 #include <string.h>
 
-#if defined(POLARSSL_SELF_TEST)
-#if defined(POLARSSL_PLATFORM_C)
+#if defined(MBEDTLS_SELF_TEST)
+#if defined(MBEDTLS_PLATFORM_C)
 #include "mbedtls/platform.h"
 #else
 #include <stdio.h>
-#define polarssl_printf printf
-#endif /* POLARSSL_PLATFORM_C */
-#endif /* POLARSSL_SELF_TEST */
+#define mbedtls_printf printf
+#endif /* MBEDTLS_PLATFORM_C */
+#endif /* MBEDTLS_SELF_TEST */
 
-#if !defined(POLARSSL_ARC4_ALT)
+#if !defined(MBEDTLS_ARC4_ALT)
 
 /* 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;
 }
 
-void arc4_init( arc4_context *ctx )
+void mbedtls_arc4_init( mbedtls_arc4_context *ctx )
 {
-    memset( ctx, 0, sizeof( arc4_context ) );
+    memset( ctx, 0, sizeof( mbedtls_arc4_context ) );
 }
 
-void arc4_free( arc4_context *ctx )
+void mbedtls_arc4_free( mbedtls_arc4_context *ctx )
 {
     if( ctx == NULL )
         return;
 
-    polarssl_zeroize( ctx, sizeof( arc4_context ) );
+    mbedtls_zeroize( ctx, sizeof( mbedtls_arc4_context ) );
 }
 
 /*
  * ARC4 key schedule
  */
-void arc4_setup( arc4_context *ctx, const unsigned char *key,
+void mbedtls_arc4_setup( mbedtls_arc4_context *ctx, const unsigned char *key,
                  unsigned int keylen )
 {
     int i, j, a;
@@ -99,7 +99,7 @@
 /*
  * ARC4 cipher function
  */
-int arc4_crypt( arc4_context *ctx, size_t length, const unsigned char *input,
+int mbedtls_arc4_crypt( mbedtls_arc4_context *ctx, size_t length, const unsigned char *input,
                 unsigned char *output )
 {
     int x, y, a, b;
@@ -128,9 +128,9 @@
     return( 0 );
 }
 
-#endif /* !POLARSSL_ARC4_ALT */
+#endif /* !MBEDTLS_ARC4_ALT */
 
-#if defined(POLARSSL_SELF_TEST)
+#if defined(MBEDTLS_SELF_TEST)
 /*
  * ARC4 tests vectors as posted by Eric Rescorla in sep. 1994:
  *
@@ -160,47 +160,47 @@
 /*
  * Checkup routine
  */
-int arc4_self_test( int verbose )
+int mbedtls_arc4_self_test( int verbose )
 {
     int i, ret = 0;
     unsigned char ibuf[8];
     unsigned char obuf[8];
-    arc4_context ctx;
+    mbedtls_arc4_context ctx;
 
-    arc4_init( &ctx );
+    mbedtls_arc4_init( &ctx );
 
     for( i = 0; i < 3; i++ )
     {
         if( verbose != 0 )
-            polarssl_printf( "  ARC4 test #%d: ", i + 1 );
+            mbedtls_printf( "  ARC4 test #%d: ", i + 1 );
 
         memcpy( ibuf, arc4_test_pt[i], 8 );
 
-        arc4_setup( &ctx, arc4_test_key[i], 8 );
-        arc4_crypt( &ctx, 8, ibuf, obuf );
+        mbedtls_arc4_setup( &ctx, arc4_test_key[i], 8 );
+        mbedtls_arc4_crypt( &ctx, 8, ibuf, obuf );
 
         if( memcmp( obuf, arc4_test_ct[i], 8 ) != 0 )
         {
             if( verbose != 0 )
-                polarssl_printf( "failed\n" );
+                mbedtls_printf( "failed\n" );
 
             ret = 1;
             goto exit;
         }
 
         if( verbose != 0 )
-            polarssl_printf( "passed\n" );
+            mbedtls_printf( "passed\n" );
     }
 
     if( verbose != 0 )
-        polarssl_printf( "\n" );
+        mbedtls_printf( "\n" );
 
 exit:
-    arc4_free( &ctx );
+    mbedtls_arc4_free( &ctx );
 
     return( ret );
 }
 
-#endif /* POLARSSL_SELF_TEST */
+#endif /* MBEDTLS_SELF_TEST */
 
-#endif /* POLARSSL_ARC4_C */
+#endif /* MBEDTLS_ARC4_C */
diff --git a/library/asn1parse.c b/library/asn1parse.c
index 202a6ce..a399a7f 100644
--- a/library/asn1parse.c
+++ b/library/asn1parse.c
@@ -20,44 +20,44 @@
  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#if !defined(POLARSSL_CONFIG_FILE)
+#if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else
-#include POLARSSL_CONFIG_FILE
+#include MBEDTLS_CONFIG_FILE
 #endif
 
-#if defined(POLARSSL_ASN1_PARSE_C)
+#if defined(MBEDTLS_ASN1_PARSE_C)
 
 #include "mbedtls/asn1.h"
 
 #include <string.h>
 
-#if defined(POLARSSL_BIGNUM_C)
+#if defined(MBEDTLS_BIGNUM_C)
 #include "mbedtls/bignum.h"
 #endif
 
-#if defined(POLARSSL_PLATFORM_C)
+#if defined(MBEDTLS_PLATFORM_C)
 #include "mbedtls/platform.h"
 #else
 #include <stdlib.h>
-#define polarssl_malloc     malloc
-#define polarssl_free       free
+#define mbedtls_malloc     malloc
+#define mbedtls_free       free
 #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;
 }
 
 /*
  * ASN.1 DER decoding routines
  */
-int asn1_get_len( unsigned char **p,
+int mbedtls_asn1_get_len( unsigned char **p,
                   const unsigned char *end,
                   size_t *len )
 {
     if( ( end - *p ) < 1 )
-        return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
+        return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
 
     if( ( **p & 0x80 ) == 0 )
         *len = *(*p)++;
@@ -67,7 +67,7 @@
         {
         case 1:
             if( ( end - *p ) < 2 )
-                return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
+                return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
 
             *len = (*p)[1];
             (*p) += 2;
@@ -75,7 +75,7 @@
 
         case 2:
             if( ( end - *p ) < 3 )
-                return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
+                return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
 
             *len = ( (size_t)(*p)[1] << 8 ) | (*p)[2];
             (*p) += 3;
@@ -83,7 +83,7 @@
 
         case 3:
             if( ( end - *p ) < 4 )
-                return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
+                return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
 
             *len = ( (size_t)(*p)[1] << 16 ) |
                    ( (size_t)(*p)[2] << 8  ) | (*p)[3];
@@ -92,7 +92,7 @@
 
         case 4:
             if( ( end - *p ) < 5 )
-                return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
+                return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
 
             *len = ( (size_t)(*p)[1] << 24 ) | ( (size_t)(*p)[2] << 16 ) |
                    ( (size_t)(*p)[3] << 8  ) |           (*p)[4];
@@ -100,43 +100,43 @@
             break;
 
         default:
-            return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
+            return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
         }
     }
 
     if( *len > (size_t) ( end - *p ) )
-        return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
+        return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
 
     return( 0 );
 }
 
-int asn1_get_tag( unsigned char **p,
+int mbedtls_asn1_get_tag( unsigned char **p,
                   const unsigned char *end,
                   size_t *len, int tag )
 {
     if( ( end - *p ) < 1 )
-        return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
+        return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
 
     if( **p != tag )
-        return( POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
+        return( MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
 
     (*p)++;
 
-    return( asn1_get_len( p, end, len ) );
+    return( mbedtls_asn1_get_len( p, end, len ) );
 }
 
-int asn1_get_bool( unsigned char **p,
+int mbedtls_asn1_get_bool( unsigned char **p,
                    const unsigned char *end,
                    int *val )
 {
     int ret;
     size_t len;
 
-    if( ( ret = asn1_get_tag( p, end, &len, ASN1_BOOLEAN ) ) != 0 )
+    if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_BOOLEAN ) ) != 0 )
         return( ret );
 
     if( len != 1 )
-        return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
+        return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
 
     *val = ( **p != 0 ) ? 1 : 0;
     (*p)++;
@@ -144,18 +144,18 @@
     return( 0 );
 }
 
-int asn1_get_int( unsigned char **p,
+int mbedtls_asn1_get_int( unsigned char **p,
                   const unsigned char *end,
                   int *val )
 {
     int ret;
     size_t len;
 
-    if( ( ret = asn1_get_tag( p, end, &len, ASN1_INTEGER ) ) != 0 )
+    if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
         return( ret );
 
     if( len > sizeof( int ) || ( **p & 0x80 ) != 0 )
-        return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
+        return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
 
     *val = 0;
 
@@ -168,43 +168,43 @@
     return( 0 );
 }
 
-#if defined(POLARSSL_BIGNUM_C)
-int asn1_get_mpi( unsigned char **p,
+#if defined(MBEDTLS_BIGNUM_C)
+int mbedtls_asn1_get_mpi( unsigned char **p,
                   const unsigned char *end,
-                  mpi *X )
+                  mbedtls_mpi *X )
 {
     int ret;
     size_t len;
 
-    if( ( ret = asn1_get_tag( p, end, &len, ASN1_INTEGER ) ) != 0 )
+    if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
         return( ret );
 
-    ret = mpi_read_binary( X, *p, len );
+    ret = mbedtls_mpi_read_binary( X, *p, len );
 
     *p += len;
 
     return( ret );
 }
-#endif /* POLARSSL_BIGNUM_C */
+#endif /* MBEDTLS_BIGNUM_C */
 
-int asn1_get_bitstring( unsigned char **p, const unsigned char *end,
-                        asn1_bitstring *bs)
+int mbedtls_asn1_get_bitstring( unsigned char **p, const unsigned char *end,
+                        mbedtls_asn1_bitstring *bs)
 {
     int ret;
 
     /* Certificate type is a single byte bitstring */
-    if( ( ret = asn1_get_tag( p, end, &bs->len, ASN1_BIT_STRING ) ) != 0 )
+    if( ( ret = mbedtls_asn1_get_tag( p, end, &bs->len, MBEDTLS_ASN1_BIT_STRING ) ) != 0 )
         return( ret );
 
     /* Check length, subtract one for actual bit string length */
     if( bs->len < 1 )
-        return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
+        return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
     bs->len -= 1;
 
     /* Get number of unused bits, ensure unused bits <= 7 */
     bs->unused_bits = **p;
     if( bs->unused_bits > 7 )
-        return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
+        return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
     (*p)++;
 
     /* Get actual bitstring */
@@ -212,7 +212,7 @@
     *p += bs->len;
 
     if( *p != end )
-        return( POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
+        return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
 
     return( 0 );
 }
@@ -220,16 +220,16 @@
 /*
  * Get a bit string without unused bits
  */
-int asn1_get_bitstring_null( unsigned char **p, const unsigned char *end,
+int mbedtls_asn1_get_bitstring_null( unsigned char **p, const unsigned char *end,
                              size_t *len )
 {
     int ret;
 
-    if( ( ret = asn1_get_tag( p, end, len, ASN1_BIT_STRING ) ) != 0 )
+    if( ( ret = mbedtls_asn1_get_tag( p, end, len, MBEDTLS_ASN1_BIT_STRING ) ) != 0 )
         return( ret );
 
     if( (*len)-- < 2 || *(*p)++ != 0 )
-        return( POLARSSL_ERR_ASN1_INVALID_DATA );
+        return( MBEDTLS_ERR_ASN1_INVALID_DATA );
 
     return( 0 );
 }
@@ -239,29 +239,29 @@
 /*
  *  Parses and splits an ASN.1 "SEQUENCE OF <tag>"
  */
-int asn1_get_sequence_of( unsigned char **p,
+int mbedtls_asn1_get_sequence_of( unsigned char **p,
                           const unsigned char *end,
-                          asn1_sequence *cur,
+                          mbedtls_asn1_sequence *cur,
                           int tag)
 {
     int ret;
     size_t len;
-    asn1_buf *buf;
+    mbedtls_asn1_buf *buf;
 
     /* Get main sequence tag */
-    if( ( ret = asn1_get_tag( p, end, &len,
-            ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
+    if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
+            MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
         return( ret );
 
     if( *p + len != end )
-        return( POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
+        return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
 
     while( *p < end )
     {
         buf = &(cur->buf);
         buf->tag = **p;
 
-        if( ( ret = asn1_get_tag( p, end, &buf->len, tag ) ) != 0 )
+        if( ( ret = mbedtls_asn1_get_tag( p, end, &buf->len, tag ) ) != 0 )
             return( ret );
 
         buf->p = *p;
@@ -270,12 +270,12 @@
         /* Allocate and assign next pointer */
         if( *p < end )
         {
-            cur->next = polarssl_malloc( sizeof( asn1_sequence ) );
+            cur->next = mbedtls_malloc( sizeof( mbedtls_asn1_sequence ) );
 
             if( cur->next == NULL )
-                return( POLARSSL_ERR_ASN1_MALLOC_FAILED );
+                return( MBEDTLS_ERR_ASN1_MALLOC_FAILED );
 
-            memset( cur->next, 0, sizeof( asn1_sequence ) );
+            memset( cur->next, 0, sizeof( mbedtls_asn1_sequence ) );
 
             cur = cur->next;
         }
@@ -285,29 +285,29 @@
     cur->next = NULL;
 
     if( *p != end )
-        return( POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
+        return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
 
     return( 0 );
 }
 
-int asn1_get_alg( unsigned char **p,
+int mbedtls_asn1_get_alg( unsigned char **p,
                   const unsigned char *end,
-                  asn1_buf *alg, asn1_buf *params )
+                  mbedtls_asn1_buf *alg, mbedtls_asn1_buf *params )
 {
     int ret;
     size_t len;
 
-    if( ( ret = asn1_get_tag( p, end, &len,
-            ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
+    if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
+            MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
         return( ret );
 
     if( ( end - *p ) < 1 )
-        return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
+        return( MBEDTLS_ERR_ASN1_OUT_OF_DATA );
 
     alg->tag = **p;
     end = *p + len;
 
-    if( ( ret = asn1_get_tag( p, end, &alg->len, ASN1_OID ) ) != 0 )
+    if( ( ret = mbedtls_asn1_get_tag( p, end, &alg->len, MBEDTLS_ASN1_OID ) ) != 0 )
         return( ret );
 
     alg->p = *p;
@@ -315,67 +315,67 @@
 
     if( *p == end )
     {
-        polarssl_zeroize( params, sizeof(asn1_buf) );
+        mbedtls_zeroize( params, sizeof(mbedtls_asn1_buf) );
         return( 0 );
     }
 
     params->tag = **p;
     (*p)++;
 
-    if( ( ret = asn1_get_len( p, end, &params->len ) ) != 0 )
+    if( ( ret = mbedtls_asn1_get_len( p, end, &params->len ) ) != 0 )
         return( ret );
 
     params->p = *p;
     *p += params->len;
 
     if( *p != end )
-        return( POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
+        return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
 
     return( 0 );
 }
 
-int asn1_get_alg_null( unsigned char **p,
+int mbedtls_asn1_get_alg_null( unsigned char **p,
                        const unsigned char *end,
-                       asn1_buf *alg )
+                       mbedtls_asn1_buf *alg )
 {
     int ret;
-    asn1_buf params;
+    mbedtls_asn1_buf params;
 
-    memset( &params, 0, sizeof(asn1_buf) );
+    memset( &params, 0, sizeof(mbedtls_asn1_buf) );
 
-    if( ( ret = asn1_get_alg( p, end, alg, &params ) ) != 0 )
+    if( ( ret = mbedtls_asn1_get_alg( p, end, alg, &params ) ) != 0 )
         return( ret );
 
-    if( ( params.tag != ASN1_NULL && params.tag != 0 ) || params.len != 0 )
-        return( POLARSSL_ERR_ASN1_INVALID_DATA );
+    if( ( params.tag != MBEDTLS_ASN1_NULL && params.tag != 0 ) || params.len != 0 )
+        return( MBEDTLS_ERR_ASN1_INVALID_DATA );
 
     return( 0 );
 }
 
-void asn1_free_named_data( asn1_named_data *cur )
+void mbedtls_asn1_free_named_data( mbedtls_asn1_named_data *cur )
 {
     if( cur == NULL )
         return;
 
-    polarssl_free( cur->oid.p );
-    polarssl_free( cur->val.p );
+    mbedtls_free( cur->oid.p );
+    mbedtls_free( cur->val.p );
 
-    polarssl_zeroize( cur, sizeof( asn1_named_data ) );
+    mbedtls_zeroize( cur, sizeof( mbedtls_asn1_named_data ) );
 }
 
-void asn1_free_named_data_list( asn1_named_data **head )
+void mbedtls_asn1_free_named_data_list( mbedtls_asn1_named_data **head )
 {
-    asn1_named_data *cur;
+    mbedtls_asn1_named_data *cur;
 
     while( ( cur = *head ) != NULL )
     {
         *head = cur->next;
-        asn1_free_named_data( cur );
-        polarssl_free( cur );
+        mbedtls_asn1_free_named_data( cur );
+        mbedtls_free( cur );
     }
 }
 
-asn1_named_data *asn1_find_named_data( asn1_named_data *list,
+mbedtls_asn1_named_data *mbedtls_asn1_find_named_data( mbedtls_asn1_named_data *list,
                                        const char *oid, size_t len )
 {
     while( list != NULL )
@@ -392,4 +392,4 @@
     return( list );
 }
 
-#endif /* POLARSSL_ASN1_PARSE_C */
+#endif /* MBEDTLS_ASN1_PARSE_C */
diff --git a/library/asn1write.c b/library/asn1write.c
index a5a35f6..5219fcf 100644
--- a/library/asn1write.c
+++ b/library/asn1write.c
@@ -20,32 +20,32 @@
  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#if !defined(POLARSSL_CONFIG_FILE)
+#if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else
-#include POLARSSL_CONFIG_FILE
+#include MBEDTLS_CONFIG_FILE
 #endif
 
-#if defined(POLARSSL_ASN1_WRITE_C)
+#if defined(MBEDTLS_ASN1_WRITE_C)
 
 #include "mbedtls/asn1write.h"
 
 #include <string.h>
 
-#if defined(POLARSSL_PLATFORM_C)
+#if defined(MBEDTLS_PLATFORM_C)
 #include "mbedtls/platform.h"
 #else
 #include <stdlib.h>
-#define polarssl_malloc     malloc
-#define polarssl_free       free
+#define mbedtls_malloc     malloc
+#define mbedtls_free       free
 #endif
 
-int asn1_write_len( unsigned char **p, unsigned char *start, size_t len )
+int mbedtls_asn1_write_len( unsigned char **p, unsigned char *start, size_t len )
 {
     if( len < 0x80 )
     {
         if( *p - start < 1 )
-            return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
+            return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
 
         *--(*p) = (unsigned char) len;
         return( 1 );
@@ -54,7 +54,7 @@
     if( len <= 0xFF )
     {
         if( *p - start < 2 )
-            return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
+            return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
 
         *--(*p) = (unsigned char) len;
         *--(*p) = 0x81;
@@ -62,7 +62,7 @@
     }
 
     if( *p - start < 3 )
-        return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
+        return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
 
     // We assume we never have lengths larger than 65535 bytes
     //
@@ -73,23 +73,23 @@
     return( 3 );
 }
 
-int asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
+int mbedtls_asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag )
 {
     if( *p - start < 1 )
-        return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
+        return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
 
     *--(*p) = tag;
 
     return( 1 );
 }
 
-int asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
+int mbedtls_asn1_write_raw_buffer( unsigned char **p, unsigned char *start,
                            const unsigned char *buf, size_t size )
 {
     size_t len = 0;
 
     if( *p - start < (int) size )
-        return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
+        return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
 
     len = size;
     (*p) -= len;
@@ -98,21 +98,21 @@
     return( (int) len );
 }
 
-#if defined(POLARSSL_BIGNUM_C)
-int asn1_write_mpi( unsigned char **p, unsigned char *start, const mpi *X )
+#if defined(MBEDTLS_BIGNUM_C)
+int mbedtls_asn1_write_mpi( unsigned char **p, unsigned char *start, const mbedtls_mpi *X )
 {
     int ret;
     size_t len = 0;
 
     // Write the MPI
     //
-    len = mpi_size( X );
+    len = mbedtls_mpi_size( X );
 
     if( *p - start < (int) len )
-        return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
+        return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
 
     (*p) -= len;
-    MPI_CHK( mpi_write_binary( X, *p, len ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( X, *p, len ) );
 
     // DER format assumes 2s complement for numbers, so the leftmost bit
     // should be 0 for positive numbers and 1 for negative numbers.
@@ -120,50 +120,50 @@
     if( X->s ==1 && **p & 0x80 )
     {
         if( *p - start < 1 )
-            return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
+            return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
 
         *--(*p) = 0x00;
         len += 1;
     }
 
-    ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
-    ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_INTEGER ) );
+    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
+    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) );
 
     ret = (int) len;
 
 cleanup:
     return( ret );
 }
-#endif /* POLARSSL_BIGNUM_C */
+#endif /* MBEDTLS_BIGNUM_C */
 
-int asn1_write_null( unsigned char **p, unsigned char *start )
+int mbedtls_asn1_write_null( unsigned char **p, unsigned char *start )
 {
     int ret;
     size_t len = 0;
 
     // Write NULL
     //
-    ASN1_CHK_ADD( len, asn1_write_len( p, start, 0) );
-    ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_NULL ) );
+    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, 0) );
+    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_NULL ) );
 
     return( (int) len );
 }
 
-int asn1_write_oid( unsigned char **p, unsigned char *start,
+int mbedtls_asn1_write_oid( unsigned char **p, unsigned char *start,
                     const char *oid, size_t oid_len )
 {
     int ret;
     size_t len = 0;
 
-    ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
+    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
                                   (const unsigned char *) oid, oid_len ) );
-    ASN1_CHK_ADD( len , asn1_write_len( p, start, len ) );
-    ASN1_CHK_ADD( len , asn1_write_tag( p, start, ASN1_OID ) );
+    MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_len( p, start, len ) );
+    MBEDTLS_ASN1_CHK_ADD( len , mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OID ) );
 
     return( (int) len );
 }
 
-int asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
+int mbedtls_asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start,
                                      const char *oid, size_t oid_len,
                                      size_t par_len )
 {
@@ -171,37 +171,37 @@
     size_t len = 0;
 
     if( par_len == 0 )
-        ASN1_CHK_ADD( len, asn1_write_null( p, start ) );
+        MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_null( p, start ) );
     else
         len += par_len;
 
-    ASN1_CHK_ADD( len, asn1_write_oid( p, start, oid, oid_len ) );
+    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
 
-    ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
-    ASN1_CHK_ADD( len, asn1_write_tag( p, start,
-                                       ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
+    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
+    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
+                                       MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
 
     return( (int) len );
 }
 
-int asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
+int mbedtls_asn1_write_bool( unsigned char **p, unsigned char *start, int boolean )
 {
     int ret;
     size_t len = 0;
 
     if( *p - start < 1 )
-        return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
+        return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
 
     *--(*p) = (boolean) ? 1 : 0;
     len++;
 
-    ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
-    ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BOOLEAN ) );
+    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
+    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BOOLEAN ) );
 
     return( (int) len );
 }
 
-int asn1_write_int( unsigned char **p, unsigned char *start, int val )
+int mbedtls_asn1_write_int( unsigned char **p, unsigned char *start, int val )
 {
     int ret;
     size_t len = 0;
@@ -211,7 +211,7 @@
     // should be 0 for positive numbers and 1 for negative numbers.
     //
     if( *p - start < 1 )
-        return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
+        return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
 
     len += 1;
     *--(*p) = val;
@@ -219,49 +219,49 @@
     if( val > 0 && **p & 0x80 )
     {
         if( *p - start < 1 )
-            return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
+            return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
 
         *--(*p) = 0x00;
         len += 1;
     }
 
-    ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
-    ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_INTEGER ) );
+    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
+    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_INTEGER ) );
 
     return( (int) len );
 }
 
-int asn1_write_printable_string( unsigned char **p, unsigned char *start,
+int mbedtls_asn1_write_printable_string( unsigned char **p, unsigned char *start,
                                  const char *text, size_t text_len )
 {
     int ret;
     size_t len = 0;
 
-    ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
+    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
                   (const unsigned char *) text, text_len ) );
 
-    ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
-    ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_PRINTABLE_STRING ) );
+    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
+    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_PRINTABLE_STRING ) );
 
     return( (int) len );
 }
 
-int asn1_write_ia5_string( unsigned char **p, unsigned char *start,
+int mbedtls_asn1_write_ia5_string( unsigned char **p, unsigned char *start,
                            const char *text, size_t text_len )
 {
     int ret;
     size_t len = 0;
 
-    ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start,
+    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
                   (const unsigned char *) text, text_len ) );
 
-    ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
-    ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_IA5_STRING ) );
+    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
+    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_IA5_STRING ) );
 
     return( (int) len );
 }
 
-int asn1_write_bitstring( unsigned char **p, unsigned char *start,
+int mbedtls_asn1_write_bitstring( unsigned char **p, unsigned char *start,
                           const unsigned char *buf, size_t bits )
 {
     int ret;
@@ -272,7 +272,7 @@
     // Calculate byte length
     //
     if( *p - start < (int) size + 1 )
-        return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL );
+        return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
 
     len = size + 1;
     (*p) -= size;
@@ -282,58 +282,58 @@
     //
     *--(*p) = (unsigned char) (size * 8 - bits);
 
-    ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
-    ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_BIT_STRING ) );
+    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
+    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BIT_STRING ) );
 
     return( (int) len );
 }
 
-int asn1_write_octet_string( unsigned char **p, unsigned char *start,
+int mbedtls_asn1_write_octet_string( unsigned char **p, unsigned char *start,
                              const unsigned char *buf, size_t size )
 {
     int ret;
     size_t len = 0;
 
-    ASN1_CHK_ADD( len, asn1_write_raw_buffer( p, start, buf, size ) );
+    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, buf, size ) );
 
-    ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) );
-    ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_OCTET_STRING ) );
+    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
+    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OCTET_STRING ) );
 
     return( (int) len );
 }
 
-asn1_named_data *asn1_store_named_data( asn1_named_data **head,
+mbedtls_asn1_named_data *mbedtls_asn1_store_named_data( mbedtls_asn1_named_data **head,
                                         const char *oid, size_t oid_len,
                                         const unsigned char *val,
                                         size_t val_len )
 {
-    asn1_named_data *cur;
+    mbedtls_asn1_named_data *cur;
 
-    if( ( cur = asn1_find_named_data( *head, oid, oid_len ) ) == NULL )
+    if( ( cur = mbedtls_asn1_find_named_data( *head, oid, oid_len ) ) == NULL )
     {
         // Add new entry if not present yet based on OID
         //
-        if( ( cur = polarssl_malloc( sizeof(asn1_named_data) ) ) == NULL )
+        if( ( cur = mbedtls_malloc( sizeof(mbedtls_asn1_named_data) ) ) == NULL )
             return( NULL );
 
-        memset( cur, 0, sizeof(asn1_named_data) );
+        memset( cur, 0, sizeof(mbedtls_asn1_named_data) );
 
         cur->oid.len = oid_len;
-        cur->oid.p = polarssl_malloc( oid_len );
+        cur->oid.p = mbedtls_malloc( oid_len );
         if( cur->oid.p == NULL )
         {
-            polarssl_free( cur );
+            mbedtls_free( cur );
             return( NULL );
         }
 
         memcpy( cur->oid.p, oid, oid_len );
 
         cur->val.len = val_len;
-        cur->val.p = polarssl_malloc( val_len );
+        cur->val.p = mbedtls_malloc( val_len );
         if( cur->val.p == NULL )
         {
-            polarssl_free( cur->oid.p );
-            polarssl_free( cur );
+            mbedtls_free( cur->oid.p );
+            mbedtls_free( cur );
             return( NULL );
         }
 
@@ -344,15 +344,15 @@
     {
         // Enlarge existing value buffer if needed
         //
-        polarssl_free( cur->val.p );
+        mbedtls_free( cur->val.p );
         cur->val.p = NULL;
 
         cur->val.len = val_len;
-        cur->val.p = polarssl_malloc( val_len );
+        cur->val.p = mbedtls_malloc( val_len );
         if( cur->val.p == NULL )
         {
-            polarssl_free( cur->oid.p );
-            polarssl_free( cur );
+            mbedtls_free( cur->oid.p );
+            mbedtls_free( cur );
             return( NULL );
         }
     }
@@ -362,4 +362,4 @@
 
     return( cur );
 }
-#endif /* POLARSSL_ASN1_WRITE_C */
+#endif /* MBEDTLS_ASN1_WRITE_C */
diff --git a/library/base64.c b/library/base64.c
index 5af79df..e0a6f18 100644
--- a/library/base64.c
+++ b/library/base64.c
@@ -20,13 +20,13 @@
  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#if !defined(POLARSSL_CONFIG_FILE)
+#if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else
-#include POLARSSL_CONFIG_FILE
+#include MBEDTLS_CONFIG_FILE
 #endif
 
-#if defined(POLARSSL_BASE64_C)
+#if defined(MBEDTLS_BASE64_C)
 
 #include "mbedtls/base64.h"
 
@@ -37,15 +37,15 @@
 #include <inttypes.h>
 #endif
 
-#if defined(POLARSSL_SELF_TEST)
+#if defined(MBEDTLS_SELF_TEST)
 #include <string.h>
-#if defined(POLARSSL_PLATFORM_C)
+#if defined(MBEDTLS_PLATFORM_C)
 #include "mbedtls/platform.h"
 #else
 #include <stdio.h>
-#define polarssl_printf printf
-#endif /* POLARSSL_PLATFORM_C */
-#endif /* POLARSSL_SELF_TEST */
+#define mbedtls_printf printf
+#endif /* MBEDTLS_PLATFORM_C */
+#endif /* MBEDTLS_SELF_TEST */
 
 static const unsigned char base64_enc_map[64] =
 {
@@ -78,7 +78,7 @@
 /*
  * Encode a buffer into base64 format
  */
-int base64_encode( unsigned char *dst, size_t *dlen,
+int mbedtls_base64_encode( unsigned char *dst, size_t *dlen,
                    const unsigned char *src, size_t slen )
 {
     size_t i, n;
@@ -103,7 +103,7 @@
     if( *dlen < n + 1 )
     {
         *dlen = n + 1;
-        return( POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL );
+        return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
     }
 
     n = ( slen / 3 ) * 3;
@@ -144,7 +144,7 @@
 /*
  * Decode a base64-formatted buffer
  */
-int base64_decode( unsigned char *dst, size_t *dlen,
+int mbedtls_base64_decode( unsigned char *dst, size_t *dlen,
                    const unsigned char *src, size_t slen )
 {
     size_t i, n;
@@ -175,16 +175,16 @@
 
         /* Space inside a line is an error */
         if( x != 0 )
-            return( POLARSSL_ERR_BASE64_INVALID_CHARACTER );
+            return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
 
         if( src[i] == '=' && ++j > 2 )
-            return( POLARSSL_ERR_BASE64_INVALID_CHARACTER );
+            return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
 
         if( src[i] > 127 || base64_dec_map[src[i]] == 127 )
-            return( POLARSSL_ERR_BASE64_INVALID_CHARACTER );
+            return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
 
         if( base64_dec_map[src[i]] < 64 && j != 0 )
-            return( POLARSSL_ERR_BASE64_INVALID_CHARACTER );
+            return( MBEDTLS_ERR_BASE64_INVALID_CHARACTER );
 
         n++;
     }
@@ -198,7 +198,7 @@
     if( dst == NULL || *dlen < n )
     {
         *dlen = n;
-        return( POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL );
+        return( MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL );
     }
 
    for( j = 3, n = x = 0, p = dst; i > 0; i--, src++ )
@@ -223,7 +223,7 @@
     return( 0 );
 }
 
-#if defined(POLARSSL_SELF_TEST)
+#if defined(MBEDTLS_SELF_TEST)
 
 static const unsigned char base64_test_dec[64] =
 {
@@ -244,48 +244,48 @@
 /*
  * Checkup routine
  */
-int base64_self_test( int verbose )
+int mbedtls_base64_self_test( int verbose )
 {
     size_t len;
     const unsigned char *src;
     unsigned char buffer[128];
 
     if( verbose != 0 )
-        polarssl_printf( "  Base64 encoding test: " );
+        mbedtls_printf( "  Base64 encoding test: " );
 
     len = sizeof( buffer );
     src = base64_test_dec;
 
-    if( base64_encode( buffer, &len, src, 64 ) != 0 ||
+    if( mbedtls_base64_encode( buffer, &len, src, 64 ) != 0 ||
          memcmp( base64_test_enc, buffer, 88 ) != 0 )
     {
         if( verbose != 0 )
-            polarssl_printf( "failed\n" );
+            mbedtls_printf( "failed\n" );
 
         return( 1 );
     }
 
     if( verbose != 0 )
-        polarssl_printf( "passed\n  Base64 decoding test: " );
+        mbedtls_printf( "passed\n  Base64 decoding test: " );
 
     len = sizeof( buffer );
     src = base64_test_enc;
 
-    if( base64_decode( buffer, &len, src, 88 ) != 0 ||
+    if( mbedtls_base64_decode( buffer, &len, src, 88 ) != 0 ||
          memcmp( base64_test_dec, buffer, 64 ) != 0 )
     {
         if( verbose != 0 )
-            polarssl_printf( "failed\n" );
+            mbedtls_printf( "failed\n" );
 
         return( 1 );
     }
 
     if( verbose != 0 )
-        polarssl_printf( "passed\n\n" );
+        mbedtls_printf( "passed\n\n" );
 
     return( 0 );
 }
 
-#endif /* POLARSSL_SELF_TEST */
+#endif /* MBEDTLS_SELF_TEST */
 
-#endif /* POLARSSL_BASE64_C */
+#endif /* MBEDTLS_BASE64_C */
diff --git a/library/bignum.c b/library/bignum.c
index 8a751a3..45f9667 100644
--- a/library/bignum.c
+++ b/library/bignum.c
@@ -23,39 +23,39 @@
  *  This MPI implementation is based on:
  *
  *  http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf
- *  http://www.stillhq.com/extracted/gnupg-api/mpi/
+ *  http://www.stillhq.com/extracted/gnupg-api/mbedtls_mpi/
  *  http://math.libtomcrypt.com/files/tommath.pdf
  */
 
-#if !defined(POLARSSL_CONFIG_FILE)
+#if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else
-#include POLARSSL_CONFIG_FILE
+#include MBEDTLS_CONFIG_FILE
 #endif
 
-#if defined(POLARSSL_BIGNUM_C)
+#if defined(MBEDTLS_BIGNUM_C)
 
 #include "mbedtls/bignum.h"
 #include "mbedtls/bn_mul.h"
 
 #include <string.h>
 
-#if defined(POLARSSL_PLATFORM_C)
+#if defined(MBEDTLS_PLATFORM_C)
 #include "mbedtls/platform.h"
 #else
 #include <stdio.h>
 #include <stdlib.h>
-#define polarssl_printf     printf
-#define polarssl_malloc     malloc
-#define polarssl_free       free
+#define mbedtls_printf     printf
+#define mbedtls_malloc     malloc
+#define mbedtls_free       free
 #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;
 }
 
-#define ciL    (sizeof(t_uint))         /* chars in limb  */
+#define ciL    (sizeof(mbedtls_mpi_uint))         /* chars in limb  */
 #define biL    (ciL << 3)               /* bits  in limb  */
 #define biH    (ciL << 2)               /* half limb size */
 
@@ -68,7 +68,7 @@
 /*
  * Initialize one MPI
  */
-void mpi_init( mpi *X )
+void mbedtls_mpi_init( mbedtls_mpi *X )
 {
     if( X == NULL )
         return;
@@ -81,15 +81,15 @@
 /*
  * Unallocate one MPI
  */
-void mpi_free( mpi *X )
+void mbedtls_mpi_free( mbedtls_mpi *X )
 {
     if( X == NULL )
         return;
 
     if( X->p != NULL )
     {
-        polarssl_zeroize( X->p, X->n * ciL );
-        polarssl_free( X->p );
+        mbedtls_zeroize( X->p, X->n * ciL );
+        mbedtls_free( X->p );
     }
 
     X->s = 1;
@@ -100,25 +100,25 @@
 /*
  * Enlarge to the specified number of limbs
  */
-int mpi_grow( mpi *X, size_t nblimbs )
+int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs )
 {
-    t_uint *p;
+    mbedtls_mpi_uint *p;
 
-    if( nblimbs > POLARSSL_MPI_MAX_LIMBS )
-        return( POLARSSL_ERR_MPI_MALLOC_FAILED );
+    if( nblimbs > MBEDTLS_MPI_MAX_LIMBS )
+        return( MBEDTLS_ERR_MPI_MALLOC_FAILED );
 
     if( X->n < nblimbs )
     {
-        if( ( p = polarssl_malloc( nblimbs * ciL ) ) == NULL )
-            return( POLARSSL_ERR_MPI_MALLOC_FAILED );
+        if( ( p = mbedtls_malloc( nblimbs * ciL ) ) == NULL )
+            return( MBEDTLS_ERR_MPI_MALLOC_FAILED );
 
         memset( p, 0, nblimbs * ciL );
 
         if( X->p != NULL )
         {
             memcpy( p, X->p, X->n * ciL );
-            polarssl_zeroize( X->p, X->n * ciL );
-            polarssl_free( X->p );
+            mbedtls_zeroize( X->p, X->n * ciL );
+            mbedtls_free( X->p );
         }
 
         X->n = nblimbs;
@@ -132,14 +132,14 @@
  * Resize down as much as possible,
  * while keeping at least the specified number of limbs
  */
-int mpi_shrink( mpi *X, size_t nblimbs )
+int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs )
 {
-    t_uint *p;
+    mbedtls_mpi_uint *p;
     size_t i;
 
     /* Actually resize up in this case */
     if( X->n <= nblimbs )
-        return( mpi_grow( X, nblimbs ) );
+        return( mbedtls_mpi_grow( X, nblimbs ) );
 
     for( i = X->n - 1; i > 0; i-- )
         if( X->p[i] != 0 )
@@ -149,16 +149,16 @@
     if( i < nblimbs )
         i = nblimbs;
 
-    if( ( p = polarssl_malloc( i * ciL ) ) == NULL )
-        return( POLARSSL_ERR_MPI_MALLOC_FAILED );
+    if( ( p = mbedtls_malloc( i * ciL ) ) == NULL )
+        return( MBEDTLS_ERR_MPI_MALLOC_FAILED );
 
     memset( p, 0, i * ciL );
 
     if( X->p != NULL )
     {
         memcpy( p, X->p, i * ciL );
-        polarssl_zeroize( X->p, X->n * ciL );
-        polarssl_free( X->p );
+        mbedtls_zeroize( X->p, X->n * ciL );
+        mbedtls_free( X->p );
     }
 
     X->n = i;
@@ -170,7 +170,7 @@
 /*
  * Copy the contents of Y into X
  */
-int mpi_copy( mpi *X, const mpi *Y )
+int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y )
 {
     int ret;
     size_t i;
@@ -180,7 +180,7 @@
 
     if( Y->p == NULL )
     {
-        mpi_free( X );
+        mbedtls_mpi_free( X );
         return( 0 );
     }
 
@@ -191,7 +191,7 @@
 
     X->s = Y->s;
 
-    MPI_CHK( mpi_grow( X, i ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i ) );
 
     memset( X->p, 0, X->n * ciL );
     memcpy( X->p, Y->p, i * ciL );
@@ -204,13 +204,13 @@
 /*
  * Swap the contents of X and Y
  */
-void mpi_swap( mpi *X, mpi *Y )
+void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y )
 {
-    mpi T;
+    mbedtls_mpi T;
 
-    memcpy( &T,  X, sizeof( mpi ) );
-    memcpy(  X,  Y, sizeof( mpi ) );
-    memcpy(  Y, &T, sizeof( mpi ) );
+    memcpy( &T,  X, sizeof( mbedtls_mpi ) );
+    memcpy(  X,  Y, sizeof( mbedtls_mpi ) );
+    memcpy(  Y, &T, sizeof( mbedtls_mpi ) );
 }
 
 /*
@@ -218,7 +218,7 @@
  * about whether the assignment was made or not.
  * (Leaking information about the respective sizes of X and Y is ok however.)
  */
-int mpi_safe_cond_assign( mpi *X, const mpi *Y, unsigned char assign )
+int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign )
 {
     int ret = 0;
     size_t i;
@@ -226,7 +226,7 @@
     /* make sure assign is 0 or 1 */
     assign = ( assign != 0 );
 
-    MPI_CHK( mpi_grow( X, Y->n ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) );
 
     X->s = X->s * ( 1 - assign ) + Y->s * assign;
 
@@ -246,11 +246,11 @@
  * Here it is not ok to simply swap the pointers, which whould lead to
  * different memory access patterns when X and Y are used afterwards.
  */
-int mpi_safe_cond_swap( mpi *X, mpi *Y, unsigned char swap )
+int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char swap )
 {
     int ret, s;
     size_t i;
-    t_uint tmp;
+    mbedtls_mpi_uint tmp;
 
     if( X == Y )
         return( 0 );
@@ -258,8 +258,8 @@
     /* make sure swap is 0 or 1 */
     swap = ( swap != 0 );
 
-    MPI_CHK( mpi_grow( X, Y->n ) );
-    MPI_CHK( mpi_grow( Y, X->n ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, Y->n ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_grow( Y, X->n ) );
 
     s = X->s;
     X->s = X->s * ( 1 - swap ) + Y->s * swap;
@@ -280,11 +280,11 @@
 /*
  * Set value from integer
  */
-int mpi_lset( mpi *X, t_sint z )
+int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z )
 {
     int ret;
 
-    MPI_CHK( mpi_grow( X, 1 ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, 1 ) );
     memset( X->p, 0, X->n * ciL );
 
     X->p[0] = ( z < 0 ) ? -z : z;
@@ -298,7 +298,7 @@
 /*
  * Get a specific bit
  */
-int mpi_get_bit( const mpi *X, size_t pos )
+int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos )
 {
     if( X->n * biL <= pos )
         return( 0 );
@@ -309,25 +309,25 @@
 /*
  * Set a bit to a specific value of 0 or 1
  */
-int mpi_set_bit( mpi *X, size_t pos, unsigned char val )
+int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val )
 {
     int ret = 0;
     size_t off = pos / biL;
     size_t idx = pos % biL;
 
     if( val != 0 && val != 1 )
-        return( POLARSSL_ERR_MPI_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
 
     if( X->n * biL <= pos )
     {
         if( val == 0 )
             return( 0 );
 
-        MPI_CHK( mpi_grow( X, off + 1 ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, off + 1 ) );
     }
 
-    X->p[off] &= ~( (t_uint) 0x01 << idx );
-    X->p[off] |= (t_uint) val << idx;
+    X->p[off] &= ~( (mbedtls_mpi_uint) 0x01 << idx );
+    X->p[off] |= (mbedtls_mpi_uint) val << idx;
 
 cleanup:
 
@@ -337,7 +337,7 @@
 /*
  * Return the number of least significant bits
  */
-size_t mpi_lsb( const mpi *X )
+size_t mbedtls_mpi_lsb( const mbedtls_mpi *X )
 {
     size_t i, j, count = 0;
 
@@ -352,7 +352,7 @@
 /*
  * Return the number of most significant bits
  */
-size_t mpi_msb( const mpi *X )
+size_t mbedtls_mpi_msb( const mbedtls_mpi *X )
 {
     size_t i, j;
 
@@ -370,15 +370,15 @@
 /*
  * Return the total size in bytes
  */
-size_t mpi_size( const mpi *X )
+size_t mbedtls_mpi_size( const mbedtls_mpi *X )
 {
-    return( ( mpi_msb( X ) + 7 ) >> 3 );
+    return( ( mbedtls_mpi_msb( X ) + 7 ) >> 3 );
 }
 
 /*
  * Convert an ASCII character to digit value
  */
-static int mpi_get_digit( t_uint *d, int radix, char c )
+static int mpi_get_digit( mbedtls_mpi_uint *d, int radix, char c )
 {
     *d = 255;
 
@@ -386,8 +386,8 @@
     if( c >= 0x41 && c <= 0x46 ) *d = c - 0x37;
     if( c >= 0x61 && c <= 0x66 ) *d = c - 0x57;
 
-    if( *d >= (t_uint) radix )
-        return( POLARSSL_ERR_MPI_INVALID_CHARACTER );
+    if( *d >= (mbedtls_mpi_uint) radix )
+        return( MBEDTLS_ERR_MPI_INVALID_CHARACTER );
 
     return( 0 );
 }
@@ -395,17 +395,17 @@
 /*
  * Import from an ASCII string
  */
-int mpi_read_string( mpi *X, int radix, const char *s )
+int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s )
 {
     int ret;
     size_t i, j, slen, n;
-    t_uint d;
-    mpi T;
+    mbedtls_mpi_uint d;
+    mbedtls_mpi T;
 
     if( radix < 2 || radix > 16 )
-        return( POLARSSL_ERR_MPI_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
 
-    mpi_init( &T );
+    mbedtls_mpi_init( &T );
 
     slen = strlen( s );
 
@@ -413,8 +413,8 @@
     {
         n = BITS_TO_LIMBS( slen << 2 );
 
-        MPI_CHK( mpi_grow( X, n ) );
-        MPI_CHK( mpi_lset( X, 0 ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, n ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
 
         for( i = slen, j = 0; i > 0; i--, j++ )
         {
@@ -424,13 +424,13 @@
                 break;
             }
 
-            MPI_CHK( mpi_get_digit( &d, radix, s[i - 1] ) );
+            MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i - 1] ) );
             X->p[j / ( 2 * ciL )] |= d << ( ( j % ( 2 * ciL ) ) << 2 );
         }
     }
     else
     {
-        MPI_CHK( mpi_lset( X, 0 ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
 
         for( i = 0; i < slen; i++ )
         {
@@ -440,23 +440,23 @@
                 continue;
             }
 
-            MPI_CHK( mpi_get_digit( &d, radix, s[i] ) );
-            MPI_CHK( mpi_mul_int( &T, X, radix ) );
+            MBEDTLS_MPI_CHK( mpi_get_digit( &d, radix, s[i] ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T, X, radix ) );
 
             if( X->s == 1 )
             {
-                MPI_CHK( mpi_add_int( X, &T, d ) );
+                MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, &T, d ) );
             }
             else
             {
-                MPI_CHK( mpi_sub_int( X, &T, d ) );
+                MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( X, &T, d ) );
             }
         }
     }
 
 cleanup:
 
-    mpi_free( &T );
+    mbedtls_mpi_free( &T );
 
     return( ret );
 }
@@ -464,19 +464,19 @@
 /*
  * Helper to write the digits high-order first
  */
-static int mpi_write_hlp( mpi *X, int radix, char **p )
+static int mpi_write_hlp( mbedtls_mpi *X, int radix, char **p )
 {
     int ret;
-    t_uint r;
+    mbedtls_mpi_uint r;
 
     if( radix < 2 || radix > 16 )
-        return( POLARSSL_ERR_MPI_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
 
-    MPI_CHK( mpi_mod_int( &r, X, radix ) );
-    MPI_CHK( mpi_div_int( X, NULL, X, radix ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, radix ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_div_int( X, NULL, X, radix ) );
 
-    if( mpi_cmp_int( X, 0 ) != 0 )
-        MPI_CHK( mpi_write_hlp( X, radix, p ) );
+    if( mbedtls_mpi_cmp_int( X, 0 ) != 0 )
+        MBEDTLS_MPI_CHK( mpi_write_hlp( X, radix, p ) );
 
     if( r < 10 )
         *(*p)++ = (char)( r + 0x30 );
@@ -491,17 +491,17 @@
 /*
  * Export into an ASCII string
  */
-int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen )
+int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix, char *s, size_t *slen )
 {
     int ret = 0;
     size_t n;
     char *p;
-    mpi T;
+    mbedtls_mpi T;
 
     if( radix < 2 || radix > 16 )
-        return( POLARSSL_ERR_MPI_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
 
-    n = mpi_msb( X );
+    n = mbedtls_mpi_msb( X );
     if( radix >=  4 ) n >>= 1;
     if( radix >= 16 ) n >>= 1;
     n += 3;
@@ -509,11 +509,11 @@
     if( *slen < n )
     {
         *slen = n;
-        return( POLARSSL_ERR_MPI_BUFFER_TOO_SMALL );
+        return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
     }
 
     p = s;
-    mpi_init( &T );
+    mbedtls_mpi_init( &T );
 
     if( X->s == -1 )
         *p++ = '-';
@@ -540,12 +540,12 @@
     }
     else
     {
-        MPI_CHK( mpi_copy( &T, X ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T, X ) );
 
         if( T.s == -1 )
             T.s = 1;
 
-        MPI_CHK( mpi_write_hlp( &T, radix, &p ) );
+        MBEDTLS_MPI_CHK( mpi_write_hlp( &T, radix, &p ) );
     }
 
     *p++ = '\0';
@@ -553,33 +553,33 @@
 
 cleanup:
 
-    mpi_free( &T );
+    mbedtls_mpi_free( &T );
 
     return( ret );
 }
 
-#if defined(POLARSSL_FS_IO)
+#if defined(MBEDTLS_FS_IO)
 /*
  * Read X from an opened file
  */
-int mpi_read_file( mpi *X, int radix, FILE *fin )
+int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin )
 {
-    t_uint d;
+    mbedtls_mpi_uint d;
     size_t slen;
     char *p;
     /*
      * Buffer should have space for (short) label and decimal formatted MPI,
      * newline characters and '\0'
      */
-    char s[ POLARSSL_MPI_RW_BUFFER_SIZE ];
+    char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ];
 
     memset( s, 0, sizeof( s ) );
     if( fgets( s, sizeof( s ) - 1, fin ) == NULL )
-        return( POLARSSL_ERR_MPI_FILE_IO_ERROR );
+        return( MBEDTLS_ERR_MPI_FILE_IO_ERROR );
 
     slen = strlen( s );
     if( slen == sizeof( s ) - 2 )
-        return( POLARSSL_ERR_MPI_BUFFER_TOO_SMALL );
+        return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
 
     if( s[slen - 1] == '\n' ) { slen--; s[slen] = '\0'; }
     if( s[slen - 1] == '\r' ) { slen--; s[slen] = '\0'; }
@@ -589,13 +589,13 @@
         if( mpi_get_digit( &d, radix, *p ) != 0 )
             break;
 
-    return( mpi_read_string( X, radix, p + 1 ) );
+    return( mbedtls_mpi_read_string( X, radix, p + 1 ) );
 }
 
 /*
  * Write X into an opened file (or stdout if fout == NULL)
  */
-int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout )
+int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout )
 {
     int ret;
     size_t n, slen, plen;
@@ -603,13 +603,13 @@
      * Buffer should have space for (short) label and decimal formatted MPI,
      * newline characters and '\0'
      */
-    char s[ POLARSSL_MPI_RW_BUFFER_SIZE ];
+    char s[ MBEDTLS_MPI_RW_BUFFER_SIZE ];
 
     n = sizeof( s );
     memset( s, 0, n );
     n -= 2;
 
-    MPI_CHK( mpi_write_string( X, radix, s, (size_t *) &n ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_write_string( X, radix, s, (size_t *) &n ) );
 
     if( p == NULL ) p = "";
 
@@ -622,21 +622,21 @@
     {
         if( fwrite( p, 1, plen, fout ) != plen ||
             fwrite( s, 1, slen, fout ) != slen )
-            return( POLARSSL_ERR_MPI_FILE_IO_ERROR );
+            return( MBEDTLS_ERR_MPI_FILE_IO_ERROR );
     }
     else
-        polarssl_printf( "%s%s", p, s );
+        mbedtls_printf( "%s%s", p, s );
 
 cleanup:
 
     return( ret );
 }
-#endif /* POLARSSL_FS_IO */
+#endif /* MBEDTLS_FS_IO */
 
 /*
  * Import X from unsigned binary data, big endian
  */
-int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen )
+int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen )
 {
     int ret;
     size_t i, j, n;
@@ -645,11 +645,11 @@
         if( buf[n] != 0 )
             break;
 
-    MPI_CHK( mpi_grow( X, CHARS_TO_LIMBS( buflen - n ) ) );
-    MPI_CHK( mpi_lset( X, 0 ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, CHARS_TO_LIMBS( buflen - n ) ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
 
     for( i = buflen, j = 0; i > n; i--, j++ )
-        X->p[j / ciL] |= ((t_uint) buf[i - 1]) << ((j % ciL) << 3);
+        X->p[j / ciL] |= ((mbedtls_mpi_uint) buf[i - 1]) << ((j % ciL) << 3);
 
 cleanup:
 
@@ -659,14 +659,14 @@
 /*
  * Export X into unsigned binary data, big endian
  */
-int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen )
+int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf, size_t buflen )
 {
     size_t i, j, n;
 
-    n = mpi_size( X );
+    n = mbedtls_mpi_size( X );
 
     if( buflen < n )
-        return( POLARSSL_ERR_MPI_BUFFER_TOO_SMALL );
+        return( MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL );
 
     memset( buf, 0, buflen );
 
@@ -679,19 +679,19 @@
 /*
  * Left-shift: X <<= count
  */
-int mpi_shift_l( mpi *X, size_t count )
+int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count )
 {
     int ret;
     size_t i, v0, t1;
-    t_uint r0 = 0, r1;
+    mbedtls_mpi_uint r0 = 0, r1;
 
     v0 = count / (biL    );
     t1 = count & (biL - 1);
 
-    i = mpi_msb( X ) + count;
+    i = mbedtls_mpi_msb( X ) + count;
 
     if( X->n * biL < i )
-        MPI_CHK( mpi_grow( X, BITS_TO_LIMBS( i ) ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, BITS_TO_LIMBS( i ) ) );
 
     ret = 0;
 
@@ -729,16 +729,16 @@
 /*
  * Right-shift: X >>= count
  */
-int mpi_shift_r( mpi *X, size_t count )
+int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count )
 {
     size_t i, v0, v1;
-    t_uint r0 = 0, r1;
+    mbedtls_mpi_uint r0 = 0, r1;
 
     v0 = count /  biL;
     v1 = count & (biL - 1);
 
     if( v0 > X->n || ( v0 == X->n && v1 > 0 ) )
-        return mpi_lset( X, 0 );
+        return mbedtls_mpi_lset( X, 0 );
 
     /*
      * shift by count / limb_size
@@ -772,7 +772,7 @@
 /*
  * Compare unsigned values
  */
-int mpi_cmp_abs( const mpi *X, const mpi *Y )
+int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y )
 {
     size_t i, j;
 
@@ -802,7 +802,7 @@
 /*
  * Compare signed values
  */
-int mpi_cmp_mpi( const mpi *X, const mpi *Y )
+int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y )
 {
     size_t i, j;
 
@@ -835,35 +835,35 @@
 /*
  * Compare signed values
  */
-int mpi_cmp_int( const mpi *X, t_sint z )
+int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z )
 {
-    mpi Y;
-    t_uint p[1];
+    mbedtls_mpi Y;
+    mbedtls_mpi_uint p[1];
 
     *p  = ( z < 0 ) ? -z : z;
     Y.s = ( z < 0 ) ? -1 : 1;
     Y.n = 1;
     Y.p = p;
 
-    return( mpi_cmp_mpi( X, &Y ) );
+    return( mbedtls_mpi_cmp_mpi( X, &Y ) );
 }
 
 /*
  * Unsigned addition: X = |A| + |B|  (HAC 14.7)
  */
-int mpi_add_abs( mpi *X, const mpi *A, const mpi *B )
+int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
 {
     int ret;
     size_t i, j;
-    t_uint *o, *p, c;
+    mbedtls_mpi_uint *o, *p, c;
 
     if( X == B )
     {
-        const mpi *T = A; A = X; B = T;
+        const mbedtls_mpi *T = A; A = X; B = T;
     }
 
     if( X != A )
-        MPI_CHK( mpi_copy( X, A ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
 
     /*
      * X should always be positive as a result of unsigned additions.
@@ -874,7 +874,7 @@
         if( B->p[j - 1] != 0 )
             break;
 
-    MPI_CHK( mpi_grow( X, j ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
 
     o = B->p; p = X->p; c = 0;
 
@@ -888,7 +888,7 @@
     {
         if( i >= X->n )
         {
-            MPI_CHK( mpi_grow( X, i + 1 ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + 1 ) );
             p = X->p + i;
         }
 
@@ -901,12 +901,12 @@
 }
 
 /*
- * Helper for mpi subtraction
+ * Helper for mbedtls_mpi subtraction
  */
-static void mpi_sub_hlp( size_t n, t_uint *s, t_uint *d )
+static void mpi_sub_hlp( size_t n, mbedtls_mpi_uint *s, mbedtls_mpi_uint *d )
 {
     size_t i;
-    t_uint c, z;
+    mbedtls_mpi_uint c, z;
 
     for( i = c = 0; i < n; i++, s++, d++ )
     {
@@ -924,25 +924,25 @@
 /*
  * Unsigned subtraction: X = |A| - |B|  (HAC 14.9)
  */
-int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B )
+int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
 {
-    mpi TB;
+    mbedtls_mpi TB;
     int ret;
     size_t n;
 
-    if( mpi_cmp_abs( A, B ) < 0 )
-        return( POLARSSL_ERR_MPI_NEGATIVE_VALUE );
+    if( mbedtls_mpi_cmp_abs( A, B ) < 0 )
+        return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
 
-    mpi_init( &TB );
+    mbedtls_mpi_init( &TB );
 
     if( X == B )
     {
-        MPI_CHK( mpi_copy( &TB, B ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) );
         B = &TB;
     }
 
     if( X != A )
-        MPI_CHK( mpi_copy( X, A ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, A ) );
 
     /*
      * X should always be positive as a result of unsigned subtractions.
@@ -959,7 +959,7 @@
 
 cleanup:
 
-    mpi_free( &TB );
+    mbedtls_mpi_free( &TB );
 
     return( ret );
 }
@@ -967,26 +967,26 @@
 /*
  * Signed addition: X = A + B
  */
-int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B )
+int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
 {
     int ret, s = A->s;
 
     if( A->s * B->s < 0 )
     {
-        if( mpi_cmp_abs( A, B ) >= 0 )
+        if( mbedtls_mpi_cmp_abs( A, B ) >= 0 )
         {
-            MPI_CHK( mpi_sub_abs( X, A, B ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) );
             X->s =  s;
         }
         else
         {
-            MPI_CHK( mpi_sub_abs( X, B, A ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) );
             X->s = -s;
         }
     }
     else
     {
-        MPI_CHK( mpi_add_abs( X, A, B ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) );
         X->s = s;
     }
 
@@ -998,26 +998,26 @@
 /*
  * Signed subtraction: X = A - B
  */
-int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B )
+int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
 {
     int ret, s = A->s;
 
     if( A->s * B->s > 0 )
     {
-        if( mpi_cmp_abs( A, B ) >= 0 )
+        if( mbedtls_mpi_cmp_abs( A, B ) >= 0 )
         {
-            MPI_CHK( mpi_sub_abs( X, A, B ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, A, B ) );
             X->s =  s;
         }
         else
         {
-            MPI_CHK( mpi_sub_abs( X, B, A ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( X, B, A ) );
             X->s = -s;
         }
     }
     else
     {
-        MPI_CHK( mpi_add_abs( X, A, B ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( X, A, B ) );
         X->s = s;
     }
 
@@ -1029,37 +1029,37 @@
 /*
  * Signed addition: X = A + b
  */
-int mpi_add_int( mpi *X, const mpi *A, t_sint b )
+int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b )
 {
-    mpi _B;
-    t_uint p[1];
+    mbedtls_mpi _B;
+    mbedtls_mpi_uint p[1];
 
     p[0] = ( b < 0 ) ? -b : b;
     _B.s = ( b < 0 ) ? -1 : 1;
     _B.n = 1;
     _B.p = p;
 
-    return( mpi_add_mpi( X, A, &_B ) );
+    return( mbedtls_mpi_add_mpi( X, A, &_B ) );
 }
 
 /*
  * Signed subtraction: X = A - b
  */
-int mpi_sub_int( mpi *X, const mpi *A, t_sint b )
+int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b )
 {
-    mpi _B;
-    t_uint p[1];
+    mbedtls_mpi _B;
+    mbedtls_mpi_uint p[1];
 
     p[0] = ( b < 0 ) ? -b : b;
     _B.s = ( b < 0 ) ? -1 : 1;
     _B.n = 1;
     _B.p = p;
 
-    return( mpi_sub_mpi( X, A, &_B ) );
+    return( mbedtls_mpi_sub_mpi( X, A, &_B ) );
 }
 
 /*
- * Helper for mpi multiplication
+ * Helper for mbedtls_mpi multiplication
  */
 static
 #if defined(__APPLE__) && defined(__arm__)
@@ -1069,9 +1069,9 @@
  */
 __attribute__ ((noinline))
 #endif
-void mpi_mul_hlp( size_t i, t_uint *s, t_uint *d, t_uint b )
+void mpi_mul_hlp( size_t i, mbedtls_mpi_uint *s, mbedtls_mpi_uint *d, mbedtls_mpi_uint b )
 {
-    t_uint c = 0, t = 0;
+    mbedtls_mpi_uint c = 0, t = 0;
 
 #if defined(MULADDC_HUIT)
     for( ; i >= 8; i -= 8 )
@@ -1133,16 +1133,16 @@
 /*
  * Baseline multiplication: X = A * B  (HAC 14.12)
  */
-int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B )
+int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B )
 {
     int ret;
     size_t i, j;
-    mpi TA, TB;
+    mbedtls_mpi TA, TB;
 
-    mpi_init( &TA ); mpi_init( &TB );
+    mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
 
-    if( X == A ) { MPI_CHK( mpi_copy( &TA, A ) ); A = &TA; }
-    if( X == B ) { MPI_CHK( mpi_copy( &TB, B ) ); B = &TB; }
+    if( X == A ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) ); A = &TA; }
+    if( X == B ) { MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) ); B = &TB; }
 
     for( i = A->n; i > 0; i-- )
         if( A->p[i - 1] != 0 )
@@ -1152,8 +1152,8 @@
         if( B->p[j - 1] != 0 )
             break;
 
-    MPI_CHK( mpi_grow( X, i + j ) );
-    MPI_CHK( mpi_lset( X, 0 ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, i + j ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_lset( X, 0 ) );
 
     for( i++; j > 0; j-- )
         mpi_mul_hlp( i - 1, A->p, X->p + j - 1, B->p[j - 1] );
@@ -1162,7 +1162,7 @@
 
 cleanup:
 
-    mpi_free( &TB ); mpi_free( &TA );
+    mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TA );
 
     return( ret );
 }
@@ -1170,69 +1170,69 @@
 /*
  * Baseline multiplication: X = A * b
  */
-int mpi_mul_int( mpi *X, const mpi *A, t_uint b )
+int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b )
 {
-    mpi _B;
-    t_uint p[1];
+    mbedtls_mpi _B;
+    mbedtls_mpi_uint p[1];
 
     _B.s = 1;
     _B.n = 1;
     _B.p = p;
     p[0] = b;
 
-    return( mpi_mul_mpi( X, A, &_B ) );
+    return( mbedtls_mpi_mul_mpi( X, A, &_B ) );
 }
 
 /*
- * Division by mpi: A = Q * B + R  (HAC 14.20)
+ * Division by mbedtls_mpi: A = Q * B + R  (HAC 14.20)
  */
-int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B )
+int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B )
 {
     int ret;
     size_t i, n, t, k;
-    mpi X, Y, Z, T1, T2;
+    mbedtls_mpi X, Y, Z, T1, T2;
 
-    if( mpi_cmp_int( B, 0 ) == 0 )
-        return( POLARSSL_ERR_MPI_DIVISION_BY_ZERO );
+    if( mbedtls_mpi_cmp_int( B, 0 ) == 0 )
+        return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
 
-    mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
-    mpi_init( &T1 ); mpi_init( &T2 );
+    mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
+    mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
 
-    if( mpi_cmp_abs( A, B ) < 0 )
+    if( mbedtls_mpi_cmp_abs( A, B ) < 0 )
     {
-        if( Q != NULL ) MPI_CHK( mpi_lset( Q, 0 ) );
-        if( R != NULL ) MPI_CHK( mpi_copy( R, A ) );
+        if( Q != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_lset( Q, 0 ) );
+        if( R != NULL ) MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, A ) );
         return( 0 );
     }
 
-    MPI_CHK( mpi_copy( &X, A ) );
-    MPI_CHK( mpi_copy( &Y, B ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &X, A ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, B ) );
     X.s = Y.s = 1;
 
-    MPI_CHK( mpi_grow( &Z, A->n + 2 ) );
-    MPI_CHK( mpi_lset( &Z,  0 ) );
-    MPI_CHK( mpi_grow( &T1, 2 ) );
-    MPI_CHK( mpi_grow( &T2, 3 ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &Z, A->n + 2 ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Z,  0 ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T1, 2 ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T2, 3 ) );
 
-    k = mpi_msb( &Y ) % biL;
+    k = mbedtls_mpi_msb( &Y ) % biL;
     if( k < biL - 1 )
     {
         k = biL - 1 - k;
-        MPI_CHK( mpi_shift_l( &X, k ) );
-        MPI_CHK( mpi_shift_l( &Y, k ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &X, k ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, k ) );
     }
     else k = 0;
 
     n = X.n - 1;
     t = Y.n - 1;
-    MPI_CHK( mpi_shift_l( &Y, biL * ( n - t ) ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &Y, biL * ( n - t ) ) );
 
-    while( mpi_cmp_mpi( &X, &Y ) >= 0 )
+    while( mbedtls_mpi_cmp_mpi( &X, &Y ) >= 0 )
     {
         Z.p[n - t]++;
-        MPI_CHK( mpi_sub_mpi( &X, &X, &Y ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &Y ) );
     }
-    MPI_CHK( mpi_shift_r( &Y, biL * ( n - t ) ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, biL * ( n - t ) ) );
 
     for( i = n; i > t ; i-- )
     {
@@ -1240,22 +1240,22 @@
             Z.p[i - t - 1] = ~0;
         else
         {
-#if defined(POLARSSL_HAVE_UDBL)
-            t_udbl r;
+#if defined(MBEDTLS_HAVE_UDBL)
+            mbedtls_t_udbl r;
 
-            r  = (t_udbl) X.p[i] << biL;
-            r |= (t_udbl) X.p[i - 1];
+            r  = (mbedtls_t_udbl) X.p[i] << biL;
+            r |= (mbedtls_t_udbl) X.p[i - 1];
             r /= Y.p[t];
-            if( r > ( (t_udbl) 1 << biL ) - 1 )
-                r = ( (t_udbl) 1 << biL ) - 1;
+            if( r > ( (mbedtls_t_udbl) 1 << biL ) - 1 )
+                r = ( (mbedtls_t_udbl) 1 << biL ) - 1;
 
-            Z.p[i - t - 1] = (t_uint) r;
+            Z.p[i - t - 1] = (mbedtls_mpi_uint) r;
 #else
             /*
              * __udiv_qrnnd_c, from gmp/longlong.h
              */
-            t_uint q0, q1, r0, r1;
-            t_uint d0, d1, d, m;
+            mbedtls_mpi_uint q0, q1, r0, r1;
+            mbedtls_mpi_uint d0, d1, d, m;
 
             d  = Y.p[t];
             d0 = ( d << biH ) >> biH;
@@ -1290,7 +1290,7 @@
             r0 -= m;
 
             Z.p[i - t - 1] = ( q1 << biH ) | q0;
-#endif /* POLARSSL_HAVE_UDBL && !64-bit Apple with Clang 5.0 */
+#endif /* MBEDTLS_HAVE_UDBL && !64-bit Apple with Clang 5.0 */
         }
 
         Z.p[i - t - 1]++;
@@ -1298,51 +1298,51 @@
         {
             Z.p[i - t - 1]--;
 
-            MPI_CHK( mpi_lset( &T1, 0 ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &T1, 0 ) );
             T1.p[0] = ( t < 1 ) ? 0 : Y.p[t - 1];
             T1.p[1] = Y.p[t];
-            MPI_CHK( mpi_mul_int( &T1, &T1, Z.p[i - t - 1] ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &T1, Z.p[i - t - 1] ) );
 
-            MPI_CHK( mpi_lset( &T2, 0 ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &T2, 0 ) );
             T2.p[0] = ( i < 2 ) ? 0 : X.p[i - 2];
             T2.p[1] = ( i < 1 ) ? 0 : X.p[i - 1];
             T2.p[2] = X.p[i];
         }
-        while( mpi_cmp_mpi( &T1, &T2 ) > 0 );
+        while( mbedtls_mpi_cmp_mpi( &T1, &T2 ) > 0 );
 
-        MPI_CHK( mpi_mul_int( &T1, &Y, Z.p[i - t - 1] ) );
-        MPI_CHK( mpi_shift_l( &T1,  biL * ( i - t - 1 ) ) );
-        MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1, &Y, Z.p[i - t - 1] ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1,  biL * ( i - t - 1 ) ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X, &X, &T1 ) );
 
-        if( mpi_cmp_int( &X, 0 ) < 0 )
+        if( mbedtls_mpi_cmp_int( &X, 0 ) < 0 )
         {
-            MPI_CHK( mpi_copy( &T1, &Y ) );
-            MPI_CHK( mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
-            MPI_CHK( mpi_add_mpi( &X, &X, &T1 ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &T1, &Y ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &T1, biL * ( i - t - 1 ) ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &X, &X, &T1 ) );
             Z.p[i - t - 1]--;
         }
     }
 
     if( Q != NULL )
     {
-        MPI_CHK( mpi_copy( Q, &Z ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_copy( Q, &Z ) );
         Q->s = A->s * B->s;
     }
 
     if( R != NULL )
     {
-        MPI_CHK( mpi_shift_r( &X, k ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &X, k ) );
         X.s = A->s;
-        MPI_CHK( mpi_copy( R, &X ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_copy( R, &X ) );
 
-        if( mpi_cmp_int( R, 0 ) == 0 )
+        if( mbedtls_mpi_cmp_int( R, 0 ) == 0 )
             R->s = 1;
     }
 
 cleanup:
 
-    mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
-    mpi_free( &T1 ); mpi_free( &T2 );
+    mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
+    mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
 
     return( ret );
 }
@@ -1350,36 +1350,36 @@
 /*
  * Division by int: A = Q * b + R
  */
-int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_sint b )
+int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, mbedtls_mpi_sint b )
 {
-    mpi _B;
-    t_uint p[1];
+    mbedtls_mpi _B;
+    mbedtls_mpi_uint p[1];
 
     p[0] = ( b < 0 ) ? -b : b;
     _B.s = ( b < 0 ) ? -1 : 1;
     _B.n = 1;
     _B.p = p;
 
-    return( mpi_div_mpi( Q, R, A, &_B ) );
+    return( mbedtls_mpi_div_mpi( Q, R, A, &_B ) );
 }
 
 /*
  * Modulo: R = A mod B
  */
-int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B )
+int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B )
 {
     int ret;
 
-    if( mpi_cmp_int( B, 0 ) < 0 )
-        return( POLARSSL_ERR_MPI_NEGATIVE_VALUE );
+    if( mbedtls_mpi_cmp_int( B, 0 ) < 0 )
+        return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
 
-    MPI_CHK( mpi_div_mpi( NULL, R, A, B ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( NULL, R, A, B ) );
 
-    while( mpi_cmp_int( R, 0 ) < 0 )
-      MPI_CHK( mpi_add_mpi( R, R, B ) );
+    while( mbedtls_mpi_cmp_int( R, 0 ) < 0 )
+      MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( R, R, B ) );
 
-    while( mpi_cmp_mpi( R, B ) >= 0 )
-      MPI_CHK( mpi_sub_mpi( R, R, B ) );
+    while( mbedtls_mpi_cmp_mpi( R, B ) >= 0 )
+      MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( R, R, B ) );
 
 cleanup:
 
@@ -1389,16 +1389,16 @@
 /*
  * Modulo: r = A mod b
  */
-int mpi_mod_int( t_uint *r, const mpi *A, t_sint b )
+int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b )
 {
     size_t i;
-    t_uint x, y, z;
+    mbedtls_mpi_uint x, y, z;
 
     if( b == 0 )
-        return( POLARSSL_ERR_MPI_DIVISION_BY_ZERO );
+        return( MBEDTLS_ERR_MPI_DIVISION_BY_ZERO );
 
     if( b < 0 )
-        return( POLARSSL_ERR_MPI_NEGATIVE_VALUE );
+        return( MBEDTLS_ERR_MPI_NEGATIVE_VALUE );
 
     /*
      * handle trivial cases
@@ -1446,9 +1446,9 @@
 /*
  * Fast Montgomery initialization (thanks to Tom St Denis)
  */
-static void mpi_montg_init( t_uint *mm, const mpi *N )
+static void mpi_montg_init( mbedtls_mpi_uint *mm, const mbedtls_mpi *N )
 {
-    t_uint x, m0 = N->p[0];
+    mbedtls_mpi_uint x, m0 = N->p[0];
     unsigned int i;
 
     x  = m0;
@@ -1463,11 +1463,11 @@
 /*
  * Montgomery multiplication: A = A * B * R^-1 mod N  (HAC 14.36)
  */
-static void mpi_montmul( mpi *A, const mpi *B, const mpi *N, t_uint mm,
-                         const mpi *T )
+static void mpi_montmul( mbedtls_mpi *A, const mbedtls_mpi *B, const mbedtls_mpi *N, mbedtls_mpi_uint mm,
+                         const mbedtls_mpi *T )
 {
     size_t i, n, m;
-    t_uint u0, u1, *d;
+    mbedtls_mpi_uint u0, u1, *d;
 
     memset( T->p, 0, T->n * ciL );
 
@@ -1491,7 +1491,7 @@
 
     memcpy( A->p, d, ( n + 1 ) * ciL );
 
-    if( mpi_cmp_abs( A, N ) >= 0 )
+    if( mbedtls_mpi_cmp_abs( A, N ) >= 0 )
         mpi_sub_hlp( n, N->p, A->p );
     else
         /* prevent timing attacks */
@@ -1501,10 +1501,10 @@
 /*
  * Montgomery reduction: A = A * R^-1 mod N
  */
-static void mpi_montred( mpi *A, const mpi *N, t_uint mm, const mpi *T )
+static void mpi_montred( mbedtls_mpi *A, const mbedtls_mpi *N, mbedtls_mpi_uint mm, const mbedtls_mpi *T )
 {
-    t_uint z = 1;
-    mpi U;
+    mbedtls_mpi_uint z = 1;
+    mbedtls_mpi U;
 
     U.n = U.s = (int) z;
     U.p = &z;
@@ -1515,42 +1515,42 @@
 /*
  * Sliding-window exponentiation: X = A^E mod N  (HAC 14.85)
  */
-int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR )
+int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *_RR )
 {
     int ret;
     size_t wbits, wsize, one = 1;
     size_t i, j, nblimbs;
     size_t bufsize, nbits;
-    t_uint ei, mm, state;
-    mpi RR, T, W[ 2 << POLARSSL_MPI_WINDOW_SIZE ], Apos;
+    mbedtls_mpi_uint ei, mm, state;
+    mbedtls_mpi RR, T, W[ 2 << MBEDTLS_MPI_WINDOW_SIZE ], Apos;
     int neg;
 
-    if( mpi_cmp_int( N, 0 ) < 0 || ( N->p[0] & 1 ) == 0 )
-        return( POLARSSL_ERR_MPI_BAD_INPUT_DATA );
+    if( mbedtls_mpi_cmp_int( N, 0 ) < 0 || ( N->p[0] & 1 ) == 0 )
+        return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
 
-    if( mpi_cmp_int( E, 0 ) < 0 )
-        return( POLARSSL_ERR_MPI_BAD_INPUT_DATA );
+    if( mbedtls_mpi_cmp_int( E, 0 ) < 0 )
+        return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
 
     /*
      * Init temps and window size
      */
     mpi_montg_init( &mm, N );
-    mpi_init( &RR ); mpi_init( &T );
-    mpi_init( &Apos );
+    mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &T );
+    mbedtls_mpi_init( &Apos );
     memset( W, 0, sizeof( W ) );
 
-    i = mpi_msb( E );
+    i = mbedtls_mpi_msb( E );
 
     wsize = ( i > 671 ) ? 6 : ( i > 239 ) ? 5 :
             ( i >  79 ) ? 4 : ( i >  23 ) ? 3 : 1;
 
-    if( wsize > POLARSSL_MPI_WINDOW_SIZE )
-        wsize = POLARSSL_MPI_WINDOW_SIZE;
+    if( wsize > MBEDTLS_MPI_WINDOW_SIZE )
+        wsize = MBEDTLS_MPI_WINDOW_SIZE;
 
     j = N->n + 1;
-    MPI_CHK( mpi_grow( X, j ) );
-    MPI_CHK( mpi_grow( &W[1],  j ) );
-    MPI_CHK( mpi_grow( &T, j * 2 ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_grow( X, j ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[1],  j ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &T, j * 2 ) );
 
     /*
      * Compensate for negative A (and correct at the end)
@@ -1558,7 +1558,7 @@
     neg = ( A->s == -1 );
     if( neg )
     {
-        MPI_CHK( mpi_copy( &Apos, A ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Apos, A ) );
         Apos.s = 1;
         A = &Apos;
     }
@@ -1568,30 +1568,30 @@
      */
     if( _RR == NULL || _RR->p == NULL )
     {
-        MPI_CHK( mpi_lset( &RR, 1 ) );
-        MPI_CHK( mpi_shift_l( &RR, N->n * 2 * biL ) );
-        MPI_CHK( mpi_mod_mpi( &RR, &RR, N ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &RR, 1 ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &RR, N->n * 2 * biL ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &RR, &RR, N ) );
 
         if( _RR != NULL )
-            memcpy( _RR, &RR, sizeof( mpi ) );
+            memcpy( _RR, &RR, sizeof( mbedtls_mpi ) );
     }
     else
-        memcpy( &RR, _RR, sizeof( mpi ) );
+        memcpy( &RR, _RR, sizeof( mbedtls_mpi ) );
 
     /*
      * W[1] = A * R^2 * R^-1 mod N = A * R mod N
      */
-    if( mpi_cmp_mpi( A, N ) >= 0 )
-        MPI_CHK( mpi_mod_mpi( &W[1], A, N ) );
+    if( mbedtls_mpi_cmp_mpi( A, N ) >= 0 )
+        MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &W[1], A, N ) );
     else
-        MPI_CHK( mpi_copy( &W[1], A ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[1], A ) );
 
     mpi_montmul( &W[1], &RR, N, mm, &T );
 
     /*
      * X = R^2 * R^-1 mod N = R mod N
      */
-    MPI_CHK( mpi_copy( X, &RR ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &RR ) );
     mpi_montred( X, N, mm, &T );
 
     if( wsize > 1 )
@@ -1601,8 +1601,8 @@
          */
         j =  one << ( wsize - 1 );
 
-        MPI_CHK( mpi_grow( &W[j], N->n + 1 ) );
-        MPI_CHK( mpi_copy( &W[j], &W[1]    ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[j], N->n + 1 ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[j], &W[1]    ) );
 
         for( i = 0; i < wsize - 1; i++ )
             mpi_montmul( &W[j], &W[j], N, mm, &T );
@@ -1612,8 +1612,8 @@
          */
         for( i = j + 1; i < ( one << wsize ); i++ )
         {
-            MPI_CHK( mpi_grow( &W[i], N->n + 1 ) );
-            MPI_CHK( mpi_copy( &W[i], &W[i - 1] ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_grow( &W[i], N->n + 1 ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &W[i], &W[i - 1] ) );
 
             mpi_montmul( &W[i], &W[1], N, mm, &T );
         }
@@ -1634,7 +1634,7 @@
 
             nblimbs--;
 
-            bufsize = sizeof( t_uint ) << 3;
+            bufsize = sizeof( mbedtls_mpi_uint ) << 3;
         }
 
         bufsize--;
@@ -1704,18 +1704,18 @@
     if( neg )
     {
         X->s = -1;
-        MPI_CHK( mpi_add_mpi( X, N, X ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( X, N, X ) );
     }
 
 cleanup:
 
     for( i = ( one << ( wsize - 1 ) ); i < ( one << wsize ); i++ )
-        mpi_free( &W[i] );
+        mbedtls_mpi_free( &W[i] );
 
-    mpi_free( &W[1] ); mpi_free( &T ); mpi_free( &Apos );
+    mbedtls_mpi_free( &W[1] ); mbedtls_mpi_free( &T ); mbedtls_mpi_free( &Apos );
 
     if( _RR == NULL || _RR->p == NULL )
-        mpi_free( &RR );
+        mbedtls_mpi_free( &RR );
 
     return( ret );
 }
@@ -1723,51 +1723,51 @@
 /*
  * Greatest common divisor: G = gcd(A, B)  (HAC 14.54)
  */
-int mpi_gcd( mpi *G, const mpi *A, const mpi *B )
+int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B )
 {
     int ret;
     size_t lz, lzt;
-    mpi TG, TA, TB;
+    mbedtls_mpi TG, TA, TB;
 
-    mpi_init( &TG ); mpi_init( &TA ); mpi_init( &TB );
+    mbedtls_mpi_init( &TG ); mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TB );
 
-    MPI_CHK( mpi_copy( &TA, A ) );
-    MPI_CHK( mpi_copy( &TB, B ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TA, A ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, B ) );
 
-    lz = mpi_lsb( &TA );
-    lzt = mpi_lsb( &TB );
+    lz = mbedtls_mpi_lsb( &TA );
+    lzt = mbedtls_mpi_lsb( &TB );
 
     if( lzt < lz )
         lz = lzt;
 
-    MPI_CHK( mpi_shift_r( &TA, lz ) );
-    MPI_CHK( mpi_shift_r( &TB, lz ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, lz ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, lz ) );
 
     TA.s = TB.s = 1;
 
-    while( mpi_cmp_int( &TA, 0 ) != 0 )
+    while( mbedtls_mpi_cmp_int( &TA, 0 ) != 0 )
     {
-        MPI_CHK( mpi_shift_r( &TA, mpi_lsb( &TA ) ) );
-        MPI_CHK( mpi_shift_r( &TB, mpi_lsb( &TB ) ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, mbedtls_mpi_lsb( &TA ) ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, mbedtls_mpi_lsb( &TB ) ) );
 
-        if( mpi_cmp_mpi( &TA, &TB ) >= 0 )
+        if( mbedtls_mpi_cmp_mpi( &TA, &TB ) >= 0 )
         {
-            MPI_CHK( mpi_sub_abs( &TA, &TA, &TB ) );
-            MPI_CHK( mpi_shift_r( &TA, 1 ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TA, &TA, &TB ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TA, 1 ) );
         }
         else
         {
-            MPI_CHK( mpi_sub_abs( &TB, &TB, &TA ) );
-            MPI_CHK( mpi_shift_r( &TB, 1 ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &TB, &TB, &TA ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TB, 1 ) );
         }
     }
 
-    MPI_CHK( mpi_shift_l( &TB, lz ) );
-    MPI_CHK( mpi_copy( G, &TB ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &TB, lz ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_copy( G, &TB ) );
 
 cleanup:
 
-    mpi_free( &TG ); mpi_free( &TA ); mpi_free( &TB );
+    mbedtls_mpi_free( &TG ); mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TB );
 
     return( ret );
 }
@@ -1779,18 +1779,18 @@
  * regardless of the platform endianness (useful when f_rng is actually
  * deterministic, eg for tests).
  */
-int mpi_fill_random( mpi *X, size_t size,
+int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
                      int (*f_rng)(void *, unsigned char *, size_t),
                      void *p_rng )
 {
     int ret;
-    unsigned char buf[POLARSSL_MPI_MAX_SIZE];
+    unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
 
-    if( size > POLARSSL_MPI_MAX_SIZE )
-        return( POLARSSL_ERR_MPI_BAD_INPUT_DATA );
+    if( size > MBEDTLS_MPI_MAX_SIZE )
+        return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
 
-    MPI_CHK( f_rng( p_rng, buf, size ) );
-    MPI_CHK( mpi_read_binary( X, buf, size ) );
+    MBEDTLS_MPI_CHK( f_rng( p_rng, buf, size ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( X, buf, size ) );
 
 cleanup:
     return( ret );
@@ -1799,99 +1799,99 @@
 /*
  * Modular inverse: X = A^-1 mod N  (HAC 14.61 / 14.64)
  */
-int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N )
+int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N )
 {
     int ret;
-    mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
+    mbedtls_mpi G, TA, TU, U1, U2, TB, TV, V1, V2;
 
-    if( mpi_cmp_int( N, 0 ) <= 0 )
-        return( POLARSSL_ERR_MPI_BAD_INPUT_DATA );
+    if( mbedtls_mpi_cmp_int( N, 0 ) <= 0 )
+        return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
 
-    mpi_init( &TA ); mpi_init( &TU ); mpi_init( &U1 ); mpi_init( &U2 );
-    mpi_init( &G ); mpi_init( &TB ); mpi_init( &TV );
-    mpi_init( &V1 ); mpi_init( &V2 );
+    mbedtls_mpi_init( &TA ); mbedtls_mpi_init( &TU ); mbedtls_mpi_init( &U1 ); mbedtls_mpi_init( &U2 );
+    mbedtls_mpi_init( &G ); mbedtls_mpi_init( &TB ); mbedtls_mpi_init( &TV );
+    mbedtls_mpi_init( &V1 ); mbedtls_mpi_init( &V2 );
 
-    MPI_CHK( mpi_gcd( &G, A, N ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, A, N ) );
 
-    if( mpi_cmp_int( &G, 1 ) != 0 )
+    if( mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
     {
-        ret = POLARSSL_ERR_MPI_NOT_ACCEPTABLE;
+        ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
         goto cleanup;
     }
 
-    MPI_CHK( mpi_mod_mpi( &TA, A, N ) );
-    MPI_CHK( mpi_copy( &TU, &TA ) );
-    MPI_CHK( mpi_copy( &TB, N ) );
-    MPI_CHK( mpi_copy( &TV, N ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &TA, A, N ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TU, &TA ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TB, N ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &TV, N ) );
 
-    MPI_CHK( mpi_lset( &U1, 1 ) );
-    MPI_CHK( mpi_lset( &U2, 0 ) );
-    MPI_CHK( mpi_lset( &V1, 0 ) );
-    MPI_CHK( mpi_lset( &V2, 1 ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U1, 1 ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &U2, 0 ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V1, 0 ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &V2, 1 ) );
 
     do
     {
         while( ( TU.p[0] & 1 ) == 0 )
         {
-            MPI_CHK( mpi_shift_r( &TU, 1 ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TU, 1 ) );
 
             if( ( U1.p[0] & 1 ) != 0 || ( U2.p[0] & 1 ) != 0 )
             {
-                MPI_CHK( mpi_add_mpi( &U1, &U1, &TB ) );
-                MPI_CHK( mpi_sub_mpi( &U2, &U2, &TA ) );
+                MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &U1, &U1, &TB ) );
+                MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &TA ) );
             }
 
-            MPI_CHK( mpi_shift_r( &U1, 1 ) );
-            MPI_CHK( mpi_shift_r( &U2, 1 ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U1, 1 ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &U2, 1 ) );
         }
 
         while( ( TV.p[0] & 1 ) == 0 )
         {
-            MPI_CHK( mpi_shift_r( &TV, 1 ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &TV, 1 ) );
 
             if( ( V1.p[0] & 1 ) != 0 || ( V2.p[0] & 1 ) != 0 )
             {
-                MPI_CHK( mpi_add_mpi( &V1, &V1, &TB ) );
-                MPI_CHK( mpi_sub_mpi( &V2, &V2, &TA ) );
+                MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, &TB ) );
+                MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &TA ) );
             }
 
-            MPI_CHK( mpi_shift_r( &V1, 1 ) );
-            MPI_CHK( mpi_shift_r( &V2, 1 ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V1, 1 ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &V2, 1 ) );
         }
 
-        if( mpi_cmp_mpi( &TU, &TV ) >= 0 )
+        if( mbedtls_mpi_cmp_mpi( &TU, &TV ) >= 0 )
         {
-            MPI_CHK( mpi_sub_mpi( &TU, &TU, &TV ) );
-            MPI_CHK( mpi_sub_mpi( &U1, &U1, &V1 ) );
-            MPI_CHK( mpi_sub_mpi( &U2, &U2, &V2 ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TU, &TU, &TV ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U1, &U1, &V1 ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &U2, &U2, &V2 ) );
         }
         else
         {
-            MPI_CHK( mpi_sub_mpi( &TV, &TV, &TU ) );
-            MPI_CHK( mpi_sub_mpi( &V1, &V1, &U1 ) );
-            MPI_CHK( mpi_sub_mpi( &V2, &V2, &U2 ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &TV, &TV, &TU ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, &U1 ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V2, &V2, &U2 ) );
         }
     }
-    while( mpi_cmp_int( &TU, 0 ) != 0 );
+    while( mbedtls_mpi_cmp_int( &TU, 0 ) != 0 );
 
-    while( mpi_cmp_int( &V1, 0 ) < 0 )
-        MPI_CHK( mpi_add_mpi( &V1, &V1, N ) );
+    while( mbedtls_mpi_cmp_int( &V1, 0 ) < 0 )
+        MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &V1, &V1, N ) );
 
-    while( mpi_cmp_mpi( &V1, N ) >= 0 )
-        MPI_CHK( mpi_sub_mpi( &V1, &V1, N ) );
+    while( mbedtls_mpi_cmp_mpi( &V1, N ) >= 0 )
+        MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &V1, &V1, N ) );
 
-    MPI_CHK( mpi_copy( X, &V1 ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_copy( X, &V1 ) );
 
 cleanup:
 
-    mpi_free( &TA ); mpi_free( &TU ); mpi_free( &U1 ); mpi_free( &U2 );
-    mpi_free( &G ); mpi_free( &TB ); mpi_free( &TV );
-    mpi_free( &V1 ); mpi_free( &V2 );
+    mbedtls_mpi_free( &TA ); mbedtls_mpi_free( &TU ); mbedtls_mpi_free( &U1 ); mbedtls_mpi_free( &U2 );
+    mbedtls_mpi_free( &G ); mbedtls_mpi_free( &TB ); mbedtls_mpi_free( &TV );
+    mbedtls_mpi_free( &V1 ); mbedtls_mpi_free( &V2 );
 
     return( ret );
 }
 
-#if defined(POLARSSL_GENPRIME)
+#if defined(MBEDTLS_GENPRIME)
 
 static const int small_prime[] =
 {
@@ -1924,27 +1924,27 @@
  * Return values:
  * 0: no small factor (possible prime, more tests needed)
  * 1: certain prime
- * POLARSSL_ERR_MPI_NOT_ACCEPTABLE: certain non-prime
+ * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE: certain non-prime
  * other negative: error
  */
-static int mpi_check_small_factors( const mpi *X )
+static int mpi_check_small_factors( const mbedtls_mpi *X )
 {
     int ret = 0;
     size_t i;
-    t_uint r;
+    mbedtls_mpi_uint r;
 
     if( ( X->p[0] & 1 ) == 0 )
-        return( POLARSSL_ERR_MPI_NOT_ACCEPTABLE );
+        return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
 
     for( i = 0; small_prime[i] > 0; i++ )
     {
-        if( mpi_cmp_int( X, small_prime[i] ) <= 0 )
+        if( mbedtls_mpi_cmp_int( X, small_prime[i] ) <= 0 )
             return( 1 );
 
-        MPI_CHK( mpi_mod_int( &r, X, small_prime[i] ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, small_prime[i] ) );
 
         if( r == 0 )
-            return( POLARSSL_ERR_MPI_NOT_ACCEPTABLE );
+            return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
     }
 
 cleanup:
@@ -1954,27 +1954,27 @@
 /*
  * Miller-Rabin pseudo-primality test  (HAC 4.24)
  */
-static int mpi_miller_rabin( const mpi *X,
+static int mpi_miller_rabin( const mbedtls_mpi *X,
                              int (*f_rng)(void *, unsigned char *, size_t),
                              void *p_rng )
 {
     int ret;
     size_t i, j, n, s;
-    mpi W, R, T, A, RR;
+    mbedtls_mpi W, R, T, A, RR;
 
-    mpi_init( &W ); mpi_init( &R ); mpi_init( &T ); mpi_init( &A );
-    mpi_init( &RR );
+    mbedtls_mpi_init( &W ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &T ); mbedtls_mpi_init( &A );
+    mbedtls_mpi_init( &RR );
 
     /*
      * W = |X| - 1
      * R = W >> lsb( W )
      */
-    MPI_CHK( mpi_sub_int( &W, X, 1 ) );
-    s = mpi_lsb( &W );
-    MPI_CHK( mpi_copy( &R, &W ) );
-    MPI_CHK( mpi_shift_r( &R, s ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &W, X, 1 ) );
+    s = mbedtls_mpi_lsb( &W );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R, &W ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &R, s ) );
 
-    i = mpi_msb( X );
+    i = mbedtls_mpi_msb( X );
     /*
      * HAC, table 4.4
      */
@@ -1987,34 +1987,34 @@
         /*
          * pick a random A, 1 < A < |X| - 1
          */
-        MPI_CHK( mpi_fill_random( &A, X->n * ciL, f_rng, p_rng ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &A, X->n * ciL, f_rng, p_rng ) );
 
-        if( mpi_cmp_mpi( &A, &W ) >= 0 )
+        if( mbedtls_mpi_cmp_mpi( &A, &W ) >= 0 )
         {
-            j = mpi_msb( &A ) - mpi_msb( &W );
-            MPI_CHK( mpi_shift_r( &A, j + 1 ) );
+            j = mbedtls_mpi_msb( &A ) - mbedtls_mpi_msb( &W );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &A, j + 1 ) );
         }
         A.p[0] |= 3;
 
         /*
          * A = A^R mod |X|
          */
-        MPI_CHK( mpi_exp_mod( &A, &A, &R, X, &RR ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &A, &A, &R, X, &RR ) );
 
-        if( mpi_cmp_mpi( &A, &W ) == 0 ||
-            mpi_cmp_int( &A,  1 ) == 0 )
+        if( mbedtls_mpi_cmp_mpi( &A, &W ) == 0 ||
+            mbedtls_mpi_cmp_int( &A,  1 ) == 0 )
             continue;
 
         j = 1;
-        while( j < s && mpi_cmp_mpi( &A, &W ) != 0 )
+        while( j < s && mbedtls_mpi_cmp_mpi( &A, &W ) != 0 )
         {
             /*
              * A = A * A mod |X|
              */
-            MPI_CHK( mpi_mul_mpi( &T, &A, &A ) );
-            MPI_CHK( mpi_mod_mpi( &A, &T, X  ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &A, &A ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &A, &T, X  ) );
 
-            if( mpi_cmp_int( &A, 1 ) == 0 )
+            if( mbedtls_mpi_cmp_int( &A, 1 ) == 0 )
                 break;
 
             j++;
@@ -2023,17 +2023,17 @@
         /*
          * not prime if A != |X| - 1 or A == 1
          */
-        if( mpi_cmp_mpi( &A, &W ) != 0 ||
-            mpi_cmp_int( &A,  1 ) == 0 )
+        if( mbedtls_mpi_cmp_mpi( &A, &W ) != 0 ||
+            mbedtls_mpi_cmp_int( &A,  1 ) == 0 )
         {
-            ret = POLARSSL_ERR_MPI_NOT_ACCEPTABLE;
+            ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
             break;
         }
     }
 
 cleanup:
-    mpi_free( &W ); mpi_free( &R ); mpi_free( &T ); mpi_free( &A );
-    mpi_free( &RR );
+    mbedtls_mpi_free( &W ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &T ); mbedtls_mpi_free( &A );
+    mbedtls_mpi_free( &RR );
 
     return( ret );
 }
@@ -2041,22 +2041,22 @@
 /*
  * Pseudo-primality test: small factors, then Miller-Rabin
  */
-int mpi_is_prime( const mpi *X,
+int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
                   int (*f_rng)(void *, unsigned char *, size_t),
                   void *p_rng )
 {
     int ret;
-    mpi XX;
+    mbedtls_mpi XX;
 
     XX.s = 1;
     XX.n = X->n;
     XX.p = X->p;
 
-    if( mpi_cmp_int( &XX, 0 ) == 0 ||
-        mpi_cmp_int( &XX, 1 ) == 0 )
-        return( POLARSSL_ERR_MPI_NOT_ACCEPTABLE );
+    if( mbedtls_mpi_cmp_int( &XX, 0 ) == 0 ||
+        mbedtls_mpi_cmp_int( &XX, 1 ) == 0 )
+        return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
 
-    if( mpi_cmp_int( &XX, 2 ) == 0 )
+    if( mbedtls_mpi_cmp_int( &XX, 2 ) == 0 )
         return( 0 );
 
     if( ( ret = mpi_check_small_factors( &XX ) ) != 0 )
@@ -2073,38 +2073,38 @@
 /*
  * Prime number generation
  */
-int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
+int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int dh_flag,
                    int (*f_rng)(void *, unsigned char *, size_t),
                    void *p_rng )
 {
     int ret;
     size_t k, n;
-    t_uint r;
-    mpi Y;
+    mbedtls_mpi_uint r;
+    mbedtls_mpi Y;
 
-    if( nbits < 3 || nbits > POLARSSL_MPI_MAX_BITS )
-        return( POLARSSL_ERR_MPI_BAD_INPUT_DATA );
+    if( nbits < 3 || nbits > MBEDTLS_MPI_MAX_BITS )
+        return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
 
-    mpi_init( &Y );
+    mbedtls_mpi_init( &Y );
 
     n = BITS_TO_LIMBS( nbits );
 
-    MPI_CHK( mpi_fill_random( X, n * ciL, f_rng, p_rng ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( X, n * ciL, f_rng, p_rng ) );
 
-    k = mpi_msb( X );
-    if( k < nbits ) MPI_CHK( mpi_shift_l( X, nbits - k ) );
-    if( k > nbits ) MPI_CHK( mpi_shift_r( X, k - nbits ) );
+    k = mbedtls_mpi_msb( X );
+    if( k < nbits ) MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( X, nbits - k ) );
+    if( k > nbits ) MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( X, k - nbits ) );
 
     X->p[0] |= 3;
 
     if( dh_flag == 0 )
     {
-        while( ( ret = mpi_is_prime( X, f_rng, p_rng ) ) != 0 )
+        while( ( ret = mbedtls_mpi_is_prime( X, f_rng, p_rng ) ) != 0 )
         {
-            if( ret != POLARSSL_ERR_MPI_NOT_ACCEPTABLE )
+            if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
                 goto cleanup;
 
-            MPI_CHK( mpi_add_int( X, X, 2 ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 2 ) );
         }
     }
     else
@@ -2114,15 +2114,15 @@
          * is X = 2 mod 3 (which is equivalent to Y = 2 mod 3).
          * Make sure it is satisfied, while keeping X = 3 mod 4
          */
-        MPI_CHK( mpi_mod_int( &r, X, 3 ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_mod_int( &r, X, 3 ) );
         if( r == 0 )
-            MPI_CHK( mpi_add_int( X, X, 8 ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 8 ) );
         else if( r == 1 )
-            MPI_CHK( mpi_add_int( X, X, 4 ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( X, X, 4 ) );
 
         /* Set Y = (X-1) / 2, which is X / 2 because X is odd */
-        MPI_CHK( mpi_copy( &Y, X ) );
-        MPI_CHK( mpi_shift_r( &Y, 1 ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Y, X ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &Y, 1 ) );
 
         while( 1 )
         {
@@ -2138,7 +2138,7 @@
                 break;
             }
 
-            if( ret != POLARSSL_ERR_MPI_NOT_ACCEPTABLE )
+            if( ret != MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
                 goto cleanup;
 
             /*
@@ -2146,21 +2146,21 @@
              * Y = 1 mod 2 and Y = 2 mod 3 (eq X = 3 mod 4 and X = 2 mod 3)
              * so up Y by 6 and X by 12.
              */
-            MPI_CHK( mpi_add_int(  X,  X, 12 ) );
-            MPI_CHK( mpi_add_int( &Y, &Y, 6  ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_add_int(  X,  X, 12 ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &Y, &Y, 6  ) );
         }
     }
 
 cleanup:
 
-    mpi_free( &Y );
+    mbedtls_mpi_free( &Y );
 
     return( ret );
 }
 
-#endif /* POLARSSL_GENPRIME */
+#endif /* MBEDTLS_GENPRIME */
 
-#if defined(POLARSSL_SELF_TEST)
+#if defined(MBEDTLS_SELF_TEST)
 
 #define GCD_PAIR_COUNT  3
 
@@ -2174,34 +2174,34 @@
 /*
  * Checkup routine
  */
-int mpi_self_test( int verbose )
+int mbedtls_mpi_self_test( int verbose )
 {
     int ret, i;
-    mpi A, E, N, X, Y, U, V;
+    mbedtls_mpi A, E, N, X, Y, U, V;
 
-    mpi_init( &A ); mpi_init( &E ); mpi_init( &N ); mpi_init( &X );
-    mpi_init( &Y ); mpi_init( &U ); mpi_init( &V );
+    mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N ); mbedtls_mpi_init( &X );
+    mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &U ); mbedtls_mpi_init( &V );
 
-    MPI_CHK( mpi_read_string( &A, 16,
+    MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &A, 16,
         "EFE021C2645FD1DC586E69184AF4A31E" \
         "D5F53E93B5F123FA41680867BA110131" \
         "944FE7952E2517337780CB0DB80E61AA" \
         "E7C8DDC6C5C6AADEB34EB38A2F40D5E6" ) );
 
-    MPI_CHK( mpi_read_string( &E, 16,
+    MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &E, 16,
         "B2E7EFD37075B9F03FF989C7C5051C20" \
         "34D2A323810251127E7BF8625A4F49A5" \
         "F3E27F4DA8BD59C47D6DAABA4C8127BD" \
         "5B5C25763222FEFCCFC38B832366C29E" ) );
 
-    MPI_CHK( mpi_read_string( &N, 16,
+    MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &N, 16,
         "0066A198186C18C10B2F5ED9B522752A" \
         "9830B69916E535C8F047518A889A43A5" \
         "94B6BED27A168D31D4A52F88925AA8F5" ) );
 
-    MPI_CHK( mpi_mul_mpi( &X, &A, &N ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X, &A, &N ) );
 
-    MPI_CHK( mpi_read_string( &U, 16,
+    MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
         "602AB7ECA597A3D6B56FF9829A5E8B85" \
         "9E857EA95A03512E2BAE7391688D264A" \
         "A5663B0341DB9CCFD2C4C5F421FEC814" \
@@ -2211,104 +2211,104 @@
         "30879B56C61DE584A0F53A2447A51E" ) );
 
     if( verbose != 0 )
-        polarssl_printf( "  MPI test #1 (mul_mpi): " );
+        mbedtls_printf( "  MPI test #1 (mul_mpi): " );
 
-    if( mpi_cmp_mpi( &X, &U ) != 0 )
+    if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
     {
         if( verbose != 0 )
-            polarssl_printf( "failed\n" );
+            mbedtls_printf( "failed\n" );
 
         ret = 1;
         goto cleanup;
     }
 
     if( verbose != 0 )
-        polarssl_printf( "passed\n" );
+        mbedtls_printf( "passed\n" );
 
-    MPI_CHK( mpi_div_mpi( &X, &Y, &A, &N ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &X, &Y, &A, &N ) );
 
-    MPI_CHK( mpi_read_string( &U, 16,
+    MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
         "256567336059E52CAE22925474705F39A94" ) );
 
-    MPI_CHK( mpi_read_string( &V, 16,
+    MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &V, 16,
         "6613F26162223DF488E9CD48CC132C7A" \
         "0AC93C701B001B092E4E5B9F73BCD27B" \
         "9EE50D0657C77F374E903CDFA4C642" ) );
 
     if( verbose != 0 )
-        polarssl_printf( "  MPI test #2 (div_mpi): " );
+        mbedtls_printf( "  MPI test #2 (div_mpi): " );
 
-    if( mpi_cmp_mpi( &X, &U ) != 0 ||
-        mpi_cmp_mpi( &Y, &V ) != 0 )
+    if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 ||
+        mbedtls_mpi_cmp_mpi( &Y, &V ) != 0 )
     {
         if( verbose != 0 )
-            polarssl_printf( "failed\n" );
+            mbedtls_printf( "failed\n" );
 
         ret = 1;
         goto cleanup;
     }
 
     if( verbose != 0 )
-        polarssl_printf( "passed\n" );
+        mbedtls_printf( "passed\n" );
 
-    MPI_CHK( mpi_exp_mod( &X, &A, &E, &N, NULL ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &X, &A, &E, &N, NULL ) );
 
-    MPI_CHK( mpi_read_string( &U, 16,
+    MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
         "36E139AEA55215609D2816998ED020BB" \
         "BD96C37890F65171D948E9BC7CBAA4D9" \
         "325D24D6A3C12710F10A09FA08AB87" ) );
 
     if( verbose != 0 )
-        polarssl_printf( "  MPI test #3 (exp_mod): " );
+        mbedtls_printf( "  MPI test #3 (exp_mod): " );
 
-    if( mpi_cmp_mpi( &X, &U ) != 0 )
+    if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
     {
         if( verbose != 0 )
-            polarssl_printf( "failed\n" );
+            mbedtls_printf( "failed\n" );
 
         ret = 1;
         goto cleanup;
     }
 
     if( verbose != 0 )
-        polarssl_printf( "passed\n" );
+        mbedtls_printf( "passed\n" );
 
-    MPI_CHK( mpi_inv_mod( &X, &A, &N ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &X, &A, &N ) );
 
-    MPI_CHK( mpi_read_string( &U, 16,
+    MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &U, 16,
         "003A0AAEDD7E784FC07D8F9EC6E3BFD5" \
         "C3DBA76456363A10869622EAC2DD84EC" \
         "C5B8A74DAC4D09E03B5E0BE779F2DF61" ) );
 
     if( verbose != 0 )
-        polarssl_printf( "  MPI test #4 (inv_mod): " );
+        mbedtls_printf( "  MPI test #4 (inv_mod): " );
 
-    if( mpi_cmp_mpi( &X, &U ) != 0 )
+    if( mbedtls_mpi_cmp_mpi( &X, &U ) != 0 )
     {
         if( verbose != 0 )
-            polarssl_printf( "failed\n" );
+            mbedtls_printf( "failed\n" );
 
         ret = 1;
         goto cleanup;
     }
 
     if( verbose != 0 )
-        polarssl_printf( "passed\n" );
+        mbedtls_printf( "passed\n" );
 
     if( verbose != 0 )
-        polarssl_printf( "  MPI test #5 (simple gcd): " );
+        mbedtls_printf( "  MPI test #5 (simple gcd): " );
 
     for( i = 0; i < GCD_PAIR_COUNT; i++ )
     {
-        MPI_CHK( mpi_lset( &X, gcd_pairs[i][0] ) );
-        MPI_CHK( mpi_lset( &Y, gcd_pairs[i][1] ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &X, gcd_pairs[i][0] ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &Y, gcd_pairs[i][1] ) );
 
-        MPI_CHK( mpi_gcd( &A, &X, &Y ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &A, &X, &Y ) );
 
-        if( mpi_cmp_int( &A, gcd_pairs[i][2] ) != 0 )
+        if( mbedtls_mpi_cmp_int( &A, gcd_pairs[i][2] ) != 0 )
         {
             if( verbose != 0 )
-                polarssl_printf( "failed at %d\n", i );
+                mbedtls_printf( "failed at %d\n", i );
 
             ret = 1;
             goto cleanup;
@@ -2316,22 +2316,22 @@
     }
 
     if( verbose != 0 )
-        polarssl_printf( "passed\n" );
+        mbedtls_printf( "passed\n" );
 
 cleanup:
 
     if( ret != 0 && verbose != 0 )
-        polarssl_printf( "Unexpected error, return code = %08X\n", ret );
+        mbedtls_printf( "Unexpected error, return code = %08X\n", ret );
 
-    mpi_free( &A ); mpi_free( &E ); mpi_free( &N ); mpi_free( &X );
-    mpi_free( &Y ); mpi_free( &U ); mpi_free( &V );
+    mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N ); mbedtls_mpi_free( &X );
+    mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &U ); mbedtls_mpi_free( &V );
 
     if( verbose != 0 )
-        polarssl_printf( "\n" );
+        mbedtls_printf( "\n" );
 
     return( ret );
 }
 
-#endif /* POLARSSL_SELF_TEST */
+#endif /* MBEDTLS_SELF_TEST */
 
-#endif /* POLARSSL_BIGNUM_C */
+#endif /* MBEDTLS_BIGNUM_C */
diff --git a/library/blowfish.c b/library/blowfish.c
index 76ceb86..84cf0d8 100644
--- a/library/blowfish.c
+++ b/library/blowfish.c
@@ -26,22 +26,22 @@
  *
  */
 
-#if !defined(POLARSSL_CONFIG_FILE)
+#if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else
-#include POLARSSL_CONFIG_FILE
+#include MBEDTLS_CONFIG_FILE
 #endif
 
-#if defined(POLARSSL_BLOWFISH_C)
+#if defined(MBEDTLS_BLOWFISH_C)
 
 #include "mbedtls/blowfish.h"
 
 #include <string.h>
 
-#if !defined(POLARSSL_BLOWFISH_ALT)
+#if !defined(MBEDTLS_BLOWFISH_ALT)
 
 /* 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;
 }
 
@@ -68,7 +68,7 @@
 }
 #endif
 
-static const uint32_t P[BLOWFISH_ROUNDS + 2] = {
+static const uint32_t P[MBEDTLS_BLOWFISH_ROUNDS + 2] = {
         0x243F6A88L, 0x85A308D3L, 0x13198A2EL, 0x03707344L,
         0xA4093822L, 0x299F31D0L, 0x082EFA98L, 0xEC4E6C89L,
         0x452821E6L, 0x38D01377L, 0xBE5466CFL, 0x34E90C6CL,
@@ -79,7 +79,7 @@
 /* declarations of data at the end of this file */
 static const uint32_t S[4][256];
 
-static uint32_t F( blowfish_context *ctx, uint32_t x )
+static uint32_t F( mbedtls_blowfish_context *ctx, uint32_t x )
 {
    unsigned short a, b, c, d;
    uint32_t  y;
@@ -98,7 +98,7 @@
    return( y );
 }
 
-static void blowfish_enc( blowfish_context *ctx, uint32_t *xl, uint32_t *xr )
+static void blowfish_enc( mbedtls_blowfish_context *ctx, uint32_t *xl, uint32_t *xr )
 {
     uint32_t  Xl, Xr, temp;
     short i;
@@ -106,7 +106,7 @@
     Xl = *xl;
     Xr = *xr;
 
-    for( i = 0; i < BLOWFISH_ROUNDS; ++i )
+    for( i = 0; i < MBEDTLS_BLOWFISH_ROUNDS; ++i )
     {
         Xl = Xl ^ ctx->P[i];
         Xr = F( ctx, Xl ) ^ Xr;
@@ -120,14 +120,14 @@
     Xl = Xr;
     Xr = temp;
 
-    Xr = Xr ^ ctx->P[BLOWFISH_ROUNDS];
-    Xl = Xl ^ ctx->P[BLOWFISH_ROUNDS + 1];
+    Xr = Xr ^ ctx->P[MBEDTLS_BLOWFISH_ROUNDS];
+    Xl = Xl ^ ctx->P[MBEDTLS_BLOWFISH_ROUNDS + 1];
 
     *xl = Xl;
     *xr = Xr;
 }
 
-static void blowfish_dec( blowfish_context *ctx, uint32_t *xl, uint32_t *xr )
+static void blowfish_dec( mbedtls_blowfish_context *ctx, uint32_t *xl, uint32_t *xr )
 {
     uint32_t  Xl, Xr, temp;
     short i;
@@ -135,7 +135,7 @@
     Xl = *xl;
     Xr = *xr;
 
-    for( i = BLOWFISH_ROUNDS + 1; i > 1; --i )
+    for( i = MBEDTLS_BLOWFISH_ROUNDS + 1; i > 1; --i )
     {
         Xl = Xl ^ ctx->P[i];
         Xr = F( ctx, Xl ) ^ Xr;
@@ -156,32 +156,32 @@
     *xr = Xr;
 }
 
-void blowfish_init( blowfish_context *ctx )
+void mbedtls_blowfish_init( mbedtls_blowfish_context *ctx )
 {
-    memset( ctx, 0, sizeof( blowfish_context ) );
+    memset( ctx, 0, sizeof( mbedtls_blowfish_context ) );
 }
 
-void blowfish_free( blowfish_context *ctx )
+void mbedtls_blowfish_free( mbedtls_blowfish_context *ctx )
 {
     if( ctx == NULL )
         return;
 
-    polarssl_zeroize( ctx, sizeof( blowfish_context ) );
+    mbedtls_zeroize( ctx, sizeof( mbedtls_blowfish_context ) );
 }
 
 /*
  * Blowfish key schedule
  */
-int blowfish_setkey( blowfish_context *ctx, const unsigned char *key,
+int mbedtls_blowfish_setkey( mbedtls_blowfish_context *ctx, const unsigned char *key,
                      unsigned int keysize )
 {
     unsigned int i, j, k;
     uint32_t data, datal, datar;
 
-    if( keysize < BLOWFISH_MIN_KEY || keysize > BLOWFISH_MAX_KEY ||
+    if( keysize < MBEDTLS_BLOWFISH_MIN_KEY || keysize > MBEDTLS_BLOWFISH_MAX_KEY ||
         ( keysize % 8 ) )
     {
-        return( POLARSSL_ERR_BLOWFISH_INVALID_KEY_LENGTH );
+        return( MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH );
     }
 
     keysize >>= 3;
@@ -193,7 +193,7 @@
     }
 
     j = 0;
-    for( i = 0; i < BLOWFISH_ROUNDS + 2; ++i )
+    for( i = 0; i < MBEDTLS_BLOWFISH_ROUNDS + 2; ++i )
     {
         data = 0x00000000;
         for( k = 0; k < 4; ++k )
@@ -208,7 +208,7 @@
     datal = 0x00000000;
     datar = 0x00000000;
 
-    for( i = 0; i < BLOWFISH_ROUNDS + 2; i += 2 )
+    for( i = 0; i < MBEDTLS_BLOWFISH_ROUNDS + 2; i += 2 )
     {
         blowfish_enc( ctx, &datal, &datar );
         ctx->P[i] = datal;
@@ -230,21 +230,21 @@
 /*
  * Blowfish-ECB block encryption/decryption
  */
-int blowfish_crypt_ecb( blowfish_context *ctx,
+int mbedtls_blowfish_crypt_ecb( mbedtls_blowfish_context *ctx,
                     int mode,
-                    const unsigned char input[BLOWFISH_BLOCKSIZE],
-                    unsigned char output[BLOWFISH_BLOCKSIZE] )
+                    const unsigned char input[MBEDTLS_BLOWFISH_BLOCKSIZE],
+                    unsigned char output[MBEDTLS_BLOWFISH_BLOCKSIZE] )
 {
     uint32_t X0, X1;
 
     GET_UINT32_BE( X0, input,  0 );
     GET_UINT32_BE( X1, input,  4 );
 
-    if( mode == BLOWFISH_DECRYPT )
+    if( mode == MBEDTLS_BLOWFISH_DECRYPT )
     {
         blowfish_dec( ctx, &X0, &X1 );
     }
-    else /* BLOWFISH_ENCRYPT */
+    else /* MBEDTLS_BLOWFISH_ENCRYPT */
     {
         blowfish_enc( ctx, &X0, &X1 );
     }
@@ -255,87 +255,87 @@
     return( 0 );
 }
 
-#if defined(POLARSSL_CIPHER_MODE_CBC)
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
 /*
  * Blowfish-CBC buffer encryption/decryption
  */
-int blowfish_crypt_cbc( blowfish_context *ctx,
+int mbedtls_blowfish_crypt_cbc( mbedtls_blowfish_context *ctx,
                     int mode,
                     size_t length,
-                    unsigned char iv[BLOWFISH_BLOCKSIZE],
+                    unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
                     const unsigned char *input,
                     unsigned char *output )
 {
     int i;
-    unsigned char temp[BLOWFISH_BLOCKSIZE];
+    unsigned char temp[MBEDTLS_BLOWFISH_BLOCKSIZE];
 
-    if( length % BLOWFISH_BLOCKSIZE )
-        return( POLARSSL_ERR_BLOWFISH_INVALID_INPUT_LENGTH );
+    if( length % MBEDTLS_BLOWFISH_BLOCKSIZE )
+        return( MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH );
 
-    if( mode == BLOWFISH_DECRYPT )
+    if( mode == MBEDTLS_BLOWFISH_DECRYPT )
     {
         while( length > 0 )
         {
-            memcpy( temp, input, BLOWFISH_BLOCKSIZE );
-            blowfish_crypt_ecb( ctx, mode, input, output );
+            memcpy( temp, input, MBEDTLS_BLOWFISH_BLOCKSIZE );
+            mbedtls_blowfish_crypt_ecb( ctx, mode, input, output );
 
-            for( i = 0; i < BLOWFISH_BLOCKSIZE;i++ )
+            for( i = 0; i < MBEDTLS_BLOWFISH_BLOCKSIZE;i++ )
                 output[i] = (unsigned char)( output[i] ^ iv[i] );
 
-            memcpy( iv, temp, BLOWFISH_BLOCKSIZE );
+            memcpy( iv, temp, MBEDTLS_BLOWFISH_BLOCKSIZE );
 
-            input  += BLOWFISH_BLOCKSIZE;
-            output += BLOWFISH_BLOCKSIZE;
-            length -= BLOWFISH_BLOCKSIZE;
+            input  += MBEDTLS_BLOWFISH_BLOCKSIZE;
+            output += MBEDTLS_BLOWFISH_BLOCKSIZE;
+            length -= MBEDTLS_BLOWFISH_BLOCKSIZE;
         }
     }
     else
     {
         while( length > 0 )
         {
-            for( i = 0; i < BLOWFISH_BLOCKSIZE; i++ )
+            for( i = 0; i < MBEDTLS_BLOWFISH_BLOCKSIZE; i++ )
                 output[i] = (unsigned char)( input[i] ^ iv[i] );
 
-            blowfish_crypt_ecb( ctx, mode, output, output );
-            memcpy( iv, output, BLOWFISH_BLOCKSIZE );
+            mbedtls_blowfish_crypt_ecb( ctx, mode, output, output );
+            memcpy( iv, output, MBEDTLS_BLOWFISH_BLOCKSIZE );
 
-            input  += BLOWFISH_BLOCKSIZE;
-            output += BLOWFISH_BLOCKSIZE;
-            length -= BLOWFISH_BLOCKSIZE;
+            input  += MBEDTLS_BLOWFISH_BLOCKSIZE;
+            output += MBEDTLS_BLOWFISH_BLOCKSIZE;
+            length -= MBEDTLS_BLOWFISH_BLOCKSIZE;
         }
     }
 
     return( 0 );
 }
-#endif /* POLARSSL_CIPHER_MODE_CBC */
+#endif /* MBEDTLS_CIPHER_MODE_CBC */
 
-#if defined(POLARSSL_CIPHER_MODE_CFB)
+#if defined(MBEDTLS_CIPHER_MODE_CFB)
 /*
  * Blowfish CFB buffer encryption/decryption
  */
-int blowfish_crypt_cfb64( blowfish_context *ctx,
+int mbedtls_blowfish_crypt_cfb64( mbedtls_blowfish_context *ctx,
                        int mode,
                        size_t length,
                        size_t *iv_off,
-                       unsigned char iv[BLOWFISH_BLOCKSIZE],
+                       unsigned char iv[MBEDTLS_BLOWFISH_BLOCKSIZE],
                        const unsigned char *input,
                        unsigned char *output )
 {
     int c;
     size_t n = *iv_off;
 
-    if( mode == BLOWFISH_DECRYPT )
+    if( mode == MBEDTLS_BLOWFISH_DECRYPT )
     {
         while( length-- )
         {
             if( n == 0 )
-                blowfish_crypt_ecb( ctx, BLOWFISH_ENCRYPT, iv, iv );
+                mbedtls_blowfish_crypt_ecb( ctx, MBEDTLS_BLOWFISH_ENCRYPT, iv, iv );
 
             c = *input++;
             *output++ = (unsigned char)( c ^ iv[n] );
             iv[n] = (unsigned char) c;
 
-            n = ( n + 1 ) % BLOWFISH_BLOCKSIZE;
+            n = ( n + 1 ) % MBEDTLS_BLOWFISH_BLOCKSIZE;
         }
     }
     else
@@ -343,11 +343,11 @@
         while( length-- )
         {
             if( n == 0 )
-                blowfish_crypt_ecb( ctx, BLOWFISH_ENCRYPT, iv, iv );
+                mbedtls_blowfish_crypt_ecb( ctx, MBEDTLS_BLOWFISH_ENCRYPT, iv, iv );
 
             iv[n] = *output++ = (unsigned char)( iv[n] ^ *input++ );
 
-            n = ( n + 1 ) % BLOWFISH_BLOCKSIZE;
+            n = ( n + 1 ) % MBEDTLS_BLOWFISH_BLOCKSIZE;
         }
     }
 
@@ -355,17 +355,17 @@
 
     return( 0 );
 }
-#endif /*POLARSSL_CIPHER_MODE_CFB */
+#endif /*MBEDTLS_CIPHER_MODE_CFB */
 
-#if defined(POLARSSL_CIPHER_MODE_CTR)
+#if defined(MBEDTLS_CIPHER_MODE_CTR)
 /*
  * Blowfish CTR buffer encryption/decryption
  */
-int blowfish_crypt_ctr( blowfish_context *ctx,
+int mbedtls_blowfish_crypt_ctr( mbedtls_blowfish_context *ctx,
                        size_t length,
                        size_t *nc_off,
-                       unsigned char nonce_counter[BLOWFISH_BLOCKSIZE],
-                       unsigned char stream_block[BLOWFISH_BLOCKSIZE],
+                       unsigned char nonce_counter[MBEDTLS_BLOWFISH_BLOCKSIZE],
+                       unsigned char stream_block[MBEDTLS_BLOWFISH_BLOCKSIZE],
                        const unsigned char *input,
                        unsigned char *output )
 {
@@ -375,24 +375,24 @@
     while( length-- )
     {
         if( n == 0 ) {
-            blowfish_crypt_ecb( ctx, BLOWFISH_ENCRYPT, nonce_counter,
+            mbedtls_blowfish_crypt_ecb( ctx, MBEDTLS_BLOWFISH_ENCRYPT, nonce_counter,
                                 stream_block );
 
-            for( i = BLOWFISH_BLOCKSIZE; i > 0; i-- )
+            for( i = MBEDTLS_BLOWFISH_BLOCKSIZE; i > 0; i-- )
                 if( ++nonce_counter[i - 1] != 0 )
                     break;
         }
         c = *input++;
         *output++ = (unsigned char)( c ^ stream_block[n] );
 
-        n = ( n + 1 ) % BLOWFISH_BLOCKSIZE;
+        n = ( n + 1 ) % MBEDTLS_BLOWFISH_BLOCKSIZE;
     }
 
     *nc_off = n;
 
     return( 0 );
 }
-#endif /* POLARSSL_CIPHER_MODE_CTR */
+#endif /* MBEDTLS_CIPHER_MODE_CTR */
 
 static const uint32_t S[4][256] = {
     {   0xD1310BA6L, 0x98DFB5ACL, 0x2FFD72DBL, 0xD01ADFB7L,
@@ -653,5 +653,5 @@
         0xB74E6132L, 0xCE77E25BL, 0x578FDFE3L, 0x3AC372E6L  }
 };
 
-#endif /* !POLARSSL_BLOWFISH_ALT */
-#endif /* POLARSSL_BLOWFISH_C */
+#endif /* !MBEDTLS_BLOWFISH_ALT */
+#endif /* MBEDTLS_BLOWFISH_C */
diff --git a/library/camellia.c b/library/camellia.c
index 1aa6516..c9df94a 100644
--- a/library/camellia.c
+++ b/library/camellia.c
@@ -26,31 +26,31 @@
  *  http://info.isl.ntt.co.jp/crypt/eng/camellia/dl/01espec.pdf
  */
 
-#if !defined(POLARSSL_CONFIG_FILE)
+#if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else
-#include POLARSSL_CONFIG_FILE
+#include MBEDTLS_CONFIG_FILE
 #endif
 
-#if defined(POLARSSL_CAMELLIA_C)
+#if defined(MBEDTLS_CAMELLIA_C)
 
 #include "mbedtls/camellia.h"
 
 #include <string.h>
 
-#if defined(POLARSSL_SELF_TEST)
-#if defined(POLARSSL_PLATFORM_C)
+#if defined(MBEDTLS_SELF_TEST)
+#if defined(MBEDTLS_PLATFORM_C)
 #include "mbedtls/platform.h"
 #else
 #include <stdio.h>
-#define polarssl_printf printf
-#endif /* POLARSSL_PLATFORM_C */
-#endif /* POLARSSL_SELF_TEST */
+#define mbedtls_printf printf
+#endif /* MBEDTLS_PLATFORM_C */
+#endif /* MBEDTLS_SELF_TEST */
 
-#if !defined(POLARSSL_CAMELLIA_ALT)
+#if !defined(MBEDTLS_CAMELLIA_ALT)
 
 /* 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;
 }
 
@@ -87,7 +87,7 @@
     { 0xb0, 0x56, 0x88, 0xc2, 0xb3, 0xe6, 0xc1, 0xfd }
 };
 
-#if defined(POLARSSL_CAMELLIA_SMALL_MEMORY)
+#if defined(MBEDTLS_CAMELLIA_SMALL_MEMORY)
 
 static const unsigned char FSb[256] =
 {
@@ -114,7 +114,7 @@
 #define SBOX3(n) (unsigned char)((FSb[(n)] >> 1 ^ FSb[(n)] << 7) & 0xff)
 #define SBOX4(n) FSb[((n) << 1 ^ (n) >> 7) &0xff]
 
-#else /* POLARSSL_CAMELLIA_SMALL_MEMORY */
+#else /* MBEDTLS_CAMELLIA_SMALL_MEMORY */
 
 static const unsigned char FSb[256] =
 {
@@ -201,7 +201,7 @@
 #define SBOX3(n) FSb3[(n)]
 #define SBOX4(n) FSb4[(n)]
 
-#endif /* POLARSSL_CAMELLIA_SMALL_MEMORY */
+#endif /* MBEDTLS_CAMELLIA_SMALL_MEMORY */
 
 static const unsigned char shifts[2][4][4] =
 {
@@ -324,23 +324,23 @@
     z[1] ^= I0;
 }
 
-void camellia_init( camellia_context *ctx )
+void mbedtls_camellia_init( mbedtls_camellia_context *ctx )
 {
-    memset( ctx, 0, sizeof( camellia_context ) );
+    memset( ctx, 0, sizeof( mbedtls_camellia_context ) );
 }
 
-void camellia_free( camellia_context *ctx )
+void mbedtls_camellia_free( mbedtls_camellia_context *ctx )
 {
     if( ctx == NULL )
         return;
 
-    polarssl_zeroize( ctx, sizeof( camellia_context ) );
+    mbedtls_zeroize( ctx, sizeof( mbedtls_camellia_context ) );
 }
 
 /*
  * Camellia key schedule (encryption)
  */
-int camellia_setkey_enc( camellia_context *ctx, const unsigned char *key,
+int mbedtls_camellia_setkey_enc( mbedtls_camellia_context *ctx, const unsigned char *key,
                          unsigned int keysize )
 {
     int idx;
@@ -361,7 +361,7 @@
         case 128: ctx->nr = 3; idx = 0; break;
         case 192:
         case 256: ctx->nr = 4; idx = 1; break;
-        default : return( POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH );
+        default : return( MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH );
     }
 
     for( i = 0; i < keysize / 8; ++i )
@@ -445,19 +445,19 @@
 /*
  * Camellia key schedule (decryption)
  */
-int camellia_setkey_dec( camellia_context *ctx, const unsigned char *key,
+int mbedtls_camellia_setkey_dec( mbedtls_camellia_context *ctx, const unsigned char *key,
                          unsigned int keysize )
 {
     int idx, ret;
     size_t i;
-    camellia_context cty;
+    mbedtls_camellia_context cty;
     uint32_t *RK;
     uint32_t *SK;
 
-    camellia_init( &cty );
+    mbedtls_camellia_init( &cty );
 
     /* Also checks keysize */
-    if( ( ret = camellia_setkey_enc( &cty, key, keysize ) ) != 0 )
+    if( ( ret = mbedtls_camellia_setkey_enc( &cty, key, keysize ) ) != 0 )
         goto exit;
 
     ctx->nr = cty.nr;
@@ -485,7 +485,7 @@
     *RK++ = *SK++;
 
 exit:
-    camellia_free( &cty );
+    mbedtls_camellia_free( &cty );
 
     return( ret );
 }
@@ -493,7 +493,7 @@
 /*
  * Camellia-ECB block encryption/decryption
  */
-int camellia_crypt_ecb( camellia_context *ctx,
+int mbedtls_camellia_crypt_ecb( mbedtls_camellia_context *ctx,
                     int mode,
                     const unsigned char input[16],
                     unsigned char output[16] )
@@ -552,11 +552,11 @@
     return( 0 );
 }
 
-#if defined(POLARSSL_CIPHER_MODE_CBC)
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
 /*
  * Camellia-CBC buffer encryption/decryption
  */
-int camellia_crypt_cbc( camellia_context *ctx,
+int mbedtls_camellia_crypt_cbc( mbedtls_camellia_context *ctx,
                     int mode,
                     size_t length,
                     unsigned char iv[16],
@@ -567,14 +567,14 @@
     unsigned char temp[16];
 
     if( length % 16 )
-        return( POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH );
+        return( MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH );
 
-    if( mode == CAMELLIA_DECRYPT )
+    if( mode == MBEDTLS_CAMELLIA_DECRYPT )
     {
         while( length > 0 )
         {
             memcpy( temp, input, 16 );
-            camellia_crypt_ecb( ctx, mode, input, output );
+            mbedtls_camellia_crypt_ecb( ctx, mode, input, output );
 
             for( i = 0; i < 16; i++ )
                 output[i] = (unsigned char)( output[i] ^ iv[i] );
@@ -593,7 +593,7 @@
             for( i = 0; i < 16; i++ )
                 output[i] = (unsigned char)( input[i] ^ iv[i] );
 
-            camellia_crypt_ecb( ctx, mode, output, output );
+            mbedtls_camellia_crypt_ecb( ctx, mode, output, output );
             memcpy( iv, output, 16 );
 
             input  += 16;
@@ -604,13 +604,13 @@
 
     return( 0 );
 }
-#endif /* POLARSSL_CIPHER_MODE_CBC */
+#endif /* MBEDTLS_CIPHER_MODE_CBC */
 
-#if defined(POLARSSL_CIPHER_MODE_CFB)
+#if defined(MBEDTLS_CIPHER_MODE_CFB)
 /*
  * Camellia-CFB128 buffer encryption/decryption
  */
-int camellia_crypt_cfb128( camellia_context *ctx,
+int mbedtls_camellia_crypt_cfb128( mbedtls_camellia_context *ctx,
                        int mode,
                        size_t length,
                        size_t *iv_off,
@@ -621,12 +621,12 @@
     int c;
     size_t n = *iv_off;
 
-    if( mode == CAMELLIA_DECRYPT )
+    if( mode == MBEDTLS_CAMELLIA_DECRYPT )
     {
         while( length-- )
         {
             if( n == 0 )
-                camellia_crypt_ecb( ctx, CAMELLIA_ENCRYPT, iv, iv );
+                mbedtls_camellia_crypt_ecb( ctx, MBEDTLS_CAMELLIA_ENCRYPT, iv, iv );
 
             c = *input++;
             *output++ = (unsigned char)( c ^ iv[n] );
@@ -640,7 +640,7 @@
         while( length-- )
         {
             if( n == 0 )
-                camellia_crypt_ecb( ctx, CAMELLIA_ENCRYPT, iv, iv );
+                mbedtls_camellia_crypt_ecb( ctx, MBEDTLS_CAMELLIA_ENCRYPT, iv, iv );
 
             iv[n] = *output++ = (unsigned char)( iv[n] ^ *input++ );
 
@@ -652,13 +652,13 @@
 
     return( 0 );
 }
-#endif /* POLARSSL_CIPHER_MODE_CFB */
+#endif /* MBEDTLS_CIPHER_MODE_CFB */
 
-#if defined(POLARSSL_CIPHER_MODE_CTR)
+#if defined(MBEDTLS_CIPHER_MODE_CTR)
 /*
  * Camellia-CTR buffer encryption/decryption
  */
-int camellia_crypt_ctr( camellia_context *ctx,
+int mbedtls_camellia_crypt_ctr( mbedtls_camellia_context *ctx,
                        size_t length,
                        size_t *nc_off,
                        unsigned char nonce_counter[16],
@@ -672,7 +672,7 @@
     while( length-- )
     {
         if( n == 0 ) {
-            camellia_crypt_ecb( ctx, CAMELLIA_ENCRYPT, nonce_counter,
+            mbedtls_camellia_crypt_ecb( ctx, MBEDTLS_CAMELLIA_ENCRYPT, nonce_counter,
                                 stream_block );
 
             for( i = 16; i > 0; i-- )
@@ -689,10 +689,10 @@
 
     return( 0 );
 }
-#endif /* POLARSSL_CIPHER_MODE_CTR */
-#endif /* !POLARSSL_CAMELLIA_ALT */
+#endif /* MBEDTLS_CIPHER_MODE_CTR */
+#endif /* !MBEDTLS_CAMELLIA_ALT */
 
-#if defined(POLARSSL_SELF_TEST)
+#if defined(MBEDTLS_SELF_TEST)
 
 /*
  * Camellia test vectors from:
@@ -762,7 +762,7 @@
     }
 };
 
-#if defined(POLARSSL_CIPHER_MODE_CBC)
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
 #define CAMELLIA_TESTS_CBC  3
 
 static const unsigned char camellia_test_cbc_key[3][32] =
@@ -824,9 +824,9 @@
           0x33, 0x30, 0xCD, 0xF1, 0xB1, 0x86, 0x0A, 0x83 }
     }
 };
-#endif /* POLARSSL_CIPHER_MODE_CBC */
+#endif /* MBEDTLS_CIPHER_MODE_CBC */
 
-#if defined(POLARSSL_CIPHER_MODE_CTR)
+#if defined(MBEDTLS_CIPHER_MODE_CTR)
 /*
  * Camellia-CTR test vectors from:
  *
@@ -887,28 +887,28 @@
 
 static const int camellia_test_ctr_len[3] =
     { 16, 32, 36 };
-#endif /* POLARSSL_CIPHER_MODE_CTR */
+#endif /* MBEDTLS_CIPHER_MODE_CTR */
 
 /*
  * Checkup routine
  */
-int camellia_self_test( int verbose )
+int mbedtls_camellia_self_test( int verbose )
 {
     int i, j, u, v;
     unsigned char key[32];
     unsigned char buf[64];
     unsigned char src[16];
     unsigned char dst[16];
-#if defined(POLARSSL_CIPHER_MODE_CBC)
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
     unsigned char iv[16];
 #endif
-#if defined(POLARSSL_CIPHER_MODE_CTR)
+#if defined(MBEDTLS_CIPHER_MODE_CTR)
     size_t offset, len;
     unsigned char nonce_counter[16];
     unsigned char stream_block[16];
 #endif
 
-    camellia_context ctx;
+    mbedtls_camellia_context ctx;
 
     memset( key, 0, 32 );
 
@@ -917,41 +917,41 @@
     v = j & 1;
 
     if( verbose != 0 )
-        polarssl_printf( "  CAMELLIA-ECB-%3d (%s): ", 128 + u * 64,
-                         (v == CAMELLIA_DECRYPT) ? "dec" : "enc");
+        mbedtls_printf( "  CAMELLIA-ECB-%3d (%s): ", 128 + u * 64,
+                         (v == MBEDTLS_CAMELLIA_DECRYPT) ? "dec" : "enc");
 
     for( i = 0; i < CAMELLIA_TESTS_ECB; i++ ) {
         memcpy( key, camellia_test_ecb_key[u][i], 16 + 8 * u );
 
-        if( v == CAMELLIA_DECRYPT ) {
-            camellia_setkey_dec( &ctx, key, 128 + u * 64 );
+        if( v == MBEDTLS_CAMELLIA_DECRYPT ) {
+            mbedtls_camellia_setkey_dec( &ctx, key, 128 + u * 64 );
             memcpy( src, camellia_test_ecb_cipher[u][i], 16 );
             memcpy( dst, camellia_test_ecb_plain[i], 16 );
-        } else { /* CAMELLIA_ENCRYPT */
-            camellia_setkey_enc( &ctx, key, 128 + u * 64 );
+        } else { /* MBEDTLS_CAMELLIA_ENCRYPT */
+            mbedtls_camellia_setkey_enc( &ctx, key, 128 + u * 64 );
             memcpy( src, camellia_test_ecb_plain[i], 16 );
             memcpy( dst, camellia_test_ecb_cipher[u][i], 16 );
         }
 
-        camellia_crypt_ecb( &ctx, v, src, buf );
+        mbedtls_camellia_crypt_ecb( &ctx, v, src, buf );
 
         if( memcmp( buf, dst, 16 ) != 0 )
         {
             if( verbose != 0 )
-                polarssl_printf( "failed\n" );
+                mbedtls_printf( "failed\n" );
 
             return( 1 );
         }
     }
 
     if( verbose != 0 )
-        polarssl_printf( "passed\n" );
+        mbedtls_printf( "passed\n" );
     }
 
     if( verbose != 0 )
-        polarssl_printf( "\n" );
+        mbedtls_printf( "\n" );
 
-#if defined(POLARSSL_CIPHER_MODE_CBC)
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
     /*
      * CBC mode
      */
@@ -961,51 +961,51 @@
         v = j  & 1;
 
         if( verbose != 0 )
-            polarssl_printf( "  CAMELLIA-CBC-%3d (%s): ", 128 + u * 64,
-                             ( v == CAMELLIA_DECRYPT ) ? "dec" : "enc" );
+            mbedtls_printf( "  CAMELLIA-CBC-%3d (%s): ", 128 + u * 64,
+                             ( v == MBEDTLS_CAMELLIA_DECRYPT ) ? "dec" : "enc" );
 
     memcpy( src, camellia_test_cbc_iv, 16 );
     memcpy( dst, camellia_test_cbc_iv, 16 );
     memcpy( key, camellia_test_cbc_key[u], 16 + 8 * u );
 
-    if( v == CAMELLIA_DECRYPT ) {
-        camellia_setkey_dec( &ctx, key, 128 + u * 64 );
+    if( v == MBEDTLS_CAMELLIA_DECRYPT ) {
+        mbedtls_camellia_setkey_dec( &ctx, key, 128 + u * 64 );
     } else {
-        camellia_setkey_enc( &ctx, key, 128 + u * 64 );
+        mbedtls_camellia_setkey_enc( &ctx, key, 128 + u * 64 );
     }
 
     for( i = 0; i < CAMELLIA_TESTS_CBC; i++ ) {
 
-        if( v == CAMELLIA_DECRYPT ) {
+        if( v == MBEDTLS_CAMELLIA_DECRYPT ) {
             memcpy( iv , src, 16 );
             memcpy( src, camellia_test_cbc_cipher[u][i], 16 );
             memcpy( dst, camellia_test_cbc_plain[i], 16 );
-        } else { /* CAMELLIA_ENCRYPT */
+        } else { /* MBEDTLS_CAMELLIA_ENCRYPT */
             memcpy( iv , dst, 16 );
             memcpy( src, camellia_test_cbc_plain[i], 16 );
             memcpy( dst, camellia_test_cbc_cipher[u][i], 16 );
         }
 
-        camellia_crypt_cbc( &ctx, v, 16, iv, src, buf );
+        mbedtls_camellia_crypt_cbc( &ctx, v, 16, iv, src, buf );
 
         if( memcmp( buf, dst, 16 ) != 0 )
         {
             if( verbose != 0 )
-                polarssl_printf( "failed\n" );
+                mbedtls_printf( "failed\n" );
 
             return( 1 );
         }
     }
 
         if( verbose != 0 )
-            polarssl_printf( "passed\n" );
+            mbedtls_printf( "passed\n" );
     }
-#endif /* POLARSSL_CIPHER_MODE_CBC */
+#endif /* MBEDTLS_CIPHER_MODE_CBC */
 
     if( verbose != 0 )
-        polarssl_printf( "\n" );
+        mbedtls_printf( "\n" );
 
-#if defined(POLARSSL_CIPHER_MODE_CTR)
+#if defined(MBEDTLS_CIPHER_MODE_CTR)
     /*
      * CTR mode
      */
@@ -1015,27 +1015,27 @@
         v = i  & 1;
 
         if( verbose != 0 )
-            polarssl_printf( "  CAMELLIA-CTR-128 (%s): ",
-                             ( v == CAMELLIA_DECRYPT ) ? "dec" : "enc" );
+            mbedtls_printf( "  CAMELLIA-CTR-128 (%s): ",
+                             ( v == MBEDTLS_CAMELLIA_DECRYPT ) ? "dec" : "enc" );
 
         memcpy( nonce_counter, camellia_test_ctr_nonce_counter[u], 16 );
         memcpy( key, camellia_test_ctr_key[u], 16 );
 
         offset = 0;
-        camellia_setkey_enc( &ctx, key, 128 );
+        mbedtls_camellia_setkey_enc( &ctx, key, 128 );
 
-        if( v == CAMELLIA_DECRYPT )
+        if( v == MBEDTLS_CAMELLIA_DECRYPT )
         {
             len = camellia_test_ctr_len[u];
             memcpy( buf, camellia_test_ctr_ct[u], len );
 
-            camellia_crypt_ctr( &ctx, len, &offset, nonce_counter, stream_block,
+            mbedtls_camellia_crypt_ctr( &ctx, len, &offset, nonce_counter, stream_block,
                                 buf, buf );
 
             if( memcmp( buf, camellia_test_ctr_pt[u], len ) != 0 )
             {
                 if( verbose != 0 )
-                    polarssl_printf( "failed\n" );
+                    mbedtls_printf( "failed\n" );
 
                 return( 1 );
             }
@@ -1045,29 +1045,29 @@
             len = camellia_test_ctr_len[u];
             memcpy( buf, camellia_test_ctr_pt[u], len );
 
-            camellia_crypt_ctr( &ctx, len, &offset, nonce_counter, stream_block,
+            mbedtls_camellia_crypt_ctr( &ctx, len, &offset, nonce_counter, stream_block,
                                 buf, buf );
 
             if( memcmp( buf, camellia_test_ctr_ct[u], len ) != 0 )
             {
                 if( verbose != 0 )
-                    polarssl_printf( "failed\n" );
+                    mbedtls_printf( "failed\n" );
 
                 return( 1 );
             }
         }
 
         if( verbose != 0 )
-            polarssl_printf( "passed\n" );
+            mbedtls_printf( "passed\n" );
     }
 
     if( verbose != 0 )
-        polarssl_printf( "\n" );
-#endif /* POLARSSL_CIPHER_MODE_CTR */
+        mbedtls_printf( "\n" );
+#endif /* MBEDTLS_CIPHER_MODE_CTR */
 
     return( 0 );
 }
 
-#endif /* POLARSSL_SELF_TEST */
+#endif /* MBEDTLS_SELF_TEST */
 
-#endif /* POLARSSL_CAMELLIA_C */
+#endif /* MBEDTLS_CAMELLIA_C */
diff --git a/library/ccm.c b/library/ccm.c
index 6f3db01..72eed38 100644
--- a/library/ccm.c
+++ b/library/ccm.c
@@ -29,29 +29,29 @@
  * RFC 5116 "An Interface and Algorithms for Authenticated Encryption"
  */
 
-#if !defined(POLARSSL_CONFIG_FILE)
+#if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else
-#include POLARSSL_CONFIG_FILE
+#include MBEDTLS_CONFIG_FILE
 #endif
 
-#if defined(POLARSSL_CCM_C)
+#if defined(MBEDTLS_CCM_C)
 
 #include "mbedtls/ccm.h"
 
 #include <string.h>
 
-#if defined(POLARSSL_SELF_TEST) && defined(POLARSSL_AES_C)
-#if defined(POLARSSL_PLATFORM_C)
+#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
+#if defined(MBEDTLS_PLATFORM_C)
 #include "mbedtls/platform.h"
 #else
 #include <stdio.h>
-#define polarssl_printf printf
-#endif /* POLARSSL_PLATFORM_C */
-#endif /* POLARSSL_SELF_TEST && POLARSSL_AES_C */
+#define mbedtls_printf printf
+#endif /* MBEDTLS_PLATFORM_C */
+#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
 
 /* 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;
 }
 
@@ -61,28 +61,28 @@
 /*
  * Initialize context
  */
-int ccm_init( ccm_context *ctx, cipher_id_t cipher,
+int mbedtls_ccm_init( mbedtls_ccm_context *ctx, mbedtls_cipher_id_t cipher,
               const unsigned char *key, unsigned int keysize )
 {
     int ret;
-    const cipher_info_t *cipher_info;
+    const mbedtls_cipher_info_t *cipher_info;
 
-    memset( ctx, 0, sizeof( ccm_context ) );
+    memset( ctx, 0, sizeof( mbedtls_ccm_context ) );
 
-    cipher_init( &ctx->cipher_ctx );
+    mbedtls_cipher_init( &ctx->cipher_ctx );
 
-    cipher_info = cipher_info_from_values( cipher, keysize, POLARSSL_MODE_ECB );
+    cipher_info = mbedtls_cipher_info_from_values( cipher, keysize, MBEDTLS_MODE_ECB );
     if( cipher_info == NULL )
-        return( POLARSSL_ERR_CCM_BAD_INPUT );
+        return( MBEDTLS_ERR_CCM_BAD_INPUT );
 
     if( cipher_info->block_size != 16 )
-        return( POLARSSL_ERR_CCM_BAD_INPUT );
+        return( MBEDTLS_ERR_CCM_BAD_INPUT );
 
-    if( ( ret = cipher_init_ctx( &ctx->cipher_ctx, cipher_info ) ) != 0 )
+    if( ( ret = mbedtls_cipher_init_ctx( &ctx->cipher_ctx, cipher_info ) ) != 0 )
         return( ret );
 
-    if( ( ret = cipher_setkey( &ctx->cipher_ctx, key, keysize,
-                               POLARSSL_ENCRYPT ) ) != 0 )
+    if( ( ret = mbedtls_cipher_setkey( &ctx->cipher_ctx, key, keysize,
+                               MBEDTLS_ENCRYPT ) ) != 0 )
     {
         return( ret );
     }
@@ -93,10 +93,10 @@
 /*
  * Free context
  */
-void ccm_free( ccm_context *ctx )
+void mbedtls_ccm_free( mbedtls_ccm_context *ctx )
 {
-    cipher_free( &ctx->cipher_ctx );
-    polarssl_zeroize( ctx, sizeof( ccm_context ) );
+    mbedtls_cipher_free( &ctx->cipher_ctx );
+    mbedtls_zeroize( ctx, sizeof( mbedtls_ccm_context ) );
 }
 
 /*
@@ -112,7 +112,7 @@
     for( i = 0; i < 16; i++ )                                               \
         y[i] ^= b[i];                                                       \
                                                                             \
-    if( ( ret = cipher_update( &ctx->cipher_ctx, y, 16, y, &olen ) ) != 0 ) \
+    if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, y, 16, y, &olen ) ) != 0 ) \
         return( ret );
 
 /*
@@ -121,7 +121,7 @@
  * This avoids allocating one more 16 bytes buffer while allowing src == dst.
  */
 #define CTR_CRYPT( dst, src, len  )                                            \
-    if( ( ret = cipher_update( &ctx->cipher_ctx, ctr, 16, b, &olen ) ) != 0 )  \
+    if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, ctr, 16, b, &olen ) ) != 0 )  \
         return( ret );                                                         \
                                                                                \
     for( i = 0; i < len; i++ )                                                 \
@@ -130,7 +130,7 @@
 /*
  * Authenticated encryption or decryption
  */
-static int ccm_auth_crypt( ccm_context *ctx, int mode, size_t length,
+static int ccm_auth_crypt( mbedtls_ccm_context *ctx, int mode, size_t length,
                            const unsigned char *iv, size_t iv_len,
                            const unsigned char *add, size_t add_len,
                            const unsigned char *input, unsigned char *output,
@@ -152,14 +152,14 @@
      * 'length' checked later (when writing it to the first block)
      */
     if( tag_len < 4 || tag_len > 16 || tag_len % 2 != 0 )
-        return( POLARSSL_ERR_CCM_BAD_INPUT );
+        return( MBEDTLS_ERR_CCM_BAD_INPUT );
 
     /* Also implies q is within bounds */
     if( iv_len < 7 || iv_len > 13 )
-        return( POLARSSL_ERR_CCM_BAD_INPUT );
+        return( MBEDTLS_ERR_CCM_BAD_INPUT );
 
     if( add_len > 0xFF00 )
-        return( POLARSSL_ERR_CCM_BAD_INPUT );
+        return( MBEDTLS_ERR_CCM_BAD_INPUT );
 
     /*
      * First block B_0:
@@ -184,7 +184,7 @@
         b[15-i] = (unsigned char)( len_left & 0xFF );
 
     if( len_left > 0 )
-        return( POLARSSL_ERR_CCM_BAD_INPUT );
+        return( MBEDTLS_ERR_CCM_BAD_INPUT );
 
 
     /* Start CBC-MAC with first block */
@@ -298,7 +298,7 @@
 /*
  * Authenticated encryption
  */
-int ccm_encrypt_and_tag( ccm_context *ctx, size_t length,
+int mbedtls_ccm_encrypt_and_tag( mbedtls_ccm_context *ctx, size_t length,
                          const unsigned char *iv, size_t iv_len,
                          const unsigned char *add, size_t add_len,
                          const unsigned char *input, unsigned char *output,
@@ -311,7 +311,7 @@
 /*
  * Authenticated decryption
  */
-int ccm_auth_decrypt( ccm_context *ctx, size_t length,
+int mbedtls_ccm_auth_decrypt( mbedtls_ccm_context *ctx, size_t length,
                       const unsigned char *iv, size_t iv_len,
                       const unsigned char *add, size_t add_len,
                       const unsigned char *input, unsigned char *output,
@@ -335,15 +335,15 @@
 
     if( diff != 0 )
     {
-        polarssl_zeroize( output, length );
-        return( POLARSSL_ERR_CCM_AUTH_FAILED );
+        mbedtls_zeroize( output, length );
+        return( MBEDTLS_ERR_CCM_AUTH_FAILED );
     }
 
     return( 0 );
 }
 
 
-#if defined(POLARSSL_SELF_TEST) && defined(POLARSSL_AES_C)
+#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
 /*
  * Examples 1 to 3 from SP800-38C Appendix C
  */
@@ -391,17 +391,17 @@
         0x48, 0x43, 0x92, 0xfb, 0xc1, 0xb0, 0x99, 0x51 }
 };
 
-int ccm_self_test( int verbose )
+int mbedtls_ccm_self_test( int verbose )
 {
-    ccm_context ctx;
+    mbedtls_ccm_context ctx;
     unsigned char out[32];
     size_t i;
     int ret;
 
-    if( ccm_init( &ctx, POLARSSL_CIPHER_ID_AES, key, 8 * sizeof key ) != 0 )
+    if( mbedtls_ccm_init( &ctx, MBEDTLS_CIPHER_ID_AES, key, 8 * sizeof key ) != 0 )
     {
         if( verbose != 0 )
-            polarssl_printf( "  CCM: setup failed" );
+            mbedtls_printf( "  CCM: setup failed" );
 
         return( 1 );
     }
@@ -409,9 +409,9 @@
     for( i = 0; i < NB_TESTS; i++ )
     {
         if( verbose != 0 )
-            polarssl_printf( "  CCM-AES #%u: ", (unsigned int) i + 1 );
+            mbedtls_printf( "  CCM-AES #%u: ", (unsigned int) i + 1 );
 
-        ret = ccm_encrypt_and_tag( &ctx, msg_len[i],
+        ret = mbedtls_ccm_encrypt_and_tag( &ctx, msg_len[i],
                                    iv, iv_len[i], ad, add_len[i],
                                    msg, out,
                                    out + msg_len[i], tag_len[i] );
@@ -420,12 +420,12 @@
             memcmp( out, res[i], msg_len[i] + tag_len[i] ) != 0 )
         {
             if( verbose != 0 )
-                polarssl_printf( "failed\n" );
+                mbedtls_printf( "failed\n" );
 
             return( 1 );
         }
 
-        ret = ccm_auth_decrypt( &ctx, msg_len[i],
+        ret = mbedtls_ccm_auth_decrypt( &ctx, msg_len[i],
                                 iv, iv_len[i], ad, add_len[i],
                                 res[i], out,
                                 res[i] + msg_len[i], tag_len[i] );
@@ -434,23 +434,23 @@
             memcmp( out, msg, msg_len[i] ) != 0 )
         {
             if( verbose != 0 )
-                polarssl_printf( "failed\n" );
+                mbedtls_printf( "failed\n" );
 
             return( 1 );
         }
 
         if( verbose != 0 )
-            polarssl_printf( "passed\n" );
+            mbedtls_printf( "passed\n" );
     }
 
-    ccm_free( &ctx );
+    mbedtls_ccm_free( &ctx );
 
     if( verbose != 0 )
-        polarssl_printf( "\n" );
+        mbedtls_printf( "\n" );
 
     return( 0 );
 }
 
-#endif /* POLARSSL_SELF_TEST && POLARSSL_AES_C */
+#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
 
-#endif /* POLARSSL_CCM_C */
+#endif /* MBEDTLS_CCM_C */
diff --git a/library/certs.c b/library/certs.c
index 13f89e0..8c7ec8f 100644
--- a/library/certs.c
+++ b/library/certs.c
@@ -20,17 +20,17 @@
  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#if !defined(POLARSSL_CONFIG_FILE)
+#if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else
-#include POLARSSL_CONFIG_FILE
+#include MBEDTLS_CONFIG_FILE
 #endif
 
 #include "mbedtls/certs.h"
 
-#if defined(POLARSSL_CERTS_C)
+#if defined(MBEDTLS_CERTS_C)
 
-#if defined(POLARSSL_ECDSA_C)
+#if defined(MBEDTLS_ECDSA_C)
 #define TEST_CA_CRT_EC                                                  \
 "-----BEGIN CERTIFICATE-----\r\n"                                       \
 "MIICUjCCAdegAwIBAgIJAMFD4n5iQ8zoMAoGCCqGSM49BAMCMD4xCzAJBgNVBAYT\r\n"  \
@@ -47,9 +47,9 @@
 "t4d0PCu412mUC6Nnd7izvtE2MgIxAP1nnJQjZ8BWukszFQDG48wxCCyci9qpdSMv\r\n"  \
 "uCjn8pwUOkABXK8Mss90fzCfCEOtIA==\r\n"                                  \
 "-----END CERTIFICATE-----\r\n"
-const char test_ca_crt_ec[] = TEST_CA_CRT_EC;
+const char mbedtls_test_ca_crt_ec[] = TEST_CA_CRT_EC;
 
-const char test_ca_key_ec[] =
+const char mbedtls_test_ca_key_ec[] =
 "-----BEGIN EC PRIVATE KEY-----\r\n"
 "Proc-Type: 4,ENCRYPTED\r\n"
 "DEK-Info: DES-EDE3-CBC,307EAB469933D64E\r\n"
@@ -60,9 +60,9 @@
 "a77x/sY1Bvii8S9/XhDTb6pTMx06wzrm\r\n"
 "-----END EC PRIVATE KEY-----\r\n";
 
-const char test_ca_pwd_ec[] = "PolarSSLTest";
+const char mbedtls_test_ca_pwd_ec[] = "PolarSSLTest";
 
-const char test_srv_crt_ec[] =
+const char mbedtls_test_srv_crt_ec[] =
 "-----BEGIN CERTIFICATE-----\r\n"
 "MIICHzCCAaWgAwIBAgIBCTAKBggqhkjOPQQDAjA+MQswCQYDVQQGEwJOTDERMA8G\r\n"
 "A1UEChMIUG9sYXJTU0wxHDAaBgNVBAMTE1BvbGFyc3NsIFRlc3QgRUMgQ0EwHhcN\r\n"
@@ -78,14 +78,14 @@
 "fGa5kHvHARBPc8YAIVIqDvHH1Q==\r\n"
 "-----END CERTIFICATE-----\r\n";
 
-const char test_srv_key_ec[] =
+const char mbedtls_test_srv_key_ec[] =
 "-----BEGIN EC PRIVATE KEY-----\r\n"
 "MHcCAQEEIPEqEyB2AnCoPL/9U/YDHvdqXYbIogTywwyp6/UfDw6noAoGCCqGSM49\r\n"
 "AwEHoUQDQgAEN8xW2XYJHlpyPsdZLf8gbu58+QaRdNCtFLX3aCJZYpJO5QDYIxH/\r\n"
 "6i/SNF1dFr2KiMJrdw1VzYoqDvoByLTt/w==\r\n"
 "-----END EC PRIVATE KEY-----\r\n";
 
-const char test_cli_crt_ec[] =
+const char mbedtls_test_cli_crt_ec[] =
 "-----BEGIN CERTIFICATE-----\r\n"
 "MIICLDCCAbKgAwIBAgIBDTAKBggqhkjOPQQDAjA+MQswCQYDVQQGEwJOTDERMA8G\r\n"
 "A1UEChMIUG9sYXJTU0wxHDAaBgNVBAMTE1BvbGFyc3NsIFRlc3QgRUMgQ0EwHhcN\r\n"
@@ -101,25 +101,25 @@
 "LwjQje5PDGHfd3h9tP38Qknu5bJqws0md2KOKHyeV0U=\r\n"
 "-----END CERTIFICATE-----\r\n";
 
-const char test_cli_key_ec[] =
+const char mbedtls_test_cli_key_ec[] =
 "-----BEGIN EC PRIVATE KEY-----\r\n"
 "MHcCAQEEIPb3hmTxZ3/mZI3vyk7p3U3wBf+WIop6hDhkFzJhmLcqoAoGCCqGSM49\r\n"
 "AwEHoUQDQgAEV+WusXPf06y7k7iB/xKu7uZTrM5VU/Y0Dswu42MlC9+Y4vNcYDaW\r\n"
 "wNUYFHDlf5/VS0UY5bBs1Vz4lo+HcKPkxw==\r\n"
 "-----END EC PRIVATE KEY-----\r\n";
 
-const size_t test_ca_crt_ec_len  = sizeof( test_ca_crt_ec );
-const size_t test_ca_key_ec_len  = sizeof( test_ca_key_ec );
-const size_t test_ca_pwd_ec_len  = sizeof( test_ca_pwd_ec ) - 1;
-const size_t test_srv_crt_ec_len = sizeof( test_srv_crt_ec );
-const size_t test_srv_key_ec_len = sizeof( test_srv_key_ec );
-const size_t test_cli_crt_ec_len = sizeof( test_cli_crt_ec );
-const size_t test_cli_key_ec_len = sizeof( test_cli_key_ec );
+const size_t mbedtls_test_ca_crt_ec_len  = sizeof( mbedtls_test_ca_crt_ec );
+const size_t mbedtls_test_ca_key_ec_len  = sizeof( mbedtls_test_ca_key_ec );
+const size_t mbedtls_test_ca_pwd_ec_len  = sizeof( mbedtls_test_ca_pwd_ec ) - 1;
+const size_t mbedtls_test_srv_crt_ec_len = sizeof( mbedtls_test_srv_crt_ec );
+const size_t mbedtls_test_srv_key_ec_len = sizeof( mbedtls_test_srv_key_ec );
+const size_t mbedtls_test_cli_crt_ec_len = sizeof( mbedtls_test_cli_crt_ec );
+const size_t mbedtls_test_cli_key_ec_len = sizeof( mbedtls_test_cli_key_ec );
 #else
 #define TEST_CA_CRT_EC
-#endif /* POLARSSL_ECDSA_C */
+#endif /* MBEDTLS_ECDSA_C */
 
-#if defined(POLARSSL_RSA_C)
+#if defined(MBEDTLS_RSA_C)
 #define TEST_CA_CRT_RSA                                                 \
 "-----BEGIN CERTIFICATE-----\r\n"                                       \
 "MIIDhzCCAm+gAwIBAgIBADANBgkqhkiG9w0BAQUFADA7MQswCQYDVQQGEwJOTDER\r\n"  \
@@ -142,9 +142,9 @@
 "m/UTSLBNFNHesiTZeH31NcxYGdHSme9Nc/gfidRa0FLOCfWxRlFqAI47zG9jAQCZ\r\n"  \
 "7Z2mCGDNMhjQc+BYcdnl0lPXjdDK6V0qCg1dVewhUBcW5gZKzV7e9+DpVA==\r\n"      \
 "-----END CERTIFICATE-----\r\n"
-const char test_ca_crt_rsa[] = TEST_CA_CRT_RSA;
+const char mbedtls_test_ca_crt_rsa[] = TEST_CA_CRT_RSA;
 
-const char test_ca_key_rsa[] =
+const char mbedtls_test_ca_key_rsa[] =
 "-----BEGIN RSA PRIVATE KEY-----\r\n"
 "Proc-Type: 4,ENCRYPTED\r\n"
 "DEK-Info: DES-EDE3-CBC,A8A95B05D5B7206B\r\n"
@@ -176,9 +176,9 @@
 "P/eQiddSf0brnpiLJRh7qZrl9XuqYdpUqnoEdMAfotDOID8OtV7gt8a48ad8VPW2\r\n"
 "-----END RSA PRIVATE KEY-----\r\n";
 
-const char test_ca_pwd_rsa[] = "PolarSSLTest";
+const char mbedtls_test_ca_pwd_rsa[] = "PolarSSLTest";
 
-const char test_srv_crt_rsa[] =
+const char mbedtls_test_srv_crt_rsa[] =
 "-----BEGIN CERTIFICATE-----\r\n"
 "MIIDNzCCAh+gAwIBAgIBAjANBgkqhkiG9w0BAQUFADA7MQswCQYDVQQGEwJOTDER\r\n"
 "MA8GA1UEChMIUG9sYXJTU0wxGTAXBgNVBAMTEFBvbGFyU1NMIFRlc3QgQ0EwHhcN\r\n"
@@ -200,7 +200,7 @@
 "zhuYwjVuX6JHG0c=\r\n"
 "-----END CERTIFICATE-----\r\n";
 
-const char test_srv_key_rsa[] =
+const char mbedtls_test_srv_key_rsa[] =
 "-----BEGIN RSA PRIVATE KEY-----\r\n"
 "MIIEpAIBAAKCAQEAwU2j3efNHdEE10lyuJmsDnjkOjxKzzoTFtBa5M2jAIin7h5r\r\n"
 "lqdStJDvLXJ6PiSa/LY0rCT1d+AmZIycsCh9odrqjObJHJa8/sEEUrM21KP64bF2\r\n"
@@ -229,7 +229,7 @@
 "TB6l9VGoxJL4fyHnZb8L5gGvnB1bbD8cL6YPaDiOhcRseC9vBiEuVg==\r\n"
 "-----END RSA PRIVATE KEY-----\r\n";
 
-const char test_cli_crt_rsa[] =
+const char mbedtls_test_cli_crt_rsa[] =
 "-----BEGIN CERTIFICATE-----\r\n"
 "MIIDPzCCAiegAwIBAgIBBDANBgkqhkiG9w0BAQUFADA7MQswCQYDVQQGEwJOTDER\r\n"
 "MA8GA1UEChMIUG9sYXJTU0wxGTAXBgNVBAMTEFBvbGFyU1NMIFRlc3QgQ0EwHhcN\r\n"
@@ -251,7 +251,7 @@
 "D+stpAKiQLAWaAusIWKYEyw9MQ==\r\n"
 "-----END CERTIFICATE-----\r\n";
 
-const char test_cli_key_rsa[] =
+const char mbedtls_test_cli_key_rsa[] =
 "-----BEGIN RSA PRIVATE KEY-----\r\n"
 "MIIEpAIBAAKCAQEAyHTEzLn5tXnpRdkUYLB9u5Pyax6fM60Nj4o8VmXl3ETZzGaF\r\n"
 "B9X4J7BKNdBjngpuG7fa8H6r7gwQk4ZJGDTzqCrSV/Uu1C93KYRhTYJQj6eVSHD1\r\n"
@@ -280,83 +280,83 @@
 "8u4ytY0F+Vlanj5lm3TaoHSVF1+NWPyOTiwevIECGKwSxvlki4fDAA==\r\n"
 "-----END RSA PRIVATE KEY-----\r\n";
 
-const size_t test_ca_crt_rsa_len  = sizeof( test_ca_crt_rsa );
-const size_t test_ca_key_rsa_len  = sizeof( test_ca_key_rsa );
-const size_t test_ca_pwd_rsa_len  = sizeof( test_ca_pwd_rsa ) - 1;
-const size_t test_srv_crt_rsa_len = sizeof( test_srv_crt_rsa );
-const size_t test_srv_key_rsa_len = sizeof( test_srv_key_rsa );
-const size_t test_cli_crt_rsa_len = sizeof( test_cli_crt_rsa );
-const size_t test_cli_key_rsa_len = sizeof( test_cli_key_rsa );
+const size_t mbedtls_test_ca_crt_rsa_len  = sizeof( mbedtls_test_ca_crt_rsa );
+const size_t mbedtls_test_ca_key_rsa_len  = sizeof( mbedtls_test_ca_key_rsa );
+const size_t mbedtls_test_ca_pwd_rsa_len  = sizeof( mbedtls_test_ca_pwd_rsa ) - 1;
+const size_t mbedtls_test_srv_crt_rsa_len = sizeof( mbedtls_test_srv_crt_rsa );
+const size_t mbedtls_test_srv_key_rsa_len = sizeof( mbedtls_test_srv_key_rsa );
+const size_t mbedtls_test_cli_crt_rsa_len = sizeof( mbedtls_test_cli_crt_rsa );
+const size_t mbedtls_test_cli_key_rsa_len = sizeof( mbedtls_test_cli_key_rsa );
 #else
 #define TEST_CA_CRT_RSA
-#endif /* POLARSSL_RSA_C */
+#endif /* MBEDTLS_RSA_C */
 
-#if defined(POLARSSL_DHM_C)
-const char test_dhm_params[] =
+#if defined(MBEDTLS_DHM_C)
+const char mbedtls_test_dhm_params[] =
 "-----BEGIN DH PARAMETERS-----\r\n"
 "MIGHAoGBAJ419DBEOgmQTzo5qXl5fQcN9TN455wkOL7052HzxxRVMyhYmwQcgJvh\r\n"
 "1sa18fyfR9OiVEMYglOpkqVoGLN7qd5aQNNi5W7/C+VBdHTBJcGZJyyP5B3qcz32\r\n"
 "9mLJKudlVudV0Qxk5qUJaPZ/xupz0NyoVpviuiBOI1gNi8ovSXWzAgEC\r\n"
 "-----END DH PARAMETERS-----\r\n";
-const size_t test_dhm_params_len = sizeof( test_dhm_params );
+const size_t mbedtls_test_dhm_params_len = sizeof( mbedtls_test_dhm_params );
 #endif
 
-#if defined(POLARSSL_PEM_PARSE_C)
+#if defined(MBEDTLS_PEM_PARSE_C)
 /* Concatenation of all available CA certificates */
-const char test_cas_pem[] = TEST_CA_CRT_RSA TEST_CA_CRT_EC;
-const size_t test_cas_pem_len = sizeof( test_cas_pem );
+const char mbedtls_test_cas_pem[] = TEST_CA_CRT_RSA TEST_CA_CRT_EC;
+const size_t mbedtls_test_cas_pem_len = sizeof( mbedtls_test_cas_pem );
 #endif
 
 /* List of all available CA certificates */
-const char * test_cas[] = {
-#if defined(POLARSSL_RSA_C)
-    test_ca_crt_rsa,
+const char * mbedtls_test_cas[] = {
+#if defined(MBEDTLS_RSA_C)
+    mbedtls_test_ca_crt_rsa,
 #endif
-#if defined(POLARSSL_ECDSA_C)
-    test_ca_crt_ec,
+#if defined(MBEDTLS_ECDSA_C)
+    mbedtls_test_ca_crt_ec,
 #endif
     NULL
 };
-const size_t test_cas_len[] = {
-#if defined(POLARSSL_RSA_C)
-    sizeof( test_ca_crt_rsa_len ),
+const size_t mbedtls_test_cas_len[] = {
+#if defined(MBEDTLS_RSA_C)
+    sizeof( mbedtls_test_ca_crt_rsa_len ),
 #endif
-#if defined(POLARSSL_ECDSA_C)
-    sizeof( test_ca_crt_ec_len ),
+#if defined(MBEDTLS_ECDSA_C)
+    sizeof( mbedtls_test_ca_crt_ec_len ),
 #endif
     0
 };
 
-#if defined(POLARSSL_RSA_C)
-const char *test_ca_crt  = test_ca_crt_rsa;
-const char *test_ca_key  = test_ca_key_rsa;
-const char *test_ca_pwd  = test_ca_pwd_rsa;
-const char *test_srv_crt = test_srv_crt_rsa;
-const char *test_srv_key = test_srv_key_rsa;
-const char *test_cli_crt = test_cli_crt_rsa;
-const char *test_cli_key = test_cli_key_rsa;
-const size_t test_ca_crt_len  = sizeof( test_ca_crt_rsa );
-const size_t test_ca_key_len  = sizeof( test_ca_key_rsa );
-const size_t test_ca_pwd_len  = sizeof( test_ca_pwd_rsa ) - 1;
-const size_t test_srv_crt_len = sizeof( test_srv_crt_rsa );
-const size_t test_srv_key_len = sizeof( test_srv_key_rsa );
-const size_t test_cli_crt_len = sizeof( test_cli_crt_rsa );
-const size_t test_cli_key_len = sizeof( test_cli_key_rsa );
-#else /* ! POLARSSL_RSA_C, so POLARSSL_ECDSA_C */
-const char *test_ca_crt  = test_ca_crt_ec;
-const char *test_ca_key  = test_ca_key_ec;
-const char *test_ca_pwd  = test_ca_pwd_ec;
-const char *test_srv_crt = test_srv_crt_ec;
-const char *test_srv_key = test_srv_key_ec;
-const char *test_cli_crt = test_cli_crt_ec;
-const char *test_cli_key = test_cli_key_ec;
-const size_t test_ca_crt_len  = sizeof( test_ca_crt_ec );
-const size_t test_ca_key_len  = sizeof( test_ca_key_ec );
-const size_t test_ca_pwd_len  = sizeof( test_ca_pwd_ec ) - 1;
-const size_t test_srv_crt_len = sizeof( test_srv_crt_ec );
-const size_t test_srv_key_len = sizeof( test_srv_key_ec );
-const size_t test_cli_crt_len = sizeof( test_cli_crt_ec );
-const size_t test_cli_key_len = sizeof( test_cli_key_ec );
-#endif /* POLARSSL_RSA_C */
+#if defined(MBEDTLS_RSA_C)
+const char *mbedtls_test_ca_crt  = mbedtls_test_ca_crt_rsa;
+const char *mbedtls_test_ca_key  = mbedtls_test_ca_key_rsa;
+const char *mbedtls_test_ca_pwd  = mbedtls_test_ca_pwd_rsa;
+const char *mbedtls_test_srv_crt = mbedtls_test_srv_crt_rsa;
+const char *mbedtls_test_srv_key = mbedtls_test_srv_key_rsa;
+const char *mbedtls_test_cli_crt = mbedtls_test_cli_crt_rsa;
+const char *mbedtls_test_cli_key = mbedtls_test_cli_key_rsa;
+const size_t mbedtls_test_ca_crt_len  = sizeof( mbedtls_test_ca_crt_rsa );
+const size_t mbedtls_test_ca_key_len  = sizeof( mbedtls_test_ca_key_rsa );
+const size_t mbedtls_test_ca_pwd_len  = sizeof( mbedtls_test_ca_pwd_rsa ) - 1;
+const size_t mbedtls_test_srv_crt_len = sizeof( mbedtls_test_srv_crt_rsa );
+const size_t mbedtls_test_srv_key_len = sizeof( mbedtls_test_srv_key_rsa );
+const size_t mbedtls_test_cli_crt_len = sizeof( mbedtls_test_cli_crt_rsa );
+const size_t mbedtls_test_cli_key_len = sizeof( mbedtls_test_cli_key_rsa );
+#else /* ! MBEDTLS_RSA_C, so MBEDTLS_ECDSA_C */
+const char *mbedtls_test_ca_crt  = mbedtls_test_ca_crt_ec;
+const char *mbedtls_test_ca_key  = mbedtls_test_ca_key_ec;
+const char *mbedtls_test_ca_pwd  = mbedtls_test_ca_pwd_ec;
+const char *mbedtls_test_srv_crt = mbedtls_test_srv_crt_ec;
+const char *mbedtls_test_srv_key = mbedtls_test_srv_key_ec;
+const char *mbedtls_test_cli_crt = mbedtls_test_cli_crt_ec;
+const char *mbedtls_test_cli_key = mbedtls_test_cli_key_ec;
+const size_t mbedtls_test_ca_crt_len  = sizeof( mbedtls_test_ca_crt_ec );
+const size_t mbedtls_test_ca_key_len  = sizeof( mbedtls_test_ca_key_ec );
+const size_t mbedtls_test_ca_pwd_len  = sizeof( mbedtls_test_ca_pwd_ec ) - 1;
+const size_t mbedtls_test_srv_crt_len = sizeof( mbedtls_test_srv_crt_ec );
+const size_t mbedtls_test_srv_key_len = sizeof( mbedtls_test_srv_key_ec );
+const size_t mbedtls_test_cli_crt_len = sizeof( mbedtls_test_cli_crt_ec );
+const size_t mbedtls_test_cli_key_len = sizeof( mbedtls_test_cli_key_ec );
+#endif /* MBEDTLS_RSA_C */
 
-#endif /* POLARSSL_CERTS_C */
+#endif /* MBEDTLS_CERTS_C */
diff --git a/library/cipher.c b/library/cipher.c
index e04b5b6..d16893b 100644
--- a/library/cipher.c
+++ b/library/cipher.c
@@ -24,13 +24,13 @@
  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#if !defined(POLARSSL_CONFIG_FILE)
+#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 "mbedtls/cipher.h"
 #include "mbedtls/cipher_wrap.h"
@@ -38,16 +38,16 @@
 #include <stdlib.h>
 #include <string.h>
 
-#if defined(POLARSSL_GCM_C)
+#if defined(MBEDTLS_GCM_C)
 #include "mbedtls/gcm.h"
 #endif
 
-#if defined(POLARSSL_CCM_C)
+#if defined(MBEDTLS_CCM_C)
 #include "mbedtls/ccm.h"
 #endif
 
-#if defined(POLARSSL_ARC4_C) || defined(POLARSSL_CIPHER_NULL_CIPHER)
-#define POLARSSL_CIPHER_MODE_STREAM
+#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
+#define MBEDTLS_CIPHER_MODE_STREAM
 #endif
 
 #if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
@@ -56,21 +56,21 @@
 #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;
@@ -80,41 +80,41 @@
         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++ )
+    for( def = mbedtls_cipher_definitions; def->info != NULL; def++ )
         if( !  strcasecmp( def->info->name, cipher_name ) )
             return( def->info );
 
     return( NULL );
 }
 
-const cipher_info_t *cipher_info_from_values( const cipher_id_t cipher_id,
+const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values( const mbedtls_cipher_id_t cipher_id,
                                               int key_length,
-                                              const cipher_mode_t mode )
+                                              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->mode == mode )
@@ -123,12 +123,12 @@
     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;
@@ -136,45 +136,45 @@
     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_init_ctx( 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 */
+#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_length, 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 &&
+    if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
         (int) ctx->cipher_info->key_length != key_length )
     {
-        return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
     }
 
     ctx->key_length = key_length;
@@ -183,34 +183,34 @@
     /*
      * 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 );
     }
 
-    if( POLARSSL_DECRYPT == operation )
+    if( MBEDTLS_DECRYPT == operation )
         return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
                 ctx->key_length );
 
-    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
     {
@@ -218,7 +218,7 @@
 
         /* 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 );
@@ -227,49 +227,49 @@
     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;
 
@@ -282,33 +282,33 @@
         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 );
@@ -322,20 +322,20 @@
          */
         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;
@@ -347,9 +347,9 @@
          */
         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 );
@@ -374,10 +374,10 @@
 
         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,
@@ -390,10 +390,10 @@
 
         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,
@@ -406,10 +406,10 @@
 
         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 ) ) )
@@ -421,13 +421,13 @@
 
         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
  */
@@ -448,7 +448,7 @@
     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;
@@ -463,11 +463,11 @@
     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
  */
@@ -489,7 +489,7 @@
     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;
@@ -501,12 +501,12 @@
         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
  */
@@ -528,7 +528,7 @@
     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;
@@ -542,11 +542,11 @@
     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
  */
@@ -566,7 +566,7 @@
     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-- )
@@ -578,70 +578,70 @@
 
     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,
@@ -650,118 +650,118 @@
             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 );
@@ -772,19 +772,19 @@
             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 )
@@ -792,16 +792,16 @@
     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;
@@ -809,84 +809,84 @@
     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 */
+#endif /* MBEDTLS_CIPHER_MODE_AEAD */
 
-#endif /* POLARSSL_CIPHER_C */
+#endif /* MBEDTLS_CIPHER_C */
diff --git a/library/cipher_wrap.c b/library/cipher_wrap.c
index 4478cc8..ebc3c4f 100644
--- a/library/cipher_wrap.c
+++ b/library/cipher_wrap.c
@@ -24,164 +24,164 @@
  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#if !defined(POLARSSL_CONFIG_FILE)
+#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 "mbedtls/cipher_wrap.h"
 
-#if defined(POLARSSL_AES_C)
+#if defined(MBEDTLS_AES_C)
 #include "mbedtls/aes.h"
 #endif
 
-#if defined(POLARSSL_ARC4_C)
+#if defined(MBEDTLS_ARC4_C)
 #include "mbedtls/arc4.h"
 #endif
 
-#if defined(POLARSSL_CAMELLIA_C)
+#if defined(MBEDTLS_CAMELLIA_C)
 #include "mbedtls/camellia.h"
 #endif
 
-#if defined(POLARSSL_DES_C)
+#if defined(MBEDTLS_DES_C)
 #include "mbedtls/des.h"
 #endif
 
-#if defined(POLARSSL_BLOWFISH_C)
+#if defined(MBEDTLS_BLOWFISH_C)
 #include "mbedtls/blowfish.h"
 #endif
 
-#if defined(POLARSSL_GCM_C)
+#if defined(MBEDTLS_GCM_C)
 #include "mbedtls/gcm.h"
 #endif
 
-#if defined(POLARSSL_CCM_C)
+#if defined(MBEDTLS_CCM_C)
 #include "mbedtls/ccm.h"
 #endif
 
-#if defined(POLARSSL_CIPHER_NULL_CIPHER)
+#if defined(MBEDTLS_CIPHER_NULL_CIPHER)
 #include <string.h>
 #endif
 
-#if defined(POLARSSL_PLATFORM_C)
+#if defined(MBEDTLS_PLATFORM_C)
 #include "mbedtls/platform.h"
 #else
 #include <stdlib.h>
-#define polarssl_malloc     malloc
-#define polarssl_free       free
+#define mbedtls_malloc     malloc
+#define mbedtls_free       free
 #endif
 
-#if defined(POLARSSL_GCM_C)
+#if defined(MBEDTLS_GCM_C)
 /* shared by all GCM ciphers */
 static void *gcm_ctx_alloc( void )
 {
-    return polarssl_malloc( sizeof( gcm_context ) );
+    return mbedtls_malloc( sizeof( mbedtls_gcm_context ) );
 }
 
 static void gcm_ctx_free( void *ctx )
 {
-    gcm_free( ctx );
-    polarssl_free( ctx );
+    mbedtls_gcm_free( ctx );
+    mbedtls_free( ctx );
 }
-#endif /* POLARSSL_GCM_C */
+#endif /* MBEDTLS_GCM_C */
 
-#if defined(POLARSSL_CCM_C)
+#if defined(MBEDTLS_CCM_C)
 /* shared by all CCM ciphers */
 static void *ccm_ctx_alloc( void )
 {
-    return polarssl_malloc( sizeof( ccm_context ) );
+    return mbedtls_malloc( sizeof( mbedtls_ccm_context ) );
 }
 
 static void ccm_ctx_free( void *ctx )
 {
-    ccm_free( ctx );
-    polarssl_free( ctx );
+    mbedtls_ccm_free( ctx );
+    mbedtls_free( ctx );
 }
-#endif /* POLARSSL_CCM_C */
+#endif /* MBEDTLS_CCM_C */
 
-#if defined(POLARSSL_AES_C)
+#if defined(MBEDTLS_AES_C)
 
-static int aes_crypt_ecb_wrap( void *ctx, operation_t operation,
+static int aes_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
         const unsigned char *input, unsigned char *output )
 {
-    return aes_crypt_ecb( (aes_context *) ctx, operation, input, output );
+    return mbedtls_aes_crypt_ecb( (mbedtls_aes_context *) ctx, operation, input, output );
 }
 
-#if defined(POLARSSL_CIPHER_MODE_CBC)
-static int aes_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
+static int aes_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
         unsigned char *iv, const unsigned char *input, unsigned char *output )
 {
-    return aes_crypt_cbc( (aes_context *) ctx, operation, length, iv, input,
+    return mbedtls_aes_crypt_cbc( (mbedtls_aes_context *) ctx, operation, length, iv, input,
                           output );
 }
-#endif /* POLARSSL_CIPHER_MODE_CBC */
+#endif /* MBEDTLS_CIPHER_MODE_CBC */
 
-#if defined(POLARSSL_CIPHER_MODE_CFB)
-static int aes_crypt_cfb128_wrap( void *ctx, operation_t operation,
+#if defined(MBEDTLS_CIPHER_MODE_CFB)
+static int aes_crypt_cfb128_wrap( void *ctx, mbedtls_operation_t operation,
         size_t length, size_t *iv_off, unsigned char *iv,
         const unsigned char *input, unsigned char *output )
 {
-    return aes_crypt_cfb128( (aes_context *) ctx, operation, length, iv_off, iv,
+    return mbedtls_aes_crypt_cfb128( (mbedtls_aes_context *) ctx, operation, length, iv_off, iv,
                              input, output );
 }
-#endif /* POLARSSL_CIPHER_MODE_CFB */
+#endif /* MBEDTLS_CIPHER_MODE_CFB */
 
-#if defined(POLARSSL_CIPHER_MODE_CTR)
+#if defined(MBEDTLS_CIPHER_MODE_CTR)
 static int aes_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
         unsigned char *nonce_counter, unsigned char *stream_block,
         const unsigned char *input, unsigned char *output )
 {
-    return aes_crypt_ctr( (aes_context *) ctx, length, nc_off, nonce_counter,
+    return mbedtls_aes_crypt_ctr( (mbedtls_aes_context *) ctx, length, nc_off, nonce_counter,
                           stream_block, input, output );
 }
-#endif /* POLARSSL_CIPHER_MODE_CTR */
+#endif /* MBEDTLS_CIPHER_MODE_CTR */
 
 static int aes_setkey_dec_wrap( void *ctx, const unsigned char *key,
                                 unsigned int key_length )
 {
-    return aes_setkey_dec( (aes_context *) ctx, key, key_length );
+    return mbedtls_aes_setkey_dec( (mbedtls_aes_context *) ctx, key, key_length );
 }
 
 static int aes_setkey_enc_wrap( void *ctx, const unsigned char *key,
                                 unsigned int key_length )
 {
-    return aes_setkey_enc( (aes_context *) ctx, key, key_length );
+    return mbedtls_aes_setkey_enc( (mbedtls_aes_context *) ctx, key, key_length );
 }
 
 static void * aes_ctx_alloc( void )
 {
-    aes_context *aes = polarssl_malloc( sizeof( aes_context ) );
+    mbedtls_aes_context *aes = mbedtls_malloc( sizeof( mbedtls_aes_context ) );
 
     if( aes == NULL )
         return( NULL );
 
-    aes_init( aes );
+    mbedtls_aes_init( aes );
 
     return( aes );
 }
 
 static void aes_ctx_free( void *ctx )
 {
-    aes_free( (aes_context *) ctx );
-    polarssl_free( ctx );
+    mbedtls_aes_free( (mbedtls_aes_context *) ctx );
+    mbedtls_free( ctx );
 }
 
-static const cipher_base_t aes_info = {
-    POLARSSL_CIPHER_ID_AES,
+static const mbedtls_cipher_base_t aes_info = {
+    MBEDTLS_CIPHER_ID_AES,
     aes_crypt_ecb_wrap,
-#if defined(POLARSSL_CIPHER_MODE_CBC)
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
     aes_crypt_cbc_wrap,
 #endif
-#if defined(POLARSSL_CIPHER_MODE_CFB)
+#if defined(MBEDTLS_CIPHER_MODE_CFB)
     aes_crypt_cfb128_wrap,
 #endif
-#if defined(POLARSSL_CIPHER_MODE_CTR)
+#if defined(MBEDTLS_CIPHER_MODE_CTR)
     aes_crypt_ctr_wrap,
 #endif
-#if defined(POLARSSL_CIPHER_MODE_STREAM)
+#if defined(MBEDTLS_CIPHER_MODE_STREAM)
     NULL,
 #endif
     aes_setkey_enc_wrap,
@@ -190,9 +190,9 @@
     aes_ctx_free
 };
 
-static const cipher_info_t aes_128_ecb_info = {
-    POLARSSL_CIPHER_AES_128_ECB,
-    POLARSSL_MODE_ECB,
+static const mbedtls_cipher_info_t aes_128_ecb_info = {
+    MBEDTLS_CIPHER_AES_128_ECB,
+    MBEDTLS_MODE_ECB,
     128,
     "AES-128-ECB",
     16,
@@ -201,9 +201,9 @@
     &aes_info
 };
 
-static const cipher_info_t aes_192_ecb_info = {
-    POLARSSL_CIPHER_AES_192_ECB,
-    POLARSSL_MODE_ECB,
+static const mbedtls_cipher_info_t aes_192_ecb_info = {
+    MBEDTLS_CIPHER_AES_192_ECB,
+    MBEDTLS_MODE_ECB,
     192,
     "AES-192-ECB",
     16,
@@ -212,9 +212,9 @@
     &aes_info
 };
 
-static const cipher_info_t aes_256_ecb_info = {
-    POLARSSL_CIPHER_AES_256_ECB,
-    POLARSSL_MODE_ECB,
+static const mbedtls_cipher_info_t aes_256_ecb_info = {
+    MBEDTLS_CIPHER_AES_256_ECB,
+    MBEDTLS_MODE_ECB,
     256,
     "AES-256-ECB",
     16,
@@ -223,10 +223,10 @@
     &aes_info
 };
 
-#if defined(POLARSSL_CIPHER_MODE_CBC)
-static const cipher_info_t aes_128_cbc_info = {
-    POLARSSL_CIPHER_AES_128_CBC,
-    POLARSSL_MODE_CBC,
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
+static const mbedtls_cipher_info_t aes_128_cbc_info = {
+    MBEDTLS_CIPHER_AES_128_CBC,
+    MBEDTLS_MODE_CBC,
     128,
     "AES-128-CBC",
     16,
@@ -235,9 +235,9 @@
     &aes_info
 };
 
-static const cipher_info_t aes_192_cbc_info = {
-    POLARSSL_CIPHER_AES_192_CBC,
-    POLARSSL_MODE_CBC,
+static const mbedtls_cipher_info_t aes_192_cbc_info = {
+    MBEDTLS_CIPHER_AES_192_CBC,
+    MBEDTLS_MODE_CBC,
     192,
     "AES-192-CBC",
     16,
@@ -246,9 +246,9 @@
     &aes_info
 };
 
-static const cipher_info_t aes_256_cbc_info = {
-    POLARSSL_CIPHER_AES_256_CBC,
-    POLARSSL_MODE_CBC,
+static const mbedtls_cipher_info_t aes_256_cbc_info = {
+    MBEDTLS_CIPHER_AES_256_CBC,
+    MBEDTLS_MODE_CBC,
     256,
     "AES-256-CBC",
     16,
@@ -256,12 +256,12 @@
     16,
     &aes_info
 };
-#endif /* POLARSSL_CIPHER_MODE_CBC */
+#endif /* MBEDTLS_CIPHER_MODE_CBC */
 
-#if defined(POLARSSL_CIPHER_MODE_CFB)
-static const cipher_info_t aes_128_cfb128_info = {
-    POLARSSL_CIPHER_AES_128_CFB128,
-    POLARSSL_MODE_CFB,
+#if defined(MBEDTLS_CIPHER_MODE_CFB)
+static const mbedtls_cipher_info_t aes_128_cfb128_info = {
+    MBEDTLS_CIPHER_AES_128_CFB128,
+    MBEDTLS_MODE_CFB,
     128,
     "AES-128-CFB128",
     16,
@@ -270,9 +270,9 @@
     &aes_info
 };
 
-static const cipher_info_t aes_192_cfb128_info = {
-    POLARSSL_CIPHER_AES_192_CFB128,
-    POLARSSL_MODE_CFB,
+static const mbedtls_cipher_info_t aes_192_cfb128_info = {
+    MBEDTLS_CIPHER_AES_192_CFB128,
+    MBEDTLS_MODE_CFB,
     192,
     "AES-192-CFB128",
     16,
@@ -281,9 +281,9 @@
     &aes_info
 };
 
-static const cipher_info_t aes_256_cfb128_info = {
-    POLARSSL_CIPHER_AES_256_CFB128,
-    POLARSSL_MODE_CFB,
+static const mbedtls_cipher_info_t aes_256_cfb128_info = {
+    MBEDTLS_CIPHER_AES_256_CFB128,
+    MBEDTLS_MODE_CFB,
     256,
     "AES-256-CFB128",
     16,
@@ -291,12 +291,12 @@
     16,
     &aes_info
 };
-#endif /* POLARSSL_CIPHER_MODE_CFB */
+#endif /* MBEDTLS_CIPHER_MODE_CFB */
 
-#if defined(POLARSSL_CIPHER_MODE_CTR)
-static const cipher_info_t aes_128_ctr_info = {
-    POLARSSL_CIPHER_AES_128_CTR,
-    POLARSSL_MODE_CTR,
+#if defined(MBEDTLS_CIPHER_MODE_CTR)
+static const mbedtls_cipher_info_t aes_128_ctr_info = {
+    MBEDTLS_CIPHER_AES_128_CTR,
+    MBEDTLS_MODE_CTR,
     128,
     "AES-128-CTR",
     16,
@@ -305,9 +305,9 @@
     &aes_info
 };
 
-static const cipher_info_t aes_192_ctr_info = {
-    POLARSSL_CIPHER_AES_192_CTR,
-    POLARSSL_MODE_CTR,
+static const mbedtls_cipher_info_t aes_192_ctr_info = {
+    MBEDTLS_CIPHER_AES_192_CTR,
+    MBEDTLS_MODE_CTR,
     192,
     "AES-192-CTR",
     16,
@@ -316,9 +316,9 @@
     &aes_info
 };
 
-static const cipher_info_t aes_256_ctr_info = {
-    POLARSSL_CIPHER_AES_256_CTR,
-    POLARSSL_MODE_CTR,
+static const mbedtls_cipher_info_t aes_256_ctr_info = {
+    MBEDTLS_CIPHER_AES_256_CTR,
+    MBEDTLS_MODE_CTR,
     256,
     "AES-256-CTR",
     16,
@@ -326,29 +326,29 @@
     16,
     &aes_info
 };
-#endif /* POLARSSL_CIPHER_MODE_CTR */
+#endif /* MBEDTLS_CIPHER_MODE_CTR */
 
-#if defined(POLARSSL_GCM_C)
+#if defined(MBEDTLS_GCM_C)
 static int gcm_aes_setkey_wrap( void *ctx, const unsigned char *key,
                                 unsigned int key_length )
 {
-    return gcm_init( (gcm_context *) ctx, POLARSSL_CIPHER_ID_AES,
+    return mbedtls_gcm_init( (mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
                      key, key_length );
 }
 
-static const cipher_base_t gcm_aes_info = {
-    POLARSSL_CIPHER_ID_AES,
+static const mbedtls_cipher_base_t gcm_aes_info = {
+    MBEDTLS_CIPHER_ID_AES,
     NULL,
-#if defined(POLARSSL_CIPHER_MODE_CBC)
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
     NULL,
 #endif
-#if defined(POLARSSL_CIPHER_MODE_CFB)
+#if defined(MBEDTLS_CIPHER_MODE_CFB)
     NULL,
 #endif
-#if defined(POLARSSL_CIPHER_MODE_CTR)
+#if defined(MBEDTLS_CIPHER_MODE_CTR)
     NULL,
 #endif
-#if defined(POLARSSL_CIPHER_MODE_STREAM)
+#if defined(MBEDTLS_CIPHER_MODE_STREAM)
     NULL,
 #endif
     gcm_aes_setkey_wrap,
@@ -357,61 +357,61 @@
     gcm_ctx_free,
 };
 
-static const cipher_info_t aes_128_gcm_info = {
-    POLARSSL_CIPHER_AES_128_GCM,
-    POLARSSL_MODE_GCM,
+static const mbedtls_cipher_info_t aes_128_gcm_info = {
+    MBEDTLS_CIPHER_AES_128_GCM,
+    MBEDTLS_MODE_GCM,
     128,
     "AES-128-GCM",
     12,
-    POLARSSL_CIPHER_VARIABLE_IV_LEN,
+    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
     16,
     &gcm_aes_info
 };
 
-static const cipher_info_t aes_192_gcm_info = {
-    POLARSSL_CIPHER_AES_192_GCM,
-    POLARSSL_MODE_GCM,
+static const mbedtls_cipher_info_t aes_192_gcm_info = {
+    MBEDTLS_CIPHER_AES_192_GCM,
+    MBEDTLS_MODE_GCM,
     192,
     "AES-192-GCM",
     12,
-    POLARSSL_CIPHER_VARIABLE_IV_LEN,
+    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
     16,
     &gcm_aes_info
 };
 
-static const cipher_info_t aes_256_gcm_info = {
-    POLARSSL_CIPHER_AES_256_GCM,
-    POLARSSL_MODE_GCM,
+static const mbedtls_cipher_info_t aes_256_gcm_info = {
+    MBEDTLS_CIPHER_AES_256_GCM,
+    MBEDTLS_MODE_GCM,
     256,
     "AES-256-GCM",
     12,
-    POLARSSL_CIPHER_VARIABLE_IV_LEN,
+    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
     16,
     &gcm_aes_info
 };
-#endif /* POLARSSL_GCM_C */
+#endif /* MBEDTLS_GCM_C */
 
-#if defined(POLARSSL_CCM_C)
+#if defined(MBEDTLS_CCM_C)
 static int ccm_aes_setkey_wrap( void *ctx, const unsigned char *key,
                                 unsigned int key_length )
 {
-    return ccm_init( (ccm_context *) ctx, POLARSSL_CIPHER_ID_AES,
+    return mbedtls_ccm_init( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
                      key, key_length );
 }
 
-static const cipher_base_t ccm_aes_info = {
-    POLARSSL_CIPHER_ID_AES,
+static const mbedtls_cipher_base_t ccm_aes_info = {
+    MBEDTLS_CIPHER_ID_AES,
     NULL,
-#if defined(POLARSSL_CIPHER_MODE_CBC)
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
     NULL,
 #endif
-#if defined(POLARSSL_CIPHER_MODE_CFB)
+#if defined(MBEDTLS_CIPHER_MODE_CFB)
     NULL,
 #endif
-#if defined(POLARSSL_CIPHER_MODE_CTR)
+#if defined(MBEDTLS_CIPHER_MODE_CTR)
     NULL,
 #endif
-#if defined(POLARSSL_CIPHER_MODE_STREAM)
+#if defined(MBEDTLS_CIPHER_MODE_STREAM)
     NULL,
 #endif
     ccm_aes_setkey_wrap,
@@ -420,125 +420,125 @@
     ccm_ctx_free,
 };
 
-static const cipher_info_t aes_128_ccm_info = {
-    POLARSSL_CIPHER_AES_128_CCM,
-    POLARSSL_MODE_CCM,
+static const mbedtls_cipher_info_t aes_128_ccm_info = {
+    MBEDTLS_CIPHER_AES_128_CCM,
+    MBEDTLS_MODE_CCM,
     128,
     "AES-128-CCM",
     12,
-    POLARSSL_CIPHER_VARIABLE_IV_LEN,
+    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
     16,
     &ccm_aes_info
 };
 
-static const cipher_info_t aes_192_ccm_info = {
-    POLARSSL_CIPHER_AES_192_CCM,
-    POLARSSL_MODE_CCM,
+static const mbedtls_cipher_info_t aes_192_ccm_info = {
+    MBEDTLS_CIPHER_AES_192_CCM,
+    MBEDTLS_MODE_CCM,
     192,
     "AES-192-CCM",
     12,
-    POLARSSL_CIPHER_VARIABLE_IV_LEN,
+    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
     16,
     &ccm_aes_info
 };
 
-static const cipher_info_t aes_256_ccm_info = {
-    POLARSSL_CIPHER_AES_256_CCM,
-    POLARSSL_MODE_CCM,
+static const mbedtls_cipher_info_t aes_256_ccm_info = {
+    MBEDTLS_CIPHER_AES_256_CCM,
+    MBEDTLS_MODE_CCM,
     256,
     "AES-256-CCM",
     12,
-    POLARSSL_CIPHER_VARIABLE_IV_LEN,
+    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
     16,
     &ccm_aes_info
 };
-#endif /* POLARSSL_CCM_C */
+#endif /* MBEDTLS_CCM_C */
 
-#endif /* POLARSSL_AES_C */
+#endif /* MBEDTLS_AES_C */
 
-#if defined(POLARSSL_CAMELLIA_C)
+#if defined(MBEDTLS_CAMELLIA_C)
 
-static int camellia_crypt_ecb_wrap( void *ctx, operation_t operation,
+static int camellia_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
         const unsigned char *input, unsigned char *output )
 {
-    return camellia_crypt_ecb( (camellia_context *) ctx, operation, input,
+    return mbedtls_camellia_crypt_ecb( (mbedtls_camellia_context *) ctx, operation, input,
                                output );
 }
 
-#if defined(POLARSSL_CIPHER_MODE_CBC)
-static int camellia_crypt_cbc_wrap( void *ctx, operation_t operation,
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
+static int camellia_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation,
         size_t length, unsigned char *iv,
         const unsigned char *input, unsigned char *output )
 {
-    return camellia_crypt_cbc( (camellia_context *) ctx, operation, length, iv,
+    return mbedtls_camellia_crypt_cbc( (mbedtls_camellia_context *) ctx, operation, length, iv,
                                input, output );
 }
-#endif /* POLARSSL_CIPHER_MODE_CBC */
+#endif /* MBEDTLS_CIPHER_MODE_CBC */
 
-#if defined(POLARSSL_CIPHER_MODE_CFB)
-static int camellia_crypt_cfb128_wrap( void *ctx, operation_t operation,
+#if defined(MBEDTLS_CIPHER_MODE_CFB)
+static int camellia_crypt_cfb128_wrap( void *ctx, mbedtls_operation_t operation,
         size_t length, size_t *iv_off, unsigned char *iv,
         const unsigned char *input, unsigned char *output )
 {
-    return camellia_crypt_cfb128( (camellia_context *) ctx, operation, length,
+    return mbedtls_camellia_crypt_cfb128( (mbedtls_camellia_context *) ctx, operation, length,
                                   iv_off, iv, input, output );
 }
-#endif /* POLARSSL_CIPHER_MODE_CFB */
+#endif /* MBEDTLS_CIPHER_MODE_CFB */
 
-#if defined(POLARSSL_CIPHER_MODE_CTR)
+#if defined(MBEDTLS_CIPHER_MODE_CTR)
 static int camellia_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
         unsigned char *nonce_counter, unsigned char *stream_block,
         const unsigned char *input, unsigned char *output )
 {
-    return camellia_crypt_ctr( (camellia_context *) ctx, length, nc_off,
+    return mbedtls_camellia_crypt_ctr( (mbedtls_camellia_context *) ctx, length, nc_off,
                                nonce_counter, stream_block, input, output );
 }
-#endif /* POLARSSL_CIPHER_MODE_CTR */
+#endif /* MBEDTLS_CIPHER_MODE_CTR */
 
 static int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key,
                                      unsigned int key_length )
 {
-    return camellia_setkey_dec( (camellia_context *) ctx, key, key_length );
+    return mbedtls_camellia_setkey_dec( (mbedtls_camellia_context *) ctx, key, key_length );
 }
 
 static int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key,
                                      unsigned int key_length )
 {
-    return camellia_setkey_enc( (camellia_context *) ctx, key, key_length );
+    return mbedtls_camellia_setkey_enc( (mbedtls_camellia_context *) ctx, key, key_length );
 }
 
 static void * camellia_ctx_alloc( void )
 {
-    camellia_context *ctx;
-    ctx = polarssl_malloc( sizeof( camellia_context ) );
+    mbedtls_camellia_context *ctx;
+    ctx = mbedtls_malloc( sizeof( mbedtls_camellia_context ) );
 
     if( ctx == NULL )
         return( NULL );
 
-    camellia_init( ctx );
+    mbedtls_camellia_init( ctx );
 
     return( ctx );
 }
 
 static void camellia_ctx_free( void *ctx )
 {
-    camellia_free( (camellia_context *) ctx );
-    polarssl_free( ctx );
+    mbedtls_camellia_free( (mbedtls_camellia_context *) ctx );
+    mbedtls_free( ctx );
 }
 
-static const cipher_base_t camellia_info = {
-    POLARSSL_CIPHER_ID_CAMELLIA,
+static const mbedtls_cipher_base_t camellia_info = {
+    MBEDTLS_CIPHER_ID_CAMELLIA,
     camellia_crypt_ecb_wrap,
-#if defined(POLARSSL_CIPHER_MODE_CBC)
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
     camellia_crypt_cbc_wrap,
 #endif
-#if defined(POLARSSL_CIPHER_MODE_CFB)
+#if defined(MBEDTLS_CIPHER_MODE_CFB)
     camellia_crypt_cfb128_wrap,
 #endif
-#if defined(POLARSSL_CIPHER_MODE_CTR)
+#if defined(MBEDTLS_CIPHER_MODE_CTR)
     camellia_crypt_ctr_wrap,
 #endif
-#if defined(POLARSSL_CIPHER_MODE_STREAM)
+#if defined(MBEDTLS_CIPHER_MODE_STREAM)
     NULL,
 #endif
     camellia_setkey_enc_wrap,
@@ -547,9 +547,9 @@
     camellia_ctx_free
 };
 
-static const cipher_info_t camellia_128_ecb_info = {
-    POLARSSL_CIPHER_CAMELLIA_128_ECB,
-    POLARSSL_MODE_ECB,
+static const mbedtls_cipher_info_t camellia_128_ecb_info = {
+    MBEDTLS_CIPHER_CAMELLIA_128_ECB,
+    MBEDTLS_MODE_ECB,
     128,
     "CAMELLIA-128-ECB",
     16,
@@ -558,9 +558,9 @@
     &camellia_info
 };
 
-static const cipher_info_t camellia_192_ecb_info = {
-    POLARSSL_CIPHER_CAMELLIA_192_ECB,
-    POLARSSL_MODE_ECB,
+static const mbedtls_cipher_info_t camellia_192_ecb_info = {
+    MBEDTLS_CIPHER_CAMELLIA_192_ECB,
+    MBEDTLS_MODE_ECB,
     192,
     "CAMELLIA-192-ECB",
     16,
@@ -569,9 +569,9 @@
     &camellia_info
 };
 
-static const cipher_info_t camellia_256_ecb_info = {
-    POLARSSL_CIPHER_CAMELLIA_256_ECB,
-    POLARSSL_MODE_ECB,
+static const mbedtls_cipher_info_t camellia_256_ecb_info = {
+    MBEDTLS_CIPHER_CAMELLIA_256_ECB,
+    MBEDTLS_MODE_ECB,
     256,
     "CAMELLIA-256-ECB",
     16,
@@ -580,10 +580,10 @@
     &camellia_info
 };
 
-#if defined(POLARSSL_CIPHER_MODE_CBC)
-static const cipher_info_t camellia_128_cbc_info = {
-    POLARSSL_CIPHER_CAMELLIA_128_CBC,
-    POLARSSL_MODE_CBC,
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
+static const mbedtls_cipher_info_t camellia_128_cbc_info = {
+    MBEDTLS_CIPHER_CAMELLIA_128_CBC,
+    MBEDTLS_MODE_CBC,
     128,
     "CAMELLIA-128-CBC",
     16,
@@ -592,9 +592,9 @@
     &camellia_info
 };
 
-static const cipher_info_t camellia_192_cbc_info = {
-    POLARSSL_CIPHER_CAMELLIA_192_CBC,
-    POLARSSL_MODE_CBC,
+static const mbedtls_cipher_info_t camellia_192_cbc_info = {
+    MBEDTLS_CIPHER_CAMELLIA_192_CBC,
+    MBEDTLS_MODE_CBC,
     192,
     "CAMELLIA-192-CBC",
     16,
@@ -603,9 +603,9 @@
     &camellia_info
 };
 
-static const cipher_info_t camellia_256_cbc_info = {
-    POLARSSL_CIPHER_CAMELLIA_256_CBC,
-    POLARSSL_MODE_CBC,
+static const mbedtls_cipher_info_t camellia_256_cbc_info = {
+    MBEDTLS_CIPHER_CAMELLIA_256_CBC,
+    MBEDTLS_MODE_CBC,
     256,
     "CAMELLIA-256-CBC",
     16,
@@ -613,12 +613,12 @@
     16,
     &camellia_info
 };
-#endif /* POLARSSL_CIPHER_MODE_CBC */
+#endif /* MBEDTLS_CIPHER_MODE_CBC */
 
-#if defined(POLARSSL_CIPHER_MODE_CFB)
-static const cipher_info_t camellia_128_cfb128_info = {
-    POLARSSL_CIPHER_CAMELLIA_128_CFB128,
-    POLARSSL_MODE_CFB,
+#if defined(MBEDTLS_CIPHER_MODE_CFB)
+static const mbedtls_cipher_info_t camellia_128_cfb128_info = {
+    MBEDTLS_CIPHER_CAMELLIA_128_CFB128,
+    MBEDTLS_MODE_CFB,
     128,
     "CAMELLIA-128-CFB128",
     16,
@@ -627,9 +627,9 @@
     &camellia_info
 };
 
-static const cipher_info_t camellia_192_cfb128_info = {
-    POLARSSL_CIPHER_CAMELLIA_192_CFB128,
-    POLARSSL_MODE_CFB,
+static const mbedtls_cipher_info_t camellia_192_cfb128_info = {
+    MBEDTLS_CIPHER_CAMELLIA_192_CFB128,
+    MBEDTLS_MODE_CFB,
     192,
     "CAMELLIA-192-CFB128",
     16,
@@ -638,9 +638,9 @@
     &camellia_info
 };
 
-static const cipher_info_t camellia_256_cfb128_info = {
-    POLARSSL_CIPHER_CAMELLIA_256_CFB128,
-    POLARSSL_MODE_CFB,
+static const mbedtls_cipher_info_t camellia_256_cfb128_info = {
+    MBEDTLS_CIPHER_CAMELLIA_256_CFB128,
+    MBEDTLS_MODE_CFB,
     256,
     "CAMELLIA-256-CFB128",
     16,
@@ -648,12 +648,12 @@
     16,
     &camellia_info
 };
-#endif /* POLARSSL_CIPHER_MODE_CFB */
+#endif /* MBEDTLS_CIPHER_MODE_CFB */
 
-#if defined(POLARSSL_CIPHER_MODE_CTR)
-static const cipher_info_t camellia_128_ctr_info = {
-    POLARSSL_CIPHER_CAMELLIA_128_CTR,
-    POLARSSL_MODE_CTR,
+#if defined(MBEDTLS_CIPHER_MODE_CTR)
+static const mbedtls_cipher_info_t camellia_128_ctr_info = {
+    MBEDTLS_CIPHER_CAMELLIA_128_CTR,
+    MBEDTLS_MODE_CTR,
     128,
     "CAMELLIA-128-CTR",
     16,
@@ -662,9 +662,9 @@
     &camellia_info
 };
 
-static const cipher_info_t camellia_192_ctr_info = {
-    POLARSSL_CIPHER_CAMELLIA_192_CTR,
-    POLARSSL_MODE_CTR,
+static const mbedtls_cipher_info_t camellia_192_ctr_info = {
+    MBEDTLS_CIPHER_CAMELLIA_192_CTR,
+    MBEDTLS_MODE_CTR,
     192,
     "CAMELLIA-192-CTR",
     16,
@@ -673,9 +673,9 @@
     &camellia_info
 };
 
-static const cipher_info_t camellia_256_ctr_info = {
-    POLARSSL_CIPHER_CAMELLIA_256_CTR,
-    POLARSSL_MODE_CTR,
+static const mbedtls_cipher_info_t camellia_256_ctr_info = {
+    MBEDTLS_CIPHER_CAMELLIA_256_CTR,
+    MBEDTLS_MODE_CTR,
     256,
     "CAMELLIA-256-CTR",
     16,
@@ -683,29 +683,29 @@
     16,
     &camellia_info
 };
-#endif /* POLARSSL_CIPHER_MODE_CTR */
+#endif /* MBEDTLS_CIPHER_MODE_CTR */
 
-#if defined(POLARSSL_GCM_C)
+#if defined(MBEDTLS_GCM_C)
 static int gcm_camellia_setkey_wrap( void *ctx, const unsigned char *key,
                                      unsigned int key_length )
 {
-    return gcm_init( (gcm_context *) ctx, POLARSSL_CIPHER_ID_CAMELLIA,
+    return mbedtls_gcm_init( (mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
                      key, key_length );
 }
 
-static const cipher_base_t gcm_camellia_info = {
-    POLARSSL_CIPHER_ID_CAMELLIA,
+static const mbedtls_cipher_base_t gcm_camellia_info = {
+    MBEDTLS_CIPHER_ID_CAMELLIA,
     NULL,
-#if defined(POLARSSL_CIPHER_MODE_CBC)
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
     NULL,
 #endif
-#if defined(POLARSSL_CIPHER_MODE_CFB)
+#if defined(MBEDTLS_CIPHER_MODE_CFB)
     NULL,
 #endif
-#if defined(POLARSSL_CIPHER_MODE_CTR)
+#if defined(MBEDTLS_CIPHER_MODE_CTR)
     NULL,
 #endif
-#if defined(POLARSSL_CIPHER_MODE_STREAM)
+#if defined(MBEDTLS_CIPHER_MODE_STREAM)
     NULL,
 #endif
     gcm_camellia_setkey_wrap,
@@ -714,61 +714,61 @@
     gcm_ctx_free,
 };
 
-static const cipher_info_t camellia_128_gcm_info = {
-    POLARSSL_CIPHER_CAMELLIA_128_GCM,
-    POLARSSL_MODE_GCM,
+static const mbedtls_cipher_info_t camellia_128_gcm_info = {
+    MBEDTLS_CIPHER_CAMELLIA_128_GCM,
+    MBEDTLS_MODE_GCM,
     128,
     "CAMELLIA-128-GCM",
     12,
-    POLARSSL_CIPHER_VARIABLE_IV_LEN,
+    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
     16,
     &gcm_camellia_info
 };
 
-static const cipher_info_t camellia_192_gcm_info = {
-    POLARSSL_CIPHER_CAMELLIA_192_GCM,
-    POLARSSL_MODE_GCM,
+static const mbedtls_cipher_info_t camellia_192_gcm_info = {
+    MBEDTLS_CIPHER_CAMELLIA_192_GCM,
+    MBEDTLS_MODE_GCM,
     192,
     "CAMELLIA-192-GCM",
     12,
-    POLARSSL_CIPHER_VARIABLE_IV_LEN,
+    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
     16,
     &gcm_camellia_info
 };
 
-static const cipher_info_t camellia_256_gcm_info = {
-    POLARSSL_CIPHER_CAMELLIA_256_GCM,
-    POLARSSL_MODE_GCM,
+static const mbedtls_cipher_info_t camellia_256_gcm_info = {
+    MBEDTLS_CIPHER_CAMELLIA_256_GCM,
+    MBEDTLS_MODE_GCM,
     256,
     "CAMELLIA-256-GCM",
     12,
-    POLARSSL_CIPHER_VARIABLE_IV_LEN,
+    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
     16,
     &gcm_camellia_info
 };
-#endif /* POLARSSL_GCM_C */
+#endif /* MBEDTLS_GCM_C */
 
-#if defined(POLARSSL_CCM_C)
+#if defined(MBEDTLS_CCM_C)
 static int ccm_camellia_setkey_wrap( void *ctx, const unsigned char *key,
                                      unsigned int key_length )
 {
-    return ccm_init( (ccm_context *) ctx, POLARSSL_CIPHER_ID_CAMELLIA,
+    return mbedtls_ccm_init( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
                      key, key_length );
 }
 
-static const cipher_base_t ccm_camellia_info = {
-    POLARSSL_CIPHER_ID_CAMELLIA,
+static const mbedtls_cipher_base_t ccm_camellia_info = {
+    MBEDTLS_CIPHER_ID_CAMELLIA,
     NULL,
-#if defined(POLARSSL_CIPHER_MODE_CBC)
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
     NULL,
 #endif
-#if defined(POLARSSL_CIPHER_MODE_CFB)
+#if defined(MBEDTLS_CIPHER_MODE_CFB)
     NULL,
 #endif
-#if defined(POLARSSL_CIPHER_MODE_CTR)
+#if defined(MBEDTLS_CIPHER_MODE_CTR)
     NULL,
 #endif
-#if defined(POLARSSL_CIPHER_MODE_STREAM)
+#if defined(MBEDTLS_CIPHER_MODE_STREAM)
     NULL,
 #endif
     ccm_camellia_setkey_wrap,
@@ -777,82 +777,82 @@
     ccm_ctx_free,
 };
 
-static const cipher_info_t camellia_128_ccm_info = {
-    POLARSSL_CIPHER_CAMELLIA_128_CCM,
-    POLARSSL_MODE_CCM,
+static const mbedtls_cipher_info_t camellia_128_ccm_info = {
+    MBEDTLS_CIPHER_CAMELLIA_128_CCM,
+    MBEDTLS_MODE_CCM,
     128,
     "CAMELLIA-128-CCM",
     12,
-    POLARSSL_CIPHER_VARIABLE_IV_LEN,
+    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
     16,
     &ccm_camellia_info
 };
 
-static const cipher_info_t camellia_192_ccm_info = {
-    POLARSSL_CIPHER_CAMELLIA_192_CCM,
-    POLARSSL_MODE_CCM,
+static const mbedtls_cipher_info_t camellia_192_ccm_info = {
+    MBEDTLS_CIPHER_CAMELLIA_192_CCM,
+    MBEDTLS_MODE_CCM,
     192,
     "CAMELLIA-192-CCM",
     12,
-    POLARSSL_CIPHER_VARIABLE_IV_LEN,
+    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
     16,
     &ccm_camellia_info
 };
 
-static const cipher_info_t camellia_256_ccm_info = {
-    POLARSSL_CIPHER_CAMELLIA_256_CCM,
-    POLARSSL_MODE_CCM,
+static const mbedtls_cipher_info_t camellia_256_ccm_info = {
+    MBEDTLS_CIPHER_CAMELLIA_256_CCM,
+    MBEDTLS_MODE_CCM,
     256,
     "CAMELLIA-256-CCM",
     12,
-    POLARSSL_CIPHER_VARIABLE_IV_LEN,
+    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
     16,
     &ccm_camellia_info
 };
-#endif /* POLARSSL_CCM_C */
+#endif /* MBEDTLS_CCM_C */
 
-#endif /* POLARSSL_CAMELLIA_C */
+#endif /* MBEDTLS_CAMELLIA_C */
 
-#if defined(POLARSSL_DES_C)
+#if defined(MBEDTLS_DES_C)
 
-static int des_crypt_ecb_wrap( void *ctx, operation_t operation,
+static int des_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
         const unsigned char *input, unsigned char *output )
 {
     ((void) operation);
-    return des_crypt_ecb( (des_context *) ctx, input, output );
+    return mbedtls_des_crypt_ecb( (mbedtls_des_context *) ctx, input, output );
 }
 
-static int des3_crypt_ecb_wrap( void *ctx, operation_t operation,
+static int des3_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
         const unsigned char *input, unsigned char *output )
 {
     ((void) operation);
-    return des3_crypt_ecb( (des3_context *) ctx, input, output );
+    return mbedtls_des3_crypt_ecb( (mbedtls_des3_context *) ctx, input, output );
 }
 
-#if defined(POLARSSL_CIPHER_MODE_CBC)
-static int des_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
+static int des_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
         unsigned char *iv, const unsigned char *input, unsigned char *output )
 {
-    return des_crypt_cbc( (des_context *) ctx, operation, length, iv, input,
+    return mbedtls_des_crypt_cbc( (mbedtls_des_context *) ctx, operation, length, iv, input,
                           output );
 }
-#endif /* POLARSSL_CIPHER_MODE_CBC */
+#endif /* MBEDTLS_CIPHER_MODE_CBC */
 
-#if defined(POLARSSL_CIPHER_MODE_CBC)
-static int des3_crypt_cbc_wrap( void *ctx, operation_t operation, size_t length,
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
+static int des3_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
         unsigned char *iv, const unsigned char *input, unsigned char *output )
 {
-    return des3_crypt_cbc( (des3_context *) ctx, operation, length, iv, input,
+    return mbedtls_des3_crypt_cbc( (mbedtls_des3_context *) ctx, operation, length, iv, input,
                            output );
 }
-#endif /* POLARSSL_CIPHER_MODE_CBC */
+#endif /* MBEDTLS_CIPHER_MODE_CBC */
 
 static int des_setkey_dec_wrap( void *ctx, const unsigned char *key,
                                 unsigned int key_length )
 {
     ((void) key_length);
 
-    return des_setkey_dec( (des_context *) ctx, key );
+    return mbedtls_des_setkey_dec( (mbedtls_des_context *) ctx, key );
 }
 
 static int des_setkey_enc_wrap( void *ctx, const unsigned char *key,
@@ -860,7 +860,7 @@
 {
     ((void) key_length);
 
-    return des_setkey_enc( (des_context *) ctx, key );
+    return mbedtls_des_setkey_enc( (mbedtls_des_context *) ctx, key );
 }
 
 static int des3_set2key_dec_wrap( void *ctx, const unsigned char *key,
@@ -868,7 +868,7 @@
 {
     ((void) key_length);
 
-    return des3_set2key_dec( (des3_context *) ctx, key );
+    return mbedtls_des3_set2key_dec( (mbedtls_des3_context *) ctx, key );
 }
 
 static int des3_set2key_enc_wrap( void *ctx, const unsigned char *key,
@@ -876,7 +876,7 @@
 {
     ((void) key_length);
 
-    return des3_set2key_enc( (des3_context *) ctx, key );
+    return mbedtls_des3_set2key_enc( (mbedtls_des3_context *) ctx, key );
 }
 
 static int des3_set3key_dec_wrap( void *ctx, const unsigned char *key,
@@ -884,7 +884,7 @@
 {
     ((void) key_length);
 
-    return des3_set3key_dec( (des3_context *) ctx, key );
+    return mbedtls_des3_set3key_dec( (mbedtls_des3_context *) ctx, key );
 }
 
 static int des3_set3key_enc_wrap( void *ctx, const unsigned char *key,
@@ -892,59 +892,59 @@
 {
     ((void) key_length);
 
-    return des3_set3key_enc( (des3_context *) ctx, key );
+    return mbedtls_des3_set3key_enc( (mbedtls_des3_context *) ctx, key );
 }
 
 static void * des_ctx_alloc( void )
 {
-    des_context *des = polarssl_malloc( sizeof( des_context ) );
+    mbedtls_des_context *des = mbedtls_malloc( sizeof( mbedtls_des_context ) );
 
     if( des == NULL )
         return( NULL );
 
-    des_init( des );
+    mbedtls_des_init( des );
 
     return( des );
 }
 
 static void des_ctx_free( void *ctx )
 {
-    des_free( (des_context *) ctx );
-    polarssl_free( ctx );
+    mbedtls_des_free( (mbedtls_des_context *) ctx );
+    mbedtls_free( ctx );
 }
 
 static void * des3_ctx_alloc( void )
 {
-    des3_context *des3;
-    des3 = polarssl_malloc( sizeof( des3_context ) );
+    mbedtls_des3_context *des3;
+    des3 = mbedtls_malloc( sizeof( mbedtls_des3_context ) );
 
     if( des3 == NULL )
         return( NULL );
 
-    des3_init( des3 );
+    mbedtls_des3_init( des3 );
 
     return( des3 );
 }
 
 static void des3_ctx_free( void *ctx )
 {
-    des3_free( (des3_context *) ctx );
-    polarssl_free( ctx );
+    mbedtls_des3_free( (mbedtls_des3_context *) ctx );
+    mbedtls_free( ctx );
 }
 
-static const cipher_base_t des_info = {
-    POLARSSL_CIPHER_ID_DES,
+static const mbedtls_cipher_base_t des_info = {
+    MBEDTLS_CIPHER_ID_DES,
     des_crypt_ecb_wrap,
-#if defined(POLARSSL_CIPHER_MODE_CBC)
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
     des_crypt_cbc_wrap,
 #endif
-#if defined(POLARSSL_CIPHER_MODE_CFB)
+#if defined(MBEDTLS_CIPHER_MODE_CFB)
     NULL,
 #endif
-#if defined(POLARSSL_CIPHER_MODE_CTR)
+#if defined(MBEDTLS_CIPHER_MODE_CTR)
     NULL,
 #endif
-#if defined(POLARSSL_CIPHER_MODE_STREAM)
+#if defined(MBEDTLS_CIPHER_MODE_STREAM)
     NULL,
 #endif
     des_setkey_enc_wrap,
@@ -953,10 +953,10 @@
     des_ctx_free
 };
 
-static const cipher_info_t des_ecb_info = {
-    POLARSSL_CIPHER_DES_ECB,
-    POLARSSL_MODE_ECB,
-    POLARSSL_KEY_LENGTH_DES,
+static const mbedtls_cipher_info_t des_ecb_info = {
+    MBEDTLS_CIPHER_DES_ECB,
+    MBEDTLS_MODE_ECB,
+    MBEDTLS_KEY_LENGTH_DES,
     "DES-ECB",
     8,
     0,
@@ -964,32 +964,32 @@
     &des_info
 };
 
-#if defined(POLARSSL_CIPHER_MODE_CBC)
-static const cipher_info_t des_cbc_info = {
-    POLARSSL_CIPHER_DES_CBC,
-    POLARSSL_MODE_CBC,
-    POLARSSL_KEY_LENGTH_DES,
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
+static const mbedtls_cipher_info_t des_cbc_info = {
+    MBEDTLS_CIPHER_DES_CBC,
+    MBEDTLS_MODE_CBC,
+    MBEDTLS_KEY_LENGTH_DES,
     "DES-CBC",
     8,
     0,
     8,
     &des_info
 };
-#endif /* POLARSSL_CIPHER_MODE_CBC */
+#endif /* MBEDTLS_CIPHER_MODE_CBC */
 
-static const cipher_base_t des_ede_info = {
-    POLARSSL_CIPHER_ID_DES,
+static const mbedtls_cipher_base_t des_ede_info = {
+    MBEDTLS_CIPHER_ID_DES,
     des3_crypt_ecb_wrap,
-#if defined(POLARSSL_CIPHER_MODE_CBC)
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
     des3_crypt_cbc_wrap,
 #endif
-#if defined(POLARSSL_CIPHER_MODE_CFB)
+#if defined(MBEDTLS_CIPHER_MODE_CFB)
     NULL,
 #endif
-#if defined(POLARSSL_CIPHER_MODE_CTR)
+#if defined(MBEDTLS_CIPHER_MODE_CTR)
     NULL,
 #endif
-#if defined(POLARSSL_CIPHER_MODE_STREAM)
+#if defined(MBEDTLS_CIPHER_MODE_STREAM)
     NULL,
 #endif
     des3_set2key_enc_wrap,
@@ -998,10 +998,10 @@
     des3_ctx_free
 };
 
-static const cipher_info_t des_ede_ecb_info = {
-    POLARSSL_CIPHER_DES_EDE_ECB,
-    POLARSSL_MODE_ECB,
-    POLARSSL_KEY_LENGTH_DES_EDE,
+static const mbedtls_cipher_info_t des_ede_ecb_info = {
+    MBEDTLS_CIPHER_DES_EDE_ECB,
+    MBEDTLS_MODE_ECB,
+    MBEDTLS_KEY_LENGTH_DES_EDE,
     "DES-EDE-ECB",
     8,
     0,
@@ -1009,32 +1009,32 @@
     &des_ede_info
 };
 
-#if defined(POLARSSL_CIPHER_MODE_CBC)
-static const cipher_info_t des_ede_cbc_info = {
-    POLARSSL_CIPHER_DES_EDE_CBC,
-    POLARSSL_MODE_CBC,
-    POLARSSL_KEY_LENGTH_DES_EDE,
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
+static const mbedtls_cipher_info_t des_ede_cbc_info = {
+    MBEDTLS_CIPHER_DES_EDE_CBC,
+    MBEDTLS_MODE_CBC,
+    MBEDTLS_KEY_LENGTH_DES_EDE,
     "DES-EDE-CBC",
     8,
     0,
     8,
     &des_ede_info
 };
-#endif /* POLARSSL_CIPHER_MODE_CBC */
+#endif /* MBEDTLS_CIPHER_MODE_CBC */
 
-static const cipher_base_t des_ede3_info = {
-    POLARSSL_CIPHER_ID_DES,
+static const mbedtls_cipher_base_t des_ede3_info = {
+    MBEDTLS_CIPHER_ID_DES,
     des3_crypt_ecb_wrap,
-#if defined(POLARSSL_CIPHER_MODE_CBC)
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
     des3_crypt_cbc_wrap,
 #endif
-#if defined(POLARSSL_CIPHER_MODE_CFB)
+#if defined(MBEDTLS_CIPHER_MODE_CFB)
     NULL,
 #endif
-#if defined(POLARSSL_CIPHER_MODE_CTR)
+#if defined(MBEDTLS_CIPHER_MODE_CTR)
     NULL,
 #endif
-#if defined(POLARSSL_CIPHER_MODE_STREAM)
+#if defined(MBEDTLS_CIPHER_MODE_STREAM)
     NULL,
 #endif
     des3_set3key_enc_wrap,
@@ -1043,107 +1043,107 @@
     des3_ctx_free
 };
 
-static const cipher_info_t des_ede3_ecb_info = {
-    POLARSSL_CIPHER_DES_EDE3_ECB,
-    POLARSSL_MODE_ECB,
-    POLARSSL_KEY_LENGTH_DES_EDE3,
+static const mbedtls_cipher_info_t des_ede3_ecb_info = {
+    MBEDTLS_CIPHER_DES_EDE3_ECB,
+    MBEDTLS_MODE_ECB,
+    MBEDTLS_KEY_LENGTH_DES_EDE3,
     "DES-EDE3-ECB",
     8,
     0,
     8,
     &des_ede3_info
 };
-#if defined(POLARSSL_CIPHER_MODE_CBC)
-static const cipher_info_t des_ede3_cbc_info = {
-    POLARSSL_CIPHER_DES_EDE3_CBC,
-    POLARSSL_MODE_CBC,
-    POLARSSL_KEY_LENGTH_DES_EDE3,
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
+static const mbedtls_cipher_info_t des_ede3_cbc_info = {
+    MBEDTLS_CIPHER_DES_EDE3_CBC,
+    MBEDTLS_MODE_CBC,
+    MBEDTLS_KEY_LENGTH_DES_EDE3,
     "DES-EDE3-CBC",
     8,
     0,
     8,
     &des_ede3_info
 };
-#endif /* POLARSSL_CIPHER_MODE_CBC */
-#endif /* POLARSSL_DES_C */
+#endif /* MBEDTLS_CIPHER_MODE_CBC */
+#endif /* MBEDTLS_DES_C */
 
-#if defined(POLARSSL_BLOWFISH_C)
+#if defined(MBEDTLS_BLOWFISH_C)
 
-static int blowfish_crypt_ecb_wrap( void *ctx, operation_t operation,
+static int blowfish_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
         const unsigned char *input, unsigned char *output )
 {
-    return blowfish_crypt_ecb( (blowfish_context *) ctx, operation, input,
+    return mbedtls_blowfish_crypt_ecb( (mbedtls_blowfish_context *) ctx, operation, input,
                                output );
 }
 
-#if defined(POLARSSL_CIPHER_MODE_CBC)
-static int blowfish_crypt_cbc_wrap( void *ctx, operation_t operation,
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
+static int blowfish_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation,
         size_t length, unsigned char *iv, const unsigned char *input,
         unsigned char *output )
 {
-    return blowfish_crypt_cbc( (blowfish_context *) ctx, operation, length, iv,
+    return mbedtls_blowfish_crypt_cbc( (mbedtls_blowfish_context *) ctx, operation, length, iv,
                                input, output );
 }
-#endif /* POLARSSL_CIPHER_MODE_CBC */
+#endif /* MBEDTLS_CIPHER_MODE_CBC */
 
-#if defined(POLARSSL_CIPHER_MODE_CFB)
-static int blowfish_crypt_cfb64_wrap( void *ctx, operation_t operation,
+#if defined(MBEDTLS_CIPHER_MODE_CFB)
+static int blowfish_crypt_cfb64_wrap( void *ctx, mbedtls_operation_t operation,
         size_t length, size_t *iv_off, unsigned char *iv,
         const unsigned char *input, unsigned char *output )
 {
-    return blowfish_crypt_cfb64( (blowfish_context *) ctx, operation, length,
+    return mbedtls_blowfish_crypt_cfb64( (mbedtls_blowfish_context *) ctx, operation, length,
                                  iv_off, iv, input, output );
 }
-#endif /* POLARSSL_CIPHER_MODE_CFB */
+#endif /* MBEDTLS_CIPHER_MODE_CFB */
 
-#if defined(POLARSSL_CIPHER_MODE_CTR)
+#if defined(MBEDTLS_CIPHER_MODE_CTR)
 static int blowfish_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
         unsigned char *nonce_counter, unsigned char *stream_block,
         const unsigned char *input, unsigned char *output )
 {
-    return blowfish_crypt_ctr( (blowfish_context *) ctx, length, nc_off,
+    return mbedtls_blowfish_crypt_ctr( (mbedtls_blowfish_context *) ctx, length, nc_off,
                                nonce_counter, stream_block, input, output );
 }
-#endif /* POLARSSL_CIPHER_MODE_CTR */
+#endif /* MBEDTLS_CIPHER_MODE_CTR */
 
 static int blowfish_setkey_wrap( void *ctx, const unsigned char *key,
                                  unsigned int key_length )
 {
-    return blowfish_setkey( (blowfish_context *) ctx, key, key_length );
+    return mbedtls_blowfish_setkey( (mbedtls_blowfish_context *) ctx, key, key_length );
 }
 
 static void * blowfish_ctx_alloc( void )
 {
-    blowfish_context *ctx;
-    ctx = polarssl_malloc( sizeof( blowfish_context ) );
+    mbedtls_blowfish_context *ctx;
+    ctx = mbedtls_malloc( sizeof( mbedtls_blowfish_context ) );
 
     if( ctx == NULL )
         return( NULL );
 
-    blowfish_init( ctx );
+    mbedtls_blowfish_init( ctx );
 
     return( ctx );
 }
 
 static void blowfish_ctx_free( void *ctx )
 {
-    blowfish_free( (blowfish_context *) ctx );
-    polarssl_free( ctx );
+    mbedtls_blowfish_free( (mbedtls_blowfish_context *) ctx );
+    mbedtls_free( ctx );
 }
 
-static const cipher_base_t blowfish_info = {
-    POLARSSL_CIPHER_ID_BLOWFISH,
+static const mbedtls_cipher_base_t blowfish_info = {
+    MBEDTLS_CIPHER_ID_BLOWFISH,
     blowfish_crypt_ecb_wrap,
-#if defined(POLARSSL_CIPHER_MODE_CBC)
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
     blowfish_crypt_cbc_wrap,
 #endif
-#if defined(POLARSSL_CIPHER_MODE_CFB)
+#if defined(MBEDTLS_CIPHER_MODE_CFB)
     blowfish_crypt_cfb64_wrap,
 #endif
-#if defined(POLARSSL_CIPHER_MODE_CTR)
+#if defined(MBEDTLS_CIPHER_MODE_CTR)
     blowfish_crypt_ctr_wrap,
 #endif
-#if defined(POLARSSL_CIPHER_MODE_STREAM)
+#if defined(MBEDTLS_CIPHER_MODE_STREAM)
     NULL,
 #endif
     blowfish_setkey_wrap,
@@ -1152,63 +1152,63 @@
     blowfish_ctx_free
 };
 
-static const cipher_info_t blowfish_ecb_info = {
-    POLARSSL_CIPHER_BLOWFISH_ECB,
-    POLARSSL_MODE_ECB,
+static const mbedtls_cipher_info_t blowfish_ecb_info = {
+    MBEDTLS_CIPHER_BLOWFISH_ECB,
+    MBEDTLS_MODE_ECB,
     128,
     "BLOWFISH-ECB",
     8,
-    POLARSSL_CIPHER_VARIABLE_KEY_LEN,
+    MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
     8,
     &blowfish_info
 };
 
-#if defined(POLARSSL_CIPHER_MODE_CBC)
-static const cipher_info_t blowfish_cbc_info = {
-    POLARSSL_CIPHER_BLOWFISH_CBC,
-    POLARSSL_MODE_CBC,
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
+static const mbedtls_cipher_info_t blowfish_cbc_info = {
+    MBEDTLS_CIPHER_BLOWFISH_CBC,
+    MBEDTLS_MODE_CBC,
     128,
     "BLOWFISH-CBC",
     8,
-    POLARSSL_CIPHER_VARIABLE_KEY_LEN,
+    MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
     8,
     &blowfish_info
 };
-#endif /* POLARSSL_CIPHER_MODE_CBC */
+#endif /* MBEDTLS_CIPHER_MODE_CBC */
 
-#if defined(POLARSSL_CIPHER_MODE_CFB)
-static const cipher_info_t blowfish_cfb64_info = {
-    POLARSSL_CIPHER_BLOWFISH_CFB64,
-    POLARSSL_MODE_CFB,
+#if defined(MBEDTLS_CIPHER_MODE_CFB)
+static const mbedtls_cipher_info_t blowfish_cfb64_info = {
+    MBEDTLS_CIPHER_BLOWFISH_CFB64,
+    MBEDTLS_MODE_CFB,
     128,
     "BLOWFISH-CFB64",
     8,
-    POLARSSL_CIPHER_VARIABLE_KEY_LEN,
+    MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
     8,
     &blowfish_info
 };
-#endif /* POLARSSL_CIPHER_MODE_CFB */
+#endif /* MBEDTLS_CIPHER_MODE_CFB */
 
-#if defined(POLARSSL_CIPHER_MODE_CTR)
-static const cipher_info_t blowfish_ctr_info = {
-    POLARSSL_CIPHER_BLOWFISH_CTR,
-    POLARSSL_MODE_CTR,
+#if defined(MBEDTLS_CIPHER_MODE_CTR)
+static const mbedtls_cipher_info_t blowfish_ctr_info = {
+    MBEDTLS_CIPHER_BLOWFISH_CTR,
+    MBEDTLS_MODE_CTR,
     128,
     "BLOWFISH-CTR",
     8,
-    POLARSSL_CIPHER_VARIABLE_KEY_LEN,
+    MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
     8,
     &blowfish_info
 };
-#endif /* POLARSSL_CIPHER_MODE_CTR */
-#endif /* POLARSSL_BLOWFISH_C */
+#endif /* MBEDTLS_CIPHER_MODE_CTR */
+#endif /* MBEDTLS_BLOWFISH_C */
 
-#if defined(POLARSSL_ARC4_C)
+#if defined(MBEDTLS_ARC4_C)
 static int arc4_crypt_stream_wrap( void *ctx, size_t length,
                                    const unsigned char *input,
                                    unsigned char *output )
 {
-    return( arc4_crypt( (arc4_context *) ctx, length, input, output ) );
+    return( mbedtls_arc4_crypt( (mbedtls_arc4_context *) ctx, length, input, output ) );
 }
 
 static int arc4_setkey_wrap( void *ctx, const unsigned char *key,
@@ -1216,44 +1216,44 @@
 {
     /* we get key_length in bits, arc4 expects it in bytes */
     if( key_length % 8 != 0 )
-        return( POLARSSL_ERR_CIPHER_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
 
-    arc4_setup( (arc4_context *) ctx, key, key_length / 8 );
+    mbedtls_arc4_setup( (mbedtls_arc4_context *) ctx, key, key_length / 8 );
     return( 0 );
 }
 
 static void * arc4_ctx_alloc( void )
 {
-    arc4_context *ctx;
-    ctx = polarssl_malloc( sizeof( arc4_context ) );
+    mbedtls_arc4_context *ctx;
+    ctx = mbedtls_malloc( sizeof( mbedtls_arc4_context ) );
 
     if( ctx == NULL )
         return( NULL );
 
-    arc4_init( ctx );
+    mbedtls_arc4_init( ctx );
 
     return( ctx );
 }
 
 static void arc4_ctx_free( void *ctx )
 {
-    arc4_free( (arc4_context *) ctx );
-    polarssl_free( ctx );
+    mbedtls_arc4_free( (mbedtls_arc4_context *) ctx );
+    mbedtls_free( ctx );
 }
 
-static const cipher_base_t arc4_base_info = {
-    POLARSSL_CIPHER_ID_ARC4,
+static const mbedtls_cipher_base_t arc4_base_info = {
+    MBEDTLS_CIPHER_ID_ARC4,
     NULL,
-#if defined(POLARSSL_CIPHER_MODE_CBC)
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
     NULL,
 #endif
-#if defined(POLARSSL_CIPHER_MODE_CFB)
+#if defined(MBEDTLS_CIPHER_MODE_CFB)
     NULL,
 #endif
-#if defined(POLARSSL_CIPHER_MODE_CTR)
+#if defined(MBEDTLS_CIPHER_MODE_CTR)
     NULL,
 #endif
-#if defined(POLARSSL_CIPHER_MODE_STREAM)
+#if defined(MBEDTLS_CIPHER_MODE_STREAM)
     arc4_crypt_stream_wrap,
 #endif
     arc4_setkey_wrap,
@@ -1262,9 +1262,9 @@
     arc4_ctx_free
 };
 
-static const cipher_info_t arc4_128_info = {
-    POLARSSL_CIPHER_ARC4_128,
-    POLARSSL_MODE_STREAM,
+static const mbedtls_cipher_info_t arc4_128_info = {
+    MBEDTLS_CIPHER_ARC4_128,
+    MBEDTLS_MODE_STREAM,
     128,
     "ARC4-128",
     0,
@@ -1272,9 +1272,9 @@
     1,
     &arc4_base_info
 };
-#endif /* POLARSSL_ARC4_C */
+#endif /* MBEDTLS_ARC4_C */
 
-#if defined(POLARSSL_CIPHER_NULL_CIPHER)
+#if defined(MBEDTLS_CIPHER_NULL_CIPHER)
 static int null_crypt_stream( void *ctx, size_t length,
                               const unsigned char *input,
                               unsigned char *output )
@@ -1304,19 +1304,19 @@
     ((void) ctx);
 }
 
-static const cipher_base_t null_base_info = {
-    POLARSSL_CIPHER_ID_NULL,
+static const mbedtls_cipher_base_t null_base_info = {
+    MBEDTLS_CIPHER_ID_NULL,
     NULL,
-#if defined(POLARSSL_CIPHER_MODE_CBC)
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
     NULL,
 #endif
-#if defined(POLARSSL_CIPHER_MODE_CFB)
+#if defined(MBEDTLS_CIPHER_MODE_CFB)
     NULL,
 #endif
-#if defined(POLARSSL_CIPHER_MODE_CTR)
+#if defined(MBEDTLS_CIPHER_MODE_CTR)
     NULL,
 #endif
-#if defined(POLARSSL_CIPHER_MODE_STREAM)
+#if defined(MBEDTLS_CIPHER_MODE_STREAM)
     null_crypt_stream,
 #endif
     null_setkey,
@@ -1325,9 +1325,9 @@
     null_ctx_free
 };
 
-static const cipher_info_t null_cipher_info = {
-    POLARSSL_CIPHER_NULL,
-    POLARSSL_MODE_STREAM,
+static const mbedtls_cipher_info_t null_cipher_info = {
+    MBEDTLS_CIPHER_NULL,
+    MBEDTLS_MODE_STREAM,
     0,
     "NULL",
     0,
@@ -1335,108 +1335,108 @@
     1,
     &null_base_info
 };
-#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
+#endif /* defined(MBEDTLS_CIPHER_NULL_CIPHER) */
 
-const cipher_definition_t cipher_definitions[] =
+const mbedtls_cipher_definition_t mbedtls_cipher_definitions[] =
 {
-#if defined(POLARSSL_AES_C)
-    { POLARSSL_CIPHER_AES_128_ECB,          &aes_128_ecb_info },
-    { POLARSSL_CIPHER_AES_192_ECB,          &aes_192_ecb_info },
-    { POLARSSL_CIPHER_AES_256_ECB,          &aes_256_ecb_info },
-#if defined(POLARSSL_CIPHER_MODE_CBC)
-    { POLARSSL_CIPHER_AES_128_CBC,          &aes_128_cbc_info },
-    { POLARSSL_CIPHER_AES_192_CBC,          &aes_192_cbc_info },
-    { POLARSSL_CIPHER_AES_256_CBC,          &aes_256_cbc_info },
+#if defined(MBEDTLS_AES_C)
+    { MBEDTLS_CIPHER_AES_128_ECB,          &aes_128_ecb_info },
+    { MBEDTLS_CIPHER_AES_192_ECB,          &aes_192_ecb_info },
+    { MBEDTLS_CIPHER_AES_256_ECB,          &aes_256_ecb_info },
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
+    { MBEDTLS_CIPHER_AES_128_CBC,          &aes_128_cbc_info },
+    { MBEDTLS_CIPHER_AES_192_CBC,          &aes_192_cbc_info },
+    { MBEDTLS_CIPHER_AES_256_CBC,          &aes_256_cbc_info },
 #endif
-#if defined(POLARSSL_CIPHER_MODE_CFB)
-    { POLARSSL_CIPHER_AES_128_CFB128,       &aes_128_cfb128_info },
-    { POLARSSL_CIPHER_AES_192_CFB128,       &aes_192_cfb128_info },
-    { POLARSSL_CIPHER_AES_256_CFB128,       &aes_256_cfb128_info },
+#if defined(MBEDTLS_CIPHER_MODE_CFB)
+    { MBEDTLS_CIPHER_AES_128_CFB128,       &aes_128_cfb128_info },
+    { MBEDTLS_CIPHER_AES_192_CFB128,       &aes_192_cfb128_info },
+    { MBEDTLS_CIPHER_AES_256_CFB128,       &aes_256_cfb128_info },
 #endif
-#if defined(POLARSSL_CIPHER_MODE_CTR)
-    { POLARSSL_CIPHER_AES_128_CTR,          &aes_128_ctr_info },
-    { POLARSSL_CIPHER_AES_192_CTR,          &aes_192_ctr_info },
-    { POLARSSL_CIPHER_AES_256_CTR,          &aes_256_ctr_info },
+#if defined(MBEDTLS_CIPHER_MODE_CTR)
+    { MBEDTLS_CIPHER_AES_128_CTR,          &aes_128_ctr_info },
+    { MBEDTLS_CIPHER_AES_192_CTR,          &aes_192_ctr_info },
+    { MBEDTLS_CIPHER_AES_256_CTR,          &aes_256_ctr_info },
 #endif
-#if defined(POLARSSL_GCM_C)
-    { POLARSSL_CIPHER_AES_128_GCM,          &aes_128_gcm_info },
-    { POLARSSL_CIPHER_AES_192_GCM,          &aes_192_gcm_info },
-    { POLARSSL_CIPHER_AES_256_GCM,          &aes_256_gcm_info },
+#if defined(MBEDTLS_GCM_C)
+    { MBEDTLS_CIPHER_AES_128_GCM,          &aes_128_gcm_info },
+    { MBEDTLS_CIPHER_AES_192_GCM,          &aes_192_gcm_info },
+    { MBEDTLS_CIPHER_AES_256_GCM,          &aes_256_gcm_info },
 #endif
-#if defined(POLARSSL_CCM_C)
-    { POLARSSL_CIPHER_AES_128_CCM,          &aes_128_ccm_info },
-    { POLARSSL_CIPHER_AES_192_CCM,          &aes_192_ccm_info },
-    { POLARSSL_CIPHER_AES_256_CCM,          &aes_256_ccm_info },
+#if defined(MBEDTLS_CCM_C)
+    { MBEDTLS_CIPHER_AES_128_CCM,          &aes_128_ccm_info },
+    { MBEDTLS_CIPHER_AES_192_CCM,          &aes_192_ccm_info },
+    { MBEDTLS_CIPHER_AES_256_CCM,          &aes_256_ccm_info },
 #endif
-#endif /* POLARSSL_AES_C */
+#endif /* MBEDTLS_AES_C */
 
-#if defined(POLARSSL_ARC4_C)
-    { POLARSSL_CIPHER_ARC4_128,             &arc4_128_info },
+#if defined(MBEDTLS_ARC4_C)
+    { MBEDTLS_CIPHER_ARC4_128,             &arc4_128_info },
 #endif
 
-#if defined(POLARSSL_BLOWFISH_C)
-    { POLARSSL_CIPHER_BLOWFISH_ECB,         &blowfish_ecb_info },
-#if defined(POLARSSL_CIPHER_MODE_CBC)
-    { POLARSSL_CIPHER_BLOWFISH_CBC,         &blowfish_cbc_info },
+#if defined(MBEDTLS_BLOWFISH_C)
+    { MBEDTLS_CIPHER_BLOWFISH_ECB,         &blowfish_ecb_info },
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
+    { MBEDTLS_CIPHER_BLOWFISH_CBC,         &blowfish_cbc_info },
 #endif
-#if defined(POLARSSL_CIPHER_MODE_CFB)
-    { POLARSSL_CIPHER_BLOWFISH_CFB64,       &blowfish_cfb64_info },
+#if defined(MBEDTLS_CIPHER_MODE_CFB)
+    { MBEDTLS_CIPHER_BLOWFISH_CFB64,       &blowfish_cfb64_info },
 #endif
-#if defined(POLARSSL_CIPHER_MODE_CTR)
-    { POLARSSL_CIPHER_BLOWFISH_CTR,         &blowfish_ctr_info },
+#if defined(MBEDTLS_CIPHER_MODE_CTR)
+    { MBEDTLS_CIPHER_BLOWFISH_CTR,         &blowfish_ctr_info },
 #endif
-#endif /* POLARSSL_BLOWFISH_C */
+#endif /* MBEDTLS_BLOWFISH_C */
 
-#if defined(POLARSSL_CAMELLIA_C)
-    { POLARSSL_CIPHER_CAMELLIA_128_ECB,     &camellia_128_ecb_info },
-    { POLARSSL_CIPHER_CAMELLIA_192_ECB,     &camellia_192_ecb_info },
-    { POLARSSL_CIPHER_CAMELLIA_256_ECB,     &camellia_256_ecb_info },
-#if defined(POLARSSL_CIPHER_MODE_CBC)
-    { POLARSSL_CIPHER_CAMELLIA_128_CBC,     &camellia_128_cbc_info },
-    { POLARSSL_CIPHER_CAMELLIA_192_CBC,     &camellia_192_cbc_info },
-    { POLARSSL_CIPHER_CAMELLIA_256_CBC,     &camellia_256_cbc_info },
+#if defined(MBEDTLS_CAMELLIA_C)
+    { MBEDTLS_CIPHER_CAMELLIA_128_ECB,     &camellia_128_ecb_info },
+    { MBEDTLS_CIPHER_CAMELLIA_192_ECB,     &camellia_192_ecb_info },
+    { MBEDTLS_CIPHER_CAMELLIA_256_ECB,     &camellia_256_ecb_info },
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
+    { MBEDTLS_CIPHER_CAMELLIA_128_CBC,     &camellia_128_cbc_info },
+    { MBEDTLS_CIPHER_CAMELLIA_192_CBC,     &camellia_192_cbc_info },
+    { MBEDTLS_CIPHER_CAMELLIA_256_CBC,     &camellia_256_cbc_info },
 #endif
-#if defined(POLARSSL_CIPHER_MODE_CFB)
-    { POLARSSL_CIPHER_CAMELLIA_128_CFB128,  &camellia_128_cfb128_info },
-    { POLARSSL_CIPHER_CAMELLIA_192_CFB128,  &camellia_192_cfb128_info },
-    { POLARSSL_CIPHER_CAMELLIA_256_CFB128,  &camellia_256_cfb128_info },
+#if defined(MBEDTLS_CIPHER_MODE_CFB)
+    { MBEDTLS_CIPHER_CAMELLIA_128_CFB128,  &camellia_128_cfb128_info },
+    { MBEDTLS_CIPHER_CAMELLIA_192_CFB128,  &camellia_192_cfb128_info },
+    { MBEDTLS_CIPHER_CAMELLIA_256_CFB128,  &camellia_256_cfb128_info },
 #endif
-#if defined(POLARSSL_CIPHER_MODE_CTR)
-    { POLARSSL_CIPHER_CAMELLIA_128_CTR,     &camellia_128_ctr_info },
-    { POLARSSL_CIPHER_CAMELLIA_192_CTR,     &camellia_192_ctr_info },
-    { POLARSSL_CIPHER_CAMELLIA_256_CTR,     &camellia_256_ctr_info },
+#if defined(MBEDTLS_CIPHER_MODE_CTR)
+    { MBEDTLS_CIPHER_CAMELLIA_128_CTR,     &camellia_128_ctr_info },
+    { MBEDTLS_CIPHER_CAMELLIA_192_CTR,     &camellia_192_ctr_info },
+    { MBEDTLS_CIPHER_CAMELLIA_256_CTR,     &camellia_256_ctr_info },
 #endif
-#if defined(POLARSSL_GCM_C)
-    { POLARSSL_CIPHER_CAMELLIA_128_GCM,     &camellia_128_gcm_info },
-    { POLARSSL_CIPHER_CAMELLIA_192_GCM,     &camellia_192_gcm_info },
-    { POLARSSL_CIPHER_CAMELLIA_256_GCM,     &camellia_256_gcm_info },
+#if defined(MBEDTLS_GCM_C)
+    { MBEDTLS_CIPHER_CAMELLIA_128_GCM,     &camellia_128_gcm_info },
+    { MBEDTLS_CIPHER_CAMELLIA_192_GCM,     &camellia_192_gcm_info },
+    { MBEDTLS_CIPHER_CAMELLIA_256_GCM,     &camellia_256_gcm_info },
 #endif
-#if defined(POLARSSL_CCM_C)
-    { POLARSSL_CIPHER_CAMELLIA_128_CCM,     &camellia_128_ccm_info },
-    { POLARSSL_CIPHER_CAMELLIA_192_CCM,     &camellia_192_ccm_info },
-    { POLARSSL_CIPHER_CAMELLIA_256_CCM,     &camellia_256_ccm_info },
+#if defined(MBEDTLS_CCM_C)
+    { MBEDTLS_CIPHER_CAMELLIA_128_CCM,     &camellia_128_ccm_info },
+    { MBEDTLS_CIPHER_CAMELLIA_192_CCM,     &camellia_192_ccm_info },
+    { MBEDTLS_CIPHER_CAMELLIA_256_CCM,     &camellia_256_ccm_info },
 #endif
-#endif /* POLARSSL_CAMELLIA_C */
+#endif /* MBEDTLS_CAMELLIA_C */
 
-#if defined(POLARSSL_DES_C)
-    { POLARSSL_CIPHER_DES_ECB,              &des_ecb_info },
-    { POLARSSL_CIPHER_DES_EDE_ECB,          &des_ede_ecb_info },
-    { POLARSSL_CIPHER_DES_EDE3_ECB,         &des_ede3_ecb_info },
-#if defined(POLARSSL_CIPHER_MODE_CBC)
-    { POLARSSL_CIPHER_DES_CBC,              &des_cbc_info },
-    { POLARSSL_CIPHER_DES_EDE_CBC,          &des_ede_cbc_info },
-    { POLARSSL_CIPHER_DES_EDE3_CBC,         &des_ede3_cbc_info },
+#if defined(MBEDTLS_DES_C)
+    { MBEDTLS_CIPHER_DES_ECB,              &des_ecb_info },
+    { MBEDTLS_CIPHER_DES_EDE_ECB,          &des_ede_ecb_info },
+    { MBEDTLS_CIPHER_DES_EDE3_ECB,         &des_ede3_ecb_info },
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
+    { MBEDTLS_CIPHER_DES_CBC,              &des_cbc_info },
+    { MBEDTLS_CIPHER_DES_EDE_CBC,          &des_ede_cbc_info },
+    { MBEDTLS_CIPHER_DES_EDE3_CBC,         &des_ede3_cbc_info },
 #endif
-#endif /* POLARSSL_DES_C */
+#endif /* MBEDTLS_DES_C */
 
-#if defined(POLARSSL_CIPHER_NULL_CIPHER)
-    { POLARSSL_CIPHER_NULL,                 &null_cipher_info },
-#endif /* POLARSSL_CIPHER_NULL_CIPHER */
+#if defined(MBEDTLS_CIPHER_NULL_CIPHER)
+    { MBEDTLS_CIPHER_NULL,                 &null_cipher_info },
+#endif /* MBEDTLS_CIPHER_NULL_CIPHER */
 
-    { POLARSSL_CIPHER_NONE, NULL }
+    { MBEDTLS_CIPHER_NONE, NULL }
 };
 
-#define NUM_CIPHERS sizeof cipher_definitions / sizeof cipher_definitions[0]
-int supported_ciphers[NUM_CIPHERS];
+#define NUM_CIPHERS sizeof mbedtls_cipher_definitions / sizeof mbedtls_cipher_definitions[0]
+int mbedtls_cipher_supported[NUM_CIPHERS];
 
-#endif /* POLARSSL_CIPHER_C */
+#endif /* MBEDTLS_CIPHER_C */
diff --git a/library/ctr_drbg.c b/library/ctr_drbg.c
index aee3833..355d0f4 100644
--- a/library/ctr_drbg.c
+++ b/library/ctr_drbg.c
@@ -25,33 +25,33 @@
  *  http://csrc.nist.gov/publications/nistpubs/800-90/SP800-90revised_March2007.pdf
  */
 
-#if !defined(POLARSSL_CONFIG_FILE)
+#if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else
-#include POLARSSL_CONFIG_FILE
+#include MBEDTLS_CONFIG_FILE
 #endif
 
-#if defined(POLARSSL_CTR_DRBG_C)
+#if defined(MBEDTLS_CTR_DRBG_C)
 
 #include "mbedtls/ctr_drbg.h"
 
 #include <string.h>
 
-#if defined(POLARSSL_FS_IO)
+#if defined(MBEDTLS_FS_IO)
 #include <stdio.h>
 #endif
 
-#if defined(POLARSSL_SELF_TEST)
-#if defined(POLARSSL_PLATFORM_C)
+#if defined(MBEDTLS_SELF_TEST)
+#if defined(MBEDTLS_PLATFORM_C)
 #include "mbedtls/platform.h"
 #else
 #include <stdio.h>
-#define polarssl_printf printf
-#endif /* POLARSSL_PLATFORM_C */
-#endif /* POLARSSL_SELF_TEST */
+#define mbedtls_printf printf
+#endif /* MBEDTLS_PLATFORM_C */
+#endif /* MBEDTLS_SELF_TEST */
 
 /* 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;
 }
 
@@ -59,8 +59,8 @@
  * Non-public function wrapped by ctr_crbg_init(). Necessary to allow NIST
  * tests to succeed (which require known length fixed entropy)
  */
-int ctr_drbg_init_entropy_len(
-                   ctr_drbg_context *ctx,
+int mbedtls_ctr_drbg_init_entropy_len(
+                   mbedtls_ctr_drbg_context *ctx,
                    int (*f_entropy)(void *, unsigned char *, size_t),
                    void *p_entropy,
                    const unsigned char *custom,
@@ -68,60 +68,60 @@
                    size_t entropy_len )
 {
     int ret;
-    unsigned char key[CTR_DRBG_KEYSIZE];
+    unsigned char key[MBEDTLS_CTR_DRBG_KEYSIZE];
 
-    memset( ctx, 0, sizeof(ctr_drbg_context) );
-    memset( key, 0, CTR_DRBG_KEYSIZE );
+    memset( ctx, 0, sizeof(mbedtls_ctr_drbg_context) );
+    memset( key, 0, MBEDTLS_CTR_DRBG_KEYSIZE );
 
-    aes_init( &ctx->aes_ctx );
+    mbedtls_aes_init( &ctx->aes_ctx );
 
     ctx->f_entropy = f_entropy;
     ctx->p_entropy = p_entropy;
 
     ctx->entropy_len = entropy_len;
-    ctx->reseed_interval = CTR_DRBG_RESEED_INTERVAL;
+    ctx->reseed_interval = MBEDTLS_CTR_DRBG_RESEED_INTERVAL;
 
     /*
      * Initialize with an empty key
      */
-    aes_setkey_enc( &ctx->aes_ctx, key, CTR_DRBG_KEYBITS );
+    mbedtls_aes_setkey_enc( &ctx->aes_ctx, key, MBEDTLS_CTR_DRBG_KEYBITS );
 
-    if( ( ret = ctr_drbg_reseed( ctx, custom, len ) ) != 0 )
+    if( ( ret = mbedtls_ctr_drbg_reseed( ctx, custom, len ) ) != 0 )
         return( ret );
 
     return( 0 );
 }
 
-int ctr_drbg_init( ctr_drbg_context *ctx,
+int mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx,
                    int (*f_entropy)(void *, unsigned char *, size_t),
                    void *p_entropy,
                    const unsigned char *custom,
                    size_t len )
 {
-    return( ctr_drbg_init_entropy_len( ctx, f_entropy, p_entropy, custom, len,
-                                       CTR_DRBG_ENTROPY_LEN ) );
+    return( mbedtls_ctr_drbg_init_entropy_len( ctx, f_entropy, p_entropy, custom, len,
+                                       MBEDTLS_CTR_DRBG_ENTROPY_LEN ) );
 }
 
-void ctr_drbg_free( ctr_drbg_context *ctx )
+void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx )
 {
     if( ctx == NULL )
         return;
 
-    aes_free( &ctx->aes_ctx );
-    polarssl_zeroize( ctx, sizeof( ctr_drbg_context ) );
+    mbedtls_aes_free( &ctx->aes_ctx );
+    mbedtls_zeroize( ctx, sizeof( mbedtls_ctr_drbg_context ) );
 }
 
-void ctr_drbg_set_prediction_resistance( ctr_drbg_context *ctx, int resistance )
+void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx, int resistance )
 {
     ctx->prediction_resistance = resistance;
 }
 
-void ctr_drbg_set_entropy_len( ctr_drbg_context *ctx, size_t len )
+void mbedtls_ctr_drbg_set_entropy_len( mbedtls_ctr_drbg_context *ctx, size_t len )
 {
     ctx->entropy_len = len;
 }
 
-void ctr_drbg_set_reseed_interval( ctr_drbg_context *ctx, int interval )
+void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx, int interval )
 {
     ctx->reseed_interval = interval;
 }
@@ -129,21 +129,21 @@
 static int block_cipher_df( unsigned char *output,
                             const unsigned char *data, size_t data_len )
 {
-    unsigned char buf[CTR_DRBG_MAX_SEED_INPUT + CTR_DRBG_BLOCKSIZE + 16];
-    unsigned char tmp[CTR_DRBG_SEEDLEN];
-    unsigned char key[CTR_DRBG_KEYSIZE];
-    unsigned char chain[CTR_DRBG_BLOCKSIZE];
+    unsigned char buf[MBEDTLS_CTR_DRBG_MAX_SEED_INPUT + MBEDTLS_CTR_DRBG_BLOCKSIZE + 16];
+    unsigned char tmp[MBEDTLS_CTR_DRBG_SEEDLEN];
+    unsigned char key[MBEDTLS_CTR_DRBG_KEYSIZE];
+    unsigned char chain[MBEDTLS_CTR_DRBG_BLOCKSIZE];
     unsigned char *p, *iv;
-    aes_context aes_ctx;
+    mbedtls_aes_context aes_ctx;
 
     int i, j;
     size_t buf_len, use_len;
 
-    if( data_len > CTR_DRBG_MAX_SEED_INPUT )
-        return( POLARSSL_ERR_CTR_DRBG_INPUT_TOO_BIG );
+    if( data_len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT )
+        return( MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG );
 
-    memset( buf, 0, CTR_DRBG_MAX_SEED_INPUT + CTR_DRBG_BLOCKSIZE + 16 );
-    aes_init( &aes_ctx );
+    memset( buf, 0, MBEDTLS_CTR_DRBG_MAX_SEED_INPUT + MBEDTLS_CTR_DRBG_BLOCKSIZE + 16 );
+    mbedtls_aes_init( &aes_ctx );
 
     /*
      * Construct IV (16 bytes) and S in buffer
@@ -152,44 +152,44 @@
      *     data || 0x80
      *     (Total is padded to a multiple of 16-bytes with zeroes)
      */
-    p = buf + CTR_DRBG_BLOCKSIZE;
+    p = buf + MBEDTLS_CTR_DRBG_BLOCKSIZE;
     *p++ = ( data_len >> 24 ) & 0xff;
     *p++ = ( data_len >> 16 ) & 0xff;
     *p++ = ( data_len >> 8  ) & 0xff;
     *p++ = ( data_len       ) & 0xff;
     p += 3;
-    *p++ = CTR_DRBG_SEEDLEN;
+    *p++ = MBEDTLS_CTR_DRBG_SEEDLEN;
     memcpy( p, data, data_len );
     p[data_len] = 0x80;
 
-    buf_len = CTR_DRBG_BLOCKSIZE + 8 + data_len + 1;
+    buf_len = MBEDTLS_CTR_DRBG_BLOCKSIZE + 8 + data_len + 1;
 
-    for( i = 0; i < CTR_DRBG_KEYSIZE; i++ )
+    for( i = 0; i < MBEDTLS_CTR_DRBG_KEYSIZE; i++ )
         key[i] = i;
 
-    aes_setkey_enc( &aes_ctx, key, CTR_DRBG_KEYBITS );
+    mbedtls_aes_setkey_enc( &aes_ctx, key, MBEDTLS_CTR_DRBG_KEYBITS );
 
     /*
-     * Reduce data to CTR_DRBG_SEEDLEN bytes of data
+     * Reduce data to MBEDTLS_CTR_DRBG_SEEDLEN bytes of data
      */
-    for( j = 0; j < CTR_DRBG_SEEDLEN; j += CTR_DRBG_BLOCKSIZE )
+    for( j = 0; j < MBEDTLS_CTR_DRBG_SEEDLEN; j += MBEDTLS_CTR_DRBG_BLOCKSIZE )
     {
         p = buf;
-        memset( chain, 0, CTR_DRBG_BLOCKSIZE );
+        memset( chain, 0, MBEDTLS_CTR_DRBG_BLOCKSIZE );
         use_len = buf_len;
 
         while( use_len > 0 )
         {
-            for( i = 0; i < CTR_DRBG_BLOCKSIZE; i++ )
+            for( i = 0; i < MBEDTLS_CTR_DRBG_BLOCKSIZE; i++ )
                 chain[i] ^= p[i];
-            p += CTR_DRBG_BLOCKSIZE;
-            use_len -= ( use_len >= CTR_DRBG_BLOCKSIZE ) ?
-                       CTR_DRBG_BLOCKSIZE : use_len;
+            p += MBEDTLS_CTR_DRBG_BLOCKSIZE;
+            use_len -= ( use_len >= MBEDTLS_CTR_DRBG_BLOCKSIZE ) ?
+                       MBEDTLS_CTR_DRBG_BLOCKSIZE : use_len;
 
-            aes_crypt_ecb( &aes_ctx, AES_ENCRYPT, chain, chain );
+            mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_ENCRYPT, chain, chain );
         }
 
-        memcpy( tmp + j, chain, CTR_DRBG_BLOCKSIZE );
+        memcpy( tmp + j, chain, MBEDTLS_CTR_DRBG_BLOCKSIZE );
 
         /*
          * Update IV
@@ -200,87 +200,87 @@
     /*
      * Do final encryption with reduced data
      */
-    aes_setkey_enc( &aes_ctx, tmp, CTR_DRBG_KEYBITS );
-    iv = tmp + CTR_DRBG_KEYSIZE;
+    mbedtls_aes_setkey_enc( &aes_ctx, tmp, MBEDTLS_CTR_DRBG_KEYBITS );
+    iv = tmp + MBEDTLS_CTR_DRBG_KEYSIZE;
     p = output;
 
-    for( j = 0; j < CTR_DRBG_SEEDLEN; j += CTR_DRBG_BLOCKSIZE )
+    for( j = 0; j < MBEDTLS_CTR_DRBG_SEEDLEN; j += MBEDTLS_CTR_DRBG_BLOCKSIZE )
     {
-        aes_crypt_ecb( &aes_ctx, AES_ENCRYPT, iv, iv );
-        memcpy( p, iv, CTR_DRBG_BLOCKSIZE );
-        p += CTR_DRBG_BLOCKSIZE;
+        mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_ENCRYPT, iv, iv );
+        memcpy( p, iv, MBEDTLS_CTR_DRBG_BLOCKSIZE );
+        p += MBEDTLS_CTR_DRBG_BLOCKSIZE;
     }
 
-    aes_free( &aes_ctx );
+    mbedtls_aes_free( &aes_ctx );
 
     return( 0 );
 }
 
-static int ctr_drbg_update_internal( ctr_drbg_context *ctx,
-                              const unsigned char data[CTR_DRBG_SEEDLEN] )
+static int ctr_drbg_update_internal( mbedtls_ctr_drbg_context *ctx,
+                              const unsigned char data[MBEDTLS_CTR_DRBG_SEEDLEN] )
 {
-    unsigned char tmp[CTR_DRBG_SEEDLEN];
+    unsigned char tmp[MBEDTLS_CTR_DRBG_SEEDLEN];
     unsigned char *p = tmp;
     int i, j;
 
-    memset( tmp, 0, CTR_DRBG_SEEDLEN );
+    memset( tmp, 0, MBEDTLS_CTR_DRBG_SEEDLEN );
 
-    for( j = 0; j < CTR_DRBG_SEEDLEN; j += CTR_DRBG_BLOCKSIZE )
+    for( j = 0; j < MBEDTLS_CTR_DRBG_SEEDLEN; j += MBEDTLS_CTR_DRBG_BLOCKSIZE )
     {
         /*
          * Increase counter
          */
-        for( i = CTR_DRBG_BLOCKSIZE; i > 0; i-- )
+        for( i = MBEDTLS_CTR_DRBG_BLOCKSIZE; i > 0; i-- )
             if( ++ctx->counter[i - 1] != 0 )
                 break;
 
         /*
          * Crypt counter block
          */
-        aes_crypt_ecb( &ctx->aes_ctx, AES_ENCRYPT, ctx->counter, p );
+        mbedtls_aes_crypt_ecb( &ctx->aes_ctx, MBEDTLS_AES_ENCRYPT, ctx->counter, p );
 
-        p += CTR_DRBG_BLOCKSIZE;
+        p += MBEDTLS_CTR_DRBG_BLOCKSIZE;
     }
 
-    for( i = 0; i < CTR_DRBG_SEEDLEN; i++ )
+    for( i = 0; i < MBEDTLS_CTR_DRBG_SEEDLEN; i++ )
         tmp[i] ^= data[i];
 
     /*
      * Update key and counter
      */
-    aes_setkey_enc( &ctx->aes_ctx, tmp, CTR_DRBG_KEYBITS );
-    memcpy( ctx->counter, tmp + CTR_DRBG_KEYSIZE, CTR_DRBG_BLOCKSIZE );
+    mbedtls_aes_setkey_enc( &ctx->aes_ctx, tmp, MBEDTLS_CTR_DRBG_KEYBITS );
+    memcpy( ctx->counter, tmp + MBEDTLS_CTR_DRBG_KEYSIZE, MBEDTLS_CTR_DRBG_BLOCKSIZE );
 
     return( 0 );
 }
 
-void ctr_drbg_update( ctr_drbg_context *ctx,
+void mbedtls_ctr_drbg_update( mbedtls_ctr_drbg_context *ctx,
                       const unsigned char *additional, size_t add_len )
 {
-    unsigned char add_input[CTR_DRBG_SEEDLEN];
+    unsigned char add_input[MBEDTLS_CTR_DRBG_SEEDLEN];
 
     if( add_len > 0 )
     {
         /* MAX_INPUT would be more logical here, but we have to match
          * block_cipher_df()'s limits since we can't propagate errors */
-        if( add_len > CTR_DRBG_MAX_SEED_INPUT )
-            add_len = CTR_DRBG_MAX_SEED_INPUT;
+        if( add_len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT )
+            add_len = MBEDTLS_CTR_DRBG_MAX_SEED_INPUT;
 
         block_cipher_df( add_input, additional, add_len );
         ctr_drbg_update_internal( ctx, add_input );
     }
 }
 
-int ctr_drbg_reseed( ctr_drbg_context *ctx,
+int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
                      const unsigned char *additional, size_t len )
 {
-    unsigned char seed[CTR_DRBG_MAX_SEED_INPUT];
+    unsigned char seed[MBEDTLS_CTR_DRBG_MAX_SEED_INPUT];
     size_t seedlen = 0;
 
-    if( ctx->entropy_len + len > CTR_DRBG_MAX_SEED_INPUT )
-        return( POLARSSL_ERR_CTR_DRBG_INPUT_TOO_BIG );
+    if( ctx->entropy_len + len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT )
+        return( MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG );
 
-    memset( seed, 0, CTR_DRBG_MAX_SEED_INPUT );
+    memset( seed, 0, MBEDTLS_CTR_DRBG_MAX_SEED_INPUT );
 
     /*
      * Gather entropy_len bytes of entropy to seed state
@@ -288,7 +288,7 @@
     if( 0 != ctx->f_entropy( ctx->p_entropy, seed,
                              ctx->entropy_len ) )
     {
-        return( POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED );
+        return( MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED );
     }
 
     seedlen += ctx->entropy_len;
@@ -316,30 +316,30 @@
     return( 0 );
 }
 
-int ctr_drbg_random_with_add( void *p_rng,
+int mbedtls_ctr_drbg_random_with_add( void *p_rng,
                               unsigned char *output, size_t output_len,
                               const unsigned char *additional, size_t add_len )
 {
     int ret = 0;
-    ctr_drbg_context *ctx = (ctr_drbg_context *) p_rng;
-    unsigned char add_input[CTR_DRBG_SEEDLEN];
+    mbedtls_ctr_drbg_context *ctx = (mbedtls_ctr_drbg_context *) p_rng;
+    unsigned char add_input[MBEDTLS_CTR_DRBG_SEEDLEN];
     unsigned char *p = output;
-    unsigned char tmp[CTR_DRBG_BLOCKSIZE];
+    unsigned char tmp[MBEDTLS_CTR_DRBG_BLOCKSIZE];
     int i;
     size_t use_len;
 
-    if( output_len > CTR_DRBG_MAX_REQUEST )
-        return( POLARSSL_ERR_CTR_DRBG_REQUEST_TOO_BIG );
+    if( output_len > MBEDTLS_CTR_DRBG_MAX_REQUEST )
+        return( MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG );
 
-    if( add_len > CTR_DRBG_MAX_INPUT )
-        return( POLARSSL_ERR_CTR_DRBG_INPUT_TOO_BIG );
+    if( add_len > MBEDTLS_CTR_DRBG_MAX_INPUT )
+        return( MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG );
 
-    memset( add_input, 0, CTR_DRBG_SEEDLEN );
+    memset( add_input, 0, MBEDTLS_CTR_DRBG_SEEDLEN );
 
     if( ctx->reseed_counter > ctx->reseed_interval ||
         ctx->prediction_resistance )
     {
-        if( ( ret = ctr_drbg_reseed( ctx, additional, add_len ) ) != 0 )
+        if( ( ret = mbedtls_ctr_drbg_reseed( ctx, additional, add_len ) ) != 0 )
             return( ret );
 
         add_len = 0;
@@ -356,16 +356,16 @@
         /*
          * Increase counter
          */
-        for( i = CTR_DRBG_BLOCKSIZE; i > 0; i-- )
+        for( i = MBEDTLS_CTR_DRBG_BLOCKSIZE; i > 0; i-- )
             if( ++ctx->counter[i - 1] != 0 )
                 break;
 
         /*
          * Crypt counter block
          */
-        aes_crypt_ecb( &ctx->aes_ctx, AES_ENCRYPT, ctx->counter, tmp );
+        mbedtls_aes_crypt_ecb( &ctx->aes_ctx, MBEDTLS_AES_ENCRYPT, ctx->counter, tmp );
 
-        use_len = ( output_len > CTR_DRBG_BLOCKSIZE ) ? CTR_DRBG_BLOCKSIZE :
+        use_len = ( output_len > MBEDTLS_CTR_DRBG_BLOCKSIZE ) ? MBEDTLS_CTR_DRBG_BLOCKSIZE :
                                                        output_len;
         /*
          * Copy random block to destination
@@ -382,27 +382,27 @@
     return( 0 );
 }
 
-int ctr_drbg_random( void *p_rng, unsigned char *output, size_t output_len )
+int mbedtls_ctr_drbg_random( void *p_rng, unsigned char *output, size_t output_len )
 {
-    return ctr_drbg_random_with_add( p_rng, output, output_len, NULL, 0 );
+    return mbedtls_ctr_drbg_random_with_add( p_rng, output, output_len, NULL, 0 );
 }
 
-#if defined(POLARSSL_FS_IO)
-int ctr_drbg_write_seed_file( ctr_drbg_context *ctx, const char *path )
+#if defined(MBEDTLS_FS_IO)
+int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path )
 {
-    int ret = POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR;
+    int ret = MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR;
     FILE *f;
-    unsigned char buf[ CTR_DRBG_MAX_INPUT ];
+    unsigned char buf[ MBEDTLS_CTR_DRBG_MAX_INPUT ];
 
     if( ( f = fopen( path, "wb" ) ) == NULL )
-        return( POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR );
+        return( MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR );
 
-    if( ( ret = ctr_drbg_random( ctx, buf, CTR_DRBG_MAX_INPUT ) ) != 0 )
+    if( ( ret = mbedtls_ctr_drbg_random( ctx, buf, MBEDTLS_CTR_DRBG_MAX_INPUT ) ) != 0 )
         goto exit;
 
-    if( fwrite( buf, 1, CTR_DRBG_MAX_INPUT, f ) != CTR_DRBG_MAX_INPUT )
+    if( fwrite( buf, 1, MBEDTLS_CTR_DRBG_MAX_INPUT, f ) != MBEDTLS_CTR_DRBG_MAX_INPUT )
     {
-        ret = POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR;
+        ret = MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR;
         goto exit;
     }
 
@@ -413,40 +413,40 @@
     return( ret );
 }
 
-int ctr_drbg_update_seed_file( ctr_drbg_context *ctx, const char *path )
+int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path )
 {
     FILE *f;
     size_t n;
-    unsigned char buf[ CTR_DRBG_MAX_INPUT ];
+    unsigned char buf[ MBEDTLS_CTR_DRBG_MAX_INPUT ];
 
     if( ( f = fopen( path, "rb" ) ) == NULL )
-        return( POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR );
+        return( MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR );
 
     fseek( f, 0, SEEK_END );
     n = (size_t) ftell( f );
     fseek( f, 0, SEEK_SET );
 
-    if( n > CTR_DRBG_MAX_INPUT )
+    if( n > MBEDTLS_CTR_DRBG_MAX_INPUT )
     {
         fclose( f );
-        return( POLARSSL_ERR_CTR_DRBG_INPUT_TOO_BIG );
+        return( MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG );
     }
 
     if( fread( buf, 1, n, f ) != n )
     {
         fclose( f );
-        return( POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR );
+        return( MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR );
     }
 
     fclose( f );
 
-    ctr_drbg_update( ctx, buf, n );
+    mbedtls_ctr_drbg_update( ctx, buf, n );
 
-    return( ctr_drbg_write_seed_file( ctx, path ) );
+    return( mbedtls_ctr_drbg_write_seed_file( ctx, path ) );
 }
-#endif /* POLARSSL_FS_IO */
+#endif /* MBEDTLS_FS_IO */
 
-#if defined(POLARSSL_SELF_TEST)
+#if defined(MBEDTLS_SELF_TEST)
 
 static const unsigned char entropy_source_pr[96] =
     { 0xc1, 0x80, 0x81, 0xa6, 0x5d, 0x44, 0x02, 0x16,
@@ -501,57 +501,57 @@
 #define CHK( c )    if( (c) != 0 )                          \
                     {                                       \
                         if( verbose != 0 )                  \
-                            polarssl_printf( "failed\n" );  \
+                            mbedtls_printf( "failed\n" );  \
                         return( 1 );                        \
                     }
 
 /*
  * Checkup routine
  */
-int ctr_drbg_self_test( int verbose )
+int mbedtls_ctr_drbg_self_test( int verbose )
 {
-    ctr_drbg_context ctx;
+    mbedtls_ctr_drbg_context ctx;
     unsigned char buf[16];
 
     /*
      * Based on a NIST CTR_DRBG test vector (PR = True)
      */
     if( verbose != 0 )
-        polarssl_printf( "  CTR_DRBG (PR = TRUE) : " );
+        mbedtls_printf( "  CTR_DRBG (PR = TRUE) : " );
 
     test_offset = 0;
-    CHK( ctr_drbg_init_entropy_len( &ctx, ctr_drbg_self_test_entropy,
+    CHK( mbedtls_ctr_drbg_init_entropy_len( &ctx, ctr_drbg_self_test_entropy,
                                 (void *) entropy_source_pr, nonce_pers_pr, 16, 32 ) );
-    ctr_drbg_set_prediction_resistance( &ctx, CTR_DRBG_PR_ON );
-    CHK( ctr_drbg_random( &ctx, buf, CTR_DRBG_BLOCKSIZE ) );
-    CHK( ctr_drbg_random( &ctx, buf, CTR_DRBG_BLOCKSIZE ) );
-    CHK( memcmp( buf, result_pr, CTR_DRBG_BLOCKSIZE ) );
+    mbedtls_ctr_drbg_set_prediction_resistance( &ctx, MBEDTLS_CTR_DRBG_PR_ON );
+    CHK( mbedtls_ctr_drbg_random( &ctx, buf, MBEDTLS_CTR_DRBG_BLOCKSIZE ) );
+    CHK( mbedtls_ctr_drbg_random( &ctx, buf, MBEDTLS_CTR_DRBG_BLOCKSIZE ) );
+    CHK( memcmp( buf, result_pr, MBEDTLS_CTR_DRBG_BLOCKSIZE ) );
 
     if( verbose != 0 )
-        polarssl_printf( "passed\n" );
+        mbedtls_printf( "passed\n" );
 
     /*
      * Based on a NIST CTR_DRBG test vector (PR = FALSE)
      */
     if( verbose != 0 )
-        polarssl_printf( "  CTR_DRBG (PR = FALSE): " );
+        mbedtls_printf( "  CTR_DRBG (PR = FALSE): " );
 
     test_offset = 0;
-    CHK( ctr_drbg_init_entropy_len( &ctx, ctr_drbg_self_test_entropy,
+    CHK( mbedtls_ctr_drbg_init_entropy_len( &ctx, ctr_drbg_self_test_entropy,
                             (void *) entropy_source_nopr, nonce_pers_nopr, 16, 32 ) );
-    CHK( ctr_drbg_random( &ctx, buf, 16 ) );
-    CHK( ctr_drbg_reseed( &ctx, NULL, 0 ) );
-    CHK( ctr_drbg_random( &ctx, buf, 16 ) );
+    CHK( mbedtls_ctr_drbg_random( &ctx, buf, 16 ) );
+    CHK( mbedtls_ctr_drbg_reseed( &ctx, NULL, 0 ) );
+    CHK( mbedtls_ctr_drbg_random( &ctx, buf, 16 ) );
     CHK( memcmp( buf, result_nopr, 16 ) );
 
     if( verbose != 0 )
-        polarssl_printf( "passed\n" );
+        mbedtls_printf( "passed\n" );
 
     if( verbose != 0 )
-            polarssl_printf( "\n" );
+            mbedtls_printf( "\n" );
 
     return( 0 );
 }
-#endif /* POLARSSL_SELF_TEST */
+#endif /* MBEDTLS_SELF_TEST */
 
-#endif /* POLARSSL_CTR_DRBG_C */
+#endif /* MBEDTLS_CTR_DRBG_C */
diff --git a/library/debug.c b/library/debug.c
index ade98d4..8dae2ed 100644
--- a/library/debug.c
+++ b/library/debug.c
@@ -20,13 +20,13 @@
  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#if !defined(POLARSSL_CONFIG_FILE)
+#if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else
-#include POLARSSL_CONFIG_FILE
+#include MBEDTLS_CONFIG_FILE
 #endif
 
-#if defined(POLARSSL_DEBUG_C)
+#if defined(MBEDTLS_DEBUG_C)
 
 #include "mbedtls/debug.h"
 
@@ -44,26 +44,26 @@
 #endif
 #endif /* _MSC_VER */
 
-#if defined(POLARSSL_PLATFORM_C)
+#if defined(MBEDTLS_PLATFORM_C)
 #include "mbedtls/platform.h"
 #else
-#define polarssl_snprintf snprintf
+#define mbedtls_snprintf snprintf
 #endif
 
-static int debug_log_mode = POLARSSL_DEBUG_DFL_MODE;
+static int debug_log_mode = MBEDTLS_DEBUG_DFL_MODE;
 static int debug_threshold = 0;
 
-void debug_set_log_mode( int log_mode )
+void mbedtls_debug_set_log_mode( int log_mode )
 {
     debug_log_mode = log_mode;
 }
 
-void debug_set_threshold( int threshold )
+void mbedtls_debug_set_threshold( int threshold )
 {
     debug_threshold = threshold;
 }
 
-char *debug_fmt( const char *format, ... )
+char *mbedtls_debug_fmt( const char *format, ... )
 {
     va_list argp;
     static char str[512];
@@ -77,7 +77,7 @@
     return( str );
 }
 
-void debug_print_msg( const ssl_context *ssl, int level,
+void mbedtls_debug_print_msg( const mbedtls_ssl_context *ssl, int level,
                       const char *file, int line, const char *text )
 {
     char str[512];
@@ -86,18 +86,18 @@
     if( ssl->f_dbg == NULL || level > debug_threshold )
         return;
 
-    if( debug_log_mode == POLARSSL_DEBUG_LOG_RAW )
+    if( debug_log_mode == MBEDTLS_DEBUG_LOG_RAW )
     {
         ssl->f_dbg( ssl->p_dbg, level, text );
         return;
     }
 
-    polarssl_snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text );
+    mbedtls_snprintf( str, maxlen, "%s(%04d): %s\n", file, line, text );
     str[maxlen] = '\0';
     ssl->f_dbg( ssl->p_dbg, level, str );
 }
 
-void debug_print_ret( const ssl_context *ssl, int level,
+void mbedtls_debug_print_ret( const mbedtls_ssl_context *ssl, int level,
                       const char *file, int line,
                       const char *text, int ret )
 {
@@ -108,17 +108,17 @@
     if( ssl->f_dbg == NULL || level > debug_threshold )
         return;
 
-    if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
-        idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line );
+    if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
+        idx = mbedtls_snprintf( str, maxlen, "%s(%04d): ", file, line );
 
-    polarssl_snprintf( str + idx, maxlen - idx, "%s() returned %d (-0x%04x)\n",
+    mbedtls_snprintf( str + idx, maxlen - idx, "%s() returned %d (-0x%04x)\n",
               text, ret, -ret );
 
     str[maxlen] = '\0';
     ssl->f_dbg( ssl->p_dbg, level, str );
 }
 
-void debug_print_buf( const ssl_context *ssl, int level,
+void mbedtls_debug_print_buf( const mbedtls_ssl_context *ssl, int level,
                       const char *file, int line, const char *text,
                       const unsigned char *buf, size_t len )
 {
@@ -129,10 +129,10 @@
     if( ssl->f_dbg == NULL || level > debug_threshold )
         return;
 
-    if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
-        idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line );
+    if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
+        idx = mbedtls_snprintf( str, maxlen, "%s(%04d): ", file, line );
 
-    polarssl_snprintf( str + idx, maxlen - idx, "dumping '%s' (%u bytes)\n",
+    mbedtls_snprintf( str + idx, maxlen - idx, "dumping '%s' (%u bytes)\n",
               text, (unsigned int) len );
 
     str[maxlen] = '\0';
@@ -149,22 +149,22 @@
         {
             if( i > 0 )
             {
-                polarssl_snprintf( str + idx, maxlen - idx, "  %s\n", txt );
+                mbedtls_snprintf( str + idx, maxlen - idx, "  %s\n", txt );
                 ssl->f_dbg( ssl->p_dbg, level, str );
 
                 idx = 0;
                 memset( txt, 0, sizeof( txt ) );
             }
 
-            if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
-                idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line );
+            if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
+                idx = mbedtls_snprintf( str, maxlen, "%s(%04d): ", file, line );
 
-            idx += polarssl_snprintf( str + idx, maxlen - idx, "%04x: ",
+            idx += mbedtls_snprintf( str + idx, maxlen - idx, "%04x: ",
                              (unsigned int) i );
 
         }
 
-        idx += polarssl_snprintf( str + idx, maxlen - idx, " %02x",
+        idx += mbedtls_snprintf( str + idx, maxlen - idx, " %02x",
                          (unsigned int) buf[i] );
         txt[i % 16] = ( buf[i] > 31 && buf[i] < 127 ) ? buf[i] : '.' ;
     }
@@ -172,17 +172,17 @@
     if( len > 0 )
     {
         for( /* i = i */; i % 16 != 0; i++ )
-            idx += polarssl_snprintf( str + idx, maxlen - idx, "   " );
+            idx += mbedtls_snprintf( str + idx, maxlen - idx, "   " );
 
-        polarssl_snprintf( str + idx, maxlen - idx, "  %s\n", txt );
+        mbedtls_snprintf( str + idx, maxlen - idx, "  %s\n", txt );
         ssl->f_dbg( ssl->p_dbg, level, str );
     }
 }
 
-#if defined(POLARSSL_ECP_C)
-void debug_print_ecp( const ssl_context *ssl, int level,
+#if defined(MBEDTLS_ECP_C)
+void mbedtls_debug_print_ecp( const mbedtls_ssl_context *ssl, int level,
                       const char *file, int line,
-                      const char *text, const ecp_point *X )
+                      const char *text, const mbedtls_ecp_point *X )
 {
     char str[512];
     int maxlen = sizeof( str ) - 1;
@@ -190,20 +190,20 @@
     if( ssl->f_dbg == NULL || level > debug_threshold )
         return;
 
-    polarssl_snprintf( str, maxlen, "%s(X)", text );
+    mbedtls_snprintf( str, maxlen, "%s(X)", text );
     str[maxlen] = '\0';
-    debug_print_mpi( ssl, level, file, line, str, &X->X );
+    mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->X );
 
-    polarssl_snprintf( str, maxlen, "%s(Y)", text );
+    mbedtls_snprintf( str, maxlen, "%s(Y)", text );
     str[maxlen] = '\0';
-    debug_print_mpi( ssl, level, file, line, str, &X->Y );
+    mbedtls_debug_print_mpi( ssl, level, file, line, str, &X->Y );
 }
-#endif /* POLARSSL_ECP_C */
+#endif /* MBEDTLS_ECP_C */
 
-#if defined(POLARSSL_BIGNUM_C)
-void debug_print_mpi( const ssl_context *ssl, int level,
+#if defined(MBEDTLS_BIGNUM_C)
+void mbedtls_debug_print_mpi( const mbedtls_ssl_context *ssl, int level,
                       const char *file, int line,
-                      const char *text, const mpi *X )
+                      const char *text, const mbedtls_mpi *X )
 {
     char str[512];
     int j, k, maxlen = sizeof( str ) - 1, zeros = 1;
@@ -216,15 +216,15 @@
         if( X->p[n] != 0 )
             break;
 
-    for( j = ( sizeof(t_uint) << 3 ) - 1; j >= 0; j-- )
+    for( j = ( sizeof(mbedtls_mpi_uint) << 3 ) - 1; j >= 0; j-- )
         if( ( ( X->p[n] >> j ) & 1 ) != 0 )
             break;
 
-    if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
-        idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line );
+    if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
+        idx = mbedtls_snprintf( str, maxlen, "%s(%04d): ", file, line );
 
-    polarssl_snprintf( str + idx, maxlen - idx, "value of '%s' (%d bits) is:\n",
-              text, (int) ( ( n * ( sizeof(t_uint) << 3 ) ) + j + 1 ) );
+    mbedtls_snprintf( str + idx, maxlen - idx, "value of '%s' (%d bits) is:\n",
+              text, (int) ( ( n * ( sizeof(mbedtls_mpi_uint) << 3 ) ) + j + 1 ) );
 
     str[maxlen] = '\0';
     ssl->f_dbg( ssl->p_dbg, level, str );
@@ -235,7 +235,7 @@
         if( zeros && X->p[i - 1] == 0 )
             continue;
 
-        for( k = sizeof( t_uint ) - 1; k >= 0; k-- )
+        for( k = sizeof( mbedtls_mpi_uint ) - 1; k >= 0; k-- )
         {
             if( zeros && ( ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF ) == 0 )
                 continue;
@@ -246,16 +246,16 @@
             {
                 if( j > 0 )
                 {
-                    polarssl_snprintf( str + idx, maxlen - idx, "\n" );
+                    mbedtls_snprintf( str + idx, maxlen - idx, "\n" );
                     ssl->f_dbg( ssl->p_dbg, level, str );
                     idx = 0;
                 }
 
-                if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
-                    idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line );
+                if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
+                    idx = mbedtls_snprintf( str, maxlen, "%s(%04d): ", file, line );
             }
 
-            idx += polarssl_snprintf( str + idx, maxlen - idx, " %02x", (unsigned int)
+            idx += mbedtls_snprintf( str + idx, maxlen - idx, " %02x", (unsigned int)
                              ( X->p[i - 1] >> ( k << 3 ) ) & 0xFF );
 
             j++;
@@ -265,59 +265,59 @@
 
     if( zeros == 1 )
     {
-        if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
+        if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
         {
-            idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line );
+            idx = mbedtls_snprintf( str, maxlen, "%s(%04d): ", file, line );
 
         }
-        idx += polarssl_snprintf( str + idx, maxlen - idx, " 00" );
+        idx += mbedtls_snprintf( str + idx, maxlen - idx, " 00" );
     }
 
-    polarssl_snprintf( str + idx, maxlen - idx, "\n" );
+    mbedtls_snprintf( str + idx, maxlen - idx, "\n" );
     ssl->f_dbg( ssl->p_dbg, level, str );
 }
-#endif /* POLARSSL_BIGNUM_C */
+#endif /* MBEDTLS_BIGNUM_C */
 
-#if defined(POLARSSL_X509_CRT_PARSE_C)
-static void debug_print_pk( const ssl_context *ssl, int level,
+#if defined(MBEDTLS_X509_CRT_PARSE_C)
+static void debug_print_pk( const mbedtls_ssl_context *ssl, int level,
                             const char *file, int line,
-                            const char *text, const pk_context *pk )
+                            const char *text, const mbedtls_pk_context *pk )
 {
     size_t i;
-    pk_debug_item items[POLARSSL_PK_DEBUG_MAX_ITEMS];
+    mbedtls_pk_debug_item items[MBEDTLS_PK_DEBUG_MAX_ITEMS];
     char name[16];
 
     memset( items, 0, sizeof( items ) );
 
-    if( pk_debug( pk, items ) != 0 )
+    if( mbedtls_pk_debug( pk, items ) != 0 )
     {
-        debug_print_msg( ssl, level, file, line, "invalid PK context" );
+        mbedtls_debug_print_msg( ssl, level, file, line, "invalid PK context" );
         return;
     }
 
-    for( i = 0; i < POLARSSL_PK_DEBUG_MAX_ITEMS; i++ )
+    for( i = 0; i < MBEDTLS_PK_DEBUG_MAX_ITEMS; i++ )
     {
-        if( items[i].type == POLARSSL_PK_DEBUG_NONE )
+        if( items[i].type == MBEDTLS_PK_DEBUG_NONE )
             return;
 
-        polarssl_snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
+        mbedtls_snprintf( name, sizeof( name ), "%s%s", text, items[i].name );
         name[sizeof( name ) - 1] = '\0';
 
-        if( items[i].type == POLARSSL_PK_DEBUG_MPI )
-            debug_print_mpi( ssl, level, file, line, name, items[i].value );
+        if( items[i].type == MBEDTLS_PK_DEBUG_MPI )
+            mbedtls_debug_print_mpi( ssl, level, file, line, name, items[i].value );
         else
-#if defined(POLARSSL_ECP_C)
-        if( items[i].type == POLARSSL_PK_DEBUG_ECP )
-            debug_print_ecp( ssl, level, file, line, name, items[i].value );
+#if defined(MBEDTLS_ECP_C)
+        if( items[i].type == MBEDTLS_PK_DEBUG_ECP )
+            mbedtls_debug_print_ecp( ssl, level, file, line, name, items[i].value );
         else
 #endif
-            debug_print_msg( ssl, level, file, line, "should not happen" );
+            mbedtls_debug_print_msg( ssl, level, file, line, "should not happen" );
     }
 }
 
-void debug_print_crt( const ssl_context *ssl, int level,
+void mbedtls_debug_print_crt( const mbedtls_ssl_context *ssl, int level,
                       const char *file, int line,
-                      const char *text, const x509_crt *crt )
+                      const char *text, const mbedtls_x509_crt *crt )
 {
     char str[1024], prefix[64];
     int i = 0, maxlen = sizeof( prefix ) - 1, idx = 0;
@@ -325,9 +325,9 @@
     if( ssl->f_dbg == NULL || crt == NULL || level > debug_threshold )
         return;
 
-    if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
+    if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
     {
-        polarssl_snprintf( prefix, maxlen, "%s(%04d): ", file, line );
+        mbedtls_snprintf( prefix, maxlen, "%s(%04d): ", file, line );
         prefix[maxlen] = '\0';
     }
     else
@@ -338,12 +338,12 @@
     while( crt != NULL )
     {
         char buf[1024];
-        x509_crt_info( buf, sizeof( buf ) - 1, prefix, crt );
+        mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, prefix, crt );
 
-        if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
-            idx = polarssl_snprintf( str, maxlen, "%s(%04d): ", file, line );
+        if( debug_log_mode == MBEDTLS_DEBUG_LOG_FULL )
+            idx = mbedtls_snprintf( str, maxlen, "%s(%04d): ", file, line );
 
-        polarssl_snprintf( str + idx, maxlen - idx, "%s #%d:\n%s",
+        mbedtls_snprintf( str + idx, maxlen - idx, "%s #%d:\n%s",
                   text, ++i, buf );
 
         str[maxlen] = '\0';
@@ -354,6 +354,6 @@
         crt = crt->next;
     }
 }
-#endif /* POLARSSL_X509_CRT_PARSE_C */
+#endif /* MBEDTLS_X509_CRT_PARSE_C */
 
-#endif /* POLARSSL_DEBUG_C */
+#endif /* MBEDTLS_DEBUG_C */
diff --git a/library/des.c b/library/des.c
index be7348b..f669552 100644
--- a/library/des.c
+++ b/library/des.c
@@ -26,31 +26,31 @@
  *  http://csrc.nist.gov/publications/fips/fips46-3/fips46-3.pdf
  */
 
-#if !defined(POLARSSL_CONFIG_FILE)
+#if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else
-#include POLARSSL_CONFIG_FILE
+#include MBEDTLS_CONFIG_FILE
 #endif
 
-#if defined(POLARSSL_DES_C)
+#if defined(MBEDTLS_DES_C)
 
 #include "mbedtls/des.h"
 
 #include <string.h>
 
-#if defined(POLARSSL_SELF_TEST)
-#if defined(POLARSSL_PLATFORM_C)
+#if defined(MBEDTLS_SELF_TEST)
+#if defined(MBEDTLS_PLATFORM_C)
 #include "mbedtls/platform.h"
 #else
 #include <stdio.h>
-#define polarssl_printf printf
-#endif /* POLARSSL_PLATFORM_C */
-#endif /* POLARSSL_SELF_TEST */
+#define mbedtls_printf printf
+#endif /* MBEDTLS_PLATFORM_C */
+#endif /* MBEDTLS_SELF_TEST */
 
-#if !defined(POLARSSL_DES_ALT)
+#if !defined(MBEDTLS_DES_ALT)
 
 /* 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;
 }
 
@@ -307,30 +307,30 @@
 
 #define SWAP(a,b) { uint32_t t = a; a = b; b = t; t = 0; }
 
-void des_init( des_context *ctx )
+void mbedtls_des_init( mbedtls_des_context *ctx )
 {
-    memset( ctx, 0, sizeof( des_context ) );
+    memset( ctx, 0, sizeof( mbedtls_des_context ) );
 }
 
-void des_free( des_context *ctx )
+void mbedtls_des_free( mbedtls_des_context *ctx )
 {
     if( ctx == NULL )
         return;
 
-    polarssl_zeroize( ctx, sizeof( des_context ) );
+    mbedtls_zeroize( ctx, sizeof( mbedtls_des_context ) );
 }
 
-void des3_init( des3_context *ctx )
+void mbedtls_des3_init( mbedtls_des3_context *ctx )
 {
-    memset( ctx, 0, sizeof( des3_context ) );
+    memset( ctx, 0, sizeof( mbedtls_des3_context ) );
 }
 
-void des3_free( des3_context *ctx )
+void mbedtls_des3_free( mbedtls_des3_context *ctx )
 {
     if( ctx == NULL )
         return;
 
-    polarssl_zeroize( ctx, sizeof( des3_context ) );
+    mbedtls_zeroize( ctx, sizeof( mbedtls_des3_context ) );
 }
 
 static const unsigned char odd_parity_table[128] = { 1,  2,  4,  7,  8,
@@ -344,22 +344,22 @@
         227, 229, 230, 233, 234, 236, 239, 241, 242, 244, 247, 248, 251, 253,
         254 };
 
-void des_key_set_parity( unsigned char key[DES_KEY_SIZE] )
+void mbedtls_des_key_set_parity( unsigned char key[MBEDTLS_DES_KEY_SIZE] )
 {
     int i;
 
-    for( i = 0; i < DES_KEY_SIZE; i++ )
+    for( i = 0; i < MBEDTLS_DES_KEY_SIZE; i++ )
         key[i] = odd_parity_table[key[i] / 2];
 }
 
 /*
  * Check the given key's parity, returns 1 on failure, 0 on SUCCESS
  */
-int des_key_check_key_parity( const unsigned char key[DES_KEY_SIZE] )
+int mbedtls_des_key_check_key_parity( const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
 {
     int i;
 
-    for( i = 0; i < DES_KEY_SIZE; i++ )
+    for( i = 0; i < MBEDTLS_DES_KEY_SIZE; i++ )
         if( key[i] != odd_parity_table[key[i] / 2] )
             return( 1 );
 
@@ -389,7 +389,7 @@
 
 #define WEAK_KEY_COUNT 16
 
-static const unsigned char weak_key_table[WEAK_KEY_COUNT][DES_KEY_SIZE] =
+static const unsigned char weak_key_table[WEAK_KEY_COUNT][MBEDTLS_DES_KEY_SIZE] =
 {
     { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
     { 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE },
@@ -410,18 +410,18 @@
     { 0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1 }
 };
 
-int des_key_check_weak( const unsigned char key[DES_KEY_SIZE] )
+int mbedtls_des_key_check_weak( const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
 {
     int i;
 
     for( i = 0; i < WEAK_KEY_COUNT; i++ )
-        if( memcmp( weak_key_table[i], key, DES_KEY_SIZE) == 0 )
+        if( memcmp( weak_key_table[i], key, MBEDTLS_DES_KEY_SIZE) == 0 )
             return( 1 );
 
     return( 0 );
 }
 
-static void des_setkey( uint32_t SK[32], const unsigned char key[DES_KEY_SIZE] )
+static void des_setkey( uint32_t SK[32], const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
 {
     int i;
     uint32_t X, Y, T;
@@ -493,7 +493,7 @@
 /*
  * DES key schedule (56-bit, encryption)
  */
-int des_setkey_enc( des_context *ctx, const unsigned char key[DES_KEY_SIZE] )
+int mbedtls_des_setkey_enc( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
 {
     des_setkey( ctx->sk, key );
 
@@ -503,7 +503,7 @@
 /*
  * DES key schedule (56-bit, decryption)
  */
-int des_setkey_dec( des_context *ctx, const unsigned char key[DES_KEY_SIZE] )
+int mbedtls_des_setkey_dec( mbedtls_des_context *ctx, const unsigned char key[MBEDTLS_DES_KEY_SIZE] )
 {
     int i;
 
@@ -520,7 +520,7 @@
 
 static void des3_set2key( uint32_t esk[96],
                           uint32_t dsk[96],
-                          const unsigned char key[DES_KEY_SIZE*2] )
+                          const unsigned char key[MBEDTLS_DES_KEY_SIZE*2] )
 {
     int i;
 
@@ -546,13 +546,13 @@
 /*
  * Triple-DES key schedule (112-bit, encryption)
  */
-int des3_set2key_enc( des3_context *ctx,
-                      const unsigned char key[DES_KEY_SIZE * 2] )
+int mbedtls_des3_set2key_enc( mbedtls_des3_context *ctx,
+                      const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] )
 {
     uint32_t sk[96];
 
     des3_set2key( ctx->sk, sk, key );
-    polarssl_zeroize( sk,  sizeof( sk ) );
+    mbedtls_zeroize( sk,  sizeof( sk ) );
 
     return( 0 );
 }
@@ -560,13 +560,13 @@
 /*
  * Triple-DES key schedule (112-bit, decryption)
  */
-int des3_set2key_dec( des3_context *ctx,
-                      const unsigned char key[DES_KEY_SIZE * 2] )
+int mbedtls_des3_set2key_dec( mbedtls_des3_context *ctx,
+                      const unsigned char key[MBEDTLS_DES_KEY_SIZE * 2] )
 {
     uint32_t sk[96];
 
     des3_set2key( sk, ctx->sk, key );
-    polarssl_zeroize( sk,  sizeof( sk ) );
+    mbedtls_zeroize( sk,  sizeof( sk ) );
 
     return( 0 );
 }
@@ -597,13 +597,13 @@
 /*
  * Triple-DES key schedule (168-bit, encryption)
  */
-int des3_set3key_enc( des3_context *ctx,
-                      const unsigned char key[DES_KEY_SIZE * 3] )
+int mbedtls_des3_set3key_enc( mbedtls_des3_context *ctx,
+                      const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] )
 {
     uint32_t sk[96];
 
     des3_set3key( ctx->sk, sk, key );
-    polarssl_zeroize( sk,  sizeof( sk ) );
+    mbedtls_zeroize( sk,  sizeof( sk ) );
 
     return( 0 );
 }
@@ -611,13 +611,13 @@
 /*
  * Triple-DES key schedule (168-bit, decryption)
  */
-int des3_set3key_dec( des3_context *ctx,
-                      const unsigned char key[DES_KEY_SIZE * 3] )
+int mbedtls_des3_set3key_dec( mbedtls_des3_context *ctx,
+                      const unsigned char key[MBEDTLS_DES_KEY_SIZE * 3] )
 {
     uint32_t sk[96];
 
     des3_set3key( sk, ctx->sk, key );
-    polarssl_zeroize( sk,  sizeof( sk ) );
+    mbedtls_zeroize( sk,  sizeof( sk ) );
 
     return( 0 );
 }
@@ -625,7 +625,7 @@
 /*
  * DES-ECB block encryption/decryption
  */
-int des_crypt_ecb( des_context *ctx,
+int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx,
                     const unsigned char input[8],
                     unsigned char output[8] )
 {
@@ -653,11 +653,11 @@
     return( 0 );
 }
 
-#if defined(POLARSSL_CIPHER_MODE_CBC)
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
 /*
  * DES-CBC buffer encryption/decryption
  */
-int des_crypt_cbc( des_context *ctx,
+int mbedtls_des_crypt_cbc( mbedtls_des_context *ctx,
                     int mode,
                     size_t length,
                     unsigned char iv[8],
@@ -668,16 +668,16 @@
     unsigned char temp[8];
 
     if( length % 8 )
-        return( POLARSSL_ERR_DES_INVALID_INPUT_LENGTH );
+        return( MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH );
 
-    if( mode == DES_ENCRYPT )
+    if( mode == MBEDTLS_DES_ENCRYPT )
     {
         while( length > 0 )
         {
             for( i = 0; i < 8; i++ )
                 output[i] = (unsigned char)( input[i] ^ iv[i] );
 
-            des_crypt_ecb( ctx, output, output );
+            mbedtls_des_crypt_ecb( ctx, output, output );
             memcpy( iv, output, 8 );
 
             input  += 8;
@@ -685,12 +685,12 @@
             length -= 8;
         }
     }
-    else /* DES_DECRYPT */
+    else /* MBEDTLS_DES_DECRYPT */
     {
         while( length > 0 )
         {
             memcpy( temp, input, 8 );
-            des_crypt_ecb( ctx, input, output );
+            mbedtls_des_crypt_ecb( ctx, input, output );
 
             for( i = 0; i < 8; i++ )
                 output[i] = (unsigned char)( output[i] ^ iv[i] );
@@ -705,12 +705,12 @@
 
     return( 0 );
 }
-#endif /* POLARSSL_CIPHER_MODE_CBC */
+#endif /* MBEDTLS_CIPHER_MODE_CBC */
 
 /*
  * 3DES-ECB block encryption/decryption
  */
-int des3_crypt_ecb( des3_context *ctx,
+int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx,
                      const unsigned char input[8],
                      unsigned char output[8] )
 {
@@ -750,11 +750,11 @@
     return( 0 );
 }
 
-#if defined(POLARSSL_CIPHER_MODE_CBC)
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
 /*
  * 3DES-CBC buffer encryption/decryption
  */
-int des3_crypt_cbc( des3_context *ctx,
+int mbedtls_des3_crypt_cbc( mbedtls_des3_context *ctx,
                      int mode,
                      size_t length,
                      unsigned char iv[8],
@@ -765,16 +765,16 @@
     unsigned char temp[8];
 
     if( length % 8 )
-        return( POLARSSL_ERR_DES_INVALID_INPUT_LENGTH );
+        return( MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH );
 
-    if( mode == DES_ENCRYPT )
+    if( mode == MBEDTLS_DES_ENCRYPT )
     {
         while( length > 0 )
         {
             for( i = 0; i < 8; i++ )
                 output[i] = (unsigned char)( input[i] ^ iv[i] );
 
-            des3_crypt_ecb( ctx, output, output );
+            mbedtls_des3_crypt_ecb( ctx, output, output );
             memcpy( iv, output, 8 );
 
             input  += 8;
@@ -782,12 +782,12 @@
             length -= 8;
         }
     }
-    else /* DES_DECRYPT */
+    else /* MBEDTLS_DES_DECRYPT */
     {
         while( length > 0 )
         {
             memcpy( temp, input, 8 );
-            des3_crypt_ecb( ctx, input, output );
+            mbedtls_des3_crypt_ecb( ctx, input, output );
 
             for( i = 0; i < 8; i++ )
                 output[i] = (unsigned char)( output[i] ^ iv[i] );
@@ -802,11 +802,11 @@
 
     return( 0 );
 }
-#endif /* POLARSSL_CIPHER_MODE_CBC */
+#endif /* MBEDTLS_CIPHER_MODE_CBC */
 
-#endif /* !POLARSSL_DES_ALT */
+#endif /* !MBEDTLS_DES_ALT */
 
-#if defined(POLARSSL_SELF_TEST)
+#if defined(MBEDTLS_SELF_TEST)
 /*
  * DES and 3DES test vectors from:
  *
@@ -838,7 +838,7 @@
     { 0xDD, 0x17, 0xE8, 0xB8, 0xB4, 0x37, 0xD2, 0x32 }
 };
 
-#if defined(POLARSSL_CIPHER_MODE_CBC)
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
 static const unsigned char des3_test_iv[8] =
 {
     0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF,
@@ -857,24 +857,24 @@
     { 0x35, 0x76, 0x11, 0x56, 0x5F, 0xA1, 0x8E, 0x4D },
     { 0xCB, 0x19, 0x1F, 0x85, 0xD1, 0xED, 0x84, 0x39 }
 };
-#endif /* POLARSSL_CIPHER_MODE_CBC */
+#endif /* MBEDTLS_CIPHER_MODE_CBC */
 
 /*
  * Checkup routine
  */
-int des_self_test( int verbose )
+int mbedtls_des_self_test( int verbose )
 {
     int i, j, u, v, ret = 0;
-    des_context ctx;
-    des3_context ctx3;
+    mbedtls_des_context ctx;
+    mbedtls_des3_context ctx3;
     unsigned char buf[8];
-#if defined(POLARSSL_CIPHER_MODE_CBC)
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
     unsigned char prv[8];
     unsigned char iv[8];
 #endif
 
-    des_init( &ctx );
-    des3_init( &ctx3 );
+    mbedtls_des_init( &ctx );
+    mbedtls_des3_init( &ctx3 );
     /*
      * ECB mode
      */
@@ -884,36 +884,36 @@
         v = i  & 1;
 
         if( verbose != 0 )
-            polarssl_printf( "  DES%c-ECB-%3d (%s): ",
+            mbedtls_printf( "  DES%c-ECB-%3d (%s): ",
                              ( u == 0 ) ? ' ' : '3', 56 + u * 56,
-                             ( v == DES_DECRYPT ) ? "dec" : "enc" );
+                             ( v == MBEDTLS_DES_DECRYPT ) ? "dec" : "enc" );
 
         memcpy( buf, des3_test_buf, 8 );
 
         switch( i )
         {
         case 0:
-            des_setkey_dec( &ctx, des3_test_keys );
+            mbedtls_des_setkey_dec( &ctx, des3_test_keys );
             break;
 
         case 1:
-            des_setkey_enc( &ctx, des3_test_keys );
+            mbedtls_des_setkey_enc( &ctx, des3_test_keys );
             break;
 
         case 2:
-            des3_set2key_dec( &ctx3, des3_test_keys );
+            mbedtls_des3_set2key_dec( &ctx3, des3_test_keys );
             break;
 
         case 3:
-            des3_set2key_enc( &ctx3, des3_test_keys );
+            mbedtls_des3_set2key_enc( &ctx3, des3_test_keys );
             break;
 
         case 4:
-            des3_set3key_dec( &ctx3, des3_test_keys );
+            mbedtls_des3_set3key_dec( &ctx3, des3_test_keys );
             break;
 
         case 5:
-            des3_set3key_enc( &ctx3, des3_test_keys );
+            mbedtls_des3_set3key_enc( &ctx3, des3_test_keys );
             break;
 
         default:
@@ -923,31 +923,31 @@
         for( j = 0; j < 10000; j++ )
         {
             if( u == 0 )
-                des_crypt_ecb( &ctx, buf, buf );
+                mbedtls_des_crypt_ecb( &ctx, buf, buf );
             else
-                des3_crypt_ecb( &ctx3, buf, buf );
+                mbedtls_des3_crypt_ecb( &ctx3, buf, buf );
         }
 
-        if( ( v == DES_DECRYPT &&
+        if( ( v == MBEDTLS_DES_DECRYPT &&
                 memcmp( buf, des3_test_ecb_dec[u], 8 ) != 0 ) ||
-            ( v != DES_DECRYPT &&
+            ( v != MBEDTLS_DES_DECRYPT &&
                 memcmp( buf, des3_test_ecb_enc[u], 8 ) != 0 ) )
         {
             if( verbose != 0 )
-                polarssl_printf( "failed\n" );
+                mbedtls_printf( "failed\n" );
 
             ret = 1;
             goto exit;
         }
 
         if( verbose != 0 )
-            polarssl_printf( "passed\n" );
+            mbedtls_printf( "passed\n" );
     }
 
     if( verbose != 0 )
-        polarssl_printf( "\n" );
+        mbedtls_printf( "\n" );
 
-#if defined(POLARSSL_CIPHER_MODE_CBC)
+#if defined(MBEDTLS_CIPHER_MODE_CBC)
     /*
      * CBC mode
      */
@@ -957,9 +957,9 @@
         v = i  & 1;
 
         if( verbose != 0 )
-            polarssl_printf( "  DES%c-CBC-%3d (%s): ",
+            mbedtls_printf( "  DES%c-CBC-%3d (%s): ",
                              ( u == 0 ) ? ' ' : '3', 56 + u * 56,
-                             ( v == DES_DECRYPT ) ? "dec" : "enc" );
+                             ( v == MBEDTLS_DES_DECRYPT ) ? "dec" : "enc" );
 
         memcpy( iv,  des3_test_iv,  8 );
         memcpy( prv, des3_test_iv,  8 );
@@ -968,41 +968,41 @@
         switch( i )
         {
         case 0:
-            des_setkey_dec( &ctx, des3_test_keys );
+            mbedtls_des_setkey_dec( &ctx, des3_test_keys );
             break;
 
         case 1:
-            des_setkey_enc( &ctx, des3_test_keys );
+            mbedtls_des_setkey_enc( &ctx, des3_test_keys );
             break;
 
         case 2:
-            des3_set2key_dec( &ctx3, des3_test_keys );
+            mbedtls_des3_set2key_dec( &ctx3, des3_test_keys );
             break;
 
         case 3:
-            des3_set2key_enc( &ctx3, des3_test_keys );
+            mbedtls_des3_set2key_enc( &ctx3, des3_test_keys );
             break;
 
         case 4:
-            des3_set3key_dec( &ctx3, des3_test_keys );
+            mbedtls_des3_set3key_dec( &ctx3, des3_test_keys );
             break;
 
         case 5:
-            des3_set3key_enc( &ctx3, des3_test_keys );
+            mbedtls_des3_set3key_enc( &ctx3, des3_test_keys );
             break;
 
         default:
             return( 1 );
         }
 
-        if( v == DES_DECRYPT )
+        if( v == MBEDTLS_DES_DECRYPT )
         {
             for( j = 0; j < 10000; j++ )
             {
                 if( u == 0 )
-                    des_crypt_cbc( &ctx, v, 8, iv, buf, buf );
+                    mbedtls_des_crypt_cbc( &ctx, v, 8, iv, buf, buf );
                 else
-                    des3_crypt_cbc( &ctx3, v, 8, iv, buf, buf );
+                    mbedtls_des3_crypt_cbc( &ctx3, v, 8, iv, buf, buf );
             }
         }
         else
@@ -1012,9 +1012,9 @@
                 unsigned char tmp[8];
 
                 if( u == 0 )
-                    des_crypt_cbc( &ctx, v, 8, iv, buf, buf );
+                    mbedtls_des_crypt_cbc( &ctx, v, 8, iv, buf, buf );
                 else
-                    des3_crypt_cbc( &ctx3, v, 8, iv, buf, buf );
+                    mbedtls_des3_crypt_cbc( &ctx3, v, 8, iv, buf, buf );
 
                 memcpy( tmp, prv, 8 );
                 memcpy( prv, buf, 8 );
@@ -1024,33 +1024,33 @@
             memcpy( buf, prv, 8 );
         }
 
-        if( ( v == DES_DECRYPT &&
+        if( ( v == MBEDTLS_DES_DECRYPT &&
                 memcmp( buf, des3_test_cbc_dec[u], 8 ) != 0 ) ||
-            ( v != DES_DECRYPT &&
+            ( v != MBEDTLS_DES_DECRYPT &&
                 memcmp( buf, des3_test_cbc_enc[u], 8 ) != 0 ) )
         {
             if( verbose != 0 )
-                polarssl_printf( "failed\n" );
+                mbedtls_printf( "failed\n" );
 
             ret = 1;
             goto exit;
         }
 
         if( verbose != 0 )
-            polarssl_printf( "passed\n" );
+            mbedtls_printf( "passed\n" );
     }
-#endif /* POLARSSL_CIPHER_MODE_CBC */
+#endif /* MBEDTLS_CIPHER_MODE_CBC */
 
     if( verbose != 0 )
-        polarssl_printf( "\n" );
+        mbedtls_printf( "\n" );
 
 exit:
-    des_free( &ctx );
-    des3_free( &ctx3 );
+    mbedtls_des_free( &ctx );
+    mbedtls_des3_free( &ctx3 );
 
     return( ret );
 }
 
-#endif /* POLARSSL_SELF_TEST */
+#endif /* MBEDTLS_SELF_TEST */
 
-#endif /* POLARSSL_DES_C */
+#endif /* MBEDTLS_DES_C */
diff --git a/library/dhm.c b/library/dhm.c
index a71e52f..05cbbd9 100644
--- a/library/dhm.c
+++ b/library/dhm.c
@@ -25,61 +25,61 @@
  *  http://www.cacr.math.uwaterloo.ca/hac/ (chapter 12)
  */
 
-#if !defined(POLARSSL_CONFIG_FILE)
+#if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else
-#include POLARSSL_CONFIG_FILE
+#include MBEDTLS_CONFIG_FILE
 #endif
 
-#if defined(POLARSSL_DHM_C)
+#if defined(MBEDTLS_DHM_C)
 
 #include "mbedtls/dhm.h"
 
 #include <string.h>
 
-#if defined(POLARSSL_PEM_PARSE_C)
+#if defined(MBEDTLS_PEM_PARSE_C)
 #include "mbedtls/pem.h"
 #endif
 
-#if defined(POLARSSL_ASN1_PARSE_C)
+#if defined(MBEDTLS_ASN1_PARSE_C)
 #include "mbedtls/asn1.h"
 #endif
 
-#if defined(POLARSSL_PLATFORM_C)
+#if defined(MBEDTLS_PLATFORM_C)
 #include "mbedtls/platform.h"
 #else
 #include <stdlib.h>
 #include <stdio.h>
-#define polarssl_printf     printf
-#define polarssl_malloc     malloc
-#define polarssl_free       free
+#define mbedtls_printf     printf
+#define mbedtls_malloc     malloc
+#define mbedtls_free       free
 #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;
 }
 
 /*
- * helper to validate the mpi size and import it
+ * helper to validate the mbedtls_mpi size and import it
  */
-static int dhm_read_bignum( mpi *X,
+static int dhm_read_bignum( mbedtls_mpi *X,
                             unsigned char **p,
                             const unsigned char *end )
 {
     int ret, n;
 
     if( end - *p < 2 )
-        return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
 
     n = ( (*p)[0] << 8 ) | (*p)[1];
     (*p) += 2;
 
     if( (int)( end - *p ) < n )
-        return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
 
-    if( ( ret = mpi_read_binary( X, *p, n ) ) != 0 )
-        return( POLARSSL_ERR_DHM_READ_PARAMS_FAILED + ret );
+    if( ( ret = mbedtls_mpi_read_binary( X, *p, n ) ) != 0 )
+        return( MBEDTLS_ERR_DHM_READ_PARAMS_FAILED + ret );
 
     (*p) += n;
 
@@ -95,36 +95,36 @@
  *  http://www.cl.cam.ac.uk/~rja14/Papers/psandqs.pdf
  *  http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2005-2643
  */
-static int dhm_check_range( const mpi *param, const mpi *P )
+static int dhm_check_range( const mbedtls_mpi *param, const mbedtls_mpi *P )
 {
-    mpi L, U;
-    int ret = POLARSSL_ERR_DHM_BAD_INPUT_DATA;
+    mbedtls_mpi L, U;
+    int ret = MBEDTLS_ERR_DHM_BAD_INPUT_DATA;
 
-    mpi_init( &L ); mpi_init( &U );
+    mbedtls_mpi_init( &L ); mbedtls_mpi_init( &U );
 
-    MPI_CHK( mpi_lset( &L, 2 ) );
-    MPI_CHK( mpi_sub_int( &U, P, 2 ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &L, 2 ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &U, P, 2 ) );
 
-    if( mpi_cmp_mpi( param, &L ) >= 0 &&
-        mpi_cmp_mpi( param, &U ) <= 0 )
+    if( mbedtls_mpi_cmp_mpi( param, &L ) >= 0 &&
+        mbedtls_mpi_cmp_mpi( param, &U ) <= 0 )
     {
         ret = 0;
     }
 
 cleanup:
-    mpi_free( &L ); mpi_free( &U );
+    mbedtls_mpi_free( &L ); mbedtls_mpi_free( &U );
     return( ret );
 }
 
-void dhm_init( dhm_context *ctx )
+void mbedtls_dhm_init( mbedtls_dhm_context *ctx )
 {
-    memset( ctx, 0, sizeof( dhm_context ) );
+    memset( ctx, 0, sizeof( mbedtls_dhm_context ) );
 }
 
 /*
  * Parse the ServerKeyExchange parameters
  */
-int dhm_read_params( dhm_context *ctx,
+int mbedtls_dhm_read_params( mbedtls_dhm_context *ctx,
                      unsigned char **p,
                      const unsigned char *end )
 {
@@ -138,7 +138,7 @@
     if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
         return( ret );
 
-    ctx->len = mpi_size( &ctx->P );
+    ctx->len = mbedtls_mpi_size( &ctx->P );
 
     return( 0 );
 }
@@ -146,7 +146,7 @@
 /*
  * Setup and write the ServerKeyExchange parameters
  */
-int dhm_make_params( dhm_context *ctx, int x_size,
+int mbedtls_dhm_make_params( mbedtls_dhm_context *ctx, int x_size,
                      unsigned char *output, size_t *olen,
                      int (*f_rng)(void *, unsigned char *, size_t),
                      void *p_rng )
@@ -155,28 +155,28 @@
     size_t n1, n2, n3;
     unsigned char *p;
 
-    if( mpi_cmp_int( &ctx->P, 0 ) == 0 )
-        return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
+    if( mbedtls_mpi_cmp_int( &ctx->P, 0 ) == 0 )
+        return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
 
     /*
      * Generate X as large as possible ( < P )
      */
     do
     {
-        mpi_fill_random( &ctx->X, x_size, f_rng, p_rng );
+        mbedtls_mpi_fill_random( &ctx->X, x_size, f_rng, p_rng );
 
-        while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
-            MPI_CHK( mpi_shift_r( &ctx->X, 1 ) );
+        while( mbedtls_mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
+            MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &ctx->X, 1 ) );
 
         if( count++ > 10 )
-            return( POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED );
+            return( MBEDTLS_ERR_DHM_MAKE_PARAMS_FAILED );
     }
     while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
 
     /*
      * Calculate GX = G^X mod P
      */
-    MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
+    MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
                           &ctx->P , &ctx->RP ) );
 
     if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
@@ -186,13 +186,13 @@
      * export P, G, GX
      */
 #define DHM_MPI_EXPORT(X,n)                     \
-    MPI_CHK( mpi_write_binary( X, p + 2, n ) ); \
+    MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( X, p + 2, n ) ); \
     *p++ = (unsigned char)( n >> 8 );           \
     *p++ = (unsigned char)( n      ); p += n;
 
-    n1 = mpi_size( &ctx->P  );
-    n2 = mpi_size( &ctx->G  );
-    n3 = mpi_size( &ctx->GX );
+    n1 = mbedtls_mpi_size( &ctx->P  );
+    n2 = mbedtls_mpi_size( &ctx->G  );
+    n3 = mbedtls_mpi_size( &ctx->GX );
 
     p = output;
     DHM_MPI_EXPORT( &ctx->P , n1 );
@@ -206,7 +206,7 @@
 cleanup:
 
     if( ret != 0 )
-        return( POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED + ret );
+        return( MBEDTLS_ERR_DHM_MAKE_PARAMS_FAILED + ret );
 
     return( 0 );
 }
@@ -214,16 +214,16 @@
 /*
  * Import the peer's public value G^Y
  */
-int dhm_read_public( dhm_context *ctx,
+int mbedtls_dhm_read_public( mbedtls_dhm_context *ctx,
                      const unsigned char *input, size_t ilen )
 {
     int ret;
 
     if( ctx == NULL || ilen < 1 || ilen > ctx->len )
-        return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
 
-    if( ( ret = mpi_read_binary( &ctx->GY, input, ilen ) ) != 0 )
-        return( POLARSSL_ERR_DHM_READ_PUBLIC_FAILED + ret );
+    if( ( ret = mbedtls_mpi_read_binary( &ctx->GY, input, ilen ) ) != 0 )
+        return( MBEDTLS_ERR_DHM_READ_PUBLIC_FAILED + ret );
 
     return( 0 );
 }
@@ -231,7 +231,7 @@
 /*
  * Create own private value X and export G^X
  */
-int dhm_make_public( dhm_context *ctx, int x_size,
+int mbedtls_dhm_make_public( mbedtls_dhm_context *ctx, int x_size,
                      unsigned char *output, size_t olen,
                      int (*f_rng)(void *, unsigned char *, size_t),
                      void *p_rng )
@@ -239,38 +239,38 @@
     int ret, count = 0;
 
     if( ctx == NULL || olen < 1 || olen > ctx->len )
-        return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
 
-    if( mpi_cmp_int( &ctx->P, 0 ) == 0 )
-        return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
+    if( mbedtls_mpi_cmp_int( &ctx->P, 0 ) == 0 )
+        return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
 
     /*
      * generate X and calculate GX = G^X mod P
      */
     do
     {
-        mpi_fill_random( &ctx->X, x_size, f_rng, p_rng );
+        mbedtls_mpi_fill_random( &ctx->X, x_size, f_rng, p_rng );
 
-        while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
-            MPI_CHK( mpi_shift_r( &ctx->X, 1 ) );
+        while( mbedtls_mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
+            MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &ctx->X, 1 ) );
 
         if( count++ > 10 )
-            return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED );
+            return( MBEDTLS_ERR_DHM_MAKE_PUBLIC_FAILED );
     }
     while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
 
-    MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
+    MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
                           &ctx->P , &ctx->RP ) );
 
     if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
         return( ret );
 
-    MPI_CHK( mpi_write_binary( &ctx->GX, output, olen ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->GX, output, olen ) );
 
 cleanup:
 
     if( ret != 0 )
-        return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED + ret );
+        return( MBEDTLS_ERR_DHM_MAKE_PUBLIC_FAILED + ret );
 
     return( 0 );
 }
@@ -281,7 +281,7 @@
  *  DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
  *  Berlin Heidelberg, 1996. p. 104-113.
  */
-static int dhm_update_blinding( dhm_context *ctx,
+static int dhm_update_blinding( mbedtls_dhm_context *ctx,
                     int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
 {
     int ret, count;
@@ -290,11 +290,11 @@
      * Don't use any blinding the first time a particular X is used,
      * but remember it to use blinding next time.
      */
-    if( mpi_cmp_mpi( &ctx->X, &ctx->pX ) != 0 )
+    if( mbedtls_mpi_cmp_mpi( &ctx->X, &ctx->pX ) != 0 )
     {
-        MPI_CHK( mpi_copy( &ctx->pX, &ctx->X ) );
-        MPI_CHK( mpi_lset( &ctx->Vi, 1 ) );
-        MPI_CHK( mpi_lset( &ctx->Vf, 1 ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &ctx->pX, &ctx->X ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->Vi, 1 ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->Vf, 1 ) );
 
         return( 0 );
     }
@@ -303,13 +303,13 @@
      * Ok, we need blinding. Can we re-use existing values?
      * If yes, just update them by squaring them.
      */
-    if( mpi_cmp_int( &ctx->Vi, 1 ) != 0 )
+    if( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 )
     {
-        MPI_CHK( mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
-        MPI_CHK( mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->P ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->P ) );
 
-        MPI_CHK( mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
-        MPI_CHK( mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->P ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->P ) );
 
         return( 0 );
     }
@@ -322,19 +322,19 @@
     count = 0;
     do
     {
-        mpi_fill_random( &ctx->Vi, mpi_size( &ctx->P ), f_rng, p_rng );
+        mbedtls_mpi_fill_random( &ctx->Vi, mbedtls_mpi_size( &ctx->P ), f_rng, p_rng );
 
-        while( mpi_cmp_mpi( &ctx->Vi, &ctx->P ) >= 0 )
-            MPI_CHK( mpi_shift_r( &ctx->Vi, 1 ) );
+        while( mbedtls_mpi_cmp_mpi( &ctx->Vi, &ctx->P ) >= 0 )
+            MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &ctx->Vi, 1 ) );
 
         if( count++ > 10 )
-            return( POLARSSL_ERR_MPI_NOT_ACCEPTABLE );
+            return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
     }
-    while( mpi_cmp_int( &ctx->Vi, 1 ) <= 0 );
+    while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) <= 0 );
 
     /* Vf = Vi^-X mod P */
-    MPI_CHK( mpi_inv_mod( &ctx->Vf, &ctx->Vi, &ctx->P ) );
-    MPI_CHK( mpi_exp_mod( &ctx->Vf, &ctx->Vf, &ctx->X, &ctx->P, &ctx->RP ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vf, &ctx->Vi, &ctx->P ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->Vf, &ctx->Vf, &ctx->X, &ctx->P, &ctx->RP ) );
 
 cleanup:
     return( ret );
@@ -343,52 +343,52 @@
 /*
  * Derive and export the shared secret (G^Y)^X mod P
  */
-int dhm_calc_secret( dhm_context *ctx,
+int mbedtls_dhm_calc_secret( mbedtls_dhm_context *ctx,
                      unsigned char *output, size_t *olen,
                      int (*f_rng)(void *, unsigned char *, size_t),
                      void *p_rng )
 {
     int ret;
-    mpi GYb;
+    mbedtls_mpi GYb;
 
     if( ctx == NULL || *olen < ctx->len )
-        return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
 
     if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
         return( ret );
 
-    mpi_init( &GYb );
+    mbedtls_mpi_init( &GYb );
 
     /* Blind peer's value */
     if( f_rng != NULL )
     {
-        MPI_CHK( dhm_update_blinding( ctx, f_rng, p_rng ) );
-        MPI_CHK( mpi_mul_mpi( &GYb, &ctx->GY, &ctx->Vi ) );
-        MPI_CHK( mpi_mod_mpi( &GYb, &GYb, &ctx->P ) );
+        MBEDTLS_MPI_CHK( dhm_update_blinding( ctx, f_rng, p_rng ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &GYb, &ctx->GY, &ctx->Vi ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &GYb, &GYb, &ctx->P ) );
     }
     else
-        MPI_CHK( mpi_copy( &GYb, &ctx->GY ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &GYb, &ctx->GY ) );
 
     /* Do modular exponentiation */
-    MPI_CHK( mpi_exp_mod( &ctx->K, &GYb, &ctx->X,
+    MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->K, &GYb, &ctx->X,
                           &ctx->P, &ctx->RP ) );
 
     /* Unblind secret value */
     if( f_rng != NULL )
     {
-        MPI_CHK( mpi_mul_mpi( &ctx->K, &ctx->K, &ctx->Vf ) );
-        MPI_CHK( mpi_mod_mpi( &ctx->K, &ctx->K, &ctx->P ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->K, &ctx->K, &ctx->Vf ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->K, &ctx->K, &ctx->P ) );
     }
 
-    *olen = mpi_size( &ctx->K );
+    *olen = mbedtls_mpi_size( &ctx->K );
 
-    MPI_CHK( mpi_write_binary( &ctx->K, output, *olen ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->K, output, *olen ) );
 
 cleanup:
-    mpi_free( &GYb );
+    mbedtls_mpi_free( &GYb );
 
     if( ret != 0 )
-        return( POLARSSL_ERR_DHM_CALC_SECRET_FAILED + ret );
+        return( MBEDTLS_ERR_DHM_CALC_SECRET_FAILED + ret );
 
     return( 0 );
 }
@@ -396,32 +396,32 @@
 /*
  * Free the components of a DHM key
  */
-void dhm_free( dhm_context *ctx )
+void mbedtls_dhm_free( mbedtls_dhm_context *ctx )
 {
-    mpi_free( &ctx->pX); mpi_free( &ctx->Vf ); mpi_free( &ctx->Vi );
-    mpi_free( &ctx->RP ); mpi_free( &ctx->K ); mpi_free( &ctx->GY );
-    mpi_free( &ctx->GX ); mpi_free( &ctx->X ); mpi_free( &ctx->G );
-    mpi_free( &ctx->P );
+    mbedtls_mpi_free( &ctx->pX); mbedtls_mpi_free( &ctx->Vf ); mbedtls_mpi_free( &ctx->Vi );
+    mbedtls_mpi_free( &ctx->RP ); mbedtls_mpi_free( &ctx->K ); mbedtls_mpi_free( &ctx->GY );
+    mbedtls_mpi_free( &ctx->GX ); mbedtls_mpi_free( &ctx->X ); mbedtls_mpi_free( &ctx->G );
+    mbedtls_mpi_free( &ctx->P );
 
-    polarssl_zeroize( ctx, sizeof( dhm_context ) );
+    mbedtls_zeroize( ctx, sizeof( mbedtls_dhm_context ) );
 }
 
-#if defined(POLARSSL_ASN1_PARSE_C)
+#if defined(MBEDTLS_ASN1_PARSE_C)
 /*
  * Parse DHM parameters
  */
-int dhm_parse_dhm( dhm_context *dhm, const unsigned char *dhmin,
+int mbedtls_dhm_parse_dhm( mbedtls_dhm_context *dhm, const unsigned char *dhmin,
                    size_t dhminlen )
 {
     int ret;
     size_t len;
     unsigned char *p, *end;
-#if defined(POLARSSL_PEM_PARSE_C)
-    pem_context pem;
+#if defined(MBEDTLS_PEM_PARSE_C)
+    mbedtls_pem_context pem;
 
-    pem_init( &pem );
+    mbedtls_pem_init( &pem );
 
-    ret = pem_read_buffer( &pem,
+    ret = mbedtls_pem_read_buffer( &pem,
                            "-----BEGIN DH PARAMETERS-----",
                            "-----END DH PARAMETERS-----",
                            dhmin, NULL, 0, &dhminlen );
@@ -433,13 +433,13 @@
          */
         dhminlen = pem.buflen;
     }
-    else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
+    else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
         goto exit;
 
     p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
 #else
     p = (unsigned char *) dhmin;
-#endif /* POLARSSL_PEM_PARSE_C */
+#endif /* MBEDTLS_PEM_PARSE_C */
     end = p + dhminlen;
 
     /*
@@ -448,44 +448,44 @@
      *      generator        INTEGER,  -- g
      *  }
      */
-    if( ( ret = asn1_get_tag( &p, end, &len,
-            ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
+    if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
+            MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
     {
-        ret = POLARSSL_ERR_DHM_INVALID_FORMAT + ret;
+        ret = MBEDTLS_ERR_DHM_INVALID_FORMAT + ret;
         goto exit;
     }
 
     end = p + len;
 
-    if( ( ret = asn1_get_mpi( &p, end, &dhm->P  ) ) != 0 ||
-        ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
+    if( ( ret = mbedtls_asn1_get_mpi( &p, end, &dhm->P  ) ) != 0 ||
+        ( ret = mbedtls_asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
     {
-        ret = POLARSSL_ERR_DHM_INVALID_FORMAT + ret;
+        ret = MBEDTLS_ERR_DHM_INVALID_FORMAT + ret;
         goto exit;
     }
 
     if( p != end )
     {
-        ret = POLARSSL_ERR_DHM_INVALID_FORMAT +
-              POLARSSL_ERR_ASN1_LENGTH_MISMATCH;
+        ret = MBEDTLS_ERR_DHM_INVALID_FORMAT +
+              MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
         goto exit;
     }
 
     ret = 0;
 
-    dhm->len = mpi_size( &dhm->P );
+    dhm->len = mbedtls_mpi_size( &dhm->P );
 
 exit:
-#if defined(POLARSSL_PEM_PARSE_C)
-    pem_free( &pem );
+#if defined(MBEDTLS_PEM_PARSE_C)
+    mbedtls_pem_free( &pem );
 #endif
     if( ret != 0 )
-        dhm_free( dhm );
+        mbedtls_dhm_free( dhm );
 
     return( ret );
 }
 
-#if defined(POLARSSL_FS_IO)
+#if defined(MBEDTLS_FS_IO)
 /*
  * Load all data from a file into a given buffer.
  */
@@ -495,30 +495,30 @@
     long size;
 
     if( ( f = fopen( path, "rb" ) ) == NULL )
-        return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
+        return( MBEDTLS_ERR_DHM_FILE_IO_ERROR );
 
     fseek( f, 0, SEEK_END );
     if( ( size = ftell( f ) ) == -1 )
     {
         fclose( f );
-        return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
+        return( MBEDTLS_ERR_DHM_FILE_IO_ERROR );
     }
     fseek( f, 0, SEEK_SET );
 
     *n = (size_t) size;
 
     if( *n + 1 == 0 ||
-        ( *buf = polarssl_malloc( *n + 1 ) ) == NULL )
+        ( *buf = mbedtls_malloc( *n + 1 ) ) == NULL )
     {
         fclose( f );
-        return( POLARSSL_ERR_DHM_MALLOC_FAILED );
+        return( MBEDTLS_ERR_DHM_MALLOC_FAILED );
     }
 
     if( fread( *buf, 1, *n, f ) != *n )
     {
         fclose( f );
-        polarssl_free( *buf );
-        return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
+        mbedtls_free( *buf );
+        return( MBEDTLS_ERR_DHM_FILE_IO_ERROR );
     }
 
     fclose( f );
@@ -531,7 +531,7 @@
 /*
  * Load and parse DHM parameters
  */
-int dhm_parse_dhmfile( dhm_context *dhm, const char *path )
+int mbedtls_dhm_parse_dhmfile( mbedtls_dhm_context *dhm, const char *path )
 {
     int ret;
     size_t n;
@@ -540,59 +540,59 @@
     if( ( ret = load_file( path, &buf, &n ) ) != 0 )
         return( ret );
 
-    ret = dhm_parse_dhm( dhm, buf, n );
+    ret = mbedtls_dhm_parse_dhm( dhm, buf, n );
 
-    polarssl_zeroize( buf, n + 1 );
-    polarssl_free( buf );
+    mbedtls_zeroize( buf, n + 1 );
+    mbedtls_free( buf );
 
     return( ret );
 }
-#endif /* POLARSSL_FS_IO */
-#endif /* POLARSSL_ASN1_PARSE_C */
+#endif /* MBEDTLS_FS_IO */
+#endif /* MBEDTLS_ASN1_PARSE_C */
 
-#if defined(POLARSSL_SELF_TEST)
+#if defined(MBEDTLS_SELF_TEST)
 
 #include "mbedtls/certs.h"
 
 /*
  * Checkup routine
  */
-int dhm_self_test( int verbose )
+int mbedtls_dhm_self_test( int verbose )
 {
-#if defined(POLARSSL_CERTS_C)
+#if defined(MBEDTLS_CERTS_C)
     int ret;
-    dhm_context dhm;
+    mbedtls_dhm_context dhm;
 
-    dhm_init( &dhm );
+    mbedtls_dhm_init( &dhm );
 
     if( verbose != 0 )
-        polarssl_printf( "  DHM parameter load: " );
+        mbedtls_printf( "  DHM parameter load: " );
 
-    if( ( ret = dhm_parse_dhm( &dhm, (const unsigned char *) test_dhm_params,
-                               strlen( test_dhm_params ) ) ) != 0 )
+    if( ( ret = mbedtls_dhm_parse_dhm( &dhm, (const unsigned char *) mbedtls_test_dhm_params,
+                               strlen( mbedtls_test_dhm_params ) ) ) != 0 )
     {
         if( verbose != 0 )
-            polarssl_printf( "failed\n" );
+            mbedtls_printf( "failed\n" );
 
         ret = 1;
         goto exit;
     }
 
     if( verbose != 0 )
-        polarssl_printf( "passed\n\n" );
+        mbedtls_printf( "passed\n\n" );
 
 exit:
-    dhm_free( &dhm );
+    mbedtls_dhm_free( &dhm );
 
     return( ret );
 #else
     if( verbose != 0 )
-        polarssl_printf( "  DHM parameter load: skipped\n" );
+        mbedtls_printf( "  DHM parameter load: skipped\n" );
 
     return( 0 );
-#endif /* POLARSSL_CERTS_C */
+#endif /* MBEDTLS_CERTS_C */
 }
 
-#endif /* POLARSSL_SELF_TEST */
+#endif /* MBEDTLS_SELF_TEST */
 
-#endif /* POLARSSL_DHM_C */
+#endif /* MBEDTLS_DHM_C */
diff --git a/library/ecdh.c b/library/ecdh.c
index d620108..8d88977 100644
--- a/library/ecdh.c
+++ b/library/ecdh.c
@@ -27,58 +27,58 @@
  * RFC 4492
  */
 
-#if !defined(POLARSSL_CONFIG_FILE)
+#if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else
-#include POLARSSL_CONFIG_FILE
+#include MBEDTLS_CONFIG_FILE
 #endif
 
-#if defined(POLARSSL_ECDH_C)
+#if defined(MBEDTLS_ECDH_C)
 
 #include "mbedtls/ecdh.h"
 
 #include <string.h>
 
 /*
- * Generate public key: simple wrapper around ecp_gen_keypair
+ * Generate public key: simple wrapper around mbedtls_ecp_gen_keypair
  */
-int ecdh_gen_public( ecp_group *grp, mpi *d, ecp_point *Q,
+int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
                      int (*f_rng)(void *, unsigned char *, size_t),
                      void *p_rng )
 {
-    return ecp_gen_keypair( grp, d, Q, f_rng, p_rng );
+    return mbedtls_ecp_gen_keypair( grp, d, Q, f_rng, p_rng );
 }
 
 /*
  * Compute shared secret (SEC1 3.3.1)
  */
-int ecdh_compute_shared( ecp_group *grp, mpi *z,
-                         const ecp_point *Q, const mpi *d,
+int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
+                         const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
                          int (*f_rng)(void *, unsigned char *, size_t),
                          void *p_rng )
 {
     int ret;
-    ecp_point P;
+    mbedtls_ecp_point P;
 
-    ecp_point_init( &P );
+    mbedtls_ecp_point_init( &P );
 
     /*
      * Make sure Q is a valid pubkey before using it
      */
-    MPI_CHK( ecp_check_pubkey( grp, Q ) );
+    MBEDTLS_MPI_CHK( mbedtls_ecp_check_pubkey( grp, Q ) );
 
-    MPI_CHK( ecp_mul( grp, &P, d, Q, f_rng, p_rng ) );
+    MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, &P, d, Q, f_rng, p_rng ) );
 
-    if( ecp_is_zero( &P ) )
+    if( mbedtls_ecp_is_zero( &P ) )
     {
-        ret = POLARSSL_ERR_ECP_BAD_INPUT_DATA;
+        ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
         goto cleanup;
     }
 
-    MPI_CHK( mpi_copy( z, &P.X ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_copy( z, &P.X ) );
 
 cleanup:
-    ecp_point_free( &P );
+    mbedtls_ecp_point_free( &P );
 
     return( ret );
 }
@@ -86,27 +86,27 @@
 /*
  * Initialize context
  */
-void ecdh_init( ecdh_context *ctx )
+void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx )
 {
-    memset( ctx, 0, sizeof( ecdh_context ) );
+    memset( ctx, 0, sizeof( mbedtls_ecdh_context ) );
 }
 
 /*
  * Free context
  */
-void ecdh_free( ecdh_context *ctx )
+void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx )
 {
     if( ctx == NULL )
         return;
 
-    ecp_group_free( &ctx->grp );
-    ecp_point_free( &ctx->Q   );
-    ecp_point_free( &ctx->Qp  );
-    ecp_point_free( &ctx->Vi  );
-    ecp_point_free( &ctx->Vf  );
-    mpi_free( &ctx->d  );
-    mpi_free( &ctx->z  );
-    mpi_free( &ctx->_d );
+    mbedtls_ecp_group_free( &ctx->grp );
+    mbedtls_ecp_point_free( &ctx->Q   );
+    mbedtls_ecp_point_free( &ctx->Qp  );
+    mbedtls_ecp_point_free( &ctx->Vi  );
+    mbedtls_ecp_point_free( &ctx->Vf  );
+    mbedtls_mpi_free( &ctx->d  );
+    mbedtls_mpi_free( &ctx->z  );
+    mbedtls_mpi_free( &ctx->_d );
 }
 
 /*
@@ -116,7 +116,7 @@
  *          ECPoint         public;
  *      } ServerECDHParams;
  */
-int ecdh_make_params( ecdh_context *ctx, size_t *olen,
+int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
                       unsigned char *buf, size_t blen,
                       int (*f_rng)(void *, unsigned char *, size_t),
                       void *p_rng )
@@ -125,20 +125,20 @@
     size_t grp_len, pt_len;
 
     if( ctx == NULL || ctx->grp.pbits == 0 )
-        return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
 
-    if( ( ret = ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) )
+    if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) )
                 != 0 )
         return( ret );
 
-    if( ( ret = ecp_tls_write_group( &ctx->grp, &grp_len, buf, blen ) )
+    if( ( ret = mbedtls_ecp_tls_write_group( &ctx->grp, &grp_len, buf, blen ) )
                 != 0 )
         return( ret );
 
     buf += grp_len;
     blen -= grp_len;
 
-    if( ( ret = ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
+    if( ( ret = mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
                                      &pt_len, buf, blen ) ) != 0 )
         return( ret );
 
@@ -153,15 +153,15 @@
  *          ECPoint         public;
  *      } ServerECDHParams;
  */
-int ecdh_read_params( ecdh_context *ctx,
+int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
                       const unsigned char **buf, const unsigned char *end )
 {
     int ret;
 
-    if( ( ret = ecp_tls_read_group( &ctx->grp, buf, end - *buf ) ) != 0 )
+    if( ( ret = mbedtls_ecp_tls_read_group( &ctx->grp, buf, end - *buf ) ) != 0 )
         return( ret );
 
-    if( ( ret = ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf, end - *buf ) )
+    if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf, end - *buf ) )
                 != 0 )
         return( ret );
 
@@ -171,24 +171,24 @@
 /*
  * Get parameters from a keypair
  */
-int ecdh_get_params( ecdh_context *ctx, const ecp_keypair *key,
-                     ecdh_side side )
+int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key,
+                     mbedtls_ecdh_side side )
 {
     int ret;
 
-    if( ( ret = ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 )
+    if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 )
         return( ret );
 
     /* If it's not our key, just import the public part as Qp */
-    if( side == POLARSSL_ECDH_THEIRS )
-        return( ecp_copy( &ctx->Qp, &key->Q ) );
+    if( side == MBEDTLS_ECDH_THEIRS )
+        return( mbedtls_ecp_copy( &ctx->Qp, &key->Q ) );
 
     /* Our key: import public (as Q) and private parts */
-    if( side != POLARSSL_ECDH_OURS )
-        return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
+    if( side != MBEDTLS_ECDH_OURS )
+        return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
 
-    if( ( ret = ecp_copy( &ctx->Q, &key->Q ) ) != 0 ||
-        ( ret = mpi_copy( &ctx->d, &key->d ) ) != 0 )
+    if( ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 ||
+        ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 )
         return( ret );
 
     return( 0 );
@@ -197,7 +197,7 @@
 /*
  * Setup and export the client public value
  */
-int ecdh_make_public( ecdh_context *ctx, size_t *olen,
+int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
                       unsigned char *buf, size_t blen,
                       int (*f_rng)(void *, unsigned char *, size_t),
                       void *p_rng )
@@ -205,33 +205,33 @@
     int ret;
 
     if( ctx == NULL || ctx->grp.pbits == 0 )
-        return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
 
-    if( ( ret = ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) )
+    if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) )
                 != 0 )
         return( ret );
 
-    return ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
+    return mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
                                 olen, buf, blen );
 }
 
 /*
  * Parse and import the client's public value
  */
-int ecdh_read_public( ecdh_context *ctx,
+int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
                       const unsigned char *buf, size_t blen )
 {
     int ret;
     const unsigned char *p = buf;
 
     if( ctx == NULL )
-        return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
 
-    if( ( ret = ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p, blen ) ) != 0 )
+    if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p, blen ) ) != 0 )
         return( ret );
 
     if( (size_t)( p - buf ) != blen )
-        return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
 
     return( 0 );
 }
@@ -239,7 +239,7 @@
 /*
  * Derive and export the shared secret
  */
-int ecdh_calc_secret( ecdh_context *ctx, size_t *olen,
+int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
                       unsigned char *buf, size_t blen,
                       int (*f_rng)(void *, unsigned char *, size_t),
                       void *p_rng )
@@ -247,19 +247,19 @@
     int ret;
 
     if( ctx == NULL )
-        return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
 
-    if( ( ret = ecdh_compute_shared( &ctx->grp, &ctx->z, &ctx->Qp, &ctx->d,
+    if( ( ret = mbedtls_ecdh_compute_shared( &ctx->grp, &ctx->z, &ctx->Qp, &ctx->d,
                                      f_rng, p_rng ) ) != 0 )
     {
         return( ret );
     }
 
-    if( mpi_size( &ctx->z ) > blen )
-        return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
+    if( mbedtls_mpi_size( &ctx->z ) > blen )
+        return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
 
     *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 );
-    return mpi_write_binary( &ctx->z, buf, *olen );
+    return mbedtls_mpi_write_binary( &ctx->z, buf, *olen );
 }
 
-#endif /* POLARSSL_ECDH_C */
+#endif /* MBEDTLS_ECDH_C */
diff --git a/library/ecdsa.c b/library/ecdsa.c
index b4cdfca..0e864fc 100644
--- a/library/ecdsa.c
+++ b/library/ecdsa.c
@@ -26,20 +26,20 @@
  * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
  */
 
-#if !defined(POLARSSL_CONFIG_FILE)
+#if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else
-#include POLARSSL_CONFIG_FILE
+#include MBEDTLS_CONFIG_FILE
 #endif
 
-#if defined(POLARSSL_ECDSA_C)
+#if defined(MBEDTLS_ECDSA_C)
 
 #include "mbedtls/ecdsa.h"
 #include "mbedtls/asn1write.h"
 
 #include <string.h>
 
-#if defined(POLARSSL_ECDSA_DETERMINISTIC)
+#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
 #include "mbedtls/hmac_drbg.h"
 #endif
 
@@ -47,20 +47,20 @@
  * Derive a suitable integer for group grp from a buffer of length len
  * SEC1 4.1.3 step 5 aka SEC1 4.1.4 step 3
  */
-static int derive_mpi( const ecp_group *grp, mpi *x,
+static int derive_mpi( const mbedtls_ecp_group *grp, mbedtls_mpi *x,
                        const unsigned char *buf, size_t blen )
 {
     int ret;
     size_t n_size = ( grp->nbits + 7 ) / 8;
     size_t use_size = blen > n_size ? n_size : blen;
 
-    MPI_CHK( mpi_read_binary( x, buf, use_size ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( x, buf, use_size ) );
     if( use_size * 8 > grp->nbits )
-        MPI_CHK( mpi_shift_r( x, use_size * 8 - grp->nbits ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( x, use_size * 8 - grp->nbits ) );
 
     /* While at it, reduce modulo N */
-    if( mpi_cmp_mpi( x, &grp->N ) >= 0 )
-        MPI_CHK( mpi_sub_mpi( x, x, &grp->N ) );
+    if( mbedtls_mpi_cmp_mpi( x, &grp->N ) >= 0 )
+        MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( x, x, &grp->N ) );
 
 cleanup:
     return( ret );
@@ -70,20 +70,20 @@
  * Compute ECDSA signature of a hashed message (SEC1 4.1.3)
  * Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message)
  */
-int ecdsa_sign( ecp_group *grp, mpi *r, mpi *s,
-                const mpi *d, const unsigned char *buf, size_t blen,
+int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
+                const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
                 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
 {
     int ret, key_tries, sign_tries, blind_tries;
-    ecp_point R;
-    mpi k, e, t;
+    mbedtls_ecp_point R;
+    mbedtls_mpi k, e, t;
 
     /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
     if( grp->N.p == NULL )
-        return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
 
-    ecp_point_init( &R );
-    mpi_init( &k ); mpi_init( &e ); mpi_init( &t );
+    mbedtls_ecp_point_init( &R );
+    mbedtls_mpi_init( &k ); mbedtls_mpi_init( &e ); mbedtls_mpi_init( &t );
 
     sign_tries = 0;
     do
@@ -95,21 +95,21 @@
         key_tries = 0;
         do
         {
-            MPI_CHK( ecp_gen_keypair( grp, &k, &R, f_rng, p_rng ) );
-            MPI_CHK( mpi_mod_mpi( r, &R.X, &grp->N ) );
+            MBEDTLS_MPI_CHK( mbedtls_ecp_gen_keypair( grp, &k, &R, f_rng, p_rng ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( r, &R.X, &grp->N ) );
 
             if( key_tries++ > 10 )
             {
-                ret = POLARSSL_ERR_ECP_RANDOM_FAILED;
+                ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
                 goto cleanup;
             }
         }
-        while( mpi_cmp_int( r, 0 ) == 0 );
+        while( mbedtls_mpi_cmp_int( r, 0 ) == 0 );
 
         /*
          * Step 5: derive MPI from hashed message
          */
-        MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
+        MBEDTLS_MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
 
         /*
          * Generate a random value to blind inv_mod in next step,
@@ -119,143 +119,143 @@
         do
         {
             size_t n_size = ( grp->nbits + 7 ) / 8;
-            MPI_CHK( mpi_fill_random( &t, n_size, f_rng, p_rng ) );
-            MPI_CHK( mpi_shift_r( &t, 8 * n_size - grp->nbits ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &t, n_size, f_rng, p_rng ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &t, 8 * n_size - grp->nbits ) );
 
-            /* See ecp_gen_keypair() */
+            /* See mbedtls_ecp_gen_keypair() */
             if( ++blind_tries > 30 )
-                return( POLARSSL_ERR_ECP_RANDOM_FAILED );
+                return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
         }
-        while( mpi_cmp_int( &t, 1 ) < 0 ||
-               mpi_cmp_mpi( &t, &grp->N ) >= 0 );
+        while( mbedtls_mpi_cmp_int( &t, 1 ) < 0 ||
+               mbedtls_mpi_cmp_mpi( &t, &grp->N ) >= 0 );
 
         /*
          * Step 6: compute s = (e + r * d) / k = t (e + rd) / (kt) mod n
          */
-        MPI_CHK( mpi_mul_mpi( s, r, d ) );
-        MPI_CHK( mpi_add_mpi( &e, &e, s ) );
-        MPI_CHK( mpi_mul_mpi( &e, &e, &t ) );
-        MPI_CHK( mpi_mul_mpi( &k, &k, &t ) );
-        MPI_CHK( mpi_inv_mod( s, &k, &grp->N ) );
-        MPI_CHK( mpi_mul_mpi( s, s, &e ) );
-        MPI_CHK( mpi_mod_mpi( s, s, &grp->N ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( s, r, d ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &e, &e, s ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &e, &e, &t ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &k, &k, &t ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( s, &k, &grp->N ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( s, s, &e ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( s, s, &grp->N ) );
 
         if( sign_tries++ > 10 )
         {
-            ret = POLARSSL_ERR_ECP_RANDOM_FAILED;
+            ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
             goto cleanup;
         }
     }
-    while( mpi_cmp_int( s, 0 ) == 0 );
+    while( mbedtls_mpi_cmp_int( s, 0 ) == 0 );
 
 cleanup:
-    ecp_point_free( &R );
-    mpi_free( &k ); mpi_free( &e ); mpi_free( &t );
+    mbedtls_ecp_point_free( &R );
+    mbedtls_mpi_free( &k ); mbedtls_mpi_free( &e ); mbedtls_mpi_free( &t );
 
     return( ret );
 }
 
-#if defined(POLARSSL_ECDSA_DETERMINISTIC)
+#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
 /*
  * Deterministic signature wrapper
  */
-int ecdsa_sign_det( ecp_group *grp, mpi *r, mpi *s,
-                    const mpi *d, const unsigned char *buf, size_t blen,
-                    md_type_t md_alg )
+int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
+                    const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
+                    mbedtls_md_type_t md_alg )
 {
     int ret;
-    hmac_drbg_context rng_ctx;
-    unsigned char data[2 * POLARSSL_ECP_MAX_BYTES];
+    mbedtls_hmac_drbg_context rng_ctx;
+    unsigned char data[2 * MBEDTLS_ECP_MAX_BYTES];
     size_t grp_len = ( grp->nbits + 7 ) / 8;
-    const md_info_t *md_info;
-    mpi h;
+    const mbedtls_md_info_t *md_info;
+    mbedtls_mpi h;
 
-    if( ( md_info = md_info_from_type( md_alg ) ) == NULL )
-        return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
+    if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
+        return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
 
-    mpi_init( &h );
-    memset( &rng_ctx, 0, sizeof( hmac_drbg_context ) );
+    mbedtls_mpi_init( &h );
+    memset( &rng_ctx, 0, sizeof( mbedtls_hmac_drbg_context ) );
 
     /* Use private key and message hash (reduced) to initialize HMAC_DRBG */
-    MPI_CHK( mpi_write_binary( d, data, grp_len ) );
-    MPI_CHK( derive_mpi( grp, &h, buf, blen ) );
-    MPI_CHK( mpi_write_binary( &h, data + grp_len, grp_len ) );
-    hmac_drbg_init_buf( &rng_ctx, md_info, data, 2 * grp_len );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( d, data, grp_len ) );
+    MBEDTLS_MPI_CHK( derive_mpi( grp, &h, buf, blen ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &h, data + grp_len, grp_len ) );
+    mbedtls_hmac_drbg_init_buf( &rng_ctx, md_info, data, 2 * grp_len );
 
-    ret = ecdsa_sign( grp, r, s, d, buf, blen,
-                      hmac_drbg_random, &rng_ctx );
+    ret = mbedtls_ecdsa_sign( grp, r, s, d, buf, blen,
+                      mbedtls_hmac_drbg_random, &rng_ctx );
 
 cleanup:
-    hmac_drbg_free( &rng_ctx );
-    mpi_free( &h );
+    mbedtls_hmac_drbg_free( &rng_ctx );
+    mbedtls_mpi_free( &h );
 
     return( ret );
 }
-#endif /* POLARSSL_ECDSA_DETERMINISTIC */
+#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
 
 /*
  * Verify ECDSA signature of hashed message (SEC1 4.1.4)
  * Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message)
  */
-int ecdsa_verify( ecp_group *grp,
+int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
                   const unsigned char *buf, size_t blen,
-                  const ecp_point *Q, const mpi *r, const mpi *s)
+                  const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s)
 {
     int ret;
-    mpi e, s_inv, u1, u2;
-    ecp_point R, P;
+    mbedtls_mpi e, s_inv, u1, u2;
+    mbedtls_ecp_point R, P;
 
-    ecp_point_init( &R ); ecp_point_init( &P );
-    mpi_init( &e ); mpi_init( &s_inv ); mpi_init( &u1 ); mpi_init( &u2 );
+    mbedtls_ecp_point_init( &R ); mbedtls_ecp_point_init( &P );
+    mbedtls_mpi_init( &e ); mbedtls_mpi_init( &s_inv ); mbedtls_mpi_init( &u1 ); mbedtls_mpi_init( &u2 );
 
     /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
     if( grp->N.p == NULL )
-        return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
 
     /*
      * Step 1: make sure r and s are in range 1..n-1
      */
-    if( mpi_cmp_int( r, 1 ) < 0 || mpi_cmp_mpi( r, &grp->N ) >= 0 ||
-        mpi_cmp_int( s, 1 ) < 0 || mpi_cmp_mpi( s, &grp->N ) >= 0 )
+    if( mbedtls_mpi_cmp_int( r, 1 ) < 0 || mbedtls_mpi_cmp_mpi( r, &grp->N ) >= 0 ||
+        mbedtls_mpi_cmp_int( s, 1 ) < 0 || mbedtls_mpi_cmp_mpi( s, &grp->N ) >= 0 )
     {
-        ret = POLARSSL_ERR_ECP_VERIFY_FAILED;
+        ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
         goto cleanup;
     }
 
     /*
      * Additional precaution: make sure Q is valid
      */
-    MPI_CHK( ecp_check_pubkey( grp, Q ) );
+    MBEDTLS_MPI_CHK( mbedtls_ecp_check_pubkey( grp, Q ) );
 
     /*
      * Step 3: derive MPI from hashed message
      */
-    MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
+    MBEDTLS_MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
 
     /*
      * Step 4: u1 = e / s mod n, u2 = r / s mod n
      */
-    MPI_CHK( mpi_inv_mod( &s_inv, s, &grp->N ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &s_inv, s, &grp->N ) );
 
-    MPI_CHK( mpi_mul_mpi( &u1, &e, &s_inv ) );
-    MPI_CHK( mpi_mod_mpi( &u1, &u1, &grp->N ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &u1, &e, &s_inv ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &u1, &u1, &grp->N ) );
 
-    MPI_CHK( mpi_mul_mpi( &u2, r, &s_inv ) );
-    MPI_CHK( mpi_mod_mpi( &u2, &u2, &grp->N ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &u2, r, &s_inv ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &u2, &u2, &grp->N ) );
 
     /*
      * Step 5: R = u1 G + u2 Q
      *
      * Since we're not using any secret data, no need to pass a RNG to
-     * ecp_mul() for countermesures.
+     * mbedtls_ecp_mul() for countermesures.
      */
-    MPI_CHK( ecp_mul( grp, &R, &u1, &grp->G, NULL, NULL ) );
-    MPI_CHK( ecp_mul( grp, &P, &u2, Q, NULL, NULL ) );
-    MPI_CHK( ecp_add( grp, &R, &R, &P ) );
+    MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, &R, &u1, &grp->G, NULL, NULL ) );
+    MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, &P, &u2, Q, NULL, NULL ) );
+    MBEDTLS_MPI_CHK( mbedtls_ecp_add( grp, &R, &R, &P ) );
 
-    if( ecp_is_zero( &R ) )
+    if( mbedtls_ecp_is_zero( &R ) )
     {
-        ret = POLARSSL_ERR_ECP_VERIFY_FAILED;
+        ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
         goto cleanup;
     }
 
@@ -263,20 +263,20 @@
      * Step 6: convert xR to an integer (no-op)
      * Step 7: reduce xR mod n (gives v)
      */
-    MPI_CHK( mpi_mod_mpi( &R.X, &R.X, &grp->N ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &R.X, &R.X, &grp->N ) );
 
     /*
      * Step 8: check if v (that is, R.X) is equal to r
      */
-    if( mpi_cmp_mpi( &R.X, r ) != 0 )
+    if( mbedtls_mpi_cmp_mpi( &R.X, r ) != 0 )
     {
-        ret = POLARSSL_ERR_ECP_VERIFY_FAILED;
+        ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
         goto cleanup;
     }
 
 cleanup:
-    ecp_point_free( &R ); ecp_point_free( &P );
-    mpi_free( &e ); mpi_free( &s_inv ); mpi_free( &u1 ); mpi_free( &u2 );
+    mbedtls_ecp_point_free( &R ); mbedtls_ecp_point_free( &P );
+    mbedtls_mpi_free( &e ); mbedtls_mpi_free( &s_inv ); mbedtls_mpi_free( &u1 ); mbedtls_mpi_free( &u2 );
 
     return( ret );
 }
@@ -284,20 +284,20 @@
 /*
  * Convert a signature (given by context) to ASN.1
  */
-static int ecdsa_signature_to_asn1( const mpi *r, const mpi *s,
+static int ecdsa_signature_to_asn1( const mbedtls_mpi *r, const mbedtls_mpi *s,
                                     unsigned char *sig, size_t *slen )
 {
     int ret;
-    unsigned char buf[POLARSSL_ECDSA_MAX_LEN];
+    unsigned char buf[MBEDTLS_ECDSA_MAX_LEN];
     unsigned char *p = buf + sizeof( buf );
     size_t len = 0;
 
-    ASN1_CHK_ADD( len, asn1_write_mpi( &p, buf, s ) );
-    ASN1_CHK_ADD( len, asn1_write_mpi( &p, buf, r ) );
+    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &p, buf, s ) );
+    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &p, buf, r ) );
 
-    ASN1_CHK_ADD( len, asn1_write_len( &p, buf, len ) );
-    ASN1_CHK_ADD( len, asn1_write_tag( &p, buf,
-                                       ASN1_CONSTRUCTED | ASN1_SEQUENCE ) );
+    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &p, buf, len ) );
+    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &p, buf,
+                                       MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
 
     memcpy( sig, p, len );
     *slen = len;
@@ -308,48 +308,48 @@
 /*
  * Compute and write signature
  */
-int ecdsa_write_signature( ecdsa_context *ctx, md_type_t md_alg,
+int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, mbedtls_md_type_t md_alg,
                            const unsigned char *hash, size_t hlen,
                            unsigned char *sig, size_t *slen,
                            int (*f_rng)(void *, unsigned char *, size_t),
                            void *p_rng )
 {
     int ret;
-    mpi r, s;
+    mbedtls_mpi r, s;
 
-    mpi_init( &r );
-    mpi_init( &s );
+    mbedtls_mpi_init( &r );
+    mbedtls_mpi_init( &s );
 
-#if defined(POLARSSL_ECDSA_DETERMINISTIC)
+#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
     (void) f_rng;
     (void) p_rng;
 
-    MPI_CHK( ecdsa_sign_det( &ctx->grp, &r, &s, &ctx->d,
+    MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ctx->grp, &r, &s, &ctx->d,
                              hash, hlen, md_alg ) );
 #else
     (void) md_alg;
 
-    MPI_CHK( ecdsa_sign( &ctx->grp, &r, &s, &ctx->d,
+    MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ctx->grp, &r, &s, &ctx->d,
                          hash, hlen, f_rng, p_rng ) );
 #endif
 
-    MPI_CHK( ecdsa_signature_to_asn1( &r, &s, sig, slen ) );
+    MBEDTLS_MPI_CHK( ecdsa_signature_to_asn1( &r, &s, sig, slen ) );
 
 cleanup:
-    mpi_free( &r );
-    mpi_free( &s );
+    mbedtls_mpi_free( &r );
+    mbedtls_mpi_free( &s );
 
     return( ret );
 }
 
-#if ! defined(POLARSSL_DEPRECATED_REMOVED) && \
-    defined(POLARSSL_ECDSA_DETERMINISTIC)
-int ecdsa_write_signature_det( ecdsa_context *ctx,
+#if ! defined(MBEDTLS_DEPRECATED_REMOVED) && \
+    defined(MBEDTLS_ECDSA_DETERMINISTIC)
+int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
                                const unsigned char *hash, size_t hlen,
                                unsigned char *sig, size_t *slen,
-                               md_type_t md_alg )
+                               mbedtls_md_type_t md_alg )
 {
-    return( ecdsa_write_signature( ctx, md_alg, hash, hlen, sig, slen,
+    return( mbedtls_ecdsa_write_signature( ctx, md_alg, hash, hlen, sig, slen,
                                    NULL, NULL ) );
 }
 #endif
@@ -357,7 +357,7 @@
 /*
  * Read and check signature
  */
-int ecdsa_read_signature( ecdsa_context *ctx,
+int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
                           const unsigned char *hash, size_t hlen,
                           const unsigned char *sig, size_t slen )
 {
@@ -365,42 +365,42 @@
     unsigned char *p = (unsigned char *) sig;
     const unsigned char *end = sig + slen;
     size_t len;
-    mpi r, s;
+    mbedtls_mpi r, s;
 
-    mpi_init( &r );
-    mpi_init( &s );
+    mbedtls_mpi_init( &r );
+    mbedtls_mpi_init( &s );
 
-    if( ( ret = asn1_get_tag( &p, end, &len,
-                    ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
+    if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
+                    MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
     {
-        ret += POLARSSL_ERR_ECP_BAD_INPUT_DATA;
+        ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
         goto cleanup;
     }
 
     if( p + len != end )
     {
-        ret = POLARSSL_ERR_ECP_BAD_INPUT_DATA +
-              POLARSSL_ERR_ASN1_LENGTH_MISMATCH;
+        ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA +
+              MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
         goto cleanup;
     }
 
-    if( ( ret = asn1_get_mpi( &p, end, &r ) ) != 0 ||
-        ( ret = asn1_get_mpi( &p, end, &s ) ) != 0 )
+    if( ( ret = mbedtls_asn1_get_mpi( &p, end, &r ) ) != 0 ||
+        ( ret = mbedtls_asn1_get_mpi( &p, end, &s ) ) != 0 )
     {
-        ret += POLARSSL_ERR_ECP_BAD_INPUT_DATA;
+        ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
         goto cleanup;
     }
 
-    if( ( ret = ecdsa_verify( &ctx->grp, hash, hlen,
+    if( ( ret = mbedtls_ecdsa_verify( &ctx->grp, hash, hlen,
                               &ctx->Q, &r, &s ) ) != 0 )
         goto cleanup;
 
     if( p != end )
-        ret = POLARSSL_ERR_ECP_SIG_LEN_MISMATCH;
+        ret = MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH;
 
 cleanup:
-    mpi_free( &r );
-    mpi_free( &s );
+    mbedtls_mpi_free( &r );
+    mbedtls_mpi_free( &s );
 
     return( ret );
 }
@@ -408,25 +408,25 @@
 /*
  * Generate key pair
  */
-int ecdsa_genkey( ecdsa_context *ctx, ecp_group_id gid,
+int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
                   int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
 {
-    return( ecp_use_known_dp( &ctx->grp, gid ) ||
-            ecp_gen_keypair( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) );
+    return( mbedtls_ecp_use_known_dp( &ctx->grp, gid ) ||
+            mbedtls_ecp_gen_keypair( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) );
 }
 
 /*
- * Set context from an ecp_keypair
+ * Set context from an mbedtls_ecp_keypair
  */
-int ecdsa_from_keypair( ecdsa_context *ctx, const ecp_keypair *key )
+int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key )
 {
     int ret;
 
-    if( ( ret = ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 ||
-        ( ret = mpi_copy( &ctx->d, &key->d ) ) != 0 ||
-        ( ret = ecp_copy( &ctx->Q, &key->Q ) ) != 0 )
+    if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 ||
+        ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 ||
+        ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 )
     {
-        ecdsa_free( ctx );
+        mbedtls_ecdsa_free( ctx );
     }
 
     return( ret );
@@ -435,17 +435,17 @@
 /*
  * Initialize context
  */
-void ecdsa_init( ecdsa_context *ctx )
+void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx )
 {
-    ecp_keypair_init( ctx );
+    mbedtls_ecp_keypair_init( ctx );
 }
 
 /*
  * Free context
  */
-void ecdsa_free( ecdsa_context *ctx )
+void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx )
 {
-    ecp_keypair_free( ctx );
+    mbedtls_ecp_keypair_free( ctx );
 }
 
-#endif /* POLARSSL_ECDSA_C */
+#endif /* MBEDTLS_ECDSA_C */
diff --git a/library/ecp.c b/library/ecp.c
index f04b6fe..6019500 100644
--- a/library/ecp.c
+++ b/library/ecp.c
@@ -41,26 +41,26 @@
  *     <http://eprint.iacr.org/2004/342.pdf>
  */
 
-#if !defined(POLARSSL_CONFIG_FILE)
+#if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else
-#include POLARSSL_CONFIG_FILE
+#include MBEDTLS_CONFIG_FILE
 #endif
 
-#if defined(POLARSSL_ECP_C)
+#if defined(MBEDTLS_ECP_C)
 
 #include "mbedtls/ecp.h"
 
 #include <string.h>
 
-#if defined(POLARSSL_PLATFORM_C)
+#if defined(MBEDTLS_PLATFORM_C)
 #include "mbedtls/platform.h"
 #else
 #include <stdlib.h>
 #include <stdio.h>
-#define polarssl_printf     printf
-#define polarssl_malloc     malloc
-#define polarssl_free       free
+#define mbedtls_printf     printf
+#define mbedtls_malloc     malloc
+#define mbedtls_free       free
 #endif
 
 #if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
@@ -77,11 +77,11 @@
 #endif /*_MSC_VER */
 
 /* 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;
 }
 
-#if defined(POLARSSL_SELF_TEST)
+#if defined(MBEDTLS_SELF_TEST)
 /*
  * Counts of point addition and doubling, and field multiplications.
  * Used to test resistance of point multiplication to simple timing attacks.
@@ -89,25 +89,25 @@
 static unsigned long add_count, dbl_count, mul_count;
 #endif
 
-#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED) ||   \
-    defined(POLARSSL_ECP_DP_SECP224R1_ENABLED) ||   \
-    defined(POLARSSL_ECP_DP_SECP256R1_ENABLED) ||   \
-    defined(POLARSSL_ECP_DP_SECP384R1_ENABLED) ||   \
-    defined(POLARSSL_ECP_DP_SECP521R1_ENABLED) ||   \
-    defined(POLARSSL_ECP_DP_BP256R1_ENABLED)   ||   \
-    defined(POLARSSL_ECP_DP_BP384R1_ENABLED)   ||   \
-    defined(POLARSSL_ECP_DP_BP512R1_ENABLED)   ||   \
-    defined(POLARSSL_ECP_DP_SECP192K1_ENABLED) ||   \
-    defined(POLARSSL_ECP_DP_SECP224K1_ENABLED) ||   \
-    defined(POLARSSL_ECP_DP_SECP256K1_ENABLED)
-#define POLARSSL_ECP_SHORT_WEIERSTRASS
+#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) ||   \
+    defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) ||   \
+    defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) ||   \
+    defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) ||   \
+    defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) ||   \
+    defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)   ||   \
+    defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)   ||   \
+    defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)   ||   \
+    defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) ||   \
+    defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) ||   \
+    defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
+#define MBEDTLS_ECP_SHORT_WEIERSTRASS
 #endif
 
-#if defined(POLARSSL_ECP_DP_M221_ENABLED) ||   \
-    defined(POLARSSL_ECP_DP_M255_ENABLED) ||   \
-    defined(POLARSSL_ECP_DP_M383_ENABLED) ||   \
-    defined(POLARSSL_ECP_DP_M511_ENABLED)
-#define POLARSSL_ECP_MONTGOMERY
+#if defined(MBEDTLS_ECP_DP_M221_ENABLED) ||   \
+    defined(MBEDTLS_ECP_DP_M255_ENABLED) ||   \
+    defined(MBEDTLS_ECP_DP_M383_ENABLED) ||   \
+    defined(MBEDTLS_ECP_DP_M511_ENABLED)
+#define MBEDTLS_ECP_MONTGOMERY
 #endif
 
 /*
@@ -115,9 +115,9 @@
  */
 typedef enum
 {
-    POLARSSL_ECP_TYPE_NONE = 0,
-    POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS,    /* y^2 = x^3 + a x + b      */
-    POLARSSL_ECP_TYPE_MONTGOMERY,           /* y^2 = x^3 + a x^2 + x    */
+    MBEDTLS_ECP_TYPE_NONE = 0,
+    MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS,    /* y^2 = x^3 + a x + b      */
+    MBEDTLS_ECP_TYPE_MONTGOMERY,           /* y^2 = x^3 + a x^2 + x    */
 } ecp_curve_type;
 
 /*
@@ -130,53 +130,53 @@
  * Curves are listed in order: largest curves first, and for a given size,
  * fastest curves first. This provides the default order for the SSL module.
  */
-static const ecp_curve_info ecp_supported_curves[] =
+static const mbedtls_ecp_curve_info ecp_supported_curves[] =
 {
-#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
-    { POLARSSL_ECP_DP_SECP521R1,    25,     521,    "secp521r1"         },
+#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
+    { MBEDTLS_ECP_DP_SECP521R1,    25,     521,    "secp521r1"         },
 #endif
-#if defined(POLARSSL_ECP_DP_BP512R1_ENABLED)
-    { POLARSSL_ECP_DP_BP512R1,      28,     512,    "brainpoolP512r1"   },
+#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
+    { MBEDTLS_ECP_DP_BP512R1,      28,     512,    "brainpoolP512r1"   },
 #endif
-#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
-    { POLARSSL_ECP_DP_SECP384R1,    24,     384,    "secp384r1"         },
+#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
+    { MBEDTLS_ECP_DP_SECP384R1,    24,     384,    "secp384r1"         },
 #endif
-#if defined(POLARSSL_ECP_DP_BP384R1_ENABLED)
-    { POLARSSL_ECP_DP_BP384R1,      27,     384,    "brainpoolP384r1"   },
+#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
+    { MBEDTLS_ECP_DP_BP384R1,      27,     384,    "brainpoolP384r1"   },
 #endif
-#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
-    { POLARSSL_ECP_DP_SECP256R1,    23,     256,    "secp256r1"         },
+#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
+    { MBEDTLS_ECP_DP_SECP256R1,    23,     256,    "secp256r1"         },
 #endif
-#if defined(POLARSSL_ECP_DP_SECP256K1_ENABLED)
-    { POLARSSL_ECP_DP_SECP256K1,    22,     256,    "secp256k1"         },
+#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
+    { MBEDTLS_ECP_DP_SECP256K1,    22,     256,    "secp256k1"         },
 #endif
-#if defined(POLARSSL_ECP_DP_BP256R1_ENABLED)
-    { POLARSSL_ECP_DP_BP256R1,      26,     256,    "brainpoolP256r1"   },
+#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
+    { MBEDTLS_ECP_DP_BP256R1,      26,     256,    "brainpoolP256r1"   },
 #endif
-#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
-    { POLARSSL_ECP_DP_SECP224R1,    21,     224,    "secp224r1"         },
+#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
+    { MBEDTLS_ECP_DP_SECP224R1,    21,     224,    "secp224r1"         },
 #endif
-#if defined(POLARSSL_ECP_DP_SECP224K1_ENABLED)
-    { POLARSSL_ECP_DP_SECP224K1,    20,     224,    "secp224k1"         },
+#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
+    { MBEDTLS_ECP_DP_SECP224K1,    20,     224,    "secp224k1"         },
 #endif
-#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
-    { POLARSSL_ECP_DP_SECP192R1,    19,     192,    "secp192r1"         },
+#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
+    { MBEDTLS_ECP_DP_SECP192R1,    19,     192,    "secp192r1"         },
 #endif
-#if defined(POLARSSL_ECP_DP_SECP192K1_ENABLED)
-    { POLARSSL_ECP_DP_SECP192K1,    18,     192,    "secp192k1"         },
+#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
+    { MBEDTLS_ECP_DP_SECP192K1,    18,     192,    "secp192k1"         },
 #endif
-    { POLARSSL_ECP_DP_NONE,          0,     0,      NULL                },
+    { MBEDTLS_ECP_DP_NONE,          0,     0,      NULL                },
 };
 
 #define ECP_NB_CURVES   sizeof( ecp_supported_curves ) /    \
                         sizeof( ecp_supported_curves[0] )
 
-static ecp_group_id ecp_supported_grp_id[ECP_NB_CURVES];
+static mbedtls_ecp_group_id ecp_supported_grp_id[ECP_NB_CURVES];
 
 /*
  * List of supported curves and associated info
  */
-const ecp_curve_info *ecp_curve_list( void )
+const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void )
 {
     return( ecp_supported_curves );
 }
@@ -184,22 +184,22 @@
 /*
  * List of supported curves, group ID only
  */
-const ecp_group_id *ecp_grp_id_list( void )
+const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void )
 {
     static int init_done = 0;
 
     if( ! init_done )
     {
         size_t i = 0;
-        const ecp_curve_info *curve_info;
+        const mbedtls_ecp_curve_info *curve_info;
 
-        for( curve_info = ecp_curve_list();
-             curve_info->grp_id != POLARSSL_ECP_DP_NONE;
+        for( curve_info = mbedtls_ecp_curve_list();
+             curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
              curve_info++ )
         {
             ecp_supported_grp_id[i++] = curve_info->grp_id;
         }
-        ecp_supported_grp_id[i] = POLARSSL_ECP_DP_NONE;
+        ecp_supported_grp_id[i] = MBEDTLS_ECP_DP_NONE;
 
         init_done = 1;
     }
@@ -210,12 +210,12 @@
 /*
  * Get the curve info for the internal identifier
  */
-const ecp_curve_info *ecp_curve_info_from_grp_id( ecp_group_id grp_id )
+const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id( mbedtls_ecp_group_id grp_id )
 {
-    const ecp_curve_info *curve_info;
+    const mbedtls_ecp_curve_info *curve_info;
 
-    for( curve_info = ecp_curve_list();
-         curve_info->grp_id != POLARSSL_ECP_DP_NONE;
+    for( curve_info = mbedtls_ecp_curve_list();
+         curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
          curve_info++ )
     {
         if( curve_info->grp_id == grp_id )
@@ -228,12 +228,12 @@
 /*
  * Get the curve info from the TLS identifier
  */
-const ecp_curve_info *ecp_curve_info_from_tls_id( uint16_t tls_id )
+const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id )
 {
-    const ecp_curve_info *curve_info;
+    const mbedtls_ecp_curve_info *curve_info;
 
-    for( curve_info = ecp_curve_list();
-         curve_info->grp_id != POLARSSL_ECP_DP_NONE;
+    for( curve_info = mbedtls_ecp_curve_list();
+         curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
          curve_info++ )
     {
         if( curve_info->tls_id == tls_id )
@@ -246,12 +246,12 @@
 /*
  * Get the curve info from the name
  */
-const ecp_curve_info *ecp_curve_info_from_name( const char *name )
+const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name )
 {
-    const ecp_curve_info *curve_info;
+    const mbedtls_ecp_curve_info *curve_info;
 
-    for( curve_info = ecp_curve_list();
-         curve_info->grp_id != POLARSSL_ECP_DP_NONE;
+    for( curve_info = mbedtls_ecp_curve_list();
+         curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
          curve_info++ )
     {
         if( strcasecmp( curve_info->name, name ) == 0 )
@@ -264,71 +264,71 @@
 /*
  * Get the type of a curve
  */
-static inline ecp_curve_type ecp_get_type( const ecp_group *grp )
+static inline ecp_curve_type ecp_get_type( const mbedtls_ecp_group *grp )
 {
     if( grp->G.X.p == NULL )
-        return( POLARSSL_ECP_TYPE_NONE );
+        return( MBEDTLS_ECP_TYPE_NONE );
 
     if( grp->G.Y.p == NULL )
-        return( POLARSSL_ECP_TYPE_MONTGOMERY );
+        return( MBEDTLS_ECP_TYPE_MONTGOMERY );
     else
-        return( POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS );
+        return( MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS );
 }
 
 /*
  * Initialize (the components of) a point
  */
-void ecp_point_init( ecp_point *pt )
+void mbedtls_ecp_point_init( mbedtls_ecp_point *pt )
 {
     if( pt == NULL )
         return;
 
-    mpi_init( &pt->X );
-    mpi_init( &pt->Y );
-    mpi_init( &pt->Z );
+    mbedtls_mpi_init( &pt->X );
+    mbedtls_mpi_init( &pt->Y );
+    mbedtls_mpi_init( &pt->Z );
 }
 
 /*
  * Initialize (the components of) a group
  */
-void ecp_group_init( ecp_group *grp )
+void mbedtls_ecp_group_init( mbedtls_ecp_group *grp )
 {
     if( grp == NULL )
         return;
 
-    memset( grp, 0, sizeof( ecp_group ) );
+    memset( grp, 0, sizeof( mbedtls_ecp_group ) );
 }
 
 /*
  * Initialize (the components of) a key pair
  */
-void ecp_keypair_init( ecp_keypair *key )
+void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key )
 {
     if( key == NULL )
         return;
 
-    ecp_group_init( &key->grp );
-    mpi_init( &key->d );
-    ecp_point_init( &key->Q );
+    mbedtls_ecp_group_init( &key->grp );
+    mbedtls_mpi_init( &key->d );
+    mbedtls_ecp_point_init( &key->Q );
 }
 
 /*
  * Unallocate (the components of) a point
  */
-void ecp_point_free( ecp_point *pt )
+void mbedtls_ecp_point_free( mbedtls_ecp_point *pt )
 {
     if( pt == NULL )
         return;
 
-    mpi_free( &( pt->X ) );
-    mpi_free( &( pt->Y ) );
-    mpi_free( &( pt->Z ) );
+    mbedtls_mpi_free( &( pt->X ) );
+    mbedtls_mpi_free( &( pt->Y ) );
+    mbedtls_mpi_free( &( pt->Z ) );
 }
 
 /*
  * Unallocate (the components of) a group
  */
-void ecp_group_free( ecp_group *grp )
+void mbedtls_ecp_group_free( mbedtls_ecp_group *grp )
 {
     size_t i;
 
@@ -337,46 +337,46 @@
 
     if( grp->h != 1 )
     {
-        mpi_free( &grp->P );
-        mpi_free( &grp->A );
-        mpi_free( &grp->B );
-        ecp_point_free( &grp->G );
-        mpi_free( &grp->N );
+        mbedtls_mpi_free( &grp->P );
+        mbedtls_mpi_free( &grp->A );
+        mbedtls_mpi_free( &grp->B );
+        mbedtls_ecp_point_free( &grp->G );
+        mbedtls_mpi_free( &grp->N );
     }
 
     if( grp->T != NULL )
     {
         for( i = 0; i < grp->T_size; i++ )
-            ecp_point_free( &grp->T[i] );
-        polarssl_free( grp->T );
+            mbedtls_ecp_point_free( &grp->T[i] );
+        mbedtls_free( grp->T );
     }
 
-    polarssl_zeroize( grp, sizeof( ecp_group ) );
+    mbedtls_zeroize( grp, sizeof( mbedtls_ecp_group ) );
 }
 
 /*
  * Unallocate (the components of) a key pair
  */
-void ecp_keypair_free( ecp_keypair *key )
+void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key )
 {
     if( key == NULL )
         return;
 
-    ecp_group_free( &key->grp );
-    mpi_free( &key->d );
-    ecp_point_free( &key->Q );
+    mbedtls_ecp_group_free( &key->grp );
+    mbedtls_mpi_free( &key->d );
+    mbedtls_ecp_point_free( &key->Q );
 }
 
 /*
  * Copy the contents of a point
  */
-int ecp_copy( ecp_point *P, const ecp_point *Q )
+int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q )
 {
     int ret;
 
-    MPI_CHK( mpi_copy( &P->X, &Q->X ) );
-    MPI_CHK( mpi_copy( &P->Y, &Q->Y ) );
-    MPI_CHK( mpi_copy( &P->Z, &Q->Z ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->X, &Q->X ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->Y, &Q->Y ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &P->Z, &Q->Z ) );
 
 cleanup:
     return( ret );
@@ -385,21 +385,21 @@
 /*
  * Copy the contents of a group object
  */
-int ecp_group_copy( ecp_group *dst, const ecp_group *src )
+int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src )
 {
-    return ecp_use_known_dp( dst, src->id );
+    return mbedtls_ecp_use_known_dp( dst, src->id );
 }
 
 /*
  * Set point to zero
  */
-int ecp_set_zero( ecp_point *pt )
+int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt )
 {
     int ret;
 
-    MPI_CHK( mpi_lset( &pt->X , 1 ) );
-    MPI_CHK( mpi_lset( &pt->Y , 1 ) );
-    MPI_CHK( mpi_lset( &pt->Z , 0 ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->X , 1 ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Y , 1 ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z , 0 ) );
 
 cleanup:
     return( ret );
@@ -408,22 +408,22 @@
 /*
  * Tell if a point is zero
  */
-int ecp_is_zero( ecp_point *pt )
+int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt )
 {
-    return( mpi_cmp_int( &pt->Z, 0 ) == 0 );
+    return( mbedtls_mpi_cmp_int( &pt->Z, 0 ) == 0 );
 }
 
 /*
  * Import a non-zero point from ASCII strings
  */
-int ecp_point_read_string( ecp_point *P, int radix,
+int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix,
                            const char *x, const char *y )
 {
     int ret;
 
-    MPI_CHK( mpi_read_string( &P->X, radix, x ) );
-    MPI_CHK( mpi_read_string( &P->Y, radix, y ) );
-    MPI_CHK( mpi_lset( &P->Z, 1 ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &P->X, radix, x ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &P->Y, radix, y ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &P->Z, 1 ) );
 
 cleanup:
     return( ret );
@@ -432,24 +432,24 @@
 /*
  * Export a point into unsigned binary data (SEC1 2.3.3)
  */
-int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P,
+int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P,
                             int format, size_t *olen,
                             unsigned char *buf, size_t buflen )
 {
     int ret = 0;
     size_t plen;
 
-    if( format != POLARSSL_ECP_PF_UNCOMPRESSED &&
-        format != POLARSSL_ECP_PF_COMPRESSED )
-        return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
+    if( format != MBEDTLS_ECP_PF_UNCOMPRESSED &&
+        format != MBEDTLS_ECP_PF_COMPRESSED )
+        return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
 
     /*
      * Common case: P == 0
      */
-    if( mpi_cmp_int( &P->Z, 0 ) == 0 )
+    if( mbedtls_mpi_cmp_int( &P->Z, 0 ) == 0 )
     {
         if( buflen < 1 )
-            return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
+            return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
 
         buf[0] = 0x00;
         *olen = 1;
@@ -457,28 +457,28 @@
         return( 0 );
     }
 
-    plen = mpi_size( &grp->P );
+    plen = mbedtls_mpi_size( &grp->P );
 
-    if( format == POLARSSL_ECP_PF_UNCOMPRESSED )
+    if( format == MBEDTLS_ECP_PF_UNCOMPRESSED )
     {
         *olen = 2 * plen + 1;
 
         if( buflen < *olen )
-            return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
+            return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
 
         buf[0] = 0x04;
-        MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
-        MPI_CHK( mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->X, buf + 1, plen ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
     }
-    else if( format == POLARSSL_ECP_PF_COMPRESSED )
+    else if( format == MBEDTLS_ECP_PF_COMPRESSED )
     {
         *olen = plen + 1;
 
         if( buflen < *olen )
-            return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
+            return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
 
-        buf[0] = 0x02 + mpi_get_bit( &P->Y, 0 );
-        MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
+        buf[0] = 0x02 + mbedtls_mpi_get_bit( &P->Y, 0 );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &P->X, buf + 1, plen ) );
     }
 
 cleanup:
@@ -488,34 +488,34 @@
 /*
  * Import a point from unsigned binary data (SEC1 2.3.4)
  */
-int ecp_point_read_binary( const ecp_group *grp, ecp_point *pt,
+int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
                            const unsigned char *buf, size_t ilen )
 {
     int ret;
     size_t plen;
 
     if( ilen < 1 )
-        return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
 
     if( buf[0] == 0x00 )
     {
         if( ilen == 1 )
-            return( ecp_set_zero( pt ) );
+            return( mbedtls_ecp_set_zero( pt ) );
         else
-            return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
+            return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
     }
 
-    plen = mpi_size( &grp->P );
+    plen = mbedtls_mpi_size( &grp->P );
 
     if( buf[0] != 0x04 )
-        return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
+        return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
 
     if( ilen != 2 * plen + 1 )
-        return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
 
-    MPI_CHK( mpi_read_binary( &pt->X, buf + 1, plen ) );
-    MPI_CHK( mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) );
-    MPI_CHK( mpi_lset( &pt->Z, 1 ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &pt->X, buf + 1, plen ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z, 1 ) );
 
 cleanup:
     return( ret );
@@ -527,7 +527,7 @@
  *          opaque point <1..2^8-1>;
  *      } ECPoint;
  */
-int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt,
+int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
                         const unsigned char **buf, size_t buf_len )
 {
     unsigned char data_len;
@@ -537,11 +537,11 @@
      * We must have at least two bytes (1 for length, at least one for data)
      */
     if( buf_len < 2 )
-        return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
 
     data_len = *(*buf)++;
     if( data_len < 1 || data_len > buf_len - 1 )
-        return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
 
     /*
      * Save buffer start for read_binary and update buf
@@ -549,7 +549,7 @@
     buf_start = *buf;
     *buf += data_len;
 
-    return ecp_point_read_binary( grp, pt, buf_start, data_len );
+    return mbedtls_ecp_point_read_binary( grp, pt, buf_start, data_len );
 }
 
 /*
@@ -558,7 +558,7 @@
  *          opaque point <1..2^8-1>;
  *      } ECPoint;
  */
-int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
+int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
                          int format, size_t *olen,
                          unsigned char *buf, size_t blen )
 {
@@ -568,9 +568,9 @@
      * buffer length must be at least one, for our length byte
      */
     if( blen < 1 )
-        return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
 
-    if( ( ret = ecp_point_write_binary( grp, pt, format,
+    if( ( ret = mbedtls_ecp_point_write_binary( grp, pt, format,
                     olen, buf + 1, blen - 1) ) != 0 )
         return( ret );
 
@@ -586,23 +586,23 @@
 /*
  * Import an ECP group from ASCII strings, case A == -3
  */
-int ecp_group_read_string( ecp_group *grp, int radix,
+int mbedtls_ecp_group_read_string( mbedtls_ecp_group *grp, int radix,
                            const char *p, const char *b,
                            const char *gx, const char *gy, const char *n)
 {
     int ret;
 
-    MPI_CHK( mpi_read_string( &grp->P, radix, p ) );
-    MPI_CHK( mpi_read_string( &grp->B, radix, b ) );
-    MPI_CHK( ecp_point_read_string( &grp->G, radix, gx, gy ) );
-    MPI_CHK( mpi_read_string( &grp->N, radix, n ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &grp->P, radix, p ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &grp->B, radix, b ) );
+    MBEDTLS_MPI_CHK( mbedtls_ecp_point_read_string( &grp->G, radix, gx, gy ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &grp->N, radix, n ) );
 
-    grp->pbits = mpi_msb( &grp->P );
-    grp->nbits = mpi_msb( &grp->N );
+    grp->pbits = mbedtls_mpi_msb( &grp->P );
+    grp->nbits = mbedtls_mpi_msb( &grp->N );
 
 cleanup:
     if( ret != 0 )
-        ecp_group_free( grp );
+        mbedtls_ecp_group_free( grp );
 
     return( ret );
 }
@@ -610,22 +610,22 @@
 /*
  * Set a group from an ECParameters record (RFC 4492)
  */
-int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len )
+int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp, const unsigned char **buf, size_t len )
 {
     uint16_t tls_id;
-    const ecp_curve_info *curve_info;
+    const mbedtls_ecp_curve_info *curve_info;
 
     /*
      * We expect at least three bytes (see below)
      */
     if( len < 3 )
-        return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
 
     /*
      * First byte is curve_type; only named_curve is handled
      */
-    if( *(*buf)++ != POLARSSL_ECP_TLS_NAMED_CURVE )
-        return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
+    if( *(*buf)++ != MBEDTLS_ECP_TLS_NAMED_CURVE )
+        return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
 
     /*
      * Next two bytes are the namedcurve value
@@ -634,34 +634,34 @@
     tls_id <<= 8;
     tls_id |= *(*buf)++;
 
-    if( ( curve_info = ecp_curve_info_from_tls_id( tls_id ) ) == NULL )
-        return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
+    if( ( curve_info = mbedtls_ecp_curve_info_from_tls_id( tls_id ) ) == NULL )
+        return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
 
-    return ecp_use_known_dp( grp, curve_info->grp_id );
+    return mbedtls_ecp_use_known_dp( grp, curve_info->grp_id );
 }
 
 /*
  * Write the ECParameters record corresponding to a group (RFC 4492)
  */
-int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
+int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen,
                          unsigned char *buf, size_t blen )
 {
-    const ecp_curve_info *curve_info;
+    const mbedtls_ecp_curve_info *curve_info;
 
-    if( ( curve_info = ecp_curve_info_from_grp_id( grp->id ) ) == NULL )
-        return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
+    if( ( curve_info = mbedtls_ecp_curve_info_from_grp_id( grp->id ) ) == NULL )
+        return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
 
     /*
      * We are going to write 3 bytes (see below)
      */
     *olen = 3;
     if( blen < *olen )
-        return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
+        return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
 
     /*
      * First byte is curve_type, always named_curve
      */
-    *buf++ = POLARSSL_ECP_TLS_NAMED_CURVE;
+    *buf++ = MBEDTLS_ECP_TLS_NAMED_CURVE;
 
     /*
      * Next two bytes are the namedcurve value
@@ -673,34 +673,34 @@
 }
 
 /*
- * Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi.
- * See the documentation of struct ecp_group.
+ * Wrapper around fast quasi-modp functions, with fall-back to mbedtls_mpi_mod_mpi.
+ * See the documentation of struct mbedtls_ecp_group.
  *
- * This function is in the critial loop for ecp_mul, so pay attention to perf.
+ * This function is in the critial loop for mbedtls_ecp_mul, so pay attention to perf.
  */
-static int ecp_modp( mpi *N, const ecp_group *grp )
+static int ecp_modp( mbedtls_mpi *N, const mbedtls_ecp_group *grp )
 {
     int ret;
 
     if( grp->modp == NULL )
-        return( mpi_mod_mpi( N, N, &grp->P ) );
+        return( mbedtls_mpi_mod_mpi( N, N, &grp->P ) );
 
     /* N->s < 0 is a much faster test, which fails only if N is 0 */
-    if( ( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 ) ||
-        mpi_msb( N ) > 2 * grp->pbits )
+    if( ( N->s < 0 && mbedtls_mpi_cmp_int( N, 0 ) != 0 ) ||
+        mbedtls_mpi_msb( N ) > 2 * grp->pbits )
     {
-        return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
     }
 
-    MPI_CHK( grp->modp( N ) );
+    MBEDTLS_MPI_CHK( grp->modp( N ) );
 
     /* N->s < 0 is a much faster test, which fails only if N is 0 */
-    while( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 )
-        MPI_CHK( mpi_add_mpi( N, N, &grp->P ) );
+    while( N->s < 0 && mbedtls_mpi_cmp_int( N, 0 ) != 0 )
+        MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( N, N, &grp->P ) );
 
-    while( mpi_cmp_mpi( N, &grp->P ) >= 0 )
+    while( mbedtls_mpi_cmp_mpi( N, &grp->P ) >= 0 )
         /* we known P, N and the result are positive */
-        MPI_CHK( mpi_sub_abs( N, N, &grp->P ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( N, N, &grp->P ) );
 
 cleanup:
     return( ret );
@@ -710,42 +710,42 @@
  * Fast mod-p functions expect their argument to be in the 0..p^2 range.
  *
  * In order to guarantee that, we need to ensure that operands of
- * mpi_mul_mpi are in the 0..p range. So, after each operation we will
+ * mbedtls_mpi_mul_mpi are in the 0..p range. So, after each operation we will
  * bring the result back to this range.
  *
  * The following macros are shortcuts for doing that.
  */
 
 /*
- * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi
+ * Reduce a mbedtls_mpi mod p in-place, general case, to use after mbedtls_mpi_mul_mpi
  */
-#if defined(POLARSSL_SELF_TEST)
+#if defined(MBEDTLS_SELF_TEST)
 #define INC_MUL_COUNT   mul_count++;
 #else
 #define INC_MUL_COUNT
 #endif
 
-#define MOD_MUL( N )    do { MPI_CHK( ecp_modp( &N, grp ) ); INC_MUL_COUNT } \
+#define MOD_MUL( N )    do { MBEDTLS_MPI_CHK( ecp_modp( &N, grp ) ); INC_MUL_COUNT } \
                         while( 0 )
 
 /*
- * Reduce a mpi mod p in-place, to use after mpi_sub_mpi
+ * Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_sub_mpi
  * N->s < 0 is a very fast test, which fails only if N is 0
  */
 #define MOD_SUB( N )                                \
-    while( N.s < 0 && mpi_cmp_int( &N, 0 ) != 0 )   \
-        MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) )
+    while( N.s < 0 && mbedtls_mpi_cmp_int( &N, 0 ) != 0 )   \
+        MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &N, &N, &grp->P ) )
 
 /*
- * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int.
+ * Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_add_mpi and mbedtls_mpi_mul_int.
  * We known P, N and the result are positive, so sub_abs is correct, and
  * a bit faster.
  */
 #define MOD_ADD( N )                                \
-    while( mpi_cmp_mpi( &N, &grp->P ) >= 0 )        \
-        MPI_CHK( mpi_sub_abs( &N, &N, &grp->P ) )
+    while( mbedtls_mpi_cmp_mpi( &N, &grp->P ) >= 0 )        \
+        MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( &N, &N, &grp->P ) )
 
-#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
+#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS)
 /*
  * For curves in short Weierstrass form, we do all the internal operations in
  * Jacobian coordinates.
@@ -758,37 +758,37 @@
  * Normalize jacobian coordinates so that Z == 0 || Z == 1  (GECC 3.2.1)
  * Cost: 1N := 1I + 3M + 1S
  */
-static int ecp_normalize_jac( const ecp_group *grp, ecp_point *pt )
+static int ecp_normalize_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt )
 {
     int ret;
-    mpi Zi, ZZi;
+    mbedtls_mpi Zi, ZZi;
 
-    if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
+    if( mbedtls_mpi_cmp_int( &pt->Z, 0 ) == 0 )
         return( 0 );
 
-    mpi_init( &Zi ); mpi_init( &ZZi );
+    mbedtls_mpi_init( &Zi ); mbedtls_mpi_init( &ZZi );
 
     /*
      * X = X / Z^2  mod p
      */
-    MPI_CHK( mpi_inv_mod( &Zi,      &pt->Z,     &grp->P ) );
-    MPI_CHK( mpi_mul_mpi( &ZZi,     &Zi,        &Zi     ) ); MOD_MUL( ZZi );
-    MPI_CHK( mpi_mul_mpi( &pt->X,   &pt->X,     &ZZi    ) ); MOD_MUL( pt->X );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &Zi,      &pt->Z,     &grp->P ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ZZi,     &Zi,        &Zi     ) ); MOD_MUL( ZZi );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->X,   &pt->X,     &ZZi    ) ); MOD_MUL( pt->X );
 
     /*
      * Y = Y / Z^3  mod p
      */
-    MPI_CHK( mpi_mul_mpi( &pt->Y,   &pt->Y,     &ZZi    ) ); MOD_MUL( pt->Y );
-    MPI_CHK( mpi_mul_mpi( &pt->Y,   &pt->Y,     &Zi     ) ); MOD_MUL( pt->Y );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Y,   &pt->Y,     &ZZi    ) ); MOD_MUL( pt->Y );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Y,   &pt->Y,     &Zi     ) ); MOD_MUL( pt->Y );
 
     /*
      * Z = 1
      */
-    MPI_CHK( mpi_lset( &pt->Z, 1 ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &pt->Z, 1 ) );
 
 cleanup:
 
-    mpi_free( &Zi ); mpi_free( &ZZi );
+    mbedtls_mpi_free( &Zi ); mbedtls_mpi_free( &ZZi );
 
     return( ret );
 }
@@ -804,37 +804,37 @@
  *
  * Cost: 1N(t) := 1I + (6t - 3)M + 1S
  */
-static int ecp_normalize_jac_many( const ecp_group *grp,
-                                   ecp_point *T[], size_t t_len )
+static int ecp_normalize_jac_many( const mbedtls_ecp_group *grp,
+                                   mbedtls_ecp_point *T[], size_t t_len )
 {
     int ret;
     size_t i;
-    mpi *c, u, Zi, ZZi;
+    mbedtls_mpi *c, u, Zi, ZZi;
 
     if( t_len < 2 )
         return( ecp_normalize_jac( grp, *T ) );
 
-    if( ( c = polarssl_malloc( t_len * sizeof( mpi ) ) ) == NULL )
-        return( POLARSSL_ERR_ECP_MALLOC_FAILED );
+    if( ( c = mbedtls_malloc( t_len * sizeof( mbedtls_mpi ) ) ) == NULL )
+        return( MBEDTLS_ERR_ECP_MALLOC_FAILED );
 
-    mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
+    mbedtls_mpi_init( &u ); mbedtls_mpi_init( &Zi ); mbedtls_mpi_init( &ZZi );
     for( i = 0; i < t_len; i++ )
-        mpi_init( &c[i] );
+        mbedtls_mpi_init( &c[i] );
 
     /*
      * c[i] = Z_0 * ... * Z_i
      */
-    MPI_CHK( mpi_copy( &c[0], &T[0]->Z ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &c[0], &T[0]->Z ) );
     for( i = 1; i < t_len; i++ )
     {
-        MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i]->Z ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &c[i], &c[i-1], &T[i]->Z ) );
         MOD_MUL( c[i] );
     }
 
     /*
      * u = 1 / (Z_0 * ... * Z_n) mod P
      */
-    MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
 
     for( i = t_len - 1; ; i-- )
     {
@@ -843,21 +843,21 @@
          * u = 1 / (Z_0 * ... * Z_i) mod P
          */
         if( i == 0 ) {
-            MPI_CHK( mpi_copy( &Zi, &u ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &Zi, &u ) );
         }
         else
         {
-            MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1]  ) ); MOD_MUL( Zi );
-            MPI_CHK( mpi_mul_mpi( &u,  &u, &T[i]->Z ) ); MOD_MUL( u );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &Zi, &u, &c[i-1]  ) ); MOD_MUL( Zi );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &u,  &u, &T[i]->Z ) ); MOD_MUL( u );
         }
 
         /*
          * proceed as in normalize()
          */
-        MPI_CHK( mpi_mul_mpi( &ZZi,     &Zi,      &Zi  ) ); MOD_MUL( ZZi );
-        MPI_CHK( mpi_mul_mpi( &T[i]->X, &T[i]->X, &ZZi ) ); MOD_MUL( T[i]->X );
-        MPI_CHK( mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &ZZi ) ); MOD_MUL( T[i]->Y );
-        MPI_CHK( mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &Zi  ) ); MOD_MUL( T[i]->Y );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ZZi,     &Zi,      &Zi  ) ); MOD_MUL( ZZi );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T[i]->X, &T[i]->X, &ZZi ) ); MOD_MUL( T[i]->X );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &ZZi ) ); MOD_MUL( T[i]->Y );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &Zi  ) ); MOD_MUL( T[i]->Y );
 
         /*
          * Post-precessing: reclaim some memory by shrinking coordinates
@@ -865,9 +865,9 @@
          * - shrinking other coordinates, but still keeping the same number of
          *   limbs as P, as otherwise it will too likely be regrown too fast.
          */
-        MPI_CHK( mpi_shrink( &T[i]->X, grp->P.n ) );
-        MPI_CHK( mpi_shrink( &T[i]->Y, grp->P.n ) );
-        mpi_free( &T[i]->Z );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( &T[i]->X, grp->P.n ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_shrink( &T[i]->Y, grp->P.n ) );
+        mbedtls_mpi_free( &T[i]->Z );
 
         if( i == 0 )
             break;
@@ -875,10 +875,10 @@
 
 cleanup:
 
-    mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
+    mbedtls_mpi_free( &u ); mbedtls_mpi_free( &Zi ); mbedtls_mpi_free( &ZZi );
     for( i = 0; i < t_len; i++ )
-        mpi_free( &c[i] );
-    polarssl_free( c );
+        mbedtls_mpi_free( &c[i] );
+    mbedtls_free( c );
 
     return( ret );
 }
@@ -887,23 +887,23 @@
  * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
  * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
  */
-static int ecp_safe_invert_jac( const ecp_group *grp,
-                            ecp_point *Q,
+static int ecp_safe_invert_jac( const mbedtls_ecp_group *grp,
+                            mbedtls_ecp_point *Q,
                             unsigned char inv )
 {
     int ret;
     unsigned char nonzero;
-    mpi mQY;
+    mbedtls_mpi mQY;
 
-    mpi_init( &mQY );
+    mbedtls_mpi_init( &mQY );
 
     /* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */
-    MPI_CHK( mpi_sub_mpi( &mQY, &grp->P, &Q->Y ) );
-    nonzero = mpi_cmp_int( &Q->Y, 0 ) != 0;
-    MPI_CHK( mpi_safe_cond_assign( &Q->Y, &mQY, inv & nonzero ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &mQY, &grp->P, &Q->Y ) );
+    nonzero = mbedtls_mpi_cmp_int( &Q->Y, 0 ) != 0;
+    MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &Q->Y, &mQY, inv & nonzero ) );
 
 cleanup:
-    mpi_free( &mQY );
+    mbedtls_mpi_free( &mQY );
 
     return( ret );
 }
@@ -918,63 +918,63 @@
  *
  * Cost: 1D := 2M + 8S
  */
-static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
-                           const ecp_point *P )
+static int ecp_double_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
+                           const mbedtls_ecp_point *P )
 {
     int ret;
-    mpi T1, T2, T3, X3, Y3, Z3;
+    mbedtls_mpi T1, T2, T3, X3, Y3, Z3;
 
-#if defined(POLARSSL_SELF_TEST)
+#if defined(MBEDTLS_SELF_TEST)
     dbl_count++;
 #endif
 
-    mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
-    mpi_init( &X3 ); mpi_init( &Y3 ); mpi_init( &Z3 );
+    mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 ); mbedtls_mpi_init( &T3 );
+    mbedtls_mpi_init( &X3 ); mbedtls_mpi_init( &Y3 ); mbedtls_mpi_init( &Z3 );
 
-    MPI_CHK( mpi_mul_mpi( &T3,  &P->X,  &P->X   ) ); MOD_MUL( T3 );
-    MPI_CHK( mpi_mul_mpi( &T2,  &P->Y,  &P->Y   ) ); MOD_MUL( T2 );
-    MPI_CHK( mpi_mul_mpi( &Y3,  &T2,    &T2     ) ); MOD_MUL( Y3 );
-    MPI_CHK( mpi_add_mpi( &X3,  &P->X,  &T2     ) ); MOD_ADD( X3 );
-    MPI_CHK( mpi_mul_mpi( &X3,  &X3,    &X3     ) ); MOD_MUL( X3 );
-    MPI_CHK( mpi_sub_mpi( &X3,  &X3,    &Y3     ) ); MOD_SUB( X3 );
-    MPI_CHK( mpi_sub_mpi( &X3,  &X3,    &T3     ) ); MOD_SUB( X3 );
-    MPI_CHK( mpi_mul_int( &T1,  &X3,    2       ) ); MOD_ADD( T1 );
-    MPI_CHK( mpi_mul_mpi( &Z3,  &P->Z,  &P->Z   ) ); MOD_MUL( Z3 );
-    MPI_CHK( mpi_mul_mpi( &X3,  &Z3,    &Z3     ) ); MOD_MUL( X3 );
-    MPI_CHK( mpi_mul_int( &T3,  &T3,    3       ) ); MOD_ADD( T3 );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3,  &P->X,  &P->X   ) ); MOD_MUL( T3 );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T2,  &P->Y,  &P->Y   ) ); MOD_MUL( T2 );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &Y3,  &T2,    &T2     ) ); MOD_MUL( Y3 );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &X3,  &P->X,  &T2     ) ); MOD_ADD( X3 );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X3,  &X3,    &X3     ) ); MOD_MUL( X3 );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X3,  &X3,    &Y3     ) ); MOD_SUB( X3 );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X3,  &X3,    &T3     ) ); MOD_SUB( X3 );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1,  &X3,    2       ) ); MOD_ADD( T1 );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &Z3,  &P->Z,  &P->Z   ) ); MOD_MUL( Z3 );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X3,  &Z3,    &Z3     ) ); MOD_MUL( X3 );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T3,  &T3,    3       ) ); MOD_ADD( T3 );
 
     /* Special case for A = -3 */
     if( grp->A.p == NULL )
     {
-        MPI_CHK( mpi_mul_int( &X3, &X3, 3 ) );
-        X3.s = -1; /* mpi_mul_int doesn't handle negative numbers */
+        MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &X3, &X3, 3 ) );
+        X3.s = -1; /* mbedtls_mpi_mul_int doesn't handle negative numbers */
         MOD_SUB( X3 );
     }
     else
     {
-        MPI_CHK( mpi_mul_mpi( &X3,  &X3,    &grp->A ) ); MOD_MUL( X3 );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X3,  &X3,    &grp->A ) ); MOD_MUL( X3 );
     }
 
-    MPI_CHK( mpi_add_mpi( &T3,  &T3,    &X3     ) ); MOD_ADD( T3 );
-    MPI_CHK( mpi_mul_mpi( &X3,  &T3,    &T3     ) ); MOD_MUL( X3 );
-    MPI_CHK( mpi_sub_mpi( &X3,  &X3,    &T1     ) ); MOD_SUB( X3 );
-    MPI_CHK( mpi_sub_mpi( &X3,  &X3,    &T1     ) ); MOD_SUB( X3 );
-    MPI_CHK( mpi_sub_mpi( &T1,  &T1,    &X3     ) ); MOD_SUB( T1 );
-    MPI_CHK( mpi_mul_mpi( &T1,  &T3,    &T1     ) ); MOD_MUL( T1 );
-    MPI_CHK( mpi_mul_int( &T3,  &Y3,    8       ) ); MOD_ADD( T3 );
-    MPI_CHK( mpi_sub_mpi( &Y3,  &T1,    &T3     ) ); MOD_SUB( Y3 );
-    MPI_CHK( mpi_add_mpi( &T1,  &P->Y,  &P->Z   ) ); MOD_ADD( T1 );
-    MPI_CHK( mpi_mul_mpi( &T1,  &T1,    &T1     ) ); MOD_MUL( T1 );
-    MPI_CHK( mpi_sub_mpi( &T1,  &T1,    &T2     ) ); MOD_SUB( T1 );
-    MPI_CHK( mpi_sub_mpi( &Z3,  &T1,    &Z3     ) ); MOD_SUB( Z3 );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T3,  &T3,    &X3     ) ); MOD_ADD( T3 );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X3,  &T3,    &T3     ) ); MOD_MUL( X3 );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X3,  &X3,    &T1     ) ); MOD_SUB( X3 );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X3,  &X3,    &T1     ) ); MOD_SUB( X3 );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T1,  &T1,    &X3     ) ); MOD_SUB( T1 );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1,  &T3,    &T1     ) ); MOD_MUL( T1 );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T3,  &Y3,    8       ) ); MOD_ADD( T3 );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &Y3,  &T1,    &T3     ) ); MOD_SUB( Y3 );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T1,  &P->Y,  &P->Z   ) ); MOD_ADD( T1 );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1,  &T1,    &T1     ) ); MOD_MUL( T1 );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T1,  &T1,    &T2     ) ); MOD_SUB( T1 );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &Z3,  &T1,    &Z3     ) ); MOD_SUB( Z3 );
 
-    MPI_CHK( mpi_copy( &R->X, &X3 ) );
-    MPI_CHK( mpi_copy( &R->Y, &Y3 ) );
-    MPI_CHK( mpi_copy( &R->Z, &Z3 ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->X, &X3 ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Y, &Y3 ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Z, &Z3 ) );
 
 cleanup:
-    mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
-    mpi_free( &X3 ); mpi_free( &Y3 ); mpi_free( &Z3 );
+    mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 ); mbedtls_mpi_free( &T3 );
+    mbedtls_mpi_free( &X3 ); mbedtls_mpi_free( &Y3 ); mbedtls_mpi_free( &Z3 );
 
     return( ret );
 }
@@ -997,77 +997,77 @@
  *
  * Cost: 1A := 8M + 3S
  */
-static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
-                          const ecp_point *P, const ecp_point *Q )
+static int ecp_add_mixed( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
+                          const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q )
 {
     int ret;
-    mpi T1, T2, T3, T4, X, Y, Z;
+    mbedtls_mpi T1, T2, T3, T4, X, Y, Z;
 
-#if defined(POLARSSL_SELF_TEST)
+#if defined(MBEDTLS_SELF_TEST)
     add_count++;
 #endif
 
     /*
      * Trivial cases: P == 0 or Q == 0 (case 1)
      */
-    if( mpi_cmp_int( &P->Z, 0 ) == 0 )
-        return( ecp_copy( R, Q ) );
+    if( mbedtls_mpi_cmp_int( &P->Z, 0 ) == 0 )
+        return( mbedtls_ecp_copy( R, Q ) );
 
-    if( Q->Z.p != NULL && mpi_cmp_int( &Q->Z, 0 ) == 0 )
-        return( ecp_copy( R, P ) );
+    if( Q->Z.p != NULL && mbedtls_mpi_cmp_int( &Q->Z, 0 ) == 0 )
+        return( mbedtls_ecp_copy( R, P ) );
 
     /*
      * Make sure Q coordinates are normalized
      */
-    if( Q->Z.p != NULL && mpi_cmp_int( &Q->Z, 1 ) != 0 )
-        return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
+    if( Q->Z.p != NULL && mbedtls_mpi_cmp_int( &Q->Z, 1 ) != 0 )
+        return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
 
-    mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
-    mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
+    mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 ); mbedtls_mpi_init( &T3 ); mbedtls_mpi_init( &T4 );
+    mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
 
-    MPI_CHK( mpi_mul_mpi( &T1,  &P->Z,  &P->Z ) );  MOD_MUL( T1 );
-    MPI_CHK( mpi_mul_mpi( &T2,  &T1,    &P->Z ) );  MOD_MUL( T2 );
-    MPI_CHK( mpi_mul_mpi( &T1,  &T1,    &Q->X ) );  MOD_MUL( T1 );
-    MPI_CHK( mpi_mul_mpi( &T2,  &T2,    &Q->Y ) );  MOD_MUL( T2 );
-    MPI_CHK( mpi_sub_mpi( &T1,  &T1,    &P->X ) );  MOD_SUB( T1 );
-    MPI_CHK( mpi_sub_mpi( &T2,  &T2,    &P->Y ) );  MOD_SUB( T2 );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1,  &P->Z,  &P->Z ) );  MOD_MUL( T1 );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T2,  &T1,    &P->Z ) );  MOD_MUL( T2 );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1,  &T1,    &Q->X ) );  MOD_MUL( T1 );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T2,  &T2,    &Q->Y ) );  MOD_MUL( T2 );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T1,  &T1,    &P->X ) );  MOD_SUB( T1 );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T2,  &T2,    &P->Y ) );  MOD_SUB( T2 );
 
     /* Special cases (2) and (3) */
-    if( mpi_cmp_int( &T1, 0 ) == 0 )
+    if( mbedtls_mpi_cmp_int( &T1, 0 ) == 0 )
     {
-        if( mpi_cmp_int( &T2, 0 ) == 0 )
+        if( mbedtls_mpi_cmp_int( &T2, 0 ) == 0 )
         {
             ret = ecp_double_jac( grp, R, P );
             goto cleanup;
         }
         else
         {
-            ret = ecp_set_zero( R );
+            ret = mbedtls_ecp_set_zero( R );
             goto cleanup;
         }
     }
 
-    MPI_CHK( mpi_mul_mpi( &Z,   &P->Z,  &T1   ) );  MOD_MUL( Z  );
-    MPI_CHK( mpi_mul_mpi( &T3,  &T1,    &T1   ) );  MOD_MUL( T3 );
-    MPI_CHK( mpi_mul_mpi( &T4,  &T3,    &T1   ) );  MOD_MUL( T4 );
-    MPI_CHK( mpi_mul_mpi( &T3,  &T3,    &P->X ) );  MOD_MUL( T3 );
-    MPI_CHK( mpi_mul_int( &T1,  &T3,    2     ) );  MOD_ADD( T1 );
-    MPI_CHK( mpi_mul_mpi( &X,   &T2,    &T2   ) );  MOD_MUL( X  );
-    MPI_CHK( mpi_sub_mpi( &X,   &X,     &T1   ) );  MOD_SUB( X  );
-    MPI_CHK( mpi_sub_mpi( &X,   &X,     &T4   ) );  MOD_SUB( X  );
-    MPI_CHK( mpi_sub_mpi( &T3,  &T3,    &X    ) );  MOD_SUB( T3 );
-    MPI_CHK( mpi_mul_mpi( &T3,  &T3,    &T2   ) );  MOD_MUL( T3 );
-    MPI_CHK( mpi_mul_mpi( &T4,  &T4,    &P->Y ) );  MOD_MUL( T4 );
-    MPI_CHK( mpi_sub_mpi( &Y,   &T3,    &T4   ) );  MOD_SUB( Y  );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &Z,   &P->Z,  &T1   ) );  MOD_MUL( Z  );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3,  &T1,    &T1   ) );  MOD_MUL( T3 );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T4,  &T3,    &T1   ) );  MOD_MUL( T4 );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3,  &T3,    &P->X ) );  MOD_MUL( T3 );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &T1,  &T3,    2     ) );  MOD_ADD( T1 );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &X,   &T2,    &T2   ) );  MOD_MUL( X  );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X,   &X,     &T1   ) );  MOD_SUB( X  );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &X,   &X,     &T4   ) );  MOD_SUB( X  );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T3,  &T3,    &X    ) );  MOD_SUB( T3 );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T3,  &T3,    &T2   ) );  MOD_MUL( T3 );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T4,  &T4,    &P->Y ) );  MOD_MUL( T4 );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &Y,   &T3,    &T4   ) );  MOD_SUB( Y  );
 
-    MPI_CHK( mpi_copy( &R->X, &X ) );
-    MPI_CHK( mpi_copy( &R->Y, &Y ) );
-    MPI_CHK( mpi_copy( &R->Z, &Z ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->X, &X ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Y, &Y ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &R->Z, &Z ) );
 
 cleanup:
 
-    mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
-    mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
+    mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 ); mbedtls_mpi_free( &T3 ); mbedtls_mpi_free( &T4 );
+    mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
 
     return( ret );
 }
@@ -1075,16 +1075,16 @@
 /*
  * Addition: R = P + Q, result's coordinates normalized
  */
-int ecp_add( const ecp_group *grp, ecp_point *R,
-             const ecp_point *P, const ecp_point *Q )
+int mbedtls_ecp_add( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
+             const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q )
 {
     int ret;
 
-    if( ecp_get_type( grp ) != POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
-        return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
+    if( ecp_get_type( grp ) != MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS )
+        return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
 
-    MPI_CHK( ecp_add_mixed( grp, R, P, Q ) );
-    MPI_CHK( ecp_normalize_jac( grp, R ) );
+    MBEDTLS_MPI_CHK( ecp_add_mixed( grp, R, P, Q ) );
+    MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, R ) );
 
 cleanup:
     return( ret );
@@ -1093,27 +1093,27 @@
 /*
  * Subtraction: R = P - Q, result's coordinates normalized
  */
-int ecp_sub( const ecp_group *grp, ecp_point *R,
-             const ecp_point *P, const ecp_point *Q )
+int mbedtls_ecp_sub( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
+             const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q )
 {
     int ret;
-    ecp_point mQ;
+    mbedtls_ecp_point mQ;
 
-    ecp_point_init( &mQ );
+    mbedtls_ecp_point_init( &mQ );
 
-    if( ecp_get_type( grp ) != POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
-        return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
+    if( ecp_get_type( grp ) != MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS )
+        return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
 
     /* mQ = - Q */
-    MPI_CHK( ecp_copy( &mQ, Q ) );
-    if( mpi_cmp_int( &mQ.Y, 0 ) != 0 )
-        MPI_CHK( mpi_sub_mpi( &mQ.Y, &grp->P, &mQ.Y ) );
+    MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &mQ, Q ) );
+    if( mbedtls_mpi_cmp_int( &mQ.Y, 0 ) != 0 )
+        MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &mQ.Y, &grp->P, &mQ.Y ) );
 
-    MPI_CHK( ecp_add_mixed( grp, R, P, &mQ ) );
-    MPI_CHK( ecp_normalize_jac( grp, R ) );
+    MBEDTLS_MPI_CHK( ecp_add_mixed( grp, R, P, &mQ ) );
+    MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, R ) );
 
 cleanup:
-    ecp_point_free( &mQ );
+    mbedtls_ecp_point_free( &mQ );
 
     return( ret );
 }
@@ -1125,42 +1125,42 @@
  *
  * This countermeasure was first suggested in [2].
  */
-static int ecp_randomize_jac( const ecp_group *grp, ecp_point *pt,
+static int ecp_randomize_jac( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
                 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
 {
     int ret;
-    mpi l, ll;
+    mbedtls_mpi l, ll;
     size_t p_size = ( grp->pbits + 7 ) / 8;
     int count = 0;
 
-    mpi_init( &l ); mpi_init( &ll );
+    mbedtls_mpi_init( &l ); mbedtls_mpi_init( &ll );
 
     /* Generate l such that 1 < l < p */
     do
     {
-        mpi_fill_random( &l, p_size, f_rng, p_rng );
+        mbedtls_mpi_fill_random( &l, p_size, f_rng, p_rng );
 
-        while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
-            MPI_CHK( mpi_shift_r( &l, 1 ) );
+        while( mbedtls_mpi_cmp_mpi( &l, &grp->P ) >= 0 )
+            MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &l, 1 ) );
 
         if( count++ > 10 )
-            return( POLARSSL_ERR_ECP_RANDOM_FAILED );
+            return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
     }
-    while( mpi_cmp_int( &l, 1 ) <= 0 );
+    while( mbedtls_mpi_cmp_int( &l, 1 ) <= 0 );
 
     /* Z = l * Z */
-    MPI_CHK( mpi_mul_mpi( &pt->Z,   &pt->Z,     &l  ) ); MOD_MUL( pt->Z );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Z,   &pt->Z,     &l  ) ); MOD_MUL( pt->Z );
 
     /* X = l^2 * X */
-    MPI_CHK( mpi_mul_mpi( &ll,      &l,         &l  ) ); MOD_MUL( ll );
-    MPI_CHK( mpi_mul_mpi( &pt->X,   &pt->X,     &ll ) ); MOD_MUL( pt->X );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ll,      &l,         &l  ) ); MOD_MUL( ll );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->X,   &pt->X,     &ll ) ); MOD_MUL( pt->X );
 
     /* Y = l^3 * Y */
-    MPI_CHK( mpi_mul_mpi( &ll,      &ll,        &l  ) ); MOD_MUL( ll );
-    MPI_CHK( mpi_mul_mpi( &pt->Y,   &pt->Y,     &ll ) ); MOD_MUL( pt->Y );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ll,      &ll,        &l  ) ); MOD_MUL( ll );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &pt->Y,   &pt->Y,     &ll ) ); MOD_MUL( pt->Y );
 
 cleanup:
-    mpi_free( &l ); mpi_free( &ll );
+    mbedtls_mpi_free( &l ); mbedtls_mpi_free( &ll );
 
     return( ret );
 }
@@ -1168,15 +1168,15 @@
 /*
  * Check and define parameters used by the comb method (see below for details)
  */
-#if POLARSSL_ECP_WINDOW_SIZE < 2 || POLARSSL_ECP_WINDOW_SIZE > 7
-#error "POLARSSL_ECP_WINDOW_SIZE out of bounds"
+#if MBEDTLS_ECP_WINDOW_SIZE < 2 || MBEDTLS_ECP_WINDOW_SIZE > 7
+#error "MBEDTLS_ECP_WINDOW_SIZE out of bounds"
 #endif
 
 /* d = ceil( n / w ) */
-#define COMB_MAX_D      ( POLARSSL_ECP_MAX_BITS + 1 ) / 2
+#define COMB_MAX_D      ( MBEDTLS_ECP_MAX_BITS + 1 ) / 2
 
 /* number of precomputed points */
-#define COMB_MAX_PRE    ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) )
+#define COMB_MAX_PRE    ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
 
 /*
  * Compute the representation of m that will be used with our comb method.
@@ -1194,12 +1194,12 @@
  * Calling conventions:
  * - x is an array of size d + 1
  * - w is the size, ie number of teeth, of the comb, and must be between
- *   2 and 7 (in practice, between 2 and POLARSSL_ECP_WINDOW_SIZE)
+ *   2 and 7 (in practice, between 2 and MBEDTLS_ECP_WINDOW_SIZE)
  * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
  *   (the result will be incorrect if these assumptions are not satisfied)
  */
 static void ecp_comb_fixed( unsigned char x[], size_t d,
-                            unsigned char w, const mpi *m )
+                            unsigned char w, const mbedtls_mpi *m )
 {
     size_t i, j;
     unsigned char c, cc, adjust;
@@ -1209,7 +1209,7 @@
     /* First get the classical comb values (except for x_d = 0) */
     for( i = 0; i < d; i++ )
         for( j = 0; j < w; j++ )
-            x[i] |= mpi_get_bit( m, i + d * j ) << j;
+            x[i] |= mbedtls_mpi_get_bit( m, i + d * j ) << j;
 
     /* Now make sure x_1 .. x_d are odd */
     c = 0;
@@ -1238,33 +1238,33 @@
  *
  * Cost: d(w-1) D + (2^{w-1} - 1) A + 1 N(w-1) + 1 N(2^{w-1} - 1)
  */
-static int ecp_precompute_comb( const ecp_group *grp,
-                                ecp_point T[], const ecp_point *P,
+static int ecp_precompute_comb( const mbedtls_ecp_group *grp,
+                                mbedtls_ecp_point T[], const mbedtls_ecp_point *P,
                                 unsigned char w, size_t d )
 {
     int ret;
     unsigned char i, k;
     size_t j;
-    ecp_point *cur, *TT[COMB_MAX_PRE - 1];
+    mbedtls_ecp_point *cur, *TT[COMB_MAX_PRE - 1];
 
     /*
      * Set T[0] = P and
      * T[2^{l-1}] = 2^{dl} P for l = 1 .. w-1 (this is not the final value)
      */
-    MPI_CHK( ecp_copy( &T[0], P ) );
+    MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &T[0], P ) );
 
     k = 0;
     for( i = 1; i < ( 1U << ( w - 1 ) ); i <<= 1 )
     {
         cur = T + i;
-        MPI_CHK( ecp_copy( cur, T + ( i >> 1 ) ) );
+        MBEDTLS_MPI_CHK( mbedtls_ecp_copy( cur, T + ( i >> 1 ) ) );
         for( j = 0; j < d; j++ )
-            MPI_CHK( ecp_double_jac( grp, cur, cur ) );
+            MBEDTLS_MPI_CHK( ecp_double_jac( grp, cur, cur ) );
 
         TT[k++] = cur;
     }
 
-    MPI_CHK( ecp_normalize_jac_many( grp, TT, k ) );
+    MBEDTLS_MPI_CHK( ecp_normalize_jac_many( grp, TT, k ) );
 
     /*
      * Compute the remaining ones using the minimal number of additions
@@ -1276,12 +1276,12 @@
         j = i;
         while( j-- )
         {
-            MPI_CHK( ecp_add_mixed( grp, &T[i + j], &T[j], &T[i] ) );
+            MBEDTLS_MPI_CHK( ecp_add_mixed( grp, &T[i + j], &T[j], &T[i] ) );
             TT[k++] = &T[i + j];
         }
     }
 
-    MPI_CHK( ecp_normalize_jac_many( grp, TT, k ) );
+    MBEDTLS_MPI_CHK( ecp_normalize_jac_many( grp, TT, k ) );
 
 cleanup:
     return( ret );
@@ -1290,8 +1290,8 @@
 /*
  * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
  */
-static int ecp_select_comb( const ecp_group *grp, ecp_point *R,
-                            const ecp_point T[], unsigned char t_len,
+static int ecp_select_comb( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
+                            const mbedtls_ecp_point T[], unsigned char t_len,
                             unsigned char i )
 {
     int ret;
@@ -1303,12 +1303,12 @@
     /* Read the whole table to thwart cache-based timing attacks */
     for( j = 0; j < t_len; j++ )
     {
-        MPI_CHK( mpi_safe_cond_assign( &R->X, &T[j].X, j == ii ) );
-        MPI_CHK( mpi_safe_cond_assign( &R->Y, &T[j].Y, j == ii ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &R->X, &T[j].X, j == ii ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &R->Y, &T[j].Y, j == ii ) );
     }
 
     /* Safely invert result if i is "negative" */
-    MPI_CHK( ecp_safe_invert_jac( grp, R, i >> 7 ) );
+    MBEDTLS_MPI_CHK( ecp_safe_invert_jac( grp, R, i >> 7 ) );
 
 cleanup:
     return( ret );
@@ -1320,34 +1320,34 @@
  *
  * Cost: d A + d D + 1 R
  */
-static int ecp_mul_comb_core( const ecp_group *grp, ecp_point *R,
-                              const ecp_point T[], unsigned char t_len,
+static int ecp_mul_comb_core( const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
+                              const mbedtls_ecp_point T[], unsigned char t_len,
                               const unsigned char x[], size_t d,
                               int (*f_rng)(void *, unsigned char *, size_t),
                               void *p_rng )
 {
     int ret;
-    ecp_point Txi;
+    mbedtls_ecp_point Txi;
     size_t i;
 
-    ecp_point_init( &Txi );
+    mbedtls_ecp_point_init( &Txi );
 
     /* Start with a non-zero point and randomize its coordinates */
     i = d;
-    MPI_CHK( ecp_select_comb( grp, R, T, t_len, x[i] ) );
-    MPI_CHK( mpi_lset( &R->Z, 1 ) );
+    MBEDTLS_MPI_CHK( ecp_select_comb( grp, R, T, t_len, x[i] ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->Z, 1 ) );
     if( f_rng != 0 )
-        MPI_CHK( ecp_randomize_jac( grp, R, f_rng, p_rng ) );
+        MBEDTLS_MPI_CHK( ecp_randomize_jac( grp, R, f_rng, p_rng ) );
 
     while( i-- != 0 )
     {
-        MPI_CHK( ecp_double_jac( grp, R, R ) );
-        MPI_CHK( ecp_select_comb( grp, &Txi, T, t_len, x[i] ) );
-        MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) );
+        MBEDTLS_MPI_CHK( ecp_double_jac( grp, R, R ) );
+        MBEDTLS_MPI_CHK( ecp_select_comb( grp, &Txi, T, t_len, x[i] ) );
+        MBEDTLS_MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) );
     }
 
 cleanup:
-    ecp_point_free( &Txi );
+    mbedtls_ecp_point_free( &Txi );
 
     return( ret );
 }
@@ -1356,8 +1356,8 @@
  * Multiplication using the comb method,
  * for curves in short Weierstrass form
  */
-static int ecp_mul_comb( ecp_group *grp, ecp_point *R,
-                         const mpi *m, const ecp_point *P,
+static int ecp_mul_comb( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
+                         const mbedtls_mpi *m, const mbedtls_ecp_point *P,
                          int (*f_rng)(void *, unsigned char *, size_t),
                          void *p_rng )
 {
@@ -1365,15 +1365,15 @@
     unsigned char w, m_is_odd, p_eq_g, pre_len, i;
     size_t d;
     unsigned char k[COMB_MAX_D + 1];
-    ecp_point *T;
-    mpi M, mm;
+    mbedtls_ecp_point *T;
+    mbedtls_mpi M, mm;
 
-    mpi_init( &M );
-    mpi_init( &mm );
+    mbedtls_mpi_init( &M );
+    mbedtls_mpi_init( &mm );
 
     /* we need N to be odd to trnaform m in an odd number, check now */
-    if( mpi_get_bit( &grp->N, 0 ) != 1 )
-        return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
+    if( mbedtls_mpi_get_bit( &grp->N, 0 ) != 1 )
+        return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
 
     /*
      * Minimize the number of multiplications, that is minimize
@@ -1387,9 +1387,9 @@
      * Just adding one avoids upping the cost of the first mul too much,
      * and the memory cost too.
      */
-#if POLARSSL_ECP_FIXED_POINT_OPTIM == 1
-    p_eq_g = ( mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
-               mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
+#if MBEDTLS_ECP_FIXED_POINT_OPTIM == 1
+    p_eq_g = ( mbedtls_mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
+               mbedtls_mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
     if( p_eq_g )
         w++;
 #else
@@ -1400,8 +1400,8 @@
      * Make sure w is within bounds.
      * (The last test is useful only for very small curves in the test suite.)
      */
-    if( w > POLARSSL_ECP_WINDOW_SIZE )
-        w = POLARSSL_ECP_WINDOW_SIZE;
+    if( w > MBEDTLS_ECP_WINDOW_SIZE )
+        w = MBEDTLS_ECP_WINDOW_SIZE;
     if( w >= grp->nbits )
         w = 2;
 
@@ -1417,17 +1417,17 @@
 
     if( T == NULL )
     {
-        T = polarssl_malloc( pre_len * sizeof( ecp_point ) );
+        T = mbedtls_malloc( pre_len * sizeof( mbedtls_ecp_point ) );
         if( T == NULL )
         {
-            ret = POLARSSL_ERR_ECP_MALLOC_FAILED;
+            ret = MBEDTLS_ERR_ECP_MALLOC_FAILED;
             goto cleanup;
         }
 
         for( i = 0; i < pre_len; i++ )
-            ecp_point_init( &T[i] );
+            mbedtls_ecp_point_init( &T[i] );
 
-        MPI_CHK( ecp_precompute_comb( grp, T, P, w, d ) );
+        MBEDTLS_MPI_CHK( ecp_precompute_comb( grp, T, P, w, d ) );
 
         if( p_eq_g )
         {
@@ -1440,44 +1440,44 @@
      * Make sure M is odd (M = m or M = N - m, since N is odd)
      * using the fact that m * P = - (N - m) * P
      */
-    m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
-    MPI_CHK( mpi_copy( &M, m ) );
-    MPI_CHK( mpi_sub_mpi( &mm, &grp->N, m ) );
-    MPI_CHK( mpi_safe_cond_assign( &M, &mm, ! m_is_odd ) );
+    m_is_odd = ( mbedtls_mpi_get_bit( m, 0 ) == 1 );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &M, m ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &mm, &grp->N, m ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_assign( &M, &mm, ! m_is_odd ) );
 
     /*
      * Go for comb multiplication, R = M * P
      */
     ecp_comb_fixed( k, d, w, &M );
-    MPI_CHK( ecp_mul_comb_core( grp, R, T, pre_len, k, d, f_rng, p_rng ) );
+    MBEDTLS_MPI_CHK( ecp_mul_comb_core( grp, R, T, pre_len, k, d, f_rng, p_rng ) );
 
     /*
      * Now get m * P from M * P and normalize it
      */
-    MPI_CHK( ecp_safe_invert_jac( grp, R, ! m_is_odd ) );
-    MPI_CHK( ecp_normalize_jac( grp, R ) );
+    MBEDTLS_MPI_CHK( ecp_safe_invert_jac( grp, R, ! m_is_odd ) );
+    MBEDTLS_MPI_CHK( ecp_normalize_jac( grp, R ) );
 
 cleanup:
 
     if( T != NULL && ! p_eq_g )
     {
         for( i = 0; i < pre_len; i++ )
-            ecp_point_free( &T[i] );
-        polarssl_free( T );
+            mbedtls_ecp_point_free( &T[i] );
+        mbedtls_free( T );
     }
 
-    mpi_free( &M );
-    mpi_free( &mm );
+    mbedtls_mpi_free( &M );
+    mbedtls_mpi_free( &mm );
 
     if( ret != 0 )
-        ecp_point_free( R );
+        mbedtls_ecp_point_free( R );
 
     return( ret );
 }
 
-#endif /* POLARSSL_ECP_SHORT_WEIERSTRASS */
+#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS */
 
-#if defined(POLARSSL_ECP_MONTGOMERY)
+#if defined(MBEDTLS_ECP_MONTGOMERY)
 /*
  * For Montgomery curves, we do all the internal arithmetic in projective
  * coordinates. Import/export of points uses only the x coordinates, which is
@@ -1490,13 +1490,13 @@
  * Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
  * Cost: 1M + 1I
  */
-static int ecp_normalize_mxz( const ecp_group *grp, ecp_point *P )
+static int ecp_normalize_mxz( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P )
 {
     int ret;
 
-    MPI_CHK( mpi_inv_mod( &P->Z, &P->Z, &grp->P ) );
-    MPI_CHK( mpi_mul_mpi( &P->X, &P->X, &P->Z ) ); MOD_MUL( P->X );
-    MPI_CHK( mpi_lset( &P->Z, 1 ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &P->Z, &P->Z, &grp->P ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->X, &P->X, &P->Z ) ); MOD_MUL( P->X );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &P->Z, 1 ) );
 
 cleanup:
     return( ret );
@@ -1510,34 +1510,34 @@
  * This countermeasure was first suggested in [2].
  * Cost: 2M
  */
-static int ecp_randomize_mxz( const ecp_group *grp, ecp_point *P,
+static int ecp_randomize_mxz( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
                 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
 {
     int ret;
-    mpi l;
+    mbedtls_mpi l;
     size_t p_size = ( grp->pbits + 7 ) / 8;
     int count = 0;
 
-    mpi_init( &l );
+    mbedtls_mpi_init( &l );
 
     /* Generate l such that 1 < l < p */
     do
     {
-        mpi_fill_random( &l, p_size, f_rng, p_rng );
+        mbedtls_mpi_fill_random( &l, p_size, f_rng, p_rng );
 
-        while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
-            MPI_CHK( mpi_shift_r( &l, 1 ) );
+        while( mbedtls_mpi_cmp_mpi( &l, &grp->P ) >= 0 )
+            MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &l, 1 ) );
 
         if( count++ > 10 )
-            return( POLARSSL_ERR_ECP_RANDOM_FAILED );
+            return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
     }
-    while( mpi_cmp_int( &l, 1 ) <= 0 );
+    while( mbedtls_mpi_cmp_int( &l, 1 ) <= 0 );
 
-    MPI_CHK( mpi_mul_mpi( &P->X, &P->X, &l ) ); MOD_MUL( P->X );
-    MPI_CHK( mpi_mul_mpi( &P->Z, &P->Z, &l ) ); MOD_MUL( P->Z );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->X, &P->X, &l ) ); MOD_MUL( P->X );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &P->Z, &P->Z, &l ) ); MOD_MUL( P->Z );
 
 cleanup:
-    mpi_free( &l );
+    mbedtls_mpi_free( &l );
 
     return( ret );
 }
@@ -1557,41 +1557,41 @@
  *
  * Cost: 5M + 4S
  */
-static int ecp_double_add_mxz( const ecp_group *grp,
-                               ecp_point *R, ecp_point *S,
-                               const ecp_point *P, const ecp_point *Q,
-                               const mpi *d )
+static int ecp_double_add_mxz( const mbedtls_ecp_group *grp,
+                               mbedtls_ecp_point *R, mbedtls_ecp_point *S,
+                               const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q,
+                               const mbedtls_mpi *d )
 {
     int ret;
-    mpi A, AA, B, BB, E, C, D, DA, CB;
+    mbedtls_mpi A, AA, B, BB, E, C, D, DA, CB;
 
-    mpi_init( &A ); mpi_init( &AA ); mpi_init( &B );
-    mpi_init( &BB ); mpi_init( &E ); mpi_init( &C );
-    mpi_init( &D ); mpi_init( &DA ); mpi_init( &CB );
+    mbedtls_mpi_init( &A ); mbedtls_mpi_init( &AA ); mbedtls_mpi_init( &B );
+    mbedtls_mpi_init( &BB ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &C );
+    mbedtls_mpi_init( &D ); mbedtls_mpi_init( &DA ); mbedtls_mpi_init( &CB );
 
-    MPI_CHK( mpi_add_mpi( &A,    &P->X,   &P->Z ) ); MOD_ADD( A    );
-    MPI_CHK( mpi_mul_mpi( &AA,   &A,      &A    ) ); MOD_MUL( AA   );
-    MPI_CHK( mpi_sub_mpi( &B,    &P->X,   &P->Z ) ); MOD_SUB( B    );
-    MPI_CHK( mpi_mul_mpi( &BB,   &B,      &B    ) ); MOD_MUL( BB   );
-    MPI_CHK( mpi_sub_mpi( &E,    &AA,     &BB   ) ); MOD_SUB( E    );
-    MPI_CHK( mpi_add_mpi( &C,    &Q->X,   &Q->Z ) ); MOD_ADD( C    );
-    MPI_CHK( mpi_sub_mpi( &D,    &Q->X,   &Q->Z ) ); MOD_SUB( D    );
-    MPI_CHK( mpi_mul_mpi( &DA,   &D,      &A    ) ); MOD_MUL( DA   );
-    MPI_CHK( mpi_mul_mpi( &CB,   &C,      &B    ) ); MOD_MUL( CB   );
-    MPI_CHK( mpi_add_mpi( &S->X, &DA,     &CB   ) ); MOD_MUL( S->X );
-    MPI_CHK( mpi_mul_mpi( &S->X, &S->X,   &S->X ) ); MOD_MUL( S->X );
-    MPI_CHK( mpi_sub_mpi( &S->Z, &DA,     &CB   ) ); MOD_SUB( S->Z );
-    MPI_CHK( mpi_mul_mpi( &S->Z, &S->Z,   &S->Z ) ); MOD_MUL( S->Z );
-    MPI_CHK( mpi_mul_mpi( &S->Z, d,       &S->Z ) ); MOD_MUL( S->Z );
-    MPI_CHK( mpi_mul_mpi( &R->X, &AA,     &BB   ) ); MOD_MUL( R->X );
-    MPI_CHK( mpi_mul_mpi( &R->Z, &grp->A, &E    ) ); MOD_MUL( R->Z );
-    MPI_CHK( mpi_add_mpi( &R->Z, &BB,     &R->Z ) ); MOD_ADD( R->Z );
-    MPI_CHK( mpi_mul_mpi( &R->Z, &E,      &R->Z ) ); MOD_MUL( R->Z );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &A,    &P->X,   &P->Z ) ); MOD_ADD( A    );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &AA,   &A,      &A    ) ); MOD_MUL( AA   );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &B,    &P->X,   &P->Z ) ); MOD_SUB( B    );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &BB,   &B,      &B    ) ); MOD_MUL( BB   );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &E,    &AA,     &BB   ) ); MOD_SUB( E    );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &C,    &Q->X,   &Q->Z ) ); MOD_ADD( C    );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &D,    &Q->X,   &Q->Z ) ); MOD_SUB( D    );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DA,   &D,      &A    ) ); MOD_MUL( DA   );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &CB,   &C,      &B    ) ); MOD_MUL( CB   );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &S->X, &DA,     &CB   ) ); MOD_MUL( S->X );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->X, &S->X,   &S->X ) ); MOD_MUL( S->X );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &S->Z, &DA,     &CB   ) ); MOD_SUB( S->Z );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->Z, &S->Z,   &S->Z ) ); MOD_MUL( S->Z );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &S->Z, d,       &S->Z ) ); MOD_MUL( S->Z );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->X, &AA,     &BB   ) ); MOD_MUL( R->X );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->Z, &grp->A, &E    ) ); MOD_MUL( R->Z );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &R->Z, &BB,     &R->Z ) ); MOD_ADD( R->Z );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &R->Z, &E,      &R->Z ) ); MOD_MUL( R->Z );
 
 cleanup:
-    mpi_free( &A ); mpi_free( &AA ); mpi_free( &B );
-    mpi_free( &BB ); mpi_free( &E ); mpi_free( &C );
-    mpi_free( &D ); mpi_free( &DA ); mpi_free( &CB );
+    mbedtls_mpi_free( &A ); mbedtls_mpi_free( &AA ); mbedtls_mpi_free( &B );
+    mbedtls_mpi_free( &BB ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &C );
+    mbedtls_mpi_free( &D ); mbedtls_mpi_free( &DA ); mbedtls_mpi_free( &CB );
 
     return( ret );
 }
@@ -1600,40 +1600,40 @@
  * Multiplication with Montgomery ladder in x/z coordinates,
  * for curves in Montgomery form
  */
-static int ecp_mul_mxz( ecp_group *grp, ecp_point *R,
-                        const mpi *m, const ecp_point *P,
+static int ecp_mul_mxz( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
+                        const mbedtls_mpi *m, const mbedtls_ecp_point *P,
                         int (*f_rng)(void *, unsigned char *, size_t),
                         void *p_rng )
 {
     int ret;
     size_t i;
     unsigned char b;
-    ecp_point RP;
-    mpi PX;
+    mbedtls_ecp_point RP;
+    mbedtls_mpi PX;
 
-    ecp_point_init( &RP ); mpi_init( &PX );
+    mbedtls_ecp_point_init( &RP ); mbedtls_mpi_init( &PX );
 
     /* Save PX and read from P before writing to R, in case P == R */
-    MPI_CHK( mpi_copy( &PX, &P->X ) );
-    MPI_CHK( ecp_copy( &RP, P ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &PX, &P->X ) );
+    MBEDTLS_MPI_CHK( mbedtls_ecp_copy( &RP, P ) );
 
     /* Set R to zero in modified x/z coordinates */
-    MPI_CHK( mpi_lset( &R->X, 1 ) );
-    MPI_CHK( mpi_lset( &R->Z, 0 ) );
-    mpi_free( &R->Y );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->X, 1 ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &R->Z, 0 ) );
+    mbedtls_mpi_free( &R->Y );
 
     /* RP.X might be sligtly larger than P, so reduce it */
     MOD_ADD( RP.X );
 
     /* Randomize coordinates of the starting point */
     if( f_rng != NULL )
-        MPI_CHK( ecp_randomize_mxz( grp, &RP, f_rng, p_rng ) );
+        MBEDTLS_MPI_CHK( ecp_randomize_mxz( grp, &RP, f_rng, p_rng ) );
 
     /* Loop invariant: R = result so far, RP = R + P */
-    i = mpi_msb( m ); /* one past the (zero-based) most significant bit */
+    i = mbedtls_mpi_msb( m ); /* one past the (zero-based) most significant bit */
     while( i-- > 0 )
     {
-        b = mpi_get_bit( m, i );
+        b = mbedtls_mpi_get_bit( m, i );
         /*
          *  if (b) R = 2R + P else R = 2R,
          * which is:
@@ -1641,207 +1641,207 @@
          *  else   double_add( R, RP, R, RP )
          * but using safe conditional swaps to avoid leaks
          */
-        MPI_CHK( mpi_safe_cond_swap( &R->X, &RP.X, b ) );
-        MPI_CHK( mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
-        MPI_CHK( ecp_double_add_mxz( grp, R, &RP, R, &RP, &PX ) );
-        MPI_CHK( mpi_safe_cond_swap( &R->X, &RP.X, b ) );
-        MPI_CHK( mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->X, &RP.X, b ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
+        MBEDTLS_MPI_CHK( ecp_double_add_mxz( grp, R, &RP, R, &RP, &PX ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->X, &RP.X, b ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
     }
 
-    MPI_CHK( ecp_normalize_mxz( grp, R ) );
+    MBEDTLS_MPI_CHK( ecp_normalize_mxz( grp, R ) );
 
 cleanup:
-    ecp_point_free( &RP ); mpi_free( &PX );
+    mbedtls_ecp_point_free( &RP ); mbedtls_mpi_free( &PX );
 
     return( ret );
 }
 
-#endif /* POLARSSL_ECP_MONTGOMERY */
+#endif /* MBEDTLS_ECP_MONTGOMERY */
 
 /*
  * Multiplication R = m * P
  */
-int ecp_mul( ecp_group *grp, ecp_point *R,
-             const mpi *m, const ecp_point *P,
+int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
+             const mbedtls_mpi *m, const mbedtls_ecp_point *P,
              int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
 {
     int ret;
 
     /* Common sanity checks */
-    if( mpi_cmp_int( &P->Z, 1 ) != 0 )
-        return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
+    if( mbedtls_mpi_cmp_int( &P->Z, 1 ) != 0 )
+        return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
 
-    if( ( ret = ecp_check_privkey( grp, m ) ) != 0 ||
-        ( ret = ecp_check_pubkey( grp, P ) ) != 0 )
+    if( ( ret = mbedtls_ecp_check_privkey( grp, m ) ) != 0 ||
+        ( ret = mbedtls_ecp_check_pubkey( grp, P ) ) != 0 )
         return( ret );
 
-#if defined(POLARSSL_ECP_MONTGOMERY)
-    if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
+#if defined(MBEDTLS_ECP_MONTGOMERY)
+    if( ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
         return( ecp_mul_mxz( grp, R, m, P, f_rng, p_rng ) );
 #endif
-#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
-    if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
+#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS)
+    if( ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS )
         return( ecp_mul_comb( grp, R, m, P, f_rng, p_rng ) );
 #endif
-    return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
+    return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
 }
 
-#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
+#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS)
 /*
  * Check that an affine point is valid as a public key,
  * short weierstrass curves (SEC1 3.2.3.1)
  */
-static int ecp_check_pubkey_sw( const ecp_group *grp, const ecp_point *pt )
+static int ecp_check_pubkey_sw( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
 {
     int ret;
-    mpi YY, RHS;
+    mbedtls_mpi YY, RHS;
 
     /* pt coordinates must be normalized for our checks */
-    if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
-        mpi_cmp_int( &pt->Y, 0 ) < 0 ||
-        mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
-        mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
-        return( POLARSSL_ERR_ECP_INVALID_KEY );
+    if( mbedtls_mpi_cmp_int( &pt->X, 0 ) < 0 ||
+        mbedtls_mpi_cmp_int( &pt->Y, 0 ) < 0 ||
+        mbedtls_mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
+        mbedtls_mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
+        return( MBEDTLS_ERR_ECP_INVALID_KEY );
 
-    mpi_init( &YY ); mpi_init( &RHS );
+    mbedtls_mpi_init( &YY ); mbedtls_mpi_init( &RHS );
 
     /*
      * YY = Y^2
      * RHS = X (X^2 + A) + B = X^3 + A X + B
      */
-    MPI_CHK( mpi_mul_mpi( &YY,  &pt->Y,   &pt->Y  ) );  MOD_MUL( YY  );
-    MPI_CHK( mpi_mul_mpi( &RHS, &pt->X,   &pt->X  ) );  MOD_MUL( RHS );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &YY,  &pt->Y,   &pt->Y  ) );  MOD_MUL( YY  );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &RHS, &pt->X,   &pt->X  ) );  MOD_MUL( RHS );
 
     /* Special case for A = -3 */
     if( grp->A.p == NULL )
     {
-        MPI_CHK( mpi_sub_int( &RHS, &RHS, 3       ) );  MOD_SUB( RHS );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &RHS, &RHS, 3       ) );  MOD_SUB( RHS );
     }
     else
     {
-        MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->A ) );  MOD_ADD( RHS );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &RHS, &RHS, &grp->A ) );  MOD_ADD( RHS );
     }
 
-    MPI_CHK( mpi_mul_mpi( &RHS, &RHS,     &pt->X  ) );  MOD_MUL( RHS );
-    MPI_CHK( mpi_add_mpi( &RHS, &RHS,     &grp->B ) );  MOD_ADD( RHS );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &RHS, &RHS,     &pt->X  ) );  MOD_MUL( RHS );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &RHS, &RHS,     &grp->B ) );  MOD_ADD( RHS );
 
-    if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
-        ret = POLARSSL_ERR_ECP_INVALID_KEY;
+    if( mbedtls_mpi_cmp_mpi( &YY, &RHS ) != 0 )
+        ret = MBEDTLS_ERR_ECP_INVALID_KEY;
 
 cleanup:
 
-    mpi_free( &YY ); mpi_free( &RHS );
+    mbedtls_mpi_free( &YY ); mbedtls_mpi_free( &RHS );
 
     return( ret );
 }
-#endif /* POLARSSL_ECP_SHORT_WEIERSTRASS */
+#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS */
 
 
-#if defined(POLARSSL_ECP_MONTGOMERY)
+#if defined(MBEDTLS_ECP_MONTGOMERY)
 /*
  * Check validity of a public key for Montgomery curves with x-only schemes
  */
-static int ecp_check_pubkey_mx( const ecp_group *grp, const ecp_point *pt )
+static int ecp_check_pubkey_mx( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
 {
     /* [M255 p. 5] Just check X is the correct number of bytes */
-    if( mpi_size( &pt->X ) > ( grp->nbits + 7 ) / 8 )
-        return( POLARSSL_ERR_ECP_INVALID_KEY );
+    if( mbedtls_mpi_size( &pt->X ) > ( grp->nbits + 7 ) / 8 )
+        return( MBEDTLS_ERR_ECP_INVALID_KEY );
 
     return( 0 );
 }
-#endif /* POLARSSL_ECP_MONTGOMERY */
+#endif /* MBEDTLS_ECP_MONTGOMERY */
 
 /*
  * Check that a point is valid as a public key
  */
-int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
+int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt )
 {
     /* Must use affine coordinates */
-    if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
-        return( POLARSSL_ERR_ECP_INVALID_KEY );
+    if( mbedtls_mpi_cmp_int( &pt->Z, 1 ) != 0 )
+        return( MBEDTLS_ERR_ECP_INVALID_KEY );
 
-#if defined(POLARSSL_ECP_MONTGOMERY)
-    if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
+#if defined(MBEDTLS_ECP_MONTGOMERY)
+    if( ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
         return( ecp_check_pubkey_mx( grp, pt ) );
 #endif
-#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
-    if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
+#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS)
+    if( ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS )
         return( ecp_check_pubkey_sw( grp, pt ) );
 #endif
-    return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
+    return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
 }
 
 /*
- * Check that an mpi is valid as a private key
+ * Check that an mbedtls_mpi is valid as a private key
  */
-int ecp_check_privkey( const ecp_group *grp, const mpi *d )
+int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp, const mbedtls_mpi *d )
 {
-#if defined(POLARSSL_ECP_MONTGOMERY)
-    if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
+#if defined(MBEDTLS_ECP_MONTGOMERY)
+    if( ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
     {
         /* see [M255] page 5 */
-        if( mpi_get_bit( d, 0 ) != 0 ||
-            mpi_get_bit( d, 1 ) != 0 ||
-            mpi_get_bit( d, 2 ) != 0 ||
-            mpi_msb( d ) - 1 != grp->nbits ) /* mpi_msb is one-based! */
-            return( POLARSSL_ERR_ECP_INVALID_KEY );
+        if( mbedtls_mpi_get_bit( d, 0 ) != 0 ||
+            mbedtls_mpi_get_bit( d, 1 ) != 0 ||
+            mbedtls_mpi_get_bit( d, 2 ) != 0 ||
+            mbedtls_mpi_msb( d ) - 1 != grp->nbits ) /* mbedtls_mpi_msb is one-based! */
+            return( MBEDTLS_ERR_ECP_INVALID_KEY );
         else
             return( 0 );
     }
-#endif /* POLARSSL_ECP_MONTGOMERY */
-#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
-    if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
+#endif /* MBEDTLS_ECP_MONTGOMERY */
+#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS)
+    if( ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS )
     {
         /* see SEC1 3.2 */
-        if( mpi_cmp_int( d, 1 ) < 0 ||
-            mpi_cmp_mpi( d, &grp->N ) >= 0 )
-            return( POLARSSL_ERR_ECP_INVALID_KEY );
+        if( mbedtls_mpi_cmp_int( d, 1 ) < 0 ||
+            mbedtls_mpi_cmp_mpi( d, &grp->N ) >= 0 )
+            return( MBEDTLS_ERR_ECP_INVALID_KEY );
         else
             return( 0 );
     }
-#endif /* POLARSSL_ECP_SHORT_WEIERSTRASS */
+#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS */
 
-    return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
+    return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
 }
 
 /*
  * Generate a keypair
  */
-int ecp_gen_keypair( ecp_group *grp, mpi *d, ecp_point *Q,
+int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
                      int (*f_rng)(void *, unsigned char *, size_t),
                      void *p_rng )
 {
     int ret;
     size_t n_size = ( grp->nbits + 7 ) / 8;
 
-#if defined(POLARSSL_ECP_MONTGOMERY)
-    if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
+#if defined(MBEDTLS_ECP_MONTGOMERY)
+    if( ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY )
     {
         /* [M225] page 5 */
         size_t b;
 
-        MPI_CHK( mpi_fill_random( d, n_size, f_rng, p_rng ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( d, n_size, f_rng, p_rng ) );
 
         /* Make sure the most significant bit is nbits */
-        b = mpi_msb( d ) - 1; /* mpi_msb is one-based */
+        b = mbedtls_mpi_msb( d ) - 1; /* mbedtls_mpi_msb is one-based */
         if( b > grp->nbits )
-            MPI_CHK( mpi_shift_r( d, b - grp->nbits ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( d, b - grp->nbits ) );
         else
-            MPI_CHK( mpi_set_bit( d, grp->nbits, 1 ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, grp->nbits, 1 ) );
 
         /* Make sure the last three bits are unset */
-        MPI_CHK( mpi_set_bit( d, 0, 0 ) );
-        MPI_CHK( mpi_set_bit( d, 1, 0 ) );
-        MPI_CHK( mpi_set_bit( d, 2, 0 ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 0, 0 ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 1, 0 ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( d, 2, 0 ) );
     }
     else
-#endif /* POLARSSL_ECP_MONTGOMERY */
-#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
-    if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
+#endif /* MBEDTLS_ECP_MONTGOMERY */
+#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS)
+    if( ecp_get_type( grp ) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS )
     {
         /* SEC1 3.2.1: Generate d such that 1 <= n < N */
         int count = 0;
-        unsigned char rnd[POLARSSL_ECP_MAX_BYTES];
+        unsigned char rnd[MBEDTLS_ECP_MAX_BYTES];
 
         /*
          * Match the procedure given in RFC 6979 (deterministic ECDSA):
@@ -1852,9 +1852,9 @@
          */
         do
         {
-            MPI_CHK( f_rng( p_rng, rnd, n_size ) );
-            MPI_CHK( mpi_read_binary( d, rnd, n_size ) );
-            MPI_CHK( mpi_shift_r( d, 8 * n_size - grp->nbits ) );
+            MBEDTLS_MPI_CHK( f_rng( p_rng, rnd, n_size ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( d, rnd, n_size ) );
+            MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( d, 8 * n_size - grp->nbits ) );
 
             /*
              * Each try has at worst a probability 1/2 of failing (the msb has
@@ -1866,90 +1866,90 @@
              * such as secp224k1 are actually very close to the worst case.
              */
             if( ++count > 30 )
-                return( POLARSSL_ERR_ECP_RANDOM_FAILED );
+                return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
         }
-        while( mpi_cmp_int( d, 1 ) < 0 ||
-               mpi_cmp_mpi( d, &grp->N ) >= 0 );
+        while( mbedtls_mpi_cmp_int( d, 1 ) < 0 ||
+               mbedtls_mpi_cmp_mpi( d, &grp->N ) >= 0 );
     }
     else
-#endif /* POLARSSL_ECP_SHORT_WEIERSTRASS */
-        return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
+#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS */
+        return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
 
 cleanup:
     if( ret != 0 )
         return( ret );
 
-    return( ecp_mul( grp, Q, d, &grp->G, f_rng, p_rng ) );
+    return( mbedtls_ecp_mul( grp, Q, d, &grp->G, f_rng, p_rng ) );
 }
 
 /*
  * Generate a keypair, prettier wrapper
  */
-int ecp_gen_key( ecp_group_id grp_id, ecp_keypair *key,
+int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
                 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
 {
     int ret;
 
-    if( ( ret = ecp_use_known_dp( &key->grp, grp_id ) ) != 0 )
+    if( ( ret = mbedtls_ecp_use_known_dp( &key->grp, grp_id ) ) != 0 )
         return( ret );
 
-    return( ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) );
+    return( mbedtls_ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) );
 }
 
 /*
  * Check a public-private key pair
  */
-int ecp_check_pub_priv( const ecp_keypair *pub, const ecp_keypair *prv )
+int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv )
 {
     int ret;
-    ecp_point Q;
-    ecp_group grp;
+    mbedtls_ecp_point Q;
+    mbedtls_ecp_group grp;
 
-    if( pub->grp.id == POLARSSL_ECP_DP_NONE ||
+    if( pub->grp.id == MBEDTLS_ECP_DP_NONE ||
         pub->grp.id != prv->grp.id ||
-        mpi_cmp_mpi( &pub->Q.X, &prv->Q.X ) ||
-        mpi_cmp_mpi( &pub->Q.Y, &prv->Q.Y ) ||
-        mpi_cmp_mpi( &pub->Q.Z, &prv->Q.Z ) )
+        mbedtls_mpi_cmp_mpi( &pub->Q.X, &prv->Q.X ) ||
+        mbedtls_mpi_cmp_mpi( &pub->Q.Y, &prv->Q.Y ) ||
+        mbedtls_mpi_cmp_mpi( &pub->Q.Z, &prv->Q.Z ) )
     {
-        return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
+        return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
     }
 
-    ecp_point_init( &Q );
-    ecp_group_init( &grp );
+    mbedtls_ecp_point_init( &Q );
+    mbedtls_ecp_group_init( &grp );
 
-    /* ecp_mul() needs a non-const group... */
-    ecp_group_copy( &grp, &prv->grp );
+    /* mbedtls_ecp_mul() needs a non-const group... */
+    mbedtls_ecp_group_copy( &grp, &prv->grp );
 
     /* Also checks d is valid */
-    MPI_CHK( ecp_mul( &grp, &Q, &prv->d, &prv->grp.G, NULL, NULL ) );
+    MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &Q, &prv->d, &prv->grp.G, NULL, NULL ) );
 
-    if( mpi_cmp_mpi( &Q.X, &prv->Q.X ) ||
-        mpi_cmp_mpi( &Q.Y, &prv->Q.Y ) ||
-        mpi_cmp_mpi( &Q.Z, &prv->Q.Z ) )
+    if( mbedtls_mpi_cmp_mpi( &Q.X, &prv->Q.X ) ||
+        mbedtls_mpi_cmp_mpi( &Q.Y, &prv->Q.Y ) ||
+        mbedtls_mpi_cmp_mpi( &Q.Z, &prv->Q.Z ) )
     {
-        ret = POLARSSL_ERR_ECP_BAD_INPUT_DATA;
+        ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
         goto cleanup;
     }
 
 cleanup:
-    ecp_point_free( &Q );
-    ecp_group_free( &grp );
+    mbedtls_ecp_point_free( &Q );
+    mbedtls_ecp_group_free( &grp );
 
     return( ret );
 }
 
-#if defined(POLARSSL_SELF_TEST)
+#if defined(MBEDTLS_SELF_TEST)
 
 /*
  * Checkup routine
  */
-int ecp_self_test( int verbose )
+int mbedtls_ecp_self_test( int verbose )
 {
     int ret;
     size_t i;
-    ecp_group grp;
-    ecp_point R, P;
-    mpi m;
+    mbedtls_ecp_group grp;
+    mbedtls_ecp_point R, P;
+    mbedtls_mpi m;
     unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
     /* exponents especially adapted for secp192r1 */
     const char *exponents[] =
@@ -1962,30 +1962,30 @@
         "555555555555555555555555555555555555555555555555", /* 101010... */
     };
 
-    ecp_group_init( &grp );
-    ecp_point_init( &R );
-    ecp_point_init( &P );
-    mpi_init( &m );
+    mbedtls_ecp_group_init( &grp );
+    mbedtls_ecp_point_init( &R );
+    mbedtls_ecp_point_init( &P );
+    mbedtls_mpi_init( &m );
 
     /* Use secp192r1 if available, or any available curve */
-#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
-    MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
+#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
+    MBEDTLS_MPI_CHK( mbedtls_ecp_use_known_dp( &grp, MBEDTLS_ECP_DP_SECP192R1 ) );
 #else
-    MPI_CHK( ecp_use_known_dp( &grp, ecp_curve_list()->grp_id ) );
+    MBEDTLS_MPI_CHK( mbedtls_ecp_use_known_dp( &grp, mbedtls_ecp_curve_list()->grp_id ) );
 #endif
 
     if( verbose != 0 )
-        polarssl_printf( "  ECP test #1 (constant op_count, base point G): " );
+        mbedtls_printf( "  ECP test #1 (constant op_count, base point G): " );
 
     /* Do a dummy multiplication first to trigger precomputation */
-    MPI_CHK( mpi_lset( &m, 2 ) );
-    MPI_CHK( ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &m, 2 ) );
+    MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
 
     add_count = 0;
     dbl_count = 0;
     mul_count = 0;
-    MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
-    MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[0] ) );
+    MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
 
     for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
     {
@@ -1996,15 +1996,15 @@
         dbl_count = 0;
         mul_count = 0;
 
-        MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
-        MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[i] ) );
+        MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
 
         if( add_count != add_c_prev ||
             dbl_count != dbl_c_prev ||
             mul_count != mul_c_prev )
         {
             if( verbose != 0 )
-                polarssl_printf( "failed (%u)\n", (unsigned int) i );
+                mbedtls_printf( "failed (%u)\n", (unsigned int) i );
 
             ret = 1;
             goto cleanup;
@@ -2012,17 +2012,17 @@
     }
 
     if( verbose != 0 )
-        polarssl_printf( "passed\n" );
+        mbedtls_printf( "passed\n" );
 
     if( verbose != 0 )
-        polarssl_printf( "  ECP test #2 (constant op_count, other point): " );
+        mbedtls_printf( "  ECP test #2 (constant op_count, other point): " );
     /* We computed P = 2G last time, use it */
 
     add_count = 0;
     dbl_count = 0;
     mul_count = 0;
-    MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
-    MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[0] ) );
+    MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
 
     for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
     {
@@ -2033,15 +2033,15 @@
         dbl_count = 0;
         mul_count = 0;
 
-        MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
-        MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
+        MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &m, 16, exponents[i] ) );
+        MBEDTLS_MPI_CHK( mbedtls_ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
 
         if( add_count != add_c_prev ||
             dbl_count != dbl_c_prev ||
             mul_count != mul_c_prev )
         {
             if( verbose != 0 )
-                polarssl_printf( "failed (%u)\n", (unsigned int) i );
+                mbedtls_printf( "failed (%u)\n", (unsigned int) i );
 
             ret = 1;
             goto cleanup;
@@ -2049,24 +2049,24 @@
     }
 
     if( verbose != 0 )
-        polarssl_printf( "passed\n" );
+        mbedtls_printf( "passed\n" );
 
 cleanup:
 
     if( ret < 0 && verbose != 0 )
-        polarssl_printf( "Unexpected error, return code = %08X\n", ret );
+        mbedtls_printf( "Unexpected error, return code = %08X\n", ret );
 
-    ecp_group_free( &grp );
-    ecp_point_free( &R );
-    ecp_point_free( &P );
-    mpi_free( &m );
+    mbedtls_ecp_group_free( &grp );
+    mbedtls_ecp_point_free( &R );
+    mbedtls_ecp_point_free( &P );
+    mbedtls_mpi_free( &m );
 
     if( verbose != 0 )
-        polarssl_printf( "\n" );
+        mbedtls_printf( "\n" );
 
     return( ret );
 }
 
-#endif /* POLARSSL_SELF_TEST */
+#endif /* MBEDTLS_SELF_TEST */
 
-#endif /* POLARSSL_ECP_C */
+#endif /* MBEDTLS_ECP_C */
diff --git a/library/ecp_curves.c b/library/ecp_curves.c
index 893bfa9..4cac3ab 100644
--- a/library/ecp_curves.c
+++ b/library/ecp_curves.c
@@ -20,13 +20,13 @@
  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#if !defined(POLARSSL_CONFIG_FILE)
+#if !defined(MBEDTLS_CONFIG_FILE)
 #include "mbedtls/config.h"
 #else
-#include POLARSSL_CONFIG_FILE
+#include MBEDTLS_CONFIG_FILE
 #endif
 
-#if defined(POLARSSL_ECP_C)
+#if defined(MBEDTLS_ECP_C)
 
 #include "mbedtls/ecp.h"
 
@@ -42,9 +42,9 @@
 
 /*
  * Conversion macros for embedded constants:
- * build lists of t_uint's from lists of unsigned char's grouped by 8, 4 or 2
+ * build lists of mbedtls_mpi_uint's from lists of unsigned char's grouped by 8, 4 or 2
  */
-#if defined(POLARSSL_HAVE_INT8)
+#if defined(MBEDTLS_HAVE_INT8)
 
 #define BYTES_TO_T_UINT_8( a, b, c, d, e, f, g, h ) \
     a, b, c, d, e, f, g, h
@@ -55,11 +55,11 @@
 #define BYTES_TO_T_UINT_2( a, b )                   \
     a, b
 
-#elif defined(POLARSSL_HAVE_INT16)
+#elif defined(MBEDTLS_HAVE_INT16)
 
 #define BYTES_TO_T_UINT_2( a, b )                   \
-    ( (t_uint) a << 0 ) |                           \
-    ( (t_uint) b << 8 )
+    ( (mbedtls_mpi_uint) a << 0 ) |                           \
+    ( (mbedtls_mpi_uint) b << 8 )
 
 #define BYTES_TO_T_UINT_4( a, b, c, d )             \
     BYTES_TO_T_UINT_2( a, b ),                      \
@@ -71,13 +71,13 @@
     BYTES_TO_T_UINT_2( e, f ),                      \
     BYTES_TO_T_UINT_2( g, h )
 
-#elif defined(POLARSSL_HAVE_INT32)
+#elif defined(MBEDTLS_HAVE_INT32)
 
 #define BYTES_TO_T_UINT_4( a, b, c, d )             \
-    ( (t_uint) a <<  0 ) |                          \
-    ( (t_uint) b <<  8 ) |                          \
-    ( (t_uint) c << 16 ) |                          \
-    ( (t_uint) d << 24 )
+    ( (mbedtls_mpi_uint) a <<  0 ) |                          \
+    ( (mbedtls_mpi_uint) b <<  8 ) |                          \
+    ( (mbedtls_mpi_uint) c << 16 ) |                          \
+    ( (mbedtls_mpi_uint) d << 24 )
 
 #define BYTES_TO_T_UINT_2( a, b )                   \
     BYTES_TO_T_UINT_4( a, b, 0, 0 )
@@ -89,14 +89,14 @@
 #else /* 64-bits */
 
 #define BYTES_TO_T_UINT_8( a, b, c, d, e, f, g, h ) \
-    ( (t_uint) a <<  0 ) |                          \
-    ( (t_uint) b <<  8 ) |                          \
-    ( (t_uint) c << 16 ) |                          \
-    ( (t_uint) d << 24 ) |                          \
-    ( (t_uint) e << 32 ) |                          \
-    ( (t_uint) f << 40 ) |                          \
-    ( (t_uint) g << 48 ) |                          \
-    ( (t_uint) h << 56 )
+    ( (mbedtls_mpi_uint) a <<  0 ) |                          \
+    ( (mbedtls_mpi_uint) b <<  8 ) |                          \
+    ( (mbedtls_mpi_uint) c << 16 ) |                          \
+    ( (mbedtls_mpi_uint) d << 24 ) |                          \
+    ( (mbedtls_mpi_uint) e << 32 ) |                          \
+    ( (mbedtls_mpi_uint) f << 40 ) |                          \
+    ( (mbedtls_mpi_uint) g << 48 ) |                          \
+    ( (mbedtls_mpi_uint) h << 56 )
 
 #define BYTES_TO_T_UINT_4( a, b, c, d )             \
     BYTES_TO_T_UINT_8( a, b, c, d, 0, 0, 0, 0 )
@@ -104,7 +104,7 @@
 #define BYTES_TO_T_UINT_2( a, b )                   \
     BYTES_TO_T_UINT_8( a, b, 0, 0, 0, 0, 0, 0 )
 
-#endif /* bits in t_uint */
+#endif /* bits in mbedtls_mpi_uint */
 
 /*
  * Note: the constants are in little-endian order
@@ -114,111 +114,111 @@
 /*
  * Domain parameters for secp192r1
  */
-#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
-static const t_uint secp192r1_p[] = {
+#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
+static const mbedtls_mpi_uint secp192r1_p[] = {
     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
     BYTES_TO_T_UINT_8( 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
 };
-static const t_uint secp192r1_b[] = {
+static const mbedtls_mpi_uint secp192r1_b[] = {
     BYTES_TO_T_UINT_8( 0xB1, 0xB9, 0x46, 0xC1, 0xEC, 0xDE, 0xB8, 0xFE ),
     BYTES_TO_T_UINT_8( 0x49, 0x30, 0x24, 0x72, 0xAB, 0xE9, 0xA7, 0x0F ),
     BYTES_TO_T_UINT_8( 0xE7, 0x80, 0x9C, 0xE5, 0x19, 0x05, 0x21, 0x64 ),
 };
-static const t_uint secp192r1_gx[] = {
+static const mbedtls_mpi_uint secp192r1_gx[] = {
     BYTES_TO_T_UINT_8( 0x12, 0x10, 0xFF, 0x82, 0xFD, 0x0A, 0xFF, 0xF4 ),
     BYTES_TO_T_UINT_8( 0x00, 0x88, 0xA1, 0x43, 0xEB, 0x20, 0xBF, 0x7C ),
     BYTES_TO_T_UINT_8( 0xF6, 0x90, 0x30, 0xB0, 0x0E, 0xA8, 0x8D, 0x18 ),
 };
-static const t_uint secp192r1_gy[] = {
+static const mbedtls_mpi_uint secp192r1_gy[] = {
     BYTES_TO_T_UINT_8( 0x11, 0x48, 0x79, 0x1E, 0xA1, 0x77, 0xF9, 0x73 ),
     BYTES_TO_T_UINT_8( 0xD5, 0xCD, 0x24, 0x6B, 0xED, 0x11, 0x10, 0x63 ),
     BYTES_TO_T_UINT_8( 0x78, 0xDA, 0xC8, 0xFF, 0x95, 0x2B, 0x19, 0x07 ),
 };
-static const t_uint secp192r1_n[] = {
+static const mbedtls_mpi_uint secp192r1_n[] = {
     BYTES_TO_T_UINT_8( 0x31, 0x28, 0xD2, 0xB4, 0xB1, 0xC9, 0x6B, 0x14 ),
     BYTES_TO_T_UINT_8( 0x36, 0xF8, 0xDE, 0x99, 0xFF, 0xFF, 0xFF, 0xFF ),
     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
 };
-#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
+#endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
 
 /*
  * Domain parameters for secp224r1
  */
-#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
-static const t_uint secp224r1_p[] = {
+#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
+static const mbedtls_mpi_uint secp224r1_p[] = {
     BYTES_TO_T_UINT_8( 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ),
     BYTES_TO_T_UINT_8( 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF ),
     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00 ),
 };
-static const t_uint secp224r1_b[] = {
+static const mbedtls_mpi_uint secp224r1_b[] = {
     BYTES_TO_T_UINT_8( 0xB4, 0xFF, 0x55, 0x23, 0x43, 0x39, 0x0B, 0x27 ),
     BYTES_TO_T_UINT_8( 0xBA, 0xD8, 0xBF, 0xD7, 0xB7, 0xB0, 0x44, 0x50 ),
     BYTES_TO_T_UINT_8( 0x56, 0x32, 0x41, 0xF5, 0xAB, 0xB3, 0x04, 0x0C ),
     BYTES_TO_T_UINT_4( 0x85, 0x0A, 0x05, 0xB4 ),
 };
-static const t_uint secp224r1_gx[] = {
+static const mbedtls_mpi_uint secp224r1_gx[] = {
     BYTES_TO_T_UINT_8( 0x21, 0x1D, 0x5C, 0x11, 0xD6, 0x80, 0x32, 0x34 ),
     BYTES_TO_T_UINT_8( 0x22, 0x11, 0xC2, 0x56, 0xD3, 0xC1, 0x03, 0x4A ),
     BYTES_TO_T_UINT_8( 0xB9, 0x90, 0x13, 0x32, 0x7F, 0xBF, 0xB4, 0x6B ),
     BYTES_TO_T_UINT_4( 0xBD, 0x0C, 0x0E, 0xB7 ),
 };
-static const t_uint secp224r1_gy[] = {
+static const mbedtls_mpi_uint secp224r1_gy[] = {
     BYTES_TO_T_UINT_8( 0x34, 0x7E, 0x00, 0x85, 0x99, 0x81, 0xD5, 0x44 ),
     BYTES_TO_T_UINT_8( 0x64, 0x47, 0x07, 0x5A, 0xA0, 0x75, 0x43, 0xCD ),
     BYTES_TO_T_UINT_8( 0xE6, 0xDF, 0x22, 0x4C, 0xFB, 0x23, 0xF7, 0xB5 ),
     BYTES_TO_T_UINT_4( 0x88, 0x63, 0x37, 0xBD ),
 };
-static const t_uint secp224r1_n[] = {
+static const mbedtls_mpi_uint secp224r1_n[] = {
     BYTES_TO_T_UINT_8( 0x3D, 0x2A, 0x5C, 0x5C, 0x45, 0x29, 0xDD, 0x13 ),
     BYTES_TO_T_UINT_8( 0x3E, 0xF0, 0xB8, 0xE0, 0xA2, 0x16, 0xFF, 0xFF ),
     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
     BYTES_TO_T_UINT_4( 0xFF, 0xFF, 0xFF, 0xFF ),
 };
-#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */
+#endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
 
 /*
  * Domain parameters for secp256r1
  */
-#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
-static const t_uint secp256r1_p[] = {
+#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
+static const mbedtls_mpi_uint secp256r1_p[] = {
     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00 ),
     BYTES_TO_T_UINT_8( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ),
     BYTES_TO_T_UINT_8( 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF ),
 };
-static const t_uint secp256r1_b[] = {
+static const mbedtls_mpi_uint secp256r1_b[] = {
     BYTES_TO_T_UINT_8( 0x4B, 0x60, 0xD2, 0x27, 0x3E, 0x3C, 0xCE, 0x3B ),
     BYTES_TO_T_UINT_8( 0xF6, 0xB0, 0x53, 0xCC, 0xB0, 0x06, 0x1D, 0x65 ),
     BYTES_TO_T_UINT_8( 0xBC, 0x86, 0x98, 0x76, 0x55, 0xBD, 0xEB, 0xB3 ),
     BYTES_TO_T_UINT_8( 0xE7, 0x93, 0x3A, 0xAA, 0xD8, 0x35, 0xC6, 0x5A ),
 };
-static const t_uint secp256r1_gx[] = {
+static const mbedtls_mpi_uint secp256r1_gx[] = {
     BYTES_TO_T_UINT_8( 0x96, 0xC2, 0x98, 0xD8, 0x45, 0x39, 0xA1, 0xF4 ),
     BYTES_TO_T_UINT_8( 0xA0, 0x33, 0xEB, 0x2D, 0x81, 0x7D, 0x03, 0x77 ),
     BYTES_TO_T_UINT_8( 0xF2, 0x40, 0xA4, 0x63, 0xE5, 0xE6, 0xBC, 0xF8 ),
     BYTES_TO_T_UINT_8( 0x47, 0x42, 0x2C, 0xE1, 0xF2, 0xD1, 0x17, 0x6B ),
 };
-static const t_uint secp256r1_gy[] = {
+static const mbedtls_mpi_uint secp256r1_gy[] = {
     BYTES_TO_T_UINT_8( 0xF5, 0x51, 0xBF, 0x37, 0x68, 0x40, 0xB6, 0xCB ),
     BYTES_TO_T_UINT_8( 0xCE, 0x5E, 0x31, 0x6B, 0x57, 0x33, 0xCE, 0x2B ),
     BYTES_TO_T_UINT_8( 0x16, 0x9E, 0x0F, 0x7C, 0x4A, 0xEB, 0xE7, 0x8E ),
     BYTES_TO_T_UINT_8( 0x9B, 0x7F, 0x1A, 0xFE, 0xE2, 0x42, 0xE3, 0x4F ),
 };
-static const t_uint secp256r1_n[] = {
+static const mbedtls_mpi_uint secp256r1_n[] = {
     BYTES_TO_T_UINT_8( 0x51, 0x25, 0x63, 0xFC, 0xC2, 0xCA, 0xB9, 0xF3 ),
     BYTES_TO_T_UINT_8( 0x84, 0x9E, 0x17, 0xA7, 0xAD, 0xFA, 0xE6, 0xBC ),
     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
     BYTES_TO_T_UINT_8( 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF ),
 };
-#endif /* POLARSSL_ECP_DP_SECP256R1_ENABLED */
+#endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */
 
 /*
  * Domain parameters for secp384r1
  */
-#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
-static const t_uint secp384r1_p[] = {
+#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
+static const mbedtls_mpi_uint secp384r1_p[] = {
     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00 ),
     BYTES_TO_T_UINT_8( 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF ),
     BYTES_TO_T_UINT_8( 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
@@ -226,7 +226,7 @@
     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
 };
-static const t_uint secp384r1_b[] = {
+static const mbedtls_mpi_uint secp384r1_b[] = {
     BYTES_TO_T_UINT_8( 0xEF, 0x2A, 0xEC, 0xD3, 0xED, 0xC8, 0x85, 0x2A ),
     BYTES_TO_T_UINT_8( 0x9D, 0xD1, 0x2E, 0x8A, 0x8D, 0x39, 0x56, 0xC6 ),
     BYTES_TO_T_UINT_8( 0x5A, 0x87, 0x13, 0x50, 0x8F, 0x08, 0x14, 0x03 ),
@@ -234,7 +234,7 @@
     BYTES_TO_T_UINT_8( 0x19, 0x2D, 0xF8, 0xE3, 0x6B, 0x05, 0x8E, 0x98 ),
     BYTES_TO_T_UINT_8( 0xE4, 0xE7, 0x3E, 0xE2, 0xA7, 0x2F, 0x31, 0xB3 ),
 };
-static const t_uint secp384r1_gx[] = {
+static const mbedtls_mpi_uint secp384r1_gx[] = {
     BYTES_TO_T_UINT_8( 0xB7, 0x0A, 0x76, 0x72, 0x38, 0x5E, 0x54, 0x3A ),
     BYTES_TO_T_UINT_8( 0x6C, 0x29, 0x55, 0xBF, 0x5D, 0xF2, 0x02, 0x55 ),
     BYTES_TO_T_UINT_8( 0x38, 0x2A, 0x54, 0x82, 0xE0, 0x41, 0xF7, 0x59 ),
@@ -242,7 +242,7 @@
     BYTES_TO_T_UINT_8( 0x74, 0xAD, 0x20, 0xF3, 0x1E, 0xC7, 0xB1, 0x8E ),
     BYTES_TO_T_UINT_8( 0x37, 0x05, 0x8B, 0xBE, 0x22, 0xCA, 0x87, 0xAA ),
 };
-static const t_uint secp384r1_gy[] = {
+static const mbedtls_mpi_uint secp384r1_gy[] = {
     BYTES_TO_T_UINT_8( 0x5F, 0x0E, 0xEA, 0x90, 0x7C, 0x1D, 0x43, 0x7A ),
     BYTES_TO_T_UINT_8( 0x9D, 0x81, 0x7E, 0x1D, 0xCE, 0xB1, 0x60, 0x0A ),
     BYTES_TO_T_UINT_8( 0xC0, 0xB8, 0xF0, 0xB5, 0x13, 0x31, 0xDA, 0xE9 ),
@@ -250,7 +250,7 @@
     BYTES_TO_T_UINT_8( 0x29, 0xDC, 0x92, 0x92, 0xBF, 0x98, 0x9E, 0x5D ),
     BYTES_TO_T_UINT_8( 0x6F, 0x2C, 0x26, 0x96, 0x4A, 0xDE, 0x17, 0x36 ),
 };
-static const t_uint secp384r1_n[] = {
+static const mbedtls_mpi_uint secp384r1_n[] = {
     BYTES_TO_T_UINT_8( 0x73, 0x29, 0xC5, 0xCC, 0x6A, 0x19, 0xEC, 0xEC ),
     BYTES_TO_T_UINT_8( 0x7A, 0xA7, 0xB0, 0x48, 0xB2, 0x0D, 0x1A, 0x58 ),
     BYTES_TO_T_UINT_8( 0xDF, 0x2D, 0x37, 0xF4, 0x81, 0x4D, 0x63, 0xC7 ),
@@ -258,13 +258,13 @@
     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
 };
-#endif /* POLARSSL_ECP_DP_SECP384R1_ENABLED */
+#endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
 
 /*
  * Domain parameters for secp521r1
  */
-#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
-static const t_uint secp521r1_p[] = {
+#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
+static const mbedtls_mpi_uint secp521r1_p[] = {
     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
@@ -275,7 +275,7 @@
     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
     BYTES_TO_T_UINT_2( 0xFF, 0x01 ),
 };
-static const t_uint secp521r1_b[] = {
+static const mbedtls_mpi_uint secp521r1_b[] = {
     BYTES_TO_T_UINT_8( 0x00, 0x3F, 0x50, 0x6B, 0xD4, 0x1F, 0x45, 0xEF ),
     BYTES_TO_T_UINT_8( 0xF1, 0x34, 0x2C, 0x3D, 0x88, 0xDF, 0x73, 0x35 ),
     BYTES_TO_T_UINT_8( 0x07, 0xBF, 0xB1, 0x3B, 0xBD, 0xC0, 0x52, 0x16 ),
@@ -286,7 +286,7 @@
     BYTES_TO_T_UINT_8( 0x1F, 0x9A, 0x1C, 0x8E, 0x61, 0xB9, 0x3E, 0x95 ),
     BYTES_TO_T_UINT_2( 0x51, 0x00 ),
 };
-static const t_uint secp521r1_gx[] = {
+static const mbedtls_mpi_uint secp521r1_gx[] = {
     BYTES_TO_T_UINT_8( 0x66, 0xBD, 0xE5, 0xC2, 0x31, 0x7E, 0x7E, 0xF9 ),
     BYTES_TO_T_UINT_8( 0x9B, 0x42, 0x6A, 0x85, 0xC1, 0xB3, 0x48, 0x33 ),
     BYTES_TO_T_UINT_8( 0xDE, 0xA8, 0xFF, 0xA2, 0x27, 0xC1, 0x1D, 0xFE ),
@@ -297,7 +297,7 @@
     BYTES_TO_T_UINT_8( 0xCD, 0xE9, 0x04, 0x04, 0xB7, 0x06, 0x8E, 0x85 ),
     BYTES_TO_T_UINT_2( 0xC6, 0x00 ),
 };
-static const t_uint secp521r1_gy[] = {
+static const mbedtls_mpi_uint secp521r1_gy[] = {
     BYTES_TO_T_UINT_8( 0x50, 0x66, 0xD1, 0x9F, 0x76, 0x94, 0xBE, 0x88 ),
     BYTES_TO_T_UINT_8( 0x40, 0xC2, 0x72, 0xA2, 0x86, 0x70, 0x3C, 0x35 ),
     BYTES_TO_T_UINT_8( 0x61, 0x07, 0xAD, 0x3F, 0x01, 0xB9, 0x50, 0xC5 ),
@@ -308,7 +308,7 @@
     BYTES_TO_T_UINT_8( 0x04, 0xC0, 0x3B, 0x9A, 0x78, 0x6A, 0x29, 0x39 ),
     BYTES_TO_T_UINT_2( 0x18, 0x01 ),
 };
-static const t_uint secp521r1_n[] = {
+static const mbedtls_mpi_uint secp521r1_n[] = {
     BYTES_TO_T_UINT_8( 0x09, 0x64, 0x38, 0x91, 0x1E, 0xB7, 0x6F, 0xBB ),
     BYTES_TO_T_UINT_8( 0xAE, 0x47, 0x9C, 0x89, 0xB8, 0xC9, 0xB5, 0x3B ),
     BYTES_TO_T_UINT_8( 0xD0, 0xA5, 0x09, 0xF7, 0x48, 0x01, 0xCC, 0x7F ),
@@ -319,150 +319,150 @@
     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
     BYTES_TO_T_UINT_2( 0xFF, 0x01 ),
 };
-#endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */
+#endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
 
-#if defined(POLARSSL_ECP_DP_SECP192K1_ENABLED)
-static const t_uint secp192k1_p[] = {
+#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
+static const mbedtls_mpi_uint secp192k1_p[] = {
     BYTES_TO_T_UINT_8( 0x37, 0xEE, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF ),
     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
 };
-static const t_uint secp192k1_a[] = {
+static const mbedtls_mpi_uint secp192k1_a[] = {
     BYTES_TO_T_UINT_2( 0x00, 0x00 ),
 };
-static const t_uint secp192k1_b[] = {
+static const mbedtls_mpi_uint secp192k1_b[] = {
     BYTES_TO_T_UINT_2( 0x03, 0x00 ),
 };
-static const t_uint secp192k1_gx[] = {
+static const mbedtls_mpi_uint secp192k1_gx[] = {
     BYTES_TO_T_UINT_8( 0x7D, 0x6C, 0xE0, 0xEA, 0xB1, 0xD1, 0xA5, 0x1D ),
     BYTES_TO_T_UINT_8( 0x34, 0xF4, 0xB7, 0x80, 0x02, 0x7D, 0xB0, 0x26 ),
     BYTES_TO_T_UINT_8( 0xAE, 0xE9, 0x57, 0xC0, 0x0E, 0xF1, 0x4F, 0xDB ),
 };
-static const t_uint secp192k1_gy[] = {
+static const mbedtls_mpi_uint secp192k1_gy[] = {
     BYTES_TO_T_UINT_8( 0x9D, 0x2F, 0x5E, 0xD9, 0x88, 0xAA, 0x82, 0x40 ),
     BYTES_TO_T_UINT_8( 0x34, 0x86, 0xBE, 0x15, 0xD0, 0x63, 0x41, 0x84 ),
     BYTES_TO_T_UINT_8( 0xA7, 0x28, 0x56, 0x9C, 0x6D, 0x2F, 0x2F, 0x9B ),
 };
-static const t_uint secp192k1_n[] = {
+static const mbedtls_mpi_uint secp192k1_n[] = {
     BYTES_TO_T_UINT_8( 0x8D, 0xFD, 0xDE, 0x74, 0x6A, 0x46, 0x69, 0x0F ),
     BYTES_TO_T_UINT_8( 0x17, 0xFC, 0xF2, 0x26, 0xFE, 0xFF, 0xFF, 0xFF ),
     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
 };
-#endif /* POLARSSL_ECP_DP_SECP192K1_ENABLED */
+#endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
 
-#if defined(POLARSSL_ECP_DP_SECP224K1_ENABLED)
-static const t_uint secp224k1_p[] = {
+#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
+static const mbedtls_mpi_uint secp224k1_p[] = {
     BYTES_TO_T_UINT_8( 0x6D, 0xE5, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF ),
     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
     BYTES_TO_T_UINT_4( 0xFF, 0xFF, 0xFF, 0xFF ),
 };
-static const t_uint secp224k1_a[] = {
+static const mbedtls_mpi_uint secp224k1_a[] = {
     BYTES_TO_T_UINT_2( 0x00, 0x00 ),
 };
-static const t_uint secp224k1_b[] = {
+static const mbedtls_mpi_uint secp224k1_b[] = {
     BYTES_TO_T_UINT_2( 0x05, 0x00 ),
 };
-static const t_uint secp224k1_gx[] = {
+static const mbedtls_mpi_uint secp224k1_gx[] = {
     BYTES_TO_T_UINT_8( 0x5C, 0xA4, 0xB7, 0xB6, 0x0E, 0x65, 0x7E, 0x0F ),
     BYTES_TO_T_UINT_8( 0xA9, 0x75, 0x70, 0xE4, 0xE9, 0x67, 0xA4, 0x69 ),
     BYTES_TO_T_UINT_8( 0xA1, 0x28, 0xFC, 0x30, 0xDF, 0x99, 0xF0, 0x4D ),
     BYTES_TO_T_UINT_4( 0x33, 0x5B, 0x45, 0xA1 ),
 };
-static const t_uint secp224k1_gy[] = {
+static const mbedtls_mpi_uint secp224k1_gy[] = {
     BYTES_TO_T_UINT_8( 0xA5, 0x61, 0x6D, 0x55, 0xDB, 0x4B, 0xCA, 0xE2 ),
     BYTES_TO_T_UINT_8( 0x59, 0xBD, 0xB0, 0xC0, 0xF7, 0x19, 0xE3, 0xF7 ),
     BYTES_TO_T_UINT_8( 0xD6, 0xFB, 0xCA, 0x82, 0x42, 0x34, 0xBA, 0x7F ),
     BYTES_TO_T_UINT_4( 0xED, 0x9F, 0x08, 0x7E ),
 };
-static const t_uint secp224k1_n[] = {
+static const mbedtls_mpi_uint secp224k1_n[] = {
     BYTES_TO_T_UINT_8( 0xF7, 0xB1, 0x9F, 0x76, 0x71, 0xA9, 0xF0, 0xCA ),
     BYTES_TO_T_UINT_8( 0x84, 0x61, 0xEC, 0xD2, 0xE8, 0xDC, 0x01, 0x00 ),
     BYTES_TO_T_UINT_8( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ),
     BYTES_TO_T_UINT_8( 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00 ),
 };
-#endif /* POLARSSL_ECP_DP_SECP224K1_ENABLED */
+#endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
 
-#if defined(POLARSSL_ECP_DP_SECP256K1_ENABLED)
-static const t_uint secp256k1_p[] = {
+#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
+static const mbedtls_mpi_uint secp256k1_p[] = {
     BYTES_TO_T_UINT_8( 0x2F, 0xFC, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF ),
     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
 };
-static const t_uint secp256k1_a[] = {
+static const mbedtls_mpi_uint secp256k1_a[] = {
     BYTES_TO_T_UINT_2( 0x00, 0x00 ),
 };
-static const t_uint secp256k1_b[] = {
+static const mbedtls_mpi_uint secp256k1_b[] = {
     BYTES_TO_T_UINT_2( 0x07, 0x00 ),
 };
-static const t_uint secp256k1_gx[] = {
+static const mbedtls_mpi_uint secp256k1_gx[] = {
     BYTES_TO_T_UINT_8( 0x98, 0x17, 0xF8, 0x16, 0x5B, 0x81, 0xF2, 0x59 ),
     BYTES_TO_T_UINT_8( 0xD9, 0x28, 0xCE, 0x2D, 0xDB, 0xFC, 0x9B, 0x02 ),
     BYTES_TO_T_UINT_8( 0x07, 0x0B, 0x87, 0xCE, 0x95, 0x62, 0xA0, 0x55 ),
     BYTES_TO_T_UINT_8( 0xAC, 0xBB, 0xDC, 0xF9, 0x7E, 0x66, 0xBE, 0x79 ),
 };
-static const t_uint secp256k1_gy[] = {
+static const mbedtls_mpi_uint secp256k1_gy[] = {
     BYTES_TO_T_UINT_8( 0xB8, 0xD4, 0x10, 0xFB, 0x8F, 0xD0, 0x47, 0x9C ),
     BYTES_TO_T_UINT_8( 0x19, 0x54, 0x85, 0xA6, 0x48, 0xB4, 0x17, 0xFD ),
     BYTES_TO_T_UINT_8( 0xA8, 0x08, 0x11, 0x0E, 0xFC, 0xFB, 0xA4, 0x5D ),
     BYTES_TO_T_UINT_8( 0x65, 0xC4, 0xA3, 0x26, 0x77, 0xDA, 0x3A, 0x48 ),
 };
-static const t_uint secp256k1_n[] = {
+static const mbedtls_mpi_uint secp256k1_n[] = {
     BYTES_TO_T_UINT_8( 0x41, 0x41, 0x36, 0xD0, 0x8C, 0x5E, 0xD2, 0xBF ),
     BYTES_TO_T_UINT_8( 0x3B, 0xA0, 0x48, 0xAF, 0xE6, 0xDC, 0xAE, 0xBA ),
     BYTES_TO_T_UINT_8( 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
     BYTES_TO_T_UINT_8( 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF ),
 };
-#endif /* POLARSSL_ECP_DP_SECP256K1_ENABLED */
+#endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
 
 /*
  * Domain parameters for brainpoolP256r1 (RFC 5639 3.4)
  */
-#if defined(POLARSSL_ECP_DP_BP256R1_ENABLED)
-static const t_uint brainpoolP256r1_p[] = {
+#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
+static const mbedtls_mpi_uint brainpoolP256r1_p[] = {
     BYTES_TO_T_UINT_8( 0x77, 0x53, 0x6E, 0x1F, 0x1D, 0x48, 0x13, 0x20 ),
     BYTES_TO_T_UINT_8( 0x28, 0x20, 0x26, 0xD5, 0x23, 0xF6, 0x3B, 0x6E ),
     BYTES_TO_T_UINT_8( 0x72, 0x8D, 0x83, 0x9D, 0x90, 0x0A, 0x66, 0x3E ),
     BYTES_TO_T_UINT_8( 0xBC, 0xA9, 0xEE, 0xA1, 0xDB, 0x57, 0xFB, 0xA9 ),
 };
-static const t_uint brainpoolP256r1_a[] = {
+static const mbedtls_mpi_uint brainpoolP256r1_a[] = {
     BYTES_TO_T_UINT_8( 0xD9, 0xB5, 0x30, 0xF3, 0x44, 0x4B, 0x4A, 0xE9 ),
     BYTES_TO_T_UINT_8( 0x6C, 0x5C, 0xDC, 0x26, 0xC1, 0x55, 0x80, 0xFB ),
     BYTES_TO_T_UINT_8( 0xE7, 0xFF, 0x7A, 0x41, 0x30, 0x75, 0xF6, 0xEE ),
     BYTES_TO_T_UINT_8( 0x57, 0x30, 0x2C, 0xFC, 0x75, 0x09, 0x5A, 0x7D ),
 };
-static const t_uint brainpoolP256r1_b[] = {
+static const mbedtls_mpi_uint brainpoolP256r1_b[] = {
     BYTES_TO_T_UINT_8( 0xB6, 0x07, 0x8C, 0xFF, 0x18, 0xDC, 0xCC, 0x6B ),
     BYTES_TO_T_UINT_8( 0xCE, 0xE1, 0xF7, 0x5C, 0x29, 0x16, 0x84, 0x95 ),
     BYTES_TO_T_UINT_8( 0xBF, 0x7C, 0xD7, 0xBB, 0xD9, 0xB5, 0x30, 0xF3 ),
     BYTES_TO_T_UINT_8( 0x44, 0x4B, 0x4A, 0xE9, 0x6C, 0x5C, 0xDC, 0x26 ),
 };
-static const t_uint brainpoolP256r1_gx[] = {
+static const mbedtls_mpi_uint brainpoolP256r1_gx[] = {
     BYTES_TO_T_UINT_8( 0x62, 0x32, 0xCE, 0x9A, 0xBD, 0x53, 0x44, 0x3A ),
     BYTES_TO_T_UINT_8( 0xC2, 0x23, 0xBD, 0xE3, 0xE1, 0x27, 0xDE, 0xB9 ),
     BYTES_TO_T_UINT_8( 0xAF, 0xB7, 0x81, 0xFC, 0x2F, 0x48, 0x4B, 0x2C ),
     BYTES_TO_T_UINT_8( 0xCB, 0x57, 0x7E, 0xCB, 0xB9, 0xAE, 0xD2, 0x8B ),
 };
-static const t_uint brainpoolP256r1_gy[] = {
+static const mbedtls_mpi_uint brainpoolP256r1_gy[] = {
     BYTES_TO_T_UINT_8( 0x97, 0x69, 0x04, 0x2F, 0xC7, 0x54, 0x1D, 0x5C ),
     BYTES_TO_T_UINT_8( 0x54, 0x8E, 0xED, 0x2D, 0x13, 0x45, 0x77, 0xC2 ),
     BYTES_TO_T_UINT_8( 0xC9, 0x1D, 0x61, 0x14, 0x1A, 0x46, 0xF8, 0x97 ),
     BYTES_TO_T_UINT_8( 0xFD, 0xC4, 0xDA, 0xC3, 0x35, 0xF8, 0x7E, 0x54 ),
 };
-static const t_uint brainpoolP256r1_n[] = {
+static const mbedtls_mpi_uint brainpoolP256r1_n[] = {
     BYTES_TO_T_UINT_8( 0xA7, 0x56, 0x48, 0x97, 0x82, 0x0E, 0x1E, 0x90 ),
     BYTES_TO_T_UINT_8( 0xF7, 0xA6, 0x61, 0xB5, 0xA3, 0x7A, 0x39, 0x8C ),
     BYTES_TO_T_UINT_8( 0x71, 0x8D, 0x83, 0x9D, 0x90, 0x0A, 0x66, 0x3E ),
     BYTES_TO_T_UINT_8( 0xBC, 0xA9, 0xEE, 0xA1, 0xDB, 0x57, 0xFB, 0xA9 ),
 };
-#endif /* POLARSSL_ECP_DP_BP256R1_ENABLED */
+#endif /* MBEDTLS_ECP_DP_BP256R1_ENABLED */
 
 /*
  * Domain parameters for brainpoolP384r1 (RFC 5639 3.6)
  */
-#if defined(POLARSSL_ECP_DP_BP384R1_ENABLED)
-static const t_uint brainpoolP384r1_p[] = {
+#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
+static const mbedtls_mpi_uint brainpoolP384r1_p[] = {
     BYTES_TO_T_UINT_8( 0x53, 0xEC, 0x07, 0x31, 0x13, 0x00, 0x47, 0x87 ),
     BYTES_TO_T_UINT_8( 0x71, 0x1A, 0x1D, 0x90, 0x29, 0xA7, 0xD3, 0xAC ),
     BYTES_TO_T_UINT_8( 0x23, 0x11, 0xB7, 0x7F, 0x19, 0xDA, 0xB1, 0x12 ),
@@ -470,7 +470,7 @@
     BYTES_TO_T_UINT_8( 0xDF, 0x41, 0xE6, 0x50, 0x7E, 0x6F, 0x5D, 0x0F ),
     BYTES_TO_T_UINT_8( 0x28, 0x6D, 0x38, 0xA3, 0x82, 0x1E, 0xB9, 0x8C ),
 };
-static const t_uint brainpoolP384r1_a[] = {
+static const mbedtls_mpi_uint brainpoolP384r1_a[] = {
     BYTES_TO_T_UINT_8( 0x26, 0x28, 0xCE, 0x22, 0xDD, 0xC7, 0xA8, 0x04 ),
     BYTES_TO_T_UINT_8( 0xEB, 0xD4, 0x3A, 0x50, 0x4A, 0x81, 0xA5, 0x8A ),
     BYTES_TO_T_UINT_8( 0x0F, 0xF9, 0x91, 0xBA, 0xEF, 0x65, 0x91, 0x13 ),
@@ -478,7 +478,7 @@
     BYTES_TO_T_UINT_8( 0xA0, 0xAF, 0x05, 0xCE, 0x0A, 0x08, 0x72, 0x3C ),
     BYTES_TO_T_UINT_8( 0x0C, 0x15, 0x8C, 0x3D, 0xC6, 0x82, 0xC3, 0x7B ),
 };
-static const t_uint brainpoolP384r1_b[] = {
+static const mbedtls_mpi_uint brainpoolP384r1_b[] = {
     BYTES_TO_T_UINT_8( 0x11, 0x4C, 0x50, 0xFA, 0x96, 0x86, 0xB7, 0x3A ),
     BYTES_TO_T_UINT_8( 0x94, 0xC9, 0xDB, 0x95, 0x02, 0x39, 0xB4, 0x7C ),
     BYTES_TO_T_UINT_8( 0xD5, 0x62, 0xEB, 0x3E, 0xA5, 0x0E, 0x88, 0x2E ),
@@ -486,7 +486,7 @@
     BYTES_TO_T_UINT_8( 0x7C, 0x44, 0xF0, 0x16, 0x54, 0xB5, 0x39, 0x8B ),
     BYTES_TO_T_UINT_8( 0x26, 0x28, 0xCE, 0x22, 0xDD, 0xC7, 0xA8, 0x04 ),
 };
-static const t_uint brainpoolP384r1_gx[] = {
+static const mbedtls_mpi_uint brainpoolP384r1_gx[] = {
     BYTES_TO_T_UINT_8( 0x1E, 0xAF, 0xD4, 0x47, 0xE2, 0xB2, 0x87, 0xEF ),
     BYTES_TO_T_UINT_8( 0xAA, 0x46, 0xD6, 0x36, 0x34, 0xE0, 0x26, 0xE8 ),
     BYTES_TO_T_UINT_8( 0xE8, 0x10, 0xBD, 0x0C, 0xFE, 0xCA, 0x7F, 0xDB ),
@@ -494,7 +494,7 @@
     BYTES_TO_T_UINT_8( 0x6B, 0x3F, 0xC1, 0xB7, 0x81, 0x3A, 0xA6, 0xA2 ),
     BYTES_TO_T_UINT_8( 0xFF, 0x45, 0xCF, 0x68, 0xF0, 0x64, 0x1C, 0x1D ),
 };
-static const t_uint brainpoolP384r1_gy[] = {
+static const mbedtls_mpi_uint brainpoolP384r1_gy[] = {
     BYTES_TO_T_UINT_8( 0x15, 0x53, 0x3C, 0x26, 0x41, 0x03, 0x82, 0x42 ),
     BYTES_TO_T_UINT_8( 0x11, 0x81, 0x91, 0x77, 0x21, 0x46, 0x46, 0x0E ),
     BYTES_TO_T_UINT_8( 0x28, 0x29, 0x91, 0xF9, 0x4F, 0x05, 0x9C, 0xE1 ),
@@ -502,7 +502,7 @@
     BYTES_TO_T_UINT_8( 0x52, 0xD5, 0xCF, 0x95, 0x8E, 0xEB, 0xB1, 0x5C ),
     BYTES_TO_T_UINT_8( 0xA4, 0xC2, 0xF9, 0x20, 0x75, 0x1D, 0xBE, 0x8A ),
 };
-static const t_uint brainpoolP384r1_n[] = {
+static const mbedtls_mpi_uint brainpoolP384r1_n[] = {
     BYTES_TO_T_UINT_8( 0x65, 0x65, 0x04, 0xE9, 0x02, 0x32, 0x88, 0x3B ),
     BYTES_TO_T_UINT_8( 0x10, 0xC3, 0x7F, 0x6B, 0xAF, 0xB6, 0x3A, 0xCF ),
     BYTES_TO_T_UINT_8( 0xA7, 0x25, 0x04, 0xAC, 0x6C, 0x6E, 0x16, 0x1F ),
@@ -510,13 +510,13 @@
     BYTES_TO_T_UINT_8( 0xDF, 0x41, 0xE6, 0x50, 0x7E, 0x6F, 0x5D, 0x0F ),
     BYTES_TO_T_UINT_8( 0x28, 0x6D, 0x38, 0xA3, 0x82, 0x1E, 0xB9, 0x8C ),
 };
-#endif /* POLARSSL_ECP_DP_BP384R1_ENABLED */
+#endif /* MBEDTLS_ECP_DP_BP384R1_ENABLED */
 
 /*
  * Domain parameters for brainpoolP512r1 (RFC 5639 3.7)
  */
-#if defined(POLARSSL_ECP_DP_BP512R1_ENABLED)
-static const t_uint brainpoolP512r1_p[] = {
+#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
+static const mbedtls_mpi_uint brainpoolP512r1_p[] = {
     BYTES_TO_T_UINT_8( 0xF3, 0x48, 0x3A, 0x58, 0x56, 0x60, 0xAA, 0x28 ),
     BYTES_TO_T_UINT_8( 0x85, 0xC6, 0x82, 0x2D, 0x2F, 0xFF, 0x81, 0x28 ),
     BYTES_TO_T_UINT_8( 0xE6, 0x80, 0xA3, 0xE6, 0x2A, 0xA1, 0xCD, 0xAE ),
@@ -526,7 +526,7 @@
     BYTES_TO_T_UINT_8( 0x07, 0xFC, 0xC9, 0x33, 0xAE, 0xE6, 0xD4, 0x3F ),
     BYTES_TO_T_UINT_8( 0x8B, 0xC4, 0xE9, 0xDB, 0xB8, 0x9D, 0xDD, 0xAA ),
 };
-static const t_uint brainpoolP512r1_a[] = {
+static const mbedtls_mpi_uint brainpoolP512r1_a[] = {
     BYTES_TO_T_UINT_8( 0xCA, 0x94, 0xFC, 0x77, 0x4D, 0xAC, 0xC1, 0xE7 ),
     BYTES_TO_T_UINT_8( 0xB9, 0xC7, 0xF2, 0x2B, 0xA7, 0x17, 0x11, 0x7F ),
     BYTES_TO_T_UINT_8( 0xB5, 0xC8, 0x9A, 0x8B, 0xC9, 0xF1, 0x2E, 0x0A ),
@@ -536,7 +536,7 @@
     BYTES_TO_T_UINT_8( 0xC5, 0x4C, 0x23, 0xAC, 0x45, 0x71, 0x32, 0xE2 ),
     BYTES_TO_T_UINT_8( 0x89, 0x3B, 0x60, 0x8B, 0x31, 0xA3, 0x30, 0x78 ),
 };
-static const t_uint brainpoolP512r1_b[] = {
+static const mbedtls_mpi_uint brainpoolP512r1_b[] = {
     BYTES_TO_T_UINT_8( 0x23, 0xF7, 0x16, 0x80, 0x63, 0xBD, 0x09, 0x28 ),
     BYTES_TO_T_UINT_8( 0xDD, 0xE5, 0xBA, 0x5E, 0xB7, 0x50, 0x40, 0x98 ),
     BYTES_TO_T_UINT_8( 0x67, 0x3E, 0x08, 0xDC, 0xCA, 0x94, 0xFC, 0x77 ),
@@ -546,7 +546,7 @@
     BYTES_TO_T_UINT_8( 0x5A, 0x5D, 0xED, 0x2D, 0xBC, 0x63, 0x98, 0xEA ),
     BYTES_TO_T_UINT_8( 0xCA, 0x41, 0x34, 0xA8, 0x10, 0x16, 0xF9, 0x3D ),
 };
-static const t_uint brainpoolP512r1_gx[] = {
+static const mbedtls_mpi_uint brainpoolP512r1_gx[] = {
     BYTES_TO_T_UINT_8( 0x22, 0xF8, 0xB9, 0xBC, 0x09, 0x22, 0x35, 0x8B ),
     BYTES_TO_T_UINT_8( 0x68, 0x5E, 0x6A, 0x40, 0x47, 0x50, 0x6D, 0x7C ),
     BYTES_TO_T_UINT_8( 0x5F, 0x7D, 0xB9, 0x93, 0x7B, 0x68, 0xD1, 0x50 ),
@@ -556,7 +556,7 @@
     BYTES_TO_T_UINT_8( 0x93, 0x6A, 0x4C, 0x9C, 0x2E, 0x32, 0x21, 0x5A ),
     BYTES_TO_T_UINT_8( 0x64, 0xD9, 0x2E, 0xD8, 0xBD, 0xE4, 0xAE, 0x81 ),
 };
-static const t_uint brainpoolP512r1_gy[] = {
+static const mbedtls_mpi_uint brainpoolP512r1_gy[] = {
     BYTES_TO_T_UINT_8( 0x92, 0x08, 0xD8, 0x3A, 0x0F, 0x1E, 0xCD, 0x78 ),
     BYTES_TO_T_UINT_8( 0x06, 0x54, 0xF0, 0xA8, 0x2F, 0x2B, 0xCA, 0xD1 ),
     BYTES_TO_T_UINT_8( 0xAE, 0x63, 0x27, 0x8A, 0xD8, 0x4B, 0xCA, 0x5B ),
@@ -566,7 +566,7 @@
     BYTES_TO_T_UINT_8( 0xFD, 0x22, 0x78, 0xCF, 0xA9, 0xBF, 0xEA, 0xC0 ),
     BYTES_TO_T_UINT_8( 0xEC, 0x32, 0x63, 0x56, 0x5D, 0x38, 0xDE, 0x7D ),
 };
-static const t_uint brainpoolP512r1_n[] = {
+static const mbedtls_mpi_uint brainpoolP512r1_n[] = {
     BYTES_TO_T_UINT_8( 0x69, 0x00, 0xA9, 0x9C, 0x82, 0x96, 0x87, 0xB5 ),
     BYTES_TO_T_UINT_8( 0xDD, 0xDA, 0x5D, 0x08, 0x81, 0xD3, 0xB1, 0x1D ),
     BYTES_TO_T_UINT_8( 0x47, 0x10, 0xAC, 0x7F, 0x19, 0x61, 0x86, 0x41 ),
@@ -576,25 +576,25 @@
     BYTES_TO_T_UINT_8( 0x07, 0xFC, 0xC9, 0x33, 0xAE, 0xE6, 0xD4, 0x3F ),
     BYTES_TO_T_UINT_8( 0x8B, 0xC4, 0xE9, 0xDB, 0xB8, 0x9D, 0xDD, 0xAA ),
 };
-#endif /* POLARSSL_ECP_DP_BP512R1_ENABLED */
+#endif /* MBEDTLS_ECP_DP_BP512R1_ENABLED */
 
 /*
  * Create an MPI from embedded constants
- * (assumes len is an exact multiple of sizeof t_uint)
+ * (assumes len is an exact multiple of sizeof mbedtls_mpi_uint)
  */
-static inline void ecp_mpi_load( mpi *X, const t_uint *p, size_t len )
+static inline void ecp_mpi_load( mbedtls_mpi *X, const mbedtls_mpi_uint *p, size_t len )
 {
     X->s = 1;
-    X->n = len / sizeof( t_uint );
-    X->p = (t_uint *) p;
+    X->n = len / sizeof( mbedtls_mpi_uint );
+    X->p = (mbedtls_mpi_uint *) p;
 }
 
 /*
  * Set an MPI to static value 1
  */
-static inline void ecp_mpi_set1( mpi *X )
+static inline void ecp_mpi_set1( mbedtls_mpi *X )
 {
-    static t_uint one[] = { 1 };
+    static mbedtls_mpi_uint one[] = { 1 };
     X->s = 1;
     X->n = 1;
     X->p = one;
@@ -603,13 +603,13 @@
 /*
  * Make group available from embedded constants
  */
-static int ecp_group_load( ecp_group *grp,
-                           const t_uint *p,  size_t plen,
-                           const t_uint *a,  size_t alen,
-                           const t_uint *b,  size_t blen,
-                           const t_uint *gx, size_t gxlen,
-                           const t_uint *gy, size_t gylen,
-                           const t_uint *n,  size_t nlen)
+static int ecp_group_load( mbedtls_ecp_group *grp,
+                           const mbedtls_mpi_uint *p,  size_t plen,
+                           const mbedtls_mpi_uint *a,  size_t alen,
+                           const mbedtls_mpi_uint *b,  size_t blen,
+                           const mbedtls_mpi_uint *gx, size_t gxlen,
+                           const mbedtls_mpi_uint *gy, size_t gylen,
+                           const mbedtls_mpi_uint *n,  size_t nlen)
 {
     ecp_mpi_load( &grp->P, p, plen );
     if( a != NULL )
@@ -621,49 +621,49 @@
     ecp_mpi_load( &grp->G.Y, gy, gylen );
     ecp_mpi_set1( &grp->G.Z );
 
-    grp->pbits = mpi_msb( &grp->P );
-    grp->nbits = mpi_msb( &grp->N );
+    grp->pbits = mbedtls_mpi_msb( &grp->P );
+    grp->nbits = mbedtls_mpi_msb( &grp->N );
 
     grp->h = 1;
 
     return( 0 );
 }
 
-#if defined(POLARSSL_ECP_NIST_OPTIM)
+#if defined(MBEDTLS_ECP_NIST_OPTIM)
 /* Forward declarations */
-#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
-static int ecp_mod_p192( mpi * );
+#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
+static int ecp_mod_p192( mbedtls_mpi * );
 #endif
-#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
-static int ecp_mod_p224( mpi * );
+#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
+static int ecp_mod_p224( mbedtls_mpi * );
 #endif
-#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
-static int ecp_mod_p256( mpi * );
+#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
+static int ecp_mod_p256( mbedtls_mpi * );
 #endif
-#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
-static int ecp_mod_p384( mpi * );
+#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
+static int ecp_mod_p384( mbedtls_mpi * );
 #endif
-#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
-static int ecp_mod_p521( mpi * );
+#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
+static int ecp_mod_p521( mbedtls_mpi * );
 #endif
 
 #define NIST_MODP( P )      grp->modp = ecp_mod_ ## P;
 #else
 #define NIST_MODP( P )
-#endif /* POLARSSL_ECP_NIST_OPTIM */
+#endif /* MBEDTLS_ECP_NIST_OPTIM */
 
 /* Additional forward declarations */
-#if defined(POLARSSL_ECP_DP_M255_ENABLED)
-static int ecp_mod_p255( mpi * );
+#if defined(MBEDTLS_ECP_DP_M255_ENABLED)
+static int ecp_mod_p255( mbedtls_mpi * );
 #endif
-#if defined(POLARSSL_ECP_DP_SECP192K1_ENABLED)
-static int ecp_mod_p192k1( mpi * );
+#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
+static int ecp_mod_p192k1( mbedtls_mpi * );
 #endif
-#if defined(POLARSSL_ECP_DP_SECP224K1_ENABLED)
-static int ecp_mod_p224k1( mpi * );
+#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
+static int ecp_mod_p224k1( mbedtls_mpi * );
 #endif
-#if defined(POLARSSL_ECP_DP_SECP256K1_ENABLED)
-static int ecp_mod_p256k1( mpi * );
+#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
+static int ecp_mod_p256k1( mbedtls_mpi * );
 #endif
 
 #define LOAD_GROUP_A( G )   ecp_group_load( grp,            \
@@ -682,127 +682,127 @@
                             G ## _gy, sizeof( G ## _gy ),   \
                             G ## _n,  sizeof( G ## _n  ) )
 
-#if defined(POLARSSL_ECP_DP_M255_ENABLED)
+#if defined(MBEDTLS_ECP_DP_M255_ENABLED)
 /*
  * Specialized function for creating the Curve25519 group
  */
-static int ecp_use_curve25519( ecp_group *grp )
+static int ecp_use_curve25519( mbedtls_ecp_group *grp )
 {
     int ret;
 
     /* Actually ( A + 2 ) / 4 */
-    MPI_CHK( mpi_read_string( &grp->A, 16, "01DB42" ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &grp->A, 16, "01DB42" ) );
 
     /* P = 2^255 - 19 */
-    MPI_CHK( mpi_lset( &grp->P, 1 ) );
-    MPI_CHK( mpi_shift_l( &grp->P, 255 ) );
-    MPI_CHK( mpi_sub_int( &grp->P, &grp->P, 19 ) );
-    grp->pbits = mpi_msb( &grp->P );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &grp->P, 1 ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &grp->P, 255 ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &grp->P, &grp->P, 19 ) );
+    grp->pbits = mbedtls_mpi_msb( &grp->P );
 
     /* Y intentionaly not set, since we use x/z coordinates.
      * This is used as a marker to identify Montgomery curves! */
-    MPI_CHK( mpi_lset( &grp->G.X, 9 ) );
-    MPI_CHK( mpi_lset( &grp->G.Z, 1 ) );
-    mpi_free( &grp->G.Y );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &grp->G.X, 9 ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &grp->G.Z, 1 ) );
+    mbedtls_mpi_free( &grp->G.Y );
 
     /* Actually, the required msb for private keys */
     grp->nbits = 254;
 
 cleanup:
     if( ret != 0 )
-        ecp_group_free( grp );
+        mbedtls_ecp_group_free( grp );
 
     return( ret );
 }
-#endif /* POLARSSL_ECP_DP_M255_ENABLED */
+#endif /* MBEDTLS_ECP_DP_M255_ENABLED */
 
 /*
  * Set a group using well-known domain parameters
  */
-int ecp_use_known_dp( ecp_group *grp, ecp_group_id id )
+int mbedtls_ecp_use_known_dp( mbedtls_ecp_group *grp, mbedtls_ecp_group_id id )
 {
-    ecp_group_free( grp );
+    mbedtls_ecp_group_free( grp );
 
     grp->id = id;
 
     switch( id )
     {
-#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
-        case POLARSSL_ECP_DP_SECP192R1:
+#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
+        case MBEDTLS_ECP_DP_SECP192R1:
             NIST_MODP( p192 );
             return( LOAD_GROUP( secp192r1 ) );
-#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
+#endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
 
-#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
-        case POLARSSL_ECP_DP_SECP224R1:
+#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
+        case MBEDTLS_ECP_DP_SECP224R1:
             NIST_MODP( p224 );
             return( LOAD_GROUP( secp224r1 ) );
-#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */
+#endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
 
-#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
-        case POLARSSL_ECP_DP_SECP256R1:
+#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
+        case MBEDTLS_ECP_DP_SECP256R1:
             NIST_MODP( p256 );
             return( LOAD_GROUP( secp256r1 ) );
-#endif /* POLARSSL_ECP_DP_SECP256R1_ENABLED */
+#endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */
 
-#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
-        case POLARSSL_ECP_DP_SECP384R1:
+#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
+        case MBEDTLS_ECP_DP_SECP384R1:
             NIST_MODP( p384 );
             return( LOAD_GROUP( secp384r1 ) );
-#endif /* POLARSSL_ECP_DP_SECP384R1_ENABLED */
+#endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
 
-#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
-        case POLARSSL_ECP_DP_SECP521R1:
+#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
+        case MBEDTLS_ECP_DP_SECP521R1:
             NIST_MODP( p521 );
             return( LOAD_GROUP( secp521r1 ) );
-#endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */
+#endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
 
-#if defined(POLARSSL_ECP_DP_SECP192K1_ENABLED)
-        case POLARSSL_ECP_DP_SECP192K1:
+#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
+        case MBEDTLS_ECP_DP_SECP192K1:
             grp->modp = ecp_mod_p192k1;
             return( LOAD_GROUP_A( secp192k1 ) );
-#endif /* POLARSSL_ECP_DP_SECP192K1_ENABLED */
+#endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
 
-#if defined(POLARSSL_ECP_DP_SECP224K1_ENABLED)
-        case POLARSSL_ECP_DP_SECP224K1:
+#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
+        case MBEDTLS_ECP_DP_SECP224K1:
             grp->modp = ecp_mod_p224k1;
             return( LOAD_GROUP_A( secp224k1 ) );
-#endif /* POLARSSL_ECP_DP_SECP224K1_ENABLED */
+#endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
 
-#if defined(POLARSSL_ECP_DP_SECP256K1_ENABLED)
-        case POLARSSL_ECP_DP_SECP256K1:
+#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
+        case MBEDTLS_ECP_DP_SECP256K1:
             grp->modp = ecp_mod_p256k1;
             return( LOAD_GROUP_A( secp256k1 ) );
-#endif /* POLARSSL_ECP_DP_SECP256K1_ENABLED */
+#endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
 
-#if defined(POLARSSL_ECP_DP_BP256R1_ENABLED)
-        case POLARSSL_ECP_DP_BP256R1:
+#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
+        case MBEDTLS_ECP_DP_BP256R1:
             return( LOAD_GROUP_A( brainpoolP256r1 ) );
-#endif /* POLARSSL_ECP_DP_BP256R1_ENABLED */
+#endif /* MBEDTLS_ECP_DP_BP256R1_ENABLED */
 
-#if defined(POLARSSL_ECP_DP_BP384R1_ENABLED)
-        case POLARSSL_ECP_DP_BP384R1:
+#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
+        case MBEDTLS_ECP_DP_BP384R1:
             return( LOAD_GROUP_A( brainpoolP384r1 ) );
-#endif /* POLARSSL_ECP_DP_BP384R1_ENABLED */
+#endif /* MBEDTLS_ECP_DP_BP384R1_ENABLED */
 
-#if defined(POLARSSL_ECP_DP_BP512R1_ENABLED)
-        case POLARSSL_ECP_DP_BP512R1:
+#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
+        case MBEDTLS_ECP_DP_BP512R1:
             return( LOAD_GROUP_A( brainpoolP512r1 ) );
-#endif /* POLARSSL_ECP_DP_BP512R1_ENABLED */
+#endif /* MBEDTLS_ECP_DP_BP512R1_ENABLED */
 
-#if defined(POLARSSL_ECP_DP_M255_ENABLED)
-        case POLARSSL_ECP_DP_M255:
+#if defined(MBEDTLS_ECP_DP_M255_ENABLED)
+        case MBEDTLS_ECP_DP_M255:
             grp->modp = ecp_mod_p255;
             return( ecp_use_curve25519( grp ) );
-#endif /* POLARSSL_ECP_DP_M255_ENABLED */
+#endif /* MBEDTLS_ECP_DP_M255_ENABLED */
 
         default:
-            ecp_group_free( grp );
-            return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
+            mbedtls_ecp_group_free( grp );
+            return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
     }
 }
 
-#if defined(POLARSSL_ECP_NIST_OPTIM)
+#if defined(MBEDTLS_ECP_NIST_OPTIM)
 /*
  * Fast reduction modulo the primes used by the NIST curves.
  *
@@ -813,7 +813,7 @@
  * MPI remains loose, since these functions can be deactivated at will.
  */
 
-#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
+#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
 /*
  * Compared to the way things are presented in FIPS 186-3 D.2,
  * we proceed in columns, from right (least significant chunk) to left,
@@ -822,16 +822,16 @@
  * compared to the more straightforward, line-oriented approach.
  *
  * For this prime we need to handle data in chunks of 64 bits.
- * Since this is always a multiple of our basic t_uint, we can
- * use a t_uint * to designate such a chunk, and small loops to handle it.
+ * Since this is always a multiple of our basic mbedtls_mpi_uint, we can
+ * use a mbedtls_mpi_uint * to designate such a chunk, and small loops to handle it.
  */
 
 /* Add 64-bit chunks (dst += src) and update carry */
-static inline void add64( t_uint *dst, t_uint *src, t_uint *carry )
+static inline void add64( mbedtls_mpi_uint *dst, mbedtls_mpi_uint *src, mbedtls_mpi_uint *carry )
 {
     unsigned char i;
-    t_uint c = 0;
-    for( i = 0; i < 8 / sizeof( t_uint ); i++, dst++, src++ )
+    mbedtls_mpi_uint c = 0;
+    for( i = 0; i < 8 / sizeof( mbedtls_mpi_uint ); i++, dst++, src++ )
     {
         *dst += c;      c  = ( *dst < c );
         *dst += *src;   c += ( *dst < *src );
@@ -840,17 +840,17 @@
 }
 
 /* Add carry to a 64-bit chunk and update carry */
-static inline void carry64( t_uint *dst, t_uint *carry )
+static inline void carry64( mbedtls_mpi_uint *dst, mbedtls_mpi_uint *carry )
 {
     unsigned char i;
-    for( i = 0; i < 8 / sizeof( t_uint ); i++, dst++ )
+    for( i = 0; i < 8 / sizeof( mbedtls_mpi_uint ); i++, dst++ )
     {
         *dst += *carry;
         *carry  = ( *dst < *carry );
     }
 }
 
-#define WIDTH       8 / sizeof( t_uint )
+#define WIDTH       8 / sizeof( mbedtls_mpi_uint )
 #define A( i )      N->p + i * WIDTH
 #define ADD( i )    add64( p, A( i ), &c )
 #define NEXT        p += WIDTH; carry64( p, &c )
@@ -859,14 +859,14 @@
 /*
  * Fast quasi-reduction modulo p192 (FIPS 186-3 D.2.1)
  */
-static int ecp_mod_p192( mpi *N )
+static int ecp_mod_p192( mbedtls_mpi *N )
 {
     int ret;
-    t_uint c = 0;
-    t_uint *p, *end;
+    mbedtls_mpi_uint c = 0;
+    mbedtls_mpi_uint *p, *end;
 
     /* Make sure we have enough blocks so that A(5) is legal */
-    MPI_CHK( mpi_grow( N, 6 * WIDTH ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_grow( N, 6 * WIDTH ) );
 
     p = N->p;
     end = p + N->n;
@@ -884,11 +884,11 @@
 #undef ADD
 #undef NEXT
 #undef LAST
-#endif /* POLARSSL_ECP_DP_SECP192R1_ENABLED */
+#endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
 
-#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED) ||   \
-    defined(POLARSSL_ECP_DP_SECP256R1_ENABLED) ||   \
-    defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
+#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) ||   \
+    defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) ||   \
+    defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
 /*
  * The reader is advised to first understand ecp_mod_p192() since the same
  * general structure is used here, but with additional complications:
@@ -907,26 +907,26 @@
  */
 #define LOAD32      cur = A( i );
 
-#if defined(POLARSSL_HAVE_INT8)     /* 8 bit */
+#if defined(MBEDTLS_HAVE_INT8)     /* 8 bit */
 
 #define MAX32       N->n / 4
 #define A( j )      (uint32_t)( N->p[4*j+0]       ) |  \
                               ( N->p[4*j+1] << 8  ) |  \
                               ( N->p[4*j+2] << 16 ) |  \
                               ( N->p[4*j+3] << 24 )
-#define STORE32     N->p[4*i+0] = (t_uint)( cur       );   \
-                    N->p[4*i+1] = (t_uint)( cur >> 8  );   \
-                    N->p[4*i+2] = (t_uint)( cur >> 16 );   \
-                    N->p[4*i+3] = (t_uint)( cur >> 24 );
+#define STORE32     N->p[4*i+0] = (mbedtls_mpi_uint)( cur       );   \
+                    N->p[4*i+1] = (mbedtls_mpi_uint)( cur >> 8  );   \
+                    N->p[4*i+2] = (mbedtls_mpi_uint)( cur >> 16 );   \
+                    N->p[4*i+3] = (mbedtls_mpi_uint)( cur >> 24 );
 
-#elif defined(POLARSSL_HAVE_INT16)  /* 16 bit */
+#elif defined(MBEDTLS_HAVE_INT16)  /* 16 bit */
 
 #define MAX32       N->n / 2
 #define A( j )      (uint32_t)( N->p[2*j] ) | ( N->p[2*j+1] << 16 )
-#define STORE32     N->p[2*i+0] = (t_uint)( cur       );  \
-                    N->p[2*i+1] = (t_uint)( cur >> 16 );
+#define STORE32     N->p[2*i+0] = (mbedtls_mpi_uint)( cur       );  \
+                    N->p[2*i+1] = (mbedtls_mpi_uint)( cur >> 16 );
 
-#elif defined(POLARSSL_HAVE_INT32)  /* 32 bit */
+#elif defined(MBEDTLS_HAVE_INT32)  /* 32 bit */
 
 #define MAX32       N->n
 #define A( j )      N->p[j]
@@ -939,13 +939,13 @@
 #define STORE32                                   \
     if( i % 2 ) {                                 \
         N->p[i/2] &= 0x00000000FFFFFFFF;          \
-        N->p[i/2] |= ((t_uint) cur) << 32;        \
+        N->p[i/2] |= ((mbedtls_mpi_uint) cur) << 32;        \
     } else {                                      \
         N->p[i/2] &= 0xFFFFFFFF00000000;          \
-        N->p[i/2] |= (t_uint) cur;                \
+        N->p[i/2] |= (mbedtls_mpi_uint) cur;                \
     }
 
-#endif /* sizeof( t_uint ) */
+#endif /* sizeof( mbedtls_mpi_uint ) */
 
 /*
  * Helpers for addition and subtraction of chunks, with signed carry.
@@ -974,15 +974,15 @@
     signed char c = 0, cc;                                  \
     uint32_t cur;                                           \
     size_t i = 0, bits = b;                                 \
-    mpi C;                                                  \
-    t_uint Cp[ b / 8 / sizeof( t_uint) + 1 ];               \
+    mbedtls_mpi C;                                                  \
+    mbedtls_mpi_uint Cp[ b / 8 / sizeof( mbedtls_mpi_uint) + 1 ];               \
                                                             \
     C.s = 1;                                                \
-    C.n = b / 8 / sizeof( t_uint) + 1;                      \
+    C.n = b / 8 / sizeof( mbedtls_mpi_uint) + 1;                      \
     C.p = Cp;                                               \
-    memset( Cp, 0, C.n * sizeof( t_uint ) );                \
+    memset( Cp, 0, C.n * sizeof( mbedtls_mpi_uint ) );                \
                                                             \
-    MPI_CHK( mpi_grow( N, b * 2 / 8 / sizeof( t_uint ) ) ); \
+    MBEDTLS_MPI_CHK( mbedtls_mpi_grow( N, b * 2 / 8 / sizeof( mbedtls_mpi_uint ) ) ); \
     LOAD32;
 
 #define NEXT                    \
@@ -1003,22 +1003,22 @@
  * If the result is negative, we get it in the form
  * c * 2^(bits + 32) + N, with c negative and N positive shorter than 'bits'
  */
-static inline int fix_negative( mpi *N, signed char c, mpi *C, size_t bits )
+static inline int fix_negative( mbedtls_mpi *N, signed char c, mbedtls_mpi *C, size_t bits )
 {
     int ret;
 
     /* C = - c * 2^(bits + 32) */
-#if !defined(POLARSSL_HAVE_INT64)
+#if !defined(MBEDTLS_HAVE_INT64)
     ((void) bits);
 #else
     if( bits == 224 )
-        C->p[ C->n - 1 ] = ((t_uint) -c) << 32;
+        C->p[ C->n - 1 ] = ((mbedtls_mpi_uint) -c) << 32;
     else
 #endif
-        C->p[ C->n - 1 ] = (t_uint) -c;
+        C->p[ C->n - 1 ] = (mbedtls_mpi_uint) -c;
 
     /* N = - ( C - N ) */
-    MPI_CHK( mpi_sub_abs( N, C, N ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_sub_abs( N, C, N ) );
     N->s = -1;
 
 cleanup:
@@ -1026,11 +1026,11 @@
     return( ret );
 }
 
-#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
+#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
 /*
  * Fast quasi-reduction modulo p224 (FIPS 186-3 D.2.2)
  */
-static int ecp_mod_p224( mpi *N )
+static int ecp_mod_p224( mbedtls_mpi *N )
 {
     INIT( 224 );
 
@@ -1045,13 +1045,13 @@
 cleanup:
     return( ret );
 }
-#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED */
+#endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
 
-#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
+#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
 /*
  * Fast quasi-reduction modulo p256 (FIPS 186-3 D.2.3)
  */
-static int ecp_mod_p256( mpi *N )
+static int ecp_mod_p256( mbedtls_mpi *N )
 {
     INIT( 256 );
 
@@ -1082,13 +1082,13 @@
 cleanup:
     return( ret );
 }
-#endif /* POLARSSL_ECP_DP_SECP256R1_ENABLED */
+#endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */
 
-#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
+#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
 /*
  * Fast quasi-reduction modulo p384 (FIPS 186-3 D.2.4)
  */
-static int ecp_mod_p384( mpi *N )
+static int ecp_mod_p384( mbedtls_mpi *N )
 {
     INIT( 384 );
 
@@ -1131,7 +1131,7 @@
 cleanup:
     return( ret );
 }
-#endif /* POLARSSL_ECP_DP_SECP384R1_ENABLED */
+#endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
 
 #undef A
 #undef LOAD32
@@ -1141,21 +1141,21 @@
 #undef NEXT
 #undef LAST
 
-#endif /* POLARSSL_ECP_DP_SECP224R1_ENABLED ||
-          POLARSSL_ECP_DP_SECP256R1_ENABLED ||
-          POLARSSL_ECP_DP_SECP384R1_ENABLED */
+#endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED ||
+          MBEDTLS_ECP_DP_SECP256R1_ENABLED ||
+          MBEDTLS_ECP_DP_SECP384R1_ENABLED */
 
-#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
+#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
 /*
  * Here we have an actual Mersenne prime, so things are more straightforward.
  * However, chunks are aligned on a 'weird' boundary (521 bits).
  */
 
-/* Size of p521 in terms of t_uint */
-#define P521_WIDTH      ( 521 / 8 / sizeof( t_uint ) + 1 )
+/* Size of p521 in terms of mbedtls_mpi_uint */
+#define P521_WIDTH      ( 521 / 8 / sizeof( mbedtls_mpi_uint ) + 1 )
 
-/* Bits to keep in the most significant t_uint */
-#if defined(POLARSSL_HAVE_INT8)
+/* Bits to keep in the most significant mbedtls_mpi_uint */
+#if defined(MBEDTLS_HAVE_INT8)
 #define P521_MASK       0x01
 #else
 #define P521_MASK       0x01FF
@@ -1165,13 +1165,13 @@
  * Fast quasi-reduction modulo p521 (FIPS 186-3 D.2.5)
  * Write N as A1 + 2^521 A0, return A0 + A1
  */
-static int ecp_mod_p521( mpi *N )
+static int ecp_mod_p521( mbedtls_mpi *N )
 {
     int ret;
     size_t i;
-    mpi M;
-    t_uint Mp[P521_WIDTH + 1];
-    /* Worst case for the size of M is when t_uint is 16 bits:
+    mbedtls_mpi M;
+    mbedtls_mpi_uint Mp[P521_WIDTH + 1];
+    /* Worst case for the size of M is when mbedtls_mpi_uint is 16 bits:
      * we need to hold bits 513 to 1056, which is 34 limbs, that is
      * P521_WIDTH + 1. Otherwise P521_WIDTH is enough. */
 
@@ -1184,8 +1184,8 @@
     if( M.n > P521_WIDTH + 1 )
         M.n = P521_WIDTH + 1;
     M.p = Mp;
-    memcpy( Mp, N->p + P521_WIDTH - 1, M.n * sizeof( t_uint ) );
-    MPI_CHK( mpi_shift_r( &M, 521 % ( 8 * sizeof( t_uint ) ) ) );
+    memcpy( Mp, N->p + P521_WIDTH - 1, M.n * sizeof( mbedtls_mpi_uint ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &M, 521 % ( 8 * sizeof( mbedtls_mpi_uint ) ) ) );
 
     /* N = A0 */
     N->p[P521_WIDTH - 1] &= P521_MASK;
@@ -1193,7 +1193,7 @@
         N->p[i] = 0;
 
     /* N = A0 + A1 */
-    MPI_CHK( mpi_add_abs( N, N, &M ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( N, N, &M ) );
 
 cleanup:
     return( ret );
@@ -1201,25 +1201,25 @@
 
 #undef P521_WIDTH
 #undef P521_MASK
-#endif /* POLARSSL_ECP_DP_SECP521R1_ENABLED */
+#endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
 
-#endif /* POLARSSL_ECP_NIST_OPTIM */
+#endif /* MBEDTLS_ECP_NIST_OPTIM */
 
-#if defined(POLARSSL_ECP_DP_M255_ENABLED)
+#if defined(MBEDTLS_ECP_DP_M255_ENABLED)
 
-/* Size of p255 in terms of t_uint */
-#define P255_WIDTH      ( 255 / 8 / sizeof( t_uint ) + 1 )
+/* Size of p255 in terms of mbedtls_mpi_uint */
+#define P255_WIDTH      ( 255 / 8 / sizeof( mbedtls_mpi_uint ) + 1 )
 
 /*
  * Fast quasi-reduction modulo p255 = 2^255 - 19
  * Write N as A0 + 2^255 A1, return A0 + 19 * A1
  */
-static int ecp_mod_p255( mpi *N )
+static int ecp_mod_p255( mbedtls_mpi *N )
 {
     int ret;
     size_t i;
-    mpi M;
-    t_uint Mp[P255_WIDTH + 2];
+    mbedtls_mpi M;
+    mbedtls_mpi_uint Mp[P255_WIDTH + 2];
 
     if( N->n < P255_WIDTH )
         return( 0 );
@@ -1231,27 +1231,27 @@
         M.n = P255_WIDTH + 1;
     M.p = Mp;
     memset( Mp, 0, sizeof Mp );
-    memcpy( Mp, N->p + P255_WIDTH - 1, M.n * sizeof( t_uint ) );
-    MPI_CHK( mpi_shift_r( &M, 255 % ( 8 * sizeof( t_uint ) ) ) );
+    memcpy( Mp, N->p + P255_WIDTH - 1, M.n * sizeof( mbedtls_mpi_uint ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &M, 255 % ( 8 * sizeof( mbedtls_mpi_uint ) ) ) );
     M.n++; /* Make room for multiplication by 19 */
 
     /* N = A0 */
-    MPI_CHK( mpi_set_bit( N, 255, 0 ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_set_bit( N, 255, 0 ) );
     for( i = P255_WIDTH; i < N->n; i++ )
         N->p[i] = 0;
 
     /* N = A0 + 19 * A1 */
-    MPI_CHK( mpi_mul_int( &M, &M, 19 ) );
-    MPI_CHK( mpi_add_abs( N, N, &M ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_mul_int( &M, &M, 19 ) );
+    MBEDTLS_MPI_CHK( mbedtls_mpi_add_abs( N, N, &M ) );
 
 cleanup:
     return( ret );
 }
-#endif /* POLARSSL_ECP_DP_M255_ENABLED */
+#endif /* MBEDTLS_ECP_DP_M255_ENABLED */
 
-#if defined(POLARSSL_ECP_DP_SECP192K1_ENABLED) ||   \
-    defined(POLARSSL_ECP_DP_SECP224K1_ENABLED) ||   \
-    defined(POLARSSL_ECP_DP_SECP256K1_ENABLED)
+#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) ||   \
+    defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) ||   \
+    defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
 /*
  * Fast quasi-reduction modulo P = 2^s - R,
  * with R about 33 bits, used by the Koblitz curves.
@@ -1259,15 +1259,15 @@
  * Write N as A0 + 2^224 A1, return A0 + R * A1.
  * Actually do two passes, since R is big.
  */
-#define P_KOBLITZ_MAX   ( 256 / 8 / sizeof( t_uint ) )  // Max limbs in P
-#define P_KOBLITZ_R     ( 8 / sizeof( t_uint ) )        // Limbs in R
-static inline int ecp_mod_koblitz( mpi *N, t_uint *Rp, size_t p_limbs,
-                                   size_t adjust, size_t shift, t_uint mask )
+#define P_KOBLITZ_MAX   ( 256 / 8 / sizeof( mbedtls_mpi_uint ) )  // Max limbs in P
+#define P_KOBLITZ_R     ( 8 / sizeof( mbedtls_mpi_uint ) )        // Limbs in R
+static inline int ecp_mod_koblitz( mbedtls_mpi *N, mbedtls_mpi_uint *Rp, size_t p_limbs,
+                                   size_t adjust, size_t shift, mbedtls_mpi_uint mask )
 {
     int ret;
     size_t i;