Merge pull request #7840 from yanrayw/7381_aes_gen_table

AES: use uint8_t for array of pow and log to save RAM usage
This commit is contained in:
Tom Cosgrove 2023-07-04 08:34:12 +01:00 committed by GitHub
commit 9b20c6fcc1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -391,25 +391,26 @@ static int aes_init_done = 0;
static void aes_gen_tables(void) static void aes_gen_tables(void)
{ {
int i, x, y, z; int i;
int pow[256]; uint8_t x, y, z;
int log[256]; uint8_t pow[256];
uint8_t log[256];
/* /*
* compute pow and log tables over GF(2^8) * compute pow and log tables over GF(2^8)
*/ */
for (i = 0, x = 1; i < 256; i++) { for (i = 0, x = 1; i < 256; i++) {
pow[i] = x; pow[i] = x;
log[x] = i; log[x] = (uint8_t) i;
x = MBEDTLS_BYTE_0(x ^ XTIME(x)); x ^= XTIME(x);
} }
/* /*
* calculate the round constants * calculate the round constants
*/ */
for (i = 0, x = 1; i < 10; i++) { for (i = 0, x = 1; i < 10; i++) {
RCON[i] = (uint32_t) x; RCON[i] = x;
x = MBEDTLS_BYTE_0(XTIME(x)); x = XTIME(x);
} }
/* /*
@ -421,13 +422,13 @@ static void aes_gen_tables(void)
for (i = 1; i < 256; i++) { for (i = 1; i < 256; i++) {
x = pow[255 - log[i]]; x = pow[255 - log[i]];
y = x; y = MBEDTLS_BYTE_0((y << 1) | (y >> 7)); y = x; y = (y << 1) | (y >> 7);
x ^= y; y = MBEDTLS_BYTE_0((y << 1) | (y >> 7)); x ^= y; y = (y << 1) | (y >> 7);
x ^= y; y = MBEDTLS_BYTE_0((y << 1) | (y >> 7)); x ^= y; y = (y << 1) | (y >> 7);
x ^= y; y = MBEDTLS_BYTE_0((y << 1) | (y >> 7)); x ^= y; y = (y << 1) | (y >> 7);
x ^= y ^ 0x63; x ^= y ^ 0x63;
FSb[i] = (unsigned char) x; FSb[i] = x;
RSb[x] = (unsigned char) i; RSb[x] = (unsigned char) i;
} }
@ -436,8 +437,8 @@ static void aes_gen_tables(void)
*/ */
for (i = 0; i < 256; i++) { for (i = 0; i < 256; i++) {
x = FSb[i]; x = FSb[i];
y = MBEDTLS_BYTE_0(XTIME(x)); y = XTIME(x);
z = MBEDTLS_BYTE_0(y ^ x); z = y ^ x;
FT0[i] = ((uint32_t) y) ^ FT0[i] = ((uint32_t) y) ^
((uint32_t) x << 8) ^ ((uint32_t) x << 8) ^