This commit is contained in:
Frank Denis 2013-04-26 22:19:24 -07:00
parent 77d96e79b7
commit 6ec9828b9f

View File

@ -13,22 +13,15 @@
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
/* key setup for AES-256*/ /* key setup for AES-256*/
static void ECRYPT_keysetup( static void
ECRYPT_ctx* ctx, ECRYPT_keysetup(ECRYPT_ctx* ctx, const u8* key, u32 keysize, u32 ivsize)
const u8* key,
u32 keysize,
u32 ivsize)
{ {
unsigned int w[Nk*(Nr+1)], temp; unsigned int w[Nk*(Nr+1)], temp;
int i, j; int i, j;
(void) sizeof(char[sizeof *ctx == crypto_stream_BEFORENMBYTES ? 1 : -1]); (void) sizeof(char[sizeof *ctx == crypto_stream_BEFORENMBYTES ? 1 : -1]);
i = 0; for( i = 0; i < Nk; i++ ) {
for( i = 0; i < Nk; i++ )
{
w[i] = key[(i << 2)]; w[i] = key[(i << 2)];
w[i] |= key[(i << 2)+1] << 8; w[i] |= key[(i << 2)+1] << 8;
w[i] |= key[(i << 2)+2] << 16; w[i] |= key[(i << 2)+2] << 16;
@ -37,8 +30,7 @@ static void ECRYPT_keysetup(
i = Nk; i = Nk;
while( i < Nb*(Nr+1) ) while( i < Nb*(Nr+1) ) {
{
temp = w[i-1]; temp = w[i-1];
temp = Sbox[ temp & 0xFF] << 24 ^ temp = Sbox[ temp & 0xFF] << 24 ^
@ -82,19 +74,17 @@ static void ECRYPT_keysetup(
i++; i++;
} }
for (i = 0; i <= Nr; i++) {
for (i = 0; i <= Nr; i++) for (j = 0; j < Nb; j++) {
for (j = 0; j < Nb; j++)
ctx->round_key[i][j] = w[(i<<2)+j]; ctx->round_key[i][j] = w[(i<<2)+j];
} }
}
}
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
static void
static void ECRYPT_ivsetup( ECRYPT_ivsetup(ECRYPT_ctx* ctx, const u8* iv)
ECRYPT_ctx* ctx,
const u8* iv)
{ {
(void) sizeof(char[(sizeof ctx->counter) == crypto_stream_NONCEBYTES ? 1 : -1]); (void) sizeof(char[(sizeof ctx->counter) == crypto_stream_NONCEBYTES ? 1 : -1]);
memcpy(ctx->counter, iv, crypto_stream_NONCEBYTES); memcpy(ctx->counter, iv, crypto_stream_NONCEBYTES);
@ -102,20 +92,17 @@ static void ECRYPT_ivsetup(
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */
static void ECRYPT_process_bytes( static void
int action, ECRYPT_process_bytes(int action, ECRYPT_ctx* ctx, const u8* input, u8* output,
ECRYPT_ctx* ctx,
const u8* input,
u8* output,
u32 msglen) u32 msglen)
{ {
u8 keystream[16]; u8 keystream[16];
u32 i; u32 i;
memset(keystream, 0, sizeof keystream);
partial_precompute_tworounds(ctx); partial_precompute_tworounds(ctx);
for ( ; msglen >= 16; msglen -= 16, input += 16, output += 16) for ( ; msglen >= 16; msglen -= 16, input += 16, output += 16) {
{
aes256_enc_block(ctx->counter, keystream, ctx); aes256_enc_block(ctx->counter, keystream, ctx);
((u32*)output)[0] = ((u32*)input)[0] ^ ((u32*)keystream)[0] ^ ctx->round_key[Nr][0]; ((u32*)output)[0] = ((u32*)input)[0] ^ ((u32*)keystream)[0] ^ ctx->round_key[Nr][0];
@ -125,22 +112,23 @@ static void ECRYPT_process_bytes(
ctx->counter[0]++; ctx->counter[0]++;
if ((ctx->counter[0] & 0xff)== 0) partial_precompute_tworounds(ctx); if ((ctx->counter[0] & 0xff)== 0) {
partial_precompute_tworounds(ctx);
}
} }
if (msglen > 0) if (msglen > 0) {
{
aes256_enc_block(ctx->counter, keystream, ctx); aes256_enc_block(ctx->counter, keystream, ctx);
((u32*)keystream)[0] ^= ctx->round_key[Nr][0]; ((u32*)keystream)[0] ^= ctx->round_key[Nr][0];
((u32*)keystream)[1] ^= ctx->round_key[Nr][1]; ((u32*)keystream)[1] ^= ctx->round_key[Nr][1];
((u32*)keystream)[2] ^= ctx->round_key[Nr][2]; ((u32*)keystream)[2] ^= ctx->round_key[Nr][2];
((u32*)keystream)[3] ^= ctx->round_key[Nr][3]; ((u32*)keystream)[3] ^= ctx->round_key[Nr][3];
for (i = 0; i < msglen; i ++) for (i = 0; i < msglen; i ++) {
output[i] = input[i] ^ keystream[i]; output[i] = input[i] ^ keystream[i];
} }
} }
}
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */