-
Notifications
You must be signed in to change notification settings - Fork 0
/
cipher.s
280 lines (246 loc) · 5.85 KB
/
cipher.s
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
//go:build amd64
// +build amd64
#include "textflag.h"
#include "go_asm.h"
TEXT feistel(SB),NOSPLIT,$80
MOVQ SI, (SP)
MOVQ R8, 8(SP)
VMOVDQU Y8, 16(SP)
VMOVDQU Y9, 48(SP)
#define subkeysptr AX // *[rounds]v64
#define in Y0 // [4]v64
#define out Y0 // [4]v64
#define left Y8
#define right Y9
VPSRLQ $const_v32Size*8, in, left
VPSLLQ $const_v32Size*8, in, right
VPSRLQ $const_v32Size*8, right, right
LEAQ const_rounds*const_v64Size(subkeysptr), R8
MOVQ subkeysptr, SI
MOVQ $0x8000000000000000, DX
MOVQ DX, X6
VPBROADCASTQ X6, Y6
loopstart:
MOVQ (SI), AX
VMOVDQU right, out
CALL fVec4(SB)
VPXOR left, out, out
VMOVDQU right, left
VMOVDQU out, right
ADDQ $const_v64Size, SI
CMPQ SI, R8
JNE loopstart
VPSLLQ $const_v32Size*8, right, right
VPXOR right, left, out
#undef left
#undef right
#undef subkeysptr
#undef in
#undef out
MOVQ (SP), SI
MOVQ 8(SP), R8
VMOVDQU 16(SP), Y8
VMOVDQU 48(SP), Y9
RET
TEXT ·desECBCrypt(SB),NOSPLIT,$56
MOVQ R8, (SP)
MOVQ DI, 8(SP)
MOVQ SI, 16(SP)
MOVQ R12, 24(SP)
MOVQ R13, 32(SP)
#define subkeysptr R8 // *[rounds]v64
#define dstptr DI // []byte
#define dstlen R12 // int
#define srcptr SI // []byte
#define srclen R13 // int
MOVQ main·subkeys(FP), subkeysptr
MOVQ ·dst_base+8(FP), dstptr
MOVQ ·dst_len+16(FP), dstlen
MOVQ ·src_base+32(FP), srcptr
MOVQ ·src_len+40(FP), srclen
MOVQ R9, 40(SP)
MOVQ R11, 48(SP)
#define dstnxt R9
#define srcnxtptr R11
ADDQ dstptr, dstlen
ADDQ srcptr, srclen
loopstart:
MOVQ srcptr, srcnxtptr
ADDQ $const_v64Size*4, srcnxtptr
CMPQ srcnxtptr, srclen
JG loadthree
VMOVDQU (srcptr), Y0
CALL ipVec4(SB)
MOVQ subkeysptr, AX
CALL feistel(SB)
CALL ipInverseVec4(SB)
MOVQ dstptr, dstnxt
ADDQ $const_v64Size*4, dstnxt
CMPQ dstnxt, dstlen
JG loadthree
VMOVDQU Y0, (dstptr)
MOVQ dstnxt, dstptr
MOVQ srcnxtptr, srcptr
JMP loopstart
loadthree:
MOVQ srcptr, srcnxtptr
ADDQ $const_v64Size*3, srcnxtptr
CMPQ srcnxtptr, srclen
JG loadtwo
PINSRQ $0, const_v64Size*2(srcptr), X0
VINSERTI128 $1, X0, Y0, Y0
loadtwo:
MOVQ srcptr, srcnxtptr
ADDQ $const_v64Size*2, srcnxtptr
CMPQ srcnxtptr, srclen
JG loadone
PINSRQ $1, const_v64Size(srcptr), X0
loadone:
MOVQ srcptr, srcnxtptr
ADDQ $const_v64Size, srcnxtptr
CMPQ srcnxtptr, srclen
JG end
PINSRQ $0, (srcptr), X0
CALL ipVec4(SB)
MOVQ subkeysptr, AX
CALL feistel(SB)
CALL ipInverseVec4(SB)
extractthree:
MOVQ dstptr, dstnxt
ADDQ $const_v64Size*3, dstnxt
CMPQ dstnxt, dstlen
JG extracttwo
VEXTRACTI128 $1, Y0, X1
PEXTRQ $0, X1, const_v64Size*2(dstptr)
extracttwo:
MOVQ dstptr, dstnxt
ADDQ $const_v64Size*2, dstnxt
CMPQ dstnxt, dstlen
JG extractone
PEXTRQ $1, X0, const_v64Size(dstptr)
extractone:
MOVQ dstptr, dstnxt
ADDQ $const_v64Size, dstnxt
CMPQ dstnxt, dstlen
PEXTRQ $0, X0, (dstptr)
end:
#undef dstnxt
#undef srcnxtptr
MOVQ 40(SP), R9
MOVQ 48(SP), R11
#undef subkeysptr
#undef dstptr
#undef dstlen
#undef srcptr
#undef srclen
MOVQ R8, (SP)
MOVQ DI, 8(SP)
MOVQ SI, 16(SP)
MOVQ R12, 24(SP)
MOVQ R13, 32(SP)
RET
TEXT ·desTripleECBCrypt(SB),NOSPLIT,$72
MOVQ R8, (SP)
MOVQ DI, 8(SP)
MOVQ SI, 16(SP)
MOVQ R12, 24(SP)
MOVQ R13, 32(SP)
#define subkeysptr R8 // *[3][rounds]v64
#define dstptr DI // []byte
#define dstlen R12 // int
#define srcptr SI // []byte
#define srclen R13 // int
MOVQ main·subkeysTriple(FP), subkeysptr
MOVQ ·dst_base+8(FP), dstptr
MOVQ ·dst_len+16(FP), dstlen
MOVQ ·src_base+32(FP), srcptr
MOVQ ·src_len+40(FP), srclen
MOVQ R9, 40(SP)
MOVQ R11, 48(SP)
#define dstnxt R9
#define srcnxtptr R11
ADDQ dstptr, dstlen
ADDQ srcptr, srclen
loopstart:
MOVQ dstptr, dstnxt
ADDQ $const_v64Size*4, dstnxt
CMPQ dstnxt, dstlen
JG loadthree
VMOVDQU (srcptr), Y0
CALL ipVec4(SB)
LEAQ (subkeysptr), AX
CALL feistel(SB)
LEAQ const_rounds*const_v64Size(subkeysptr), AX
CALL feistel(SB)
LEAQ const_rounds*const_v64Size*2(subkeysptr), AX
CALL feistel(SB)
CALL ipInverseVec4(SB)
MOVQ srcptr, srcnxtptr
ADDQ $const_v64Size*4, srcnxtptr
CMPQ srcnxtptr, srclen
JG loadthree
VMOVDQU Y0, (dstptr)
MOVQ dstnxt, dstptr
MOVQ srcnxtptr, srcptr
JMP loopstart
loadthree:
MOVQ srcptr, srcnxtptr
ADDQ $const_v64Size*3, srcnxtptr
CMPQ srcnxtptr, srclen
JG loadtwo
PINSRQ $0, const_v64Size*2(srcptr), X0
VINSERTI128 $1, X0, Y0, Y0
loadtwo:
MOVQ srcptr, srcnxtptr
ADDQ $const_v64Size*2, srcnxtptr
CMPQ srcnxtptr, srclen
JG loadone
PINSRQ $1, const_v64Size(srcptr), X0
loadone:
MOVQ srcptr, srcnxtptr
ADDQ $const_v64Size, srcnxtptr
CMPQ srcnxtptr, srclen
JG end
PINSRQ $0, (srcptr), X0
CALL ipVec4(SB)
LEAQ (subkeysptr), AX
CALL feistel(SB)
LEAQ const_rounds*const_v64Size(subkeysptr), AX
CALL feistel(SB)
LEAQ const_rounds*const_v64Size*2(subkeysptr), AX
CALL feistel(SB)
CALL ipInverseVec4(SB)
extractthree:
MOVQ dstptr, dstnxt
ADDQ $const_v64Size*3, dstnxt
CMPQ dstnxt, dstlen
JG extracttwo
VEXTRACTI128 $1, Y0, X1
PEXTRQ $0, X1, const_v64Size*2(dstptr)
extracttwo:
MOVQ dstptr, dstnxt
ADDQ $const_v64Size*2, dstnxt
CMPQ dstnxt, dstlen
JG extractone
PEXTRQ $1, X0, const_v64Size(dstptr)
extractone:
MOVQ dstptr, dstnxt
ADDQ $const_v64Size, dstnxt
CMPQ dstnxt, dstlen
PEXTRQ $0, X0, (dstptr)
end:
#undef dstnxt
#undef srcnxtptr
MOVQ 40(SP), R9
MOVQ 48(SP), R11
#undef subkeysptr
#undef dstptr
#undef dstlen
#undef srcptr
#undef srclen
MOVQ R8, (SP)
MOVQ DI, 8(SP)
MOVQ SI, 16(SP)
MOVQ R12, 24(SP)
MOVQ R13, 32(SP)
RET