]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - lib/librpcsvc/xcrypt.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.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 <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <rpc/des_crypt.h>
42
43 static char hex[16] = {
44         '0', '1', '2', '3', '4', '5', '6', '7',
45         '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
46 };
47
48 static char hexval( char );
49 static void bin2hex( int, unsigned char *, char * );
50 static void hex2bin( int, char *, char * );
51 void passwd2des( char *, char * );
52
53 /*
54  * Encrypt a secret key given passwd
55  * The secret key is passed and returned in hex notation.
56  * Its length must be a multiple of 16 hex digits (64 bits).
57  */
58 int
59 xencrypt(secret, passwd)
60         char *secret;
61         char *passwd;
62 {
63         char key[8];
64         char ivec[8];
65         char *buf;
66         int err;
67         int len;
68
69         len = strlen(secret) / 2;
70         if ((buf = malloc((unsigned)len)) == NULL) {
71                 return(0);
72         }
73
74         hex2bin(len, secret, buf);
75         passwd2des(passwd, key);
76         bzero(ivec, 8);
77
78         err = cbc_crypt(key, buf, len, DES_ENCRYPT | DES_HW, ivec);
79         if (DES_FAILED(err)) {  
80                 free(buf);
81                 return (0);
82         }
83         bin2hex(len, (unsigned char *) buf, secret);
84         free(buf);
85         return (1);
86 }
87
88 /*
89  * Decrypt secret key using passwd
90  * The secret key is passed and returned in hex notation.
91  * Once again, the length is a multiple of 16 hex digits
92  */
93 int
94 xdecrypt(secret, passwd)
95         char *secret;
96         char *passwd;
97 {
98         char key[8];
99         char ivec[8];
100         char *buf;
101         int err;
102         int len;
103
104         len = strlen(secret) / 2;
105         if ((buf = malloc((unsigned)len)) == NULL) {
106                 return(0);
107         }
108
109         hex2bin(len, secret, buf);
110         passwd2des(passwd, key);        
111         bzero(ivec, 8);
112
113         err = cbc_crypt(key, buf, len, DES_DECRYPT | DES_HW, ivec);
114         if (DES_FAILED(err)) {
115                 free(buf);
116                 return (0);
117         }
118         bin2hex(len, (unsigned char *) buf, secret);
119         free(buf);
120         return (1);
121 }
122
123
124 /*
125  * Turn password into DES key
126  */
127 void
128 passwd2des(pw, key)
129         char *pw;
130         char *key;
131 {
132         int i;
133
134         bzero(key, 8);
135         for (i = 0; *pw; i = (i+1)%8) {
136                 key[i] ^= *pw++ << 1;
137         }
138         des_setparity(key);
139 }
140
141
142
143 /*
144  * Hex to binary conversion
145  */
146 static void
147 hex2bin(len, hexnum, binnum)
148         int len;
149         char *hexnum;
150         char *binnum;
151 {
152         int i;
153
154         for (i = 0; i < len; i++) {
155                 *binnum++ = 16 * hexval(hexnum[2*i]) + hexval(hexnum[2*i+1]);
156         }
157 }
158
159 /*
160  * Binary to hex conversion
161  */
162 static void
163 bin2hex(len, binnum, hexnum)
164         int len;
165         unsigned char *binnum;
166         char *hexnum;
167 {
168         int i;
169         unsigned val;
170
171         for (i = 0; i < len; i++) {
172                 val = binnum[i];
173                 hexnum[i*2] = hex[val >> 4];
174                 hexnum[i*2+1] = hex[val & 0xf];
175         }
176         hexnum[len*2] = 0;
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 }