1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
/* $NetBSD: kstream-des.c,v 1.4 2002/08/29 14:53:58 itojun Exp $ */
/* DES-encrypted-stream implementation for MIT Kerberos.
Written by Ken Raeburn (Raeburn@Cygnus.COM), based on algorithms
in the original MIT Kerberos code.
Copyright (C) 1991, 1992 by Cygnus Support.
This file is distributed under the same terms as Kerberos.
For copying and distribution information, please see the file
<kerberosIV/mit-copyright.h>.
from: kstream-des.c,v 1.9 1996/06/02 07:37:27 ghudson Exp $
*/
#include <assert.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <des.h>
#include <kerberosIV/kstream.h>
typedef struct {
union {
long align_me;
double align_me_harder;
Key_schedule sched;
} u;
des_cblock ivec;
} kstream_des_init_block;
typedef struct {
kstream_des_init_block x;
int no_right_justify;
int protect_rlogin_oob;
char *buf1, *buf2;
size_t len1, len2;
} priv;
typedef struct kstream_data_block ksdb;
/* The data stream consists of four bytes representing a net-order
integer, followed by enough data to produce that many
cleartext bytes. This means the size of that data must be rounded
up to a multiple of 8, even though the cleartext may only be one
byte. For blocks of less than eight bytes, most software (well,
exactly half of the two already-existing programs, and all of the
ones we write using this library :-) pads on the *left* with
random values.
Some existing software that we have to be compatible with may send
blocks of data that decrypt to more than 8 bytes, but not an exact
multiple. In that case, the padding is on the right, as would be
considered "normal". This software currently will not generate such
a sequence, but a future version could. Don't break compatibility
with that mode. */
#ifdef sun
static kstream_ptr losing_realloc (old_ptr, new_size)
kstream_ptr old_ptr;
size_t new_size;
{
return old_ptr ? realloc (old_ptr, new_size) : malloc (new_size);
}
#define realloc losing_realloc
#endif
/* Do the actual encryption work. This routine will handle chunks of
any size up to 16 bytes, or any multiple of 8 over that. It makes
the padding a little easier to write it this way. Handling sizes
between 8 and 16 is an annoyance, but rlogin actually relies on being
able to send 12 bytes in one chunk. Bleah! */
static void
do_encrypt (ksdb *out, ksdb *inp, priv *p)
{
union {
char buf[16];
int junk[16 / sizeof (int)];
} u;
ksdb in;
char *ptr;
static int seeded;
if (!seeded) {
srandom ((int) time ((time_t *) 0));
seeded = 1;
}
in = *inp;
if (in.length < 8)
{
if (! p->no_right_justify)
{
u.junk[0] = random ();
memcpy (u.buf + 8 - in.length, in.ptr, in.length);
}
else
{
u.junk[(sizeof (u.junk[0]) + 7) / sizeof (u.junk[0]) - 1] = random ();
memcpy (u.buf, in.ptr, in.length);
}
in.ptr = u.buf;
in.length = 8;
}
else if (in.length == 8)
{
memcpy (u.buf, in.ptr, 8);
in.ptr = u.buf;
}
else if (in.length < sizeof (u.buf))
{
if (in.length % 8 == 0)
abort ();
u.junk[(sizeof (u.junk) / sizeof (u.junk[0])) - 1] = random ();
memcpy (u.buf, in.ptr, in.length);
in.ptr = u.buf;
}
else if (in.length % 8 != 0)
abort ();
{
unsigned long x;
x = inp->length; /* not in.length! */
ptr = (char *) out->ptr;
ptr[3] = x & 0xff; x >>= 8;
ptr[2] = x & 0xff; x >>= 8;
ptr[1] = x & 0xff; x >>= 8;
ptr[0] = x & 0xff; x >>= 8;
ptr += 4;
if (x)
abort ();
}
des_pcbc_encrypt (in.ptr, ptr, in.length,
p->x.u.sched, (des_cblock *)p->x.ivec, 1);
out->ptr = ptr + ((in.length + 7) & ~7);
}
static int
encrypt (ksdb *outp, ksdb *inp, kstream k)
{
const int small_block_size = 16;
priv *p = (priv *) k->data;
if (inp->length > small_block_size && inp->length % 8 != 0)
{
/* do two */
ksdb in, out;
size_t sz;
in.ptr = inp->ptr;
in.length = inp->length & ~7;
sz = in.length + 4; /* first block */
sz += 8 + 4; /* second block */
outp->length = sz;
out.ptr = outp->ptr = p->buf1 = realloc (p->buf1, sz);
out.length = sz;
assert (out.ptr != 0 || out.length == 0);
do_encrypt (&out, &in, p);
in.ptr = (char *) in.ptr + in.length;
in.length = inp->length - in.length;
do_encrypt (&out, &in, p);
return inp->length;
}
else
{
size_t sz = (inp->length + 7) & ~7;
sz += 4;
outp->length = sz;
outp->ptr = p->buf1 = realloc (p->buf1, sz);
assert (outp->ptr != 0 || outp->length == 0);
do_encrypt (outp, inp, p);
outp->ptr = p->buf1;
return inp->length;
}
}
int _kstream_des_debug_OOB = 0;
static int
decrypt (ksdb *outp, ksdb *inp, kstream k)
{
char *ptr = inp->ptr;
unsigned long x = 0;
int error_count = 0;
size_t sz;
priv *p = (priv *) k->data;
if(inp->length < 1) return -12; /* make sure we have at least one byte */
if (p->protect_rlogin_oob) {
/* here's where we handle an attack. The first byte ends up being the
highest. If it's not zero, skip it. If it is zero, we can't detect it,
and we still lose... */
x = *ptr & 0xff; /* get the first char */
while (x) {
if(_kstream_des_debug_OOB) fprintf(stderr,"BAD BYTE %02lx\n\r", x);
error_count++; /* count the bad byte */
ptr++; /* and skip it */
if(inp->length == error_count) {
return -12; /* we've used up all of the input */
}
x = *ptr & 0xff; /* get the next potentially first char */
}
ptr++;
} else {
x <<= 8; x += *ptr++ & 0xff;
}
/* If we've got four bytes, we can at least determine the correct
amount that still needs to be read. If not, we can assume a
minimum of 12 good bytes (4-byte length plus one 8-byte block)
and we know how many of the ones we've got are bad. */
if (inp->length < 4 + error_count)
return inp->length - error_count - 12;
/* x <<= 8; x += *ptr++ & 0xff; */ /* x already has first byte loaded */
x <<= 8; x += *ptr++ & 0xff;
x <<= 8; x += *ptr++ & 0xff;
x <<= 8; x += *ptr++ & 0xff;
sz = (x + 7) & ~7;
if (inp->length < sz + 4 + error_count)
return - (sz + 4 + error_count - inp->length);
assert (sz <= sizeof (k->in_crypt.data));
if (p->buf1)
p->buf1 = realloc (p->buf1, sz);
else
p->buf1 = malloc (sz);
assert (p->buf1 != 0 || sz == 0);
outp->ptr = p->buf1;
outp->length = x;
pcbc_encrypt (ptr, outp->ptr, sz, p->x.u.sched,
(des_cblock *)p->x.ivec, 0);
if (p->no_right_justify == 0
&& x < 8)
outp->ptr = p->buf1 + 8 - x;
return sz + 4 + error_count;
}
static int
init (kstream k, void *data)
{
priv *p;
p = (priv *) malloc (sizeof (priv));
k->data = (kstream_ptr) p;
if (!p)
return errno;
p->buf1 = p->buf2 = 0;
p->len1 = p->len2 = 0;
p->no_right_justify = 0;
p->protect_rlogin_oob = 0;
p->x = * (kstream_des_init_block *) data;
return 0;
}
static int
rcp_init (kstream k, void *data)
{
int x = init (k, data);
((priv *)(k->data))->no_right_justify = 1;
return x;
}
static int
rlogin_init (kstream k, void *data)
{
int x = init (k, data);
((priv *)(k->data))->protect_rlogin_oob = 1;
return x;
}
static void
destroy (kstream k)
{
priv *p = (priv *) k->data;
if (p->buf1)
free (p->buf1);
memset (p, '\\', sizeof (*p)); /* scribble to make sure it's gone */
free (p);
k->data = 0;
}
static const struct kstream_crypt_ctl_block kstream_des_ccb = {
encrypt, decrypt, rlogin_init, destroy
};
static const struct kstream_crypt_ctl_block kstream_des_rcp_ccb = {
encrypt, decrypt, rcp_init, destroy
};
kstream
kstream_create_rlogin_from_fd (int fd, kstream_ptr P_sched,
des_cblock (*ivec))
{
Key_schedule *sched = (Key_schedule *) P_sched;
kstream_des_init_block x;
kstream k;
memcpy (&x.u.sched, sched, sizeof (Key_schedule));
memcpy (&x.ivec, ivec, sizeof (des_cblock));
k = kstream_create_from_fd (fd, &kstream_des_ccb, &x);
memset (&x, '\\', sizeof (x));
return k;
}
kstream
kstream_create_rcp_from_fd (int fd, kstream_ptr P_sched,
des_cblock (*ivec))
{
Key_schedule *sched = (Key_schedule *) P_sched;
kstream_des_init_block x;
kstream k;
memcpy (&x.u.sched, sched, sizeof (Key_schedule));
memcpy (&x.ivec, ivec, sizeof (des_cblock));
k = kstream_create_from_fd (fd, &kstream_des_rcp_ccb, &x);
memset (&x, '\\', sizeof (x));
return k;
}
|