aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--libkeccak.h2
-rw-r--r--libkeccak/digest.c2
-rw-r--r--libkeccak/hmac.c48
-rw-r--r--test.c120
4 files changed, 107 insertions, 65 deletions
diff --git a/libkeccak.h b/libkeccak.h
index 21b4b47..7a95f91 100644
--- a/libkeccak.h
+++ b/libkeccak.h
@@ -834,7 +834,7 @@ typedef struct libkeccak_hmac_state
*/
char leftover;
- char __pad[sizeof(void*) / sizeof(char) - 1];
+ char __pad[sizeof(void *) / sizeof(char) - 1];
} libkeccak_hmac_state_t;
diff --git a/libkeccak/digest.c b/libkeccak/digest.c
index 3e74361..891b471 100644
--- a/libkeccak/digest.c
+++ b/libkeccak/digest.c
@@ -181,7 +181,7 @@ libkeccak_f(register libkeccak_state_t *restrict state)
libkeccak_f_round64(state, (int_fast64_t)(RC[i]));
} else {
for (; i < nr; i++)
- libkeccak_f_round(state, (int_fast64_t)(RC[i] & wmod));
+ libkeccak_f_round(state, (int_fast64_t)(RC[i] & (uint_fast64_t)wmod));
}
}
diff --git a/libkeccak/hmac.c b/libkeccak/hmac.c
index aca938e..8b14eba 100644
--- a/libkeccak/hmac.c
+++ b/libkeccak/hmac.c
@@ -51,7 +51,7 @@ libkeccak_hmac_set_key(libkeccak_hmac_state_t *restrict state, const void *restr
memcpy(state->key_opad, key, key_bytes);
if (key_length & 7)
- state->key_opad[(key_bytes >> 3) - 1] &= (1 << (key_length & 7)) - 1;
+ state->key_opad[(key_bytes >> 3) - 1] &= (char)((1 << (key_length & 7)) - 1);
if ((size_t)(state->sponge.r) > key_length)
__builtin_memset(state->key_opad + key_bytes / sizeof(char), 0, size - key_bytes);
@@ -147,8 +147,10 @@ libkeccak_hmac_unmarshal(libkeccak_hmac_state_t *restrict state, const void *res
size = (state->key_length + 7) >> 3;
state->key_opad = malloc(2 * size);
- if (state->key_opad == NULL)
- return libkeccak_state_destroy(&state->sponge), -1;
+ if (!state->key_opad) {
+ libkeccak_state_destroy(&state->sponge);
+ return 0;
+ }
memcpy(state->key_opad, data, size);
data += size / sizeof(char);
@@ -208,9 +210,9 @@ libkeccak_hmac_fast_update(libkeccak_hmac_state_t *restrict state, const void *r
n = (int)(state->key_length & 7);
cn = 8 - n;
for (i = 1; i < msglen; i++)
- state->buffer[i] = (((unsigned char)(msg[i - 1])) >> cn) | (msg[i] << n);
- state->buffer[0] = (state->leftover & ((1 << n) - 1)) | (msg[0] << n);
- state->leftover = ((unsigned char)(msg[msglen - 1])) >> cn;
+ state->buffer[i] = (char)((msg[i - 1] >> cn) | (msg[i] << n));
+ state->buffer[0] = (char)((state->leftover & ((1 << n) - 1)) | (msg[0] << n));
+ state->leftover = (char)((unsigned char)msg[msglen - 1] >> cn);
return libkeccak_fast_update(&state->sponge, state->buffer, msglen);
}
@@ -256,9 +258,9 @@ libkeccak_hmac_update(libkeccak_hmac_state_t *restrict state, const void *restri
n = (int)(state->key_length & 7);
cn = 8 - n;
for (i = 1; i < msglen; i++)
- state->buffer[i] = (((unsigned char)(msg[i - 1])) >> cn) | (msg[i] << n);
- state->buffer[0] = (state->leftover & ((1 << n) - 1)) | (msg[0] << n);
- state->leftover = ((unsigned char)(msg[msglen - 1])) >> cn;
+ state->buffer[i] = (char)(((unsigned char)msg[i - 1] >> cn) | (msg[i] << n));
+ state->buffer[0] = (char)((state->leftover & ((1 << n) - 1)) | (msg[0] << n));
+ state->leftover = (char)((unsigned char)msg[msglen - 1] >> cn);
r = libkeccak_update(&state->sponge, state->buffer, msglen);
my_explicit_bzero(state->buffer, msglen);
@@ -285,8 +287,8 @@ libkeccak_hmac_fast_digest(libkeccak_hmac_state_t *restrict state, const void *r
size_t bits, const char *restrict suffix, void *restrict hashsum)
{
const char *restrict msg = msg_;
- size_t hashsize = state->sponge.n >> 3;
- char *tmp = malloc(((state->sponge.n + 7) >> 3) * sizeof(char));
+ size_t hashsize = (size_t)state->sponge.n >> 3;
+ char *tmp = malloc((size_t)((state->sponge.n + 7) >> 3) * sizeof(char));
char leftover[2];
size_t newlen;
@@ -303,8 +305,8 @@ libkeccak_hmac_fast_digest(libkeccak_hmac_state_t *restrict state, const void *r
goto fail;
leftover[0] = state->leftover;
if (bits) {
- leftover[0] |= msg[msglen] >> (state->key_length & 7);
- leftover[1] = ((unsigned char)(msg[msglen])) << (8 - (state->key_length & 7));
+ leftover[0] |= (char)(msg[msglen] >> (state->key_length & 7));
+ leftover[1] = (char)((unsigned char)msg[msglen] << (8 - (state->key_length & 7)));
}
newlen = (state->key_length & 7) + bits;
if (libkeccak_fast_digest(&state->sponge, leftover, newlen >> 3, newlen & 7, suffix, tmp) < 0)
@@ -326,8 +328,8 @@ stage_2:
goto fail;
leftover[0] = state->leftover;
if (bits) {
- leftover[0] |= tmp[hashsize] >> (state->key_length & 7);
- leftover[1] = ((unsigned char)(tmp[hashsize])) << (8 - (state->key_length & 7));
+ leftover[0] |= (char)(tmp[hashsize] >> (state->key_length & 7));
+ leftover[1] = (char)((unsigned char)tmp[hashsize] << (8 - (state->key_length & 7)));
}
newlen = (state->key_length & 7) + bits;
if (libkeccak_fast_digest(&state->sponge, leftover, newlen >> 3, newlen & 7, suffix, tmp) < 0)
@@ -361,8 +363,8 @@ libkeccak_hmac_digest(libkeccak_hmac_state_t *restrict state, const void *restri
size_t bits, const char *restrict suffix, void *restrict hashsum)
{
const char *restrict msg = msg_;
- size_t hashsize = state->sponge.n >> 3;
- char *tmp = malloc(((state->sponge.n + 7) >> 3) * sizeof(char));
+ size_t hashsize = (size_t)(state->sponge.n >> 3);
+ char *tmp = malloc((size_t)((state->sponge.n + 7) >> 3) * sizeof(char));
char leftover[2];
size_t newlen;
@@ -379,8 +381,8 @@ libkeccak_hmac_digest(libkeccak_hmac_state_t *restrict state, const void *restri
goto fail;
leftover[0] = state->leftover;
if (bits) {
- leftover[0] |= msg[msglen] >> (state->key_length & 7);
- leftover[1] = ((unsigned char)(msg[msglen])) << (8 - (state->key_length & 7));
+ leftover[0] |= (char)(msg[msglen] >> (state->key_length & 7));
+ leftover[1] = (char)((unsigned char)msg[msglen] << (8 - (state->key_length & 7)));
}
newlen = (state->key_length & 7) + bits;
if (libkeccak_digest(&state->sponge, leftover, newlen >> 3, newlen & 7, suffix, tmp) < 0)
@@ -402,19 +404,19 @@ stage_2:
goto fail;
leftover[0] = state->leftover;
if (bits) {
- leftover[0] |= tmp[hashsize] >> (state->key_length & 7);
- leftover[1] = ((unsigned char)(tmp[hashsize])) << (8 - (state->key_length & 7));
+ leftover[0] |= (char)(tmp[hashsize] >> (state->key_length & 7));
+ leftover[1] = (char)((unsigned char)tmp[hashsize] << (8 - (state->key_length & 7)));
}
newlen = (state->key_length & 7) + bits;
if (libkeccak_digest(&state->sponge, leftover, newlen >> 3, newlen & 7, suffix, tmp) < 0)
goto fail;
stage_3:
- my_explicit_bzero(tmp, ((state->sponge.n + 7) >> 3) * sizeof(char));
+ my_explicit_bzero(tmp, (size_t)((state->sponge.n + 7) >> 3) * sizeof(char));
free(tmp);
return 0;
fail:
- my_explicit_bzero(tmp, ((state->sponge.n + 7) >> 3) * sizeof(char));
+ my_explicit_bzero(tmp, (size_t)((state->sponge.n + 7) >> 3) * sizeof(char));
free(tmp);
return -1;
}
diff --git a/test.c b/test.c
index 9831657..9b9cdc1 100644
--- a/test.c
+++ b/test.c
@@ -130,18 +130,26 @@ test_digest_case(const libkeccak_spec_t *restrict spec, const char *restrict suf
char *restrict hexsum;
int ok;
- if (libkeccak_state_initialise(&state, spec))
- return perror("libkeccak_state_initialise"), -1;
- if (hashsum = malloc((spec->output + 7) / 8), hashsum == NULL)
- return perror("malloc"), -1;
- if (hexsum = malloc((spec->output + 7) / 8 * 2 + 1), hexsum == NULL)
- return perror("malloc"), -1;
-
- if (libkeccak_digest(&state, msg, strlen(msg) - !!bits, bits, suffix, hashsum))
+ if (libkeccak_state_initialise(&state, spec)) {
+ perror("libkeccak_state_initialise");
+ return -1;
+ }
+ hashsum = malloc((size_t)((spec->output + 7) / 8));
+ if (!hashsum) {
+ perror("malloc");
+ return -1;
+ }
+ hexsum = malloc((size_t)((spec->output + 7) / 8 * 2 + 1));
+ if (!hexsum) {
+ perror("malloc");
+ return -1;
+ }
+
+ if (libkeccak_digest(&state, msg, strlen(msg) - !!bits, (size_t)bits, suffix, hashsum))
return perror("libkeccak_digest"), -1;
libkeccak_state_fast_destroy(&state);
- libkeccak_behex_lower(hexsum, hashsum, (spec->output + 7) / 8);
+ libkeccak_behex_lower(hexsum, hashsum, (size_t)((spec->output + 7) / 8));
ok = !strcmp(hexsum, expected_answer);
printf("%s%s\n", ok ? "OK" : "Fail: ", ok ? "" : hexsum);
if (!ok)
@@ -384,20 +392,32 @@ test_update_case(const libkeccak_spec_t *restrict spec, const char *restrict suf
char *restrict hexsum;
int ok;
- if (libkeccak_state_initialise(&state, spec))
- return perror("libkeccak_state_initialise"), -1;
- if (hashsum = malloc((spec->output + 7) / 8), hashsum == NULL)
- return perror("malloc"), -1;
- if (hexsum = malloc((spec->output + 7) / 8 * 2 + 1), hexsum == NULL)
- return perror("malloc"), -1;
-
- if (libkeccak_update(&state, msg, strlen(msg)))
- return perror("libkeccak_update"), -1;
- if (libkeccak_digest(&state, NULL, 0, 0, suffix, hashsum))
- return perror("libkeccak_digest"), -1;
+ if (libkeccak_state_initialise(&state, spec)) {
+ perror("libkeccak_state_initialise");
+ return -1;
+ }
+ hashsum = malloc((size_t)((spec->output + 7) / 8));
+ if (!hashsum) {
+ perror("malloc");
+ return -1;
+ }
+ hexsum = malloc((size_t)((spec->output + 7) / 8 * 2 + 1));
+ if (!hexsum) {
+ perror("malloc");
+ return -1;
+ }
+
+ if (libkeccak_update(&state, msg, strlen(msg))) {
+ perror("libkeccak_update");
+ return -1;
+ }
+ if (libkeccak_digest(&state, NULL, 0, 0, suffix, hashsum)) {
+ perror("libkeccak_digest");
+ return -1;
+ }
libkeccak_state_fast_destroy(&state);
- libkeccak_behex_lower(hexsum, hashsum, (spec->output + 7) / 8);
+ libkeccak_behex_lower(hexsum, hashsum, (size_t)((spec->output + 7) / 8));
ok = !strcmp(hexsum, expected_answer);
printf("%s%s\n", ok ? "OK" : "Fail: ", ok ? "" : hexsum);
if (!ok)
@@ -495,7 +515,7 @@ static int test_squeeze_case(libkeccak_state_t *restrict state, const libkeccak_
for (i = fast_squeezes; i < squeezes; i++)
libkeccak_squeeze(state, hashsum);
- libkeccak_behex_lower(hexsum, hashsum, (spec->output + 7) / 8);
+ libkeccak_behex_lower(hexsum, hashsum, (size_t)((spec->output + 7) / 8));
ok = !strcmp(hexsum, expected_answer);
printf("%s%s\n", ok ? "OK" : "Fail: ", ok ? "" : hexsum);
if (!ok)
@@ -528,12 +548,20 @@ test_squeeze(void)
char *restrict hexsum;
libkeccak_spec_sha3(&spec, 224);
- if (hashsum = malloc((spec.output + 7) / 8), hashsum == NULL)
- return perror("malloc"), -1;
- if (hexsum = malloc((spec.output + 7) / 8 * 2 + 1), hexsum == NULL)
- return perror("malloc"), -1;
- if (libkeccak_state_initialise(&state, &spec))
- return perror("libkeccak_state_initialise"), -1;
+ hashsum = malloc((size_t)((spec.output + 7) / 8));
+ if (!hashsum) {
+ perror("malloc");
+ return -1;
+ }
+ hexsum = malloc((size_t)((spec.output + 7) / 8 * 2 + 1));
+ if (!hexsum) {
+ perror("malloc");
+ return -1;
+ }
+ if (libkeccak_state_initialise(&state, &spec)) {
+ perror("libkeccak_state_initialise");
+ return -1;
+ }
printf("Testing squeeze functions with slow initial digest:\n");
printf(" 1 extra squeeze, including 0 fast squeezes: "), run_test(0, 1, 0);
@@ -595,18 +623,30 @@ test_file(const libkeccak_spec_t *restrict spec, const char *restrict suffix,
printf("Testing libkeccak_generalised_sum_fd on %s: ", filename);
- if (hashsum = malloc((spec->output + 7) / 8), hashsum == NULL)
- return perror("malloc"), -1;
- if (hexsum = malloc((spec->output + 7) / 8 * 2 + 1), hexsum == NULL)
- return perror("malloc"), -1;
-
- if (fd = open(filename, O_RDONLY), fd < 0)
- return perror("open"), -1;
-
- if (libkeccak_generalised_sum_fd(fd, &state, spec, suffix, hashsum))
- return perror("libkeccak_generalised_sum_fd"), close(fd), -1;
-
- libkeccak_behex_lower(hexsum, hashsum, (spec->output + 7) / 8);
+ hashsum = malloc((size_t)((spec->output + 7) / 8));
+ if (!hashsum) {
+ perror("malloc");
+ return -1;
+ }
+ hexsum = malloc((size_t)((spec->output + 7) / 8 * 2 + 1));
+ if (!hexsum) {
+ perror("malloc");
+ return -1;
+ }
+
+ fd = open(filename, O_RDONLY);
+ if (fd < 0) {
+ perror("open");
+ return -1;
+ }
+
+ if (libkeccak_generalised_sum_fd(fd, &state, spec, suffix, hashsum)) {
+ perror("libkeccak_generalised_sum_fd");
+ close(fd);
+ return -1;
+ }
+
+ libkeccak_behex_lower(hexsum, hashsum, (size_t)((spec->output + 7) / 8));
ok = !strcmp(hexsum, expected_answer);
printf("%s%s\n", ok ? "OK" : "Fail: ", ok ? "" : hexsum);
if (!ok)