totp

Simple cli tool for storing TOTP secrets and generating tokens
git clone https://git.inz.fi/totp/
Log | Files | Refs | Submodules

commit 6db6c6fec6fabedb82cc062791fa645208773aa7
parent e2d20608af48ffb09e571406c0fa1c03fd0bbb61
Author: Santtu Lakkala <santtu.lakkala@digital14.com>
Date:   Fri, 22 Sep 2023 13:31:01 +0300

Add unity build

Make internal functions unique to enable unity build, and add make
target to do that.

Diffstat:
MMakefile | 12++++++++++--
Msha1.c | 26+++++++++++++-------------
Msha256.c | 22+++++++++++-----------
Msha512.c | 22+++++++++++-----------
4 files changed, 45 insertions(+), 37 deletions(-)

diff --git a/Makefile b/Makefile @@ -23,6 +23,14 @@ debug: clean ${NAME}: ${OBJS} ${CC} ${CFLAGS} -o $@ ${OBJS} ${LDFLAGS} +unity.c: Makefile + for i in ${SOURCES}; do echo "#include \"$$i\""; done > unity.c + +unity: ${NAME}-unity + +${NAME}-unity: unity.c ${SOURCES} ${HEADERS} + ${CC} ${CFLAGS} -o ${NAME}-unity $< ${LDFLAGS} + algotest: ${TEST_OBJS}; ${CC} ${CFLAGS} -o $@ ${TEST_OBJS} ${LDFLAGS} @@ -34,7 +42,7 @@ test: algotest ${NAME} ${CC} -c $< -o $@ ${CFLAGS} ${AES_CFLAGS} clean: - rm -f ${OBJS} ${TEST_OBJS} + rm -f ${OBJS} ${TEST_OBJS} unity.c install: all mkdir -p "${DESTDIR}${BINDIR}" @@ -80,4 +88,4 @@ util.o: util.h ${OBJS} ${TEST_OBJS}: Makefile -.PHONY: test depend all debug +.PHONY: test depend all debug unity diff --git a/sha1.c b/sha1.c @@ -8,12 +8,12 @@ #include "sha1.h" #include "util.h" -static inline uint32_t rotl32(uint32_t x, uint8_t n) +static inline uint32_t _sha1_rotl32(uint32_t x, uint8_t n) { return x << n | x >> (32 - n); } -static inline void add5(uint32_t *dest, const uint32_t *src) +static inline void _sha1_add5(uint32_t *dest, const uint32_t *src) { size_t i; @@ -21,17 +21,17 @@ static inline void add5(uint32_t *dest, const uint32_t *src) dest[i] += src[i]; } -static inline void rotmod5(uint32_t *a, uint32_t f, uint32_t k, uint32_t w) +static inline void _sha1_rotmod5(uint32_t *a, uint32_t f, uint32_t k, uint32_t w) { - uint32_t t = rotl32(a[0], 5) + f + a[4] + k + w; + uint32_t t = _sha1_rotl32(a[0], 5) + f + a[4] + k + w; memmove(a + 1, a, 4 * sizeof(*a)); - a[2] = rotl32(a[2], 30); + a[2] = _sha1_rotl32(a[2], 30); a[0] = t; } -static inline uint32_t getnw(uint32_t *w, size_t i) +static inline uint32_t _sha1_getnw(uint32_t *w, size_t i) { - return w[i & 15] = rotl32(w[(i + 13) & 15] ^ w[(i + 8) & 15] ^ w[(i + 2) & 15] ^ w[i & 15], 1); + return w[i & 15] = _sha1_rotl32(w[(i + 13) & 15] ^ w[(i + 8) & 15] ^ w[(i + 2) & 15] ^ w[i & 15], 1); } void sha1_init(struct sha1 *s) @@ -52,17 +52,17 @@ static inline void _sha1_update(uint32_t *h, const void *data) memcpy(wr, h, sizeof(wr)); for (i = 0; i < 16; i++) - rotmod5(wr, (wr[1] & wr[2]) | (~wr[1] & wr[3]), k[0], w[i] = ntohl(d[i])); + _sha1_rotmod5(wr, (wr[1] & wr[2]) | (~wr[1] & wr[3]), k[0], w[i] = ntohl(d[i])); for (; i < 20; i++) - rotmod5(wr, (wr[1] & wr[2]) | (~wr[1] & wr[3]), k[0], getnw(w, i)); + _sha1_rotmod5(wr, (wr[1] & wr[2]) | (~wr[1] & wr[3]), k[0], _sha1_getnw(w, i)); for (; i < 40; i++) - rotmod5(wr, wr[1] ^ wr[2] ^ wr[3], k[1], getnw(w, i)); + _sha1_rotmod5(wr, wr[1] ^ wr[2] ^ wr[3], k[1], _sha1_getnw(w, i)); for (; i < 60; i++) - rotmod5(wr, (wr[1] & wr[2]) | (wr[1] & wr[3]) | (wr[2] & wr[3]), k[2], getnw(w, i)); + _sha1_rotmod5(wr, (wr[1] & wr[2]) | (wr[1] & wr[3]) | (wr[2] & wr[3]), k[2], _sha1_getnw(w, i)); for (; i < 80; i++) - rotmod5(wr, wr[1] ^ wr[2] ^ wr[3], k[3], getnw(w, i)); + _sha1_rotmod5(wr, wr[1] ^ wr[2] ^ wr[3], k[3], _sha1_getnw(w, i)); - add5(h, wr); + _sha1_add5(h, wr); } void sha1_update(struct sha1 *s, const void *data, size_t len) diff --git a/sha256.c b/sha256.c @@ -8,12 +8,12 @@ #include "sha256.h" #include "util.h" -static inline uint32_t rotr32(uint32_t x, uint8_t n) +static inline uint32_t _sha256_rotr32(uint32_t x, uint8_t n) { return x >> n | x << (32 - n); } -static inline void add8(uint32_t *dest, const uint32_t *src) +static inline void _sha256_add8(uint32_t *dest, const uint32_t *src) { size_t i; @@ -21,10 +21,10 @@ static inline void add8(uint32_t *dest, const uint32_t *src) dest[i] += src[i]; } -static inline void rotmod8(uint32_t *a, uint32_t k, uint32_t w) +static inline void _sha256_rotmod8(uint32_t *a, uint32_t k, uint32_t w) { - uint32_t t1 = a[7] + (rotr32(a[4], 6) ^ rotr32(a[4], 11) ^ rotr32(a[4], 25)) + ((a[4] & a[5]) ^ (~a[4] & a[6])) + k + w; - uint32_t t2 = (rotr32(a[0], 2) ^ rotr32(a[0], 13) ^ rotr32(a[0], 22)) + ((a[0] & a[1]) ^ (a[0] & a[2]) ^ (a[1] & a[2])); + uint32_t t1 = a[7] + (_sha256_rotr32(a[4], 6) ^ _sha256_rotr32(a[4], 11) ^ _sha256_rotr32(a[4], 25)) + ((a[4] & a[5]) ^ (~a[4] & a[6])) + k + w; + uint32_t t2 = (_sha256_rotr32(a[0], 2) ^ _sha256_rotr32(a[0], 13) ^ _sha256_rotr32(a[0], 22)) + ((a[0] & a[1]) ^ (a[0] & a[2]) ^ (a[1] & a[2])); memmove(a + 1, a, 7 * sizeof(*a)); @@ -32,12 +32,12 @@ static inline void rotmod8(uint32_t *a, uint32_t k, uint32_t w) a[0] = t1 + t2; } -static inline uint32_t getnw(uint32_t *w, size_t i) +static inline uint32_t _sha256_getnw(uint32_t *w, size_t i) { return w[i & 15] += - (rotr32(w[(i + 1) & 15], 7) ^ rotr32(w[(i + 1) & 15], 18) ^ (w[(i + 1) & 15] >> 3)) + + (_sha256_rotr32(w[(i + 1) & 15], 7) ^ _sha256_rotr32(w[(i + 1) & 15], 18) ^ (w[(i + 1) & 15] >> 3)) + w[(i + 9) & 15] + - (rotr32(w[(i + 14) & 15], 17) ^ rotr32(w[(i + 14) & 15], 19) ^ (w[(i + 14) & 15] >> 10)); + (_sha256_rotr32(w[(i + 14) & 15], 17) ^ _sha256_rotr32(w[(i + 14) & 15], 19) ^ (w[(i + 14) & 15] >> 10)); } void sha256_init(struct sha256 *s) @@ -67,12 +67,12 @@ static inline void _sha256_update(uint32_t *h, const void *data) memcpy(wr, h, sizeof(wr)); for (i = 0; i < 16; i++) - rotmod8(wr, k[i], w[i] = ntohl(d[i])); + _sha256_rotmod8(wr, k[i], w[i] = ntohl(d[i])); for (; i < 64; i++) - rotmod8(wr, k[i], getnw(w, i)); + _sha256_rotmod8(wr, k[i], _sha256_getnw(w, i)); - add8(h, wr); + _sha256_add8(h, wr); } void sha256_update(struct sha256 *s, const void *data, size_t len) diff --git a/sha512.c b/sha512.c @@ -8,12 +8,12 @@ #include "sha512.h" #include "util.h" -static inline uint64_t rotr64(uint64_t x, uint8_t n) +static inline uint64_t _sha512_rotr64(uint64_t x, uint8_t n) { return x >> n | x << (64 - n); } -static inline void add8(uint64_t *dest, const uint64_t *src) +static inline void _sha512_add8(uint64_t *dest, const uint64_t *src) { size_t i; @@ -21,10 +21,10 @@ static inline void add8(uint64_t *dest, const uint64_t *src) dest[i] += src[i]; } -static inline void rotmod8(uint64_t *a, uint64_t k, uint64_t w) +static inline void _sha512_rotmod8(uint64_t *a, uint64_t k, uint64_t w) { - uint64_t t1 = a[7] + (rotr64(a[4], 14) ^ rotr64(a[4], 18) ^ rotr64(a[4], 41)) + ((a[4] & a[5]) ^ (~a[4] & a[6])) + k + w; - uint64_t t2 = (rotr64(a[0], 28) ^ rotr64(a[0], 34) ^ rotr64(a[0], 39)) + ((a[0] & a[1]) ^ (a[0] & a[2]) ^ (a[1] & a[2])); + uint64_t t1 = a[7] + (_sha512_rotr64(a[4], 14) ^ _sha512_rotr64(a[4], 18) ^ _sha512_rotr64(a[4], 41)) + ((a[4] & a[5]) ^ (~a[4] & a[6])) + k + w; + uint64_t t2 = (_sha512_rotr64(a[0], 28) ^ _sha512_rotr64(a[0], 34) ^ _sha512_rotr64(a[0], 39)) + ((a[0] & a[1]) ^ (a[0] & a[2]) ^ (a[1] & a[2])); memmove(a + 1, a, 7 * sizeof(*a)); @@ -32,12 +32,12 @@ static inline void rotmod8(uint64_t *a, uint64_t k, uint64_t w) a[0] = t1 + t2; } -static inline uint64_t getnw(uint64_t *w, size_t i) +static inline uint64_t _sha512_getnw(uint64_t *w, size_t i) { return w[i & 15] += - (rotr64(w[(i + 1) & 15], 1) ^ rotr64(w[(i + 1) & 15], 8) ^ (w[(i + 1) & 15] >> 7)) + + (_sha512_rotr64(w[(i + 1) & 15], 1) ^ _sha512_rotr64(w[(i + 1) & 15], 8) ^ (w[(i + 1) & 15] >> 7)) + w[(i + 9) & 15] + - (rotr64(w[(i + 14) & 15], 19) ^ rotr64(w[(i + 14) & 15], 61) ^ (w[(i + 14) & 15] >> 6)); + (_sha512_rotr64(w[(i + 14) & 15], 19) ^ _sha512_rotr64(w[(i + 14) & 15], 61) ^ (w[(i + 14) & 15] >> 6)); } void sha512_init(struct sha512 *s) @@ -79,13 +79,13 @@ static inline void _sha512_update(uint64_t *h, const void *data) for (i = 0; i < sizeof(w) / sizeof(*w); i++) { w[i] = readbeu64(&d[i * sizeof(uint64_t)]); - rotmod8(wr, k[i], w[i]); + _sha512_rotmod8(wr, k[i], w[i]); } for (; i < sizeof(k) / sizeof(*k); i++) - rotmod8(wr, k[i], getnw(w, i)); + _sha512_rotmod8(wr, k[i], _sha512_getnw(w, i)); - add8(h, wr); + _sha512_add8(h, wr); } void sha512_update(struct sha512 *s, const void *data, size_t len)