Merge from vendor branch TNF:
[pkgsrcv2.git] / net / ppp-mppe / patches / patch-bj
1 $NetBSD$
2
3 --- pppd/extra_crypto.c Wed Dec 31 16:00:00 1969
4 +++ pppd/extra_crypto.c Thu Jun 24 16:05:42 1999
5 @@ -0,0 +1,162 @@
6 +/*
7 + * Copyright (c) Tim Hockin, Cobalt Networks Inc. and others
8 + *
9 + * crypto routines used by multiple c files 
10 + */
11 +#include <stdio.h>
12 +#include <ctype.h>
13 +#include <stdlib.h>
14 +#include <unistd.h>
15 +#include <string.h>
16 +#include "extra_crypto.h"
17 +#include "pppd.h"
18 +#include "md4.h"
19 +#ifndef USE_CRYPT
20 +#include <des.h>
21 +#endif
22 +
23 +/* quick wrapper for easy md4 */
24 +void
25 +md4(unsigned char *from, int from_len, unsigned char *to)
26 +{
27 +  MD4_CTX Context;
28 +
29 +#ifndef __NetBSD__
30 +  from_len <<= 3; /* bytes->bits */
31 +#endif
32 +
33 +  MD4Init(&Context);
34 +  MD4Update(&Context, from, from_len);
35 +  MD4Final(to, &Context);
36 +}
37 +
38 +
39 +/* Microsoft LANMAN Password hashing */
40 +static u_char *MSStdText = (u_char *)"KGS!@#$%"; /* key from rasapi32.dll */
41 +
42 +void
43 +LmPasswordHash(char *password, int len, char *hash)
44 +{
45 +    int        i;
46 +    u_char up_pass[MAX_NT_PASSWORD]; /* max is actually 14 */
47 +
48 +    /* LANMan password is case insensitive */
49 +    BZERO(up_pass, sizeof(up_pass));
50 +    for (i = 0; i < len; i++)
51 +       up_pass[i] = (u_char)toupper(up_pass[i]);
52 +    DesEncrypt(MSStdText, up_pass + 0, hash + 0);
53 +    DesEncrypt(MSStdText, up_pass + 7, hash + 8);
54 +}
55 +
56 +void
57 +NtPasswordHash(char *secret, int secret_len, unsigned char *hash)
58 +{
59 +    int        i;
60 +    u_char unicodePassword[MAX_NT_PASSWORD * 2];
61 +
62 +    /* Initialize the Unicode version of the secret (== password). */
63 +    /* This implicitly supports 8-bit ISO8859/1 characters. */
64 +    BZERO(unicodePassword, sizeof(unicodePassword));
65 +    for (i = 0; i < secret_len; i++)
66 +       unicodePassword[i * 2] = (u_char)secret[i];
67 +
68 +    /* Unicode is 2 bytes per char */
69 +    md4(unicodePassword, secret_len * 2, hash);
70 +}
71 +
72 +
73 +static u_char Get7Bits(unsigned char *input, int startBit)
74 +{
75 +    register unsigned int       word;
76 +
77 +    word  = (unsigned)input[startBit / 8] << 8;
78 +    word |= (unsigned)input[startBit / 8 + 1];
79 +
80 +    word >>= 15 - (startBit % 8 + 7);
81 +
82 +    return word & 0xFE;
83 +}
84 +
85 +
86 +static void MakeKey(unsigned char *key, unsigned char *des_key)
87 +{
88 +    des_key[0] = Get7Bits(key,  0);
89 +    des_key[1] = Get7Bits(key,  7);
90 +    des_key[2] = Get7Bits(key, 14);
91 +    des_key[3] = Get7Bits(key, 21);
92 +    des_key[4] = Get7Bits(key, 28);
93 +    des_key[5] = Get7Bits(key, 35);
94 +    des_key[6] = Get7Bits(key, 42);
95 +    des_key[7] = Get7Bits(key, 49);
96 +
97 +#ifndef USE_CRYPT
98 +    des_set_odd_parity((des_cblock *)des_key);
99 +#endif
100 +}
101 +
102 +
103 +#ifdef USE_CRYPT
104 +/* in == 8-byte string (expanded version of the 56-bit key)
105 + * out == 64-byte string where each byte is either 1 or 0
106 + * Note that the low-order "bit" is always ignored by by setkey()
107 + */
108 +static void Expand(unsigned char *in, unsigned char *out)
109 +{
110 +        int j, c;
111 +        int i;
112 +
113 +        for(i = 0; i < 64; in++){
114 +               c = *in;
115 +                for(j = 7; j >= 0; j--)
116 +                        *out++ = (c >> j) & 01;
117 +                i += 8;
118 +        }
119 +}
120 +
121 +/* The inverse of Expand
122 + */
123 +static void Collapse(unsigned char *in, unsigned char *out)
124 +{
125 +        int j;
126 +        int i;
127 +       unsigned int c;
128 +
129 +       for (i = 0; i < 64; i += 8, out++) {
130 +           c = 0;
131 +           for (j = 7; j >= 0; j--, in++)
132 +               c |= *in << j;
133 +           *out = c & 0xff;
134 +       }
135 +}
136 +void
137 +DesEncrypt(unsigned char *clear, unsigned char *key, unsigned char *cipher)
138 +{
139 +    u_char des_key[8];
140 +    u_char crypt_key[66];
141 +    u_char des_input[66];
142 +
143 +    MakeKey(key, des_key);
144 +
145 +    Expand(des_key, crypt_key);
146 +    setkey(crypt_key);
147 +
148 +    Expand(clear, des_input);
149 +    encrypt(des_input, 0);
150 +    Collapse(des_input, cipher);
151 +}
152 +#else /* don't USE_CRYPT */
153 +void
154 +DesEncrypt(unsigned char *clear, unsigned char *key, unsigned char *cipher)
155 +{
156 +    des_cblock         des_key;
157 +    des_key_schedule   key_schedule;
158 +
159 +    MakeKey(key, des_key);
160 +
161 +    des_set_key(&des_key, key_schedule);
162 +
163 +    des_ecb_encrypt((des_cblock *)clear, (des_cblock *)cipher, key_schedule, 1);
164 +}
165 +#endif /* USE_CRYPT */
166 +
167 +