Update.
[glibc.git] / sunrpc / xcrypt.c
blobd770a7d648ea91ceccc401e8c35b0c929fd293ec
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 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 (char *pw, char *key);
75 * Encrypt a secret key given passwd
76 * The secret key is passed and returned in hex notation.
77 * Its length must be a multiple of 16 hex digits (64 bits).
79 int
80 xencrypt (char *secret, char *passwd)
82 char key[8];
83 char ivec[8];
84 char *buf;
85 int err;
86 int len;
88 len = strlen (secret) / 2;
89 buf = malloc ((unsigned) len);
90 hex2bin (len, secret, buf);
91 passwd2des (passwd, key);
92 memset (ivec, 0, 8);
94 err = cbc_crypt (key, buf, len, DES_ENCRYPT | DES_HW, ivec);
95 if (DES_FAILED (err))
97 free (buf);
98 return 0;
100 bin2hex (len, (unsigned char *) buf, secret);
101 free (buf);
102 return 1;
106 * Decrypt secret key using passwd
107 * The secret key is passed and returned in hex notation.
108 * Once again, the length is a multiple of 16 hex digits
111 xdecrypt (char *secret, char *passwd)
113 char key[8];
114 char ivec[8];
115 char *buf;
116 int err;
117 int len;
119 len = strlen (secret) / 2;
120 buf = malloc ((unsigned) len);
122 hex2bin (len, secret, buf);
123 passwd2des (passwd, key);
124 memset (ivec, 0, 8);
126 err = cbc_crypt (key, buf, len, DES_DECRYPT | DES_HW, ivec);
127 if (DES_FAILED (err))
129 free (buf);
130 return 0;
132 bin2hex (len, (unsigned char *) buf, secret);
133 free (buf);
134 return 1;
138 * Turn password into DES key
140 void
141 passwd2des (char *pw, char *key)
143 int i;
145 memset (key, 0, 8);
146 for (i = 0; *pw; i = (i + 1) % 8)
147 key[i] ^= *pw++ << 1;
149 des_setparity (key);
153 * Hex to binary conversion
155 static void
156 internal_function
157 hex2bin (int len, char *hexnum, char *binnum)
159 int i;
161 for (i = 0; i < len; i++)
162 *binnum++ = 16 * hexval (hexnum[2 * i]) + hexval (hexnum[2 * i + 1]);
166 * Binary to hex conversion
168 static void
169 internal_function
170 bin2hex (int len, unsigned char *binnum, char *hexnum)
172 int i;
173 unsigned val;
175 for (i = 0; i < len; i++)
177 val = binnum[i];
178 hexnum[i * 2] = hex[val >> 4];
179 hexnum[i * 2 + 1] = hex[val & 0xf];
181 hexnum[len * 2] = 0;
184 #ifndef _LIBC
185 static char
186 hexval (char c)
188 if (c >= '0' && c <= '9')
189 return (c - '0');
190 else if (c >= 'a' && c <= 'z')
191 return (c - 'a' + 10);
192 else if (c >= 'A' && c <= 'Z')
193 return (c - 'A' + 10);
194 else
195 return -1;
197 #endif