2 * Copyright (c) 2010, Oracle America, Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 * * Neither the name of the "Oracle America, Inc." nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #ident "@(#)xcrypt.c 1.11 94/08/23 SMI"
36 #if !defined(lint) && defined(SCCSIDS)
37 static char sccsid
[] = "@(#)xcrypt.c 1.3 89/03/24 Copyr 1986 Sun Micro";
41 * xcrypt.c: Hex encryption/decryption and utility routines
48 #include <sys/types.h>
49 #include <rpc/des_crypt.h>
51 static const char hex
[16] =
53 '0', '1', '2', '3', '4', '5', '6', '7',
54 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
60 (c >= '0' && c <= '9' \
62 : ({ int upp = toupper (c); \
63 upp >= 'A' && upp <= 'Z' ? upp - 'A' + 10 : -1; }))
65 static char hexval (char) internal_function
;
68 static void hex2bin (int, char *, char *) internal_function
;
69 static void bin2hex (int, unsigned char *, char *) internal_function
;
70 void passwd2des_internal (char *pw
, char *key
);
72 libc_hidden_proto (passwd2des_internal
)
76 * Turn password into DES key
79 passwd2des_internal (char *pw
, char *key
)
84 for (i
= 0; *pw
&& i
< 8; ++i
)
91 libc_hidden_def (passwd2des_internal
)
92 strong_alias (passwd2des_internal
, passwd2des
)
94 void passwd2des (char *pw
, char *key
)
96 return passwd2des_internal (pw
, key
);
101 * Encrypt a secret key given passwd
102 * The secret key is passed and returned in hex notation.
103 * Its length must be a multiple of 16 hex digits (64 bits).
106 xencrypt (char *secret
, char *passwd
)
114 len
= strlen (secret
) / 2;
115 buf
= malloc ((unsigned) len
);
116 hex2bin (len
, secret
, buf
);
117 passwd2des_internal (passwd
, key
);
120 err
= cbc_crypt (key
, buf
, len
, DES_ENCRYPT
| DES_HW
, ivec
);
121 if (DES_FAILED (err
))
126 bin2hex (len
, (unsigned char *) buf
, secret
);
132 * Decrypt secret key using passwd
133 * The secret key is passed and returned in hex notation.
134 * Once again, the length is a multiple of 16 hex digits
137 xdecrypt (char *secret
, char *passwd
)
145 len
= strlen (secret
) / 2;
146 buf
= malloc ((unsigned) len
);
148 hex2bin (len
, secret
, buf
);
149 passwd2des_internal (passwd
, key
);
152 err
= cbc_crypt (key
, buf
, len
, DES_DECRYPT
| DES_HW
, ivec
);
153 if (DES_FAILED (err
))
158 bin2hex (len
, (unsigned char *) buf
, secret
);
164 * Hex to binary conversion
168 hex2bin (int len
, char *hexnum
, char *binnum
)
172 for (i
= 0; i
< len
; i
++)
173 *binnum
++ = 16 * hexval (hexnum
[2 * i
]) + hexval (hexnum
[2 * i
+ 1]);
177 * Binary to hex conversion
181 bin2hex (int len
, unsigned char *binnum
, char *hexnum
)
186 for (i
= 0; i
< len
; i
++)
189 hexnum
[i
* 2] = hex
[val
>> 4];
190 hexnum
[i
* 2 + 1] = hex
[val
& 0xf];
199 if (c
>= '0' && c
<= '9')
201 else if (c
>= 'a' && c
<= 'z')
202 return (c
- 'a' + 10);
203 else if (c
>= 'A' && c
<= 'Z')
204 return (c
- 'A' + 10);