sha{512,256}: use a local loop instead of if + memcpy()

Compilers can't figure out the max inlen value, so help them with an
explicit AND.

Unify the name of the input pointer by the way.
This commit is contained in:
Frank Denis
2016-04-12 02:14:45 +02:00
parent 727703a8a1
commit d7294320c4
2 changed files with 13 additions and 10 deletions
@@ -210,6 +210,7 @@ crypto_hash_sha256_update(crypto_hash_sha256_state *state,
const unsigned char *in,
unsigned long long inlen)
{
unsigned long long i;
uint32_t r;
if (inlen <= 0U) {
@@ -232,8 +233,9 @@ crypto_hash_sha256_update(crypto_hash_sha256_state *state,
in += 64;
inlen -= 64;
}
if (inlen > 0) {
memcpy(state->buf, in, inlen); /* inlen < 64 */
inlen &= 63;
for (i = 0; i < inlen; i++) {
state->buf[i] = in[i];
}
return 0;
}
@@ -232,9 +232,9 @@ crypto_hash_sha512_update(crypto_hash_sha512_state *state,
const unsigned char *in,
unsigned long long inlen)
{
unsigned long long i;
uint64_t bitlen[2];
uint64_t r;
const unsigned char *src = in;
r = (state->count[1] >> 3) & 0x7f;
@@ -249,21 +249,22 @@ crypto_hash_sha512_update(crypto_hash_sha512_state *state,
state->count[0] += bitlen[0];
if (inlen < 128 - r) {
memcpy(&state->buf[r], src, inlen);
memcpy(&state->buf[r], in, inlen);
return 0;
}
memcpy(&state->buf[r], src, 128 - r);
memcpy(&state->buf[r], in, 128 - r);
SHA512_Transform(state->state, state->buf);
src += 128 - r;
in += 128 - r;
inlen -= 128 - r;
while (inlen >= 128) {
SHA512_Transform(state->state, src);
src += 128;
SHA512_Transform(state->state, in);
in += 128;
inlen -= 128;
}
if (inlen > 0) {
memcpy(state->buf, src, inlen); /* inlen < 128 */
inlen &= 127;
for (i = 0; i < inlen; i++) {
state->buf[i] = in[i];
}
return 0;
}