sha1-compress.asm 7.54 KB
Newer Older
1
2
C nettle, low-level cryptographics library
C 
3
C Copyright (C) 2004, 2008, 2013 Niels Möller
4
5
6
7
8
9
10
11
12
13
14
15
16
C  
C The nettle library is free software; you can redistribute it and/or modify
C it under the terms of the GNU Lesser General Public License as published by
C the Free Software Foundation; either version 2.1 of the License, or (at your
C option) any later version.
C 
C The nettle library is distributed in the hope that it will be useful, but
C WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
C or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
C License for more details.
C 
C You should have received a copy of the GNU Lesser General Public License
C along with the nettle library; see the file COPYING.LIB.  If not, write to
17
18
C the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
C MA 02111-1301, USA.
19

20
C Register usage. KVALUE and INPUT share a register.
21
define(<SA>,<%eax>)dnl
22
define(<SB>,<%r8d>)dnl
23
24
define(<SC>,<%ecx>)dnl
define(<SD>,<%edx>)dnl
25
define(<SE>,<%r9d>)dnl
26
define(<DATA>,<%rsp>)dnl
27
28
define(<T1>,<%r10d>)dnl
define(<T2>,<%r11d>)dnl
29
define(<KVALUE>, <%esi>)dnl			
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

C Arguments
define(<STATE>,<%rdi>)dnl
define(<INPUT>,<%rsi>)dnl

C Constants
define(<K1VALUE>, <<$>0x5A827999>)dnl		C  Rounds  0-19
define(<K2VALUE>, <<$>0x6ED9EBA1>)dnl		C  Rounds 20-39
define(<K3VALUE>, <<$>0x8F1BBCDC>)dnl		C  Rounds 40-59
define(<K4VALUE>, <<$>0xCA62C1D6>)dnl		C  Rounds 60-79
	
C Reads the input into register, byteswaps it, and stores it in the DATA array.
C SWAP(index, register)
define(<SWAP>, <
	movl	OFFSET($1)(INPUT), $2
	bswap	$2
	movl	$2, OFFSET($1) (DATA)
>)dnl

C The f functions,
C
C  f1(x,y,z) = z ^ (x & (y ^ z))
C  f2(x,y,z) = x ^ y ^ z
C  f3(x,y,z) = (x & y) | (z & (x | y))
54
C	     = (x & (y ^ z)) + (y & z)
55
C  f4 = f2
56
57
58
59
60

C This form for f3 was suggested by George Spelvin. The terms can be
C added into the result one at a time, saving one temporary.

C expand(i) is the expansion function
61
C
62
63
64
C   W[i] = (W[i - 16] ^ W[i - 14] ^ W[i - 8] ^ W[i - 3]) <<< 1
C
C where W[i] is stored in DATA[i mod 16].
65
66
67
68
69
70
71
72
73
74
75
76
77
78

C The form of one sha1 round is
C
C   a' = e + a <<< 5 + f( b, c, d ) + k + w;
C   b' = a;
C   c' = b <<< 30;
C   d' = c;
C   e' = d;
C
C where <<< denotes rotation. We permute our variables, so that we
C instead get
C
C   e += a <<< 5 + f( b, c, d ) + k + w;
C   b <<<= 30
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

dnl ROUND_F1(a, b, c, d, e, i)
define(<ROUND_F1>, <
	movl	OFFSET(eval($6 % 16)) (DATA), T1
	xorl	OFFSET(eval(($6 +  2) % 16)) (DATA), T1
	xorl	OFFSET(eval(($6 +  8) % 16)) (DATA), T1
	xorl	OFFSET(eval(($6 + 13) % 16)) (DATA), T1
	roll	<$>1, T1
	movl	T1, OFFSET(eval($6 % 16)) (DATA)
	movl	$4, T2
	xorl	$3, T2
	andl	$2, T2
	xorl	$4, T2
	roll	<$>30, $2
	addl	T1, $5
	addl	KVALUE, $5
	movl	$1, T1
	roll	<$>5, T1
	addl	T1, $5
	addl	T2, $5
>)

dnl ROUND_F1_NOEXP(a, b, c, d, e, i)
define(<ROUND_F1_NOEXP>, <
	movl	$4, T2
	xorl	$3, T2
	movl	$1, T1
	andl	$2, T2
	addl	OFFSET($6) (DATA), $5
	xorl	$4, T2
	addl	T2, $5
	roll	<$>30, $2
	roll	<$>5, T1
	addl	T1, $5
113
	addl	KVALUE, $5
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
dnl ROUND_F2(a, b, c, d, e, i)
define(<ROUND_F2>, <
	movl	OFFSET(eval($6 % 16)) (DATA), T1
	xorl	OFFSET(eval(($6 +  2) % 16)) (DATA), T1
	xorl	OFFSET(eval(($6 +  8) % 16)) (DATA), T1
	xorl	OFFSET(eval(($6 + 13) % 16)) (DATA), T1
	roll	<$>1, T1
	movl	T1, OFFSET(eval($6 % 16)) (DATA)
	movl	$4, T2
	xorl	$3, T2
	xorl	$2, T2
	roll	<$>30, $2
	addl	T1, $5
	addl	KVALUE, $5
	movl	$1, T1
	roll	<$>5, T1
	addl	T1, $5
	addl	T2, $5
>)

dnl ROUND_F3(a, b, c, d, e, i)
define(<ROUND_F3>, <
	movl	OFFSET(eval($6 % 16)) (DATA), T1
	xorl	OFFSET(eval(($6 +  2) % 16)) (DATA), T1
	xorl	OFFSET(eval(($6 +  8) % 16)) (DATA), T1
	xorl	OFFSET(eval(($6 + 13) % 16)) (DATA), T1
	roll	<$>1, T1
	movl	T1, OFFSET(eval($6 % 16)) (DATA)
	movl	$4, T2
	andl	$3, T2
	addl	T1, $5
 	addl	KVALUE, $5
	movl	$4, T1
	xorl	$3, T1
	andl	$2, T1
	addl	T2, $5
	roll	<$>30, $2
	movl	$1, T2
	roll	<$>5, T2
	addl	T1, $5
	addl	T2, $5
>)
158
159
160
161
162
163

	.file "sha1-compress.asm"

	C _nettle_sha1_compress(uint32_t *state, uint8_t *input)
	
	.text
164
	ALIGN(16)
165
166
PROLOGUE(_nettle_sha1_compress)
	C save all registers that need to be saved
167
	W64_ENTRY(2, 0)
168
	
169
	sub	$64, %rsp	C  %rsp = W
170
171

	C Load and byteswap data
172
173
174
175
	SWAP( 0, SA) SWAP( 1, SB) SWAP( 2, SC) SWAP( 3, SD)
	SWAP( 4, SA) SWAP( 5, SB) SWAP( 6, SC) SWAP( 7, SD)
	SWAP( 8, SA) SWAP( 9, SB) SWAP(10, SC) SWAP(11, SD)
	SWAP(12, SA) SWAP(13, SB) SWAP(14, SC) SWAP(15, SD)
176
177
178
179
180
181
182
183
184

	C Load the state vector
	movl	  (STATE), SA
	movl	 4(STATE), SB
	movl	 8(STATE), SC
	movl	12(STATE), SD
	movl	16(STATE), SE

	movl	K1VALUE, KVALUE
185
186
187
188
189
	ROUND_F1_NOEXP(SA, SB, SC, SD, SE,  0)
	ROUND_F1_NOEXP(SE, SA, SB, SC, SD,  1)
	ROUND_F1_NOEXP(SD, SE, SA, SB, SC,  2)
	ROUND_F1_NOEXP(SC, SD, SE, SA, SB,  3)
	ROUND_F1_NOEXP(SB, SC, SD, SE, SA,  4)
190

191
192
193
194
195
	ROUND_F1_NOEXP(SA, SB, SC, SD, SE,  5)
	ROUND_F1_NOEXP(SE, SA, SB, SC, SD,  6)
	ROUND_F1_NOEXP(SD, SE, SA, SB, SC,  7)
	ROUND_F1_NOEXP(SC, SD, SE, SA, SB,  8)
	ROUND_F1_NOEXP(SB, SC, SD, SE, SA,  9)
196

197
198
199
200
201
	ROUND_F1_NOEXP(SA, SB, SC, SD, SE, 10)
	ROUND_F1_NOEXP(SE, SA, SB, SC, SD, 11)
	ROUND_F1_NOEXP(SD, SE, SA, SB, SC, 12)
	ROUND_F1_NOEXP(SC, SD, SE, SA, SB, 13)
	ROUND_F1_NOEXP(SB, SC, SD, SE, SA, 14)
202

203
204
205
206
207
	ROUND_F1_NOEXP(SA, SB, SC, SD, SE, 15)
	ROUND_F1(SE, SA, SB, SC, SD, 16)
	ROUND_F1(SD, SE, SA, SB, SC, 17)
	ROUND_F1(SC, SD, SE, SA, SB, 18)
	ROUND_F1(SB, SC, SD, SE, SA, 19)
208
209

	movl	K2VALUE, KVALUE
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
	ROUND_F2(SA, SB, SC, SD, SE, 20)
	ROUND_F2(SE, SA, SB, SC, SD, 21)
	ROUND_F2(SD, SE, SA, SB, SC, 22)
	ROUND_F2(SC, SD, SE, SA, SB, 23)
	ROUND_F2(SB, SC, SD, SE, SA, 24)
				     
	ROUND_F2(SA, SB, SC, SD, SE, 25)
	ROUND_F2(SE, SA, SB, SC, SD, 26)
	ROUND_F2(SD, SE, SA, SB, SC, 27)
	ROUND_F2(SC, SD, SE, SA, SB, 28)
	ROUND_F2(SB, SC, SD, SE, SA, 29)
				     
	ROUND_F2(SA, SB, SC, SD, SE, 30)
	ROUND_F2(SE, SA, SB, SC, SD, 31)
	ROUND_F2(SD, SE, SA, SB, SC, 32)
	ROUND_F2(SC, SD, SE, SA, SB, 33)
	ROUND_F2(SB, SC, SD, SE, SA, 34)
				     
	ROUND_F2(SA, SB, SC, SD, SE, 35)
	ROUND_F2(SE, SA, SB, SC, SD, 36)
	ROUND_F2(SD, SE, SA, SB, SC, 37)
	ROUND_F2(SC, SD, SE, SA, SB, 38)
	ROUND_F2(SB, SC, SD, SE, SA, 39)
233
234

	movl	K3VALUE, KVALUE
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
	ROUND_F3(SA, SB, SC, SD, SE, 40)
	ROUND_F3(SE, SA, SB, SC, SD, 41)
	ROUND_F3(SD, SE, SA, SB, SC, 42)
	ROUND_F3(SC, SD, SE, SA, SB, 43)
	ROUND_F3(SB, SC, SD, SE, SA, 44)
				     
	ROUND_F3(SA, SB, SC, SD, SE, 45)
	ROUND_F3(SE, SA, SB, SC, SD, 46)
	ROUND_F3(SD, SE, SA, SB, SC, 47)
	ROUND_F3(SC, SD, SE, SA, SB, 48)
	ROUND_F3(SB, SC, SD, SE, SA, 49)
				     
	ROUND_F3(SA, SB, SC, SD, SE, 50)
	ROUND_F3(SE, SA, SB, SC, SD, 51)
	ROUND_F3(SD, SE, SA, SB, SC, 52)
	ROUND_F3(SC, SD, SE, SA, SB, 53)
	ROUND_F3(SB, SC, SD, SE, SA, 54)
				     
	ROUND_F3(SA, SB, SC, SD, SE, 55)
	ROUND_F3(SE, SA, SB, SC, SD, 56)
	ROUND_F3(SD, SE, SA, SB, SC, 57)
	ROUND_F3(SC, SD, SE, SA, SB, 58)
	ROUND_F3(SB, SC, SD, SE, SA, 59)
258
259

	movl	K4VALUE, KVALUE
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
	ROUND_F2(SA, SB, SC, SD, SE, 60)
	ROUND_F2(SE, SA, SB, SC, SD, 61)
	ROUND_F2(SD, SE, SA, SB, SC, 62)
	ROUND_F2(SC, SD, SE, SA, SB, 63)
	ROUND_F2(SB, SC, SD, SE, SA, 64)
				     
	ROUND_F2(SA, SB, SC, SD, SE, 65)
	ROUND_F2(SE, SA, SB, SC, SD, 66)
	ROUND_F2(SD, SE, SA, SB, SC, 67)
	ROUND_F2(SC, SD, SE, SA, SB, 68)
	ROUND_F2(SB, SC, SD, SE, SA, 69)
				     
	ROUND_F2(SA, SB, SC, SD, SE, 70)
	ROUND_F2(SE, SA, SB, SC, SD, 71)
	ROUND_F2(SD, SE, SA, SB, SC, 72)
	ROUND_F2(SC, SD, SE, SA, SB, 73)
	ROUND_F2(SB, SC, SD, SE, SA, 74)
				     
	ROUND_F2(SA, SB, SC, SD, SE, 75)
	ROUND_F2(SE, SA, SB, SC, SD, 76)
	ROUND_F2(SD, SE, SA, SB, SC, 77)
	ROUND_F2(SC, SD, SE, SA, SB, 78)
	ROUND_F2(SB, SC, SD, SE, SA, 79)
283
284
285
286
287
288
289
290

	C Update the state vector
	addl	SA,   (STATE) 
	addl	SB,  4(STATE) 
	addl	SC,  8(STATE) 
	addl	SD, 12(STATE) 
	addl	SE, 16(STATE)

291
	add	$64, %rsp
292
	W64_EXIT(2, 0)
293
294
	ret
EPILOGUE(_nettle_sha1_compress)