feat: working on tiny crypto lib (WIP)

This commit is contained in:
d0p1 🏳️‍⚧️ 2023-06-05 10:03:05 +02:00
parent 3cb67bf990
commit fcdb3ded8e
9 changed files with 211 additions and 2 deletions

View file

@ -45,6 +45,7 @@ Timestamp: Updated yyyy/mm/dd
Group: Kernel {
File: head.s (kernel/head.s)
File: gdt.s (kernel/gdt.s)
File: idt.s (kernel/idt.s)
File: multiboot.inc (kernel/multiboot.inc)
File: pic.s (kernel/pic.s)
@ -55,17 +56,37 @@ Group: Kernel {
File: serial.s (no auto-title, kernel/drivers/serial.s)
} # Group: Driver
File: msg_en (kernel/debug/msg_en.s)
} # Group: Kernel
Group: Lib {
File: base.inc (lib/base.inc)
File: log.s (lib/base/log.s)
Group: Base {
File: log.s (lib/base/log.s)
} # Group: Base
Group: Crypto {
Group: Hash {
File: sha256.s (lib/crypto/hash/sha256.s)
File: keccak.s (lib/crypto/hash/keccak.s)
} # Group: Hash
} # Group: Crypto
File: ecdsa.s (lib/crypto/sign/ecdsa.s)
} # Group: Lib
File: crt0.s (bin/crt0.s)
Group: Index {
Index: Everything
Constant Index: Constants
File Index: Files
Function Index: Functions
Macro Index: Macros

4
kernel/base/console.s Normal file
View file

@ -0,0 +1,4 @@
struc console
.init:
endstruc

29
kernel/debug/msg_en.s Normal file
View file

@ -0,0 +1,29 @@
; file: msg_en
; English strings
.section rodata
msg_hello_world:
db "StupidOS v", STUPID_VERSION, " (built with ", NASM_VERSION, " on ", BUILD_DATE, ")", 0
msg_boot_info:
db "Bootloader: %s", 0
msg_pmm_initialized:
db "PMM initialized", 0
err_invalid_boot_magic:
db "[ERROR] Invalid boot magic (got: %x, expected: 0x2BADB002)", 0
err_cannot_map_memory:
db "[ERROR] Can't map memory", 0
warn_no_mmap:
db "[WARN] mmap flag not set", 0
msg_mmap_entry:
db "Memory Map Entry:", 0xA
db 0x9, "Address: (hi): %x (lo): %x", 0xA
db 0x9, "Length: (hi): %x (lo): %x", 0xA
db 0x9, "Type: %x", 0
msg_mem_block:
db "Free Memory:", 0xA
db 0x9, "Address: %x", 0xA
db 0x9, "Length: %x", 0
msg_max_mem:
db "Max memory: %x", 0
msg_bitmap_stored_at:
db "Bitmap stored at: %x", 0

View file

@ -1,7 +1,18 @@
; file: gdt.s
;
;
[BITS 32]
section .text
; Function: setup_gdt
;
; in:
; none
;
; out:
; none
;
global setup_gdt
setup_gdt:
lgdt [gdt_ptr]

View file

@ -1,3 +1,5 @@
; file: pic.s
;
[BITS 32]
PIC1_CMD equ 0x20

3
lib/crypto/hash/keccak.s Normal file
View file

@ -0,0 +1,3 @@
; file: keccak.s
; Keccak 256 cryptographic hash

137
lib/crypto/hash/sha256.s Normal file
View file

@ -0,0 +1,137 @@
; file: sha256.s
; SHA-256 cryptographic hash
;
; Implementation is based on <Wikipedia's pseudocode at https://en.wikipedia.org/wiki/SHA-2#Pseudocode>
;
; About: CPU compatibility
; /!\ Only work on *486+* due to bswap for now.
[BITS 32]
section .rodata
; Constant: K
; SHA-256 round constants
K:
dd 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
dd 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
dd 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
dd 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
dd 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
dd 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
dd 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
dd 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
section .text
; Function: sha256_compute_block
sha256_compute_block:
push ebp
mov ebp, esp
leave
ret
; Function: sha256_internal
;
; in:
; - state
; - Input buffer
; - Input buffer size
;
; out:
; none
;
sha256_internal:
push ebp
push edi
mov ebp, esp
sub esp, 64+8
mov eax, dword [ebp+16]
xor edx, edx
; set padlen to (len << 3)
shld edx, eax, 3 ; 64-bit left shit
sal eax, 3
mov dword [ebp-(64+8)], eax
mov dword [ebp-(64+4)], edx
.for_block:
cmp dword [ebp+16], 64
jb .done
push dword [ebp+12] ; push in
push dword [ebp+8] ; push state
call sha256_compute_block
add esp, 8
sub dword [ebp+16], 64 ; len -= 64
add dword [ebp+12], 64 ; in += 64
jmp .for_block
.done:
; ensure padding buffer is full of zero
lea edi, [ebp-64]
mov ecx, 16
xor eax, eax
rep stosd
pop edi
leave
ret
; Function: sha256
;
; in:
; - Output buffer
; - Input buffer
; - Input buffer size
;
; out:
; none
;
global sha256
sha256:
push ebp
mov ebp, esp
sub esp, 8*4 ; uint32_t state[8]
; setup initial state
mov dword [ebp-8*4], 0x6a09e667
mov dword [ebp-7*4], 0xbb67ae85
mov dword [ebp-6*4], 0x3c6ef372
mov dword [ebp-5*4], 0xa54ff53a
mov dword [ebp-4*4], 0x510e527f
mov dword [ebp-3*4], 0x9b05688c
mov dword [ebp-2*4], 0x1f83d9ab
mov dword [ebp-1*4], 0x5be0cd19
;
; copy state to uint8_t *out
mov edx, dword [ebp+8]
mov eax, dword [ebp-8*4]
bswap eax
mov dword [edx], eax ; out[0] = bswap(state[0])
mov eax, dword [ebp-7*4]
bswap eax
mov dword [edx+4], eax ; out[1] = bswap(state[1])
mov eax, dword [ebp-6*4]
bswap eax
mov dword [edx+8], eax ; out[2] = bswap(state[2])
mov eax, dword [ebp-5*4]
bswap eax
mov dword [edx+12], eax ; out[3] = bswap(state[3])
mov eax, dword [ebp-4*4]
bswap eax
mov dword [edx+16], eax ; out[4] = bswap(state[4])
mov eax, dword [ebp-3*4]
bswap eax
mov dword [edx+20], eax ; out[5] = bswap(state[5])
mov eax, dword [ebp-2*4]
bswap eax
mov dword [edx+24], eax ; out[6] = bswap(state[6])
mov eax, dword [ebp-1*4]
bswap eax
mov dword [edx+28], eax ; out[7] = bswap(state[7])
leave
ret

2
lib/crypto/sign/ecdsa.s Normal file
View file

@ -0,0 +1,2 @@
; file: ecdsa.s
; Elliptic Curve Digital Signature Algorithm (ECDSA)