sha1-thumb.pl 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #! /usr/bin/env perl
  2. # Copyright 2007-2016 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 for Thumb.
  15. #
  16. # January 2007.
  17. #
  18. # The code does not present direct interest to OpenSSL, because of low
  19. # performance. Its purpose is to establish _size_ benchmark. Pretty
  20. # useless one I must say, because 30% or 88 bytes larger ARMv4 code
  21. # [available on demand] is almost _twice_ as fast. It should also be
  22. # noted that in-lining of .Lcommon and .Lrotate improves performance
  23. # by over 40%, while code increases by only 10% or 32 bytes. But once
  24. # again, the goal was to establish _size_ benchmark, not performance.
  25. $output=pop and open STDOUT,">$output";
  26. $inline=0;
  27. #$cheat_on_binutils=1;
  28. $t0="r0";
  29. $t1="r1";
  30. $t2="r2";
  31. $a="r3";
  32. $b="r4";
  33. $c="r5";
  34. $d="r6";
  35. $e="r7";
  36. $K="r8"; # "upper" registers can be used in add/sub and mov insns
  37. $ctx="r9";
  38. $inp="r10";
  39. $len="r11";
  40. $Xi="r12";
  41. sub common {
  42. <<___;
  43. sub $t0,#4
  44. ldr $t1,[$t0]
  45. add $e,$K @ E+=K_xx_xx
  46. lsl $t2,$a,#5
  47. add $t2,$e
  48. lsr $e,$a,#27
  49. add $t2,$e @ E+=ROR(A,27)
  50. add $t2,$t1 @ E+=X[i]
  51. ___
  52. }
  53. sub rotate {
  54. <<___;
  55. mov $e,$d @ E=D
  56. mov $d,$c @ D=C
  57. lsl $c,$b,#30
  58. lsr $b,$b,#2
  59. orr $c,$b @ C=ROR(B,2)
  60. mov $b,$a @ B=A
  61. add $a,$t2,$t1 @ A=E+F_xx_xx(B,C,D)
  62. ___
  63. }
  64. sub BODY_00_19 {
  65. $code.=$inline?&common():"\tbl .Lcommon\n";
  66. $code.=<<___;
  67. mov $t1,$c
  68. eor $t1,$d
  69. and $t1,$b
  70. eor $t1,$d @ F_00_19(B,C,D)
  71. ___
  72. $code.=$inline?&rotate():"\tbl .Lrotate\n";
  73. }
  74. sub BODY_20_39 {
  75. $code.=$inline?&common():"\tbl .Lcommon\n";
  76. $code.=<<___;
  77. mov $t1,$b
  78. eor $t1,$c
  79. eor $t1,$d @ F_20_39(B,C,D)
  80. ___
  81. $code.=$inline?&rotate():"\tbl .Lrotate\n";
  82. }
  83. sub BODY_40_59 {
  84. $code.=$inline?&common():"\tbl .Lcommon\n";
  85. $code.=<<___;
  86. mov $t1,$b
  87. and $t1,$c
  88. mov $e,$b
  89. orr $e,$c
  90. and $e,$d
  91. orr $t1,$e @ F_40_59(B,C,D)
  92. ___
  93. $code.=$inline?&rotate():"\tbl .Lrotate\n";
  94. }
  95. $code=<<___;
  96. .text
  97. .code 16
  98. .global sha1_block_data_order
  99. .type sha1_block_data_order,%function
  100. .align 2
  101. sha1_block_data_order:
  102. ___
  103. if ($cheat_on_binutils) {
  104. $code.=<<___;
  105. .code 32
  106. add r3,pc,#1
  107. bx r3 @ switch to Thumb ISA
  108. .code 16
  109. ___
  110. }
  111. $code.=<<___;
  112. push {r4-r7}
  113. mov r3,r8
  114. mov r4,r9
  115. mov r5,r10
  116. mov r6,r11
  117. mov r7,r12
  118. push {r3-r7,lr}
  119. lsl r2,#6
  120. mov $ctx,r0 @ save context
  121. mov $inp,r1 @ save inp
  122. mov $len,r2 @ save len
  123. add $len,$inp @ $len to point at inp end
  124. .Lloop:
  125. mov $Xi,sp
  126. mov $t2,sp
  127. sub $t2,#16*4 @ [3]
  128. .LXload:
  129. ldrb $a,[$t1,#0] @ $t1 is r1 and holds inp
  130. ldrb $b,[$t1,#1]
  131. ldrb $c,[$t1,#2]
  132. ldrb $d,[$t1,#3]
  133. lsl $a,#24
  134. lsl $b,#16
  135. lsl $c,#8
  136. orr $a,$b
  137. orr $a,$c
  138. orr $a,$d
  139. add $t1,#4
  140. push {$a}
  141. cmp sp,$t2
  142. bne .LXload @ [+14*16]
  143. mov $inp,$t1 @ update $inp
  144. sub $t2,#32*4
  145. sub $t2,#32*4
  146. mov $e,#31 @ [+4]
  147. .LXupdate:
  148. ldr $a,[sp,#15*4]
  149. ldr $b,[sp,#13*4]
  150. ldr $c,[sp,#7*4]
  151. ldr $d,[sp,#2*4]
  152. eor $a,$b
  153. eor $a,$c
  154. eor $a,$d
  155. ror $a,$e
  156. push {$a}
  157. cmp sp,$t2
  158. bne .LXupdate @ [+(11+1)*64]
  159. ldmia $t0!,{$a,$b,$c,$d,$e} @ $t0 is r0 and holds ctx
  160. mov $t0,$Xi
  161. ldr $t2,.LK_00_19
  162. mov $t1,$t0
  163. sub $t1,#20*4
  164. mov $Xi,$t1
  165. mov $K,$t2 @ [+7+4]
  166. .L_00_19:
  167. ___
  168. &BODY_00_19();
  169. $code.=<<___;
  170. cmp $Xi,$t0
  171. bne .L_00_19 @ [+(2+9+4+2+8+2)*20]
  172. ldr $t2,.LK_20_39
  173. mov $t1,$t0
  174. sub $t1,#20*4
  175. mov $Xi,$t1
  176. mov $K,$t2 @ [+5]
  177. .L_20_39_or_60_79:
  178. ___
  179. &BODY_20_39();
  180. $code.=<<___;
  181. cmp $Xi,$t0
  182. bne .L_20_39_or_60_79 @ [+(2+9+3+2+8+2)*20*2]
  183. cmp sp,$t0
  184. beq .Ldone @ [+2]
  185. ldr $t2,.LK_40_59
  186. mov $t1,$t0
  187. sub $t1,#20*4
  188. mov $Xi,$t1
  189. mov $K,$t2 @ [+5]
  190. .L_40_59:
  191. ___
  192. &BODY_40_59();
  193. $code.=<<___;
  194. cmp $Xi,$t0
  195. bne .L_40_59 @ [+(2+9+6+2+8+2)*20]
  196. ldr $t2,.LK_60_79
  197. mov $Xi,sp
  198. mov $K,$t2
  199. b .L_20_39_or_60_79 @ [+4]
  200. .Ldone:
  201. mov $t0,$ctx
  202. ldr $t1,[$t0,#0]
  203. ldr $t2,[$t0,#4]
  204. add $a,$t1
  205. ldr $t1,[$t0,#8]
  206. add $b,$t2
  207. ldr $t2,[$t0,#12]
  208. add $c,$t1
  209. ldr $t1,[$t0,#16]
  210. add $d,$t2
  211. add $e,$t1
  212. stmia $t0!,{$a,$b,$c,$d,$e} @ [+20]
  213. add sp,#80*4 @ deallocate stack frame
  214. mov $t0,$ctx @ restore ctx
  215. mov $t1,$inp @ restore inp
  216. cmp $t1,$len
  217. beq .Lexit
  218. b .Lloop @ [+6] total 3212 cycles
  219. .Lexit:
  220. pop {r2-r7}
  221. mov r8,r2
  222. mov r9,r3
  223. mov r10,r4
  224. mov r11,r5
  225. mov r12,r6
  226. mov lr,r7
  227. pop {r4-r7}
  228. bx lr
  229. .align 2
  230. ___
  231. $code.=".Lcommon:\n".&common()."\tmov pc,lr\n" if (!$inline);
  232. $code.=".Lrotate:\n".&rotate()."\tmov pc,lr\n" if (!$inline);
  233. $code.=<<___;
  234. .align 2
  235. .LK_00_19: .word 0x5a827999
  236. .LK_20_39: .word 0x6ed9eba1
  237. .LK_40_59: .word 0x8f1bbcdc
  238. .LK_60_79: .word 0xca62c1d6
  239. .size sha1_block_data_order,.-sha1_block_data_order
  240. .asciz "SHA1 block transform for Thumb, CRYPTOGAMS by <appro\@openssl.org>"
  241. ___
  242. print $code;
  243. close STDOUT; # enforce flush