diff options
Diffstat (limited to 'src/lib/libcrypto/perlasm')
| -rw-r--r-- | src/lib/libcrypto/perlasm/cbc.pl | 351 | ||||
| -rw-r--r-- | src/lib/libcrypto/perlasm/readme | 124 | ||||
| -rwxr-xr-x | src/lib/libcrypto/perlasm/x86_64-xlate.pl | 557 | ||||
| -rw-r--r-- | src/lib/libcrypto/perlasm/x86asm.pl | 136 | ||||
| -rw-r--r-- | src/lib/libcrypto/perlasm/x86unix.pl | 91 |
5 files changed, 1255 insertions, 4 deletions
diff --git a/src/lib/libcrypto/perlasm/cbc.pl b/src/lib/libcrypto/perlasm/cbc.pl new file mode 100644 index 0000000000..e43dc9ae15 --- /dev/null +++ b/src/lib/libcrypto/perlasm/cbc.pl | |||
| @@ -0,0 +1,351 @@ | |||
| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | # void des_ncbc_encrypt(input, output, length, schedule, ivec, enc) | ||
| 4 | # des_cblock (*input); | ||
| 5 | # des_cblock (*output); | ||
| 6 | # long length; | ||
| 7 | # des_key_schedule schedule; | ||
| 8 | # des_cblock (*ivec); | ||
| 9 | # int enc; | ||
| 10 | # | ||
| 11 | # calls | ||
| 12 | # des_encrypt((DES_LONG *)tin,schedule,DES_ENCRYPT); | ||
| 13 | # | ||
| 14 | |||
| 15 | #&cbc("des_ncbc_encrypt","des_encrypt",0); | ||
| 16 | #&cbc("BF_cbc_encrypt","BF_encrypt","BF_encrypt", | ||
| 17 | # 1,4,5,3,5,-1); | ||
| 18 | #&cbc("des_ncbc_encrypt","des_encrypt","des_encrypt", | ||
| 19 | # 0,4,5,3,5,-1); | ||
| 20 | #&cbc("des_ede3_cbc_encrypt","des_encrypt3","des_decrypt3", | ||
| 21 | # 0,6,7,3,4,5); | ||
| 22 | # | ||
| 23 | # When doing a cipher that needs bigendian order, | ||
| 24 | # for encrypt, the iv is kept in bigendian form, | ||
| 25 | # while for decrypt, it is kept in little endian. | ||
| 26 | sub cbc | ||
| 27 | { | ||
| 28 | local($name,$enc_func,$dec_func,$swap,$iv_off,$enc_off,$p1,$p2,$p3)=@_; | ||
| 29 | # name is the function name | ||
| 30 | # enc_func and dec_func and the functions to call for encrypt/decrypt | ||
| 31 | # swap is true if byte order needs to be reversed | ||
| 32 | # iv_off is parameter number for the iv | ||
| 33 | # enc_off is parameter number for the encrypt/decrypt flag | ||
| 34 | # p1,p2,p3 are the offsets for parameters to be passed to the | ||
| 35 | # underlying calls. | ||
| 36 | |||
| 37 | &function_begin_B($name,""); | ||
| 38 | &comment(""); | ||
| 39 | |||
| 40 | $in="esi"; | ||
| 41 | $out="edi"; | ||
| 42 | $count="ebp"; | ||
| 43 | |||
| 44 | &push("ebp"); | ||
| 45 | &push("ebx"); | ||
| 46 | &push("esi"); | ||
| 47 | &push("edi"); | ||
| 48 | |||
| 49 | $data_off=4; | ||
| 50 | $data_off+=4 if ($p1 > 0); | ||
| 51 | $data_off+=4 if ($p2 > 0); | ||
| 52 | $data_off+=4 if ($p3 > 0); | ||
| 53 | |||
| 54 | &mov($count, &wparam(2)); # length | ||
| 55 | |||
| 56 | &comment("getting iv ptr from parameter $iv_off"); | ||
| 57 | &mov("ebx", &wparam($iv_off)); # Get iv ptr | ||
| 58 | |||
| 59 | &mov($in, &DWP(0,"ebx","",0));# iv[0] | ||
| 60 | &mov($out, &DWP(4,"ebx","",0));# iv[1] | ||
| 61 | |||
| 62 | &push($out); | ||
| 63 | &push($in); | ||
| 64 | &push($out); # used in decrypt for iv[1] | ||
| 65 | &push($in); # used in decrypt for iv[0] | ||
| 66 | |||
| 67 | &mov("ebx", "esp"); # This is the address of tin[2] | ||
| 68 | |||
| 69 | &mov($in, &wparam(0)); # in | ||
| 70 | &mov($out, &wparam(1)); # out | ||
| 71 | |||
| 72 | # We have loaded them all, how lets push things | ||
| 73 | &comment("getting encrypt flag from parameter $enc_off"); | ||
| 74 | &mov("ecx", &wparam($enc_off)); # Get enc flag | ||
| 75 | if ($p3 > 0) | ||
| 76 | { | ||
| 77 | &comment("get and push parameter $p3"); | ||
| 78 | if ($enc_off != $p3) | ||
| 79 | { &mov("eax", &wparam($p3)); &push("eax"); } | ||
| 80 | else { &push("ecx"); } | ||
| 81 | } | ||
| 82 | if ($p2 > 0) | ||
| 83 | { | ||
| 84 | &comment("get and push parameter $p2"); | ||
| 85 | if ($enc_off != $p2) | ||
| 86 | { &mov("eax", &wparam($p2)); &push("eax"); } | ||
| 87 | else { &push("ecx"); } | ||
| 88 | } | ||
| 89 | if ($p1 > 0) | ||
| 90 | { | ||
| 91 | &comment("get and push parameter $p1"); | ||
| 92 | if ($enc_off != $p1) | ||
| 93 | { &mov("eax", &wparam($p1)); &push("eax"); } | ||
| 94 | else { &push("ecx"); } | ||
| 95 | } | ||
| 96 | &push("ebx"); # push data/iv | ||
| 97 | |||
| 98 | &cmp("ecx",0); | ||
| 99 | &jz(&label("decrypt")); | ||
| 100 | |||
| 101 | &and($count,0xfffffff8); | ||
| 102 | &mov("eax", &DWP($data_off,"esp","",0)); # load iv[0] | ||
| 103 | &mov("ebx", &DWP($data_off+4,"esp","",0)); # load iv[1] | ||
| 104 | |||
| 105 | &jz(&label("encrypt_finish")); | ||
| 106 | |||
| 107 | ############################################################# | ||
| 108 | |||
| 109 | &set_label("encrypt_loop"); | ||
| 110 | # encrypt start | ||
| 111 | # "eax" and "ebx" hold iv (or the last cipher text) | ||
| 112 | |||
| 113 | &mov("ecx", &DWP(0,$in,"",0)); # load first 4 bytes | ||
| 114 | &mov("edx", &DWP(4,$in,"",0)); # second 4 bytes | ||
| 115 | |||
| 116 | &xor("eax", "ecx"); | ||
| 117 | &xor("ebx", "edx"); | ||
| 118 | |||
| 119 | &bswap("eax") if $swap; | ||
| 120 | &bswap("ebx") if $swap; | ||
| 121 | |||
| 122 | &mov(&DWP($data_off,"esp","",0), "eax"); # put in array for call | ||
| 123 | &mov(&DWP($data_off+4,"esp","",0), "ebx"); # | ||
| 124 | |||
| 125 | &call($enc_func); | ||
| 126 | |||
| 127 | &mov("eax", &DWP($data_off,"esp","",0)); | ||
| 128 | &mov("ebx", &DWP($data_off+4,"esp","",0)); | ||
| 129 | |||
| 130 | &bswap("eax") if $swap; | ||
| 131 | &bswap("ebx") if $swap; | ||
| 132 | |||
| 133 | &mov(&DWP(0,$out,"",0),"eax"); | ||
| 134 | &mov(&DWP(4,$out,"",0),"ebx"); | ||
| 135 | |||
| 136 | # eax and ebx are the next iv. | ||
| 137 | |||
| 138 | &add($in, 8); | ||
| 139 | &add($out, 8); | ||
| 140 | |||
| 141 | &sub($count, 8); | ||
| 142 | &jnz(&label("encrypt_loop")); | ||
| 143 | |||
| 144 | ###################################################################3 | ||
| 145 | &set_label("encrypt_finish"); | ||
| 146 | &mov($count, &wparam(2)); # length | ||
| 147 | &and($count, 7); | ||
| 148 | &jz(&label("finish")); | ||
| 149 | &call(&label("PIC_point")); | ||
| 150 | &set_label("PIC_point"); | ||
| 151 | &blindpop("edx"); | ||
| 152 | &lea("ecx",&DWP(&label("cbc_enc_jmp_table")."-".&label("PIC_point"),"edx")); | ||
| 153 | &mov($count,&DWP(0,"ecx",$count,4)) | ||
| 154 | &add($count,"edx"); | ||
| 155 | &xor("ecx","ecx"); | ||
| 156 | &xor("edx","edx"); | ||
| 157 | #&mov($count,&DWP(&label("cbc_enc_jmp_table"),"",$count,4)); | ||
| 158 | &jmp_ptr($count); | ||
| 159 | |||
| 160 | &set_label("ej7"); | ||
| 161 | &xor("edx", "edx") if $ppro; # ppro friendly | ||
| 162 | &movb(&HB("edx"), &BP(6,$in,"",0)); | ||
| 163 | &shl("edx",8); | ||
| 164 | &set_label("ej6"); | ||
| 165 | &movb(&HB("edx"), &BP(5,$in,"",0)); | ||
| 166 | &set_label("ej5"); | ||
| 167 | &movb(&LB("edx"), &BP(4,$in,"",0)); | ||
| 168 | &set_label("ej4"); | ||
| 169 | &mov("ecx", &DWP(0,$in,"",0)); | ||
| 170 | &jmp(&label("ejend")); | ||
| 171 | &set_label("ej3"); | ||
| 172 | &movb(&HB("ecx"), &BP(2,$in,"",0)); | ||
| 173 | &xor("ecx", "ecx") if $ppro; # ppro friendly | ||
| 174 | &shl("ecx",8); | ||
| 175 | &set_label("ej2"); | ||
| 176 | &movb(&HB("ecx"), &BP(1,$in,"",0)); | ||
| 177 | &set_label("ej1"); | ||
| 178 | &movb(&LB("ecx"), &BP(0,$in,"",0)); | ||
| 179 | &set_label("ejend"); | ||
| 180 | |||
| 181 | &xor("eax", "ecx"); | ||
| 182 | &xor("ebx", "edx"); | ||
| 183 | |||
| 184 | &bswap("eax") if $swap; | ||
| 185 | &bswap("ebx") if $swap; | ||
| 186 | |||
| 187 | &mov(&DWP($data_off,"esp","",0), "eax"); # put in array for call | ||
| 188 | &mov(&DWP($data_off+4,"esp","",0), "ebx"); # | ||
| 189 | |||
| 190 | &call($enc_func); | ||
| 191 | |||
| 192 | &mov("eax", &DWP($data_off,"esp","",0)); | ||
| 193 | &mov("ebx", &DWP($data_off+4,"esp","",0)); | ||
| 194 | |||
| 195 | &bswap("eax") if $swap; | ||
| 196 | &bswap("ebx") if $swap; | ||
| 197 | |||
| 198 | &mov(&DWP(0,$out,"",0),"eax"); | ||
| 199 | &mov(&DWP(4,$out,"",0),"ebx"); | ||
| 200 | |||
| 201 | &jmp(&label("finish")); | ||
| 202 | |||
| 203 | ############################################################# | ||
| 204 | ############################################################# | ||
| 205 | &set_label("decrypt",1); | ||
| 206 | # decrypt start | ||
| 207 | &and($count,0xfffffff8); | ||
| 208 | # The next 2 instructions are only for if the jz is taken | ||
| 209 | &mov("eax", &DWP($data_off+8,"esp","",0)); # get iv[0] | ||
| 210 | &mov("ebx", &DWP($data_off+12,"esp","",0)); # get iv[1] | ||
| 211 | &jz(&label("decrypt_finish")); | ||
| 212 | |||
| 213 | &set_label("decrypt_loop"); | ||
| 214 | &mov("eax", &DWP(0,$in,"",0)); # load first 4 bytes | ||
| 215 | &mov("ebx", &DWP(4,$in,"",0)); # second 4 bytes | ||
| 216 | |||
| 217 | &bswap("eax") if $swap; | ||
| 218 | &bswap("ebx") if $swap; | ||
| 219 | |||
| 220 | &mov(&DWP($data_off,"esp","",0), "eax"); # put back | ||
| 221 | &mov(&DWP($data_off+4,"esp","",0), "ebx"); # | ||
| 222 | |||
| 223 | &call($dec_func); | ||
| 224 | |||
| 225 | &mov("eax", &DWP($data_off,"esp","",0)); # get return | ||
| 226 | &mov("ebx", &DWP($data_off+4,"esp","",0)); # | ||
| 227 | |||
| 228 | &bswap("eax") if $swap; | ||
| 229 | &bswap("ebx") if $swap; | ||
| 230 | |||
| 231 | &mov("ecx", &DWP($data_off+8,"esp","",0)); # get iv[0] | ||
| 232 | &mov("edx", &DWP($data_off+12,"esp","",0)); # get iv[1] | ||
| 233 | |||
| 234 | &xor("ecx", "eax"); | ||
| 235 | &xor("edx", "ebx"); | ||
| 236 | |||
| 237 | &mov("eax", &DWP(0,$in,"",0)); # get old cipher text, | ||
| 238 | &mov("ebx", &DWP(4,$in,"",0)); # next iv actually | ||
| 239 | |||
| 240 | &mov(&DWP(0,$out,"",0),"ecx"); | ||
| 241 | &mov(&DWP(4,$out,"",0),"edx"); | ||
| 242 | |||
| 243 | &mov(&DWP($data_off+8,"esp","",0), "eax"); # save iv | ||
| 244 | &mov(&DWP($data_off+12,"esp","",0), "ebx"); # | ||
| 245 | |||
| 246 | &add($in, 8); | ||
| 247 | &add($out, 8); | ||
| 248 | |||
| 249 | &sub($count, 8); | ||
| 250 | &jnz(&label("decrypt_loop")); | ||
| 251 | ############################ ENDIT #######################3 | ||
| 252 | &set_label("decrypt_finish"); | ||
| 253 | &mov($count, &wparam(2)); # length | ||
| 254 | &and($count, 7); | ||
| 255 | &jz(&label("finish")); | ||
| 256 | |||
| 257 | &mov("eax", &DWP(0,$in,"",0)); # load first 4 bytes | ||
| 258 | &mov("ebx", &DWP(4,$in,"",0)); # second 4 bytes | ||
| 259 | |||
| 260 | &bswap("eax") if $swap; | ||
| 261 | &bswap("ebx") if $swap; | ||
| 262 | |||
| 263 | &mov(&DWP($data_off,"esp","",0), "eax"); # put back | ||
| 264 | &mov(&DWP($data_off+4,"esp","",0), "ebx"); # | ||
| 265 | |||
| 266 | &call($dec_func); | ||
| 267 | |||
| 268 | &mov("eax", &DWP($data_off,"esp","",0)); # get return | ||
| 269 | &mov("ebx", &DWP($data_off+4,"esp","",0)); # | ||
| 270 | |||
| 271 | &bswap("eax") if $swap; | ||
| 272 | &bswap("ebx") if $swap; | ||
| 273 | |||
| 274 | &mov("ecx", &DWP($data_off+8,"esp","",0)); # get iv[0] | ||
| 275 | &mov("edx", &DWP($data_off+12,"esp","",0)); # get iv[1] | ||
| 276 | |||
| 277 | &xor("ecx", "eax"); | ||
| 278 | &xor("edx", "ebx"); | ||
| 279 | |||
| 280 | # this is for when we exit | ||
| 281 | &mov("eax", &DWP(0,$in,"",0)); # get old cipher text, | ||
| 282 | &mov("ebx", &DWP(4,$in,"",0)); # next iv actually | ||
| 283 | |||
| 284 | &set_label("dj7"); | ||
| 285 | &rotr("edx", 16); | ||
| 286 | &movb(&BP(6,$out,"",0), &LB("edx")); | ||
| 287 | &shr("edx",16); | ||
| 288 | &set_label("dj6"); | ||
| 289 | &movb(&BP(5,$out,"",0), &HB("edx")); | ||
| 290 | &set_label("dj5"); | ||
| 291 | &movb(&BP(4,$out,"",0), &LB("edx")); | ||
| 292 | &set_label("dj4"); | ||
| 293 | &mov(&DWP(0,$out,"",0), "ecx"); | ||
| 294 | &jmp(&label("djend")); | ||
| 295 | &set_label("dj3"); | ||
| 296 | &rotr("ecx", 16); | ||
| 297 | &movb(&BP(2,$out,"",0), &LB("ecx")); | ||
| 298 | &shl("ecx",16); | ||
| 299 | &set_label("dj2"); | ||
| 300 | &movb(&BP(1,$in,"",0), &HB("ecx")); | ||
| 301 | &set_label("dj1"); | ||
| 302 | &movb(&BP(0,$in,"",0), &LB("ecx")); | ||
| 303 | &set_label("djend"); | ||
| 304 | |||
| 305 | # final iv is still in eax:ebx | ||
| 306 | &jmp(&label("finish")); | ||
| 307 | |||
| 308 | |||
| 309 | ############################ FINISH #######################3 | ||
| 310 | &set_label("finish",1); | ||
| 311 | &mov("ecx", &wparam($iv_off)); # Get iv ptr | ||
| 312 | |||
| 313 | ################################################# | ||
| 314 | $total=16+4; | ||
| 315 | $total+=4 if ($p1 > 0); | ||
| 316 | $total+=4 if ($p2 > 0); | ||
| 317 | $total+=4 if ($p3 > 0); | ||
| 318 | &add("esp",$total); | ||
| 319 | |||
| 320 | &mov(&DWP(0,"ecx","",0), "eax"); # save iv | ||
| 321 | &mov(&DWP(4,"ecx","",0), "ebx"); # save iv | ||
| 322 | |||
| 323 | &function_end_A($name); | ||
| 324 | |||
| 325 | &align(64); | ||
| 326 | &set_label("cbc_enc_jmp_table"); | ||
| 327 | &data_word("0"); | ||
| 328 | &data_word(&label("ej1")."-".&label("PIC_point")); | ||
| 329 | &data_word(&label("ej2")."-".&label("PIC_point")); | ||
| 330 | &data_word(&label("ej3")."-".&label("PIC_point")); | ||
| 331 | &data_word(&label("ej4")."-".&label("PIC_point")); | ||
| 332 | &data_word(&label("ej5")."-".&label("PIC_point")); | ||
| 333 | &data_word(&label("ej6")."-".&label("PIC_point")); | ||
| 334 | &data_word(&label("ej7")."-".&label("PIC_point")); | ||
| 335 | # not used | ||
| 336 | #&set_label("cbc_dec_jmp_table",1); | ||
| 337 | #&data_word("0"); | ||
| 338 | #&data_word(&label("dj1")."-".&label("PIC_point")); | ||
| 339 | #&data_word(&label("dj2")."-".&label("PIC_point")); | ||
| 340 | #&data_word(&label("dj3")."-".&label("PIC_point")); | ||
| 341 | #&data_word(&label("dj4")."-".&label("PIC_point")); | ||
| 342 | #&data_word(&label("dj5")."-".&label("PIC_point")); | ||
| 343 | #&data_word(&label("dj6")."-".&label("PIC_point")); | ||
| 344 | #&data_word(&label("dj7")."-".&label("PIC_point")); | ||
| 345 | &align(64); | ||
| 346 | |||
| 347 | &function_end_B($name); | ||
| 348 | |||
| 349 | } | ||
| 350 | |||
| 351 | 1; | ||
diff --git a/src/lib/libcrypto/perlasm/readme b/src/lib/libcrypto/perlasm/readme new file mode 100644 index 0000000000..f02bbee75a --- /dev/null +++ b/src/lib/libcrypto/perlasm/readme | |||
| @@ -0,0 +1,124 @@ | |||
| 1 | The perl scripts in this directory are my 'hack' to generate | ||
| 2 | multiple different assembler formats via the one origional script. | ||
| 3 | |||
| 4 | The way to use this library is to start with adding the path to this directory | ||
| 5 | and then include it. | ||
| 6 | |||
| 7 | push(@INC,"perlasm","../../perlasm"); | ||
| 8 | require "x86asm.pl"; | ||
| 9 | |||
| 10 | The first thing we do is setup the file and type of assember | ||
| 11 | |||
| 12 | &asm_init($ARGV[0],$0); | ||
| 13 | |||
| 14 | The first argument is the 'type'. Currently | ||
| 15 | 'cpp', 'sol', 'a.out', 'elf' or 'win32'. | ||
| 16 | Argument 2 is the file name. | ||
| 17 | |||
| 18 | The reciprocal function is | ||
| 19 | &asm_finish() which should be called at the end. | ||
| 20 | |||
| 21 | There are 2 main 'packages'. x86ms.pl, which is the microsoft assembler, | ||
| 22 | and x86unix.pl which is the unix (gas) version. | ||
| 23 | |||
| 24 | Functions of interest are: | ||
| 25 | &external_label("des_SPtrans"); declare and external variable | ||
| 26 | &LB(reg); Low byte for a register | ||
| 27 | &HB(reg); High byte for a register | ||
| 28 | &BP(off,base,index,scale) Byte pointer addressing | ||
| 29 | &DWP(off,base,index,scale) Word pointer addressing | ||
| 30 | &stack_push(num) Basically a 'sub esp, num*4' with extra | ||
| 31 | &stack_pop(num) inverse of stack_push | ||
| 32 | &function_begin(name,extra) Start a function with pushing of | ||
| 33 | edi, esi, ebx and ebp. extra is extra win32 | ||
| 34 | external info that may be required. | ||
| 35 | &function_begin_B(name,extra) Same as norma function_begin but no pushing. | ||
| 36 | &function_end(name) Call at end of function. | ||
| 37 | &function_end_A(name) Standard pop and ret, for use inside functions | ||
| 38 | &function_end_B(name) Call at end but with poping or 'ret'. | ||
| 39 | &swtmp(num) Address on stack temp word. | ||
| 40 | &wparam(num) Parameter number num, that was push | ||
| 41 | in C convention. This all works over pushes | ||
| 42 | and pops. | ||
| 43 | &comment("hello there") Put in a comment. | ||
| 44 | &label("loop") Refer to a label, normally a jmp target. | ||
| 45 | &set_label("loop") Set a label at this point. | ||
| 46 | &data_word(word) Put in a word of data. | ||
| 47 | |||
| 48 | So how does this all hold together? Given | ||
| 49 | |||
| 50 | int calc(int len, int *data) | ||
| 51 | { | ||
| 52 | int i,j=0; | ||
| 53 | |||
| 54 | for (i=0; i<len; i++) | ||
| 55 | { | ||
| 56 | j+=other(data[i]); | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | So a very simple version of this function could be coded as | ||
| 61 | |||
| 62 | push(@INC,"perlasm","../../perlasm"); | ||
| 63 | require "x86asm.pl"; | ||
| 64 | |||
| 65 | &asm_init($ARGV[0],"cacl.pl"); | ||
| 66 | |||
| 67 | &external_label("other"); | ||
| 68 | |||
| 69 | $tmp1= "eax"; | ||
| 70 | $j= "edi"; | ||
| 71 | $data= "esi"; | ||
| 72 | $i= "ebp"; | ||
| 73 | |||
| 74 | &comment("a simple function"); | ||
| 75 | &function_begin("calc"); | ||
| 76 | &mov( $data, &wparam(1)); # data | ||
| 77 | &xor( $j, $j); | ||
| 78 | &xor( $i, $i); | ||
| 79 | |||
| 80 | &set_label("loop"); | ||
| 81 | &cmp( $i, &wparam(0)); | ||
| 82 | &jge( &label("end")); | ||
| 83 | |||
| 84 | &mov( $tmp1, &DWP(0,$data,$i,4)); | ||
| 85 | &push( $tmp1); | ||
| 86 | &call( "other"); | ||
| 87 | &add( $j, "eax"); | ||
| 88 | &pop( $tmp1); | ||
| 89 | &inc( $i); | ||
| 90 | &jmp( &label("loop")); | ||
| 91 | |||
| 92 | &set_label("end"); | ||
| 93 | &mov( "eax", $j); | ||
| 94 | |||
| 95 | &function_end("calc"); | ||
| 96 | |||
| 97 | &asm_finish(); | ||
| 98 | |||
| 99 | The above example is very very unoptimised but gives an idea of how | ||
| 100 | things work. | ||
| 101 | |||
| 102 | There is also a cbc mode function generator in cbc.pl | ||
| 103 | |||
| 104 | &cbc( $name, | ||
| 105 | $encrypt_function_name, | ||
| 106 | $decrypt_function_name, | ||
| 107 | $true_if_byte_swap_needed, | ||
| 108 | $parameter_number_for_iv, | ||
| 109 | $parameter_number_for_encrypt_flag, | ||
| 110 | $first_parameter_to_pass, | ||
| 111 | $second_parameter_to_pass, | ||
| 112 | $third_parameter_to_pass); | ||
| 113 | |||
| 114 | So for example, given | ||
| 115 | void BF_encrypt(BF_LONG *data,BF_KEY *key); | ||
| 116 | void BF_decrypt(BF_LONG *data,BF_KEY *key); | ||
| 117 | void BF_cbc_encrypt(unsigned char *in, unsigned char *out, long length, | ||
| 118 | BF_KEY *ks, unsigned char *iv, int enc); | ||
| 119 | |||
| 120 | &cbc("BF_cbc_encrypt","BF_encrypt","BF_encrypt",1,4,5,3,-1,-1); | ||
| 121 | |||
| 122 | &cbc("des_ncbc_encrypt","des_encrypt","des_encrypt",0,4,5,3,5,-1); | ||
| 123 | &cbc("des_ede3_cbc_encrypt","des_encrypt3","des_decrypt3",0,6,7,3,4,5); | ||
| 124 | |||
diff --git a/src/lib/libcrypto/perlasm/x86_64-xlate.pl b/src/lib/libcrypto/perlasm/x86_64-xlate.pl new file mode 100755 index 0000000000..74153b017d --- /dev/null +++ b/src/lib/libcrypto/perlasm/x86_64-xlate.pl | |||
| @@ -0,0 +1,557 @@ | |||
| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | # Ascetic x86_64 AT&T to MASM assembler translator by <appro>. | ||
| 4 | # | ||
| 5 | # Why AT&T to MASM and not vice versa? Several reasons. Because AT&T | ||
| 6 | # format is way easier to parse. Because it's simpler to "gear" from | ||
| 7 | # Unix ABI to Windows one [see cross-reference "card" at the end of | ||
| 8 | # file]. Because Linux targets were available first... | ||
| 9 | # | ||
| 10 | # In addition the script also "distills" code suitable for GNU | ||
| 11 | # assembler, so that it can be compiled with more rigid assemblers, | ||
| 12 | # such as Solaris /usr/ccs/bin/as. | ||
| 13 | # | ||
| 14 | # This translator is not designed to convert *arbitrary* assembler | ||
| 15 | # code from AT&T format to MASM one. It's designed to convert just | ||
| 16 | # enough to provide for dual-ABI OpenSSL modules development... | ||
| 17 | # There *are* limitations and you might have to modify your assembler | ||
| 18 | # code or this script to achieve the desired result... | ||
| 19 | # | ||
| 20 | # Currently recognized limitations: | ||
| 21 | # | ||
| 22 | # - can't use multiple ops per line; | ||
| 23 | # - indirect calls and jumps are not supported; | ||
| 24 | # | ||
| 25 | # Dual-ABI styling rules. | ||
| 26 | # | ||
| 27 | # 1. Adhere to Unix register and stack layout [see the end for | ||
| 28 | # explanation]. | ||
| 29 | # 2. Forget about "red zone," stick to more traditional blended | ||
| 30 | # stack frame allocation. If volatile storage is actually required | ||
| 31 | # that is. If not, just leave the stack as is. | ||
| 32 | # 3. Functions tagged with ".type name,@function" get crafted with | ||
| 33 | # unified Win64 prologue and epilogue automatically. If you want | ||
| 34 | # to take care of ABI differences yourself, tag functions as | ||
| 35 | # ".type name,@abi-omnipotent" instead. | ||
| 36 | # 4. To optimize the Win64 prologue you can specify number of input | ||
| 37 | # arguments as ".type name,@function,N." Keep in mind that if N is | ||
| 38 | # larger than 6, then you *have to* write "abi-omnipotent" code, | ||
| 39 | # because >6 cases can't be addressed with unified prologue. | ||
| 40 | # 5. Name local labels as .L*, do *not* use dynamic labels such as 1: | ||
| 41 | # (sorry about latter). | ||
| 42 | # 6. Don't use [or hand-code with .byte] "rep ret." "ret" mnemonic is | ||
| 43 | # required to identify the spots, where to inject Win64 epilogue! | ||
| 44 | # But on the pros, it's then prefixed with rep automatically:-) | ||
| 45 | # 7. Due to MASM limitations [and certain general counter-intuitivity | ||
| 46 | # of ip-relative addressing] generation of position-independent | ||
| 47 | # code is assisted by synthetic directive, .picmeup, which puts | ||
| 48 | # address of the *next* instruction into target register. | ||
| 49 | # | ||
| 50 | # Example 1: | ||
| 51 | # .picmeup %rax | ||
| 52 | # lea .Label-.(%rax),%rax | ||
| 53 | # Example 2: | ||
| 54 | # .picmeup %rcx | ||
| 55 | # .Lpic_point: | ||
| 56 | # ... | ||
| 57 | # lea .Label-.Lpic_point(%rcx),%rbp | ||
| 58 | |||
| 59 | my $output = shift; | ||
| 60 | |||
| 61 | { my ($stddev,$stdino,@junk)=stat(STDOUT); | ||
| 62 | my ($outdev,$outino,@junk)=stat($output); | ||
| 63 | |||
| 64 | open STDOUT,">$output" || die "can't open $output: $!" | ||
| 65 | if ($stddev!=$outdev || $stdino!=$outino); | ||
| 66 | } | ||
| 67 | |||
| 68 | my $masmref=8 + 50727*2**-32; # 8.00.50727 shipped with VS2005 | ||
| 69 | my $masm=$masmref if ($output =~ /\.asm/); | ||
| 70 | if ($masm && `ml64 2>&1` =~ m/Version ([0-9]+)\.([0-9]+)(\.([0-9]+))?/) | ||
| 71 | { $masm=$1 + $2*2**-16 + $4*2**-32; } | ||
| 72 | |||
| 73 | my $current_segment; | ||
| 74 | my $current_function; | ||
| 75 | |||
| 76 | { package opcode; # pick up opcodes | ||
| 77 | sub re { | ||
| 78 | my $self = shift; # single instance in enough... | ||
| 79 | local *line = shift; | ||
| 80 | undef $ret; | ||
| 81 | |||
| 82 | if ($line =~ /^([a-z][a-z0-9]*)/i) { | ||
| 83 | $self->{op} = $1; | ||
| 84 | $ret = $self; | ||
| 85 | $line = substr($line,@+[0]); $line =~ s/^\s+//; | ||
| 86 | |||
| 87 | undef $self->{sz}; | ||
| 88 | if ($self->{op} =~ /^(movz)b.*/) { # movz is pain... | ||
| 89 | $self->{op} = $1; | ||
| 90 | $self->{sz} = "b"; | ||
| 91 | } elsif ($self->{op} =~ /call/) { | ||
| 92 | $self->{sz} = "" | ||
| 93 | } elsif ($self->{op} =~ /([a-z]{3,})([qlwb])$/) { | ||
| 94 | $self->{op} = $1; | ||
| 95 | $self->{sz} = $2; | ||
| 96 | } | ||
| 97 | } | ||
| 98 | $ret; | ||
| 99 | } | ||
| 100 | sub size { | ||
| 101 | my $self = shift; | ||
| 102 | my $sz = shift; | ||
| 103 | $self->{sz} = $sz if (defined($sz) && !defined($self->{sz})); | ||
| 104 | $self->{sz}; | ||
| 105 | } | ||
| 106 | sub out { | ||
| 107 | my $self = shift; | ||
| 108 | if (!$masm) { | ||
| 109 | if ($self->{op} eq "movz") { # movz is pain... | ||
| 110 | sprintf "%s%s%s",$self->{op},$self->{sz},shift; | ||
| 111 | } elsif ($self->{op} =~ /^set/) { | ||
| 112 | "$self->{op}"; | ||
| 113 | } elsif ($self->{op} eq "ret") { | ||
| 114 | ".byte 0xf3,0xc3"; | ||
| 115 | } else { | ||
| 116 | "$self->{op}$self->{sz}"; | ||
| 117 | } | ||
| 118 | } else { | ||
| 119 | $self->{op} =~ s/^movz/movzx/; | ||
| 120 | if ($self->{op} eq "ret") { | ||
| 121 | $self->{op} = ""; | ||
| 122 | if ($current_function->{abi} eq "svr4") { | ||
| 123 | $self->{op} = "mov rdi,QWORD PTR 8[rsp]\t;WIN64 epilogue\n\t". | ||
| 124 | "mov rsi,QWORD PTR 16[rsp]\n\t"; | ||
| 125 | } | ||
| 126 | $self->{op} .= "DB\t0F3h,0C3h\t\t;repret"; | ||
| 127 | } | ||
| 128 | $self->{op}; | ||
| 129 | } | ||
| 130 | } | ||
| 131 | } | ||
| 132 | { package const; # pick up constants, which start with $ | ||
| 133 | sub re { | ||
| 134 | my $self = shift; # single instance in enough... | ||
| 135 | local *line = shift; | ||
| 136 | undef $ret; | ||
| 137 | |||
| 138 | if ($line =~ /^\$([^,]+)/) { | ||
| 139 | $self->{value} = $1; | ||
| 140 | $ret = $self; | ||
| 141 | $line = substr($line,@+[0]); $line =~ s/^\s+//; | ||
| 142 | } | ||
| 143 | $ret; | ||
| 144 | } | ||
| 145 | sub out { | ||
| 146 | my $self = shift; | ||
| 147 | |||
| 148 | if (!$masm) { | ||
| 149 | # Solaris /usr/ccs/bin/as can't handle multiplications | ||
| 150 | # in $self->{value} | ||
| 151 | $self->{value} =~ s/(?<![0-9a-f])(0[x0-9a-f]+)/oct($1)/egi; | ||
| 152 | $self->{value} =~ s/([0-9]+\s*[\*\/\%]\s*[0-9]+)/eval($1)/eg; | ||
| 153 | sprintf "\$%s",$self->{value}; | ||
| 154 | } else { | ||
| 155 | $self->{value} =~ s/0x([0-9a-f]+)/0$1h/ig; | ||
| 156 | sprintf "%s",$self->{value}; | ||
| 157 | } | ||
| 158 | } | ||
| 159 | } | ||
| 160 | { package ea; # pick up effective addresses: expr(%reg,%reg,scale) | ||
| 161 | sub re { | ||
| 162 | my $self = shift; # single instance in enough... | ||
| 163 | local *line = shift; | ||
| 164 | undef $ret; | ||
| 165 | |||
| 166 | if ($line =~ /^([^\(,]*)\(([%\w,]+)\)/ && | ||
| 167 | !($line =~ /^PIC_(GOT|PLT)/)) { | ||
| 168 | $self->{label} = $1; | ||
| 169 | ($self->{base},$self->{index},$self->{scale})=split(/,/,$2); | ||
| 170 | $self->{scale} = 1 if (!defined($self->{scale})); | ||
| 171 | $ret = $self; | ||
| 172 | $line = substr($line,@+[0]); $line =~ s/^\s+//; | ||
| 173 | |||
| 174 | $self->{base} =~ s/^%//; | ||
| 175 | $self->{index} =~ s/^%// if (defined($self->{index})); | ||
| 176 | } | ||
| 177 | $ret; | ||
| 178 | } | ||
| 179 | sub size {} | ||
| 180 | sub out { | ||
| 181 | my $self = shift; | ||
| 182 | my $sz = shift; | ||
| 183 | |||
| 184 | # Silently convert all EAs to 64-bit. This is required for | ||
| 185 | # elder GNU assembler and results in more compact code, | ||
| 186 | # *but* most importantly AES module depends on this feature! | ||
| 187 | $self->{index} =~ s/^[er](.?[0-9xpi])[d]?$/r\1/; | ||
| 188 | $self->{base} =~ s/^[er](.?[0-9xpi])[d]?$/r\1/; | ||
| 189 | |||
| 190 | if (!$masm) { | ||
| 191 | # Solaris /usr/ccs/bin/as can't handle multiplications | ||
| 192 | # in $self->{label} | ||
| 193 | $self->{label} =~ s/(?<![0-9a-f])(0[x0-9a-f]+)/oct($1)/egi; | ||
| 194 | $self->{label} =~ s/([0-9]+\s*[\*\/\%]\s*[0-9]+)/eval($1)/eg; | ||
| 195 | |||
| 196 | if (defined($self->{index})) { | ||
| 197 | sprintf "%s(%%%s,%%%s,%d)", | ||
| 198 | $self->{label},$self->{base}, | ||
| 199 | $self->{index},$self->{scale}; | ||
| 200 | } else { | ||
| 201 | sprintf "%s(%%%s)", $self->{label},$self->{base}; | ||
| 202 | } | ||
| 203 | } else { | ||
| 204 | %szmap = ( b=>"BYTE", w=>"WORD", l=>"DWORD", q=>"QWORD" ); | ||
| 205 | |||
| 206 | $self->{label} =~ s/\./\$/g; | ||
| 207 | $self->{label} =~ s/0x([0-9a-f]+)/0$1h/ig; | ||
| 208 | $self->{label} = "($self->{label})" if ($self->{label} =~ /[\*\+\-\/]/); | ||
| 209 | |||
| 210 | if (defined($self->{index})) { | ||
| 211 | sprintf "%s PTR %s[%s*%d+%s]",$szmap{$sz}, | ||
| 212 | $self->{label}, | ||
| 213 | $self->{index},$self->{scale}, | ||
| 214 | $self->{base}; | ||
| 215 | } elsif ($self->{base} eq "rip") { | ||
| 216 | sprintf "%s PTR %s",$szmap{$sz},$self->{label}; | ||
| 217 | } else { | ||
| 218 | sprintf "%s PTR %s[%s]",$szmap{$sz}, | ||
| 219 | $self->{label},$self->{base}; | ||
| 220 | } | ||
| 221 | } | ||
| 222 | } | ||
| 223 | } | ||
| 224 | { package register; # pick up registers, which start with %. | ||
| 225 | sub re { | ||
| 226 | my $class = shift; # muliple instances... | ||
| 227 | my $self = {}; | ||
| 228 | local *line = shift; | ||
| 229 | undef $ret; | ||
| 230 | |||
| 231 | if ($line =~ /^%(\w+)/) { | ||
| 232 | bless $self,$class; | ||
| 233 | $self->{value} = $1; | ||
| 234 | $ret = $self; | ||
| 235 | $line = substr($line,@+[0]); $line =~ s/^\s+//; | ||
| 236 | } | ||
| 237 | $ret; | ||
| 238 | } | ||
| 239 | sub size { | ||
| 240 | my $self = shift; | ||
| 241 | undef $ret; | ||
| 242 | |||
| 243 | if ($self->{value} =~ /^r[\d]+b$/i) { $ret="b"; } | ||
| 244 | elsif ($self->{value} =~ /^r[\d]+w$/i) { $ret="w"; } | ||
| 245 | elsif ($self->{value} =~ /^r[\d]+d$/i) { $ret="l"; } | ||
| 246 | elsif ($self->{value} =~ /^r[\w]+$/i) { $ret="q"; } | ||
| 247 | elsif ($self->{value} =~ /^[a-d][hl]$/i){ $ret="b"; } | ||
| 248 | elsif ($self->{value} =~ /^[\w]{2}l$/i) { $ret="b"; } | ||
| 249 | elsif ($self->{value} =~ /^[\w]{2}$/i) { $ret="w"; } | ||
| 250 | elsif ($self->{value} =~ /^e[a-z]{2}$/i){ $ret="l"; } | ||
| 251 | |||
| 252 | $ret; | ||
| 253 | } | ||
| 254 | sub out { | ||
| 255 | my $self = shift; | ||
| 256 | sprintf $masm?"%s":"%%%s",$self->{value}; | ||
| 257 | } | ||
| 258 | } | ||
| 259 | { package label; # pick up labels, which end with : | ||
| 260 | sub re { | ||
| 261 | my $self = shift; # single instance is enough... | ||
| 262 | local *line = shift; | ||
| 263 | undef $ret; | ||
| 264 | |||
| 265 | if ($line =~ /(^[\.\w]+\:)/) { | ||
| 266 | $self->{value} = $1; | ||
| 267 | $ret = $self; | ||
| 268 | $line = substr($line,@+[0]); $line =~ s/^\s+//; | ||
| 269 | |||
| 270 | $self->{value} =~ s/\.L/\$L/ if ($masm); | ||
| 271 | } | ||
| 272 | $ret; | ||
| 273 | } | ||
| 274 | sub out { | ||
| 275 | my $self = shift; | ||
| 276 | |||
| 277 | if (!$masm) { | ||
| 278 | $self->{value}; | ||
| 279 | } elsif ($self->{value} ne "$current_function->{name}:") { | ||
| 280 | $self->{value}; | ||
| 281 | } elsif ($current_function->{abi} eq "svr4") { | ||
| 282 | my $func = "$current_function->{name} PROC\n". | ||
| 283 | " mov QWORD PTR 8[rsp],rdi\t;WIN64 prologue\n". | ||
| 284 | " mov QWORD PTR 16[rsp],rsi\n"; | ||
| 285 | my $narg = $current_function->{narg}; | ||
| 286 | $narg=6 if (!defined($narg)); | ||
| 287 | $func .= " mov rdi,rcx\n" if ($narg>0); | ||
| 288 | $func .= " mov rsi,rdx\n" if ($narg>1); | ||
| 289 | $func .= " mov rdx,r8\n" if ($narg>2); | ||
| 290 | $func .= " mov rcx,r9\n" if ($narg>3); | ||
| 291 | $func .= " mov r8,QWORD PTR 40[rsp]\n" if ($narg>4); | ||
| 292 | $func .= " mov r9,QWORD PTR 48[rsp]\n" if ($narg>5); | ||
| 293 | $func .= "\n"; | ||
| 294 | } else { | ||
| 295 | "$current_function->{name} PROC"; | ||
| 296 | } | ||
| 297 | } | ||
| 298 | } | ||
| 299 | { package expr; # pick up expressioins | ||
| 300 | sub re { | ||
| 301 | my $self = shift; # single instance is enough... | ||
| 302 | local *line = shift; | ||
| 303 | undef $ret; | ||
| 304 | |||
| 305 | if ($line =~ /(^[^,]+)/) { | ||
| 306 | $self->{value} = $1; | ||
| 307 | $ret = $self; | ||
| 308 | $line = substr($line,@+[0]); $line =~ s/^\s+//; | ||
| 309 | |||
| 310 | $self->{value} =~ s/\.L/\$L/g if ($masm); | ||
| 311 | } | ||
| 312 | $ret; | ||
| 313 | } | ||
| 314 | sub out { | ||
| 315 | my $self = shift; | ||
| 316 | $self->{value}; | ||
| 317 | } | ||
| 318 | } | ||
| 319 | { package directive; # pick up directives, which start with . | ||
| 320 | sub re { | ||
| 321 | my $self = shift; # single instance is enough... | ||
| 322 | local *line = shift; | ||
| 323 | undef $ret; | ||
| 324 | my $dir; | ||
| 325 | my %opcode = # lea 2f-1f(%rip),%dst; 1: nop; 2: | ||
| 326 | ( "%rax"=>0x01058d48, "%rcx"=>0x010d8d48, | ||
| 327 | "%rdx"=>0x01158d48, "%rbx"=>0x011d8d48, | ||
| 328 | "%rsp"=>0x01258d48, "%rbp"=>0x012d8d48, | ||
| 329 | "%rsi"=>0x01358d48, "%rdi"=>0x013d8d48, | ||
| 330 | "%r8" =>0x01058d4c, "%r9" =>0x010d8d4c, | ||
| 331 | "%r10"=>0x01158d4c, "%r11"=>0x011d8d4c, | ||
| 332 | "%r12"=>0x01258d4c, "%r13"=>0x012d8d4c, | ||
| 333 | "%r14"=>0x01358d4c, "%r15"=>0x013d8d4c ); | ||
| 334 | |||
| 335 | if ($line =~ /^\s*(\.\w+)/) { | ||
| 336 | if (!$masm) { | ||
| 337 | $self->{value} = $1; | ||
| 338 | $line =~ s/\@abi\-omnipotent/\@function/; | ||
| 339 | $line =~ s/\@function.*/\@function/; | ||
| 340 | if ($line =~ /\.picmeup\s+(%r[\w]+)/i) { | ||
| 341 | $self->{value} = sprintf "\t.long\t0x%x,0x90000000",$opcode{$1}; | ||
| 342 | } elsif ($line =~ /\.asciz\s+"(.*)"$/) { | ||
| 343 | $self->{value} = ".byte\t".join(",",unpack("C*",$1),0); | ||
| 344 | } elsif ($line =~ /\.extern/) { | ||
| 345 | $self->{value} = ""; # swallow extern | ||
| 346 | } else { | ||
| 347 | $self->{value} = $line; | ||
| 348 | } | ||
| 349 | $line = ""; | ||
| 350 | return $self; | ||
| 351 | } | ||
| 352 | |||
| 353 | $dir = $1; | ||
| 354 | $ret = $self; | ||
| 355 | undef $self->{value}; | ||
| 356 | $line = substr($line,@+[0]); $line =~ s/^\s+//; | ||
| 357 | SWITCH: for ($dir) { | ||
| 358 | /\.(text)/ | ||
| 359 | && do { my $v=undef; | ||
| 360 | $v="$current_segment\tENDS\n" if ($current_segment); | ||
| 361 | $current_segment = "_$1\$"; | ||
| 362 | $current_segment =~ tr/[a-z]/[A-Z]/; | ||
| 363 | $v.="$current_segment\tSEGMENT "; | ||
| 364 | $v.=$masm>=$masmref ? "ALIGN(64)" : "PAGE"; | ||
| 365 | $v.=" 'CODE'"; | ||
| 366 | $self->{value} = $v; | ||
| 367 | last; | ||
| 368 | }; | ||
| 369 | /\.extern/ && do { $self->{value} = "EXTRN\t".$line.":BYTE"; last; }; | ||
| 370 | /\.globl/ && do { $self->{value} = "PUBLIC\t".$line; last; }; | ||
| 371 | /\.type/ && do { ($sym,$type,$narg) = split(',',$line); | ||
| 372 | if ($type eq "\@function") { | ||
| 373 | undef $current_function; | ||
| 374 | $current_function->{name} = $sym; | ||
| 375 | $current_function->{abi} = "svr4"; | ||
| 376 | $current_function->{narg} = $narg; | ||
| 377 | } elsif ($type eq "\@abi-omnipotent") { | ||
| 378 | undef $current_function; | ||
| 379 | $current_function->{name} = $sym; | ||
| 380 | } | ||
| 381 | last; | ||
| 382 | }; | ||
| 383 | /\.size/ && do { if (defined($current_function)) { | ||
| 384 | $self->{value}="$current_function->{name}\tENDP"; | ||
| 385 | undef $current_function; | ||
| 386 | } | ||
| 387 | last; | ||
| 388 | }; | ||
| 389 | /\.align/ && do { $self->{value} = "ALIGN\t".$line; last; }; | ||
| 390 | /\.(byte|value|long|quad)/ | ||
| 391 | && do { my @arr = split(',',$line); | ||
| 392 | my $sz = substr($1,0,1); | ||
| 393 | my $last = pop(@arr); | ||
| 394 | my $conv = sub { my $var=shift; | ||
| 395 | if ($var=~s/0x([0-9a-f]+)/0$1h/i) { $var; } | ||
| 396 | else { sprintf"0%Xh",$var; } | ||
| 397 | }; | ||
| 398 | |||
| 399 | $sz =~ tr/bvlq/BWDQ/; | ||
| 400 | $self->{value} = "\tD$sz\t"; | ||
| 401 | for (@arr) { $self->{value} .= &$conv($_).","; } | ||
| 402 | $self->{value} .= &$conv($last); | ||
| 403 | last; | ||
| 404 | }; | ||
| 405 | /\.picmeup/ && do { $self->{value} = sprintf"\tDD\t 0%Xh,090000000h",$opcode{$line}; | ||
| 406 | last; | ||
| 407 | }; | ||
| 408 | /\.asciz/ && do { if ($line =~ /^"(.*)"$/) { | ||
| 409 | my @str=unpack("C*",$1); | ||
| 410 | push @str,0; | ||
| 411 | while ($#str>15) { | ||
| 412 | $self->{value}.="DB\t" | ||
| 413 | .join(",",@str[0..15])."\n"; | ||
| 414 | foreach (0..15) { shift @str; } | ||
| 415 | } | ||
| 416 | $self->{value}.="DB\t" | ||
| 417 | .join(",",@str) if (@str); | ||
| 418 | } | ||
| 419 | last; | ||
| 420 | }; | ||
| 421 | } | ||
| 422 | $line = ""; | ||
| 423 | } | ||
| 424 | |||
| 425 | $ret; | ||
| 426 | } | ||
| 427 | sub out { | ||
| 428 | my $self = shift; | ||
| 429 | $self->{value}; | ||
| 430 | } | ||
| 431 | } | ||
| 432 | |||
| 433 | print "#include <machine/asm.h>\n"; | ||
| 434 | |||
| 435 | while($line=<>) { | ||
| 436 | |||
| 437 | chomp($line); | ||
| 438 | |||
| 439 | $line =~ s|[#!].*$||; # get rid of asm-style comments... | ||
| 440 | $line =~ s|/\*.*\*/||; # ... and C-style comments... | ||
| 441 | $line =~ s|^\s+||; # ... and skip white spaces in beginning | ||
| 442 | |||
| 443 | undef $label; | ||
| 444 | undef $opcode; | ||
| 445 | undef $dst; | ||
| 446 | undef $src; | ||
| 447 | undef $sz; | ||
| 448 | |||
| 449 | if ($label=label->re(\$line)) { print $label->out(); } | ||
| 450 | |||
| 451 | if (directive->re(\$line)) { | ||
| 452 | printf "%s",directive->out(); | ||
| 453 | } elsif ($opcode=opcode->re(\$line)) { ARGUMENT: { | ||
| 454 | |||
| 455 | if ($src=register->re(\$line)) { opcode->size($src->size()); } | ||
| 456 | elsif ($src=const->re(\$line)) { } | ||
| 457 | elsif ($src=ea->re(\$line)) { } | ||
| 458 | elsif ($src=expr->re(\$line)) { } | ||
| 459 | |||
| 460 | last ARGUMENT if ($line !~ /^,/); | ||
| 461 | |||
| 462 | $line = substr($line,1); $line =~ s/^\s+//; | ||
| 463 | |||
| 464 | if ($dst=register->re(\$line)) { opcode->size($dst->size()); } | ||
| 465 | elsif ($dst=const->re(\$line)) { } | ||
| 466 | elsif ($dst=ea->re(\$line)) { } | ||
| 467 | |||
| 468 | } # ARGUMENT: | ||
| 469 | |||
| 470 | $sz=opcode->size(); | ||
| 471 | |||
| 472 | if (defined($dst)) { | ||
| 473 | if (!$masm) { | ||
| 474 | printf "\t%s\t%s,%s", $opcode->out($dst->size()), | ||
| 475 | $src->out($sz),$dst->out($sz); | ||
| 476 | } else { | ||
| 477 | printf "\t%s\t%s,%s", $opcode->out(), | ||
| 478 | $dst->out($sz),$src->out($sz); | ||
| 479 | } | ||
| 480 | } elsif (defined($src)) { | ||
| 481 | printf "\t%s\t%s",$opcode->out(),$src->out($sz); | ||
| 482 | } else { | ||
| 483 | printf "\t%s",$opcode->out(); | ||
| 484 | } | ||
| 485 | } | ||
| 486 | |||
| 487 | print $line,"\n"; | ||
| 488 | } | ||
| 489 | |||
| 490 | print "\n$current_segment\tENDS\nEND\n" if ($masm); | ||
| 491 | |||
| 492 | close STDOUT; | ||
| 493 | |||
| 494 | ################################################# | ||
| 495 | # Cross-reference x86_64 ABI "card" | ||
| 496 | # | ||
| 497 | # Unix Win64 | ||
| 498 | # %rax * * | ||
| 499 | # %rbx - - | ||
| 500 | # %rcx #4 #1 | ||
| 501 | # %rdx #3 #2 | ||
| 502 | # %rsi #2 - | ||
| 503 | # %rdi #1 - | ||
| 504 | # %rbp - - | ||
| 505 | # %rsp - - | ||
| 506 | # %r8 #5 #3 | ||
| 507 | # %r9 #6 #4 | ||
| 508 | # %r10 * * | ||
| 509 | # %r11 * * | ||
| 510 | # %r12 - - | ||
| 511 | # %r13 - - | ||
| 512 | # %r14 - - | ||
| 513 | # %r15 - - | ||
| 514 | # | ||
| 515 | # (*) volatile register | ||
| 516 | # (-) preserved by callee | ||
| 517 | # (#) Nth argument, volatile | ||
| 518 | # | ||
| 519 | # In Unix terms top of stack is argument transfer area for arguments | ||
| 520 | # which could not be accomodated in registers. Or in other words 7th | ||
| 521 | # [integer] argument resides at 8(%rsp) upon function entry point. | ||
| 522 | # 128 bytes above %rsp constitute a "red zone" which is not touched | ||
| 523 | # by signal handlers and can be used as temporal storage without | ||
| 524 | # allocating a frame. | ||
| 525 | # | ||
| 526 | # In Win64 terms N*8 bytes on top of stack is argument transfer area, | ||
| 527 | # which belongs to/can be overwritten by callee. N is the number of | ||
| 528 | # arguments passed to callee, *but* not less than 4! This means that | ||
| 529 | # upon function entry point 5th argument resides at 40(%rsp), as well | ||
| 530 | # as that 32 bytes from 8(%rsp) can always be used as temporal | ||
| 531 | # storage [without allocating a frame]. One can actually argue that | ||
| 532 | # one can assume a "red zone" above stack pointer under Win64 as well. | ||
| 533 | # Point is that at apparently no occasion Windows kernel would alter | ||
| 534 | # the area above user stack pointer in true asynchronous manner... | ||
| 535 | # | ||
| 536 | # All the above means that if assembler programmer adheres to Unix | ||
| 537 | # register and stack layout, but disregards the "red zone" existense, | ||
| 538 | # it's possible to use following prologue and epilogue to "gear" from | ||
| 539 | # Unix to Win64 ABI in leaf functions with not more than 6 arguments. | ||
| 540 | # | ||
| 541 | # omnipotent_function: | ||
| 542 | # ifdef WIN64 | ||
| 543 | # movq %rdi,8(%rsp) | ||
| 544 | # movq %rsi,16(%rsp) | ||
| 545 | # movq %rcx,%rdi ; if 1st argument is actually present | ||
| 546 | # movq %rdx,%rsi ; if 2nd argument is actually ... | ||
| 547 | # movq %r8,%rdx ; if 3rd argument is ... | ||
| 548 | # movq %r9,%rcx ; if 4th argument ... | ||
| 549 | # movq 40(%rsp),%r8 ; if 5th ... | ||
| 550 | # movq 48(%rsp),%r9 ; if 6th ... | ||
| 551 | # endif | ||
| 552 | # ... | ||
| 553 | # ifdef WIN64 | ||
| 554 | # movq 8(%rsp),%rdi | ||
| 555 | # movq 16(%rsp),%rsi | ||
| 556 | # endif | ||
| 557 | # ret | ||
diff --git a/src/lib/libcrypto/perlasm/x86asm.pl b/src/lib/libcrypto/perlasm/x86asm.pl new file mode 100644 index 0000000000..f535c9c7fa --- /dev/null +++ b/src/lib/libcrypto/perlasm/x86asm.pl | |||
| @@ -0,0 +1,136 @@ | |||
| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | # require 'x86asm.pl'; | ||
| 4 | # &asm_init("cpp","des-586.pl"); | ||
| 5 | # XXX | ||
| 6 | # XXX | ||
| 7 | # main'asm_finish | ||
| 8 | |||
| 9 | sub main'asm_finish | ||
| 10 | { | ||
| 11 | &file_end(); | ||
| 12 | &asm_finish_cpp() if $cpp; | ||
| 13 | print &asm_get_output(); | ||
| 14 | } | ||
| 15 | |||
| 16 | sub main'asm_init | ||
| 17 | { | ||
| 18 | ($type,$fn,$i386)=@_; | ||
| 19 | $filename=$fn; | ||
| 20 | |||
| 21 | $elf=$cpp=$coff=$aout=$win32=$netware=$mwerks=$openbsd=0; | ||
| 22 | if ( ($type eq "elf")) | ||
| 23 | { $elf=1; require "x86unix.pl"; } | ||
| 24 | elsif ( ($type eq "openbsd-elf")) | ||
| 25 | { $openbsd=$elf=1; require "x86unix.pl"; } | ||
| 26 | elsif ( ($type eq "openbsd-a.out")) | ||
| 27 | { $openbsd=1; require "x86unix.pl"; } | ||
| 28 | elsif ( ($type eq "a.out")) | ||
| 29 | { $aout=1; require "x86unix.pl"; } | ||
| 30 | elsif ( ($type eq "coff" or $type eq "gaswin")) | ||
| 31 | { $coff=1; require "x86unix.pl"; } | ||
| 32 | elsif ( ($type eq "cpp")) | ||
| 33 | { $cpp=1; require "x86unix.pl"; } | ||
| 34 | elsif ( ($type eq "win32")) | ||
| 35 | { $win32=1; require "x86ms.pl"; } | ||
| 36 | elsif ( ($type eq "win32n")) | ||
| 37 | { $win32=1; require "x86nasm.pl"; } | ||
| 38 | elsif ( ($type eq "nw-nasm")) | ||
| 39 | { $netware=1; require "x86nasm.pl"; } | ||
| 40 | elsif ( ($type eq "nw-mwasm")) | ||
| 41 | { $netware=1; $mwerks=1; require "x86nasm.pl"; } | ||
| 42 | else | ||
| 43 | { | ||
| 44 | print STDERR <<"EOF"; | ||
| 45 | Pick one target type from | ||
| 46 | elf - Linux, FreeBSD, Solaris x86, etc. | ||
| 47 | a.out - OpenBSD, DJGPP, etc. | ||
| 48 | coff - GAS/COFF such as Win32 targets | ||
| 49 | win32 - Windows 95/Windows NT | ||
| 50 | win32n - Windows 95/Windows NT NASM format | ||
| 51 | openbsd-elf - OpenBSD elf | ||
| 52 | openbsd-a.out - OpenBSD a.out | ||
| 53 | nw-nasm - NetWare NASM format | ||
| 54 | nw-mwasm- NetWare Metrowerks Assembler | ||
| 55 | EOF | ||
| 56 | exit(1); | ||
| 57 | } | ||
| 58 | |||
| 59 | $pic=0; | ||
| 60 | for (@ARGV) { $pic=1 if (/\-[fK]PIC/i); } | ||
| 61 | |||
| 62 | &asm_init_output(); | ||
| 63 | |||
| 64 | &comment("Don't even think of reading this code"); | ||
| 65 | &comment("It was automatically generated by $filename"); | ||
| 66 | &comment("Which is a perl program used to generate the x86 assember for"); | ||
| 67 | &comment("any of ELF, a.out, COFF, Win32, ..."); | ||
| 68 | &comment("eric <eay\@cryptsoft.com>"); | ||
| 69 | &comment(""); | ||
| 70 | |||
| 71 | $filename =~ s/\.pl$//; | ||
| 72 | &file($filename); | ||
| 73 | } | ||
| 74 | |||
| 75 | sub asm_finish_cpp | ||
| 76 | { | ||
| 77 | return unless $cpp; | ||
| 78 | |||
| 79 | local($tmp,$i); | ||
| 80 | foreach $i (&get_labels()) | ||
| 81 | { | ||
| 82 | $tmp.="#define $i _$i\n"; | ||
| 83 | } | ||
| 84 | print <<"EOF"; | ||
| 85 | /* Run the C pre-processor over this file with one of the following defined | ||
| 86 | * ELF - elf object files, | ||
| 87 | * OUT - a.out object files, | ||
| 88 | * BSDI - BSDI style a.out object files | ||
| 89 | * SOL - Solaris style elf | ||
| 90 | */ | ||
| 91 | |||
| 92 | #define TYPE(a,b) .type a,b | ||
| 93 | #define SIZE(a,b) .size a,b | ||
| 94 | |||
| 95 | #if defined(OUT) || (defined(BSDI) && !defined(ELF)) | ||
| 96 | $tmp | ||
| 97 | #endif | ||
| 98 | |||
| 99 | #ifdef OUT | ||
| 100 | #define OK 1 | ||
| 101 | #define ALIGN 4 | ||
| 102 | #if defined(__CYGWIN__) || defined(__DJGPP__) || (__MINGW32__) | ||
| 103 | #undef SIZE | ||
| 104 | #undef TYPE | ||
| 105 | #define SIZE(a,b) | ||
| 106 | #define TYPE(a,b) .def a; .scl 2; .type 32; .endef | ||
| 107 | #endif /* __CYGWIN || __DJGPP */ | ||
| 108 | #endif | ||
| 109 | |||
| 110 | #if defined(BSDI) && !defined(ELF) | ||
| 111 | #define OK 1 | ||
| 112 | #define ALIGN 4 | ||
| 113 | #undef SIZE | ||
| 114 | #undef TYPE | ||
| 115 | #define SIZE(a,b) | ||
| 116 | #define TYPE(a,b) | ||
| 117 | #endif | ||
| 118 | |||
| 119 | #if defined(ELF) || defined(SOL) | ||
| 120 | #define OK 1 | ||
| 121 | #define ALIGN 16 | ||
| 122 | #endif | ||
| 123 | |||
| 124 | #ifndef OK | ||
| 125 | You need to define one of | ||
| 126 | ELF - elf systems - linux-elf, NetBSD and DG-UX | ||
| 127 | OUT - a.out systems - linux-a.out and FreeBSD | ||
| 128 | SOL - solaris systems, which are elf with strange comment lines | ||
| 129 | BSDI - a.out with a very primative version of as. | ||
| 130 | #endif | ||
| 131 | |||
| 132 | /* Let the Assembler begin :-) */ | ||
| 133 | EOF | ||
| 134 | } | ||
| 135 | |||
| 136 | 1; | ||
diff --git a/src/lib/libcrypto/perlasm/x86unix.pl b/src/lib/libcrypto/perlasm/x86unix.pl index a4c947165e..ae8f0964dc 100644 --- a/src/lib/libcrypto/perlasm/x86unix.pl +++ b/src/lib/libcrypto/perlasm/x86unix.pl | |||
| @@ -16,6 +16,12 @@ sub main'asm_get_output { return(@out); } | |||
| 16 | sub main'get_labels { return(@labels); } | 16 | sub main'get_labels { return(@labels); } |
| 17 | sub main'external_label { push(@labels,@_); } | 17 | sub main'external_label { push(@labels,@_); } |
| 18 | 18 | ||
| 19 | if ($main'openbsd) | ||
| 20 | { | ||
| 21 | $com_start='/*'; | ||
| 22 | $com_end='*/'; | ||
| 23 | } | ||
| 24 | |||
| 19 | if ($main'cpp) | 25 | if ($main'cpp) |
| 20 | { | 26 | { |
| 21 | $align="ALIGN"; | 27 | $align="ALIGN"; |
| @@ -338,6 +344,9 @@ sub main'file | |||
| 338 | { | 344 | { |
| 339 | local($file)=@_; | 345 | local($file)=@_; |
| 340 | 346 | ||
| 347 | if ($main'openbsd) | ||
| 348 | { push(@out,"#include <machine/asm.h>\n"); } | ||
| 349 | |||
| 341 | local($tmp)=<<"EOF"; | 350 | local($tmp)=<<"EOF"; |
| 342 | .file "$file.s" | 351 | .file "$file.s" |
| 343 | EOF | 352 | EOF |
| @@ -346,11 +355,18 @@ EOF | |||
| 346 | 355 | ||
| 347 | sub main'function_begin | 356 | sub main'function_begin |
| 348 | { | 357 | { |
| 349 | local($func)=@_; | 358 | local($func,$junk,$llabel)=@_; |
| 350 | 359 | ||
| 351 | &main'external_label($func); | 360 | &main'external_label($func); |
| 352 | $func=$under.$func; | 361 | $func=$under.$func; |
| 353 | 362 | ||
| 363 | if ($main'openbsd) | ||
| 364 | { | ||
| 365 | push (@out, "\nENTRY($func)\n"); | ||
| 366 | push (@out, "$llabel:\n") if $llabel; | ||
| 367 | goto skip; | ||
| 368 | } | ||
| 369 | |||
| 354 | local($tmp)=<<"EOF"; | 370 | local($tmp)=<<"EOF"; |
| 355 | .text | 371 | .text |
| 356 | .globl $func | 372 | .globl $func |
| @@ -365,6 +381,7 @@ EOF | |||
| 365 | else { $tmp=push(@out,".type\t$func,\@function\n"); } | 381 | else { $tmp=push(@out,".type\t$func,\@function\n"); } |
| 366 | push(@out,".align\t$align\n"); | 382 | push(@out,".align\t$align\n"); |
| 367 | push(@out,"$func:\n"); | 383 | push(@out,"$func:\n"); |
| 384 | skip: | ||
| 368 | $tmp=<<"EOF"; | 385 | $tmp=<<"EOF"; |
| 369 | pushl %ebp | 386 | pushl %ebp |
| 370 | pushl %ebx | 387 | pushl %ebx |
| @@ -383,6 +400,47 @@ sub main'function_begin_B | |||
| 383 | &main'external_label($func); | 400 | &main'external_label($func); |
| 384 | $func=$under.$func; | 401 | $func=$under.$func; |
| 385 | 402 | ||
| 403 | if ($main'openbsd) | ||
| 404 | { push(@out, "\nENTRY($func)\n"); goto skip; } | ||
| 405 | |||
| 406 | local($tmp)=<<"EOF"; | ||
| 407 | .text | ||
| 408 | .globl $func | ||
| 409 | EOF | ||
| 410 | push(@out,$tmp); | ||
| 411 | if ($main'cpp) | ||
| 412 | { push(@out,"TYPE($func,\@function)\n"); } | ||
| 413 | elsif ($main'coff) | ||
| 414 | { $tmp=push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); } | ||
| 415 | elsif ($main'aout and !$main'pic) | ||
| 416 | { } | ||
| 417 | else { push(@out,".type $func,\@function\n"); } | ||
| 418 | push(@out,".align\t$align\n"); | ||
| 419 | push(@out,"$func:\n"); | ||
| 420 | skip: | ||
| 421 | $stack=4; | ||
| 422 | } | ||
| 423 | |||
| 424 | # Like function_begin_B but with static linkage | ||
| 425 | sub main'function_begin_C | ||
| 426 | { | ||
| 427 | local($func,$extra)=@_; | ||
| 428 | |||
| 429 | &main'external_label($func); | ||
| 430 | $func=$under.$func; | ||
| 431 | |||
| 432 | if ($main'openbsd) | ||
| 433 | { | ||
| 434 | local($tmp)=<<"EOF"; | ||
| 435 | .text | ||
| 436 | _ALIGN_TEXT | ||
| 437 | .type $func,\@function | ||
| 438 | $func: | ||
| 439 | EOF | ||
| 440 | push(@out, $tmp); | ||
| 441 | goto skip; | ||
| 442 | } | ||
| 443 | |||
| 386 | local($tmp)=<<"EOF"; | 444 | local($tmp)=<<"EOF"; |
| 387 | .text | 445 | .text |
| 388 | .globl $func | 446 | .globl $func |
| @@ -397,6 +455,7 @@ EOF | |||
| 397 | else { push(@out,".type $func,\@function\n"); } | 455 | else { push(@out,".type $func,\@function\n"); } |
| 398 | push(@out,".align\t$align\n"); | 456 | push(@out,".align\t$align\n"); |
| 399 | push(@out,"$func:\n"); | 457 | push(@out,"$func:\n"); |
| 458 | skip: | ||
| 400 | $stack=4; | 459 | $stack=4; |
| 401 | } | 460 | } |
| 402 | 461 | ||
| @@ -457,6 +516,8 @@ sub main'function_end_B | |||
| 457 | %label=(); | 516 | %label=(); |
| 458 | } | 517 | } |
| 459 | 518 | ||
| 519 | sub main'function_end_C { function_end_B(@_); } | ||
| 520 | |||
| 460 | sub main'wparam | 521 | sub main'wparam |
| 461 | { | 522 | { |
| 462 | local($num)=@_; | 523 | local($num)=@_; |
| @@ -493,7 +554,7 @@ sub main'swtmp | |||
| 493 | 554 | ||
| 494 | sub main'comment | 555 | sub main'comment |
| 495 | { | 556 | { |
| 496 | if (!defined($com_start) or $main'elf) | 557 | if (!defined($com_start) or (!$main'openbsd && $main'elf)) |
| 497 | { # Regarding $main'elf above... | 558 | { # Regarding $main'elf above... |
| 498 | # GNU and SVR4 as'es use different comment delimiters, | 559 | # GNU and SVR4 as'es use different comment delimiters, |
| 499 | push(@out,"\n"); # so we just skip ELF comments... | 560 | push(@out,"\n"); # so we just skip ELF comments... |
| @@ -534,7 +595,13 @@ sub main'set_label | |||
| 534 | if ($_[1]!=0) | 595 | if ($_[1]!=0) |
| 535 | { | 596 | { |
| 536 | if ($_[1]>1) { main'align($_[1]); } | 597 | if ($_[1]>1) { main'align($_[1]); } |
| 537 | else { push(@out,".align $align\n"); } | 598 | else |
| 599 | { | ||
| 600 | if ($main'openbsd) | ||
| 601 | { push(@out,"_ALIGN_TEXT\n"); } | ||
| 602 | else | ||
| 603 | { push(@out,".align $align\n"); } | ||
| 604 | } | ||
| 538 | } | 605 | } |
| 539 | push(@out,"$label{$_[0]}:\n"); | 606 | push(@out,"$label{$_[0]}:\n"); |
| 540 | } | 607 | } |
| @@ -578,6 +645,10 @@ sub main'align | |||
| 578 | $val.=",0x90"; | 645 | $val.=",0x90"; |
| 579 | } | 646 | } |
| 580 | push(@out,".align\t$val\n"); | 647 | push(@out,".align\t$val\n"); |
| 648 | if ($main'openbsd) | ||
| 649 | { push(@out,"_ALIGN_TEXT\n"); } | ||
| 650 | else | ||
| 651 | { push(@out,".align $tval\n"); } | ||
| 581 | } | 652 | } |
| 582 | 653 | ||
| 583 | # debug output functions: puts, putx, printf | 654 | # debug output functions: puts, putx, printf |
| @@ -669,6 +740,16 @@ sub main'picmeup | |||
| 669 | ___ | 740 | ___ |
| 670 | push(@out,$tmp); | 741 | push(@out,$tmp); |
| 671 | } | 742 | } |
| 743 | elsif ($main'openbsd) | ||
| 744 | { | ||
| 745 | push(@out, "#ifdef PIC\n"); | ||
| 746 | push(@out, "\tPIC_PROLOGUE\n"); | ||
| 747 | &main'mov($dst,"PIC_GOT($sym)"); | ||
| 748 | push(@out, "\tPIC_EPILOGUE\n"); | ||
| 749 | push(@out, "#else\n"); | ||
| 750 | &main'lea($dst,&main'DWP($sym)); | ||
| 751 | push(@out, "#endif\n"); | ||
| 752 | } | ||
| 672 | elsif ($main'pic && ($main'elf || $main'aout)) | 753 | elsif ($main'pic && ($main'elf || $main'aout)) |
| 673 | { | 754 | { |
| 674 | &main'call(&main'label("PIC_me_up")); | 755 | &main'call(&main'label("PIC_me_up")); |
| @@ -694,7 +775,9 @@ sub main'initseg | |||
| 694 | { | 775 | { |
| 695 | $tmp=<<___; | 776 | $tmp=<<___; |
| 696 | .section .init | 777 | .section .init |
| 697 | call $under$f | 778 | PIC_PROLOGUE |
| 779 | call PIC_PLT($under$f) | ||
| 780 | PIC_EPILOGUE | ||
| 698 | jmp .Linitalign | 781 | jmp .Linitalign |
| 699 | .align $align | 782 | .align $align |
| 700 | .Linitalign: | 783 | .Linitalign: |
