2 # Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the OpenSSL license (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 # ====================================================================
19 # Initial support for Fujitsu SPARC64 X/X+ comprises minimally
20 # required key setup and single-block procedures.
23 open STDOUT,">$output";
26 my ($inp,$out,$key,$rounds,$tmp,$mask) = map("%o$_",(0..5));
34 and $inp, 7, $tmp ! is input aligned?
35 alignaddr $inp, %g0, $inp
36 ld [$key + 240], $rounds
40 ldd [$inp + 0], %f0 ! load input
41 brz,pt $tmp, .Lenc_inp_aligned
45 faligndata %f0, %f2, %f0
46 faligndata %f2, %f4, %f2
53 fxor %f0, %f6, %f0 ! ^=round[0]
57 sub $rounds, 4, $rounds
61 faesencx %f2, %f10, %f0
62 faesencx %f4, %f12, %f2
68 faesencx %f2, %f6, %f0
69 faesencx %f4, %f8, %f2
73 brnz,a $rounds, .Loop_enc
74 sub $rounds, 2, $rounds
76 andcc $out, 7, $tmp ! is output aligned?
78 alignaddrl $out, %g0, $out
79 srl $mask, $tmp, $mask
82 faesencx %f2, %f10, %f0
83 faesencx %f4, %f12, %f2
85 faesenclx %f2, %f6, %f0
86 faesenclx %f4, %f8, %f2
88 bnz,pn %icc, .Lenc_out_unaligned
96 faligndata %f0, %f0, %f4
97 faligndata %f0, %f2, %f6
98 faligndata %f2, %f2, %f8
100 stda %f4, [$out + $mask]0xc0 ! partial store
103 orn %g0, $mask, $mask
105 stda %f8, [$out + $mask]0xc0 ! partial store
106 .type aes_fx_encrypt,#function
107 .size aes_fx_encrypt,.-aes_fx_encrypt
109 .globl aes_fx_decrypt
112 and $inp, 7, $tmp ! is input aligned?
113 alignaddr $inp, %g0, $inp
114 ld [$key + 240], $rounds
118 ldd [$inp + 0], %f0 ! load input
119 brz,pt $tmp, .Ldec_inp_aligned
123 faligndata %f0, %f2, %f0
124 faligndata %f2, %f4, %f2
127 ldd [$key + 16], %f10
128 ldd [$key + 24], %f12
131 fxor %f0, %f6, %f0 ! ^=round[0]
135 sub $rounds, 4, $rounds
139 faesdecx %f2, %f10, %f0
140 faesdecx %f4, %f12, %f2
141 ldd [$key + 16], %f10
142 ldd [$key + 24], %f12
146 faesdecx %f2, %f6, %f0
147 faesdecx %f4, %f8, %f2
151 brnz,a $rounds, .Loop_dec
152 sub $rounds, 2, $rounds
154 andcc $out, 7, $tmp ! is output aligned?
156 alignaddrl $out, %g0, $out
157 srl $mask, $tmp, $mask
160 faesdecx %f2, %f10, %f0
161 faesdecx %f4, %f12, %f2
163 faesdeclx %f2, %f6, %f0
164 faesdeclx %f4, %f8, %f2
166 bnz,pn %icc, .Ldec_out_unaligned
174 faligndata %f0, %f0, %f4
175 faligndata %f0, %f2, %f6
176 faligndata %f2, %f2, %f8
178 stda %f4, [$out + $mask]0xc0 ! partial store
181 orn %g0, $mask, $mask
183 stda %f8, [$out + $mask]0xc0 ! partial store
184 .type aes_fx_decrypt,#function
185 .size aes_fx_decrypt,.-aes_fx_decrypt
189 my ($inp,$bits,$out,$tmp,$inc) = map("%o$_",(0..5));
191 .globl aes_fx_set_decrypt_key
193 aes_fx_set_decrypt_key:
198 .type aes_fx_set_decrypt_key,#function
199 .size aes_fx_set_decrypt_key,.-aes_fx_set_decrypt_key
201 .globl aes_fx_set_encrypt_key
203 aes_fx_set_encrypt_key:
207 alignaddr $inp, %g0, $inp
217 brz,pt $tmp, .L256aligned
221 faligndata %f0, %f2, %f0
222 faligndata %f2, %f4, %f2
223 faligndata %f4, %f6, %f4
224 faligndata %f6, %f8, %f6
228 and $inc, `14*16`, $tmp
229 st $bits, [$out + 240] ! store rounds
230 add $out, $tmp, $out ! start or end of key schedule
231 sllx $inc, 4, $inc ! 16 or -16
233 for ($i=0; $i<6; $i++) {
236 faeskeyx %f6, `0x10+$i`, %f0
239 faeskeyx %f0, 0x00, %f2
241 faeskeyx %f2, 0x01, %f4
244 faeskeyx %f4, 0x00, %f6
249 faeskeyx %f6, `0x10+$i`, %f0
252 faeskeyx %f0, 0x00, %f2
259 xor %o0, %o0, %o0 ! return 0
263 brz,pt $tmp, .L192aligned
267 faligndata %f0, %f2, %f0
268 faligndata %f2, %f4, %f2
269 faligndata %f4, %f6, %f4
273 and $inc, `12*16`, $tmp
274 st $bits, [$out + 240] ! store rounds
275 add $out, $tmp, $out ! start or end of key schedule
276 sllx $inc, 4, $inc ! 16 or -16
278 for ($i=0; $i<8; $i+=2) {
281 faeskeyx %f4, `0x10+$i`, %f0
284 faeskeyx %f0, 0x00, %f2
286 faeskeyx %f2, 0x00, %f4
289 faeskeyx %f4, `0x10+$i+1`, %f0
291 faeskeyx %f0, 0x00, %f2
295 $code.=<<___ if ($i<6);
296 faeskeyx %f2, 0x00, %f4
303 xor %o0, %o0, %o0 ! return 0
307 brz,pt $tmp, .L128aligned
311 faligndata %f0, %f2, %f0
312 faligndata %f2, %f4, %f2
316 and $inc, `10*16`, $tmp
317 st $bits, [$out + 240] ! store rounds
318 add $out, $tmp, $out ! start or end of key schedule
319 sllx $inc, 4, $inc ! 16 or -16
321 for ($i=0; $i<10; $i++) {
324 faeskeyx %f2, `0x10+$i`, %f0
327 faeskeyx %f0, 0x00, %f2
334 xor %o0, %o0, %o0 ! return 0
335 .type aes_fx_set_encrypt_key,#function
336 .size aes_fx_set_encrypt_key,.-aes_fx_set_encrypt_key
340 # Purpose of these subroutines is to explicitly encode VIS instructions,
341 # so that one can compile the module without having to specify VIS
342 # extensions on compiler command line, e.g. -xarch=v9 vs. -xarch=v9a.
343 # Idea is to reserve for option to produce "universal" binary and let
344 # programmer detect if current CPU is VIS capable at run-time.
346 my ($mnemonic,$rs1,$rs2,$rd)=@_;
348 my %visopf = ( "faligndata" => 0x048,
353 $ref = "$mnemonic\t$rs1,$rs2,$rd";
355 if ($opf=$visopf{$mnemonic}) {
356 foreach ($rs1,$rs2,$rd) {
357 return $ref if (!/%f([0-9]{1,2})/);
360 return $ref if ($1&1);
361 # re-encode for upper double register addressing
366 return sprintf ".word\t0x%08x !%s",
367 0x81b00000|$rd<<25|$rs1<<14|$opf<<5|$rs2,
375 my ($mnemonic,$rs1,$rs2,$rd)=@_;
376 my %bias = ( "g" => 0, "o" => 8, "l" => 16, "i" => 24 );
378 my %visopf = ( "alignaddr" => 0x018,
380 "alignaddrl" => 0x01a );
382 $ref = "$mnemonic\t$rs1,$rs2,$rd";
384 if ($opf=$visopf{$mnemonic}) {
385 foreach ($rs1,$rs2,$rd) {
386 return $ref if (!/%([goli])([0-9])/);
390 return sprintf ".word\t0x%08x !%s",
391 0x81b00000|$rd<<25|$rs1<<14|$opf<<5|$rs2,
399 my ($mnemonic,$rs1,$rs2,$rd)=@_;
401 my %aesopf = ( "faesencx" => 0x90,
405 "faeskeyx" => 0x94 );
407 $ref = "$mnemonic\t$rs1,$rs2,$rd";
409 if (defined($opf=$aesopf{$mnemonic})) {
410 $rs2 = ($rs2 =~ /%f([0-6]*[02468])/) ? (($1|$1>>5)&31) : $rs2;
411 $rs2 = oct($rs2) if ($rs2 =~ /^0/);
414 return $ref if (!/%f([0-9]{1,2})/);
417 return $ref if ($1&1);
418 # re-encode for upper double register addressing
423 return sprintf ".word\t0x%08x !%s",
424 2<<30|$rd<<25|0x36<<19|$rs1<<14|$opf<<5|$rs2,
431 foreach (split("\n",$code)) {
432 s/\`([^\`]*)\`/eval $1/ge;
434 s/\b(faes[^x]{3,4}x)\s+(%f[0-9]{1,2}),\s*([%fx0-9]+),\s*(%f[0-9]{1,2})/
435 &unfx($1,$2,$3,$4,$5)
437 s/\b([fb][^\s]*)\s+(%f[0-9]{1,2}),\s*(%f[0-9]{1,2}),\s*(%f[0-9]{1,2})/
440 s/\b(alignaddr[l]*)\s+(%[goli][0-7]),\s*(%[goli][0-7]),\s*(%[goli][0-7])/