Initial import from FreeBSD RELENG_4:
[dragonfly.git] / lib / librpcsvc / 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  * Hex encryption/decryption and utility routines
31  *
32  * Copyright (C) 1986, Sun Microsystems, Inc. 
33  */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/cdefs.h>
39 #include <rpc/des_crypt.h>
40
41 #ifndef lint
42 /*from: static char sccsid[] = "@(#)xcrypt.c    2.2 88/08/10 4.0 RPCSRC"; */
43 static const char rcsid[] = "$FreeBSD: src/lib/librpcsvc/xcrypt.c,v 1.2 1999/08/28 00:05:24 peter Exp $";
44 #endif
45
46 static char hex[];      /* forward */
47 static char hexval __P(( char ));
48 static void bin2hex __P(( int, unsigned char *, char * ));
49 static void hex2bin __P(( int, char *, char * ));
50 void passwd2des __P(( char *, char * ));
51
52 /*
53  * Encrypt a secret key given passwd
54  * The secret key is passed and returned in hex notation.
55  * Its length must be a multiple of 16 hex digits (64 bits).
56  */
57 int
58 xencrypt(secret, passwd)
59         char *secret;
60         char *passwd;
61 {
62         char key[8];
63         char ivec[8];
64         char *buf;
65         int err;
66         int len;
67
68         len = strlen(secret) / 2;
69         buf = malloc((unsigned)len);
70
71         hex2bin(len, secret, buf);
72         passwd2des(passwd, key);
73         bzero(ivec, 8);
74
75         err = cbc_crypt(key, buf, len, DES_ENCRYPT | DES_HW, ivec);
76         if (DES_FAILED(err)) {  
77                 free(buf);
78                 return (0);
79         }
80         bin2hex(len, (unsigned char *) buf, secret);
81         free(buf);
82         return (1);
83 }
84
85 /*
86  * Decrypt secret key using passwd
87  * The secret key is passed and returned in hex notation.
88  * Once again, the length is a multiple of 16 hex digits
89  */
90 int
91 xdecrypt(secret, passwd)
92         char *secret;
93         char *passwd;
94 {
95         char key[8];
96         char ivec[8];
97         char *buf;
98         int err;
99         int len;
100
101         len = strlen(secret) / 2;
102         buf = malloc((unsigned)len);
103
104         hex2bin(len, secret, buf);
105         passwd2des(passwd, key);        
106         bzero(ivec, 8);
107
108         err = cbc_crypt(key, buf, len, DES_DECRYPT | DES_HW, ivec);
109         if (DES_FAILED(err)) {
110                 free(buf);
111                 return (0);
112         }
113         bin2hex(len, (unsigned char *) buf, secret);
114         free(buf);
115         return (1);
116 }
117
118
119 /*
120  * Turn password into DES key
121  */
122 void
123 passwd2des(pw, key)
124         char *pw;
125         char *key;
126 {
127         int i;
128
129         bzero(key, 8);
130         for (i = 0; *pw; i = (i+1)%8) {
131                 key[i] ^= *pw++ << 1;
132         }
133         des_setparity(key);
134 }
135
136
137
138 /*
139  * Hex to binary conversion
140  */
141 static void
142 hex2bin(len, hexnum, binnum)
143         int len;
144         char *hexnum;
145         char *binnum;
146 {
147         int i;
148
149         for (i = 0; i < len; i++) {
150                 *binnum++ = 16 * hexval(hexnum[2*i]) + hexval(hexnum[2*i+1]);
151         }
152 }
153
154 /*
155  * Binary to hex conversion
156  */
157 static void
158 bin2hex(len, binnum, hexnum)
159         int len;
160         unsigned char *binnum;
161         char *hexnum;
162 {
163         int i;
164         unsigned val;
165
166         for (i = 0; i < len; i++) {
167                 val = binnum[i];
168                 hexnum[i*2] = hex[val >> 4];
169                 hexnum[i*2+1] = hex[val & 0xf];
170         }
171         hexnum[len*2] = 0;
172 }
173
174 static char hex[16] = {
175         '0', '1', '2', '3', '4', '5', '6', '7',
176         '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
177 };
178
179 static char
180 hexval(c)
181         char c;
182 {
183         if (c >= '0' && c <= '9') {
184                 return (c - '0');
185         } else if (c >= 'a' && c <= 'z') {
186                 return (c - 'a' + 10);
187         } else if (c >= 'A' && c <= 'Z') {
188                 return (c - 'A' + 10);
189         } else {
190                 return (-1);
191         }
192 }