.
[glibc/history.git] / sunrpc / xcrypt.c
blobea8ff451053eeb3bf0dae3c838bc364e4439b544
1 /*
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.
26 * 2550 Garcia Avenue
27 * Mountain View, California 94043
31 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
34 #if 0
35 #ident "@(#)xcrypt.c 1.11 94/08/23 SMI"
36 #endif
38 #if !defined(lint) && defined(SCCSIDS)
39 static char sccsid[] = "@(#)xcrypt.c 1.3 89/03/24 Copyr 1986 Sun Micro";
40 #endif
43 * xcrypt.c: Hex encryption/decryption and utility routines
46 #include <ctype.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
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',
60 #ifdef _LIBC
61 # define hexval(c) \
62 (c >= '0' && c <= '9' \
63 ? c - '0' \
64 : ({ int upp = toupper (c); \
65 upp >= 'A' && upp <= 'Z' ? upp - 'A' + 10 : -1; }))
66 #else
67 static char hexval (char) internal_function;
68 #endif
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);
73 #ifdef _LIBC
74 libc_hidden_proto (passwd2des_internal)
75 #endif
78 * Turn password into DES key
80 void
81 passwd2des_internal (char *pw, char *key)
83 int i;
85 memset (key, 0, 8);
86 for (i = 0; *pw && i < 8; ++i)
87 key[i] ^= *pw++ << 1;
89 des_setparity (key);
92 #ifdef _LIBC
93 libc_hidden_def (passwd2des_internal)
94 strong_alias (passwd2des_internal, passwd2des)
95 #else
96 void passwd2des (char *pw, char *key)
98 return passwd2des_internal (pw, key);
100 #endif
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)
110 char key[8];
111 char ivec[8];
112 char *buf;
113 int err;
114 int len;
116 len = strlen (secret) / 2;
117 buf = malloc ((unsigned) len);
118 hex2bin (len, secret, buf);
119 passwd2des_internal (passwd, key);
120 memset (ivec, 0, 8);
122 err = cbc_crypt (key, buf, len, DES_ENCRYPT | DES_HW, ivec);
123 if (DES_FAILED (err))
125 free (buf);
126 return 0;
128 bin2hex (len, (unsigned char *) buf, secret);
129 free (buf);
130 return 1;
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)
141 char key[8];
142 char ivec[8];
143 char *buf;
144 int err;
145 int len;
147 len = strlen (secret) / 2;
148 buf = malloc ((unsigned) len);
150 hex2bin (len, secret, buf);
151 passwd2des_internal (passwd, key);
152 memset (ivec, 0, 8);
154 err = cbc_crypt (key, buf, len, DES_DECRYPT | DES_HW, ivec);
155 if (DES_FAILED (err))
157 free (buf);
158 return 0;
160 bin2hex (len, (unsigned char *) buf, secret);
161 free (buf);
162 return 1;
166 * Hex to binary conversion
168 static void
169 internal_function
170 hex2bin (int len, char *hexnum, char *binnum)
172 int i;
174 for (i = 0; i < len; i++)
175 *binnum++ = 16 * hexval (hexnum[2 * i]) + hexval (hexnum[2 * i + 1]);
179 * Binary to hex conversion
181 static void
182 internal_function
183 bin2hex (int len, unsigned char *binnum, char *hexnum)
185 int i;
186 unsigned val;
188 for (i = 0; i < len; i++)
190 val = binnum[i];
191 hexnum[i * 2] = hex[val >> 4];
192 hexnum[i * 2 + 1] = hex[val & 0xf];
194 hexnum[len * 2] = 0;
197 #ifndef _LIBC
198 static char
199 hexval (char c)
201 if (c >= '0' && c <= '9')
202 return (c - '0');
203 else if (c >= 'a' && c <= 'z')
204 return (c - 'a' + 10);
205 else if (c >= 'A' && c <= 'Z')
206 return (c - 'A' + 10);
207 else
208 return -1;
210 #endif