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
302
303
304
305
306
307
308
309
310
311
312
313
314
|
/* $OpenBSD: sha1_amd64_generic.S,v 1.2 2025/01/18 02:56:07 jsing Exp $ */
/*
* Copyright (c) 2024 Joel Sing <jsing@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifdef __CET__
#include <cet.h>
#else
#define _CET_ENDBR
#endif
#define ctx %rdi
#define in %rsi
#define num %rdx
#define end %rbp
#define hs0 %r8d
#define hs1 %r9d
#define hs2 %r10d
#define hs3 %r11d
#define hs4 %r12d
#define tmp0 %eax
#define tmp1 %ebx
#define tmp2 %ecx
#define tmp3 %edx
/*
* Load message into wt, storing a copy in the message schedule:
*
* Wt = Mt
*/
#define sha1_message_schedule_load(idx, m, w, wt) \
movl ((idx&0xf)*4)(m), wt; \
bswapl wt; \
movl wt, ((idx&0xf)*4)(w);
/*
* Update message schedule and return current value in wt:
*
* W0 = rol(W13 ^ W8 ^ W2 ^ W0, 1)
*/
#define sha1_message_schedule_update(idx, w, wt) \
movl (((idx-3)&0xf)*4)(w), wt; /* W13 */ \
xorl (((idx-8)&0xf)*4)(w), wt; /* W8 */ \
xorl (((idx-14)&0xf)*4)(w), wt; /* W2 */ \
xorl (((idx)&0xf)*4)(w), wt; /* W0 */ \
roll $1, wt; \
\
movl wt, ((idx&0xf)*4)(w);
/*
* Compute a SHA-1 round without logic function:
*
* T = rol(a, 5) + e + Kt + Wt
*
* The caller is required to compute the appropriate logic function
* (Ch, Maj, Parity) and add it to e.
*
* Upon completion b = rol(b, 30), e = T, pending rotation.
*/
#define sha1_round(a, b, c, d, e, kt, wt) \
leal kt(wt, e, 1), e; /* Kt + Wt */ \
\
movl a, tmp1; /* rol(a, 5) */ \
roll $5, tmp1; \
addl tmp1, e; \
\
roll $30, b; /* rol(b, 30) */
/*
* Compute a SHA-1 round with Ch:
*
* T = rol(a, 5) + Ch(b, c, d) + e + Kt + Wt
*
* Ch(x, y, z) = (x & y) ^ (~x & z) = ((y ^ z) & x) ^ z
*
* Upon completion b = rol(b, 30), e = T, pending rotation.
*/
#define sha1_round_ch(a, b, c, d, e, kt, wt) \
movl c, tmp2; /* Ch */ \
xorl d, tmp2; /* Ch */ \
andl b, tmp2; /* Ch */ \
xorl d, tmp2; /* Ch */ \
addl tmp2, e; /* Ch */ \
\
sha1_round(a, b, c, d, e, kt, wt);
/*
* Compute a SHA-1 round with Parity:
*
* T = rol(a, 5) + Parity(b, c, d) + e + Kt + Wt
*
* Parity(x, y, z) = x ^ y ^ z
*
* Upon completion b = rol(b, 30), e = T, pending rotation.
*/
#define sha1_round_parity(a, b, c, d, e, kt, wt) \
movl b, tmp2; /* Parity */ \
xorl c, tmp2; /* Parity */ \
xorl d, tmp2; /* Parity */ \
addl tmp2, e; /* Parity */ \
\
sha1_round(a, b, c, d, e, kt, wt);
/*
* Compute a SHA-1 round with Maj:
*
* T = rol(a, 5) + Maj(b, c, d) + e + Kt + Wt
*
* Maj(x, y, z) = (x & y) ^ (x & z) ^ (y & z) = ((y ^ z) & x) ^ (y & z)
*
* Upon completion b = rol(b, 30), e = T, pending rotation.
*/
#define sha1_round_maj(a, b, c, d, e, kt, wt) \
movl c, tmp2; /* Maj */ \
xorl d, tmp2; /* Maj */ \
andl b, tmp2; /* Maj */ \
movl c, tmp3; /* Maj */ \
andl d, tmp3; /* Maj */ \
xorl tmp2, tmp3; /* Maj */ \
addl tmp3, e; /* Maj */ \
\
sha1_round(a, b, c, d, e, kt, wt);
#define sha1_round1_load(idx, a, b, c, d, e) \
sha1_message_schedule_load(idx, in, %rsp, tmp0) \
sha1_round_ch(a, b, c, d, e, 0x5a827999, tmp0)
#define sha1_round1_update(idx, a, b, c, d, e) \
sha1_message_schedule_update(idx, %rsp, tmp0) \
sha1_round_ch(a, b, c, d, e, 0x5a827999, tmp0)
#define sha1_round2_update(idx, a, b, c, d, e) \
sha1_message_schedule_update(idx, %rsp, tmp0) \
sha1_round_parity(a, b, c, d, e, 0x6ed9eba1, tmp0)
#define sha1_round3_update(idx, a, b, c, d, e) \
sha1_message_schedule_update(idx, %rsp, tmp0) \
sha1_round_maj(a, b, c, d, e, 0x8f1bbcdc, tmp0)
#define sha1_round4_update(idx, a, b, c, d, e) \
sha1_message_schedule_update(idx, %rsp, tmp0) \
sha1_round_parity(a, b, c, d, e, 0xca62c1d6, tmp0)
.text
/*
* void sha1_block_generic(SHA1_CTX *ctx, const void *in, size_t num);
*
* Standard x86-64 ABI: rdi = ctx, rsi = in, rdx = num
*/
.align 16
.globl sha1_block_generic
.type sha1_block_generic,@function
sha1_block_generic:
_CET_ENDBR
/* Save callee save registers. */
pushq %rbx
pushq %rbp
pushq %r12
/* Allocate space for message schedule. */
movq %rsp, %rax
subq $(64+1*8), %rsp
andq $~63, %rsp
movq %rax, (64+0*8)(%rsp)
/* Compute end of message. */
shlq $6, num
leaq (in, num, 1), end
/* Load current hash state from context. */
movl (0*4)(ctx), hs0
movl (1*4)(ctx), hs1
movl (2*4)(ctx), hs2
movl (3*4)(ctx), hs3
movl (4*4)(ctx), hs4
jmp .Lblock_loop
.align 16
.Lblock_loop:
/* Round 0 through 15. */
sha1_round1_load(0, hs0, hs1, hs2, hs3, hs4)
sha1_round1_load(1, hs4, hs0, hs1, hs2, hs3)
sha1_round1_load(2, hs3, hs4, hs0, hs1, hs2)
sha1_round1_load(3, hs2, hs3, hs4, hs0, hs1)
sha1_round1_load(4, hs1, hs2, hs3, hs4, hs0)
sha1_round1_load(5, hs0, hs1, hs2, hs3, hs4)
sha1_round1_load(6, hs4, hs0, hs1, hs2, hs3)
sha1_round1_load(7, hs3, hs4, hs0, hs1, hs2)
sha1_round1_load(8, hs2, hs3, hs4, hs0, hs1)
sha1_round1_load(9, hs1, hs2, hs3, hs4, hs0)
sha1_round1_load(10, hs0, hs1, hs2, hs3, hs4)
sha1_round1_load(11, hs4, hs0, hs1, hs2, hs3)
sha1_round1_load(12, hs3, hs4, hs0, hs1, hs2)
sha1_round1_load(13, hs2, hs3, hs4, hs0, hs1)
sha1_round1_load(14, hs1, hs2, hs3, hs4, hs0)
sha1_round1_load(15, hs0, hs1, hs2, hs3, hs4)
/* Round 16 through 31. */
sha1_round1_update(16, hs4, hs0, hs1, hs2, hs3);
sha1_round1_update(17, hs3, hs4, hs0, hs1, hs2);
sha1_round1_update(18, hs2, hs3, hs4, hs0, hs1);
sha1_round1_update(19, hs1, hs2, hs3, hs4, hs0);
sha1_round2_update(20, hs0, hs1, hs2, hs3, hs4);
sha1_round2_update(21, hs4, hs0, hs1, hs2, hs3);
sha1_round2_update(22, hs3, hs4, hs0, hs1, hs2);
sha1_round2_update(23, hs2, hs3, hs4, hs0, hs1);
sha1_round2_update(24, hs1, hs2, hs3, hs4, hs0);
sha1_round2_update(25, hs0, hs1, hs2, hs3, hs4);
sha1_round2_update(26, hs4, hs0, hs1, hs2, hs3);
sha1_round2_update(27, hs3, hs4, hs0, hs1, hs2);
sha1_round2_update(28, hs2, hs3, hs4, hs0, hs1);
sha1_round2_update(29, hs1, hs2, hs3, hs4, hs0);
sha1_round2_update(30, hs0, hs1, hs2, hs3, hs4);
sha1_round2_update(31, hs4, hs0, hs1, hs2, hs3);
/* Round 32 through 47. */
sha1_round2_update(32, hs3, hs4, hs0, hs1, hs2);
sha1_round2_update(33, hs2, hs3, hs4, hs0, hs1);
sha1_round2_update(34, hs1, hs2, hs3, hs4, hs0);
sha1_round2_update(35, hs0, hs1, hs2, hs3, hs4);
sha1_round2_update(36, hs4, hs0, hs1, hs2, hs3);
sha1_round2_update(37, hs3, hs4, hs0, hs1, hs2);
sha1_round2_update(38, hs2, hs3, hs4, hs0, hs1);
sha1_round2_update(39, hs1, hs2, hs3, hs4, hs0);
sha1_round3_update(40, hs0, hs1, hs2, hs3, hs4);
sha1_round3_update(41, hs4, hs0, hs1, hs2, hs3);
sha1_round3_update(42, hs3, hs4, hs0, hs1, hs2);
sha1_round3_update(43, hs2, hs3, hs4, hs0, hs1);
sha1_round3_update(44, hs1, hs2, hs3, hs4, hs0);
sha1_round3_update(45, hs0, hs1, hs2, hs3, hs4);
sha1_round3_update(46, hs4, hs0, hs1, hs2, hs3);
sha1_round3_update(47, hs3, hs4, hs0, hs1, hs2);
/* Round 48 through 63. */
sha1_round3_update(48, hs2, hs3, hs4, hs0, hs1);
sha1_round3_update(49, hs1, hs2, hs3, hs4, hs0);
sha1_round3_update(50, hs0, hs1, hs2, hs3, hs4);
sha1_round3_update(51, hs4, hs0, hs1, hs2, hs3);
sha1_round3_update(52, hs3, hs4, hs0, hs1, hs2);
sha1_round3_update(53, hs2, hs3, hs4, hs0, hs1);
sha1_round3_update(54, hs1, hs2, hs3, hs4, hs0);
sha1_round3_update(55, hs0, hs1, hs2, hs3, hs4);
sha1_round3_update(56, hs4, hs0, hs1, hs2, hs3);
sha1_round3_update(57, hs3, hs4, hs0, hs1, hs2);
sha1_round3_update(58, hs2, hs3, hs4, hs0, hs1);
sha1_round3_update(59, hs1, hs2, hs3, hs4, hs0);
sha1_round4_update(60, hs0, hs1, hs2, hs3, hs4);
sha1_round4_update(61, hs4, hs0, hs1, hs2, hs3);
sha1_round4_update(62, hs3, hs4, hs0, hs1, hs2);
sha1_round4_update(63, hs2, hs3, hs4, hs0, hs1);
/* Round 64 through 79. */
sha1_round4_update(64, hs1, hs2, hs3, hs4, hs0);
sha1_round4_update(65, hs0, hs1, hs2, hs3, hs4);
sha1_round4_update(66, hs4, hs0, hs1, hs2, hs3);
sha1_round4_update(67, hs3, hs4, hs0, hs1, hs2);
sha1_round4_update(68, hs2, hs3, hs4, hs0, hs1);
sha1_round4_update(69, hs1, hs2, hs3, hs4, hs0);
sha1_round4_update(70, hs0, hs1, hs2, hs3, hs4);
sha1_round4_update(71, hs4, hs0, hs1, hs2, hs3);
sha1_round4_update(72, hs3, hs4, hs0, hs1, hs2);
sha1_round4_update(73, hs2, hs3, hs4, hs0, hs1);
sha1_round4_update(74, hs1, hs2, hs3, hs4, hs0);
sha1_round4_update(75, hs0, hs1, hs2, hs3, hs4);
sha1_round4_update(76, hs4, hs0, hs1, hs2, hs3);
sha1_round4_update(77, hs3, hs4, hs0, hs1, hs2);
sha1_round4_update(78, hs2, hs3, hs4, hs0, hs1);
sha1_round4_update(79, hs1, hs2, hs3, hs4, hs0);
/* Add intermediate state to hash state. */
addl (0*4)(ctx), hs0
addl (1*4)(ctx), hs1
addl (2*4)(ctx), hs2
addl (3*4)(ctx), hs3
addl (4*4)(ctx), hs4
/* Store new hash state to context. */
movl hs0, (0*4)(ctx)
movl hs1, (1*4)(ctx)
movl hs2, (2*4)(ctx)
movl hs3, (3*4)(ctx)
movl hs4, (4*4)(ctx)
addq $64, in
cmpq end, in
jb .Lblock_loop
movq (64+0*8)(%rsp), %rsp
/* Restore callee save registers. */
popq %r12
popq %rbp
popq %rbx
ret
|