sha1-mips.pl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. #! /usr/bin/env perl
  2. # Copyright 2009-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. # ====================================================================
  9. # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
  10. # project. The module is, however, dual licensed under OpenSSL and
  11. # CRYPTOGAMS licenses depending on where you obtain it. For further
  12. # details see http://www.openssl.org/~appro/cryptogams/.
  13. # ====================================================================
  14. # SHA1 block procedure for MIPS.
  15. # Performance improvement is 30% on unaligned input. The "secret" is
  16. # to deploy lwl/lwr pair to load unaligned input. One could have
  17. # vectorized Xupdate on MIPSIII/IV, but the goal was to code MIPS32-
  18. # compatible subroutine. There is room for minor optimization on
  19. # little-endian platforms...
  20. # September 2012.
  21. #
  22. # Add MIPS32r2 code (>25% less instructions).
  23. ######################################################################
  24. # There is a number of MIPS ABI in use, O32 and N32/64 are most
  25. # widely used. Then there is a new contender: NUBI. It appears that if
  26. # one picks the latter, it's possible to arrange code in ABI neutral
  27. # manner. Therefore let's stick to NUBI register layout:
  28. #
  29. ($zero,$at,$t0,$t1,$t2)=map("\$$_",(0..2,24,25));
  30. ($a0,$a1,$a2,$a3,$a4,$a5,$a6,$a7)=map("\$$_",(4..11));
  31. ($s0,$s1,$s2,$s3,$s4,$s5,$s6,$s7,$s8,$s9,$s10,$s11)=map("\$$_",(12..23));
  32. ($gp,$tp,$sp,$fp,$ra)=map("\$$_",(3,28..31));
  33. #
  34. # The return value is placed in $a0. Following coding rules facilitate
  35. # interoperability:
  36. #
  37. # - never ever touch $tp, "thread pointer", former $gp;
  38. # - copy return value to $t0, former $v0 [or to $a0 if you're adapting
  39. # old code];
  40. # - on O32 populate $a4-$a7 with 'lw $aN,4*N($sp)' if necessary;
  41. #
  42. # For reference here is register layout for N32/64 MIPS ABIs:
  43. #
  44. # ($zero,$at,$v0,$v1)=map("\$$_",(0..3));
  45. # ($a0,$a1,$a2,$a3,$a4,$a5,$a6,$a7)=map("\$$_",(4..11));
  46. # ($t0,$t1,$t2,$t3,$t8,$t9)=map("\$$_",(12..15,24,25));
  47. # ($s0,$s1,$s2,$s3,$s4,$s5,$s6,$s7)=map("\$$_",(16..23));
  48. # ($gp,$sp,$fp,$ra)=map("\$$_",(28..31));
  49. # $output is the last argument if it looks like a file (it has an extension)
  50. # $flavour is the first argument if it doesn't look like a file
  51. $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
  52. # supported flavours are o32,n32,64,nubi32,nubi64, default is o32
  53. $flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : "o32";
  54. if ($flavour =~ /64|n32/i) {
  55. $PTR_ADD="daddu"; # incidentally works even on n32
  56. $PTR_SUB="dsubu"; # incidentally works even on n32
  57. $REG_S="sd";
  58. $REG_L="ld";
  59. $PTR_SLL="dsll"; # incidentally works even on n32
  60. $SZREG=8;
  61. } else {
  62. $PTR_ADD="addu";
  63. $PTR_SUB="subu";
  64. $REG_S="sw";
  65. $REG_L="lw";
  66. $PTR_SLL="sll";
  67. $SZREG=4;
  68. }
  69. #
  70. # <appro@openssl.org>
  71. #
  72. ######################################################################
  73. $big_endian=(`echo MIPSEB | $ENV{CC} -E -`=~/MIPSEB/)?0:1 if ($ENV{CC});
  74. $output and open STDOUT,">$output";
  75. if (!defined($big_endian))
  76. { $big_endian=(unpack('L',pack('N',1))==1); }
  77. # offsets of the Most and Least Significant Bytes
  78. $MSB=$big_endian?0:3;
  79. $LSB=3&~$MSB;
  80. @X=map("\$$_",(8..23)); # a4-a7,s0-s11
  81. $ctx=$a0;
  82. $inp=$a1;
  83. $num=$a2;
  84. $A="\$1";
  85. $B="\$2";
  86. $C="\$3";
  87. $D="\$7";
  88. $E="\$24"; @V=($A,$B,$C,$D,$E);
  89. $t0="\$25";
  90. $t1=$num; # $num is offloaded to stack
  91. $t2="\$30"; # fp
  92. $K="\$31"; # ra
  93. sub BODY_00_14 {
  94. my ($i,$a,$b,$c,$d,$e)=@_;
  95. my $j=$i+1;
  96. $code.=<<___ if (!$big_endian);
  97. #if defined(_MIPS_ARCH_MIPS32R2) || defined(_MIPS_ARCH_MIPS64R2)
  98. wsbh @X[$i],@X[$i] # byte swap($i)
  99. rotr @X[$i],@X[$i],16
  100. #else
  101. srl $t0,@X[$i],24 # byte swap($i)
  102. srl $t1,@X[$i],8
  103. andi $t2,@X[$i],0xFF00
  104. sll @X[$i],@X[$i],24
  105. andi $t1,0xFF00
  106. sll $t2,$t2,8
  107. or @X[$i],$t0
  108. or $t1,$t2
  109. or @X[$i],$t1
  110. #endif
  111. ___
  112. $code.=<<___;
  113. #if defined(_MIPS_ARCH_MIPS32R2) || defined(_MIPS_ARCH_MIPS64R2)
  114. addu $e,$K # $i
  115. xor $t0,$c,$d
  116. rotr $t1,$a,27
  117. and $t0,$b
  118. addu $e,$t1
  119. #if defined(_MIPS_ARCH_MIPS32R6) || defined(_MIPS_ARCH_MIPS64R6)
  120. lw @X[$j],$j*4($inp)
  121. #else
  122. lwl @X[$j],$j*4+$MSB($inp)
  123. lwr @X[$j],$j*4+$LSB($inp)
  124. #endif
  125. xor $t0,$d
  126. addu $e,@X[$i]
  127. rotr $b,$b,2
  128. addu $e,$t0
  129. #else
  130. lwl @X[$j],$j*4+$MSB($inp)
  131. sll $t0,$a,5 # $i
  132. addu $e,$K
  133. lwr @X[$j],$j*4+$LSB($inp)
  134. srl $t1,$a,27
  135. addu $e,$t0
  136. xor $t0,$c,$d
  137. addu $e,$t1
  138. sll $t2,$b,30
  139. and $t0,$b
  140. srl $b,$b,2
  141. xor $t0,$d
  142. addu $e,@X[$i]
  143. or $b,$t2
  144. addu $e,$t0
  145. #endif
  146. ___
  147. }
  148. sub BODY_15_19 {
  149. my ($i,$a,$b,$c,$d,$e)=@_;
  150. my $j=$i+1;
  151. $code.=<<___ if (!$big_endian && $i==15);
  152. #if defined(_MIPS_ARCH_MIPS32R2) || defined(_MIPS_ARCH_MIPS64R2)
  153. wsbh @X[$i],@X[$i] # byte swap($i)
  154. rotr @X[$i],@X[$i],16
  155. #else
  156. srl $t0,@X[$i],24 # byte swap($i)
  157. srl $t1,@X[$i],8
  158. andi $t2,@X[$i],0xFF00
  159. sll @X[$i],@X[$i],24
  160. andi $t1,0xFF00
  161. sll $t2,$t2,8
  162. or @X[$i],$t0
  163. or @X[$i],$t1
  164. or @X[$i],$t2
  165. #endif
  166. ___
  167. $code.=<<___;
  168. #if defined(_MIPS_ARCH_MIPS32R2) || defined(_MIPS_ARCH_MIPS64R2)
  169. addu $e,$K # $i
  170. xor @X[$j%16],@X[($j+2)%16]
  171. xor $t0,$c,$d
  172. rotr $t1,$a,27
  173. xor @X[$j%16],@X[($j+8)%16]
  174. and $t0,$b
  175. addu $e,$t1
  176. xor @X[$j%16],@X[($j+13)%16]
  177. xor $t0,$d
  178. addu $e,@X[$i%16]
  179. rotr @X[$j%16],@X[$j%16],31
  180. rotr $b,$b,2
  181. addu $e,$t0
  182. #else
  183. xor @X[$j%16],@X[($j+2)%16]
  184. sll $t0,$a,5 # $i
  185. addu $e,$K
  186. srl $t1,$a,27
  187. addu $e,$t0
  188. xor @X[$j%16],@X[($j+8)%16]
  189. xor $t0,$c,$d
  190. addu $e,$t1
  191. xor @X[$j%16],@X[($j+13)%16]
  192. sll $t2,$b,30
  193. and $t0,$b
  194. srl $t1,@X[$j%16],31
  195. addu @X[$j%16],@X[$j%16]
  196. srl $b,$b,2
  197. xor $t0,$d
  198. or @X[$j%16],$t1
  199. addu $e,@X[$i%16]
  200. or $b,$t2
  201. addu $e,$t0
  202. #endif
  203. ___
  204. }
  205. sub BODY_20_39 {
  206. my ($i,$a,$b,$c,$d,$e)=@_;
  207. my $j=$i+1;
  208. $code.=<<___ if ($i<79);
  209. #if defined(_MIPS_ARCH_MIPS32R2) || defined(_MIPS_ARCH_MIPS64R2)
  210. xor @X[$j%16],@X[($j+2)%16]
  211. addu $e,$K # $i
  212. rotr $t1,$a,27
  213. xor @X[$j%16],@X[($j+8)%16]
  214. xor $t0,$c,$d
  215. addu $e,$t1
  216. xor @X[$j%16],@X[($j+13)%16]
  217. xor $t0,$b
  218. addu $e,@X[$i%16]
  219. rotr @X[$j%16],@X[$j%16],31
  220. rotr $b,$b,2
  221. addu $e,$t0
  222. #else
  223. xor @X[$j%16],@X[($j+2)%16]
  224. sll $t0,$a,5 # $i
  225. addu $e,$K
  226. srl $t1,$a,27
  227. addu $e,$t0
  228. xor @X[$j%16],@X[($j+8)%16]
  229. xor $t0,$c,$d
  230. addu $e,$t1
  231. xor @X[$j%16],@X[($j+13)%16]
  232. sll $t2,$b,30
  233. xor $t0,$b
  234. srl $t1,@X[$j%16],31
  235. addu @X[$j%16],@X[$j%16]
  236. srl $b,$b,2
  237. addu $e,@X[$i%16]
  238. or @X[$j%16],$t1
  239. or $b,$t2
  240. addu $e,$t0
  241. #endif
  242. ___
  243. $code.=<<___ if ($i==79);
  244. #if defined(_MIPS_ARCH_MIPS32R2) || defined(_MIPS_ARCH_MIPS64R2)
  245. lw @X[0],0($ctx)
  246. addu $e,$K # $i
  247. lw @X[1],4($ctx)
  248. rotr $t1,$a,27
  249. lw @X[2],8($ctx)
  250. xor $t0,$c,$d
  251. addu $e,$t1
  252. lw @X[3],12($ctx)
  253. xor $t0,$b
  254. addu $e,@X[$i%16]
  255. lw @X[4],16($ctx)
  256. rotr $b,$b,2
  257. addu $e,$t0
  258. #else
  259. lw @X[0],0($ctx)
  260. sll $t0,$a,5 # $i
  261. addu $e,$K
  262. lw @X[1],4($ctx)
  263. srl $t1,$a,27
  264. addu $e,$t0
  265. lw @X[2],8($ctx)
  266. xor $t0,$c,$d
  267. addu $e,$t1
  268. lw @X[3],12($ctx)
  269. sll $t2,$b,30
  270. xor $t0,$b
  271. lw @X[4],16($ctx)
  272. srl $b,$b,2
  273. addu $e,@X[$i%16]
  274. or $b,$t2
  275. addu $e,$t0
  276. #endif
  277. ___
  278. }
  279. sub BODY_40_59 {
  280. my ($i,$a,$b,$c,$d,$e)=@_;
  281. my $j=$i+1;
  282. $code.=<<___ if ($i<79);
  283. #if defined(_MIPS_ARCH_MIPS32R2) || defined(_MIPS_ARCH_MIPS64R2)
  284. addu $e,$K # $i
  285. and $t0,$c,$d
  286. xor @X[$j%16],@X[($j+2)%16]
  287. rotr $t1,$a,27
  288. addu $e,$t0
  289. xor @X[$j%16],@X[($j+8)%16]
  290. xor $t0,$c,$d
  291. addu $e,$t1
  292. xor @X[$j%16],@X[($j+13)%16]
  293. and $t0,$b
  294. addu $e,@X[$i%16]
  295. rotr @X[$j%16],@X[$j%16],31
  296. rotr $b,$b,2
  297. addu $e,$t0
  298. #else
  299. xor @X[$j%16],@X[($j+2)%16]
  300. sll $t0,$a,5 # $i
  301. addu $e,$K
  302. srl $t1,$a,27
  303. addu $e,$t0
  304. xor @X[$j%16],@X[($j+8)%16]
  305. and $t0,$c,$d
  306. addu $e,$t1
  307. xor @X[$j%16],@X[($j+13)%16]
  308. sll $t2,$b,30
  309. addu $e,$t0
  310. srl $t1,@X[$j%16],31
  311. xor $t0,$c,$d
  312. addu @X[$j%16],@X[$j%16]
  313. and $t0,$b
  314. srl $b,$b,2
  315. or @X[$j%16],$t1
  316. addu $e,@X[$i%16]
  317. or $b,$t2
  318. addu $e,$t0
  319. #endif
  320. ___
  321. }
  322. $FRAMESIZE=16; # large enough to accommodate NUBI saved registers
  323. $SAVED_REGS_MASK = ($flavour =~ /nubi/i) ? "0xc0fff008" : "0xc0ff0000";
  324. $code=<<___;
  325. #include "mips_arch.h"
  326. .text
  327. .set noat
  328. .set noreorder
  329. .align 5
  330. .globl sha1_block_data_order
  331. .ent sha1_block_data_order
  332. sha1_block_data_order:
  333. .frame $sp,$FRAMESIZE*$SZREG,$ra
  334. .mask $SAVED_REGS_MASK,-$SZREG
  335. .set noreorder
  336. $PTR_SUB $sp,$FRAMESIZE*$SZREG
  337. $REG_S $ra,($FRAMESIZE-1)*$SZREG($sp)
  338. $REG_S $fp,($FRAMESIZE-2)*$SZREG($sp)
  339. $REG_S $s11,($FRAMESIZE-3)*$SZREG($sp)
  340. $REG_S $s10,($FRAMESIZE-4)*$SZREG($sp)
  341. $REG_S $s9,($FRAMESIZE-5)*$SZREG($sp)
  342. $REG_S $s8,($FRAMESIZE-6)*$SZREG($sp)
  343. $REG_S $s7,($FRAMESIZE-7)*$SZREG($sp)
  344. $REG_S $s6,($FRAMESIZE-8)*$SZREG($sp)
  345. $REG_S $s5,($FRAMESIZE-9)*$SZREG($sp)
  346. $REG_S $s4,($FRAMESIZE-10)*$SZREG($sp)
  347. ___
  348. $code.=<<___ if ($flavour =~ /nubi/i); # optimize non-nubi prologue
  349. $REG_S $s3,($FRAMESIZE-11)*$SZREG($sp)
  350. $REG_S $s2,($FRAMESIZE-12)*$SZREG($sp)
  351. $REG_S $s1,($FRAMESIZE-13)*$SZREG($sp)
  352. $REG_S $s0,($FRAMESIZE-14)*$SZREG($sp)
  353. $REG_S $gp,($FRAMESIZE-15)*$SZREG($sp)
  354. ___
  355. $code.=<<___;
  356. $PTR_SLL $num,6
  357. $PTR_ADD $num,$inp
  358. $REG_S $num,0($sp)
  359. lw $A,0($ctx)
  360. lw $B,4($ctx)
  361. lw $C,8($ctx)
  362. lw $D,12($ctx)
  363. b .Loop
  364. lw $E,16($ctx)
  365. .align 4
  366. .Loop:
  367. .set reorder
  368. #if defined(_MIPS_ARCH_MIPS32R6) || defined(_MIPS_ARCH_MIPS64R6)
  369. lui $K,0x5a82
  370. lw @X[0],($inp)
  371. ori $K,0x7999 # K_00_19
  372. #else
  373. lwl @X[0],$MSB($inp)
  374. lui $K,0x5a82
  375. lwr @X[0],$LSB($inp)
  376. ori $K,0x7999 # K_00_19
  377. #endif
  378. ___
  379. for ($i=0;$i<15;$i++) { &BODY_00_14($i,@V); unshift(@V,pop(@V)); }
  380. for (;$i<20;$i++) { &BODY_15_19($i,@V); unshift(@V,pop(@V)); }
  381. $code.=<<___;
  382. lui $K,0x6ed9
  383. ori $K,0xeba1 # K_20_39
  384. ___
  385. for (;$i<40;$i++) { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
  386. $code.=<<___;
  387. lui $K,0x8f1b
  388. ori $K,0xbcdc # K_40_59
  389. ___
  390. for (;$i<60;$i++) { &BODY_40_59($i,@V); unshift(@V,pop(@V)); }
  391. $code.=<<___;
  392. lui $K,0xca62
  393. ori $K,0xc1d6 # K_60_79
  394. ___
  395. for (;$i<80;$i++) { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
  396. $code.=<<___;
  397. $PTR_ADD $inp,64
  398. $REG_L $num,0($sp)
  399. addu $A,$X[0]
  400. addu $B,$X[1]
  401. sw $A,0($ctx)
  402. addu $C,$X[2]
  403. addu $D,$X[3]
  404. sw $B,4($ctx)
  405. addu $E,$X[4]
  406. sw $C,8($ctx)
  407. sw $D,12($ctx)
  408. sw $E,16($ctx)
  409. .set noreorder
  410. bne $inp,$num,.Loop
  411. nop
  412. .set noreorder
  413. $REG_L $ra,($FRAMESIZE-1)*$SZREG($sp)
  414. $REG_L $fp,($FRAMESIZE-2)*$SZREG($sp)
  415. $REG_L $s11,($FRAMESIZE-3)*$SZREG($sp)
  416. $REG_L $s10,($FRAMESIZE-4)*$SZREG($sp)
  417. $REG_L $s9,($FRAMESIZE-5)*$SZREG($sp)
  418. $REG_L $s8,($FRAMESIZE-6)*$SZREG($sp)
  419. $REG_L $s7,($FRAMESIZE-7)*$SZREG($sp)
  420. $REG_L $s6,($FRAMESIZE-8)*$SZREG($sp)
  421. $REG_L $s5,($FRAMESIZE-9)*$SZREG($sp)
  422. $REG_L $s4,($FRAMESIZE-10)*$SZREG($sp)
  423. ___
  424. $code.=<<___ if ($flavour =~ /nubi/i);
  425. $REG_L $s3,($FRAMESIZE-11)*$SZREG($sp)
  426. $REG_L $s2,($FRAMESIZE-12)*$SZREG($sp)
  427. $REG_L $s1,($FRAMESIZE-13)*$SZREG($sp)
  428. $REG_L $s0,($FRAMESIZE-14)*$SZREG($sp)
  429. $REG_L $gp,($FRAMESIZE-15)*$SZREG($sp)
  430. ___
  431. $code.=<<___;
  432. jr $ra
  433. $PTR_ADD $sp,$FRAMESIZE*$SZREG
  434. .end sha1_block_data_order
  435. .rdata
  436. .asciiz "SHA1 for MIPS, CRYPTOGAMS by <appro\@openssl.org>"
  437. ___
  438. print $code;
  439. close STDOUT or die "error closing STDOUT: $!";