build: define INCDIR,BINDIR,LIBDIR...
Some checks are pending
Build / test (push) Waiting to run
Docs / test (push) Waiting to run

This commit is contained in:
d0p1 🏳️‍⚧️ 2024-03-26 07:48:32 +01:00
parent 9a5b474ad2
commit 0365dd5da5
17 changed files with 312 additions and 9 deletions

View file

@ -4,6 +4,11 @@ TOPDIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST))))
SYSROOTDIR := $(TOPDIR)/sysroot SYSROOTDIR := $(TOPDIR)/sysroot
TOOLSDIR := $(TOPDIR)/tools TOOLSDIR := $(TOPDIR)/tools
BINDIR = /bin
LIBDIR = /usr/lib
INCDIR = /usr/include
ASMDIR = /usr/asm
AS = fasm AS = fasm
RM = rm -f RM = rm -f

View file

@ -1,5 +1,3 @@
INCDIR = $(DESTDIR)/usr/include
INCS = coff.h INCS = coff.h
.PHONY: all .PHONY: all

View file

@ -1,11 +1,11 @@
SUBDIRS = csu crypto SUBDIRS = csu crypto lzp
TOPGOALS = all clean install TOPGOALS = all clean install
.PHONY: $(SUBDIRS) .PHONY: $(SUBDIRS)
$(SUBDIRS): $(SUBDIRS):
@echo "📁 lib/$@" @echo "📁 lib/$@"
@DESTDIR=$(DESTDIR)/usr/lib $(MAKE) -C $@ $(MAKECMDGOALS) @DESTDIR=$(DESTDIR) $(MAKE) -C $@ $(MAKECMDGOALS)
.PHONY: $(TOPGOALS) .PHONY: $(TOPGOALS)
$(TOPGOALS): $(SUBDIRS) $(TOPGOALS): $(SUBDIRS)

View file

@ -1,5 +1,13 @@
TARGET = libcrypto.a 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) all: $(TARGET)
@ -13,5 +21,12 @@ clean:
$(RM) $(TARGET) $(OBJS) $(RM) $(TARGET) $(OBJS)
install: $(TARGET) install: $(TARGET)
@ mkdir -p $(DESTDIR) @ mkdir -p $(DESTDIR)$(LIBDIR)
install $(TARGET) $(DESTDIR) 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)

View file

@ -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

View file

@ -0,0 +1,20 @@
#ifndef CHACHA20_H
# define CHACHA20_H 1
# include <stdint.h>
# 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 */

View file

@ -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

4
lib/crypto/crypto.h Normal file
View file

@ -0,0 +1,4 @@
#ifndef CRYPTO_H
# define CRYPTO_H 1
#endif /* !CRYPTO_H */

15
lib/crypto/sha2/sha2.h Normal file
View file

@ -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 */

9
lib/crypto/sha2/sha2.inc Normal file
View file

@ -0,0 +1,9 @@
SHA224_BYTES = 28
SHA256_BYTES = 32
SHA384_BYTES = 48
SHA512_BYTES = 64
extrn sha224
extrn sha256
extrn sha384
extrn sha512

View file

@ -0,0 +1,2 @@
format COFF
use32

View file

@ -9,5 +9,5 @@ clean:
$(RM) $(TARGET) $(RM) $(TARGET)
install: $(TARGET) install: $(TARGET)
@ mkdir -p $(DESTDIR) @ mkdir -p $(DESTDIR)$(LIBDIR)
install $(TARGET) $(DESTDIR) install $(TARGET) $(DESTDIR)$(LIBDIR)

21
lib/lzp/Makefile Normal file
View file

@ -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)

127
lib/lzp/lzp.asm Normal file
View file

@ -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)

7
lib/lzp/lzp.h Normal file
View file

@ -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 */

2
lib/lzp/lzp.inc Normal file
View file

@ -0,0 +1,2 @@
public lzp_compress
public lzp_decompress