mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-22 22:00:39 -06:00
Update external polarssl to 1.3.8
There were some fixes back on March 13th, 2014 for fixing compiling on MIPS64. Also some fixes on June 25th, 2014 for SPARC64 fixes. Probably more things, but those are what I care about.
This commit is contained in:
200
Externals/polarssl/library/aes.c
vendored
200
Externals/polarssl/library/aes.c
vendored
@ -1,7 +1,7 @@
|
||||
/*
|
||||
* FIPS-197 compliant AES implementation
|
||||
*
|
||||
* Copyright (C) 2006-2013, Brainspark B.V.
|
||||
* Copyright (C) 2006-2014, Brainspark B.V.
|
||||
*
|
||||
* This file is part of PolarSSL (http://www.polarssl.org)
|
||||
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
||||
@ -29,7 +29,11 @@
|
||||
* http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf
|
||||
*/
|
||||
|
||||
#if !defined(POLARSSL_CONFIG_FILE)
|
||||
#include "polarssl/config.h"
|
||||
#else
|
||||
#include POLARSSL_CONFIG_FILE
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_AES_C)
|
||||
|
||||
@ -41,8 +45,19 @@
|
||||
#include "polarssl/aesni.h"
|
||||
#endif
|
||||
|
||||
#if defined(POLARSSL_PLATFORM_C)
|
||||
#include "polarssl/platform.h"
|
||||
#else
|
||||
#define polarssl_printf printf
|
||||
#endif
|
||||
|
||||
#if !defined(POLARSSL_AES_ALT)
|
||||
|
||||
/* Implementation that should never be optimized out by the compiler */
|
||||
static void polarssl_zeroize( void *v, size_t n ) {
|
||||
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* 32-bit integer manipulation macros (little endian)
|
||||
*/
|
||||
@ -336,16 +351,16 @@ static const uint32_t RCON[10] =
|
||||
0x0000001B, 0x00000036
|
||||
};
|
||||
|
||||
#else
|
||||
#else /* POLARSSL_AES_ROM_TABLES */
|
||||
|
||||
/*
|
||||
* Forward S-box & tables
|
||||
*/
|
||||
static unsigned char FSb[256];
|
||||
static uint32_t FT0[256];
|
||||
static uint32_t FT1[256];
|
||||
static uint32_t FT2[256];
|
||||
static uint32_t FT3[256];
|
||||
static uint32_t FT0[256];
|
||||
static uint32_t FT1[256];
|
||||
static uint32_t FT2[256];
|
||||
static uint32_t FT3[256];
|
||||
|
||||
/*
|
||||
* Reverse S-box & tables
|
||||
@ -405,10 +420,10 @@ static void aes_gen_tables( void )
|
||||
{
|
||||
x = pow[255 - log[i]];
|
||||
|
||||
y = x; y = ( (y << 1) | (y >> 7) ) & 0xFF;
|
||||
x ^= y; y = ( (y << 1) | (y >> 7) ) & 0xFF;
|
||||
x ^= y; y = ( (y << 1) | (y >> 7) ) & 0xFF;
|
||||
x ^= y; y = ( (y << 1) | (y >> 7) ) & 0xFF;
|
||||
y = x; y = ( ( y << 1 ) | ( y >> 7 ) ) & 0xFF;
|
||||
x ^= y; y = ( ( y << 1 ) | ( y >> 7 ) ) & 0xFF;
|
||||
x ^= y; y = ( ( y << 1 ) | ( y >> 7 ) ) & 0xFF;
|
||||
x ^= y; y = ( ( y << 1 ) | ( y >> 7 ) ) & 0xFF;
|
||||
x ^= y ^ 0x63;
|
||||
|
||||
FSb[i] = (unsigned char) x;
|
||||
@ -446,12 +461,26 @@ static void aes_gen_tables( void )
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif /* POLARSSL_AES_ROM_TABLES */
|
||||
|
||||
void aes_init( aes_context *ctx )
|
||||
{
|
||||
memset( ctx, 0, sizeof( aes_context ) );
|
||||
}
|
||||
|
||||
void aes_free( aes_context *ctx )
|
||||
{
|
||||
if( ctx == NULL )
|
||||
return;
|
||||
|
||||
polarssl_zeroize( ctx, sizeof( aes_context ) );
|
||||
}
|
||||
|
||||
/*
|
||||
* AES key schedule (encryption)
|
||||
*/
|
||||
int aes_setkey_enc( aes_context *ctx, const unsigned char *key, unsigned int keysize )
|
||||
int aes_setkey_enc( aes_context *ctx, const unsigned char *key,
|
||||
unsigned int keysize )
|
||||
{
|
||||
unsigned int i;
|
||||
uint32_t *RK;
|
||||
@ -488,7 +517,7 @@ int aes_setkey_enc( aes_context *ctx, const unsigned char *key, unsigned int key
|
||||
return( aesni_setkey_enc( (unsigned char *) ctx->rk, key, keysize ) );
|
||||
#endif
|
||||
|
||||
for( i = 0; i < (keysize >> 5); i++ )
|
||||
for( i = 0; i < ( keysize >> 5 ); i++ )
|
||||
{
|
||||
GET_UINT32_LE( RK[i], key, i << 2 );
|
||||
}
|
||||
@ -554,10 +583,6 @@ int aes_setkey_enc( aes_context *ctx, const unsigned char *key, unsigned int key
|
||||
RK[15] = RK[7] ^ RK[14];
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return( 0 );
|
||||
@ -566,21 +591,15 @@ int aes_setkey_enc( aes_context *ctx, const unsigned char *key, unsigned int key
|
||||
/*
|
||||
* AES key schedule (decryption)
|
||||
*/
|
||||
int aes_setkey_dec( aes_context *ctx, const unsigned char *key, unsigned int keysize )
|
||||
int aes_setkey_dec( aes_context *ctx, const unsigned char *key,
|
||||
unsigned int keysize )
|
||||
{
|
||||
int i, j;
|
||||
int i, j, ret;
|
||||
aes_context cty;
|
||||
uint32_t *RK;
|
||||
uint32_t *SK;
|
||||
int ret;
|
||||
|
||||
switch( keysize )
|
||||
{
|
||||
case 128: ctx->nr = 10; break;
|
||||
case 192: ctx->nr = 12; break;
|
||||
case 256: ctx->nr = 14; break;
|
||||
default : return( POLARSSL_ERR_AES_INVALID_KEY_LENGTH );
|
||||
}
|
||||
aes_init( &cty );
|
||||
|
||||
#if defined(POLARSSL_PADLOCK_C) && defined(PADLOCK_ALIGN16)
|
||||
if( aes_padlock_ace == -1 )
|
||||
@ -592,16 +611,18 @@ int aes_setkey_dec( aes_context *ctx, const unsigned char *key, unsigned int key
|
||||
#endif
|
||||
ctx->rk = RK = ctx->buf;
|
||||
|
||||
ret = aes_setkey_enc( &cty, key, keysize );
|
||||
if( ret != 0 )
|
||||
return( ret );
|
||||
/* Also checks keysize */
|
||||
if( ( ret = aes_setkey_enc( &cty, key, keysize ) ) != 0 )
|
||||
goto exit;
|
||||
|
||||
ctx->nr = cty.nr;
|
||||
|
||||
#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;
|
||||
goto exit;
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -628,12 +649,10 @@ int aes_setkey_dec( aes_context *ctx, const unsigned char *key, unsigned int key
|
||||
*RK++ = *SK++;
|
||||
*RK++ = *SK++;
|
||||
|
||||
#if defined(POLARSSL_AESNI_C) && defined(POLARSSL_HAVE_X86_64)
|
||||
done:
|
||||
#endif
|
||||
memset( &cty, 0, sizeof( aes_context ) );
|
||||
exit:
|
||||
aes_free( &cty );
|
||||
|
||||
return( 0 );
|
||||
return( ret );
|
||||
}
|
||||
|
||||
#define AES_FROUND(X0,X1,X2,X3,Y0,Y1,Y2,Y3) \
|
||||
@ -719,7 +738,7 @@ int aes_crypt_ecb( aes_context *ctx,
|
||||
|
||||
if( mode == AES_DECRYPT )
|
||||
{
|
||||
for( i = (ctx->nr >> 1) - 1; i > 0; i-- )
|
||||
for( i = ( ctx->nr >> 1 ) - 1; i > 0; i-- )
|
||||
{
|
||||
AES_RROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 );
|
||||
AES_RROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 );
|
||||
@ -753,7 +772,7 @@ int aes_crypt_ecb( aes_context *ctx,
|
||||
}
|
||||
else /* AES_ENCRYPT */
|
||||
{
|
||||
for( i = (ctx->nr >> 1) - 1; i > 0; i-- )
|
||||
for( i = ( ctx->nr >> 1 ) - 1; i > 0; i-- )
|
||||
{
|
||||
AES_FROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 );
|
||||
AES_FROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 );
|
||||
@ -816,7 +835,7 @@ int aes_crypt_cbc( aes_context *ctx,
|
||||
{
|
||||
if( padlock_xcryptcbc( ctx, mode, length, iv, input, output ) == 0 )
|
||||
return( 0 );
|
||||
|
||||
|
||||
// If padlock data misaligned, we just fall back to
|
||||
// unaccelerated mode
|
||||
//
|
||||
@ -886,7 +905,7 @@ int aes_crypt_cfb128( aes_context *ctx,
|
||||
*output++ = (unsigned char)( c ^ iv[n] );
|
||||
iv[n] = (unsigned char) c;
|
||||
|
||||
n = (n + 1) & 0x0F;
|
||||
n = ( n + 1 ) & 0x0F;
|
||||
}
|
||||
}
|
||||
else
|
||||
@ -898,7 +917,7 @@ int aes_crypt_cfb128( aes_context *ctx,
|
||||
|
||||
iv[n] = *output++ = (unsigned char)( iv[n] ^ *input++ );
|
||||
|
||||
n = (n + 1) & 0x0F;
|
||||
n = ( n + 1 ) & 0x0F;
|
||||
}
|
||||
}
|
||||
|
||||
@ -923,7 +942,7 @@ int aes_crypt_cfb8( aes_context *ctx,
|
||||
|
||||
while( length-- )
|
||||
{
|
||||
memcpy(ov, iv, 16);
|
||||
memcpy( ov, iv, 16 );
|
||||
aes_crypt_ecb( ctx, AES_ENCRYPT, iv, iv );
|
||||
|
||||
if( mode == AES_DECRYPT )
|
||||
@ -934,7 +953,7 @@ int aes_crypt_cfb8( aes_context *ctx,
|
||||
if( mode == AES_ENCRYPT )
|
||||
ov[16] = c;
|
||||
|
||||
memcpy(iv, ov + 1, 16);
|
||||
memcpy( iv, ov + 1, 16 );
|
||||
}
|
||||
|
||||
return( 0 );
|
||||
@ -968,7 +987,7 @@ int aes_crypt_ctr( aes_context *ctx,
|
||||
c = *input++;
|
||||
*output++ = (unsigned char)( c ^ stream_block[n] );
|
||||
|
||||
n = (n + 1) & 0x0F;
|
||||
n = ( n + 1 ) & 0x0F;
|
||||
}
|
||||
|
||||
*nc_off = n;
|
||||
@ -976,6 +995,7 @@ int aes_crypt_ctr( aes_context *ctx,
|
||||
return( 0 );
|
||||
}
|
||||
#endif /* POLARSSL_CIPHER_MODE_CTR */
|
||||
|
||||
#endif /* !POLARSSL_AES_ALT */
|
||||
|
||||
#if defined(POLARSSL_SELF_TEST)
|
||||
@ -1163,7 +1183,7 @@ static const int aes_test_ctr_len[3] =
|
||||
*/
|
||||
int aes_self_test( int verbose )
|
||||
{
|
||||
int i, j, u, v;
|
||||
int ret = 0, i, j, u, v;
|
||||
unsigned char key[32];
|
||||
unsigned char buf[64];
|
||||
unsigned char iv[16];
|
||||
@ -1181,6 +1201,7 @@ int aes_self_test( int verbose )
|
||||
aes_context ctx;
|
||||
|
||||
memset( key, 0, 32 );
|
||||
aes_init( &ctx );
|
||||
|
||||
/*
|
||||
* ECB mode
|
||||
@ -1191,8 +1212,8 @@ int aes_self_test( int verbose )
|
||||
v = i & 1;
|
||||
|
||||
if( verbose != 0 )
|
||||
printf( " AES-ECB-%3d (%s): ", 128 + u * 64,
|
||||
( v == AES_DECRYPT ) ? "dec" : "enc" );
|
||||
polarssl_printf( " AES-ECB-%3d (%s): ", 128 + u * 64,
|
||||
( v == AES_DECRYPT ) ? "dec" : "enc" );
|
||||
|
||||
memset( buf, 0, 16 );
|
||||
|
||||
@ -1206,9 +1227,10 @@ int aes_self_test( int verbose )
|
||||
if( memcmp( buf, aes_test_ecb_dec[u], 16 ) != 0 )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
printf( "failed\n" );
|
||||
polarssl_printf( "failed\n" );
|
||||
|
||||
return( 1 );
|
||||
ret = 1;
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
else
|
||||
@ -1221,18 +1243,19 @@ int aes_self_test( int verbose )
|
||||
if( memcmp( buf, aes_test_ecb_enc[u], 16 ) != 0 )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
printf( "failed\n" );
|
||||
polarssl_printf( "failed\n" );
|
||||
|
||||
return( 1 );
|
||||
ret = 1;
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
|
||||
if( verbose != 0 )
|
||||
printf( "passed\n" );
|
||||
polarssl_printf( "passed\n" );
|
||||
}
|
||||
|
||||
if( verbose != 0 )
|
||||
printf( "\n" );
|
||||
polarssl_printf( "\n" );
|
||||
|
||||
#if defined(POLARSSL_CIPHER_MODE_CBC)
|
||||
/*
|
||||
@ -1244,8 +1267,8 @@ int aes_self_test( int verbose )
|
||||
v = i & 1;
|
||||
|
||||
if( verbose != 0 )
|
||||
printf( " AES-CBC-%3d (%s): ", 128 + u * 64,
|
||||
( v == AES_DECRYPT ) ? "dec" : "enc" );
|
||||
polarssl_printf( " AES-CBC-%3d (%s): ", 128 + u * 64,
|
||||
( v == AES_DECRYPT ) ? "dec" : "enc" );
|
||||
|
||||
memset( iv , 0, 16 );
|
||||
memset( prv, 0, 16 );
|
||||
@ -1261,9 +1284,10 @@ int aes_self_test( int verbose )
|
||||
if( memcmp( buf, aes_test_cbc_dec[u], 16 ) != 0 )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
printf( "failed\n" );
|
||||
polarssl_printf( "failed\n" );
|
||||
|
||||
return( 1 );
|
||||
ret = 1;
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
else
|
||||
@ -1284,18 +1308,19 @@ int aes_self_test( int verbose )
|
||||
if( memcmp( prv, aes_test_cbc_enc[u], 16 ) != 0 )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
printf( "failed\n" );
|
||||
polarssl_printf( "failed\n" );
|
||||
|
||||
return( 1 );
|
||||
ret = 1;
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
|
||||
if( verbose != 0 )
|
||||
printf( "passed\n" );
|
||||
polarssl_printf( "passed\n" );
|
||||
}
|
||||
|
||||
if( verbose != 0 )
|
||||
printf( "\n" );
|
||||
polarssl_printf( "\n" );
|
||||
#endif /* POLARSSL_CIPHER_MODE_CBC */
|
||||
|
||||
#if defined(POLARSSL_CIPHER_MODE_CFB)
|
||||
@ -1308,8 +1333,8 @@ int aes_self_test( int verbose )
|
||||
v = i & 1;
|
||||
|
||||
if( verbose != 0 )
|
||||
printf( " AES-CFB128-%3d (%s): ", 128 + u * 64,
|
||||
( v == AES_DECRYPT ) ? "dec" : "enc" );
|
||||
polarssl_printf( " AES-CFB128-%3d (%s): ", 128 + u * 64,
|
||||
( v == AES_DECRYPT ) ? "dec" : "enc" );
|
||||
|
||||
memcpy( iv, aes_test_cfb128_iv, 16 );
|
||||
memcpy( key, aes_test_cfb128_key[u], 16 + u * 8 );
|
||||
@ -1325,9 +1350,10 @@ int aes_self_test( int verbose )
|
||||
if( memcmp( buf, aes_test_cfb128_pt, 64 ) != 0 )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
printf( "failed\n" );
|
||||
polarssl_printf( "failed\n" );
|
||||
|
||||
return( 1 );
|
||||
ret = 1;
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
else
|
||||
@ -1338,18 +1364,19 @@ int aes_self_test( int verbose )
|
||||
if( memcmp( buf, aes_test_cfb128_ct[u], 64 ) != 0 )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
printf( "failed\n" );
|
||||
polarssl_printf( "failed\n" );
|
||||
|
||||
return( 1 );
|
||||
ret = 1;
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
|
||||
if( verbose != 0 )
|
||||
printf( "passed\n" );
|
||||
polarssl_printf( "passed\n" );
|
||||
}
|
||||
|
||||
if( verbose != 0 )
|
||||
printf( "\n" );
|
||||
polarssl_printf( "\n" );
|
||||
#endif /* POLARSSL_CIPHER_MODE_CFB */
|
||||
|
||||
#if defined(POLARSSL_CIPHER_MODE_CTR)
|
||||
@ -1362,8 +1389,8 @@ int aes_self_test( int verbose )
|
||||
v = i & 1;
|
||||
|
||||
if( verbose != 0 )
|
||||
printf( " AES-CTR-128 (%s): ",
|
||||
( v == AES_DECRYPT ) ? "dec" : "enc" );
|
||||
polarssl_printf( " AES-CTR-128 (%s): ",
|
||||
( v == AES_DECRYPT ) ? "dec" : "enc" );
|
||||
|
||||
memcpy( nonce_counter, aes_test_ctr_nonce_counter[u], 16 );
|
||||
memcpy( key, aes_test_ctr_key[u], 16 );
|
||||
@ -1376,14 +1403,16 @@ int aes_self_test( int verbose )
|
||||
len = aes_test_ctr_len[u];
|
||||
memcpy( buf, aes_test_ctr_ct[u], len );
|
||||
|
||||
aes_crypt_ctr( &ctx, len, &offset, nonce_counter, stream_block, buf, buf );
|
||||
aes_crypt_ctr( &ctx, len, &offset, nonce_counter, stream_block,
|
||||
buf, buf );
|
||||
|
||||
if( memcmp( buf, aes_test_ctr_pt[u], len ) != 0 )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
printf( "failed\n" );
|
||||
polarssl_printf( "failed\n" );
|
||||
|
||||
return( 1 );
|
||||
ret = 1;
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
else
|
||||
@ -1391,28 +1420,35 @@ int aes_self_test( int verbose )
|
||||
len = aes_test_ctr_len[u];
|
||||
memcpy( buf, aes_test_ctr_pt[u], len );
|
||||
|
||||
aes_crypt_ctr( &ctx, len, &offset, nonce_counter, stream_block, buf, buf );
|
||||
aes_crypt_ctr( &ctx, len, &offset, nonce_counter, stream_block,
|
||||
buf, buf );
|
||||
|
||||
if( memcmp( buf, aes_test_ctr_ct[u], len ) != 0 )
|
||||
{
|
||||
if( verbose != 0 )
|
||||
printf( "failed\n" );
|
||||
polarssl_printf( "failed\n" );
|
||||
|
||||
return( 1 );
|
||||
ret = 1;
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
|
||||
if( verbose != 0 )
|
||||
printf( "passed\n" );
|
||||
polarssl_printf( "passed\n" );
|
||||
}
|
||||
|
||||
if( verbose != 0 )
|
||||
printf( "\n" );
|
||||
polarssl_printf( "\n" );
|
||||
#endif /* POLARSSL_CIPHER_MODE_CTR */
|
||||
|
||||
return( 0 );
|
||||
ret = 0;
|
||||
|
||||
exit:
|
||||
aes_free( &ctx );
|
||||
|
||||
return( ret );
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif /* POLARSSL_SELF_TEST */
|
||||
|
||||
#endif
|
||||
#endif /* POLARSSL_AES_C */
|
||||
|
Reference in New Issue
Block a user