2 * pppcrypt.c - PPP/DES linkage for MS-CHAP and EAP SRP-SHA1
4 * Extracted from chap_ms.c by James Carlson.
6 * Copyright (c) 1995 Eric Rosenquist. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. The name(s) of the authors of this software must not be used to
21 * endorse or promote products derived from this software without
22 * prior written permission.
24 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
25 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
26 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
27 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
28 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
29 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
30 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
38 Get7Bits(u_char *input, int startBit)
42 word = (unsigned)input[startBit / 8] << 8;
43 word |= (unsigned)input[startBit / 8 + 1];
45 word >>= 15 - (startBit % 8 + 7);
51 MakeKey(u_char *key, u_char *des_key)
53 /* key IN 56 bit DES key missing parity bits */
54 /* des_key OUT 64 bit DES key with parity bits added */
55 des_key[0] = Get7Bits(key, 0);
56 des_key[1] = Get7Bits(key, 7);
57 des_key[2] = Get7Bits(key, 14);
58 des_key[3] = Get7Bits(key, 21);
59 des_key[4] = Get7Bits(key, 28);
60 des_key[5] = Get7Bits(key, 35);
61 des_key[6] = Get7Bits(key, 42);
62 des_key[7] = Get7Bits(key, 49);
65 DES_set_odd_parity((DES_cblock *)des_key);
71 * in == 8-byte string (expanded version of the 56-bit key)
72 * out == 64-byte string where each byte is either 1 or 0
73 * Note that the low-order "bit" is always ignored by by setkey()
76 Expand(u_char *in, u_char *out)
81 for (i = 0; i < 64; in++){
83 for (j = 7; j >= 0; j--)
84 *out++ = (c >> j) & 01;
89 /* The inverse of Expand
92 Collapse(u_char *in, u_char *out)
98 for (i = 0; i < 64; i += 8, out++) {
100 for (j = 7; j >= 0; j--, in++)
107 DesSetkey(u_char *key)
110 u_char crypt_key[66];
112 MakeKey(key, des_key);
113 Expand(des_key, crypt_key);
115 setkey((const char *)crypt_key);
122 DesEncrypt(u_char *clear, u_char *cipher)
124 u_char des_input[66];
126 Expand(clear, des_input);
128 encrypt((char *)des_input, 0);
131 Collapse(des_input, cipher);
136 DesDecrypt(u_char *cipher, u_char *clear)
138 u_char des_input[66];
140 Expand(cipher, des_input);
142 encrypt((char *)des_input, 1);
145 Collapse(des_input, clear);
149 #else /* USE_CRYPT */
150 static DES_key_schedule key_schedule;
153 DesSetkey(u_char *key)
156 MakeKey(key, des_key);
157 DES_set_key(&des_key, &key_schedule);
162 DesEncrypt(u_char *clear, u_char *cipher)
164 DES_ecb_encrypt((DES_cblock *)clear, (DES_cblock *)cipher,
170 DesDecrypt(u_char *cipher, u_char *clear)
172 DES_ecb_encrypt((DES_cblock *)cipher, (DES_cblock *)clear,
177 #endif /* USE_CRYPT */