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.
43 Get7Bits(u_char *input, int startBit)
47 word = (unsigned)input[startBit / 8] << 8;
48 word |= (unsigned)input[startBit / 8 + 1];
50 word >>= 15 - (startBit % 8 + 7);
56 MakeKey(u_char *key, u_char *des_key)
58 /* key IN 56 bit DES key missing parity bits */
59 /* des_key OUT 64 bit DES key with parity bits added */
60 des_key[0] = Get7Bits(key, 0);
61 des_key[1] = Get7Bits(key, 7);
62 des_key[2] = Get7Bits(key, 14);
63 des_key[3] = Get7Bits(key, 21);
64 des_key[4] = Get7Bits(key, 28);
65 des_key[5] = Get7Bits(key, 35);
66 des_key[6] = Get7Bits(key, 42);
67 des_key[7] = Get7Bits(key, 49);
70 DES_set_odd_parity((DES_cblock *)des_key);
76 * in == 8-byte string (expanded version of the 56-bit key)
77 * out == 64-byte string where each byte is either 1 or 0
78 * Note that the low-order "bit" is always ignored by by setkey()
81 Expand(u_char *in, u_char *out)
86 for (i = 0; i < 64; in++){
88 for (j = 7; j >= 0; j--)
89 *out++ = (c >> j) & 01;
94 /* The inverse of Expand
97 Collapse(u_char *in, u_char *out)
103 for (i = 0; i < 64; i += 8, out++) {
105 for (j = 7; j >= 0; j--, in++)
112 DesSetkey(u_char *key)
115 u_char crypt_key[66];
117 MakeKey(key, des_key);
118 Expand(des_key, crypt_key);
120 setkey((const char *)crypt_key);
127 DesEncrypt(u_char *clear, u_char *cipher)
129 u_char des_input[66];
131 Expand(clear, des_input);
133 encrypt((char *)des_input, 0);
136 Collapse(des_input, cipher);
141 DesDecrypt(u_char *cipher, u_char *clear)
143 u_char des_input[66];
145 Expand(cipher, des_input);
147 encrypt((char *)des_input, 1);
150 Collapse(des_input, clear);
154 #else /* USE_CRYPT */
155 static DES_key_schedule key_schedule;
158 DesSetkey(u_char *key)
161 MakeKey(key, des_key);
162 DES_set_key(&des_key, &key_schedule);
167 DesEncrypt(u_char *clear, u_char *cipher)
169 DES_ecb_encrypt((DES_cblock *)clear, (DES_cblock *)cipher,
175 DesDecrypt(u_char *cipher, u_char *clear)
177 DES_ecb_encrypt((DES_cblock *)cipher, (DES_cblock *)clear,
182 #endif /* USE_CRYPT */