42 #include "netif/ppp/ppp_opts.h" 43 #if PPP_SUPPORT && LWIP_INCLUDED_POLARSSL_DES 51 #define GET_ULONG_BE(n,b,i) \ 53 (n) = ( (unsigned long) (b)[(i) ] << 24 ) \ 54 | ( (unsigned long) (b)[(i) + 1] << 16 ) \ 55 | ( (unsigned long) (b)[(i) + 2] << 8 ) \ 56 | ( (unsigned long) (b)[(i) + 3] ); \ 61 #define PUT_ULONG_BE(n,b,i) \ 63 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ 64 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ 65 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ 66 (b)[(i) + 3] = (unsigned char) ( (n) ); \ 73 static const unsigned long SB1[64] =
75 0x01010400, 0x00000000, 0x00010000, 0x01010404,
76 0x01010004, 0x00010404, 0x00000004, 0x00010000,
77 0x00000400, 0x01010400, 0x01010404, 0x00000400,
78 0x01000404, 0x01010004, 0x01000000, 0x00000004,
79 0x00000404, 0x01000400, 0x01000400, 0x00010400,
80 0x00010400, 0x01010000, 0x01010000, 0x01000404,
81 0x00010004, 0x01000004, 0x01000004, 0x00010004,
82 0x00000000, 0x00000404, 0x00010404, 0x01000000,
83 0x00010000, 0x01010404, 0x00000004, 0x01010000,
84 0x01010400, 0x01000000, 0x01000000, 0x00000400,
85 0x01010004, 0x00010000, 0x00010400, 0x01000004,
86 0x00000400, 0x00000004, 0x01000404, 0x00010404,
87 0x01010404, 0x00010004, 0x01010000, 0x01000404,
88 0x01000004, 0x00000404, 0x00010404, 0x01010400,
89 0x00000404, 0x01000400, 0x01000400, 0x00000000,
90 0x00010004, 0x00010400, 0x00000000, 0x01010004
93 static const unsigned long SB2[64] =
95 0x80108020, 0x80008000, 0x00008000, 0x00108020,
96 0x00100000, 0x00000020, 0x80100020, 0x80008020,
97 0x80000020, 0x80108020, 0x80108000, 0x80000000,
98 0x80008000, 0x00100000, 0x00000020, 0x80100020,
99 0x00108000, 0x00100020, 0x80008020, 0x00000000,
100 0x80000000, 0x00008000, 0x00108020, 0x80100000,
101 0x00100020, 0x80000020, 0x00000000, 0x00108000,
102 0x00008020, 0x80108000, 0x80100000, 0x00008020,
103 0x00000000, 0x00108020, 0x80100020, 0x00100000,
104 0x80008020, 0x80100000, 0x80108000, 0x00008000,
105 0x80100000, 0x80008000, 0x00000020, 0x80108020,
106 0x00108020, 0x00000020, 0x00008000, 0x80000000,
107 0x00008020, 0x80108000, 0x00100000, 0x80000020,
108 0x00100020, 0x80008020, 0x80000020, 0x00100020,
109 0x00108000, 0x00000000, 0x80008000, 0x00008020,
110 0x80000000, 0x80100020, 0x80108020, 0x00108000
113 static const unsigned long SB3[64] =
115 0x00000208, 0x08020200, 0x00000000, 0x08020008,
116 0x08000200, 0x00000000, 0x00020208, 0x08000200,
117 0x00020008, 0x08000008, 0x08000008, 0x00020000,
118 0x08020208, 0x00020008, 0x08020000, 0x00000208,
119 0x08000000, 0x00000008, 0x08020200, 0x00000200,
120 0x00020200, 0x08020000, 0x08020008, 0x00020208,
121 0x08000208, 0x00020200, 0x00020000, 0x08000208,
122 0x00000008, 0x08020208, 0x00000200, 0x08000000,
123 0x08020200, 0x08000000, 0x00020008, 0x00000208,
124 0x00020000, 0x08020200, 0x08000200, 0x00000000,
125 0x00000200, 0x00020008, 0x08020208, 0x08000200,
126 0x08000008, 0x00000200, 0x00000000, 0x08020008,
127 0x08000208, 0x00020000, 0x08000000, 0x08020208,
128 0x00000008, 0x00020208, 0x00020200, 0x08000008,
129 0x08020000, 0x08000208, 0x00000208, 0x08020000,
130 0x00020208, 0x00000008, 0x08020008, 0x00020200
133 static const unsigned long SB4[64] =
135 0x00802001, 0x00002081, 0x00002081, 0x00000080,
136 0x00802080, 0x00800081, 0x00800001, 0x00002001,
137 0x00000000, 0x00802000, 0x00802000, 0x00802081,
138 0x00000081, 0x00000000, 0x00800080, 0x00800001,
139 0x00000001, 0x00002000, 0x00800000, 0x00802001,
140 0x00000080, 0x00800000, 0x00002001, 0x00002080,
141 0x00800081, 0x00000001, 0x00002080, 0x00800080,
142 0x00002000, 0x00802080, 0x00802081, 0x00000081,
143 0x00800080, 0x00800001, 0x00802000, 0x00802081,
144 0x00000081, 0x00000000, 0x00000000, 0x00802000,
145 0x00002080, 0x00800080, 0x00800081, 0x00000001,
146 0x00802001, 0x00002081, 0x00002081, 0x00000080,
147 0x00802081, 0x00000081, 0x00000001, 0x00002000,
148 0x00800001, 0x00002001, 0x00802080, 0x00800081,
149 0x00002001, 0x00002080, 0x00800000, 0x00802001,
150 0x00000080, 0x00800000, 0x00002000, 0x00802080
153 static const unsigned long SB5[64] =
155 0x00000100, 0x02080100, 0x02080000, 0x42000100,
156 0x00080000, 0x00000100, 0x40000000, 0x02080000,
157 0x40080100, 0x00080000, 0x02000100, 0x40080100,
158 0x42000100, 0x42080000, 0x00080100, 0x40000000,
159 0x02000000, 0x40080000, 0x40080000, 0x00000000,
160 0x40000100, 0x42080100, 0x42080100, 0x02000100,
161 0x42080000, 0x40000100, 0x00000000, 0x42000000,
162 0x02080100, 0x02000000, 0x42000000, 0x00080100,
163 0x00080000, 0x42000100, 0x00000100, 0x02000000,
164 0x40000000, 0x02080000, 0x42000100, 0x40080100,
165 0x02000100, 0x40000000, 0x42080000, 0x02080100,
166 0x40080100, 0x00000100, 0x02000000, 0x42080000,
167 0x42080100, 0x00080100, 0x42000000, 0x42080100,
168 0x02080000, 0x00000000, 0x40080000, 0x42000000,
169 0x00080100, 0x02000100, 0x40000100, 0x00080000,
170 0x00000000, 0x40080000, 0x02080100, 0x40000100
173 static const unsigned long SB6[64] =
175 0x20000010, 0x20400000, 0x00004000, 0x20404010,
176 0x20400000, 0x00000010, 0x20404010, 0x00400000,
177 0x20004000, 0x00404010, 0x00400000, 0x20000010,
178 0x00400010, 0x20004000, 0x20000000, 0x00004010,
179 0x00000000, 0x00400010, 0x20004010, 0x00004000,
180 0x00404000, 0x20004010, 0x00000010, 0x20400010,
181 0x20400010, 0x00000000, 0x00404010, 0x20404000,
182 0x00004010, 0x00404000, 0x20404000, 0x20000000,
183 0x20004000, 0x00000010, 0x20400010, 0x00404000,
184 0x20404010, 0x00400000, 0x00004010, 0x20000010,
185 0x00400000, 0x20004000, 0x20000000, 0x00004010,
186 0x20000010, 0x20404010, 0x00404000, 0x20400000,
187 0x00404010, 0x20404000, 0x00000000, 0x20400010,
188 0x00000010, 0x00004000, 0x20400000, 0x00404010,
189 0x00004000, 0x00400010, 0x20004010, 0x00000000,
190 0x20404000, 0x20000000, 0x00400010, 0x20004010
193 static const unsigned long SB7[64] =
195 0x00200000, 0x04200002, 0x04000802, 0x00000000,
196 0x00000800, 0x04000802, 0x00200802, 0x04200800,
197 0x04200802, 0x00200000, 0x00000000, 0x04000002,
198 0x00000002, 0x04000000, 0x04200002, 0x00000802,
199 0x04000800, 0x00200802, 0x00200002, 0x04000800,
200 0x04000002, 0x04200000, 0x04200800, 0x00200002,
201 0x04200000, 0x00000800, 0x00000802, 0x04200802,
202 0x00200800, 0x00000002, 0x04000000, 0x00200800,
203 0x04000000, 0x00200800, 0x00200000, 0x04000802,
204 0x04000802, 0x04200002, 0x04200002, 0x00000002,
205 0x00200002, 0x04000000, 0x04000800, 0x00200000,
206 0x04200800, 0x00000802, 0x00200802, 0x04200800,
207 0x00000802, 0x04000002, 0x04200802, 0x04200000,
208 0x00200800, 0x00000000, 0x00000002, 0x04200802,
209 0x00000000, 0x00200802, 0x04200000, 0x00000800,
210 0x04000002, 0x04000800, 0x00000800, 0x00200002
213 static const unsigned long SB8[64] =
215 0x10001040, 0x00001000, 0x00040000, 0x10041040,
216 0x10000000, 0x10001040, 0x00000040, 0x10000000,
217 0x00040040, 0x10040000, 0x10041040, 0x00041000,
218 0x10041000, 0x00041040, 0x00001000, 0x00000040,
219 0x10040000, 0x10000040, 0x10001000, 0x00001040,
220 0x00041000, 0x00040040, 0x10040040, 0x10041000,
221 0x00001040, 0x00000000, 0x00000000, 0x10040040,
222 0x10000040, 0x10001000, 0x00041040, 0x00040000,
223 0x00041040, 0x00040000, 0x10041000, 0x00001000,
224 0x00000040, 0x10040040, 0x00001000, 0x00041040,
225 0x10001000, 0x00000040, 0x10000040, 0x10040000,
226 0x10040040, 0x10000000, 0x00040000, 0x10001040,
227 0x00000000, 0x10041040, 0x00040040, 0x10000040,
228 0x10040000, 0x10001000, 0x10001040, 0x00000000,
229 0x10041040, 0x00041000, 0x00041000, 0x00001040,
230 0x00001040, 0x00040040, 0x10000000, 0x10041000
236 static const unsigned long LHs[16] =
238 0x00000000, 0x00000001, 0x00000100, 0x00000101,
239 0x00010000, 0x00010001, 0x00010100, 0x00010101,
240 0x01000000, 0x01000001, 0x01000100, 0x01000101,
241 0x01010000, 0x01010001, 0x01010100, 0x01010101
244 static const unsigned long RHs[16] =
246 0x00000000, 0x01000000, 0x00010000, 0x01010000,
247 0x00000100, 0x01000100, 0x00010100, 0x01010100,
248 0x00000001, 0x01000001, 0x00010001, 0x01010001,
249 0x00000101, 0x01000101, 0x00010101, 0x01010101,
255 #define DES_IP(X,Y) \ 257 T = ((X >> 4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T << 4); \ 258 T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16); \ 259 T = ((Y >> 2) ^ X) & 0x33333333; X ^= T; Y ^= (T << 2); \ 260 T = ((Y >> 8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T << 8); \ 261 Y = ((Y << 1) | (Y >> 31)) & 0xFFFFFFFF; \ 262 T = (X ^ Y) & 0xAAAAAAAA; Y ^= T; X ^= T; \ 263 X = ((X << 1) | (X >> 31)) & 0xFFFFFFFF; \ 269 #define DES_FP(X,Y) \ 271 X = ((X << 31) | (X >> 1)) & 0xFFFFFFFF; \ 272 T = (X ^ Y) & 0xAAAAAAAA; X ^= T; Y ^= T; \ 273 Y = ((Y << 31) | (Y >> 1)) & 0xFFFFFFFF; \ 274 T = ((Y >> 8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T << 8); \ 275 T = ((Y >> 2) ^ X) & 0x33333333; X ^= T; Y ^= (T << 2); \ 276 T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16); \ 277 T = ((X >> 4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T << 4); \ 283 #define DES_ROUND(X,Y) \ 286 Y ^= SB8[ (T ) & 0x3F ] ^ \ 287 SB6[ (T >> 8) & 0x3F ] ^ \ 288 SB4[ (T >> 16) & 0x3F ] ^ \ 289 SB2[ (T >> 24) & 0x3F ]; \ 291 T = *SK++ ^ ((X << 28) | (X >> 4)); \ 292 Y ^= SB7[ (T ) & 0x3F ] ^ \ 293 SB5[ (T >> 8) & 0x3F ] ^ \ 294 SB3[ (T >> 16) & 0x3F ] ^ \ 295 SB1[ (T >> 24) & 0x3F ]; \ 298 #define SWAP(a,b) { unsigned long t = a; a = b; b = t; t = 0; } 300 static void des_setkey(
unsigned long SK[32],
unsigned char key[8] )
303 unsigned long X, Y, T;
305 GET_ULONG_BE( X, key, 0 );
306 GET_ULONG_BE( Y, key, 4 );
311 T = ((Y >> 4) ^ X) & 0x0F0F0F0F; X ^= T; Y ^= (T << 4);
312 T = ((Y ) ^ X) & 0x10101010; X ^= T; Y ^= (T );
314 X = (LHs[ (X ) & 0xF] << 3) | (LHs[ (X >> 8) & 0xF ] << 2)
315 | (LHs[ (X >> 16) & 0xF] << 1) | (LHs[ (X >> 24) & 0xF ] )
316 | (LHs[ (X >> 5) & 0xF] << 7) | (LHs[ (X >> 13) & 0xF ] << 6)
317 | (LHs[ (X >> 21) & 0xF] << 5) | (LHs[ (X >> 29) & 0xF ] << 4);
319 Y = (RHs[ (Y >> 1) & 0xF] << 3) | (RHs[ (Y >> 9) & 0xF ] << 2)
320 | (RHs[ (Y >> 17) & 0xF] << 1) | (RHs[ (Y >> 25) & 0xF ] )
321 | (RHs[ (Y >> 4) & 0xF] << 7) | (RHs[ (Y >> 12) & 0xF ] << 6)
322 | (RHs[ (Y >> 20) & 0xF] << 5) | (RHs[ (Y >> 28) & 0xF ] << 4);
330 for( i = 0; i < 16; i++ )
332 if( i < 2 || i == 8 || i == 15 )
334 X = ((X << 1) | (X >> 27)) & 0x0FFFFFFF;
335 Y = ((Y << 1) | (Y >> 27)) & 0x0FFFFFFF;
339 X = ((X << 2) | (X >> 26)) & 0x0FFFFFFF;
340 Y = ((Y << 2) | (Y >> 26)) & 0x0FFFFFFF;
343 *SK++ = ((X << 4) & 0x24000000) | ((X << 28) & 0x10000000)
344 | ((X << 14) & 0x08000000) | ((X << 18) & 0x02080000)
345 | ((X << 6) & 0x01000000) | ((X << 9) & 0x00200000)
346 | ((X >> 1) & 0x00100000) | ((X << 10) & 0x00040000)
347 | ((X << 2) & 0x00020000) | ((X >> 10) & 0x00010000)
348 | ((Y >> 13) & 0x00002000) | ((Y >> 4) & 0x00001000)
349 | ((Y << 6) & 0x00000800) | ((Y >> 1) & 0x00000400)
350 | ((Y >> 14) & 0x00000200) | ((Y ) & 0x00000100)
351 | ((Y >> 5) & 0x00000020) | ((Y >> 10) & 0x00000010)
352 | ((Y >> 3) & 0x00000008) | ((Y >> 18) & 0x00000004)
353 | ((Y >> 26) & 0x00000002) | ((Y >> 24) & 0x00000001);
355 *SK++ = ((X << 15) & 0x20000000) | ((X << 17) & 0x10000000)
356 | ((X << 10) & 0x08000000) | ((X << 22) & 0x04000000)
357 | ((X >> 2) & 0x02000000) | ((X << 1) & 0x01000000)
358 | ((X << 16) & 0x00200000) | ((X << 11) & 0x00100000)
359 | ((X << 3) & 0x00080000) | ((X >> 6) & 0x00040000)
360 | ((X << 15) & 0x00020000) | ((X >> 4) & 0x00010000)
361 | ((Y >> 2) & 0x00002000) | ((Y << 8) & 0x00001000)
362 | ((Y >> 14) & 0x00000808) | ((Y >> 9) & 0x00000400)
363 | ((Y ) & 0x00000200) | ((Y << 7) & 0x00000100)
364 | ((Y >> 7) & 0x00000020) | ((Y >> 3) & 0x00000011)
365 | ((Y << 2) & 0x00000004) | ((Y >> 21) & 0x00000002);
372 void des_setkey_enc( des_context *ctx,
unsigned char key[8] )
374 des_setkey( ctx->sk, key );
380 void des_setkey_dec( des_context *ctx,
unsigned char key[8] )
384 des_setkey( ctx->sk, key );
386 for( i = 0; i < 16; i += 2 )
388 SWAP( ctx->sk[i ], ctx->sk[30 - i] );
389 SWAP( ctx->sk[i + 1], ctx->sk[31 - i] );
396 void des_crypt_ecb( des_context *ctx,
397 const unsigned char input[8],
398 unsigned char output[8] )
401 unsigned long X, Y, T, *SK;
405 GET_ULONG_BE( X, input, 0 );
406 GET_ULONG_BE( Y, input, 4 );
410 for( i = 0; i < 8; i++ )
418 PUT_ULONG_BE( Y, output, 0 );
419 PUT_ULONG_BE( X, output, 4 );