xcrypt.c revision 267654
133965Sjdp/*
2218822Sdim * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
333965Sjdp * unrestricted use provided that this legend is included on all tape
433965Sjdp * media and as a part of the software program in whole or part.  Users
533965Sjdp * may copy or modify Sun RPC without charge, but are not authorized
633965Sjdp * to license or distribute it to anyone else except as part of a product or
733965Sjdp * program developed by the user.
833965Sjdp *
933965Sjdp * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
1033965Sjdp * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
1133965Sjdp * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
1233965Sjdp *
1333965Sjdp * Sun RPC is provided with no support and without any obligation on the
1433965Sjdp * part of Sun Microsystems, Inc. to assist in its use, correction,
1533965Sjdp * modification or enhancement.
1633965Sjdp *
1733965Sjdp * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
1833965Sjdp * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
1933965Sjdp * OR ANY PART THEREOF.
2033965Sjdp *
21218822Sdim * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22218822Sdim * or profits or other special, indirect and consequential damages, even if
2333965Sjdp * Sun has been advised of the possibility of such damages.
2433965Sjdp *
2533965Sjdp * Sun Microsystems, Inc.
2633965Sjdp * 2550 Garcia Avenue
2733965Sjdp * Mountain View, California  94043
28218822Sdim */
2933965Sjdp/*
3033965Sjdp * Hex encryption/decryption and utility routines
3133965Sjdp *
3233965Sjdp * Copyright (C) 1986, Sun Microsystems, Inc.
3333965Sjdp */
3433965Sjdp
3533965Sjdp#include <sys/cdefs.h>
3633965Sjdp__FBSDID("$FreeBSD: releng/9.3/lib/librpcsvc/xcrypt.c 189087 2009-02-26 20:32:11Z ed $");
3733965Sjdp
3833965Sjdp#include <stdio.h>
3933965Sjdp#include <stdlib.h>
4033965Sjdp#include <string.h>
4133965Sjdp#include <rpc/des_crypt.h>
4233965Sjdp
4333965Sjdpstatic char hex[16] = {
44130561Sobrien	'0', '1', '2', '3', '4', '5', '6', '7',
4533965Sjdp	'8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
46218822Sdim};
4733965Sjdp
48218822Sdimstatic char hexval( char );
49218822Sdimstatic void bin2hex( int, unsigned char *, char * );
50218822Sdimstatic void hex2bin( int, char *, char * );
5133965Sjdpvoid passwd2des( char *, char * );
52218822Sdim
53218822Sdim/*
54218822Sdim * Encrypt a secret key given passwd
55218822Sdim * The secret key is passed and returned in hex notation.
56218822Sdim * Its length must be a multiple of 16 hex digits (64 bits).
57218822Sdim */
5833965Sjdpint
5933965Sjdpxencrypt(char *secret, char *passwd)
60218822Sdim{
61218822Sdim	char key[8];
6233965Sjdp	char ivec[8];
63218822Sdim	char *buf;
64218822Sdim	int err;
65218822Sdim	int len;
66218822Sdim
67218822Sdim	len = strlen(secret) / 2;
6833965Sjdp	if ((buf = malloc((unsigned)len)) == NULL) {
6933965Sjdp		return(0);
70130561Sobrien	}
71130561Sobrien
72130561Sobrien	hex2bin(len, secret, buf);
73218822Sdim	passwd2des(passwd, key);
74130561Sobrien	bzero(ivec, 8);
75130561Sobrien
76130561Sobrien	err = cbc_crypt(key, buf, len, DES_ENCRYPT | DES_HW, ivec);
77130561Sobrien	if (DES_FAILED(err)) {
78130561Sobrien		free(buf);
79130561Sobrien		return (0);
80130561Sobrien	}
8133965Sjdp	bin2hex(len, (unsigned char *) buf, secret);
8233965Sjdp	free(buf);
83130561Sobrien	return (1);
8433965Sjdp}
8533965Sjdp
86/*
87 * Decrypt secret key using passwd
88 * The secret key is passed and returned in hex notation.
89 * Once again, the length is a multiple of 16 hex digits
90 */
91int
92xdecrypt(char *secret, char *passwd)
93{
94	char key[8];
95	char ivec[8];
96	char *buf;
97	int err;
98	int len;
99
100	len = strlen(secret) / 2;
101	if ((buf = malloc((unsigned)len)) == NULL) {
102		return(0);
103	}
104
105	hex2bin(len, secret, buf);
106	passwd2des(passwd, key);
107	bzero(ivec, 8);
108
109	err = cbc_crypt(key, buf, len, DES_DECRYPT | DES_HW, ivec);
110	if (DES_FAILED(err)) {
111		free(buf);
112		return (0);
113	}
114	bin2hex(len, (unsigned char *) buf, secret);
115	free(buf);
116	return (1);
117}
118
119
120/*
121 * Turn password into DES key
122 */
123void
124passwd2des(char *pw, char *key)
125{
126	int i;
127
128	bzero(key, 8);
129	for (i = 0; *pw; i = (i+1)%8) {
130		key[i] ^= *pw++ << 1;
131	}
132	des_setparity(key);
133}
134
135
136
137/*
138 * Hex to binary conversion
139 */
140static void
141hex2bin(int len, char *hexnum, char *binnum)
142{
143	int i;
144
145	for (i = 0; i < len; i++) {
146		*binnum++ = 16 * hexval(hexnum[2*i]) + hexval(hexnum[2*i+1]);
147	}
148}
149
150/*
151 * Binary to hex conversion
152 */
153static void
154bin2hex(int len, unsigned char *binnum, char *hexnum)
155{
156	int i;
157	unsigned val;
158
159	for (i = 0; i < len; i++) {
160		val = binnum[i];
161		hexnum[i*2] = hex[val >> 4];
162		hexnum[i*2+1] = hex[val & 0xf];
163	}
164	hexnum[len*2] = 0;
165}
166
167static char
168hexval(char c)
169{
170	if (c >= '0' && c <= '9') {
171		return (c - '0');
172	} else if (c >= 'a' && c <= 'z') {
173		return (c - 'a' + 10);
174	} else if (c >= 'A' && c <= 'Z') {
175		return (c - 'A' + 10);
176	} else {
177		return (-1);
178	}
179}
180