sha1-thumb.pl 4.7 KB

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