1#! /usr/bin/env perl 2# Copyright 1995-2016 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 10# Normal is the 11# md5_block_x86(MD5_CTX *c, ULONG *X); 12# version, non-normal is the 13# md5_block_x86(MD5_CTX *c, ULONG *X,int blocks); 14 15$normal=0; 16 17$0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1; 18push(@INC,"${dir}","${dir}../../perlasm"); 19require "x86asm.pl"; 20 21$output=pop; 22open STDOUT,">$output"; 23 24&asm_init($ARGV[0]); 25 26$A="eax"; 27$B="ebx"; 28$C="ecx"; 29$D="edx"; 30$tmp1="edi"; 31$tmp2="ebp"; 32$X="esi"; 33 34# What we need to load into $tmp for the next round 35%Ltmp1=("R0",&Np($C), "R1",&Np($C), "R2",&Np($C), "R3",&Np($D)); 36@xo=( 37 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, # R0 38 1, 6, 11, 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, # R1 39 5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3, 6, 9, 12, 15, 2, # R2 40 0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9, # R3 41 ); 42 43&md5_block("md5_block_asm_data_order"); 44&asm_finish(); 45 46close STDOUT or die "error closing STDOUT: $!"; 47 48sub Np 49 { 50 local($p)=@_; 51 local(%n)=($A,$D,$B,$A,$C,$B,$D,$C); 52 return($n{$p}); 53 } 54 55sub R0 56 { 57 local($pos,$a,$b,$c,$d,$K,$ki,$s,$t)=@_; 58 59 &mov($tmp1,$C) if $pos < 0; 60 &mov($tmp2,&DWP($xo[$ki]*4,$K,"",0)) if $pos < 0; # very first one 61 62 # body proper 63 64 &comment("R0 $ki"); 65 &xor($tmp1,$d); # F function - part 2 66 67 &and($tmp1,$b); # F function - part 3 68 &lea($a,&DWP($t,$a,$tmp2,1)); 69 70 &xor($tmp1,$d); # F function - part 4 71 72 &add($a,$tmp1); 73 &mov($tmp1,&Np($c)) if $pos < 1; # next tmp1 for R0 74 &mov($tmp1,&Np($c)) if $pos == 1; # next tmp1 for R1 75 76 &rotl($a,$s); 77 78 &mov($tmp2,&DWP($xo[$ki+1]*4,$K,"",0)) if ($pos != 2); 79 80 &add($a,$b); 81 } 82 83sub R1 84 { 85 local($pos,$a,$b,$c,$d,$K,$ki,$s,$t)=@_; 86 87 &comment("R1 $ki"); 88 89 &lea($a,&DWP($t,$a,$tmp2,1)); 90 91 &xor($tmp1,$b); # G function - part 2 92 &and($tmp1,$d); # G function - part 3 93 94 &mov($tmp2,&DWP($xo[$ki+1]*4,$K,"",0)) if ($pos != 2); 95 &xor($tmp1,$c); # G function - part 4 96 97 &add($a,$tmp1); 98 &mov($tmp1,&Np($c)) if $pos < 1; # G function - part 1 99 &mov($tmp1,&Np($c)) if $pos == 1; # G function - part 1 100 101 &rotl($a,$s); 102 103 &add($a,$b); 104 } 105 106sub R2 107 { 108 local($n,$pos,$a,$b,$c,$d,$K,$ki,$s,$t)=@_; 109 # This one is different, only 3 logical operations 110 111if (($n & 1) == 0) 112 { 113 &comment("R2 $ki"); 114 # make sure to do 'D' first, not 'B', else we clash with 115 # the last add from the previous round. 116 117 &xor($tmp1,$d); # H function - part 2 118 119 &xor($tmp1,$b); # H function - part 3 120 &lea($a,&DWP($t,$a,$tmp2,1)); 121 122 &add($a,$tmp1); 123 124 &rotl($a,$s); 125 126 &mov($tmp2,&DWP($xo[$ki+1]*4,$K,"",0)); 127 &mov($tmp1,&Np($c)); 128 } 129else 130 { 131 &comment("R2 $ki"); 132 # make sure to do 'D' first, not 'B', else we clash with 133 # the last add from the previous round. 134 135 &lea($a,&DWP($t,$a,$tmp2,1)); 136 137 &add($b,$c); # MOVED FORWARD 138 &xor($tmp1,$d); # H function - part 2 139 140 &xor($tmp1,$b); # H function - part 3 141 &mov($tmp2,&DWP($xo[$ki+1]*4,$K,"",0)) if ($pos != 2); 142 143 &add($a,$tmp1); 144 &mov($tmp1,&Np($c)) if $pos < 1; # H function - part 1 145 &mov($tmp1,-1) if $pos == 1; # I function - part 1 146 147 &rotl($a,$s); 148 149 &add($a,$b); 150 } 151 } 152 153sub R3 154 { 155 local($pos,$a,$b,$c,$d,$K,$ki,$s,$t)=@_; 156 157 &comment("R3 $ki"); 158 159 # ¬($tmp1) 160 &xor($tmp1,$d) if $pos < 0; # I function - part 2 161 162 &or($tmp1,$b); # I function - part 3 163 &lea($a,&DWP($t,$a,$tmp2,1)); 164 165 &xor($tmp1,$c); # I function - part 4 166 &mov($tmp2,&DWP($xo[$ki+1]*4,$K,"",0)) if $pos != 2; # load X/k value 167 &mov($tmp2,&wparam(0)) if $pos == 2; 168 169 &add($a,$tmp1); 170 &mov($tmp1,-1) if $pos < 1; # H function - part 1 171 &add($K,64) if $pos >=1 && !$normal; 172 173 &rotl($a,$s); 174 175 &xor($tmp1,&Np($d)) if $pos <= 0; # I function - part = first time 176 &mov($tmp1,&DWP( 0,$tmp2,"",0)) if $pos > 0; 177 &add($a,$b); 178 } 179 180 181sub md5_block 182 { 183 local($name)=@_; 184 185 &function_begin_B($name,"",3); 186 187 # parameter 1 is the MD5_CTX structure. 188 # A 0 189 # B 4 190 # C 8 191 # D 12 192 193 &push("esi"); 194 &push("edi"); 195 &mov($tmp1, &wparam(0)); # edi 196 &mov($X, &wparam(1)); # esi 197 &mov($C, &wparam(2)); 198 &push("ebp"); 199 &shl($C, 6); 200 &push("ebx"); 201 &add($C, $X); # offset we end at 202 &sub($C, 64); 203 &mov($A, &DWP( 0,$tmp1,"",0)); 204 &push($C); # Put on the TOS 205 &mov($B, &DWP( 4,$tmp1,"",0)); 206 &mov($C, &DWP( 8,$tmp1,"",0)); 207 &mov($D, &DWP(12,$tmp1,"",0)); 208 209 &set_label("start") unless $normal; 210 &comment(""); 211 &comment("R0 section"); 212 213 &R0(-2,$A,$B,$C,$D,$X, 0, 7,0xd76aa478); 214 &R0( 0,$D,$A,$B,$C,$X, 1,12,0xe8c7b756); 215 &R0( 0,$C,$D,$A,$B,$X, 2,17,0x242070db); 216 &R0( 0,$B,$C,$D,$A,$X, 3,22,0xc1bdceee); 217 &R0( 0,$A,$B,$C,$D,$X, 4, 7,0xf57c0faf); 218 &R0( 0,$D,$A,$B,$C,$X, 5,12,0x4787c62a); 219 &R0( 0,$C,$D,$A,$B,$X, 6,17,0xa8304613); 220 &R0( 0,$B,$C,$D,$A,$X, 7,22,0xfd469501); 221 &R0( 0,$A,$B,$C,$D,$X, 8, 7,0x698098d8); 222 &R0( 0,$D,$A,$B,$C,$X, 9,12,0x8b44f7af); 223 &R0( 0,$C,$D,$A,$B,$X,10,17,0xffff5bb1); 224 &R0( 0,$B,$C,$D,$A,$X,11,22,0x895cd7be); 225 &R0( 0,$A,$B,$C,$D,$X,12, 7,0x6b901122); 226 &R0( 0,$D,$A,$B,$C,$X,13,12,0xfd987193); 227 &R0( 0,$C,$D,$A,$B,$X,14,17,0xa679438e); 228 &R0( 1,$B,$C,$D,$A,$X,15,22,0x49b40821); 229 230 &comment(""); 231 &comment("R1 section"); 232 &R1(-1,$A,$B,$C,$D,$X,16, 5,0xf61e2562); 233 &R1( 0,$D,$A,$B,$C,$X,17, 9,0xc040b340); 234 &R1( 0,$C,$D,$A,$B,$X,18,14,0x265e5a51); 235 &R1( 0,$B,$C,$D,$A,$X,19,20,0xe9b6c7aa); 236 &R1( 0,$A,$B,$C,$D,$X,20, 5,0xd62f105d); 237 &R1( 0,$D,$A,$B,$C,$X,21, 9,0x02441453); 238 &R1( 0,$C,$D,$A,$B,$X,22,14,0xd8a1e681); 239 &R1( 0,$B,$C,$D,$A,$X,23,20,0xe7d3fbc8); 240 &R1( 0,$A,$B,$C,$D,$X,24, 5,0x21e1cde6); 241 &R1( 0,$D,$A,$B,$C,$X,25, 9,0xc33707d6); 242 &R1( 0,$C,$D,$A,$B,$X,26,14,0xf4d50d87); 243 &R1( 0,$B,$C,$D,$A,$X,27,20,0x455a14ed); 244 &R1( 0,$A,$B,$C,$D,$X,28, 5,0xa9e3e905); 245 &R1( 0,$D,$A,$B,$C,$X,29, 9,0xfcefa3f8); 246 &R1( 0,$C,$D,$A,$B,$X,30,14,0x676f02d9); 247 &R1( 1,$B,$C,$D,$A,$X,31,20,0x8d2a4c8a); 248 249 &comment(""); 250 &comment("R2 section"); 251 &R2( 0,-1,$A,$B,$C,$D,$X,32, 4,0xfffa3942); 252 &R2( 1, 0,$D,$A,$B,$C,$X,33,11,0x8771f681); 253 &R2( 2, 0,$C,$D,$A,$B,$X,34,16,0x6d9d6122); 254 &R2( 3, 0,$B,$C,$D,$A,$X,35,23,0xfde5380c); 255 &R2( 4, 0,$A,$B,$C,$D,$X,36, 4,0xa4beea44); 256 &R2( 5, 0,$D,$A,$B,$C,$X,37,11,0x4bdecfa9); 257 &R2( 6, 0,$C,$D,$A,$B,$X,38,16,0xf6bb4b60); 258 &R2( 7, 0,$B,$C,$D,$A,$X,39,23,0xbebfbc70); 259 &R2( 8, 0,$A,$B,$C,$D,$X,40, 4,0x289b7ec6); 260 &R2( 9, 0,$D,$A,$B,$C,$X,41,11,0xeaa127fa); 261 &R2(10, 0,$C,$D,$A,$B,$X,42,16,0xd4ef3085); 262 &R2(11, 0,$B,$C,$D,$A,$X,43,23,0x04881d05); 263 &R2(12, 0,$A,$B,$C,$D,$X,44, 4,0xd9d4d039); 264 &R2(13, 0,$D,$A,$B,$C,$X,45,11,0xe6db99e5); 265 &R2(14, 0,$C,$D,$A,$B,$X,46,16,0x1fa27cf8); 266 &R2(15, 1,$B,$C,$D,$A,$X,47,23,0xc4ac5665); 267 268 &comment(""); 269 &comment("R3 section"); 270 &R3(-1,$A,$B,$C,$D,$X,48, 6,0xf4292244); 271 &R3( 0,$D,$A,$B,$C,$X,49,10,0x432aff97); 272 &R3( 0,$C,$D,$A,$B,$X,50,15,0xab9423a7); 273 &R3( 0,$B,$C,$D,$A,$X,51,21,0xfc93a039); 274 &R3( 0,$A,$B,$C,$D,$X,52, 6,0x655b59c3); 275 &R3( 0,$D,$A,$B,$C,$X,53,10,0x8f0ccc92); 276 &R3( 0,$C,$D,$A,$B,$X,54,15,0xffeff47d); 277 &R3( 0,$B,$C,$D,$A,$X,55,21,0x85845dd1); 278 &R3( 0,$A,$B,$C,$D,$X,56, 6,0x6fa87e4f); 279 &R3( 0,$D,$A,$B,$C,$X,57,10,0xfe2ce6e0); 280 &R3( 0,$C,$D,$A,$B,$X,58,15,0xa3014314); 281 &R3( 0,$B,$C,$D,$A,$X,59,21,0x4e0811a1); 282 &R3( 0,$A,$B,$C,$D,$X,60, 6,0xf7537e82); 283 &R3( 0,$D,$A,$B,$C,$X,61,10,0xbd3af235); 284 &R3( 0,$C,$D,$A,$B,$X,62,15,0x2ad7d2bb); 285 &R3( 2,$B,$C,$D,$A,$X,63,21,0xeb86d391); 286 287 # &mov($tmp2,&wparam(0)); # done in the last R3 288 # &mov($tmp1, &DWP( 0,$tmp2,"",0)); # done is the last R3 289 290 &add($A,$tmp1); 291 &mov($tmp1, &DWP( 4,$tmp2,"",0)); 292 293 &add($B,$tmp1); 294 &mov($tmp1, &DWP( 8,$tmp2,"",0)); 295 296 &add($C,$tmp1); 297 &mov($tmp1, &DWP(12,$tmp2,"",0)); 298 299 &add($D,$tmp1); 300 &mov(&DWP( 0,$tmp2,"",0),$A); 301 302 &mov(&DWP( 4,$tmp2,"",0),$B); 303 &mov($tmp1,&swtmp(0)) unless $normal; 304 305 &mov(&DWP( 8,$tmp2,"",0),$C); 306 &mov(&DWP(12,$tmp2,"",0),$D); 307 308 &cmp($tmp1,$X) unless $normal; # check count 309 &jae(&label("start")) unless $normal; 310 311 &pop("eax"); # pop the temp variable off the stack 312 &pop("ebx"); 313 &pop("ebp"); 314 &pop("edi"); 315 &pop("esi"); 316 &ret(); 317 &function_end_B($name); 318 } 319 320