md5-x86_64.pl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. #! /usr/bin/env perl
  2. # Author: Marc Bevand <bevand_m (at) epita.fr>
  3. # Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved.
  4. #
  5. # Licensed under the OpenSSL license (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. my $flavour = shift;
  115. my $output = shift;
  116. if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
  117. my $win64=0; $win64=1 if ($flavour =~ /[nm]asm|mingw64/ || $output =~ /\.asm$/);
  118. $0 =~ m/(.*[\/\\])[^\/\\]+$/; my $dir=$1; my $xlate;
  119. ( $xlate="${dir}x86_64-xlate.pl" and -f $xlate ) or
  120. ( $xlate="${dir}../../perlasm/x86_64-xlate.pl" and -f $xlate) or
  121. die "can't locate x86_64-xlate.pl";
  122. open OUT,"| \"$^X\" \"$xlate\" $flavour \"$output\"";
  123. *STDOUT=*OUT;
  124. $code .= <<EOF;
  125. .text
  126. .align 16
  127. .globl md5_block_asm_data_order
  128. .type md5_block_asm_data_order,\@function,3
  129. md5_block_asm_data_order:
  130. .cfi_startproc
  131. push %rbp
  132. .cfi_push %rbp
  133. push %rbx
  134. .cfi_push %rbx
  135. push %r12
  136. .cfi_push %r12
  137. push %r14
  138. .cfi_push %r14
  139. push %r15
  140. .cfi_push %r15
  141. .Lprologue:
  142. # rdi = arg #1 (ctx, MD5_CTX pointer)
  143. # rsi = arg #2 (ptr, data pointer)
  144. # rdx = arg #3 (nbr, number of 16-word blocks to process)
  145. mov %rdi, %rbp # rbp = ctx
  146. shl \$6, %rdx # rdx = nbr in bytes
  147. lea (%rsi,%rdx), %rdi # rdi = end
  148. mov 0*4(%rbp), %eax # eax = ctx->A
  149. mov 1*4(%rbp), %ebx # ebx = ctx->B
  150. mov 2*4(%rbp), %ecx # ecx = ctx->C
  151. mov 3*4(%rbp), %edx # edx = ctx->D
  152. # end is 'rdi'
  153. # ptr is 'rsi'
  154. # A is 'eax'
  155. # B is 'ebx'
  156. # C is 'ecx'
  157. # D is 'edx'
  158. cmp %rdi, %rsi # cmp end with ptr
  159. je .Lend # jmp if ptr == end
  160. # BEGIN of loop over 16-word blocks
  161. .Lloop: # save old values of A, B, C, D
  162. mov %eax, %r8d
  163. mov %ebx, %r9d
  164. mov %ecx, %r14d
  165. mov %edx, %r15d
  166. EOF
  167. round1_step(-1,'%eax','%ebx','%ecx','%edx', '1','0xd76aa478', '7');
  168. round1_step( 0,'%edx','%eax','%ebx','%ecx', '2','0xe8c7b756','12');
  169. round1_step( 0,'%ecx','%edx','%eax','%ebx', '3','0x242070db','17');
  170. round1_step( 0,'%ebx','%ecx','%edx','%eax', '4','0xc1bdceee','22');
  171. round1_step( 0,'%eax','%ebx','%ecx','%edx', '5','0xf57c0faf', '7');
  172. round1_step( 0,'%edx','%eax','%ebx','%ecx', '6','0x4787c62a','12');
  173. round1_step( 0,'%ecx','%edx','%eax','%ebx', '7','0xa8304613','17');
  174. round1_step( 0,'%ebx','%ecx','%edx','%eax', '8','0xfd469501','22');
  175. round1_step( 0,'%eax','%ebx','%ecx','%edx', '9','0x698098d8', '7');
  176. round1_step( 0,'%edx','%eax','%ebx','%ecx','10','0x8b44f7af','12');
  177. round1_step( 0,'%ecx','%edx','%eax','%ebx','11','0xffff5bb1','17');
  178. round1_step( 0,'%ebx','%ecx','%edx','%eax','12','0x895cd7be','22');
  179. round1_step( 0,'%eax','%ebx','%ecx','%edx','13','0x6b901122', '7');
  180. round1_step( 0,'%edx','%eax','%ebx','%ecx','14','0xfd987193','12');
  181. round1_step( 0,'%ecx','%edx','%eax','%ebx','15','0xa679438e','17');
  182. round1_step( 1,'%ebx','%ecx','%edx','%eax', '1','0x49b40821','22');
  183. round2_step(-1,'%eax','%ebx','%ecx','%edx', '6','0xf61e2562', '5');
  184. round2_step( 0,'%edx','%eax','%ebx','%ecx','11','0xc040b340', '9');
  185. round2_step( 0,'%ecx','%edx','%eax','%ebx', '0','0x265e5a51','14');
  186. round2_step( 0,'%ebx','%ecx','%edx','%eax', '5','0xe9b6c7aa','20');
  187. round2_step( 0,'%eax','%ebx','%ecx','%edx','10','0xd62f105d', '5');
  188. round2_step( 0,'%edx','%eax','%ebx','%ecx','15', '0x2441453', '9');
  189. round2_step( 0,'%ecx','%edx','%eax','%ebx', '4','0xd8a1e681','14');
  190. round2_step( 0,'%ebx','%ecx','%edx','%eax', '9','0xe7d3fbc8','20');
  191. round2_step( 0,'%eax','%ebx','%ecx','%edx','14','0x21e1cde6', '5');
  192. round2_step( 0,'%edx','%eax','%ebx','%ecx', '3','0xc33707d6', '9');
  193. round2_step( 0,'%ecx','%edx','%eax','%ebx', '8','0xf4d50d87','14');
  194. round2_step( 0,'%ebx','%ecx','%edx','%eax','13','0x455a14ed','20');
  195. round2_step( 0,'%eax','%ebx','%ecx','%edx', '2','0xa9e3e905', '5');
  196. round2_step( 0,'%edx','%eax','%ebx','%ecx', '7','0xfcefa3f8', '9');
  197. round2_step( 0,'%ecx','%edx','%eax','%ebx','12','0x676f02d9','14');
  198. round2_step( 1,'%ebx','%ecx','%edx','%eax', '5','0x8d2a4c8a','20');
  199. round3_step(-1,'%eax','%ebx','%ecx','%edx', '8','0xfffa3942', '4');
  200. round3_step( 0,'%edx','%eax','%ebx','%ecx','11','0x8771f681','11');
  201. round3_step( 0,'%ecx','%edx','%eax','%ebx','14','0x6d9d6122','16');
  202. round3_step( 0,'%ebx','%ecx','%edx','%eax', '1','0xfde5380c','23');
  203. round3_step( 0,'%eax','%ebx','%ecx','%edx', '4','0xa4beea44', '4');
  204. round3_step( 0,'%edx','%eax','%ebx','%ecx', '7','0x4bdecfa9','11');
  205. round3_step( 0,'%ecx','%edx','%eax','%ebx','10','0xf6bb4b60','16');
  206. round3_step( 0,'%ebx','%ecx','%edx','%eax','13','0xbebfbc70','23');
  207. round3_step( 0,'%eax','%ebx','%ecx','%edx', '0','0x289b7ec6', '4');
  208. round3_step( 0,'%edx','%eax','%ebx','%ecx', '3','0xeaa127fa','11');
  209. round3_step( 0,'%ecx','%edx','%eax','%ebx', '6','0xd4ef3085','16');
  210. round3_step( 0,'%ebx','%ecx','%edx','%eax', '9', '0x4881d05','23');
  211. round3_step( 0,'%eax','%ebx','%ecx','%edx','12','0xd9d4d039', '4');
  212. round3_step( 0,'%edx','%eax','%ebx','%ecx','15','0xe6db99e5','11');
  213. round3_step( 0,'%ecx','%edx','%eax','%ebx', '2','0x1fa27cf8','16');
  214. round3_step( 1,'%ebx','%ecx','%edx','%eax', '0','0xc4ac5665','23');
  215. round4_step(-1,'%eax','%ebx','%ecx','%edx', '7','0xf4292244', '6');
  216. round4_step( 0,'%edx','%eax','%ebx','%ecx','14','0x432aff97','10');
  217. round4_step( 0,'%ecx','%edx','%eax','%ebx', '5','0xab9423a7','15');
  218. round4_step( 0,'%ebx','%ecx','%edx','%eax','12','0xfc93a039','21');
  219. round4_step( 0,'%eax','%ebx','%ecx','%edx', '3','0x655b59c3', '6');
  220. round4_step( 0,'%edx','%eax','%ebx','%ecx','10','0x8f0ccc92','10');
  221. round4_step( 0,'%ecx','%edx','%eax','%ebx', '1','0xffeff47d','15');
  222. round4_step( 0,'%ebx','%ecx','%edx','%eax', '8','0x85845dd1','21');
  223. round4_step( 0,'%eax','%ebx','%ecx','%edx','15','0x6fa87e4f', '6');
  224. round4_step( 0,'%edx','%eax','%ebx','%ecx', '6','0xfe2ce6e0','10');
  225. round4_step( 0,'%ecx','%edx','%eax','%ebx','13','0xa3014314','15');
  226. round4_step( 0,'%ebx','%ecx','%edx','%eax', '4','0x4e0811a1','21');
  227. round4_step( 0,'%eax','%ebx','%ecx','%edx','11','0xf7537e82', '6');
  228. round4_step( 0,'%edx','%eax','%ebx','%ecx', '2','0xbd3af235','10');
  229. round4_step( 0,'%ecx','%edx','%eax','%ebx', '9','0x2ad7d2bb','15');
  230. round4_step( 1,'%ebx','%ecx','%edx','%eax', '0','0xeb86d391','21');
  231. $code .= <<EOF;
  232. # add old values of A, B, C, D
  233. add %r8d, %eax
  234. add %r9d, %ebx
  235. add %r14d, %ecx
  236. add %r15d, %edx
  237. # loop control
  238. add \$64, %rsi # ptr += 64
  239. cmp %rdi, %rsi # cmp end with ptr
  240. jb .Lloop # jmp if ptr < end
  241. # END of loop over 16-word blocks
  242. .Lend:
  243. mov %eax, 0*4(%rbp) # ctx->A = A
  244. mov %ebx, 1*4(%rbp) # ctx->B = B
  245. mov %ecx, 2*4(%rbp) # ctx->C = C
  246. mov %edx, 3*4(%rbp) # ctx->D = D
  247. mov (%rsp),%r15
  248. .cfi_restore %r15
  249. mov 8(%rsp),%r14
  250. .cfi_restore %r14
  251. mov 16(%rsp),%r12
  252. .cfi_restore %r12
  253. mov 24(%rsp),%rbx
  254. .cfi_restore %rbx
  255. mov 32(%rsp),%rbp
  256. .cfi_restore %rbp
  257. add \$40,%rsp
  258. .cfi_adjust_cfa_offset -40
  259. .Lepilogue:
  260. ret
  261. .cfi_endproc
  262. .size md5_block_asm_data_order,.-md5_block_asm_data_order
  263. EOF
  264. # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
  265. # CONTEXT *context,DISPATCHER_CONTEXT *disp)
  266. if ($win64) {
  267. my $rec="%rcx";
  268. my $frame="%rdx";
  269. my $context="%r8";
  270. my $disp="%r9";
  271. $code.=<<___;
  272. .extern __imp_RtlVirtualUnwind
  273. .type se_handler,\@abi-omnipotent
  274. .align 16
  275. se_handler:
  276. push %rsi
  277. push %rdi
  278. push %rbx
  279. push %rbp
  280. push %r12
  281. push %r13
  282. push %r14
  283. push %r15
  284. pushfq
  285. sub \$64,%rsp
  286. mov 120($context),%rax # pull context->Rax
  287. mov 248($context),%rbx # pull context->Rip
  288. lea .Lprologue(%rip),%r10
  289. cmp %r10,%rbx # context->Rip<.Lprologue
  290. jb .Lin_prologue
  291. mov 152($context),%rax # pull context->Rsp
  292. lea .Lepilogue(%rip),%r10
  293. cmp %r10,%rbx # context->Rip>=.Lepilogue
  294. jae .Lin_prologue
  295. lea 40(%rax),%rax
  296. mov -8(%rax),%rbp
  297. mov -16(%rax),%rbx
  298. mov -24(%rax),%r12
  299. mov -32(%rax),%r14
  300. mov -40(%rax),%r15
  301. mov %rbx,144($context) # restore context->Rbx
  302. mov %rbp,160($context) # restore context->Rbp
  303. mov %r12,216($context) # restore context->R12
  304. mov %r14,232($context) # restore context->R14
  305. mov %r15,240($context) # restore context->R15
  306. .Lin_prologue:
  307. mov 8(%rax),%rdi
  308. mov 16(%rax),%rsi
  309. mov %rax,152($context) # restore context->Rsp
  310. mov %rsi,168($context) # restore context->Rsi
  311. mov %rdi,176($context) # restore context->Rdi
  312. mov 40($disp),%rdi # disp->ContextRecord
  313. mov $context,%rsi # context
  314. mov \$154,%ecx # sizeof(CONTEXT)
  315. .long 0xa548f3fc # cld; rep movsq
  316. mov $disp,%rsi
  317. xor %rcx,%rcx # arg1, UNW_FLAG_NHANDLER
  318. mov 8(%rsi),%rdx # arg2, disp->ImageBase
  319. mov 0(%rsi),%r8 # arg3, disp->ControlPc
  320. mov 16(%rsi),%r9 # arg4, disp->FunctionEntry
  321. mov 40(%rsi),%r10 # disp->ContextRecord
  322. lea 56(%rsi),%r11 # &disp->HandlerData
  323. lea 24(%rsi),%r12 # &disp->EstablisherFrame
  324. mov %r10,32(%rsp) # arg5
  325. mov %r11,40(%rsp) # arg6
  326. mov %r12,48(%rsp) # arg7
  327. mov %rcx,56(%rsp) # arg8, (NULL)
  328. call *__imp_RtlVirtualUnwind(%rip)
  329. mov \$1,%eax # ExceptionContinueSearch
  330. add \$64,%rsp
  331. popfq
  332. pop %r15
  333. pop %r14
  334. pop %r13
  335. pop %r12
  336. pop %rbp
  337. pop %rbx
  338. pop %rdi
  339. pop %rsi
  340. ret
  341. .size se_handler,.-se_handler
  342. .section .pdata
  343. .align 4
  344. .rva .LSEH_begin_md5_block_asm_data_order
  345. .rva .LSEH_end_md5_block_asm_data_order
  346. .rva .LSEH_info_md5_block_asm_data_order
  347. .section .xdata
  348. .align 8
  349. .LSEH_info_md5_block_asm_data_order:
  350. .byte 9,0,0,0
  351. .rva se_handler
  352. ___
  353. }
  354. print $code;
  355. close STDOUT;