You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

371 lines
12 KiB

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