]> git.ozlabs.org Git - ppp.git/blob - pppd/pppcrypt.c
pppd man page: Update header to refer to pppd 2.5.x
[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 #include <errno.h>
34 #include "pppd.h"
35 #include "pppcrypt.h"
36
37 static u_char
38 Get7Bits(u_char *input, int startBit)
39 {
40         unsigned int word;
41
42         word  = (unsigned)input[startBit / 8] << 8;
43         word |= (unsigned)input[startBit / 8 + 1];
44
45         word >>= 15 - (startBit % 8 + 7);
46
47         return word & 0xFE;
48 }
49
50 static void
51 MakeKey(u_char *key, u_char *des_key)
52 {
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);
63
64 #ifndef USE_CRYPT
65         DES_set_odd_parity((DES_cblock *)des_key);
66 #endif
67 }
68
69 #ifdef USE_CRYPT
70 /*
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()
74  */
75 static void
76 Expand(u_char *in, u_char *out)
77 {
78         int j, c;
79         int i;
80
81         for (i = 0; i < 64; in++){
82                 c = *in;
83                 for (j = 7; j >= 0; j--)
84                         *out++ = (c >> j) & 01;
85                 i += 8;
86         }
87 }
88
89 /* The inverse of Expand
90  */
91 static void
92 Collapse(u_char *in, u_char *out)
93 {
94         int j;
95         int i;
96         unsigned int c;
97
98         for (i = 0; i < 64; i += 8, out++) {
99             c = 0;
100             for (j = 7; j >= 0; j--, in++)
101                 c |= *in << j;
102             *out = c & 0xff;
103         }
104 }
105
106 bool
107 DesSetkey(u_char *key)
108 {
109         u_char des_key[8];
110         u_char crypt_key[66];
111
112         MakeKey(key, des_key);
113         Expand(des_key, crypt_key);
114         errno = 0;
115         setkey((const char *)crypt_key);
116         if (errno != 0)
117                 return (0);
118         return (1);
119 }
120
121 bool
122 DesEncrypt(u_char *clear, u_char *cipher)
123 {
124         u_char des_input[66];
125
126         Expand(clear, des_input);
127         errno = 0;
128         encrypt((char *)des_input, 0);
129         if (errno != 0)
130                 return (0);
131         Collapse(des_input, cipher);
132         return (1);
133 }
134
135 bool
136 DesDecrypt(u_char *cipher, u_char *clear)
137 {
138         u_char des_input[66];
139
140         Expand(cipher, des_input);
141         errno = 0;
142         encrypt((char *)des_input, 1);
143         if (errno != 0)
144                 return (0);
145         Collapse(des_input, clear);
146         return (1);
147 }
148
149 #else /* USE_CRYPT */
150 static DES_key_schedule key_schedule;
151
152 bool
153 DesSetkey(u_char *key)
154 {
155         DES_cblock des_key;
156         MakeKey(key, des_key);
157         DES_set_key(&des_key, &key_schedule);
158         return (1);
159 }
160
161 bool
162 DesEncrypt(u_char *clear, u_char *cipher)
163 {
164         DES_ecb_encrypt((DES_cblock *)clear, (DES_cblock *)cipher,
165             &key_schedule, 1);
166         return (1);
167 }
168
169 bool
170 DesDecrypt(u_char *cipher, u_char *clear)
171 {
172         DES_ecb_encrypt((DES_cblock *)cipher, (DES_cblock *)clear,
173             &key_schedule, 0);
174         return (1);
175 }
176
177 #endif /* USE_CRYPT */