Smaller table version of the AES encrypt/decrypt

Use WOLFSSL_AES_SMALL_TABLES.
Much slower. Decrypt much slower then encrypt.
This commit is contained in:
Sean Parkinson
2020-03-27 15:53:01 +10:00
parent ddb4b5eb89
commit 9339808ea1

View File

@ -933,6 +933,7 @@ static const word32 rcon[] = {
/* for 128-bit blocks, Rijndael never uses more than 10 rcon values */
};
#ifndef WOLFSSL_AES_SMALL_TABLES
static const word32 Te[4][256] = {
{
0xc66363a5U, 0xf87c7c84U, 0xee777799U, 0xf67b7b8dU,
@ -1468,8 +1469,10 @@ static const word32 Td[4][256] = {
0xcb84617bU, 0x32b670d5U, 0x6c5c7448U, 0xb85742d0U,
}
};
#endif /* HAVE_AES_DECRYPT */
#endif
#ifdef HAVE_AES_DECRYPT
#if (defined(HAVE_AES_CBC) && !defined(WOLFSSL_DEVCRYPTO_CBC)) \
|| defined(WOLFSSL_AES_DIRECT)
static const byte Td4[256] =
@ -1512,7 +1515,62 @@ static const byte Td4[256] =
#define GETBYTE(x, y) (word32)((byte)((x) >> (8 * (y))))
#ifdef WOLFSSL_AES_SMALL_TABLES
static const byte Tsbox[256] = {
0x63U, 0x7cU, 0x77U, 0x7bU, 0xf2U, 0x6bU, 0x6fU, 0xc5U,
0x30U, 0x01U, 0x67U, 0x2bU, 0xfeU, 0xd7U, 0xabU, 0x76U,
0xcaU, 0x82U, 0xc9U, 0x7dU, 0xfaU, 0x59U, 0x47U, 0xf0U,
0xadU, 0xd4U, 0xa2U, 0xafU, 0x9cU, 0xa4U, 0x72U, 0xc0U,
0xb7U, 0xfdU, 0x93U, 0x26U, 0x36U, 0x3fU, 0xf7U, 0xccU,
0x34U, 0xa5U, 0xe5U, 0xf1U, 0x71U, 0xd8U, 0x31U, 0x15U,
0x04U, 0xc7U, 0x23U, 0xc3U, 0x18U, 0x96U, 0x05U, 0x9aU,
0x07U, 0x12U, 0x80U, 0xe2U, 0xebU, 0x27U, 0xb2U, 0x75U,
0x09U, 0x83U, 0x2cU, 0x1aU, 0x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U,
0xe1U, 0xf8U, 0x98U, 0x11U, 0x69U, 0xd9U, 0x8eU, 0x94U,
0x9bU, 0x1eU, 0x87U, 0xe9U, 0xceU, 0x55U, 0x28U, 0xdfU,
0x8cU, 0xa1U, 0x89U, 0x0dU, 0xbfU, 0xe6U, 0x42U, 0x68U,
0x41U, 0x99U, 0x2dU, 0x0fU, 0xb0U, 0x54U, 0xbbU, 0x16U
};
#define AES_XTIME(x) ((byte)((byte)((x) << 1) ^ ((0 - ((x) >> 7)) & 0x1b)))
static word32 col_mul(word32 t, int i2, int i3, int ia, int ib)
{
byte t3 = GETBYTE(t, i3);
byte tm = AES_XTIME(GETBYTE(t, i2) ^ t3);
return GETBYTE(t, ia) ^ GETBYTE(t, ib) ^ t3 ^ tm;
}
static word32 inv_col_mul(word32 t, int i9, int ib, int id, int ie)
{
byte t9 = GETBYTE(t, i9);
byte tb = GETBYTE(t, ib);
byte td = GETBYTE(t, id);
byte te = GETBYTE(t, ie);
byte t0 = t9 ^ tb ^ td;
return t0 ^ AES_XTIME(AES_XTIME(AES_XTIME(t0 ^ te) ^ td ^ te) ^ tb ^ te);
}
#endif
#if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT) || defined(HAVE_AESGCM)
@ -1527,6 +1585,7 @@ static const byte Td4[256] =
#endif
#ifndef WOLFSSL_AES_SMALL_TABLES
/* load 4 Te Tables into cache by cache line stride */
static WC_INLINE word32 PreFetchTe(void)
{
@ -1541,6 +1600,19 @@ static WC_INLINE word32 PreFetchTe(void)
}
return x;
}
#else
/* load sbox into cache by cache line stride */
static WC_INLINE word32 PreFetchSBox(void)
{
word32 x = 0;
int i;
for (i = 0; i < 256; i += WC_CACHE_LINE_SZ/4) {
x &= Tsbox[i];
}
return x;
}
#endif
/* Software AES - ECB Encrypt */
static void wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock)
@ -1620,11 +1692,13 @@ static void wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock)
s3 = ByteReverseWord32(s3);
#endif
/* AddRoundKey */
s0 ^= rk[0];
s1 ^= rk[1];
s2 ^= rk[2];
s3 ^= rk[3];
#ifndef WOLFSSL_AES_SMALL_TABLES
s0 |= PreFetchTe();
/*
@ -1633,28 +1707,28 @@ static void wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock)
for (;;) {
t0 =
Te[0][GETBYTE(s0, 3)] ^
Te[1][GETBYTE(s1, 2)] ^
Te[2][GETBYTE(s2, 1)] ^
Te[3][GETBYTE(s3, 0)] ^
Te[0][GETBYTE(s0, 3)] ^
Te[1][GETBYTE(s1, 2)] ^
Te[2][GETBYTE(s2, 1)] ^
Te[3][GETBYTE(s3, 0)] ^
rk[4];
t1 =
Te[0][GETBYTE(s1, 3)] ^
Te[1][GETBYTE(s2, 2)] ^
Te[2][GETBYTE(s3, 1)] ^
Te[3][GETBYTE(s0, 0)] ^
Te[0][GETBYTE(s1, 3)] ^
Te[1][GETBYTE(s2, 2)] ^
Te[2][GETBYTE(s3, 1)] ^
Te[3][GETBYTE(s0, 0)] ^
rk[5];
t2 =
Te[0][GETBYTE(s2, 3)] ^
Te[1][GETBYTE(s3, 2)] ^
Te[2][GETBYTE(s0, 1)] ^
Te[3][GETBYTE(s1, 0)] ^
Te[1][GETBYTE(s3, 2)] ^
Te[2][GETBYTE(s0, 1)] ^
Te[3][GETBYTE(s1, 0)] ^
rk[6];
t3 =
Te[0][GETBYTE(s3, 3)] ^
Te[1][GETBYTE(s0, 2)] ^
Te[2][GETBYTE(s1, 1)] ^
Te[3][GETBYTE(s2, 0)] ^
Te[1][GETBYTE(s0, 2)] ^
Te[2][GETBYTE(s1, 1)] ^
Te[3][GETBYTE(s2, 0)] ^
rk[7];
rk += 8;
@ -1717,6 +1791,84 @@ static void wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock)
(Te[0][GETBYTE(t1, 1)] & 0x0000ff00) ^
(Te[1][GETBYTE(t2, 0)] & 0x000000ff) ^
rk[3];
#else
s0 |= PreFetchSBox();
r *= 2;
/* Two rounds at a time */
for (rk += 4; r > 1; r--, rk += 4) {
t0 =
((word32)Tsbox[GETBYTE(s0, 3)] << 24) ^
((word32)Tsbox[GETBYTE(s1, 2)] << 16) ^
((word32)Tsbox[GETBYTE(s2, 1)] << 8) ^
((word32)Tsbox[GETBYTE(s3, 0)]);
t1 =
((word32)Tsbox[GETBYTE(s1, 3)] << 24) ^
((word32)Tsbox[GETBYTE(s2, 2)] << 16) ^
((word32)Tsbox[GETBYTE(s3, 1)] << 8) ^
((word32)Tsbox[GETBYTE(s0, 0)]);
t2 =
((word32)Tsbox[GETBYTE(s2, 3)] << 24) ^
((word32)Tsbox[GETBYTE(s3, 2)] << 16) ^
((word32)Tsbox[GETBYTE(s0, 1)] << 8) ^
((word32)Tsbox[GETBYTE(s1, 0)]);
t3 =
((word32)Tsbox[GETBYTE(s3, 3)] << 24) ^
((word32)Tsbox[GETBYTE(s0, 2)] << 16) ^
((word32)Tsbox[GETBYTE(s1, 1)] << 8) ^
((word32)Tsbox[GETBYTE(s2, 0)]);
s0 =
(col_mul(t0, 3, 2, 0, 1) << 24) ^
(col_mul(t0, 2, 1, 0, 3) << 16) ^
(col_mul(t0, 1, 0, 2, 3) << 8) ^
(col_mul(t0, 0, 3, 2, 1) ) ^
rk[0];
s1 =
(col_mul(t1, 3, 2, 0, 1) << 24) ^
(col_mul(t1, 2, 1, 0, 3) << 16) ^
(col_mul(t1, 1, 0, 2, 3) << 8) ^
(col_mul(t1, 0, 3, 2, 1) ) ^
rk[1];
s2 =
(col_mul(t2, 3, 2, 0, 1) << 24) ^
(col_mul(t2, 2, 1, 0, 3) << 16) ^
(col_mul(t2, 1, 0, 2, 3) << 8) ^
(col_mul(t2, 0, 3, 2, 1) ) ^
rk[2];
s3 =
(col_mul(t3, 3, 2, 0, 1) << 24) ^
(col_mul(t3, 2, 1, 0, 3) << 16) ^
(col_mul(t3, 1, 0, 2, 3) << 8) ^
(col_mul(t3, 0, 3, 2, 1) ) ^
rk[3];
}
t0 =
((word32)Tsbox[GETBYTE(s0, 3)] << 24) ^
((word32)Tsbox[GETBYTE(s1, 2)] << 16) ^
((word32)Tsbox[GETBYTE(s2, 1)] << 8) ^
((word32)Tsbox[GETBYTE(s3, 0)]);
t1 =
((word32)Tsbox[GETBYTE(s1, 3)] << 24) ^
((word32)Tsbox[GETBYTE(s2, 2)] << 16) ^
((word32)Tsbox[GETBYTE(s3, 1)] << 8) ^
((word32)Tsbox[GETBYTE(s0, 0)]);
t2 =
((word32)Tsbox[GETBYTE(s2, 3)] << 24) ^
((word32)Tsbox[GETBYTE(s3, 2)] << 16) ^
((word32)Tsbox[GETBYTE(s0, 1)] << 8) ^
((word32)Tsbox[GETBYTE(s1, 0)]);
t3 =
((word32)Tsbox[GETBYTE(s3, 3)] << 24) ^
((word32)Tsbox[GETBYTE(s0, 2)] << 16) ^
((word32)Tsbox[GETBYTE(s1, 1)] << 8) ^
((word32)Tsbox[GETBYTE(s2, 0)]);
s0 = t0 ^ rk[0];
s1 = t1 ^ rk[1];
s2 = t2 ^ rk[2];
s3 = t3 ^ rk[3];
#endif
/* write out */
#ifdef LITTLE_ENDIAN_ORDER
@ -1738,6 +1890,7 @@ static void wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock)
#if (defined(HAVE_AES_CBC) && !defined(WOLFSSL_DEVCRYPTO_CBC)) || \
defined(WOLFSSL_AES_DIRECT)
#ifndef WOLFSSL_AES_SMALL_TABLES
/* load 4 Td Tables into cache by cache line stride */
static WC_INLINE word32 PreFetchTd(void)
{
@ -1752,6 +1905,7 @@ static WC_INLINE word32 PreFetchTd(void)
}
return x;
}
#endif
/* load Td Table4 into cache by cache line stride */
static WC_INLINE word32 PreFetchTd4(void)
@ -1826,6 +1980,7 @@ static void wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock)
s2 ^= rk[2];
s3 ^= rk[3];
#ifndef WOLFSSL_AES_SMALL_TABLES
s0 |= PreFetchTd();
/*
@ -1919,6 +2074,83 @@ static void wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock)
((word32)Td4[GETBYTE(t1, 1)] << 8) ^
((word32)Td4[GETBYTE(t0, 0)]) ^
rk[3];
#else
s0 |= PreFetchTd4();
r *= 2;
for (rk += 4; r > 1; r--, rk += 4) {
t0 =
((word32)Td4[GETBYTE(s0, 3)] << 24) ^
((word32)Td4[GETBYTE(s3, 2)] << 16) ^
((word32)Td4[GETBYTE(s2, 1)] << 8) ^
((word32)Td4[GETBYTE(s1, 0)]) ^
rk[0];
t1 =
((word32)Td4[GETBYTE(s1, 3)] << 24) ^
((word32)Td4[GETBYTE(s0, 2)] << 16) ^
((word32)Td4[GETBYTE(s3, 1)] << 8) ^
((word32)Td4[GETBYTE(s2, 0)]) ^
rk[1];
t2 =
((word32)Td4[GETBYTE(s2, 3)] << 24) ^
((word32)Td4[GETBYTE(s1, 2)] << 16) ^
((word32)Td4[GETBYTE(s0, 1)] << 8) ^
((word32)Td4[GETBYTE(s3, 0)]) ^
rk[2];
t3 =
((word32)Td4[GETBYTE(s3, 3)] << 24) ^
((word32)Td4[GETBYTE(s2, 2)] << 16) ^
((word32)Td4[GETBYTE(s1, 1)] << 8) ^
((word32)Td4[GETBYTE(s0, 0)]) ^
rk[3];
s0 =
(inv_col_mul(t0, 0, 2, 1, 3) << 24) ^
(inv_col_mul(t0, 3, 1, 0, 2) << 16) ^
(inv_col_mul(t0, 2, 0, 3, 1) << 8) ^
(inv_col_mul(t0, 1, 3, 2, 0) );
s1 =
(inv_col_mul(t1, 0, 2, 1, 3) << 24) ^
(inv_col_mul(t1, 3, 1, 0, 2) << 16) ^
(inv_col_mul(t1, 2, 0, 3, 1) << 8) ^
(inv_col_mul(t1, 1, 3, 2, 0) );
s2 =
(inv_col_mul(t2, 0, 2, 1, 3) << 24) ^
(inv_col_mul(t2, 3, 1, 0, 2) << 16) ^
(inv_col_mul(t2, 2, 0, 3, 1) << 8) ^
(inv_col_mul(t2, 1, 3, 2, 0) );
s3 =
(inv_col_mul(t3, 0, 2, 1, 3) << 24) ^
(inv_col_mul(t3, 3, 1, 0, 2) << 16) ^
(inv_col_mul(t3, 2, 0, 3, 1) << 8) ^
(inv_col_mul(t3, 1, 3, 2, 0) );
}
t0 =
((word32)Td4[GETBYTE(s0, 3)] << 24) ^
((word32)Td4[GETBYTE(s3, 2)] << 16) ^
((word32)Td4[GETBYTE(s2, 1)] << 8) ^
((word32)Td4[GETBYTE(s1, 0)]);
t1 =
((word32)Td4[GETBYTE(s1, 3)] << 24) ^
((word32)Td4[GETBYTE(s0, 2)] << 16) ^
((word32)Td4[GETBYTE(s3, 1)] << 8) ^
((word32)Td4[GETBYTE(s2, 0)]);
t2 =
((word32)Td4[GETBYTE(s2, 3)] << 24) ^
((word32)Td4[GETBYTE(s1, 2)] << 16) ^
((word32)Td4[GETBYTE(s0, 1)] << 8) ^
((word32)Td4[GETBYTE(s3, 0)]);
t3 =
((word32)Td4[GETBYTE(s3, 3)] << 24) ^
((word32)Td4[GETBYTE(s2, 2)] << 16) ^
((word32)Td4[GETBYTE(s1, 1)] << 8) ^
((word32)Td4[GETBYTE(s0, 0)]);
s0 = t0 ^ rk[0];
s1 = t1 ^ rk[1];
s2 = t2 ^ rk[2];
s3 = t3 ^ rk[3];
#endif
/* write out */
#ifdef LITTLE_ENDIAN_ORDER
@ -2319,10 +2551,17 @@ static void wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock)
{
temp = rk[3];
rk[4] = rk[0] ^
#ifndef WOLFSSL_AES_SMALL_TABLES
(Te[2][GETBYTE(temp, 2)] & 0xff000000) ^
(Te[3][GETBYTE(temp, 1)] & 0x00ff0000) ^
(Te[0][GETBYTE(temp, 0)] & 0x0000ff00) ^
(Te[1][GETBYTE(temp, 3)] & 0x000000ff) ^
#else
((word32)Tsbox[GETBYTE(temp, 2)] << 24) ^
((word32)Tsbox[GETBYTE(temp, 1)] << 16) ^
((word32)Tsbox[GETBYTE(temp, 0)] << 8) ^
((word32)Tsbox[GETBYTE(temp, 3)]) ^
#endif
rcon[i];
rk[5] = rk[1] ^ rk[4];
rk[6] = rk[2] ^ rk[5];
@ -2342,10 +2581,17 @@ static void wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock)
{
temp = rk[ 5];
rk[ 6] = rk[ 0] ^
#ifndef WOLFSSL_AES_SMALL_TABLES
(Te[2][GETBYTE(temp, 2)] & 0xff000000) ^
(Te[3][GETBYTE(temp, 1)] & 0x00ff0000) ^
(Te[0][GETBYTE(temp, 0)] & 0x0000ff00) ^
(Te[1][GETBYTE(temp, 3)] & 0x000000ff) ^
#else
((word32)Tsbox[GETBYTE(temp, 2)] << 24) ^
((word32)Tsbox[GETBYTE(temp, 1)] << 16) ^
((word32)Tsbox[GETBYTE(temp, 0)] << 8) ^
((word32)Tsbox[GETBYTE(temp, 3)]) ^
#endif
rcon[i];
rk[ 7] = rk[ 1] ^ rk[ 6];
rk[ 8] = rk[ 2] ^ rk[ 7];
@ -2366,10 +2612,17 @@ static void wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock)
{
temp = rk[ 7];
rk[ 8] = rk[ 0] ^
#ifndef WOLFSSL_AES_SMALL_TABLES
(Te[2][GETBYTE(temp, 2)] & 0xff000000) ^
(Te[3][GETBYTE(temp, 1)] & 0x00ff0000) ^
(Te[0][GETBYTE(temp, 0)] & 0x0000ff00) ^
(Te[1][GETBYTE(temp, 3)] & 0x000000ff) ^
#else
((word32)Tsbox[GETBYTE(temp, 2)] << 24) ^
((word32)Tsbox[GETBYTE(temp, 1)] << 16) ^
((word32)Tsbox[GETBYTE(temp, 0)] << 8) ^
((word32)Tsbox[GETBYTE(temp, 3)]) ^
#endif
rcon[i];
rk[ 9] = rk[ 1] ^ rk[ 8];
rk[10] = rk[ 2] ^ rk[ 9];
@ -2378,10 +2631,17 @@ static void wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock)
break;
temp = rk[11];
rk[12] = rk[ 4] ^
#ifndef WOLFSSL_AES_SMALL_TABLES
(Te[2][GETBYTE(temp, 3)] & 0xff000000) ^
(Te[3][GETBYTE(temp, 2)] & 0x00ff0000) ^
(Te[0][GETBYTE(temp, 1)] & 0x0000ff00) ^
(Te[1][GETBYTE(temp, 0)] & 0x000000ff);
#else
((word32)Tsbox[GETBYTE(temp, 3)] << 24) ^
((word32)Tsbox[GETBYTE(temp, 2)] << 16) ^
((word32)Tsbox[GETBYTE(temp, 1)] << 8) ^
((word32)Tsbox[GETBYTE(temp, 0)]);
#endif
rk[13] = rk[ 5] ^ rk[12];
rk[14] = rk[ 6] ^ rk[13];
rk[15] = rk[ 7] ^ rk[14];
@ -2395,7 +2655,7 @@ static void wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock)
return BAD_FUNC_ARG;
} /* switch */
#ifdef HAVE_AES_DECRYPT
#if defined(HAVE_AES_DECRYPT)
if (dir == AES_DECRYPTION) {
unsigned int j;
rk = aes->key;
@ -2407,6 +2667,7 @@ static void wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock)
temp = rk[i + 2]; rk[i + 2] = rk[j + 2]; rk[j + 2] = temp;
temp = rk[i + 3]; rk[i + 3] = rk[j + 3]; rk[j + 3] = temp;
}
#if !defined(WOLFSSL_AES_SMALL_TABLES)
/* apply the inverse MixColumn transform to all round keys but the
first and the last: */
for (i = 1; i < aes->rounds; i++) {
@ -2432,6 +2693,7 @@ static void wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock)
Td[2][Te[1][GETBYTE(rk[3], 1)] & 0xff] ^
Td[3][Te[1][GETBYTE(rk[3], 0)] & 0xff];
}
#endif
}
#else
(void)dir;