This commit is contained in:
Frank Denis
2016-09-30 08:40:15 +02:00
parent b20d227f37
commit 42dc78b38b
3 changed files with 130 additions and 125 deletions
@@ -4,128 +4,133 @@
void xor2(aes_uint128_t *r, const aes_uint128_t *x)
{
r->u64[0] ^= x->u64[0];
r->u64[1] ^= x->u64[1];
r->u64[0] ^= x->u64[0];
r->u64[1] ^= x->u64[1];
}
void and2(aes_uint128_t *r, const aes_uint128_t *x)
{
r->u64[0] &= x->u64[0];
r->u64[1] &= x->u64[1];
r->u64[0] &= x->u64[0];
r->u64[1] &= x->u64[1];
}
void or2(aes_uint128_t *r, const aes_uint128_t *x)
{
r->u64[0] |= x->u64[0];
r->u64[1] |= x->u64[1];
r->u64[0] |= x->u64[0];
r->u64[1] |= x->u64[1];
}
void copy2(aes_uint128_t *r, const aes_uint128_t *x)
{
r->u64[0] = x->u64[0];
r->u64[1] = x->u64[1];
r->u64[0] = x->u64[0];
r->u64[1] = x->u64[1];
}
void shufb(aes_uint128_t *r, const unsigned char *l)
{
aes_uint128_t t;
uint8_t *ct;
uint8_t *cr;
aes_uint128_t t;
uint8_t *ct;
uint8_t *cr;
copy2(&t, r);
cr = r->u8;
ct = t.u8;
cr[0] = ct[l[0]];
cr[1] = ct[l[1]];
cr[2] = ct[l[2]];
cr[3] = ct[l[3]];
cr[4] = ct[l[4]];
cr[5] = ct[l[5]];
cr[6] = ct[l[6]];
cr[7] = ct[l[7]];
cr[8] = ct[l[8]];
cr[9] = ct[l[9]];
cr[10] = ct[l[10]];
cr[11] = ct[l[11]];
cr[12] = ct[l[12]];
cr[13] = ct[l[13]];
cr[14] = ct[l[14]];
cr[15] = ct[l[15]];
copy2(&t, r);
cr = r->u8;
ct = t.u8;
cr[0] = ct[l[0]];
cr[1] = ct[l[1]];
cr[2] = ct[l[2]];
cr[3] = ct[l[3]];
cr[4] = ct[l[4]];
cr[5] = ct[l[5]];
cr[6] = ct[l[6]];
cr[7] = ct[l[7]];
cr[8] = ct[l[8]];
cr[9] = ct[l[9]];
cr[10] = ct[l[10]];
cr[11] = ct[l[11]];
cr[12] = ct[l[12]];
cr[13] = ct[l[13]];
cr[14] = ct[l[14]];
cr[15] = ct[l[15]];
}
void shufd(aes_uint128_t *r, const aes_uint128_t *x, const unsigned int c)
{
aes_uint128_t t;
aes_uint128_t t;
t.u32[0] = x->u32[c >> 0 & 3];
t.u32[1] = x->u32[c >> 2 & 3];
t.u32[2] = x->u32[c >> 4 & 3];
t.u32[3] = x->u32[c >> 6 & 3];
copy2(r, &t);
t.u32[0] = x->u32[c >> 0 & 3];
t.u32[1] = x->u32[c >> 2 & 3];
t.u32[2] = x->u32[c >> 4 & 3];
t.u32[3] = x->u32[c >> 6 & 3];
copy2(r, &t);
}
void rshift32_littleendian(aes_uint128_t *r, const unsigned int n)
{
unsigned char *rp = (unsigned char *)r;
uint32_t t;
t = LOAD32_LE(rp);
t >>= n;
STORE32_LE(rp, t);
t = LOAD32_LE(rp+4);
t >>= n;
STORE32_LE(rp+4, t);
t = LOAD32_LE(rp+8);
t >>= n;
STORE32_LE(rp+8, t);
t = LOAD32_LE(rp+12);
t >>= n;
STORE32_LE(rp+12, t);
unsigned char *rp = (unsigned char *)r;
uint32_t t;
t = LOAD32_LE(rp);
t >>= n;
STORE32_LE(rp, t);
t = LOAD32_LE(rp+4);
t >>= n;
STORE32_LE(rp+4, t);
t = LOAD32_LE(rp+8);
t >>= n;
STORE32_LE(rp+8, t);
t = LOAD32_LE(rp+12);
t >>= n;
STORE32_LE(rp+12, t);
}
void rshift64_littleendian(aes_uint128_t *r, const unsigned int n)
{
unsigned char *rp = (unsigned char *)r;
uint64 t;
t = LOAD64_LE(rp);
t >>= n;
STORE64_LE(rp, t);
t = LOAD64_LE(rp+8);
t >>= n;
STORE64_LE(rp+8, t);
unsigned char *rp = (unsigned char *)r;
uint64 t;
t = LOAD64_LE(rp);
t >>= n;
STORE64_LE(rp, t);
t = LOAD64_LE(rp+8);
t >>= n;
STORE64_LE(rp+8, t);
}
void lshift64_littleendian(aes_uint128_t *r, const unsigned int n)
{
unsigned char *rp = (unsigned char *)r;
uint64 t;
t = LOAD64_LE(rp);
t <<= n;
STORE64_LE(rp, t);
t = LOAD64_LE(rp+8);
t <<= n;
STORE64_LE(rp+8, t);
unsigned char *rp = (unsigned char *)r;
uint64 t;
t = LOAD64_LE(rp);
t <<= n;
STORE64_LE(rp, t);
t = LOAD64_LE(rp+8);
t <<= n;
STORE64_LE(rp+8, t);
}
void toggle(aes_uint128_t *r)
{
r->u64[0] ^= 0xffffffffffffffffULL;
r->u64[1] ^= 0xffffffffffffffffULL;
r->u64[0] ^= 0xffffffffffffffffULL;
r->u64[1] ^= 0xffffffffffffffffULL;
}
void xor_rcon(aes_uint128_t *r)
{
unsigned char *rp = (unsigned char *)r;
uint32_t t;
t = LOAD32_LE(rp+12);
t ^= 0xffffffff;
STORE32_LE(rp+12, t);
unsigned char *rp = (unsigned char *)r;
uint32_t t;
t = LOAD32_LE(rp+12);
t ^= 0xffffffff;
STORE32_LE(rp+12, t);
}
void add_uint32_big(aes_uint128_t *r, uint32_t x)
{
unsigned char *rp = (unsigned char *)r;
uint32_t t;
t = LOAD32_LE(rp+12);
t += x;
STORE32_LE(rp+12, t);
unsigned char *rp = (unsigned char *)r;
uint32_t t;
t = LOAD32_LE(rp+12);
t += x;
STORE32_LE(rp+12, t);
}
@@ -1,29 +1,27 @@
#include "crypto_stream_aes128ctr.h"
int crypto_stream_aes128ctr(
unsigned char *out,
unsigned long long outlen,
const unsigned char *n,
const unsigned char *k
)
int crypto_stream_aes128ctr(unsigned char *out,
unsigned long long outlen,
const unsigned char *n,
const unsigned char *k)
{
unsigned char d[crypto_stream_aes128ctr_BEFORENMBYTES];
crypto_stream_aes128ctr_beforenm(d, k);
crypto_stream_aes128ctr_afternm(out, outlen, n, d);
return 0;
}
int crypto_stream_aes128ctr_xor(
unsigned char *out,
const unsigned char *in,
unsigned long long inlen,
const unsigned char *n,
const unsigned char *k
)
int crypto_stream_aes128ctr_xor(unsigned char *out,
const unsigned char *in,
unsigned long long inlen,
const unsigned char *n,
const unsigned char *k)
{
unsigned char d[crypto_stream_aes128ctr_BEFORENMBYTES];
crypto_stream_aes128ctr_beforenm(d, k);
crypto_stream_aes128ctr_xor_afternm(out, in, inlen, n, d);
return 0;
}
@@ -7,41 +7,43 @@
#include "common.h"
#include "consts.h"
int crypto_stream_aes128ctr_xor_afternm(unsigned char *out, const unsigned char *in, unsigned long long len, const unsigned char *nonce, const unsigned char *c)
int crypto_stream_aes128ctr_xor_afternm(unsigned char *out,
const unsigned char *in,
unsigned long long len,
const unsigned char *nonce,
const unsigned char *c)
{
aes_uint128_t xmm0;
aes_uint128_t xmm1;
aes_uint128_t xmm2;
aes_uint128_t xmm3;
aes_uint128_t xmm4;
aes_uint128_t xmm5;
aes_uint128_t xmm6;
aes_uint128_t xmm7;
aes_uint128_t xmm8;
aes_uint128_t xmm9;
aes_uint128_t xmm10;
aes_uint128_t xmm11;
aes_uint128_t xmm12;
aes_uint128_t xmm13;
aes_uint128_t xmm14;
aes_uint128_t xmm15;
aes_uint128_t xmm0;
aes_uint128_t xmm1;
aes_uint128_t xmm2;
aes_uint128_t xmm3;
aes_uint128_t xmm4;
aes_uint128_t xmm5;
aes_uint128_t xmm6;
aes_uint128_t xmm7;
aes_uint128_t nonce_stack;
unsigned long long lensav;
unsigned char bl[128];
unsigned char *blp;
unsigned char *np;
unsigned char b;
aes_uint128_t xmm8;
aes_uint128_t xmm9;
aes_uint128_t xmm10;
aes_uint128_t xmm11;
aes_uint128_t xmm12;
aes_uint128_t xmm13;
aes_uint128_t xmm14;
aes_uint128_t xmm15;
uint32_t tmp;
aes_uint128_t nonce_stack;
unsigned long long lensav;
unsigned char bl[128];
unsigned char *blp;
unsigned char *np;
unsigned char b;
/* Copy nonce on the stack */
copy2(&nonce_stack, (const aes_uint128_t *) (nonce + 0));
np = (unsigned char *)&nonce_stack;
uint32_t tmp;
/* Copy nonce on the stack */
copy2(&nonce_stack, (const aes_uint128_t *) (nonce + 0));
np = (unsigned char *)&nonce_stack;
enc_block:
enc_block:
xmm0 = *(aes_uint128_t *) (np + 0);
copy2(&xmm1, &xmm0);
@@ -116,7 +118,7 @@ int crypto_stream_aes128ctr_xor_afternm(unsigned char *out, const unsigned char
goto enc_block;
partial:
partial:
lensav = len;
len >>= 4;
@@ -135,7 +137,7 @@ int crypto_stream_aes128ctr_xor_afternm(unsigned char *out, const unsigned char
*(aes_uint128_t *)(blp + 96) = xmm10;
*(aes_uint128_t *)(blp + 112) = xmm13;
bytes:
bytes:
if(lensav == 0) goto end;
@@ -150,7 +152,7 @@ int crypto_stream_aes128ctr_xor_afternm(unsigned char *out, const unsigned char
goto bytes;
full:
full:
tmp = LOAD32_BE(np + 12);
tmp += 8;
@@ -174,7 +176,7 @@ int crypto_stream_aes128ctr_xor_afternm(unsigned char *out, const unsigned char
*(aes_uint128_t *) (out + 96) = xmm10;
*(aes_uint128_t *) (out + 112) = xmm13;
end:
end:
return 0;
}