md5-loongarch64.pl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #! /usr/bin/env perl
  2. # Author: Min Zhou <zhoumin@loongson.cn>
  3. # Copyright 2023 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. # Reference to crypto/md5/asm/md5-x86_64.pl
  10. # MD5 optimized for LoongArch.
  11. use strict;
  12. my $code;
  13. my ($zero,$ra,$tp,$sp,$fp)=map("\$r$_",(0..3,22));
  14. my ($a0,$a1,$a2,$a3,$a4,$a5,$a6,$a7)=map("\$r$_",(4..11));
  15. my ($t0,$t1,$t2,$t3,$t4,$t5,$t6,$t7,$t8,$x)=map("\$r$_",(12..21));
  16. my $output;
  17. for (@ARGV) { $output=$_ if (/\w[\w\-]*\.\w+$/); }
  18. open STDOUT,">$output";
  19. # round1_step() does:
  20. # dst = x + ((dst + F(x,y,z) + X[k] + T_i) <<< s)
  21. # $t1 = y ^ z
  22. # $t2 = dst + X[k_next]
  23. sub round1_step
  24. {
  25. my ($pos, $dst, $x, $y, $z, $k_next, $T_i, $s) = @_;
  26. my $T_i_h = ($T_i & 0xfffff000) >> 12;
  27. my $T_i_l = $T_i & 0xfff;
  28. # In LoongArch we have to use two instructions of lu12i.w and ori to load a
  29. # 32-bit immediate into a general register. Meanwhile, the instruction lu12i.w
  30. # treats the 20-bit immediate as a signed number. So if the T_i_h is greater
  31. # than or equal to (1<<19), we need provide lu12i.w a corresponding negative
  32. # number whose complement equals to the sign extension of T_i_h.
  33. # The details of the instruction lu12i.w can be found as following:
  34. # https://loongson.github.io/LoongArch-Documentation/LoongArch-Vol1-EN.html#_lu12i_w_lu32i_d_lu52i_d
  35. $T_i_h = -((1<<32) - (0xfff00000 | $T_i_h)) if ($T_i_h >= (1<<19));
  36. $code .= " ld.w $t0,$a1,0 /* (NEXT STEP) X[0] */\n" if ($pos == -1);
  37. $code .= " xor $t1,$y,$z /* y ^ z */\n" if ($pos == -1);
  38. $code .= " add.w $t2,$dst,$t0 /* dst + X[k] */\n" if ($pos == -1);
  39. $code .= <<EOF;
  40. lu12i.w $t8,$T_i_h /* load bits [31:12] of constant */
  41. and $t1,$x,$t1 /* x & ... */
  42. ori $t8,$t8,$T_i_l /* load bits [11:0] of constant */
  43. xor $t1,$z,$t1 /* z ^ ... */
  44. add.w $t7,$t2,$t8 /* dst + X[k] + Const */
  45. ld.w $t0,$a1,$k_next*4 /* (NEXT STEP) X[$k_next] */
  46. add.w $dst,$t7,$t1 /* dst += ... */
  47. add.w $t2,$z,$t0 /* (NEXT STEP) dst + X[$k_next] */
  48. EOF
  49. $code .= " rotri.w $dst,$dst,32-$s /* dst <<< s */\n";
  50. if ($pos != 1) {
  51. $code .= " xor $t1,$x,$y /* (NEXT STEP) y ^ z */\n";
  52. } else {
  53. $code .= " move $t0,$a7 /* (NEXT ROUND) $t0 = z' (copy of z) */\n";
  54. $code .= " nor $t1,$zero,$a7 /* (NEXT ROUND) $t1 = not z' (copy of not z) */\n";
  55. }
  56. $code .= " add.w $dst,$dst,$x /* dst += x */\n";
  57. }
  58. # round2_step() does:
  59. # dst = x + ((dst + G(x,y,z) + X[k] + T_i) <<< s)
  60. # $t0 = z' (copy of z for the next step)
  61. # $t1 = not z' (copy of not z for the next step)
  62. # $t2 = dst + X[k_next]
  63. sub round2_step
  64. {
  65. my ($pos, $dst, $x, $y, $z, $k_next, $T_i, $s) = @_;
  66. my $T_i_h = ($T_i & 0xfffff000) >> 12;
  67. my $T_i_l = $T_i & 0xfff;
  68. $T_i_h = -((1<<32) - (0xfff00000 | $T_i_h)) if ($T_i_h >= (1<<19));
  69. $code .= <<EOF;
  70. lu12i.w $t8,$T_i_h /* load bits [31:12] of Constant */
  71. and $t0,$x,$t0 /* x & z */
  72. ori $t8,$t8,$T_i_l /* load bits [11:0] of Constant */
  73. and $t1,$y,$t1 /* y & (not z) */
  74. add.w $t7,$t2,$t8 /* dst + X[k] + Const */
  75. or $t1,$t0,$t1 /* (y & (not z)) | (x & z) */
  76. ld.w $t0,$a1,$k_next*4 /* (NEXT STEP) X[$k_next] */
  77. add.w $dst,$t7,$t1 /* dst += ... */
  78. add.w $t2,$z,$t0 /* (NEXT STEP) dst + X[$k_next] */
  79. EOF
  80. $code .= " rotri.w $dst,$dst,32-$s /* dst <<< s */\n";
  81. if ($pos != 1) {
  82. $code .= " move $t0,$y /* (NEXT STEP) z' = $y */\n";
  83. $code .= " nor $t1,$zero,$y /* (NEXT STEP) not z' = not $y */\n";
  84. } else {
  85. $code .= " xor $t1,$a6,$a7 /* (NEXT ROUND) $t1 = y ^ z */\n";
  86. }
  87. $code .= " add.w $dst,$dst,$x /* dst += x */\n";
  88. }
  89. # round3_step() does:
  90. # dst = x + ((dst + H(x,y,z) + X[k] + T_i) <<< s)
  91. # $t1 = y ^ z
  92. # $t2 = dst + X[k_next]
  93. sub round3_step
  94. {
  95. my ($pos, $dst, $x, $y, $z, $k_next, $T_i, $s) = @_;
  96. my $T_i_h = ($T_i & 0xfffff000) >> 12;
  97. my $T_i_l = $T_i & 0xfff;
  98. $T_i_h = -((1<<32) - (0xfff00000 | $T_i_h)) if ($T_i_h >= (1<<19));
  99. $code .= <<EOF;
  100. lu12i.w $t8,$T_i_h /* load bits [31:12] of Constant */
  101. xor $t1,$x,$t1 /* x ^ ... */
  102. ori $t8,$t8,$T_i_l /* load bits [11:0] of Constant */
  103. add.w $t7,$t2,$t8 /* dst + X[k] + Const */
  104. ld.w $t0,$a1,$k_next*4 /* (NEXT STEP) X[$k_next] */
  105. add.w $dst,$t7,$t1 /* dst += ... */
  106. add.w $t2,$z,$t0 /* (NEXT STEP) dst + X[$k_next] */
  107. EOF
  108. $code .= " rotri.w $dst,$dst,32-$s /* dst <<< s */\n";
  109. if ($pos != 1) {
  110. $code .= " xor $t1,$x,$y /* (NEXT STEP) y ^ z */\n";
  111. } else {
  112. $code .= " nor $t1,$zero,$a7 /* (NEXT ROUND) $t1 = not z */\n";
  113. }
  114. $code .= " add.w $dst,$dst,$x /* dst += x */\n";
  115. }
  116. # round4_step() does:
  117. # dst = x + ((dst + I(x,y,z) + X[k] + T_i) <<< s)
  118. # $t1 = not z' (copy of not z for the next step)
  119. # $t2 = dst + X[k_next]
  120. sub round4_step
  121. {
  122. my ($pos, $dst, $x, $y, $z, $k_next, $T_i, $s) = @_;
  123. my $T_i_h = ($T_i & 0xfffff000) >> 12;
  124. my $T_i_l = $T_i & 0xfff;
  125. $T_i_h = -((1<<32) - (0xfff00000 | $T_i_h)) if ($T_i_h >= (1<<19));
  126. $code .= <<EOF;
  127. lu12i.w $t8,$T_i_h /* load bits [31:12] of Constant */
  128. or $t1,$x,$t1 /* x | ... */
  129. ori $t8,$t8,$T_i_l /* load bits [11:0] of Constant */
  130. xor $t1,$y,$t1 /* y ^ ... */
  131. add.w $t7,$t2,$t8 /* dst + X[k] + Const */
  132. EOF
  133. if ($pos != 1) {
  134. $code .= " ld.w $t0,$a1,$k_next*4 /* (NEXT STEP) X[$k_next] */\n";
  135. $code .= " add.w $dst,$t7,$t1 /* dst += ... */\n";
  136. $code .= " add.w $t2,$z,$t0 /* (NEXT STEP) dst + X[$k_next] */\n";
  137. $code .= " rotri.w $dst,$dst,32-$s /* dst <<< s */\n";
  138. $code .= " nor $t1,$zero,$y /* (NEXT STEP) not z' = not $y */\n";
  139. $code .= " add.w $dst,$dst,$x /* dst += x */\n";
  140. } else {
  141. $code .= " add.w $a4,$t3,$a4 /* (NEXT LOOP) add old value of A */\n";
  142. $code .= " add.w $dst,$t7,$t1 /* dst += ... */\n";
  143. $code .= " add.w $a7,$t6,$a7 /* (NEXT LOOP) add old value of D */\n";
  144. $code .= " rotri.w $dst,$dst,32-$s /* dst <<< s */\n";
  145. $code .= " addi.d $a1,$a1,64 /* (NEXT LOOP) ptr += 64 */\n";
  146. $code .= " add.w $dst,$dst,$x /* dst += x */\n";
  147. }
  148. }
  149. $code .= <<EOF;
  150. .text
  151. .globl ossl_md5_block_asm_data_order
  152. .type ossl_md5_block_asm_data_order function
  153. ossl_md5_block_asm_data_order:
  154. # $a0 = arg #1 (ctx, MD5_CTX pointer)
  155. # $a1 = arg #2 (ptr, data pointer)
  156. # $a2 = arg #3 (nbr, number of 16-word blocks to process)
  157. beqz $a2,.Lend # cmp nbr with 0, jmp if nbr == 0
  158. # ptr is '$a1'
  159. # end is '$a3'
  160. slli.d $t0,$a2,6
  161. add.d $a3,$a1,$t0
  162. # A is '$a4'
  163. # B is '$a5'
  164. # C is '$a6'
  165. # D is '$a7'
  166. ld.w $a4,$a0,0 # a4 = ctx->A
  167. ld.w $a5,$a0,4 # a5 = ctx->B
  168. ld.w $a6,$a0,8 # a6 = ctx->C
  169. ld.w $a7,$a0,12 # a7 = ctx->D
  170. # BEGIN of loop over 16-word blocks
  171. .align 6
  172. .Lloop:
  173. # save old values of A, B, C, D
  174. move $t3,$a4
  175. move $t4,$a5
  176. move $t5,$a6
  177. move $t6,$a7
  178. preld 0,$a1,0
  179. preld 0,$a1,64
  180. EOF
  181. round1_step(-1, $a4, $a5, $a6, $a7, '1', 0xd76aa478, '7');
  182. round1_step(0, $a7, $a4, $a5, $a6, '2', 0xe8c7b756, '12');
  183. round1_step(0, $a6, $a7, $a4, $a5, '3', 0x242070db, '17');
  184. round1_step(0, $a5, $a6, $a7, $a4, '4', 0xc1bdceee, '22');
  185. round1_step(0, $a4, $a5, $a6, $a7, '5', 0xf57c0faf, '7');
  186. round1_step(0, $a7, $a4, $a5, $a6, '6', 0x4787c62a, '12');
  187. round1_step(0, $a6, $a7, $a4, $a5, '7', 0xa8304613, '17');
  188. round1_step(0, $a5, $a6, $a7, $a4, '8', 0xfd469501, '22');
  189. round1_step(0, $a4, $a5, $a6, $a7, '9', 0x698098d8, '7');
  190. round1_step(0, $a7, $a4, $a5, $a6, '10', 0x8b44f7af, '12');
  191. round1_step(0, $a6, $a7, $a4, $a5, '11', 0xffff5bb1, '17');
  192. round1_step(0, $a5, $a6, $a7, $a4, '12', 0x895cd7be, '22');
  193. round1_step(0, $a4, $a5, $a6, $a7, '13', 0x6b901122, '7');
  194. round1_step(0, $a7, $a4, $a5, $a6, '14', 0xfd987193, '12');
  195. round1_step(0, $a6, $a7, $a4, $a5, '15', 0xa679438e, '17');
  196. round1_step(1, $a5, $a6, $a7, $a4, '1', 0x49b40821, '22');
  197. round2_step(-1, $a4, $a5, $a6, $a7, '6', 0xf61e2562, '5');
  198. round2_step(0, $a7, $a4, $a5, $a6, '11', 0xc040b340, '9');
  199. round2_step(0, $a6, $a7, $a4, $a5, '0', 0x265e5a51, '14');
  200. round2_step(0, $a5, $a6, $a7, $a4, '5', 0xe9b6c7aa, '20');
  201. round2_step(0, $a4, $a5, $a6, $a7, '10', 0xd62f105d, '5');
  202. round2_step(0, $a7, $a4, $a5, $a6, '15', 0x2441453, '9');
  203. round2_step(0, $a6, $a7, $a4, $a5, '4', 0xd8a1e681, '14');
  204. round2_step(0, $a5, $a6, $a7, $a4, '9', 0xe7d3fbc8, '20');
  205. round2_step(0, $a4, $a5, $a6, $a7, '14', 0x21e1cde6, '5');
  206. round2_step(0, $a7, $a4, $a5, $a6, '3', 0xc33707d6, '9');
  207. round2_step(0, $a6, $a7, $a4, $a5, '8', 0xf4d50d87, '14');
  208. round2_step(0, $a5, $a6, $a7, $a4, '13', 0x455a14ed, '20');
  209. round2_step(0, $a4, $a5, $a6, $a7, '2', 0xa9e3e905, '5');
  210. round2_step(0, $a7, $a4, $a5, $a6, '7', 0xfcefa3f8, '9');
  211. round2_step(0, $a6, $a7, $a4, $a5, '12', 0x676f02d9, '14');
  212. round2_step(1, $a5, $a6, $a7, $a4, '5', 0x8d2a4c8a, '20');
  213. round3_step(-1, $a4, $a5, $a6, $a7, '8', 0xfffa3942, '4');
  214. round3_step(0, $a7, $a4, $a5, $a6, '11', 0x8771f681, '11');
  215. round3_step(0, $a6, $a7, $a4, $a5, '14', 0x6d9d6122, '16');
  216. round3_step(0, $a5, $a6, $a7, $a4, '1', 0xfde5380c, '23');
  217. round3_step(0, $a4, $a5, $a6, $a7, '4', 0xa4beea44, '4');
  218. round3_step(0, $a7, $a4, $a5, $a6, '7', 0x4bdecfa9, '11');
  219. round3_step(0, $a6, $a7, $a4, $a5, '10', 0xf6bb4b60, '16');
  220. round3_step(0, $a5, $a6, $a7, $a4, '13', 0xbebfbc70, '23');
  221. round3_step(0, $a4, $a5, $a6, $a7, '0', 0x289b7ec6, '4');
  222. round3_step(0, $a7, $a4, $a5, $a6, '3', 0xeaa127fa, '11');
  223. round3_step(0, $a6, $a7, $a4, $a5, '6', 0xd4ef3085, '16');
  224. round3_step(0, $a5, $a6, $a7, $a4, '9', 0x4881d05, '23');
  225. round3_step(0, $a4, $a5, $a6, $a7, '12', 0xd9d4d039, '4');
  226. round3_step(0, $a7, $a4, $a5, $a6, '15', 0xe6db99e5, '11');
  227. round3_step(0, $a6, $a7, $a4, $a5, '2', 0x1fa27cf8, '16');
  228. round3_step(1, $a5, $a6, $a7, $a4, '0', 0xc4ac5665, '23');
  229. round4_step(-1, $a4, $a5, $a6, $a7, '7', 0xf4292244, '6');
  230. round4_step(0, $a7, $a4, $a5, $a6, '14', 0x432aff97, '10');
  231. round4_step(0, $a6, $a7, $a4, $a5, '5', 0xab9423a7, '15');
  232. round4_step(0, $a5, $a6, $a7, $a4, '12', 0xfc93a039, '21');
  233. round4_step(0, $a4, $a5, $a6, $a7, '3', 0x655b59c3, '6');
  234. round4_step(0, $a7, $a4, $a5, $a6, '10', 0x8f0ccc92, '10');
  235. round4_step(0, $a6, $a7, $a4, $a5, '1', 0xffeff47d, '15');
  236. round4_step(0, $a5, $a6, $a7, $a4, '8', 0x85845dd1, '21');
  237. round4_step(0, $a4, $a5, $a6, $a7, '15', 0x6fa87e4f, '6');
  238. round4_step(0, $a7, $a4, $a5, $a6, '6', 0xfe2ce6e0, '10');
  239. round4_step(0, $a6, $a7, $a4, $a5, '13', 0xa3014314, '15');
  240. round4_step(0, $a5, $a6, $a7, $a4, '4', 0x4e0811a1, '21');
  241. round4_step(0, $a4, $a5, $a6, $a7, '11', 0xf7537e82, '6');
  242. round4_step(0, $a7, $a4, $a5, $a6, '2', 0xbd3af235, '10');
  243. round4_step(0, $a6, $a7, $a4, $a5, '9', 0x2ad7d2bb, '15');
  244. round4_step(1, $a5, $a6, $a7, $a4, '0', 0xeb86d391, '21');
  245. $code .= <<EOF;
  246. # add old values of B, C
  247. add.w $a5,$t4,$a5
  248. add.w $a6,$t5,$a6
  249. bltu $a1,$a3,.Lloop # jmp if ptr < end
  250. st.w $a4,$a0,0 # ctx->A = A
  251. st.w $a5,$a0,4 # ctx->B = B
  252. st.w $a6,$a0,8 # ctx->C = C
  253. st.w $a7,$a0,12 # ctx->D = D
  254. .Lend:
  255. jr $ra
  256. .size ossl_md5_block_asm_data_order,.-ossl_md5_block_asm_data_order
  257. EOF
  258. $code =~ s/\`([^\`]*)\`/eval($1)/gem;
  259. print $code;
  260. close STDOUT;