e6f4e1e932d29fe8565d1245790e84e437a81299
[platform/upstream/nettle.git] / arm / v6 / sha256-compress.asm
1 C arm/v6/sha256-compress.asm
2
3 ifelse(<
4    Copyright (C) 2013 Niels Möller
5
6    This file is part of GNU Nettle.
7
8    GNU Nettle is free software: you can redistribute it and/or
9    modify it under the terms of either:
10
11      * the GNU Lesser General Public License as published by the Free
12        Software Foundation; either version 3 of the License, or (at your
13        option) any later version.
14
15    or
16
17      * the GNU General Public License as published by the Free
18        Software Foundation; either version 2 of the License, or (at your
19        option) any later version.
20
21    or both in parallel, as here.
22
23    GNU Nettle is distributed in the hope that it will be useful,
24    but WITHOUT ANY WARRANTY; without even the implied warranty of
25    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
26    General Public License for more details.
27
28    You should have received copies of the GNU General Public License and
29    the GNU Lesser General Public License along with this program.  If
30    not, see http://www.gnu.org/licenses/.
31 >) 
32
33         .file "sha256-compress.asm"
34         .arch armv6
35
36 define(<STATE>, <r0>)
37 define(<INPUT>, <r1>)
38 define(<K>, <r2>)
39 define(<SA>, <r3>)
40 define(<SB>, <r4>)
41 define(<SC>, <r5>)
42 define(<SD>, <r6>)
43 define(<SE>, <r7>)
44 define(<SF>, <r8>)
45 define(<SG>, <r10>)
46 define(<SH>, <r11>)
47 define(<T0>, <r12>)
48 define(<T1>, <r1>)      C Overlap INPUT
49 define(<COUNT>, <r0>)   C Overlap STATE
50 define(<W>, <r14>)
51
52 C Used for data load
53 define(<I0>, <r3>)
54 define(<I1>, <r4>)
55 define(<I2>, <r5>)
56 define(<I3>, <r6>)
57 define(<I4>, <r7>)
58 define(<DST>, <r8>)
59 define(<SHIFT>, <r10>)
60 define(<ILEFT>, <r11>)
61
62 define(<EXPN>, <
63         ldr     W, [sp, #+eval(4*$1)]
64         ldr     T0, [sp, #+eval(4*(($1 + 14) % 16))]
65         ror     T1, T0, #17
66         eor     T1, T1, T0, ror #19
67         eor     T1, T1, T0, lsr #10
68         add     W, W, T1
69         ldr     T0, [sp, #+eval(4*(($1 + 9) % 16))]
70         add     W, W, T0
71         ldr     T0, [sp, #+eval(4*(($1 + 1) % 16))]
72         ror     T1, T0, #7
73         eor     T1, T1, T0, ror #18
74         eor     T1, T1, T0, lsr #3
75         add     W, W, T1
76         str     W, [sp, #+eval(4*$1)]
77 >)
78
79 C ROUND(A,B,C,D,E,F,G,H)
80 C
81 C H += S1(E) + Choice(E,F,G) + K + W
82 C D += H
83 C H += S0(A) + Majority(A,B,C)
84 C
85 C Where
86 C
87 C S1(E) = E<<<26 ^ E<<<21 ^ E<<<7
88 C S0(A) = A<<<30 ^ A<<<19 ^ A<<<10
89 C Choice (E, F, G) = G^(E&(F^G))
90 C Majority (A,B,C) = (A&B) + (C&(A^B))
91         
92 define(<ROUND>, <
93         ror     T0, $5, #6
94         eor     T0, T0, $5, ror #11
95         eor     T0, T0, $5, ror #25
96         add     $8, $8, T0
97         eor     T0, $6, $7
98         and     T0, T0, $5
99         eor     T0, T0, $7
100         add     $8,$8, T0
101         ldr     T0, [K], #+4
102         add     $8, $8, W
103         add     $8, $8, T0
104         add     $4, $4, $8
105         ror     T0, $1, #2
106         eor     T0, T0, $1, ror #13
107         eor     T0, T0, $1, ror #22
108         add     $8, $8, T0
109         and     T0, $1, $2
110         add     $8, $8, T0
111         eor     T0, $1, $2
112         and     T0, T0, $3
113         add     $8, $8, T0
114 >)
115
116 define(<NOEXPN>, <
117         ldr     W, [sp, + $1]
118         add     $1, $1, #4
119 >)
120         C void
121         C _nettle_sha256_compress(uint32_t *state, const uint8_t *input, const uint32_t *k)
122
123         .text
124         .align 2
125
126 PROLOGUE(_nettle_sha256_compress)
127         push    {r4,r5,r6,r7,r8,r10,r11,r14}
128         sub     sp, sp, #68
129         str     STATE, [sp, #+64]
130
131         C Load data up front, since we don't have enough registers
132         C to load and shift on-the-fly
133         ands    SHIFT, INPUT, #3
134         and     INPUT, INPUT, $-4
135         ldr     I0, [INPUT]
136         addne   INPUT, INPUT, #4
137         lsl     SHIFT, SHIFT, #3
138         mov     T0, #0
139         movne   T0, #-1
140         lsl     I1, T0, SHIFT
141         uadd8   T0, T0, I1              C Sets APSR.GE bits
142
143         mov     DST, sp
144         mov     ILEFT, #4
145 .Lcopy:
146         ldm     INPUT!, {I1,I2,I3,I4}
147         sel     I0, I0, I1
148         ror     I0, I0, SHIFT
149         rev     I0, I0
150         sel     I1, I1, I2
151         ror     I1, I1, SHIFT
152         rev     I1, I1
153         sel     I2, I2, I3
154         ror     I2, I2, SHIFT
155         rev     I2, I2
156         sel     I3, I3, I4
157         ror     I3, I3, SHIFT
158         rev     I3, I3
159         subs    ILEFT, ILEFT, #1
160         stm     DST!, {I0,I1,I2,I3}
161         mov     I0, I4  
162         bne     .Lcopy
163         
164         ldm     STATE, {SA,SB,SC,SD,SE,SF,SG,SH}
165
166         mov     COUNT,#0
167
168 .Loop1:
169         NOEXPN(COUNT) ROUND(SA,SB,SC,SD,SE,SF,SG,SH)
170         NOEXPN(COUNT) ROUND(SH,SA,SB,SC,SD,SE,SF,SG)
171         NOEXPN(COUNT) ROUND(SG,SH,SA,SB,SC,SD,SE,SF)
172         NOEXPN(COUNT) ROUND(SF,SG,SH,SA,SB,SC,SD,SE)
173         NOEXPN(COUNT) ROUND(SE,SF,SG,SH,SA,SB,SC,SD)
174         NOEXPN(COUNT) ROUND(SD,SE,SF,SG,SH,SA,SB,SC)
175         NOEXPN(COUNT) ROUND(SC,SD,SE,SF,SG,SH,SA,SB)
176         NOEXPN(COUNT) ROUND(SB,SC,SD,SE,SF,SG,SH,SA)
177         cmp     COUNT,#64
178         bne     .Loop1
179
180         mov     COUNT, #3
181 .Loop2:
182         
183         EXPN( 0) ROUND(SA,SB,SC,SD,SE,SF,SG,SH)
184         EXPN( 1) ROUND(SH,SA,SB,SC,SD,SE,SF,SG)
185         EXPN( 2) ROUND(SG,SH,SA,SB,SC,SD,SE,SF)
186         EXPN( 3) ROUND(SF,SG,SH,SA,SB,SC,SD,SE)
187         EXPN( 4) ROUND(SE,SF,SG,SH,SA,SB,SC,SD)
188         EXPN( 5) ROUND(SD,SE,SF,SG,SH,SA,SB,SC)
189         EXPN( 6) ROUND(SC,SD,SE,SF,SG,SH,SA,SB)
190         EXPN( 7) ROUND(SB,SC,SD,SE,SF,SG,SH,SA)
191         EXPN( 8) ROUND(SA,SB,SC,SD,SE,SF,SG,SH)
192         EXPN( 9) ROUND(SH,SA,SB,SC,SD,SE,SF,SG)
193         EXPN(10) ROUND(SG,SH,SA,SB,SC,SD,SE,SF)
194         EXPN(11) ROUND(SF,SG,SH,SA,SB,SC,SD,SE)
195         EXPN(12) ROUND(SE,SF,SG,SH,SA,SB,SC,SD)
196         EXPN(13) ROUND(SD,SE,SF,SG,SH,SA,SB,SC)
197         EXPN(14) ROUND(SC,SD,SE,SF,SG,SH,SA,SB)
198         subs    COUNT, COUNT, #1
199         EXPN(15) ROUND(SB,SC,SD,SE,SF,SG,SH,SA)
200         bne     .Loop2
201
202         ldr     STATE, [sp, #+64]
203         C No longer needed registers
204         ldm     STATE, {r1,r2,r12,r14}
205         add     SA, SA, r1
206         add     SB, SB, r2
207         add     SC, SC, r12
208         add     SD, SD, r14
209         stm     STATE!, {SA,SB,SC,SD}
210         ldm     STATE, {r1,r2,r12,r14}
211         add     SE, SE, r1
212         add     SF, SF, r2
213         add     SG, SG, r12
214         add     SH, SH, r14
215         stm     STATE!, {SE,SF,SG,SH}
216         add     sp, sp, #68
217         pop     {r4,r5,r6,r7,r8,r10,r11,pc}
218 EPILOGUE(_nettle_sha256_compress)