From 747a83a0f775361140592bb684ea2f30f97c40ad Mon Sep 17 00:00:00 2001 From: Paul Bakker Date: Sat, 1 Feb 2014 22:50:07 +0100 Subject: [PATCH 1/7] Platform abstraction layer for memory, printf and fprintf --- include/polarssl/config.h | 35 +++++++++++++++ include/polarssl/platform.h | 88 +++++++++++++++++++++++++++++++++++++ library/CMakeLists.txt | 1 + library/Makefile | 2 +- library/platform.c | 80 +++++++++++++++++++++++++++++++++ 5 files changed, 205 insertions(+), 1 deletion(-) create mode 100644 include/polarssl/platform.h create mode 100644 library/platform.c diff --git a/include/polarssl/config.h b/include/polarssl/config.h index dd172ce16..be1005710 100644 --- a/include/polarssl/config.h +++ b/include/polarssl/config.h @@ -113,6 +113,24 @@ * Comment if your system does not support the IPv6 socket interface */ #define POLARSSL_HAVE_IPV6 + +/** + * \def POLARSSL_PLATFORM_XXX_ALT + * + * Uncomment a macro to let PolarSSL support the function in the platform + * abstraction layer. + * + * Example: In case you uncomment POLARSSL_PLATFORM_PRINTF_ALT, PolarSSL will + * provide a function "platform_set_printf()" that allows you to set an + * alternative printf function pointer. + * + * All these define require POLARSSL_PLATFORM_C to be defined! + * + * Uncomment a macro to enable alternate implementation of specific base + * platform function + */ +//#define POLARSSL_PLATFORM_PRINTF_ALT +//#define POLARSSL_PLATFORM_FPRINTF_ALT /* \} name SECTION: System support */ /** @@ -1620,6 +1638,19 @@ */ #define POLARSSL_PKCS12_C +/** + * \def POLARSSL_PLATFORM_C + * + * Enable the platform abstraction layer that allows you to re-assign + * functions like malloc(), free(), printf(), fprintf() + * + * Module: library/platform.c + * Caller: Most other .c files + * + * This module enables abstraction of common (libc) functions. + */ +#define POLARSSL_PLATFORM_C + /** * \def POLARSSL_RIPEMD160_C * @@ -1969,6 +2000,10 @@ #define POLARSSL_MEMORY_STDMALLOC malloc /**< Default allocator to use, can be undefined */ #define POLARSSL_MEMORY_STDFREE free /**< Default free to use, can be undefined */ +// Platform options +#define POLARSSL_PLATFORM_STD_PRINTF printf /**< Default printf to use, can be undefined */ +#define POLARSSL_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use, can be undefined */ + // SSL Cache options // #define SSL_CACHE_DEFAULT_TIMEOUT 86400 /**< 1 day */ diff --git a/include/polarssl/platform.h b/include/polarssl/platform.h new file mode 100644 index 000000000..61b31190f --- /dev/null +++ b/include/polarssl/platform.h @@ -0,0 +1,88 @@ +/** + * \file platform.h + * + * \brief PolarSSL Platform abstraction layer + * + * Copyright (C) 2006-2014, Brainspark B.V. + * + * This file is part of PolarSSL (http://www.polarssl.org) + * Lead Maintainer: Paul Bakker + * + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ +#ifndef POLARSSL_PLATFORM_H +#define POLARSSL_PLATFORM_H + +#include "config.h" + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#if !defined(POLARSSL_CONFIG_OPTIONS) +#define POLARSSL_PLATFORM_STD_PRINTF printf /**< Default printf to use */ +#define POLARSSL_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use */ +#endif /* POLARSSL_CONFIG_OPTIONS */ + +/* + * The function pointers for malloc and free + */ +#if defined(POLARSSL_MEMORY_C) +#include "memory.h" +#else +#define polarssl_malloc malloc +#define polarssl_free free +#endif + +/* + * The function pointers for printf + */ +#if defined(POLARSSL_PLATFORM_PRINTF_ALT) +extern int (*polarssl_printf)( const char *format, ... ); + +/** + * \brief Set your own printf function pointer + * + * \param printf_func the printf function implementation + * + * \return 0 + */ +int platform_set_printf( int (*printf_func)( const char *, ... ) ); +#else +#define polarssl_printf printf +#endif + +/* + * The function pointers for fprintf + */ +#if defined(POLARSSL_PLATFORM_FPRINTF_ALT) +extern int (*polarssl_fprintf)( FILE *stream, const char *format, ... ); + +int platform_set_fprintf( int (*fprintf_func)( FILE *stream, const char *, + ... ) ); +#else +#define polarssl_fprintf fprintf +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* platform.h */ diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index 77aac2042..d948ca0c4 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -46,6 +46,7 @@ set(src pk_wrap.c pkparse.c pkwrite.c + platform.c ripemd160.c rsa.c sha1.c diff --git a/library/Makefile b/library/Makefile index 311e31637..931602426 100644 --- a/library/Makefile +++ b/library/Makefile @@ -52,7 +52,7 @@ OBJS= aes.o aesni.o arc4.o \ padlock.o pbkdf2.o pem.o \ pkcs5.o pkcs11.o pkcs12.o \ pk.o pk_wrap.o pkparse.o \ - pkwrite.o ripemd160.o \ + pkwrite.o platform.o ripemd160.o \ rsa.o sha1.o sha256.o \ sha512.o ssl_cache.o ssl_cli.o \ ssl_srv.o ssl_ciphersuites.o \ diff --git a/library/platform.c b/library/platform.c new file mode 100644 index 000000000..32f949ff0 --- /dev/null +++ b/library/platform.c @@ -0,0 +1,80 @@ +/* + * Platform abstraction layer + * + * Copyright (C) 2006-2014, Brainspark B.V. + * + * This file is part of PolarSSL (http://www.polarssl.org) + * Lead Maintainer: Paul Bakker + * + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "polarssl/config.h" + +#if defined(POLARSSL_PLATFORM_C) + +#include "polarssl/platform.h" + +#if defined(POLARSSL_PLATFORM_PRINTF_ALT) +#if !defined(POLARSSL_PLATFORM_STD_PRINTF) +/* + * Make dummy function to prevent NULL pointer dereferences + */ +static int platform_printf_uninit( const char *format, ... ) +{ + ((void) format); + return( 0 ); +} + +#define POLARSSL_PLATFORM_STD_PRINTF platform_printf_uninit +#endif /* !POLARSSL_PLATFORM_STD_PRINTF */ + +int (*polarssl_printf)( const char *, ... ) = POLARSSL_PLATFORM_STD_PRINTF; + +int platform_set_printf( int (*printf_func)( const char *, ... ) ) +{ + polarssl_printf = printf_func; + return( 0 ); +} +#endif /* POLARSSL_PLATFORM_PRINTF_ALT */ + +#if defined(POLARSSL_PLATFORM_FPRINTF_ALT) +#if !defined(POLARSSL_PLATFORM_STD_FPRINTF) +/* + * Make dummy function to prevent NULL pointer dereferences + */ +static int platform_fprintf_uninit( FILE *stream, const char *format, ... ) +{ + ((void) stream); + ((void) format); + return( 0 ); +} + +#define POLARSSL_PLATFORM_STD_fPRINTF platform_fprintf_uninit +#endif /* !POLARSSL_PLATFORM_STD_FPRINTF */ + +int (*polarssl_fprintf)( FILE *, const char *, ... ) = + POLARSSL_PLATFORM_STD_FPRINTF; + +int platform_set_fprintf( int (*fprintf_func)( FILE *, const char *, ... ) ) +{ + polarssl_fprintf = fprintf_func; + return( 0 ); +} +#endif /* POLARSSL_PLATFORM_FPRINTF_ALT */ + +#endif /* POLARSSL_PLATFORM_C */ From 7dc4c44267f83ab1a0148047534836d655ee931f Mon Sep 17 00:00:00 2001 From: Paul Bakker Date: Sat, 1 Feb 2014 22:50:26 +0100 Subject: [PATCH 2/7] Library files moved to use platform layer --- library/aes.c | 56 ++++++++++++++----------- library/arc4.c | 16 ++++--- library/asn1parse.c | 6 +-- library/asn1write.c | 6 +-- library/base64.c | 18 +++++--- library/bignum.c | 43 +++++++++---------- library/camellia.c | 40 ++++++++++-------- library/cipher.c | 11 +---- library/cipher_wrap.c | 6 +-- library/ctr_drbg.c | 28 ++++++++----- library/des.c | 32 ++++++++------ library/dhm.c | 13 +++--- library/ecp.c | 23 +++++----- library/gcm.c | 54 ++++++++++++++---------- library/hmac_drbg.c | 28 ++++++++----- library/md2.c | 16 ++++--- library/md4.c | 16 ++++--- library/md5.c | 24 +++++++---- library/md_wrap.c | 6 +-- library/memory_buffer_alloc.c | 79 +++++++++++++++++++++-------------- library/pem.c | 6 +-- library/pk_wrap.c | 6 +-- library/pkcs11.c | 9 ++-- library/pkcs5.c | 16 ++++--- library/pkparse.c | 4 +- library/pkwrite.c | 6 +-- library/ripemd160.c | 21 ++++++---- library/rsa.c | 32 ++++++++------ library/sha1.c | 24 +++++++---- library/sha256.c | 24 +++++++---- library/sha512.c | 24 +++++++---- library/ssl_cache.c | 6 +-- library/ssl_cli.c | 6 +-- library/ssl_srv.c | 6 +-- library/ssl_tls.c | 6 +-- library/x509.c | 21 +++++----- library/x509_crl.c | 6 +-- library/x509_crt.c | 6 +-- library/x509_csr.c | 6 +-- library/xtea.c | 14 +++++-- 40 files changed, 447 insertions(+), 323 deletions(-) diff --git a/library/aes.c b/library/aes.c index bc4b178ed..f516fba67 100644 --- a/library/aes.c +++ b/library/aes.c @@ -1,7 +1,7 @@ /* * FIPS-197 compliant AES implementation * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -41,6 +41,12 @@ #include "polarssl/aesni.h" #endif +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" +#else +#define polarssl_printf printf +#endif + #if !defined(POLARSSL_AES_ALT) /* @@ -1191,8 +1197,8 @@ int aes_self_test( int verbose ) v = i & 1; if( verbose != 0 ) - printf( " AES-ECB-%3d (%s): ", 128 + u * 64, - ( v == AES_DECRYPT ) ? "dec" : "enc" ); + polarssl_printf( " AES-ECB-%3d (%s): ", 128 + u * 64, + ( v == AES_DECRYPT ) ? "dec" : "enc" ); memset( buf, 0, 16 ); @@ -1206,7 +1212,7 @@ int aes_self_test( int verbose ) if( memcmp( buf, aes_test_ecb_dec[u], 16 ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } @@ -1221,18 +1227,18 @@ int aes_self_test( int verbose ) if( memcmp( buf, aes_test_ecb_enc[u], 16 ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } } if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); } if( verbose != 0 ) - printf( "\n" ); + polarssl_printf( "\n" ); #if defined(POLARSSL_CIPHER_MODE_CBC) /* @@ -1244,8 +1250,8 @@ int aes_self_test( int verbose ) v = i & 1; if( verbose != 0 ) - printf( " AES-CBC-%3d (%s): ", 128 + u * 64, - ( v == AES_DECRYPT ) ? "dec" : "enc" ); + polarssl_printf( " AES-CBC-%3d (%s): ", 128 + u * 64, + ( v == AES_DECRYPT ) ? "dec" : "enc" ); memset( iv , 0, 16 ); memset( prv, 0, 16 ); @@ -1261,7 +1267,7 @@ int aes_self_test( int verbose ) if( memcmp( buf, aes_test_cbc_dec[u], 16 ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } @@ -1284,18 +1290,18 @@ int aes_self_test( int verbose ) if( memcmp( prv, aes_test_cbc_enc[u], 16 ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } } if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); } if( verbose != 0 ) - printf( "\n" ); + polarssl_printf( "\n" ); #endif /* POLARSSL_CIPHER_MODE_CBC */ #if defined(POLARSSL_CIPHER_MODE_CFB) @@ -1308,8 +1314,8 @@ int aes_self_test( int verbose ) v = i & 1; if( verbose != 0 ) - printf( " AES-CFB128-%3d (%s): ", 128 + u * 64, - ( v == AES_DECRYPT ) ? "dec" : "enc" ); + polarssl_printf( " AES-CFB128-%3d (%s): ", 128 + u * 64, + ( v == AES_DECRYPT ) ? "dec" : "enc" ); memcpy( iv, aes_test_cfb128_iv, 16 ); memcpy( key, aes_test_cfb128_key[u], 16 + u * 8 ); @@ -1325,7 +1331,7 @@ int aes_self_test( int verbose ) if( memcmp( buf, aes_test_cfb128_pt, 64 ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } @@ -1338,18 +1344,18 @@ int aes_self_test( int verbose ) if( memcmp( buf, aes_test_cfb128_ct[u], 64 ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } } if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); } if( verbose != 0 ) - printf( "\n" ); + polarssl_printf( "\n" ); #endif /* POLARSSL_CIPHER_MODE_CFB */ #if defined(POLARSSL_CIPHER_MODE_CTR) @@ -1362,8 +1368,8 @@ int aes_self_test( int verbose ) v = i & 1; if( verbose != 0 ) - printf( " AES-CTR-128 (%s): ", - ( v == AES_DECRYPT ) ? "dec" : "enc" ); + polarssl_printf( " AES-CTR-128 (%s): ", + ( v == AES_DECRYPT ) ? "dec" : "enc" ); memcpy( nonce_counter, aes_test_ctr_nonce_counter[u], 16 ); memcpy( key, aes_test_ctr_key[u], 16 ); @@ -1381,7 +1387,7 @@ int aes_self_test( int verbose ) if( memcmp( buf, aes_test_ctr_pt[u], len ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } @@ -1396,18 +1402,18 @@ int aes_self_test( int verbose ) if( memcmp( buf, aes_test_ctr_ct[u], len ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } } if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); } if( verbose != 0 ) - printf( "\n" ); + polarssl_printf( "\n" ); #endif /* POLARSSL_CIPHER_MODE_CTR */ return( 0 ); diff --git a/library/arc4.c b/library/arc4.c index 85b78f5ba..536ea8ccc 100644 --- a/library/arc4.c +++ b/library/arc4.c @@ -1,7 +1,7 @@ /* * An implementation of the ARCFOUR algorithm * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -34,6 +34,12 @@ #include "polarssl/arc4.h" +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" +#else +#define polarssl_printf printf +#endif + #if !defined(POLARSSL_ARC4_ALT) /* @@ -143,7 +149,7 @@ int arc4_self_test( int verbose ) for( i = 0; i < 3; i++ ) { if( verbose != 0 ) - printf( " ARC4 test #%d: ", i + 1 ); + polarssl_printf( " ARC4 test #%d: ", i + 1 ); memcpy( ibuf, arc4_test_pt[i], 8 ); @@ -153,17 +159,17 @@ int arc4_self_test( int verbose ) if( memcmp( obuf, arc4_test_ct[i], 8 ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); } if( verbose != 0 ) - printf( "\n" ); + polarssl_printf( "\n" ); return( 0 ); } diff --git a/library/asn1parse.c b/library/asn1parse.c index 957359917..c9ce75acc 100644 --- a/library/asn1parse.c +++ b/library/asn1parse.c @@ -1,7 +1,7 @@ /* * Generic ASN.1 parsing * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -33,8 +33,8 @@ #include "polarssl/bignum.h" #endif -#if defined(POLARSSL_MEMORY_C) -#include "polarssl/memory.h" +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" #else #define polarssl_malloc malloc #define polarssl_free free diff --git a/library/asn1write.c b/library/asn1write.c index 32d1c736f..13f4a394d 100644 --- a/library/asn1write.c +++ b/library/asn1write.c @@ -1,7 +1,7 @@ /* * ASN.1 buffer writing functionality * - * Copyright (C) 2006-2012, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -29,8 +29,8 @@ #include "polarssl/asn1write.h" -#if defined(POLARSSL_MEMORY_C) -#include "polarssl/memory.h" +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" #else #include #define polarssl_malloc malloc diff --git a/library/base64.c b/library/base64.c index 3b4376dac..ee9c05c5d 100644 --- a/library/base64.c +++ b/library/base64.c @@ -1,7 +1,7 @@ /* * RFC 1521 base64 encoding/decoding * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -36,6 +36,12 @@ typedef UINT32 uint32_t; #include #endif +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" +#else +#define polarssl_printf printf +#endif + static const unsigned char base64_enc_map[64] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', @@ -222,7 +228,7 @@ int base64_self_test( int verbose ) unsigned char buffer[128]; if( verbose != 0 ) - printf( " Base64 encoding test: " ); + polarssl_printf( " Base64 encoding test: " ); len = sizeof( buffer ); src = base64_test_dec; @@ -231,13 +237,13 @@ int base64_self_test( int verbose ) memcmp( base64_test_enc, buffer, 88 ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } if( verbose != 0 ) - printf( "passed\n Base64 decoding test: " ); + polarssl_printf( "passed\n Base64 decoding test: " ); len = sizeof( buffer ); src = base64_test_enc; @@ -246,13 +252,13 @@ int base64_self_test( int verbose ) memcmp( base64_test_dec, buffer, 64 ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } if( verbose != 0 ) - printf( "passed\n\n" ); + polarssl_printf( "passed\n\n" ); return( 0 ); } diff --git a/library/bignum.c b/library/bignum.c index bbd901e83..cb55586ff 100644 --- a/library/bignum.c +++ b/library/bignum.c @@ -1,7 +1,7 @@ /* * Multi-precision integer library * - * Copyright (C) 2006-2010, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -37,9 +37,10 @@ #include "polarssl/bignum.h" #include "polarssl/bn_mul.h" -#if defined(POLARSSL_MEMORY_C) -#include "polarssl/memory.h" +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" #else +#define polarssl_printf printf #define polarssl_malloc malloc #define polarssl_free free #endif @@ -616,7 +617,7 @@ int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout ) return( POLARSSL_ERR_MPI_FILE_IO_ERROR ); } else - printf( "%s%s", p, s ); + polarssl_printf( "%s%s", p, s ); cleanup: @@ -2189,19 +2190,19 @@ int mpi_self_test( int verbose ) "30879B56C61DE584A0F53A2447A51E" ) ); if( verbose != 0 ) - printf( " MPI test #1 (mul_mpi): " ); + polarssl_printf( " MPI test #1 (mul_mpi): " ); if( mpi_cmp_mpi( &X, &U ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); ret = 1; goto cleanup; } if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); MPI_CHK( mpi_div_mpi( &X, &Y, &A, &N ) ); @@ -2214,20 +2215,20 @@ int mpi_self_test( int verbose ) "9EE50D0657C77F374E903CDFA4C642" ) ); if( verbose != 0 ) - printf( " MPI test #2 (div_mpi): " ); + polarssl_printf( " MPI test #2 (div_mpi): " ); if( mpi_cmp_mpi( &X, &U ) != 0 || mpi_cmp_mpi( &Y, &V ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); ret = 1; goto cleanup; } if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); MPI_CHK( mpi_exp_mod( &X, &A, &E, &N, NULL ) ); @@ -2237,19 +2238,19 @@ int mpi_self_test( int verbose ) "325D24D6A3C12710F10A09FA08AB87" ) ); if( verbose != 0 ) - printf( " MPI test #3 (exp_mod): " ); + polarssl_printf( " MPI test #3 (exp_mod): " ); if( mpi_cmp_mpi( &X, &U ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); ret = 1; goto cleanup; } if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); MPI_CHK( mpi_inv_mod( &X, &A, &N ) ); @@ -2259,22 +2260,22 @@ int mpi_self_test( int verbose ) "C5B8A74DAC4D09E03B5E0BE779F2DF61" ) ); if( verbose != 0 ) - printf( " MPI test #4 (inv_mod): " ); + polarssl_printf( " MPI test #4 (inv_mod): " ); if( mpi_cmp_mpi( &X, &U ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); ret = 1; goto cleanup; } if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); if( verbose != 0 ) - printf( " MPI test #5 (simple gcd): " ); + polarssl_printf( " MPI test #5 (simple gcd): " ); for ( i = 0; i < GCD_PAIR_COUNT; i++) { @@ -2286,7 +2287,7 @@ int mpi_self_test( int verbose ) if( mpi_cmp_int( &A, gcd_pairs[i][2] ) != 0 ) { if( verbose != 0 ) - printf( "failed at %d\n", i ); + polarssl_printf( "failed at %d\n", i ); ret = 1; goto cleanup; @@ -2294,18 +2295,18 @@ int mpi_self_test( int verbose ) } if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); cleanup: if( ret != 0 && verbose != 0 ) - printf( "Unexpected error, return code = %08X\n", ret ); + polarssl_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 ); if( verbose != 0 ) - printf( "\n" ); + polarssl_printf( "\n" ); return( ret ); } diff --git a/library/camellia.c b/library/camellia.c index 2366caed6..f007a46a3 100644 --- a/library/camellia.c +++ b/library/camellia.c @@ -1,7 +1,7 @@ /* * Camellia implementation * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -35,6 +35,12 @@ #include "polarssl/camellia.h" +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" +#else +#define polarssl_printf printf +#endif + #if !defined(POLARSSL_CAMELLIA_ALT) /* @@ -889,8 +895,8 @@ int camellia_self_test( int verbose ) v = j & 1; if( verbose != 0 ) - printf( " CAMELLIA-ECB-%3d (%s): ", 128 + u * 64, - (v == CAMELLIA_DECRYPT) ? "dec" : "enc"); + polarssl_printf( " CAMELLIA-ECB-%3d (%s): ", 128 + u * 64, + (v == CAMELLIA_DECRYPT) ? "dec" : "enc"); for (i = 0; i < CAMELLIA_TESTS_ECB; i++ ) { memcpy( key, camellia_test_ecb_key[u][i], 16 + 8 * u); @@ -910,18 +916,18 @@ int camellia_self_test( int verbose ) if( memcmp( buf, dst, 16 ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } } if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); } if( verbose != 0 ) - printf( "\n" ); + polarssl_printf( "\n" ); #if defined(POLARSSL_CIPHER_MODE_CBC) /* @@ -933,8 +939,8 @@ int camellia_self_test( int verbose ) v = j & 1; if( verbose != 0 ) - printf( " CAMELLIA-CBC-%3d (%s): ", 128 + u * 64, - ( v == CAMELLIA_DECRYPT ) ? "dec" : "enc" ); + polarssl_printf( " CAMELLIA-CBC-%3d (%s): ", 128 + u * 64, + ( v == CAMELLIA_DECRYPT ) ? "dec" : "enc" ); memcpy( src, camellia_test_cbc_iv, 16); memcpy( dst, camellia_test_cbc_iv, 16); @@ -963,19 +969,19 @@ int camellia_self_test( int verbose ) if( memcmp( buf, dst, 16 ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } } if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); } #endif /* POLARSSL_CIPHER_MODE_CBC */ if( verbose != 0 ) - printf( "\n" ); + polarssl_printf( "\n" ); #if defined(POLARSSL_CIPHER_MODE_CTR) /* @@ -987,8 +993,8 @@ int camellia_self_test( int verbose ) v = i & 1; if( verbose != 0 ) - printf( " CAMELLIA-CTR-128 (%s): ", - ( v == CAMELLIA_DECRYPT ) ? "dec" : "enc" ); + polarssl_printf( " CAMELLIA-CTR-128 (%s): ", + ( v == CAMELLIA_DECRYPT ) ? "dec" : "enc" ); memcpy( nonce_counter, camellia_test_ctr_nonce_counter[u], 16 ); memcpy( key, camellia_test_ctr_key[u], 16 ); @@ -1006,7 +1012,7 @@ int camellia_self_test( int verbose ) if( memcmp( buf, camellia_test_ctr_pt[u], len ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } @@ -1021,18 +1027,18 @@ int camellia_self_test( int verbose ) if( memcmp( buf, camellia_test_ctr_ct[u], len ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } } if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); } if( verbose != 0 ) - printf( "\n" ); + polarssl_printf( "\n" ); #endif /* POLARSSL_CIPHER_MODE_CTR */ return ( 0 ); diff --git a/library/cipher.c b/library/cipher.c index bfb229e2f..a103c263f 100644 --- a/library/cipher.c +++ b/library/cipher.c @@ -1,11 +1,11 @@ /** * \file cipher.c - * + * * \brief Generic cipher wrapper for PolarSSL * * \author Adriaan de Jong * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -769,16 +769,9 @@ int cipher_check_tag( cipher_context_t *ctx, #if defined(POLARSSL_SELF_TEST) -#include - -#define ASSERT(x) if (!(x)) { \ - printf( "failed with %i at %s\n", value, (#x) ); \ - return( 1 ); \ -} /* * Checkup routine */ - int cipher_self_test( int verbose ) { ((void) verbose); diff --git a/library/cipher_wrap.c b/library/cipher_wrap.c index 3020e14e2..23065c4ba 100644 --- a/library/cipher_wrap.c +++ b/library/cipher_wrap.c @@ -5,7 +5,7 @@ * * \author Adriaan de Jong * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -57,8 +57,8 @@ #include "polarssl/gcm.h" #endif -#if defined(POLARSSL_MEMORY_C) -#include "polarssl/memory.h" +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" #else #define polarssl_malloc malloc #define polarssl_free free diff --git a/library/ctr_drbg.c b/library/ctr_drbg.c index d2b43137c..3be65e102 100644 --- a/library/ctr_drbg.c +++ b/library/ctr_drbg.c @@ -1,7 +1,7 @@ /* * CTR_DRBG implementation based on AES-256 (NIST SP 800-90) * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -38,6 +38,12 @@ #include #endif +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" +#else +#define polarssl_printf printf +#endif + /* * Non-public function wrapped by ctr_crbg_init(). Necessary to allow NIST * tests to succeed (which require known length fixed entropy) @@ -460,11 +466,11 @@ static int ctr_drbg_self_test_entropy( void *data, unsigned char *buf, return( 0 ); } -#define CHK( c ) if( (c) != 0 ) \ - { \ - if( verbose != 0 ) \ - printf( "failed\n" ); \ - return( 1 ); \ +#define CHK( c ) if( (c) != 0 ) \ + { \ + if( verbose != 0 ) \ + polarssl_printf( "failed\n" ); \ + return( 1 ); \ } /* @@ -479,7 +485,7 @@ int ctr_drbg_self_test( int verbose ) * Based on a NIST CTR_DRBG test vector (PR = True) */ if( verbose != 0 ) - printf( " CTR_DRBG (PR = TRUE) : " ); + polarssl_printf( " CTR_DRBG (PR = TRUE) : " ); test_offset = 0; CHK( ctr_drbg_init_entropy_len( &ctx, ctr_drbg_self_test_entropy, @@ -490,13 +496,13 @@ int ctr_drbg_self_test( int verbose ) CHK( memcmp( buf, result_pr, CTR_DRBG_BLOCKSIZE ) ); if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); /* * Based on a NIST CTR_DRBG test vector (PR = FALSE) */ if( verbose != 0 ) - printf( " CTR_DRBG (PR = FALSE): " ); + polarssl_printf( " CTR_DRBG (PR = FALSE): " ); test_offset = 0; CHK( ctr_drbg_init_entropy_len( &ctx, ctr_drbg_self_test_entropy, @@ -507,10 +513,10 @@ int ctr_drbg_self_test( int verbose ) CHK( memcmp( buf, result_nopr, 16 ) ); if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); if( verbose != 0 ) - printf( "\n" ); + polarssl_printf( "\n" ); return( 0 ); } diff --git a/library/des.c b/library/des.c index 153810d72..6e1523408 100644 --- a/library/des.c +++ b/library/des.c @@ -1,7 +1,7 @@ /* * FIPS-46-3 compliant Triple-DES implementation * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -35,6 +35,12 @@ #include "polarssl/des.h" +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" +#else +#define polarssl_printf printf +#endif + #if !defined(POLARSSL_DES_ALT) /* @@ -839,9 +845,9 @@ int des_self_test( int verbose ) v = i & 1; if( verbose != 0 ) - printf( " DES%c-ECB-%3d (%s): ", - ( u == 0 ) ? ' ' : '3', 56 + u * 56, - ( v == DES_DECRYPT ) ? "dec" : "enc" ); + polarssl_printf( " DES%c-ECB-%3d (%s): ", + ( u == 0 ) ? ' ' : '3', 56 + u * 56, + ( v == DES_DECRYPT ) ? "dec" : "enc" ); memcpy( buf, des3_test_buf, 8 ); @@ -889,17 +895,17 @@ int des_self_test( int verbose ) memcmp( buf, des3_test_ecb_enc[u], 8 ) != 0 ) ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); } if( verbose != 0 ) - printf( "\n" ); + polarssl_printf( "\n" ); #if defined(POLARSSL_CIPHER_MODE_CBC) /* @@ -911,9 +917,9 @@ int des_self_test( int verbose ) v = i & 1; if( verbose != 0 ) - printf( " DES%c-CBC-%3d (%s): ", - ( u == 0 ) ? ' ' : '3', 56 + u * 56, - ( v == DES_DECRYPT ) ? "dec" : "enc" ); + polarssl_printf( " DES%c-CBC-%3d (%s): ", + ( u == 0 ) ? ' ' : '3', 56 + u * 56, + ( v == DES_DECRYPT ) ? "dec" : "enc" ); memcpy( iv, des3_test_iv, 8 ); memcpy( prv, des3_test_iv, 8 ); @@ -984,18 +990,18 @@ int des_self_test( int verbose ) memcmp( buf, des3_test_cbc_enc[u], 8 ) != 0 ) ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); } #endif /* POLARSSL_CIPHER_MODE_CBC */ if( verbose != 0 ) - printf( "\n" ); + polarssl_printf( "\n" ); return( 0 ); } diff --git a/library/dhm.c b/library/dhm.c index e8aa8191c..9d00d9bbc 100644 --- a/library/dhm.c +++ b/library/dhm.c @@ -1,7 +1,7 @@ /* * Diffie-Hellman-Merkle key exchange * - * Copyright (C) 2006-2010, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -42,10 +42,11 @@ #include "polarssl/asn1.h" #endif -#if defined(POLARSSL_MEMORY_C) -#include "polarssl/memory.h" +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" #else #include +#define polarssl_printf printf #define polarssl_malloc malloc #define polarssl_free free #endif @@ -548,19 +549,19 @@ int dhm_self_test( int verbose ) dhm_context dhm; if( verbose != 0 ) - printf( " DHM parameter load: " ); + polarssl_printf( " DHM parameter load: " ); if( ( ret = dhm_parse_dhm( &dhm, (const unsigned char *) test_dhm_params, strlen( test_dhm_params ) ) ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( ret ); } if( verbose != 0 ) - printf( "passed\n\n" ); + polarssl_printf( "passed\n\n" ); dhm_free( &dhm ); diff --git a/library/ecp.c b/library/ecp.c index ad6e5f586..2577fa745 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -1,7 +1,7 @@ /* * Elliptic curves over GF(p): generic functions * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -50,9 +50,10 @@ #include "polarssl/ecp.h" -#if defined(POLARSSL_MEMORY_C) -#include "polarssl/memory.h" +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" #else +#define polarssl_printf printf #define polarssl_malloc malloc #define polarssl_free free #endif @@ -1904,7 +1905,7 @@ int ecp_self_test( int verbose ) #endif if( verbose != 0 ) - printf( " ECP test #1 (constant op_count, base point G): " ); + polarssl_printf( " ECP test #1 (constant op_count, base point G): " ); /* Do a dummy multiplication first to trigger precomputation */ MPI_CHK( mpi_lset( &m, 2 ) ); @@ -1933,7 +1934,7 @@ int ecp_self_test( int verbose ) mul_count != mul_c_prev ) { if( verbose != 0 ) - printf( "failed (%u)\n", (unsigned int) i ); + polarssl_printf( "failed (%u)\n", (unsigned int) i ); ret = 1; goto cleanup; @@ -1941,10 +1942,10 @@ int ecp_self_test( int verbose ) } if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); if( verbose != 0 ) - printf( " ECP test #2 (constant op_count, other point): " ); + polarssl_printf( " ECP test #2 (constant op_count, other point): " ); /* We computed P = 2G last time, use it */ add_count = 0; @@ -1970,7 +1971,7 @@ int ecp_self_test( int verbose ) mul_count != mul_c_prev ) { if( verbose != 0 ) - printf( "failed (%u)\n", (unsigned int) i ); + polarssl_printf( "failed (%u)\n", (unsigned int) i ); ret = 1; goto cleanup; @@ -1978,12 +1979,12 @@ int ecp_self_test( int verbose ) } if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); cleanup: if( ret < 0 && verbose != 0 ) - printf( "Unexpected error, return code = %08X\n", ret ); + polarssl_printf( "Unexpected error, return code = %08X\n", ret ); ecp_group_free( &grp ); ecp_point_free( &R ); @@ -1991,7 +1992,7 @@ cleanup: mpi_free( &m ); if( verbose != 0 ) - printf( "\n" ); + polarssl_printf( "\n" ); return( ret ); } diff --git a/library/gcm.c b/library/gcm.c index 3e1bc77d6..1c6cf9f93 100644 --- a/library/gcm.c +++ b/library/gcm.c @@ -1,7 +1,7 @@ /* * NIST SP800-38D compliant GCM implementation * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -43,6 +43,12 @@ #include "polarssl/aesni.h" #endif +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" +#else +#define polarssl_printf printf +#endif + /* * 32-bit integer manipulation macros (big endian) */ @@ -716,7 +722,8 @@ int gcm_self_test( int verbose ) for( i = 0; i < MAX_TESTS; i++ ) { if( verbose != 0 ) - printf( " AES-GCM-%3d #%d (%s): ", key_len, i, "enc" ); + polarssl_printf( " AES-GCM-%3d #%d (%s): ", + key_len, i, "enc" ); gcm_init( &ctx, cipher, key[key_index[i]], key_len ); @@ -731,7 +738,7 @@ int gcm_self_test( int verbose ) memcmp( tag_buf, tag[j * 6 + i], 16 ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } @@ -739,10 +746,11 @@ int gcm_self_test( int verbose ) gcm_free( &ctx ); if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); if( verbose != 0 ) - printf( " AES-GCM-%3d #%d (%s): ", key_len, i, "dec" ); + polarssl_printf( " AES-GCM-%3d #%d (%s): ", + key_len, i, "dec" ); gcm_init( &ctx, cipher, key[key_index[i]], key_len ); @@ -757,7 +765,7 @@ int gcm_self_test( int verbose ) memcmp( tag_buf, tag[j * 6 + i], 16 ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } @@ -765,10 +773,11 @@ int gcm_self_test( int verbose ) gcm_free( &ctx ); if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); if( verbose != 0 ) - printf( " AES-GCM-%3d #%d split (%s): ", key_len, i, "enc" ); + polarssl_printf( " AES-GCM-%3d #%d split (%s): ", + key_len, i, "enc" ); gcm_init( &ctx, cipher, key[key_index[i]], key_len ); @@ -778,7 +787,7 @@ int gcm_self_test( int verbose ) if( ret != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } @@ -790,7 +799,7 @@ int gcm_self_test( int verbose ) if( ret != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } @@ -799,7 +808,7 @@ int gcm_self_test( int verbose ) if( ret != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } @@ -810,7 +819,7 @@ int gcm_self_test( int verbose ) if( ret != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } @@ -822,7 +831,7 @@ int gcm_self_test( int verbose ) memcmp( tag_buf, tag[j * 6 + i], 16 ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } @@ -830,10 +839,11 @@ int gcm_self_test( int verbose ) gcm_free( &ctx ); if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); if( verbose != 0 ) - printf( " AES-GCM-%3d #%d split (%s): ", key_len, i, "dec" ); + polarssl_printf( " AES-GCM-%3d #%d split (%s): ", + key_len, i, "dec" ); gcm_init( &ctx, cipher, key[key_index[i]], key_len ); @@ -843,7 +853,7 @@ int gcm_self_test( int verbose ) if( ret != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } @@ -855,7 +865,7 @@ int gcm_self_test( int verbose ) if( ret != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } @@ -864,7 +874,7 @@ int gcm_self_test( int verbose ) if( ret != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } @@ -875,7 +885,7 @@ int gcm_self_test( int verbose ) if( ret != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } @@ -887,7 +897,7 @@ int gcm_self_test( int verbose ) memcmp( tag_buf, tag[j * 6 + i], 16 ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } @@ -895,13 +905,13 @@ int gcm_self_test( int verbose ) gcm_free( &ctx ); if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); } } if( verbose != 0 ) - printf( "\n" ); + polarssl_printf( "\n" ); return( 0 ); } diff --git a/library/hmac_drbg.c b/library/hmac_drbg.c index 33a20ef9c..7a210925e 100644 --- a/library/hmac_drbg.c +++ b/library/hmac_drbg.c @@ -39,6 +39,12 @@ #include #endif +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" +#else +#define polarssl_printf printf +#endif + /* * HMAC_DRBG update, using optional additional data (10.1.2.2) */ @@ -368,7 +374,7 @@ int hmac_drbg_self_test( int verbose ) { if( verbose != 0 ) - printf( "\n" ); + polarssl_printf( "\n" ); return( 0 ); } @@ -418,11 +424,11 @@ static int hmac_drbg_self_test_entropy( void *data, return( 0 ); } -#define CHK( c ) if( (c) != 0 ) \ - { \ - if( verbose != 0 ) \ - printf( "failed\n" ); \ - return( 1 ); \ +#define CHK( c ) if( (c) != 0 ) \ + { \ + if( verbose != 0 ) \ + polarssl_printf( "failed\n" ); \ + return( 1 ); \ } /* @@ -438,7 +444,7 @@ int hmac_drbg_self_test( int verbose ) * PR = True */ if( verbose != 0 ) - printf( " HMAC_DRBG (PR = True) : " ); + polarssl_printf( " HMAC_DRBG (PR = True) : " ); test_offset = 0; CHK( hmac_drbg_init( &ctx, md_info, @@ -451,13 +457,13 @@ int hmac_drbg_self_test( int verbose ) hmac_drbg_free( &ctx ); if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); /* * PR = False */ if( verbose != 0 ) - printf( " HMAC_DRBG (PR = False) : " ); + polarssl_printf( " HMAC_DRBG (PR = False) : " ); test_offset = 0; CHK( hmac_drbg_init( &ctx, md_info, @@ -470,10 +476,10 @@ int hmac_drbg_self_test( int verbose ) hmac_drbg_free( &ctx ); if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); if( verbose != 0 ) - printf( "\n" ); + polarssl_printf( "\n" ); return( 0 ); } diff --git a/library/md2.c b/library/md2.c index 93e77d2f6..f29877f90 100644 --- a/library/md2.c +++ b/library/md2.c @@ -1,7 +1,7 @@ /* * RFC 1115/1319 compliant MD2 implementation * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -39,6 +39,12 @@ #include #endif +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" +#else +#define polarssl_printf printf +#endif + #if !defined(POLARSSL_MD2_ALT) static const unsigned char PI_SUBST[256] = @@ -340,7 +346,7 @@ int md2_self_test( int verbose ) for( i = 0; i < 7; i++ ) { if( verbose != 0 ) - printf( " MD2 test #%d: ", i + 1 ); + polarssl_printf( " MD2 test #%d: ", i + 1 ); md2( (unsigned char *) md2_test_str[i], strlen( md2_test_str[i] ), md2sum ); @@ -348,17 +354,17 @@ int md2_self_test( int verbose ) if( memcmp( md2sum, md2_test_sum[i], 16 ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); } if( verbose != 0 ) - printf( "\n" ); + polarssl_printf( "\n" ); return( 0 ); } diff --git a/library/md4.c b/library/md4.c index e14c83dc2..8ac6c0181 100644 --- a/library/md4.c +++ b/library/md4.c @@ -1,7 +1,7 @@ /* * RFC 1186/1320 compliant MD4 implementation * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -39,6 +39,12 @@ #include #endif +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" +#else +#define polarssl_printf printf +#endif + #if !defined(POLARSSL_MD4_ALT) /* @@ -436,7 +442,7 @@ int md4_self_test( int verbose ) for( i = 0; i < 7; i++ ) { if( verbose != 0 ) - printf( " MD4 test #%d: ", i + 1 ); + polarssl_printf( " MD4 test #%d: ", i + 1 ); md4( (unsigned char *) md4_test_str[i], strlen( md4_test_str[i] ), md4sum ); @@ -444,17 +450,17 @@ int md4_self_test( int verbose ) if( memcmp( md4sum, md4_test_sum[i], 16 ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); } if( verbose != 0 ) - printf( "\n" ); + polarssl_printf( "\n" ); return( 0 ); } diff --git a/library/md5.c b/library/md5.c index b28461e9b..c596e43b2 100644 --- a/library/md5.c +++ b/library/md5.c @@ -1,7 +1,7 @@ /* * RFC 1321 compliant MD5 implementation * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -38,6 +38,12 @@ #include #endif +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" +#else +#define polarssl_printf printf +#endif + #if !defined(POLARSSL_MD5_ALT) /* @@ -522,29 +528,29 @@ int md5_self_test( int verbose ) for( i = 0; i < 7; i++ ) { if( verbose != 0 ) - printf( " MD5 test #%d: ", i + 1 ); + polarssl_printf( " MD5 test #%d: ", i + 1 ); md5( md5_test_buf[i], md5_test_buflen[i], md5sum ); if( memcmp( md5sum, md5_test_sum[i], 16 ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); } if( verbose != 0 ) - printf( "\n" ); + polarssl_printf( "\n" ); for( i = 0; i < 7; i++ ) { if( verbose != 0 ) - printf( " HMAC-MD5 test #%d: ", i + 1 ); + polarssl_printf( " HMAC-MD5 test #%d: ", i + 1 ); if( i == 5 || i == 6 ) { @@ -565,17 +571,17 @@ int md5_self_test( int verbose ) if( memcmp( md5sum, md5_hmac_test_sum[i], buflen ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); } if( verbose != 0 ) - printf( "\n" ); + polarssl_printf( "\n" ); return( 0 ); } diff --git a/library/md_wrap.c b/library/md_wrap.c index bca5ab2b6..a6c1bacbb 100644 --- a/library/md_wrap.c +++ b/library/md_wrap.c @@ -5,7 +5,7 @@ * * \author Adriaan de Jong * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -61,8 +61,8 @@ #include "polarssl/sha512.h" #endif -#if defined(POLARSSL_MEMORY_C) -#include "polarssl/memory.h" +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" #else #define polarssl_malloc malloc #define polarssl_free free diff --git a/library/memory_buffer_alloc.c b/library/memory_buffer_alloc.c index 1038c85a9..fef43312f 100644 --- a/library/memory_buffer_alloc.c +++ b/library/memory_buffer_alloc.c @@ -1,7 +1,7 @@ /* * Buffer-based memory allocator * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -42,6 +42,12 @@ #include "polarssl/threading.h" #endif +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" +#else +#define polarssl_fprintf fprintf +#endif + #define MAGIC1 0xFF00AA55 #define MAGIC2 0xEE119966 #define MAX_BT 20 @@ -94,17 +100,18 @@ static void debug_header( memory_header *hdr ) size_t i; #endif - fprintf( stderr, "HDR: PTR(%10u), PREV(%10u), NEXT(%10u), ALLOC(%u), SIZE(%10u)\n", - (size_t) hdr, (size_t) hdr->prev, (size_t) hdr->next, - hdr->alloc, hdr->size ); - fprintf( stderr, " FPREV(%10u), FNEXT(%10u)\n", - (size_t) hdr->prev_free, (size_t) hdr->next_free ); + polarssl_fprintf( stderr, "HDR: PTR(%10u), PREV(%10u), NEXT(%10u), " + "ALLOC(%u), SIZE(%10u)\n", + (size_t) hdr, (size_t) hdr->prev, (size_t) hdr->next, + hdr->alloc, hdr->size ); + polarssl_fprintf( stderr, " FPREV(%10u), FNEXT(%10u)\n", + (size_t) hdr->prev_free, (size_t) hdr->next_free ); #if defined(POLARSSL_MEMORY_BACKTRACE) - fprintf( stderr, "TRACE: \n" ); + polarssl_fprintf( stderr, "TRACE: \n" ); for( i = 0; i < hdr->trace_count; i++ ) - fprintf( stderr, "%s\n", hdr->trace[i] ); - fprintf( stderr, "\n" ); + polarssl_fprintf( stderr, "%s\n", hdr->trace[i] ); + polarssl_fprintf( stderr, "\n" ); #endif } @@ -112,14 +119,14 @@ static void debug_chain() { memory_header *cur = heap.first; - fprintf( stderr, "\nBlock list\n" ); + polarssl_fprintf( stderr, "\nBlock list\n" ); while( cur != NULL ) { debug_header( cur ); cur = cur->next; } - fprintf( stderr, "Free list\n" ); + polarssl_fprintf( stderr, "Free list\n" ); cur = heap.first_free; while( cur != NULL ) @@ -135,7 +142,7 @@ static int verify_header( memory_header *hdr ) if( hdr->magic1 != MAGIC1 ) { #if defined(POLARSSL_MEMORY_DEBUG) - fprintf( stderr, "FATAL: MAGIC1 mismatch\n" ); + polarssl_fprintf( stderr, "FATAL: MAGIC1 mismatch\n" ); #endif return( 1 ); } @@ -143,7 +150,7 @@ static int verify_header( memory_header *hdr ) if( hdr->magic2 != MAGIC2 ) { #if defined(POLARSSL_MEMORY_DEBUG) - fprintf( stderr, "FATAL: MAGIC2 mismatch\n" ); + polarssl_fprintf( stderr, "FATAL: MAGIC2 mismatch\n" ); #endif return( 1 ); } @@ -151,7 +158,7 @@ static int verify_header( memory_header *hdr ) if( hdr->alloc > 1 ) { #if defined(POLARSSL_MEMORY_DEBUG) - fprintf( stderr, "FATAL: alloc has illegal value\n" ); + polarssl_fprintf( stderr, "FATAL: alloc has illegal value\n" ); #endif return( 1 ); } @@ -159,7 +166,7 @@ static int verify_header( memory_header *hdr ) if( hdr->prev != NULL && hdr->prev == hdr->next ) { #if defined(POLARSSL_MEMORY_DEBUG) - fprintf( stderr, "FATAL: prev == next\n" ); + polarssl_fprintf( stderr, "FATAL: prev == next\n" ); #endif return( 1 ); } @@ -167,7 +174,7 @@ static int verify_header( memory_header *hdr ) if( hdr->prev_free != NULL && hdr->prev_free == hdr->next_free ) { #if defined(POLARSSL_MEMORY_DEBUG) - fprintf( stderr, "FATAL: prev_free == next_free\n" ); + polarssl_fprintf( stderr, "FATAL: prev_free == next_free\n" ); #endif return( 1 ); } @@ -182,7 +189,8 @@ static int verify_chain() if( verify_header( heap.first ) != 0 ) { #if defined(POLARSSL_MEMORY_DEBUG) - fprintf( stderr, "FATAL: verification of first header failed\n" ); + polarssl_fprintf( stderr, "FATAL: verification of first header " + "failed\n" ); #endif return( 1 ); } @@ -190,7 +198,8 @@ static int verify_chain() if( heap.first->prev != NULL ) { #if defined(POLARSSL_MEMORY_DEBUG) - fprintf( stderr, "FATAL: verification failed: first->prev != NULL\n" ); + polarssl_fprintf( stderr, "FATAL: verification failed: " + "first->prev != NULL\n" ); #endif return( 1 ); } @@ -200,7 +209,8 @@ static int verify_chain() if( verify_header( cur ) != 0 ) { #if defined(POLARSSL_MEMORY_DEBUG) - fprintf( stderr, "FATAL: verification of header failed\n" ); + polarssl_fprintf( stderr, "FATAL: verification of header " + "failed\n" ); #endif return( 1 ); } @@ -208,7 +218,8 @@ static int verify_chain() if( cur->prev != prv ) { #if defined(POLARSSL_MEMORY_DEBUG) - fprintf( stderr, "FATAL: verification failed: cur->prev != prv\n" ); + polarssl_fprintf( stderr, "FATAL: verification failed: " + "cur->prev != prv\n" ); #endif return( 1 ); } @@ -254,7 +265,8 @@ static void *buffer_alloc_malloc( size_t len ) if( cur->alloc != 0 ) { #if defined(POLARSSL_MEMORY_DEBUG) - fprintf( stderr, "FATAL: block in free_list but allocated data\n" ); + polarssl_fprintf( stderr, "FATAL: block in free_list but allocated " + "data\n" ); #endif exit( 1 ); } @@ -365,7 +377,8 @@ static void buffer_alloc_free( void *ptr ) if( p < heap.buf || p > heap.buf + heap.len ) { #if defined(POLARSSL_MEMORY_DEBUG) - fprintf( stderr, "FATAL: polarssl_free() outside of managed space\n" ); + polarssl_fprintf( stderr, "FATAL: polarssl_free() outside of managed " + "space\n" ); #endif exit( 1 ); } @@ -379,7 +392,8 @@ static void buffer_alloc_free( void *ptr ) if( hdr->alloc != 1 ) { #if defined(POLARSSL_MEMORY_DEBUG) - fprintf( stderr, "FATAL: polarssl_free() on unallocated data\n" ); + polarssl_fprintf( stderr, "FATAL: polarssl_free() on unallocated " + "data\n" ); #endif exit( 1 ); } @@ -486,19 +500,20 @@ int memory_buffer_alloc_verify() #if defined(POLARSSL_MEMORY_DEBUG) void memory_buffer_alloc_status() { - fprintf( stderr, - "Current use: %u blocks / %u bytes, max: %u blocks / %u bytes (total %u bytes), malloc / free: %u / %u\n", - heap.header_count, heap.total_used, - heap.maximum_header_count, heap.maximum_used, - heap.maximum_header_count * sizeof( memory_header ) - + heap.maximum_used, - heap.malloc_count, heap.free_count ); + polarssl_fprintf( stderr, + "Current use: %u blocks / %u bytes, max: %u blocks / " + "%u bytes (total %u bytes), malloc / free: %u / %u\n", + heap.header_count, heap.total_used, + heap.maximum_header_count, heap.maximum_used, + heap.maximum_header_count * sizeof( memory_header ) + + heap.maximum_used, + heap.malloc_count, heap.free_count ); if( heap.first->next == NULL ) - fprintf( stderr, "All memory de-allocated in stack buffer\n" ); + polarssl_fprintf( stderr, "All memory de-allocated in stack buffer\n" ); else { - fprintf( stderr, "Memory currently allocated:\n" ); + polarssl_fprintf( stderr, "Memory currently allocated:\n" ); debug_chain(); } } diff --git a/library/pem.c b/library/pem.c index d602d8aa2..2c9d10d25 100644 --- a/library/pem.c +++ b/library/pem.c @@ -1,7 +1,7 @@ /* * Privacy Enhanced Mail (PEM) decoding * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -33,8 +33,8 @@ #include "polarssl/md5.h" #include "polarssl/cipher.h" -#if defined(POLARSSL_MEMORY_C) -#include "polarssl/memory.h" +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" #else #define polarssl_malloc malloc #define polarssl_free free diff --git a/library/pk_wrap.c b/library/pk_wrap.c index 3f05edd97..99535d613 100644 --- a/library/pk_wrap.c +++ b/library/pk_wrap.c @@ -1,7 +1,7 @@ /* * Public Key abstraction layer: wrapper functions * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -40,8 +40,8 @@ #include "polarssl/ecdsa.h" #endif -#if defined(POLARSSL_MEMORY_C) -#include "polarssl/memory.h" +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" #else #include #define polarssl_malloc malloc diff --git a/library/pkcs11.c b/library/pkcs11.c index 8a99f2871..d758460e3 100644 --- a/library/pkcs11.c +++ b/library/pkcs11.c @@ -5,7 +5,7 @@ * * \author Adriaan de Jong * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -34,15 +34,14 @@ #include "polarssl/oid.h" #include "polarssl/x509_crt.h" -#if defined(POLARSSL_MEMORY_C) -#include "polarssl/memory.h" +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" #else +#include #define polarssl_malloc malloc #define polarssl_free free #endif -#include - int pkcs11_x509_cert_init( x509_crt *cert, pkcs11h_certificate_t pkcs11_cert ) { int ret = 1; diff --git a/library/pkcs5.c b/library/pkcs5.c index 39aa5b96e..c4cdf0354 100644 --- a/library/pkcs5.c +++ b/library/pkcs5.c @@ -5,7 +5,7 @@ * * \author Mathias Olsson * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -42,6 +42,12 @@ #include "polarssl/cipher.h" #include "polarssl/oid.h" +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" +#else +#define polarssl_printf printf +#endif + static int pkcs5_parse_pbkdf2_params( asn1_buf *params, asn1_buf *salt, int *iterations, int *keylen, md_type_t *md_type ) @@ -357,7 +363,7 @@ int pkcs5_self_test( int verbose ) for( i = 0; i < MAX_TESTS; i++ ) { - printf( " PBKDF2 (SHA1) #%d: ", i ); + polarssl_printf( " PBKDF2 (SHA1) #%d: ", i ); ret = pkcs5_pbkdf2_hmac( &sha1_ctx, password[i], plen[i], salt[i], slen[i], it_cnt[i], key_len[i], key ); @@ -365,16 +371,16 @@ int pkcs5_self_test( int verbose ) memcmp( result_key[i], key, key_len[i] ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); } - printf( "\n" ); + polarssl_printf( "\n" ); if( ( ret = md_free_ctx( &sha1_ctx ) ) != 0 ) return( 1 ); diff --git a/library/pkparse.c b/library/pkparse.c index a5de20b9f..dcb0f3f91 100644 --- a/library/pkparse.c +++ b/library/pkparse.c @@ -50,8 +50,8 @@ #include "polarssl/pkcs12.h" #endif -#if defined(POLARSSL_MEMORY_C) -#include "polarssl/memory.h" +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" #else #include #define polarssl_malloc malloc diff --git a/library/pkwrite.c b/library/pkwrite.c index 8b6d7356e..4cbba10b7 100644 --- a/library/pkwrite.c +++ b/library/pkwrite.c @@ -1,7 +1,7 @@ /* * Public Key layer for writing key files and structures * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -44,8 +44,8 @@ #include "polarssl/pem.h" #endif -#if defined(POLARSSL_MEMORY_C) -#include "polarssl/memory.h" +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" #else #include #define polarssl_malloc malloc diff --git a/library/ripemd160.c b/library/ripemd160.c index 569cbdec8..10e60df84 100644 --- a/library/ripemd160.c +++ b/library/ripemd160.c @@ -43,6 +43,12 @@ #include #endif +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" +#else +#define polarssl_printf printf +#endif + /* * 32-bit integer manipulation macros (little endian) */ @@ -573,7 +579,7 @@ int ripemd160_self_test( int verbose ) for( i = 0; i < TESTS; i++ ) { if( verbose != 0 ) - printf( " RIPEMD-160 test #%d: ", i + 1 ); + polarssl_printf( " RIPEMD-160 test #%d: ", i + 1 ); ripemd160( (const unsigned char *) ripemd160_test_input[i], strlen( ripemd160_test_input[i] ), @@ -582,18 +588,19 @@ int ripemd160_self_test( int verbose ) if( memcmp( output, ripemd160_test_md[i], 20 ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); for( j = 0; j < KEYS; j++ ) { if( verbose != 0 ) - printf( " HMAC-RIPEMD-160 test #%d, key #%d: ", i + 1, j + 1 ); + polarssl_printf( " HMAC-RIPEMD-160 test #%d, key #%d: ", + i + 1, j + 1 ); ripemd160_hmac( ripemd160_test_key[j], 20, (const unsigned char *) ripemd160_test_input[i], @@ -603,17 +610,17 @@ int ripemd160_self_test( int verbose ) if( memcmp( output, ripemd160_test_hmac[j][i], 20 ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); } if( verbose != 0 ) - printf( "\n" ); + polarssl_printf( "\n" ); } return( 0 ); diff --git a/library/rsa.c b/library/rsa.c index f4ff2373c..3a1ea355c 100644 --- a/library/rsa.c +++ b/library/rsa.c @@ -1,7 +1,7 @@ /* * The RSA public-key cryptosystem * - * Copyright (C) 2006-2011, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -43,6 +43,12 @@ #include #include +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" +#else +#define polarssl_printf printf +#endif + /* * Initialize an RSA context */ @@ -1495,19 +1501,19 @@ int rsa_self_test( int verbose ) mpi_read_string( &rsa.QP, 16, RSA_QP ); if( verbose != 0 ) - printf( " RSA key validation: " ); + polarssl_printf( " RSA key validation: " ); if( rsa_check_pubkey( &rsa ) != 0 || rsa_check_privkey( &rsa ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } if( verbose != 0 ) - printf( "passed\n PKCS#1 encryption : " ); + polarssl_printf( "passed\n PKCS#1 encryption : " ); memcpy( rsa_plaintext, RSA_PT, PT_LEN ); @@ -1515,20 +1521,20 @@ int rsa_self_test( int verbose ) rsa_plaintext, rsa_ciphertext ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } if( verbose != 0 ) - printf( "passed\n PKCS#1 decryption : " ); + polarssl_printf( "passed\n PKCS#1 decryption : " ); if( rsa_pkcs1_decrypt( &rsa, myrand, NULL, RSA_PRIVATE, &len, rsa_ciphertext, rsa_decrypted, sizeof(rsa_decrypted) ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } @@ -1536,14 +1542,14 @@ int rsa_self_test( int verbose ) if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } #if defined(POLARSSL_SHA1_C) if( verbose != 0 ) - printf( "passed\n PKCS#1 data sign : " ); + polarssl_printf( "passed\n PKCS#1 data sign : " ); sha1( rsa_plaintext, PT_LEN, sha1sum ); @@ -1551,25 +1557,25 @@ int rsa_self_test( int verbose ) sha1sum, rsa_ciphertext ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } if( verbose != 0 ) - printf( "passed\n PKCS#1 sig. verify: " ); + polarssl_printf( "passed\n PKCS#1 sig. verify: " ); if( rsa_pkcs1_verify( &rsa, NULL, NULL, RSA_PUBLIC, POLARSSL_MD_SHA1, 0, sha1sum, rsa_ciphertext ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } if( verbose != 0 ) - printf( "passed\n\n" ); + polarssl_printf( "passed\n\n" ); #endif /* POLARSSL_SHA1_C */ rsa_free( &rsa ); diff --git a/library/sha1.c b/library/sha1.c index b301b0979..f02d6e6ef 100644 --- a/library/sha1.c +++ b/library/sha1.c @@ -1,7 +1,7 @@ /* * FIPS-180-1 compliant SHA-1 implementation * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -38,6 +38,12 @@ #include #endif +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" +#else +#define polarssl_printf printf +#endif + #if !defined(POLARSSL_SHA1_ALT) /* @@ -548,7 +554,7 @@ int sha1_self_test( int verbose ) for( i = 0; i < 3; i++ ) { if( verbose != 0 ) - printf( " SHA-1 test #%d: ", i + 1 ); + polarssl_printf( " SHA-1 test #%d: ", i + 1 ); sha1_starts( &ctx ); @@ -568,22 +574,22 @@ int sha1_self_test( int verbose ) if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); } if( verbose != 0 ) - printf( "\n" ); + polarssl_printf( "\n" ); for( i = 0; i < 7; i++ ) { if( verbose != 0 ) - printf( " HMAC-SHA-1 test #%d: ", i + 1 ); + polarssl_printf( " HMAC-SHA-1 test #%d: ", i + 1 ); if( i == 5 || i == 6 ) { @@ -604,17 +610,17 @@ int sha1_self_test( int verbose ) if( memcmp( sha1sum, sha1_hmac_test_sum[i], buflen ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); } if( verbose != 0 ) - printf( "\n" ); + polarssl_printf( "\n" ); return( 0 ); } diff --git a/library/sha256.c b/library/sha256.c index e4d48427d..638188f53 100644 --- a/library/sha256.c +++ b/library/sha256.c @@ -1,7 +1,7 @@ /* * FIPS-180-2 compliant SHA-256 implementation * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -38,6 +38,12 @@ #include #endif +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" +#else +#define polarssl_printf printf +#endif + #if !defined(POLARSSL_SHA256_ALT) /* @@ -626,7 +632,7 @@ int sha256_self_test( int verbose ) k = i < 3; if( verbose != 0 ) - printf( " SHA-%d test #%d: ", 256 - k * 32, j + 1 ); + polarssl_printf( " SHA-%d test #%d: ", 256 - k * 32, j + 1 ); sha256_starts( &ctx, k ); @@ -646,17 +652,17 @@ int sha256_self_test( int verbose ) if( memcmp( sha256sum, sha256_test_sum[i], 32 - k * 4 ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); } if( verbose != 0 ) - printf( "\n" ); + polarssl_printf( "\n" ); for( i = 0; i < 14; i++ ) { @@ -664,7 +670,7 @@ int sha256_self_test( int verbose ) k = i < 7; if( verbose != 0 ) - printf( " HMAC-SHA-%d test #%d: ", 256 - k * 32, j + 1 ); + polarssl_printf( " HMAC-SHA-%d test #%d: ", 256 - k * 32, j + 1 ); if( j == 5 || j == 6 ) { @@ -685,17 +691,17 @@ int sha256_self_test( int verbose ) if( memcmp( sha256sum, sha256_hmac_test_sum[i], buflen ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); } if( verbose != 0 ) - printf( "\n" ); + polarssl_printf( "\n" ); return( 0 ); } diff --git a/library/sha512.c b/library/sha512.c index 2366e7c2c..1bef2e90c 100644 --- a/library/sha512.c +++ b/library/sha512.c @@ -1,7 +1,7 @@ /* * FIPS-180-2 compliant SHA-384/512 implementation * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -38,6 +38,12 @@ #include #endif +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" +#else +#define polarssl_printf printf +#endif + #if !defined(POLARSSL_SHA512_ALT) /* @@ -681,7 +687,7 @@ int sha512_self_test( int verbose ) k = i < 3; if( verbose != 0 ) - printf( " SHA-%d test #%d: ", 512 - k * 128, j + 1 ); + polarssl_printf( " SHA-%d test #%d: ", 512 - k * 128, j + 1 ); sha512_starts( &ctx, k ); @@ -701,17 +707,17 @@ int sha512_self_test( int verbose ) if( memcmp( sha512sum, sha512_test_sum[i], 64 - k * 16 ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); } if( verbose != 0 ) - printf( "\n" ); + polarssl_printf( "\n" ); for( i = 0; i < 14; i++ ) { @@ -719,7 +725,7 @@ int sha512_self_test( int verbose ) k = i < 7; if( verbose != 0 ) - printf( " HMAC-SHA-%d test #%d: ", 512 - k * 128, j + 1 ); + polarssl_printf( " HMAC-SHA-%d test #%d: ", 512 - k * 128, j + 1 ); if( j == 5 || j == 6 ) { @@ -740,17 +746,17 @@ int sha512_self_test( int verbose ) if( memcmp( sha512sum, sha512_hmac_test_sum[i], buflen ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); } if( verbose != 0 ) - printf( "\n" ); + polarssl_printf( "\n" ); return( 0 ); } diff --git a/library/ssl_cache.c b/library/ssl_cache.c index e0847b6dc..6fff54b32 100644 --- a/library/ssl_cache.c +++ b/library/ssl_cache.c @@ -1,7 +1,7 @@ /* * SSL session cache implementation * - * Copyright (C) 2006-2012, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -33,8 +33,8 @@ #include "polarssl/ssl_cache.h" -#if defined(POLARSSL_MEMORY_C) -#include "polarssl/memory.h" +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" #else #define polarssl_malloc malloc #define polarssl_free free diff --git a/library/ssl_cli.c b/library/ssl_cli.c index bdd2d9502..b4ab55043 100644 --- a/library/ssl_cli.c +++ b/library/ssl_cli.c @@ -1,7 +1,7 @@ /* * SSLv3/TLSv1 client-side functions * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -30,8 +30,8 @@ #include "polarssl/debug.h" #include "polarssl/ssl.h" -#if defined(POLARSSL_MEMORY_C) -#include "polarssl/memory.h" +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" #else #define polarssl_malloc malloc #define polarssl_free free diff --git a/library/ssl_srv.c b/library/ssl_srv.c index e045fdc98..f484cf6a4 100644 --- a/library/ssl_srv.c +++ b/library/ssl_srv.c @@ -1,7 +1,7 @@ /* * SSLv3/TLSv1 server-side functions * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -33,8 +33,8 @@ #include "polarssl/ecp.h" #endif -#if defined(POLARSSL_MEMORY_C) -#include "polarssl/memory.h" +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" #else #define polarssl_malloc malloc #define polarssl_free free diff --git a/library/ssl_tls.c b/library/ssl_tls.c index a5205836c..4a9211f13 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -1,7 +1,7 @@ /* * SSLv3/TLSv1 shared functions * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -38,8 +38,8 @@ #include "polarssl/debug.h" #include "polarssl/ssl.h" -#if defined(POLARSSL_MEMORY_C) -#include "polarssl/memory.h" +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" #else #define polarssl_malloc malloc #define polarssl_free free diff --git a/library/x509.c b/library/x509.c index 74a8f8e0d..17e8b4d5e 100644 --- a/library/x509.c +++ b/library/x509.c @@ -1,7 +1,7 @@ /* * X.509 certificate and private key decoding * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -45,9 +45,10 @@ #include "polarssl/pem.h" #endif -#if defined(POLARSSL_MEMORY_C) -#include "polarssl/memory.h" +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" #else +#define polarssl_printf printf #define polarssl_malloc malloc #define polarssl_free free #endif @@ -991,7 +992,7 @@ int x509_self_test( int verbose ) x509_crt clicert; if( verbose != 0 ) - printf( " X.509 certificate load: " ); + polarssl_printf( " X.509 certificate load: " ); x509_crt_init( &clicert ); @@ -1000,7 +1001,7 @@ int x509_self_test( int verbose ) if( ret != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( ret ); } @@ -1012,27 +1013,27 @@ int x509_self_test( int verbose ) if( ret != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( ret ); } if( verbose != 0 ) - printf( "passed\n X.509 signature verify: "); + polarssl_printf( "passed\n X.509 signature verify: "); ret = x509_crt_verify( &clicert, &cacert, NULL, NULL, &flags, NULL, NULL ); if( ret != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); - printf("ret = %d, &flags = %04x\n", ret, flags); + polarssl_printf("ret = %d, &flags = %04x\n", ret, flags); return( ret ); } if( verbose != 0 ) - printf( "passed\n\n"); + polarssl_printf( "passed\n\n"); x509_crt_free( &cacert ); x509_crt_free( &clicert ); diff --git a/library/x509_crl.c b/library/x509_crl.c index 964aa7e83..92ede6d49 100644 --- a/library/x509_crl.c +++ b/library/x509_crl.c @@ -1,7 +1,7 @@ /* * X.509 certificate and private key decoding * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -44,8 +44,8 @@ #include "polarssl/pem.h" #endif -#if defined(POLARSSL_MEMORY_C) -#include "polarssl/memory.h" +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" #else #define polarssl_malloc malloc #define polarssl_free free diff --git a/library/x509_crt.c b/library/x509_crt.c index 585972542..4c79a7a6c 100644 --- a/library/x509_crt.c +++ b/library/x509_crt.c @@ -1,7 +1,7 @@ /* * X.509 certificate and private key decoding * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -44,8 +44,8 @@ #include "polarssl/pem.h" #endif -#if defined(POLARSSL_MEMORY_C) -#include "polarssl/memory.h" +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" #else #define polarssl_malloc malloc #define polarssl_free free diff --git a/library/x509_csr.c b/library/x509_csr.c index a5cef4c30..bdadec3e3 100644 --- a/library/x509_csr.c +++ b/library/x509_csr.c @@ -1,7 +1,7 @@ /* * X.509 Certificate Signing Request (CSR) parsing * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -44,8 +44,8 @@ #include "polarssl/pem.h" #endif -#if defined(POLARSSL_MEMORY_C) -#include "polarssl/memory.h" +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" #else #define polarssl_malloc malloc #define polarssl_free free diff --git a/library/xtea.c b/library/xtea.c index 2cb2f30d1..1bb6f29dd 100644 --- a/library/xtea.c +++ b/library/xtea.c @@ -29,6 +29,12 @@ #include "polarssl/xtea.h" +#if defined(POLARSSL_PLATFORM_C) +#include "polarssl/platform.h" +#else +#define polarssl_printf printf +#endif + #if !defined(POLARSSL_XTEA_ALT) /* @@ -220,7 +226,7 @@ int xtea_self_test( int verbose ) for( i = 0; i < 6; i++ ) { if( verbose != 0 ) - printf( " XTEA test #%d: ", i + 1 ); + polarssl_printf( " XTEA test #%d: ", i + 1 ); memcpy( buf, xtea_test_pt[i], 8 ); @@ -230,17 +236,17 @@ int xtea_self_test( int verbose ) if( memcmp( buf, xtea_test_ct[i], 8 ) != 0 ) { if( verbose != 0 ) - printf( "failed\n" ); + polarssl_printf( "failed\n" ); return( 1 ); } if( verbose != 0 ) - printf( "passed\n" ); + polarssl_printf( "passed\n" ); } if( verbose != 0 ) - printf( "\n" ); + polarssl_printf( "\n" ); return( 0 ); } From b2f66c91586653d402f1d526eac88d1f8b67a935 Mon Sep 17 00:00:00 2001 From: Paul Bakker Date: Tue, 4 Feb 2014 16:27:57 +0100 Subject: [PATCH 3/7] Only include platform files when needed --- include/polarssl/platform.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/polarssl/platform.h b/include/polarssl/platform.h index 61b31190f..22a1f464c 100644 --- a/include/polarssl/platform.h +++ b/include/polarssl/platform.h @@ -29,7 +29,6 @@ #include "config.h" -#include #include #ifdef __cplusplus @@ -47,6 +46,7 @@ extern "C" { #if defined(POLARSSL_MEMORY_C) #include "memory.h" #else +#include #define polarssl_malloc malloc #define polarssl_free free #endif From defc0ca337836263b4e8ae063133860c958029d9 Mon Sep 17 00:00:00 2001 From: Paul Bakker Date: Tue, 4 Feb 2014 17:30:24 +0100 Subject: [PATCH 4/7] Migrated the Memory layer to the Platform layer Deprecated POLARSSL_MEMORY_C and placed placeholder for memory.h to make sure current code will not break on new version. --- include/polarssl/config.h | 44 ++++++---- include/polarssl/memory.h | 103 +++-------------------- include/polarssl/memory_buffer_alloc.h | 108 +++++++++++++++++++++++++ include/polarssl/platform.h | 16 +++- library/CMakeLists.txt | 1 - library/Makefile | 2 +- library/memory.c | 63 --------------- library/memory_buffer_alloc.c | 13 ++- library/platform.c | 32 ++++++++ 9 files changed, 199 insertions(+), 183 deletions(-) create mode 100644 include/polarssl/memory_buffer_alloc.h delete mode 100644 library/memory.c diff --git a/include/polarssl/config.h b/include/polarssl/config.h index be1005710..b191e3174 100644 --- a/include/polarssl/config.h +++ b/include/polarssl/config.h @@ -3,7 +3,7 @@ * * \brief Configuration options (set of defines) * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -114,6 +114,24 @@ */ #define POLARSSL_HAVE_IPV6 +/** + * \def POLARSSL_PLATFORM_MEMORY + * + * Enable the memory allocation layer. + * + * By default PolarSSL uses the system-provided malloc() and free(). + * This allows different allocators (self-implemented or provided) to be + * provided to the platform abstraction layer. + * + * Enabling POLARSSL_PLATFORM_MEMORY will provide "platform_set_malloc_free()" + * to allow you to set an alternative malloc() and free() function pointer. + * + * Requires: POLARSSL_PLATFORM_C + * + * Enable this layer to allow use of alternative memory allocators. + */ +//#define POLARSSL_PLATFORM_MEMORY + /** * \def POLARSSL_PLATFORM_XXX_ALT * @@ -642,7 +660,6 @@ * function for 'debug output' of allocated memory. * * Requires: POLARSSL_MEMORY_BUFFER_ALLOC_C - * fprintf() * * Uncomment this macro to let the buffer allocator print out error messages. */ @@ -1426,17 +1443,8 @@ /** * \def POLARSSL_MEMORY_C - * - * Enable the memory allocation layer. - * By default PolarSSL uses the system-provided malloc() and free(). - * (As long as POLARSSL_MEMORY_STDMALLOC and POLARSSL_MEMORY_STDFREE - * are defined and unmodified) - * - * This allows different allocators (self-implemented or provided) - * - * Enable this layer to allow use of alternative memory allocators. + * Deprecated since 1.3.5. Please use POLARSSL_PLATFORM_MEMORY instead. */ -//#define POLARSSL_MEMORY_C /** * \def POLARSSL_MEMORY_BUFFER_ALLOC_C @@ -1447,7 +1455,8 @@ * * Module: library/memory_buffer_alloc.c * - * Requires: POLARSSL_MEMORY_C + * Requires: POLARSSL_PLATFORM_C + * POLARSSL_PLATFORM_MEMORY (to use it within PolarSSL) * * Enable this module to enable the buffer memory allocator. */ @@ -1995,12 +2004,12 @@ #define ENTROPY_MAX_SOURCES 20 /**< Maximum number of sources supported */ #define ENTROPY_MAX_GATHER 128 /**< Maximum amount requested from entropy sources */ -// Memory options +// Memory buffer allocator options #define MEMORY_ALIGN_MULTIPLE 4 /**< Align on multiples of this value */ -#define POLARSSL_MEMORY_STDMALLOC malloc /**< Default allocator to use, can be undefined */ -#define POLARSSL_MEMORY_STDFREE free /**< Default free to use, can be undefined */ // Platform options +#define POLARSSL_PLATFORM_STD_MALLOC malloc /**< Default allocator to use, can be undefined */ +#define POLARSSL_PLATFORM_STD_FREE free /**< Default free to use, can be undefined */ #define POLARSSL_PLATFORM_STD_PRINTF printf /**< Default printf to use, can be undefined */ #define POLARSSL_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use, can be undefined */ @@ -2143,7 +2152,8 @@ #error "POLARSSL_KEY_EXCHANGE_RSA_ENABLED defined, but not all prerequisites" #endif -#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) && !defined(POLARSSL_MEMORY_C) +#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) && \ + ( !defined(POLARSSL_PLATFORM_C) || !defined(POLARSSL_PLATFORM_MEMORY) ) #error "POLARSSL_MEMORY_BUFFER_ALLOC_C defined, but not all prerequisites" #endif diff --git a/include/polarssl/memory.h b/include/polarssl/memory.h index 6a3dab94b..64690bed9 100644 --- a/include/polarssl/memory.h +++ b/include/polarssl/memory.h @@ -1,9 +1,9 @@ /** * \file memory.h * - * \brief Memory allocation layer + * \brief Memory allocation layer (Deprecated to platform layer) * - * Copyright (C) 2006-2013, Brainspark B.V. + * Copyright (C) 2006-2014, Brainspark B.V. * * This file is part of PolarSSL (http://www.polarssl.org) * Lead Maintainer: Paul Bakker @@ -31,101 +31,18 @@ #include -#if !defined(POLARSSL_CONFIG_OPTIONS) -#define POLARSSL_MEMORY_ALIGN_MULTIPLE 4 /**< Align on multiples of this value */ - -#define POLARSSL_MEMORY_STDMALLOC malloc /**< Default allocator to use, can be undefined */ -#define POLARSSL_MEMORY_STDFREE free /**< Default free to use, can be undefined */ -#endif /* POLARSSL_CONFIG_OPTIONS */ - -#define MEMORY_VERIFY_NONE 0 -#define MEMORY_VERIFY_ALLOC (1 << 0) -#define MEMORY_VERIFY_FREE (1 << 1) -#define MEMORY_VERIFY_ALWAYS (MEMORY_VERIFY_ALLOC | MEMORY_VERIFY_FREE) - -#ifdef __cplusplus -extern "C" { +#if defined(POLARSSL_MEMORY_C) && !defined(POLARSSL_PLATFORM_MEMORY) +#define POLARSSL_PLATFORM_MEMORY #endif -/* - * The function pointers for malloc and free - */ -extern void * (*polarssl_malloc)( size_t len ); -extern void (*polarssl_free)( void *ptr ); +#include "platform.h" +#include "memory_buffer_alloc.h" -/** - * \brief Set your own memory implementation function pointers - * - * \param malloc_func the malloc function implementation - * \param free_func the free function implementation - * - * \return 0 if successful - */ int memory_set_own( void * (*malloc_func)( size_t ), - void (*free_func)( void * ) ); - -#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) -/** - * \brief Initialize use of stack-based memory allocator. - * The stack-based allocator does memory management inside the - * presented buffer and does not call malloc() and free(). - * It sets the global polarssl_malloc() and polarssl_free() pointers - * to its own functions. - * (Provided polarssl_malloc() and polarssl_free() are thread-safe if - * POLARSSL_THREADING_C is defined) - * - * \note This code is not optimized and provides a straight-forward - * implementation of a stack-based memory allocator. - * - * \param buf buffer to use as heap - * \param len size of the buffer - * - * \return 0 if successful - */ -int memory_buffer_alloc_init( unsigned char *buf, size_t len ); - -/** - * \brief Free the mutex for thread-safety and clear remaining memory - */ -void memory_buffer_alloc_free(); - -/** - * \brief Determine when the allocator should automatically verify the state - * of the entire chain of headers / meta-data. - * (Default: MEMORY_VERIFY_NONE) - * - * \param verify One of MEMORY_VERIFY_NONE, MEMORY_VERIFY_ALLOC, - * MEMORY_VERIFY_FREE or MEMORY_VERIFY_ALWAYS - */ -void memory_buffer_set_verify( int verify ); - -#if defined(POLARSSL_MEMORY_DEBUG) -/** - * \brief Print out the status of the allocated memory (primarily for use - * after a program should have de-allocated all memory) - * Prints out a list of 'still allocated' blocks and their stack - * trace if POLARSSL_MEMORY_BACKTRACE is defined. - */ -void memory_buffer_alloc_status(); -#endif /* POLARSSL_MEMORY_DEBUG */ - -/** - * \brief Verifies that all headers in the memory buffer are correct - * and contain sane values. Helps debug buffer-overflow errors. - * - * Prints out first failure if POLARSSL_MEMORY_DEBUG is defined. - * Prints out full header information if POLARSSL_MEMORY_DEBUG_HEADERS - * is defined. (Includes stack trace information for each block if - * POLARSSL_MEMORY_BACKTRACE is defined as well). - * - * \returns 0 if verified, 1 otherwise - */ -int memory_buffer_alloc_verify(); - -#endif /* POLARSSL_MEMORY_BUFFER_ALLOC_C */ - -#ifdef __cplusplus + void (*free_func)( void * ) ) +{ + return platform_set_malloc_free( malloc_func, free_func ); } -#endif + #endif /* memory.h */ diff --git a/include/polarssl/memory_buffer_alloc.h b/include/polarssl/memory_buffer_alloc.h new file mode 100644 index 000000000..053b1dd41 --- /dev/null +++ b/include/polarssl/memory_buffer_alloc.h @@ -0,0 +1,108 @@ +/** + * \file memory_buffer_alloc.h + * + * \brief Buffer-based memory allocator + * + * Copyright (C) 2006-2014, Brainspark B.V. + * + * This file is part of PolarSSL (http://www.polarssl.org) + * Lead Maintainer: Paul Bakker + * + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ +#ifndef POLARSSL_MEMORY_BUFFER_ALLOC_H +#define POLARSSL_MEMORY_BUFFER_ALLOC_H + +#include "config.h" + +#include + +#if !defined(POLARSSL_CONFIG_OPTIONS) +#define POLARSSL_MEMORY_ALIGN_MULTIPLE 4 /**< Align on multiples of this value */ +#endif /* POLARSSL_CONFIG_OPTIONS */ + +#define MEMORY_VERIFY_NONE 0 +#define MEMORY_VERIFY_ALLOC (1 << 0) +#define MEMORY_VERIFY_FREE (1 << 1) +#define MEMORY_VERIFY_ALWAYS (MEMORY_VERIFY_ALLOC | MEMORY_VERIFY_FREE) + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * \brief Initialize use of stack-based memory allocator. + * The stack-based allocator does memory management inside the + * presented buffer and does not call malloc() and free(). + * It sets the global polarssl_malloc() and polarssl_free() pointers + * to its own functions. + * (Provided polarssl_malloc() and polarssl_free() are thread-safe if + * POLARSSL_THREADING_C is defined) + * + * \note This code is not optimized and provides a straight-forward + * implementation of a stack-based memory allocator. + * + * \param buf buffer to use as heap + * \param len size of the buffer + * + * \return 0 if successful + */ +int memory_buffer_alloc_init( unsigned char *buf, size_t len ); + +/** + * \brief Free the mutex for thread-safety and clear remaining memory + */ +void memory_buffer_alloc_free(); + +/** + * \brief Determine when the allocator should automatically verify the state + * of the entire chain of headers / meta-data. + * (Default: MEMORY_VERIFY_NONE) + * + * \param verify One of MEMORY_VERIFY_NONE, MEMORY_VERIFY_ALLOC, + * MEMORY_VERIFY_FREE or MEMORY_VERIFY_ALWAYS + */ +void memory_buffer_set_verify( int verify ); + +#if defined(POLARSSL_MEMORY_DEBUG) +/** + * \brief Print out the status of the allocated memory (primarily for use + * after a program should have de-allocated all memory) + * Prints out a list of 'still allocated' blocks and their stack + * trace if POLARSSL_MEMORY_BACKTRACE is defined. + */ +void memory_buffer_alloc_status(); +#endif /* POLARSSL_MEMORY_DEBUG */ + +/** + * \brief Verifies that all headers in the memory buffer are correct + * and contain sane values. Helps debug buffer-overflow errors. + * + * Prints out first failure if POLARSSL_MEMORY_DEBUG is defined. + * Prints out full header information if POLARSSL_MEMORY_DEBUG_HEADERS + * is defined. (Includes stack trace information for each block if + * POLARSSL_MEMORY_BACKTRACE is defined as well). + * + * \returns 0 if verified, 1 otherwise + */ +int memory_buffer_alloc_verify(); + +#ifdef __cplusplus +} +#endif + +#endif /* memory_buffer_alloc.h */ diff --git a/include/polarssl/platform.h b/include/polarssl/platform.h index 22a1f464c..08832b3c3 100644 --- a/include/polarssl/platform.h +++ b/include/polarssl/platform.h @@ -38,13 +38,27 @@ extern "C" { #if !defined(POLARSSL_CONFIG_OPTIONS) #define POLARSSL_PLATFORM_STD_PRINTF printf /**< Default printf to use */ #define POLARSSL_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use */ +#define POLARSSL_PLATFORM_STD_MALLOC malloc /**< Default allocator to use */ +#define POLARSSL_PLATFORM_STD_FREE free /**< Default free to use */ #endif /* POLARSSL_CONFIG_OPTIONS */ /* * The function pointers for malloc and free */ #if defined(POLARSSL_MEMORY_C) -#include "memory.h" +extern void * (*polarssl_malloc)( size_t len ); +extern void (*polarssl_free)( void *ptr ); + +/** + * \brief Set your own memory implementation function pointers + * + * \param malloc_func the malloc function implementation + * \param free_func the free function implementation + * + * \return 0 if successful + */ +int platform_set_malloc_free( void * (*malloc_func)( size_t ), + void (*free_func)( void * ) ); #else #include #define polarssl_malloc malloc diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index d948ca0c4..2d7455963 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -32,7 +32,6 @@ set(src md2.c md4.c md5.c - memory.c memory_buffer_alloc.c net.c oid.c diff --git a/library/Makefile b/library/Makefile index 931602426..a4a3ce724 100644 --- a/library/Makefile +++ b/library/Makefile @@ -46,7 +46,7 @@ OBJS= aes.o aesni.o arc4.o \ error.o gcm.o havege.o \ hmac_drbg.o \ md.o md_wrap.o md2.o \ - md4.o md5.o memory.o \ + md4.o md5.o \ memory_buffer_alloc.o net.o \ oid.o \ padlock.o pbkdf2.o pem.o \ diff --git a/library/memory.c b/library/memory.c deleted file mode 100644 index 93ca37916..000000000 --- a/library/memory.c +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Memory allocation layer - * - * Copyright (C) 2006-2013, Brainspark B.V. - * - * This file is part of PolarSSL (http://www.polarssl.org) - * Lead Maintainer: Paul Bakker - * - * All rights reserved. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - -#include "polarssl/config.h" - -#if defined(POLARSSL_MEMORY_C) - -#include "polarssl/memory.h" - -#if !defined(POLARSSL_MEMORY_STDMALLOC) -static void *memory_malloc_uninit( size_t len ) -{ - ((void) len); - return( NULL ); -} - -#define POLARSSL_MEMORY_STDMALLOC memory_malloc_uninit -#endif /* !POLARSSL_MEMORY_STDMALLOC */ - -#if !defined(POLARSSL_MEMORY_STDFREE) -static void memory_free_uninit( void *ptr ) -{ - ((void) ptr); -} - -#define POLARSSL_MEMORY_STDFREE memory_free_uninit -#endif /* !POLARSSL_MEMORY_STDFREE */ - -void * (*polarssl_malloc)( size_t ) = POLARSSL_MEMORY_STDMALLOC; -void (*polarssl_free)( void * ) = POLARSSL_MEMORY_STDFREE; - -int memory_set_own( void * (*malloc_func)( size_t ), - void (*free_func)( void * ) ) -{ - polarssl_malloc = malloc_func; - polarssl_free = free_func; - - return( 0 ); -} - -#endif /* POLARSSL_MEMORY_C */ diff --git a/library/memory_buffer_alloc.c b/library/memory_buffer_alloc.c index fef43312f..21968914f 100644 --- a/library/memory_buffer_alloc.c +++ b/library/memory_buffer_alloc.c @@ -25,9 +25,9 @@ #include "polarssl/config.h" -#if defined(POLARSSL_MEMORY_C) && defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) +#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) -#include "polarssl/memory.h" +#include "polarssl/memory_buffer_alloc.h" #include @@ -544,11 +544,10 @@ int memory_buffer_alloc_init( unsigned char *buf, size_t len ) #if defined(POLARSSL_THREADING_C) polarssl_mutex_init( &heap.mutex ); - polarssl_malloc = buffer_alloc_malloc_mutexed; - polarssl_free = buffer_alloc_free_mutexed; + platform_set_malloc_free( buffer_alloc_malloc_mutexed, + buffer_alloc_free_mutexed ); #else - polarssl_malloc = buffer_alloc_malloc; - polarssl_free = buffer_alloc_free; + platform_set_malloc_free( buffer_alloc_malloc, buffer_alloc_free ); #endif heap.buf = buf; @@ -570,4 +569,4 @@ void memory_buffer_alloc_free() memset( &heap, 0, sizeof(buffer_alloc_ctx) ); } -#endif /* POLARSSL_MEMORY_C && POLARSSL_MEMORY_BUFFER_ALLOC_C */ +#endif /* POLARSSL_MEMORY_BUFFER_ALLOC_C */ diff --git a/library/platform.c b/library/platform.c index 32f949ff0..823b17d13 100644 --- a/library/platform.c +++ b/library/platform.c @@ -29,6 +29,38 @@ #include "polarssl/platform.h" +#if defined(POLARSSL_PLATFORM_MEMORY) +#if !defined(POLARSSL_PLATFORM_STD_MALLOC) +static void *platform_malloc_uninit( size_t len ) +{ + ((void) len); + return( NULL ); +} + +#define POLARSSL_PLATFORM_STD_MALLOC memory_malloc_uninit +#endif /* !POLARSSL_PLATFORM_STD_MALLOC */ + +#if !defined(POLARSSL_PLATFORM_STD_FREE) +static void platform_free_uninit( void *ptr ) +{ + ((void) ptr); +} + +#define POLARSSL_PLATFORM_STD_FREE memory_free_uninit +#endif /* !POLARSSL_PLATFORM_STD_FREE */ + +void * (*polarssl_malloc)( size_t ) = POLARSSL_PLATFORM_STD_MALLOC; +void (*polarssl_free)( void * ) = POLARSSL_PLATFORM_STD_FREE; + +int platform_set_malloc_free( void * (*malloc_func)( size_t ), + void (*free_func)( void * ) ) +{ + polarssl_malloc = malloc_func; + polarssl_free = free_func; + return( 0 ); +} +#endif /* POLARSSL_PLATFORM_MEMORY */ + #if defined(POLARSSL_PLATFORM_PRINTF_ALT) #if !defined(POLARSSL_PLATFORM_STD_PRINTF) /* From a9066cf8f15ae42e57e3b9ff744acd7c286286b4 Mon Sep 17 00:00:00 2001 From: Paul Bakker Date: Wed, 5 Feb 2014 15:13:04 +0100 Subject: [PATCH 5/7] Include stdlib in the right spot --- include/polarssl/platform.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/polarssl/platform.h b/include/polarssl/platform.h index 08832b3c3..b5dd4d144 100644 --- a/include/polarssl/platform.h +++ b/include/polarssl/platform.h @@ -36,6 +36,7 @@ extern "C" { #endif #if !defined(POLARSSL_CONFIG_OPTIONS) +#include #define POLARSSL_PLATFORM_STD_PRINTF printf /**< Default printf to use */ #define POLARSSL_PLATFORM_STD_FPRINTF fprintf /**< Default fprintf to use */ #define POLARSSL_PLATFORM_STD_MALLOC malloc /**< Default allocator to use */ @@ -45,7 +46,7 @@ extern "C" { /* * The function pointers for malloc and free */ -#if defined(POLARSSL_MEMORY_C) +#if defined(POLARSSL_PLATFORM_MEMORY) extern void * (*polarssl_malloc)( size_t len ); extern void (*polarssl_free)( void *ptr ); @@ -60,7 +61,6 @@ extern void (*polarssl_free)( void *ptr ); int platform_set_malloc_free( void * (*malloc_func)( size_t ), void (*free_func)( void * ) ); #else -#include #define polarssl_malloc malloc #define polarssl_free free #endif From 71dfa861a6707de02ea8b69545940bc013ca2541 Mon Sep 17 00:00:00 2001 From: Paul Bakker Date: Wed, 5 Feb 2014 15:38:15 +0100 Subject: [PATCH 6/7] Made valid prototypes by adding ( void ) as parameter prototype --- include/polarssl/memory_buffer_alloc.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/polarssl/memory_buffer_alloc.h b/include/polarssl/memory_buffer_alloc.h index 053b1dd41..ccddc0020 100644 --- a/include/polarssl/memory_buffer_alloc.h +++ b/include/polarssl/memory_buffer_alloc.h @@ -66,7 +66,7 @@ int memory_buffer_alloc_init( unsigned char *buf, size_t len ); /** * \brief Free the mutex for thread-safety and clear remaining memory */ -void memory_buffer_alloc_free(); +void memory_buffer_alloc_free( void ); /** * \brief Determine when the allocator should automatically verify the state @@ -85,7 +85,7 @@ void memory_buffer_set_verify( int verify ); * Prints out a list of 'still allocated' blocks and their stack * trace if POLARSSL_MEMORY_BACKTRACE is defined. */ -void memory_buffer_alloc_status(); +void memory_buffer_alloc_status( void ); #endif /* POLARSSL_MEMORY_DEBUG */ /** @@ -99,7 +99,7 @@ void memory_buffer_alloc_status(); * * \returns 0 if verified, 1 otherwise */ -int memory_buffer_alloc_verify(); +int memory_buffer_alloc_verify( void ); #ifdef __cplusplus } From 119602bdde824268f7d0f7cd40d54f6d015e426e Mon Sep 17 00:00:00 2001 From: Paul Bakker Date: Wed, 5 Feb 2014 15:42:55 +0100 Subject: [PATCH 7/7] Typo fix in memory_buffer_alloc.c --- library/memory_buffer_alloc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/memory_buffer_alloc.c b/library/memory_buffer_alloc.c index 21968914f..33ab5ac51 100644 --- a/library/memory_buffer_alloc.c +++ b/library/memory_buffer_alloc.c @@ -517,7 +517,7 @@ void memory_buffer_alloc_status() debug_chain(); } } -#endif /* POLARSSL_MEMORY_BUFFER_ALLOC_DEBUG */ +#endif /* POLARSSL_MEMORY_DEBUG */ #if defined(POLARSSL_THREADING_C) static void *buffer_alloc_malloc_mutexed( size_t len )