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