chacha: calculate rounds in separate function (helps registerizer), get rid of unrolled code

This commit is contained in:
cinap_lenrek 2016-06-26 15:28:01 +02:00
parent 69b0980a42
commit 67a9174ff8

View file

@ -15,13 +15,13 @@ enum{
};
/* little-endian data order */
#define GET4(p) ((((((p)[3]<<8) | (p)[2])<<8) | (p)[1])<<8 | (p)[0])
#define PUT4(p, v) (((p)[0]=v), (v>>=8), ((p)[1]=v), (v>>=8), ((p)[2]=v), (v>>=8), ((p)[3]=v))
#define GET4(p) ((p)[0]|((p)[1]<<8)|((p)[2]<<16)|((p)[3]<<24))
#define PUT4(p,v) (p)[0]=(v);(p)[1]=(v)>>8;(p)[2]=(v)>>16;(p)[3]=(v)>>24
#define ROTATE(v,c) ((u32int)((v) << (c)) | ((v) >> (32 - (c))))
#define QUARTERROUND(ia,ib,ic,id) { \
u32int a, b, c, d, t;\
u32int a, b, c, d, t; \
a = x[ia]; b = x[ib]; c = x[ic]; d = x[id]; \
a += b; t = d^a; d = ROTATE(t,16); \
c += d; t = b^c; b = ROTATE(t,12); \
@ -94,13 +94,28 @@ chacha_setblock(Chachastate *s, u64int blockno)
s->input[13] = blockno>>32;
}
static void
dorounds(u32int x[Blockwords], int rounds)
{
for(; rounds > 0; rounds -= 2) {
QUARTERROUND(0, 4, 8,12)
QUARTERROUND(1, 5, 9,13)
QUARTERROUND(2, 6,10,14)
QUARTERROUND(3, 7,11,15)
QUARTERROUND(0, 5,10,15)
QUARTERROUND(1, 6,11,12)
QUARTERROUND(2, 7, 8,13)
QUARTERROUND(3, 4, 9,14)
}
}
static void
encryptblock(Chachastate *s, uchar *src, uchar *dst)
{
u32int x[Blockwords];
int i, rounds;
int i;
rounds = s->rounds;
x[0] = s->input[0];
x[1] = s->input[1];
x[2] = s->input[2];
@ -117,37 +132,8 @@ encryptblock(Chachastate *s, uchar *src, uchar *dst)
x[13] = s->input[13];
x[14] = s->input[14];
x[15] = s->input[15];
dorounds(x, s->rounds);
for(i = rounds; i > 0; i -= 2) {
QUARTERROUND(0, 4, 8,12)
QUARTERROUND(1, 5, 9,13)
QUARTERROUND(2, 6,10,14)
QUARTERROUND(3, 7,11,15)
QUARTERROUND(0, 5,10,15)
QUARTERROUND(1, 6,11,12)
QUARTERROUND(2, 7, 8,13)
QUARTERROUND(3, 4, 9,14)
}
#ifdef FULL_UNROLL
ENCRYPT(src+0*4, x[0], s->input[0], dst+0*4);
ENCRYPT(src+1*4, x[1], s->input[1], dst+1*4);
ENCRYPT(src+2*4, x[2], s->input[2], dst+2*4);
ENCRYPT(src+3*4, x[3], s->input[3], dst+3*4);
ENCRYPT(src+4*4, x[4], s->input[4], dst+4*4);
ENCRYPT(src+5*4, x[5], s->input[5], dst+5*4);
ENCRYPT(src+6*4, x[6], s->input[6], dst+6*4);
ENCRYPT(src+7*4, x[7], s->input[7], dst+7*4);
ENCRYPT(src+8*4, x[8], s->input[8], dst+8*4);
ENCRYPT(src+9*4, x[9], s->input[9], dst+9*4);
ENCRYPT(src+10*4, x[10], s->input[10], dst+10*4);
ENCRYPT(src+11*4, x[11], s->input[11], dst+11*4);
ENCRYPT(src+12*4, x[12], s->input[12], dst+12*4);
ENCRYPT(src+13*4, x[13], s->input[13], dst+13*4);
ENCRYPT(src+14*4, x[14], s->input[14], dst+14*4);
ENCRYPT(src+15*4, x[15], s->input[15], dst+15*4);
#else
for(i=0; i<nelem(x); i+=4){
ENCRYPT(src, x[i], s->input[i], dst);
ENCRYPT(src+4, x[i+1], s->input[i+1], dst+4);
@ -156,7 +142,6 @@ encryptblock(Chachastate *s, uchar *src, uchar *dst)
src += 16;
dst += 16;
}
#endif
if(++s->input[12] == 0 && s->ivwords == 2)
s->input[13]++;