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"