1#! /usr/bin/env perl 2# Author: Marc Bevand <bevand_m (at) epita.fr> 3# Copyright 2005-2020 The OpenSSL Project Authors. All Rights Reserved. 4# 5# Licensed under the Apache License 2.0 (the "License"). You may not use 6# this file except in compliance with the License. You can obtain a copy 7# in the file LICENSE in the source distribution or at 8# https://www.openssl.org/source/license.html 9 10# MD5 optimized for AMD64. 11 12use strict; 13 14my $code; 15 16# round1_step() does: 17# dst = x + ((dst + F(x,y,z) + X[k] + T_i) <<< s) 18# %r10d = X[k_next] 19# %r11d = z' (copy of z for the next step) 20# Each round1_step() takes about 5.3 clocks (9 instructions, 1.7 IPC) 21sub round1_step 22{ 23 my ($pos, $dst, $x, $y, $z, $k_next, $T_i, $s) = @_; 24 $code .= " mov 0*4(%rsi), %r10d /* (NEXT STEP) X[0] */\n" if ($pos == -1); 25 $code .= " mov %edx, %r11d /* (NEXT STEP) z' = %edx */\n" if ($pos == -1); 26 $code .= <<EOF; 27 xor $y, %r11d /* y ^ ... */ 28 lea $T_i($dst,%r10d),$dst /* Const + dst + ... */ 29 and $x, %r11d /* x & ... */ 30 mov $k_next*4(%rsi),%r10d /* (NEXT STEP) X[$k_next] */ 31 xor $z, %r11d /* z ^ ... */ 32 add %r11d, $dst /* dst += ... */ 33 rol \$$s, $dst /* dst <<< s */ 34 mov $y, %r11d /* (NEXT STEP) z' = $y */ 35 add $x, $dst /* dst += x */ 36EOF 37} 38 39# round2_step() does: 40# dst = x + ((dst + G(x,y,z) + X[k] + T_i) <<< s) 41# %r10d = X[k_next] 42# %r11d = z' (copy of z for the next step) 43# %r12d = z' (copy of z for the next step) 44sub round2_step 45{ 46 my ($pos, $dst, $x, $y, $z, $k_next, $T_i, $s) = @_; 47 $code .= " mov %edx, %r11d /* (NEXT STEP) z' = %edx */\n" if ($pos == -1); 48 $code .= " mov %edx, %r12d /* (NEXT STEP) z' = %edx */\n" if ($pos == -1); 49 $code .= <<EOF; 50 not %r11d /* not z */ 51 and $x, %r12d /* x & z */ 52 lea $T_i($dst,%r10d),$dst /* Const + dst + ... */ 53 and $y, %r11d /* y & (not z) */ 54 mov $k_next*4(%rsi),%r10d /* (NEXT STEP) X[$k_next] */ 55 add %r11d, $dst /* dst += (y & (not z)) */ 56 mov $y, %r11d /* (NEXT STEP) z' = $y */ 57 add %r12d, $dst /* dst += (x & z) */ 58 mov $y, %r12d /* (NEXT STEP) z' = $y */ 59 rol \$$s, $dst /* dst <<< s */ 60 add $x, $dst /* dst += x */ 61EOF 62} 63 64# round3_step() does: 65# dst = x + ((dst + H(x,y,z) + X[k] + T_i) <<< s) 66# %r10d = X[k_next] 67# %r11d = y' (copy of y for the next step) 68# Each round3_step() takes about 4.2 clocks (8 instructions, 1.9 IPC) 69{ my $round3_alter=0; 70sub round3_step 71{ 72 my ($pos, $dst, $x, $y, $z, $k_next, $T_i, $s) = @_; 73 $code .= " mov %ecx, %r11d /* (NEXT STEP) y' = %ecx */\n" if ($pos == -1); 74 $code .= <<EOF; 75 lea $T_i($dst,%r10d),$dst /* Const + dst + ... */ 76 xor $z, %r11d /* z ^ ... */ 77 mov $k_next*4(%rsi),%r10d /* (NEXT STEP) X[$k_next] */ 78 xor $x, %r11d /* x ^ ... */ 79 add %r11d, $dst /* dst += ... */ 80EOF 81 $code .= <<EOF if ($round3_alter); 82 rol \$$s, $dst /* dst <<< s */ 83 mov $x, %r11d /* (NEXT STEP) y' = $x */ 84EOF 85 $code .= <<EOF if (!$round3_alter); 86 mov $x, %r11d /* (NEXT STEP) y' = $x */ 87 rol \$$s, $dst /* dst <<< s */ 88EOF 89 $code .= <<EOF; 90 add $x, $dst /* dst += x */ 91EOF 92 $round3_alter^=1; 93} 94} 95 96# round4_step() does: 97# dst = x + ((dst + I(x,y,z) + X[k] + T_i) <<< s) 98# %r10d = X[k_next] 99# %r11d = not z' (copy of not z for the next step) 100# Each round4_step() takes about 5.2 clocks (9 instructions, 1.7 IPC) 101sub round4_step 102{ 103 my ($pos, $dst, $x, $y, $z, $k_next, $T_i, $s) = @_; 104 $code .= " mov \$0xffffffff, %r11d\n" if ($pos == -1); 105 $code .= " xor %edx, %r11d /* (NEXT STEP) not z' = not %edx*/\n" 106 if ($pos == -1); 107 $code .= <<EOF; 108 lea $T_i($dst,%r10d),$dst /* Const + dst + ... */ 109 or $x, %r11d /* x | ... */ 110 mov $k_next*4(%rsi),%r10d /* (NEXT STEP) X[$k_next] */ 111 xor $y, %r11d /* y ^ ... */ 112 add %r11d, $dst /* dst += ... */ 113 mov \$0xffffffff, %r11d 114 rol \$$s, $dst /* dst <<< s */ 115 xor $y, %r11d /* (NEXT STEP) not z' = not $y */ 116 add $x, $dst /* dst += x */ 117EOF 118} 119 120no warnings qw(uninitialized); 121# $output is the last argument if it looks like a file (it has an extension) 122# $flavour is the first argument if it doesn't look like a file 123my $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef; 124my $flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef; 125 126my $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/); 127 128$0 =~ m/(.*[\/\\])[^\/\\]+$/; my $dir=$1; my $xlate; 129( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or 130( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or 131die "can't locate x86_64-xlate.pl"; 132 133open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\"" 134 or die "can't call $xlate: $!"; 135*STDOUT=*OUT; 136 137$code .= <<EOF; 138.text 139.align 16 140 141.globl ossl_md5_block_asm_data_order 142.type ossl_md5_block_asm_data_order,\@function,3 143ossl_md5_block_asm_data_order: 144.cfi_startproc 145 push %rbp 146.cfi_push %rbp 147 push %rbx 148.cfi_push %rbx 149 push %r12 150.cfi_push %r12 151 push %r14 152.cfi_push %r14 153 push %r15 154.cfi_push %r15 155.Lprologue: 156 157 # rdi = arg #1 (ctx, MD5_CTX pointer) 158 # rsi = arg #2 (ptr, data pointer) 159 # rdx = arg #3 (nbr, number of 16-word blocks to process) 160 mov %rdi, %rbp # rbp = ctx 161 shl \$6, %rdx # rdx = nbr in bytes 162 lea (%rsi,%rdx), %rdi # rdi = end 163 mov 0*4(%rbp), %eax # eax = ctx->A 164 mov 1*4(%rbp), %ebx # ebx = ctx->B 165 mov 2*4(%rbp), %ecx # ecx = ctx->C 166 mov 3*4(%rbp), %edx # edx = ctx->D 167 # end is 'rdi' 168 # ptr is 'rsi' 169 # A is 'eax' 170 # B is 'ebx' 171 # C is 'ecx' 172 # D is 'edx' 173 174 cmp %rdi, %rsi # cmp end with ptr 175 je .Lend # jmp if ptr == end 176 177 # BEGIN of loop over 16-word blocks 178.Lloop: # save old values of A, B, C, D 179 mov %eax, %r8d 180 mov %ebx, %r9d 181 mov %ecx, %r14d 182 mov %edx, %r15d 183EOF 184round1_step(-1,'%eax','%ebx','%ecx','%edx', '1','0xd76aa478', '7'); 185round1_step( 0,'%edx','%eax','%ebx','%ecx', '2','0xe8c7b756','12'); 186round1_step( 0,'%ecx','%edx','%eax','%ebx', '3','0x242070db','17'); 187round1_step( 0,'%ebx','%ecx','%edx','%eax', '4','0xc1bdceee','22'); 188round1_step( 0,'%eax','%ebx','%ecx','%edx', '5','0xf57c0faf', '7'); 189round1_step( 0,'%edx','%eax','%ebx','%ecx', '6','0x4787c62a','12'); 190round1_step( 0,'%ecx','%edx','%eax','%ebx', '7','0xa8304613','17'); 191round1_step( 0,'%ebx','%ecx','%edx','%eax', '8','0xfd469501','22'); 192round1_step( 0,'%eax','%ebx','%ecx','%edx', '9','0x698098d8', '7'); 193round1_step( 0,'%edx','%eax','%ebx','%ecx','10','0x8b44f7af','12'); 194round1_step( 0,'%ecx','%edx','%eax','%ebx','11','0xffff5bb1','17'); 195round1_step( 0,'%ebx','%ecx','%edx','%eax','12','0x895cd7be','22'); 196round1_step( 0,'%eax','%ebx','%ecx','%edx','13','0x6b901122', '7'); 197round1_step( 0,'%edx','%eax','%ebx','%ecx','14','0xfd987193','12'); 198round1_step( 0,'%ecx','%edx','%eax','%ebx','15','0xa679438e','17'); 199round1_step( 1,'%ebx','%ecx','%edx','%eax', '1','0x49b40821','22'); 200 201round2_step(-1,'%eax','%ebx','%ecx','%edx', '6','0xf61e2562', '5'); 202round2_step( 0,'%edx','%eax','%ebx','%ecx','11','0xc040b340', '9'); 203round2_step( 0,'%ecx','%edx','%eax','%ebx', '0','0x265e5a51','14'); 204round2_step( 0,'%ebx','%ecx','%edx','%eax', '5','0xe9b6c7aa','20'); 205round2_step( 0,'%eax','%ebx','%ecx','%edx','10','0xd62f105d', '5'); 206round2_step( 0,'%edx','%eax','%ebx','%ecx','15', '0x2441453', '9'); 207round2_step( 0,'%ecx','%edx','%eax','%ebx', '4','0xd8a1e681','14'); 208round2_step( 0,'%ebx','%ecx','%edx','%eax', '9','0xe7d3fbc8','20'); 209round2_step( 0,'%eax','%ebx','%ecx','%edx','14','0x21e1cde6', '5'); 210round2_step( 0,'%edx','%eax','%ebx','%ecx', '3','0xc33707d6', '9'); 211round2_step( 0,'%ecx','%edx','%eax','%ebx', '8','0xf4d50d87','14'); 212round2_step( 0,'%ebx','%ecx','%edx','%eax','13','0x455a14ed','20'); 213round2_step( 0,'%eax','%ebx','%ecx','%edx', '2','0xa9e3e905', '5'); 214round2_step( 0,'%edx','%eax','%ebx','%ecx', '7','0xfcefa3f8', '9'); 215round2_step( 0,'%ecx','%edx','%eax','%ebx','12','0x676f02d9','14'); 216round2_step( 1,'%ebx','%ecx','%edx','%eax', '5','0x8d2a4c8a','20'); 217 218round3_step(-1,'%eax','%ebx','%ecx','%edx', '8','0xfffa3942', '4'); 219round3_step( 0,'%edx','%eax','%ebx','%ecx','11','0x8771f681','11'); 220round3_step( 0,'%ecx','%edx','%eax','%ebx','14','0x6d9d6122','16'); 221round3_step( 0,'%ebx','%ecx','%edx','%eax', '1','0xfde5380c','23'); 222round3_step( 0,'%eax','%ebx','%ecx','%edx', '4','0xa4beea44', '4'); 223round3_step( 0,'%edx','%eax','%ebx','%ecx', '7','0x4bdecfa9','11'); 224round3_step( 0,'%ecx','%edx','%eax','%ebx','10','0xf6bb4b60','16'); 225round3_step( 0,'%ebx','%ecx','%edx','%eax','13','0xbebfbc70','23'); 226round3_step( 0,'%eax','%ebx','%ecx','%edx', '0','0x289b7ec6', '4'); 227round3_step( 0,'%edx','%eax','%ebx','%ecx', '3','0xeaa127fa','11'); 228round3_step( 0,'%ecx','%edx','%eax','%ebx', '6','0xd4ef3085','16'); 229round3_step( 0,'%ebx','%ecx','%edx','%eax', '9', '0x4881d05','23'); 230round3_step( 0,'%eax','%ebx','%ecx','%edx','12','0xd9d4d039', '4'); 231round3_step( 0,'%edx','%eax','%ebx','%ecx','15','0xe6db99e5','11'); 232round3_step( 0,'%ecx','%edx','%eax','%ebx', '2','0x1fa27cf8','16'); 233round3_step( 1,'%ebx','%ecx','%edx','%eax', '0','0xc4ac5665','23'); 234 235round4_step(-1,'%eax','%ebx','%ecx','%edx', '7','0xf4292244', '6'); 236round4_step( 0,'%edx','%eax','%ebx','%ecx','14','0x432aff97','10'); 237round4_step( 0,'%ecx','%edx','%eax','%ebx', '5','0xab9423a7','15'); 238round4_step( 0,'%ebx','%ecx','%edx','%eax','12','0xfc93a039','21'); 239round4_step( 0,'%eax','%ebx','%ecx','%edx', '3','0x655b59c3', '6'); 240round4_step( 0,'%edx','%eax','%ebx','%ecx','10','0x8f0ccc92','10'); 241round4_step( 0,'%ecx','%edx','%eax','%ebx', '1','0xffeff47d','15'); 242round4_step( 0,'%ebx','%ecx','%edx','%eax', '8','0x85845dd1','21'); 243round4_step( 0,'%eax','%ebx','%ecx','%edx','15','0x6fa87e4f', '6'); 244round4_step( 0,'%edx','%eax','%ebx','%ecx', '6','0xfe2ce6e0','10'); 245round4_step( 0,'%ecx','%edx','%eax','%ebx','13','0xa3014314','15'); 246round4_step( 0,'%ebx','%ecx','%edx','%eax', '4','0x4e0811a1','21'); 247round4_step( 0,'%eax','%ebx','%ecx','%edx','11','0xf7537e82', '6'); 248round4_step( 0,'%edx','%eax','%ebx','%ecx', '2','0xbd3af235','10'); 249round4_step( 0,'%ecx','%edx','%eax','%ebx', '9','0x2ad7d2bb','15'); 250round4_step( 1,'%ebx','%ecx','%edx','%eax', '0','0xeb86d391','21'); 251$code .= <<EOF; 252 # add old values of A, B, C, D 253 add %r8d, %eax 254 add %r9d, %ebx 255 add %r14d, %ecx 256 add %r15d, %edx 257 258 # loop control 259 add \$64, %rsi # ptr += 64 260 cmp %rdi, %rsi # cmp end with ptr 261 jb .Lloop # jmp if ptr < end 262 # END of loop over 16-word blocks 263 264.Lend: 265 mov %eax, 0*4(%rbp) # ctx->A = A 266 mov %ebx, 1*4(%rbp) # ctx->B = B 267 mov %ecx, 2*4(%rbp) # ctx->C = C 268 mov %edx, 3*4(%rbp) # ctx->D = D 269 270 mov (%rsp),%r15 271.cfi_restore %r15 272 mov 8(%rsp),%r14 273.cfi_restore %r14 274 mov 16(%rsp),%r12 275.cfi_restore %r12 276 mov 24(%rsp),%rbx 277.cfi_restore %rbx 278 mov 32(%rsp),%rbp 279.cfi_restore %rbp 280 add \$40,%rsp 281.cfi_adjust_cfa_offset -40 282.Lepilogue: 283 ret 284.cfi_endproc 285.size ossl_md5_block_asm_data_order,.-ossl_md5_block_asm_data_order 286EOF 287 288# EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame, 289# CONTEXT *context,DISPATCHER_CONTEXT *disp) 290if ($win64) { 291my $rec="%rcx"; 292my $frame="%rdx"; 293my $context="%r8"; 294my $disp="%r9"; 295 296$code.=<<___; 297.extern __imp_RtlVirtualUnwind 298.type se_handler,\@abi-omnipotent 299.align 16 300se_handler: 301 push %rsi 302 push %rdi 303 push %rbx 304 push %rbp 305 push %r12 306 push %r13 307 push %r14 308 push %r15 309 pushfq 310 sub \$64,%rsp 311 312 mov 120($context),%rax # pull context->Rax 313 mov 248($context),%rbx # pull context->Rip 314 315 lea .Lprologue(%rip),%r10 316 cmp %r10,%rbx # context->Rip<.Lprologue 317 jb .Lin_prologue 318 319 mov 152($context),%rax # pull context->Rsp 320 321 lea .Lepilogue(%rip),%r10 322 cmp %r10,%rbx # context->Rip>=.Lepilogue 323 jae .Lin_prologue 324 325 lea 40(%rax),%rax 326 327 mov -8(%rax),%rbp 328 mov -16(%rax),%rbx 329 mov -24(%rax),%r12 330 mov -32(%rax),%r14 331 mov -40(%rax),%r15 332 mov %rbx,144($context) # restore context->Rbx 333 mov %rbp,160($context) # restore context->Rbp 334 mov %r12,216($context) # restore context->R12 335 mov %r14,232($context) # restore context->R14 336 mov %r15,240($context) # restore context->R15 337 338.Lin_prologue: 339 mov 8(%rax),%rdi 340 mov 16(%rax),%rsi 341 mov %rax,152($context) # restore context->Rsp 342 mov %rsi,168($context) # restore context->Rsi 343 mov %rdi,176($context) # restore context->Rdi 344 345 mov 40($disp),%rdi # disp->ContextRecord 346 mov $context,%rsi # context 347 mov \$154,%ecx # sizeof(CONTEXT) 348 .long 0xa548f3fc # cld; rep movsq 349 350 mov $disp,%rsi 351 xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER 352 mov 8(%rsi),%rdx # arg2, disp->ImageBase 353 mov 0(%rsi),%r8 # arg3, disp->ControlPc 354 mov 16(%rsi),%r9 # arg4, disp->FunctionEntry 355 mov 40(%rsi),%r10 # disp->ContextRecord 356 lea 56(%rsi),%r11 # &disp->HandlerData 357 lea 24(%rsi),%r12 # &disp->EstablisherFrame 358 mov %r10,32(%rsp) # arg5 359 mov %r11,40(%rsp) # arg6 360 mov %r12,48(%rsp) # arg7 361 mov %rcx,56(%rsp) # arg8, (NULL) 362 call *__imp_RtlVirtualUnwind(%rip) 363 364 mov \$1,%eax # ExceptionContinueSearch 365 add \$64,%rsp 366 popfq 367 pop %r15 368 pop %r14 369 pop %r13 370 pop %r12 371 pop %rbp 372 pop %rbx 373 pop %rdi 374 pop %rsi 375 ret 376.size se_handler,.-se_handler 377 378.section .pdata 379.align 4 380 .rva .LSEH_begin_ossl_md5_block_asm_data_order 381 .rva .LSEH_end_ossl_md5_block_asm_data_order 382 .rva .LSEH_info_ossl_md5_block_asm_data_order 383 384.section .xdata 385.align 8 386.LSEH_info_ossl_md5_block_asm_data_order: 387 .byte 9,0,0,0 388 .rva se_handler 389___ 390} 391 392print $code; 393 394close STDOUT or die "error closing STDOUT: $!"; 395