BSD 4_3_Net_2 release
[unix-history] / usr / src / lib / librpc / secure_rpc / rpc / xcrypt.c
CommitLineData
af359dea
C
1#ifndef lint
2static char sccsid[] = "@(#)xcrypt.c 2.2 88/08/10 4.0 RPCSRC";
3#endif
4/*
5 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
6 * unrestricted use provided that this legend is included on all tape
7 * media and as a part of the software program in whole or part. Users
8 * may copy or modify Sun RPC without charge, but are not authorized
9 * to license or distribute it to anyone else except as part of a product or
10 * program developed by the user.
11 *
12 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
13 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
14 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15 *
16 * Sun RPC is provided with no support and without any obligation on the
17 * part of Sun Microsystems, Inc. to assist in its use, correction,
18 * modification or enhancement.
19 *
20 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
21 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
22 * OR ANY PART THEREOF.
23 *
24 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
25 * or profits or other special, indirect and consequential damages, even if
26 * Sun has been advised of the possibility of such damages.
27 *
28 * Sun Microsystems, Inc.
29 * 2550 Garcia Avenue
30 * Mountain View, California 94043
31 */
32/*
33 * Hex encryption/decryption and utility routines
34 *
35 * Copyright (C) 1986, Sun Microsystems, Inc.
36 */
37
38#include <stdio.h>
39#include <des_crypt.h>
40
41extern char *malloc();
42
43extern char hex[]; /* forward */
44static char hexval();
45
46/*
47 * Encrypt a secret key given passwd
48 * The secret key is passed and returned in hex notation.
49 * Its length must be a multiple of 16 hex digits (64 bits).
50 */
51xencrypt(secret, passwd)
52 char *secret;
53 char *passwd;
54{
55 char key[8];
56 char ivec[8];
57 char *buf;
58 int err;
59 int len;
60
61 len = strlen(secret) / 2;
62 buf = malloc((unsigned)len);
63
64 hex2bin(len, secret, buf);
65 passwd2des(passwd, key);
66 bzero(ivec, 8);
67
68 err = cbc_crypt(key, buf, len, DES_ENCRYPT | DES_HW, ivec);
69 if (DES_FAILED(err)) {
70 free(buf);
71 return (0);
72 }
73 bin2hex(len, (unsigned char *) buf, secret);
74 free(buf);
75 return (1);
76}
77
78/*
79 * Decrypt secret key using passwd
80 * The secret key is passed and returned in hex notation.
81 * Once again, the length is a multiple of 16 hex digits
82 */
83xdecrypt(secret, passwd)
84 char *secret;
85 char *passwd;
86{
87 char key[8];
88 char ivec[8];
89 char *buf;
90 int err;
91 int len;
92
93 len = strlen(secret) / 2;
94 buf = malloc((unsigned)len);
95
96 hex2bin(len, secret, buf);
97 passwd2des(passwd, key);
98 bzero(ivec, 8);
99
100 err = cbc_crypt(key, buf, len, DES_DECRYPT | DES_HW, ivec);
101 if (DES_FAILED(err)) {
102 free(buf);
103 return (0);
104 }
105 bin2hex(len, (unsigned char *) buf, secret);
106 free(buf);
107 return (1);
108}
109
110
111/*
112 * Turn password into DES key
113 */
114passwd2des(pw, key)
115 char *pw;
116 char *key;
117{
118 int i;
119
120 bzero(key, 8);
121 for (i = 0; *pw; i = (i+1)%8) {
122 key[i] ^= *pw++ << 1;
123 }
124 des_setparity(key);
125}
126
127
128
129/*
130 * Hex to binary conversion
131 */
132static
133hex2bin(len, hexnum, binnum)
134 int len;
135 char *hexnum;
136 char *binnum;
137{
138 int i;
139
140 for (i = 0; i < len; i++) {
141 *binnum++ = 16 * hexval(hexnum[2*i]) + hexval(hexnum[2*i+1]);
142 }
143}
144
145/*
146 * Binary to hex conversion
147 */
148static
149bin2hex(len, binnum, hexnum)
150 int len;
151 unsigned char *binnum;
152 char *hexnum;
153{
154 int i;
155 unsigned val;
156
157 for (i = 0; i < len; i++) {
158 val = binnum[i];
159 hexnum[i*2] = hex[val >> 4];
160 hexnum[i*2+1] = hex[val & 0xf];
161 }
162 hexnum[len*2] = 0;
163}
164
165static char hex[16] = {
166 '0', '1', '2', '3', '4', '5', '6', '7',
167 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
168};
169
170static char
171hexval(c)
172 char c;
173{
174 if (c >= '0' && c <= '9') {
175 return (c - '0');
176 } else if (c >= 'a' && c <= 'z') {
177 return (c - 'a' + 10);
178 } else if (c >= 'A' && c <= 'Z') {
179 return (c - 'A' + 10);
180 } else {
181 return (-1);
182 }
183}