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 void passwd2des_internal (char *pw
, char *key
);
74 libc_hidden_proto (passwd2des_internal
)
78 * Turn password into DES key
81 passwd2des_internal (char *pw
, char *key
)
86 for (i
= 0; *pw
&& i
< 8; ++i
)
93 libc_hidden_def (passwd2des_internal
)
94 strong_alias (passwd2des_internal
, passwd2des
)
96 void passwd2des (char *pw
, char *key
)
98 return passwd2des_internal (pw
, key
);
103 * Encrypt a secret key given passwd
104 * The secret key is passed and returned in hex notation.
105 * Its length must be a multiple of 16 hex digits (64 bits).
108 xencrypt (char *secret
, char *passwd
)
116 len
= strlen (secret
) / 2;
117 buf
= malloc ((unsigned) len
);
118 hex2bin (len
, secret
, buf
);
119 passwd2des_internal (passwd
, key
);
122 err
= cbc_crypt (key
, buf
, len
, DES_ENCRYPT
| DES_HW
, ivec
);
123 if (DES_FAILED (err
))
128 bin2hex (len
, (unsigned char *) buf
, secret
);
134 * Decrypt secret key using passwd
135 * The secret key is passed and returned in hex notation.
136 * Once again, the length is a multiple of 16 hex digits
139 xdecrypt (char *secret
, char *passwd
)
147 len
= strlen (secret
) / 2;
148 buf
= malloc ((unsigned) len
);
150 hex2bin (len
, secret
, buf
);
151 passwd2des_internal (passwd
, key
);
154 err
= cbc_crypt (key
, buf
, len
, DES_DECRYPT
| DES_HW
, ivec
);
155 if (DES_FAILED (err
))
160 bin2hex (len
, (unsigned char *) buf
, secret
);
166 * Hex to binary conversion
170 hex2bin (int len
, char *hexnum
, char *binnum
)
174 for (i
= 0; i
< len
; i
++)
175 *binnum
++ = 16 * hexval (hexnum
[2 * i
]) + hexval (hexnum
[2 * i
+ 1]);
179 * Binary to hex conversion
183 bin2hex (int len
, unsigned char *binnum
, char *hexnum
)
188 for (i
= 0; i
< len
; i
++)
191 hexnum
[i
* 2] = hex
[val
>> 4];
192 hexnum
[i
* 2 + 1] = hex
[val
& 0xf];
201 if (c
>= '0' && c
<= '9')
203 else if (c
>= 'a' && c
<= 'z')
204 return (c
- 'a' + 10);
205 else if (c
>= 'A' && c
<= 'Z')
206 return (c
- 'A' + 10);