]> git.ozlabs.org Git - ppp.git/blob - pppd/pppcrypt.c
Use autoconf/automake to configure and make ppp
[ppp.git] / pppd / pppcrypt.c
1 /*
2  * pppcrypt.c - PPP/DES linkage for MS-CHAP and EAP SRP-SHA1
3  *
4  * Extracted from chap_ms.c by James Carlson.
5  *
6  * Copyright (c) 1995 Eric Rosenquist.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
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
18  *    distribution.
19  *
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.
23  *
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.
31  */
32
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36
37 #include <errno.h>
38
39 #include "pppd.h"
40 #include "pppcrypt.h"
41
42 static u_char
43 Get7Bits(u_char *input, int startBit)
44 {
45         unsigned int word;
46
47         word  = (unsigned)input[startBit / 8] << 8;
48         word |= (unsigned)input[startBit / 8 + 1];
49
50         word >>= 15 - (startBit % 8 + 7);
51
52         return word & 0xFE;
53 }
54
55 static void
56 MakeKey(u_char *key, u_char *des_key)
57 {
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);
68
69 #ifndef USE_CRYPT
70         DES_set_odd_parity((DES_cblock *)des_key);
71 #endif
72 }
73
74 #ifdef USE_CRYPT
75 /*
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()
79  */
80 static void
81 Expand(u_char *in, u_char *out)
82 {
83         int j, c;
84         int i;
85
86         for (i = 0; i < 64; in++){
87                 c = *in;
88                 for (j = 7; j >= 0; j--)
89                         *out++ = (c >> j) & 01;
90                 i += 8;
91         }
92 }
93
94 /* The inverse of Expand
95  */
96 static void
97 Collapse(u_char *in, u_char *out)
98 {
99         int j;
100         int i;
101         unsigned int c;
102
103         for (i = 0; i < 64; i += 8, out++) {
104             c = 0;
105             for (j = 7; j >= 0; j--, in++)
106                 c |= *in << j;
107             *out = c & 0xff;
108         }
109 }
110
111 bool
112 DesSetkey(u_char *key)
113 {
114         u_char des_key[8];
115         u_char crypt_key[66];
116
117         MakeKey(key, des_key);
118         Expand(des_key, crypt_key);
119         errno = 0;
120         setkey((const char *)crypt_key);
121         if (errno != 0)
122                 return (0);
123         return (1);
124 }
125
126 bool
127 DesEncrypt(u_char *clear, u_char *cipher)
128 {
129         u_char des_input[66];
130
131         Expand(clear, des_input);
132         errno = 0;
133         encrypt((char *)des_input, 0);
134         if (errno != 0)
135                 return (0);
136         Collapse(des_input, cipher);
137         return (1);
138 }
139
140 bool
141 DesDecrypt(u_char *cipher, u_char *clear)
142 {
143         u_char des_input[66];
144
145         Expand(cipher, des_input);
146         errno = 0;
147         encrypt((char *)des_input, 1);
148         if (errno != 0)
149                 return (0);
150         Collapse(des_input, clear);
151         return (1);
152 }
153
154 #else /* USE_CRYPT */
155 static DES_key_schedule key_schedule;
156
157 bool
158 DesSetkey(u_char *key)
159 {
160         DES_cblock des_key;
161         MakeKey(key, des_key);
162         DES_set_key(&des_key, &key_schedule);
163         return (1);
164 }
165
166 bool
167 DesEncrypt(u_char *clear, u_char *cipher)
168 {
169         DES_ecb_encrypt((DES_cblock *)clear, (DES_cblock *)cipher,
170             &key_schedule, 1);
171         return (1);
172 }
173
174 bool
175 DesDecrypt(u_char *cipher, u_char *clear)
176 {
177         DES_ecb_encrypt((DES_cblock *)cipher, (DES_cblock *)clear,
178             &key_schedule, 0);
179         return (1);
180 }
181
182 #endif /* USE_CRYPT */