xcrypt.c revision 92917
1181053Srwatson/*
2180701Srwatson * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3155192Srwatson * unrestricted use provided that this legend is included on all tape
4155192Srwatson * media and as a part of the software program in whole or part.  Users
5155192Srwatson * may copy or modify Sun RPC without charge, but are not authorized
6155192Srwatson * to license or distribute it to anyone else except as part of a product or
7155192Srwatson * program developed by the user.
8155192Srwatson *
9155192Srwatson * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10155192Srwatson * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11155192Srwatson * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12155192Srwatson *
13180701Srwatson * Sun RPC is provided with no support and without any obligation on the
14155192Srwatson * part of Sun Microsystems, Inc. to assist in its use, correction,
15155192Srwatson * modification or enhancement.
16155192Srwatson *
17155192Srwatson * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18155192Srwatson * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19155192Srwatson * OR ANY PART THEREOF.
20155192Srwatson *
21155192Srwatson * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22155192Srwatson * or profits or other special, indirect and consequential damages, even if
23155192Srwatson * Sun has been advised of the possibility of such damages.
24155192Srwatson *
25155192Srwatson * Sun Microsystems, Inc.
26155192Srwatson * 2550 Garcia Avenue
27155192Srwatson * Mountain View, California  94043
28155192Srwatson */
29155192Srwatson/*
30178186Srwatson * Hex encryption/decryption and utility routines
31178186Srwatson *
32178186Srwatson * Copyright (C) 1986, Sun Microsystems, Inc.
33155192Srwatson */
34155192Srwatson
35155192Srwatson#include <sys/cdefs.h>
36155192Srwatson__FBSDID("$FreeBSD: head/lib/librpcsvc/xcrypt.c 92917 2002-03-21 23:54:04Z obrien $");
37155192Srwatson
38155192Srwatson#include <stdio.h>
39155192Srwatson#include <stdlib.h>
40155192Srwatson#include <string.h>
41155192Srwatson#include <rpc/des_crypt.h>
42159277Srwatson
43155192Srwatsonstatic char hex[];	/* forward */
44155192Srwatsonstatic char hexval( char );
45155192Srwatsonstatic void bin2hex( int, unsigned char *, char * );
46155192Srwatsonstatic void hex2bin( int, char *, char * );
47155192Srwatsonvoid passwd2des( char *, char * );
48155192Srwatson
49155192Srwatson/*
50155192Srwatson * Encrypt a secret key given passwd
51155192Srwatson * The secret key is passed and returned in hex notation.
52155192Srwatson * Its length must be a multiple of 16 hex digits (64 bits).
53155192Srwatson */
54155192Srwatsonint
55170196Srwatsonxencrypt(secret, passwd)
56170196Srwatson	char *secret;
57170196Srwatson	char *passwd;
58170196Srwatson{
59155192Srwatson	char key[8];
60155192Srwatson	char ivec[8];
61155192Srwatson	char *buf;
62155192Srwatson	int err;
63155192Srwatson	int len;
64170585Srwatson
65155192Srwatson	len = strlen(secret) / 2;
66155192Srwatson	buf = malloc((unsigned)len);
67155192Srwatson
68155192Srwatson	hex2bin(len, secret, buf);
69155192Srwatson	passwd2des(passwd, key);
70155192Srwatson	bzero(ivec, 8);
71155192Srwatson
72155192Srwatson	err = cbc_crypt(key, buf, len, DES_ENCRYPT | DES_HW, ivec);
73155192Srwatson	if (DES_FAILED(err)) {
74155192Srwatson		free(buf);
75155192Srwatson		return (0);
76155192Srwatson	}
77155192Srwatson	bin2hex(len, (unsigned char *) buf, secret);
78155192Srwatson	free(buf);
79155192Srwatson	return (1);
80155192Srwatson}
81155192Srwatson
82155192Srwatson/*
83155192Srwatson * Decrypt secret key using passwd
84155192Srwatson * The secret key is passed and returned in hex notation.
85155192Srwatson * Once again, the length is a multiple of 16 hex digits
86155192Srwatson */
87155192Srwatsonint
88155192Srwatsonxdecrypt(secret, passwd)
89155192Srwatson	char *secret;
90155192Srwatson	char *passwd;
91155192Srwatson{
92155192Srwatson	char key[8];
93155192Srwatson	char ivec[8];
94155192Srwatson	char *buf;
95155192Srwatson	int err;
96155192Srwatson	int len;
97155192Srwatson
98155192Srwatson	len = strlen(secret) / 2;
99155192Srwatson	buf = malloc((unsigned)len);
100155192Srwatson
101155192Srwatson	hex2bin(len, secret, buf);
102155192Srwatson	passwd2des(passwd, key);
103155192Srwatson	bzero(ivec, 8);
104195925Srwatson
105195925Srwatson	err = cbc_crypt(key, buf, len, DES_DECRYPT | DES_HW, ivec);
106195925Srwatson	if (DES_FAILED(err)) {
107195925Srwatson		free(buf);
108195925Srwatson		return (0);
109195925Srwatson	}
110195925Srwatson	bin2hex(len, (unsigned char *) buf, secret);
111195925Srwatson	free(buf);
112195925Srwatson	return (1);
113195925Srwatson}
114195925Srwatson
115195925Srwatson
116195925Srwatson/*
117195925Srwatson * Turn password into DES key
118195925Srwatson */
119195925Srwatsonvoid
120195925Srwatsonpasswd2des(pw, key)
121195925Srwatson	char *pw;
122195925Srwatson	char *key;
123195925Srwatson{
124195925Srwatson	int i;
125195925Srwatson
126195925Srwatson	bzero(key, 8);
127195925Srwatson	for (i = 0; *pw; i = (i+1)%8) {
128195925Srwatson		key[i] ^= *pw++ << 1;
129195925Srwatson	}
130155192Srwatson	des_setparity(key);
131155192Srwatson}
132155192Srwatson
133155192Srwatson
134155192Srwatson
135155192Srwatson/*
136155192Srwatson * Hex to binary conversion
137155192Srwatson */
138155192Srwatsonstatic void
139155192Srwatsonhex2bin(len, hexnum, binnum)
140155192Srwatson	int len;
141155192Srwatson	char *hexnum;
142155192Srwatson	char *binnum;
143155192Srwatson{
144155192Srwatson	int i;
145155192Srwatson
146155192Srwatson	for (i = 0; i < len; i++) {
147155192Srwatson		*binnum++ = 16 * hexval(hexnum[2*i]) + hexval(hexnum[2*i+1]);
148155192Srwatson	}
149155192Srwatson}
150155192Srwatson
151155192Srwatson/*
152155192Srwatson * Binary to hex conversion
153155192Srwatson */
154155192Srwatsonstatic void
155155192Srwatsonbin2hex(len, binnum, hexnum)
156155192Srwatson	int len;
157155192Srwatson	unsigned char *binnum;
158155192Srwatson	char *hexnum;
159155192Srwatson{
160155192Srwatson	int i;
161155192Srwatson	unsigned val;
162155192Srwatson
163155192Srwatson	for (i = 0; i < len; i++) {
164155192Srwatson		val = binnum[i];
165155192Srwatson		hexnum[i*2] = hex[val >> 4];
166155192Srwatson		hexnum[i*2+1] = hex[val & 0xf];
167155192Srwatson	}
168155192Srwatson	hexnum[len*2] = 0;
169155192Srwatson}
170155192Srwatson
171155192Srwatsonstatic char hex[16] = {
172155192Srwatson	'0', '1', '2', '3', '4', '5', '6', '7',
173155192Srwatson	'8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
174155192Srwatson};
175155192Srwatson
176155192Srwatsonstatic char
177155192Srwatsonhexval(c)
178155192Srwatson	char c;
179155192Srwatson{
180155192Srwatson	if (c >= '0' && c <= '9') {
181155192Srwatson		return (c - '0');
182155192Srwatson	} else if (c >= 'a' && c <= 'z') {
183155192Srwatson		return (c - 'a' + 10);
184155192Srwatson	} else if (c >= 'A' && c <= 'Z') {
185155192Srwatson		return (c - 'A' + 10);
186155192Srwatson	} else {
187155192Srwatson		return (-1);
188155192Srwatson	}
189155192Srwatson}
190155192Srwatson