diff options
Diffstat (limited to 'src/lib/libcrypto/perlasm')
| -rw-r--r-- | src/lib/libcrypto/perlasm/cbc.pl | 349 | ||||
| -rw-r--r-- | src/lib/libcrypto/perlasm/readme | 124 | ||||
| -rw-r--r-- | src/lib/libcrypto/perlasm/x86asm.pl | 135 | ||||
| -rw-r--r-- | src/lib/libcrypto/perlasm/x86nasm.pl | 10 | ||||
| -rw-r--r-- | src/lib/libcrypto/perlasm/x86unix.pl | 45 |
5 files changed, 651 insertions, 12 deletions
diff --git a/src/lib/libcrypto/perlasm/cbc.pl b/src/lib/libcrypto/perlasm/cbc.pl new file mode 100644 index 0000000000..22149c680e --- /dev/null +++ b/src/lib/libcrypto/perlasm/cbc.pl | |||
| @@ -0,0 +1,349 @@ | |||
| 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 | &set_label("cbc_enc_jmp_table",1); | ||
| 326 | &data_word("0"); | ||
| 327 | &data_word(&label("ej1")."-".&label("PIC_point")); | ||
| 328 | &data_word(&label("ej2")."-".&label("PIC_point")); | ||
| 329 | &data_word(&label("ej3")."-".&label("PIC_point")); | ||
| 330 | &data_word(&label("ej4")."-".&label("PIC_point")); | ||
| 331 | &data_word(&label("ej5")."-".&label("PIC_point")); | ||
| 332 | &data_word(&label("ej6")."-".&label("PIC_point")); | ||
| 333 | &data_word(&label("ej7")."-".&label("PIC_point")); | ||
| 334 | # not used | ||
| 335 | #&set_label("cbc_dec_jmp_table",1); | ||
| 336 | #&data_word("0"); | ||
| 337 | #&data_word(&label("dj1")."-".&label("PIC_point")); | ||
| 338 | #&data_word(&label("dj2")."-".&label("PIC_point")); | ||
| 339 | #&data_word(&label("dj3")."-".&label("PIC_point")); | ||
| 340 | #&data_word(&label("dj4")."-".&label("PIC_point")); | ||
| 341 | #&data_word(&label("dj5")."-".&label("PIC_point")); | ||
| 342 | #&data_word(&label("dj6")."-".&label("PIC_point")); | ||
| 343 | #&data_word(&label("dj7")."-".&label("PIC_point")); | ||
| 344 | |||
| 345 | &function_end_B($name); | ||
| 346 | |||
| 347 | } | ||
| 348 | |||
| 349 | 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/x86asm.pl b/src/lib/libcrypto/perlasm/x86asm.pl new file mode 100644 index 0000000000..60233f80e8 --- /dev/null +++ b/src/lib/libcrypto/perlasm/x86asm.pl | |||
| @@ -0,0 +1,135 @@ | |||
| 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=$sol=$aout=$win32=$gaswin=$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 "gaswin")) | ||
| 31 | { $gaswin=1; $aout=1; require "x86unix.pl"; } | ||
| 32 | elsif ( ($type eq "sol")) | ||
| 33 | { $sol=1; require "x86unix.pl"; } | ||
| 34 | elsif ( ($type eq "cpp")) | ||
| 35 | { $cpp=1; require "x86unix.pl"; } | ||
| 36 | elsif ( ($type eq "win32")) | ||
| 37 | { $win32=1; require "x86ms.pl"; } | ||
| 38 | elsif ( ($type eq "win32n")) | ||
| 39 | { $win32=1; require "x86nasm.pl"; } | ||
| 40 | else | ||
| 41 | { | ||
| 42 | print STDERR <<"EOF"; | ||
| 43 | Pick one target type from | ||
| 44 | elf - linux, FreeBSD etc | ||
| 45 | a.out - old linux | ||
| 46 | sol - x86 solaris | ||
| 47 | cpp - format so x86unix.cpp can be used | ||
| 48 | win32 - Windows 95/Windows NT | ||
| 49 | win32n - Windows 95/Windows NT NASM format | ||
| 50 | openbsd-elf - OpenBSD elf | ||
| 51 | openbsd-a.out - OpenBSD a.out | ||
| 52 | EOF | ||
| 53 | exit(1); | ||
| 54 | } | ||
| 55 | |||
| 56 | $pic=0; | ||
| 57 | for (@ARGV) { $pic=1 if (/\-[fK]PIC/i); } | ||
| 58 | |||
| 59 | &asm_init_output(); | ||
| 60 | |||
| 61 | &comment("Don't even think of reading this code"); | ||
| 62 | &comment("It was automatically generated by $filename"); | ||
| 63 | &comment("Which is a perl program used to generate the x86 assember for"); | ||
| 64 | &comment("any of elf, a.out, BSDI, Win32, gaswin (for GNU as on Win32) or Solaris"); | ||
| 65 | &comment("eric <eay\@cryptsoft.com>"); | ||
| 66 | &comment(""); | ||
| 67 | |||
| 68 | $filename =~ s/\.pl$//; | ||
| 69 | &file($filename); | ||
| 70 | } | ||
| 71 | |||
| 72 | sub asm_finish_cpp | ||
| 73 | { | ||
| 74 | return unless $cpp; | ||
| 75 | |||
| 76 | local($tmp,$i); | ||
| 77 | foreach $i (&get_labels()) | ||
| 78 | { | ||
| 79 | $tmp.="#define $i _$i\n"; | ||
| 80 | } | ||
| 81 | print <<"EOF"; | ||
| 82 | /* Run the C pre-processor over this file with one of the following defined | ||
| 83 | * ELF - elf object files, | ||
| 84 | * OUT - a.out object files, | ||
| 85 | * BSDI - BSDI style a.out object files | ||
| 86 | * SOL - Solaris style elf | ||
| 87 | */ | ||
| 88 | |||
| 89 | #define TYPE(a,b) .type a,b | ||
| 90 | #define SIZE(a,b) .size a,b | ||
| 91 | |||
| 92 | #if defined(OUT) || (defined(BSDI) && !defined(ELF)) | ||
| 93 | $tmp | ||
| 94 | #endif | ||
| 95 | |||
| 96 | #ifdef OUT | ||
| 97 | #define OK 1 | ||
| 98 | #define ALIGN 4 | ||
| 99 | #if defined(__CYGWIN__) || defined(__DJGPP__) | ||
| 100 | #undef SIZE | ||
| 101 | #undef TYPE | ||
| 102 | #define SIZE(a,b) | ||
| 103 | #define TYPE(a,b) .def a; .scl 2; .type 32; .endef | ||
| 104 | #endif /* __CYGWIN || __DJGPP */ | ||
| 105 | #endif | ||
| 106 | |||
| 107 | #if defined(BSDI) && !defined(ELF) | ||
| 108 | #define OK 1 | ||
| 109 | #define ALIGN 4 | ||
| 110 | #undef SIZE | ||
| 111 | #undef TYPE | ||
| 112 | #define SIZE(a,b) | ||
| 113 | #define TYPE(a,b) | ||
| 114 | #endif | ||
| 115 | |||
| 116 | #if defined(ELF) || defined(SOL) | ||
| 117 | #define OK 1 | ||
| 118 | #define ALIGN 16 | ||
| 119 | #endif | ||
| 120 | |||
| 121 | #ifndef OK | ||
| 122 | You need to define one of | ||
| 123 | ELF - elf systems - linux-elf, NetBSD and DG-UX | ||
| 124 | OUT - a.out systems - linux-a.out and FreeBSD | ||
| 125 | SOL - solaris systems, which are elf with strange comment lines | ||
| 126 | BSDI - a.out with a very primative version of as. | ||
| 127 | #endif | ||
| 128 | |||
| 129 | /* Let the Assembler begin :-) */ | ||
| 130 | EOF | ||
| 131 | } | ||
| 132 | |||
| 133 | sub main'align() {} # swallow align statements in 0.9.7 context | ||
| 134 | |||
| 135 | 1; | ||
diff --git a/src/lib/libcrypto/perlasm/x86nasm.pl b/src/lib/libcrypto/perlasm/x86nasm.pl index 4bdb3fe180..5009acb4b3 100644 --- a/src/lib/libcrypto/perlasm/x86nasm.pl +++ b/src/lib/libcrypto/perlasm/x86nasm.pl | |||
| @@ -221,15 +221,7 @@ sub using486 | |||
| 221 | 221 | ||
| 222 | sub main'file | 222 | sub main'file |
| 223 | { | 223 | { |
| 224 | local $tmp; | 224 | push(@out, "segment .text use32\n"); |
| 225 | $tmp=<<___; | ||
| 226 | %ifdef __omf__ | ||
| 227 | section code use32 class=code | ||
| 228 | %else | ||
| 229 | section .text | ||
| 230 | %endif | ||
| 231 | ___ | ||
| 232 | push(@out,$tmp); | ||
| 233 | } | 225 | } |
| 234 | 226 | ||
| 235 | sub main'function_begin | 227 | sub main'function_begin |
diff --git a/src/lib/libcrypto/perlasm/x86unix.pl b/src/lib/libcrypto/perlasm/x86unix.pl index a31a25c12b..b61425e951 100644 --- a/src/lib/libcrypto/perlasm/x86unix.pl +++ b/src/lib/libcrypto/perlasm/x86unix.pl | |||
| @@ -15,6 +15,12 @@ sub main'asm_get_output { return(@out); } | |||
| 15 | sub main'get_labels { return(@labels); } | 15 | sub main'get_labels { return(@labels); } |
| 16 | sub main'external_label { push(@labels,@_); } | 16 | sub main'external_label { push(@labels,@_); } |
| 17 | 17 | ||
| 18 | if ($main'openbsd) | ||
| 19 | { | ||
| 20 | $com_start='/*'; | ||
| 21 | $com_end='*/'; | ||
| 22 | } | ||
| 23 | |||
| 18 | if ($main'cpp) | 24 | if ($main'cpp) |
| 19 | { | 25 | { |
| 20 | $align="ALIGN"; | 26 | $align="ALIGN"; |
| @@ -173,7 +179,9 @@ sub main'not { &out1("notl",@_); } | |||
| 173 | sub main'call { &out1("call",($_[0]=~/^\.L/?'':$under).$_[0]); } | 179 | sub main'call { &out1("call",($_[0]=~/^\.L/?'':$under).$_[0]); } |
| 174 | sub main'ret { &out0("ret"); } | 180 | sub main'ret { &out0("ret"); } |
| 175 | sub main'nop { &out0("nop"); } | 181 | sub main'nop { &out0("nop"); } |
| 182 | sub main'test { &out2("testl",@_); } | ||
| 176 | sub main'movz { &out2("movzbl",@_); } | 183 | sub main'movz { &out2("movzbl",@_); } |
| 184 | sub main'neg { &out1("negl",@_); } | ||
| 177 | 185 | ||
| 178 | # The bswapl instruction is new for the 486. Emulate if i386. | 186 | # The bswapl instruction is new for the 486. Emulate if i386. |
| 179 | sub main'bswap | 187 | sub main'bswap |
| @@ -277,6 +285,9 @@ sub main'file | |||
| 277 | { | 285 | { |
| 278 | local($file)=@_; | 286 | local($file)=@_; |
| 279 | 287 | ||
| 288 | if ($main'openbsd) | ||
| 289 | { push(@out,"#include <machine/asm.h>\n"); return; } | ||
| 290 | |||
| 280 | local($tmp)=<<"EOF"; | 291 | local($tmp)=<<"EOF"; |
| 281 | .file "$file.s" | 292 | .file "$file.s" |
| 282 | .version "01.01" | 293 | .version "01.01" |
| @@ -292,6 +303,9 @@ sub main'function_begin | |||
| 292 | &main'external_label($func); | 303 | &main'external_label($func); |
| 293 | $func=$under.$func; | 304 | $func=$under.$func; |
| 294 | 305 | ||
| 306 | if ($main'openbsd) | ||
| 307 | { push (@out, "\nENTRY($func)\n"); goto skip; } | ||
| 308 | |||
| 295 | local($tmp)=<<"EOF"; | 309 | local($tmp)=<<"EOF"; |
| 296 | .text | 310 | .text |
| 297 | .align $align | 311 | .align $align |
| @@ -304,6 +318,7 @@ EOF | |||
| 304 | { $tmp=push(@out,"\t.def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); } | 318 | { $tmp=push(@out,"\t.def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); } |
| 305 | else { $tmp=push(@out,"\t.type\t$func,\@function\n"); } | 319 | else { $tmp=push(@out,"\t.type\t$func,\@function\n"); } |
| 306 | push(@out,"$func:\n"); | 320 | push(@out,"$func:\n"); |
| 321 | skip: | ||
| 307 | $tmp=<<"EOF"; | 322 | $tmp=<<"EOF"; |
| 308 | pushl %ebp | 323 | pushl %ebp |
| 309 | pushl %ebx | 324 | pushl %ebx |
| @@ -322,6 +337,9 @@ sub main'function_begin_B | |||
| 322 | &main'external_label($func); | 337 | &main'external_label($func); |
| 323 | $func=$under.$func; | 338 | $func=$under.$func; |
| 324 | 339 | ||
| 340 | if ($main'openbsd) | ||
| 341 | { push(@out, "\nENTRY($func)\n"); goto skip; } | ||
| 342 | |||
| 325 | local($tmp)=<<"EOF"; | 343 | local($tmp)=<<"EOF"; |
| 326 | .text | 344 | .text |
| 327 | .align $align | 345 | .align $align |
| @@ -334,6 +352,7 @@ EOF | |||
| 334 | { $tmp=push(@out,"\t.def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); } | 352 | { $tmp=push(@out,"\t.def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); } |
| 335 | else { push(@out,"\t.type $func,\@function\n"); } | 353 | else { push(@out,"\t.type $func,\@function\n"); } |
| 336 | push(@out,"$func:\n"); | 354 | push(@out,"$func:\n"); |
| 355 | skip: | ||
| 337 | $stack=4; | 356 | $stack=4; |
| 338 | } | 357 | } |
| 339 | 358 | ||
| @@ -430,7 +449,8 @@ sub main'swtmp | |||
| 430 | 449 | ||
| 431 | sub main'comment | 450 | sub main'comment |
| 432 | { | 451 | { |
| 433 | if ($main'elf) # GNU and SVR4 as'es use different comment delimiters, | 452 | if (!$main'openbsd && $main'elf) |
| 453 | # GNU and SVR4 as'es use different comment delimiters, | ||
| 434 | { # so we just skip comments... | 454 | { # so we just skip comments... |
| 435 | push(@out,"\n"); | 455 | push(@out,"\n"); |
| 436 | return; | 456 | return; |
| @@ -444,6 +464,12 @@ sub main'comment | |||
| 444 | } | 464 | } |
| 445 | } | 465 | } |
| 446 | 466 | ||
| 467 | sub main'public_label | ||
| 468 | { | ||
| 469 | $label{$_[0]}="${under}${_[0]}" if (!defined($label{$_[0]})); | ||
| 470 | push(@out,".globl\t$label{$_[0]}\n"); | ||
| 471 | } | ||
| 472 | |||
| 447 | sub main'label | 473 | sub main'label |
| 448 | { | 474 | { |
| 449 | if (!defined($label{$_[0]})) | 475 | if (!defined($label{$_[0]})) |
| @@ -461,7 +487,10 @@ sub main'set_label | |||
| 461 | $label{$_[0]}=".${label}${_[0]}"; | 487 | $label{$_[0]}=".${label}${_[0]}"; |
| 462 | $label++; | 488 | $label++; |
| 463 | } | 489 | } |
| 464 | push(@out,".align $align\n") if ($_[1] != 0); | 490 | if ($main'openbsd) |
| 491 | { push(@out,"_ALIGN_TEXT\n") if ($_[1] != 0); } | ||
| 492 | else | ||
| 493 | { push(@out,".align $align\n") if ($_[1] != 0); } | ||
| 465 | push(@out,"$label{$_[0]}:\n"); | 494 | push(@out,"$label{$_[0]}:\n"); |
| 466 | } | 495 | } |
| 467 | 496 | ||
| @@ -477,7 +506,7 @@ sub main'file_end | |||
| 477 | 506 | ||
| 478 | sub main'data_word | 507 | sub main'data_word |
| 479 | { | 508 | { |
| 480 | push(@out,"\t.long $_[0]\n"); | 509 | push(@out,"\t.long\t".join(',',@_)."\n"); |
| 481 | } | 510 | } |
| 482 | 511 | ||
| 483 | # debug output functions: puts, putx, printf | 512 | # debug output functions: puts, putx, printf |
| @@ -570,6 +599,16 @@ sub main'picmeup | |||
| 570 | ___ | 599 | ___ |
| 571 | push(@out,$tmp); | 600 | push(@out,$tmp); |
| 572 | } | 601 | } |
| 602 | elsif ($main'openbsd) | ||
| 603 | { | ||
| 604 | push(@out, "#ifdef PIC\n"); | ||
| 605 | push(@out, "\tPIC_PROLOGUE\n"); | ||
| 606 | &main'mov($dst,"PIC_GOT($sym)"); | ||
| 607 | push(@out, "\tPIC_EPILOGUE\n"); | ||
| 608 | push(@out, "#else\n"); | ||
| 609 | &main'lea($dst,&main'DWP($sym)); | ||
| 610 | push(@out, "#endif\n"); | ||
| 611 | } | ||
| 573 | elsif ($main'pic && ($main'elf || $main'aout)) | 612 | elsif ($main'pic && ($main'elf || $main'aout)) |
| 574 | { | 613 | { |
| 575 | push(@out,"\t.align\t8\n"); | 614 | push(@out,"\t.align\t8\n"); |
