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