crypto: sha512 - Avoid stack bloat on i386

Unfortunately in reducing W from 80 to 16 we ended up unrolling
the loop twice.  As gcc has issues dealing with 64-bit ops on
i386 this means that we end up using even more stack space (>1K).

This patch solves the W reduction by moving LOAD_OP/BLEND_OP
into the loop itself, thus avoiding the need to duplicate it.

While the stack space still isn't great (>0.5K) it is at least
in the same ball park as the amount of stack used for our C sha1
implementation.

Note that this patch basically reverts to the original code so
the diff looks bigger than it really is.

Cc: stable@vger.kernel.org
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
This commit is contained in:
Herbert Xu 2012-02-05 15:09:28 +11:00
parent 58d7d18b52
commit 3a92d687c8

View file

@ -89,46 +89,42 @@ sha512_transform(u64 *state, const u8 *input)
int i; int i;
u64 W[16]; u64 W[16];
/* load the input */
for (i = 0; i < 16; i++)
LOAD_OP(i, W, input);
/* load the state into our registers */ /* load the state into our registers */
a=state[0]; b=state[1]; c=state[2]; d=state[3]; a=state[0]; b=state[1]; c=state[2]; d=state[3];
e=state[4]; f=state[5]; g=state[6]; h=state[7]; e=state[4]; f=state[5]; g=state[6]; h=state[7];
#define SHA512_0_15(i, a, b, c, d, e, f, g, h) \ /* now iterate */
t1 = h + e1(e) + Ch(e, f, g) + sha512_K[i] + W[i]; \ for (i=0; i<80; i+=8) {
t2 = e0(a) + Maj(a, b, c); \ if (!(i & 8)) {
d += t1; \ int j;
h = t1 + t2
#define SHA512_16_79(i, a, b, c, d, e, f, g, h) \ if (i < 16) {
BLEND_OP(i, W); \ /* load the input */
t1 = h + e1(e) + Ch(e, f, g) + sha512_K[i] + W[(i)&15]; \ for (j = 0; j < 16; j++)
t2 = e0(a) + Maj(a, b, c); \ LOAD_OP(i + j, W, input);
d += t1; \ } else {
h = t1 + t2 for (j = 0; j < 16; j++) {
BLEND_OP(i + j, W);
}
}
}
for (i = 0; i < 16; i += 8) { t1 = h + e1(e) + Ch(e,f,g) + sha512_K[i ] + W[(i & 15)];
SHA512_0_15(i, a, b, c, d, e, f, g, h); t2 = e0(a) + Maj(a,b,c); d+=t1; h=t1+t2;
SHA512_0_15(i + 1, h, a, b, c, d, e, f, g); t1 = g + e1(d) + Ch(d,e,f) + sha512_K[i+1] + W[(i & 15) + 1];
SHA512_0_15(i + 2, g, h, a, b, c, d, e, f); t2 = e0(h) + Maj(h,a,b); c+=t1; g=t1+t2;
SHA512_0_15(i + 3, f, g, h, a, b, c, d, e); t1 = f + e1(c) + Ch(c,d,e) + sha512_K[i+2] + W[(i & 15) + 2];
SHA512_0_15(i + 4, e, f, g, h, a, b, c, d); t2 = e0(g) + Maj(g,h,a); b+=t1; f=t1+t2;
SHA512_0_15(i + 5, d, e, f, g, h, a, b, c); t1 = e + e1(b) + Ch(b,c,d) + sha512_K[i+3] + W[(i & 15) + 3];
SHA512_0_15(i + 6, c, d, e, f, g, h, a, b); t2 = e0(f) + Maj(f,g,h); a+=t1; e=t1+t2;
SHA512_0_15(i + 7, b, c, d, e, f, g, h, a); t1 = d + e1(a) + Ch(a,b,c) + sha512_K[i+4] + W[(i & 15) + 4];
} t2 = e0(e) + Maj(e,f,g); h+=t1; d=t1+t2;
for (i = 16; i < 80; i += 8) { t1 = c + e1(h) + Ch(h,a,b) + sha512_K[i+5] + W[(i & 15) + 5];
SHA512_16_79(i, a, b, c, d, e, f, g, h); t2 = e0(d) + Maj(d,e,f); g+=t1; c=t1+t2;
SHA512_16_79(i + 1, h, a, b, c, d, e, f, g); t1 = b + e1(g) + Ch(g,h,a) + sha512_K[i+6] + W[(i & 15) + 6];
SHA512_16_79(i + 2, g, h, a, b, c, d, e, f); t2 = e0(c) + Maj(c,d,e); f+=t1; b=t1+t2;
SHA512_16_79(i + 3, f, g, h, a, b, c, d, e); t1 = a + e1(f) + Ch(f,g,h) + sha512_K[i+7] + W[(i & 15) + 7];
SHA512_16_79(i + 4, e, f, g, h, a, b, c, d); t2 = e0(b) + Maj(b,c,d); e+=t1; a=t1+t2;
SHA512_16_79(i + 5, d, e, f, g, h, a, b, c);
SHA512_16_79(i + 6, c, d, e, f, g, h, a, b);
SHA512_16_79(i + 7, b, c, d, e, f, g, h, a);
} }
state[0] += a; state[1] += b; state[2] += c; state[3] += d; state[0] += a; state[1] += b; state[2] += c; state[3] += d;