summaryrefslogtreecommitdiff
path: root/assembly/passworddoor/passworddoor.S
blob: a854bb44f029abb64cae31221f828f8fa3c52f7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# 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

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

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

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
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)

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\n\nPlease wait...\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\nSalting\nPlease wait...\n"
msgchanging2: .asciz "\nChanging Password\n\n\nHashing\nPlease wait...\n"
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"