Skip to content

Commit

Permalink
crypto: lib/sha256 - Clear W[] in sha256_update() instead of sha256_t…
Browse files Browse the repository at this point in the history
…ransform()

The temporary W[] array is currently zeroed out once every call to
sha256_transform(), i.e. once every 64 bytes of input data. Moving it to
sha256_update() instead so that it is cleared only once per update can
save about 2-3% of the total time taken to compute the digest, with a
reasonable memset() implementation, and considerably more (~20%) with a
bad one (eg the x86 purgatory currently uses a memset() coded in C).

Signed-off-by: Arvind Sankar <[email protected]>
Reviewed-by: Eric Biggers <[email protected]>
Acked-by: Ard Biesheuvel <[email protected]>
Signed-off-by: Herbert Xu <[email protected]>
  • Loading branch information
nivedita76 authored and herbertx committed Oct 30, 2020
1 parent 7a4295f commit b839981
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions lib/crypto/sha256.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ static inline void BLEND_OP(int I, u32 *W)
W[I] = s1(W[I-2]) + W[I-7] + s0(W[I-15]) + W[I-16];
}

static void sha256_transform(u32 *state, const u8 *input)
static void sha256_transform(u32 *state, const u8 *input, u32 *W)
{
u32 a, b, c, d, e, f, g, h, t1, t2;
u32 W[64];
int i;

/* load the input */
Expand Down Expand Up @@ -200,15 +199,13 @@ static void sha256_transform(u32 *state, const u8 *input)

state[0] += a; state[1] += b; state[2] += c; state[3] += d;
state[4] += e; state[5] += f; state[6] += g; state[7] += h;

/* clear any sensitive info... */
memzero_explicit(W, 64 * sizeof(u32));
}

void sha256_update(struct sha256_state *sctx, const u8 *data, unsigned int len)
{
unsigned int partial, done;
const u8 *src;
u32 W[64];

partial = sctx->count & 0x3f;
sctx->count += len;
Expand All @@ -223,11 +220,13 @@ void sha256_update(struct sha256_state *sctx, const u8 *data, unsigned int len)
}

do {
sha256_transform(sctx->state, src);
sha256_transform(sctx->state, src, W);
done += 64;
src = data + done;
} while (done + 63 < len);

memzero_explicit(W, sizeof(W));

partial = 0;
}
memcpy(sctx->buf + partial, src, len - done);
Expand Down

0 comments on commit b839981

Please sign in to comment.