Update.
[glibc.git] / sunrpc / xcrypt.c
blob9f8c142649154a39a8c1eace273a5bf2406b2154
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 static void passwd2des_internal (char *pw, char *key);
76 * Turn password into DES key
78 static void
79 passwd2des_internal (char *pw, char *key)
81 int i;
83 memset (key, 0, 8);
84 for (i = 0; *pw && i < 8; ++i)
85 key[i] ^= *pw++ << 1;
87 des_setparity (key);
90 #ifdef _LIBC
91 strong_alias (passwd2des_internal, passwd2des)
92 #else
93 void passwd2des (char *pw, char *key)
95 return passwd2des_internal (pw, key);
97 #endif
100 * Encrypt a secret key given passwd
101 * The secret key is passed and returned in hex notation.
102 * Its length must be a multiple of 16 hex digits (64 bits).
105 xencrypt (char *secret, char *passwd)
107 char key[8];
108 char ivec[8];
109 char *buf;
110 int err;
111 int len;
113 len = strlen (secret) / 2;
114 buf = malloc ((unsigned) len);
115 hex2bin (len, secret, buf);
116 passwd2des_internal (passwd, key);
117 memset (ivec, 0, 8);
119 err = cbc_crypt (key, buf, len, DES_ENCRYPT | DES_HW, ivec);
120 if (DES_FAILED (err))
122 free (buf);
123 return 0;
125 bin2hex (len, (unsigned char *) buf, secret);
126 free (buf);
127 return 1;
131 * Decrypt secret key using passwd
132 * The secret key is passed and returned in hex notation.
133 * Once again, the length is a multiple of 16 hex digits
136 xdecrypt (char *secret, char *passwd)
138 char key[8];
139 char ivec[8];
140 char *buf;
141 int err;
142 int len;
144 len = strlen (secret) / 2;
145 buf = malloc ((unsigned) len);
147 hex2bin (len, secret, buf);
148 passwd2des_internal (passwd, key);
149 memset (ivec, 0, 8);
151 err = cbc_crypt (key, buf, len, DES_DECRYPT | DES_HW, ivec);
152 if (DES_FAILED (err))
154 free (buf);
155 return 0;
157 bin2hex (len, (unsigned char *) buf, secret);
158 free (buf);
159 return 1;
163 * Hex to binary conversion
165 static void
166 internal_function
167 hex2bin (int len, char *hexnum, char *binnum)
169 int i;
171 for (i = 0; i < len; i++)
172 *binnum++ = 16 * hexval (hexnum[2 * i]) + hexval (hexnum[2 * i + 1]);
176 * Binary to hex conversion
178 static void
179 internal_function
180 bin2hex (int len, unsigned char *binnum, char *hexnum)
182 int i;
183 unsigned val;
185 for (i = 0; i < len; i++)
187 val = binnum[i];
188 hexnum[i * 2] = hex[val >> 4];
189 hexnum[i * 2 + 1] = hex[val & 0xf];
191 hexnum[len * 2] = 0;
194 #ifndef _LIBC
195 static char
196 hexval (char c)
198 if (c >= '0' && c <= '9')
199 return (c - '0');
200 else if (c >= 'a' && c <= 'z')
201 return (c - 'a' + 10);
202 else if (c >= 'A' && c <= 'Z')
203 return (c - 'A' + 10);
204 else
205 return -1;
207 #endif