md5-x86_64.pl 13 KB

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