summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcheapie <cheapiephp@gmail.com>2026-11-13 07:16:56 -0600
committercheapie <cheapiephp@gmail.com>2026-11-13 07:16:56 -0600
commitf19d1bf238c31b0d56fc6d676cd065334a93ba57 (patch)
tree85e3c8bba10bd13c2047875c977fe53ca44d564b
parent08c992a191f83777373bb8584c57cbdccfcc8368 (diff)
downloadrvcontroller-f19d1bf238c31b0d56fc6d676cd065334a93ba57.tar
rvcontroller-f19d1bf238c31b0d56fc6d676cd065334a93ba57.tar.gz
rvcontroller-f19d1bf238c31b0d56fc6d676cd065334a93ba57.tar.bz2
rvcontroller-f19d1bf238c31b0d56fc6d676cd065334a93ba57.tar.xz
rvcontroller-f19d1bf238c31b0d56fc6d676cd065334a93ba57.zip
Make passworddoor do some more silly trickery because yes
-rw-r--r--assembly/passworddoor/Makefile3
-rw-r--r--assembly/passworddoor/passworddoor.S42
-rw-r--r--assembly/passworddoor/sha256sum.S19
3 files changed, 53 insertions, 11 deletions
diff --git a/assembly/passworddoor/Makefile b/assembly/passworddoor/Makefile
index 735dda5..5a3c485 100644
--- a/assembly/passworddoor/Makefile
+++ b/assembly/passworddoor/Makefile
@@ -5,6 +5,9 @@ MARCH ?= rv32imacbzicboz_zicfilp_zicntr_zicond_zicsr_zifencei_zihintpause_zihpm_
all: passworddoor.hex
passworddoor.o: passworddoor.S sha256sum.S
+# Even if I did assemble these as two separate objects, I don't think there's any way to
+# link a big-endian and little-endian object together into the same executable, so just
+# lie to the assembler/linker and say it's all big-endian instead
riscv32be-none-elf-as -I../rvcontroller-libraries -march=${MARCH} -o passworddoor.o passworddoor.S
passworddoor.elf: passworddoor.o
diff --git a/assembly/passworddoor/passworddoor.S b/assembly/passworddoor/passworddoor.S
index 687becf..a808609 100644
--- a/assembly/passworddoor/passworddoor.S
+++ b/assembly/passworddoor/passworddoor.S
@@ -1,9 +1,18 @@
+# Thanks to the Hazard3 reference manual for the macro this is based on
+.macro h3.bextmi rd rs1 shamt nbits
+ .if (\nbits < 1) || (\nbits > 8)
+ .err
+ .endif
+ .if (\shamt < 0) || (\shamt > 31)
+ .err
+ .endif
+ .insn i 0x0b, 0x4, \rd, \rs1, (\shamt & 0x1f) | (((\nbits - 1) & 0x7 ) << 6)
+.endm
+
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
@@ -215,10 +224,15 @@ 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
+
+h3.bextmi t0,a0,30,2 # Extract just the OPST field
+beqz t0,randbytenz # BIST, wait and try again
+li t1,1
+beq t0,t1,randbytenz # WAIT, wait and try again
+li t1,3
+beq t0,t1,rngfail # DEAD, fatal error
+# Otherwise ES16, output is OK
+
andi a0,a0,0xff # Trim down to one byte
beqz a0,randbytenz # If it's zero then try again
ret
@@ -240,10 +254,18 @@ 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)
+lw t1,0(t0) # Copy message to hash function input
+rev8 t1,t1 # Making sure to write it as big-endian
+sw t1,0(t3)
+lw t1,4(t0)
+rev8 t1,t1
+sw t1,4(t3)
+lw t1,8(t0)
+rev8 t1,t1
+sw t1,8(t3)
+lw t1,12(t0)
+rev8 t1,t1
+sw t1,12(t3)
sd x0,0(t0) # Then wipe it
sd x0,8(t0)
diff --git a/assembly/passworddoor/sha256sum.S b/assembly/passworddoor/sha256sum.S
index c2add42..657f4c5 100644
--- a/assembly/passworddoor/sha256sum.S
+++ b/assembly/passworddoor/sha256sum.S
@@ -2,6 +2,14 @@
sha256sum:
# WARNING: Clobbers a whole bunch of registers!
+
+csrr t0,mstatush
+bexti t0,t0,5 # Get current MBE
+la t1,origendian
+sb t0,0(t1) # Save it for later
+li t0,0x20
+csrs mstatush,t0 # And go big-endian
+
# Count the message length and replace the null terminator with 0x80
la t0,message
li t1,0
@@ -159,7 +167,14 @@ 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
+
+la t0,origendian
+lb t0,0(t0) # Read what MBE was at the start
+bnez t0,1f
+li t0,0x20
+csrc mstatush,t0 # If it was little, then put it back to little
+
+1: ret
horig: .word 0x6a09e667,0xbb67ae85,0x3c6ef372,0xa54ff53a,0x510e527f,0x9b05688c,0x1f83d9ab,0x5be0cd19
h: .skip 8*4
@@ -180,3 +195,5 @@ w: .skip 64*4
message:
.word 0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000
.word 0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000,0x00000000
+
+origendian: .byte 0