Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/aes/asm/aes-cfb-avx512.pl

raw
1 #! /usr/bin/env perl
2 # Copyright 2025-2026 The OpenSSL Project Authors. All Rights Reserved.
3 # Copyright (c) 2025, Intel Corporation. All Rights Reserved.
4 #
5 # Licensed under the Apache License 2.0 (the "License"). You may not use
6 # this file except in compliance with the License. You can obtain a copy
7 # in the file LICENSE in the source distribution or at
8 # https://www.openssl.org/source/license.html
9 #
10 # Implements AES-CFB128 encryption and decryption with Intel(R) VAES
11
12 $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
13 $flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
14
15 $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
16
17 $avx512vaes=0; # will be non-zero if tooling supports Intel AVX-512 and VAES
18
19 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
20 ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
21 ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
22 die "can't locate x86_64-xlate.pl";
23
24 if (`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
25 =~ /GNU assembler version ([2-9]\.[0-9]+)/) {
26 $avx512vaes = ($1>=2.30);
27 }
28
29 if (!$avx512vaes && $win64 && ($flavour =~ /nasm/ || $ENV{ASM} =~ /nasm/) &&
30 `nasm -v 2>&1` =~ /NASM version ([2-9]\.[0-9]+)(?:\.([0-9]+))?/) {
31 $avx512vaes = ($1==2.13 && $2>=3) + ($1>=2.14);
32 }
33
34 if (!$avx512vaes && $win64 && ($flavour =~ /masm/ || $ENV{ASM} =~ /ml64/) &&
35 `ml64 2>&1` =~ /Version ([0-9]+\.[0-9]+)\./) {
36 $avx512vaes = ($1>=14.16);
37 }
38
39 if (!$avx512vaes && `$ENV{CC} -v 2>&1`
40 =~ /(Apple)?\s*((?:clang|LLVM) version|.*based on LLVM) ([0-9]+)\.([0-9]+)\.([0-9]+)?/) {
41 my $ver = $3 + $4/100.0 + $5/10000.0; # 3.1.0->3.01, 3.10.1->3.1001
42 if ($1) {
43 # Apple conditions, they use a different version series, see
44 # https://en.wikipedia.org/wiki/Xcode#Xcode_7.0_-_10.x_(since_Free_On-Device_Development)_2
45 # clang 7.0.0 is Apple clang 10.0.1
46 $avx512vaes = ($ver>=10.0001)
47 } else {
48 $avx512vaes = ($ver>=7.0);
49 }
50 }
51
52 open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\""
53 or die "can't call $xlate: $!";
54 *STDOUT=*OUT;
55
56 ##################################################################
57
58 $code=".text\n";
59
60 if ($avx512vaes) {
61
62 $code.=<<___;
63 .extern OPENSSL_ia32cap_P
64
65 #################################################################
66 # Signature:
67 #
68 # int ossl_aes_cfb128_vaes_eligible(void);
69 #
70 # Detects if the underlying hardware supports all the features
71 # required to run the Intel AVX-512 implementations of AES-CFB128 algorithms.
72 #
73 # Returns: non zero if all the required features are detected, 0 otherwise
74 #################################################################
75
76 .globl ossl_aes_cfb128_vaes_eligible
77 .type ossl_aes_cfb128_vaes_eligible,\@abi-omnipotent
78 .balign 64
79
80 ossl_aes_cfb128_vaes_eligible:
81 .cfi_startproc
82 endbranch
83
84 mov OPENSSL_ia32cap_P+8(%rip),%ecx
85 xor %eax,%eax
86
87 # Check 3rd 32-bit word of OPENSSL_ia32cap_P for the feature bit(s):
88 # AVX512BW (bit 30) + AVX512DQ (bit 17) + AVX512F (bit 16)
89
90 and \$0x40030000,%ecx # mask is 1<<30|1<<17|1<<16
91 cmp \$0x40030000,%ecx
92 jne .Laes_cfb128_vaes_eligible_done
93
94 mov OPENSSL_ia32cap_P+12(%rip),%ecx
95
96 # Check 4th 32-bit word of OPENSSL_ia32cap_P for the feature bit(s):
97 # AVX512VAES (bit 9)
98
99 and \$0x200,%ecx # mask is 1<<9
100 cmp \$0x200,%ecx
101 cmove %ecx,%eax
102
103 .Laes_cfb128_vaes_eligible_done:
104 ret
105 .cfi_endproc
106 .size ossl_aes_cfb128_vaes_eligible, .-ossl_aes_cfb128_vaes_eligible
107 ___
108
109 #################################################################
110 #
111 # AES subroutines for:
112 # - preloading the AES key schedule into AVX registers
113 # - single-block AES encryption used by CFB encryption and decryption
114 # - multiple-block AES encryption used by CFB decryption
115 #
116 # The CFB mode only uses block cipher encryption.
117 #
118 # The AES encryption step is described in Section 5.1 Cipher() of
119 # FIPS 197 https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.197-upd1.pdf
120 # and implemented with Intel(R) AES-NI and VAES instructions:
121 #
122 # - AESKEYGENASSIST for key expansion, elsewhere in aesni_set_encrypt_key()
123 # - VPXORD for AES pre-whitening
124 # - VAESENC for performing one AES encryption round
125 # - VAESENCLAST for performing the last AES encryption round
126 #
127 # For more information please consult:
128 # - the Intel(R) 64 and IA-32 Architectures Optimization Reference Manual,
129 # Chapter 21: Cryptography & Finite Field Arithmetic Instructions
130 # https://www.intel.com/content/www/us/en/developer/articles/technical/intel64-and-ia32-architectures-optimization.html
131 # - the Intel(R) Advanced Encryption Standard (AES) New Instructions Set Whitepaper
132 # https://www.intel.com/content/dam/doc/white-paper/advanced-encryption-standard-new-instructions-set-paper.pdf
133 #
134 #################################################################
135
136 # expects the key schedule address in $key_original
137 sub load_aes_key_schedule_1x() {
138 $code.=<<___;
139 vmovdqu8 0($key_original),%xmm17 # schedule 0 whitening
140 vmovdqu8 16($key_original),%xmm18 # 1
141 vmovdqu8 32($key_original),%xmm19 # 2
142 vmovdqu8 48($key_original),%xmm20 # 3
143 vmovdqu8 64($key_original),%xmm21 # 4
144 vmovdqu8 80($key_original),%xmm22 # 5
145 vmovdqu8 96($key_original),%xmm23 # 6
146 vmovdqu8 112($key_original),%xmm24 # 7
147 vmovdqu8 128($key_original),%xmm25 # 8
148 vmovdqu8 144($key_original),%xmm26 # 9
149 vmovdqu8 160($key_original),%xmm27 # 10 last for AES-128
150 vmovdqu8 176($key_original),%xmm28 # 11
151 vmovdqu8 192($key_original),%xmm29 # 12 last for AES-192
152 vmovdqu8 208($key_original),%xmm30 # 13
153 vmovdqu8 224($key_original),%xmm31 # 14 last for AES-256
154
155 mov 240($key_original),$rounds # load AES rounds
156 # 240 is the byte-offset of the rounds field in AES_KEY
157 ___
158 }
159
160
161 # expects the key schedule address in $key_original
162 sub load_aes_key_schedule_4x() {
163 $code.=<<___;
164 vbroadcasti32x4 0($key_original),%zmm17 # schedule 0 whitening
165 vbroadcasti32x4 16($key_original),%zmm18 # 1
166 vbroadcasti32x4 32($key_original),%zmm19 # 2
167 vbroadcasti32x4 48($key_original),%zmm20 # 3
168 vbroadcasti32x4 64($key_original),%zmm21 # 4
169 vbroadcasti32x4 80($key_original),%zmm22 # 5
170 vbroadcasti32x4 96($key_original),%zmm23 # 6
171 vbroadcasti32x4 112($key_original),%zmm24 # 7
172 vbroadcasti32x4 128($key_original),%zmm25 # 8
173 vbroadcasti32x4 144($key_original),%zmm26 # 9
174 vbroadcasti32x4 160($key_original),%zmm27 # 10 last for AES-128
175 vbroadcasti32x4 176($key_original),%zmm28 # 11
176 vbroadcasti32x4 192($key_original),%zmm29 # 12 last for AES-192
177 vbroadcasti32x4 208($key_original),%zmm30 # 13
178 vbroadcasti32x4 224($key_original),%zmm31 # 14 last for AES-256
179
180 mov 240($key_original),$rounds # load AES rounds
181 # 240 is the byte-offset of the rounds field in AES_KEY
182 ___
183 }
184
185 # Performs AES encryption of 1 128-bit block
186 # Expects iv in $temp, non-final AES rounds in $rounds and key schedule in xmm17..31
187 sub vaes_encrypt_block_1x() {
188 my ($label_prefix)=@_;
189 $code.=<<___;
190 vpxord %xmm17,$temp,$temp # AES pre-whitening
191 vaesenc %xmm18,$temp,$temp
192 vaesenc %xmm19,$temp,$temp
193 vaesenc %xmm20,$temp,$temp
194 vaesenc %xmm21,$temp,$temp
195 vaesenc %xmm22,$temp,$temp
196 vaesenc %xmm23,$temp,$temp
197 vaesenc %xmm24,$temp,$temp
198 vaesenc %xmm25,$temp,$temp
199 vaesenc %xmm26,$temp,$temp
200
201 cmp \$0x09,$rounds
202 ja ${label_prefix}_192_256
203
204 vaesenclast %xmm27,$temp,$temp # last AES-128 encryption round
205 jmp ${label_prefix}_end
206
207 .balign 32
208 ${label_prefix}_192_256:
209
210 vaesenc %xmm27,$temp,$temp
211 vaesenc %xmm28,$temp,$temp
212
213 cmp \$0x0B,$rounds
214 ja ${label_prefix}_256
215
216 vaesenclast %xmm29,$temp,$temp # last AES-192 encryption round
217 jmp ${label_prefix}_end
218
219 .balign 32
220 ${label_prefix}_256:
221
222 vaesenc %xmm29,$temp,$temp
223 vaesenc %xmm30,$temp,$temp
224 vaesenclast %xmm31,$temp,$temp # last AES-256 encryption round
225
226 .balign 32
227 ${label_prefix}_end:
228 ___
229 }
230
231
232 # Performs parallel AES encryption of 4 128-bit blocks
233 # Expects iv in $temp_4x, non-final AES rounds in $rounds and key schedule in zmm17..31
234 sub vaes_encrypt_block_4x() {
235 my ($label_prefix)=@_;
236 $code.=<<___;
237 vpxord %zmm17,$temp_4x,$temp_4x # AES pre-whitening
238 vaesenc %zmm18,$temp_4x,$temp_4x
239 vaesenc %zmm19,$temp_4x,$temp_4x
240 vaesenc %zmm20,$temp_4x,$temp_4x
241 vaesenc %zmm21,$temp_4x,$temp_4x
242 vaesenc %zmm22,$temp_4x,$temp_4x
243 vaesenc %zmm23,$temp_4x,$temp_4x
244 vaesenc %zmm24,$temp_4x,$temp_4x
245 vaesenc %zmm25,$temp_4x,$temp_4x
246 vaesenc %zmm26,$temp_4x,$temp_4x
247
248 cmp \$0x09,$rounds
249 ja ${label_prefix}_192_256
250
251 vaesenclast %zmm27,$temp_4x,$temp_4x # last AES-128 encryption round
252 jmp ${label_prefix}_end
253
254 .balign 32
255 ${label_prefix}_192_256:
256
257 vaesenc %zmm27,$temp_4x,$temp_4x
258 vaesenc %zmm28,$temp_4x,$temp_4x
259
260 cmp \$0x0B,$rounds
261 ja ${label_prefix}_256
262
263 vaesenclast %zmm29,$temp_4x,$temp_4x # last AES-192 encryption round
264 jmp ${label_prefix}_end
265
266 .balign 32
267 ${label_prefix}_256:
268
269 vaesenc %zmm29,$temp_4x,$temp_4x
270 vaesenc %zmm30,$temp_4x,$temp_4x
271 vaesenclast %zmm31,$temp_4x,$temp_4x # last AES-256 encryption round
272
273 .balign 32
274 ${label_prefix}_end:
275 ___
276 }
277
278
279 # Performs parallel AES encryption of 16 128-bit blocks
280 # Expects input in $temp_*x, non-final AES rounds in $rounds and key schedule in zmm17..31
281 sub vaes_encrypt_block_16x() {
282 my ($label_prefix)=@_;
283 $code.=<<___;
284 vpxord %zmm17,$temp_4x, $temp_4x # parallel AES pre-whitening
285 vpxord %zmm17,$temp_8x, $temp_8x
286 vpxord %zmm17,$temp_12x,$temp_12x
287 vpxord %zmm17,$temp_16x,$temp_16x
288
289 vaesenc %zmm18,$temp_4x, $temp_4x
290 vaesenc %zmm18,$temp_8x, $temp_8x
291 vaesenc %zmm18,$temp_12x,$temp_12x
292 vaesenc %zmm18,$temp_16x,$temp_16x
293
294 vaesenc %zmm19,$temp_4x, $temp_4x
295 vaesenc %zmm19,$temp_8x, $temp_8x
296 vaesenc %zmm19,$temp_12x,$temp_12x
297 vaesenc %zmm19,$temp_16x,$temp_16x
298
299 vaesenc %zmm20,$temp_4x, $temp_4x
300 vaesenc %zmm20,$temp_8x, $temp_8x
301 vaesenc %zmm20,$temp_12x,$temp_12x
302 vaesenc %zmm20,$temp_16x,$temp_16x
303
304 vaesenc %zmm21,$temp_4x, $temp_4x
305 vaesenc %zmm21,$temp_8x, $temp_8x
306 vaesenc %zmm21,$temp_12x,$temp_12x
307 vaesenc %zmm21,$temp_16x,$temp_16x
308
309 vaesenc %zmm22,$temp_4x, $temp_4x
310 vaesenc %zmm22,$temp_8x, $temp_8x
311 vaesenc %zmm22,$temp_12x,$temp_12x
312 vaesenc %zmm22,$temp_16x,$temp_16x
313
314 vaesenc %zmm23,$temp_4x, $temp_4x
315 vaesenc %zmm23,$temp_8x, $temp_8x
316 vaesenc %zmm23,$temp_12x,$temp_12x
317 vaesenc %zmm23,$temp_16x,$temp_16x
318
319 vaesenc %zmm24,$temp_4x, $temp_4x
320 vaesenc %zmm24,$temp_8x, $temp_8x
321 vaesenc %zmm24,$temp_12x,$temp_12x
322 vaesenc %zmm24,$temp_16x,$temp_16x
323
324 vaesenc %zmm25,$temp_4x, $temp_4x
325 vaesenc %zmm25,$temp_8x, $temp_8x
326 vaesenc %zmm25,$temp_12x,$temp_12x
327 vaesenc %zmm25,$temp_16x,$temp_16x
328
329 vaesenc %zmm26,$temp_4x, $temp_4x
330 vaesenc %zmm26,$temp_8x, $temp_8x
331 vaesenc %zmm26,$temp_12x,$temp_12x
332 vaesenc %zmm26,$temp_16x,$temp_16x
333
334 cmp \$0x09,$rounds
335 ja ${label_prefix}_192_256
336
337 vaesenclast %zmm27,$temp_4x, $temp_4x # last AES-128 encryption round
338 vaesenclast %zmm27,$temp_8x, $temp_8x
339 vaesenclast %zmm27,$temp_12x,$temp_12x
340 vaesenclast %zmm27,$temp_16x,$temp_16x
341 jmp ${label_prefix}_end
342
343 .balign 32
344 ${label_prefix}_192_256:
345
346 vaesenc %zmm27,$temp_4x, $temp_4x
347 vaesenc %zmm27,$temp_8x, $temp_8x
348 vaesenc %zmm27,$temp_12x,$temp_12x
349 vaesenc %zmm27,$temp_16x,$temp_16x
350
351 vaesenc %zmm28,$temp_4x, $temp_4x
352 vaesenc %zmm28,$temp_8x, $temp_8x
353 vaesenc %zmm28,$temp_12x,$temp_12x
354 vaesenc %zmm28,$temp_16x,$temp_16x
355
356 cmp \$0x0B,$rounds
357 ja ${label_prefix}_256
358
359 vaesenclast %zmm29,$temp_4x, $temp_4x # last AES-192 encryption round
360 vaesenclast %zmm29,$temp_8x, $temp_8x
361 vaesenclast %zmm29,$temp_12x,$temp_12x
362 vaesenclast %zmm29,$temp_16x,$temp_16x
363 jmp ${label_prefix}_end
364
365 .balign 32
366 ${label_prefix}_256:
367
368 vaesenc %zmm29,$temp_4x, $temp_4x
369 vaesenc %zmm29,$temp_8x, $temp_8x
370 vaesenc %zmm29,$temp_12x,$temp_12x
371 vaesenc %zmm29,$temp_16x,$temp_16x
372
373 vaesenc %zmm30,$temp_4x, $temp_4x
374 vaesenc %zmm30,$temp_8x, $temp_8x
375 vaesenc %zmm30,$temp_12x,$temp_12x
376 vaesenc %zmm30,$temp_16x,$temp_16x
377
378 vaesenclast %zmm31,$temp_4x, $temp_4x # last AES-256 encryption round
379 vaesenclast %zmm31,$temp_8x, $temp_8x
380 vaesenclast %zmm31,$temp_12x,$temp_12x
381 vaesenclast %zmm31,$temp_16x,$temp_16x
382
383 .balign 32
384 ${label_prefix}_end:
385 ___
386 }
387
388 #################################################################
389 # Signature:
390 #
391 # void ossl_aes_cfb128_vaes_enc(
392 # const unsigned char *in,
393 # unsigned char *out,
394 # size_t len,
395 # const AES_KEY *ks,
396 # const unsigned char ivec[16],
397 # /*in-out*/ ossl_ssize_t *num);
398 #
399 # Preconditions:
400 # - all pointers are valid (not NULL...)
401 # - AES key schedule and rounds in `ks` are precomputed
402 #
403 # Invariants:
404 # - `*num` is between 0 and 15 (inclusive)
405 #
406 #################################################################
407 #
408 # The implementation follows closely the encryption half of CRYPTO_cfb128_encrypt:
409 # - "pre" step: processes the last bytes of a partial block
410 # - "mid" step: processes complete blocks
411 # - "post" step: processes the first bytes of a partial block
412 #
413 # To obtain the next ciphertext block `cipher <N>` from
414 # the plaintext block `plain <N>`, the previous ciphertext
415 # block `cipher <N-1>` is required as input.
416 #
417 # The dependency on previous encryption outputs (ciphertexts)
418 # makes CFB encryption inherently serial.
419 #
420 # +----+ +----------+
421 # | iv | +---------------> cipher 0 |
422 # +--+-+ | +----------+
423 # | | |
424 # | | |
425 # +------v------+ | +------v------+
426 # | AES encrypt | | | AES encrypt |
427 # | with key | | | with key |
428 # +------+------+ | +------+------+
429 # | | |
430 # | | |
431 # +---------+ +--v--+ | +---------+ +--v--+
432 # | plain 0 +----> XOR | | | plain 1 +----> XOR |
433 # +---------+ +--+--+ | +---------+ +--+--+
434 # | | |
435 # | | |
436 # +-----v----+ | +-----v----+
437 # | cipher 0 +----+ | cipher 1 |
438 # +----------+ +----------+
439 #
440 #################################################################
441
442 $code.=<<___;
443 .globl ossl_aes_cfb128_vaes_enc
444 .type ossl_aes_cfb128_vaes_enc,\@function,6
445 .balign 64
446 ossl_aes_cfb128_vaes_enc:
447 .cfi_startproc
448 endbranch
449 ___
450
451 $inp="%rdi"; # arg0
452 $out="%rsi"; # arg1
453 $len="%rdx"; # arg2
454
455 $key_original="%rcx"; # arg3
456 $key_backup="%r10";
457
458 $ivp="%r8"; # arg4
459 $nump="%r9"; # arg5
460
461 $num="%r11";
462 $left="%rcx";
463 $mask="%rax";
464
465 $rounds="%r11d";
466
467 $temp="%xmm2";
468 $plain="%xmm3";
469
470 $code.=<<___;
471
472 mov ($nump),$num # $num is the current byte index in the first partial block
473 # $num belongs to 0..15; non-zero means a partial first block
474
475 test $len,$len # return early if $len==0, unlikely to occur
476 jz .Laes_cfb128_vaes_enc_done
477
478 test $num,$num # check if the first block is partial
479 jz .Laes_cfb128_enc_mid # if not, jump to processing full blocks
480
481 ###########################################################
482 # first partial block pre-processing
483 ###########################################################
484
485 mov $key_original,$key_backup # make room for variable shl with cl
486
487 mov \$0x10,$left # first block is partial
488 sub $num,$left # calculate how many bytes $left to process in the block
489 cmp $len,$left #
490 cmova $len,$left # $left = min(16-$num,$len)
491
492 mov \$1,$mask # build a mask with the least significant $left bits set
493 shlq %cl,$mask # $left is left shift counter
494 dec $mask # $mask is 2^$left-1
495 kmovq $mask,%k1
496
497 mov $num,%rax # keep in-out $num in %al
498 add $left,%rax # advance $num
499 and \$0x0F,%al # wrap-around $num in a 16-byte block
500
501 leaq ($num,$ivp),%r11 # process $left iv bytes
502 vmovdqu8 (%r11),%xmm0{%k1}{z}
503 vmovdqu8 ($inp),%xmm1{%k1}{z} # process $left input bytes
504 vpxor %xmm0,%xmm1,%xmm2 # CipherFeedBack XOR
505 vmovdqu8 %xmm2,($out){%k1} # write $left output bytes
506 vmovdqu8 %xmm2,(%r11){%k1} # blend $left output bytes into iv
507
508 add $left,$inp # advance pointers
509 add $left,$out
510 sub $left,$len
511 jz .Laes_cfb128_enc_zero_pre # return early if no AES encryption required
512
513 mov $key_backup,$key_original # restore "key_original" as arg3
514
515 .Laes_cfb128_enc_mid:
516 ___
517
518 &load_aes_key_schedule_1x();
519
520 $code.=<<___;
521 ###########################################################
522 # inner full blocks processing
523 ###########################################################
524
525 vmovdqu ($ivp),$temp # load iv
526
527 cmp \$0x10,$len # is there a full plaintext block left (128 bits) ?
528 jb .Laes_cfb128_enc_post
529
530 .balign 32
531 .Loop_aes_cfb128_enc_main:
532 sub \$0x10,$len
533
534 vmovdqu ($inp),$plain # load plaintext block
535 lea 16($inp),$inp # $inp points to next plaintext
536 ___
537
538 &vaes_encrypt_block_1x(".Laes_cfb128_enc_mid");
539
540 $code.=<<___;
541
542 vpxor $plain,$temp,$temp # CipherFeedBack XOR
543 cmp \$0x10,$len
544 vmovdqu $temp,($out) # write ciphertext
545 lea 16($out),$out # $out points to the next output block
546 jae .Loop_aes_cfb128_enc_main
547
548 xor %eax,%eax # reset num when processing full blocks
549
550 vmovdqu $temp,($ivp) # latest ciphertext block is next encryption input
551
552 .Laes_cfb128_enc_post:
553
554 ###########################################################
555 # last partial block post-processing
556 ###########################################################
557
558 test $len,$len # check if the last block is partial
559 jz .Laes_cfb128_enc_zero_all
560 ___
561
562 &vaes_encrypt_block_1x(".Laes_cfb128_enc_post");
563
564 $code.=<<___;
565
566 mov $len,%rax # num=$len
567
568 mov \$1,%r11 # build a mask with the least significant $len bits set
569 mov %dl,%cl # $len is left shift counter less than 16
570 shlq %cl,%r11
571 dec %r11 # mask is 2^$len-1
572 kmovq %r11,%k1
573
574 vmovdqu8 ($inp),%xmm1{%k1}{z} # read $len input bytes, zero the rest to not impact XOR
575 vpxor $temp,%xmm1,%xmm0 # CipherFeedBack XOR
576 vmovdqu8 %xmm0,($out){%k1} # write $len output bytes
577 vmovdqu8 %xmm0,($ivp) # write chained/streaming iv
578
579 # clear registers
580
581 .Laes_cfb128_enc_zero_all:
582 vpxord %xmm17,%xmm17,%xmm17 # clear the AES key schedule
583 vpxord %xmm18,%xmm18,%xmm18
584 vpxord %xmm19,%xmm19,%xmm19
585 vpxord %xmm20,%xmm20,%xmm20
586 vpxord %xmm21,%xmm21,%xmm21
587 vpxord %xmm22,%xmm22,%xmm22
588 vpxord %xmm23,%xmm23,%xmm23
589 vpxord %xmm24,%xmm24,%xmm24
590 vpxord %xmm25,%xmm25,%xmm25
591 vpxord %xmm26,%xmm26,%xmm26
592 vpxord %xmm27,%xmm27,%xmm27
593 vpxord %xmm28,%xmm28,%xmm28
594 vpxord %xmm29,%xmm29,%xmm29
595 vpxord %xmm30,%xmm30,%xmm30
596 vpxord %xmm31,%xmm31,%xmm31
597
598 vpxor %xmm3,%xmm3,%xmm3 # clear registers used during AES encryption
599
600 .Laes_cfb128_enc_zero_pre:
601 vpxor %xmm0,%xmm0,%xmm0 # clear the rest of the registers
602 vpxor %xmm1,%xmm1,%xmm1
603 vpxor %xmm2,%xmm2,%xmm2
604
605 mov %rax,($nump) # num is in/out, update for future/chained calls
606
607 vzeroupper
608
609 .Laes_cfb128_vaes_enc_done:
610 ret
611 .cfi_endproc
612 .size ossl_aes_cfb128_vaes_enc,.-ossl_aes_cfb128_vaes_enc
613 ___
614
615
616 #################################################################
617 # Signature:
618 #
619 # void ossl_aes_cfb128_vaes_dec(
620 # const unsigned char *in,
621 # unsigned char *out,
622 # size_t len,
623 # const AES_KEY *ks,
624 # const unsigned char ivec[16],
625 # /*in-out*/ ossl_ssize_t *num);
626 #
627 # Preconditions:
628 # - all pointers are valid (not NULL...)
629 # - AES key schedule and rounds in `ks` are precomputed
630 #
631 # Invariants:
632 # - `*num` is between 0 and 15 (inclusive)
633 #
634 #################################################################
635 #
636 # The implementation follows closely the decryption half of CRYPTO_cfb128_encrypt:
637 #
638 # - "pre" step: processes the last bytes of a partial block
639 # - "mid" step: processes complete blocks using an unrolled approach:
640 # - processes 16 blocks in parallel until fewer than 16 blocks remain
641 # - processes 4 blocks in parallel until fewer than 4 blocks remain
642 # - processes 1 block in series until none are left
643 # - "post" step: processes the first bytes of a partial block
644 #
645 # To obtain the next plaintext block `plain <N>` from
646 # its ciphertext block `cipher <N>`, the previous ciphertext
647 # block `cipher <N-1>` is required as input.
648 #
649 # Since CFB decryption for the current block only depends on
650 # iv and ciphertext blocks (already available as inputs)
651 # and not on plaintext blocks, it can be efficiently parallelized.
652 #
653 # +----+ +----------+ +----------+ +----------+
654 # | iv | | cipher 0 | | cipher 1 | | cipher 2 |
655 # +--+-+ +----+-----+ +----+-----+ +----+-----+
656 # | | | |
657 # | | | |
658 # +------v------+ +------v------+ +------v------+ +------v------+
659 # | AES encrypt | | AES encrypt | | AES encrypt | | AES encrypt |
660 # | with key | | with key | | with key | | with key |
661 # +------+------+ +------+------+ +------+------+ +------+------+
662 # | | | |
663 # | | | |
664 # +--v--+ +----------+ +--v--+ +----------+ +--v--+ +----------+ +--v--+ +----------+
665 # | XOR <-----+ cipher 0 | | XOR <-----+ cipher 1 | | XOR <-----+ cipher 2 | | XOR <-----+ cipher 3 |
666 # +--+--+ +----------+ +--+--+ +----------+ +--+--+ +----------+ +--+--+ +----------+
667 # | | | |
668 # | | | |
669 # +----v----+ +----v----+ +----v----+ +----v----+
670 # | plain 0 | | plain 1 | | plain 2 | | plain 3 |
671 # +---------+ +---------+ +---------+ +---------+
672 #
673 # To produce N (4 in the diagram above) output/plaintext blocks we require as inputs:
674 # - iv
675 # - N ciphertext blocks
676 # The N-th ciphertext block is not encrypted and becomes the next iv input.
677 #
678 #################################################################
679
680 $code.=<<___;
681 .globl ossl_aes_cfb128_vaes_dec
682 .type ossl_aes_cfb128_vaes_dec,\@function,6
683 .balign 64
684 ossl_aes_cfb128_vaes_dec:
685 .cfi_startproc
686 endbranch
687 ___
688
689 $inp="%rdi"; # arg0
690 $out="%rsi"; # arg1
691 $len="%rdx"; # arg2
692
693 $key_original="%rcx"; # arg3
694 $key_backup="%r10";
695
696 $ivp="%r8"; # arg4
697 $nump="%r9"; # arg5
698
699 $num="%r11";
700 $left="%rcx";
701 $mask="%rax";
702
703 $rounds="%r11d";
704
705 $temp="%xmm2";
706 $temp_4x="%zmm2";
707 $temp_8x="%zmm4";
708 $temp_12x="%zmm0";
709 $temp_16x="%zmm6";
710
711 $cipher="%xmm3";
712 $cipher_4x="%zmm3";
713 $cipher_8x="%zmm5";
714 $cipher_12x="%zmm1";
715 $cipher_16x="%zmm16";
716
717 $code.=<<___;
718
719 mov ($nump),$num # $num is the current byte index in the first partial block
720 # $num belongs to 0..15; non-zero means a partial first block
721
722 test $len,$len # return early if $len==0, unlikely to occur
723 jz .Laes_cfb128_vaes_dec_done
724 ___
725
726 $code.=<<___ if($win64);
727 sub \$0x10,%rsp
728 .cfi_adjust_cfa_offset 16
729 vmovdqu %xmm6,(%rsp) # xmm6 needs to be maintained for Windows
730 ___
731
732 $code.=<<___;
733 test $num,$num # check if the first block is partial
734 jz .Laes_cfb128_dec_mid # if not, jump to processing full blocks
735
736 ###########################################################
737 # first partial block pre-processing
738 ###########################################################
739
740 mov $key_original,$key_backup # make room for variable shl with cl
741
742 mov \$0x10,$left # first block is partial
743 sub $num,$left # calculate how many bytes $left to process in the block
744 cmp $len,$left #
745 cmova $len,$left # $left = min(16-$num,$len)
746
747 mov \$1,$mask # build a mask with the least significant $left bits set
748 shlq %cl,$mask # $left is left shift counter
749 dec $mask # $mask is 2^$left-1
750 kmovq $mask,%k1
751
752 lea ($num,$left),%rax # keep in-out num in %al, advance by $left
753 and \$0x0F,%al # wrap-around in a 16-byte block
754
755 leaq ($num,$ivp),%r11 # process $left iv bytes
756 vmovdqu8 (%r11),%xmm0{%k1}{z}
757 vmovdqu8 ($inp),%xmm1{%k1}{z} # process $left input bytes
758 vpxor %xmm0,%xmm1,%xmm2 # CipherFeedBack XOR
759 vmovdqu8 %xmm2,($out){%k1} # write $left output bytes
760 vmovdqu8 %xmm1,(%r11){%k1} # blend $left input bytes into iv
761
762 add $left,$inp # advance pointers
763 add $left,$out
764 sub $left,$len
765 jz .Laes_cfb128_dec_zero_pre # return early if no AES encryption required
766
767 mov $key_backup,$key_original # restore "key_original" as arg3
768
769 .Laes_cfb128_dec_mid:
770 ___
771
772 &load_aes_key_schedule_4x();
773
774 $code.=<<___;
775 ###########################################################
776 # inner full blocks processing
777 ###########################################################
778
779 # $temp_4x is "iv | iv | iv | iv"
780 vbroadcasti32x4 ($ivp),$temp_4x # load iv
781
782 cmp \$0x100,$len # are there 16 ciphertext blocks left (2048 bits) ?
783 jb .Laes_cfb128_dec_check_4x
784
785 ###########################################################
786 # decrypt groups of 16 128-bit blocks in parallel
787 # behaves as 16x loop unroll
788 ###########################################################
789
790 .balign 32
791 .Loop_aes_cfb128_dec_mid_16x:
792 sub \$0x100,$len
793
794 # load 16 ciphertext blocks
795
796 # $cipher_4x is "ciphertext 0 | ciphertext 1 | ciphertext 2 | ciphertext 3"
797 vmovdqu32 ($inp),$cipher_4x
798 # $cipher_8x is "ciphertext 4 | ciphertext 5 | ciphertext 6 | ciphertext 7"
799 vmovdqu32 64($inp),$cipher_8x
800 # $cipher_12x is "ciphertext 8 | ciphertext 9 | ciphertext 10 | ciphertext 11"
801 vmovdqu32 128($inp),$cipher_12x
802 # $cipher_16x is "ciphertext 12 | ciphertext 13 | ciphertext 14 | ciphertext 15"
803 vmovdqu32 192($inp),$cipher_16x
804
805 # $temp_4x is "iv | ciphertext 0 | ciphertext 1 | ciphertext 2"
806 valignq \$6,$temp_4x,$cipher_4x,$temp_4x
807 # $temp_8x is "ciphertext 3 | ciphertext 4 | ciphertext 5 | ciphertext 6"
808 valignq \$6,$cipher_4x,$cipher_8x,$temp_8x
809 # $temp_12x is "ciphertext 7 | ciphertext 8 | ciphertext 9 | ciphertext 10"
810 valignq \$6,$cipher_8x,$cipher_12x,$temp_12x
811 # $temp_16x is "ciphertext 11 | ciphertext 12 | ciphertext 13 | ciphertext 14"
812 valignq \$6,$cipher_12x,$cipher_16x,$temp_16x
813
814 lea 256($inp),$inp # $inp points to next ciphertext
815 ___
816
817 &vaes_encrypt_block_16x(".Laes_cfb128_dec_mid_16x");
818
819 $code.=<<___;
820
821 vpxord $cipher_4x,$temp_4x,$temp_4x # CipherFeedBack XOR of 16 blocks
822 vpxord $cipher_8x,$temp_8x,$temp_8x
823 vpxord $cipher_12x,$temp_12x,$temp_12x
824 vpxord $cipher_16x,$temp_16x,$temp_16x
825
826 cmp \$0x100,$len
827
828 vmovdqu32 $temp_4x,($out) # write 16 plaintext blocks
829 vmovdqu32 $temp_8x,64($out)
830 vmovdqu32 $temp_12x,128($out)
831 vmovdqu32 $temp_16x,192($out)
832
833 vmovdqu8 $cipher_16x,$temp_4x
834
835 lea 256($out),$out # $out points to the next output block
836
837 jae .Loop_aes_cfb128_dec_mid_16x
838
839 vextracti64x2 \$3,$cipher_16x,$temp # latest ciphertext block is next decryption iv
840 vinserti32x4 \$3,$temp,$temp_4x,$temp_4x # move ciphertext3 to positions 0 and 3 in preparation for next shuffle
841
842 xor %eax,%eax # reset $num when processing full blocks
843
844 vmovdqu $temp,($ivp) # latest plaintext block is next decryption iv
845
846 .Laes_cfb128_dec_check_4x:
847 cmp \$0x40,$len # are there 4 ciphertext blocks left (512 bits) ?
848 jb .Laes_cfb128_dec_check_1x
849
850 ###########################################################
851 # decrypt groups of 4 128-bit blocks in parallel
852 # behaves as 4x loop unroll
853 ###########################################################
854
855 # expects $temp_4x to contain "iv" in the 3rd (most significant) lane
856
857 .balign 32
858 .Loop_aes_cfb128_dec_mid_4x:
859 sub \$0x40,$len
860
861 # $cipher_4x is "ciphertext0 | ciphertext1 | ciphertext 2 | ciphertext 3"
862 vmovdqu32 ($inp),$cipher_4x # load 4 ciphertext blocks
863
864 # $temp_4x is "iv | ciphertext0 | ciphertext 1 | ciphertext 2"
865 valignq \$6,$temp_4x,$cipher_4x,$temp_4x
866
867 lea 64($inp),$inp # $inp points to next ciphertext
868 ___
869
870 &vaes_encrypt_block_4x(".Laes_cfb128_dec_mid_4x");
871
872 $code.=<<___;
873 vpxord $cipher_4x,$temp_4x,$temp_4x # CipherFeedBack XOR of 4 blocks
874 cmp \$0x40,$len
875 vmovdqu32 $temp_4x,($out) # write 4 plaintext blocks
876 vmovdqu8 $cipher_4x,$temp_4x
877 lea 64($out),$out # $out points to the next output block
878
879 jae .Loop_aes_cfb128_dec_mid_4x
880
881 vextracti64x2 \$3,$temp_4x,$temp # latest ciphertext block is next decryption iv
882 # move ciphertext3 to position 0 in preparation for next step
883
884 xor %eax,%eax # reset $num when processing full blocks
885
886 vmovdqu $temp,($ivp) # latest plaintext block is next decryption iv
887
888 .Laes_cfb128_dec_check_1x:
889 cmp \$0x10,$len # are there full ciphertext blocks left (128 bits) ?
890 jb .Laes_cfb128_dec_post
891
892 ###########################################################
893 # decrypt the rest of full 128-bit blocks in series
894 ###########################################################
895
896 # expects $temp to contain iv
897
898 .balign 32
899 .Loop_aes_cfb128_dec_mid_1x:
900 sub \$0x10,$len
901
902 vmovdqu ($inp),$cipher # load ciphertext block
903 lea 16($inp),$inp # $inp points to next ciphertext
904 ___
905
906 &vaes_encrypt_block_1x(".Loop_aes_cfb128_dec_mid_1x_inner");
907
908 $code.=<<___;
909 vpxor $cipher,$temp,$temp # CipherFeedBack XOR
910 cmp \$0x10,$len
911 vmovdqu $temp,($out) # write plaintext
912 vmovdqu8 $cipher,$temp
913 lea 16($out),$out # $out points to the next output block
914 jae .Loop_aes_cfb128_dec_mid_1x
915
916 xor %eax,%eax # reset $num when processing full blocks
917
918 vmovdqu $temp,($ivp) # latest plaintext block is next decryption input
919
920 .Laes_cfb128_dec_post:
921
922 ###########################################################
923 # last partial block post-processing
924 ###########################################################
925
926 test $len,$len # check if the last block is partial
927 jz .Laes_cfb128_dec_zero_all
928 ___
929
930 &vaes_encrypt_block_1x(".Loop_aes_cfb128_dec_post");
931
932 $code.=<<___;
933
934 mov $len,%rax # num=$len
935 mov \$1,%r11 # build a mask with the least significant $len bits set
936 mov %dl,%cl # $len is left shift counter less than 16
937 shlq %cl,%r11
938 dec %r11 # mask is 2^$len-1
939 kmovq %r11,%k1
940
941 vmovdqu8 ($inp),%xmm1{%k1}{z} # read $len input bytes, zero the rest to not impact XOR
942 vpxor $temp,%xmm1,%xmm0 # CipherFeedBack XOR
943 vmovdqu8 %xmm0,($out){%k1} # write $len output bytes
944 vpblendmb %xmm1,$temp,${temp}{%k1} # blend $len input bytes into iv
945
946 vmovdqu8 $temp,($ivp) # write chained/streaming iv
947
948 # clear registers
949
950 .Laes_cfb128_dec_zero_all:
951 vpxord %xmm17,%xmm17,%xmm17 # clear the AES key schedule
952 vpxord %xmm18,%xmm18,%xmm18 # zero the upper lanes of zmm registers
953 vpxord %xmm19,%xmm19,%xmm19
954 vpxord %xmm20,%xmm20,%xmm20
955 vpxord %xmm21,%xmm21,%xmm21
956 vpxord %xmm22,%xmm22,%xmm22
957 vpxord %xmm23,%xmm23,%xmm23
958 vpxord %xmm24,%xmm24,%xmm24
959 vpxord %xmm25,%xmm25,%xmm25
960 vpxord %xmm26,%xmm26,%xmm26
961 vpxord %xmm27,%xmm27,%xmm27
962 vpxord %xmm28,%xmm28,%xmm28
963 vpxord %xmm29,%xmm29,%xmm29
964 vpxord %xmm30,%xmm30,%xmm30
965 vpxord %xmm31,%xmm31,%xmm31
966
967 vpxord %xmm3,%xmm3,%xmm3 # clear registers used during AES encryption
968 vpxord %xmm4,%xmm4,%xmm4
969 vpxord %xmm5,%xmm5,%xmm5
970 vpxord %xmm6,%xmm6,%xmm6
971 vpxord %xmm16,%xmm16,%xmm16
972
973 .Laes_cfb128_dec_zero_pre:
974
975 vpxord %xmm0,%xmm0,%xmm0 # clear the rest of the registers
976 vpxord %xmm1,%xmm1,%xmm1
977 vpxord %xmm2,%xmm2,%xmm2
978
979 vzeroupper
980 ___
981
982 $code.=<<___ if($win64);
983 vmovdqu (%rsp),%xmm6 # xmm6 needs to be maintained for Windows
984 add \$16,%rsp
985 .cfi_adjust_cfa_offset -16
986 ___
987
988 $code.=<<___;
989 mov %rax,($nump) # num is in/out, update for future/chained calls
990
991 .Laes_cfb128_vaes_dec_done:
992 ret
993 .cfi_endproc
994 .size ossl_aes_cfb128_vaes_dec,.-ossl_aes_cfb128_vaes_dec
995 ___
996
997 } else {
998
999 $code .= <<___;
1000 .globl ossl_aes_cfb128_vaes_enc
1001 .globl ossl_aes_cfb128_vaes_dec
1002
1003 # Mock implementations of AES-CFB128 encryption/decryption
1004 # that always fail. Should not be executed under normal circumstances.
1005
1006 ossl_aes_cfb128_vaes_enc:
1007 ossl_aes_cfb128_vaes_dec:
1008 .byte 0x0f,0x0b # Undefined Instruction in the Intel architecture
1009 # Raises the Invalid Opcode exception
1010 ret
1011
1012 #################################################################
1013 # Signature:
1014 #
1015 # int ossl_aes_cfb128_vaes_eligible(void);
1016 #
1017 # Always returns 0 (not eligible), meaning that tooling does not support
1018 # the Intel AVX-512 extensions. Signals higher level code to fallback
1019 # to an alternative implementation.
1020 #################################################################
1021
1022 .globl ossl_aes_cfb128_vaes_eligible
1023 .type ossl_aes_cfb128_vaes_eligible,\@abi-omnipotent
1024 ossl_aes_cfb128_vaes_eligible:
1025 xor %eax,%eax
1026 ret
1027 .size ossl_aes_cfb128_vaes_eligible, .-ossl_aes_cfb128_vaes_eligible
1028 ___
1029 }
1030
1031 print $code;
1032
1033 close STDOUT or die "error closing STDOUT: $!";