| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| CMakeCache.txt | ||
| CMakeFiles | ||
| CTestTestfile.cmake | ||
| cmake_install.cmake | ||
| Testing |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| cmake_minimum_required(VERSION 2.6) | ||
| project(POLARSSL C) | ||
|
|
||
| if(CMAKE_COMPILER_IS_GNUCC) | ||
| set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O2 -Wall -Wextra -W -Wdeclaration-after-statement") | ||
| set(CMAKE_C_FLAGS_DEBUG "-g3 -O0") | ||
| set(CMAKE_C_FLAGS_COVERAGE "-g3 -O0 -fprofile-arcs -ftest-coverage -lgcov") | ||
| endif(CMAKE_COMPILER_IS_GNUCC) | ||
|
|
||
| if(CMAKE_BUILD_TYPE STREQUAL "Coverage") | ||
| if(CMAKE_COMPILER_IS_GNUCC) | ||
| set(CMAKE_SHARED_LINKER_FLAGS "-fprofile-arcs -ftest-coverage") | ||
| endif(CMAKE_COMPILER_IS_GNUCC) | ||
| endif(CMAKE_BUILD_TYPE STREQUAL "Coverage") | ||
|
|
||
| option(USE_PKCS11_HELPER_LIBRARY "Build PolarSSL with the pkcs11-helper library." OFF) | ||
|
|
||
| option(ENABLE_ZLIB_SUPPORT "Build PolarSSL with zlib library." OFF) | ||
|
|
||
| if(LIB_INSTALL_DIR) | ||
| else() | ||
| set(LIB_INSTALL_DIR lib) | ||
| endif() | ||
|
|
||
| include_directories(include/) | ||
|
|
||
| if(ENABLE_ZLIB_SUPPORT) | ||
| find_package(ZLIB) | ||
|
|
||
| if(ZLIB_FOUND) | ||
| include_directories(ZLIB_INCLUDE_DIR) | ||
| endif(ZLIB_FOUND) | ||
| endif(ENABLE_ZLIB_SUPPORT) | ||
|
|
||
| add_subdirectory(library) | ||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| README for PolarSSL | ||
|
|
||
| -- COMPILING | ||
| There are currently three active build systems within the PolarSSL releases: | ||
| - Make | ||
| - CMake | ||
| - Microsoft Visual Studio | ||
|
|
||
| The main system used for development is CMake. That system is always the most up-to-date. The others should reflect all changes present in the CMake build system, but some features are not ported there by default. | ||
|
|
||
| --- Make | ||
| In order to build the source using Make, just enter at the command line: | ||
| make | ||
|
|
||
| In order to run the tests, enter: | ||
| make check | ||
|
|
||
| --- CMake | ||
| In order to build the source using CMake, just enter at the command line: | ||
| cmake . | ||
| make | ||
|
|
||
| There are 3 different active build modes specified within the CMake buildsystem: | ||
| - Release | ||
| This generates the default code without any unnecessary information in the binary files. | ||
| - Debug | ||
| This generates debug information and disables optimization of the code. | ||
| - Coverage | ||
| This generates code coverage information in addition to debug information. | ||
|
|
||
| Switching build modes in CMake is simple. For debug mode, enter at the command line: | ||
| cmake -D CMAKE_BUILD_TYPE:String="Debug" . | ||
|
|
||
| In order to run the tests, enter: | ||
| make test | ||
|
|
||
| --- Microsoft Visual Studio | ||
| The build files for Microsoft Visual Studio are generated for Visual Studio 6.0 all future Visual Studio's should be able to open and use this older version of the build files. | ||
|
|
||
| The workspace 'polarssl.dsw' contains all the basic projects needed to build the library and all the programs. The files in tests are not generated and compiled, as these need a perl environment as well. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Makefile |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,202 @@ | ||
| /** | ||
| * \file aes.h | ||
| * | ||
| * \brief AES block cipher | ||
| * | ||
| * Copyright (C) 2006-2013, 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_AES_H | ||
| #define POLARSSL_AES_H | ||
|
|
||
| #include "config.h" | ||
|
|
||
| #include <string.h> | ||
|
|
||
| #ifdef _MSC_VER | ||
| #include <basetsd.h> | ||
| typedef UINT32 uint32_t; | ||
| #else | ||
| #include <inttypes.h> | ||
| #endif | ||
|
|
||
| #define AES_ENCRYPT 1 | ||
| #define AES_DECRYPT 0 | ||
|
|
||
| #define POLARSSL_ERR_AES_INVALID_KEY_LENGTH -0x0020 /**< Invalid key length. */ | ||
| #define POLARSSL_ERR_AES_INVALID_INPUT_LENGTH -0x0022 /**< Invalid data input length. */ | ||
|
|
||
| #if !defined(POLARSSL_AES_ALT) | ||
| // Regular implementation | ||
| // | ||
|
|
||
| /** | ||
| * \brief AES context structure | ||
| */ | ||
| typedef struct | ||
| { | ||
| int nr; /*!< number of rounds */ | ||
| uint32_t *rk; /*!< AES round keys */ | ||
| uint32_t buf[68]; /*!< unaligned data */ | ||
| } | ||
| aes_context; | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| /** | ||
| * \brief AES key schedule (encryption) | ||
| * | ||
| * \param ctx AES context to be initialized | ||
| * \param key encryption key | ||
| * \param keysize must be 128, 192 or 256 | ||
| * | ||
| * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH | ||
| */ | ||
| int aes_setkey_enc( aes_context *ctx, const unsigned char *key, unsigned int keysize ); | ||
|
|
||
| /** | ||
| * \brief AES key schedule (decryption) | ||
| * | ||
| * \param ctx AES context to be initialized | ||
| * \param key decryption key | ||
| * \param keysize must be 128, 192 or 256 | ||
| * | ||
| * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH | ||
| */ | ||
| int aes_setkey_dec( aes_context *ctx, const unsigned char *key, unsigned int keysize ); | ||
|
|
||
| /** | ||
| * \brief AES-ECB block encryption/decryption | ||
| * | ||
| * \param ctx AES context | ||
| * \param mode AES_ENCRYPT or AES_DECRYPT | ||
| * \param input 16-byte input block | ||
| * \param output 16-byte output block | ||
| * | ||
| * \return 0 if successful | ||
| */ | ||
| int aes_crypt_ecb( aes_context *ctx, | ||
| int mode, | ||
| const unsigned char input[16], | ||
| unsigned char output[16] ); | ||
|
|
||
| /** | ||
| * \brief AES-CBC buffer encryption/decryption | ||
| * Length should be a multiple of the block | ||
| * size (16 bytes) | ||
| * | ||
| * \param ctx AES context | ||
| * \param mode AES_ENCRYPT or AES_DECRYPT | ||
| * \param length length of the input data | ||
| * \param iv initialization vector (updated after use) | ||
| * \param input buffer holding the input data | ||
| * \param output buffer holding the output data | ||
| * | ||
| * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_INPUT_LENGTH | ||
| */ | ||
| int aes_crypt_cbc( aes_context *ctx, | ||
| int mode, | ||
| size_t length, | ||
| unsigned char iv[16], | ||
| const unsigned char *input, | ||
| unsigned char *output ); | ||
|
|
||
| /** | ||
| * \brief AES-CFB128 buffer encryption/decryption. | ||
| * | ||
| * Note: Due to the nature of CFB you should use the same key schedule for | ||
| * both encryption and decryption. So a context initialized with | ||
| * aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT. | ||
| * | ||
| * both | ||
| * \param ctx AES context | ||
| * \param mode AES_ENCRYPT or AES_DECRYPT | ||
| * \param length length of the input data | ||
| * \param iv_off offset in IV (updated after use) | ||
| * \param iv initialization vector (updated after use) | ||
| * \param input buffer holding the input data | ||
| * \param output buffer holding the output data | ||
| * | ||
| * \return 0 if successful | ||
| */ | ||
| int aes_crypt_cfb128( aes_context *ctx, | ||
| int mode, | ||
| size_t length, | ||
| size_t *iv_off, | ||
| unsigned char iv[16], | ||
| const unsigned char *input, | ||
| unsigned char *output ); | ||
|
|
||
| /** | ||
| * \brief AES-CTR buffer encryption/decryption | ||
| * | ||
| * Warning: You have to keep the maximum use of your counter in mind! | ||
| * | ||
| * Note: Due to the nature of CTR you should use the same key schedule for | ||
| * both encryption and decryption. So a context initialized with | ||
| * aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT. | ||
| * | ||
| * \param length The length of the data | ||
| * \param nc_off The offset in the current stream_block (for resuming | ||
| * within current cipher stream). The offset pointer to | ||
| * should be 0 at the start of a stream. | ||
| * \param nonce_counter The 128-bit nonce and counter. | ||
| * \param stream_block The saved stream-block for resuming. Is overwritten | ||
| * by the function. | ||
| * \param input The input data stream | ||
| * \param output The output data stream | ||
| * | ||
| * \return 0 if successful | ||
| */ | ||
| int aes_crypt_ctr( aes_context *ctx, | ||
| size_t length, | ||
| size_t *nc_off, | ||
| unsigned char nonce_counter[16], | ||
| unsigned char stream_block[16], | ||
| const unsigned char *input, | ||
| unsigned char *output ); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #else /* POLARSSL_AES_ALT */ | ||
| #include "aes_alt.h" | ||
| #endif /* POLARSSL_AES_ALT */ | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| /** | ||
| * \brief Checkup routine | ||
| * | ||
| * \return 0 if successful, or 1 if the test failed | ||
| */ | ||
| int aes_self_test( int verbose ); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif /* aes.h */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /** | ||
| * \file arc4.h | ||
| * | ||
| * \brief The ARCFOUR stream cipher | ||
| * | ||
| * Copyright (C) 2006-2013, 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_ARC4_H | ||
| #define POLARSSL_ARC4_H | ||
|
|
||
| #include "config.h" | ||
|
|
||
| #include <string.h> | ||
|
|
||
| #if !defined(POLARSSL_ARC4_ALT) | ||
| // Regular implementation | ||
| // | ||
|
|
||
| /** | ||
| * \brief ARC4 context structure | ||
| */ | ||
| typedef struct | ||
| { | ||
| int x; /*!< permutation index */ | ||
| int y; /*!< permutation index */ | ||
| unsigned char m[256]; /*!< permutation table */ | ||
| } | ||
| arc4_context; | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| /** | ||
| * \brief ARC4 key schedule | ||
| * | ||
| * \param ctx ARC4 context to be initialized | ||
| * \param key the secret key | ||
| * \param keylen length of the key | ||
| */ | ||
| void arc4_setup( arc4_context *ctx, const unsigned char *key, unsigned int keylen ); | ||
|
|
||
| /** | ||
| * \brief ARC4 cipher function | ||
| * | ||
| * \param ctx ARC4 context | ||
| * \param length length of the input data | ||
| * \param input buffer holding the input data | ||
| * \param output buffer for the output data | ||
| * | ||
| * \return 0 if successful | ||
| */ | ||
| int arc4_crypt( arc4_context *ctx, size_t length, const unsigned char *input, | ||
| unsigned char *output ); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #else /* POLARSSL_ARC4_ALT */ | ||
| #include "arc4_alt.h" | ||
| #endif /* POLARSSL_ARC4_ALT */ | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| /** | ||
| * \brief Checkup routine | ||
| * | ||
| * \return 0 if successful, or 1 if the test failed | ||
| */ | ||
| int arc4_self_test( int verbose ); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif /* arc4.h */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,246 @@ | ||
| /** | ||
| * \file asn1.h | ||
| * | ||
| * \brief Generic ASN.1 parsing | ||
| * | ||
| * Copyright (C) 2006-2011, 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_ASN1_H | ||
| #define POLARSSL_ASN1_H | ||
|
|
||
| #include "config.h" | ||
|
|
||
| #if defined(POLARSSL_BIGNUM_C) | ||
| #include "bignum.h" | ||
| #endif | ||
|
|
||
| #include <string.h> | ||
|
|
||
| /** | ||
| * \addtogroup asn1_module | ||
| * \{ | ||
| */ | ||
|
|
||
| /** | ||
| * \name ASN1 Error codes | ||
| * These error codes are OR'ed to X509 error codes for | ||
| * higher error granularity. | ||
| * ASN1 is a standard to specify data structures. | ||
| * \{ | ||
| */ | ||
| #define POLARSSL_ERR_ASN1_OUT_OF_DATA -0x0060 /**< Out of data when parsing an ASN1 data structure. */ | ||
| #define POLARSSL_ERR_ASN1_UNEXPECTED_TAG -0x0062 /**< ASN1 tag was of an unexpected value. */ | ||
| #define POLARSSL_ERR_ASN1_INVALID_LENGTH -0x0064 /**< Error when trying to determine the length or invalid length. */ | ||
| #define POLARSSL_ERR_ASN1_LENGTH_MISMATCH -0x0066 /**< Actual length differs from expected length. */ | ||
| #define POLARSSL_ERR_ASN1_INVALID_DATA -0x0068 /**< Data is invalid. (not used) */ | ||
| #define POLARSSL_ERR_ASN1_MALLOC_FAILED -0x006A /**< Memory allocation failed */ | ||
| #define POLARSSL_ERR_ASN1_BUF_TOO_SMALL -0x006C /**< Buffer too small when writing ASN.1 data structure. */ | ||
|
|
||
| /* \} name */ | ||
|
|
||
| /** | ||
| * \name DER constants | ||
| * These constants comply with DER encoded the ANS1 type tags. | ||
| * DER encoding uses hexadecimal representation. | ||
| * An example DER sequence is:\n | ||
| * - 0x02 -- tag indicating INTEGER | ||
| * - 0x01 -- length in octets | ||
| * - 0x05 -- value | ||
| * Such sequences are typically read into \c ::x509_buf. | ||
| * \{ | ||
| */ | ||
| #define ASN1_BOOLEAN 0x01 | ||
| #define ASN1_INTEGER 0x02 | ||
| #define ASN1_BIT_STRING 0x03 | ||
| #define ASN1_OCTET_STRING 0x04 | ||
| #define ASN1_NULL 0x05 | ||
| #define ASN1_OID 0x06 | ||
| #define ASN1_UTF8_STRING 0x0C | ||
| #define ASN1_SEQUENCE 0x10 | ||
| #define ASN1_SET 0x11 | ||
| #define ASN1_PRINTABLE_STRING 0x13 | ||
| #define ASN1_T61_STRING 0x14 | ||
| #define ASN1_IA5_STRING 0x16 | ||
| #define ASN1_UTC_TIME 0x17 | ||
| #define ASN1_GENERALIZED_TIME 0x18 | ||
| #define ASN1_UNIVERSAL_STRING 0x1C | ||
| #define ASN1_BMP_STRING 0x1E | ||
| #define ASN1_PRIMITIVE 0x00 | ||
| #define ASN1_CONSTRUCTED 0x20 | ||
| #define ASN1_CONTEXT_SPECIFIC 0x80 | ||
| /* \} name */ | ||
| /* \} addtogroup asn1_module */ | ||
|
|
||
| /** Returns the size of the binary string, without the trailing \\0 */ | ||
| #define OID_SIZE(x) (sizeof(x) - 1) | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| /** | ||
| * \name Functions to parse ASN.1 data structures | ||
| * \{ | ||
| */ | ||
|
|
||
| /** | ||
| * Type-length-value structure that allows for ASN1 using DER. | ||
| */ | ||
| typedef struct _asn1_buf | ||
| { | ||
| int tag; /**< ASN1 type, e.g. ASN1_UTF8_STRING. */ | ||
| size_t len; /**< ASN1 length, e.g. in octets. */ | ||
| unsigned char *p; /**< ASN1 data, e.g. in ASCII. */ | ||
| } | ||
| asn1_buf; | ||
|
|
||
| /** | ||
| * Container for ASN1 bit strings. | ||
| */ | ||
| typedef struct _asn1_bitstring | ||
| { | ||
| size_t len; /**< ASN1 length, e.g. in octets. */ | ||
| unsigned char unused_bits; /**< Number of unused bits at the end of the string */ | ||
| unsigned char *p; /**< Raw ASN1 data for the bit string */ | ||
| } | ||
| asn1_bitstring; | ||
|
|
||
| /** | ||
| * Container for a sequence of ASN.1 items | ||
| */ | ||
| typedef struct _asn1_sequence | ||
| { | ||
| asn1_buf buf; /**< Buffer containing the given ASN.1 item. */ | ||
| struct _asn1_sequence *next; /**< The next entry in the sequence. */ | ||
| } | ||
| asn1_sequence; | ||
|
|
||
| /** | ||
| * Get the length of an ASN.1 element. | ||
| * Updates the pointer to immediately behind the length. | ||
| * | ||
| * \param p The position in the ASN.1 data | ||
| * \param end End of data | ||
| * \param len The variable that will receive the value | ||
| * | ||
| * \return 0 if successful, POLARSSL_ERR_ASN1_OUT_OF_DATA on reaching | ||
| * end of data, POLARSSL_ERR_ASN1_INVALID_LENGTH if length is | ||
| * unparseable. | ||
| */ | ||
| int asn1_get_len( unsigned char **p, | ||
| const unsigned char *end, | ||
| size_t *len ); | ||
|
|
||
| /** | ||
| * Get the tag and length of the tag. Check for the requested tag. | ||
| * Updates the pointer to immediately behind the tag and length. | ||
| * | ||
| * \param p The position in the ASN.1 data | ||
| * \param end End of data | ||
| * \param len The variable that will receive the length | ||
| * \param tag The expected tag | ||
| * | ||
| * \return 0 if successful, POLARSSL_ERR_ASN1_UNEXPECTED_TAG if tag did | ||
| * not match requested tag, or another specific ASN.1 error code. | ||
| */ | ||
| int asn1_get_tag( unsigned char **p, | ||
| const unsigned char *end, | ||
| size_t *len, int tag ); | ||
|
|
||
| /** | ||
| * Retrieve a boolean ASN.1 tag and its value. | ||
| * Updates the pointer to immediately behind the full tag. | ||
| * | ||
| * \param p The position in the ASN.1 data | ||
| * \param end End of data | ||
| * \param val The variable that will receive the value | ||
| * | ||
| * \return 0 if successful or a specific ASN.1 error code. | ||
| */ | ||
| int asn1_get_bool( unsigned char **p, | ||
| const unsigned char *end, | ||
| int *val ); | ||
|
|
||
| /** | ||
| * Retrieve an integer ASN.1 tag and its value. | ||
| * Updates the pointer to immediately behind the full tag. | ||
| * | ||
| * \param p The position in the ASN.1 data | ||
| * \param end End of data | ||
| * \param val The variable that will receive the value | ||
| * | ||
| * \return 0 if successful or a specific ASN.1 error code. | ||
| */ | ||
| int asn1_get_int( unsigned char **p, | ||
| const unsigned char *end, | ||
| int *val ); | ||
|
|
||
| /** | ||
| * Retrieve a bitstring ASN.1 tag and its value. | ||
| * Updates the pointer to immediately behind the full tag. | ||
| * | ||
| * \param p The position in the ASN.1 data | ||
| * \param end End of data | ||
| * \param bs The variable that will receive the value | ||
| * | ||
| * \return 0 if successful or a specific ASN.1 error code. | ||
| */ | ||
| int asn1_get_bitstring( unsigned char **p, const unsigned char *end, | ||
| asn1_bitstring *bs); | ||
|
|
||
| /** | ||
| * Parses and splits an ASN.1 "SEQUENCE OF <tag>" | ||
| * Updated the pointer to immediately behind the full sequence tag. | ||
| * | ||
| * \param p The position in the ASN.1 data | ||
| * \param end End of data | ||
| * \param cur First variable in the chain to fill | ||
| * \param tag Type of sequence | ||
| * | ||
| * \return 0 if successful or a specific ASN.1 error code. | ||
| */ | ||
| int asn1_get_sequence_of( unsigned char **p, | ||
| const unsigned char *end, | ||
| asn1_sequence *cur, | ||
| int tag); | ||
|
|
||
| #if defined(POLARSSL_BIGNUM_C) | ||
| /** | ||
| * Retrieve a MPI value from an integer ASN.1 tag. | ||
| * Updates the pointer to immediately behind the full tag. | ||
| * | ||
| * \param p The position in the ASN.1 data | ||
| * \param end End of data | ||
| * \param X The MPI that will receive the value | ||
| * | ||
| * \return 0 if successful or a specific ASN.1 or MPI error code. | ||
| */ | ||
| int asn1_get_mpi( unsigned char **p, | ||
| const unsigned char *end, | ||
| mpi *X ); | ||
| #endif | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif /* asn1.h */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /** | ||
| * \file asn1write.h | ||
| * | ||
| * \brief ASN.1 buffer writing functionality | ||
| * | ||
| * Copyright (C) 2006-2012, 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_ASN1_WRITE_H | ||
| #define POLARSSL_ASN1_WRITE_H | ||
|
|
||
| #include "asn1.h" | ||
|
|
||
| #define ASN1_CHK_ADD(g, f) if( ( ret = f ) < 0 ) return( ret ); else g += ret | ||
|
|
||
| int asn1_write_len( unsigned char **p, unsigned char *start, size_t len ); | ||
| int asn1_write_tag( unsigned char **p, unsigned char *start, unsigned char tag ); | ||
| int asn1_write_mpi( unsigned char **p, unsigned char *start, mpi *X ); | ||
| int asn1_write_null( unsigned char **p, unsigned char *start ); | ||
| int asn1_write_oid( unsigned char **p, unsigned char *start, char *oid ); | ||
| int asn1_write_algorithm_identifier( unsigned char **p, unsigned char *start, char *algorithm_oid ); | ||
| int asn1_write_int( unsigned char **p, unsigned char *start, int val ); | ||
| int asn1_write_printable_string( unsigned char **p, unsigned char *start, | ||
| char *text ); | ||
| int asn1_write_ia5_string( unsigned char **p, unsigned char *start, | ||
| char *text ); | ||
|
|
||
| #endif /* POLARSSL_ASN1_WRITE_H */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| /** | ||
| * \file base64.h | ||
| * | ||
| * \brief RFC 1521 base64 encoding/decoding | ||
| * | ||
| * Copyright (C) 2006-2010, 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_BASE64_H | ||
| #define POLARSSL_BASE64_H | ||
|
|
||
| #include <string.h> | ||
|
|
||
| #define POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL -0x002A /**< Output buffer too small. */ | ||
| #define POLARSSL_ERR_BASE64_INVALID_CHARACTER -0x002C /**< Invalid character in input. */ | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| /** | ||
| * \brief Encode a buffer into base64 format | ||
| * | ||
| * \param dst destination buffer | ||
| * \param dlen size of the buffer | ||
| * \param src source buffer | ||
| * \param slen amount of data to be encoded | ||
| * | ||
| * \return 0 if successful, or POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL. | ||
| * *dlen is always updated to reflect the amount | ||
| * of data that has (or would have) been written. | ||
| * | ||
| * \note Call this function with *dlen = 0 to obtain the | ||
| * required buffer size in *dlen | ||
| */ | ||
| int base64_encode( unsigned char *dst, size_t *dlen, | ||
| const unsigned char *src, size_t slen ); | ||
|
|
||
| /** | ||
| * \brief Decode a base64-formatted buffer | ||
| * | ||
| * \param dst destination buffer | ||
| * \param dlen size of the buffer | ||
| * \param src source buffer | ||
| * \param slen amount of data to be decoded | ||
| * | ||
| * \return 0 if successful, POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL, or | ||
| * POLARSSL_ERR_BASE64_INVALID_CHARACTER if the input data is | ||
| * not correct. *dlen is always updated to reflect the amount | ||
| * of data that has (or would have) been written. | ||
| * | ||
| * \note Call this function with *dlen = 0 to obtain the | ||
| * required buffer size in *dlen | ||
| */ | ||
| int base64_decode( unsigned char *dst, size_t *dlen, | ||
| const unsigned char *src, size_t slen ); | ||
|
|
||
| /** | ||
| * \brief Checkup routine | ||
| * | ||
| * \return 0 if successful, or 1 if the test failed | ||
| */ | ||
| int base64_self_test( int verbose ); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif /* base64.h */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| /** | ||
| * \file blowfish.h | ||
| * | ||
| * \brief Blowfish block cipher | ||
| * | ||
| * Copyright (C) 2012-2013, 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_BLOWFISH_H | ||
| #define POLARSSL_BLOWFISH_H | ||
|
|
||
| #include "config.h" | ||
|
|
||
| #include <string.h> | ||
|
|
||
| #ifdef _MSC_VER | ||
| #include <basetsd.h> | ||
| typedef UINT32 uint32_t; | ||
| #else | ||
| #include <inttypes.h> | ||
| #endif | ||
|
|
||
| #define BLOWFISH_ENCRYPT 1 | ||
| #define BLOWFISH_DECRYPT 0 | ||
| #define BLOWFISH_MAX_KEY 448 | ||
| #define BLOWFISH_MIN_KEY 32 | ||
| #define BLOWFISH_ROUNDS 16 /* when increasing this value, make sure to extend the initialisation vectors */ | ||
| #define BLOWFISH_BLOCKSIZE 8 /* Blowfish uses 64 bit blocks */ | ||
|
|
||
| #define POLARSSL_ERR_BLOWFISH_INVALID_KEY_LENGTH -0x0016 /**< Invalid key length. */ | ||
| #define POLARSSL_ERR_BLOWFISH_INVALID_INPUT_LENGTH -0x0018 /**< Invalid data input length. */ | ||
|
|
||
| #if !defined(POLARSSL_BLOWFISH_ALT) | ||
| // Regular implementation | ||
| // | ||
|
|
||
| /** | ||
| * \brief Blowfish context structure | ||
| */ | ||
| typedef struct | ||
| { | ||
| uint32_t P[BLOWFISH_ROUNDS + 2]; /*!< Blowfish round keys */ | ||
| uint32_t S[4][256]; /*!< key dependent S-boxes */ | ||
| } | ||
| blowfish_context; | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| /** | ||
| * \brief Blowfish key schedule | ||
| * | ||
| * \param ctx Blowfish context to be initialized | ||
| * \param key encryption key | ||
| * \param keysize must be between 32 and 448 bits | ||
| * | ||
| * \return 0 if successful, or POLARSSL_ERR_BLOWFISH_INVALID_KEY_LENGTH | ||
| */ | ||
| int blowfish_setkey( blowfish_context *ctx, const unsigned char *key, unsigned int keysize ); | ||
|
|
||
| /** | ||
| * \brief Blowfish-ECB block encryption/decryption | ||
| * | ||
| * \param ctx Blowfish context | ||
| * \param mode BLOWFISH_ENCRYPT or BLOWFISH_DECRYPT | ||
| * \param input 8-byte input block | ||
| * \param output 8-byte output block | ||
| * | ||
| * \return 0 if successful | ||
| */ | ||
| int blowfish_crypt_ecb( blowfish_context *ctx, | ||
| int mode, | ||
| const unsigned char input[BLOWFISH_BLOCKSIZE], | ||
| unsigned char output[BLOWFISH_BLOCKSIZE] ); | ||
|
|
||
| /** | ||
| * \brief Blowfish-CBC buffer encryption/decryption | ||
| * Length should be a multiple of the block | ||
| * size (8 bytes) | ||
| * | ||
| * \param ctx Blowfish context | ||
| * \param mode BLOWFISH_ENCRYPT or BLOWFISH_DECRYPT | ||
| * \param length length of the input data | ||
| * \param iv initialization vector (updated after use) | ||
| * \param input buffer holding the input data | ||
| * \param output buffer holding the output data | ||
| * | ||
| * \return 0 if successful, or POLARSSL_ERR_BLOWFISH_INVALID_INPUT_LENGTH | ||
| */ | ||
| int blowfish_crypt_cbc( blowfish_context *ctx, | ||
| int mode, | ||
| size_t length, | ||
| unsigned char iv[BLOWFISH_BLOCKSIZE], | ||
| const unsigned char *input, | ||
| unsigned char *output ); | ||
|
|
||
| /** | ||
| * \brief Blowfish CFB buffer encryption/decryption. | ||
| * | ||
| * both | ||
| * \param ctx Blowfish context | ||
| * \param mode BLOWFISH_ENCRYPT or BLOWFISH_DECRYPT | ||
| * \param length length of the input data | ||
| * \param iv_off offset in IV (updated after use) | ||
| * \param iv initialization vector (updated after use) | ||
| * \param input buffer holding the input data | ||
| * \param output buffer holding the output data | ||
| * | ||
| * \return 0 if successful | ||
| */ | ||
| int blowfish_crypt_cfb64( blowfish_context *ctx, | ||
| int mode, | ||
| size_t length, | ||
| size_t *iv_off, | ||
| unsigned char iv[BLOWFISH_BLOCKSIZE], | ||
| const unsigned char *input, | ||
| unsigned char *output ); | ||
|
|
||
| /** | ||
| * \brief Blowfish-CTR buffer encryption/decryption | ||
| * | ||
| * Warning: You have to keep the maximum use of your counter in mind! | ||
| * | ||
| * \param length The length of the data | ||
| * \param nc_off The offset in the current stream_block (for resuming | ||
| * within current cipher stream). The offset pointer to | ||
| * should be 0 at the start of a stream. | ||
| * \param nonce_counter The 64-bit nonce and counter. | ||
| * \param stream_block The saved stream-block for resuming. Is overwritten | ||
| * by the function. | ||
| * \param input The input data stream | ||
| * \param output The output data stream | ||
| * | ||
| * \return 0 if successful | ||
| */ | ||
| int blowfish_crypt_ctr( blowfish_context *ctx, | ||
| size_t length, | ||
| size_t *nc_off, | ||
| unsigned char nonce_counter[BLOWFISH_BLOCKSIZE], | ||
| unsigned char stream_block[BLOWFISH_BLOCKSIZE], | ||
| const unsigned char *input, | ||
| unsigned char *output ); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #else /* POLARSSL_BLOWFISH_ALT */ | ||
| #include "blowfish_alt.h" | ||
| #endif /* POLARSSL_BLOWFISH_ALT */ | ||
|
|
||
| #endif /* blowfish.h */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| /** | ||
| * \file camellia.h | ||
| * | ||
| * \brief Camellia block cipher | ||
| * | ||
| * Copyright (C) 2006-2013, 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_CAMELLIA_H | ||
| #define POLARSSL_CAMELLIA_H | ||
|
|
||
| #include "config.h" | ||
|
|
||
| #include <string.h> | ||
|
|
||
| #ifdef _MSC_VER | ||
| #include <basetsd.h> | ||
| typedef UINT32 uint32_t; | ||
| #else | ||
| #include <inttypes.h> | ||
| #endif | ||
|
|
||
| #define CAMELLIA_ENCRYPT 1 | ||
| #define CAMELLIA_DECRYPT 0 | ||
|
|
||
| #define POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH -0x0024 /**< Invalid key length. */ | ||
| #define POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH -0x0026 /**< Invalid data input length. */ | ||
|
|
||
| #if !defined(POLARSSL_CAMELLIA_ALT) | ||
| // Regular implementation | ||
| // | ||
|
|
||
| /** | ||
| * \brief CAMELLIA context structure | ||
| */ | ||
| typedef struct | ||
| { | ||
| int nr; /*!< number of rounds */ | ||
| uint32_t rk[68]; /*!< CAMELLIA round keys */ | ||
| } | ||
| camellia_context; | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| /** | ||
| * \brief CAMELLIA key schedule (encryption) | ||
| * | ||
| * \param ctx CAMELLIA context to be initialized | ||
| * \param key encryption key | ||
| * \param keysize must be 128, 192 or 256 | ||
| * | ||
| * \return 0 if successful, or POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH | ||
| */ | ||
| int camellia_setkey_enc( camellia_context *ctx, const unsigned char *key, unsigned int keysize ); | ||
|
|
||
| /** | ||
| * \brief CAMELLIA key schedule (decryption) | ||
| * | ||
| * \param ctx CAMELLIA context to be initialized | ||
| * \param key decryption key | ||
| * \param keysize must be 128, 192 or 256 | ||
| * | ||
| * \return 0 if successful, or POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH | ||
| */ | ||
| int camellia_setkey_dec( camellia_context *ctx, const unsigned char *key, unsigned int keysize ); | ||
|
|
||
| /** | ||
| * \brief CAMELLIA-ECB block encryption/decryption | ||
| * | ||
| * \param ctx CAMELLIA context | ||
| * \param mode CAMELLIA_ENCRYPT or CAMELLIA_DECRYPT | ||
| * \param input 16-byte input block | ||
| * \param output 16-byte output block | ||
| * | ||
| * \return 0 if successful | ||
| */ | ||
| int camellia_crypt_ecb( camellia_context *ctx, | ||
| int mode, | ||
| const unsigned char input[16], | ||
| unsigned char output[16] ); | ||
|
|
||
| /** | ||
| * \brief CAMELLIA-CBC buffer encryption/decryption | ||
| * Length should be a multiple of the block | ||
| * size (16 bytes) | ||
| * | ||
| * \param ctx CAMELLIA context | ||
| * \param mode CAMELLIA_ENCRYPT or CAMELLIA_DECRYPT | ||
| * \param length length of the input data | ||
| * \param iv initialization vector (updated after use) | ||
| * \param input buffer holding the input data | ||
| * \param output buffer holding the output data | ||
| * | ||
| * \return 0 if successful, or POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH | ||
| */ | ||
| int camellia_crypt_cbc( camellia_context *ctx, | ||
| int mode, | ||
| size_t length, | ||
| unsigned char iv[16], | ||
| const unsigned char *input, | ||
| unsigned char *output ); | ||
|
|
||
| /** | ||
| * \brief CAMELLIA-CFB128 buffer encryption/decryption | ||
| * | ||
| * Note: Due to the nature of CFB you should use the same key schedule for | ||
| * both encryption and decryption. So a context initialized with | ||
| * camellia_setkey_enc() for both CAMELLIA_ENCRYPT and CAMELLIE_DECRYPT. | ||
| * | ||
| * \param ctx CAMELLIA context | ||
| * \param mode CAMELLIA_ENCRYPT or CAMELLIA_DECRYPT | ||
| * \param length length of the input data | ||
| * \param iv_off offset in IV (updated after use) | ||
| * \param iv initialization vector (updated after use) | ||
| * \param input buffer holding the input data | ||
| * \param output buffer holding the output data | ||
| * | ||
| * \return 0 if successful, or POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH | ||
| */ | ||
| int camellia_crypt_cfb128( camellia_context *ctx, | ||
| int mode, | ||
| size_t length, | ||
| size_t *iv_off, | ||
| unsigned char iv[16], | ||
| const unsigned char *input, | ||
| unsigned char *output ); | ||
|
|
||
| /** | ||
| * \brief CAMELLIA-CTR buffer encryption/decryption | ||
| * | ||
| * Warning: You have to keep the maximum use of your counter in mind! | ||
| * | ||
| * Note: Due to the nature of CTR you should use the same key schedule for | ||
| * both encryption and decryption. So a context initialized with | ||
| * camellia_setkey_enc() for both CAMELLIA_ENCRYPT and CAMELLIA_DECRYPT. | ||
| * | ||
| * \param length The length of the data | ||
| * \param nc_off The offset in the current stream_block (for resuming | ||
| * within current cipher stream). The offset pointer to | ||
| * should be 0 at the start of a stream. | ||
| * \param nonce_counter The 128-bit nonce and counter. | ||
| * \param stream_block The saved stream-block for resuming. Is overwritten | ||
| * by the function. | ||
| * \param input The input data stream | ||
| * \param output The output data stream | ||
| * | ||
| * \return 0 if successful | ||
| */ | ||
| int camellia_crypt_ctr( camellia_context *ctx, | ||
| size_t length, | ||
| size_t *nc_off, | ||
| unsigned char nonce_counter[16], | ||
| unsigned char stream_block[16], | ||
| const unsigned char *input, | ||
| unsigned char *output ); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #else /* POLARSSL_CAMELLIA_ALT */ | ||
| #include "camellia_alt.h" | ||
| #endif /* POLARSSL_CAMELLIA_ALT */ | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| /** | ||
| * \brief Checkup routine | ||
| * | ||
| * \return 0 if successful, or 1 if the test failed | ||
| */ | ||
| int camellia_self_test( int verbose ); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif /* camellia.h */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /** | ||
| * \file certs.h | ||
| * | ||
| * \brief Sample certificates and DHM parameters for testing | ||
| * | ||
| * Copyright (C) 2006-2010, 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_CERTS_H | ||
| #define POLARSSL_CERTS_H | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| extern const char test_ca_crt[]; | ||
| extern const char test_ca_key[]; | ||
| extern const char test_ca_pwd[]; | ||
| extern const char test_srv_crt[]; | ||
| extern const char test_srv_key[]; | ||
| extern const char test_cli_crt[]; | ||
| extern const char test_cli_key[]; | ||
| extern const char test_dhm_params[]; | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif /* certs.h */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| /** | ||
| * \file cipher_wrap.h | ||
| * | ||
| * \brief Cipher wrappers. | ||
| * | ||
| * \author Adriaan de Jong <dejong@fox-it.com> | ||
| * | ||
| * Copyright (C) 2006-2012, 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_CIPHER_WRAP_H | ||
| #define POLARSSL_CIPHER_WRAP_H | ||
|
|
||
| #include "config.h" | ||
| #include "cipher.h" | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| #if defined(POLARSSL_AES_C) | ||
|
|
||
| extern const cipher_info_t aes_128_cbc_info; | ||
| extern const cipher_info_t aes_192_cbc_info; | ||
| extern const cipher_info_t aes_256_cbc_info; | ||
|
|
||
| #if defined(POLARSSL_CIPHER_MODE_CFB) | ||
| extern const cipher_info_t aes_128_cfb128_info; | ||
| extern const cipher_info_t aes_192_cfb128_info; | ||
| extern const cipher_info_t aes_256_cfb128_info; | ||
| #endif /* POLARSSL_CIPHER_MODE_CFB */ | ||
|
|
||
| #if defined(POLARSSL_CIPHER_MODE_CTR) | ||
| extern const cipher_info_t aes_128_ctr_info; | ||
| extern const cipher_info_t aes_192_ctr_info; | ||
| extern const cipher_info_t aes_256_ctr_info; | ||
| #endif /* POLARSSL_CIPHER_MODE_CTR */ | ||
|
|
||
| #endif /* defined(POLARSSL_AES_C) */ | ||
|
|
||
| #if defined(POLARSSL_CAMELLIA_C) | ||
|
|
||
| extern const cipher_info_t camellia_128_cbc_info; | ||
| extern const cipher_info_t camellia_192_cbc_info; | ||
| extern const cipher_info_t camellia_256_cbc_info; | ||
|
|
||
| #if defined(POLARSSL_CIPHER_MODE_CFB) | ||
| extern const cipher_info_t camellia_128_cfb128_info; | ||
| extern const cipher_info_t camellia_192_cfb128_info; | ||
| extern const cipher_info_t camellia_256_cfb128_info; | ||
| #endif /* POLARSSL_CIPHER_MODE_CFB */ | ||
|
|
||
| #if defined(POLARSSL_CIPHER_MODE_CTR) | ||
| extern const cipher_info_t camellia_128_ctr_info; | ||
| extern const cipher_info_t camellia_192_ctr_info; | ||
| extern const cipher_info_t camellia_256_ctr_info; | ||
| #endif /* POLARSSL_CIPHER_MODE_CTR */ | ||
|
|
||
| #endif /* defined(POLARSSL_CAMELLIA_C) */ | ||
|
|
||
| #if defined(POLARSSL_DES_C) | ||
|
|
||
| extern const cipher_info_t des_cbc_info; | ||
| extern const cipher_info_t des_ede_cbc_info; | ||
| extern const cipher_info_t des_ede3_cbc_info; | ||
|
|
||
| #endif /* defined(POLARSSL_DES_C) */ | ||
|
|
||
| #if defined(POLARSSL_BLOWFISH_C) | ||
| extern const cipher_info_t blowfish_cbc_info; | ||
|
|
||
| #if defined(POLARSSL_CIPHER_MODE_CFB) | ||
| extern const cipher_info_t blowfish_cfb64_info; | ||
| #endif /* POLARSSL_CIPHER_MODE_CFB */ | ||
|
|
||
| #if defined(POLARSSL_CIPHER_MODE_CTR) | ||
| extern const cipher_info_t blowfish_ctr_info; | ||
| #endif /* POLARSSL_CIPHER_MODE_CTR */ | ||
| #endif /* defined(POLARSSL_BLOWFISH_C) */ | ||
|
|
||
| #if defined(POLARSSL_CIPHER_NULL_CIPHER) | ||
| extern const cipher_info_t null_cipher_info; | ||
| #endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */ | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif /* POLARSSL_CIPHER_WRAP_H */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,231 @@ | ||
| /** | ||
| * \file ctr_drbg.h | ||
| * | ||
| * \brief CTR_DRBG based on AES-256 (NIST SP 800-90) | ||
| * | ||
| * Copyright (C) 2006-2013, 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_CTR_DRBG_H | ||
| #define POLARSSL_CTR_DRBG_H | ||
|
|
||
| #include <string.h> | ||
|
|
||
| #include "aes.h" | ||
|
|
||
| #define POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034 /**< The entropy source failed. */ | ||
| #define POLARSSL_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036 /**< Too many random requested in single call. */ | ||
| #define POLARSSL_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038 /**< Input too large (Entropy + additional). */ | ||
| #define POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A /**< Read/write error in file. */ | ||
|
|
||
| #define CTR_DRBG_BLOCKSIZE 16 /**< Block size used by the cipher */ | ||
| #define CTR_DRBG_KEYSIZE 32 /**< Key size used by the cipher */ | ||
| #define CTR_DRBG_KEYBITS ( CTR_DRBG_KEYSIZE * 8 ) | ||
| #define CTR_DRBG_SEEDLEN ( CTR_DRBG_KEYSIZE + CTR_DRBG_BLOCKSIZE ) | ||
| /**< The seed length (counter + AES key) */ | ||
|
|
||
| #if !defined(POLARSSL_CONFIG_OPTIONS) | ||
| #define CTR_DRBG_ENTROPY_LEN 48 /**< Amount of entropy used per seed by default */ | ||
| #define CTR_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */ | ||
| #define CTR_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */ | ||
| #define CTR_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */ | ||
| #define CTR_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */ | ||
| #endif /* !POLARSSL_CONFIG_OPTIONS */ | ||
|
|
||
| #define CTR_DRBG_PR_OFF 0 /**< No prediction resistance */ | ||
| #define CTR_DRBG_PR_ON 1 /**< Prediction resistance enabled */ | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| /** | ||
| * \brief CTR_DRBG context structure | ||
| */ | ||
| typedef struct | ||
| { | ||
| unsigned char counter[16]; /*!< counter (V) */ | ||
| int reseed_counter; /*!< reseed counter */ | ||
| int prediction_resistance; /*!< enable prediction resistance (Automatic | ||
| reseed before every random generation) */ | ||
| size_t entropy_len; /*!< amount of entropy grabbed on each (re)seed */ | ||
| int reseed_interval; /*!< reseed interval */ | ||
|
|
||
| aes_context aes_ctx; /*!< AES context */ | ||
|
|
||
| /* | ||
| * Callbacks (Entropy) | ||
| */ | ||
| int (*f_entropy)(void *, unsigned char *, size_t); | ||
|
|
||
| void *p_entropy; /*!< context for the entropy function */ | ||
| } | ||
| ctr_drbg_context; | ||
|
|
||
| /** | ||
| * \brief CTR_DRBG initialization | ||
| * | ||
| * Note: Personalization data can be provided in addition to the more generic | ||
| * entropy source to make this instantiation as unique as possible. | ||
| * | ||
| * \param ctx CTR_DRBG context to be initialized | ||
| * \param f_entropy Entropy callback (p_entropy, buffer to fill, buffer | ||
| * length) | ||
| * \param p_entropy Entropy context | ||
| * \param custom Personalization data (Device specific identifiers) | ||
| * (Can be NULL) | ||
| * \param len Length of personalization data | ||
| * | ||
| * \return 0 if successful, or | ||
| * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED | ||
| */ | ||
| int ctr_drbg_init( ctr_drbg_context *ctx, | ||
| int (*f_entropy)(void *, unsigned char *, size_t), | ||
| void *p_entropy, | ||
| const unsigned char *custom, | ||
| size_t len ); | ||
|
|
||
| /** | ||
| * \brief Enable / disable prediction resistance (Default: Off) | ||
| * | ||
| * Note: If enabled, entropy is used for ctx->entropy_len before each call! | ||
| * Only use this if you have ample supply of good entropy! | ||
| * | ||
| * \param ctx CTR_DRBG context | ||
| * \param resistance CTR_DRBG_PR_ON or CTR_DRBG_PR_OFF | ||
| */ | ||
| void ctr_drbg_set_prediction_resistance( ctr_drbg_context *ctx, | ||
| int resistance ); | ||
|
|
||
| /** | ||
| * \brief Set the amount of entropy grabbed on each (re)seed | ||
| * (Default: CTR_DRBG_ENTROPY_LEN) | ||
| * | ||
| * \param ctx CTR_DRBG context | ||
| * \param len Amount of entropy to grab | ||
| */ | ||
| void ctr_drbg_set_entropy_len( ctr_drbg_context *ctx, | ||
| size_t len ); | ||
|
|
||
| /** | ||
| * \brief Set the reseed interval | ||
| * (Default: CTR_DRBG_RESEED_INTERVAL) | ||
| * | ||
| * \param ctx CTR_DRBG context | ||
| * \param interval Reseed interval | ||
| */ | ||
| void ctr_drbg_set_reseed_interval( ctr_drbg_context *ctx, | ||
| int interval ); | ||
|
|
||
| /** | ||
| * \brief CTR_DRBG reseeding (extracts data from entropy source) | ||
| * | ||
| * \param ctx CTR_DRBG context | ||
| * \param additional Additional data to add to state (Can be NULL) | ||
| * \param len Length of additional data | ||
| * | ||
| * \return 0 if successful, or | ||
| * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED | ||
| */ | ||
| int ctr_drbg_reseed( ctr_drbg_context *ctx, | ||
| const unsigned char *additional, size_t len ); | ||
|
|
||
| /** | ||
| * \brief CTR_DRBG update state | ||
| * | ||
| * \param ctx CTR_DRBG context | ||
| * \param additional Additional data to update state with | ||
| * \param add_len Length of additional data | ||
| */ | ||
| void ctr_drbg_update( ctr_drbg_context *ctx, | ||
| const unsigned char *additional, size_t add_len ); | ||
|
|
||
| /** | ||
| * \brief CTR_DRBG generate random with additional update input | ||
| * | ||
| * Note: Automatically reseeds if reseed_counter is reached. | ||
| * | ||
| * \param p_rng CTR_DRBG context | ||
| * \param output Buffer to fill | ||
| * \param output_len Length of the buffer | ||
| * \param additional Additional data to update with (Can be NULL) | ||
| * \param add_len Length of additional data | ||
| * | ||
| * \return 0 if successful, or | ||
| * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED, or | ||
| * POLARSSL_ERR_CTR_DRBG_REQUEST_TOO_BIG | ||
| */ | ||
| int ctr_drbg_random_with_add( void *p_rng, | ||
| unsigned char *output, size_t output_len, | ||
| const unsigned char *additional, size_t add_len ); | ||
|
|
||
| /** | ||
| * \brief CTR_DRBG generate random | ||
| * | ||
| * Note: Automatically reseeds if reseed_counter is reached. | ||
| * | ||
| * \param p_rng CTR_DRBG context | ||
| * \param output Buffer to fill | ||
| * \param output_len Length of the buffer | ||
| * | ||
| * \return 0 if successful, or | ||
| * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED, or | ||
| * POLARSSL_ERR_CTR_DRBG_REQUEST_TOO_BIG | ||
| */ | ||
| int ctr_drbg_random( void *p_rng, | ||
| unsigned char *output, size_t output_len ); | ||
|
|
||
| #if defined(POLARSSL_FS_IO) | ||
| /** | ||
| * \brief Write a seed file | ||
| * | ||
| * \param path Name of the file | ||
| * | ||
| * \return 0 if successful, 1 on file error, or | ||
| * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED | ||
| */ | ||
| int ctr_drbg_write_seed_file( ctr_drbg_context *ctx, const char *path ); | ||
|
|
||
| /** | ||
| * \brief Read and update a seed file. Seed is added to this | ||
| * instance | ||
| * | ||
| * \param path Name of the file | ||
| * | ||
| * \return 0 if successful, 1 on file error, | ||
| * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or | ||
| * POLARSSL_ERR_CTR_DRBG_INPUT_TOO_BIG | ||
| */ | ||
| int ctr_drbg_update_seed_file( ctr_drbg_context *ctx, const char *path ); | ||
| #endif | ||
|
|
||
| /** | ||
| * \brief Checkup routine | ||
| * | ||
| * \return 0 if successful, or 1 if the test failed | ||
| */ | ||
| int ctr_drbg_self_test( int verbose ); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif /* ctr_drbg.h */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| /** | ||
| * \file debug.h | ||
| * | ||
| * \brief Debug functions | ||
| * | ||
| * Copyright (C) 2006-2011, 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_DEBUG_H | ||
| #define POLARSSL_DEBUG_H | ||
|
|
||
| #include "config.h" | ||
| #include "ssl.h" | ||
|
|
||
| #if defined(POLARSSL_DEBUG_C) | ||
|
|
||
| #define SSL_DEBUG_MSG( level, args ) \ | ||
| debug_print_msg( ssl, level, __FILE__, __LINE__, debug_fmt args ); | ||
|
|
||
| #define SSL_DEBUG_RET( level, text, ret ) \ | ||
| debug_print_ret( ssl, level, __FILE__, __LINE__, text, ret ); | ||
|
|
||
| #define SSL_DEBUG_BUF( level, text, buf, len ) \ | ||
| debug_print_buf( ssl, level, __FILE__, __LINE__, text, buf, len ); | ||
|
|
||
| #define SSL_DEBUG_MPI( level, text, X ) \ | ||
| debug_print_mpi( ssl, level, __FILE__, __LINE__, text, X ); | ||
|
|
||
| #define SSL_DEBUG_CRT( level, text, crt ) \ | ||
| debug_print_crt( ssl, level, __FILE__, __LINE__, text, crt ); | ||
|
|
||
| #else | ||
|
|
||
| #define SSL_DEBUG_MSG( level, args ) do { } while( 0 ) | ||
| #define SSL_DEBUG_RET( level, text, ret ) do { } while( 0 ) | ||
| #define SSL_DEBUG_BUF( level, text, buf, len ) do { } while( 0 ) | ||
| #define SSL_DEBUG_MPI( level, text, X ) do { } while( 0 ) | ||
| #define SSL_DEBUG_CRT( level, text, crt ) do { } while( 0 ) | ||
|
|
||
| #endif | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| char *debug_fmt( const char *format, ... ); | ||
|
|
||
| void debug_print_msg( const ssl_context *ssl, int level, | ||
| const char *file, int line, const char *text ); | ||
|
|
||
| void debug_print_ret( const ssl_context *ssl, int level, | ||
| const char *file, int line, | ||
| const char *text, int ret ); | ||
|
|
||
| void debug_print_buf( const ssl_context *ssl, int level, | ||
| const char *file, int line, const char *text, | ||
| unsigned char *buf, size_t len ); | ||
|
|
||
| void debug_print_mpi( const ssl_context *ssl, int level, | ||
| const char *file, int line, | ||
| const char *text, const mpi *X ); | ||
|
|
||
| void debug_print_crt( const ssl_context *ssl, int level, | ||
| const char *file, int line, | ||
| const char *text, const x509_cert *crt ); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif /* debug.h */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,252 @@ | ||
| /** | ||
| * \file des.h | ||
| * | ||
| * \brief DES block cipher | ||
| * | ||
| * Copyright (C) 2006-2013, 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_DES_H | ||
| #define POLARSSL_DES_H | ||
|
|
||
| #include "config.h" | ||
|
|
||
| #include <string.h> | ||
|
|
||
| #ifdef _MSC_VER | ||
| #include <basetsd.h> | ||
| typedef UINT32 uint32_t; | ||
| #else | ||
| #include <inttypes.h> | ||
| #endif | ||
|
|
||
| #define DES_ENCRYPT 1 | ||
| #define DES_DECRYPT 0 | ||
|
|
||
| #define POLARSSL_ERR_DES_INVALID_INPUT_LENGTH -0x0032 /**< The data input has an invalid length. */ | ||
|
|
||
| #define DES_KEY_SIZE 8 | ||
|
|
||
| #if !defined(POLARSSL_DES_ALT) | ||
| // Regular implementation | ||
| // | ||
|
|
||
| /** | ||
| * \brief DES context structure | ||
| */ | ||
| typedef struct | ||
| { | ||
| int mode; /*!< encrypt/decrypt */ | ||
| uint32_t sk[32]; /*!< DES subkeys */ | ||
| } | ||
| des_context; | ||
|
|
||
| /** | ||
| * \brief Triple-DES context structure | ||
| */ | ||
| typedef struct | ||
| { | ||
| int mode; /*!< encrypt/decrypt */ | ||
| uint32_t sk[96]; /*!< 3DES subkeys */ | ||
| } | ||
| des3_context; | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| /** | ||
| * \brief Set key parity on the given key to odd. | ||
| * | ||
| * DES keys are 56 bits long, but each byte is padded with | ||
| * a parity bit to allow verification. | ||
| * | ||
| * \param key 8-byte secret key | ||
| */ | ||
| void des_key_set_parity( unsigned char key[DES_KEY_SIZE] ); | ||
|
|
||
| /** | ||
| * \brief Check that key parity on the given key is odd. | ||
| * | ||
| * DES keys are 56 bits long, but each byte is padded with | ||
| * a parity bit to allow verification. | ||
| * | ||
| * \param key 8-byte secret key | ||
| * | ||
| * \return 0 is parity was ok, 1 if parity was not correct. | ||
| */ | ||
| int des_key_check_key_parity( const unsigned char key[DES_KEY_SIZE] ); | ||
|
|
||
| /** | ||
| * \brief Check that key is not a weak or semi-weak DES key | ||
| * | ||
| * \param key 8-byte secret key | ||
| * | ||
| * \return 0 if no weak key was found, 1 if a weak key was identified. | ||
| */ | ||
| int des_key_check_weak( const unsigned char key[DES_KEY_SIZE] ); | ||
|
|
||
| /** | ||
| * \brief DES key schedule (56-bit, encryption) | ||
| * | ||
| * \param ctx DES context to be initialized | ||
| * \param key 8-byte secret key | ||
| * | ||
| * \return 0 | ||
| */ | ||
| int des_setkey_enc( des_context *ctx, const unsigned char key[DES_KEY_SIZE] ); | ||
|
|
||
| /** | ||
| * \brief DES key schedule (56-bit, decryption) | ||
| * | ||
| * \param ctx DES context to be initialized | ||
| * \param key 8-byte secret key | ||
| * | ||
| * \return 0 | ||
| */ | ||
| int des_setkey_dec( des_context *ctx, const unsigned char key[DES_KEY_SIZE] ); | ||
|
|
||
| /** | ||
| * \brief Triple-DES key schedule (112-bit, encryption) | ||
| * | ||
| * \param ctx 3DES context to be initialized | ||
| * \param key 16-byte secret key | ||
| * | ||
| * \return 0 | ||
| */ | ||
| int des3_set2key_enc( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 2] ); | ||
|
|
||
| /** | ||
| * \brief Triple-DES key schedule (112-bit, decryption) | ||
| * | ||
| * \param ctx 3DES context to be initialized | ||
| * \param key 16-byte secret key | ||
| * | ||
| * \return 0 | ||
| */ | ||
| int des3_set2key_dec( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 2] ); | ||
|
|
||
| /** | ||
| * \brief Triple-DES key schedule (168-bit, encryption) | ||
| * | ||
| * \param ctx 3DES context to be initialized | ||
| * \param key 24-byte secret key | ||
| * | ||
| * \return 0 | ||
| */ | ||
| int des3_set3key_enc( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 3] ); | ||
|
|
||
| /** | ||
| * \brief Triple-DES key schedule (168-bit, decryption) | ||
| * | ||
| * \param ctx 3DES context to be initialized | ||
| * \param key 24-byte secret key | ||
| * | ||
| * \return 0 | ||
| */ | ||
| int des3_set3key_dec( des3_context *ctx, const unsigned char key[DES_KEY_SIZE * 3] ); | ||
|
|
||
| /** | ||
| * \brief DES-ECB block encryption/decryption | ||
| * | ||
| * \param ctx DES context | ||
| * \param input 64-bit input block | ||
| * \param output 64-bit output block | ||
| * | ||
| * \return 0 if successful | ||
| */ | ||
| int des_crypt_ecb( des_context *ctx, | ||
| const unsigned char input[8], | ||
| unsigned char output[8] ); | ||
|
|
||
| /** | ||
| * \brief DES-CBC buffer encryption/decryption | ||
| * | ||
| * \param ctx DES context | ||
| * \param mode DES_ENCRYPT or DES_DECRYPT | ||
| * \param length length of the input data | ||
| * \param iv initialization vector (updated after use) | ||
| * \param input buffer holding the input data | ||
| * \param output buffer holding the output data | ||
| */ | ||
| int des_crypt_cbc( des_context *ctx, | ||
| int mode, | ||
| size_t length, | ||
| unsigned char iv[8], | ||
| const unsigned char *input, | ||
| unsigned char *output ); | ||
|
|
||
| /** | ||
| * \brief 3DES-ECB block encryption/decryption | ||
| * | ||
| * \param ctx 3DES context | ||
| * \param input 64-bit input block | ||
| * \param output 64-bit output block | ||
| * | ||
| * \return 0 if successful | ||
| */ | ||
| int des3_crypt_ecb( des3_context *ctx, | ||
| const unsigned char input[8], | ||
| unsigned char output[8] ); | ||
|
|
||
| /** | ||
| * \brief 3DES-CBC buffer encryption/decryption | ||
| * | ||
| * \param ctx 3DES context | ||
| * \param mode DES_ENCRYPT or DES_DECRYPT | ||
| * \param length length of the input data | ||
| * \param iv initialization vector (updated after use) | ||
| * \param input buffer holding the input data | ||
| * \param output buffer holding the output data | ||
| * | ||
| * \return 0 if successful, or POLARSSL_ERR_DES_INVALID_INPUT_LENGTH | ||
| */ | ||
| int des3_crypt_cbc( des3_context *ctx, | ||
| int mode, | ||
| size_t length, | ||
| unsigned char iv[8], | ||
| const unsigned char *input, | ||
| unsigned char *output ); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #else /* POLARSSL_DES_ALT */ | ||
| #include "des_alt.h" | ||
| #endif /* POLARSSL_DES_ALT */ | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| /** | ||
| * \brief Checkup routine | ||
| * | ||
| * \return 0 if successful, or 1 if the test failed | ||
| */ | ||
| int des_self_test( int verbose ); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif /* des.h */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,244 @@ | ||
| /** | ||
| * \file dhm.h | ||
| * | ||
| * \brief Diffie-Hellman-Merkle key exchange | ||
| * | ||
| * Copyright (C) 2006-2010, 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_DHM_H | ||
| #define POLARSSL_DHM_H | ||
|
|
||
| #include "bignum.h" | ||
|
|
||
| /* | ||
| * DHM Error codes | ||
| */ | ||
| #define POLARSSL_ERR_DHM_BAD_INPUT_DATA -0x3080 /**< Bad input parameters to function. */ | ||
| #define POLARSSL_ERR_DHM_READ_PARAMS_FAILED -0x3100 /**< Reading of the DHM parameters failed. */ | ||
| #define POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED -0x3180 /**< Making of the DHM parameters failed. */ | ||
| #define POLARSSL_ERR_DHM_READ_PUBLIC_FAILED -0x3200 /**< Reading of the public values failed. */ | ||
| #define POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED -0x3280 /**< Making of the public value failed. */ | ||
| #define POLARSSL_ERR_DHM_CALC_SECRET_FAILED -0x3300 /**< Calculation of the DHM secret failed. */ | ||
|
|
||
| /** | ||
| * RFC 3526 defines a number of standardized Diffie-Hellman groups | ||
| * for IKE. | ||
| * RFC 5114 defines a number of standardized Diffie-Hellman groups | ||
| * that can be used. | ||
| * | ||
| * Some are included here for convenience. | ||
| * | ||
| * Included are: | ||
| * RFC 3526 3. 2048-bit MODP Group | ||
| * RFC 3526 4. 3072-bit MODP Group | ||
| * RFC 5114 2.1. 1024-bit MODP Group with 160-bit Prime Order Subgroup | ||
| * RFC 5114 2.2. 2048-bit MODP Group with 224-bit Prime Order Subgroup | ||
| */ | ||
| #define POLARSSL_DHM_RFC3526_MODP_2048_P \ | ||
| "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \ | ||
| "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \ | ||
| "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \ | ||
| "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \ | ||
| "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" \ | ||
| "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" \ | ||
| "83655D23DCA3AD961C62F356208552BB9ED529077096966D" \ | ||
| "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" \ | ||
| "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" \ | ||
| "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" \ | ||
| "15728E5A8AACAA68FFFFFFFFFFFFFFFF" | ||
|
|
||
| #define POLARSSL_DHM_RFC3526_MODP_2048_G "02" | ||
|
|
||
| #define POLARSSL_DHM_RFC3526_MODP_3072_P \ | ||
| "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1" \ | ||
| "29024E088A67CC74020BBEA63B139B22514A08798E3404DD" \ | ||
| "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245" \ | ||
| "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" \ | ||
| "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D" \ | ||
| "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F" \ | ||
| "83655D23DCA3AD961C62F356208552BB9ED529077096966D" \ | ||
| "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" \ | ||
| "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9" \ | ||
| "DE2BCBF6955817183995497CEA956AE515D2261898FA0510" \ | ||
| "15728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64" \ | ||
| "ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7" \ | ||
| "ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6B" \ | ||
| "F12FFA06D98A0864D87602733EC86A64521F2B18177B200C" \ | ||
| "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB31" \ | ||
| "43DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF" | ||
|
|
||
| #define POLARSSL_DHM_RFC3526_MODP_3072_G "02" | ||
|
|
||
| #define POLARSSL_DHM_RFC5114_MODP_1024_P \ | ||
| "B10B8F96A080E01DDE92DE5EAE5D54EC52C99FBCFB06A3C6" \ | ||
| "9A6A9DCA52D23B616073E28675A23D189838EF1E2EE652C0" \ | ||
| "13ECB4AEA906112324975C3CD49B83BFACCBDD7D90C4BD70" \ | ||
| "98488E9C219A73724EFFD6FAE5644738FAA31A4FF55BCCC0" \ | ||
| "A151AF5F0DC8B4BD45BF37DF365C1A65E68CFDA76D4DA708" \ | ||
| "DF1FB2BC2E4A4371" | ||
|
|
||
| #define POLARSSL_DHM_RFC5114_MODP_1024_G \ | ||
| "A4D1CBD5C3FD34126765A442EFB99905F8104DD258AC507F" \ | ||
| "D6406CFF14266D31266FEA1E5C41564B777E690F5504F213" \ | ||
| "160217B4B01B886A5E91547F9E2749F4D7FBD7D3B9A92EE1" \ | ||
| "909D0D2263F80A76A6A24C087A091F531DBF0A0169B6A28A" \ | ||
| "D662A4D18E73AFA32D779D5918D08BC8858F4DCEF97C2A24" \ | ||
| "855E6EEB22B3B2E5" | ||
|
|
||
| #define POLARSSL_DHM_RFC5114_MODP_2048_P \ | ||
| "AD107E1E9123A9D0D660FAA79559C51FA20D64E5683B9FD1" \ | ||
| "B54B1597B61D0A75E6FA141DF95A56DBAF9A3C407BA1DF15" \ | ||
| "EB3D688A309C180E1DE6B85A1274A0A66D3F8152AD6AC212" \ | ||
| "9037C9EDEFDA4DF8D91E8FEF55B7394B7AD5B7D0B6C12207" \ | ||
| "C9F98D11ED34DBF6C6BA0B2C8BBC27BE6A00E0A0B9C49708" \ | ||
| "B3BF8A317091883681286130BC8985DB1602E714415D9330" \ | ||
| "278273C7DE31EFDC7310F7121FD5A07415987D9ADC0A486D" \ | ||
| "CDF93ACC44328387315D75E198C641A480CD86A1B9E587E8" \ | ||
| "BE60E69CC928B2B9C52172E413042E9B23F10B0E16E79763" \ | ||
| "C9B53DCF4BA80A29E3FB73C16B8E75B97EF363E2FFA31F71" \ | ||
| "CF9DE5384E71B81C0AC4DFFE0C10E64F" | ||
|
|
||
| #define POLARSSL_DHM_RFC5114_MODP_2048_G \ | ||
| "AC4032EF4F2D9AE39DF30B5C8FFDAC506CDEBE7B89998CAF"\ | ||
| "74866A08CFE4FFE3A6824A4E10B9A6F0DD921F01A70C4AFA"\ | ||
| "AB739D7700C29F52C57DB17C620A8652BE5E9001A8D66AD7"\ | ||
| "C17669101999024AF4D027275AC1348BB8A762D0521BC98A"\ | ||
| "E247150422EA1ED409939D54DA7460CDB5F6C6B250717CBE"\ | ||
| "F180EB34118E98D119529A45D6F834566E3025E316A330EF"\ | ||
| "BB77A86F0C1AB15B051AE3D428C8F8ACB70A8137150B8EEB"\ | ||
| "10E183EDD19963DDD9E263E4770589EF6AA21E7F5F2FF381"\ | ||
| "B539CCE3409D13CD566AFBB48D6C019181E1BCFE94B30269"\ | ||
| "EDFE72FE9B6AA4BD7B5A0F1C71CFFF4C19C418E1F6EC0179"\ | ||
| "81BC087F2A7065B384B890D3191F2BFA" | ||
|
|
||
| /** | ||
| * \brief DHM context structure | ||
| */ | ||
| typedef struct | ||
| { | ||
| size_t len; /*!< size(P) in chars */ | ||
| mpi P; /*!< prime modulus */ | ||
| mpi G; /*!< generator */ | ||
| mpi X; /*!< secret value */ | ||
| mpi GX; /*!< self = G^X mod P */ | ||
| mpi GY; /*!< peer = G^Y mod P */ | ||
| mpi K; /*!< key = GY^X mod P */ | ||
| mpi RP; /*!< cached R^2 mod P */ | ||
| } | ||
| dhm_context; | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| /** | ||
| * \brief Parse the ServerKeyExchange parameters | ||
| * | ||
| * \param ctx DHM context | ||
| * \param p &(start of input buffer) | ||
| * \param end end of buffer | ||
| * | ||
| * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code | ||
| */ | ||
| int dhm_read_params( dhm_context *ctx, | ||
| unsigned char **p, | ||
| const unsigned char *end ); | ||
|
|
||
| /** | ||
| * \brief Setup and write the ServerKeyExchange parameters | ||
| * | ||
| * \param ctx DHM context | ||
| * \param x_size private value size in bytes | ||
| * \param output destination buffer | ||
| * \param olen number of chars written | ||
| * \param f_rng RNG function | ||
| * \param p_rng RNG parameter | ||
| * | ||
| * \note This function assumes that ctx->P and ctx->G | ||
| * have already been properly set (for example | ||
| * using mpi_read_string or mpi_read_binary). | ||
| * | ||
| * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code | ||
| */ | ||
| int dhm_make_params( dhm_context *ctx, int x_size, | ||
| unsigned char *output, size_t *olen, | ||
| int (*f_rng)(void *, unsigned char *, size_t), | ||
| void *p_rng ); | ||
|
|
||
| /** | ||
| * \brief Import the peer's public value G^Y | ||
| * | ||
| * \param ctx DHM context | ||
| * \param input input buffer | ||
| * \param ilen size of buffer | ||
| * | ||
| * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code | ||
| */ | ||
| int dhm_read_public( dhm_context *ctx, | ||
| const unsigned char *input, size_t ilen ); | ||
|
|
||
| /** | ||
| * \brief Create own private value X and export G^X | ||
| * | ||
| * \param ctx DHM context | ||
| * \param x_size private value size in bytes | ||
| * \param output destination buffer | ||
| * \param olen must be equal to ctx->P.len | ||
| * \param f_rng RNG function | ||
| * \param p_rng RNG parameter | ||
| * | ||
| * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code | ||
| */ | ||
| int dhm_make_public( dhm_context *ctx, int x_size, | ||
| unsigned char *output, size_t olen, | ||
| int (*f_rng)(void *, unsigned char *, size_t), | ||
| void *p_rng ); | ||
|
|
||
| /** | ||
| * \brief Derive and export the shared secret (G^Y)^X mod P | ||
| * | ||
| * \param ctx DHM context | ||
| * \param output destination buffer | ||
| * \param olen number of chars written | ||
| * | ||
| * \return 0 if successful, or an POLARSSL_ERR_DHM_XXX error code | ||
| */ | ||
| int dhm_calc_secret( dhm_context *ctx, | ||
| unsigned char *output, size_t *olen ); | ||
|
|
||
| /** | ||
| * \brief Free the components of a DHM key | ||
| */ | ||
| void dhm_free( dhm_context *ctx ); | ||
|
|
||
| /** | ||
| * \brief Checkup routine | ||
| * | ||
| * \return 0 if successful, or 1 if the test failed | ||
| */ | ||
| int dhm_self_test( int verbose ); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| /** | ||
| * \file entropy.h | ||
| * | ||
| * \brief Entropy accumulator implementation | ||
| * | ||
| * Copyright (C) 2006-2013, 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_ENTROPY_H | ||
| #define POLARSSL_ENTROPY_H | ||
|
|
||
| #include <string.h> | ||
|
|
||
| #include "config.h" | ||
|
|
||
| #include "sha4.h" | ||
| #if defined(POLARSSL_HAVEGE_C) | ||
| #include "havege.h" | ||
| #endif | ||
|
|
||
| #define POLARSSL_ERR_ENTROPY_SOURCE_FAILED -0x003C /**< Critical entropy source failure. */ | ||
| #define POLARSSL_ERR_ENTROPY_MAX_SOURCES -0x003E /**< No more sources can be added. */ | ||
| #define POLARSSL_ERR_ENTROPY_NO_SOURCES_DEFINED -0x0040 /**< No sources have been added to poll. */ | ||
|
|
||
| #if !defined(POLARSSL_CONFIG_OPTIONS) | ||
| #define ENTROPY_MAX_SOURCES 20 /**< Maximum number of sources supported */ | ||
| #define ENTROPY_MAX_GATHER 128 /**< Maximum amount requested from entropy sources */ | ||
| #endif /* !POLARSSL_CONFIG_OPTIONS */ | ||
|
|
||
| #define ENTROPY_BLOCK_SIZE 64 /**< Block size of entropy accumulator (SHA-512) */ | ||
|
|
||
| #define ENTROPY_SOURCE_MANUAL ENTROPY_MAX_SOURCES | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| /** | ||
| * \brief Entropy poll callback pointer | ||
| * | ||
| * \param data Callback-specific data pointer | ||
| * \param output Data to fill | ||
| * \param len Maximum size to provide | ||
| * \param olen The actual amount of bytes put into the buffer (Can be 0) | ||
| * | ||
| * \return 0 if no critical failures occurred, | ||
| * POLARSSL_ERR_ENTROPY_SOURCE_FAILED otherwise | ||
| */ | ||
| typedef int (*f_source_ptr)(void *, unsigned char *, size_t, size_t *); | ||
|
|
||
| /** | ||
| * \brief Entropy source state | ||
| */ | ||
| typedef struct | ||
| { | ||
| f_source_ptr f_source; /**< The entropy source callback */ | ||
| void * p_source; /**< The callback data pointer */ | ||
| size_t size; /**< Amount received */ | ||
| size_t threshold; /**< Minimum level required before release */ | ||
| } | ||
| source_state; | ||
|
|
||
| /** | ||
| * \brief Entropy context structure | ||
| */ | ||
| typedef struct | ||
| { | ||
| sha4_context accumulator; | ||
| int source_count; | ||
| source_state source[ENTROPY_MAX_SOURCES]; | ||
| #if defined(POLARSSL_HAVEGE_C) | ||
| havege_state havege_data; | ||
| #endif | ||
| } | ||
| entropy_context; | ||
|
|
||
| /** | ||
| * \brief Initialize the context | ||
| * | ||
| * \param ctx Entropy context to initialize | ||
| */ | ||
| void entropy_init( entropy_context *ctx ); | ||
|
|
||
| /** | ||
| * \brief Adds an entropy source to poll | ||
| * | ||
| * \param ctx Entropy context | ||
| * \param f_source Entropy function | ||
| * \param p_source Function data | ||
| * \param threshold Minimum required from source before entropy is released | ||
| * ( with entropy_func() ) | ||
| * | ||
| * \return 0 if successful or POLARSSL_ERR_ENTROPY_MAX_SOURCES | ||
| */ | ||
| int entropy_add_source( entropy_context *ctx, | ||
| f_source_ptr f_source, void *p_source, | ||
| size_t threshold ); | ||
|
|
||
| /** | ||
| * \brief Trigger an extra gather poll for the accumulator | ||
| * | ||
| * \param ctx Entropy context | ||
| * | ||
| * \return 0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED | ||
| */ | ||
| int entropy_gather( entropy_context *ctx ); | ||
|
|
||
| /** | ||
| * \brief Retrieve entropy from the accumulator (Max ENTROPY_BLOCK_SIZE) | ||
| * | ||
| * \param data Entropy context | ||
| * \param output Buffer to fill | ||
| * \param len Length of buffer | ||
| * | ||
| * \return 0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED | ||
| */ | ||
| int entropy_func( void *data, unsigned char *output, size_t len ); | ||
|
|
||
| /** | ||
| * \brief Add data to the accumulator manually | ||
| * | ||
| * \param ctx Entropy context | ||
| * \param data Data to add | ||
| * \param len Length of data | ||
| * | ||
| * \return 0 if successful | ||
| */ | ||
| int entropy_update_manual( entropy_context *ctx, | ||
| const unsigned char *data, size_t len ); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif /* entropy.h */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /** | ||
| * \file entropy_poll.h | ||
| * | ||
| * \brief Platform-specific and custom entropy polling functions | ||
| * | ||
| * Copyright (C) 2006-2011, 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_ENTROPY_POLL_H | ||
| #define POLARSSL_ENTROPY_POLL_H | ||
|
|
||
| #include <string.h> | ||
|
|
||
| #include "config.h" | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| /* | ||
| * Default thresholds for built-in sources | ||
| */ | ||
| #define ENTROPY_MIN_PLATFORM 128 /**< Minimum for platform source */ | ||
| #define ENTROPY_MIN_HAVEGE 128 /**< Minimum for HAVEGE */ | ||
| #define ENTROPY_MIN_HARDCLOCK 32 /**< Minimum for hardclock() */ | ||
|
|
||
| #if !defined(POLARSSL_NO_PLATFORM_ENTROPY) | ||
| /** | ||
| * \brief Platform-specific entropy poll callback | ||
| */ | ||
| int platform_entropy_poll( void *data, | ||
| unsigned char *output, size_t len, size_t *olen ); | ||
| #endif | ||
|
|
||
| #if defined(POLARSSL_HAVEGE_C) | ||
| /** | ||
| * \brief HAVEGE based entropy poll callback | ||
| * | ||
| * Requires an HAVEGE state as its data pointer. | ||
| */ | ||
| int havege_poll( void *data, | ||
| unsigned char *output, size_t len, size_t *olen ); | ||
| #endif | ||
|
|
||
| #if defined(POLARSSL_TIMING_C) | ||
| /** | ||
| * \brief hardclock-based entropy poll callback | ||
| */ | ||
| int hardclock_poll( void *data, | ||
| unsigned char *output, size_t len, size_t *olen ); | ||
| #endif | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif /* entropy_poll.h */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| /** | ||
| * \file error.h | ||
| * | ||
| * \brief Error to string translation | ||
| * | ||
| * Copyright (C) 2006-2010, 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_ERROR_H | ||
| #define POLARSSL_ERROR_H | ||
|
|
||
| #include <string.h> | ||
|
|
||
| /** | ||
| * Error code layout. | ||
| * | ||
| * Currently we try to keep all error codes within the negative space of 16 | ||
| * bytes signed integers to support all platforms (-0x0000 - -0x8000). In | ||
| * addition we'd like to give two layers of information on the error if | ||
| * possible. | ||
| * | ||
| * For that purpose the error codes are segmented in the following manner: | ||
| * | ||
| * 16 bit error code bit-segmentation | ||
| * | ||
| * 1 bit - Intentionally not used | ||
| * 3 bits - High level module ID | ||
| * 5 bits - Module-dependent error code | ||
| * 6 bits - Low level module errors | ||
| * 1 bit - Intentionally not used | ||
| * | ||
| * Low-level module errors (0x007E-0x0002) | ||
| * | ||
| * Module Nr Codes assigned | ||
| * MPI 7 0x0002-0x0010 | ||
| * GCM 2 0x0012-0x0014 | ||
| * BLOWFISH 2 0x0016-0x0018 | ||
| * AES 2 0x0020-0x0022 | ||
| * CAMELLIA 2 0x0024-0x0026 | ||
| * XTEA 1 0x0028-0x0028 | ||
| * BASE64 2 0x002A-0x002C | ||
| * PADLOCK 1 0x0030-0x0030 | ||
| * DES 1 0x0032-0x0032 | ||
| * CTR_DBRG 3 0x0034-0x003A | ||
| * ENTROPY 3 0x003C-0x0040 | ||
| * NET 11 0x0042-0x0056 | ||
| * ASN1 7 0x0060-0x006C | ||
| * MD2 1 0x0070-0x0070 | ||
| * MD4 1 0x0072-0x0072 | ||
| * MD5 1 0x0074-0x0074 | ||
| * SHA1 1 0x0076-0x0076 | ||
| * SHA2 1 0x0078-0x0078 | ||
| * SHA4 1 0x007A-0x007A | ||
| * | ||
| * High-level module nr (3 bits - 0x1...-0x8...) | ||
| * Name ID Nr of Errors | ||
| * PEM 1 9 | ||
| * PKCS#12 1 4 (Started from top) | ||
| * X509 2 23 | ||
| * DHM 3 6 | ||
| * PKCS5 3 4 (Started from top) | ||
| * RSA 4 9 | ||
| * MD 5 4 | ||
| * CIPHER 6 5 | ||
| * SSL 6 2 (Started from top) | ||
| * SSL 7 31 | ||
| * | ||
| * Module dependent error code (5 bits 0x.08.-0x.F8.) | ||
| */ | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| /** | ||
| * \brief Translate a PolarSSL error code into a string representation, | ||
| * Result is truncated if necessary and always includes a terminating | ||
| * null byte. | ||
| * | ||
| * \param errnum error code | ||
| * \param buffer buffer to place representation in | ||
| * \param buflen length of the buffer | ||
| */ | ||
| void error_strerror( int errnum, char *buffer, size_t buflen ); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif /* error.h */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| /** | ||
| * \file gcm.h | ||
| * | ||
| * \brief Galois/Counter mode for AES | ||
| * | ||
| * Copyright (C) 2006-2012, 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_GCM_H | ||
| #define POLARSSL_GCM_H | ||
|
|
||
| #include "aes.h" | ||
|
|
||
| #ifdef _MSC_VER | ||
| #include <basetsd.h> | ||
| typedef UINT64 uint64_t; | ||
| #else | ||
| #include <stdint.h> | ||
| #endif | ||
|
|
||
| #define GCM_ENCRYPT 1 | ||
| #define GCM_DECRYPT 0 | ||
|
|
||
| #define POLARSSL_ERR_GCM_AUTH_FAILED -0x0012 /**< Authenticated decryption failed. */ | ||
| #define POLARSSL_ERR_GCM_BAD_INPUT -0x0014 /**< Bad input parameters to function. */ | ||
|
|
||
| /** | ||
| * \brief GCM context structure | ||
| */ | ||
| typedef struct { | ||
| aes_context aes_ctx; /*!< AES context used */ | ||
| uint64_t HL[16]; /*!< Precalculated HTable */ | ||
| uint64_t HH[16]; /*!< Precalculated HTable */ | ||
| } | ||
| gcm_context; | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| /** | ||
| * \brief GCM initialization (encryption) | ||
| * | ||
| * \param ctx GCM context to be initialized | ||
| * \param key encryption key | ||
| * \param keysize must be 128, 192 or 256 | ||
| * | ||
| * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH | ||
| */ | ||
| int gcm_init( gcm_context *ctx, const unsigned char *key, unsigned int keysize ); | ||
|
|
||
| /** | ||
| * \brief GCM buffer encryption/decryption using AES | ||
| * | ||
| * \note On encryption, the output buffer can be the same as the input buffer. | ||
| * On decryption, the output buffer cannot be the same as input buffer. | ||
| * If buffers overlap, the output buffer must trail at least 8 bytes | ||
| * behind the input buffer. | ||
| * | ||
| * \param ctx GCM context | ||
| * \param mode GCM_ENCRYPT or GCM_DECRYPT | ||
| * \param length length of the input data | ||
| * \param iv initialization vector | ||
| * \param iv_len length of IV | ||
| * \param add additional data | ||
| * \param add_len length of additional data | ||
| * \param input buffer holding the input data | ||
| * \param output buffer for holding the output data | ||
| * \param tag_len length of the tag to generate | ||
| * \param tag buffer for holding the tag | ||
| * | ||
| * \return 0 if successful | ||
| */ | ||
| int gcm_crypt_and_tag( gcm_context *ctx, | ||
| int mode, | ||
| size_t length, | ||
| const unsigned char *iv, | ||
| size_t iv_len, | ||
| const unsigned char *add, | ||
| size_t add_len, | ||
| const unsigned char *input, | ||
| unsigned char *output, | ||
| size_t tag_len, | ||
| unsigned char *tag ); | ||
|
|
||
| /** | ||
| * \brief GCM buffer authenticated decryption using AES | ||
| * | ||
| * \note On decryption, the output buffer cannot be the same as input buffer. | ||
| * If buffers overlap, the output buffer must trail at least 8 bytes | ||
| * behind the input buffer. | ||
| * | ||
| * \param ctx GCM context | ||
| * \param length length of the input data | ||
| * \param iv initialization vector | ||
| * \param iv_len length of IV | ||
| * \param add additional data | ||
| * \param add_len length of additional data | ||
| * \param tag buffer holding the tag | ||
| * \param tag_len length of the tag | ||
| * \param input buffer holding the input data | ||
| * \param output buffer for holding the output data | ||
| * | ||
| * \return 0 if successful and authenticated, | ||
| * POLARSSL_ERR_GCM_AUTH_FAILED if tag does not match | ||
| */ | ||
| int gcm_auth_decrypt( gcm_context *ctx, | ||
| size_t length, | ||
| const unsigned char *iv, | ||
| size_t iv_len, | ||
| const unsigned char *add, | ||
| size_t add_len, | ||
| const unsigned char *tag, | ||
| size_t tag_len, | ||
| const unsigned char *input, | ||
| unsigned char *output ); | ||
|
|
||
| /** | ||
| * \brief Checkup routine | ||
| * | ||
| * \return 0 if successful, or 1 if the test failed | ||
| */ | ||
| int gcm_self_test( int verbose ); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif /* gcm.h */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /** | ||
| * \file havege.h | ||
| * | ||
| * \brief HAVEGE: HArdware Volatile Entropy Gathering and Expansion | ||
| * | ||
| * Copyright (C) 2006-2010, 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_HAVEGE_H | ||
| #define POLARSSL_HAVEGE_H | ||
|
|
||
| #include <string.h> | ||
|
|
||
| #define COLLECT_SIZE 1024 | ||
|
|
||
| /** | ||
| * \brief HAVEGE state structure | ||
| */ | ||
| typedef struct | ||
| { | ||
| int PT1, PT2, offset[2]; | ||
| int pool[COLLECT_SIZE]; | ||
| int WALK[8192]; | ||
| } | ||
| havege_state; | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| /** | ||
| * \brief HAVEGE initialization | ||
| * | ||
| * \param hs HAVEGE state to be initialized | ||
| */ | ||
| void havege_init( havege_state *hs ); | ||
|
|
||
| /** | ||
| * \brief HAVEGE rand function | ||
| * | ||
| * \param p_rng A HAVEGE state | ||
| * \param output Buffer to fill | ||
| * \param len Length of buffer | ||
| * | ||
| * \return 0 | ||
| */ | ||
| int havege_random( void *p_rng, unsigned char *output, size_t len ); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| #endif /* havege.h */ |