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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Hex encryption/decryption and utility routines
*/
#include "mt.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <rpc/rpc.h>
#include <rpc/key_prot.h> /* for KEYCHECKSUMSIZE */
#include <rpc/des_crypt.h>
#include <string.h>
#include <rpcsvc/nis_dhext.h>
#include <md5.h>
#define MD5HEXSIZE 32
extern int bin2hex(int len, unsigned char *binnum, char *hexnum);
extern int hex2bin(int len, char *hexnum, char *binnum);
static char hex[]; /* forward */
static char hexval();
int passwd2des(char *, char *);
static int weak_DES_key(des_block);
/* EXPORT DELETE START */
/*
* For export control reasons, we want to limit the maximum size of
* data that can be encrypted or decrypted. We limit this to 1024
* bits of key data, which amounts to 128 bytes.
*
* For the extended DH project, we have increased it to
* 144 bytes (128key + 16checksum) to accomadate all the 128 bytes
* being used by the new 1024bit keys plus 16 bytes MD5 checksum.
* We discussed this with Sun's export control office and lawyers
* and we have reason to believe this is ok for export.
*/
#define MAX_KEY_CRYPT_LEN 144
/* EXPORT DELETE END */
/*
* Encrypt a secret key given passwd
* The secret key is passed and returned in hex notation.
* Its length must be a multiple of 16 hex digits (64 bits).
*/
int
xencrypt(secret, passwd)
char *secret;
char *passwd;
{
/* EXPORT DELETE START */
char key[8];
char ivec[8];
char *buf;
int err;
int len;
len = (int)strlen(secret) / 2;
if (len > MAX_KEY_CRYPT_LEN)
return (0);
buf = malloc((unsigned)len);
(void) hex2bin(len, secret, buf);
(void) passwd2des(passwd, key);
(void) memset(ivec, 0, 8);
err = cbc_crypt(key, buf, len, DES_ENCRYPT | DES_HW, ivec);
if (DES_FAILED(err)) {
free(buf);
return (0);
}
(void) bin2hex(len, (unsigned char *) buf, secret);
free(buf);
return (1);
#if 0
/* EXPORT DELETE END */
return (0);
/* EXPORT DELETE START */
#endif
/* EXPORT DELETE END */
}
/*
* Decrypt secret key using passwd
* The secret key is passed and returned in hex notation.
* Once again, the length is a multiple of 16 hex digits
*/
int
xdecrypt(secret, passwd)
char *secret;
char *passwd;
{
/* EXPORT DELETE START */
char key[8];
char ivec[8];
char *buf;
int err;
int len;
len = (int)strlen(secret) / 2;
if (len > MAX_KEY_CRYPT_LEN)
return (0);
buf = malloc((unsigned)len);
(void) hex2bin(len, secret, buf);
(void) passwd2des(passwd, key);
(void) memset(ivec, 0, 8);
err = cbc_crypt(key, buf, len, DES_DECRYPT | DES_HW, ivec);
if (DES_FAILED(err)) {
free(buf);
return (0);
}
(void) bin2hex(len, (unsigned char *) buf, secret);
free(buf);
return (1);
#if 0
/* EXPORT DELETE END */
return (0);
/* EXPORT DELETE START */
#endif
/* EXPORT DELETE END */
}
/*
* Turn password into DES key
*/
int
passwd2des(pw, key)
char *pw;
char *key;
{
int i;
(void) memset(key, 0, 8);
for (i = 0; *pw; i = (i+1) % 8) {
key[i] ^= *pw++ << 1;
}
des_setparity(key);
return (1);
}
/*
* Hex to binary conversion
*/
int
hex2bin(len, hexnum, binnum)
int len;
char *hexnum;
char *binnum;
{
int i;
for (i = 0; i < len; i++) {
*binnum++ = 16 * hexval(hexnum[2 * i]) +
hexval(hexnum[2 * i + 1]);
}
return (1);
}
/*
* Binary to hex conversion
*/
int
bin2hex(len, binnum, hexnum)
int len;
unsigned char *binnum;
char *hexnum;
{
int i;
unsigned val;
for (i = 0; i < len; i++) {
val = binnum[i];
hexnum[i*2] = hex[val >> 4];
hexnum[i*2+1] = hex[val & 0xf];
}
hexnum[len*2] = 0;
return (1);
}
static char hex[16] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
};
static char
hexval(c)
char c;
{
if (c >= '0' && c <= '9') {
return (c - '0');
} else if (c >= 'a' && c <= 'z') {
return (c - 'a' + 10);
} else if (c >= 'A' && c <= 'Z') {
return (c - 'A' + 10);
} else {
return (-1);
}
}
/*
* Generic key length/algorithm version of xencrypt().
*
* Encrypt a secret key given passwd.
* The secret key is passed in hex notation.
* Arg encrypted_secret will be set to point to the encrypted
* secret key (NUL term, hex notation).
*
* Its length must be a multiple of 16 hex digits (64 bits).
*
* For 192-0 (AUTH_DES), then encrypt using the same method as xencrypt().
*
* If arg do_chksum is TRUE, append the checksum before the encrypt.
* For 192-0, the checksum is done the same as in xencrypt(). For
* bigger keys, MD5 is used.
*
* Arg netname can be NULL for 192-0.
*/
int
xencrypt_g(
char *secret, /* in */
keylen_t keylen, /* in */
algtype_t algtype, /* in */
const char *passwd, /* in */
const char netname[], /* in */
char **encrypted_secret, /* out */
bool_t do_chksum) /* in */
{
/* EXPORT DELETE START */
des_block key;
char ivec[8];
char *binkeybuf;
int err;
const int classic_des = keylen == 192 && algtype == 0;
const int hexkeybytes = BITS2NIBBLES(keylen);
const int keychecksumsize = classic_des ? KEYCHECKSUMSIZE : MD5HEXSIZE;
const int binkeybytes = do_chksum ? keylen/8 + keychecksumsize/2 :
keylen/8;
const int bufsize = do_chksum ? hexkeybytes + keychecksumsize + 1 :
hexkeybytes + 1;
char *hexkeybuf;
if (!secret || !keylen || !passwd || !encrypted_secret)
return (0);
if ((hexkeybuf = malloc(bufsize)) == 0)
return (0);
(void) memcpy(hexkeybuf, secret, hexkeybytes);
if (do_chksum)
if (classic_des) {
(void) memcpy(hexkeybuf + hexkeybytes, secret,
keychecksumsize);
} else {
MD5_CTX md5_ctx;
char md5hexbuf[MD5HEXSIZE + 1] = {0};
uint8_t digest[MD5HEXSIZE/2];
MD5Init(&md5_ctx);
MD5Update(&md5_ctx, (unsigned char *)hexkeybuf,
hexkeybytes);
MD5Final(digest, &md5_ctx);
/* convert md5 binary digest to hex */
(void) bin2hex(MD5HEXSIZE/2, digest, md5hexbuf);
/* append the hex md5 string to the end of the key */
(void) memcpy(hexkeybuf + hexkeybytes,
(void *)md5hexbuf, MD5HEXSIZE);
}
hexkeybuf[bufsize - 1] = 0;
if (binkeybytes > MAX_KEY_CRYPT_LEN) {
free(hexkeybuf);
return (0);
}
if ((binkeybuf = malloc((unsigned)binkeybytes)) == 0) {
free(hexkeybuf);
return (0);
}
(void) hex2bin(binkeybytes, hexkeybuf, binkeybuf);
if (classic_des)
(void) passwd2des((char *)passwd, key.c);
else
if (netname)
(void) passwd2des_g(passwd, netname,
(int)strlen(netname), &key, FALSE);
else {
free(hexkeybuf);
return (0);
}
(void) memset(ivec, 0, 8);
err = cbc_crypt(key.c, binkeybuf, binkeybytes, DES_ENCRYPT | DES_HW,
ivec);
if (DES_FAILED(err)) {
free(hexkeybuf);
free(binkeybuf);
return (0);
}
(void) bin2hex(binkeybytes, (unsigned char *) binkeybuf, hexkeybuf);
free(binkeybuf);
*encrypted_secret = hexkeybuf;
return (1);
#if 0
/* EXPORT DELETE END */
return (0);
/* EXPORT DELETE START */
#endif
/* EXPORT DELETE END */
}
/*
* Generic key len and alg type for version of xdecrypt.
*
* Decrypt secret key using passwd. The decrypted secret key
* *overwrites* the supplied encrypted secret key.
* The secret key is passed and returned in hex notation.
* Once again, the length is a multiple of 16 hex digits.
*
* If 'do_chksum' is TRUE, the 'secret' buffer is assumed to contain
* a checksum calculated by a call to xencrypt_g().
*
* If keylen is 192 and algtype is 0, then decrypt the same way
* as xdecrypt().
*
* Arg netname can be NULL for 192-0.
*/
int
xdecrypt_g(
char *secret, /* out */
int keylen, /* in */
int algtype, /* in */
const char *passwd, /* in */
const char netname[], /* in */
bool_t do_chksum) /* in */
{
/* EXPORT DELETE START */
des_block key;
char ivec[8];
char *buf;
int err;
int len;
const int classic_des = keylen == 192 && algtype == 0;
const int hexkeybytes = BITS2NIBBLES(keylen);
const int keychecksumsize = classic_des ? KEYCHECKSUMSIZE : MD5HEXSIZE;
len = (int)strlen(secret) / 2;
if (len > MAX_KEY_CRYPT_LEN)
return (0);
if ((buf = malloc((unsigned)len)) == 0)
return (0);
(void) hex2bin(len, secret, buf);
if (classic_des)
(void) passwd2des((char *)passwd, key.c);
else
if (netname)
(void) passwd2des_g(passwd, netname,
(int)strlen(netname), &key, FALSE);
else {
free(buf);
return (0);
}
(void) memset(ivec, 0, 8);
err = cbc_crypt(key.c, buf, len, DES_DECRYPT | DES_HW, ivec);
if (DES_FAILED(err)) {
free(buf);
return (0);
}
(void) bin2hex(len, (unsigned char *) buf, secret);
free(buf);
if (do_chksum)
if (classic_des) {
if (memcmp(secret, &(secret[hexkeybytes]),
keychecksumsize) != 0) {
secret[0] = 0;
return (0);
}
} else {
MD5_CTX md5_ctx;
char md5hexbuf[MD5HEXSIZE + 1] = {0};
uint8_t digest[MD5HEXSIZE/2];
MD5Init(&md5_ctx);
MD5Update(&md5_ctx, (unsigned char *)secret,
hexkeybytes);
MD5Final(digest, &md5_ctx);
/* convert md5 binary digest to hex */
(void) bin2hex(MD5HEXSIZE/2, digest, md5hexbuf);
/* does the digest match the appended one? */
if (memcmp(&(secret[hexkeybytes]),
md5hexbuf, MD5HEXSIZE) != 0) {
secret[0] = 0;
return (0);
}
}
secret[hexkeybytes] = '\0';
return (1);
#if 0
/* EXPORT DELETE END */
return (0);
/* EXPORT DELETE START */
#endif
/* EXPORT DELETE END */
}
/*
* Modified version of passwd2des(). passwd2des_g() uses the Kerberos
* RFC 1510 algorithm to generate a DES key from a user password
* and mix-in string. The mix-in is expected to be the netname.
* This function to be used only for extended Diffie-Hellman keys.
*
* If altarg is TRUE, reverse the concat of passwd and mix-in.
*/
int
passwd2des_g(
const char *pw,
const char *mixin,
int len,
des_block *key, /* out */
bool_t altalg)
{
int i, j, incr = 1;
des_block ivec, tkey;
char *text;
int plen, tlen;
(void) memset(tkey.c, 0, 8);
(void) memset(ivec.c, 0, 8);
/*
* Concatentate the password and the mix-in string, fan-fold and XOR them
* to the required eight byte initial DES key. Since passwords can be
* expected to use mostly seven bit ASCII, left shift the password one
* bit in order to preserve as much key space as possible.
*/
#define KEYLEN sizeof (tkey.c)
plen = strlen(pw);
tlen = ((plen + len + (KEYLEN-1))/KEYLEN)*KEYLEN;
if ((text = malloc(tlen)) == NULL) {
return (0);
}
(void) memset(text, 0, tlen);
if (!altalg) {
/*
* Concatenate the password and the mix-in string, fan-fold and XOR them
* to the required eight byte initial DES key. Since passwords can be
* expected to use mostly seven bit ASCII, left shift the password one
* bit in order to preserve as much key space as possible.
*/
(void) memcpy(text, pw, plen);
(void) memcpy(&text[plen], mixin, len);
for (i = 0, j = 0; pw[j]; j++) {
tkey.c[i] ^= pw[j] << 1;
i += incr;
if (i == 8) {
i = 7;
incr = -incr;
} else if (i == -1) {
i = 0;
incr = -incr;
}
}
for (j = 0; j < len; j++) {
tkey.c[i] ^= mixin[j];
i += incr;
if (i == 8) {
i = 7;
incr = -incr;
} else if (i == -1) {
i = 0;
incr = -incr;
}
}
} else { /* use alternative algorithm */
(void) memcpy(text, mixin, len);
(void) memcpy(&text[len], pw, plen);
for (i = 0, j = 0; j < len; j++) {
tkey.c[i] ^= mixin[j];
i += incr;
if (i == 8) {
i = 7;
incr = -incr;
} else if (i == -1) {
i = 0;
incr = -incr;
}
}
for (j = 0; pw[j]; j++) {
tkey.c[i] ^= pw[j] << 1;
i += incr;
if (i == 8) {
i = 7;
incr = -incr;
} else if (i == -1) {
i = 0;
incr = -incr;
}
}
}
des_setparity_g(&tkey);
/*
* Use the temporary key to produce a DES CBC checksum for the text
* string; cbc_crypt returns the checksum in the ivec.
*/
(void) cbc_crypt(tkey.c, text, tlen, DES_ENCRYPT|DES_HW, ivec.c);
des_setparity_g(&ivec);
free(text);
if (weak_DES_key(ivec)) {
ivec.c[7] ^= 0xf0;
/*
* XORing with 0xf0 preserves parity, so no need to check
* that again.
*/
}
(void) memcpy((*key).c, ivec.c, sizeof (ivec.c));
return (1);
}
struct DESkey {
uint32_t h1;
uint32_t h2;
};
/*
* Weak and semiweak keys from "Applied Cryptography", second edition,
* by Bruce Schneier, Wiley 1996.
*/
static struct DESkey weakDESkeys[] = {
/* Weak keys */
{0x01010101, 0x01010101},
{0x1f1f1f1f, 0x1f1f1f1f},
{0xe0e0e0e0, 0xe0e0e0e0},
{0xfefefefe, 0xfefefefe},
/* Semiweak keys */
{0x01fe01fe, 0x01fe01fe},
{0x1fe01fe0, 0x0ef10ef1},
{0x01e001e0, 0x01f101f1},
{0x1ffe1ffe, 0x0efe0efe},
{0x011f011f, 0x010e010e},
{0xe0fee0fe, 0xf1fef1fe},
{0xfe01fe01, 0xfe01fe01},
{0xe01fe01f, 0xf10ef10e},
{0xe001e001, 0xf101f101},
{0xfe1ffe1f, 0xfe0efe0e},
{0x1f011f01, 0x0e010e01},
{0xfee0fee0, 0xfef1fef1}
};
static int
weak_DES_key(des_block db)
{
int i;
for (i = 0; i < sizeof (weakDESkeys)/sizeof (struct DESkey); i++) {
if (weakDESkeys[i].h1 == db.key.high &&
weakDESkeys[i].h2 == db.key.low)
return (1);
}
return (0);
}
|