Commit 6a28e722 authored by Paul Bakker's avatar Paul Bakker
Browse files

Merged platform compatibility layer

parents 0910f32e 119602bd
......@@ -9,6 +9,10 @@ Features
* Option to set the Curve preference order
* Support for RSASSA-PSS keys and signatures in certificates, CSRs
and CRLs
* Single Platform compatilibity layer (for memory / printf / fprintf)
Changes
* Deprecated the Memory layer
Bugfix
* ecp_gen_keypair() does more tries to prevent failure because of
......
......@@ -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 <polarssl_maintainer at polarssl.org>
......@@ -113,6 +113,42 @@
* Comment if your system does not support the IPv6 socket interface
*/
#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
*
* 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 */
/**
......@@ -624,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.
*/
......@@ -1408,15 +1443,7 @@
/**
* \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
......@@ -1429,7 +1456,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.
*/
......@@ -1620,6 +1648,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
*
......@@ -1964,10 +2005,14 @@
#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 */
// SSL Cache options
//
......@@ -2108,7 +2153,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
......
/**
* \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 <polarssl_maintainer at polarssl.org>
......@@ -31,101 +31,18 @@
#include <stdlib.h>
#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 */
/**
* \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 <polarssl_maintainer at polarssl.org>
*
* 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 <stdlib.h>
#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( void );
/**
* \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( void );
#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( void );
#ifdef __cplusplus
}
#endif
#endif /* memory_buffer_alloc.h */
/**
* \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 <polarssl_maintainer at polarssl.org>
*
* 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 <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
#if !defined(POLARSSL_CONFIG_OPTIONS)
#include <stdlib.h>
#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_PLATFORM_MEMORY)
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
#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 */
......@@ -32,7 +32,6 @@ set(src
md2.c
md4.c
md5.c
memory.c
memory_buffer_alloc.c
net.c
oid.c
......@@ -46,6 +45,7 @@ set(src
pk_wrap.c
pkparse.c
pkwrite.c
platform.c
ripemd160.c
rsa.c
sha1.c
......
......@@ -46,13 +46,13 @@ 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 \
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 \
......
/*
* 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 <polarssl_maintainer at polarssl.org>
......@@ -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 )
{