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 [+128 bytes shared table]. Performance
15 # was measured to be ~18 cycles per processed byte on z10, which is
16 # almost 40% better than gcc-generated code. It should be noted that
17 # 18 cycles is worse result than expected: loop is scheduled for 12
18 # and the result should be close to 12. In the lack of instruction-
19 # level profiling data it's impossible to tell why...
23 # Adapt for -m31 build. If kernel supports what's called "highgprs"
24 # feature on Linux [see /proc/cpuinfo], it's possible to use 64-bit
25 # instructions and achieve "64-bit" performance even in 31-bit legacy
26 # application context. The feature is not specific to any particular
27 # processor, as long as it's "z-CPU". Latter implies that the code
28 # remains z/Architecture specific. On z990 it was measured to perform
29 # 2.8x better than 32-bit code generated by gcc 4.3.
33 if ($flavour =~ /3[12]/) {
41 while (($output=shift) && ($output!~/^\w[\w\-]*\.\w+$/)) {}
42 open STDOUT,">$output";
44 $softonly=1; # disable hardware support for now
49 $Xi="%r2"; # argument block
54 $rem0="%r6"; # variables
73 $code.=<<___ if(!$softonly);
74 larl %r1,OPENSSL_s390xcap_P
76 tmhl %r0,0x4000 # check for message-security-assist
80 .long 0xb93e0004 # kimd %r0,%r4
82 tmhh %r1,0x4000 # check for function 65
84 stg %r0,16($sp) # arrange 16 bytes of zero input
86 lghi %r0,65 # function 65
87 la %r1,0($Xi) # H lies right after Xi in gcm128_context
90 .long 0xb93e0004 # kimd %r0,$inp
91 brc 1,.-4 # pay attention to "partial completion"
97 stm${g} %r6,%r14,6*$SIZE_T($sp)
102 larl $rem_4bit,rem_4bit
104 lg $Zlo,8+1($Xi) # Xi
106 .type gcm_gmult_4bit,\@function
107 .size gcm_gmult_4bit,(.-gcm_gmult_4bit)
109 .globl gcm_ghash_4bit
113 $code.=<<___ if(!$softonly);
114 larl %r1,OPENSSL_s390xcap_P
116 tmhl %r0,0x4000 # check for message-security-assist
120 .long 0xb93e0004 # kimd %r0,%r4
122 tmhh %r1,0x4000 # check for function 65
124 lghi %r0,65 # function 65
125 la %r1,0($Xi) # H lies right after Xi in gcm128_context
126 .long 0xb93e0004 # kimd %r0,$inp
127 brc 1,.-4 # pay attention to "partial completion"
132 $cdoe.=<<___ if ($flavour =~ /3[12]/);
136 stm${g} %r6,%r14,6*$SIZE_T($sp)
141 larl $rem_4bit,rem_4bit
143 lg $Zlo,8+1($Xi) # Xi
147 xg $Zhi,0($inp) # Xi ^= inp
156 srlg $xi,$Zlo,8 # extract second byte
162 lg $Zlo,8($nlo,$Htbl)
163 lg $Zhi,0($nlo,$Htbl)
174 xg $Zlo,8($nhi,$Htbl)
175 xg $Zhi,0($nhi,$Htbl)
185 xg $Zlo,8($nlo,$Htbl)
188 xg $Zhi,0($nlo,$Htbl)
190 xg $Zhi,0($rem0,$rem_4bit)
200 xg $Zlo,8($nhi,$Htbl)
201 xg $Zhi,0($nhi,$Htbl)
203 xg $Zhi,0($rem1,$rem_4bit)
207 brct $cnt,.Lghash_inner
212 xg $Zlo,8($nlo,$Htbl)
213 xg $Zhi,0($nlo,$Htbl)
215 xg $Zhi,0($rem0,$rem_4bit)
222 xg $Zlo,8($nhi,$Htbl)
223 xg $Zhi,0($nhi,$Htbl)
225 xg $Zhi,0($rem1,$rem_4bit)
227 lg $tmp,0($xi,$rem_4bit)
229 sllg $tmp,$tmp,4 # correct last rem_4bit[rem]
235 lm${g} %r6,%r14,6*$SIZE_T($sp)
237 .type gcm_ghash_4bit,\@function
238 .size gcm_ghash_4bit,(.-gcm_ghash_4bit)
242 .long `0x0000<<12`,0,`0x1C20<<12`,0,`0x3840<<12`,0,`0x2460<<12`,0
243 .long `0x7080<<12`,0,`0x6CA0<<12`,0,`0x48C0<<12`,0,`0x54E0<<12`,0
244 .long `0xE100<<12`,0,`0xFD20<<12`,0,`0xD940<<12`,0,`0xC560<<12`,0
245 .long `0x9180<<12`,0,`0x8DA0<<12`,0,`0xA9C0<<12`,0,`0xB5E0<<12`,0
246 .type rem_4bit,\@object
247 .size rem_4bit,(.-rem_4bit)
248 .string "GHASH for s390x, CRYPTOGAMS by <appro\@openssl.org>"
251 $code =~ s/\`([^\`]*)\`/eval $1/gem;