From 08c992a191f83777373bb8584c57cbdccfcc8368 Mon Sep 17 00:00:00 2001 From: cheapie Date: Thu, 12 Nov 2026 19:19:11 -0600 Subject: Add password-protected door --- assembly/passworddoor/Makefile | 24 +++ assembly/passworddoor/passworddoor.S | 279 ++++++++++++++++++++++++++++++++++ assembly/passworddoor/rvcontroller.ld | 1 + assembly/passworddoor/sha256sum.S | 182 ++++++++++++++++++++++ 4 files changed, 486 insertions(+) create mode 100644 assembly/passworddoor/Makefile create mode 100644 assembly/passworddoor/passworddoor.S create mode 120000 assembly/passworddoor/rvcontroller.ld create mode 100644 assembly/passworddoor/sha256sum.S diff --git a/assembly/passworddoor/Makefile b/assembly/passworddoor/Makefile new file mode 100644 index 0000000..735dda5 --- /dev/null +++ b/assembly/passworddoor/Makefile @@ -0,0 +1,24 @@ +MARCH ?= rv32imacbzicboz_zicfilp_zicntr_zicond_zicsr_zifencei_zihintpause_zihpm_zilsd_zimop_zabha_zacas_zalasr_zawrs_zcb_zclsd_zcmop_zcmp_zcmt_zbc_zk_zkr_zks_xh3bextm1p0_xh3misa1p0 + +.PHONY: all dump load clean + +all: passworddoor.hex + +passworddoor.o: passworddoor.S sha256sum.S + riscv32be-none-elf-as -I../rvcontroller-libraries -march=${MARCH} -o passworddoor.o passworddoor.S + +passworddoor.elf: passworddoor.o + riscv32be-none-elf-ld -T rvcontroller.ld --no-warn-rwx-segments -o passworddoor.elf passworddoor.o + +dump: passworddoor.elf + riscv32be-none-elf-objdump -d passworddoor.elf + +passworddoor.hex: passworddoor.elf + riscv32be-none-elf-objcopy -O ihex passworddoor.elf passworddoor.hex + +load: passworddoor.hex + bash -c "wl-copy < passworddoor.hex" + +clean: + rm -f passworddoor.hex passworddoor.elf passworddoor.o + diff --git a/assembly/passworddoor/passworddoor.S b/assembly/passworddoor/passworddoor.S new file mode 100644 index 0000000..687becf --- /dev/null +++ b/assembly/passworddoor/passworddoor.S @@ -0,0 +1,279 @@ +li gp,0xffff0000 +csrw 0x801,gp # Map MMIO +li t0,0xf +sb t0,0(gp) # All four pins -> output +li t0,0x20 # MBE +csrs mstatush,t0 # Big-endian here we go! + +changepassword: +li a7,4 +la a0,msgnewpassword +ecall # Ask user for new password + +la t0,inbuf1 +sd x0,0(t0) # Make sure any previous data is gone +sd x0,8(t0) + +la a0,inbuf1 +li a1,17 +li a7,8 +ecall # Read it + +li a7,4 +la a0,msgnewpassword2 +ecall # Ask user to confirm password + +la t0,inbuf2 +sd x0,0(t0) # Make sure any previous data is gone +sd x0,8(t0) + +la a0,inbuf2 +li a1,17 +li a7,8 +ecall # Read it + +la a0,inbuf1 +la a1,inbuf2 +call streq # Compare the two new passwords +bnez a0,pwmismatch + +la a0,msgchanging1 +li a7,4 +ecall # This will take a bit, so tell the user that + +# Generate and save new salt value +call randbytenz +slli s0,a0,24 +call randbytenz +slli a0,a0,16 +or s0,s0,a0 +call randbytenz +slli a0,a0,8 +or s0,s0,a0 +call randbytenz +or s0,s0,a0 +la t0,salt +sw s0,0(t0) + +la a0,msgchanging2 +li a7,4 +ecall # Update progress indication + +call dohash # Come up with a hash of the new password + +la t0,inbuf1 +sd x0,0(t0) # Wipe the plaintext password from memory +sd x0,8(t0) +la t0,inbuf2 +sd x0,0(t0) +sd x0,8(t0) + +la t0,h # Hash output +la t3,hash # Saved hash value +ld t1,0(t0) # Copy it +sd t1,0(t3) +ld t1,8(t0) +sd t1,8(t3) +ld t1,16(t0) +sd t1,16(t3) +ld t1,24(t0) +sd t1,24(t3) + +j mainmenu + +pwprompt: +li a7,4 +la a0,msgpwprompt +ecall # Print password prompt + +la t0,inbuf1 +sd x0,0(t0) # Make sure any previous data is gone +sd x0,8(t0) + +la a0,inbuf1 +li a1,17 +li a7,8 +ecall # Read the password from the user + +la a0,msgwait +li a7,4 +ecall # Print wait message + +call dohash # Hash what the user gave + +la t0,inbuf1 +sd x0,0(t0) # Wipe the plaintext password from memory +sd x0,8(t0) +la t0,inbuf2 +sd x0,0(t0) +sd x0,8(t0) + +# Compare the hash to what the user entered +la t0,hash +la t1,h +li t2,8 +1: lw t3,0(t0) +lw t4,0(t1) +bne t3,t4,wrongpassword +addi t3,t3,4 +addi t4,t4,4 +addi t2,t2,-1 +bnez t2,1b + +la a0,msgcorrectpassword +li a7,4 +ecall # Print access granted message + +li a7,12 +ecall # Wait for the user to press a key + +j mainmenu + +wrongpassword: +la a0,msgwrongpassword +li a7,4 +ecall # Print wrong password message + +li a7,12 +ecall # Wait for the user to press a key + +j pwprompt + +pwmismatch: +la a0,msgmismatch +li a7,4 +ecall # Print mismatch message + +li a7,12 +ecall # Wait for the user to press a key + +j changepassword + +mainmenu: +la a0,msgmainmenu +li a7,4 +ecall # Print menu + +li a7,12 +ecall # Read the user's choice + +li t0,'1' +bne a0,t0,1f +call toggledoor +j mainmenu +1: + +li t0,'2' +bne a0,t0,1f +j changepassword +1: + +li t0,'0' +bne a0,t0,1f +j pwprompt +1: + +j mainmenu + +toggledoor: +lb t0,1(gp) +xori t0,t0,0xf # Toggle all four pin states +sb t0,1(gp) + +la a0,msgtoggledone +li a7,4 +ecall # Tell the user it was successful + +li a7,12 +ecall # And wait for them to press a key +ret + +streq: +# Expects pointers to null-terminated strings in a0/a1, returns 0 in a0 if strings match or 1 if they differ +lb t0,0(a0) +lb t1,0(a1) +bne t0,t1,1f +addi a0,a0,1 +addi a1,a1,1 +bnez t0,streq +li a0,0 +ret +1: li a0,1 +ret + +strlen: +# Expects a pointer to a null-terminated string in a0, returns the length of the string (including terminator) +mv t0,a0 +li a0,0 +1: lb t1,0(t0) +addi a0,a0,1 +addi t0,t0,1 +bnez t1,1b +ret + +randbytenz: +# Returns a random non-zero byte in a0 +pause +csrrsi a0,seed,1 +bexti t0,a0,31 +beqz t0,randbytenz # OPST=BIST or WAIT, try again +bexti t0,a0,30 +bnez t0,rngfail # OPST=DEAD, unrecoverable error +andi a0,a0,0xff # Trim down to one byte +beqz a0,randbytenz # If it's zero then try again +ret + +rngfail: +# Probably not reachable on RVController's current Zkr implementation (never goes BIST or DEAD) but whatever +la a0,msgrngfail +li a7,4 +ecall # Print RNG error message +li a7,10 +ecall # And halt + +dohash: +# No arguments, returns nothing, but clobbers (via sha256sum) a bunch of s registers +la a0,inbuf1 +mv s0,ra +call strlen # Figure out how long the password is +mv ra,s0 + +la t0,inbuf1 +la t3,message +ld t1,0(t0) # Copy input +sd t1,0(t3) # To hash function input +ld t1,8(t0) +sd t1,8(t3) +sd x0,0(t0) # Then wipe it +sd x0,8(t0) + +addi a0,a0,-1 # Back up a bit so the terminator gets overwritten +la t0,message +add a0,a0,t0 +la t0,salt +lw t0,0(t0) +sw t0,0(a0) # Append the salt to the message + +j sha256sum # Tail call optimization :D + +msgpwprompt: .asciz "\nPanel Locked\n\n\n\n\nEnter password:" +msgwait: .asciz "\nChecking Password\n\n\nPlease wait...\n\n" +msgnewpassword: .asciz "\nChanging Password\nPassword may be\nup to 16 characters\n\nEnter new\npassword:" +msgnewpassword2: .asciz "\nChanging Password\nPassword may be\nup to 16 characters\n\nConfirm new\npassword:" +msgmismatch: .asciz "\nPasswords\ndo not match!\n\n\n\nPress any key" +msgwrongpassword: .asciz "\nIncorrect Password!\n\n\nPress any key\n\n" +msgcorrectpassword: .asciz "\nAccess Granted\n\n\nPress any key\n\n" +msgchanging1: .asciz "\nChanging Password\n\n\n\nSalting\nPlease wait..." +msgchanging2: .asciz "\nChanging Password\n\n\n\nHashing\nPlease wait..." +msgrngfail: .asciz "\nHardware RNG\nreported\nfatal error\n\nSystem halted\n" +msgmainmenu: .asciz "\nMENU\n1: Toggle Door\n2: Change Password\n\n\n0: Logout" +msgtoggledone: .asciz "\nDoor Toggled\nSuccessfully\n\n\n\nPress any key" + +inbuf1: .asciz " " +inbuf2: .asciz " " + +hash: .word 0xe3b0c442,0x98fc1c14,0x9afbf4c8,0x996fb924,0x27ae41e4,0x649b934c,0xa495991b,0x7852b855 # Hash of an empty string + +salt: .word 0x00000000 # Default salt is none, this will change when a password is set + +.include "sha256sum.S" diff --git a/assembly/passworddoor/rvcontroller.ld b/assembly/passworddoor/rvcontroller.ld new file mode 120000 index 0000000..bc01402 --- /dev/null +++ b/assembly/passworddoor/rvcontroller.ld @@ -0,0 +1 @@ +../../rvcontroller.ld \ No newline at end of file diff --git a/assembly/passworddoor/sha256sum.S b/assembly/passworddoor/sha256sum.S new file mode 100644 index 0000000..c2add42 --- /dev/null +++ b/assembly/passworddoor/sha256sum.S @@ -0,0 +1,182 @@ +.align 1 # I really shouldn't need this, but IALIGN seems to go out the window otherwise + +sha256sum: +# WARNING: Clobbers a whole bunch of registers! +# Count the message length and replace the null terminator with 0x80 +la t0,message +li t1,0 +1: lb t2,0(t0) +addi t0,t0,1 +addi t1,t1,1 +bnez t2,1b +addi t0,t0,-1 +addi t1,t1,-1 +li t2,0x80 +sb t2,0(t0) +la t0,message+60 +slli t1,t1,3 +sw t1,0(t0) + +# Copy starting values into h +la t0,horig +ld t1,0(t0) +sd t1,32(t0) +ld t1,8(t0) +sd t1,40(t0) +ld t1,16(t0) +sd t1,48(t0) +ld t1,24(t0) +sd t1,56(t0) + +li s0,0 # Round counter + +# Copy message into w (full message since we're only doing the one chunk) +li t2,16 +la t0,message +la t1,w +1: ld t3,0(t0) # Copy 2 words of message... +sd t3,0(t1) # ...to w +addi t0,t0,8 +addi t1,t1,8 +addi s0,s0,2 +blt s0,t2,1b + +# Wikipedia calls this "Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array" +1: la t0,w +sh2add t0,s0,t0 +addi t0,t0,-60 # i-15 +la t1,w +sh2add t1,s0,t1 +addi t1,t1,-8 # i-2 +la t2,w +sh2add t2,s0,t2 +addi t2,t2,-64 # i-16 +la t3,w +sh2add t3,s0,t3 +addi t3,t3,-28 # i-7 +lw t0,0(t0) +lw t1,0(t1) +lw t2,0(t2) +lw t3,0(t3) +sha256sig0 t0,t0 +sha256sig1 t1,t1 +add t0,t0,t1 +add t0,t0,t2 +add t0,t0,t3 +la t1,w +sh2add t1,s0,t1 +sw t0,0(t1) +addi s0,s0,1 +li t0,64 +blt s0,t0,1b + +# Load initial working values +la t0,h +lw a0,0(t0) +lw a1,4(t0) +lw a2,8(t0) +lw a3,12(t0) +lw a4,16(t0) +lw a5,20(t0) +lw a6,24(t0) +lw a7,28(t0) + +li s0,0 +# Wikipedia calls this "Compression function main loop" +# S1 +1: sha256sum1 t0,a4 + +# ch +and t1,a4,a5 # e AND f +andn t2,a6,a4 # g AND (NOT e) - also hey, turns out this instruction is actually useful for something +xor t1,t1,t2 # (e AND f) XOR (g AND (NOT e)) + +# temp1 +add t0,t0,t1 # S1 + ch +add t0,t0,a7 # + h +la t1,k +sh2add t1,s0,t1 +lw t1,0(t1) +add t0,t0,t1 # + k[i] +la t1,w +sh2add t1,s0,t1 +lw t1,0(t1) +add t0,t0,t1 # + w[i] + +# S0 +sha256sum0 t1,a0 + +# maj +and t2,a0,a1 # a AND b +and t3,a0,a2 # a AND c +and t4,a1,a2 # b AND c +xor t2,t2,t3 # (a AND b) XOR (a AND c) +xor t2,t2,t4 # (a AND b) XOR (a AND c) XOR (b AND c) + +# temp2 +add t1,t1,t2 # S0 + maj + +mv a7,a6 # h = g +mv a6,a5 # g = f +mv a5,a4 # f = e +add a4,a3,t0 # e = d + temp1 +mv a3,a2 # d = c +mv a2,a1 # c = b +mv a1,a0 # b = a +add a0,t0,t1 # a = temp1 + temp2 + +addi s0,s0,1 +li t0,64 +blt s0,t0,1b + +la t0,h +lw t1,0(t0) +add t1,t1,a0 +sw t1,0(t0) +lw t1,4(t0) +add t1,t1,a1 +sw t1,4(t0) +lw t1,8(t0) +add t1,t1,a2 +sw t1,8(t0) +lw t1,12(t0) +add t1,t1,a3 +sw t1,12(t0) +lw t1,16(t0) +add t1,t1,a4 +sw t1,16(t0) +lw t1,20(t0) +add t1,t1,a5 +sw t1,20(t0) +lw t1,24(t0) +add t1,t1,a6 +sw t1,24(t0) +lw t1,28(t0) +add t1,t1,a7 +sw t1,28(t0) + +la t0,w +cbo.zero (t0) # Wipe memory used by hashing function +la t0,message +cbo.zero (t0) # Making sure to wipe the input too +ret + +horig: .word 0x6a09e667,0xbb67ae85,0x3c6ef372,0xa54ff53a,0x510e527f,0x9b05688c,0x1f83d9ab,0x5be0cd19 +h: .skip 8*4 + +k: +.word 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5 +.word 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174 +.word 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da +.word 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967 +.word 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85 +.word 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070 +.word 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3 +.word 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2 + +.align 8 +w: .skip 64*4 + +message: +.word 0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000 +.word 0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000 -- cgit v1.2.3