summaryrefslogtreecommitdiff
path: root/assembly/passworddoor/sha256sum.S
blob: 657f4c5062ae55eec9f01da89eca7aafbe5a4637 (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
.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!

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

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

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

origendian: .byte 0