Merged ECP memory usage optimizations
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 7b90993..257f6df 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -3,14 +3,25 @@
enable_testing()
+string(REGEX MATCH "clang" CMAKE_COMPILER_IS_CLANG "${CMAKE_C_COMPILER}")
+
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")
- set(CMAKE_C_FLAGS_CHECK "${CMAKE_C_FLAGS} -Wlogical-op -Wwrite-strings")
+ set(CMAKE_C_FLAGS_CHECK "${CMAKE_C_FLAGS} -Werror -Wlogical-op -Wwrite-strings")
set(CMAKE_C_FLAGS_CHECKFULL "${CMAKE_C_FLAGS_CHECK} -Wcast-qual")
endif(CMAKE_COMPILER_IS_GNUCC)
+if(CMAKE_COMPILER_IS_CLANG)
+ 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_CHECK "${CMAKE_C_FLAGS} -Werror -Wpointer-arith -Wwrite-strings -Wdocumentation")
+endif(CMAKE_COMPILER_IS_CLANG)
+
+set(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE}
+ CACHE STRING "Choose the type of build: None Debug Release Coverage Check CheckFull"
+ FORCE)
if(CMAKE_BUILD_TYPE STREQUAL "Coverage")
if(CMAKE_COMPILER_IS_GNUCC)
set(CMAKE_SHARED_LINKER_FLAGS "-fprofile-arcs -ftest-coverage")
diff --git a/ChangeLog b/ChangeLog
index cd3732f..030d720 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -8,6 +8,8 @@
* Support for Curve25519
* Support for ECDH-RSA and ECDH-ECDSA key exchanges and ciphersuites
* Support for IPv6 in the NET module
+ * AES-NI support for AES, AES-GCM and AES key scheduling
+ * SSL Pthread-based server example added (ssl_pthread_server)
Changes
* gen_prime() speedup
@@ -17,6 +19,7 @@
* More constant-time checks in the RSA module
* Split off curves from ecp.c into ecp_curves.c
* Curves are now stored fully in ROM
+ * Memory usage optimizations in ECP module
Bugfix
* Fixed bug in mpi_set_bit() on platforms where t_uint is wider than int
@@ -28,6 +31,10 @@
* Memory leak in benchmark application
* Fixed x509_crt_parse_path() bug on Windows platforms
+Security
+ * Possible remotely-triggered out-of-bounds memory access fixed (found by
+ TrustInSoft)
+
= PolarSSL 1.3.2 released on 2013-11-04
Features
* PK tests added to test framework
diff --git a/include/polarssl/aes.h b/include/polarssl/aes.h
index 1b93e2a..9513a71 100644
--- a/include/polarssl/aes.h
+++ b/include/polarssl/aes.h
@@ -38,6 +38,7 @@
#include <inttypes.h>
#endif
+/* padlock.c and aesni.c rely on these values! */
#define AES_ENCRYPT 1
#define AES_DECRYPT 0
@@ -54,6 +55,11 @@
/**
* \brief AES context structure
+ *
+ * \note buf is able to hold 32 extra bytes, which can be used:
+ * - for alignment purposes if VIA padlock is used, and/or
+ * - to simplify key expansion in the 256-bit case by
+ * generating an extra round key
*/
typedef struct
{
diff --git a/include/polarssl/aesni.h b/include/polarssl/aesni.h
new file mode 100644
index 0000000..92b23cd
--- /dev/null
+++ b/include/polarssl/aesni.h
@@ -0,0 +1,107 @@
+/**
+ * \file aesni.h
+ *
+ * \brief AES-NI for hardware AES acceleration on some Intel processors
+ *
+ * Copyright (C) 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_AESNI_H
+#define POLARSSL_AESNI_H
+
+#include "aes.h"
+
+#define POLARSSL_AESNI_AES 0x02000000u
+#define POLARSSL_AESNI_CLMUL 0x00000002u
+
+#if defined(POLARSSL_HAVE_ASM) && defined(__GNUC__) && \
+ ( defined(__amd64__) || defined(__x86_64__) ) && \
+ ! defined(POLARSSL_HAVE_X86_64)
+#define POLARSSL_HAVE_X86_64
+#endif
+
+#if defined(POLARSSL_HAVE_X86_64)
+
+/**
+ * \brief AES-NI features detection routine
+ *
+ * \param what The feature to detect
+ * (POLARSSL_AESNI_AES or POLARSSL_AESNI_CLMUL)
+ *
+ * \return 1 if CPU has support for the feature, 0 otherwise
+ */
+int aesni_supports( unsigned int what );
+
+/**
+ * \brief AES-NI AES-ECB block en(de)cryption
+ *
+ * \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 on success (cannot fail)
+ */
+int aesni_crypt_ecb( aes_context *ctx,
+ int mode,
+ const unsigned char input[16],
+ unsigned char output[16] );
+
+/**
+ * \brief GCM multiplication: c = a * b in GF(2^128)
+ *
+ * \param c Result
+ * \param a First operand
+ * \param b Second operand
+ *
+ * \note Both operands and result are bit strings interpreted as
+ * elements of GF(2^128) as per the GCM spec.
+ */
+void aesni_gcm_mult( unsigned char c[16],
+ const unsigned char a[16],
+ const unsigned char b[16] );
+
+/**
+ * \brief Compute decryption round keys from encryption round keys
+ *
+ * \param invkey Round keys for the equivalent inverse cipher
+ * \param fwdkey Original round keys (for encryption)
+ * \param nr Number of rounds (that is, number of round keys minus one)
+ */
+void aesni_inverse_key( unsigned char *invkey,
+ const unsigned char *fwdkey, int nr );
+
+/**
+ * \brief Perform key expansion (for encryption)
+ *
+ * \param rk Destination buffer where the round keys are written
+ * \param key Encryption key
+ * \param bits Key size in bits (must be 128, 192 or 256)
+ *
+ * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
+ */
+int aesni_setkey_enc( unsigned char *rk,
+ const unsigned char *key,
+ size_t bits );
+
+#endif /* POLARSSL_HAVE_X86_64 */
+
+#endif /* POLARSSL_AESNI_H */
diff --git a/include/polarssl/config.h b/include/polarssl/config.h
index 16643dc..dfd107a 100644
--- a/include/polarssl/config.h
+++ b/include/polarssl/config.h
@@ -875,6 +875,20 @@
*/
/**
+ * \def POLARSSL_AESNI_C
+ *
+ * Enable AES-NI support on x86-64.
+ *
+ * Module: library/aesni.c
+ * Caller: library/aes.c
+ *
+ * Requires: POLARSSL_HAVE_ASM
+ *
+ * This modules adds support for the AES-NI instructions on x86-64
+ */
+#define POLARSSL_AESNI_C
+
+/**
* \def POLARSSL_AES_C
*
* Enable the AES block cipher.
@@ -1414,6 +1428,8 @@
* Module: library/padlock.c
* Caller: library/aes.c
*
+ * Requires: POLARSSL_HAVE_ASM
+ *
* This modules adds support for the VIA PadLock on x86.
*/
#define POLARSSL_PADLOCK_C
@@ -1903,6 +1919,10 @@
/*
* Sanity checks on defines and dependencies
*/
+#if defined(POLARSSL_AESNI_C) && !defined(POLARSSL_HAVE_ASM)
+#error "POLARSSL_AESNI_C defined, but not all prerequisites"
+#endif
+
#if defined(POLARSSL_CERTS_C) && !defined(POLARSSL_PEM_PARSE_C)
#error "POLARSSL_CERTS_C defined, but not all prerequisites"
#endif
@@ -2013,6 +2033,10 @@
#error "POLARSSL_MEMORY_BUFFER_ALLOC_C defined, but not all prerequisites"
#endif
+#if defined(POLARSSL_PADLOCK_C) && !defined(POLARSSL_HAVE_ASM)
+#error "POLARSSL_PADLOCK_C defined, but not all prerequisites"
+#endif
+
#if defined(POLARSSL_PBKDF2_C) && !defined(POLARSSL_MD_C)
#error "POLARSSL_PBKDF2_C defined, but not all prerequisites"
#endif
diff --git a/include/polarssl/ecdh.h b/include/polarssl/ecdh.h
index d20f9bf..3c4800b 100644
--- a/include/polarssl/ecdh.h
+++ b/include/polarssl/ecdh.h
@@ -148,7 +148,7 @@
*
* \param ctx ECDH constext to set
* \param key EC key to use
- * \param ours Is it our key (1) or the peer's key (0) ?
+ * \param side Is it our key (1) or the peer's key (0) ?
*
* \return 0 if successful, or an POLARSSL_ERR_ECP_XXX error code
*/
diff --git a/include/polarssl/ecp.h b/include/polarssl/ecp.h
index 23351a7..1e9e737 100644
--- a/include/polarssl/ecp.h
+++ b/include/polarssl/ecp.h
@@ -237,7 +237,7 @@
/**
* \brief Get curve information from a TLS NamedCurve value
*
- * \param grp_id A POLARSSL_ECP_DP_XXX value
+ * \param tls_id A POLARSSL_ECP_DP_XXX value
*
* \return The associated curve information or NULL
*/
diff --git a/include/polarssl/entropy.h b/include/polarssl/entropy.h
index 235b773..7e65b78 100644
--- a/include/polarssl/entropy.h
+++ b/include/polarssl/entropy.h
@@ -81,7 +81,8 @@
* \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 *);
+typedef int (*f_source_ptr)(void *data, unsigned char *output, size_t len,
+ size_t *olen);
/**
* \brief Entropy source state
diff --git a/include/polarssl/gcm.h b/include/polarssl/gcm.h
index e4267c6..c2829a0 100644
--- a/include/polarssl/gcm.h
+++ b/include/polarssl/gcm.h
@@ -201,7 +201,7 @@
/**
* \brief Free a GCM context and underlying cipher sub-context
*
- * \param ctx
+ * \param ctx GCM context to free
*/
void gcm_free( gcm_context *ctx );
diff --git a/include/polarssl/padlock.h b/include/polarssl/padlock.h
index 8df93c0..545120f 100644
--- a/include/polarssl/padlock.h
+++ b/include/polarssl/padlock.h
@@ -59,7 +59,7 @@
/**
* \brief PadLock detection routine
*
- * \param The feature to detect
+ * \param feature The feature to detect
*
* \return 1 if CPU has support for the feature, 0 otherwise
*/
diff --git a/include/polarssl/pk.h b/include/polarssl/pk.h
index 958672b..013d973 100644
--- a/include/polarssl/pk.h
+++ b/include/polarssl/pk.h
@@ -453,14 +453,14 @@
* return value to determine where you should start
* using the buffer
*
- * \param key private to write away
+ * \param ctx private to write away
* \param buf buffer to write to
* \param size size of the buffer
*
* \return length of data written if successful, or a specific
* error code
*/
-int pk_write_key_der( pk_context *pk, unsigned char *buf, size_t size );
+int pk_write_key_der( pk_context *ctx, unsigned char *buf, size_t size );
/**
* \brief Write a public key to a SubjectPublicKeyInfo DER structure
@@ -468,37 +468,37 @@
* return value to determine where you should start
* using the buffer
*
- * \param key public key to write away
+ * \param ctx public key to write away
* \param buf buffer to write to
* \param size size of the buffer
*
* \return length of data written if successful, or a specific
* error code
*/
-int pk_write_pubkey_der( pk_context *key, unsigned char *buf, size_t size );
+int pk_write_pubkey_der( pk_context *ctx, unsigned char *buf, size_t size );
#if defined(POLARSSL_PEM_WRITE_C)
/**
* \brief Write a public key to a PEM string
*
- * \param key public key to write away
+ * \param ctx public key to write away
* \param buf buffer to write to
* \param size size of the buffer
*
* \return 0 successful, or a specific error code
*/
-int pk_write_pubkey_pem( pk_context *key, unsigned char *buf, size_t size );
+int pk_write_pubkey_pem( pk_context *ctx, unsigned char *buf, size_t size );
/**
* \brief Write a private key to a PKCS#1 or SEC1 PEM string
*
- * \param key private to write away
+ * \param ctx private to write away
* \param buf buffer to write to
* \param size size of the buffer
*
* \return 0 successful, or a specific error code
*/
-int pk_write_key_pem( pk_context *key, unsigned char *buf, size_t size );
+int pk_write_key_pem( pk_context *ctx, unsigned char *buf, size_t size );
#endif /* POLARSSL_PEM_WRITE_C */
#endif /* POLARSSL_PK_WRITE_C */
diff --git a/include/polarssl/pkcs12.h b/include/polarssl/pkcs12.h
index 51bea3d..4bd5018 100644
--- a/include/polarssl/pkcs12.h
+++ b/include/polarssl/pkcs12.h
@@ -38,9 +38,9 @@
#define POLARSSL_ERR_PKCS12_PBE_INVALID_FORMAT -0x1E80 /**< PBE ASN.1 data not as expected. */
#define POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH -0x1E00 /**< Given private key password does not allow for correct decryption. */
-#define PKCS12_DERIVE_KEY 1 /*< encryption/decryption key */
-#define PKCS12_DERIVE_IV 2 /*< initialization vector */
-#define PKCS12_DERIVE_MAC_KEY 3 /*< integrity / MAC key */
+#define PKCS12_DERIVE_KEY 1 /**< encryption/decryption key */
+#define PKCS12_DERIVE_IV 2 /**< initialization vector */
+#define PKCS12_DERIVE_MAC_KEY 3 /**< integrity / MAC key */
#define PKCS12_PBE_DECRYPT 0
#define PKCS12_PBE_ENCRYPT 1
diff --git a/include/polarssl/rsa.h b/include/polarssl/rsa.h
index e7b6191..504dde2 100644
--- a/include/polarssl/rsa.h
+++ b/include/polarssl/rsa.h
@@ -526,7 +526,6 @@
/**
* \brief Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY)
- * \brief Do a public RSA and check the message digest
*
* \param ctx points to an RSA public key
* \param f_rng RNG function (Only needed for RSA_PRIVATE)
diff --git a/include/polarssl/ssl.h b/include/polarssl/ssl.h
index 4b0c5f8..7e668f9 100644
--- a/include/polarssl/ssl.h
+++ b/include/polarssl/ssl.h
@@ -237,8 +237,8 @@
#endif /* !POLARSSL_CONFIG_OPTIONS */
/*
- * Allow an extra 512 bytes for the record header
- * and encryption overhead (counter + MAC + padding)
+ * Allow an extra 301 bytes for the record header
+ * and encryption overhead: counter (8) + header (5) + MAC (32) + padding (256)
* and allow for a maximum of 1024 of compression expansion if
* enabled.
*/
@@ -248,9 +248,9 @@
#define SSL_COMPRESSION_ADD 0
#endif
-#define SSL_BUFFER_LEN (SSL_MAX_CONTENT_LEN + SSL_COMPRESSION_ADD + 512)
+#define SSL_BUFFER_LEN (SSL_MAX_CONTENT_LEN + SSL_COMPRESSION_ADD + 301)
-#define SSL_EMPTY_RENEGOTIATION_INFO 0xFF /**< renegotiation info ext */
+#define SSL_EMPTY_RENEGOTIATION_INFO 0xFF /**< renegotiation info ext */
/*
* Supported Signature and Hash algorithms (For TLS 1.2)
diff --git a/include/polarssl/ssl_ciphersuites.h b/include/polarssl/ssl_ciphersuites.h
index 3b13ef3..28a5855 100644
--- a/include/polarssl/ssl_ciphersuites.h
+++ b/include/polarssl/ssl_ciphersuites.h
@@ -226,7 +226,7 @@
typedef struct _ssl_ciphersuite_t ssl_ciphersuite_t;
-#define POLARSSL_CIPHERSUITE_WEAK 0x01 /*<! Weak ciphersuite flag */
+#define POLARSSL_CIPHERSUITE_WEAK 0x01 /**< Weak ciphersuite flag */
/**
* \brief This structure is used for storing ciphersuite information
diff --git a/include/polarssl/x509_crt.h b/include/polarssl/x509_crt.h
index 4e7bbb7..ee8f9e6 100644
--- a/include/polarssl/x509_crt.h
+++ b/include/polarssl/x509_crt.h
@@ -371,7 +371,7 @@
* (e.g. POLARSSL_MD_SHA1)
*
* \param ctx CRT context to use
- * \param md_ald MD algorithm to use
+ * \param md_alg MD algorithm to use
*/
void x509write_crt_set_md_alg( x509write_cert *ctx, md_type_t md_alg );
@@ -467,7 +467,7 @@
* return value to determine where you should start
* using the buffer
*
- * \param crt certificate to write away
+ * \param ctx certificate to write away
* \param buf buffer to write to
* \param size size of the buffer
* \param f_rng RNG function (for signature, see note)
@@ -489,7 +489,7 @@
/**
* \brief Write a built up certificate to a X509 PEM string
*
- * \param crt certificate to write away
+ * \param ctx certificate to write away
* \param buf buffer to write to
* \param size size of the buffer
* \param f_rng RNG function (for signature, see note)
diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt
index 411c07d..1a5efde 100644
--- a/library/CMakeLists.txt
+++ b/library/CMakeLists.txt
@@ -2,6 +2,7 @@
set(src
aes.c
+ aesni.c
arc4.c
asn1parse.c
asn1write.c
diff --git a/library/Makefile b/library/Makefile
index a9c86f8..3f80162 100644
--- a/library/Makefile
+++ b/library/Makefile
@@ -34,7 +34,8 @@
LDFLAGS += -lws2_32
endif
-OBJS= aes.o arc4.o asn1parse.o \
+OBJS= aes.o aesni.o arc4.o \
+ asn1parse.o \
asn1write.o base64.o bignum.o \
blowfish.o camellia.o \
certs.o cipher.o cipher_wrap.o \
diff --git a/library/aes.c b/library/aes.c
index a3835ce..69da3f6 100644
--- a/library/aes.c
+++ b/library/aes.c
@@ -37,6 +37,9 @@
#if defined(POLARSSL_PADLOCK_C)
#include "polarssl/padlock.h"
#endif
+#if defined(POLARSSL_AESNI_C)
+#include "polarssl/aesni.h"
+#endif
#if !defined(POLARSSL_AES_ALT)
@@ -480,6 +483,11 @@
#endif
ctx->rk = RK = ctx->buf;
+#if defined(POLARSSL_AESNI_C) && defined(POLARSSL_HAVE_X86_64)
+ if( aesni_supports( POLARSSL_AESNI_AES ) )
+ return( aesni_setkey_enc( (unsigned char *) ctx->rk, key, keysize ) );
+#endif
+
for( i = 0; i < (keysize >> 5); i++ )
{
GET_UINT32_LE( RK[i], key, i << 2 );
@@ -588,6 +596,15 @@
if( ret != 0 )
return( ret );
+#if defined(POLARSSL_AESNI_C) && defined(POLARSSL_HAVE_X86_64)
+ if( aesni_supports( POLARSSL_AESNI_AES ) )
+ {
+ aesni_inverse_key( (unsigned char *) ctx->rk,
+ (const unsigned char *) cty.rk, ctx->nr );
+ goto done;
+ }
+#endif
+
SK = cty.rk + cty.nr * 4;
*RK++ = *SK++;
@@ -611,6 +628,9 @@
*RK++ = *SK++;
*RK++ = *SK++;
+#if defined(POLARSSL_AESNI_C) && defined(POLARSSL_HAVE_X86_64)
+done:
+#endif
memset( &cty, 0, sizeof( aes_context ) );
return( 0 );
@@ -673,6 +693,11 @@
int i;
uint32_t *RK, X0, X1, X2, X3, Y0, Y1, Y2, Y3;
+#if defined(POLARSSL_AESNI_C) && defined(POLARSSL_HAVE_X86_64)
+ if( aesni_supports( POLARSSL_AESNI_AES ) )
+ return( aesni_crypt_ecb( ctx, mode, input, output ) );
+#endif
+
#if defined(POLARSSL_PADLOCK_C) && defined(POLARSSL_HAVE_X86)
if( aes_padlock_ace )
{
diff --git a/library/aesni.c b/library/aesni.c
new file mode 100644
index 0000000..aa054cb
--- /dev/null
+++ b/library/aesni.c
@@ -0,0 +1,433 @@
+/*
+ * AES-NI support functions
+ *
+ * Copyright (C) 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.
+ */
+
+/*
+ * [AES-WP] http://software.intel.com/en-us/articles/intel-advanced-encryption-standard-aes-instructions-set
+ * [CLMUL-WP] http://software.intel.com/en-us/articles/intel-carry-less-multiplication-instruction-and-its-usage-for-computing-the-gcm-mode/
+ */
+
+#include "polarssl/config.h"
+
+#if defined(POLARSSL_AESNI_C)
+
+#include "polarssl/aesni.h"
+#include <stdio.h>
+
+#if defined(POLARSSL_HAVE_X86_64)
+
+/*
+ * AES-NI support detection routine
+ */
+int aesni_supports( unsigned int what )
+{
+ static int done = 0;
+ static unsigned int c = 0;
+
+ if( ! done )
+ {
+ asm( "movl $1, %%eax \n"
+ "cpuid \n"
+ : "=c" (c)
+ :
+ : "eax", "ebx", "edx" );
+ done = 1;
+ }
+
+ return( ( c & what ) != 0 );
+}
+
+/*
+ * AES-NI AES-ECB block en(de)cryption
+ */
+int aesni_crypt_ecb( aes_context *ctx,
+ int mode,
+ const unsigned char input[16],
+ unsigned char output[16] )
+{
+ asm( "movdqu (%3), %%xmm0 \n" // load input
+ "movdqu (%1), %%xmm1 \n" // load round key 0
+ "pxor %%xmm1, %%xmm0 \n" // round 0
+ "addq $16, %1 \n" // point to next round key
+ "subl $1, %0 \n" // normal rounds = nr - 1
+ "test %2, %2 \n" // mode?
+ "jz 2f \n" // 0 = decrypt
+
+ "1: \n" // encryption loop
+ "movdqu (%1), %%xmm1 \n" // load round key
+ "aesenc %%xmm1, %%xmm0 \n" // do round
+ "addq $16, %1 \n" // point to next round key
+ "subl $1, %0 \n" // loop
+ "jnz 1b \n"
+ "movdqu (%1), %%xmm1 \n" // load round key
+ "aesenclast %%xmm1, %%xmm0 \n" // last round
+ "jmp 3f \n"
+
+ "2: \n" // decryption loop
+ "movdqu (%1), %%xmm1 \n"
+ "aesdec %%xmm1, %%xmm0 \n"
+ "addq $16, %1 \n"
+ "subl $1, %0 \n"
+ "jnz 2b \n"
+ "movdqu (%1), %%xmm1 \n" // load round key
+ "aesdeclast %%xmm1, %%xmm0 \n" // last round
+
+ "3: \n"
+ "movdqu %%xmm0, (%4) \n" // export output
+ :
+ : "r" (ctx->nr), "r" (ctx->rk), "r" (mode), "r" (input), "r" (output)
+ : "memory", "cc", "xmm0", "xmm1" );
+
+
+ return( 0 );
+}
+
+/*
+ * GCM multiplication: c = a times b in GF(2^128)
+ * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5.
+ */
+void aesni_gcm_mult( unsigned char c[16],
+ const unsigned char a[16],
+ const unsigned char b[16] )
+{
+ unsigned char aa[16], bb[16], cc[16];
+ size_t i;
+
+ /* The inputs are in big-endian order, so byte-reverse them */
+ for( i = 0; i < 16; i++ )
+ {
+ aa[i] = a[15 - i];
+ bb[i] = b[15 - i];
+ }
+
+ asm( "movdqu (%0), %%xmm0 \n" // a1:a0
+ "movdqu (%1), %%xmm1 \n" // b1:b0
+
+ /*
+ * Caryless multiplication xmm2:xmm1 = xmm0 * xmm1
+ * using [CLMUL-WP] algorithm 1 (p. 13).
+ */
+ "movdqa %%xmm1, %%xmm2 \n" // copy of b1:b0
+ "movdqa %%xmm1, %%xmm3 \n" // same
+ "movdqa %%xmm1, %%xmm4 \n" // same
+ "pclmulqdq $0x00, %%xmm0, %%xmm1 \n" // a0*b0 = c1:c0
+ "pclmulqdq $0x11, %%xmm0, %%xmm2 \n" // a1*b1 = d1:d0
+ "pclmulqdq $0x10, %%xmm0, %%xmm3 \n" // a0*b1 = e1:e0
+ "pclmulqdq $0x01, %%xmm0, %%xmm4 \n" // a1*b0 = f1:f0
+ "pxor %%xmm3, %%xmm4 \n" // e1+f1:e0+f0
+ "movdqa %%xmm4, %%xmm3 \n" // same
+ "psrldq $8, %%xmm4 \n" // 0:e1+f1
+ "pslldq $8, %%xmm3 \n" // e0+f0:0
+ "pxor %%xmm4, %%xmm2 \n" // d1:d0+e1+f1
+ "pxor %%xmm3, %%xmm1 \n" // c1+e0+f1:c0
+
+ /*
+ * Now shift the result one bit to the left,
+ * taking advantage of [CLMUL-WP] eq 27 (p. 20)
+ */
+ "movdqa %%xmm1, %%xmm3 \n" // r1:r0
+ "movdqa %%xmm2, %%xmm4 \n" // r3:r2
+ "psllq $1, %%xmm1 \n" // r1<<1:r0<<1
+ "psllq $1, %%xmm2 \n" // r3<<1:r2<<1
+ "psrlq $63, %%xmm3 \n" // r1>>63:r0>>63
+ "psrlq $63, %%xmm4 \n" // r3>>63:r2>>63
+ "movdqa %%xmm3, %%xmm5 \n" // r1>>63:r0>>63
+ "pslldq $8, %%xmm3 \n" // r0>>63:0
+ "pslldq $8, %%xmm4 \n" // r2>>63:0
+ "psrldq $8, %%xmm5 \n" // 0:r1>>63
+ "por %%xmm3, %%xmm1 \n" // r1<<1|r0>>63:r0<<1
+ "por %%xmm4, %%xmm2 \n" // r3<<1|r2>>62:r2<<1
+ "por %%xmm5, %%xmm2 \n" // r3<<1|r2>>62:r2<<1|r1>>63
+
+ /*
+ * Now reduce modulo the GCM polynomial x^128 + x^7 + x^2 + x + 1
+ * using [CLMUL-WP] algorithm 5 (p. 20).
+ * Currently xmm2:xmm1 holds x3:x2:x1:x0 (already shifted).
+ */
+ /* Step 2 (1) */
+ "movdqa %%xmm1, %%xmm3 \n" // x1:x0
+ "movdqa %%xmm1, %%xmm4 \n" // same
+ "movdqa %%xmm1, %%xmm5 \n" // same
+ "psllq $63, %%xmm3 \n" // x1<<63:x0<<63 = stuff:a
+ "psllq $62, %%xmm4 \n" // x1<<62:x0<<62 = stuff:b
+ "psllq $57, %%xmm5 \n" // x1<<57:x0<<57 = stuff:c
+
+ /* Step 2 (2) */
+ "pxor %%xmm4, %%xmm3 \n" // stuff:a+b
+ "pxor %%xmm5, %%xmm3 \n" // stuff:a+b+c
+ "pslldq $8, %%xmm3 \n" // a+b+c:0
+ "pxor %%xmm3, %%xmm1 \n" // x1+a+b+c:x0 = d:x0
+
+ /* Steps 3 and 4 */
+ "movdqa %%xmm1,%%xmm0 \n" // d:x0
+ "movdqa %%xmm1,%%xmm4 \n" // same
+ "movdqa %%xmm1,%%xmm5 \n" // same
+ "psrlq $1, %%xmm0 \n" // e1:x0>>1 = e1:e0'
+ "psrlq $2, %%xmm4 \n" // f1:x0>>2 = f1:f0'
+ "psrlq $7, %%xmm5 \n" // g1:x0>>7 = g1:g0'
+ "pxor %%xmm4, %%xmm0 \n" // e1+f1:e0'+f0'
+ "pxor %%xmm5, %%xmm0 \n" // e1+f1+g1:e0'+f0'+g0'
+ // e0'+f0'+g0' is almost e0+f0+g0, except for some missing
+ // bits carried from d. Now get those bits back in.
+ "movdqa %%xmm1,%%xmm3 \n" // d:x0
+ "movdqa %%xmm1,%%xmm4 \n" // same
+ "movdqa %%xmm1,%%xmm5 \n" // same
+ "psllq $63, %%xmm3 \n" // d<<63:stuff
+ "psllq $62, %%xmm4 \n" // d<<62:stuff
+ "psllq $57, %%xmm5 \n" // d<<57:stuff
+ "pxor %%xmm4, %%xmm3 \n" // d<<63+d<<62:stuff
+ "pxor %%xmm5, %%xmm3 \n" // missing bits of d:stuff
+ "psrldq $8, %%xmm3 \n" // 0:missing bits of d
+ "pxor %%xmm3, %%xmm0 \n" // e1+f1+g1:e0+f0+g0
+ "pxor %%xmm1, %%xmm0 \n" // h1:h0
+ "pxor %%xmm2, %%xmm0 \n" // x3+h1:x2+h0
+
+ "movdqu %%xmm0, (%2) \n" // done
+ :
+ : "r" (aa), "r" (bb), "r" (cc)
+ : "memory", "cc", "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5" );
+
+ /* Now byte-reverse the outputs */
+ for( i = 0; i < 16; i++ )
+ c[i] = cc[15 - i];
+
+ return;
+}
+
+/*
+ * Compute decryption round keys from encryption round keys
+ */
+void aesni_inverse_key( unsigned char *invkey,
+ const unsigned char *fwdkey, int nr )
+{
+ unsigned char *ik = invkey;
+ const unsigned char *fk = fwdkey + 16 * nr;
+
+ memcpy( ik, fk, 16 );
+
+ for( fk -= 16, ik += 16; fk > fwdkey; fk -= 16, ik += 16 )
+ asm( "movdqu (%0), %%xmm0 \n"
+ "aesimc %%xmm0, %%xmm0 \n"
+ "movdqu %%xmm0, (%1) \n"
+ :
+ : "r" (fk), "r" (ik)
+ : "memory", "xmm0" );
+
+ memcpy( ik, fk, 16 );
+}
+
+/*
+ * Key expansion, 128-bit case
+ */
+static void aesni_setkey_enc_128( unsigned char *rk,
+ const unsigned char *key )
+{
+ asm( "movdqu (%1), %%xmm0 \n" // copy the original key
+ "movdqu %%xmm0, (%0) \n" // as round key 0
+ "jmp 2f \n" // skip auxiliary routine
+
+ /*
+ * Finish generating the next round key.
+ *
+ * On entry xmm0 is r3:r2:r1:r0 and xmm1 is X:stuff:stuff:stuff
+ * with X = rot( sub( r3 ) ) ^ RCON.
+ *
+ * On exit, xmm0 is r7:r6:r5:r4
+ * with r4 = X + r0, r5 = r4 + r1, r6 = r5 + r2, r7 = r6 + r3
+ * and those are written to the round key buffer.
+ */
+ "1: \n"
+ "pshufd $0xff, %%xmm1, %%xmm1 \n" // X:X:X:X
+ "pxor %%xmm0, %%xmm1 \n" // X+r3:X+r2:X+r1:r4
+ "pslldq $4, %%xmm0 \n" // r2:r1:r0:0
+ "pxor %%xmm0, %%xmm1 \n" // X+r3+r2:X+r2+r1:r5:r4
+ "pslldq $4, %%xmm0 \n" // etc
+ "pxor %%xmm0, %%xmm1 \n"
+ "pslldq $4, %%xmm0 \n"
+ "pxor %%xmm1, %%xmm0 \n" // update xmm0 for next time!
+ "add $16, %0 \n" // point to next round key
+ "movdqu %%xmm0, (%0) \n" // write it
+ "ret \n"
+
+ /* Main "loop" */
+ "2: \n"
+ "aeskeygenassist $0x01, %%xmm0, %%xmm1 \ncall 1b \n"
+ "aeskeygenassist $0x02, %%xmm0, %%xmm1 \ncall 1b \n"
+ "aeskeygenassist $0x04, %%xmm0, %%xmm1 \ncall 1b \n"
+ "aeskeygenassist $0x08, %%xmm0, %%xmm1 \ncall 1b \n"
+ "aeskeygenassist $0x10, %%xmm0, %%xmm1 \ncall 1b \n"
+ "aeskeygenassist $0x20, %%xmm0, %%xmm1 \ncall 1b \n"
+ "aeskeygenassist $0x40, %%xmm0, %%xmm1 \ncall 1b \n"
+ "aeskeygenassist $0x80, %%xmm0, %%xmm1 \ncall 1b \n"
+ "aeskeygenassist $0x1B, %%xmm0, %%xmm1 \ncall 1b \n"
+ "aeskeygenassist $0x36, %%xmm0, %%xmm1 \ncall 1b \n"
+ :
+ : "r" (rk), "r" (key)
+ : "memory", "cc", "0" );
+}
+
+/*
+ * Key expansion, 192-bit case
+ */
+static void aesni_setkey_enc_192( unsigned char *rk,
+ const unsigned char *key )
+{
+ asm( "movdqu (%1), %%xmm0 \n" // copy original round key
+ "movdqu %%xmm0, (%0) \n"
+ "add $16, %0 \n"
+ "movq 16(%1), %%xmm1 \n"
+ "movq %%xmm1, (%0) \n"
+ "add $8, %0 \n"
+ "jmp 2f \n" // skip auxiliary routine
+
+ /*
+ * Finish generating the next 6 quarter-keys.
+ *
+ * On entry xmm0 is r3:r2:r1:r0, xmm1 is stuff:stuff:r5:r4
+ * and xmm2 is stuff:stuff:X:stuff with X = rot( sub( r3 ) ) ^ RCON.
+ *
+ * On exit, xmm0 is r9:r8:r7:r6 and xmm1 is stuff:stuff:r11:r10
+ * and those are written to the round key buffer.
+ */
+ "1: \n"
+ "pshufd $0x55, %%xmm2, %%xmm2 \n" // X:X:X:X
+ "pxor %%xmm0, %%xmm2 \n" // X+r3:X+r2:X+r1:r4
+ "pslldq $4, %%xmm0 \n" // etc
+ "pxor %%xmm0, %%xmm2 \n"
+ "pslldq $4, %%xmm0 \n"
+ "pxor %%xmm0, %%xmm2 \n"
+ "pslldq $4, %%xmm0 \n"
+ "pxor %%xmm2, %%xmm0 \n" // update xmm0 = r9:r8:r7:r6
+ "movdqu %%xmm0, (%0) \n"
+ "add $16, %0 \n"
+ "pshufd $0xff, %%xmm0, %%xmm2 \n" // r9:r9:r9:r9
+ "pxor %%xmm1, %%xmm2 \n" // stuff:stuff:r9+r5:r10
+ "pslldq $4, %%xmm1 \n" // r2:r1:r0:0
+ "pxor %%xmm2, %%xmm1 \n" // update xmm1 = stuff:stuff:r11:r10
+ "movq %%xmm1, (%0) \n"
+ "add $8, %0 \n"
+ "ret \n"
+
+ "2: \n"
+ "aeskeygenassist $0x01, %%xmm1, %%xmm2 \ncall 1b \n"
+ "aeskeygenassist $0x02, %%xmm1, %%xmm2 \ncall 1b \n"
+ "aeskeygenassist $0x04, %%xmm1, %%xmm2 \ncall 1b \n"
+ "aeskeygenassist $0x08, %%xmm1, %%xmm2 \ncall 1b \n"
+ "aeskeygenassist $0x10, %%xmm1, %%xmm2 \ncall 1b \n"
+ "aeskeygenassist $0x20, %%xmm1, %%xmm2 \ncall 1b \n"
+ "aeskeygenassist $0x40, %%xmm1, %%xmm2 \ncall 1b \n"
+ "aeskeygenassist $0x80, %%xmm1, %%xmm2 \ncall 1b \n"
+
+ :
+ : "r" (rk), "r" (key)
+ : "memory", "cc", "0" );
+}
+
+/*
+ * Key expansion, 256-bit case
+ */
+static void aesni_setkey_enc_256( unsigned char *rk,
+ const unsigned char *key )
+{
+ asm( "movdqu (%1), %%xmm0 \n"
+ "movdqu %%xmm0, (%0) \n"
+ "add $16, %0 \n"
+ "movdqu 16(%1), %%xmm1 \n"
+ "movdqu %%xmm1, (%0) \n"
+ "jmp 2f \n" // skip auxiliary routine
+
+ /*
+ * Finish generating the next two round keys.
+ *
+ * On entry xmm0 is r3:r2:r1:r0, xmm1 is r7:r6:r5:r4 and
+ * xmm2 is X:stuff:stuff:stuff with X = rot( sub( r7 )) ^ RCON
+ *
+ * On exit, xmm0 is r11:r10:r9:r8 and xmm1 is r15:r14:r13:r12
+ * and those have been written to the output buffer.
+ */
+ "1: \n"
+ "pshufd $0xff, %%xmm2, %%xmm2 \n"
+ "pxor %%xmm0, %%xmm2 \n"
+ "pslldq $4, %%xmm0 \n"
+ "pxor %%xmm0, %%xmm2 \n"
+ "pslldq $4, %%xmm0 \n"
+ "pxor %%xmm0, %%xmm2 \n"
+ "pslldq $4, %%xmm0 \n"
+ "pxor %%xmm2, %%xmm0 \n"
+ "add $16, %0 \n"
+ "movdqu %%xmm0, (%0) \n"
+
+ /* Set xmm2 to stuff:Y:stuff:stuff with Y = subword( r11 )
+ * and proceed to generate next round key from there */
+ "aeskeygenassist $0, %%xmm0, %%xmm2\n"
+ "pshufd $0xaa, %%xmm2, %%xmm2 \n"
+ "pxor %%xmm1, %%xmm2 \n"
+ "pslldq $4, %%xmm1 \n"
+ "pxor %%xmm1, %%xmm2 \n"
+ "pslldq $4, %%xmm1 \n"
+ "pxor %%xmm1, %%xmm2 \n"
+ "pslldq $4, %%xmm1 \n"
+ "pxor %%xmm2, %%xmm1 \n"
+ "add $16, %0 \n"
+ "movdqu %%xmm1, (%0) \n"
+ "ret \n"
+
+ /*
+ * Main "loop" - Generating one more key than necessary,
+ * see definition of aes_context.buf
+ */
+ "2: \n"
+ "aeskeygenassist $0x01, %%xmm1, %%xmm2 \ncall 1b \n"
+ "aeskeygenassist $0x02, %%xmm1, %%xmm2 \ncall 1b \n"
+ "aeskeygenassist $0x04, %%xmm1, %%xmm2 \ncall 1b \n"
+ "aeskeygenassist $0x08, %%xmm1, %%xmm2 \ncall 1b \n"
+ "aeskeygenassist $0x10, %%xmm1, %%xmm2 \ncall 1b \n"
+ "aeskeygenassist $0x20, %%xmm1, %%xmm2 \ncall 1b \n"
+ "aeskeygenassist $0x40, %%xmm1, %%xmm2 \ncall 1b \n"
+ :
+ : "r" (rk), "r" (key)
+ : "memory", "cc", "0" );
+}
+
+/*
+ * Key expansion, wrapper
+ */
+int aesni_setkey_enc( unsigned char *rk,
+ const unsigned char *key,
+ size_t bits )
+{
+ switch( bits )
+ {
+ case 128: aesni_setkey_enc_128( rk, key ); break;
+ case 192: aesni_setkey_enc_192( rk, key ); break;
+ case 256: aesni_setkey_enc_256( rk, key ); break;
+ default : return( POLARSSL_ERR_AES_INVALID_KEY_LENGTH );
+ }
+
+ return( 0 );
+}
+
+#endif /* POLARSSL_HAVE_X86_64 */
+
+#endif /* POLARSSL_AESNI_C */
diff --git a/library/ecp.c b/library/ecp.c
index 3adab15..8b34bf3 100644
--- a/library/ecp.c
+++ b/library/ecp.c
@@ -1870,7 +1870,7 @@
mul_count != mul_c_prev )
{
if( verbose != 0 )
- printf( "failed (%zu)\n", i );
+ printf( "failed (%u)\n", (unsigned int) i );
ret = 1;
goto cleanup;
@@ -1907,7 +1907,7 @@
mul_count != mul_c_prev )
{
if( verbose != 0 )
- printf( "failed (%zu)\n", i );
+ printf( "failed (%u)\n", (unsigned int) i );
ret = 1;
goto cleanup;
diff --git a/library/gcm.c b/library/gcm.c
index a9e18c8..8950360 100644
--- a/library/gcm.c
+++ b/library/gcm.c
@@ -22,15 +22,27 @@
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
+
/*
- * http://csrc.nist.gov/publications/nistpubs/800-38D/SP-800-38D.pdf
+ * http://csrc.nist.gov/publications/nistpubs/800-38D/SP-800-38D.pdf
+ *
+ * See also:
+ * [MGV] http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-revised-spec.pdf
+ *
+ * We use the algorithm described as Shoup's method with 4-bit tables in
+ * [MGV] 4.1, pp. 12-13, to enhance speed without using too much memory.
*/
+
#include "polarssl/config.h"
#if defined(POLARSSL_GCM_C)
#include "polarssl/gcm.h"
+#if defined(POLARSSL_AESNI_C)
+#include "polarssl/aesni.h"
+#endif
+
/*
* 32-bit integer manipulation macros (big endian)
*/
@@ -54,6 +66,14 @@
}
#endif
+/*
+ * Precompute small multiples of H, that is set
+ * HH[i] || HL[i] = H times i,
+ * where i is seen as a field element as in [MGV], ie high-order bits
+ * correspond to low powers of P. The result is stored in the same way, that
+ * is the high-order bit of HH corresponds to P^0 and the low-order bit of HL
+ * corresponds to P^127.
+ */
static int gcm_gen_table( gcm_context *ctx )
{
int ret, i, j;
@@ -66,9 +86,7 @@
if( ( ret = cipher_update( &ctx->cipher_ctx, h, 16, h, &olen ) ) != 0 )
return( ret );
- ctx->HH[0] = 0;
- ctx->HL[0] = 0;
-
+ /* pack h as two 64-bits ints, big-endian */
GET_UINT32_BE( hi, h, 0 );
GET_UINT32_BE( lo, h, 4 );
vh = (uint64_t) hi << 32 | lo;
@@ -77,9 +95,20 @@
GET_UINT32_BE( lo, h, 12 );
vl = (uint64_t) hi << 32 | lo;
+ /* 8 = 1000 corresponds to 1 in GF(2^128) */
ctx->HL[8] = vl;
ctx->HH[8] = vh;
+#if defined(POLARSSL_AESNI_C) && defined(POLARSSL_HAVE_X86_64)
+ /* With CLMUL support, we need only h, not the rest of the table */
+ if( aesni_supports( POLARSSL_AESNI_CLMUL ) )
+ return( 0 );
+#endif
+
+ /* 0 corresponds to 0 in GF(2^128) */
+ ctx->HH[0] = 0;
+ ctx->HL[0] = 0;
+
for( i = 4; i > 0; i >>= 1 )
{
uint32_t T = ( vl & 1 ) * 0xe1000000U;
@@ -135,6 +164,11 @@
return( 0 );
}
+/*
+ * Shoup's method for multiplication use this table with
+ * last4[x] = x times P^128
+ * where x and last4[x] are seen as elements of GF(2^128) as in [MGV]
+ */
static const uint64_t last4[16] =
{
0x0000, 0x1c20, 0x3840, 0x2460,
@@ -143,6 +177,10 @@
0x9180, 0x8da0, 0xa9c0, 0xb5e0
};
+/*
+ * Sets output to x times H using the precomputed tables.
+ * x and output are seen as elements of GF(2^128) as in [MGV].
+ */
static void gcm_mult( gcm_context *ctx, const unsigned char x[16],
unsigned char output[16] )
{
@@ -151,6 +189,20 @@
unsigned char lo, hi, rem;
uint64_t zh, zl;
+#if defined(POLARSSL_AESNI_C) && defined(POLARSSL_HAVE_X86_64)
+ if( aesni_supports( POLARSSL_AESNI_CLMUL ) ) {
+ unsigned char h[16];
+
+ PUT_UINT32_BE( ctx->HH[8] >> 32, h, 0 );
+ PUT_UINT32_BE( ctx->HH[8], h, 4 );
+ PUT_UINT32_BE( ctx->HL[8] >> 32, h, 8 );
+ PUT_UINT32_BE( ctx->HL[8], h, 12 );
+
+ aesni_gcm_mult( output, x, h );
+ return;
+ }
+#endif
+
memset( z, 0x00, 16 );
lo = x[15] & 0xf;
diff --git a/library/net.c b/library/net.c
index fd5fd12..67080a2 100644
--- a/library/net.c
+++ b/library/net.c
@@ -40,11 +40,13 @@
#include <winsock2.h>
#include <windows.h>
+#if defined(_MSC_VER)
#if defined(_WIN32_WCE)
#pragma comment( lib, "ws2.lib" )
#else
#pragma comment( lib, "ws2_32.lib" )
#endif
+#endif /* _MSC_VER */
#define read(fd,buf,len) recv(fd,(char*)buf,(int) len,0)
#define write(fd,buf,len) send(fd,(char*)buf,(int) len,0)
diff --git a/library/padlock.c b/library/padlock.c
index a7b4c0c..6e94532 100644
--- a/library/padlock.c
+++ b/library/padlock.c
@@ -43,7 +43,7 @@
int padlock_supports( int feature )
{
static int flags = -1;
- int ebx, edx;
+ int ebx = 0, edx = 0;
if( flags == -1 )
{
@@ -76,7 +76,7 @@
const unsigned char input[16],
unsigned char output[16] )
{
- int ebx;
+ int ebx = 0;
uint32_t *rk;
uint32_t *blk;
uint32_t *ctrl;
@@ -117,7 +117,7 @@
const unsigned char *input,
unsigned char *output )
{
- int ebx;
+ int ebx = 0;
size_t count;
uint32_t *rk;
uint32_t *iw;
diff --git a/library/ssl_tls.c b/library/ssl_tls.c
index 75ba907..6ea2821 100644
--- a/library/ssl_tls.c
+++ b/library/ssl_tls.c
@@ -1610,6 +1610,21 @@
size_t pad_count = 0, real_count = 1;
size_t padding_idx = ssl->in_msglen - padlen - 1;
+ /*
+ * Padding is guaranteed to be incorrect if:
+ * 1. padlen - 1 > ssl->in_msglen
+ *
+ * 2. ssl->in_msglen + padlen >
+ * SSL_MAX_CONTENT_LEN + 256 (max padding)
+ *
+ * In both cases we reset padding_idx to a safe value (0) to
+ * prevent out-of-buffer reads.
+ */
+ correct &= ( ssl->in_msglen >= padlen - 1 );
+ correct &= ( ssl->in_msglen + padlen <= SSL_MAX_CONTENT_LEN + 256 );
+
+ padding_idx *= correct;
+
for( i = 1; i <= 256; i++ )
{
real_count &= ( i <= padlen );
diff --git a/programs/Makefile b/programs/Makefile
index 6cee0f1..9238ae8 100644
--- a/programs/Makefile
+++ b/programs/Makefile
@@ -49,6 +49,10 @@
APPS += test/o_p_test
endif
+ifdef PTHREAD
+APPS += ssl/ssl_pthread_server
+endif
+
.SILENT:
all: $(APPS)
@@ -189,6 +193,10 @@
echo " CC ssl/ssl_fork_server.c"
$(CC) $(CFLAGS) $(OFLAGS) ssl/ssl_fork_server.c $(LDFLAGS) -o $@
+ssl/ssl_pthread_server: ssl/ssl_pthread_server.c ../library/libpolarssl.a
+ echo " CC ssl/ssl_pthread_server.c"
+ $(CC) $(CFLAGS) $(OFLAGS) ssl/ssl_pthread_server.c $(LDFLAGS) -o $@ -lpthread
+
ssl/ssl_mail_client: ssl/ssl_mail_client.c ../library/libpolarssl.a
echo " CC ssl/ssl_mail_client.c"
$(CC) $(CFLAGS) $(OFLAGS) ssl/ssl_mail_client.c $(LDFLAGS) -o $@
diff --git a/programs/pkey/ecdsa.c b/programs/pkey/ecdsa.c
index 3cba7e8..3dccfeb 100644
--- a/programs/pkey/ecdsa.c
+++ b/programs/pkey/ecdsa.c
@@ -162,7 +162,7 @@
printf( " failed\n ! ecdsa_genkey returned %d\n", ret );
goto exit;
}
- printf( " ok (signature length = %zu)\n", sig_len );
+ printf( " ok (signature length = %u)\n", (unsigned int) sig_len );
dump_buf( " + Hash: ", hash, sizeof hash );
dump_buf( " + Signature: ", sig, sig_len );
diff --git a/programs/ssl/CMakeLists.txt b/programs/ssl/CMakeLists.txt
index a117b58..e0cfba0 100644
--- a/programs/ssl/CMakeLists.txt
+++ b/programs/ssl/CMakeLists.txt
@@ -1,7 +1,18 @@
+set(THREADS_USE_PTHREADS_WIN32 true)
+find_package(Threads)
+
set(libs
polarssl
)
+set(targets
+ ssl_client1
+ ssl_client2
+ ssl_server
+ ssl_fork_server
+ ssl_mail_client
+)
+
if(USE_PKCS11_HELPER_LIBRARY)
set(libs ${libs} pkcs11-helper)
endif(USE_PKCS11_HELPER_LIBRARY)
@@ -28,6 +39,12 @@
add_executable(ssl_mail_client ssl_mail_client.c)
target_link_libraries(ssl_mail_client ${libs})
-install(TARGETS ssl_client1 ssl_client2 ssl_server ssl_fork_server ssl_mail_client
+if(THREADS_FOUND)
+ add_executable(ssl_pthread_server ssl_pthread_server.c)
+ target_link_libraries(ssl_pthread_server ${libs} ${CMAKE_THREAD_LIBS_INIT})
+ set(targets ${targets} ssl_pthread_server)
+endif(THREADS_FOUND)
+
+install(TARGETS ${targets}
DESTINATION "bin"
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
diff --git a/programs/ssl/ssl_mail_client.c b/programs/ssl/ssl_mail_client.c
index a3e9f2b..6abc073 100644
--- a/programs/ssl/ssl_mail_client.c
+++ b/programs/ssl/ssl_mail_client.c
@@ -42,11 +42,13 @@
#include <winsock2.h>
#include <windows.h>
+#if defined(_MSC_VER)
#if defined(_WIN32_WCE)
#pragma comment( lib, "ws2.lib" )
#else
#pragma comment( lib, "ws2_32.lib" )
#endif
+#endif /* _MSC_VER */
#endif
#include "polarssl/base64.h"
diff --git a/programs/ssl/ssl_pthread_server.c b/programs/ssl/ssl_pthread_server.c
new file mode 100644
index 0000000..7e1b52e
--- /dev/null
+++ b/programs/ssl/ssl_pthread_server.c
@@ -0,0 +1,491 @@
+/*
+ * SSL server demonstration program using pthread for handling multiple
+ * clients.
+ *
+ * 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.
+ */
+
+#include "polarssl/config.h"
+
+#if defined(_WIN32)
+#include <windows.h>
+#endif
+
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "polarssl/entropy.h"
+#include "polarssl/ctr_drbg.h"
+#include "polarssl/certs.h"
+#include "polarssl/x509.h"
+#include "polarssl/ssl.h"
+#include "polarssl/net.h"
+#include "polarssl/error.h"
+
+#if defined(POLARSSL_SSL_CACHE_C)
+#include "polarssl/ssl_cache.h"
+#endif
+
+#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
+#include "polarssl/memory.h"
+#endif
+
+#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_CERTS_C) || \
+ !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_SSL_TLS_C) || \
+ !defined(POLARSSL_SSL_SRV_C) || !defined(POLARSSL_NET_C) || \
+ !defined(POLARSSL_RSA_C) || !defined(POLARSSL_CTR_DRBG_C) || \
+ !defined(POLARSSL_X509_CRT_PARSE_C) || \
+ !defined(POLARSSL_THREADING_C) || !defined(POLARSSL_THREADING_PTHREAD)
+int main( int argc, char *argv[] )
+{
+ ((void) argc);
+ ((void) argv);
+
+ printf("POLARSSL_BIGNUM_C and/or POLARSSL_CERTS_C and/or POLARSSL_ENTROPY_C "
+ "and/or POLARSSL_SSL_TLS_C and/or POLARSSL_SSL_SRV_C and/or "
+ "POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
+ "POLARSSL_CTR_DRBG_C and/or POLARSSL_X509_CRT_PARSE_C and/or "
+ "POLARSSL_THREADING_C and/or POLARSSL_THREADING_PTHREAD "
+ "not defined.\n");
+ return( 0 );
+}
+#else
+
+#define HTTP_RESPONSE \
+ "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
+ "<h2>PolarSSL Test Server</h2>\r\n" \
+ "<p>Successful connection using: %s</p>\r\n"
+
+#define DEBUG_LEVEL 0
+
+threading_mutex_t debug_mutex;
+
+static void my_mutexed_debug( void *ctx, int level, const char *str )
+{
+ polarssl_mutex_lock( &debug_mutex );
+ if( level < DEBUG_LEVEL )
+ {
+ fprintf( (FILE *) ctx, "%s", str );
+ fflush( (FILE *) ctx );
+ }
+ polarssl_mutex_unlock( &debug_mutex );
+}
+
+typedef struct {
+ int client_fd;
+ int thread_complete;
+ entropy_context *entropy;
+#if defined(POLARSSL_SSL_CACHE_C)
+ ssl_cache_context *cache;
+#endif
+ x509_crt *ca_chain;
+ x509_crt *server_cert;
+ pk_context *server_key;
+} thread_info_t;
+
+typedef struct {
+ int active;
+ thread_info_t data;
+ pthread_t thread;
+} pthread_info_t;
+
+#define MAX_NUM_THREADS 5
+
+static thread_info_t base_info;
+static pthread_info_t threads[MAX_NUM_THREADS];
+
+static void *handle_ssl_connection( void *data )
+{
+ int ret, len;
+ thread_info_t *thread_info = (thread_info_t *) data;
+ int client_fd = thread_info->client_fd;
+ int thread_id = (int) pthread_self();
+ unsigned char buf[1024];
+ char pers[50];
+ ssl_context ssl;
+ ctr_drbg_context ctr_drbg;
+
+ snprintf( pers, sizeof(pers), "SSL Pthread Thread %d", thread_id );
+ printf( " [ #%d ] Client FD %d\n", thread_id, client_fd );
+ printf( " [ #%d ] Seeding the random number generator...\n", thread_id );
+
+ /* entropy_func() is thread-safe if POLARSSL_THREADING_C is set
+ */
+ if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, thread_info->entropy,
+ (const unsigned char *) pers,
+ strlen( pers ) ) ) != 0 )
+ {
+ printf( " [ #%d ] failed: ctr_drbg_init returned -0x%04x\n",
+ thread_id, -ret );
+ goto thread_exit;
+ }
+
+ printf( " [ #%d ] ok\n", thread_id );
+
+ /*
+ * 4. Setup stuff
+ */
+ printf( " [ #%d ] Setting up the SSL data....\n", thread_id );
+
+ if( ( ret = ssl_init( &ssl ) ) != 0 )
+ {
+ printf( " [ #%d ] failed: ssl_init returned -0x%04x\n",
+ thread_id, -ret );
+ goto thread_exit;
+ }
+
+ ssl_set_endpoint( &ssl, SSL_IS_SERVER );
+ ssl_set_authmode( &ssl, SSL_VERIFY_NONE );
+
+ ssl_set_rng( &ssl, ctr_drbg_random, &ctr_drbg );
+ ssl_set_dbg( &ssl, my_mutexed_debug, stdout );
+
+ /* ssl_cache_get() and ssl_cache_set() are thread-safe if
+ * POLARSSL_THREADING_C is set.
+ */
+#if defined(POLARSSL_SSL_CACHE_C)
+ ssl_set_session_cache( &ssl, ssl_cache_get, thread_info->cache,
+ ssl_cache_set, thread_info->cache );
+#endif
+
+ ssl_set_ca_chain( &ssl, thread_info->ca_chain, NULL, NULL );
+ ssl_set_own_cert( &ssl, thread_info->server_cert, thread_info->server_key );
+
+ printf( " [ #%d ] ok\n", thread_id );
+
+ ssl_set_bio( &ssl, net_recv, &client_fd,
+ net_send, &client_fd );
+
+ printf( " [ #%d ] ok\n", thread_id );
+
+ /*
+ * 5. Handshake
+ */
+ printf( " [ #%d ] Performing the SSL/TLS handshake\n", thread_id );
+
+ while( ( ret = ssl_handshake( &ssl ) ) != 0 )
+ {
+ if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
+ {
+ printf( " [ #%d ] failed: ssl_handshake returned -0x%04x\n",
+ thread_id, -ret );
+ goto thread_exit;
+ }
+ }
+
+ printf( " [ #%d ] ok\n", thread_id );
+
+ /*
+ * 6. Read the HTTP Request
+ */
+ printf( " [ #%d ] < Read from client\n", thread_id );
+
+ do
+ {
+ len = sizeof( buf ) - 1;
+ memset( buf, 0, sizeof( buf ) );
+ ret = ssl_read( &ssl, buf, len );
+
+ if( ret == POLARSSL_ERR_NET_WANT_READ || ret == POLARSSL_ERR_NET_WANT_WRITE )
+ continue;
+
+ if( ret <= 0 )
+ {
+ switch( ret )
+ {
+ case POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY:
+ printf( " [ #%d ] connection was closed gracefully\n",
+ thread_id );
+ goto thread_exit;
+
+ case POLARSSL_ERR_NET_CONN_RESET:
+ printf( " [ #%d ] connection was reset by peer\n",
+ thread_id );
+ goto thread_exit;
+
+ default:
+ printf( " [ #%d ] ssl_read returned -0x%04x\n",
+ thread_id, -ret );
+ goto thread_exit;
+ }
+
+ break;
+ }
+
+ len = ret;
+ printf( " [ #%d ] %d bytes read\n=====\n%s\n=====\n",
+ thread_id, len, (char *) buf );
+
+ if( ret > 0 )
+ break;
+ }
+ while( 1 );
+
+ /*
+ * 7. Write the 200 Response
+ */
+ printf( " [ #%d ] > Write to client:\n", thread_id );
+
+ len = sprintf( (char *) buf, HTTP_RESPONSE,
+ ssl_get_ciphersuite( &ssl ) );
+
+ while( ( ret = ssl_write( &ssl, buf, len ) ) <= 0 )
+ {
+ if( ret == POLARSSL_ERR_NET_CONN_RESET )
+ {
+ printf( " [ #%d ] failed: peer closed the connection\n",
+ thread_id );
+ goto thread_exit;
+ }
+
+ if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
+ {
+ printf( " [ #%d ] failed: ssl_write returned -0x%04x\n",
+ thread_id, ret );
+ goto thread_exit;
+ }
+ }
+
+ len = ret;
+ printf( " [ #%d ] %d bytes written\n=====\n%s\n=====\n",
+ thread_id, len, (char *) buf );
+
+ ret = 0;
+
+thread_exit:
+
+#ifdef POLARSSL_ERROR_C
+ if( ret != 0 )
+ {
+ char error_buf[100];
+ polarssl_strerror( ret, error_buf, 100 );
+ printf(" [ #%d ] Last error was: -0x%04x - %s\n\n",
+ thread_id, -ret, error_buf );
+ }
+#endif
+
+ net_close( client_fd );
+ ssl_free( &ssl );
+
+ thread_info->thread_complete = 1;
+
+ return( NULL );
+}
+
+static int thread_create( int client_fd )
+{
+ int ret, i;
+
+ /*
+ * Find in-active or finished thread slot
+ */
+ for( i = 0; i < MAX_NUM_THREADS; i++ )
+ {
+ if( threads[i].active == 0 )
+ break;
+
+ if( threads[i].data.thread_complete == 1 )
+ {
+ printf( " [ main ] Cleaning up thread %d\n", i );
+ pthread_join(threads[i].thread, NULL );
+ memset( &threads[i], 0, sizeof(pthread_info_t) );
+ break;
+ }
+ }
+
+ if( i == MAX_NUM_THREADS )
+ return( -1 );
+
+ /*
+ * Fill thread-info for thread
+ */
+ memcpy( &threads[i].data, &base_info, sizeof(base_info) );
+ threads[i].active = 1;
+ threads[i].data.client_fd = client_fd;
+
+ if( ( ret = pthread_create( &threads[i].thread, NULL, handle_ssl_connection, &threads[i].data ) ) != 0 )
+ {
+ return( ret );
+ }
+
+ return( 0 );
+}
+
+int main( int argc, char *argv[] )
+{
+ int ret;
+ int listen_fd;
+ int client_fd = -1;
+
+ entropy_context entropy;
+ x509_crt srvcert;
+ pk_context pkey;
+#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
+ unsigned char alloc_buf[100000];
+#endif
+#if defined(POLARSSL_SSL_CACHE_C)
+ ssl_cache_context cache;
+#endif
+
+ ((void) argc);
+ ((void) argv);
+
+#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
+ memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
+#endif
+
+#if defined(POLARSSL_SSL_CACHE_C)
+ ssl_cache_init( &cache );
+ base_info.cache = &cache;
+#endif
+
+ memset( threads, 0, sizeof(threads) );
+
+ polarssl_mutex_init( &debug_mutex );
+
+ /*
+ * We use only a single entropy source that is used in all the threads.
+ */
+ entropy_init( &entropy );
+ base_info.entropy = &entropy;
+
+ /*
+ * 1. Load the certificates and private RSA key
+ */
+ printf( "\n . Loading the server cert. and key..." );
+ fflush( stdout );
+
+ x509_crt_init( &srvcert );
+
+ /*
+ * This demonstration program uses embedded test certificates.
+ * Instead, you may want to use x509_crt_parse_file() to read the
+ * server and CA certificates, as well as pk_parse_keyfile().
+ */
+ ret = x509_crt_parse( &srvcert, (const unsigned char *) test_srv_crt,
+ strlen( test_srv_crt ) );
+ if( ret != 0 )
+ {
+ printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
+ goto exit;
+ }
+
+ ret = x509_crt_parse( &srvcert, (const unsigned char *) test_ca_list,
+ strlen( test_ca_list ) );
+ if( ret != 0 )
+ {
+ printf( " failed\n ! x509_crt_parse returned %d\n\n", ret );
+ goto exit;
+ }
+
+ pk_init( &pkey );
+ ret = pk_parse_key( &pkey, (const unsigned char *) test_srv_key,
+ strlen( test_srv_key ), NULL, 0 );
+ if( ret != 0 )
+ {
+ printf( " failed\n ! pk_parse_key returned %d\n\n", ret );
+ goto exit;
+ }
+
+ base_info.ca_chain = srvcert.next;
+ base_info.server_cert = &srvcert;
+ base_info.server_key = &pkey;
+
+ printf( " ok\n" );
+
+ /*
+ * 2. Setup the listening TCP socket
+ */
+ printf( " . Bind on https://localhost:4433/ ..." );
+ fflush( stdout );
+
+ if( ( ret = net_bind( &listen_fd, NULL, 4433 ) ) != 0 )
+ {
+ printf( " failed\n ! net_bind returned %d\n\n", ret );
+ goto exit;
+ }
+
+ printf( " ok\n" );
+
+reset:
+#ifdef POLARSSL_ERROR_C
+ if( ret != 0 )
+ {
+ char error_buf[100];
+ polarssl_strerror( ret, error_buf, 100 );
+ printf( " [ main ] Last error was: -0x%04x - %s\n", -ret, error_buf );
+ }
+#endif
+
+ /*
+ * 3. Wait until a client connects
+ */
+ client_fd = -1;
+
+ printf( " [ main ] Waiting for a remote connection\n" );
+
+ if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
+ {
+ printf( " [ main ] failed: net_accept returned -0x%04x\n", ret );
+ goto exit;
+ }
+
+ printf( " [ main ] ok\n" );
+ printf( " [ main ] Creating a new thread\n" );
+
+ if( ( ret = thread_create( client_fd ) ) != 0 )
+ {
+ printf( " [ main ] failed: thread_create returned %d\n", ret );
+ net_close( client_fd );
+ goto reset;
+ }
+
+ ret = 0;
+ goto reset;
+
+exit:
+ x509_crt_free( &srvcert );
+ pk_free( &pkey );
+#if defined(POLARSSL_SSL_CACHE_C)
+ ssl_cache_free( &cache );
+#endif
+ entropy_free( &entropy );
+
+ polarssl_mutex_free( &debug_mutex );
+
+#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
+ memory_buffer_alloc_free();
+#endif
+
+#if defined(_WIN32)
+ printf( " Press Enter to exit this program.\n" );
+ fflush( stdout ); getchar();
+#endif
+
+ return( ret );
+}
+
+#endif /* POLARSSL_BIGNUM_C && POLARSSL_CERTS_C && POLARSSL_ENTROPY_C &&
+ POLARSSL_SSL_TLS_C && POLARSSL_SSL_SRV_C && POLARSSL_NET_C &&
+ POLARSSL_RSA_C && POLARSSL_CTR_DRBG_C && POLARSSL_THREADING_C &&
+ POLARSSL_THREADING_PTHREAD */
diff --git a/tests/suites/test_suite_x509write.function b/tests/suites/test_suite_x509write.function
index 8b57f8c..ca097e7 100644
--- a/tests/suites/test_suite_x509write.function
+++ b/tests/suites/test_suite_x509write.function
@@ -21,7 +21,7 @@
int ret;
size_t olen = 0, pem_len = 0;
FILE *f;
- char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1";
+ const char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1";
rnd_pseudo_info rnd_info;
memset( &rnd_info, 0x2a, sizeof( rnd_pseudo_info ) );
diff --git a/visualc/VS2010/PolarSSL.vcxproj b/visualc/VS2010/PolarSSL.vcxproj
index 33f0031..4c067be 100644
--- a/visualc/VS2010/PolarSSL.vcxproj
+++ b/visualc/VS2010/PolarSSL.vcxproj
@@ -143,6 +143,7 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="..\..\include\polarssl\aes.h" />
+ <ClInclude Include="..\..\include\polarssl\aesni.h" />
<ClInclude Include="..\..\include\polarssl\arc4.h" />
<ClInclude Include="..\..\include\polarssl\asn1.h" />
<ClInclude Include="..\..\include\polarssl\asn1write.h" />
@@ -201,6 +202,7 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\library\aes.c" />
+ <ClCompile Include="..\..\library\aesni.c" />
<ClCompile Include="..\..\library\arc4.c" />
<ClCompile Include="..\..\library\asn1parse.c" />
<ClCompile Include="..\..\library\asn1write.c" />
diff --git a/visualc/VS6/polarssl.dsp b/visualc/VS6/polarssl.dsp
index c40b75b..b6a82c0 100644
--- a/visualc/VS6/polarssl.dsp
+++ b/visualc/VS6/polarssl.dsp
@@ -89,6 +89,10 @@
# End Source File
# Begin Source File
+SOURCE=..\..\library\aesni.c
+# End Source File
+# Begin Source File
+
SOURCE=..\..\library\arc4.c
# End Source File
# Begin Source File
@@ -349,6 +353,10 @@
# End Source File
# Begin Source File
+SOURCE=..\..\include\polarssl\aesni.h
+# End Source File
+# Begin Source File
+
SOURCE=..\..\include\polarssl\arc4.h
# End Source File
# Begin Source File