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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
|
;;
;; Copyright (c) 2017-2018, Intel Corporation
;;
;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions are met:
;;
;; * Redistributions of source code must retain the above copyright notice,
;; this list of conditions and the following disclaimer.
;; * Redistributions in binary form must reproduce the above copyright
;; notice, this list of conditions and the following disclaimer in the
;; documentation and/or other materials provided with the distribution.
;; * Neither the name of Intel Corporation nor the names of its contributors
;; may be used to endorse or promote products derived from this software
;; without specific prior written permission.
;;
;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
;; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
;; DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
;; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
;; SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
;; CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
;; OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;;
;; In System V AMD64 ABI
;; calle saves: RBX, RBP, R12-R15
;; Windows x64 ABI
;; calle saves: RBX, RBP, RDI, RSI, RSP, R12-R15
;;
;; Registers: RAX RBX RCX RDX RBP RSI RDI R8 R9 R10 R11 R12 R13 R14 R15
;; -----------------------------------------------------------
;; Windows clobbers: RAX R8 R9 R10 R11
;; Windows preserves: RBX RCX RDX RBP RSI RDI R12 R13 R14 R15
;; -----------------------------------------------------------
;; Linux clobbers: RAX RCX RDX R10 R11
;; Linux preserves: RBX RBP RSI RDI R8 R9 R12 R13 R14 R15
;; -----------------------------------------------------------
;; Clobbers ZMM0-31, K1-7 (K1-2 and K4-6 here but DES underneath clobbers K1-7).
%include "os.asm"
%include "reg_sizes.asm"
%include "job_aes_hmac.asm"
%include "mb_mgr_datastruct.asm"
%include "constants.asm"
;%define DO_DBGPRINT
%include "dbgprint.asm"
extern docsis_des_x16_enc_avx512
extern docsis_des_x16_dec_avx512
extern des_x16_cbc_enc_avx512
extern des_x16_cbc_dec_avx512
extern des3_x16_cbc_enc_avx512
extern des3_x16_cbc_dec_avx512
%ifdef LINUX
%define arg1 rdi
%define arg2 rsi
%define arg3 rdx
%define arg4 rcx
%else
%define arg1 rcx
%define arg2 rdx
%define arg3 r8
%define arg4 r9
%endif
%define STATE arg1
%define JOB arg2
%define IA0 arg3
%define IA1 arg4
%define IA2 r10
%define MIN_IDX r11
%define MIN_LEN rax
%define LANE r11
%define AVX512_NUM_DES_LANES 16
%define ZTMP0 zmm0
%define ZTMP1 zmm1
%define ZTMP2 zmm2
%define ZTMP3 zmm3
%define ZTMP4 zmm4
%define ZTMP5 zmm5
%define ZTMP6 zmm6
%define ZTMP7 zmm7
%define ZTMP8 zmm8
%define ZTMP9 zmm9
;;; ===========================================================================
;;; ===========================================================================
;;; MACROS
;;; ===========================================================================
;;; ===========================================================================
;;; ===========================================================================
;;; DES/DOCSIS DES job submit
;;; ===========================================================================
;;; DES_DOCSIS [in] - DES, DOCSIS or 3DES cipher selection
;;; ENC_DEC [in] - ENCrypt or DECrypt seection
%macro GENERIC_DES_SUBMIT 2
%define %%DES_DOCSIS %1
%define %%ENC_DEC %2
;; get unsued lane and increment number of lanes in use
mov IA0, [STATE + _des_unused_lanes]
mov LANE, IA0
and LANE, 0xF ;; just a nibble
shr IA0, 4
mov [STATE + _des_unused_lanes], IA0
add qword [STATE + _des_lanes_in_use], 1
;; store job info in OOO structure
;; - job pointer
mov [STATE + _des_job_in_lane + LANE*8], JOB
;; - key schedule
%ifidn %%ENC_DEC, ENC
mov IA2, [JOB + _aes_enc_key_expanded]
%else
mov IA2, [JOB + _aes_dec_key_expanded]
%endif
mov [STATE + _des_args_keys + LANE*8], IA2
;; - IV
mov IA2, [JOB + _iv]
mov DWORD(IA0), [IA2]
mov DWORD(IA1), [IA2 + 4]
mov [STATE + _des_args_IV + LANE*4], DWORD(IA0)
mov [STATE + _des_args_IV + LANE*4 + (AVX512_NUM_DES_LANES*4)], DWORD(IA1)
;; - src pointer
mov IA0, [JOB + _src]
add IA0, [JOB + _cipher_start_src_offset_in_bytes]
mov [STATE + _des_args_in + LANE*8], IA0
;; - destination pointer
mov IA1, [JOB + _dst]
mov [STATE + _des_args_out + LANE*8], IA1
;; - length in bytes (block aligned)
mov IA2, [JOB + _msg_len_to_cipher_in_bytes]
and IA2, -8
mov [STATE + _des_lens + LANE*2], WORD(IA2)
%ifidn %%DES_DOCSIS, DOCSIS
;; - block length
mov [STATE + _des_args_BLen + LANE*4], DWORD(IA2)
;; - last in
add IA0, IA2
mov [STATE + _des_args_LIn + LANE*8], IA0
;; - last out
add IA1, IA2
mov [STATE + _des_args_LOut + LANE*8], IA1
;; - partial length
mov IA2, [JOB + _msg_len_to_cipher_in_bytes]
and IA2, 7
mov [STATE + _des_args_PLen + LANE*4], DWORD(IA2)
%endif ; DOCSIS
;; is there enough jobs to process them in parallel?
cmp qword [STATE + _des_lanes_in_use], AVX512_NUM_DES_LANES
jb %%_des_submit_null_end
;; schedule the processing
;; - find min job size
vmovdqa XWORD(ZTMP0), [STATE + _des_lens + 2*0]
vphminposuw XWORD(ZTMP2), XWORD(ZTMP0)
vpextrw DWORD(MIN_LEN), XWORD(ZTMP2), 0 ; min value
vpextrw DWORD(MIN_IDX), XWORD(ZTMP2), 1 ; min index
vmovdqa XWORD(ZTMP1), [STATE + _des_lens + 2*8]
vphminposuw XWORD(ZTMP2), XWORD(ZTMP1)
vpextrw DWORD(IA2), XWORD(ZTMP2), 0 ; min value
cmp DWORD(MIN_LEN), DWORD(IA2)
jle %%_use_min
vpextrw DWORD(MIN_IDX), XWORD(ZTMP2), 1 ; min index
add DWORD(MIN_IDX), 8 ; but index +8
mov MIN_LEN, IA2 ; min len
%%_use_min:
cmp MIN_LEN, 0
je %%_len_is_0
vpbroadcastw XWORD(ZTMP3), WORD(MIN_LEN)
vpsubw XWORD(ZTMP0), XWORD(ZTMP0), XWORD(ZTMP3)
vmovdqa [STATE + _des_lens + 2*0], XWORD(ZTMP0)
vpsubw XWORD(ZTMP1), XWORD(ZTMP1), XWORD(ZTMP3)
vmovdqa [STATE + _des_lens + 2*8], XWORD(ZTMP1)
push MIN_IDX
mov arg2, MIN_LEN
%ifidn %%ENC_DEC, ENC
;; encrypt
%ifidn %%DES_DOCSIS, DOCSIS
call docsis_des_x16_enc_avx512
%endif
%ifidn %%DES_DOCSIS, DES
call des_x16_cbc_enc_avx512
%endif
%ifidn %%DES_DOCSIS, 3DES
call des3_x16_cbc_enc_avx512
%endif
%else ; ENC
;; decrypt
%ifidn %%DES_DOCSIS, DOCSIS
call docsis_des_x16_dec_avx512
%endif
%ifidn %%DES_DOCSIS, DES
call des_x16_cbc_dec_avx512
%endif
%ifidn %%DES_DOCSIS, 3DES
call des3_x16_cbc_dec_avx512
%endif
%endif ; DEC
pop MIN_IDX
jmp %%_des_submit_end
%%_des_submit_null_end:
xor rax, rax
jmp %%_des_submit_return
%%_len_is_0:
%ifidn %%DES_DOCSIS, DOCSIS
cmp dword [STATE + _des_args_PLen + MIN_IDX*4], 0
jz %%_des_submit_end
push MIN_IDX
xor arg2, arg2 ; len is 0
%ifidn %%ENC_DEC, ENC
call docsis_des_x16_enc_avx512
%else ; ENC
call docsis_des_x16_dec_avx512
%endif ; DEC
pop MIN_IDX
%endif ; DOCSIS
;; fall trough
%%_des_submit_end:
;; return a job
;; - decrement number of jobs in use
sub qword [STATE + _des_lanes_in_use], 1
;; - put the lane back to free lanes pool
mov IA0, [STATE + _des_unused_lanes]
shl IA0, 4
or IA0, MIN_IDX
mov [STATE + _des_unused_lanes], IA0
;; - mark job as complete
;; - clear job pointer
mov rax, [STATE + _des_job_in_lane + MIN_IDX*8]
mov qword [STATE + _des_job_in_lane + MIN_IDX*8], 0
or dword [rax + _status], STS_COMPLETED_AES
vzeroupper
%%_des_submit_return:
%endmacro
;;; ===========================================================================
;;; DES/DOCSIS DES flush
;;; ===========================================================================
;;; DES_DOCSIS [in] - DES, DOCSIS or 3DES cipher selection
;;; ENC_DEC [in] - ENCrypt or DECrypt selection
;;;
;;; Clobbers k1, k2, k4, k5 and k6
%macro GENERIC_DES_FLUSH 2
%define %%DES_DOCSIS %1
%define %%ENC_DEC %2
cmp qword [STATE + _des_lanes_in_use], 0
je %%_des_flush_null_end
;; find non-null job
vpxord ZTMP0, ZTMP0, ZTMP0
vmovdqu64 ZTMP1, [STATE + _des_job_in_lane + (0*PTR_SZ)]
vmovdqu64 ZTMP2, [STATE + _des_job_in_lane + (8*PTR_SZ)]
vpcmpq k1, ZTMP1, ZTMP0, 4 ; NEQ
vpcmpq k2, ZTMP2, ZTMP0, 4 ; NEQ
xor IA0, IA0
xor IA1, IA1
kmovw DWORD(IA0), k1
kmovw DWORD(IA1), k2
mov DWORD(IA2), DWORD(IA1)
shl DWORD(IA2), 8
or DWORD(IA2), DWORD(IA0) ; mask of non-null jobs in IA2
not BYTE(IA0)
kmovw k4, DWORD(IA0)
not BYTE(IA1)
kmovw k5, DWORD(IA1)
mov DWORD(IA0), DWORD(IA2)
not WORD(IA0)
kmovw k6, DWORD(IA0) ; mask of NULL jobs in k4, k5 and k6
mov DWORD(IA0), DWORD(IA2)
xor IA2, IA2
bsf WORD(IA2), WORD(IA0) ; index of the 1st set bit in IA2
;; copy good lane data into NULL lanes
;; - k1(L8)/k2(H8) - masks of non-null jobs
;; - k4(L8)/k5(H8)/k6 - masks of NULL jobs
;; - IA2 index of 1st non-null job
;; - in pointer
mov IA0, [STATE + _des_args_in + IA2*8]
vpbroadcastq ZTMP1, IA0
vmovdqu64 [STATE + _des_args_in + (0*PTR_SZ)]{k4}, ZTMP1
vmovdqu64 [STATE + _des_args_in + (8*PTR_SZ)]{k5}, ZTMP1
;; - out pointer
mov IA0, [STATE + _des_args_out + IA2*8]
vpbroadcastq ZTMP1, IA0
vmovdqu64 [STATE + _des_args_out + (0*PTR_SZ)]{k4}, ZTMP1
vmovdqu64 [STATE + _des_args_out + (8*PTR_SZ)]{k5}, ZTMP1
;; - key schedule
mov IA0, [STATE + _des_args_keys + IA2*8]
vpbroadcastq ZTMP1, IA0
vmovdqu64 [STATE + _des_args_keys + (0*PTR_SZ)]{k4}, ZTMP1
vmovdqu64 [STATE + _des_args_keys + (8*PTR_SZ)]{k5}, ZTMP1
;; - zero partial len
vmovdqu32 [STATE + _des_args_PLen]{k6}, ZTMP0
;; - set len to UINT16_MAX
mov WORD(IA0), 0xffff
vpbroadcastw ZTMP1, WORD(IA0)
vmovdqu16 [STATE + _des_lens]{k6}, ZTMP1
;; - IV
mov DWORD(IA0), [STATE + _des_args_IV + IA2*4]
mov DWORD(IA1), [STATE + _des_args_IV + IA2*4 + (16*4)]
vpbroadcastd ZTMP1, DWORD(IA0)
vpbroadcastd ZTMP2, DWORD(IA1)
vmovdqu32 [STATE + _des_args_IV]{k6}, ZTMP1
vmovdqu32 [STATE + _des_args_IV + (16*4)]{k6}, ZTMP2
;; schedule the processing
;; - find min job size
vmovdqa XWORD(ZTMP0), [STATE + _des_lens + 2*0]
vphminposuw XWORD(ZTMP2), XWORD(ZTMP0)
vpextrw DWORD(MIN_LEN), XWORD(ZTMP2), 0 ; min value
vpextrw DWORD(MIN_IDX), XWORD(ZTMP2), 1 ; min index
vmovdqa XWORD(ZTMP1), [STATE + _des_lens + 2*8]
vphminposuw XWORD(ZTMP2), XWORD(ZTMP1)
vpextrw DWORD(IA2), XWORD(ZTMP2), 0 ; min value
cmp DWORD(MIN_LEN), DWORD(IA2)
jle %%_use_min
vpextrw DWORD(MIN_IDX), XWORD(ZTMP2), 1 ; min index
add DWORD(MIN_IDX), 8 ; but index +8
mov MIN_LEN, IA2 ; min len
%%_use_min:
vpbroadcastw XWORD(ZTMP3), WORD(MIN_LEN)
vpsubw XWORD(ZTMP0), XWORD(ZTMP0), XWORD(ZTMP3)
vmovdqa [STATE + _des_lens + 2*0], XWORD(ZTMP0)
vpsubw XWORD(ZTMP1), XWORD(ZTMP1), XWORD(ZTMP3)
vmovdqa [STATE + _des_lens + 2*8], XWORD(ZTMP1)
push MIN_IDX
mov arg2, MIN_LEN
%ifidn %%ENC_DEC, ENC
;; encrypt
%ifidn %%DES_DOCSIS, DOCSIS
call docsis_des_x16_enc_avx512
%endif
%ifidn %%DES_DOCSIS, DES
call des_x16_cbc_enc_avx512
%endif
%ifidn %%DES_DOCSIS, 3DES
call des3_x16_cbc_enc_avx512
%endif
%else ; ENC
;; decrypt
%ifidn %%DES_DOCSIS, DOCSIS
call docsis_des_x16_dec_avx512
%endif
%ifidn %%DES_DOCSIS, DES
call des_x16_cbc_dec_avx512
%endif
%ifidn %%DES_DOCSIS, 3DES
call des3_x16_cbc_dec_avx512
%endif
%endif ; DEC
pop MIN_IDX
jmp %%_des_flush_end
%%_des_flush_null_end:
xor rax, rax
jmp %%_des_flush_return
%%_des_flush_end:
;; return a job
;; - decrement number of jobs in use
sub qword [STATE + _des_lanes_in_use], 1
;; - put the lane back to free lanes pool
mov IA0, [STATE + _des_unused_lanes]
shl IA0, 4
or IA0, MIN_IDX
mov [STATE + _des_unused_lanes], IA0
;; - mark job as complete
mov rax, [STATE + _des_job_in_lane + MIN_IDX*8]
or dword [rax + _status], STS_COMPLETED_AES
;; - clear job pointer
mov qword [STATE + _des_job_in_lane + MIN_IDX*8], 0
%%_des_flush_return:
vzeroupper
%endmacro
;;; ========================================================
;;; DATA
section .data
default rel
;;; ========================================================
;;; CODE
section .text
;;; arg 1 : pointer to DES OOO structure
;;; arg 2 : job
align 64
MKGLOBAL(submit_job_des_cbc_enc_avx512,function,internal)
submit_job_des_cbc_enc_avx512:
GENERIC_DES_SUBMIT DES, ENC
ret
;;; arg 1 : pointer to DES OOO structure
;;; arg 2 : job
align 64
MKGLOBAL(submit_job_des_cbc_dec_avx512,function,internal)
submit_job_des_cbc_dec_avx512:
GENERIC_DES_SUBMIT DES, DEC
ret
;;; arg 1 : pointer to DES OOO structure
;;; arg 2 : job
align 64
MKGLOBAL(submit_job_docsis_des_enc_avx512,function,internal)
submit_job_docsis_des_enc_avx512:
GENERIC_DES_SUBMIT DOCSIS, ENC
ret
;;; arg 1 : pointer to DES OOO structure
;;; arg 2 : job
align 64
MKGLOBAL(submit_job_docsis_des_dec_avx512,function,internal)
submit_job_docsis_des_dec_avx512:
GENERIC_DES_SUBMIT DOCSIS, DEC
ret
;;; arg 1 : pointer to DES OOO structure
;;; arg 2 : job
align 64
MKGLOBAL(submit_job_3des_cbc_enc_avx512,function,internal)
submit_job_3des_cbc_enc_avx512:
GENERIC_DES_SUBMIT 3DES, ENC
ret
;;; arg 1 : pointer to DES OOO structure
;;; arg 2 : job
align 64
MKGLOBAL(submit_job_3des_cbc_dec_avx512,function,internal)
submit_job_3des_cbc_dec_avx512:
GENERIC_DES_SUBMIT 3DES, DEC
ret
;;; arg 1 : pointer to DES OOO structure
align 64
MKGLOBAL(flush_job_des_cbc_enc_avx512,function,internal)
flush_job_des_cbc_enc_avx512:
GENERIC_DES_FLUSH DES, ENC
ret
;;; arg 1 : pointer to DES OOO structure
align 64
MKGLOBAL(flush_job_des_cbc_dec_avx512,function,internal)
flush_job_des_cbc_dec_avx512:
GENERIC_DES_FLUSH DES, DEC
ret
;;; arg 1 : pointer to DES OOO structure
align 64
MKGLOBAL(flush_job_docsis_des_enc_avx512,function,internal)
flush_job_docsis_des_enc_avx512:
GENERIC_DES_FLUSH DOCSIS, ENC
ret
;;; arg 1 : pointer to DES OOO structure
align 64
MKGLOBAL(flush_job_docsis_des_dec_avx512,function,internal)
flush_job_docsis_des_dec_avx512:
GENERIC_DES_FLUSH DOCSIS, DEC
ret
;;; arg 1 : pointer to DES OOO structure
align 64
MKGLOBAL(flush_job_3des_cbc_enc_avx512,function,internal)
flush_job_3des_cbc_enc_avx512:
GENERIC_DES_FLUSH 3DES, ENC
ret
;;; arg 1 : pointer to DES OOO structure
align 64
MKGLOBAL(flush_job_3des_cbc_dec_avx512,function,internal)
flush_job_3des_cbc_dec_avx512:
GENERIC_DES_FLUSH 3DES, DEC
ret
%ifdef LINUX
section .note.GNU-stack noalloc noexec nowrite progbits
%endif
|