Update.
[glibc.git] / sunrpc / xcrypt.c
blobdf191f5ad29460cae63e27dae6785a5c804dfe12
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 #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";
38 #endif
41 * xcrypt.c: Hex encryption/decryption and utility routines
44 #include <ctype.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <sys/types.h>
49 #include <rpc/des_crypt.h>
51 static char hex[16] =
53 '0', '1', '2', '3', '4', '5', '6', '7',
54 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
58 #ifdef _LIBC
59 # define hexval(c) \
60 (c >= '0' && c <= '9' \
61 ? c - '0' \
62 : ({ int upp = toupper (c); \
63 upp >= 'a' && upp <= 'z' ? upp - 'a' + 10 : -1; }))
64 #else
65 static char hexval (char) internal_function;
66 #endif
68 static void hex2bin (int, char *, char *) internal_function;
69 static void bin2hex (int, unsigned char *, char *) internal_function;
70 void passwd2des (char *pw, char *key);
73 * Encrypt a secret key given passwd
74 * The secret key is passed and returned in hex notation.
75 * Its length must be a multiple of 16 hex digits (64 bits).
77 int
78 xencrypt (char *secret, char *passwd)
80 char key[8];
81 char ivec[8];
82 char *buf;
83 int err;
84 int len;
86 len = strlen (secret) / 2;
87 buf = malloc ((unsigned) len);
88 hex2bin (len, secret, buf);
89 passwd2des (passwd, key);
90 memset (ivec, 0, 8);
92 err = cbc_crypt (key, buf, len, DES_ENCRYPT | DES_HW, ivec);
93 if (DES_FAILED (err))
95 free (buf);
96 return 0;
98 bin2hex (len, (unsigned char *) buf, secret);
99 free (buf);
100 return 1;
104 * Decrypt secret key using passwd
105 * The secret key is passed and returned in hex notation.
106 * Once again, the length is a multiple of 16 hex digits
109 xdecrypt (char *secret, char *passwd)
111 char key[8];
112 char ivec[8];
113 char *buf;
114 int err;
115 int len;
117 len = strlen (secret) / 2;
118 buf = malloc ((unsigned) len);
120 hex2bin (len, secret, buf);
121 passwd2des (passwd, key);
122 memset (ivec, 0, 8);
124 err = cbc_crypt (key, buf, len, DES_DECRYPT | DES_HW, ivec);
125 if (DES_FAILED (err))
127 free (buf);
128 return 0;
130 bin2hex (len, (unsigned char *) buf, secret);
131 free (buf);
132 return 1;
136 * Turn password into DES key
138 void
139 passwd2des (char *pw, char *key)
141 int i;
143 memset (key, 0, 8);
144 for (i = 0; *pw; i = (i + 1) % 8)
145 key[i] ^= *pw++ << 1;
147 des_setparity (key);
151 * Hex to binary conversion
153 static void
154 internal_function
155 hex2bin (int len, char *hexnum, char *binnum)
157 int i;
159 for (i = 0; i < len; i++)
160 *binnum++ = 16 * hexval (hexnum[2 * i]) + hexval (hexnum[2 * i + 1]);
164 * Binary to hex conversion
166 static void
167 internal_function
168 bin2hex (int len, unsigned char *binnum, char *hexnum)
170 int i;
171 unsigned val;
173 for (i = 0; i < len; i++)
175 val = binnum[i];
176 hexnum[i * 2] = hex[val >> 4];
177 hexnum[i * 2 + 1] = hex[val & 0xf];
179 hexnum[len * 2] = 0;
182 #ifndef _LIBC
183 static char
184 hexval (char c)
186 if (c >= '0' && c <= '9')
187 return (c - '0');
188 else if (c >= 'a' && c <= 'z')
189 return (c - 'a' + 10);
190 else if (c >= 'A' && c <= 'Z')
191 return (c - 'A' + 10);
192 else
193 return -1;
195 #endif