2
0

md5-x86_64.pl 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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.71 clocks (9 instructions, 1.58 IPC)
  16. sub round1_step
  17. {
  18. my ($pos, $dst, $x, $y, $z, $k_next, $T_i, $s) = @_;
  19. $T_i = unpack("l",pack("l", hex($T_i))); # convert to 32-bit signed decimal
  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. xor $z, %r11d /* z ^ ... */
  27. mov $k_next*4(%rsi),%r10d /* (NEXT STEP) X[$k_next] */
  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 = y' (copy of y for the next step)
  38. # Each round2_step() takes about 6.22 clocks (9 instructions, 1.45 IPC)
  39. sub round2_step
  40. {
  41. my ($pos, $dst, $x, $y, $z, $k_next, $T_i, $s) = @_;
  42. $T_i = unpack("l",pack("l", hex($T_i))); # convert to 32-bit signed decimal
  43. $code .= " mov 1*4(%rsi), %r10d /* (NEXT STEP) X[1] */\n" if ($pos == -1);
  44. $code .= " mov %ecx, %r11d /* (NEXT STEP) y' = %ecx */\n" if ($pos == -1);
  45. $code .= <<EOF;
  46. xor $x, %r11d /* x ^ ... */
  47. lea $T_i($dst,%r10d),$dst /* Const + dst + ... */
  48. and $z, %r11d /* z & ... */
  49. xor $y, %r11d /* y ^ ... */
  50. mov $k_next*4(%rsi),%r10d /* (NEXT STEP) X[$k_next] */
  51. add %r11d, $dst /* dst += ... */
  52. rol \$$s, $dst /* dst <<< s */
  53. mov $x, %r11d /* (NEXT STEP) y' = $x */
  54. add $x, $dst /* dst += x */
  55. EOF
  56. }
  57. # round3_step() does:
  58. # dst = x + ((dst + H(x,y,z) + X[k] + T_i) <<< s)
  59. # %r10d = X[k_next]
  60. # %r11d = y' (copy of y for the next step)
  61. # Each round3_step() takes about 4.26 clocks (8 instructions, 1.88 IPC)
  62. sub round3_step
  63. {
  64. my ($pos, $dst, $x, $y, $z, $k_next, $T_i, $s) = @_;
  65. $T_i = unpack("l",pack("l", hex($T_i))); # convert to 32-bit signed decimal
  66. $code .= " mov 5*4(%rsi), %r10d /* (NEXT STEP) X[5] */\n" if ($pos == -1);
  67. $code .= " mov %ecx, %r11d /* (NEXT STEP) y' = %ecx */\n" if ($pos == -1);
  68. $code .= <<EOF;
  69. lea $T_i($dst,%r10d),$dst /* Const + dst + ... */
  70. mov $k_next*4(%rsi),%r10d /* (NEXT STEP) X[$k_next] */
  71. xor $z, %r11d /* z ^ ... */
  72. xor $x, %r11d /* x ^ ... */
  73. add %r11d, $dst /* dst += ... */
  74. rol \$$s, $dst /* dst <<< s */
  75. mov $x, %r11d /* (NEXT STEP) y' = $x */
  76. add $x, $dst /* dst += x */
  77. EOF
  78. }
  79. # round4_step() does:
  80. # dst = x + ((dst + I(x,y,z) + X[k] + T_i) <<< s)
  81. # %r10d = X[k_next]
  82. # %r11d = not z' (copy of not z for the next step)
  83. # Each round4_step() takes about 5.27 clocks (9 instructions, 1.71 IPC)
  84. sub round4_step
  85. {
  86. my ($pos, $dst, $x, $y, $z, $k_next, $T_i, $s) = @_;
  87. $T_i = unpack("l",pack("l", hex($T_i))); # convert to 32-bit signed decimal
  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. my $output = shift;
  105. open STDOUT,"| $^X ../perlasm/x86_64-xlate.pl $output";
  106. $code .= <<EOF;
  107. .text
  108. .align 16
  109. .globl md5_block_asm_data_order
  110. .type md5_block_asm_data_order,\@function,3
  111. md5_block_asm_data_order:
  112. push %rbp
  113. push %rbx
  114. push %r14
  115. push %r15
  116. # rdi = arg #1 (ctx, MD5_CTX pointer)
  117. # rsi = arg #2 (ptr, data pointer)
  118. # rdx = arg #3 (nbr, number of 16-word blocks to process)
  119. mov %rdi, %rbp # rbp = ctx
  120. shl \$6, %rdx # rdx = nbr in bytes
  121. lea (%rsi,%rdx), %rdi # rdi = end
  122. mov 0*4(%rbp), %eax # eax = ctx->A
  123. mov 1*4(%rbp), %ebx # ebx = ctx->B
  124. mov 2*4(%rbp), %ecx # ecx = ctx->C
  125. mov 3*4(%rbp), %edx # edx = ctx->D
  126. # end is 'rdi'
  127. # ptr is 'rsi'
  128. # A is 'eax'
  129. # B is 'ebx'
  130. # C is 'ecx'
  131. # D is 'edx'
  132. cmp %rdi, %rsi # cmp end with ptr
  133. je .Lend # jmp if ptr == end
  134. # BEGIN of loop over 16-word blocks
  135. .Lloop: # save old values of A, B, C, D
  136. mov %eax, %r8d
  137. mov %ebx, %r9d
  138. mov %ecx, %r14d
  139. mov %edx, %r15d
  140. EOF
  141. round1_step(-1,'%eax','%ebx','%ecx','%edx', '1','0xd76aa478', '7');
  142. round1_step( 0,'%edx','%eax','%ebx','%ecx', '2','0xe8c7b756','12');
  143. round1_step( 0,'%ecx','%edx','%eax','%ebx', '3','0x242070db','17');
  144. round1_step( 0,'%ebx','%ecx','%edx','%eax', '4','0xc1bdceee','22');
  145. round1_step( 0,'%eax','%ebx','%ecx','%edx', '5','0xf57c0faf', '7');
  146. round1_step( 0,'%edx','%eax','%ebx','%ecx', '6','0x4787c62a','12');
  147. round1_step( 0,'%ecx','%edx','%eax','%ebx', '7','0xa8304613','17');
  148. round1_step( 0,'%ebx','%ecx','%edx','%eax', '8','0xfd469501','22');
  149. round1_step( 0,'%eax','%ebx','%ecx','%edx', '9','0x698098d8', '7');
  150. round1_step( 0,'%edx','%eax','%ebx','%ecx','10','0x8b44f7af','12');
  151. round1_step( 0,'%ecx','%edx','%eax','%ebx','11','0xffff5bb1','17');
  152. round1_step( 0,'%ebx','%ecx','%edx','%eax','12','0x895cd7be','22');
  153. round1_step( 0,'%eax','%ebx','%ecx','%edx','13','0x6b901122', '7');
  154. round1_step( 0,'%edx','%eax','%ebx','%ecx','14','0xfd987193','12');
  155. round1_step( 0,'%ecx','%edx','%eax','%ebx','15','0xa679438e','17');
  156. round1_step( 1,'%ebx','%ecx','%edx','%eax', '0','0x49b40821','22');
  157. round2_step(-1,'%eax','%ebx','%ecx','%edx', '6','0xf61e2562', '5');
  158. round2_step( 0,'%edx','%eax','%ebx','%ecx','11','0xc040b340', '9');
  159. round2_step( 0,'%ecx','%edx','%eax','%ebx', '0','0x265e5a51','14');
  160. round2_step( 0,'%ebx','%ecx','%edx','%eax', '5','0xe9b6c7aa','20');
  161. round2_step( 0,'%eax','%ebx','%ecx','%edx','10','0xd62f105d', '5');
  162. round2_step( 0,'%edx','%eax','%ebx','%ecx','15', '0x2441453', '9');
  163. round2_step( 0,'%ecx','%edx','%eax','%ebx', '4','0xd8a1e681','14');
  164. round2_step( 0,'%ebx','%ecx','%edx','%eax', '9','0xe7d3fbc8','20');
  165. round2_step( 0,'%eax','%ebx','%ecx','%edx','14','0x21e1cde6', '5');
  166. round2_step( 0,'%edx','%eax','%ebx','%ecx', '3','0xc33707d6', '9');
  167. round2_step( 0,'%ecx','%edx','%eax','%ebx', '8','0xf4d50d87','14');
  168. round2_step( 0,'%ebx','%ecx','%edx','%eax','13','0x455a14ed','20');
  169. round2_step( 0,'%eax','%ebx','%ecx','%edx', '2','0xa9e3e905', '5');
  170. round2_step( 0,'%edx','%eax','%ebx','%ecx', '7','0xfcefa3f8', '9');
  171. round2_step( 0,'%ecx','%edx','%eax','%ebx','12','0x676f02d9','14');
  172. round2_step( 1,'%ebx','%ecx','%edx','%eax', '0','0x8d2a4c8a','20');
  173. round3_step(-1,'%eax','%ebx','%ecx','%edx', '8','0xfffa3942', '4');
  174. round3_step( 0,'%edx','%eax','%ebx','%ecx','11','0x8771f681','11');
  175. round3_step( 0,'%ecx','%edx','%eax','%ebx','14','0x6d9d6122','16');
  176. round3_step( 0,'%ebx','%ecx','%edx','%eax', '1','0xfde5380c','23');
  177. round3_step( 0,'%eax','%ebx','%ecx','%edx', '4','0xa4beea44', '4');
  178. round3_step( 0,'%edx','%eax','%ebx','%ecx', '7','0x4bdecfa9','11');
  179. round3_step( 0,'%ecx','%edx','%eax','%ebx','10','0xf6bb4b60','16');
  180. round3_step( 0,'%ebx','%ecx','%edx','%eax','13','0xbebfbc70','23');
  181. round3_step( 0,'%eax','%ebx','%ecx','%edx', '0','0x289b7ec6', '4');
  182. round3_step( 0,'%edx','%eax','%ebx','%ecx', '3','0xeaa127fa','11');
  183. round3_step( 0,'%ecx','%edx','%eax','%ebx', '6','0xd4ef3085','16');
  184. round3_step( 0,'%ebx','%ecx','%edx','%eax', '9', '0x4881d05','23');
  185. round3_step( 0,'%eax','%ebx','%ecx','%edx','12','0xd9d4d039', '4');
  186. round3_step( 0,'%edx','%eax','%ebx','%ecx','15','0xe6db99e5','11');
  187. round3_step( 0,'%ecx','%edx','%eax','%ebx', '2','0x1fa27cf8','16');
  188. round3_step( 1,'%ebx','%ecx','%edx','%eax', '0','0xc4ac5665','23');
  189. round4_step(-1,'%eax','%ebx','%ecx','%edx', '7','0xf4292244', '6');
  190. round4_step( 0,'%edx','%eax','%ebx','%ecx','14','0x432aff97','10');
  191. round4_step( 0,'%ecx','%edx','%eax','%ebx', '5','0xab9423a7','15');
  192. round4_step( 0,'%ebx','%ecx','%edx','%eax','12','0xfc93a039','21');
  193. round4_step( 0,'%eax','%ebx','%ecx','%edx', '3','0x655b59c3', '6');
  194. round4_step( 0,'%edx','%eax','%ebx','%ecx','10','0x8f0ccc92','10');
  195. round4_step( 0,'%ecx','%edx','%eax','%ebx', '1','0xffeff47d','15');
  196. round4_step( 0,'%ebx','%ecx','%edx','%eax', '8','0x85845dd1','21');
  197. round4_step( 0,'%eax','%ebx','%ecx','%edx','15','0x6fa87e4f', '6');
  198. round4_step( 0,'%edx','%eax','%ebx','%ecx', '6','0xfe2ce6e0','10');
  199. round4_step( 0,'%ecx','%edx','%eax','%ebx','13','0xa3014314','15');
  200. round4_step( 0,'%ebx','%ecx','%edx','%eax', '4','0x4e0811a1','21');
  201. round4_step( 0,'%eax','%ebx','%ecx','%edx','11','0xf7537e82', '6');
  202. round4_step( 0,'%edx','%eax','%ebx','%ecx', '2','0xbd3af235','10');
  203. round4_step( 0,'%ecx','%edx','%eax','%ebx', '9','0x2ad7d2bb','15');
  204. round4_step( 1,'%ebx','%ecx','%edx','%eax', '0','0xeb86d391','21');
  205. $code .= <<EOF;
  206. # add old values of A, B, C, D
  207. add %r8d, %eax
  208. add %r9d, %ebx
  209. add %r14d, %ecx
  210. add %r15d, %edx
  211. # loop control
  212. add \$64, %rsi # ptr += 64
  213. cmp %rdi, %rsi # cmp end with ptr
  214. jb .Lloop # jmp if ptr < end
  215. # END of loop over 16-word blocks
  216. .Lend:
  217. mov %eax, 0*4(%rbp) # ctx->A = A
  218. mov %ebx, 1*4(%rbp) # ctx->B = B
  219. mov %ecx, 2*4(%rbp) # ctx->C = C
  220. mov %edx, 3*4(%rbp) # ctx->D = D
  221. pop %r15
  222. pop %r14
  223. pop %rbx
  224. pop %rbp
  225. ret
  226. .size md5_block_asm_data_order,.-md5_block_asm_data_order
  227. EOF
  228. print $code;
  229. close STDOUT;