use aes32esi* for key schedule

This commit is contained in:
Romain Dolbeau 2021-02-16 04:43:27 -05:00
parent 4335357bae
commit e0d20299a8

View file

@ -81,6 +81,111 @@ static inline void aes256_1Tft_encrypt(const unsigned int *aes_edrk, const unsig
output[2] = (Y2);
output[3] = (Y3);
}
static inline void aes256_Tsetkey_encrypt(const unsigned int key[], unsigned int *aes_edrk) {
unsigned int i = 0;
unsigned int rotl_aes_edrk;
unsigned int tmp8, tmp9, tmp10, tmp11;
unsigned int tmp12, tmp13, tmp14, tmp15;
unsigned int temp_lds;
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
unsigned int round = 0x01000000;
#else
unsigned int round = 0x00000001;
#endif
tmp8 = (key[0]);
aes_edrk[0] = tmp8;
tmp9 = (key[1]);
aes_edrk[1] = tmp9;
tmp10 = (key[2]);
aes_edrk[2] = tmp10;
tmp11 = (key[3]);
aes_edrk[3] = tmp11;
tmp12 = (key[4]);
aes_edrk[4] = tmp12;
tmp13 = (key[5]);
aes_edrk[5] = tmp13;
tmp14 = (key[6]);
aes_edrk[6] = tmp14;
tmp15 = (key[7]);
aes_edrk[7] = tmp15;
for( i = 8; i < 56; /* i+=8 */ )
{
#ifndef __riscv
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
rotl_aes_edrk = rotl(tmp15,8);
#else
rotl_aes_edrk = rotr(tmp15,8);
#endif
temp_lds = f_FSb_32__1(rotl_aes_edrk) ^ f_FSb_32__2( rotl_aes_edrk );
tmp8 = tmp8 ^ round ^ temp_lds;
round = round << 1;
#else
tmp8 = tmp8 ^ round;
round = round << 1;
rotl_aes_edrk = rotr(tmp15,8);
tmp8 = aes32esi0(tmp8, rotl_aes_edrk);
tmp8 = aes32esi1(tmp8, rotl_aes_edrk);
tmp8 = aes32esi2(tmp8, rotl_aes_edrk);
tmp8 = aes32esi3(tmp8, rotl_aes_edrk);
#endif
aes_edrk[i++] = tmp8;
tmp9 = tmp9 ^ tmp8;
aes_edrk[i++] = tmp9;
tmp10 = tmp10 ^ tmp9;
aes_edrk[i++] = tmp10;
tmp11 = tmp11 ^ tmp10;
aes_edrk[i++] = tmp11;
#ifndef __riscv
temp_lds = f_FSb_32__1(tmp11) ^ f_FSb_32__2(tmp11);
tmp12 = tmp12 ^ temp_lds;
#else
tmp12 = aes32esi0(tmp12, tmp11);
tmp12 = aes32esi1(tmp12, tmp11);
tmp12 = aes32esi2(tmp12, tmp11);
tmp12 = aes32esi3(tmp12, tmp11);
#endif
aes_edrk[i++] = tmp12;
tmp13 = tmp13 ^ tmp12;
aes_edrk[i++] = tmp13;
tmp14 = tmp14 ^ tmp13;
aes_edrk[i++] = tmp14;
tmp15 = tmp15 ^ tmp14;
aes_edrk[i++] = tmp15;
}
#ifndef __riscv
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
rotl_aes_edrk = rotl(tmp15,8);
#else
rotl_aes_edrk = rotr(tmp15,8);
#endif
temp_lds = f_FSb_32__1(rotl_aes_edrk) ^ f_FSb_32__2( rotl_aes_edrk );
tmp8 = tmp8 ^ round ^ temp_lds;
round = round << 1;
#else
tmp8 = tmp8 ^ round;
round = round << 1;
rotl_aes_edrk = rotr(tmp15,8);
tmp8 = aes32esi0(tmp8, rotl_aes_edrk);
tmp8 = aes32esi1(tmp8, rotl_aes_edrk);
tmp8 = aes32esi2(tmp8, rotl_aes_edrk);
tmp8 = aes32esi3(tmp8, rotl_aes_edrk);
#endif
aes_edrk[i++] = tmp8;
tmp9 = tmp9 ^ tmp8;
aes_edrk[i++] = tmp9;
tmp10 = tmp10 ^ tmp9;
aes_edrk[i++] = tmp10;
tmp11 = tmp11 ^ tmp10;
aes_edrk[i++] = tmp11;
}
static inline void aes256_1ft_encrypt(const unsigned int *aes_edrk, const unsigned int *input, unsigned int *output)
{
@ -166,7 +271,7 @@ int crypto_core(
const unsigned char *c
) {
unsigned int rkeys[60];
aes256_setkey_encrypt((const unsigned int*)k,rkeys);
aes256_Tsetkey_encrypt((const unsigned int*)k,rkeys);
aes256_1Tft_encrypt(rkeys, (const unsigned int*)in, (unsigned int*)out);
return 0;
}