From 0365dd5da58a1b86a323d337af23765971bed34f Mon Sep 17 00:00:00 2001 From: d0p1 Date: Tue, 26 Mar 2024 07:48:32 +0100 Subject: [PATCH] build: define INCDIR,BINDIR,LIBDIR... --- Makefile | 5 ++ include/Makefile | 2 - lib/Makefile | 4 +- lib/crypto/Makefile | 21 ++++- lib/crypto/chacha20/chacha20.asm | 66 ++++++++++++++ lib/crypto/chacha20/chacha20.h | 20 +++++ lib/crypto/chacha20/chacha20.inc | 12 +++ lib/crypto/crypto.h | 4 + lib/crypto/sha2/sha2.h | 15 ++++ lib/crypto/sha2/sha2.inc | 9 ++ lib/crypto/{hash => sha2}/sha256.asm | 0 lib/crypto/sha2/sha512.asm | 2 + lib/csu/Makefile | 4 +- lib/lzp/Makefile | 21 +++++ lib/lzp/lzp.asm | 127 +++++++++++++++++++++++++++ lib/lzp/lzp.h | 7 ++ lib/lzp/lzp.inc | 2 + 17 files changed, 312 insertions(+), 9 deletions(-) create mode 100644 lib/crypto/chacha20/chacha20.asm create mode 100644 lib/crypto/chacha20/chacha20.h create mode 100644 lib/crypto/chacha20/chacha20.inc create mode 100644 lib/crypto/crypto.h create mode 100644 lib/crypto/sha2/sha2.h create mode 100644 lib/crypto/sha2/sha2.inc rename lib/crypto/{hash => sha2}/sha256.asm (100%) create mode 100644 lib/crypto/sha2/sha512.asm create mode 100644 lib/lzp/Makefile create mode 100644 lib/lzp/lzp.asm create mode 100644 lib/lzp/lzp.h create mode 100644 lib/lzp/lzp.inc diff --git a/Makefile b/Makefile index 69488f3..798cc99 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,11 @@ TOPDIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST)))) SYSROOTDIR := $(TOPDIR)/sysroot TOOLSDIR := $(TOPDIR)/tools +BINDIR = /bin +LIBDIR = /usr/lib +INCDIR = /usr/include +ASMDIR = /usr/asm + AS = fasm RM = rm -f diff --git a/include/Makefile b/include/Makefile index 6d73e21..e98699a 100644 --- a/include/Makefile +++ b/include/Makefile @@ -1,5 +1,3 @@ -INCDIR = $(DESTDIR)/usr/include - INCS = coff.h .PHONY: all diff --git a/lib/Makefile b/lib/Makefile index be2e97e..fdfc8f0 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -1,11 +1,11 @@ -SUBDIRS = csu crypto +SUBDIRS = csu crypto lzp TOPGOALS = all clean install .PHONY: $(SUBDIRS) $(SUBDIRS): @echo "📁 lib/$@" - @DESTDIR=$(DESTDIR)/usr/lib $(MAKE) -C $@ $(MAKECMDGOALS) + @DESTDIR=$(DESTDIR) $(MAKE) -C $@ $(MAKECMDGOALS) .PHONY: $(TOPGOALS) $(TOPGOALS): $(SUBDIRS) diff --git a/lib/crypto/Makefile b/lib/crypto/Makefile index a78ea7f..2f10d43 100644 --- a/lib/crypto/Makefile +++ b/lib/crypto/Makefile @@ -1,5 +1,13 @@ TARGET = libcrypto.a -OBJS = hash/sha256.o +OBJS = sha2/sha256.o sha2/sha512.o \ + chacha20/chacha20.o +INCS = sha2/sha2.h \ + chacha20/chacha20.h +ASMS = sha2/sha2.inc \ + chacha20/chacha20.inc + +INCCRYPOTDIR = $(INCDIR)/crypto +ASMCRYPTODIR = $(ASMDIR)/crypto all: $(TARGET) @@ -13,5 +21,12 @@ clean: $(RM) $(TARGET) $(OBJS) install: $(TARGET) - @ mkdir -p $(DESTDIR) - install $(TARGET) $(DESTDIR) + @ mkdir -p $(DESTDIR)$(LIBDIR) + install $(TARGET) $(DESTDIR)$(LIBDIR) + @ mkdir -p $(DESTDIR)$(INCCRYPOTDIR) + install $(INCS) $(DESTDIR)$(INCCRYPOTDIR) + install crypto.h $(DESTDIR)$(INCDIR) + @ mkdir -p $(DESTDIR)$(ASMCRYPTODIR) + install $(ASMS) $(DESTDIR)$(ASMCRYPTODIR) + + diff --git a/lib/crypto/chacha20/chacha20.asm b/lib/crypto/chacha20/chacha20.asm new file mode 100644 index 0000000..c4e78c4 --- /dev/null +++ b/lib/crypto/chacha20/chacha20.asm @@ -0,0 +1,66 @@ + ; https://en.wikipedia.org/wiki/Salsa20#ChaCha_variant + ; https://datatracker.ietf.org/doc/html/rfc7539 + ; https://cr.yp.to/chacha/chacha-20080120.pdf + format COFF + use32 + + include 'chacha20.inc' + virtual at 0 + ctx ChaCha20Ctx + end virtual + +CHACHA20_CONST1 = 0x61707865 +CHACHA20_CONST2 = 0x3320646e +CHACHA20_CONST3 = 0x3320646e +CHACHA20_CONST4 = 0x6b206574 + +chacha20_init: + push ebp + mov ebp, esp + + mov eax, [ebp+8] ; ptr to ChaChaCtx + mov [eax+ctx.state], CHACHA20_CONST1 + mov [eax+ctx.state+1*4], CHACHA20_CONST2 + mov [eax+ctx.state+2*4], CHACHA20_CONST3 + mov [eax+ctx.state+3*4], CHACHA20_CONST4 + + ;; key + mov edx, [ebp+12] + + mov ecx, dword [edx] + mov [eax+ctx.state+4*4], ecx + mov ecx, dword [edx+1*4] + mov [eax+ctx.state+5*4], ecx + mov ecx, dword [edx+2*4] + mov [eax+ctx.state+6*4], ecx + mov ecx, dword [edx+3*4] + mov [eax+ctx.state+7*4], ecx + mov ecx, dword [edx+4*4] + mov [eax+ctx.state+8*4], ecx + mov ecx, dword [edx+5*4] + mov [eax+ctx.state+9*4], ecx + mov ecx, dword [edx+6*4] + mov [eax+ctx.state+10*4], ecx + mov ecx, dword [edx+7*4] + mov [eax+ctx.state+11*4], ecx + + ;; ctr + mov edx, [ebp+16] + + mov [eax+ctx.state+12*4], edx + + ;; nonce + mov edx, [ebp+20] + + mov ecx, dword [edx] + mov [eax+ctx.state+13*4], ecx + mov ecx, dword [edx+4] + mov [eax+ctx.state+14*4], ecx + mov ecx, dword [edx+8] + mov [eax+ctx.state+15*4], ecx + + leave + ret + +chacha20_block: + ret diff --git a/lib/crypto/chacha20/chacha20.h b/lib/crypto/chacha20/chacha20.h new file mode 100644 index 0000000..eba6ee2 --- /dev/null +++ b/lib/crypto/chacha20/chacha20.h @@ -0,0 +1,20 @@ +#ifndef CHACHA20_H +# define CHACHA20_H 1 + +# include + +# define CHACHA20_KEY_BYTES 32 +# define CHACHA20_NONCE_BYTES 12 +# define CHACHA20_BLOCK_BYTES 64 + +typedef struct +{ + uint32_t state[4][4]; + uint32_t ctr; +} ChaCha20Ctx; + +void chacha20_init(ChaCha20Ctx *ctx, const uint8_t *key, uint32_t ctr, + const uint8_t *nonce); +void chacha20_block(ChaCha20Ctx *ctx, uint8_t *out); + +#endif /* !CHACHA20_H */ diff --git a/lib/crypto/chacha20/chacha20.inc b/lib/crypto/chacha20/chacha20.inc new file mode 100644 index 0000000..16e9fcf --- /dev/null +++ b/lib/crypto/chacha20/chacha20.inc @@ -0,0 +1,12 @@ +struc ChaCha20Ctx { + .state dd 4*4 dup(?) + .ctr dd ? +} + +CHACHA20_KEY_BYTES = 32 +CHACHA20_NONCE_BYTES = 12 +CHACHA20_ROUND = 10 +CHACHA20_BLOCK_BYTES = 64 + + public chacha20_init + public chacha20_block diff --git a/lib/crypto/crypto.h b/lib/crypto/crypto.h new file mode 100644 index 0000000..3cdb4a3 --- /dev/null +++ b/lib/crypto/crypto.h @@ -0,0 +1,4 @@ +#ifndef CRYPTO_H +# define CRYPTO_H 1 + +#endif /* !CRYPTO_H */ diff --git a/lib/crypto/sha2/sha2.h b/lib/crypto/sha2/sha2.h new file mode 100644 index 0000000..f306fa8 --- /dev/null +++ b/lib/crypto/sha2/sha2.h @@ -0,0 +1,15 @@ +#ifndef CRYPTO_SHA2_H +# define CRYPTO_SHA2_H 1 + +# define SHA224_BYTES 28 +# define SHA256_BYTES 32 +# define SHA384_BYTES 48 +# define SHA512_BYTES 64 + +void sha224(void *out, const void *in, unsigned int len); +void sha256(void *out, const void *in, unsigned int len); + +void sha384(void *out, const void *in, unsigned int len); +void sha512(void *out, const void *in, unsigned int len); + +#endif /* !CRYPTO_SHA2_H */ diff --git a/lib/crypto/sha2/sha2.inc b/lib/crypto/sha2/sha2.inc new file mode 100644 index 0000000..76c6015 --- /dev/null +++ b/lib/crypto/sha2/sha2.inc @@ -0,0 +1,9 @@ +SHA224_BYTES = 28 +SHA256_BYTES = 32 +SHA384_BYTES = 48 +SHA512_BYTES = 64 + + extrn sha224 + extrn sha256 + extrn sha384 + extrn sha512 diff --git a/lib/crypto/hash/sha256.asm b/lib/crypto/sha2/sha256.asm similarity index 100% rename from lib/crypto/hash/sha256.asm rename to lib/crypto/sha2/sha256.asm diff --git a/lib/crypto/sha2/sha512.asm b/lib/crypto/sha2/sha512.asm new file mode 100644 index 0000000..9e3f681 --- /dev/null +++ b/lib/crypto/sha2/sha512.asm @@ -0,0 +1,2 @@ + format COFF + use32 diff --git a/lib/csu/Makefile b/lib/csu/Makefile index 0eb9f6d..0084d5d 100644 --- a/lib/csu/Makefile +++ b/lib/csu/Makefile @@ -9,5 +9,5 @@ clean: $(RM) $(TARGET) install: $(TARGET) - @ mkdir -p $(DESTDIR) - install $(TARGET) $(DESTDIR) + @ mkdir -p $(DESTDIR)$(LIBDIR) + install $(TARGET) $(DESTDIR)$(LIBDIR) diff --git a/lib/lzp/Makefile b/lib/lzp/Makefile new file mode 100644 index 0000000..d72ebe5 --- /dev/null +++ b/lib/lzp/Makefile @@ -0,0 +1,21 @@ +TARGET = liblzp.a +OBJS = lzp.o + +all: $(TARGET) + +$(TARGET): $(OBJS) + $(AR) rcs $@ $^ + +%.o: %.asm + $(AS) $< $@ + +clean: + $(RM) $(TARGET) $(OBJS) + +install: $(TARGET) + @ mkdir -p $(DESTDIR)$(LIBDIR) + install $(TARGET) $(DESTDIR)$(LIBDIR) + @ mkdir -p $(DESTDIR)$(INCDIR) + install lzp.h $(DESTDIR)$(INCDIR) + @ mkdir -p $(DESTDIR)$(ASMDIR) + install lzp.inc $(DESTDIR)$(ASMDIR) diff --git a/lib/lzp/lzp.asm b/lib/lzp/lzp.asm new file mode 100644 index 0000000..a4f9361 --- /dev/null +++ b/lib/lzp/lzp.asm @@ -0,0 +1,127 @@ + ;; Lempel-Ziv + Prediction (a fast, efficient, and memory-use + ;; conservative compression algorithm) + ;; (paper: https://ieeexplore.ieee.org/document/488353) + format COFF + use32 + + ;; https://hugi.scene.org/online/coding/hugi%2012%20-%20colzp.htm + + include 'lzp.inc' + + LZP_HASH_ORDER = 16 + LZP_HASH_SIZE = (1 shl LZP_HASH_ORDER) + + ; hash(h, x) (h = (h << 4) ^ x) + + section '.code' code + + ;; xor hash, hash + ;; xor mask, mask + ;; j = 1 + ;; while (insz > 0) + ;; { + ;; if (ecx == 8) + ;; { + ;; mov [out], mask + ;; xor ecx, ecx + ;; xor mask, mask + ;; j = 1; + ;; } + ;; c = in[inpos++] + ;; if (c == table[hash]) + ;; { + ;; mask |= 1 << ecx + ;; } + ;; else + ;; { + ;; table[hash] = c + ;; out[j] = c; + ;; } + ;; HASH(hash, c) + ;; ecx++; + ;; } + + ;; Function: lzp_compress(void *out, const void *in, int size) + ;; In: + ;; - [esp+8]: out buffer address (can be null) + ;; - [esp+12]: input buffer address + ;; - [esp+16]: size of the input buffer + ;; Out: + ;; - eax: size of compressed data + param_out equ [ebp+8] + param_in equ [ebp+12] + param_insz equ [ebp+16] + local_buff equ [ebp-10] + local_inpos equ [ebp-14] +lzp_compress: + push ebp + mov ebp, esp + sub esp, 14 + + push ebx + push esi + push edi + + mov edi, param_insz + test edi, edi + je .exit + + mov ebx, 0 + mov esi, 0 + mov edi, 0 +.loop: + xor ecx, ecx + + cmp esi, param_insz + je .end + + + xor ecx, ecx + ;; fetch data + mov eax, param_in + movzx eax, byte [eax+esi] + inc esi + + cmp al, byte [table + ebx] + jne .not_in_table + mov edx, 1 + sal edx, cl + or ebx, edx + +.not_in_table: + mov byte [table + ebx], al + + inc ecx +.check: + cmp esi, param_insz + jb .loop +.end: +.exit: + mov eax, edi ; return compressed data size + ;; restore esi, edi + pop edi + pop esi + + leave + ret + +lzp_decompress: + push ebp + mov ebp, esp + mov eax, [esp+8] ; in + mov edx, [esp+12] ; size + xor ecx, ecx +.loop: + cmp ecx, edx + je .end + + inc ecx + jmp .loop +.end: + mov eax, ecx + leave + ret + + section '.data' data + +table db LZP_HASH_SIZE dup(0) diff --git a/lib/lzp/lzp.h b/lib/lzp/lzp.h new file mode 100644 index 0000000..2b47a76 --- /dev/null +++ b/lib/lzp/lzp.h @@ -0,0 +1,7 @@ +#ifndef LZP_H +# define LZP_H 1 + +int lzp_compress(void *out, const void *in, int insz); +int lzp_decompress(void *out, const void *in, int insz); + +#endif /* !LZP_H */ diff --git a/lib/lzp/lzp.inc b/lib/lzp/lzp.inc new file mode 100644 index 0000000..6a78c14 --- /dev/null +++ b/lib/lzp/lzp.inc @@ -0,0 +1,2 @@ + public lzp_compress + public lzp_decompress