Newer
Older
#!/usr/bin/env perl
#
# ====================================================================
# Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
# project. Rights for redistribution and usage in source and binary
# forms are granted according to the OpenSSL license.
# ====================================================================
#
# Version 1.2.
# aes-*-cbc benchmarks are improved by >70% [compared to gcc 3.3.2 on
# Opteron 240 CPU] plus all the bells-n-whistles from 32-bit version
# [you'll notice a lot of resemblance], such as compressed S-boxes
# in little-endian byte order, prefetch of these tables in CBC mode,
# as well as avoiding L1 cache aliasing between stack frame and key
# schedule and already mentioned tables, compressed Td4...
# Performance in number of cycles per processed byte for 128-bit key:
#
# ECB CBC encrypt
# AMD64 13.7 13.0(*)
# EM64T 20.2 18.6(*)
#
# (*) CBC benchmarks are better than ECB thanks to custom ABI used
# by the private block encryption function.
$verticalspin=1; # unlike 32-bit version $verticalspin performs
# ~15% better on both AMD and Intel cores
$output=shift;
open STDOUT,"| $^X ../perlasm/x86_64-xlate.pl $output";
$code=".text\n";
$s0="%eax";
$s1="%ebx";
$s2="%ecx";
$s3="%edx";
$acc0="%esi";
$acc1="%edi";
$acc2="%ebp";
$inp="%r8";
$out="%r9";
$t0="%r10d";
$t1="%r11d";
$t2="%r12d";
sub hi() { my $r=shift; $r =~ s/%[er]([a-d])x/%\1h/; $r; }
sub lo() { my $r=shift; $r =~ s/%[er]([a-d])x/%\1l/;
$r =~ s/%[er]([sd]i)/%\1l/;
$r =~ s/%(r[0-9]+)[d]?/%\1b/; $r; }
sub _data_word()
{ my $i;
while(defined($i=shift)) { $code.=sprintf".long\t0x%08x,0x%08x\n",$i,$i; }
}
sub data_word()
{ my $i;
my $last=pop(@_);
$code.=".long\t";
while(defined($i=shift)) { $code.=sprintf"0x%08x,",$i; }
$code.=sprintf"0x%08x\n",$last;
}
sub data_byte()
{ my $i;
my $last=pop(@_);
$code.=".byte\t";
while(defined($i=shift)) { $code.=sprintf"0x%02x,",$i&0xff; }
$code.=sprintf"0x%02x\n",$last&0xff;
}
sub encvert()
{ my $t3="%r8d"; # zaps $inp!
$code.=<<___;
# favor 3-way issue Opteron pipeline...
movzb `&lo("$s0")`,$acc0
movzb `&lo("$s1")`,$acc1
movzb `&lo("$s2")`,$acc2
mov 0($sbox,$acc0,8),$t0
mov 0($sbox,$acc1,8),$t1
mov 0($sbox,$acc2,8),$t2
movzb `&hi("$s1")`,$acc0
movzb `&hi("$s2")`,$acc1
movzb `&lo("$s3")`,$acc2
xor 3($sbox,$acc0,8),$t0
xor 3($sbox,$acc1,8),$t1
mov 0($sbox,$acc2,8),$t3
movzb `&hi("$s3")`,$acc0
movzb `&hi("$s0")`,$acc2
xor 3($sbox,$acc0,8),$t2
shr \$16,$s3
xor 3($sbox,$acc2,8),$t3
shr \$16,$s1
lea 16($key),$key
shr \$16,$s0
movzb `&lo("$s2")`,$acc0
movzb `&lo("$s3")`,$acc1
movzb `&lo("$s0")`,$acc2
xor 2($sbox,$acc0,8),$t0
xor 2($sbox,$acc1,8),$t1
xor 2($sbox,$acc2,8),$t2
movzb `&hi("$s3")`,$acc0
movzb `&hi("$s0")`,$acc1
movzb `&lo("$s1")`,$acc2
xor 1($sbox,$acc0,8),$t0
xor 1($sbox,$acc1,8),$t1
xor 2($sbox,$acc2,8),$t3
mov 12($key),$s3
movzb `&hi("$s1")`,$acc1
movzb `&hi("$s2")`,$acc2
mov 0($key),$s0
xor 1($sbox,$acc1,8),$t2
xor 1($sbox,$acc2,8),$t3
mov 4($key),$s1
mov 8($key),$s2
xor $t0,$s0
xor $t1,$s1
xor $t2,$s2
xor $t3,$s3
___
}
sub enclastvert()
{ my $t3="%r8d"; # zaps $inp!
$code.=<<___;
movzb `&lo("$s0")`,$acc0
movzb `&lo("$s1")`,$acc1
movzb `&lo("$s2")`,$acc2
mov 2($sbox,$acc0,8),$t0
mov 2($sbox,$acc1,8),$t1
mov 2($sbox,$acc2,8),$t2
and \$0x000000ff,$t0
and \$0x000000ff,$t1
and \$0x000000ff,$t2
movzb `&lo("$s3")`,$acc0
movzb `&hi("$s1")`,$acc1
movzb `&hi("$s2")`,$acc2
mov 2($sbox,$acc0,8),$t3
mov 0($sbox,$acc1,8),$acc1 #$t0
mov 0($sbox,$acc2,8),$acc2 #$t1
and \$0x000000ff,$t3
and \$0x0000ff00,$acc1
and \$0x0000ff00,$acc2
xor $acc1,$t0
xor $acc2,$t1
shr \$16,$s2
movzb `&hi("$s3")`,$acc0
movzb `&hi("$s0")`,$acc1
shr \$16,$s3
mov 0($sbox,$acc0,8),$acc0 #$t2
mov 0($sbox,$acc1,8),$acc1 #$t3
and \$0x0000ff00,$acc0
and \$0x0000ff00,$acc1
shr \$16,$s1
xor $acc0,$t2
xor $acc1,$t3
shr \$16,$s0
movzb `&lo("$s2")`,$acc0
movzb `&lo("$s3")`,$acc1
movzb `&lo("$s0")`,$acc2
mov 0($sbox,$acc0,8),$acc0 #$t0
mov 0($sbox,$acc1,8),$acc1 #$t1
mov 0($sbox,$acc2,8),$acc2 #$t2
and \$0x00ff0000,$acc0
and \$0x00ff0000,$acc1
and \$0x00ff0000,$acc2
xor $acc0,$t0
xor $acc1,$t1
xor $acc2,$t2
movzb `&lo("$s1")`,$acc0
movzb `&hi("$s3")`,$acc1
movzb `&hi("$s0")`,$acc2
mov 0($sbox,$acc0,8),$acc0 #$t3
mov 2($sbox,$acc1,8),$acc1 #$t0
mov 2($sbox,$acc2,8),$acc2 #$t1
and \$0x00ff0000,$acc0
and \$0xff000000,$acc1
and \$0xff000000,$acc2
xor $acc0,$t3
xor $acc1,$t0
xor $acc2,$t1
movzb `&hi("$s1")`,$acc0
movzb `&hi("$s2")`,$acc1
mov 16+12($key),$s3
mov 2($sbox,$acc0,8),$acc0 #$t2
mov 2($sbox,$acc1,8),$acc1 #$t3
mov 16+0($key),$s0
and \$0xff000000,$acc0
and \$0xff000000,$acc1
xor $acc0,$t2
xor $acc1,$t3
mov 16+4($key),$s1
mov 16+8($key),$s2
xor $t0,$s0
xor $t1,$s1
xor $t2,$s2
xor $t3,$s3
___
}
sub encstep()
{ my ($i,@s) = @_;
my $tmp0=$acc0;
my $tmp1=$acc1;
my $tmp2=$acc2;
my $out=($t0,$t1,$t2,$s[0])[$i];
if ($i==3) {
$tmp0=$s[1];
$tmp1=$s[2];
$tmp2=$s[3];
}
$code.=" movzb ".&lo($s[0]).",$out\n";
$code.=" mov $s[2],$tmp1\n" if ($i!=3);
$code.=" lea 16($key),$key\n" if ($i==0);
$code.=" movzb ".&hi($s[1]).",$tmp0\n";
$code.=" mov 0($sbox,$out,8),$out\n";
$code.=" shr \$16,$tmp1\n";
$code.=" mov $s[3],$tmp2\n" if ($i!=3);
$code.=" xor 3($sbox,$tmp0,8),$out\n";
$code.=" movzb ".&lo($tmp1).",$tmp1\n";
$code.=" xor 2($sbox,$tmp1,8),$out\n";
$code.=" xor 1($sbox,$tmp2,8),$out\n";
$code.=" mov $t0,$s[1]\n" if ($i==3);
$code.=" mov $t1,$s[2]\n" if ($i==3);
$code.=" mov $t2,$s[3]\n" if ($i==3);
$code.="\n";
}
sub enclast()
{ my ($i,@s)=@_;
my $tmp0=$acc0;
my $tmp1=$acc1;
my $tmp2=$acc2;
my $out=($t0,$t1,$t2,$s[0])[$i];
if ($i==3) {
$tmp0=$s[1];
$tmp1=$s[2];
$tmp2=$s[3];
}
$code.=" movzb ".&lo($s[0]).",$out\n";
$code.=" mov $s[2],$tmp1\n" if ($i!=3);
$code.=" shr \$16,$tmp1\n";
$code.=" mov $s[3],$tmp2\n" if ($i!=3);
$code.=" and \$0x000000ff,$out\n";
$code.=" movzb ".&hi($s[1]).",$tmp0\n";
$code.=" movzb ".&lo($tmp1).",$tmp1\n";
$code.=" mov 0($sbox,$tmp0,8),$tmp0\n";
$code.=" mov 0($sbox,$tmp1,8),$tmp1\n";
$code.=" mov 2($sbox,$tmp2,8),$tmp2\n";
$code.=" and \$0x0000ff00,$tmp0\n";
$code.=" and \$0x00ff0000,$tmp1\n";
$code.=" and \$0xff000000,$tmp2\n";
$code.=" xor $tmp0,$out\n";
$code.=" mov $t0,$s[1]\n" if ($i==3);
$code.=" xor $tmp1,$out\n";
$code.=" mov $t1,$s[2]\n" if ($i==3);
$code.=" xor $tmp2,$out\n";
$code.=" mov $t2,$s[3]\n" if ($i==3);
$code.="\n";
}
$code.=<<___;
.type _x86_64_AES_encrypt,\@abi-omnipotent
.align 16
_x86_64_AES_encrypt:
xor 0($key),$s0 # xor with key
xor 4($key),$s1
xor 8($key),$s2
xor 12($key),$s3
mov 240($key),$rnds # load key->rounds
sub \$1,$rnds
jmp .Lenc_loop
.align 16
if ($verticalspin) { &encvert(); }
else { &encstep(0,$s0,$s1,$s2,$s3);
&encstep(1,$s1,$s2,$s3,$s0);
&encstep(2,$s2,$s3,$s0,$s1);
&encstep(3,$s3,$s0,$s1,$s2);
}
if ($verticalspin) { &enclastvert(); }
else { &enclast(0,$s0,$s1,$s2,$s3);
&enclast(1,$s1,$s2,$s3,$s0);
&enclast(2,$s2,$s3,$s0,$s1);
&enclast(3,$s3,$s0,$s1,$s2);
$code.=<<___;
xor 16+0($key),$s0 # xor with key
xor 16+4($key),$s1
xor 16+8($key),$s2
xor 16+12($key),$s3
___
}
$code.=<<___;
.byte 0xf3,0xc3 # rep ret
.size _x86_64_AES_encrypt,.-_x86_64_AES_encrypt
___
# void AES_encrypt (const void *inp,void *out,const AES_KEY *key);
$code.=<<___;
.globl AES_encrypt
.type AES_encrypt,\@function,3
.align 16
AES_encrypt:
push %rbx
push %rbp
push %r12
push %r13
push %r14
push %r15
mov %rdx,$key
.picmeup $sbox
lea AES_Te-.($sbox),$sbox
mov 0($inp),$s0
mov 4($inp),$s1
mov 8($inp),$s2
mov 12($inp),$s3
call _x86_64_AES_encrypt
mov $s0,0($out)
mov $s1,4($out)
mov $s2,8($out)
mov $s3,12($out)
pop %r15
pop %r14
pop %r13
pop %r12
pop %rbp
pop %rbx
ret
.size AES_encrypt,.-AES_encrypt
___
#------------------------------------------------------------------#
sub decvert()
{ my $t3="%r8d"; # zaps $inp!
$code.=<<___;
# favor 3-way issue Opteron pipeline...
movzb `&lo("$s0")`,$acc0
movzb `&lo("$s1")`,$acc1
movzb `&lo("$s2")`,$acc2
mov 0($sbox,$acc0,8),$t0
mov 0($sbox,$acc1,8),$t1
mov 0($sbox,$acc2,8),$t2
movzb `&hi("$s3")`,$acc0
movzb `&hi("$s0")`,$acc1
movzb `&lo("$s3")`,$acc2
xor 3($sbox,$acc0,8),$t0
xor 3($sbox,$acc1,8),$t1
mov 0($sbox,$acc2,8),$t3
movzb `&hi("$s1")`,$acc0
movzb `&hi("$s2")`,$acc2
xor 3($sbox,$acc0,8),$t2
shr \$16,$s3
xor 3($sbox,$acc2,8),$t3
shr \$16,$s1
lea 16($key),$key
shr \$16,$s2
movzb `&lo("$s2")`,$acc0
movzb `&lo("$s3")`,$acc1
movzb `&lo("$s0")`,$acc2
xor 2($sbox,$acc0,8),$t0
xor 2($sbox,$acc1,8),$t1
xor 2($sbox,$acc2,8),$t2
movzb `&hi("$s1")`,$acc0
movzb `&hi("$s2")`,$acc1
movzb `&lo("$s1")`,$acc2
xor 1($sbox,$acc0,8),$t0
xor 1($sbox,$acc1,8),$t1
xor 2($sbox,$acc2,8),$t3
movzb `&hi("$s3")`,$acc0
movzb `&hi("$s0")`,$acc2
xor 1($sbox,$acc0,8),$t2
mov 0($key),$s0
xor 1($sbox,$acc2,8),$t3
xor $t0,$s0
mov 4($key),$s1
mov 8($key),$s2
xor $t2,$s2
xor $t1,$s1
xor $t3,$s3
___
}
sub declastvert()
{ my $t3="%r8d"; # zaps $inp!
$code.=<<___;
movzb `&lo("$s0")`,$acc0
movzb `&lo("$s1")`,$acc1
movzb `&lo("$s2")`,$acc2
movzb 2048($sbox,$acc0,1),$t0
movzb 2048($sbox,$acc1,1),$t1
movzb 2048($sbox,$acc2,1),$t2
movzb `&lo("$s3")`,$acc0
movzb `&hi("$s3")`,$acc1
movzb `&hi("$s0")`,$acc2
movzb 2048($sbox,$acc0,1),$t3
movzb 2048($sbox,$acc1,1),$acc1 #$t0
movzb 2048($sbox,$acc2,1),$acc2 #$t1
shl \$8,$acc1
shl \$8,$acc2
xor $acc1,$t0
xor $acc2,$t1
shr \$16,$s3
movzb `&hi("$s1")`,$acc0
movzb `&hi("$s2")`,$acc1
movzb 2048($sbox,$acc0,1),$acc0 #$t2
movzb 2048($sbox,$acc1,1),$acc1 #$t3
shl \$8,$acc0
shl \$8,$acc1
shr \$16,$s1
xor $acc0,$t2
xor $acc1,$t3
shr \$16,$s2
movzb `&lo("$s2")`,$acc0
movzb `&lo("$s3")`,$acc1
movzb `&lo("$s0")`,$acc2
movzb 2048($sbox,$acc0,1),$acc0 #$t0
movzb 2048($sbox,$acc1,1),$acc1 #$t1
movzb 2048($sbox,$acc2,1),$acc2 #$t2
shl \$16,$acc0
shl \$16,$acc1
shl \$16,$acc2
xor $acc0,$t0
xor $acc1,$t1
xor $acc2,$t2
movzb `&lo("$s1")`,$acc0
movzb `&hi("$s1")`,$acc1
movzb `&hi("$s2")`,$acc2
movzb 2048($sbox,$acc0,1),$acc0 #$t3
movzb 2048($sbox,$acc1,1),$acc1 #$t0
movzb 2048($sbox,$acc2,1),$acc2 #$t1
shl \$16,$acc0
shl \$24,$acc1
shl \$24,$acc2
xor $acc0,$t3
xor $acc1,$t0
xor $acc2,$t1
movzb `&hi("$s3")`,$acc0
movzb `&hi("$s0")`,$acc1
movzb 2048($sbox,$acc0,1),$acc0 #$t2
movzb 2048($sbox,$acc1,1),$acc1 #$t3
shl \$24,$acc0
shl \$24,$acc1
xor $acc0,$t2
xor $acc1,$t3
mov 16+4($key),$s1
mov 16+8($key),$s2
xor $t0,$s0
xor $t1,$s1
xor $t2,$s2
xor $t3,$s3
___
}
sub decstep()
{ my ($i,@s) = @_;
my $tmp0=$acc0;
my $tmp1=$acc1;
my $tmp2=$acc2;
my $out=($t0,$t1,$t2,$s[0])[$i];
$code.=" mov $s[0],$out\n" if ($i!=3);
$tmp1=$s[2] if ($i==3);
$code.=" mov $s[2],$tmp1\n" if ($i!=3);
$code.=" and \$0xFF,$out\n";
$code.=" shr \$16,$tmp1\n";
$tmp2=$s[3] if ($i==3);
$code.=" mov $s[3],$tmp2\n" if ($i!=3);
$tmp0=$s[1] if ($i==3);
$code.=" movzb ".&hi($s[1]).",$tmp0\n";
$code.=" and \$0xFF,$tmp1\n";
$code.=" shr \$24,$tmp2\n";
$code.=" xor 3($sbox,$tmp0,8),$out\n";
$code.=" xor 2($sbox,$tmp1,8),$out\n";
$code.=" xor 1($sbox,$tmp2,8),$out\n";
$code.=" mov $t2,$s[1]\n" if ($i==3);
$code.=" mov $t1,$s[2]\n" if ($i==3);
$code.=" mov $t0,$s[3]\n" if ($i==3);
$code.="\n";
}
sub declast()
{ my ($i,@s)=@_;
my $tmp0=$acc0;
my $tmp1=$acc1;
my $tmp2=$acc2;
my $out=($t0,$t1,$t2,$s[0])[$i];
$code.=" mov $s[0],$out\n" if ($i!=3);
$tmp1=$s[2] if ($i==3);
$code.=" mov $s[2],$tmp1\n" if ($i!=3);
$code.=" and \$0xFF,$out\n";
$code.=" movzb 2048($sbox,$out,1),$out\n";
$code.=" shr \$16,$tmp1\n";
$tmp2=$s[3] if ($i==3);
$code.=" mov $s[3],$tmp2\n" if ($i!=3);
$tmp0=$s[1] if ($i==3);
$code.=" movzb ".&hi($s[1]).",$tmp0\n";
$code.=" and \$0xFF,$tmp1\n";
$code.=" shr \$24,$tmp2\n";
$code.=" movzb 2048($sbox,$tmp0,1),$tmp0\n";
$code.=" movzb 2048($sbox,$tmp1,1),$tmp1\n";
$code.=" movzb 2048($sbox,$tmp2,1),$tmp2\n";
$code.=" shl \$8,$tmp0\n";
$code.=" shl \$16,$tmp1\n";
$code.=" shl \$24,$tmp2\n";
$code.=" xor $tmp0,$out\n";
$code.=" mov $t2,$s[1]\n" if ($i==3);
$code.=" xor $tmp1,$out\n";
$code.=" mov $t1,$s[2]\n" if ($i==3);
$code.=" xor $tmp2,$out\n";
$code.=" mov $t0,$s[3]\n" if ($i==3);
$code.="\n";
}
$code.=<<___;
.type _x86_64_AES_decrypt,\@abi-omnipotent
.align 16
_x86_64_AES_decrypt:
xor 0($key),$s0 # xor with key
xor 4($key),$s1
xor 8($key),$s2
xor 12($key),$s3
mov 240($key),$rnds # load key->rounds
sub \$1,$rnds
jmp .Ldec_loop
.align 16
if ($verticalspin) { &decvert(); }
else { &decstep(0,$s0,$s3,$s2,$s1);
&decstep(1,$s1,$s0,$s3,$s2);
&decstep(2,$s2,$s1,$s0,$s3);
&decstep(3,$s3,$s2,$s1,$s0);
$code.=<<___;
lea 16($key),$key
xor 0($key),$s0 # xor with key
xor 4($key),$s1
xor 8($key),$s2
xor 12($key),$s3
___
}
if ($verticalspin) { &declastvert(); }
else { &declast(0,$s0,$s3,$s2,$s1);
&declast(1,$s1,$s0,$s3,$s2);
&declast(2,$s2,$s1,$s0,$s3);
&declast(3,$s3,$s2,$s1,$s0);
$code.=<<___;
xor 16+0($key),$s0 # xor with key
xor 16+4($key),$s1
xor 16+8($key),$s2
xor 16+12($key),$s3
$code.=<<___;
.byte 0xf3,0xc3 # rep ret
.size _x86_64_AES_decrypt,.-_x86_64_AES_decrypt
___
# void AES_decrypt (const void *inp,void *out,const AES_KEY *key);
$code.=<<___;
.globl AES_decrypt
.type AES_decrypt,\@function,3
.align 16
AES_decrypt:
push %rbx
push %rbp
push %r12
push %r13
push %r14
push %r15
mov %rdx,$key
.picmeup $sbox
lea AES_Td-.($sbox),$sbox
# prefetch Td4
lea 2048+128($sbox),$sbox;
mov 0-128($sbox),$s0
mov 32-128($sbox),$s1
mov 64-128($sbox),$s2
mov 96-128($sbox),$s3
mov 128-128($sbox),$s0
mov 160-128($sbox),$s1
mov 192-128($sbox),$s2
mov 224-128($sbox),$s3
lea -2048-128($sbox),$sbox;
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
mov 0($inp),$s0
mov 4($inp),$s1
mov 8($inp),$s2
mov 12($inp),$s3
call _x86_64_AES_decrypt
mov $s0,0($out)
mov $s1,4($out)
mov $s2,8($out)
mov $s3,12($out)
pop %r15
pop %r14
pop %r13
pop %r12
pop %rbp
pop %rbx
ret
.size AES_decrypt,.-AES_decrypt
___
#------------------------------------------------------------------#
sub enckey()
{
$code.=<<___;
movz %dl,%esi # rk[i]>>0
mov 2(%rbp,%rsi,8),%ebx
movz %dh,%esi # rk[i]>>8
and \$0xFF000000,%ebx
xor %ebx,%eax
mov 2(%rbp,%rsi,8),%ebx
shr \$16,%edx
and \$0x000000FF,%ebx
movz %dl,%esi # rk[i]>>16
xor %ebx,%eax
mov 0(%rbp,%rsi,8),%ebx
movz %dh,%esi # rk[i]>>24
and \$0x0000FF00,%ebx
xor %ebx,%eax
mov 0(%rbp,%rsi,8),%ebx
and \$0x00FF0000,%ebx
xor %ebx,%eax
xor 2048(%rbp,%rcx,4),%eax # rcon
___
}
# int AES_set_encrypt_key(const unsigned char *userKey, const int bits,
# AES_KEY *key)
$code.=<<___;
.globl AES_set_encrypt_key
.type AES_set_encrypt_key,\@function,3
.align 16
AES_set_encrypt_key:
push %rbx
push %rbp
mov %esi,%ecx # %ecx=bits
mov %rdi,%rsi # %rsi=userKey
mov %rdx,%rdi # %rdi=key
test \$-1,%rsi
jz .Lbadpointer
test \$-1,%rdi
jz .Lbadpointer
.picmeup %rbp
lea AES_Te-.(%rbp),%rbp
cmp \$128,%ecx
je .L10rounds
cmp \$192,%ecx
je .L12rounds
cmp \$256,%ecx
je .L14rounds
mov \$-2,%rax # invalid number of bits
jmp .Lexit
.L10rounds:
mov 0(%rsi),%eax # copy first 4 dwords
mov 4(%rsi),%ebx
mov 8(%rsi),%ecx
mov 12(%rsi),%edx
mov %eax,0(%rdi)
mov %ebx,4(%rdi)
mov %ecx,8(%rdi)
mov %edx,12(%rdi)
xor %ecx,%ecx
jmp .L10shortcut
.align 4
.L10loop:
mov 0(%rdi),%eax # rk[0]
mov 12(%rdi),%edx # rk[3]
.L10shortcut:
___
&enckey ();
$code.=<<___;
mov %eax,16(%rdi) # rk[4]
xor 4(%rdi),%eax
mov %eax,20(%rdi) # rk[5]
xor 8(%rdi),%eax
mov %eax,24(%rdi) # rk[6]
xor 12(%rdi),%eax
mov %eax,28(%rdi) # rk[7]
add \$1,%ecx
lea 16(%rdi),%rdi
cmp \$10,%ecx
jl .L10loop
movl \$10,80(%rdi) # setup number of rounds
xor %rax,%rax
jmp .Lexit
.L12rounds:
mov 0(%rsi),%eax # copy first 6 dwords
mov 4(%rsi),%ebx
mov 8(%rsi),%ecx
mov 12(%rsi),%edx
mov %eax,0(%rdi)
mov %ebx,4(%rdi)
mov %ecx,8(%rdi)
mov %edx,12(%rdi)
mov 16(%rsi),%ecx
mov 20(%rsi),%edx
mov %ecx,16(%rdi)
mov %edx,20(%rdi)
xor %ecx,%ecx
jmp .L12shortcut
.align 4
.L12loop:
mov 0(%rdi),%eax # rk[0]
mov 20(%rdi),%edx # rk[5]
.L12shortcut:
___
&enckey ();
$code.=<<___;
mov %eax,24(%rdi) # rk[6]
xor 4(%rdi),%eax
mov %eax,28(%rdi) # rk[7]
xor 8(%rdi),%eax
mov %eax,32(%rdi) # rk[8]
xor 12(%rdi),%eax
mov %eax,36(%rdi) # rk[9]
cmp \$7,%ecx
je .L12break
add \$1,%ecx
xor 16(%rdi),%eax
mov %eax,40(%rdi) # rk[10]
xor 20(%rdi),%eax
mov %eax,44(%rdi) # rk[11]
lea 24(%rdi),%rdi
jmp .L12loop
.L12break:
movl \$12,72(%rdi) # setup number of rounds
xor %rax,%rax
jmp .Lexit
.L14rounds:
mov 0(%rsi),%eax # copy first 8 dwords
mov 4(%rsi),%ebx
mov 8(%rsi),%ecx
mov 12(%rsi),%edx
mov %eax,0(%rdi)
mov %ebx,4(%rdi)
mov %ecx,8(%rdi)
mov %edx,12(%rdi)
mov 16(%rsi),%eax
mov 20(%rsi),%ebx
mov 24(%rsi),%ecx
mov 28(%rsi),%edx
mov %eax,16(%rdi)
mov %ebx,20(%rdi)
mov %ecx,24(%rdi)
mov %edx,28(%rdi)
xor %ecx,%ecx
jmp .L14shortcut
.align 4
.L14loop:
mov 28(%rdi),%edx # rk[4]
.L14shortcut:
mov 0(%rdi),%eax # rk[0]
___
&enckey ();
$code.=<<___;
mov %eax,32(%rdi) # rk[8]
xor 4(%rdi),%eax
mov %eax,36(%rdi) # rk[9]
xor 8(%rdi),%eax
mov %eax,40(%rdi) # rk[10]
xor 12(%rdi),%eax
mov %eax,44(%rdi) # rk[11]
cmp \$6,%ecx
je .L14break
add \$1,%ecx
mov %eax,%edx
mov 16(%rdi),%eax # rk[4]
movz %dl,%esi # rk[11]>>0
mov 2(%rbp,%rsi,8),%ebx
movz %dh,%esi # rk[11]>>8
and \$0x000000FF,%ebx
xor %ebx,%eax
mov 0(%rbp,%rsi,8),%ebx
shr \$16,%edx
and \$0x0000FF00,%ebx
movz %dl,%esi # rk[11]>>16
xor %ebx,%eax
mov 0(%rbp,%rsi,8),%ebx
movz %dh,%esi # rk[11]>>24
and \$0x00FF0000,%ebx
xor %ebx,%eax
mov 2(%rbp,%rsi,8),%ebx
and \$0xFF000000,%ebx
xor %ebx,%eax
mov %eax,48(%rdi) # rk[12]
xor 20(%rdi),%eax
mov %eax,52(%rdi) # rk[13]
xor 24(%rdi),%eax
mov %eax,56(%rdi) # rk[14]
xor 28(%rdi),%eax
mov %eax,60(%rdi) # rk[15]
lea 32(%rdi),%rdi
jmp .L14loop
.L14break:
movl \$14,48(%rdi) # setup number of rounds
xor %rax,%rax
jmp .Lexit
.Lbadpointer:
mov \$-1,%rax
.Lexit:
pop %rbp
pop %rbx
ret
.size AES_set_encrypt_key,.-AES_set_encrypt_key
___
sub deckey()
{ my ($i,$ptr,$te,$td) = @_;
$code.=<<___;
mov $i($ptr),%eax
mov %eax,%edx
movz %ah,%ebx
shr \$16,%edx
and \$0xFF,%eax
movzb 2($te,%rax,8),%rax
movzb 2($te,%rbx,8),%rbx
mov 0($td,%rax,8),%eax
xor 3($td,%rbx,8),%eax
movzb %dh,%ebx
and \$0xFF,%edx
movzb 2($te,%rdx,8),%rdx
movzb 2($te,%rbx,8),%rbx
xor 2($td,%rdx,8),%eax
xor 1($td,%rbx,8),%eax
mov %eax,$i($ptr)
___
}
# int AES_set_decrypt_key(const unsigned char *userKey, const int bits,
# AES_KEY *key)
$code.=<<___;
.globl AES_set_decrypt_key
.type AES_set_decrypt_key,\@function,3
.align 16
AES_set_decrypt_key:
push %rdx
call AES_set_encrypt_key
cmp \$0,%eax
je .Lproceed
lea 24(%rsp),%rsp
ret
.Lproceed:
mov (%rsp),%r8 # restore key schedule
mov %rbx,(%rsp)
mov 240(%r8),%ecx # pull number of rounds
xor %rdi,%rdi
lea (%rdi,%rcx,4),%rcx
mov %r8,%rsi
lea (%r8,%rcx,4),%rdi # pointer to last chunk
.align 4
.Linvert:
mov 0(%rsi),%rax
mov 8(%rsi),%rbx
mov 0(%rdi),%rcx
mov 8(%rdi),%rdx
mov %rax,0(%rdi)
mov %rbx,8(%rdi)
mov %rcx,0(%rsi)
mov %rdx,8(%rsi)