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