Text file
src/crypto/sha1/sha1block_386.s
1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build !purego
6
7 #include "textflag.h"
8
9 // SHA-1 block routine. See sha1block.go for Go equivalent.
10 //
11 // There are 80 rounds of 4 types:
12 // - rounds 0-15 are type 1 and load data (ROUND1 macro).
13 // - rounds 16-19 are type 1 and do not load data (ROUND1x macro).
14 // - rounds 20-39 are type 2 and do not load data (ROUND2 macro).
15 // - rounds 40-59 are type 3 and do not load data (ROUND3 macro).
16 // - rounds 60-79 are type 4 and do not load data (ROUND4 macro).
17 //
18 // Each round loads or shuffles the data, then computes a per-round
19 // function of b, c, d, and then mixes the result into and rotates the
20 // five registers a, b, c, d, e holding the intermediate results.
21 //
22 // The register rotation is implemented by rotating the arguments to
23 // the round macros instead of by explicit move instructions.
24
25 // Like sha1block_amd64.s, but we keep the data and limit pointers on the stack.
26 // To free up the word pointer (R10 on amd64, DI here), we add it to e during
27 // LOAD/SHUFFLE instead of during MIX.
28 //
29 // The stack holds the intermediate word array - 16 uint32s - at 0(SP) up to 64(SP).
30 // The saved a, b, c, d, e (R11 through R15 on amd64) are at 64(SP) up to 84(SP).
31 // The saved limit pointer (DI on amd64) is at 84(SP).
32 // The saved data pointer (SI on amd64) is at 88(SP).
33
34 #define LOAD(index, e) \
35 MOVL 88(SP), SI; \
36 MOVL (index*4)(SI), DI; \
37 BSWAPL DI; \
38 MOVL DI, (index*4)(SP); \
39 ADDL DI, e
40
41 #define SHUFFLE(index, e) \
42 MOVL (((index)&0xf)*4)(SP), DI; \
43 XORL (((index-3)&0xf)*4)(SP), DI; \
44 XORL (((index-8)&0xf)*4)(SP), DI; \
45 XORL (((index-14)&0xf)*4)(SP), DI; \
46 ROLL $1, DI; \
47 MOVL DI, (((index)&0xf)*4)(SP); \
48 ADDL DI, e
49
50 #define FUNC1(a, b, c, d, e) \
51 MOVL d, DI; \
52 XORL c, DI; \
53 ANDL b, DI; \
54 XORL d, DI
55
56 #define FUNC2(a, b, c, d, e) \
57 MOVL b, DI; \
58 XORL c, DI; \
59 XORL d, DI
60
61 #define FUNC3(a, b, c, d, e) \
62 MOVL b, SI; \
63 ORL c, SI; \
64 ANDL d, SI; \
65 MOVL b, DI; \
66 ANDL c, DI; \
67 ORL SI, DI
68
69 #define FUNC4 FUNC2
70
71 #define MIX(a, b, c, d, e, const) \
72 ROLL $30, b; \
73 ADDL DI, e; \
74 MOVL a, SI; \
75 ROLL $5, SI; \
76 LEAL const(e)(SI*1), e
77
78 #define ROUND1(a, b, c, d, e, index) \
79 LOAD(index, e); \
80 FUNC1(a, b, c, d, e); \
81 MIX(a, b, c, d, e, 0x5A827999)
82
83 #define ROUND1x(a, b, c, d, e, index) \
84 SHUFFLE(index, e); \
85 FUNC1(a, b, c, d, e); \
86 MIX(a, b, c, d, e, 0x5A827999)
87
88 #define ROUND2(a, b, c, d, e, index) \
89 SHUFFLE(index, e); \
90 FUNC2(a, b, c, d, e); \
91 MIX(a, b, c, d, e, 0x6ED9EBA1)
92
93 #define ROUND3(a, b, c, d, e, index) \
94 SHUFFLE(index, e); \
95 FUNC3(a, b, c, d, e); \
96 MIX(a, b, c, d, e, 0x8F1BBCDC)
97
98 #define ROUND4(a, b, c, d, e, index) \
99 SHUFFLE(index, e); \
100 FUNC4(a, b, c, d, e); \
101 MIX(a, b, c, d, e, 0xCA62C1D6)
102
103 // func block(dig *digest, p []byte)
104 TEXT ·block(SB),NOSPLIT,$92-16
105 MOVL dig+0(FP), BP
106 MOVL p+4(FP), SI
107 MOVL p_len+8(FP), DX
108 SHRL $6, DX
109 SHLL $6, DX
110
111 LEAL (SI)(DX*1), DI
112 MOVL (0*4)(BP), AX
113 MOVL (1*4)(BP), BX
114 MOVL (2*4)(BP), CX
115 MOVL (3*4)(BP), DX
116 MOVL (4*4)(BP), BP
117
118 CMPL SI, DI
119 JEQ end
120
121 MOVL DI, 84(SP)
122
123 loop:
124 MOVL SI, 88(SP)
125
126 MOVL AX, 64(SP)
127 MOVL BX, 68(SP)
128 MOVL CX, 72(SP)
129 MOVL DX, 76(SP)
130 MOVL BP, 80(SP)
131
132 ROUND1(AX, BX, CX, DX, BP, 0)
133 ROUND1(BP, AX, BX, CX, DX, 1)
134 ROUND1(DX, BP, AX, BX, CX, 2)
135 ROUND1(CX, DX, BP, AX, BX, 3)
136 ROUND1(BX, CX, DX, BP, AX, 4)
137 ROUND1(AX, BX, CX, DX, BP, 5)
138 ROUND1(BP, AX, BX, CX, DX, 6)
139 ROUND1(DX, BP, AX, BX, CX, 7)
140 ROUND1(CX, DX, BP, AX, BX, 8)
141 ROUND1(BX, CX, DX, BP, AX, 9)
142 ROUND1(AX, BX, CX, DX, BP, 10)
143 ROUND1(BP, AX, BX, CX, DX, 11)
144 ROUND1(DX, BP, AX, BX, CX, 12)
145 ROUND1(CX, DX, BP, AX, BX, 13)
146 ROUND1(BX, CX, DX, BP, AX, 14)
147 ROUND1(AX, BX, CX, DX, BP, 15)
148
149 ROUND1x(BP, AX, BX, CX, DX, 16)
150 ROUND1x(DX, BP, AX, BX, CX, 17)
151 ROUND1x(CX, DX, BP, AX, BX, 18)
152 ROUND1x(BX, CX, DX, BP, AX, 19)
153
154 ROUND2(AX, BX, CX, DX, BP, 20)
155 ROUND2(BP, AX, BX, CX, DX, 21)
156 ROUND2(DX, BP, AX, BX, CX, 22)
157 ROUND2(CX, DX, BP, AX, BX, 23)
158 ROUND2(BX, CX, DX, BP, AX, 24)
159 ROUND2(AX, BX, CX, DX, BP, 25)
160 ROUND2(BP, AX, BX, CX, DX, 26)
161 ROUND2(DX, BP, AX, BX, CX, 27)
162 ROUND2(CX, DX, BP, AX, BX, 28)
163 ROUND2(BX, CX, DX, BP, AX, 29)
164 ROUND2(AX, BX, CX, DX, BP, 30)
165 ROUND2(BP, AX, BX, CX, DX, 31)
166 ROUND2(DX, BP, AX, BX, CX, 32)
167 ROUND2(CX, DX, BP, AX, BX, 33)
168 ROUND2(BX, CX, DX, BP, AX, 34)
169 ROUND2(AX, BX, CX, DX, BP, 35)
170 ROUND2(BP, AX, BX, CX, DX, 36)
171 ROUND2(DX, BP, AX, BX, CX, 37)
172 ROUND2(CX, DX, BP, AX, BX, 38)
173 ROUND2(BX, CX, DX, BP, AX, 39)
174
175 ROUND3(AX, BX, CX, DX, BP, 40)
176 ROUND3(BP, AX, BX, CX, DX, 41)
177 ROUND3(DX, BP, AX, BX, CX, 42)
178 ROUND3(CX, DX, BP, AX, BX, 43)
179 ROUND3(BX, CX, DX, BP, AX, 44)
180 ROUND3(AX, BX, CX, DX, BP, 45)
181 ROUND3(BP, AX, BX, CX, DX, 46)
182 ROUND3(DX, BP, AX, BX, CX, 47)
183 ROUND3(CX, DX, BP, AX, BX, 48)
184 ROUND3(BX, CX, DX, BP, AX, 49)
185 ROUND3(AX, BX, CX, DX, BP, 50)
186 ROUND3(BP, AX, BX, CX, DX, 51)
187 ROUND3(DX, BP, AX, BX, CX, 52)
188 ROUND3(CX, DX, BP, AX, BX, 53)
189 ROUND3(BX, CX, DX, BP, AX, 54)
190 ROUND3(AX, BX, CX, DX, BP, 55)
191 ROUND3(BP, AX, BX, CX, DX, 56)
192 ROUND3(DX, BP, AX, BX, CX, 57)
193 ROUND3(CX, DX, BP, AX, BX, 58)
194 ROUND3(BX, CX, DX, BP, AX, 59)
195
196 ROUND4(AX, BX, CX, DX, BP, 60)
197 ROUND4(BP, AX, BX, CX, DX, 61)
198 ROUND4(DX, BP, AX, BX, CX, 62)
199 ROUND4(CX, DX, BP, AX, BX, 63)
200 ROUND4(BX, CX, DX, BP, AX, 64)
201 ROUND4(AX, BX, CX, DX, BP, 65)
202 ROUND4(BP, AX, BX, CX, DX, 66)
203 ROUND4(DX, BP, AX, BX, CX, 67)
204 ROUND4(CX, DX, BP, AX, BX, 68)
205 ROUND4(BX, CX, DX, BP, AX, 69)
206 ROUND4(AX, BX, CX, DX, BP, 70)
207 ROUND4(BP, AX, BX, CX, DX, 71)
208 ROUND4(DX, BP, AX, BX, CX, 72)
209 ROUND4(CX, DX, BP, AX, BX, 73)
210 ROUND4(BX, CX, DX, BP, AX, 74)
211 ROUND4(AX, BX, CX, DX, BP, 75)
212 ROUND4(BP, AX, BX, CX, DX, 76)
213 ROUND4(DX, BP, AX, BX, CX, 77)
214 ROUND4(CX, DX, BP, AX, BX, 78)
215 ROUND4(BX, CX, DX, BP, AX, 79)
216
217 ADDL 64(SP), AX
218 ADDL 68(SP), BX
219 ADDL 72(SP), CX
220 ADDL 76(SP), DX
221 ADDL 80(SP), BP
222
223 MOVL 88(SP), SI
224 ADDL $64, SI
225 CMPL SI, 84(SP)
226 JB loop
227
228 end:
229 MOVL dig+0(FP), DI
230 MOVL AX, (0*4)(DI)
231 MOVL BX, (1*4)(DI)
232 MOVL CX, (2*4)(DI)
233 MOVL DX, (3*4)(DI)
234 MOVL BP, (4*4)(DI)
235 RET
236
View as plain text