Fix "make xcheck" in sunrpc.
[platform/upstream/glibc.git] / sunrpc / xcrypt.c
1 /*
2  * Copyright (c) 2010, Oracle America, Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  *       notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  *       copyright notice, this list of conditions and the following
12  *       disclaimer in the documentation and/or other materials
13  *       provided with the distribution.
14  *     * Neither the name of the "Oracle America, Inc." nor the names of its
15  *       contributors may be used to endorse or promote products derived
16  *       from this software without specific prior written permission.
17  *
18  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #if 0
33 #ident  "@(#)xcrypt.c   1.11    94/08/23 SMI"
34 #endif
35
36 #if !defined(lint) && defined(SCCSIDS)
37 static char sccsid[] = "@(#)xcrypt.c 1.3 89/03/24 Copyr 1986 Sun Micro";
38 #endif
39
40 /*
41  * xcrypt.c: Hex encryption/decryption and utility routines
42  */
43
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>
50
51 static const char hex[16] =
52 {
53   '0', '1', '2', '3', '4', '5', '6', '7',
54   '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
55 };
56
57
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
67
68 static void hex2bin (int, char *, char *) internal_function;
69 static void bin2hex (int, unsigned char *, char *) internal_function;
70 void passwd2des_internal (char *pw, char *key);
71 #ifdef _LIBC
72 libc_hidden_proto (passwd2des_internal)
73 #endif
74
75 /*
76  * Turn password into DES key
77  */
78 void
79 passwd2des_internal (char *pw, char *key)
80 {
81   int i;
82
83   memset (key, 0, 8);
84   for (i = 0; *pw && i < 8; ++i)
85     key[i] ^= *pw++ << 1;
86
87   des_setparity (key);
88 }
89
90 #ifdef _LIBC
91 libc_hidden_def (passwd2des_internal)
92 compat_symbol (libc, passwd2des_internal, passwd2des, GLIBC_2_1);
93 #else
94 void passwd2des (char *pw, char *key)
95 {
96   return passwd2des_internal (pw, key);
97 }
98 #endif
99
100 /*
101  * Encrypt a secret key given passwd
102  * The secret key is passed and returned in hex notation.
103  * Its length must be a multiple of 16 hex digits (64 bits).
104  */
105 int
106 xencrypt (char *secret, char *passwd)
107 {
108   char key[8];
109   char ivec[8];
110   char *buf;
111   int err;
112   int len;
113
114   len = strlen (secret) / 2;
115   buf = malloc ((unsigned) len);
116   hex2bin (len, secret, buf);
117   passwd2des_internal (passwd, key);
118   memset (ivec, 0, 8);
119
120   err = cbc_crypt (key, buf, len, DES_ENCRYPT | DES_HW, ivec);
121   if (DES_FAILED (err))
122     {
123       free (buf);
124       return 0;
125     }
126   bin2hex (len, (unsigned char *) buf, secret);
127   free (buf);
128   return 1;
129 }
130 libc_hidden_nolink (xencrypt, GLIBC_2_0)
131
132 /*
133  * Decrypt secret key using passwd
134  * The secret key is passed and returned in hex notation.
135  * Once again, the length is a multiple of 16 hex digits
136  */
137 int
138 xdecrypt (char *secret, char *passwd)
139 {
140   char key[8];
141   char ivec[8];
142   char *buf;
143   int err;
144   int len;
145
146   len = strlen (secret) / 2;
147   buf = malloc ((unsigned) len);
148
149   hex2bin (len, secret, buf);
150   passwd2des_internal (passwd, key);
151   memset (ivec, 0, 8);
152
153   err = cbc_crypt (key, buf, len, DES_DECRYPT | DES_HW, ivec);
154   if (DES_FAILED (err))
155     {
156       free (buf);
157       return 0;
158     }
159   bin2hex (len, (unsigned char *) buf, secret);
160   free (buf);
161   return 1;
162 }
163 #ifdef EXPORT_RPC_SYMBOLS
164 libc_hidden_def (xdecrypt)
165 #else
166 libc_hidden_nolink (xdecrypt, GLIBC_2_1)
167 #endif
168
169 /*
170  * Hex to binary conversion
171  */
172 static void
173 internal_function
174 hex2bin (int len, char *hexnum, char *binnum)
175 {
176   int i;
177
178   for (i = 0; i < len; i++)
179     *binnum++ = 16 * hexval (hexnum[2 * i]) + hexval (hexnum[2 * i + 1]);
180 }
181
182 /*
183  * Binary to hex conversion
184  */
185 static void
186 internal_function
187 bin2hex (int len, unsigned char *binnum, char *hexnum)
188 {
189   int i;
190   unsigned val;
191
192   for (i = 0; i < len; i++)
193     {
194       val = binnum[i];
195       hexnum[i * 2] = hex[val >> 4];
196       hexnum[i * 2 + 1] = hex[val & 0xf];
197     }
198   hexnum[len * 2] = 0;
199 }
200
201 #ifndef _LIBC
202 static char
203 hexval (char c)
204 {
205   if (c >= '0' && c <= '9')
206     return (c - '0');
207   else if (c >= 'a' && c <= 'z')
208     return (c - 'a' + 10);
209   else if (c >= 'A' && c <= 'Z')
210     return (c - 'A' + 10);
211   else
212     return -1;
213 }
214 #endif