Skip to content

Commit

Permalink
build: define INCDIR,BINDIR,LIBDIR...
Browse files Browse the repository at this point in the history
  • Loading branch information
d0p1s4m4 committed Mar 26, 2024
1 parent 9a5b474 commit 0365dd5
Show file tree
Hide file tree
Showing 17 changed files with 312 additions and 9 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 0 additions & 2 deletions include/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
INCDIR = $(DESTDIR)/usr/include

INCS = coff.h

.PHONY: all
Expand Down
4 changes: 2 additions & 2 deletions lib/Makefile
Original file line number Diff line number Diff line change
@@ -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)
21 changes: 18 additions & 3 deletions lib/crypto/Makefile
Original file line number Diff line number Diff line change
@@ -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)

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


66 changes: 66 additions & 0 deletions lib/crypto/chacha20/chacha20.asm
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions lib/crypto/chacha20/chacha20.h
Original file line number Diff line number Diff line change
@@ -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 */
12 changes: 12 additions & 0 deletions lib/crypto/chacha20/chacha20.inc
Original file line number Diff line number Diff line change
@@ -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 changes: 4 additions & 0 deletions lib/crypto/crypto.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#ifndef CRYPTO_H
# define CRYPTO_H 1

#endif /* !CRYPTO_H */
15 changes: 15 additions & 0 deletions lib/crypto/sha2/sha2.h
Original file line number Diff line number Diff line change
@@ -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 changes: 9 additions & 0 deletions lib/crypto/sha2/sha2.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
SHA224_BYTES = 28
SHA256_BYTES = 32
SHA384_BYTES = 48
SHA512_BYTES = 64

extrn sha224
extrn sha256
extrn sha384
extrn sha512
File renamed without changes.
2 changes: 2 additions & 0 deletions lib/crypto/sha2/sha512.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
format COFF
use32
4 changes: 2 additions & 2 deletions lib/csu/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ clean:
$(RM) $(TARGET)

install: $(TARGET)
@ mkdir -p $(DESTDIR)
install $(TARGET) $(DESTDIR)
@ mkdir -p $(DESTDIR)$(LIBDIR)
install $(TARGET) $(DESTDIR)$(LIBDIR)
21 changes: 21 additions & 0 deletions lib/lzp/Makefile
Original file line number Diff line number Diff line change
@@ -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 changes: 127 additions & 0 deletions lib/lzp/lzp.asm
Original file line number Diff line number Diff line change
@@ -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 changes: 7 additions & 0 deletions lib/lzp/lzp.h
Original file line number Diff line number Diff line change
@@ -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 changes: 2 additions & 0 deletions lib/lzp/lzp.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public lzp_compress
public lzp_decompress

0 comments on commit 0365dd5

Please sign in to comment.