remove COPYING* from .gitignore files
[platform/upstream/glibc.git] / sunrpc / xcrypt.c
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.
8  *
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.
12  *
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.
16  *
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.
20  *
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.
24  *
25  * Sun Microsystems, Inc.
26  * 2550 Garcia Avenue
27  * Mountain View, California  94043
28  */
29
30 /*
31  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
32  */
33
34 #if 0
35 #ident  "@(#)xcrypt.c   1.11    94/08/23 SMI"
36 #endif
37
38 #if !defined(lint) && defined(SCCSIDS)
39 static char sccsid[] = "@(#)xcrypt.c 1.3 89/03/24 Copyr 1986 Sun Micro";
40 #endif
41
42 /*
43  * xcrypt.c: Hex encryption/decryption and utility routines
44  */
45
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>
52
53 static const char hex[16] =
54 {
55   '0', '1', '2', '3', '4', '5', '6', '7',
56   '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
57 };
58
59
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
69
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
76
77 /*
78  * Turn password into DES key
79  */
80 void
81 passwd2des_internal (char *pw, char *key)
82 {
83   int i;
84
85   memset (key, 0, 8);
86   for (i = 0; *pw && i < 8; ++i)
87     key[i] ^= *pw++ << 1;
88
89   des_setparity (key);
90 }
91
92 #ifdef _LIBC
93 libc_hidden_def (passwd2des_internal)
94 strong_alias (passwd2des_internal, passwd2des)
95 #else
96 void passwd2des (char *pw, char *key)
97 {
98   return passwd2des_internal (pw, key);
99 }
100 #endif
101
102 /*
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).
106  */
107 int
108 xencrypt (char *secret, char *passwd)
109 {
110   char key[8];
111   char ivec[8];
112   char *buf;
113   int err;
114   int len;
115
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);
121
122   err = cbc_crypt (key, buf, len, DES_ENCRYPT | DES_HW, ivec);
123   if (DES_FAILED (err))
124     {
125       free (buf);
126       return 0;
127     }
128   bin2hex (len, (unsigned char *) buf, secret);
129   free (buf);
130   return 1;
131 }
132
133 /*
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
137  */
138 int
139 xdecrypt (char *secret, char *passwd)
140 {
141   char key[8];
142   char ivec[8];
143   char *buf;
144   int err;
145   int len;
146
147   len = strlen (secret) / 2;
148   buf = malloc ((unsigned) len);
149
150   hex2bin (len, secret, buf);
151   passwd2des_internal (passwd, key);
152   memset (ivec, 0, 8);
153
154   err = cbc_crypt (key, buf, len, DES_DECRYPT | DES_HW, ivec);
155   if (DES_FAILED (err))
156     {
157       free (buf);
158       return 0;
159     }
160   bin2hex (len, (unsigned char *) buf, secret);
161   free (buf);
162   return 1;
163 }
164
165 /*
166  * Hex to binary conversion
167  */
168 static void
169 internal_function
170 hex2bin (int len, char *hexnum, char *binnum)
171 {
172   int i;
173
174   for (i = 0; i < len; i++)
175     *binnum++ = 16 * hexval (hexnum[2 * i]) + hexval (hexnum[2 * i + 1]);
176 }
177
178 /*
179  * Binary to hex conversion
180  */
181 static void
182 internal_function
183 bin2hex (int len, unsigned char *binnum, char *hexnum)
184 {
185   int i;
186   unsigned val;
187
188   for (i = 0; i < len; i++)
189     {
190       val = binnum[i];
191       hexnum[i * 2] = hex[val >> 4];
192       hexnum[i * 2 + 1] = hex[val & 0xf];
193     }
194   hexnum[len * 2] = 0;
195 }
196
197 #ifndef _LIBC
198 static char
199 hexval (char c)
200 {
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;
209 }
210 #endif