aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <m@maandree.se>2026-05-17 21:02:39 +0200
committerMattias Andrée <m@maandree.se>2026-05-17 21:02:39 +0200
commit5fb2a5fe67521a452463e6b91cee406ec14c35a0 (patch)
treef6be3a0c9086ef7122a7d5789818bdc7c0bb1081
parentUpdate year (diff)
downloadlibsha2-5fb2a5fe67521a452463e6b91cee406ec14c35a0.tar.gz
libsha2-5fb2a5fe67521a452463e6b91cee406ec14c35a0.tar.bz2
libsha2-5fb2a5fe67521a452463e6b91cee406ec14c35a0.tar.xz
Fix mistakesHEAD1.1.5master
Signed-off-by: Mattias Andrée <m@maandree.se>
-rw-r--r--Makefile2
-rw-r--r--config-armv8.mk10
-rw-r--r--config-portable.mk10
-rw-r--r--config-x86.mk10
-rw-r--r--hmac_marshal.c8
-rw-r--r--hmac_unmarshal.c10
-rw-r--r--marshal.c10
-rw-r--r--process.c4
-rw-r--r--sum_fd.c7
-rw-r--r--unmarshal.c12
-rw-r--r--update.c4
11 files changed, 54 insertions, 33 deletions
diff --git a/Makefile b/Makefile
index ed4dfd2..9931615 100644
--- a/Makefile
+++ b/Makefile
@@ -81,7 +81,7 @@ SRC = $(OBJ:.o=.c)
all: libsha2.a libsha2.$(LIBEXT) test
-$(OBJ): $(HDR)
+$(OBJ) test.o: $(HDR)
$(LOBJ): $(HDR)
.c.o:
diff --git a/config-armv8.mk b/config-armv8.mk
index ddda52a..47efdc2 100644
--- a/config-armv8.mk
+++ b/config-armv8.mk
@@ -3,12 +3,18 @@ MANPREFIX = $(PREFIX)/share/man
CC = cc -std=c11
+COMMON_SANITIZE = -fsanitize=alignment,shift,signed-integer-overflow,object-size,null,undefined,bounds,address
+CLANG_SANITIZE = -O1 $(COMMON_SANITIZE),cfi -flto -fvisibility=hidden -fno-sanitize-trap=cfi
+GCC_SANITIZE = -O1 $(COMMON_SANITIZE)
+#SANITIZE = $(CLANG_SANITIZE)
+#SANITIZE = $(GCC_SANITIZE)
+
CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700
-CFLAGS = -Wall -O3 -march=native
+CFLAGS = $(SANITIZE) -Wall -O3 -march=native
# If you cannot use -march=native, you should do e.g -march=armv8-a+crypto
# however, you have to be careful selecting the exact version,
# so you may have to replace armv8-a with something else.
-LDFLAGS = -s
+LDFLAGS = $(SANITIZE) -s
# You can add -DALLOCA_LIMIT=# to CPPFLAGS, where # is a size_t
# value, to put a limit on how large allocation the library is
diff --git a/config-portable.mk b/config-portable.mk
index 4a92176..a905df1 100644
--- a/config-portable.mk
+++ b/config-portable.mk
@@ -3,9 +3,15 @@ MANPREFIX = $(PREFIX)/share/man
CC = cc -std=c11
+COMMON_SANITIZE = -fsanitize=alignment,shift,signed-integer-overflow,object-size,null,undefined,bounds,address
+CLANG_SANITIZE = -O1 $(COMMON_SANITIZE),cfi -flto -fvisibility=hidden -fno-sanitize-trap=cfi
+GCC_SANITIZE = -O1 $(COMMON_SANITIZE)
+#SANITIZE = $(CLANG_SANITIZE)
+#SANITIZE = $(GCC_SANITIZE)
+
CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700
-CFLAGS = -Wall -O3
-LDFLAGS = -s
+CFLAGS = $(SANITIZE) -Wall -O3
+LDFLAGS = $(SANITIZE) -s
# You can add -DALLOCA_LIMIT=# to CPPFLAGS, where # is a size_t
# value, to put a limit on how large allocation the library is
diff --git a/config-x86.mk b/config-x86.mk
index a79e63f..bd96527 100644
--- a/config-x86.mk
+++ b/config-x86.mk
@@ -3,9 +3,15 @@ MANPREFIX = $(PREFIX)/share/man
CC = cc -std=c11
+COMMON_SANITIZE = -fsanitize=alignment,shift,signed-integer-overflow,object-size,null,undefined,bounds,address
+CLANG_SANITIZE = -O1 $(COMMON_SANITIZE),cfi -flto -fvisibility=hidden -fno-sanitize-trap=cfi
+GCC_SANITIZE = -O1 $(COMMON_SANITIZE)
+#SANITIZE = $(CLANG_SANITIZE)
+#SANITIZE = $(GCC_SANITIZE)
+
CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700
-CFLAGS = -Wall -O3 -msse4 -msha
-LDFLAGS = -s
+CFLAGS = $(SANITIZE) -Wall -O3 -msse4 -msha
+LDFLAGS = $(SANITIZE) -s
# You can add -DALLOCA_LIMIT=# to CPPFLAGS, where # is a size_t
# value, to put a limit on how large allocation the library is
diff --git a/hmac_marshal.c b/hmac_marshal.c
index 6db8972..e234f76 100644
--- a/hmac_marshal.c
+++ b/hmac_marshal.c
@@ -5,21 +5,21 @@
size_t
libsha2_hmac_marshal(const struct libsha2_hmac_state *restrict state, void *restrict buf_)
{
- char *restrict buf = buf_;
+ unsigned char *restrict buf = buf_;
size_t off = 0;
if (buf)
- *(int *)buf = 0; /* version */
+ memcpy(buf, &(int){0}, sizeof(int)); /* version */
off += sizeof(int);
off += libsha2_marshal(&state->sha2_state, buf ? &buf[off] : NULL);
if (buf)
- *(size_t *)&buf[off] = state->outsize;
+ memcpy(&buf[off], &state->outsize, sizeof(size_t));
off += sizeof(size_t);
if (buf)
- *(unsigned char *)&buf[off] = state->inited;
+ buf[off] = state->inited;
off += sizeof(unsigned char);
if (buf)
diff --git a/hmac_unmarshal.c b/hmac_unmarshal.c
index 197ea32..5ce2a5e 100644
--- a/hmac_unmarshal.c
+++ b/hmac_unmarshal.c
@@ -5,16 +5,18 @@
size_t
libsha2_hmac_unmarshal(struct libsha2_hmac_state *restrict state, const void *restrict buf_, size_t bufsize)
{
- const char *restrict buf = buf_;
+ const unsigned char *restrict buf = buf_;
size_t off = 0;
size_t r;
+ int version;
if (bufsize < sizeof(int)) {
errno = EINVAL;
return 0;
}
- if (*(const int *)buf) { /* version */
+ memcpy(&version, buf, sizeof(int));
+ if (version != 0) {
errno = EINVAL;
return 0;
}
@@ -30,10 +32,10 @@ libsha2_hmac_unmarshal(struct libsha2_hmac_state *restrict state, const void *re
return 0;
}
- state->outsize = *(const size_t *)&buf[off];
+ memcpy(&state->outsize, &buf[off], sizeof(size_t));
off += sizeof(size_t);
- state->inited = *(const unsigned char *)&buf[off];
+ state->inited = buf[off];
off += sizeof(unsigned char);
memcpy(state->ipad, &buf[off], state->sha2_state.chunk_size);
diff --git a/marshal.c b/marshal.c
index 7596c03..d5ceb28 100644
--- a/marshal.c
+++ b/marshal.c
@@ -5,17 +5,17 @@
size_t
libsha2_marshal(const struct libsha2_state *restrict state, void *restrict buf_)
{
- char *restrict buf = buf_;
+ unsigned char *restrict buf = buf_;
size_t off = 0;
if (buf)
- *(int *)buf = 1; /* version */
+ memcpy(buf, &(int){1}, sizeof(int));
off += sizeof(int);
if (buf)
- *(enum libsha2_algorithm *)&buf[off] = state->algorithm;
+ memcpy(&buf[off], &state->algorithm, sizeof(enum libsha2_algorithm));
off += sizeof(enum libsha2_algorithm);
if (buf)
- *(size_t *)&buf[off] = state->message_size;
+ memcpy(&buf[off], &state->message_size, sizeof(size_t));
off += sizeof(size_t);
if (state->algorithm <= LIBSHA2_256) {
@@ -35,7 +35,7 @@ libsha2_marshal(const struct libsha2_state *restrict state, void *restrict buf_)
}
if (buf)
- *(size_t *)&buf[off] = state->chunk_size;
+ memcpy(&buf[off], &state->chunk_size, sizeof(size_t));
off += sizeof(size_t);
if (buf)
memcpy(&buf[off], state->chunk, (state->message_size / 8) % state->chunk_size);
diff --git a/process.c b/process.c
index 90d4f1f..020c33b 100644
--- a/process.c
+++ b/process.c
@@ -98,8 +98,8 @@ process_x86_sha256(struct libsha2_state *restrict state, const unsigned char *re
const unsigned char *restrict chunk;
size_t off = 0;
- temp = _mm_shuffle_epi32(_mm_loadu_si128((const __m128i *)&state->h.b32[0]), 0xB1);
- s1 = _mm_shuffle_epi32(_mm_loadu_si128((const __m128i *)&state->h.b32[4]), 0x1B);
+ temp = _mm_shuffle_epi32(_mm_loadu_si128((const __m128i *)(const unsigned char *)&state->h.b32[0]), 0xB1);
+ s1 = _mm_shuffle_epi32(_mm_loadu_si128((const __m128i *)(const unsigned char *)&state->h.b32[4]), 0x1B);
s0 = _mm_alignr_epi8(temp, s1, 8);
s1 = _mm_blend_epi16(s1, temp, 0xF0);
diff --git a/sum_fd.c b/sum_fd.c
index 1992cfd..23e4ca5 100644
--- a/sum_fd.c
+++ b/sum_fd.c
@@ -18,15 +18,15 @@ libsha2_sum_fd(int fd, enum libsha2_algorithm algorithm, void *restrict hashsum)
#ifndef _WIN32
if (fstat(fd, &attr) == 0 && attr.st_blksize > 0)
- blksize = (size_t)(attr.st_blksize);
+ blksize = (size_t)attr.st_blksize;
#endif
#if ALLOCA_LIMIT > 0
if (blksize > (size_t)ALLOCA_LIMIT) {
blksize = (size_t)ALLOCA_LIMIT;
- blksize -= blksize % sizeof(((struct libsha2_state)NULL)->chunk);
+ blksize -= blksize % sizeof(((struct libsha2_state *)NULL)->chunk);
if (!blksize)
- blksize = sizeof(((struct libsha2_state)NULL)->chunk);
+ blksize = sizeof(((struct libsha2_state *)NULL)->chunk);
}
# if defined(__clang__)
/* We are using a limit so it's just like declaring an array
@@ -60,6 +60,7 @@ libsha2_sum_fd(int fd, enum libsha2_algorithm algorithm, void *restrict hashsum)
}
libsha2_digest(&state, NULL, 0, hashsum);
+
#if ALLOCA_LIMIT <= 0
free(chunk);
#endif
diff --git a/unmarshal.c b/unmarshal.c
index 8c5780d..0bfecfd 100644
--- a/unmarshal.c
+++ b/unmarshal.c
@@ -5,7 +5,7 @@
size_t
libsha2_unmarshal(struct libsha2_state *restrict state, const void *restrict buf_, size_t bufsize)
{
- const char *restrict buf = buf_;
+ const unsigned char *restrict buf = buf_;
size_t off = 0;
int version;
@@ -14,16 +14,16 @@ libsha2_unmarshal(struct libsha2_state *restrict state, const void *restrict buf
return 0;
}
- version = *(const int *)buf;
- if (version < 0 || version > 1) { /* version */
+ memcpy(&version, buf, sizeof(int));
+ if (version < 0 || version > 1) {
errno = EINVAL;
return 0;
}
off += sizeof(int);
- state->algorithm = *(const enum libsha2_algorithm *)&buf[off];
+ memcpy(&state->algorithm, &buf[off], sizeof(enum libsha2_algorithm));
off += sizeof(enum libsha2_algorithm);
- state->message_size = *(const size_t *)&buf[off];
+ memcpy(&state->message_size, &buf[off], sizeof(size_t));
off += sizeof(size_t);
switch (state->algorithm) {
@@ -68,7 +68,7 @@ libsha2_unmarshal(struct libsha2_state *restrict state, const void *restrict buf
errno = EINVAL;
return 0;
}
- state->chunk_size = *(const size_t *)&buf[off];
+ memcpy(&state->chunk_size, &buf[off], sizeof(size_t));
off += sizeof(size_t);
if (bufsize - off < (state->message_size / 8) % state->chunk_size) {
diff --git a/update.c b/update.c
index 3c46f28..5d2a8e6 100644
--- a/update.c
+++ b/update.c
@@ -5,7 +5,7 @@
void
libsha2_update(struct libsha2_state *restrict state, const void *restrict message_, size_t msglen)
{
- const char *restrict message = message_;
+ const unsigned char *restrict message = message_;
size_t n, off;
off = (state->message_size / 8) % state->chunk_size;
@@ -21,7 +21,7 @@ libsha2_update(struct libsha2_state *restrict state, const void *restrict messag
msglen -= n;
}
- off = libsha2_process(state, (const unsigned char *)message, msglen);
+ off = libsha2_process(state, message, msglen);
if (msglen > off)
memcpy(state->chunk, &message[off], msglen - off);