Revert "Merge branch 'upstream' into tizen"
[platform/upstream/nettle.git] / x86 / camellia-crypt-internal.asm
1 C nettle, low-level cryptographics library
2
3 C Copyright (C) 2010, Niels Möller
4 C  
5 C The nettle library is free software; you can redistribute it and/or modify
6 C it under the terms of the GNU Lesser General Public License as published by
7 C the Free Software Foundation; either version 2.1 of the License, or (at your
8 C option) any later version.
9
10 C The nettle library is distributed in the hope that it will be useful, but
11 C WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 C or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
13 C License for more details.
14
15 C You should have received a copy of the GNU Lesser General Public License
16 C along with the nettle library; see the file COPYING.LIB.  If not, write to
17 C the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 C MA 02111-1301, USA.
19
20 C Register usage:
21
22 C Camellia state, 128-bit value in little endian order.
23 C L0, H0 corresponds to D1 in the spec and i0 in the C implementation.
24 C while L1, H1 corresponds to D2/i1.
25 define(<L0>,<%eax>)
26 define(<H0>,<%ebx>)
27 define(<L1>,<%ecx>)
28 define(<H1>,<%edx>)
29
30 define(<TMP>,<%ebp>)
31 define(<KEY>,<%esi>)
32 define(<T>,<%edi>)
33
34 C Locals on the stack
35
36 define(<FRAME_L0>,      <(%esp)>)
37 define(<FRAME_H0>,      <4(%esp)>)
38 define(<FRAME_L1>,      <8(%esp)>)
39 define(<FRAME_H1>,      <12(%esp)>)
40 define(<FRAME_CNT>,     <16(%esp)>)
41         
42 C Arguments on stack.
43 define(<FRAME_CTX>,     <40(%esp)>)
44 define(<FRAME_TABLE>,   <44(%esp)>)
45 define(<FRAME_LENGTH>,  <48(%esp)>)
46 define(<FRAME_DST>,     <52(%esp)>)
47 define(<FRAME_SRC>,     <56(%esp)>)
48
49 define(<SP1110>, <(T,$1,4)>)
50 define(<SP0222>, <1024(T,$1,4)>)
51 define(<SP3033>, <2048(T,$1,4)>)
52 define(<SP4404>, <3072(T,$1,4)>)
53
54 C ROUND(xl, xh, yl, yh, key-offset)
55 C xl and xh are rotated 16 bits at the end
56 C yl and yh are read from stack, and left in registers
57 define(<ROUND>, <
58         movzbl  LREG($1), TMP
59         movl    SP1110(TMP), $4
60         movzbl  HREG($1), TMP
61         xorl    SP4404(TMP), $4
62         roll    <$>16, $1
63
64         movzbl  LREG($2), TMP
65         movl    SP4404(TMP), $3
66         movzbl  HREG($2), TMP
67         xorl    SP3033(TMP), $3
68         roll    <$>16, $2
69
70         movzbl  LREG($1), TMP
71         xorl    SP3033(TMP), $4
72         movzbl  HREG($1), TMP
73         xorl    SP0222(TMP), $4
74
75         movzbl  LREG($2), TMP
76         xorl    SP0222(TMP), $3
77         movzbl  HREG($2), TMP
78         xorl    SP1110(TMP), $3
79
80         xorl    $5(KEY), $4
81         xorl    $5 + 4(KEY), $3
82
83         xorl    $3, $4
84         rorl    <$>8, $3
85         xorl    $4, $3
86
87         xorl    FRAME_$3, $3
88         xorl    FRAME_$4, $4
89 >)
90
91 C Six rounds, with inputs and outputs in registers.
92 define(<ROUND6>, <
93         movl    L0, FRAME_L0
94         movl    H0, FRAME_H0
95         movl    L1, FRAME_L1
96         movl    H1, FRAME_H1
97
98         ROUND(L0,H0,<L1>,<H1>,0)
99         movl    L1, FRAME_L1
100         movl    H1, FRAME_H1
101         ROUND(L1,H1,<L0>,<H0>,8)
102         movl    L0, FRAME_L0
103         movl    H0, FRAME_H0
104         ROUND(L0,H0,<L1>,<H1>,16)
105         movl    L1, FRAME_L1
106         movl    H1, FRAME_H1
107         ROUND(L1,H1,<L0>,<H0>,24)
108         movl    L0, FRAME_L0
109         movl    H0, FRAME_H0
110         ROUND(L0,H0,<L1>,<H1>,32)
111         ROUND(L1,H1,<L0>,<H0>,40)
112         roll    <$>16, L1
113         roll    <$>16, H1
114 >)
115
116 C FL(x0, x1, key-offset)
117 define(<FL>, <
118         movl    $3 + 4(KEY), TMP
119         andl    $2, TMP
120         roll    <$>1, TMP
121         xorl    TMP, $1
122         movl    $3(KEY), TMP
123         orl     $1, TMP
124         xorl    TMP, $2
125 >)
126 C FLINV(x0, x1, key-offset)
127 define(<FLINV>, <
128         movl    $3(KEY), TMP
129         orl     $1, TMP
130         xorl    TMP, $2
131         movl    $3 + 4(KEY), TMP
132         andl    $2, TMP
133         roll    <$>1, TMP
134         xorl    TMP, $1
135 >)
136
137 .file "camellia-encrypt-internal.asm"
138         
139         C _camellia_crypt(struct camellia_context *ctx, 
140         C                 const struct camellia_table *T,
141         C                 unsigned length, uint8_t *dst,
142         C                 uint8_t *src)
143         .text
144         ALIGN(16)
145 PROLOGUE(_nettle_camellia_crypt)
146         C save all registers that need to be saved
147         pushl   %ebx            C  32(%esp)
148         pushl   %ebp            C  28(%esp)
149         pushl   %esi            C  24(%esp)
150         pushl   %edi            C  20(%esp)
151
152         subl    $20, %esp 
153
154         movl    FRAME_LENGTH, %ebp
155         testl   %ebp,%ebp
156         jz      .Lend
157
158 .Lblock_loop:
159         C Load data, note that we'll happily do unaligned loads
160         movl    FRAME_SRC, TMP
161         movl    (TMP), H0
162         bswap   H0
163         movl    4(TMP), L0
164         bswap   L0
165         movl    8(TMP), H1
166         bswap   H1
167         movl    12(TMP), L1
168         bswap   L1
169         addl    $16, FRAME_SRC
170         movl    FRAME_CTX, KEY
171         movl    (KEY), TMP
172         subl    $8, TMP
173         movl    TMP, FRAME_CNT
174         C       Whitening using first subkey 
175         addl    $ALIGNOF_UINT64_T + 8, KEY
176         xorl    -8(KEY), L0
177         xorl    -4(KEY), H0
178
179         movl    FRAME_TABLE, T
180
181         ROUND6
182 .Lround_loop:
183         addl    $64, KEY
184         FL(L0, H0, -16)
185         FLINV(L1, H1, -8)
186         ROUND6
187         subl    $8, FRAME_CNT   
188         ja      .Lround_loop
189
190         movl    FRAME_DST, TMP
191         bswap   H0
192         movl    H0,8(TMP)
193         bswap   L0
194         movl    L0,12(TMP)
195         xorl    52(KEY), H1
196         bswap   H1
197         movl    H1, 0(TMP)
198         xorl    48(KEY), L1
199         bswap   L1
200         movl    L1, 4(TMP)
201         addl    $16, FRAME_DST
202         subl    $16, FRAME_LENGTH
203         ja      .Lblock_loop
204
205 .Lend:
206         addl    $20, %esp
207         popl    %edi
208         popl    %esi
209         popl    %ebp
210         popl    %ebx
211         ret
212 EPILOGUE(_nettle_camellia_crypt)