2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
31 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
35 #ident "@(#)xcrypt.c 1.11 94/08/23 SMI"
38 #if !defined(lint) && defined(SCCSIDS)
39 static char sccsid
[] = "@(#)xcrypt.c 1.3 89/03/24 Copyr 1986 Sun Micro";
43 * xcrypt.c: Hex encryption/decryption and utility routines
50 #include <sys/types.h>
51 #include <rpc/des_crypt.h>
53 static const char hex
[16] =
55 '0', '1', '2', '3', '4', '5', '6', '7',
56 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
62 (c >= '0' && c <= '9' \
64 : ({ int upp = toupper (c); \
65 upp >= 'A' && upp <= 'Z' ? upp - 'A' + 10 : -1; }))
67 static char hexval (char) internal_function
;
70 static void hex2bin (int, char *, char *) internal_function
;
71 static void bin2hex (int, unsigned char *, char *) internal_function
;
72 static void passwd2des_internal (char *pw
, char *key
);
76 * Turn password into DES key
79 passwd2des_internal (char *pw
, char *key
)
84 for (i
= 0; *pw
&& i
< 8; ++i
)
91 strong_alias (passwd2des_internal
, passwd2des
)
93 void passwd2des (char *pw
, char *key
)
95 return passwd2des_internal (pw
, key
);
100 * Encrypt a secret key given passwd
101 * The secret key is passed and returned in hex notation.
102 * Its length must be a multiple of 16 hex digits (64 bits).
105 xencrypt (char *secret
, char *passwd
)
113 len
= strlen (secret
) / 2;
114 buf
= malloc ((unsigned) len
);
115 hex2bin (len
, secret
, buf
);
116 passwd2des_internal (passwd
, key
);
119 err
= cbc_crypt (key
, buf
, len
, DES_ENCRYPT
| DES_HW
, ivec
);
120 if (DES_FAILED (err
))
125 bin2hex (len
, (unsigned char *) buf
, secret
);
131 * Decrypt secret key using passwd
132 * The secret key is passed and returned in hex notation.
133 * Once again, the length is a multiple of 16 hex digits
136 xdecrypt (char *secret
, char *passwd
)
144 len
= strlen (secret
) / 2;
145 buf
= malloc ((unsigned) len
);
147 hex2bin (len
, secret
, buf
);
148 passwd2des_internal (passwd
, key
);
151 err
= cbc_crypt (key
, buf
, len
, DES_DECRYPT
| DES_HW
, ivec
);
152 if (DES_FAILED (err
))
157 bin2hex (len
, (unsigned char *) buf
, secret
);
163 * Hex to binary conversion
167 hex2bin (int len
, char *hexnum
, char *binnum
)
171 for (i
= 0; i
< len
; i
++)
172 *binnum
++ = 16 * hexval (hexnum
[2 * i
]) + hexval (hexnum
[2 * i
+ 1]);
176 * Binary to hex conversion
180 bin2hex (int len
, unsigned char *binnum
, char *hexnum
)
185 for (i
= 0; i
< len
; i
++)
188 hexnum
[i
* 2] = hex
[val
>> 4];
189 hexnum
[i
* 2 + 1] = hex
[val
& 0xf];
198 if (c
>= '0' && c
<= '9')
200 else if (c
>= 'a' && c
<= 'z')
201 return (c
- 'a' + 10);
202 else if (c
>= 'A' && c
<= 'Z')
203 return (c
- 'A' + 10);