1#! /usr/bin/env perl 2# Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved. 3# 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 8 9# The inner loop instruction sequence and the IP/FP modifications are from 10# Svend Olaf Mikkelsen 11 12$0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1; 13push(@INC,"${dir}","${dir}../../perlasm"); 14require "x86asm.pl"; 15 16$output=pop; 17open STDOUT,">$output"; 18 19&asm_init($ARGV[0]); 20 21$L="edi"; 22$R="esi"; 23 24&external_label("DES_SPtrans"); 25&fcrypt_body("fcrypt_body"); 26&asm_finish(); 27 28close STDOUT or die "error closing STDOUT: $!"; 29 30sub fcrypt_body 31 { 32 local($name,$do_ip)=@_; 33 34 &function_begin($name); 35 36 &comment(""); 37 &comment("Load the 2 words"); 38 $trans="ebp"; 39 40 &xor( $L, $L); 41 &xor( $R, $R); 42 43 # PIC-ification:-) 44 &picmeup("edx","DES_SPtrans"); 45 #if ($cpp) { &picmeup("edx","DES_SPtrans"); } 46 #else { &lea("edx",&DWP("DES_SPtrans")); } 47 &push("edx"); # becomes &swtmp(1) 48 # 49 &mov($trans,&wparam(1)); # reloaded with DES_SPtrans in D_ENCRYPT 50 51 &push(&DWC(25)); # add a variable 52 53 &set_label("start"); 54 for ($i=0; $i<16; $i+=2) 55 { 56 &comment(""); 57 &comment("Round $i"); 58 &D_ENCRYPT($i,$L,$R,$i*2,$trans,"eax","ebx","ecx","edx"); 59 60 &comment(""); 61 &comment("Round ".sprintf("%d",$i+1)); 62 &D_ENCRYPT($i+1,$R,$L,($i+1)*2,$trans,"eax","ebx","ecx","edx"); 63 } 64 &mov("ebx", &swtmp(0)); 65 &mov("eax", $L); 66 &dec("ebx"); 67 &mov($L, $R); 68 &mov($R, "eax"); 69 &mov(&swtmp(0), "ebx"); 70 &jnz(&label("start")); 71 72 &comment(""); 73 &comment("FP"); 74 &mov("edx",&wparam(0)); 75 76 &FP_new($R,$L,"eax",3); 77 &mov(&DWP(0,"edx","",0),"eax"); 78 &mov(&DWP(4,"edx","",0),$L); 79 80 &add("esp",8); # remove variables 81 82 &function_end($name); 83 } 84 85sub D_ENCRYPT 86 { 87 local($r,$L,$R,$S,$trans,$u,$tmp1,$tmp2,$t)=@_; 88 89 &mov( $u, &wparam(2)); # 2 90 &mov( $t, $R); 91 &shr( $t, 16); # 1 92 &mov( $tmp2, &wparam(3)); # 2 93 &xor( $t, $R); # 1 94 95 &and( $u, $t); # 2 96 &and( $t, $tmp2); # 2 97 98 &mov( $tmp1, $u); 99 &shl( $tmp1, 16); # 1 100 &mov( $tmp2, $t); 101 &shl( $tmp2, 16); # 1 102 &xor( $u, $tmp1); # 2 103 &xor( $t, $tmp2); # 2 104 &mov( $tmp1, &DWP(&n2a($S*4),$trans,"",0)); # 2 105 &xor( $u, $tmp1); 106 &mov( $tmp2, &DWP(&n2a(($S+1)*4),$trans,"",0)); # 2 107 &xor( $u, $R); 108 &xor( $t, $R); 109 &xor( $t, $tmp2); 110 111 &and( $u, "0xfcfcfcfc" ); # 2 112 &xor( $tmp1, $tmp1); # 1 113 &and( $t, "0xcfcfcfcf" ); # 2 114 &xor( $tmp2, $tmp2); 115 &movb( &LB($tmp1), &LB($u) ); 116 &movb( &LB($tmp2), &HB($u) ); 117 &rotr( $t, 4 ); 118 &mov( $trans, &swtmp(1)); 119 &xor( $L, &DWP(" ",$trans,$tmp1,0)); 120 &movb( &LB($tmp1), &LB($t) ); 121 &xor( $L, &DWP("0x200",$trans,$tmp2,0)); 122 &movb( &LB($tmp2), &HB($t) ); 123 &shr( $u, 16); 124 &xor( $L, &DWP("0x100",$trans,$tmp1,0)); 125 &movb( &LB($tmp1), &HB($u) ); 126 &shr( $t, 16); 127 &xor( $L, &DWP("0x300",$trans,$tmp2,0)); 128 &movb( &LB($tmp2), &HB($t) ); 129 &and( $u, "0xff" ); 130 &and( $t, "0xff" ); 131 &mov( $tmp1, &DWP("0x600",$trans,$tmp1,0)); 132 &xor( $L, $tmp1); 133 &mov( $tmp1, &DWP("0x700",$trans,$tmp2,0)); 134 &xor( $L, $tmp1); 135 &mov( $tmp1, &DWP("0x400",$trans,$u,0)); 136 &xor( $L, $tmp1); 137 &mov( $tmp1, &DWP("0x500",$trans,$t,0)); 138 &xor( $L, $tmp1); 139 &mov( $trans, &wparam(1)); 140 } 141 142sub n2a 143 { 144 sprintf("%d",$_[0]); 145 } 146 147# now has a side affect of rotating $a by $shift 148sub R_PERM_OP 149 { 150 local($a,$b,$tt,$shift,$mask,$last)=@_; 151 152 &rotl( $a, $shift ) if ($shift != 0); 153 &mov( $tt, $a ); 154 &xor( $a, $b ); 155 &and( $a, $mask ); 156 if ($notlast eq $b) 157 { 158 &xor( $b, $a ); 159 &xor( $tt, $a ); 160 } 161 else 162 { 163 &xor( $tt, $a ); 164 &xor( $b, $a ); 165 } 166 &comment(""); 167 } 168 169sub IP_new 170 { 171 local($l,$r,$tt,$lr)=@_; 172 173 &R_PERM_OP($l,$r,$tt, 4,"0xf0f0f0f0",$l); 174 &R_PERM_OP($r,$tt,$l,20,"0xfff0000f",$l); 175 &R_PERM_OP($l,$tt,$r,14,"0x33333333",$r); 176 &R_PERM_OP($tt,$r,$l,22,"0x03fc03fc",$r); 177 &R_PERM_OP($l,$r,$tt, 9,"0xaaaaaaaa",$r); 178 179 if ($lr != 3) 180 { 181 if (($lr-3) < 0) 182 { &rotr($tt, 3-$lr); } 183 else { &rotl($tt, $lr-3); } 184 } 185 if ($lr != 2) 186 { 187 if (($lr-2) < 0) 188 { &rotr($r, 2-$lr); } 189 else { &rotl($r, $lr-2); } 190 } 191 } 192 193sub FP_new 194 { 195 local($l,$r,$tt,$lr)=@_; 196 197 if ($lr != 2) 198 { 199 if (($lr-2) < 0) 200 { &rotl($r, 2-$lr); } 201 else { &rotr($r, $lr-2); } 202 } 203 if ($lr != 3) 204 { 205 if (($lr-3) < 0) 206 { &rotl($l, 3-$lr); } 207 else { &rotr($l, $lr-3); } 208 } 209 210 &R_PERM_OP($l,$r,$tt, 0,"0xaaaaaaaa",$r); 211 &R_PERM_OP($tt,$r,$l,23,"0x03fc03fc",$r); 212 &R_PERM_OP($l,$r,$tt,10,"0x33333333",$l); 213 &R_PERM_OP($r,$tt,$l,18,"0xfff0000f",$l); 214 &R_PERM_OP($l,$tt,$r,12,"0xf0f0f0f0",$r); 215 &rotr($tt , 4); 216 } 217 218