diff options
| author | 2026-01-16 18:57:56 -0600 | |
|---|---|---|
| committer | 2026-01-16 18:57:56 -0600 | |
| commit | a887fd3a4ef9bcf673b286398802aa88ecb1592a (patch) | |
| tree | 568019b692e5b2b38a149bbf94c55dbe78237c7a /lib/sha1.c | |
| parent | move include directory (diff) | |
endian functions
Diffstat (limited to 'lib/sha1.c')
| -rw-r--r-- | lib/sha1.c | 42 |
1 files changed, 19 insertions, 23 deletions
@@ -1,8 +1,7 @@ #include "sha1.h" -#include <string.h> +#include "platform.h" -/* for byteswap functions (TODO: avoid GNU extensions) */ -#include <endian.h> +#include <string.h> /* code adapted from https://git.figboot.dev/l2su/tree/src/digest/sha1.c */ @@ -43,7 +42,7 @@ static void sha1_upd(vl_sha1_st *st) memcpy(w, st->vl_chunk, VL_SHA1_CHUNKLEN_BYTES); for (i = 0; i < 16; ++i) { - w[i] = htobe32(w[i]); + w[i] = vl_htobe32(w[i]); } for (i = 16; i < 80; ++i) { @@ -56,19 +55,16 @@ static void sha1_upd(vl_sha1_st *st) d = st->vl_state[3]; e = st->vl_state[4]; -#define UP \ - temp = rol(a, 5) + f + e + k + w[i]; \ - e = d; \ - d = c; \ - c = rol(b, 30); \ - b = a; \ - a = temp; - -#define OP(_start, _end, _k, _e) \ - for (i = _start; i < _end; ++i) { \ - f = _e; \ - k = _k; \ - UP \ +#define OP(_start, _end, _k, _e) \ + for (i = _start; i < _end; ++i) { \ + f = _e; \ + k = _k; \ + temp = rol(a, 5) + f + e + k + w[i]; \ + e = d; \ + d = c; \ + c = rol(b, 30); \ + b = a; \ + a = temp; \ } OP( 0, 20, SHA1_K0, (b & c) ^ ((~b) & d)) @@ -120,7 +116,7 @@ void vl_sha1_finalize(vl_sha1_st *st, vl_sha1 out) } size_t rem = (VL_SHA1_CHUNKLEN_BYTES - st->vl_nchunk) - 8; - uint64_t lenbe = htobe64(st->vl_total * 8); + uint64_t lenbe = vl_htobe64(st->vl_total * 8); memset(st->vl_chunk + st->vl_nchunk, 0, rem); st->vl_nchunk += rem; @@ -128,11 +124,11 @@ void vl_sha1_finalize(vl_sha1_st *st, vl_sha1 out) sha1_upd(st); uint32_t pout_i[VL_SHA1_STATELEN_32]; - pout_i[0] = htobe32(st->vl_state[0]); - pout_i[1] = htobe32(st->vl_state[1]); - pout_i[2] = htobe32(st->vl_state[2]); - pout_i[3] = htobe32(st->vl_state[3]); - pout_i[4] = htobe32(st->vl_state[4]); + pout_i[0] = vl_htobe32(st->vl_state[0]); + pout_i[1] = vl_htobe32(st->vl_state[1]); + pout_i[2] = vl_htobe32(st->vl_state[2]); + pout_i[3] = vl_htobe32(st->vl_state[3]); + pout_i[4] = vl_htobe32(st->vl_state[4]); memcpy(out, pout_i, sizeof(vl_sha1)); } |
