]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/intel-ipsec-mb/des_basic.c
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / spdk / intel-ipsec-mb / des_basic.c
1 /*******************************************************************************
2 Copyright (c) 2017-2018, Intel Corporation
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
6
7 * Redistributions of source code must retain the above copyright notice,
8 this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of Intel Corporation nor the names of its contributors
13 may be used to endorse or promote products derived from this software
14 without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *******************************************************************************/
27
28 /* basic DES implementation */
29
30 #include <stdint.h>
31 #include <string.h>
32
33 #include "intel-ipsec-mb.h"
34 #include "des.h"
35 #include "des_utils.h"
36
37 __forceinline
38 void permute_operation(uint32_t *pa, uint32_t *pb,
39 const uint32_t n, const uint32_t m)
40 {
41 register uint32_t t = (*pb ^ (*pa >> n)) & m;
42
43 *pb ^= t;
44 *pa ^= (t << n);
45 }
46
47 /* inital permutation */
48 __forceinline
49 void ip_z(uint32_t *pl, uint32_t *pr)
50 {
51 permute_operation(pr, pl, 4, 0x0f0f0f0f);
52 permute_operation(pl, pr, 16, 0x0000ffff);
53 permute_operation(pr, pl, 2, 0x33333333);
54 permute_operation(pl, pr, 8, 0x00ff00ff);
55 permute_operation(pr, pl, 1, 0x55555555);
56 }
57
58 /* final permuation */
59 __forceinline
60 void fp_z(uint32_t *pl, uint32_t *pr)
61 {
62 permute_operation(pl, pr, 1, 0x55555555);
63 permute_operation(pr, pl, 8, 0x00ff00ff);
64 permute_operation(pl, pr, 2, 0x33333333);
65 permute_operation(pr, pl, 16, 0x0000ffff);
66 permute_operation(pl, pr, 4, 0x0f0f0f0f);
67 }
68
69 /* 1st part of DES round
70 * - permutes and exands R(32 bits) into 48 bits
71 */
72 __forceinline
73 uint64_t e_phase(const uint64_t R)
74 {
75 /* E phase as in FIPS46-3 and also 8x6 to 8x8 expansion.
76 *
77 * Bit selection table for this operation looks as follows:
78 * 32, 1, 2, 3, 4, 5, X, X,
79 * 4, 5, 6, 7, 8, 9, X, X,
80 * 8, 9, 10, 11, 12, 13, X, X,
81 * 12, 13, 14, 15, 16, 17, X, X,
82 * 16, 17, 18, 19, 20, 21, X, X,
83 * 20, 21, 22, 23, 24, 25, X, X,
84 * 24, 25, 26, 27, 28, 29, X, X,
85 * 28, 29, 30, 31, 32, 1, X, X
86 * where 'X' is bit value 0.
87 */
88 return ((R << 1) & UINT64_C(0x3e)) | ((R >> 31) & UINT64_C(1)) |
89 ((R << 5) & UINT64_C(0x3f00)) |
90 ((R << 9) & UINT64_C(0x3f0000)) |
91 ((R << 13) & UINT64_C(0x3f000000)) |
92 ((R << 17) & UINT64_C(0x3f00000000)) |
93 ((R << 21) & UINT64_C(0x3f0000000000)) |
94 ((R << 25) & UINT64_C(0x3f000000000000)) |
95 ((R << 29) & UINT64_C(0x1f00000000000000)) |
96 ((R & UINT64_C(1)) << 61);
97 }
98
99 static const uint32_t sbox0p[64] = {
100 UINT32_C(0x00410100), UINT32_C(0x00010000),
101 UINT32_C(0x40400000), UINT32_C(0x40410100),
102 UINT32_C(0x00400000), UINT32_C(0x40010100),
103 UINT32_C(0x40010000), UINT32_C(0x40400000),
104 UINT32_C(0x40010100), UINT32_C(0x00410100),
105 UINT32_C(0x00410000), UINT32_C(0x40000100),
106 UINT32_C(0x40400100), UINT32_C(0x00400000),
107 UINT32_C(0x00000000), UINT32_C(0x40010000),
108 UINT32_C(0x00010000), UINT32_C(0x40000000),
109 UINT32_C(0x00400100), UINT32_C(0x00010100),
110 UINT32_C(0x40410100), UINT32_C(0x00410000),
111 UINT32_C(0x40000100), UINT32_C(0x00400100),
112 UINT32_C(0x40000000), UINT32_C(0x00000100),
113 UINT32_C(0x00010100), UINT32_C(0x40410000),
114 UINT32_C(0x00000100), UINT32_C(0x40400100),
115 UINT32_C(0x40410000), UINT32_C(0x00000000),
116 UINT32_C(0x00000000), UINT32_C(0x40410100),
117 UINT32_C(0x00400100), UINT32_C(0x40010000),
118 UINT32_C(0x00410100), UINT32_C(0x00010000),
119 UINT32_C(0x40000100), UINT32_C(0x00400100),
120 UINT32_C(0x40410000), UINT32_C(0x00000100),
121 UINT32_C(0x00010100), UINT32_C(0x40400000),
122 UINT32_C(0x40010100), UINT32_C(0x40000000),
123 UINT32_C(0x40400000), UINT32_C(0x00410000),
124 UINT32_C(0x40410100), UINT32_C(0x00010100),
125 UINT32_C(0x00410000), UINT32_C(0x40400100),
126 UINT32_C(0x00400000), UINT32_C(0x40000100),
127 UINT32_C(0x40010000), UINT32_C(0x00000000),
128 UINT32_C(0x00010000), UINT32_C(0x00400000),
129 UINT32_C(0x40400100), UINT32_C(0x00410100),
130 UINT32_C(0x40000000), UINT32_C(0x40410000),
131 UINT32_C(0x00000100), UINT32_C(0x40010100)
132 };
133
134 static const uint32_t sbox1p[64] = {
135 UINT32_C(0x08021002), UINT32_C(0x00000000),
136 UINT32_C(0x00021000), UINT32_C(0x08020000),
137 UINT32_C(0x08000002), UINT32_C(0x00001002),
138 UINT32_C(0x08001000), UINT32_C(0x00021000),
139 UINT32_C(0x00001000), UINT32_C(0x08020002),
140 UINT32_C(0x00000002), UINT32_C(0x08001000),
141 UINT32_C(0x00020002), UINT32_C(0x08021000),
142 UINT32_C(0x08020000), UINT32_C(0x00000002),
143 UINT32_C(0x00020000), UINT32_C(0x08001002),
144 UINT32_C(0x08020002), UINT32_C(0x00001000),
145 UINT32_C(0x00021002), UINT32_C(0x08000000),
146 UINT32_C(0x00000000), UINT32_C(0x00020002),
147 UINT32_C(0x08001002), UINT32_C(0x00021002),
148 UINT32_C(0x08021000), UINT32_C(0x08000002),
149 UINT32_C(0x08000000), UINT32_C(0x00020000),
150 UINT32_C(0x00001002), UINT32_C(0x08021002),
151 UINT32_C(0x00020002), UINT32_C(0x08021000),
152 UINT32_C(0x08001000), UINT32_C(0x00021002),
153 UINT32_C(0x08021002), UINT32_C(0x00020002),
154 UINT32_C(0x08000002), UINT32_C(0x00000000),
155 UINT32_C(0x08000000), UINT32_C(0x00001002),
156 UINT32_C(0x00020000), UINT32_C(0x08020002),
157 UINT32_C(0x00001000), UINT32_C(0x08000000),
158 UINT32_C(0x00021002), UINT32_C(0x08001002),
159 UINT32_C(0x08021000), UINT32_C(0x00001000),
160 UINT32_C(0x00000000), UINT32_C(0x08000002),
161 UINT32_C(0x00000002), UINT32_C(0x08021002),
162 UINT32_C(0x00021000), UINT32_C(0x08020000),
163 UINT32_C(0x08020002), UINT32_C(0x00020000),
164 UINT32_C(0x00001002), UINT32_C(0x08001000),
165 UINT32_C(0x08001002), UINT32_C(0x00000002),
166 UINT32_C(0x08020000), UINT32_C(0x00021000)
167 };
168
169 static const uint32_t sbox2p[64] = {
170 UINT32_C(0x20800000), UINT32_C(0x00808020),
171 UINT32_C(0x00000020), UINT32_C(0x20800020),
172 UINT32_C(0x20008000), UINT32_C(0x00800000),
173 UINT32_C(0x20800020), UINT32_C(0x00008020),
174 UINT32_C(0x00800020), UINT32_C(0x00008000),
175 UINT32_C(0x00808000), UINT32_C(0x20000000),
176 UINT32_C(0x20808020), UINT32_C(0x20000020),
177 UINT32_C(0x20000000), UINT32_C(0x20808000),
178 UINT32_C(0x00000000), UINT32_C(0x20008000),
179 UINT32_C(0x00808020), UINT32_C(0x00000020),
180 UINT32_C(0x20000020), UINT32_C(0x20808020),
181 UINT32_C(0x00008000), UINT32_C(0x20800000),
182 UINT32_C(0x20808000), UINT32_C(0x00800020),
183 UINT32_C(0x20008020), UINT32_C(0x00808000),
184 UINT32_C(0x00008020), UINT32_C(0x00000000),
185 UINT32_C(0x00800000), UINT32_C(0x20008020),
186 UINT32_C(0x00808020), UINT32_C(0x00000020),
187 UINT32_C(0x20000000), UINT32_C(0x00008000),
188 UINT32_C(0x20000020), UINT32_C(0x20008000),
189 UINT32_C(0x00808000), UINT32_C(0x20800020),
190 UINT32_C(0x00000000), UINT32_C(0x00808020),
191 UINT32_C(0x00008020), UINT32_C(0x20808000),
192 UINT32_C(0x20008000), UINT32_C(0x00800000),
193 UINT32_C(0x20808020), UINT32_C(0x20000000),
194 UINT32_C(0x20008020), UINT32_C(0x20800000),
195 UINT32_C(0x00800000), UINT32_C(0x20808020),
196 UINT32_C(0x00008000), UINT32_C(0x00800020),
197 UINT32_C(0x20800020), UINT32_C(0x00008020),
198 UINT32_C(0x00800020), UINT32_C(0x00000000),
199 UINT32_C(0x20808000), UINT32_C(0x20000020),
200 UINT32_C(0x20800000), UINT32_C(0x20008020),
201 UINT32_C(0x00000020), UINT32_C(0x00808000)
202 };
203
204 static const uint32_t sbox3p[64] = {
205 UINT32_C(0x00080201), UINT32_C(0x02000200),
206 UINT32_C(0x00000001), UINT32_C(0x02080201),
207 UINT32_C(0x00000000), UINT32_C(0x02080000),
208 UINT32_C(0x02000201), UINT32_C(0x00080001),
209 UINT32_C(0x02080200), UINT32_C(0x02000001),
210 UINT32_C(0x02000000), UINT32_C(0x00000201),
211 UINT32_C(0x02000001), UINT32_C(0x00080201),
212 UINT32_C(0x00080000), UINT32_C(0x02000000),
213 UINT32_C(0x02080001), UINT32_C(0x00080200),
214 UINT32_C(0x00000200), UINT32_C(0x00000001),
215 UINT32_C(0x00080200), UINT32_C(0x02000201),
216 UINT32_C(0x02080000), UINT32_C(0x00000200),
217 UINT32_C(0x00000201), UINT32_C(0x00000000),
218 UINT32_C(0x00080001), UINT32_C(0x02080200),
219 UINT32_C(0x02000200), UINT32_C(0x02080001),
220 UINT32_C(0x02080201), UINT32_C(0x00080000),
221 UINT32_C(0x02080001), UINT32_C(0x00000201),
222 UINT32_C(0x00080000), UINT32_C(0x02000001),
223 UINT32_C(0x00080200), UINT32_C(0x02000200),
224 UINT32_C(0x00000001), UINT32_C(0x02080000),
225 UINT32_C(0x02000201), UINT32_C(0x00000000),
226 UINT32_C(0x00000200), UINT32_C(0x00080001),
227 UINT32_C(0x00000000), UINT32_C(0x02080001),
228 UINT32_C(0x02080200), UINT32_C(0x00000200),
229 UINT32_C(0x02000000), UINT32_C(0x02080201),
230 UINT32_C(0x00080201), UINT32_C(0x00080000),
231 UINT32_C(0x02080201), UINT32_C(0x00000001),
232 UINT32_C(0x02000200), UINT32_C(0x00080201),
233 UINT32_C(0x00080001), UINT32_C(0x00080200),
234 UINT32_C(0x02080000), UINT32_C(0x02000201),
235 UINT32_C(0x00000201), UINT32_C(0x02000000),
236 UINT32_C(0x02000001), UINT32_C(0x02080200)
237 };
238
239 static const uint32_t sbox4p[64] = {
240 UINT32_C(0x01000000), UINT32_C(0x00002000),
241 UINT32_C(0x00000080), UINT32_C(0x01002084),
242 UINT32_C(0x01002004), UINT32_C(0x01000080),
243 UINT32_C(0x00002084), UINT32_C(0x01002000),
244 UINT32_C(0x00002000), UINT32_C(0x00000004),
245 UINT32_C(0x01000004), UINT32_C(0x00002080),
246 UINT32_C(0x01000084), UINT32_C(0x01002004),
247 UINT32_C(0x01002080), UINT32_C(0x00000000),
248 UINT32_C(0x00002080), UINT32_C(0x01000000),
249 UINT32_C(0x00002004), UINT32_C(0x00000084),
250 UINT32_C(0x01000080), UINT32_C(0x00002084),
251 UINT32_C(0x00000000), UINT32_C(0x01000004),
252 UINT32_C(0x00000004), UINT32_C(0x01000084),
253 UINT32_C(0x01002084), UINT32_C(0x00002004),
254 UINT32_C(0x01002000), UINT32_C(0x00000080),
255 UINT32_C(0x00000084), UINT32_C(0x01002080),
256 UINT32_C(0x01002080), UINT32_C(0x01000084),
257 UINT32_C(0x00002004), UINT32_C(0x01002000),
258 UINT32_C(0x00002000), UINT32_C(0x00000004),
259 UINT32_C(0x01000004), UINT32_C(0x01000080),
260 UINT32_C(0x01000000), UINT32_C(0x00002080),
261 UINT32_C(0x01002084), UINT32_C(0x00000000),
262 UINT32_C(0x00002084), UINT32_C(0x01000000),
263 UINT32_C(0x00000080), UINT32_C(0x00002004),
264 UINT32_C(0x01000084), UINT32_C(0x00000080),
265 UINT32_C(0x00000000), UINT32_C(0x01002084),
266 UINT32_C(0x01002004), UINT32_C(0x01002080),
267 UINT32_C(0x00000084), UINT32_C(0x00002000),
268 UINT32_C(0x00002080), UINT32_C(0x01002004),
269 UINT32_C(0x01000080), UINT32_C(0x00000084),
270 UINT32_C(0x00000004), UINT32_C(0x00002084),
271 UINT32_C(0x01002000), UINT32_C(0x01000004)
272 };
273
274 const uint32_t sbox5p[64] = {
275 UINT32_C(0x10000008), UINT32_C(0x00040008),
276 UINT32_C(0x00000000), UINT32_C(0x10040400),
277 UINT32_C(0x00040008), UINT32_C(0x00000400),
278 UINT32_C(0x10000408), UINT32_C(0x00040000),
279 UINT32_C(0x00000408), UINT32_C(0x10040408),
280 UINT32_C(0x00040400), UINT32_C(0x10000000),
281 UINT32_C(0x10000400), UINT32_C(0x10000008),
282 UINT32_C(0x10040000), UINT32_C(0x00040408),
283 UINT32_C(0x00040000), UINT32_C(0x10000408),
284 UINT32_C(0x10040008), UINT32_C(0x00000000),
285 UINT32_C(0x00000400), UINT32_C(0x00000008),
286 UINT32_C(0x10040400), UINT32_C(0x10040008),
287 UINT32_C(0x10040408), UINT32_C(0x10040000),
288 UINT32_C(0x10000000), UINT32_C(0x00000408),
289 UINT32_C(0x00000008), UINT32_C(0x00040400),
290 UINT32_C(0x00040408), UINT32_C(0x10000400),
291 UINT32_C(0x00000408), UINT32_C(0x10000000),
292 UINT32_C(0x10000400), UINT32_C(0x00040408),
293 UINT32_C(0x10040400), UINT32_C(0x00040008),
294 UINT32_C(0x00000000), UINT32_C(0x10000400),
295 UINT32_C(0x10000000), UINT32_C(0x00000400),
296 UINT32_C(0x10040008), UINT32_C(0x00040000),
297 UINT32_C(0x00040008), UINT32_C(0x10040408),
298 UINT32_C(0x00040400), UINT32_C(0x00000008),
299 UINT32_C(0x10040408), UINT32_C(0x00040400),
300 UINT32_C(0x00040000), UINT32_C(0x10000408),
301 UINT32_C(0x10000008), UINT32_C(0x10040000),
302 UINT32_C(0x00040408), UINT32_C(0x00000000),
303 UINT32_C(0x00000400), UINT32_C(0x10000008),
304 UINT32_C(0x10000408), UINT32_C(0x10040400),
305 UINT32_C(0x10040000), UINT32_C(0x00000408),
306 UINT32_C(0x00000008), UINT32_C(0x10040008)
307 };
308
309 static const uint32_t sbox6p[64] = {
310 UINT32_C(0x00000800), UINT32_C(0x00000040),
311 UINT32_C(0x00200040), UINT32_C(0x80200000),
312 UINT32_C(0x80200840), UINT32_C(0x80000800),
313 UINT32_C(0x00000840), UINT32_C(0x00000000),
314 UINT32_C(0x00200000), UINT32_C(0x80200040),
315 UINT32_C(0x80000040), UINT32_C(0x00200800),
316 UINT32_C(0x80000000), UINT32_C(0x00200840),
317 UINT32_C(0x00200800), UINT32_C(0x80000040),
318 UINT32_C(0x80200040), UINT32_C(0x00000800),
319 UINT32_C(0x80000800), UINT32_C(0x80200840),
320 UINT32_C(0x00000000), UINT32_C(0x00200040),
321 UINT32_C(0x80200000), UINT32_C(0x00000840),
322 UINT32_C(0x80200800), UINT32_C(0x80000840),
323 UINT32_C(0x00200840), UINT32_C(0x80000000),
324 UINT32_C(0x80000840), UINT32_C(0x80200800),
325 UINT32_C(0x00000040), UINT32_C(0x00200000),
326 UINT32_C(0x80000840), UINT32_C(0x00200800),
327 UINT32_C(0x80200800), UINT32_C(0x80000040),
328 UINT32_C(0x00000800), UINT32_C(0x00000040),
329 UINT32_C(0x00200000), UINT32_C(0x80200800),
330 UINT32_C(0x80200040), UINT32_C(0x80000840),
331 UINT32_C(0x00000840), UINT32_C(0x00000000),
332 UINT32_C(0x00000040), UINT32_C(0x80200000),
333 UINT32_C(0x80000000), UINT32_C(0x00200040),
334 UINT32_C(0x00000000), UINT32_C(0x80200040),
335 UINT32_C(0x00200040), UINT32_C(0x00000840),
336 UINT32_C(0x80000040), UINT32_C(0x00000800),
337 UINT32_C(0x80200840), UINT32_C(0x00200000),
338 UINT32_C(0x00200840), UINT32_C(0x80000000),
339 UINT32_C(0x80000800), UINT32_C(0x80200840),
340 UINT32_C(0x80200000), UINT32_C(0x00200840),
341 UINT32_C(0x00200800), UINT32_C(0x80000800)
342 };
343
344 static const uint32_t sbox7p[64] = {
345 UINT32_C(0x04100010), UINT32_C(0x04104000),
346 UINT32_C(0x00004010), UINT32_C(0x00000000),
347 UINT32_C(0x04004000), UINT32_C(0x00100010),
348 UINT32_C(0x04100000), UINT32_C(0x04104010),
349 UINT32_C(0x00000010), UINT32_C(0x04000000),
350 UINT32_C(0x00104000), UINT32_C(0x00004010),
351 UINT32_C(0x00104010), UINT32_C(0x04004010),
352 UINT32_C(0x04000010), UINT32_C(0x04100000),
353 UINT32_C(0x00004000), UINT32_C(0x00104010),
354 UINT32_C(0x00100010), UINT32_C(0x04004000),
355 UINT32_C(0x04104010), UINT32_C(0x04000010),
356 UINT32_C(0x00000000), UINT32_C(0x00104000),
357 UINT32_C(0x04000000), UINT32_C(0x00100000),
358 UINT32_C(0x04004010), UINT32_C(0x04100010),
359 UINT32_C(0x00100000), UINT32_C(0x00004000),
360 UINT32_C(0x04104000), UINT32_C(0x00000010),
361 UINT32_C(0x00100000), UINT32_C(0x00004000),
362 UINT32_C(0x04000010), UINT32_C(0x04104010),
363 UINT32_C(0x00004010), UINT32_C(0x04000000),
364 UINT32_C(0x00000000), UINT32_C(0x00104000),
365 UINT32_C(0x04100010), UINT32_C(0x04004010),
366 UINT32_C(0x04004000), UINT32_C(0x00100010),
367 UINT32_C(0x04104000), UINT32_C(0x00000010),
368 UINT32_C(0x00100010), UINT32_C(0x04004000),
369 UINT32_C(0x04104010), UINT32_C(0x00100000),
370 UINT32_C(0x04100000), UINT32_C(0x04000010),
371 UINT32_C(0x00104000), UINT32_C(0x00004010),
372 UINT32_C(0x04004010), UINT32_C(0x04100000),
373 UINT32_C(0x00000010), UINT32_C(0x04104000),
374 UINT32_C(0x00104010), UINT32_C(0x00000000),
375 UINT32_C(0x04000000), UINT32_C(0x04100010),
376 UINT32_C(0x00004000), UINT32_C(0x00104010)
377 };
378
379 __forceinline
380 uint32_t fRK(const uint32_t R, const uint64_t K)
381 {
382 uint64_t x;
383
384 /* Combined e-phase and 8x6bits to 8x8bits expansion.
385 * 32 bits -> 48 bits permutation
386 */
387 x = e_phase((uint64_t) R) ^ K;
388
389 /* Combined s-box and p-phase.
390 * s-box: 48 bits -> 32 bits
391 * p-phase: 32 bits -> 32 bites permutation
392 */
393 return sbox0p[x & 0x3f] |
394 sbox1p[(x >> (8 * 1)) & 0x3f] |
395 sbox2p[(x >> (8 * 2)) & 0x3f] |
396 sbox3p[(x >> (8 * 3)) & 0x3f] |
397 sbox4p[(x >> (8 * 4)) & 0x3f] |
398 sbox5p[(x >> (8 * 5)) & 0x3f] |
399 sbox6p[(x >> (8 * 6)) & 0x3f] |
400 sbox7p[(x >> (8 * 7)) & 0x3f];
401 }
402
403 __forceinline
404 uint64_t enc_dec_1(const uint64_t data, const uint64_t *ks, const int enc)
405 {
406 uint32_t l, r;
407
408 r = (uint32_t) (data);
409 l = (uint32_t) (data >> 32);
410 ip_z(&r, &l);
411
412 if (enc) {
413 l ^= fRK(r, ks[0]);
414 r ^= fRK(l, ks[1]);
415 l ^= fRK(r, ks[2]);
416 r ^= fRK(l, ks[3]);
417 l ^= fRK(r, ks[4]);
418 r ^= fRK(l, ks[5]);
419 l ^= fRK(r, ks[6]);
420 r ^= fRK(l, ks[7]);
421 l ^= fRK(r, ks[8]);
422 r ^= fRK(l, ks[9]);
423 l ^= fRK(r, ks[10]);
424 r ^= fRK(l, ks[11]);
425 l ^= fRK(r, ks[12]);
426 r ^= fRK(l, ks[13]);
427 l ^= fRK(r, ks[14]);
428 r ^= fRK(l, ks[15]);
429 } else {
430 l ^= fRK(r, ks[15]); /* l: l0 -> r1/l2 */
431 r ^= fRK(l, ks[14]); /* r: r0 -> r2 */
432 l ^= fRK(r, ks[13]);
433 r ^= fRK(l, ks[12]);
434 l ^= fRK(r, ks[11]);
435 r ^= fRK(l, ks[10]);
436 l ^= fRK(r, ks[9]);
437 r ^= fRK(l, ks[8]);
438 l ^= fRK(r, ks[7]);
439 r ^= fRK(l, ks[6]);
440 l ^= fRK(r, ks[5]);
441 r ^= fRK(l, ks[4]);
442 l ^= fRK(r, ks[3]);
443 r ^= fRK(l, ks[2]);
444 l ^= fRK(r, ks[1]);
445 r ^= fRK(l, ks[0]);
446 }
447
448 fp_z(&r, &l);
449 return ((uint64_t) l) | (((uint64_t) r) << 32);
450 }
451
452 IMB_DLL_LOCAL
453 void
454 des_enc_cbc_basic(const void *input, void *output, const int size,
455 const uint64_t *ks, const uint64_t *ivec)
456 {
457 const uint64_t *in = input;
458 uint64_t *out = output;
459 const int nblocks = size / 8;
460 int n;
461 uint64_t iv = *ivec;
462
463 IMB_ASSERT(size >= 0);
464 IMB_ASSERT(input != NULL);
465 IMB_ASSERT(output != NULL);
466 IMB_ASSERT(ks != NULL);
467 IMB_ASSERT(ivec != NULL);
468
469 for (n = 0; n < nblocks; n++)
470 out[n] = iv = enc_dec_1(in[n] ^ iv, ks, 1 /* encrypt */);
471
472 /* *ivec = iv; */
473 iv = 0;
474 }
475
476 IMB_DLL_LOCAL
477 void
478 des_dec_cbc_basic(const void *input, void *output, const int size,
479 const uint64_t *ks, const uint64_t *ivec)
480 {
481 const uint64_t *in = input;
482 uint64_t *out = output;
483 const int nblocks = size / 8;
484 int n;
485 uint64_t iv = *ivec;
486
487 IMB_ASSERT(size >= 0);
488 IMB_ASSERT(input != NULL);
489 IMB_ASSERT(output != NULL);
490 IMB_ASSERT(ks != NULL);
491 IMB_ASSERT(ivec != NULL);
492
493 for (n = 0; n < nblocks; n++) {
494 uint64_t in_block = in[n];
495
496 out[n] = enc_dec_1(in_block, ks, 0 /* decrypt */) ^ iv;
497 iv = in_block;
498 }
499
500 /* *ivec = iv; */
501 iv = 0;
502 }
503
504 IMB_DLL_LOCAL
505 void
506 des3_enc_cbc_basic(const void *input, void *output, const int size,
507 const uint64_t *ks1, const uint64_t *ks2,
508 const uint64_t *ks3, const uint64_t *ivec)
509 {
510 const uint64_t *in = input;
511 uint64_t *out = output;
512 const int nblocks = size / 8;
513 int n;
514 uint64_t iv = *ivec;
515
516 IMB_ASSERT(size >= 0);
517 IMB_ASSERT(input != NULL);
518 IMB_ASSERT(output != NULL);
519 IMB_ASSERT(ks1 != NULL);
520 IMB_ASSERT(ks2 != NULL);
521 IMB_ASSERT(ks3 != NULL);
522 IMB_ASSERT(ivec != NULL);
523
524 for (n = 0; n < nblocks; n++) {
525 uint64_t t = in[n] ^ iv;
526
527 t = enc_dec_1(t, ks1, 1 /* encrypt */);
528 t = enc_dec_1(t, ks2, 0 /* decrypt */);
529 t = enc_dec_1(t, ks3, 1 /* encrypt */);
530 out[n] = iv = t;
531 }
532
533 /* *ivec = iv; */
534 iv = 0;
535 }
536
537 IMB_DLL_LOCAL
538 void
539 des3_dec_cbc_basic(const void *input, void *output, const int size,
540 const uint64_t *ks1, const uint64_t *ks2,
541 const uint64_t *ks3, const uint64_t *ivec)
542 {
543 const uint64_t *in = input;
544 uint64_t *out = output;
545 const int nblocks = size / 8;
546 int n;
547 uint64_t iv = *ivec;
548
549 IMB_ASSERT(size >= 0);
550 IMB_ASSERT(input != NULL);
551 IMB_ASSERT(output != NULL);
552 IMB_ASSERT(ks1 != NULL);
553 IMB_ASSERT(ks2 != NULL);
554 IMB_ASSERT(ks3 != NULL);
555 IMB_ASSERT(ivec != NULL);
556
557 for (n = 0; n < nblocks; n++) {
558 uint64_t t, next_iv;
559
560 next_iv = in[n];
561 t = in[n];
562
563 t = enc_dec_1(t, ks3, 0 /* decrypt */);
564 t = enc_dec_1(t, ks2, 1 /* encrypt */);
565 t = enc_dec_1(t, ks1, 0 /* decrypt */);
566 out[n] = t ^ iv;
567
568 iv = next_iv;
569 }
570
571 /* *ivec = iv; */
572 iv = 0;
573 }
574
575 __forceinline
576 void
577 cfb_one_basic(const void *input, void *output, const int size,
578 const uint64_t *ks, const uint64_t *ivec)
579 {
580 uint8_t *out = (uint8_t *) output;
581 const uint8_t *in = (const uint8_t *) input;
582 uint64_t t;
583
584 IMB_ASSERT(size <= 8 && size >= 0);
585 IMB_ASSERT(input != NULL);
586 IMB_ASSERT(output != NULL);
587 IMB_ASSERT(ks != NULL);
588 IMB_ASSERT(ivec != NULL);
589
590 t = enc_dec_1(*ivec, ks, 1 /* encrypt */);
591
592 /* XOR and copy in one go */
593 if (size & 1) {
594 *out++ = *in++ ^ ((uint8_t) t);
595 t >>= 8;
596 }
597
598 if (size & 2) {
599 uint16_t *out2 = (uint16_t *) out;
600 const uint16_t *in2 = (const uint16_t *) in;
601
602 *out2 = *in2 ^ ((uint16_t) t);
603 t >>= 16;
604 out += 2;
605 in += 2;
606 }
607
608 if (size & 4) {
609 uint32_t *out4 = (uint32_t *) out;
610 const uint32_t *in4 = (const uint32_t *) in;
611
612 *out4 = *in4 ^ ((uint32_t) t);
613 }
614 }
615
616 IMB_DLL_LOCAL
617 void
618 docsis_des_enc_basic(const void *input, void *output, const int size,
619 const uint64_t *ks, const uint64_t *ivec)
620 {
621 const uint64_t *in = input;
622 uint64_t *out = output;
623 const int nblocks = size / DES_BLOCK_SIZE;
624 const int partial = size & 7;
625 int n;
626 uint64_t iv = *ivec;
627
628 IMB_ASSERT(size >= 0);
629 IMB_ASSERT(input != NULL);
630 IMB_ASSERT(output != NULL);
631 IMB_ASSERT(ks != NULL);
632 IMB_ASSERT(ivec != NULL);
633
634 for (n = 0; n < nblocks; n++)
635 out[n] = iv = enc_dec_1(in[n] ^ iv, ks, 1 /* encrypt */);
636
637 if (partial) {
638 if (nblocks)
639 cfb_one_basic(&in[nblocks], &out[nblocks], partial,
640 ks, &out[nblocks - 1]);
641 else
642 cfb_one_basic(input, output, partial, ks, ivec);
643 }
644
645 /* *ivec = iv; */
646 iv = 0;
647 }
648
649 IMB_DLL_LOCAL
650 void
651 docsis_des_dec_basic(const void *input, void *output, const int size,
652 const uint64_t *ks, const uint64_t *ivec)
653 {
654 const uint64_t *in = input;
655 uint64_t *out = output;
656 const int nblocks = size / DES_BLOCK_SIZE;
657 const int partial = size & 7;
658 int n;
659 uint64_t iv = *ivec;
660
661 IMB_ASSERT(size >= 0);
662 IMB_ASSERT(input != NULL);
663 IMB_ASSERT(output != NULL);
664 IMB_ASSERT(ks != NULL);
665 IMB_ASSERT(ivec != NULL);
666
667 if (partial) {
668 if (!nblocks) {
669 /* first block is the partial one */
670 cfb_one_basic(input, output, partial, ks, ivec);
671 iv = 0;
672 return;
673 }
674 /* last block is partial */
675 cfb_one_basic(&in[nblocks], &out[nblocks], partial,
676 ks, &in[nblocks - 1]);
677 }
678
679 for (n = 0; n < nblocks; n++) {
680 uint64_t in_block = in[n];
681
682 out[n] = enc_dec_1(in_block, ks, 0 /* decrypt */) ^ iv;
683 iv = in_block;
684 }
685
686 /* *ivec = iv; */
687 iv = 0;
688 }