2
0

sha1-thumb.pl 5.0 KB

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