summaryrefslogtreecommitdiffstats
path: root/lib/sha1.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/sha1.c')
-rw-r--r--lib/sha1.c42
1 files changed, 19 insertions, 23 deletions
diff --git a/lib/sha1.c b/lib/sha1.c
index ed456c8..009264d 100644
--- a/lib/sha1.c
+++ b/lib/sha1.c
@@ -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));
}