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 # Initial support for Fujitsu SPARC64 X/X+ comprises minimally
13 # required key setup and single-block procedures.
16 open STDOUT,">$output";
19 my ($inp,$out,$key,$rounds,$tmp,$mask) = map("%o$_",(0..5));
27 and $inp, 7, $tmp ! is input aligned?
28 alignaddr $inp, %g0, $inp
29 ld [$key + 240], $rounds
33 ldd [$inp + 0], %f0 ! load input
34 brz,pt $tmp, .Lenc_inp_aligned
38 faligndata %f0, %f2, %f0
39 faligndata %f2, %f4, %f2
46 fxor %f0, %f6, %f0 ! ^=round[0]
50 sub $rounds, 4, $rounds
54 faesencx %f2, %f10, %f0
55 faesencx %f4, %f12, %f2
61 faesencx %f2, %f6, %f0
62 faesencx %f4, %f8, %f2
66 brnz,a $rounds, .Loop_enc
67 sub $rounds, 2, $rounds
69 andcc $out, 7, $tmp ! is output aligned?
71 alignaddrl $out, %g0, $out
72 srl $mask, $tmp, $mask
75 faesencx %f2, %f10, %f0
76 faesencx %f4, %f12, %f2
78 faesenclx %f2, %f6, %f0
79 faesenclx %f4, %f8, %f2
81 bnz,pn %icc, .Lenc_out_unaligned
89 faligndata %f0, %f0, %f4
90 faligndata %f0, %f2, %f6
91 faligndata %f2, %f2, %f8
93 stda %f4, [$out + $mask]0xc0 ! partial store
98 stda %f8, [$out + $mask]0xc0 ! partial store
99 .size aes_fx_encrypt,.-aes_fx_encrypt
101 .globl aes_fx_decrypt
104 and $inp, 7, $tmp ! is input aligned?
105 alignaddr $inp, %g0, $inp
106 ld [$key + 240], $rounds
110 ldd [$inp + 0], %f0 ! load input
111 brz,pt $tmp, .Ldec_inp_aligned
115 faligndata %f0, %f2, %f0
116 faligndata %f2, %f4, %f2
119 ldd [$key + 16], %f10
120 ldd [$key + 24], %f12
123 fxor %f0, %f6, %f0 ! ^=round[0]
127 sub $rounds, 4, $rounds
131 faesdecx %f2, %f10, %f0
132 faesdecx %f4, %f12, %f2
133 ldd [$key + 16], %f10
134 ldd [$key + 24], %f12
138 faesdecx %f2, %f6, %f0
139 faesdecx %f4, %f8, %f2
143 brnz,a $rounds, .Loop_dec
144 sub $rounds, 2, $rounds
146 andcc $out, 7, $tmp ! is output aligned?
148 alignaddrl $out, %g0, $out
149 srl $mask, $tmp, $mask
152 faesdecx %f2, %f10, %f0
153 faesdecx %f4, %f12, %f2
155 faesdeclx %f2, %f6, %f0
156 faesdeclx %f4, %f8, %f2
158 bnz,pn %icc, .Ldec_out_unaligned
166 faligndata %f0, %f0, %f4
167 faligndata %f0, %f2, %f6
168 faligndata %f2, %f2, %f8
170 stda %f4, [$out + $mask]0xc0 ! partial store
173 orn %g0, $mask, $mask
175 stda %f8, [$out + $mask]0xc0 ! partial store
176 .size aes_fx_decrypt,.-aes_fx_decrypt
180 my ($inp,$bits,$out,$tmp,$inc) = map("%o$_",(0..5));
182 .globl aes_fx_set_decrypt_key
184 aes_fx_set_decrypt_key:
189 .size aes_fx_set_decrypt_key,.-aes_fx_set_decrypt_key
191 .globl aes_fx_set_encrypt_key
193 aes_fx_set_encrypt_key:
197 alignaddr $inp, %g0, $inp
207 brz,pt $tmp, .L256aligned
211 faligndata %f0, %f2, %f0
212 faligndata %f2, %f4, %f2
213 faligndata %f4, %f6, %f4
214 faligndata %f6, %f8, %f6
218 and $inc, `14*16`, $tmp
219 st $bits, [$out + 240] ! store rounds
220 add $out, $tmp, $out ! start or end of key schedule
221 sllx $inc, 4, $inc ! 16 or -16
223 for ($i=0; $i<6; $i++) {
226 faeskeyx %f6, `0x10+$i`, %f0
229 faeskeyx %f0, 0x00, %f2
231 faeskeyx %f2, 0x01, %f4
234 faeskeyx %f4, 0x00, %f6
239 faeskeyx %f6, `0x10+$i`, %f0
242 faeskeyx %f0, 0x00, %f2
249 xor %o0, %o0, %o0 ! return 0
253 brz,pt $tmp, .L192aligned
257 faligndata %f0, %f2, %f0
258 faligndata %f2, %f4, %f2
259 faligndata %f4, %f6, %f4
263 and $inc, `12*16`, $tmp
264 st $bits, [$out + 240] ! store rounds
265 add $out, $tmp, $out ! start or end of key schedule
266 sllx $inc, 4, $inc ! 16 or -16
268 for ($i=0; $i<8; $i+=2) {
271 faeskeyx %f4, `0x10+$i`, %f0
274 faeskeyx %f0, 0x00, %f2
276 faeskeyx %f2, 0x00, %f4
279 faeskeyx %f4, `0x10+$i+1`, %f0
281 faeskeyx %f0, 0x00, %f2
285 $code.=<<___ if ($i<6);
286 faeskeyx %f2, 0x00, %f4
293 xor %o0, %o0, %o0 ! return 0
297 brz,pt $tmp, .L128aligned
301 faligndata %f0, %f2, %f0
302 faligndata %f2, %f4, %f2
306 and $inc, `10*16`, $tmp
307 st $bits, [$out + 240] ! store rounds
308 add $out, $tmp, $out ! start or end of key schedule
309 sllx $inc, 4, $inc ! 16 or -16
311 for ($i=0; $i<10; $i++) {
314 faeskeyx %f2, `0x10+$i`, %f0
317 faeskeyx %f0, 0x00, %f2
324 xor %o0, %o0, %o0 ! return 0
325 .size aes_fx_set_encrypt_key,.-aes_fx_set_encrypt_key
329 # Purpose of these subroutines is to explicitly encode VIS instructions,
330 # so that one can compile the module without having to specify VIS
331 # extensions on compiler command line, e.g. -xarch=v9 vs. -xarch=v9a.
332 # Idea is to reserve for option to produce "universal" binary and let
333 # programmer detect if current CPU is VIS capable at run-time.
335 my ($mnemonic,$rs1,$rs2,$rd)=@_;
337 my %visopf = ( "faligndata" => 0x048,
342 $ref = "$mnemonic\t$rs1,$rs2,$rd";
344 if ($opf=$visopf{$mnemonic}) {
345 foreach ($rs1,$rs2,$rd) {
346 return $ref if (!/%f([0-9]{1,2})/);
349 return $ref if ($1&1);
350 # re-encode for upper double register addressing
355 return sprintf ".word\t0x%08x !%s",
356 0x81b00000|$rd<<25|$rs1<<14|$opf<<5|$rs2,
364 my ($mnemonic,$rs1,$rs2,$rd)=@_;
365 my %bias = ( "g" => 0, "o" => 8, "l" => 16, "i" => 24 );
367 my %visopf = ( "alignaddr" => 0x018,
369 "alignaddrl" => 0x01a );
371 $ref = "$mnemonic\t$rs1,$rs2,$rd";
373 if ($opf=$visopf{$mnemonic}) {
374 foreach ($rs1,$rs2,$rd) {
375 return $ref if (!/%([goli])([0-9])/);
379 return sprintf ".word\t0x%08x !%s",
380 0x81b00000|$rd<<25|$rs1<<14|$opf<<5|$rs2,
388 my ($mnemonic,$rs1,$rs2,$rd)=@_;
390 my %aesopf = ( "faesencx" => 0x90,
394 "faeskeyx" => 0x94 );
396 $ref = "$mnemonic\t$rs1,$rs2,$rd";
398 if (defined($opf=$aesopf{$mnemonic})) {
399 $rs2 = ($rs2 =~ /%f([0-6]*[02468])/) ? (($1|$1>>5)&31) : $rs2;
400 $rs2 = oct($rs2) if ($rs2 =~ /^0/);
403 return $ref if (!/%f([0-9]{1,2})/);
406 return $ref if ($1&1);
407 # re-encode for upper double register addressing
412 return sprintf ".word\t0x%08x !%s",
413 2<<30|$rd<<25|0x36<<19|$rs1<<14|$opf<<5|$rs2,
420 foreach (split("\n",$code)) {
421 s/\`([^\`]*)\`/eval $1/ge;
423 s/\b(faes[^x]{3,4}x)\s+(%f[0-9]{1,2}),\s*([%fx0-9]+),\s*(%f[0-9]{1,2})/
424 &unfx($1,$2,$3,$4,$5)
426 s/\b([fb][^\s]*)\s+(%f[0-9]{1,2}),\s*(%f[0-9]{1,2}),\s*(%f[0-9]{1,2})/
429 s/\b(alignaddr[l]*)\s+(%[goli][0-7]),\s*(%[goli][0-7]),\s*(%[goli][0-7])/