1 C nettle
, low-level cryptographics library
3 C Copyright
(C
) 2004, 2008 Niels Möller
5 C The nettle library is free software
; you can redistribute it and/or modify
6 C it under the terms of the GNU Lesser General
Public License as published by
7 C the Free Software Foundation
; either version 2.1 of the License, or (at your
8 C option
) any later version.
10 C The nettle library is distributed
in the hope that it will be useful
, but
11 C WITHOUT ANY WARRANTY
; without even the implied warranty of MERCHANTABILITY
12 C
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
Public
13 C License for more details.
15 C You should have received a copy of the GNU Lesser General
Public License
16 C along with the nettle library
; see the file COPYING.LIB. If not, write to
17 C the Free Software Foundation
, Inc.
, 51 Franklin Street
, Fifth Floor
, Boston
,
20 C Register usage. KVALUE
and INPUT share a register.
21 define
(<SA
>,<%eax>)dnl
22 define
(<SB
>,<%r8d
>)dnl
23 define
(<SC
>,<%ecx>)dnl
24 define
(<SD
>,<%edx>)dnl
25 define
(<SE
>,<%r9d
>)dnl
26 define
(<DATA
>,<%rsp
>)dnl
27 define
(<TMP
>,<%r10d
>)dnl
28 define
(<TMP2
>,<%r11d
>)dnl C Used by F3
29 define
(<KVALUE
>, <%esi>)dnl
32 define
(<STATE
>,<%rdi
>)dnl
33 define
(<INPUT
>,<%rsi
>)dnl
36 define
(<K1VALUE
>, <<$
>0x5A827999>)dnl C Rounds
0-19
37 define
(<K2VALUE
>, <<$
>0x6ED9EBA1>)dnl C Rounds
20-39
38 define
(<K3VALUE
>, <<$
>0x8F1BBCDC>)dnl C Rounds
40-59
39 define
(<K4VALUE
>, <<$
>0xCA62C1D6>)dnl C Rounds
60-79
41 C Reads the input
into register
, byteswaps it
, and stores it
in the DATA array.
42 C SWAP
(index
, register
)
44 movl
OFFSET($1)(INPUT
), $2
46 movl
$2, OFFSET($1) (DATA
)
49 C expand
(i
) is the expansion function
51 C W
[i
] = (W
[i
- 16] ^ W
[i
- 14] ^ W
[i
- 8] ^ W
[i
- 3]) <<< 1
53 C where W
[i
] is stored
in DATA
[i
mod 16].
55 C Result is stored back
in W
[i
], and also left
in TMP
, the only
56 C register that is used.
58 movl
OFFSET(eval
($1 % 16)) (DATA
), TMP
59 xorl
OFFSET(eval
(($1 + 2) % 16)) (DATA
), TMP
60 xorl
OFFSET(eval
(($1 + 8) % 16)) (DATA
), TMP
61 xorl
OFFSET(eval
(($1 + 13) % 16)) (DATA
), TMP
63 movl TMP
, OFFSET(eval
($1 % 16)) (DATA
)>)dnl
64 define
(<NOEXPAND
>, <OFFSET($1) (DATA
)>)dnl
68 C f1
(x
,y
,z
) = z ^
(x
& (y ^ z
))
69 C f2
(x
,y
,z
) = x ^ y ^ z
70 C f3
(x
,y
,z
) = (x
& y
) |
(z
& (x | y
))
73 C The
macro Fk
(x
,y
,z
) computes
= fk
(x
,y
,z
).
74 C Result is left
in TMP.
93 C The form of one sha1 round is
95 C a
' = e + a <<< 5 + f( b, c, d ) + k + w;
101 C where <<< denotes rotation. We permute our variables, so that we
104 C e += a <<< 5 + f( b, c, d ) + k + w;
107 C ROUND(a,b,c,d,e,f,w)
110 addl ifelse($7,,TMP,$7), $5
114 C Using the TMP register could be avoided, by rotating $1 in place,
115 C adding, and then rotating back.
121 .file "sha1-compress.asm"
123 C _nettle_sha1_compress(uint32_t *state, uint8_t *input)
127 PROLOGUE(_nettle_sha1_compress)
128 C save all registers that need to be saved
131 sub $68, %rsp C %rsp = W
133 C Load and byteswap data
134 SWAP( 0, SA) SWAP( 1, SB) SWAP( 2, SC) SWAP( 3, SD)
135 SWAP( 4, SA) SWAP( 5, SB) SWAP( 6, SC) SWAP( 7, SD)
136 SWAP( 8, SA) SWAP( 9, SB) SWAP(10, SC) SWAP(11, SD)
137 SWAP(12, SA) SWAP(13, SB) SWAP(14, SC) SWAP(15, SD)
139 C Load the state vector
147 ROUND(SA, SB, SC, SD, SE, <F1>, NOEXPAND( 0))
148 ROUND(SE, SA, SB, SC, SD, <F1>, NOEXPAND( 1))
149 ROUND(SD, SE, SA, SB, SC, <F1>, NOEXPAND( 2))
150 ROUND(SC, SD, SE, SA, SB, <F1>, NOEXPAND( 3))
151 ROUND(SB, SC, SD, SE, SA, <F1>, NOEXPAND( 4))
153 ROUND(SA, SB, SC, SD, SE, <F1>, NOEXPAND( 5))
154 ROUND(SE, SA, SB, SC, SD, <F1>, NOEXPAND( 6))
155 ROUND(SD, SE, SA, SB, SC, <F1>, NOEXPAND( 7))
156 ROUND(SC, SD, SE, SA, SB, <F1>, NOEXPAND( 8))
157 ROUND(SB, SC, SD, SE, SA, <F1>, NOEXPAND( 9))
159 ROUND(SA, SB, SC, SD, SE, <F1>, NOEXPAND(10))
160 ROUND(SE, SA, SB, SC, SD, <F1>, NOEXPAND(11))
161 ROUND(SD, SE, SA, SB, SC, <F1>, NOEXPAND(12))
162 ROUND(SC, SD, SE, SA, SB, <F1>, NOEXPAND(13))
163 ROUND(SB, SC, SD, SE, SA, <F1>, NOEXPAND(14))
165 ROUND(SA, SB, SC, SD, SE, <F1>, NOEXPAND(15))
166 EXPAND(16) ROUND(SE, SA, SB, SC, SD, <F1>)
167 EXPAND(17) ROUND(SD, SE, SA, SB, SC, <F1>)
168 EXPAND(18) ROUND(SC, SD, SE, SA, SB, <F1>)
169 EXPAND(19) ROUND(SB, SC, SD, SE, SA, <F1>)
172 EXPAND(20) ROUND(SA, SB, SC, SD, SE, <F2>)
173 EXPAND(21) ROUND(SE, SA, SB, SC, SD, <F2>)
174 EXPAND(22) ROUND(SD, SE, SA, SB, SC, <F2>)
175 EXPAND(23) ROUND(SC, SD, SE, SA, SB, <F2>)
176 EXPAND(24) ROUND(SB, SC, SD, SE, SA, <F2>)
178 EXPAND(25) ROUND(SA, SB, SC, SD, SE, <F2>)
179 EXPAND(26) ROUND(SE, SA, SB, SC, SD, <F2>)
180 EXPAND(27) ROUND(SD, SE, SA, SB, SC, <F2>)
181 EXPAND(28) ROUND(SC, SD, SE, SA, SB, <F2>)
182 EXPAND(29) ROUND(SB, SC, SD, SE, SA, <F2>)
184 EXPAND(30) ROUND(SA, SB, SC, SD, SE, <F2>)
185 EXPAND(31) ROUND(SE, SA, SB, SC, SD, <F2>)
186 EXPAND(32) ROUND(SD, SE, SA, SB, SC, <F2>)
187 EXPAND(33) ROUND(SC, SD, SE, SA, SB, <F2>)
188 EXPAND(34) ROUND(SB, SC, SD, SE, SA, <F2>)
190 EXPAND(35) ROUND(SA, SB, SC, SD, SE, <F2>)
191 EXPAND(36) ROUND(SE, SA, SB, SC, SD, <F2>)
192 EXPAND(37) ROUND(SD, SE, SA, SB, SC, <F2>)
193 EXPAND(38) ROUND(SC, SD, SE, SA, SB, <F2>)
194 EXPAND(39) ROUND(SB, SC, SD, SE, SA, <F2>)
197 EXPAND(40) ROUND(SA, SB, SC, SD, SE, <F3>)
198 EXPAND(41) ROUND(SE, SA, SB, SC, SD, <F3>)
199 EXPAND(42) ROUND(SD, SE, SA, SB, SC, <F3>)
200 EXPAND(43) ROUND(SC, SD, SE, SA, SB, <F3>)
201 EXPAND(44) ROUND(SB, SC, SD, SE, SA, <F3>)
203 EXPAND(45) ROUND(SA, SB, SC, SD, SE, <F3>)
204 EXPAND(46) ROUND(SE, SA, SB, SC, SD, <F3>)
205 EXPAND(47) ROUND(SD, SE, SA, SB, SC, <F3>)
206 EXPAND(48) ROUND(SC, SD, SE, SA, SB, <F3>)
207 EXPAND(49) ROUND(SB, SC, SD, SE, SA, <F3>)
209 EXPAND(50) ROUND(SA, SB, SC, SD, SE, <F3>)
210 EXPAND(51) ROUND(SE, SA, SB, SC, SD, <F3>)
211 EXPAND(52) ROUND(SD, SE, SA, SB, SC, <F3>)
212 EXPAND(53) ROUND(SC, SD, SE, SA, SB, <F3>)
213 EXPAND(54) ROUND(SB, SC, SD, SE, SA, <F3>)
215 EXPAND(55) ROUND(SA, SB, SC, SD, SE, <F3>)
216 EXPAND(56) ROUND(SE, SA, SB, SC, SD, <F3>)
217 EXPAND(57) ROUND(SD, SE, SA, SB, SC, <F3>)
218 EXPAND(58) ROUND(SC, SD, SE, SA, SB, <F3>)
219 EXPAND(59) ROUND(SB, SC, SD, SE, SA, <F3>)
222 EXPAND(60) ROUND(SA, SB, SC, SD, SE, <F2>)
223 EXPAND(61) ROUND(SE, SA, SB, SC, SD, <F2>)
224 EXPAND(62) ROUND(SD, SE, SA, SB, SC, <F2>)
225 EXPAND(63) ROUND(SC, SD, SE, SA, SB, <F2>)
226 EXPAND(64) ROUND(SB, SC, SD, SE, SA, <F2>)
228 EXPAND(65) ROUND(SA, SB, SC, SD, SE, <F2>)
229 EXPAND(66) ROUND(SE, SA, SB, SC, SD, <F2>)
230 EXPAND(67) ROUND(SD, SE, SA, SB, SC, <F2>)
231 EXPAND(68) ROUND(SC, SD, SE, SA, SB, <F2>)
232 EXPAND(69) ROUND(SB, SC, SD, SE, SA, <F2>)
234 EXPAND(70) ROUND(SA, SB, SC, SD, SE, <F2>)
235 EXPAND(71) ROUND(SE, SA, SB, SC, SD, <F2>)
236 EXPAND(72) ROUND(SD, SE, SA, SB, SC, <F2>)
237 EXPAND(73) ROUND(SC, SD, SE, SA, SB, <F2>)
238 EXPAND(74) ROUND(SB, SC, SD, SE, SA, <F2>)
240 EXPAND(75) ROUND(SA, SB, SC, SD, SE, <F2>)
241 EXPAND(76) ROUND(SE, SA, SB, SC, SD, <F2>)
242 EXPAND(77) ROUND(SD, SE, SA, SB, SC, <F2>)
243 EXPAND(78) ROUND(SC, SD, SE, SA, SB, <F2>)
244 EXPAND(79) ROUND(SB, SC, SD, SE, SA, <F2>)
246 C Update the state vector
256 EPILOGUE(_nettle_sha1_compress)