16a19f7e8d7cc5c31bd4866f16080d2bc785bba8
[platform/upstream/nettle.git] / sparc64 / arcfour-crypt.asm
1 C sparc64/arcfour-crypt.asm
2
3 ifelse(<
4    Copyright (C) 2002, 2005 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 C       Define to YES, to enable the complex code to special case SRC
34 C       and DST with compatible alignment.
35         
36 define(<WITH_ALIGN>, <YES>)
37
38 C       Registers
39
40 define(<CTX>,   <%i0>)
41 define(<LENGTH>,<%i1>)
42 define(<DST>,   <%i2>)
43 define(<SRC>,   <%i3>)
44
45 define(<I1>,    <%i4>)
46 define(<I2>,    <%i5>)
47 define(<J>,     <%g1>)
48 define(<SI>,    <%g2>)
49 define(<SJ>,    <%g3>)
50 define(<TMP>,   <%o0>)
51 define(<TMP2>,  <%o1>)
52 define(<N>,     <%o2>)
53 define(<DATA>,  <%o3>)
54
55 C       Computes the next byte of the key stream. As input, i must
56 C       already point to the index for the current access, the index
57 C       for the next access is stored in ni. The resulting key byte is
58 C       stored in res.
59 C       ARCFOUR_BYTE(i, ni, res)
60 define(<ARCFOUR_BYTE>, <
61         ldub    [CTX + $1], SI
62         add     $1, 1, $2
63         add     J, SI, J
64         and     J, 0xff, J
65         ldub    [CTX + J], SJ
66         and     $2, 0xff, $2
67         stb     SI, [CTX + J]
68         add     SI, SJ, SI
69         and     SI, 0xff, SI
70         stb     SJ, [CTX + $1]
71         ldub    [CTX + SI], $3
72 >)dnl
73                         
74 define(<FRAME_SIZE>, 192)
75
76         .file "arcfour-crypt.asm"
77
78         C arcfour_crypt(struct arcfour_ctx *ctx,
79         C               size_t length, uint8_t *dst,
80         C               const uint8_t *src)
81
82         .section        ".text"
83         .align 16
84         .proc   020
85         
86 PROLOGUE(nettle_arcfour_crypt)
87
88         save    %sp, -FRAME_SIZE, %sp
89         cmp     LENGTH, 0
90         be      .Lend
91         nop
92         
93         C       Load both I and J
94         lduh    [CTX + ARCFOUR_I], I1
95         and     I1, 0xff, J
96         srl     I1, 8, I1
97
98         C       We want an even address for DST
99         andcc   DST, 1, %g0
100         add     I1, 1 ,I1
101         beq     .Laligned2
102         and     I1, 0xff, I1
103
104         mov     I1, I2
105         ldub    [SRC], DATA
106         ARCFOUR_BYTE(I2, I1, TMP)
107         subcc   LENGTH, 1, LENGTH
108         add     SRC, 1, SRC
109         xor     DATA, TMP, DATA
110         stb     DATA, [DST]
111         beq     .Ldone
112         add     DST, 1, DST
113
114 .Laligned2:
115
116         cmp     LENGTH, 2
117         blu     .Lfinal1
118         C       Harmless delay slot instruction 
119         andcc   DST, 2, %g0
120         beq     .Laligned4
121         nop
122
123         ldub    [SRC], DATA
124         ARCFOUR_BYTE(I1, I2, TMP)
125         ldub    [SRC + 1], TMP2
126         add     SRC, 2, SRC
127         xor     DATA, TMP, DATA
128         sll     DATA, 8, DATA   
129
130         ARCFOUR_BYTE(I2, I1, TMP)
131         xor     TMP2, TMP, TMP
132         subcc   LENGTH, 2, LENGTH
133         or      DATA, TMP, DATA
134
135         sth     DATA, [DST]
136         beq     .Ldone
137         add     DST, 2, DST
138         
139 .Laligned4:
140         cmp     LENGTH, 4
141         blu     .Lfinal2
142         C       Harmless delay slot instruction
143         srl     LENGTH, 2, N
144         
145 .Loop:
146         C       Main loop, with aligned writes
147         
148         C       FIXME: Could check if SRC is aligned, and
149         C       use 32-bit reads in that case.
150
151         ldub    [SRC], DATA
152         ARCFOUR_BYTE(I1, I2, TMP)
153         ldub    [SRC + 1], TMP2
154         xor     TMP, DATA, DATA
155         sll     DATA, 8, DATA
156
157         ARCFOUR_BYTE(I2, I1, TMP)
158         xor     TMP2, TMP, TMP
159         ldub    [SRC + 2], TMP2
160         or      TMP, DATA, DATA
161         sll     DATA, 8, DATA
162
163         ARCFOUR_BYTE(I1, I2, TMP)
164         xor     TMP2, TMP, TMP
165         ldub    [SRC + 3], TMP2
166         or      TMP, DATA, DATA
167         sll     DATA, 8, DATA
168
169         ARCFOUR_BYTE(I2, I1, TMP)
170         xor     TMP2, TMP, TMP
171         or      TMP, DATA, DATA
172         subcc   N, 1, N
173         add     SRC, 4, SRC
174         st      DATA, [DST]
175         bne     .Loop
176         add     DST, 4, DST
177         
178         andcc   LENGTH, 3, LENGTH
179         beq     .Ldone
180         nop
181
182 .Lfinal2:
183         C       DST address must be 2-aligned
184         cmp     LENGTH, 2
185         blu     .Lfinal1
186         nop
187
188         ldub    [SRC], DATA
189         ARCFOUR_BYTE(I1, I2, TMP)
190         ldub    [SRC + 1], TMP2
191         add     SRC, 2, SRC
192         xor     DATA, TMP, DATA
193         sll     DATA, 8, DATA   
194
195         ARCFOUR_BYTE(I2, I1, TMP)
196         xor     TMP2, TMP, TMP
197         or      DATA, TMP, DATA
198
199         sth     DATA, [DST]
200         beq     .Ldone
201         add     DST, 2, DST
202
203 .Lfinal1:
204         mov     I1, I2
205         ldub    [SRC], DATA
206         ARCFOUR_BYTE(I2, I1, TMP)
207         xor     DATA, TMP, DATA
208         stb     DATA, [DST]
209
210 .Ldone:
211         C       Save back I and J
212         sll     I2, 8, I2
213         or      I2, J, I2
214         stuh    I2, [CTX + ARCFOUR_I]
215
216 .Lend:
217         ret
218         restore
219
220 EPILOGUE(nettle_arcfour_crypt)
221
222 C       Stats for AES 128 on sellafield.lysator.liu.se (UE450, 296 MHz)
223
224 C 1:    nettle-1.13 C-code
225 C 2:    New assembler code (basically the same as for sparc32)
226
227 C       MB/s    cycles/byte
228 C 1:    3.6     77.7
229 C 2:    21.8    13.0