sha1-c64xplus.pl 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. #! /usr/bin/env perl
  2. # Copyright 2012-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. # ====================================================================
  10. # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
  11. # project. The module is, however, dual licensed under OpenSSL and
  12. # CRYPTOGAMS licenses depending on where you obtain it. For further
  13. # details see http://www.openssl.org/~appro/cryptogams/.
  14. # ====================================================================
  15. #
  16. # SHA1 for C64x+.
  17. #
  18. # November 2011
  19. #
  20. # If compared to compiler-generated code with similar characteristics,
  21. # i.e. compiled with OPENSSL_SMALL_FOOTPRINT and utilizing SPLOOPs,
  22. # this implementation is 25% smaller and >2x faster. In absolute terms
  23. # performance is (quite impressive) ~6.5 cycles per processed byte.
  24. # Fully unrolled assembler would be ~5x larger and is likely to be
  25. # ~15% faster. It would be free from references to intermediate ring
  26. # buffer, but put more pressure on L1P [both because the code would be
  27. # larger and won't be using SPLOOP buffer]. There are no plans to
  28. # realize fully unrolled variant though...
  29. #
  30. # !!! Note that this module uses AMR, which means that all interrupt
  31. # service routines are expected to preserve it and for own well-being
  32. # zero it upon entry.
  33. $output = pop and open STDOUT,">$output";
  34. ($CTX,$INP,$NUM) = ("A4","B4","A6"); # arguments
  35. ($A,$B,$C,$D,$E, $Arot,$F,$F0,$T,$K) = map("A$_",(16..20, 21..25));
  36. ($X0,$X2,$X8,$X13) = ("A26","B26","A27","B27");
  37. ($TX0,$TX1,$TX2,$TX3) = map("B$_",(28..31));
  38. ($XPA,$XPB) = ("A5","B5"); # X circular buffer
  39. ($Actx,$Bctx,$Cctx,$Dctx,$Ectx) = map("A$_",(3,6..9)); # zaps $NUM
  40. $code=<<___;
  41. .text
  42. .if .ASSEMBLER_VERSION<7000000
  43. .asg 0,__TI_EABI__
  44. .endif
  45. .if __TI_EABI__
  46. .asg sha1_block_data_order,_sha1_block_data_order
  47. .endif
  48. .asg B3,RA
  49. .asg A15,FP
  50. .asg B15,SP
  51. .if .BIG_ENDIAN
  52. .asg MV,SWAP2
  53. .asg MV,SWAP4
  54. .endif
  55. .global _sha1_block_data_order
  56. _sha1_block_data_order:
  57. .asmfunc stack_usage(64)
  58. MV $NUM,A0 ; reassign $NUM
  59. || MVK -64,B0
  60. [!A0] BNOP RA ; if ($NUM==0) return;
  61. || [A0] STW FP,*SP--[16] ; save frame pointer and alloca(64)
  62. || [A0] MV SP,FP
  63. [A0] LDW *${CTX}[0],$A ; load A-E...
  64. || [A0] AND B0,SP,SP ; align stack at 64 bytes
  65. [A0] LDW *${CTX}[1],$B
  66. || [A0] SUBAW SP,2,SP ; reserve two words above buffer
  67. [A0] LDW *${CTX}[2],$C
  68. || [A0] MVK 0x00404,B0
  69. [A0] LDW *${CTX}[3],$D
  70. || [A0] MVKH 0x50000,B0 ; 0x050404, 64 bytes for $XP[AB]
  71. [A0] LDW *${CTX}[4],$E
  72. || [A0] MVC B0,AMR ; setup circular addressing
  73. LDNW *${INP}++,$TX1 ; pre-fetch input
  74. NOP 1
  75. loop?:
  76. MVK 0x00007999,$K
  77. || ADDAW SP,2,$XPA
  78. || SUB A0,1,A0
  79. || MVK 13,B0
  80. MVKH 0x5a820000,$K ; K_00_19
  81. || ADDAW SP,2,$XPB
  82. || MV $A,$Actx
  83. || MV $B,$Bctx
  84. ;;==================================================
  85. SPLOOPD 5 ; BODY_00_13
  86. || MV $C,$Cctx
  87. || MV $D,$Dctx
  88. || MV $E,$Ectx
  89. || MVC B0,ILC
  90. ROTL $A,5,$Arot
  91. || AND $C,$B,$F
  92. || ANDN $D,$B,$F0
  93. || ADD $K,$E,$T ; T=E+K
  94. XOR $F0,$F,$F ; F_00_19(B,C,D)
  95. || MV $D,$E ; E=D
  96. || MV $C,$D ; D=C
  97. || SWAP2 $TX1,$TX2
  98. || LDNW *${INP}++,$TX1
  99. ADD $F,$T,$T ; T+=F_00_19(B,C,D)
  100. || ROTL $B,30,$C ; C=ROL(B,30)
  101. || SWAP4 $TX2,$TX3 ; byte swap
  102. ADD $Arot,$T,$T ; T+=ROL(A,5)
  103. || MV $A,$B ; B=A
  104. ADD $TX3,$T,$A ; A=T+Xi
  105. || STW $TX3,*${XPB}++
  106. SPKERNEL
  107. ;;==================================================
  108. ROTL $A,5,$Arot ; BODY_14
  109. || AND $C,$B,$F
  110. || ANDN $D,$B,$F0
  111. || ADD $K,$E,$T ; T=E+K
  112. XOR $F0,$F,$F ; F_00_19(B,C,D)
  113. || MV $D,$E ; E=D
  114. || MV $C,$D ; D=C
  115. || SWAP2 $TX1,$TX2
  116. || LDNW *${INP}++,$TX1
  117. ADD $F,$T,$T ; T+=F_00_19(B,C,D)
  118. || ROTL $B,30,$C ; C=ROL(B,30)
  119. || SWAP4 $TX2,$TX2 ; byte swap
  120. || LDW *${XPA}++,$X0 ; fetches from X ring buffer are
  121. || LDW *${XPB}[4],$X2 ; 2 iterations ahead
  122. ADD $Arot,$T,$T ; T+=ROL(A,5)
  123. || MV $A,$B ; B=A
  124. || LDW *${XPA}[7],$X8
  125. || MV $TX3,$X13 ; || LDW *${XPB}[15],$X13
  126. || MV $TX2,$TX3
  127. ADD $TX2,$T,$A ; A=T+Xi
  128. || STW $TX2,*${XPB}++
  129. ;;==================================================
  130. ROTL $A,5,$Arot ; BODY_15
  131. || AND $C,$B,$F
  132. || ANDN $D,$B,$F0
  133. || ADD $K,$E,$T ; T=E+K
  134. XOR $F0,$F,$F ; F_00_19(B,C,D)
  135. || MV $D,$E ; E=D
  136. || MV $C,$D ; D=C
  137. || SWAP2 $TX1,$TX2
  138. ADD $F,$T,$T ; T+=F_00_19(B,C,D)
  139. || ROTL $B,30,$C ; C=ROL(B,30)
  140. || SWAP4 $TX2,$TX2 ; byte swap
  141. || XOR $X0,$X2,$TX0 ; Xupdate XORs are 1 iteration ahead
  142. || LDW *${XPA}++,$X0
  143. || LDW *${XPB}[4],$X2
  144. ADD $Arot,$T,$T ; T+=ROL(A,5)
  145. || MV $A,$B ; B=A
  146. || XOR $X8,$X13,$TX1
  147. || LDW *${XPA}[7],$X8
  148. || MV $TX3,$X13 ; || LDW *${XPB}[15],$X13
  149. || MV $TX2,$TX3
  150. ADD $TX2,$T,$A ; A=T+Xi
  151. || STW $TX2,*${XPB}++
  152. || XOR $TX0,$TX1,$TX1
  153. || MVK 3,B0
  154. ;;==================================================
  155. SPLOOPD 5 ; BODY_16_19
  156. || MVC B0,ILC
  157. ROTL $A,5,$Arot
  158. || AND $C,$B,$F
  159. || ANDN $D,$B,$F0
  160. || ADD $K,$E,$T ; T=E+K
  161. || ROTL $TX1,1,$TX2 ; Xupdate output
  162. XOR $F0,$F,$F ; F_00_19(B,C,D)
  163. || MV $D,$E ; E=D
  164. || MV $C,$D ; D=C
  165. ADD $F,$T,$T ; T+=F_00_19(B,C,D)
  166. || ROTL $B,30,$C ; C=ROL(B,30)
  167. || XOR $X0,$X2,$TX0
  168. || LDW *${XPA}++,$X0
  169. || LDW *${XPB}[4],$X2
  170. ADD $Arot,$T,$T ; T+=ROL(A,5)
  171. || MV $A,$B ; B=A
  172. || XOR $X8,$X13,$TX1
  173. || LDW *${XPA}[7],$X8
  174. || MV $TX3,$X13 ; || LDW *${XPB}[15],$X13
  175. || MV $TX2,$TX3
  176. ADD $TX2,$T,$A ; A=T+Xi
  177. || STW $TX2,*${XPB}++
  178. || XOR $TX0,$TX1,$TX1
  179. SPKERNEL
  180. MVK 0xffffeba1,$K
  181. || MVK 19,B0
  182. MVKH 0x6ed90000,$K ; K_20_39
  183. ___
  184. sub BODY_20_39 {
  185. $code.=<<___;
  186. ;;==================================================
  187. SPLOOPD 5 ; BODY_20_39
  188. || MVC B0,ILC
  189. ROTL $A,5,$Arot
  190. || XOR $B,$C,$F
  191. || ADD $K,$E,$T ; T=E+K
  192. || ROTL $TX1,1,$TX2 ; Xupdate output
  193. XOR $D,$F,$F ; F_20_39(B,C,D)
  194. || MV $D,$E ; E=D
  195. || MV $C,$D ; D=C
  196. ADD $F,$T,$T ; T+=F_20_39(B,C,D)
  197. || ROTL $B,30,$C ; C=ROL(B,30)
  198. || XOR $X0,$X2,$TX0
  199. || LDW *${XPA}++,$X0
  200. || LDW *${XPB}[4],$X2
  201. ADD $Arot,$T,$T ; T+=ROL(A,5)
  202. || MV $A,$B ; B=A
  203. || XOR $X8,$X13,$TX1
  204. || LDW *${XPA}[7],$X8
  205. || MV $TX3,$X13 ; || LDW *${XPB}[15],$X13
  206. || MV $TX2,$TX3
  207. ADD $TX2,$T,$A ; A=T+Xi
  208. || STW $TX2,*${XPB}++ ; last one is redundant
  209. || XOR $TX0,$TX1,$TX1
  210. SPKERNEL
  211. ___
  212. $code.=<<___ if (!shift);
  213. MVK 0xffffbcdc,$K
  214. MVKH 0x8f1b0000,$K ; K_40_59
  215. ___
  216. } &BODY_20_39();
  217. $code.=<<___;
  218. ;;==================================================
  219. SPLOOPD 5 ; BODY_40_59
  220. || MVC B0,ILC
  221. || AND $B,$C,$F
  222. || AND $B,$D,$F0
  223. ROTL $A,5,$Arot
  224. || XOR $F0,$F,$F
  225. || AND $C,$D,$F0
  226. || ADD $K,$E,$T ; T=E+K
  227. || ROTL $TX1,1,$TX2 ; Xupdate output
  228. XOR $F0,$F,$F ; F_40_59(B,C,D)
  229. || MV $D,$E ; E=D
  230. || MV $C,$D ; D=C
  231. ADD $F,$T,$T ; T+=F_40_59(B,C,D)
  232. || ROTL $B,30,$C ; C=ROL(B,30)
  233. || XOR $X0,$X2,$TX0
  234. || LDW *${XPA}++,$X0
  235. || LDW *${XPB}[4],$X2
  236. ADD $Arot,$T,$T ; T+=ROL(A,5)
  237. || MV $A,$B ; B=A
  238. || XOR $X8,$X13,$TX1
  239. || LDW *${XPA}[7],$X8
  240. || MV $TX3,$X13 ; || LDW *${XPB}[15],$X13
  241. || MV $TX2,$TX3
  242. ADD $TX2,$T,$A ; A=T+Xi
  243. || STW $TX2,*${XPB}++
  244. || XOR $TX0,$TX1,$TX1
  245. || AND $B,$C,$F
  246. || AND $B,$D,$F0
  247. SPKERNEL
  248. MVK 0xffffc1d6,$K
  249. || MVK 18,B0
  250. MVKH 0xca620000,$K ; K_60_79
  251. ___
  252. &BODY_20_39(-1); # BODY_60_78
  253. $code.=<<___;
  254. ;;==================================================
  255. [A0] B loop?
  256. || ROTL $A,5,$Arot ; BODY_79
  257. || XOR $B,$C,$F
  258. || ROTL $TX1,1,$TX2 ; Xupdate output
  259. [A0] LDNW *${INP}++,$TX1 ; pre-fetch input
  260. || ADD $K,$E,$T ; T=E+K
  261. || XOR $D,$F,$F ; F_20_39(B,C,D)
  262. ADD $F,$T,$T ; T+=F_20_39(B,C,D)
  263. || ADD $Ectx,$D,$E ; E=D,E+=Ectx
  264. || ADD $Dctx,$C,$D ; D=C,D+=Dctx
  265. || ROTL $B,30,$C ; C=ROL(B,30)
  266. ADD $Arot,$T,$T ; T+=ROL(A,5)
  267. || ADD $Bctx,$A,$B ; B=A,B+=Bctx
  268. ADD $TX2,$T,$A ; A=T+Xi
  269. ADD $Actx,$A,$A ; A+=Actx
  270. || ADD $Cctx,$C,$C ; C+=Cctx
  271. ;; end of loop?
  272. BNOP RA ; return
  273. || MV FP,SP ; restore stack pointer
  274. || LDW *FP[0],FP ; restore frame pointer
  275. STW $A,*${CTX}[0] ; emit A-E...
  276. || MVK 0,B0
  277. STW $B,*${CTX}[1]
  278. || MVC B0,AMR ; clear AMR
  279. STW $C,*${CTX}[2]
  280. STW $D,*${CTX}[3]
  281. STW $E,*${CTX}[4]
  282. .endasmfunc
  283. .sect .const
  284. .cstring "SHA1 block transform for C64x+, CRYPTOGAMS by <appro\@openssl.org>"
  285. .align 4
  286. ___
  287. print $code;
  288. close STDOUT or die "error closing STDOUT: $!";