Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3532 dunkaist 1
;    libcrash -- cryptographic hash functions
2
;
3
;    Copyright (C) 2013 Ivan Baravy (dunkaist)
4
;
5
;    This program is free software: you can redistribute it and/or modify
6
;    it under the terms of the GNU General Public License as published by
7
;    the Free Software Foundation, either version 3 of the License, or
8
;    (at your option) any later version.
9
;
10
;    This program is distributed in the hope that it will be useful,
11
;    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
;    GNU General Public License for more details.
14
;
15
;    You should have received a copy of the GNU General Public License
16
;    along with this program.  If not, see .
17
 
18
macro keccak_rol_xor nd, ncl, ncr
19
{
20
	movq	mm0, [C + 8*(ncl)]
21
	movq	mm1, mm0
22
	psllq	mm0, 1
23
	psrlq	mm1, 63
24
	por	mm0, mm1
25
	pxor	mm0, [C + 8*(ncr)]
26
	movq	[D + 8*(nd)], mm0
27
}
28
 
29
proc keccak_theta
30
locals
31
	C	rq 5
32
	D	rq 5
33
endl
34
 
35
repeat 5
36
	movq	mm0, [edi + 8*(%-1 +  0)]
37
	pxor	mm0, [edi + 8*(%-1 +  5)]
38
	pxor	mm0, [edi + 8*(%-1 + 10)]
39
	pxor	mm0, [edi + 8*(%-1 + 15)]
40
	pxor	mm0, [edi + 8*(%-1 + 20)]
41
	movq	[C + 8*(%-1)], mm0
42
end repeat
43
 
44
	keccak_rol_xor	0, 1, 4
45
	keccak_rol_xor	1, 2, 0
46
	keccak_rol_xor	2, 3, 1
47
	keccak_rol_xor	3, 4, 2
48
	keccak_rol_xor	4, 0, 3
49
 
50
repeat 5
51
	movq	mm1, [D + 8*(%-1)]
52
	movq	mm0, mm1
53
	pxor	mm0, [edi + 8*(%-1 +  0)]
54
	movq	[edi + 8*(%-1 +  0)], mm0
55
	movq	mm0, mm1
56
	pxor	mm0, [edi + 8*(%-1 +  5)]
57
	movq	[edi + 8*(%-1 +  5)], mm0
58
	movq	mm0, mm1
59
	pxor	mm0, [edi + 8*(%-1 + 10)]
60
	movq	[edi + 8*(%-1 + 10)], mm0
61
	movq	mm0, mm1
62
	pxor	mm0, [edi + 8*(%-1 + 15)]
63
	movq	[edi + 8*(%-1 + 15)], mm0
64
	movq	mm0, mm1
65
	pxor	mm0, [edi + 8*(%-1 + 20)]
66
	movq	[edi + 8*(%-1 + 20)], mm0
67
end repeat
68
 
69
	ret
70
endp
71
 
72
 
73
proc keccak_pi
74
	movq	mm1, [edi + 8*1]
75
	movq	mm0, [edi + 8*6]
76
	movq	[edi + 8*1], mm0
77
	movq	mm0, [edi + 8*9]
78
	movq	[edi + 8*6], mm0
79
	movq	mm0, [edi + 8*22]
80
	movq	[edi + 8*9], mm0
81
	movq	mm0, [edi + 8*14]
82
	movq	[edi + 8*22], mm0
83
	movq	mm0, [edi + 8*20]
84
	movq	[edi + 8*14], mm0
85
	movq	mm0, [edi + 8*2]
86
	movq	[edi + 8*20], mm0
87
	movq	mm0, [edi + 8*12]
88
	movq	[edi + 8*2], mm0
89
	movq	mm0, [edi + 8*13]
90
	movq	[edi + 8*12], mm0
91
	movq	mm0, [edi + 8*19]
92
	movq	[edi + 8*13], mm0
93
	movq	mm0, [edi + 8*23]
94
	movq	[edi + 8*19], mm0
95
	movq	mm0, [edi + 8*15]
96
	movq	[edi + 8*23], mm0
97
	movq	mm0, [edi + 8*4]
98
	movq	[edi + 8*15], mm0
99
	movq	mm0, [edi + 8*24]
100
	movq	[edi + 8*4], mm0
101
	movq	mm0, [edi + 8*21]
102
	movq	[edi + 8*24], mm0
103
	movq	mm0, [edi + 8*8]
104
	movq	[edi + 8*21], mm0
105
	movq	mm0, [edi + 8*16]
106
	movq	[edi + 8*8], mm0
107
	movq	mm0, [edi + 8*5]
108
	movq	[edi + 8*16], mm0
109
	movq	mm0, [edi + 8*3]
110
	movq	[edi + 8*5], mm0
111
	movq	mm0, [edi + 8*18]
112
	movq	[edi + 8*3], mm0
113
	movq	mm0, [edi + 8*17]
114
	movq	[edi + 8*18], mm0
115
	movq	mm0, [edi + 8*11]
116
	movq	[edi + 8*17], mm0
117
	movq	mm0, [edi + 8*7]
118
	movq	[edi + 8*11], mm0
119
	movq	mm0, [edi + 8*10]
120
	movq	[edi + 8*7], mm0
121
	movq	[edi + 8*10], mm1
122
 
123
	ret
124
endp
125
 
126
 
127
proc keccak_chi
128
 
129
	mov	eax, 0xffffffff
130
	movd	mm0, eax
131
	movq	mm2, mm0
132
	punpckldq	mm2, mm0
133
 
134
repeat 5
135
	movq	mm6, [edi + 8*(0 + 5*(%-1))]
136
	movq	mm7, [edi + 8*(1 + 5*(%-1))]
137
 
138
	movq	mm0, [edi + 8*(0 + 5*(%-1))]
139
	movq	mm1, mm7
140
	pandn	mm1, mm2
141
	pand	mm1, [edi + 8*(2 + 5*(%-1))]
142
	pxor	mm0, mm1
143
	movq	[edi + 8*(0 + 5*(%-1))], mm0
144
 
145
	movq	mm0, [edi + 8*(1 + 5*(%-1))]
146
	movq	mm1, [edi + 8*(2 + 5*(%-1))]
147
	pandn	mm1, mm2
148
	pand	mm1, [edi + 8*(3 + 5*(%-1))]
149
	pxor	mm0, mm1
150
	movq	[edi + 8*(1 + 5*(%-1))], mm0
151
 
152
	movq	mm0, [edi + 8*(2 + 5*(%-1))]
153
	movq	mm1, [edi + 8*(3 + 5*(%-1))]
154
	pandn	mm1, mm2
155
	pand	mm1, [edi + 8*(4 + 5*(%-1))]
156
	pxor	mm0, mm1
157
	movq	[edi + 8*(2 + 5*(%-1))], mm0
158
 
159
	movq	mm0, [edi + 8*(3 + 5*(%-1))]
160
	movq	mm1, [edi + 8*(4 + 5*(%-1))]
161
	pandn	mm1, mm2
162
	pand	mm1, mm6
163
	pxor	mm0, mm1
164
	movq	[edi + 8*(3 + 5*(%-1))], mm0
165
 
166
	movq	mm0, [edi + 8*(4 + 5*(%-1))]
167
	movq	mm1, mm6
168
	pandn	mm1, mm2
169
	pand	mm1, mm7
170
	pxor	mm0, mm1
171
	movq	[edi + 8*(4 + 5*(%-1))], mm0
172
end repeat
173
	ret
174
endp
175
 
176
 
177
macro keccak_rol_mov n, c
178
{
179
	movq	mm0, [edi + 8*(n)]
180
	movq	mm1, mm0
181
	psllq	mm0, (c)
182
	psrlq	mm1, (64-(c))
183
	por	mm0, mm1
184
	movq	[edi + 8*(n)], mm0
185
}
186
 
187
proc keccak_permutation
188
 
189
repeat 24
190
	stdcall	keccak_theta
191
 
192
	keccak_rol_mov	 1,  1
193
	keccak_rol_mov	 2, 62
194
	keccak_rol_mov	 3, 28
195
	keccak_rol_mov	 4, 27
196
	keccak_rol_mov	 5, 36
197
	keccak_rol_mov	 6, 44
198
	keccak_rol_mov	 7,  6
199
	keccak_rol_mov	 8, 55
200
	keccak_rol_mov	 9, 20
201
	keccak_rol_mov	10,  3
202
	keccak_rol_mov	11, 10
203
	keccak_rol_mov	12, 43
204
	keccak_rol_mov	13, 25
205
	keccak_rol_mov	14, 39
206
	keccak_rol_mov	15, 41
207
	keccak_rol_mov	16, 45
208
	keccak_rol_mov	17, 15
209
	keccak_rol_mov	18, 21
210
	keccak_rol_mov	19,  8
211
	keccak_rol_mov	20, 18
212
	keccak_rol_mov	21,  2
213
	keccak_rol_mov	22, 61
214
	keccak_rol_mov	23, 56
215
	keccak_rol_mov	24, 14
216
 
217
	stdcall	keccak_pi
218
	stdcall	keccak_chi
219
 
220
	movq	mm0, [edi + 8*(0)]
221
	pxor	mm0, [crash._.sha3_round + 8*(%-1)]
222
	movq	[edi + 8*(0)], mm0
223
end repeat
224
 
225
	ret
226
endp
227
 
228
 
229
proc crash.sha3_224 _hash, _data
230
	mov	edi, [_hash]
231
 
232
repeat 18
233
	movq	mm0, [esi + 8*(%-1)]
234
	pxor	mm0, [edi + 8*(%-1)]
235
	movq	[edi + 8*(%-1)], mm0
236
end repeat
237
 
238
	stdcall	keccak_permutation
239
 
240
	add	esi, 144
241
	ret
242
endp
243
 
244
 
245
proc crash.sha3_256 _hash, _data
246
	mov	edi, [_hash]
247
 
248
repeat 17
249
	movq	mm0, [esi + 8*(%-1)]
250
	pxor	mm0, [edi + 8*(%-1)]
251
	movq	[edi + 8*(%-1)], mm0
252
end repeat
253
 
254
	stdcall	keccak_permutation
255
 
256
	add	esi, 136
257
	ret
258
endp
259
 
260
 
261
proc crash.sha3_384 _hash, _data
262
	mov	edi, [_hash]
263
 
264
repeat 13
265
	movq	mm0, [esi + 8*(%-1)]
266
	pxor	mm0, [edi + 8*(%-1)]
267
	movq	[edi + 8*(%-1)], mm0
268
end repeat
269
 
270
	stdcall	keccak_permutation
271
 
272
	add	esi, 104
273
	ret
274
endp
275
 
276
 
277
proc crash.sha3_512 _hash, _data
278
	mov	edi, [_hash]
279
 
280
repeat 9
281
	movq	mm0, [esi + 8*(%-1)]
282
	pxor	mm0, [edi + 8*(%-1)]
283
	movq	[edi + 8*(%-1)], mm0
284
end repeat
285
 
286
	stdcall	keccak_permutation
287
 
288
	add	esi, 72
289
	ret
290
endp
291