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