diff options
-rw-r--r-- | algorithm_output_size.c | 3 | ||||
-rw-r--r-- | digest.c | 25 | ||||
-rw-r--r-- | init.c | 9 | ||||
-rw-r--r-- | marshal.c | 9 | ||||
-rw-r--r-- | unmarshal.c | 8 | ||||
-rw-r--r-- | update.c | 10 |
6 files changed, 22 insertions, 42 deletions
diff --git a/algorithm_output_size.c b/algorithm_output_size.c index 9089aa4..2824b55 100644 --- a/algorithm_output_size.c +++ b/algorithm_output_size.c @@ -19,6 +19,7 @@ libsha2_algorithm_output_size(enum libsha2_algorithm algorithm) case LIBSHA2_512_224: return 28; case LIBSHA2_512_256: return 32; default: - return errno = EINVAL, 0; + errno = EINVAL; + return 0; } } @@ -16,10 +16,10 @@ libsha2_digest(struct libsha2_state *restrict state, const char *restrict messag char *appendix; size_t i, j, k, n; - if (msglen & ~7) { - libsha2_update(state, message, msglen & ~7); - message += msglen & ~7; - msglen &= 7; + if (msglen & ~(size_t)7) { + libsha2_update(state, message, msglen & ~(size_t)7); + message += msglen & ~(size_t)7; + msglen &= (size_t)7; } k = 8 * state->chunk_size; @@ -31,36 +31,31 @@ libsha2_digest(struct libsha2_state *restrict state, const char *restrict messag if (msglen) { j = 7 - msglen; *appendix = *message; - *appendix |= 1 << j; - *appendix &= ~((1 << j) - 1); + *appendix |= (char)(1 << j); + *appendix &= (char)~((1 << j) - 1); } else { - *appendix = (unsigned char)128; + *appendix = (char)128; } k = state->message_size + msglen; i = state->chunk_size / 8; appendix += n + i - 1; for (i = i < sizeof(size_t) ? i : sizeof(size_t); i--;) - *(appendix - i) = (unsigned char)((k >> (i * 8)) & 255); + *(appendix - i) = (char)((k >> (i * 8)) & 255); n += state->chunk_size; libsha2_update(state, state->appendix, n); n = libsha2_algorithm_output_size(state->algorithm); - switch (state->algorithm) { - case LIBSHA2_224: - case LIBSHA2_256: + if (state->algorithm <= LIBSHA2_256) { for (i = 0; i < 8; i++) for (j = 0; j < (state->chunk_size / 16); j++) if (k = (i + 1) * (state->chunk_size / 16) - j - 1, k < n) output[k] = (char)((state->h.b32[i] >> (8 * j)) & 255); - break; - - default: + } else { for (i = 0; i < 8; i++) for (j = 0; j < (state->chunk_size / 16); j++) if (k = (i + 1) * (state->chunk_size / 16) - j - 1, k < n) output[k] = (char)((state->h.b64[i] >> (8 * j)) & 255); - break; } } @@ -107,18 +107,13 @@ libsha2_init(struct libsha2_state *restrict state, enum libsha2_algorithm algori } /* Set round constants, and chunk size. */ - switch (algorithm) { - case LIBSHA2_224: - case LIBSHA2_256: + if (algorithm <= LIBSHA2_256) { for (i = 0; i < 64; i++) state->k.b32[i] = (uint32_t)(ROUND_CONSTANTS[i] >> 32); state->chunk_size = 64; - break; - - default: + } else { memcpy(state->k.b64, ROUND_CONSTANTS, sizeof(ROUND_CONSTANTS)); state->chunk_size = 128; - break; } return 0; @@ -24,9 +24,7 @@ libsha2_marshal(const struct libsha2_state *restrict state, char *restrict buf) *(size_t *)&buf[off] = state->message_size; off += sizeof(size_t); - switch (state->algorithm) { - case LIBSHA2_224: - case LIBSHA2_256: + if (state->algorithm <= LIBSHA2_256) { if (buf) memcpy(&buf[off], state->k.b32, sizeof(state->k.b32)); off += sizeof(state->k.b32); @@ -36,9 +34,7 @@ libsha2_marshal(const struct libsha2_state *restrict state, char *restrict buf) if (buf) memcpy(&buf[off], state->h.b32, sizeof(state->h.b32)); off += sizeof(state->h.b32); - break; - - default: + } else { if (buf) memcpy(&buf[off], state->k.b64, sizeof(state->k.b64)); off += sizeof(state->k.b64); @@ -48,7 +44,6 @@ libsha2_marshal(const struct libsha2_state *restrict state, char *restrict buf) if (buf) memcpy(&buf[off], state->h.b64, sizeof(state->h.b64)); off += sizeof(state->h.b64); - break; } if (buf) diff --git a/unmarshal.c b/unmarshal.c index 2a47b92..13a97ff 100644 --- a/unmarshal.c +++ b/unmarshal.c @@ -20,15 +20,15 @@ libsha2_unmarshal(struct libsha2_state *restrict state, const char *restrict buf return 0; } - if (*(int *)buf) { /* version */ + if (*(const int *)buf) { /* version */ errno = EINVAL; return 0; } off += sizeof(int); - state->algorithm = *(enum libsha2_algorithm *)&buf[off]; + state->algorithm = *(const enum libsha2_algorithm *)&buf[off]; off += sizeof(enum libsha2_algorithm); - state->message_size = *(size_t *)&buf[off]; + state->message_size = *(const size_t *)&buf[off]; off += sizeof(size_t); switch (state->algorithm) { @@ -73,7 +73,7 @@ libsha2_unmarshal(struct libsha2_state *restrict state, const char *restrict buf } memcpy(state->chunk, &buf[off], sizeof(state->chunk)); off += sizeof(state->chunk); - state->chunk_size = *(size_t *)&buf[off]; + state->chunk_size = *(const size_t *)&buf[off]; off += sizeof(size_t); return off; @@ -122,16 +122,10 @@ libsha2_update(struct libsha2_state *restrict state, const char *restrict messag n = n < msglen ? n : msglen; memcpy(state->chunk + off, message, n); if (off + n == state->chunk_size) { - switch (state->algorithm) { - case LIBSHA2_224: - case LIBSHA2_256: + if (state->algorithm <= LIBSHA2_256) process256(state); - break; - - default: + else process512(state); - break; - } } message += n, mlen += n, msglen -= n; } |