040e030f168f4094e5fb26579ca09c1d7bab03c1
[platform/upstream/nettle.git] / x86_64 / camellia-crypt-internal.asm
1 C x86_64/camellia-crypt-internal.asm
2
3 ifelse(<
4    Copyright (C) 2010, 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 Performance, cycles per block
34 C
35 C               Intel SU4100
36 C                 C  asm
37 C Camellia-128  415  347
38 C Camellia-256  543  461
39
40 C Register usage:
41
42 define(<NKEYS>, <%rdi>)
43 define(<KEYS>, <%rsi>)
44 define(<TABLE>, <%rdx>)
45 define(<LENGTH>, <%rcx>)
46 define(<DST>, <%r8>)
47 define(<SRC>, <%r9>)
48
49 C Camellia state
50 define(<I0>, <%rax>)
51 define(<I1>, <%rbx>) C callee-save
52 define(<KEY>, <%r13>) C callee-save
53 define(<TMP>, <%rbp>) C callee-save
54 define(<CNT>, <%r10>)
55 define(<IL>,  <%r11>)
56 define(<IR>,  <%r12>) C callee-save
57
58 define(<SP1110>, <(TABLE,$1,4)>)
59 define(<SP0222>, <1024(TABLE,$1,4)>)
60 define(<SP3033>, <2048(TABLE,$1,4)>)
61 define(<SP4404>, <3072(TABLE,$1,4)>)
62
63 C ROUND(x, y, key-offset)
64 define(<ROUND>, <
65         C Byte 0,1
66         movzbl  LREG($1), XREG(TMP)
67         movl    SP1110(TMP), XREG(IR)
68         movzbl  HREG($1), XREG(TMP)
69         xorl    SP4404(TMP), XREG(IR)
70         ror     <$>32, $1
71
72         C Byte 4,5
73         movzbl  LREG($1), XREG(TMP)
74         movl    SP4404(TMP), XREG(IL)
75         movzbl  HREG($1), XREG(TMP)
76         xorl    SP3033(TMP), XREG(IL)
77         rol     <$>16, $1
78
79         C Byte 2,3
80         movzbl  LREG($1), XREG(TMP)
81         xorl    SP3033(TMP), XREG(IR)
82         movzbl  HREG($1), XREG(TMP)
83         xorl    SP0222(TMP), XREG(IR)
84         ror     <$>32, $1
85
86         C Byte 6,7
87         movzbl  LREG($1), XREG(TMP)
88         xorl    SP0222(TMP), XREG(IL)
89         movzbl  HREG($1), XREG(TMP)
90         xorl    SP1110(TMP), XREG(IL)
91         ror     <$>16, $1
92
93         C 76543210
94         
95         xorl    XREG(IL), XREG(IR)
96         rorl    <$>8, XREG(IL)
97         xorl    XREG(IR), XREG(IL)
98         shl     <$>32, IR
99         or      IL, IR
100         xor     $3(KEY), $2
101         xor     IR, $2
102 >)
103
104 C FL(x, key-offset)
105 define(<FL>, <
106         mov     $1, TMP
107         shr     <$>32, TMP
108         andl    $2 + 4(KEY), XREG(TMP)
109         roll    <$>1, XREG(TMP)
110 C       xorl    XREG(TMP), XREG($1)
111         xor     TMP, $1
112         movl    $2(KEY), XREG(TMP)
113         orl     XREG($1), XREG(TMP)
114         shl     <$>32, TMP
115         xor     TMP, $1
116 >)
117 C FLINV(x0, key-offset)
118 define(<FLINV>, <
119         movl    $2(KEY), XREG(TMP)
120         orl     XREG($1), XREG(TMP)
121         shl     <$>32, TMP
122         xor     TMP, $1
123         mov     $1, TMP
124         shr     <$>32, TMP
125         andl    $2 + 4(KEY), XREG(TMP)
126         roll    <$>1, XREG(TMP)
127 C       xorl    XREG(TMP), XREG($1)
128         xor     TMP, $1 
129 >)
130
131         .file "camellia-crypt-internal.asm"
132         
133         C _camellia_crypt(unsigned nkeys, const uint64_t *keys, 
134         C                 const struct camellia_table *T,
135         C                 size_t length, uint8_t *dst,
136         C                 uint8_t *src)
137         .text
138         ALIGN(16)
139 PROLOGUE(_nettle_camellia_crypt)
140
141         W64_ENTRY(6, 0)
142         test    LENGTH, LENGTH
143         jz      .Lend
144
145         push    %rbx
146         push    %rbp
147         push    %r12
148         push    %r13
149         sub     $8, NKEYS
150 .Lblock_loop:
151         C Load data, note that we'll happily do unaligned loads
152         mov     (SRC), I0
153         bswap   I0
154         mov     8(SRC), I1
155         bswap   I1
156         add     $16, SRC
157         mov     XREG(NKEYS), XREG(CNT)
158         mov     KEYS, KEY
159
160         C       Whitening using first subkey 
161         xor     (KEY), I0
162         add     $8, KEY
163
164         ROUND(I0, I1, 0)
165         ROUND(I1, I0, 8)
166         ROUND(I0, I1, 16)
167         ROUND(I1, I0, 24)
168         ROUND(I0, I1, 32) 
169         ROUND(I1, I0, 40)
170         
171 .Lround_loop:
172         add     $64, KEY
173         FL(I0, -16)
174         FLINV(I1, -8)
175         ROUND(I0, I1, 0)
176         ROUND(I1, I0, 8)
177         ROUND(I0, I1, 16)
178         ROUND(I1, I0, 24)
179         ROUND(I0, I1, 32) 
180         ROUND(I1, I0, 40)
181
182         sub     $8, CNT 
183         ja      .Lround_loop
184
185         bswap   I0
186         mov     I0, 8(DST)
187         xor     48(KEY), I1
188         bswap   I1
189         mov     I1, (DST)
190         add     $16, DST
191         sub     $16, LENGTH
192
193         ja      .Lblock_loop
194
195         pop     %r13
196         pop     %r12
197         pop     %rbp
198         pop     %rbx
199 .Lend:
200         W64_EXIT(6, 0)
201         ret
202 EPILOGUE(_nettle_camellia_crypt)