1195972f6Sopenharmony_ci/*
2195972f6Sopenharmony_ci *  FIPS-46-3 compliant Triple-DES implementation
3195972f6Sopenharmony_ci *
4195972f6Sopenharmony_ci *  Based on XySSL: Copyright (C) 2006-2008  Christophe Devine
5195972f6Sopenharmony_ci *
6195972f6Sopenharmony_ci *  Copyright (C) 2009  Paul Bakker <polarssl_maintainer at polarssl dot org>
7195972f6Sopenharmony_ci *
8195972f6Sopenharmony_ci *  All rights reserved.
9195972f6Sopenharmony_ci *
10195972f6Sopenharmony_ci *  Redistribution and use in source and binary forms, with or without
11195972f6Sopenharmony_ci *  modification, are permitted provided that the following conditions
12195972f6Sopenharmony_ci *  are met:
13195972f6Sopenharmony_ci *
14195972f6Sopenharmony_ci *    * Redistributions of source code must retain the above copyright
15195972f6Sopenharmony_ci *      notice, this list of conditions and the following disclaimer.
16195972f6Sopenharmony_ci *    * Redistributions in binary form must reproduce the above copyright
17195972f6Sopenharmony_ci *      notice, this list of conditions and the following disclaimer in the
18195972f6Sopenharmony_ci *      documentation and/or other materials provided with the distribution.
19195972f6Sopenharmony_ci *    * Neither the names of PolarSSL or XySSL nor the names of its contributors
20195972f6Sopenharmony_ci *      may be used to endorse or promote products derived from this software
21195972f6Sopenharmony_ci *      without specific prior written permission.
22195972f6Sopenharmony_ci *
23195972f6Sopenharmony_ci *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24195972f6Sopenharmony_ci *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25195972f6Sopenharmony_ci *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26195972f6Sopenharmony_ci *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27195972f6Sopenharmony_ci *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28195972f6Sopenharmony_ci *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29195972f6Sopenharmony_ci *  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30195972f6Sopenharmony_ci *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31195972f6Sopenharmony_ci *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32195972f6Sopenharmony_ci *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33195972f6Sopenharmony_ci *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34195972f6Sopenharmony_ci */
35195972f6Sopenharmony_ci/*
36195972f6Sopenharmony_ci *  DES, on which TDES is based, was originally designed by Horst Feistel
37195972f6Sopenharmony_ci *  at IBM in 1974, and was adopted as a standard by NIST (formerly NBS).
38195972f6Sopenharmony_ci *
39195972f6Sopenharmony_ci *  http://csrc.nist.gov/publications/fips/fips46-3/fips46-3.pdf
40195972f6Sopenharmony_ci */
41195972f6Sopenharmony_ci
42195972f6Sopenharmony_ci#include "netif/ppp/ppp_opts.h"
43195972f6Sopenharmony_ci#if PPP_SUPPORT && LWIP_INCLUDED_POLARSSL_DES
44195972f6Sopenharmony_ci
45195972f6Sopenharmony_ci#include "netif/ppp/polarssl/des.h"
46195972f6Sopenharmony_ci
47195972f6Sopenharmony_ci/*
48195972f6Sopenharmony_ci * 32-bit integer manipulation macros (big endian)
49195972f6Sopenharmony_ci */
50195972f6Sopenharmony_ci#ifndef GET_ULONG_BE
51195972f6Sopenharmony_ci#define GET_ULONG_BE(n,b,i)                             \
52195972f6Sopenharmony_ci{                                                       \
53195972f6Sopenharmony_ci    (n) = ( (unsigned long) (b)[(i)    ] << 24 )        \
54195972f6Sopenharmony_ci        | ( (unsigned long) (b)[(i) + 1] << 16 )        \
55195972f6Sopenharmony_ci        | ( (unsigned long) (b)[(i) + 2] <<  8 )        \
56195972f6Sopenharmony_ci        | ( (unsigned long) (b)[(i) + 3]       );       \
57195972f6Sopenharmony_ci}
58195972f6Sopenharmony_ci#endif
59195972f6Sopenharmony_ci
60195972f6Sopenharmony_ci#ifndef PUT_ULONG_BE
61195972f6Sopenharmony_ci#define PUT_ULONG_BE(n,b,i)                             \
62195972f6Sopenharmony_ci{                                                       \
63195972f6Sopenharmony_ci    (b)[(i)    ] = (unsigned char) ( (n) >> 24 );       \
64195972f6Sopenharmony_ci    (b)[(i) + 1] = (unsigned char) ( (n) >> 16 );       \
65195972f6Sopenharmony_ci    (b)[(i) + 2] = (unsigned char) ( (n) >>  8 );       \
66195972f6Sopenharmony_ci    (b)[(i) + 3] = (unsigned char) ( (n)       );       \
67195972f6Sopenharmony_ci}
68195972f6Sopenharmony_ci#endif
69195972f6Sopenharmony_ci
70195972f6Sopenharmony_ci/*
71195972f6Sopenharmony_ci * Expanded DES S-boxes
72195972f6Sopenharmony_ci */
73195972f6Sopenharmony_cistatic const unsigned long SB1[64] =
74195972f6Sopenharmony_ci{
75195972f6Sopenharmony_ci    0x01010400, 0x00000000, 0x00010000, 0x01010404,
76195972f6Sopenharmony_ci    0x01010004, 0x00010404, 0x00000004, 0x00010000,
77195972f6Sopenharmony_ci    0x00000400, 0x01010400, 0x01010404, 0x00000400,
78195972f6Sopenharmony_ci    0x01000404, 0x01010004, 0x01000000, 0x00000004,
79195972f6Sopenharmony_ci    0x00000404, 0x01000400, 0x01000400, 0x00010400,
80195972f6Sopenharmony_ci    0x00010400, 0x01010000, 0x01010000, 0x01000404,
81195972f6Sopenharmony_ci    0x00010004, 0x01000004, 0x01000004, 0x00010004,
82195972f6Sopenharmony_ci    0x00000000, 0x00000404, 0x00010404, 0x01000000,
83195972f6Sopenharmony_ci    0x00010000, 0x01010404, 0x00000004, 0x01010000,
84195972f6Sopenharmony_ci    0x01010400, 0x01000000, 0x01000000, 0x00000400,
85195972f6Sopenharmony_ci    0x01010004, 0x00010000, 0x00010400, 0x01000004,
86195972f6Sopenharmony_ci    0x00000400, 0x00000004, 0x01000404, 0x00010404,
87195972f6Sopenharmony_ci    0x01010404, 0x00010004, 0x01010000, 0x01000404,
88195972f6Sopenharmony_ci    0x01000004, 0x00000404, 0x00010404, 0x01010400,
89195972f6Sopenharmony_ci    0x00000404, 0x01000400, 0x01000400, 0x00000000,
90195972f6Sopenharmony_ci    0x00010004, 0x00010400, 0x00000000, 0x01010004
91195972f6Sopenharmony_ci};
92195972f6Sopenharmony_ci
93195972f6Sopenharmony_cistatic const unsigned long SB2[64] =
94195972f6Sopenharmony_ci{
95195972f6Sopenharmony_ci    0x80108020, 0x80008000, 0x00008000, 0x00108020,
96195972f6Sopenharmony_ci    0x00100000, 0x00000020, 0x80100020, 0x80008020,
97195972f6Sopenharmony_ci    0x80000020, 0x80108020, 0x80108000, 0x80000000,
98195972f6Sopenharmony_ci    0x80008000, 0x00100000, 0x00000020, 0x80100020,
99195972f6Sopenharmony_ci    0x00108000, 0x00100020, 0x80008020, 0x00000000,
100195972f6Sopenharmony_ci    0x80000000, 0x00008000, 0x00108020, 0x80100000,
101195972f6Sopenharmony_ci    0x00100020, 0x80000020, 0x00000000, 0x00108000,
102195972f6Sopenharmony_ci    0x00008020, 0x80108000, 0x80100000, 0x00008020,
103195972f6Sopenharmony_ci    0x00000000, 0x00108020, 0x80100020, 0x00100000,
104195972f6Sopenharmony_ci    0x80008020, 0x80100000, 0x80108000, 0x00008000,
105195972f6Sopenharmony_ci    0x80100000, 0x80008000, 0x00000020, 0x80108020,
106195972f6Sopenharmony_ci    0x00108020, 0x00000020, 0x00008000, 0x80000000,
107195972f6Sopenharmony_ci    0x00008020, 0x80108000, 0x00100000, 0x80000020,
108195972f6Sopenharmony_ci    0x00100020, 0x80008020, 0x80000020, 0x00100020,
109195972f6Sopenharmony_ci    0x00108000, 0x00000000, 0x80008000, 0x00008020,
110195972f6Sopenharmony_ci    0x80000000, 0x80100020, 0x80108020, 0x00108000
111195972f6Sopenharmony_ci};
112195972f6Sopenharmony_ci
113195972f6Sopenharmony_cistatic const unsigned long SB3[64] =
114195972f6Sopenharmony_ci{
115195972f6Sopenharmony_ci    0x00000208, 0x08020200, 0x00000000, 0x08020008,
116195972f6Sopenharmony_ci    0x08000200, 0x00000000, 0x00020208, 0x08000200,
117195972f6Sopenharmony_ci    0x00020008, 0x08000008, 0x08000008, 0x00020000,
118195972f6Sopenharmony_ci    0x08020208, 0x00020008, 0x08020000, 0x00000208,
119195972f6Sopenharmony_ci    0x08000000, 0x00000008, 0x08020200, 0x00000200,
120195972f6Sopenharmony_ci    0x00020200, 0x08020000, 0x08020008, 0x00020208,
121195972f6Sopenharmony_ci    0x08000208, 0x00020200, 0x00020000, 0x08000208,
122195972f6Sopenharmony_ci    0x00000008, 0x08020208, 0x00000200, 0x08000000,
123195972f6Sopenharmony_ci    0x08020200, 0x08000000, 0x00020008, 0x00000208,
124195972f6Sopenharmony_ci    0x00020000, 0x08020200, 0x08000200, 0x00000000,
125195972f6Sopenharmony_ci    0x00000200, 0x00020008, 0x08020208, 0x08000200,
126195972f6Sopenharmony_ci    0x08000008, 0x00000200, 0x00000000, 0x08020008,
127195972f6Sopenharmony_ci    0x08000208, 0x00020000, 0x08000000, 0x08020208,
128195972f6Sopenharmony_ci    0x00000008, 0x00020208, 0x00020200, 0x08000008,
129195972f6Sopenharmony_ci    0x08020000, 0x08000208, 0x00000208, 0x08020000,
130195972f6Sopenharmony_ci    0x00020208, 0x00000008, 0x08020008, 0x00020200
131195972f6Sopenharmony_ci};
132195972f6Sopenharmony_ci
133195972f6Sopenharmony_cistatic const unsigned long SB4[64] =
134195972f6Sopenharmony_ci{
135195972f6Sopenharmony_ci    0x00802001, 0x00002081, 0x00002081, 0x00000080,
136195972f6Sopenharmony_ci    0x00802080, 0x00800081, 0x00800001, 0x00002001,
137195972f6Sopenharmony_ci    0x00000000, 0x00802000, 0x00802000, 0x00802081,
138195972f6Sopenharmony_ci    0x00000081, 0x00000000, 0x00800080, 0x00800001,
139195972f6Sopenharmony_ci    0x00000001, 0x00002000, 0x00800000, 0x00802001,
140195972f6Sopenharmony_ci    0x00000080, 0x00800000, 0x00002001, 0x00002080,
141195972f6Sopenharmony_ci    0x00800081, 0x00000001, 0x00002080, 0x00800080,
142195972f6Sopenharmony_ci    0x00002000, 0x00802080, 0x00802081, 0x00000081,
143195972f6Sopenharmony_ci    0x00800080, 0x00800001, 0x00802000, 0x00802081,
144195972f6Sopenharmony_ci    0x00000081, 0x00000000, 0x00000000, 0x00802000,
145195972f6Sopenharmony_ci    0x00002080, 0x00800080, 0x00800081, 0x00000001,
146195972f6Sopenharmony_ci    0x00802001, 0x00002081, 0x00002081, 0x00000080,
147195972f6Sopenharmony_ci    0x00802081, 0x00000081, 0x00000001, 0x00002000,
148195972f6Sopenharmony_ci    0x00800001, 0x00002001, 0x00802080, 0x00800081,
149195972f6Sopenharmony_ci    0x00002001, 0x00002080, 0x00800000, 0x00802001,
150195972f6Sopenharmony_ci    0x00000080, 0x00800000, 0x00002000, 0x00802080
151195972f6Sopenharmony_ci};
152195972f6Sopenharmony_ci
153195972f6Sopenharmony_cistatic const unsigned long SB5[64] =
154195972f6Sopenharmony_ci{
155195972f6Sopenharmony_ci    0x00000100, 0x02080100, 0x02080000, 0x42000100,
156195972f6Sopenharmony_ci    0x00080000, 0x00000100, 0x40000000, 0x02080000,
157195972f6Sopenharmony_ci    0x40080100, 0x00080000, 0x02000100, 0x40080100,
158195972f6Sopenharmony_ci    0x42000100, 0x42080000, 0x00080100, 0x40000000,
159195972f6Sopenharmony_ci    0x02000000, 0x40080000, 0x40080000, 0x00000000,
160195972f6Sopenharmony_ci    0x40000100, 0x42080100, 0x42080100, 0x02000100,
161195972f6Sopenharmony_ci    0x42080000, 0x40000100, 0x00000000, 0x42000000,
162195972f6Sopenharmony_ci    0x02080100, 0x02000000, 0x42000000, 0x00080100,
163195972f6Sopenharmony_ci    0x00080000, 0x42000100, 0x00000100, 0x02000000,
164195972f6Sopenharmony_ci    0x40000000, 0x02080000, 0x42000100, 0x40080100,
165195972f6Sopenharmony_ci    0x02000100, 0x40000000, 0x42080000, 0x02080100,
166195972f6Sopenharmony_ci    0x40080100, 0x00000100, 0x02000000, 0x42080000,
167195972f6Sopenharmony_ci    0x42080100, 0x00080100, 0x42000000, 0x42080100,
168195972f6Sopenharmony_ci    0x02080000, 0x00000000, 0x40080000, 0x42000000,
169195972f6Sopenharmony_ci    0x00080100, 0x02000100, 0x40000100, 0x00080000,
170195972f6Sopenharmony_ci    0x00000000, 0x40080000, 0x02080100, 0x40000100
171195972f6Sopenharmony_ci};
172195972f6Sopenharmony_ci
173195972f6Sopenharmony_cistatic const unsigned long SB6[64] =
174195972f6Sopenharmony_ci{
175195972f6Sopenharmony_ci    0x20000010, 0x20400000, 0x00004000, 0x20404010,
176195972f6Sopenharmony_ci    0x20400000, 0x00000010, 0x20404010, 0x00400000,
177195972f6Sopenharmony_ci    0x20004000, 0x00404010, 0x00400000, 0x20000010,
178195972f6Sopenharmony_ci    0x00400010, 0x20004000, 0x20000000, 0x00004010,
179195972f6Sopenharmony_ci    0x00000000, 0x00400010, 0x20004010, 0x00004000,
180195972f6Sopenharmony_ci    0x00404000, 0x20004010, 0x00000010, 0x20400010,
181195972f6Sopenharmony_ci    0x20400010, 0x00000000, 0x00404010, 0x20404000,
182195972f6Sopenharmony_ci    0x00004010, 0x00404000, 0x20404000, 0x20000000,
183195972f6Sopenharmony_ci    0x20004000, 0x00000010, 0x20400010, 0x00404000,
184195972f6Sopenharmony_ci    0x20404010, 0x00400000, 0x00004010, 0x20000010,
185195972f6Sopenharmony_ci    0x00400000, 0x20004000, 0x20000000, 0x00004010,
186195972f6Sopenharmony_ci    0x20000010, 0x20404010, 0x00404000, 0x20400000,
187195972f6Sopenharmony_ci    0x00404010, 0x20404000, 0x00000000, 0x20400010,
188195972f6Sopenharmony_ci    0x00000010, 0x00004000, 0x20400000, 0x00404010,
189195972f6Sopenharmony_ci    0x00004000, 0x00400010, 0x20004010, 0x00000000,
190195972f6Sopenharmony_ci    0x20404000, 0x20000000, 0x00400010, 0x20004010
191195972f6Sopenharmony_ci};
192195972f6Sopenharmony_ci
193195972f6Sopenharmony_cistatic const unsigned long SB7[64] =
194195972f6Sopenharmony_ci{
195195972f6Sopenharmony_ci    0x00200000, 0x04200002, 0x04000802, 0x00000000,
196195972f6Sopenharmony_ci    0x00000800, 0x04000802, 0x00200802, 0x04200800,
197195972f6Sopenharmony_ci    0x04200802, 0x00200000, 0x00000000, 0x04000002,
198195972f6Sopenharmony_ci    0x00000002, 0x04000000, 0x04200002, 0x00000802,
199195972f6Sopenharmony_ci    0x04000800, 0x00200802, 0x00200002, 0x04000800,
200195972f6Sopenharmony_ci    0x04000002, 0x04200000, 0x04200800, 0x00200002,
201195972f6Sopenharmony_ci    0x04200000, 0x00000800, 0x00000802, 0x04200802,
202195972f6Sopenharmony_ci    0x00200800, 0x00000002, 0x04000000, 0x00200800,
203195972f6Sopenharmony_ci    0x04000000, 0x00200800, 0x00200000, 0x04000802,
204195972f6Sopenharmony_ci    0x04000802, 0x04200002, 0x04200002, 0x00000002,
205195972f6Sopenharmony_ci    0x00200002, 0x04000000, 0x04000800, 0x00200000,
206195972f6Sopenharmony_ci    0x04200800, 0x00000802, 0x00200802, 0x04200800,
207195972f6Sopenharmony_ci    0x00000802, 0x04000002, 0x04200802, 0x04200000,
208195972f6Sopenharmony_ci    0x00200800, 0x00000000, 0x00000002, 0x04200802,
209195972f6Sopenharmony_ci    0x00000000, 0x00200802, 0x04200000, 0x00000800,
210195972f6Sopenharmony_ci    0x04000002, 0x04000800, 0x00000800, 0x00200002
211195972f6Sopenharmony_ci};
212195972f6Sopenharmony_ci
213195972f6Sopenharmony_cistatic const unsigned long SB8[64] =
214195972f6Sopenharmony_ci{
215195972f6Sopenharmony_ci    0x10001040, 0x00001000, 0x00040000, 0x10041040,
216195972f6Sopenharmony_ci    0x10000000, 0x10001040, 0x00000040, 0x10000000,
217195972f6Sopenharmony_ci    0x00040040, 0x10040000, 0x10041040, 0x00041000,
218195972f6Sopenharmony_ci    0x10041000, 0x00041040, 0x00001000, 0x00000040,
219195972f6Sopenharmony_ci    0x10040000, 0x10000040, 0x10001000, 0x00001040,
220195972f6Sopenharmony_ci    0x00041000, 0x00040040, 0x10040040, 0x10041000,
221195972f6Sopenharmony_ci    0x00001040, 0x00000000, 0x00000000, 0x10040040,
222195972f6Sopenharmony_ci    0x10000040, 0x10001000, 0x00041040, 0x00040000,
223195972f6Sopenharmony_ci    0x00041040, 0x00040000, 0x10041000, 0x00001000,
224195972f6Sopenharmony_ci    0x00000040, 0x10040040, 0x00001000, 0x00041040,
225195972f6Sopenharmony_ci    0x10001000, 0x00000040, 0x10000040, 0x10040000,
226195972f6Sopenharmony_ci    0x10040040, 0x10000000, 0x00040000, 0x10001040,
227195972f6Sopenharmony_ci    0x00000000, 0x10041040, 0x00040040, 0x10000040,
228195972f6Sopenharmony_ci    0x10040000, 0x10001000, 0x10001040, 0x00000000,
229195972f6Sopenharmony_ci    0x10041040, 0x00041000, 0x00041000, 0x00001040,
230195972f6Sopenharmony_ci    0x00001040, 0x00040040, 0x10000000, 0x10041000
231195972f6Sopenharmony_ci};
232195972f6Sopenharmony_ci
233195972f6Sopenharmony_ci/*
234195972f6Sopenharmony_ci * PC1: left and right halves bit-swap
235195972f6Sopenharmony_ci */
236195972f6Sopenharmony_cistatic const unsigned long LHs[16] =
237195972f6Sopenharmony_ci{
238195972f6Sopenharmony_ci    0x00000000, 0x00000001, 0x00000100, 0x00000101,
239195972f6Sopenharmony_ci    0x00010000, 0x00010001, 0x00010100, 0x00010101,
240195972f6Sopenharmony_ci    0x01000000, 0x01000001, 0x01000100, 0x01000101,
241195972f6Sopenharmony_ci    0x01010000, 0x01010001, 0x01010100, 0x01010101
242195972f6Sopenharmony_ci};
243195972f6Sopenharmony_ci
244195972f6Sopenharmony_cistatic const unsigned long RHs[16] =
245195972f6Sopenharmony_ci{
246195972f6Sopenharmony_ci    0x00000000, 0x01000000, 0x00010000, 0x01010000,
247195972f6Sopenharmony_ci    0x00000100, 0x01000100, 0x00010100, 0x01010100,
248195972f6Sopenharmony_ci    0x00000001, 0x01000001, 0x00010001, 0x01010001,
249195972f6Sopenharmony_ci    0x00000101, 0x01000101, 0x00010101, 0x01010101,
250195972f6Sopenharmony_ci};
251195972f6Sopenharmony_ci
252195972f6Sopenharmony_ci/*
253195972f6Sopenharmony_ci * Initial Permutation macro
254195972f6Sopenharmony_ci */
255195972f6Sopenharmony_ci#define DES_IP(X,Y)                                             \
256195972f6Sopenharmony_ci{                                                               \
257195972f6Sopenharmony_ci    T = ((X >>  4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T <<  4);   \
258195972f6Sopenharmony_ci    T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16);   \
259195972f6Sopenharmony_ci    T = ((Y >>  2) ^ X) & 0x33333333; X ^= T; Y ^= (T <<  2);   \
260195972f6Sopenharmony_ci    T = ((Y >>  8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T <<  8);   \
261195972f6Sopenharmony_ci    Y = ((Y << 1) | (Y >> 31)) & 0xFFFFFFFF;                    \
262195972f6Sopenharmony_ci    T = (X ^ Y) & 0xAAAAAAAA; Y ^= T; X ^= T;                   \
263195972f6Sopenharmony_ci    X = ((X << 1) | (X >> 31)) & 0xFFFFFFFF;                    \
264195972f6Sopenharmony_ci}
265195972f6Sopenharmony_ci
266195972f6Sopenharmony_ci/*
267195972f6Sopenharmony_ci * Final Permutation macro
268195972f6Sopenharmony_ci */
269195972f6Sopenharmony_ci#define DES_FP(X,Y)                                             \
270195972f6Sopenharmony_ci{                                                               \
271195972f6Sopenharmony_ci    X = ((X << 31) | (X >> 1)) & 0xFFFFFFFF;                    \
272195972f6Sopenharmony_ci    T = (X ^ Y) & 0xAAAAAAAA; X ^= T; Y ^= T;                   \
273195972f6Sopenharmony_ci    Y = ((Y << 31) | (Y >> 1)) & 0xFFFFFFFF;                    \
274195972f6Sopenharmony_ci    T = ((Y >>  8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T <<  8);   \
275195972f6Sopenharmony_ci    T = ((Y >>  2) ^ X) & 0x33333333; X ^= T; Y ^= (T <<  2);   \
276195972f6Sopenharmony_ci    T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16);   \
277195972f6Sopenharmony_ci    T = ((X >>  4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T <<  4);   \
278195972f6Sopenharmony_ci}
279195972f6Sopenharmony_ci
280195972f6Sopenharmony_ci/*
281195972f6Sopenharmony_ci * DES round macro
282195972f6Sopenharmony_ci */
283195972f6Sopenharmony_ci#define DES_ROUND(X,Y)                          \
284195972f6Sopenharmony_ci{                                               \
285195972f6Sopenharmony_ci    T = *SK++ ^ X;                              \
286195972f6Sopenharmony_ci    Y ^= SB8[ (T      ) & 0x3F ] ^              \
287195972f6Sopenharmony_ci         SB6[ (T >>  8) & 0x3F ] ^              \
288195972f6Sopenharmony_ci         SB4[ (T >> 16) & 0x3F ] ^              \
289195972f6Sopenharmony_ci         SB2[ (T >> 24) & 0x3F ];               \
290195972f6Sopenharmony_ci                                                \
291195972f6Sopenharmony_ci    T = *SK++ ^ ((X << 28) | (X >> 4));         \
292195972f6Sopenharmony_ci    Y ^= SB7[ (T      ) & 0x3F ] ^              \
293195972f6Sopenharmony_ci         SB5[ (T >>  8) & 0x3F ] ^              \
294195972f6Sopenharmony_ci         SB3[ (T >> 16) & 0x3F ] ^              \
295195972f6Sopenharmony_ci         SB1[ (T >> 24) & 0x3F ];               \
296195972f6Sopenharmony_ci}
297195972f6Sopenharmony_ci
298195972f6Sopenharmony_ci#define SWAP(a,b) { unsigned long t = a; a = b; b = t; t = 0; }
299195972f6Sopenharmony_ci
300195972f6Sopenharmony_cistatic void des_setkey( unsigned long SK[32], unsigned char key[8] )
301195972f6Sopenharmony_ci{
302195972f6Sopenharmony_ci    int i;
303195972f6Sopenharmony_ci    unsigned long X, Y, T;
304195972f6Sopenharmony_ci
305195972f6Sopenharmony_ci    GET_ULONG_BE( X, key, 0 );
306195972f6Sopenharmony_ci    GET_ULONG_BE( Y, key, 4 );
307195972f6Sopenharmony_ci
308195972f6Sopenharmony_ci    /*
309195972f6Sopenharmony_ci     * Permuted Choice 1
310195972f6Sopenharmony_ci     */
311195972f6Sopenharmony_ci    T =  ((Y >>  4) ^ X) & 0x0F0F0F0F;  X ^= T; Y ^= (T <<  4);
312195972f6Sopenharmony_ci    T =  ((Y      ) ^ X) & 0x10101010;  X ^= T; Y ^= (T      );
313195972f6Sopenharmony_ci
314195972f6Sopenharmony_ci    X =   (LHs[ (X      ) & 0xF] << 3) | (LHs[ (X >>  8) & 0xF ] << 2)
315195972f6Sopenharmony_ci        | (LHs[ (X >> 16) & 0xF] << 1) | (LHs[ (X >> 24) & 0xF ]     )
316195972f6Sopenharmony_ci        | (LHs[ (X >>  5) & 0xF] << 7) | (LHs[ (X >> 13) & 0xF ] << 6)
317195972f6Sopenharmony_ci        | (LHs[ (X >> 21) & 0xF] << 5) | (LHs[ (X >> 29) & 0xF ] << 4);
318195972f6Sopenharmony_ci
319195972f6Sopenharmony_ci    Y =   (RHs[ (Y >>  1) & 0xF] << 3) | (RHs[ (Y >>  9) & 0xF ] << 2)
320195972f6Sopenharmony_ci        | (RHs[ (Y >> 17) & 0xF] << 1) | (RHs[ (Y >> 25) & 0xF ]     )
321195972f6Sopenharmony_ci        | (RHs[ (Y >>  4) & 0xF] << 7) | (RHs[ (Y >> 12) & 0xF ] << 6)
322195972f6Sopenharmony_ci        | (RHs[ (Y >> 20) & 0xF] << 5) | (RHs[ (Y >> 28) & 0xF ] << 4);
323195972f6Sopenharmony_ci
324195972f6Sopenharmony_ci    X &= 0x0FFFFFFF;
325195972f6Sopenharmony_ci    Y &= 0x0FFFFFFF;
326195972f6Sopenharmony_ci
327195972f6Sopenharmony_ci    /*
328195972f6Sopenharmony_ci     * calculate subkeys
329195972f6Sopenharmony_ci     */
330195972f6Sopenharmony_ci    for( i = 0; i < 16; i++ )
331195972f6Sopenharmony_ci    {
332195972f6Sopenharmony_ci        if( i < 2 || i == 8 || i == 15 )
333195972f6Sopenharmony_ci        {
334195972f6Sopenharmony_ci            X = ((X <<  1) | (X >> 27)) & 0x0FFFFFFF;
335195972f6Sopenharmony_ci            Y = ((Y <<  1) | (Y >> 27)) & 0x0FFFFFFF;
336195972f6Sopenharmony_ci        }
337195972f6Sopenharmony_ci        else
338195972f6Sopenharmony_ci        {
339195972f6Sopenharmony_ci            X = ((X <<  2) | (X >> 26)) & 0x0FFFFFFF;
340195972f6Sopenharmony_ci            Y = ((Y <<  2) | (Y >> 26)) & 0x0FFFFFFF;
341195972f6Sopenharmony_ci        }
342195972f6Sopenharmony_ci
343195972f6Sopenharmony_ci        *SK++ =   ((X <<  4) & 0x24000000) | ((X << 28) & 0x10000000)
344195972f6Sopenharmony_ci                | ((X << 14) & 0x08000000) | ((X << 18) & 0x02080000)
345195972f6Sopenharmony_ci                | ((X <<  6) & 0x01000000) | ((X <<  9) & 0x00200000)
346195972f6Sopenharmony_ci                | ((X >>  1) & 0x00100000) | ((X << 10) & 0x00040000)
347195972f6Sopenharmony_ci                | ((X <<  2) & 0x00020000) | ((X >> 10) & 0x00010000)
348195972f6Sopenharmony_ci                | ((Y >> 13) & 0x00002000) | ((Y >>  4) & 0x00001000)
349195972f6Sopenharmony_ci                | ((Y <<  6) & 0x00000800) | ((Y >>  1) & 0x00000400)
350195972f6Sopenharmony_ci                | ((Y >> 14) & 0x00000200) | ((Y      ) & 0x00000100)
351195972f6Sopenharmony_ci                | ((Y >>  5) & 0x00000020) | ((Y >> 10) & 0x00000010)
352195972f6Sopenharmony_ci                | ((Y >>  3) & 0x00000008) | ((Y >> 18) & 0x00000004)
353195972f6Sopenharmony_ci                | ((Y >> 26) & 0x00000002) | ((Y >> 24) & 0x00000001);
354195972f6Sopenharmony_ci
355195972f6Sopenharmony_ci        *SK++ =   ((X << 15) & 0x20000000) | ((X << 17) & 0x10000000)
356195972f6Sopenharmony_ci                | ((X << 10) & 0x08000000) | ((X << 22) & 0x04000000)
357195972f6Sopenharmony_ci                | ((X >>  2) & 0x02000000) | ((X <<  1) & 0x01000000)
358195972f6Sopenharmony_ci                | ((X << 16) & 0x00200000) | ((X << 11) & 0x00100000)
359195972f6Sopenharmony_ci                | ((X <<  3) & 0x00080000) | ((X >>  6) & 0x00040000)
360195972f6Sopenharmony_ci                | ((X << 15) & 0x00020000) | ((X >>  4) & 0x00010000)
361195972f6Sopenharmony_ci                | ((Y >>  2) & 0x00002000) | ((Y <<  8) & 0x00001000)
362195972f6Sopenharmony_ci                | ((Y >> 14) & 0x00000808) | ((Y >>  9) & 0x00000400)
363195972f6Sopenharmony_ci                | ((Y      ) & 0x00000200) | ((Y <<  7) & 0x00000100)
364195972f6Sopenharmony_ci                | ((Y >>  7) & 0x00000020) | ((Y >>  3) & 0x00000011)
365195972f6Sopenharmony_ci                | ((Y <<  2) & 0x00000004) | ((Y >> 21) & 0x00000002);
366195972f6Sopenharmony_ci    }
367195972f6Sopenharmony_ci}
368195972f6Sopenharmony_ci
369195972f6Sopenharmony_ci/*
370195972f6Sopenharmony_ci * DES key schedule (56-bit, encryption)
371195972f6Sopenharmony_ci */
372195972f6Sopenharmony_civoid des_setkey_enc( des_context *ctx, unsigned char key[8] )
373195972f6Sopenharmony_ci{
374195972f6Sopenharmony_ci    des_setkey( ctx->sk, key );
375195972f6Sopenharmony_ci}
376195972f6Sopenharmony_ci
377195972f6Sopenharmony_ci/*
378195972f6Sopenharmony_ci * DES key schedule (56-bit, decryption)
379195972f6Sopenharmony_ci */
380195972f6Sopenharmony_civoid des_setkey_dec( des_context *ctx, unsigned char key[8] )
381195972f6Sopenharmony_ci{
382195972f6Sopenharmony_ci    int i;
383195972f6Sopenharmony_ci
384195972f6Sopenharmony_ci    des_setkey( ctx->sk, key );
385195972f6Sopenharmony_ci
386195972f6Sopenharmony_ci    for( i = 0; i < 16; i += 2 )
387195972f6Sopenharmony_ci    {
388195972f6Sopenharmony_ci        SWAP( ctx->sk[i    ], ctx->sk[30 - i] );
389195972f6Sopenharmony_ci        SWAP( ctx->sk[i + 1], ctx->sk[31 - i] );
390195972f6Sopenharmony_ci    }
391195972f6Sopenharmony_ci}
392195972f6Sopenharmony_ci
393195972f6Sopenharmony_ci/*
394195972f6Sopenharmony_ci * DES-ECB block encryption/decryption
395195972f6Sopenharmony_ci */
396195972f6Sopenharmony_civoid des_crypt_ecb( des_context *ctx,
397195972f6Sopenharmony_ci                    const unsigned char input[8],
398195972f6Sopenharmony_ci                    unsigned char output[8] )
399195972f6Sopenharmony_ci{
400195972f6Sopenharmony_ci    int i;
401195972f6Sopenharmony_ci    unsigned long X, Y, T, *SK;
402195972f6Sopenharmony_ci
403195972f6Sopenharmony_ci    SK = ctx->sk;
404195972f6Sopenharmony_ci
405195972f6Sopenharmony_ci    GET_ULONG_BE( X, input, 0 );
406195972f6Sopenharmony_ci    GET_ULONG_BE( Y, input, 4 );
407195972f6Sopenharmony_ci
408195972f6Sopenharmony_ci    DES_IP( X, Y );
409195972f6Sopenharmony_ci
410195972f6Sopenharmony_ci    for( i = 0; i < 8; i++ )
411195972f6Sopenharmony_ci    {
412195972f6Sopenharmony_ci        DES_ROUND( Y, X );
413195972f6Sopenharmony_ci        DES_ROUND( X, Y );
414195972f6Sopenharmony_ci    }
415195972f6Sopenharmony_ci
416195972f6Sopenharmony_ci    DES_FP( Y, X );
417195972f6Sopenharmony_ci
418195972f6Sopenharmony_ci    PUT_ULONG_BE( Y, output, 0 );
419195972f6Sopenharmony_ci    PUT_ULONG_BE( X, output, 4 );
420195972f6Sopenharmony_ci}
421195972f6Sopenharmony_ci
422195972f6Sopenharmony_ci#endif /* PPP_SUPPORT && LWIP_INCLUDED_POLARSSL_DES */
423