3 # ====================================================================
4 # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
5 # project. The module is, however, dual licensed under OpenSSL and
6 # CRYPTOGAMS licenses depending on where you obtain it. For further
7 # details see http://www.openssl.org/~appro/cryptogams/.
8 # ====================================================================
12 # The module implements "4-bit" GCM GHASH function and underlying
13 # single multiplication operation in GF(2^128). "4-bit" means that it
14 # uses 256 bytes per-key table [+64/128 bytes fixed table]. It has two
15 # code paths: vanilla x86 and vanilla MMX. Former will be executed on
16 # 486 and Pentium, latter on all others. Performance results are for
17 # streamed GHASH subroutine and are expressed in cycles per processed
18 # byte, less is better:
20 # gcc 2.95.3(*) MMX assembler x86 assembler
22 # Pentium 100/112(**) - 50
24 # P4 96 /122 30 84(***)
25 # Opteron 50 /71 21 30
26 # Core2 54 /68 12.5 18
28 # (*) gcc 3.4.x was observed to generate few percent slower code,
29 # which is one of reasons why 2.95.3 results were chosen,
30 # another reason is lack of 3.4.x results for older CPUs;
31 # (**) second number is result for code compiled with -fPIC flag,
32 # which is actually more relevant, because assembler code is
33 # position-independent;
34 # (***) see comment in non-MMX routine for further details;
36 # To summarize, it's >2-3 times faster than gcc-generated code. To
37 # anchor it to something else SHA1 assembler processes one byte in
38 # 11-13 cycles on contemporary x86 cores.
42 # Add PCLMULQDQ version performing at 2.13 cycles per processed byte.
43 # The question is how close is it to theoretical limit? The pclmulqdq
44 # instruction latency appears to be 14 cycles and there can't be more
45 # than 2 of them executing at any given time. This means that single
46 # Karatsuba multiplication would take 28 cycles *plus* few cycles for
47 # pre- and post-processing. Then multiplication has to be followed by
48 # modulo-reduction. Given that aggregated reduction method [see
49 # "Carry-less Multiplication and Its Usage for Computing the GCM Mode"
50 # white paper by Intel] allows you to perform reduction only once in
51 # a while we can assume that asymptotic performance can be estimated
52 # as (28+Tmod/Naggr)/16, where Tmod is time to perform reduction
53 # and Naggr is the aggregation factor.
55 # Before we proceed to this implementation let's have closer look at
56 # the best-performing code suggested by Intel in their white paper.
57 # By tracing inter-register dependencies Tmod is estimated as ~19
58 # cycles and Naggr is 4, resulting in 2.05 cycles per processed byte.
59 # As implied, this is quite optimistic estimate, because it does not
60 # account for Karatsuba pre- and post-processing, which for a single
61 # multiplication is ~5 cycles. Unfortunately Intel does not provide
62 # performance data for GHASH alone, only for fused GCM mode. But
63 # we can estimate it by subtracting CTR performance result provided
64 # in "AES Instruction Set" white paper: 3.54-1.38=2.16 cycles per
65 # processed byte or 5% off the estimate. It should be noted though
66 # that 3.54 is GCM result for 16KB block size, while 1.38 is CTR for
67 # 1KB block size, meaning that real number is likely to be a bit
68 # further from estimate.
70 # Moving on to the implementation in question. Tmod is estimated as
71 # ~13 cycles and Naggr is 2, giving asymptotic performance of ...
72 # 2.16. How is it possible that measured performance is better than
73 # optimistic theoretical estimate? There is one thing Intel failed
74 # to recognize. By fusing GHASH with CTR former's performance is
75 # really limited to above (Tmul + Tmod/Naggr) equation. But if GHASH
76 # procedure is detached, the modulo-reduction can be interleaved with
77 # Naggr-1 multiplications and under ideal conditions even disappear
78 # from the equation. So that optimistic theoretical estimate for this
79 # implementation is ... 28/16=1.75, and not 2.16. Well, it's probably
80 # way too optimistic, at least for such small Naggr. I'd argue that
81 # (28+Tproc/Naggr), where Tproc is time required for Karatsuba pre-
82 # and post-processing, is more realistic estimate. In this case it
83 # gives ... 1.91 cycles per processed byte. Or in other words,
84 # depending on how well we can interleave reduction and one of the
85 # two multiplications the performance should be betwen 1.91 and 2.16.
86 # As already mentioned, this implementation processes one byte [out
87 # of 1KB buffer] in 2.13 cycles, while x86_64 counterpart - in 2.07.
88 # x86_64 performance is better, because larger register bank allows
89 # to interleave reduction and multiplication better.
91 # Does it make sense to increase Naggr? To start with it's virtually
92 # impossible in 32-bit mode, because of limited register bank
93 # capacity. Otherwise improvement has to be weighed agiainst slower
94 # setup, as well as code size and complexity increase. As even
95 # optimistic estimate doesn't promise 30% performance improvement,
96 # there are currently no plans to increase Naggr.
98 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
99 push(@INC,"${dir}","${dir}../../perlasm");
102 &asm_init($ARGV[0],"ghash-x86.pl",$x86only = $ARGV[$#ARGV] eq "386");
105 for (@ARGV) { $sse2=1 if (/-DOPENSSL_IA32_SSE2/); }
107 ($Zhh,$Zhl,$Zlh,$Zll) = ("ebp","edx","ecx","ebx");
111 $unroll = 0; # Affects x86 loop. Folded loop performs ~7% worse
112 # than unrolled, which has to be weighted against
113 # 2.5x x86-specific code size reduction.
119 &mov ($Zhh,&DWP(4,$Htbl,$Zll));
120 &mov ($Zhl,&DWP(0,$Htbl,$Zll));
121 &mov ($Zlh,&DWP(12,$Htbl,$Zll));
122 &mov ($Zll,&DWP(8,$Htbl,$Zll));
123 &xor ($rem,$rem); # avoid partial register stalls on PIII
125 # shrd practically kills P4, 2.5x deterioration, but P4 has
126 # MMX code-path to execute. shrd runs tad faster [than twice
127 # the shifts, move's and or's] on pre-MMX Pentium (as well as
128 # PIII and Core2), *but* minimizes code size, spares register
129 # and thus allows to fold the loop...
133 &jmp (&label("x86_loop"));
134 &set_label("x86_loop",16);
135 for($i=1;$i<=2;$i++) {
136 &mov (&LB($rem),&LB($Zll));
138 &and (&LB($rem),0xf);
142 &xor ($Zhh,&DWP($off+16,"esp",$rem,4));
144 &mov (&LB($rem),&BP($off,"esp",$cnt));
146 &and (&LB($rem),0xf0);
151 &xor ($Zll,&DWP(8,$Htbl,$rem));
152 &xor ($Zlh,&DWP(12,$Htbl,$rem));
153 &xor ($Zhl,&DWP(0,$Htbl,$rem));
154 &xor ($Zhh,&DWP(4,$Htbl,$rem));
158 &js (&label("x86_break"));
160 &jmp (&label("x86_loop"));
163 &set_label("x86_break",16);
165 for($i=1;$i<32;$i++) {
167 &mov (&LB($rem),&LB($Zll));
169 &and (&LB($rem),0xf);
173 &xor ($Zhh,&DWP($off+16,"esp",$rem,4));
176 &mov (&LB($rem),&BP($off+15-($i>>1),"esp"));
177 &and (&LB($rem),0xf0);
179 &mov (&LB($rem),&BP($off+15-($i>>1),"esp"));
183 &xor ($Zll,&DWP(8,$Htbl,$rem));
184 &xor ($Zlh,&DWP(12,$Htbl,$rem));
185 &xor ($Zhl,&DWP(0,$Htbl,$rem));
186 &xor ($Zhh,&DWP(4,$Htbl,$rem));
202 &function_begin_B("_x86_gmult_4bit_inner");
205 &function_end_B("_x86_gmult_4bit_inner");
208 sub deposit_rem_4bit {
211 &mov (&DWP($bias+0, "esp"),0x0000<<16);
212 &mov (&DWP($bias+4, "esp"),0x1C20<<16);
213 &mov (&DWP($bias+8, "esp"),0x3840<<16);
214 &mov (&DWP($bias+12,"esp"),0x2460<<16);
215 &mov (&DWP($bias+16,"esp"),0x7080<<16);
216 &mov (&DWP($bias+20,"esp"),0x6CA0<<16);
217 &mov (&DWP($bias+24,"esp"),0x48C0<<16);
218 &mov (&DWP($bias+28,"esp"),0x54E0<<16);
219 &mov (&DWP($bias+32,"esp"),0xE100<<16);
220 &mov (&DWP($bias+36,"esp"),0xFD20<<16);
221 &mov (&DWP($bias+40,"esp"),0xD940<<16);
222 &mov (&DWP($bias+44,"esp"),0xC560<<16);
223 &mov (&DWP($bias+48,"esp"),0x9180<<16);
224 &mov (&DWP($bias+52,"esp"),0x8DA0<<16);
225 &mov (&DWP($bias+56,"esp"),0xA9C0<<16);
226 &mov (&DWP($bias+60,"esp"),0xB5E0<<16);
229 $suffix = $x86only ? "" : "_x86";
231 &function_begin("gcm_gmult_4bit".$suffix);
232 &stack_push(16+4+1); # +1 for stack alignment
233 &mov ($inp,&wparam(0)); # load Xi
234 &mov ($Htbl,&wparam(1)); # load Htable
236 &mov ($Zhh,&DWP(0,$inp)); # load Xi[16]
237 &mov ($Zhl,&DWP(4,$inp));
238 &mov ($Zlh,&DWP(8,$inp));
239 &mov ($Zll,&DWP(12,$inp));
241 &deposit_rem_4bit(16);
243 &mov (&DWP(0,"esp"),$Zhh); # copy Xi[16] on stack
244 &mov (&DWP(4,"esp"),$Zhl);
245 &mov (&DWP(8,"esp"),$Zlh);
246 &mov (&DWP(12,"esp"),$Zll);
251 &call ("_x86_gmult_4bit_inner");
254 &mov ($inp,&wparam(0));
257 &mov (&DWP(12,$inp),$Zll);
258 &mov (&DWP(8,$inp),$Zlh);
259 &mov (&DWP(4,$inp),$Zhl);
260 &mov (&DWP(0,$inp),$Zhh);
262 &function_end("gcm_gmult_4bit".$suffix);
264 &function_begin("gcm_ghash_4bit".$suffix);
265 &stack_push(16+4+1); # +1 for 64-bit alignment
266 &mov ($Zll,&wparam(0)); # load Xi
267 &mov ($Htbl,&wparam(1)); # load Htable
268 &mov ($inp,&wparam(2)); # load in
269 &mov ("ecx",&wparam(3)); # load len
271 &mov (&wparam(3),"ecx");
273 &mov ($Zhh,&DWP(0,$Zll)); # load Xi[16]
274 &mov ($Zhl,&DWP(4,$Zll));
275 &mov ($Zlh,&DWP(8,$Zll));
276 &mov ($Zll,&DWP(12,$Zll));
278 &deposit_rem_4bit(16);
280 &set_label("x86_outer_loop",16);
281 &xor ($Zll,&DWP(12,$inp)); # xor with input
282 &xor ($Zlh,&DWP(8,$inp));
283 &xor ($Zhl,&DWP(4,$inp));
284 &xor ($Zhh,&DWP(0,$inp));
285 &mov (&DWP(12,"esp"),$Zll); # dump it on stack
286 &mov (&DWP(8,"esp"),$Zlh);
287 &mov (&DWP(4,"esp"),$Zhl);
288 &mov (&DWP(0,"esp"),$Zhh);
294 &call ("_x86_gmult_4bit_inner");
297 &mov ($inp,&wparam(2));
299 &lea ($inp,&DWP(16,$inp));
300 &cmp ($inp,&wparam(3));
301 &mov (&wparam(2),$inp) if (!$unroll);
302 &jb (&label("x86_outer_loop"));
304 &mov ($inp,&wparam(0)); # load Xi
305 &mov (&DWP(12,$inp),$Zll);
306 &mov (&DWP(8,$inp),$Zlh);
307 &mov (&DWP(4,$inp),$Zhl);
308 &mov (&DWP(0,$inp),$Zhh);
310 &function_end("gcm_ghash_4bit".$suffix);
314 &static_label("rem_4bit");
317 # MMX version performs 2.8 times better on P4 (see comment in non-MMX
318 # routine for further details), 40% better on Opteron and Core2, 50%
319 # better on PIII... In other words effort is considered to be well
322 my $rem_4bit = shift;
328 my ($Zlo,$Zhi) = ("mm0","mm1");
331 &xor ($nlo,$nlo); # avoid partial register stalls on PIII
333 &mov (&LB($nlo),&LB($nhi));
337 &movq ($Zlo,&QWP(8,$Htbl,$nlo));
338 &movq ($Zhi,&QWP(0,$Htbl,$nlo));
340 &jmp (&label("mmx_loop"));
342 &set_label("mmx_loop",16);
347 &pxor ($Zlo,&QWP(8,$Htbl,$nhi));
348 &mov (&LB($nlo),&BP(0,$inp,$cnt));
350 &pxor ($Zhi,&QWP(0,$rem_4bit,$rem,8));
353 &pxor ($Zhi,&QWP(0,$Htbl,$nhi));
356 &js (&label("mmx_break"));
364 &pxor ($Zlo,&QWP(8,$Htbl,$nlo));
366 &pxor ($Zhi,&QWP(0,$rem_4bit,$rem,8));
368 &pxor ($Zhi,&QWP(0,$Htbl,$nlo));
370 &jmp (&label("mmx_loop"));
372 &set_label("mmx_break",16);
379 &pxor ($Zlo,&QWP(8,$Htbl,$nlo));
381 &pxor ($Zhi,&QWP(0,$rem_4bit,$rem,8));
383 &pxor ($Zhi,&QWP(0,$Htbl,$nlo));
390 &pxor ($Zlo,&QWP(8,$Htbl,$nhi));
392 &pxor ($Zhi,&QWP(0,$rem_4bit,$rem,8));
394 &pxor ($Zhi,&QWP(0,$Htbl,$nhi));
397 &psrlq ($Zlo,32); # lower part of Zlo is already there
409 &function_begin("gcm_gmult_4bit_mmx");
410 &mov ($inp,&wparam(0)); # load Xi
411 &mov ($Htbl,&wparam(1)); # load Htable
413 &call (&label("pic_point"));
414 &set_label("pic_point");
416 &lea ("eax",&DWP(&label("rem_4bit")."-".&label("pic_point"),"eax"));
418 &movz ($Zll,&BP(15,$inp));
420 &mmx_loop($inp,"eax");
423 &mov (&DWP(12,$inp),$Zll);
424 &mov (&DWP(4,$inp),$Zhl);
425 &mov (&DWP(8,$inp),$Zlh);
426 &mov (&DWP(0,$inp),$Zhh);
427 &function_end("gcm_gmult_4bit_mmx");
429 # Streamed version performs 20% better on P4, 7% on Opteron,
430 # 10% on Core2 and PIII...
431 &function_begin("gcm_ghash_4bit_mmx");
432 &mov ($Zhh,&wparam(0)); # load Xi
433 &mov ($Htbl,&wparam(1)); # load Htable
434 &mov ($inp,&wparam(2)); # load in
435 &mov ($Zlh,&wparam(3)); # load len
437 &call (&label("pic_point"));
438 &set_label("pic_point");
440 &lea ("eax",&DWP(&label("rem_4bit")."-".&label("pic_point"),"eax"));
443 &mov (&wparam(3),$Zlh); # len to point at the end of input
444 &stack_push(4+1); # +1 for stack alignment
446 &mov ($Zll,&DWP(12,$Zhh)); # load Xi[16]
447 &mov ($Zhl,&DWP(4,$Zhh));
448 &mov ($Zlh,&DWP(8,$Zhh));
449 &mov ($Zhh,&DWP(0,$Zhh));
450 &jmp (&label("mmx_outer_loop"));
452 &set_label("mmx_outer_loop",16);
453 &xor ($Zll,&DWP(12,$inp));
454 &xor ($Zhl,&DWP(4,$inp));
455 &xor ($Zlh,&DWP(8,$inp));
456 &xor ($Zhh,&DWP(0,$inp));
457 &mov (&DWP(12,"esp"),$Zll);
458 &mov (&DWP(4,"esp"),$Zhl);
459 &mov (&DWP(8,"esp"),$Zlh);
460 &mov (&DWP(0,"esp"),$Zhh);
464 &mmx_loop("esp","eax");
466 &lea ($inp,&DWP(16,$inp));
467 &cmp ($inp,&wparam(3));
468 &jb (&label("mmx_outer_loop"));
470 &mov ($inp,&wparam(0)); # load Xi
472 &mov (&DWP(12,$inp),$Zll);
473 &mov (&DWP(4,$inp),$Zhl);
474 &mov (&DWP(8,$inp),$Zlh);
475 &mov (&DWP(0,$inp),$Zhh);
478 &function_end("gcm_ghash_4bit_mmx");
481 ######################################################################
490 ($Xi,$Xhi)=("xmm0","xmm1"); $Hkey="xmm2";
491 ($T1,$T2,$T3)=("xmm3","xmm4","xmm5");
492 ($Xn,$Xhn)=("xmm6","xmm7");
494 &static_label("bswap");
496 sub clmul64x64_T2 { # minimal "register" pressure
497 my ($Xhi,$Xi,$Hkey)=@_;
499 &movdqa ($Xhi,$Xi); #
500 &pshufd ($T1,$Xi,0b01001110);
501 &pshufd ($T2,$Hkey,0b01001110);
505 &pclmulqdq ($Xi,$Hkey,0x00); #######
506 &pclmulqdq ($Xhi,$Hkey,0x11); #######
507 &pclmulqdq ($T1,$T2,0x00); #######
519 # Even though this subroutine offers visually better ILP, it
520 # was empirically found to be a tad slower than above version.
521 # At least in gcm_ghash_clmul context. But it's just as well,
522 # because loop modulo-scheduling is possible only thanks to
523 # minimized "register" pressure...
524 my ($Xhi,$Xi,$Hkey)=@_;
528 &pclmulqdq ($Xi,$Hkey,0x00); #######
529 &pclmulqdq ($Xhi,$Hkey,0x11); #######
530 &pshufd ($T2,$T1,0b01001110); #
531 &pshufd ($T3,$Hkey,0b01001110);
534 &pclmulqdq ($T2,$T3,0x00); #######
545 if (1) { # Algorithm 9 with <<1 twist.
546 # Reduction is shorter and uses only two
547 # temporary registers, which makes it better
548 # candidate for interleaving with 64x64
549 # multiplication. Pre-modulo-scheduled loop
550 # was found to be ~20% faster than Algorithm 5
551 # below. Algorithm 9 was then chosen and
552 # optimized further...
554 sub reduction_alg9 { # 17/13 times faster than Intel version
581 &function_begin_B("gcm_init_clmul");
582 &mov ($Htbl,&wparam(0));
583 &mov ($Xip,&wparam(1));
585 &call (&label("pic"));
588 &lea ($const,&DWP(&label("bswap")."-".&label("pic"),$const));
590 &movdqu ($Hkey,&QWP(0,$Xip));
591 &pshufd ($Hkey,$Hkey,0b01001110);# dword swap
594 &pshufd ($T2,$Hkey,0b11111111); # broadcast uppermost dword
599 &pcmpgtd ($T3,$T2); # broadcast carry bit
601 &por ($Hkey,$T1); # H<<=1
604 &pand ($T3,&QWP(16,$const)); # 0x1c2_polynomial
605 &pxor ($Hkey,$T3); # if(carry) H^=0x1c2_polynomial
609 &clmul64x64_T2 ($Xhi,$Xi,$Hkey);
610 &reduction_alg9 ($Xhi,$Xi);
612 &movdqu (&QWP(0,$Htbl),$Hkey); # save H
613 &movdqu (&QWP(16,$Htbl),$Xi); # save H^2
616 &function_end_B("gcm_init_clmul");
618 &function_begin_B("gcm_gmult_clmul");
619 &mov ($Xip,&wparam(0));
620 &mov ($Htbl,&wparam(1));
622 &call (&label("pic"));
625 &lea ($const,&DWP(&label("bswap")."-".&label("pic"),$const));
627 &movdqu ($Xi,&QWP(0,$Xip));
628 &movdqa ($T3,&QWP(0,$const));
629 &movdqu ($Hkey,&QWP(0,$Htbl));
632 &clmul64x64_T2 ($Xhi,$Xi,$Hkey);
633 &reduction_alg9 ($Xhi,$Xi);
636 &movdqu (&QWP(0,$Xip),$Xi);
639 &function_end_B("gcm_gmult_clmul");
641 &function_begin("gcm_ghash_clmul");
642 &mov ($Xip,&wparam(0));
643 &mov ($Htbl,&wparam(1));
644 &mov ($inp,&wparam(2));
645 &mov ($len,&wparam(3));
647 &call (&label("pic"));
650 &lea ($const,&DWP(&label("bswap")."-".&label("pic"),$const));
652 &movdqu ($Xi,&QWP(0,$Xip));
653 &movdqa ($T3,&QWP(0,$const));
654 &movdqu ($Hkey,&QWP(0,$Htbl));
658 &jz (&label("odd_tail"));
661 # Xi+2 =[H*(Ii+1 + Xi+1)] mod P =
662 # [(H*Ii+1) + (H*Xi+1)] mod P =
663 # [(H*Ii+1) + H^2*(Ii+Xi)] mod P
665 &movdqu ($T1,&QWP(0,$inp)); # Ii
666 &movdqu ($Xn,&QWP(16,$inp)); # Ii+1
669 &pxor ($Xi,$T1); # Ii+Xi
671 &clmul64x64_T2 ($Xhn,$Xn,$Hkey); # H*Ii+1
672 &movdqu ($Hkey,&QWP(16,$Htbl)); # load H^2
674 &lea ($inp,&DWP(32,$inp)); # i+=2
676 &jbe (&label("even_tail"));
678 &set_label("mod_loop");
679 &clmul64x64_T2 ($Xhi,$Xi,$Hkey); # H^2*(Ii+Xi)
680 &movdqu ($T1,&QWP(0,$inp)); # Ii
681 &movdqu ($Hkey,&QWP(0,$Htbl)); # load H
683 &pxor ($Xi,$Xn); # (H*Ii+1) + H^2*(Ii+Xi)
686 &movdqu ($Xn,&QWP(16,$inp)); # Ii+1
690 &movdqa ($T3,$Xn); #&clmul64x64_TX ($Xhn,$Xn,$Hkey); H*Ii+1
692 &pxor ($Xhi,$T1); # "Ii+Xi", consume early
694 &movdqa ($T1,$Xi) #&reduction_alg9($Xhi,$Xi); 1st phase
699 &pclmulqdq ($Xn,$Hkey,0x00); #######
705 &pshufd ($T1,$T3,0b01001110);
708 &pshufd ($T3,$Hkey,0b01001110);
711 &pclmulqdq ($Xhn,$Hkey,0x11); #######
712 &movdqa ($T2,$Xi); # 2nd phase
721 &pclmulqdq ($T1,$T3,0x00); #######
722 &movdqu ($Hkey,&QWP(16,$Htbl)); # load H^2
731 &movdqa ($T3,&QWP(0,$const));
733 &lea ($inp,&DWP(32,$inp));
735 &ja (&label("mod_loop"));
737 &set_label("even_tail");
738 &clmul64x64_T2 ($Xhi,$Xi,$Hkey); # H^2*(Ii+Xi)
740 &pxor ($Xi,$Xn); # (H*Ii+1) + H^2*(Ii+Xi)
743 &reduction_alg9 ($Xhi,$Xi);
746 &jnz (&label("done"));
748 &movdqu ($Hkey,&QWP(0,$Htbl)); # load H
749 &set_label("odd_tail");
750 &movdqu ($T1,&QWP(0,$inp)); # Ii
752 &pxor ($Xi,$T1); # Ii+Xi
754 &clmul64x64_T2 ($Xhi,$Xi,$Hkey); # H*(Ii+Xi)
755 &reduction_alg9 ($Xhi,$Xi);
759 &movdqu (&QWP(0,$Xip),$Xi);
760 &function_end("gcm_ghash_clmul");
762 } else { # Algorith 5. Kept for reference purposes.
764 sub reduction_alg5 { # 19/16 times faster than Intel version
809 &function_begin_B("gcm_init_clmul");
810 &mov ($Htbl,&wparam(0));
811 &mov ($Xip,&wparam(1));
813 &call (&label("pic"));
816 &lea ($const,&DWP(&label("bswap")."-".&label("pic"),$const));
818 &movdqu ($Hkey,&QWP(0,$Xip));
819 &pshufd ($Hkey,$Hkey,0b01001110);# dword swap
823 &clmul64x64_T3 ($Xhi,$Xi,$Hkey);
824 &reduction_alg5 ($Xhi,$Xi);
826 &movdqu (&QWP(0,$Htbl),$Hkey); # save H
827 &movdqu (&QWP(16,$Htbl),$Xi); # save H^2
830 &function_end_B("gcm_init_clmul");
832 &function_begin_B("gcm_gmult_clmul");
833 &mov ($Xip,&wparam(0));
834 &mov ($Htbl,&wparam(1));
836 &call (&label("pic"));
839 &lea ($const,&DWP(&label("bswap")."-".&label("pic"),$const));
841 &movdqu ($Xi,&QWP(0,$Xip));
842 &movdqa ($Xn,&QWP(0,$const));
843 &movdqu ($Hkey,&QWP(0,$Htbl));
846 &clmul64x64_T3 ($Xhi,$Xi,$Hkey);
847 &reduction_alg5 ($Xhi,$Xi);
850 &movdqu (&QWP(0,$Xip),$Xi);
853 &function_end_B("gcm_gmult_clmul");
855 &function_begin("gcm_ghash_clmul");
856 &mov ($Xip,&wparam(0));
857 &mov ($Htbl,&wparam(1));
858 &mov ($inp,&wparam(2));
859 &mov ($len,&wparam(3));
861 &call (&label("pic"));
864 &lea ($const,&DWP(&label("bswap")."-".&label("pic"),$const));
866 &movdqu ($Xi,&QWP(0,$Xip));
867 &movdqa ($T3,&QWP(0,$const));
868 &movdqu ($Hkey,&QWP(0,$Htbl));
872 &jz (&label("odd_tail"));
875 # Xi+2 =[H*(Ii+1 + Xi+1)] mod P =
876 # [(H*Ii+1) + (H*Xi+1)] mod P =
877 # [(H*Ii+1) + H^2*(Ii+Xi)] mod P
879 &movdqu ($T1,&QWP(0,$inp)); # Ii
880 &movdqu ($Xn,&QWP(16,$inp)); # Ii+1
883 &pxor ($Xi,$T1); # Ii+Xi
885 &clmul64x64_T3 ($Xhn,$Xn,$Hkey); # H*Ii+1
886 &movdqu ($Hkey,&QWP(16,$Htbl)); # load H^2
889 &lea ($inp,&DWP(32,$inp)); # i+=2
890 &jbe (&label("even_tail"));
892 &set_label("mod_loop");
893 &clmul64x64_T3 ($Xhi,$Xi,$Hkey); # H^2*(Ii+Xi)
894 &movdqu ($Hkey,&QWP(0,$Htbl)); # load H
896 &pxor ($Xi,$Xn); # (H*Ii+1) + H^2*(Ii+Xi)
899 &reduction_alg5 ($Xhi,$Xi);
902 &movdqa ($T3,&QWP(0,$const));
903 &movdqu ($T1,&QWP(0,$inp)); # Ii
904 &movdqu ($Xn,&QWP(16,$inp)); # Ii+1
907 &pxor ($Xi,$T1); # Ii+Xi
909 &clmul64x64_T3 ($Xhn,$Xn,$Hkey); # H*Ii+1
910 &movdqu ($Hkey,&QWP(16,$Htbl)); # load H^2
913 &lea ($inp,&DWP(32,$inp));
914 &ja (&label("mod_loop"));
916 &set_label("even_tail");
917 &clmul64x64_T3 ($Xhi,$Xi,$Hkey); # H^2*(Ii+Xi)
919 &pxor ($Xi,$Xn); # (H*Ii+1) + H^2*(Ii+Xi)
922 &reduction_alg5 ($Xhi,$Xi);
924 &movdqa ($T3,&QWP(0,$const));
926 &jnz (&label("done"));
928 &movdqu ($Hkey,&QWP(0,$Htbl)); # load H
929 &set_label("odd_tail");
930 &movdqu ($T1,&QWP(0,$inp)); # Ii
932 &pxor ($Xi,$T1); # Ii+Xi
934 &clmul64x64_T3 ($Xhi,$Xi,$Hkey); # H*(Ii+Xi)
935 &reduction_alg5 ($Xhi,$Xi);
937 &movdqa ($T3,&QWP(0,$const));
940 &movdqu (&QWP(0,$Xip),$Xi);
941 &function_end("gcm_ghash_clmul");
945 &set_label("bswap",64);
946 &data_byte(15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0);
947 &data_byte(1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xc2); # 0x1c2_polynomial
950 &set_label("rem_4bit",64);
951 &data_word(0,0x0000<<16,0,0x1C20<<16,0,0x3840<<16,0,0x2460<<16);
952 &data_word(0,0x7080<<16,0,0x6CA0<<16,0,0x48C0<<16,0,0x54E0<<16);
953 &data_word(0,0xE100<<16,0,0xFD20<<16,0,0xD940<<16,0,0xC560<<16);
954 &data_word(0,0x9180<<16,0,0x8DA0<<16,0,0xA9C0<<16,0,0xB5E0<<16);
957 &asciz("GHASH for x86, CRYPTOGAMS by <appro\@openssl.org>");