pppcrypt.c revision 1.2
1/*	$NetBSD: pppcrypt.c,v 1.2 2005/02/20 10:47:17 cube Exp $	*/
2
3/*
4 * pppcrypt.c - PPP/DES linkage for MS-CHAP and EAP SRP-SHA1
5 *
6 * Extracted from chap_ms.c by James Carlson.
7 *
8 * Copyright (c) 1995 Eric Rosenquist.  All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 *
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in
19 *    the documentation and/or other materials provided with the
20 *    distribution.
21 *
22 * 3. The name(s) of the authors of this software must not be used to
23 *    endorse or promote products derived from this software without
24 *    prior written permission.
25 *
26 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
27 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
28 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
29 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
30 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
31 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
32 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
33 */
34
35#include <errno.h>
36#include <stdlib.h>
37#include <unistd.h>
38#include "pppd.h"
39#include "pppcrypt.h"
40
41static u_char Get7Bits(u_char *, int);
42static void MakeKey(u_char *, u_char *);
43
44static u_char
45Get7Bits(input, startBit)
46u_char *input;
47int startBit;
48{
49	unsigned int word;
50
51	word  = (unsigned)input[startBit / 8] << 8;
52	word |= (unsigned)input[startBit / 8 + 1];
53
54	word >>= 15 - (startBit % 8 + 7);
55
56	return word & 0xFE;
57}
58
59static void
60MakeKey(key, des_key)
61u_char *key;		/* IN  56 bit DES key missing parity bits */
62u_char *des_key;	/* OUT 64 bit DES key with parity bits added */
63{
64	des_key[0] = Get7Bits(key,  0);
65	des_key[1] = Get7Bits(key,  7);
66	des_key[2] = Get7Bits(key, 14);
67	des_key[3] = Get7Bits(key, 21);
68	des_key[4] = Get7Bits(key, 28);
69	des_key[5] = Get7Bits(key, 35);
70	des_key[6] = Get7Bits(key, 42);
71	des_key[7] = Get7Bits(key, 49);
72
73#ifndef USE_CRYPT
74	des_set_odd_parity((des_cblock *)des_key);
75#endif
76}
77
78#ifdef USE_CRYPT
79static void Expand(u_char *, u_char *);
80static void Collapse(u_char *, u_char *);
81
82/*
83 * in == 8-byte string (expanded version of the 56-bit key)
84 * out == 64-byte string where each byte is either 1 or 0
85 * Note that the low-order "bit" is always ignored by by setkey()
86 */
87static void
88Expand(in, out)
89u_char *in;
90u_char *out;
91{
92        int j, c;
93        int i;
94
95        for (i = 0; i < 64; in++){
96		c = *in;
97                for (j = 7; j >= 0; j--)
98                        *out++ = (c >> j) & 01;
99                i += 8;
100        }
101}
102
103/* The inverse of Expand
104 */
105static void
106Collapse(in, out)
107u_char *in;
108u_char *out;
109{
110        int j;
111        int i;
112	unsigned int c;
113
114	for (i = 0; i < 64; i += 8, out++) {
115	    c = 0;
116	    for (j = 7; j >= 0; j--, in++)
117		c |= *in << j;
118	    *out = c & 0xff;
119	}
120}
121
122bool
123DesSetkey(key)
124u_char *key;
125{
126	u_char des_key[8];
127	u_char crypt_key[66];
128
129	MakeKey(key, des_key);
130	Expand(des_key, crypt_key);
131	errno = 0;
132	setkey((const char *)crypt_key);
133	if (errno != 0)
134		return (0);
135	return (1);
136}
137
138bool
139DesEncrypt(clear, cipher)
140u_char *clear;	/* IN  8 octets */
141u_char *cipher;	/* OUT 8 octets */
142{
143	u_char des_input[66];
144
145	Expand(clear, des_input);
146	errno = 0;
147	encrypt((char *)des_input, 0);
148	if (errno != 0)
149		return (0);
150	Collapse(des_input, cipher);
151	return (1);
152}
153
154bool
155DesDecrypt(cipher, clear)
156u_char *cipher;	/* IN  8 octets */
157u_char *clear;	/* OUT 8 octets */
158{
159	u_char des_input[66];
160
161	Expand(cipher, des_input);
162	errno = 0;
163	encrypt((char *)des_input, 1);
164	if (errno != 0)
165		return (0);
166	Collapse(des_input, clear);
167	return (1);
168}
169
170#else /* USE_CRYPT */
171static des_key_schedule	key_schedule;
172
173bool
174DesSetkey(key)
175u_char *key;
176{
177	des_cblock des_key;
178	MakeKey(key, des_key);
179	des_set_key(&des_key, key_schedule);
180	return (1);
181}
182
183bool
184DesEncrypt(clear, key, cipher)
185u_char *clear;	/* IN  8 octets */
186u_char *cipher;	/* OUT 8 octets */
187{
188	des_ecb_encrypt((des_cblock *)clear, (des_cblock *)cipher,
189	    key_schedule, 1);
190	return (1);
191}
192
193bool
194DesDecrypt(cipher, clear)
195u_char *cipher;	/* IN  8 octets */
196u_char *clear;	/* OUT 8 octets */
197{
198	des_ecb_encrypt((des_cblock *)cipher, (des_cblock *)clear,
199	    key_schedule, 0);
200	return (1);
201}
202
203#endif /* USE_CRYPT */
204