From 92ac76f9db9c4faa29f6fb31c04fc5c1b693dcb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Mon, 16 Dec 2013 17:12:53 +0100 Subject: [PATCH 01/12] Add files for (upcoming) AES-NI support --- include/polarssl/aesni.h | 49 ++++++++++++++++++++++++++ include/polarssl/config.h | 24 +++++++++++++ library/CMakeLists.txt | 1 + library/Makefile | 3 +- library/aesni.c | 61 +++++++++++++++++++++++++++++++++ visualc/VS2010/PolarSSL.vcxproj | 2 ++ visualc/VS6/polarssl.dsp | 8 +++++ 7 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 include/polarssl/aesni.h create mode 100644 library/aesni.c diff --git a/include/polarssl/aesni.h b/include/polarssl/aesni.h new file mode 100644 index 000000000..9aaeff196 --- /dev/null +++ b/include/polarssl/aesni.h @@ -0,0 +1,49 @@ +/** + * \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 + * + * 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" + +#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 detection routine + * + * \return 1 if CPU supports AES-NI, 0 otherwise + */ +int aesni_supported( void ); + +#endif /* POLARSSL_HAVE_X86_64 */ + +#endif /* POLARSSL_AESNI_H */ diff --git a/include/polarssl/config.h b/include/polarssl/config.h index d98bdb34f..283e294a7 100644 --- a/include/polarssl/config.h +++ b/include/polarssl/config.h @@ -874,6 +874,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 * @@ -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 @@ -1902,6 +1918,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 @@ -2012,6 +2032,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/library/CMakeLists.txt b/library/CMakeLists.txt index 411c07d80..1a5efde1f 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -2,6 +2,7 @@ option(USE_SHARED_POLARSSL_LIBRARY "Build PolarSSL as a shared library." OFF) set(src aes.c + aesni.c arc4.c asn1parse.c asn1write.c diff --git a/library/Makefile b/library/Makefile index a9c86f84c..3f80162b5 100644 --- a/library/Makefile +++ b/library/Makefile @@ -34,7 +34,8 @@ DLEXT=dll 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/aesni.c b/library/aesni.c new file mode 100644 index 000000000..2882298e0 --- /dev/null +++ b/library/aesni.c @@ -0,0 +1,61 @@ +/* + * AES-NI support functions + * + * Copyright (C) 2013, Brainspark B.V. + * + * This file is part of PolarSSL (http://www.polarssl.org) + * Lead Maintainer: Paul Bakker + * + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +/* + * [AES-WP] http://software.intel.com/en-us/articles/intel-advanced-encryption-standard-aes-instructions-set + */ + +#include "polarssl/config.h" + +#if defined(POLARSSL_AESNI_C) + +#include "polarssl/aesni.h" + +#if defined(POLARSSL_HAVE_X86_64) + +/* + * AES-NI support detection routine, [AES-WP] figure 23 + */ +int aesni_supported( void ) +{ + static int supported = -1; + unsigned int c; + + if( supported == -1 ) + { + asm( "movl $1, %%eax \n" + "cpuid \n" + : "=c" (c) + : + : "eax", "ebx", "edx" ); + supported = ( ( c & 0x02000000 ) != 0 ); + } + + return( supported ); +} + +#endif /* POLARSSL_HAVE_X86_64 */ + +#endif /* POLARSSL_AESNI_C */ diff --git a/visualc/VS2010/PolarSSL.vcxproj b/visualc/VS2010/PolarSSL.vcxproj index 33f00312b..4c067beda 100644 --- a/visualc/VS2010/PolarSSL.vcxproj +++ b/visualc/VS2010/PolarSSL.vcxproj @@ -143,6 +143,7 @@ + @@ -201,6 +202,7 @@ + diff --git a/visualc/VS6/polarssl.dsp b/visualc/VS6/polarssl.dsp index c40b75b18..b6a82c0b9 100644 --- a/visualc/VS6/polarssl.dsp +++ b/visualc/VS6/polarssl.dsp @@ -89,6 +89,10 @@ SOURCE=..\..\library\aes.c # 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 @@ SOURCE=..\..\include\polarssl\aes.h # 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 From 5b685653ef16f174728ee0b3680286cdbdeb4263 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Wed, 18 Dec 2013 11:45:21 +0100 Subject: [PATCH 02/12] Add aesni_crypt_ecb() and use it --- include/polarssl/aes.h | 1 + include/polarssl/aesni.h | 17 ++++++++++++++- library/aes.c | 8 +++++++ library/aesni.c | 45 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 1 deletion(-) diff --git a/include/polarssl/aes.h b/include/polarssl/aes.h index 1b93e2a3d..0eb25dfff 100644 --- a/include/polarssl/aes.h +++ b/include/polarssl/aes.h @@ -38,6 +38,7 @@ typedef UINT32 uint32_t; #include #endif +/* padlock.c and aesni.c rely on these values! */ #define AES_ENCRYPT 1 #define AES_DECRYPT 0 diff --git a/include/polarssl/aesni.h b/include/polarssl/aesni.h index 9aaeff196..d6684fc6c 100644 --- a/include/polarssl/aesni.h +++ b/include/polarssl/aesni.h @@ -38,12 +38,27 @@ #if defined(POLARSSL_HAVE_X86_64) /** - * \brief AES_NI detection routine + * \brief AES-NI detection routine * * \return 1 if CPU supports AES-NI, 0 otherwise */ int aesni_supported( void ); +/** + * \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 if success, 1 if operation failed + */ +int aesni_crypt_ecb( aes_context *ctx, + int mode, + const unsigned char input[16], + unsigned char output[16] ); + #endif /* POLARSSL_HAVE_X86_64 */ #endif /* POLARSSL_AESNI_H */ diff --git a/library/aes.c b/library/aes.c index a3835cead..6d090a11d 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) @@ -673,6 +676,11 @@ int aes_crypt_ecb( aes_context *ctx, 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_supported() ) + 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 index 2882298e0..7628a0362 100644 --- a/library/aesni.c +++ b/library/aesni.c @@ -32,6 +32,7 @@ #if defined(POLARSSL_AESNI_C) #include "polarssl/aesni.h" +#include #if defined(POLARSSL_HAVE_X86_64) @@ -56,6 +57,50 @@ int aesni_supported( void ) return( supported ); } +/* + * 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 ); +} #endif /* POLARSSL_HAVE_X86_64 */ #endif /* POLARSSL_AESNI_C */ From 8eaf20b18d99d61c324d7d548a2982899b177ab2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Wed, 18 Dec 2013 19:14:53 +0100 Subject: [PATCH 03/12] Allow detection of CLMUL --- include/polarssl/aesni.h | 12 +++++++++--- library/aes.c | 2 +- library/aesni.c | 14 +++++++------- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/include/polarssl/aesni.h b/include/polarssl/aesni.h index d6684fc6c..b53c0c0c2 100644 --- a/include/polarssl/aesni.h +++ b/include/polarssl/aesni.h @@ -29,6 +29,9 @@ #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) @@ -38,11 +41,14 @@ #if defined(POLARSSL_HAVE_X86_64) /** - * \brief AES-NI detection routine + * \brief AES-NI features detection routine * - * \return 1 if CPU supports AES-NI, 0 otherwise + * \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_supported( void ); +int aesni_supports( unsigned int what ); /** * \brief AES-NI AES-ECB block en(de)cryption diff --git a/library/aes.c b/library/aes.c index 6d090a11d..d2d1c0c43 100644 --- a/library/aes.c +++ b/library/aes.c @@ -677,7 +677,7 @@ int aes_crypt_ecb( aes_context *ctx, uint32_t *RK, X0, X1, X2, X3, Y0, Y1, Y2, Y3; #if defined(POLARSSL_AESNI_C) && defined(POLARSSL_HAVE_X86_64) - if( aesni_supported() ) + if( aesni_supports( POLARSSL_AESNI_AES ) ) return( aesni_crypt_ecb( ctx, mode, input, output ) ); #endif diff --git a/library/aesni.c b/library/aesni.c index 7628a0362..9b41c36ff 100644 --- a/library/aesni.c +++ b/library/aesni.c @@ -37,24 +37,24 @@ #if defined(POLARSSL_HAVE_X86_64) /* - * AES-NI support detection routine, [AES-WP] figure 23 + * AES-NI support detection routine */ -int aesni_supported( void ) +int aesni_supports( unsigned int what ) { - static int supported = -1; - unsigned int c; + static int done = 0; + static unsigned int c = 0; - if( supported == -1 ) + if( ! done ) { asm( "movl $1, %%eax \n" "cpuid \n" : "=c" (c) : : "eax", "ebx", "edx" ); - supported = ( ( c & 0x02000000 ) != 0 ); + done = 1; } - return( supported ); + return( ( c & what ) != 0 ); } /* From 9d57482280583088d8fb3846156f59314ddd3f18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Wed, 25 Dec 2013 15:41:25 +0100 Subject: [PATCH 04/12] Add comments on GCM multiplication --- library/gcm.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/library/gcm.c b/library/gcm.c index a9e18c89d..7374dca71 100644 --- a/library/gcm.c +++ b/library/gcm.c @@ -22,9 +22,17 @@ * 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) @@ -54,6 +62,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,6 +82,7 @@ static int gcm_gen_table( gcm_context *ctx ) if( ( ret = cipher_update( &ctx->cipher_ctx, h, 16, h, &olen ) ) != 0 ) return( ret ); + /* 0 corresponds to 0 in GF(2^128) */ ctx->HH[0] = 0; ctx->HL[0] = 0; @@ -77,6 +94,7 @@ static int gcm_gen_table( gcm_context *ctx ) 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; @@ -135,6 +153,11 @@ int gcm_init( gcm_context *ctx, cipher_id_t cipher, const unsigned char *key, 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 +166,10 @@ static const uint64_t last4[16] = 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] ) { From d333f67f8cbdf1650936ba07a8756ec31f19a197 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Thu, 26 Dec 2013 11:44:46 +0100 Subject: [PATCH 05/12] Add aesni_gcm_mult() --- include/polarssl/aesni.h | 18 ++++++- library/aesni.c | 114 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+), 1 deletion(-) diff --git a/include/polarssl/aesni.h b/include/polarssl/aesni.h index b53c0c0c2..f3f9fc31d 100644 --- a/include/polarssl/aesni.h +++ b/include/polarssl/aesni.h @@ -58,13 +58,29 @@ int aesni_supports( unsigned int what ); * \param input 16-byte input block * \param output 16-byte output block * - * \return 0 if success, 1 if operation failed + * \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. + * + * \return 0 on success (cannot fail) + */ +int aesni_gcm_mult( unsigned char c[16], + const unsigned char a[16], + const unsigned char b[16] ); + #endif /* POLARSSL_HAVE_X86_64 */ #endif /* POLARSSL_AESNI_H */ diff --git a/library/aesni.c b/library/aesni.c index 9b41c36ff..0431f78cc 100644 --- a/library/aesni.c +++ b/library/aesni.c @@ -25,6 +25,7 @@ /* * [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" @@ -101,6 +102,119 @@ int aesni_crypt_ecb( aes_context *ctx, return( 0 ); } + +/* + * GCM multiplication: c = a times b in GF(2^128) + * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5. + */ +int 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( 0 ); +} + #endif /* POLARSSL_HAVE_X86_64 */ #endif /* POLARSSL_AESNI_C */ From 80637c7520eef006a0abb712d21f201692a3d580 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Thu, 26 Dec 2013 16:09:58 +0100 Subject: [PATCH 06/12] Use aesni_gcm_mult() if available --- library/gcm.c | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/library/gcm.c b/library/gcm.c index 7374dca71..894c5d3eb 100644 --- a/library/gcm.c +++ b/library/gcm.c @@ -39,6 +39,10 @@ #include "polarssl/gcm.h" +#if defined(POLARSSL_AESNI_C) +#include "polarssl/aesni.h" +#endif + /* * 32-bit integer manipulation macros (big endian) */ @@ -82,10 +86,7 @@ static int gcm_gen_table( gcm_context *ctx ) if( ( ret = cipher_update( &ctx->cipher_ctx, h, 16, h, &olen ) ) != 0 ) return( ret ); - /* 0 corresponds to 0 in GF(2^128) */ - 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; @@ -98,6 +99,16 @@ static int gcm_gen_table( gcm_context *ctx ) 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; @@ -178,6 +189,20 @@ static void gcm_mult( gcm_context *ctx, const unsigned char x[16], 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 ); + + (void) aesni_gcm_mult( output, x, h ); + return; + } +#endif + memset( z, 0x00, 16 ); lo = x[15] & 0xf; From 01e31bbffb3a975576b5e67dfe94764e2438370e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Sat, 28 Dec 2013 15:58:30 +0100 Subject: [PATCH 07/12] Add support for key inversion using AES-NI --- include/polarssl/aesni.h | 10 ++++++++++ library/aes.c | 10 ++++++++++ library/aesni.c | 22 ++++++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/include/polarssl/aesni.h b/include/polarssl/aesni.h index f3f9fc31d..b66aa8aed 100644 --- a/include/polarssl/aesni.h +++ b/include/polarssl/aesni.h @@ -81,6 +81,16 @@ int 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 ); + #endif /* POLARSSL_HAVE_X86_64 */ #endif /* POLARSSL_AESNI_H */ diff --git a/library/aes.c b/library/aes.c index d2d1c0c43..447708444 100644 --- a/library/aes.c +++ b/library/aes.c @@ -591,6 +591,15 @@ int aes_setkey_dec( aes_context *ctx, const unsigned char *key, unsigned int key 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++; @@ -614,6 +623,7 @@ int aes_setkey_dec( aes_context *ctx, const unsigned char *key, unsigned int key *RK++ = *SK++; *RK++ = *SK++; +done: memset( &cty, 0, sizeof( aes_context ) ); return( 0 ); diff --git a/library/aesni.c b/library/aesni.c index 0431f78cc..488731c85 100644 --- a/library/aesni.c +++ b/library/aesni.c @@ -215,6 +215,28 @@ int aesni_gcm_mult( unsigned char c[16], return( 0 ); } +/* + * 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 ); +} + #endif /* POLARSSL_HAVE_X86_64 */ #endif /* POLARSSL_AESNI_C */ From 47a3536a3185a7664effe6cfd50d60c9e9c8fab6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Sat, 28 Dec 2013 20:45:04 +0100 Subject: [PATCH 08/12] Add AES-NI key expansion for 128 bits --- include/polarssl/aesni.h | 13 ++++++++ library/aes.c | 9 ++++++ library/aesni.c | 64 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) diff --git a/include/polarssl/aesni.h b/include/polarssl/aesni.h index b66aa8aed..808ff4d49 100644 --- a/include/polarssl/aesni.h +++ b/include/polarssl/aesni.h @@ -91,6 +91,19 @@ int aesni_gcm_mult( unsigned char c[16], 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/library/aes.c b/library/aes.c index 447708444..331698f5d 100644 --- a/library/aes.c +++ b/library/aes.c @@ -483,6 +483,15 @@ int aes_setkey_enc( aes_context *ctx, const unsigned char *key, unsigned int key #endif ctx->rk = RK = ctx->buf; +#if defined(POLARSSL_AESNI_C) && defined(POLARSSL_HAVE_X86_64) + if( aesni_supports( POLARSSL_AESNI_AES ) ) + { + int ret = aesni_setkey_enc( (unsigned char *) ctx->rk, key, keysize ); + // XXX: temporary while some key size aren't handled + if( ret == 0 ) return( ret ); + } +#endif + for( i = 0; i < (keysize >> 5); i++ ) { GET_UINT32_LE( RK[i], key, i << 2 ); diff --git a/library/aesni.c b/library/aesni.c index 488731c85..dc5f8fc28 100644 --- a/library/aesni.c +++ b/library/aesni.c @@ -237,6 +237,70 @@ void aesni_inverse_key( unsigned char *invkey, memcpy( ik, fk, 16 ); } +/* + * Key expansion, 128-bit case + */ +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 + + /* + * 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, 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; + default : return( POLARSSL_ERR_AES_INVALID_KEY_LENGTH ); + } + + return( 0 ); +} + #endif /* POLARSSL_HAVE_X86_64 */ #endif /* POLARSSL_AESNI_C */ From 4a5b995c2621d49e141dd099af8bb1aec50d881e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Sun, 29 Dec 2013 13:50:32 +0100 Subject: [PATCH 09/12] Add AES-NI key expansion for 256 bits --- include/polarssl/aes.h | 5 +++ library/aesni.c | 69 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/include/polarssl/aes.h b/include/polarssl/aes.h index 0eb25dfff..9513a71ac 100644 --- a/include/polarssl/aes.h +++ b/include/polarssl/aes.h @@ -55,6 +55,11 @@ extern "C" { /** * \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/library/aesni.c b/library/aesni.c index dc5f8fc28..f303a28a8 100644 --- a/library/aesni.c +++ b/library/aesni.c @@ -248,6 +248,8 @@ void aesni_setkey_enc_128( unsigned char *rk, "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. * @@ -285,6 +287,72 @@ void aesni_setkey_enc_128( unsigned char *rk, : "memory", "cc", "0" ); } +/* + * Key expansion, 256-bit case + */ +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 */ @@ -295,6 +363,7 @@ int aesni_setkey_enc( unsigned char *rk, switch( bits ) { case 128: aesni_setkey_enc_128( rk, key ); break; + case 256: aesni_setkey_enc_256( rk, key ); break; default : return( POLARSSL_ERR_AES_INVALID_KEY_LENGTH ); } From 23c2f6fee5df807148190afe8165969a7ffcc7dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Sun, 29 Dec 2013 16:05:22 +0100 Subject: [PATCH 10/12] Add AES-NI key expansion for 192 bits --- library/aesni.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/library/aesni.c b/library/aesni.c index f303a28a8..aa1f4b1c4 100644 --- a/library/aesni.c +++ b/library/aesni.c @@ -287,6 +287,63 @@ void aesni_setkey_enc_128( unsigned char *rk, : "memory", "cc", "0" ); } +/* + * Key expansion, 192-bit case + */ +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 */ @@ -363,6 +420,7 @@ int aesni_setkey_enc( unsigned char *rk, 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 ); } From bfa3c9a85f7e794e4cf821244bb58d6c86034efc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Mon, 30 Dec 2013 13:53:58 +0100 Subject: [PATCH 11/12] Remove temporary code --- library/aes.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/library/aes.c b/library/aes.c index 331698f5d..0286a7fbf 100644 --- a/library/aes.c +++ b/library/aes.c @@ -485,11 +485,7 @@ int aes_setkey_enc( aes_context *ctx, const unsigned char *key, unsigned int key #if defined(POLARSSL_AESNI_C) && defined(POLARSSL_HAVE_X86_64) if( aesni_supports( POLARSSL_AESNI_AES ) ) - { - int ret = aesni_setkey_enc( (unsigned char *) ctx->rk, key, keysize ); - // XXX: temporary while some key size aren't handled - if( ret == 0 ) return( ret ); - } + return( aesni_setkey_enc( (unsigned char *) ctx->rk, key, keysize ) ); #endif for( i = 0; i < (keysize >> 5); i++ ) From d4588cfb6a62cc1cbc4deb09d9d8f90a5a7a535a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Mon, 30 Dec 2013 13:54:23 +0100 Subject: [PATCH 12/12] aesni_gcm_mult() now returns void --- include/polarssl/aesni.h | 8 +++----- library/aesni.c | 6 +++--- library/gcm.c | 2 +- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/include/polarssl/aesni.h b/include/polarssl/aesni.h index 808ff4d49..92b23cd6b 100644 --- a/include/polarssl/aesni.h +++ b/include/polarssl/aesni.h @@ -74,12 +74,10 @@ int aesni_crypt_ecb( aes_context *ctx, * * \note Both operands and result are bit strings interpreted as * elements of GF(2^128) as per the GCM spec. - * - * \return 0 on success (cannot fail) */ -int aesni_gcm_mult( unsigned char c[16], - const unsigned char a[16], - const unsigned char b[16] ); +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 diff --git a/library/aesni.c b/library/aesni.c index aa1f4b1c4..b5858dc35 100644 --- a/library/aesni.c +++ b/library/aesni.c @@ -107,9 +107,9 @@ int aesni_crypt_ecb( aes_context *ctx, * GCM multiplication: c = a times b in GF(2^128) * Based on [CLMUL-WP] algorithms 1 (with equation 27) and 5. */ -int aesni_gcm_mult( unsigned char c[16], - const unsigned char a[16], - const unsigned char b[16] ) +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; diff --git a/library/gcm.c b/library/gcm.c index 894c5d3eb..8950360f6 100644 --- a/library/gcm.c +++ b/library/gcm.c @@ -198,7 +198,7 @@ static void gcm_mult( gcm_context *ctx, const unsigned char x[16], PUT_UINT32_BE( ctx->HL[8] >> 32, h, 8 ); PUT_UINT32_BE( ctx->HL[8], h, 12 ); - (void) aesni_gcm_mult( output, x, h ); + aesni_gcm_mult( output, x, h ); return; } #endif