2 # Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the Apache License 2.0 (the "License"). You may not use
5 # this file except in compliance with the License. You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
10 # ====================================================================
11 # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
12 # project. The module is, however, dual licensed under OpenSSL and
13 # CRYPTOGAMS licenses depending on where you obtain it. For further
14 # details see http://www.openssl.org/~appro/cryptogams/.
15 # ====================================================================
17 # This module implements Poly1305 hash for s390x.
21 # ~6.6/2.3 cpb on z10/z196+, >2x improvement over compiler-generated
22 # code. For older compiler improvement coefficient is >3x, because
23 # then base 2^64 and base 2^32 implementations are compared.
25 # On side note, z13 enables vector base 2^26 implementation...
30 # Add vx code path (base 2^26).
32 # Copyright IBM Corp. 2019
33 # Author: Patrick Steuer <patrick.steuer@de.ibm.com>
38 # Add vector base 2^26 implementation. It's problematic to accurately
39 # measure performance, because reference system is hardly idle. But
40 # it's sub-cycle, i.e. less than 1 cycle per processed byte, and it's
41 # >=20% faster than IBM's submission on long inputs, and much faster on
42 # short ones, because calculation of key powers is postponed till we
43 # know that input is long enough to justify the additional overhead.
48 use perlasm::s390x qw(:DEFAULT :VX AUTOLOAD LABEL INCLUDE);
53 if ($flavour =~ /3[12]/) {
62 while (($output=shift) && ($output!~/\w[\w\-]*\.\w+$/)) {}
64 my $stdframe=16*$SIZE_T+4*8;
67 my ($ctx,$inp,$len,$padbit) = map("%r$_",(2..5));
69 PERLASM_BEGIN($output);
71 INCLUDE ("s390x_arch.h");
75 # static void poly1305_init(void *ctx, const unsigned char key[16])
77 GLOBL ("poly1305_init");
78 TYPE ("poly1305_init","\@function");
80 LABEL ("poly1305_init");
83 stg ("%r0","0($ctx)"); # zero hash value
84 stg ("%r0","8($ctx)");
85 stg ("%r0","16($ctx)");
86 st ("%r0","24($ctx)"); # clear is_base2_26
87 lgr ("%r5",$ctx); # reassign $ctx
90 &{$z? \&clgr:\&clr} ($inp,"%r0");
93 lrvg ("%r2","0($inp)"); # load little-endian key
94 lrvg ("%r3","8($inp)");
96 nihl ("%r1",0xffc0); # 0xffffffc0ffffffff
97 srlg ("%r0","%r1",4); # 0x0ffffffc0fffffff
99 nill ("%r1",0xfffc); # 0x0ffffffc0ffffffc
104 stmg ("%r2","%r3","32(%r5)");
106 larl ("%r1","OPENSSL_s390xcap_P");
107 lg ("%r0","16(%r1)");
108 srlg ("%r0","%r0",62);
109 nill ("%r0",1); # extract vx bit
111 larl ("%r1",".Lpoly1305_blocks");
112 larl ("%r2",".Lpoly1305_blocks_vx");
113 larl ("%r3",".Lpoly1305_emit");
114 &{$z? \&xgr:\&xr} ("%r2","%r1"); # select between scalar and vector
115 &{$z? \&ngr:\&nr} ("%r2","%r0");
116 &{$z? \&xgr:\&xr} ("%r2","%r1");
117 &{$z? \&stmg:\&stm} ("%r2","%r3","0(%r4)");
121 SIZE ("poly1305_init",".-poly1305_init");
125 # static void poly1305_blocks(void *ctx, const unsigned char *inp,
126 # size_t len, u32 padbit)
128 my ($d0hi,$d0lo,$d1hi,$d1lo,$t0,$h0,$t1,$h1,$h2) = map("%r$_",(6..14));
129 my ($r0,$r1,$s1) = map("%r$_",(0..2));
131 GLOBL ("poly1305_blocks");
132 TYPE ("poly1305_blocks","\@function");
134 LABEL ("poly1305_blocks");
135 LABEL (".Lpoly1305_blocks");
136 &{$z? \<gr:\<r} ("%r0",$len);
139 &{$z? \&stmg:\&stm} ("%r6","%r14","6*$SIZE_T($sp)");
141 lg ($h0,"0($ctx)"); # load hash value
145 LABEL (".Lpoly1305_blocks_entry");
151 llgfr ($padbit,$padbit); # clear upper half, much needed with
153 lg ($r0,"32($ctx)"); # load key
156 &{$z? \&stg:\&st} ($ctx,"2*$SIZE_T($sp)"); # off-load $ctx
158 algr ($s1,$r1); # s1 = r1 + r1>>2
163 lrvg ($d0lo,"0($inp)"); # load little-endian input
164 lrvg ($d1lo,"8($inp)");
165 la ($inp,"16($inp)");
167 algr ($d0lo,$h0); # accumulate input
172 mlgr ($d0hi,$r0); # h0*r0 -> $d0hi:$d0lo
174 mlgr ($d1hi,$s1); # h1*5*r1 -> $d1hi:$d1lo
176 mlgr ($t0,$r1); # h0*r1 -> $t0:$h0
177 mlgr ($t1,$r0); # h1*r0 -> $t1:$h1
187 msgr ($d1lo,$s1); # h2*s1
188 msgr ($h2,$r0); # h2*r0
191 alcgr ($t1,$d1hi); # $d1hi is zero
196 lghi ($h0,-4); # final reduction step
204 alcgr ($h1,$d1hi); # $d1hi is still zero
205 alcgr ($h2,$d1hi); # $d1hi is still zero
207 &{$z? \&brctg:\&brct} ($len,".Loop");
209 &{$z? \&lg:\&l} ($ctx,"2*$SIZE_T($sp)");# restore $ctx
211 stg ($h0,"0($ctx)"); # store hash value
213 stg ($h2,"16($ctx)");
215 &{$z? \&lmg:\&lm} ("%r6","%r14","6*$SIZE_T($sp)");
218 SIZE ("poly1305_blocks",".-poly1305_blocks");
222 # static void poly1305_blocks_vx(void *ctx, const unsigned char *inp,
223 # size_t len, u32 padbit)
225 my ($H0, $H1, $H2, $H3, $H4) = map("%v$_",(0..4));
226 my ($I0, $I1, $I2, $I3, $I4) = map("%v$_",(5..9));
227 my ($R0, $R1, $S1, $R2, $S2) = map("%v$_",(10..14));
228 my ($R3, $S3, $R4, $S4) = map("%v$_",(15..18));
229 my ($ACC0, $ACC1, $ACC2, $ACC3, $ACC4) = map("%v$_",(19..23));
230 my ($T1, $T2, $T3, $T4) = map("%v$_",(24..27));
231 my ($mask26,$bswaplo,$bswaphi,$bswapmi) = map("%v$_",(28..31));
233 my ($d2,$d0,$h0,$d1,$h1,$h2)=map("%r$_",(9..14));
235 TYPE ("poly1305_blocks_vx","\@function");
237 LABEL ("poly1305_blocks_vx");
238 LABEL (".Lpoly1305_blocks_vx");
239 &{$z? \&clgfi:\&clfi} ($len,128);
240 jhe ("__poly1305_blocks_vx");
242 &{$z? \&stmg:\&stm} ("%r6","%r14","6*$SIZE_T($sp)");
248 llgfr ("%r0",$d0); # base 2^26 -> base 2^64
254 sllg ("%r0","%r0",26);
258 sllg ("%r1","%r1",14);
267 llgf ("%r0","24($ctx)"); # is_base2_26
270 xgr ($h0,$d0); # choose between radixes
281 st ("%r0","24($ctx)"); # clear is_base2_26
283 j (".Lpoly1305_blocks_entry");
284 SIZE ("poly1305_blocks_vx",".-poly1305_blocks_vx");
286 TYPE ("__poly1305_mul","\@function");
288 LABEL ("__poly1305_mul");
289 vmlof ($ACC0,$H0,$R0);
290 vmlof ($ACC1,$H0,$R1);
291 vmlof ($ACC2,$H0,$R2);
292 vmlof ($ACC3,$H0,$R3);
293 vmlof ($ACC4,$H0,$R4);
295 vmalof ($ACC0,$H1,$S4,$ACC0);
296 vmalof ($ACC1,$H1,$R0,$ACC1);
297 vmalof ($ACC2,$H1,$R1,$ACC2);
298 vmalof ($ACC3,$H1,$R2,$ACC3);
299 vmalof ($ACC4,$H1,$R3,$ACC4);
301 vmalof ($ACC0,$H2,$S3,$ACC0);
302 vmalof ($ACC1,$H2,$S4,$ACC1);
303 vmalof ($ACC2,$H2,$R0,$ACC2);
304 vmalof ($ACC3,$H2,$R1,$ACC3);
305 vmalof ($ACC4,$H2,$R2,$ACC4);
307 vmalof ($ACC0,$H3,$S2,$ACC0);
308 vmalof ($ACC1,$H3,$S3,$ACC1);
309 vmalof ($ACC2,$H3,$S4,$ACC2);
310 vmalof ($ACC3,$H3,$R0,$ACC3);
311 vmalof ($ACC4,$H3,$R1,$ACC4);
313 vmalof ($ACC0,$H4,$S1,$ACC0);
314 vmalof ($ACC1,$H4,$S2,$ACC1);
315 vmalof ($ACC2,$H4,$S3,$ACC2);
316 vmalof ($ACC3,$H4,$S4,$ACC3);
317 vmalof ($ACC4,$H4,$R0,$ACC4);
319 ################################################################
322 vesrlg ($H4,$ACC3,26);
323 vesrlg ($H1,$ACC0,26);
324 vn ($H3,$ACC3,$mask26);
325 vn ($H0,$ACC0,$mask26);
326 vag ($H4,$H4,$ACC4); # h3 -> h4
327 vag ($H1,$H1,$ACC1); # h0 -> h1
329 vesrlg ($ACC4,$H4,26);
330 vesrlg ($ACC1,$H1,26);
331 vn ($H4,$H4,$mask26);
332 vn ($H1,$H1,$mask26);
334 vag ($H2,$ACC2,$ACC1); # h1 -> h2
336 veslg ($ACC4,$ACC4,2); # <<2
337 vesrlg ($ACC2,$H2,26);
338 vn ($H2,$H2,$mask26);
339 vag ($H0,$H0,$ACC4); # h4 -> h0
340 vag ($H3,$H3,$ACC2); # h2 -> h3
342 vesrlg ($ACC0,$H0,26);
343 vesrlg ($ACC3,$H3,26);
344 vn ($H0,$H0,$mask26);
345 vn ($H3,$H3,$mask26);
346 vag ($H1,$H1,$ACC0); # h0 -> h1
347 vag ($H4,$H4,$ACC3); # h3 -> h4
349 SIZE ("__poly1305_mul",".-__poly1305_mul");
351 TYPE ("__poly1305_blocks_vx","\@function");
353 LABEL ("__poly1305_blocks_vx");
354 &{$z? \&lgr:\&lr} ("%r0",$sp);
355 &{$z? \&stmg:\&stm} ("%r10","%r15","10*$SIZE_T($sp)");
357 std ("%f4","16*$SIZE_T+2*8($sp)");
358 std ("%f6","16*$SIZE_T+3*8($sp)");
359 ahi ($sp,-$stdframe);
360 st ("%r0","0($sp)"); # back-chain
362 llgfr ($len,$len); # so that srlg works on $len
364 aghi ($sp,"-($stdframe+8*8)");
365 stg ("%r0","0($sp)"); # back-chain
367 std ("%f8","$stdframe+0*8($sp)");
368 std ("%f9","$stdframe+1*8($sp)");
369 std ("%f10","$stdframe+2*8($sp)");
370 std ("%f11","$stdframe+3*8($sp)");
371 std ("%f12","$stdframe+4*8($sp)");
372 std ("%f13","$stdframe+5*8($sp)");
373 std ("%f14","$stdframe+6*8($sp)");
374 std ("%f15","$stdframe+7*8($sp)");
376 larl ("%r1",".Lconst");
377 vgmg ($mask26,38,63);
378 vlm ($bswaplo,$bswapmi,"16(%r1)");
380 < ("%r0","24($ctx)"); # is_base2_26?
383 lg ($h0,"32($ctx)"); # load key base 2^64
386 risbg ($d0,$h0,38,0x80+63,38); # base 2^64 -> 2^26
388 risbg ($h0,$h0,38,0x80+63,0);
390 risbg ($d1,$h1,38,51,12);
392 risbg ($d0,$h1,38,63,50);
407 vag ($S1,$S1,$R1); # * 5
412 brasl ("%r14","__poly1305_mul"); # r^1:- * r^1:-
414 vpdi ($R0,$H0,$R0,0); # r^2:r^1
415 vpdi ($R1,$H1,$R1,0);
416 vpdi ($R2,$H2,$R2,0);
417 vpdi ($R3,$H3,$R3,0);
418 vpdi ($R4,$H4,$R4,0);
419 vpdi ($H0,$H0,$H0,0); # r^2:r^2
420 vpdi ($H1,$H1,$H1,0);
421 vpdi ($H2,$H2,$H2,0);
422 vpdi ($H3,$H3,$H3,0);
423 vpdi ($H4,$H4,$H4,0);
428 vag ($S1,$S1,$R1); # * 5
433 brasl ("%r14,__poly1305_mul"); # r^2:r^2 * r^2:r^1
435 vl ($I0,"0(%r1)"); # borrow $I0
436 vperm ($R0,$R0,$H0,$I0); # r^2:r^4:r^1:r^3
437 vperm ($R1,$R1,$H1,$I0);
438 vperm ($R2,$R2,$H2,$I0);
439 vperm ($R3,$R3,$H3,$I0);
440 vperm ($R4,$R4,$H4,$I0);
445 vaf ($S1,$S1,$R1); # * 5
450 lg ($h0,"0($ctx)"); # load hash base 2^64
460 risbg ($d0,$h0,38,0x80+63,38); # base 2^64 -> 2^26
462 risbg ($h0,$h0,38,0x80+63,0);
464 risbg ($d1,$h1,38,51,12);
466 risbg ($d0,$h1,38,63,50);
470 risbg ($d1,$h2,37,39,24);
474 st ("%r0","24($ctx)"); # set is_base2_26
476 vstm ($R0,$S4,"48($ctx)"); # save key schedule base 2^26
478 vpdi ($R0,$R0,$R0,0); # broadcast r^2:r^4
479 vpdi ($R1,$R1,$R1,0);
480 vpdi ($S1,$S1,$S1,0);
481 vpdi ($R2,$R2,$R2,0);
482 vpdi ($S2,$S2,$S2,0);
483 vpdi ($R3,$R3,$R3,0);
484 vpdi ($S3,$S3,$S3,0);
485 vpdi ($R4,$R4,$R4,0);
486 vpdi ($S4,$S4,$S4,0);
491 LABEL (".Lskip_init");
492 vllezf ($H0,"0($ctx)"); # load hash base 2^26
493 vllezf ($H1,"4($ctx)");
494 vllezf ($H2,"8($ctx)");
495 vllezf ($H3,"12($ctx)");
496 vllezf ($H4,"16($ctx)");
498 vlrepg ($R0,"0x30($ctx)"); # broadcast r^2:r^4
499 vlrepg ($R1,"0x40($ctx)");
500 vlrepg ($S1,"0x50($ctx)");
501 vlrepg ($R2,"0x60($ctx)");
502 vlrepg ($S2,"0x70($ctx)");
503 vlrepg ($R3,"0x80($ctx)");
504 vlrepg ($S3,"0x90($ctx)");
505 vlrepg ($R4,"0xa0($ctx)");
506 vlrepg ($S4,"0xb0($ctx)");
508 LABEL (".Loaded_hash");
512 vlm ($T1,$T4,"0x00($inp)"); # load first input block
513 la ($inp,"0x40($inp)");
515 vgmf ($I4,5,5); # padbit<<2
517 vperm ($I0,$T3,$T4,$bswaplo);
518 vperm ($I2,$T3,$T4,$bswapmi);
519 vperm ($T3,$T3,$T4,$bswaphi);
521 verimg ($I1,$I0,$mask26,6); # >>26
523 veslg ($I2,$I2,28); # >>4
524 verimg ($I3,$T3,$mask26,18); # >>14
525 verimg ($I4,$T3,$mask26,58); # >>38
526 vn ($I0,$I0,$mask26);
527 vn ($I2,$I2,$mask26);
528 vesrlf ($I4,$I4,2); # >>2
530 vgmg ($mask26,38,63);
531 vperm ($T3,$T1,$T2,$bswaplo);
532 vperm ($T4,$T1,$T2,$bswaphi);
533 vperm ($T2,$T1,$T2,$bswapmi);
535 verimg ($I0,$T3,$mask26,0);
536 verimg ($I1,$T3,$mask26,38); # >>26
537 verimg ($I2,$T2,$mask26,60); # >>4
538 verimg ($I3,$T4,$mask26,50); # >>14
543 &{$z? \&aghi:\&ahi} ("%r0",-1);
547 vmlef ($ACC0,$I0,$R0);
548 vmlef ($ACC1,$I0,$R1);
549 vmlef ($ACC2,$I0,$R2);
550 vmlef ($ACC3,$I0,$R3);
551 vmlef ($ACC4,$I0,$R4);
553 vmalef ($ACC0,$I1,$S4,$ACC0);
554 vmalef ($ACC1,$I1,$R0,$ACC1);
555 vmalef ($ACC2,$I1,$R1,$ACC2);
556 vmalef ($ACC3,$I1,$R2,$ACC3);
557 vmalef ($ACC4,$I1,$R3,$ACC4);
565 vmalef ($ACC0,$I2,$S3,$ACC0);
566 vmalef ($ACC1,$I2,$S4,$ACC1);
567 vmalef ($ACC2,$I2,$R0,$ACC2);
568 vmalef ($ACC3,$I2,$R1,$ACC3);
569 vmalef ($ACC4,$I2,$R2,$ACC4);
571 vlm ($T1,$T4,"0x00($inp)"); # load next input block
572 la ($inp,"0x40($inp)");
575 vmalef ($ACC0,$I3,$S2,$ACC0);
576 vmalef ($ACC1,$I3,$S3,$ACC1);
577 vmalef ($ACC2,$I3,$S4,$ACC2);
578 vmalef ($ACC3,$I3,$R0,$ACC3);
579 vmalef ($ACC4,$I3,$R1,$ACC4);
581 vperm ($I0,$T3,$T4,$bswaplo);
582 vperm ($I2,$T3,$T4,$bswapmi);
583 vperm ($T3,$T3,$T4,$bswaphi);
585 vmalef ($ACC0,$I4,$S1,$ACC0);
586 vmalef ($ACC1,$I4,$S2,$ACC1);
587 vmalef ($ACC2,$I4,$S3,$ACC2);
588 vmalef ($ACC3,$I4,$S4,$ACC3);
589 vmalef ($ACC4,$I4,$R0,$ACC4);
591 verimg ($I1,$I0,$mask26,6); # >>26
593 veslg ($I2,$I2,28); # >>4
594 verimg ($I3,$T3,$mask26,18); # >>14
596 vmalof ($ACC0,$H0,$R0,$ACC0);
597 vmalof ($ACC1,$H0,$R1,$ACC1);
598 vmalof ($ACC2,$H0,$R2,$ACC2);
599 vmalof ($ACC3,$H0,$R3,$ACC3);
600 vmalof ($ACC4,$H0,$R4,$ACC4);
602 vgmf ($I4,5,5); # padbit<<2
603 verimg ($I4,$T3,$mask26,58); # >>38
604 vn ($I0,$I0,$mask26);
605 vn ($I2,$I2,$mask26);
606 vesrlf ($I4,$I4,2); # >>2
608 vmalof ($ACC0,$H1,$S4,$ACC0);
609 vmalof ($ACC1,$H1,$R0,$ACC1);
610 vmalof ($ACC2,$H1,$R1,$ACC2);
611 vmalof ($ACC3,$H1,$R2,$ACC3);
612 vmalof ($ACC4,$H1,$R3,$ACC4);
614 vgmg ($mask26,38,63);
615 vperm ($T3,$T1,$T2,$bswaplo);
616 vperm ($T4,$T1,$T2,$bswaphi);
617 vperm ($T2,$T1,$T2,$bswapmi);
619 vmalof ($ACC0,$H2,$S3,$ACC0);
620 vmalof ($ACC1,$H2,$S4,$ACC1);
621 vmalof ($ACC2,$H2,$R0,$ACC2);
622 vmalof ($ACC3,$H2,$R1,$ACC3);
623 vmalof ($ACC4,$H2,$R2,$ACC4);
625 verimg ($I0,$T3,$mask26,0);
626 verimg ($I1,$T3,$mask26,38); # >>26
627 verimg ($I2,$T2,$mask26,60); # >>4
629 vmalof ($ACC0,$H3,$S2,$ACC0);
630 vmalof ($ACC1,$H3,$S3,$ACC1);
631 vmalof ($ACC2,$H3,$S4,$ACC2);
632 vmalof ($ACC3,$H3,$R0,$ACC3);
633 vmalof ($ACC4,$H3,$R1,$ACC4);
635 verimg ($I3,$T4,$mask26,50); # >>14
639 vmalof ($ACC0,$H4,$S1,$ACC0);
640 vmalof ($ACC1,$H4,$S2,$ACC1);
641 vmalof ($ACC2,$H4,$S3,$ACC2);
642 vmalof ($ACC3,$H4,$S4,$ACC3);
643 vmalof ($ACC4,$H4,$R0,$ACC4);
645 ################################################################
646 # lazy reduction as discussed in "NEON crypto" by D.J. Bernstein
649 vesrlg ($H4,$ACC3,26);
650 vesrlg ($H1,$ACC0,26);
651 vn ($H3,$ACC3,$mask26);
652 vn ($H0,$ACC0,$mask26);
653 vag ($H4,$H4,$ACC4); # h3 -> h4
654 vag ($H1,$H1,$ACC1); # h0 -> h1
656 vesrlg ($ACC4,$H4,26);
657 vesrlg ($ACC1,$H1,26);
658 vn ($H4,$H4,$mask26);
659 vn ($H1,$H1,$mask26);
661 vag ($H2,$ACC2,$ACC1); # h1 -> h2
663 veslg ($ACC4,$ACC4,2); # <<2
664 vesrlg ($ACC2,$H2,26);
665 vn ($H2,$H2,$mask26);
666 vag ($H0,$H0,$ACC4); # h4 -> h0
667 vag ($H3,$H3,$ACC2); # h2 -> h3
669 vesrlg ($ACC0,$H0,26);
670 vesrlg ($ACC3,$H3,26);
671 vn ($H0,$H0,$mask26);
672 vn ($H3,$H3,$mask26);
673 vag ($H1,$H1,$ACC0); # h0 -> h1
674 vag ($H4,$H4,$ACC3); # h3 -> h4
676 &{$z? \&brctg:\&brct} ("%r0",".Loop_vx");
678 vlm ($R0,$S4,"48($ctx)"); # load all powers
681 &{$z? \&lcgr:\&lcr} ($len,$len);
682 &{$z? \&ngr:\&nr} ($len,"%r0");
683 &{$z? \&slgr:\&slr} ($inp,$len);
686 vmlef ($ACC0,$I0,$R0);
687 vmlef ($ACC1,$I0,$R1);
688 vmlef ($ACC2,$I0,$R2);
689 vmlef ($ACC3,$I0,$R3);
690 vmlef ($ACC4,$I0,$R4);
692 vmalef ($ACC0,$I1,$S4,$ACC0);
693 vmalef ($ACC1,$I1,$R0,$ACC1);
694 vmalef ($ACC2,$I1,$R1,$ACC2);
695 vmalef ($ACC3,$I1,$R2,$ACC3);
696 vmalef ($ACC4,$I1,$R3,$ACC4);
704 vmalef ($ACC0,$I2,$S3,$ACC0);
705 vmalef ($ACC1,$I2,$S4,$ACC1);
706 vmalef ($ACC2,$I2,$R0,$ACC2);
707 vmalef ($ACC3,$I2,$R1,$ACC3);
708 vmalef ($ACC4,$I2,$R2,$ACC4);
710 vmalef ($ACC0,$I3,$S2,$ACC0);
711 vmalef ($ACC1,$I3,$S3,$ACC1);
712 vmalef ($ACC2,$I3,$S4,$ACC2);
713 vmalef ($ACC3,$I3,$R0,$ACC3);
714 vmalef ($ACC4,$I3,$R1,$ACC4);
716 vmalef ($ACC0,$I4,$S1,$ACC0);
717 vmalef ($ACC1,$I4,$S2,$ACC1);
718 vmalef ($ACC2,$I4,$S3,$ACC2);
719 vmalef ($ACC3,$I4,$S4,$ACC3);
720 vmalef ($ACC4,$I4,$R0,$ACC4);
722 vmalof ($ACC0,$H0,$R0,$ACC0);
723 vmalof ($ACC1,$H0,$R1,$ACC1);
724 vmalof ($ACC2,$H0,$R2,$ACC2);
725 vmalof ($ACC3,$H0,$R3,$ACC3);
726 vmalof ($ACC4,$H0,$R4,$ACC4);
728 vmalof ($ACC0,$H1,$S4,$ACC0);
729 vmalof ($ACC1,$H1,$R0,$ACC1);
730 vmalof ($ACC2,$H1,$R1,$ACC2);
731 vmalof ($ACC3,$H1,$R2,$ACC3);
732 vmalof ($ACC4,$H1,$R3,$ACC4);
734 vmalof ($ACC0,$H2,$S3,$ACC0);
735 vmalof ($ACC1,$H2,$S4,$ACC1);
736 vmalof ($ACC2,$H2,$R0,$ACC2);
737 vmalof ($ACC3,$H2,$R1,$ACC3);
738 vmalof ($ACC4,$H2,$R2,$ACC4);
740 vmalof ($ACC0,$H3,$S2,$ACC0);
741 vmalof ($ACC1,$H3,$S3,$ACC1);
742 vmalof ($ACC2,$H3,$S4,$ACC2);
743 vmalof ($ACC3,$H3,$R0,$ACC3);
744 vmalof ($ACC4,$H3,$R1,$ACC4);
746 vmalof ($ACC0,$H4,$S1,$ACC0);
747 vmalof ($ACC1,$H4,$S2,$ACC1);
748 vmalof ($ACC2,$H4,$S3,$ACC2);
749 vmalof ($ACC3,$H4,$S4,$ACC3);
750 vmalof ($ACC4,$H4,$R0,$ACC4);
752 ################################################################
753 # horizontal addition
756 vsumqg ($ACC0,$ACC0,$H0);
757 vsumqg ($ACC1,$ACC1,$H0);
758 vsumqg ($ACC2,$ACC2,$H0);
759 vsumqg ($ACC3,$ACC3,$H0);
760 vsumqg ($ACC4,$ACC4,$H0);
762 ################################################################
765 vesrlg ($H4,$ACC3,26);
766 vesrlg ($H1,$ACC0,26);
767 vn ($H3,$ACC3,$mask26);
768 vn ($H0,$ACC0,$mask26);
769 vag ($H4,$H4,$ACC4); # h3 -> h4
770 vag ($H1,$H1,$ACC1); # h0 -> h1
772 vesrlg ($ACC4,$H4,26);
773 vesrlg ($ACC1,$H1,26);
774 vn ($H4,$H4,$mask26);
775 vn ($H1,$H1,$mask26);
777 vag ($H2,$ACC2,$ACC1); # h1 -> h2
779 veslg ($ACC4,$ACC4,2); # <<2
780 vesrlg ($ACC2,$H2,26);
781 vn ($H2,$H2,$mask26);
782 vag ($H0,$H0,$ACC4); # h4 -> h0
783 vag ($H3,$H3,$ACC2); # h2 -> h3
785 vesrlg ($ACC0,$H0,26);
786 vesrlg ($ACC3,$H3,26);
787 vn ($H0,$H0,$mask26);
788 vn ($H3,$H3,$mask26);
789 vag ($H1,$H1,$ACC0); # h0 -> h1
790 vag ($H4,$H4,$ACC3); # h3 -> h4
792 &{$z? \&clgfi:\&clfi} ($len,0);
795 vlm ($T1,$T4,"0x00($inp)"); # load last partial block
797 vgmf ($I4,5,5); # padbit<<2
799 vperm ($I0,$T3,$T4,$bswaplo);
800 vperm ($I2,$T3,$T4,$bswapmi);
801 vperm ($T3,$T3,$T4,$bswaphi);
803 vl ($ACC0,"0x30($len,%r1)"); # borrow $ACC0,1
804 vl ($ACC1,"0x60($len,%r1)");
806 verimg ($I1,$I0,$mask26,6); # >>26
808 veslg ($I2,$I2,28); # >>4
809 verimg ($I3,$T3,$mask26,18); # >>14
810 verimg ($I4,$T3,$mask26,58); # >>38
811 vn ($I0,$I0,$mask26);
812 vn ($I2,$I2,$mask26);
813 vesrlf ($I4,$I4,2); # >>2
815 vgmg ($mask26,38,63);
816 vperm ($T3,$T1,$T2,$bswaplo);
817 vperm ($T4,$T1,$T2,$bswaphi);
818 vperm ($T2,$T1,$T2,$bswapmi);
820 verimg ($I0,$T3,$mask26,0);
821 verimg ($I1,$T3,$mask26,38); # >>26
822 verimg ($I2,$T2,$mask26,60); # >>4
823 verimg ($I3,$T4,$mask26,50); # >>14
827 vperm ($H0,$H0,$H0,$ACC0); # move hash to right lane
828 vn ($I0,$I0,$ACC1); # mask redundant lane[s]
829 vperm ($H1,$H1,$H1,$ACC0);
831 vperm ($H2,$H2,$H2,$ACC0);
833 vperm ($H3,$H3,$H3,$ACC0);
835 vperm ($H4,$H4,$H4,$ACC0);
838 vaf ($I0,$I0,$H0); # accumulate hash
839 vzero ($H0); # wipe hash value
849 &{$z? \&lghi:\&lhi} ($len,0);
851 # I don't bother to tell apart cases when only one multiplication
852 # pass is sufficient, because I argue that mispredicted branch
853 # penalties are comparable to overhead of sometimes redundant
854 # multiplication pass...
857 vstef ($H0,"0($ctx)",3); # store hash base 2^26
858 vstef ($H1,"4($ctx)",3);
859 vstef ($H2,"8($ctx)",3);
860 vstef ($H3,"12($ctx)",3);
861 vstef ($H4,"16($ctx)",3);
864 ld ("%f8","$stdframe+0*8($sp)");
865 ld ("%f9","$stdframe+1*8($sp)");
866 ld ("%f10","$stdframe+2*8($sp)");
867 ld ("%f11","$stdframe+3*8($sp)");
868 ld ("%f12","$stdframe+4*8($sp)");
869 ld ("%f13","$stdframe+5*8($sp)");
870 ld ("%f14","$stdframe+6*8($sp)");
871 ld ("%f15","$stdframe+7*8($sp)");
872 &{$z? \&lmg:\&lm} ("%r10","%r15","$stdframe+8*8+10*$SIZE_T($sp)");
874 ld ("%f4","$stdframe+16*$SIZE_T+2*8($sp)");
875 ld ("%f6","$stdframe+16*$SIZE_T+3*8($sp)");
876 &{$z? \&lmg:\&lm} ("%r10","%r15","$stdframe+10*$SIZE_T($sp)");
879 SIZE ("__poly1305_blocks_vx",".-__poly1305_blocks_vx");
883 # static void poly1305_emit(void *ctx, unsigned char mac[16],
884 # const u32 nonce[4])
886 my ($mac,$nonce)=($inp,$len);
887 my ($h0,$h1,$h2,$d0,$d1,$d2)=map("%r$_",(5..10));
889 GLOBL ("poly1305_emit");
890 TYPE ("poly1305_emit","\@function");
892 LABEL ("poly1305_emit");
893 LABEL (".Lpoly1305_emit");
894 &{$z? \&stmg:\&stm} ("%r6","%r10","6*$SIZE_T($sp)");
900 llgfr ("%r0",$d0); # base 2^26 -> base 2^64
906 sllg ("%r0","%r0",26);
910 sllg ("%r1","%r1",14);
919 llgf ("%r0","24($ctx)"); # is_base2_26
922 xgr ($h0,$d0); # choose between radixes
936 algr ($h0,"%r0"); # compare to modulus
940 srlg ($h2,$h2,2); # did it borrow/carry?
941 slgr ("%r1",$h2); # 0-$h2>>2
942 lg ($d2,"0($nonce)"); # load nonce
943 lg ($ctx,"8($nonce)");
950 rllg ($d0,$d2,32); # flip nonce words
954 algr ($h0,$d0); # accumulate nonce
957 strvg ($h0,"0($mac)"); # write little-endian result
958 strvg ($h1,"8($mac)");
960 &{$z? \&lmg:\&lm} ("%r6","%r10","6*$SIZE_T($sp)");
962 SIZE ("poly1305_emit",".-poly1305_emit");
969 LONG (0x04050607,0x14151617,0x0c0d0e0f,0x1c1d1e1f); # merge odd
970 LONG (0x07060504,0x03020100,0x17161514,0x13121110); # byte swap masks
971 LONG (0x0f0e0d0c,0x0b0a0908,0x1f1e1d1c,0x1b1a1918);
972 LONG (0x00000000,0x09080706,0x00000000,0x19181716);
974 LONG (0x00000000,0x00000000,0x00000000,0x0c0d0e0f); # magic tail masks
975 LONG (0x0c0d0e0f,0x00000000,0x00000000,0x00000000);
976 LONG (0x00000000,0x00000000,0x0c0d0e0f,0x00000000);
978 LONG (0xffffffff,0x00000000,0xffffffff,0xffffffff);
979 LONG (0xffffffff,0x00000000,0xffffffff,0x00000000);
980 LONG (0x00000000,0x00000000,0xffffffff,0x00000000);
982 STRING ("\"Poly1305 for s390x, CRYPTOGAMS by <appro\@openssl.org>\"");