Bring in a transport-independent RPC (TI-RPC).
[dragonfly.git] / lib / librpcsvc / xcrypt.c
CommitLineData
984263bc
MD
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.
ce0e08e2 8 *
984263bc
MD
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.
ce0e08e2 12 *
984263bc
MD
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.
ce0e08e2 16 *
984263bc
MD
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.
ce0e08e2 20 *
984263bc
MD
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.
ce0e08e2 24 *
984263bc
MD
25 * Sun Microsystems, Inc.
26 * 2550 Garcia Avenue
27 * Mountain View, California 94043
1de703da
MD
28 *
29 * @(#)xcrypt.c 2.2 88/08/10 4.0 RPCSRC
ce0e08e2 30 * $FreeBSD: src/lib/librpcsvc/xcrypt.c,v 1.6 2008/02/04 07:56:36 matteo Exp $
58f0ca6c 31 * $DragonFly: src/lib/librpcsvc/xcrypt.c,v 1.4 2007/11/25 14:33:02 swildner Exp $
984263bc
MD
32 */
33/*
34 * Hex encryption/decryption and utility routines
35 *
ce0e08e2 36 * Copyright (C) 1986, Sun Microsystems, Inc.
984263bc
MD
37 */
38
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include <sys/cdefs.h>
43#include <rpc/des_crypt.h>
44
ce0e08e2
PA
45static char hex[16] = {
46 '0', '1', '2', '3', '4', '5', '6', '7',
47 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
48};
49
50static void bin2hex(int, unsigned char *, char *);
51static void hex2bin(int, char *, char *);
52static char hexval(char);
53void passwd2des(char *, char *);
984263bc
MD
54
55/*
56 * Encrypt a secret key given passwd
57 * The secret key is passed and returned in hex notation.
58 * Its length must be a multiple of 16 hex digits (64 bits).
59 */
60int
58f0ca6c 61xencrypt(char *secret, char *passwd)
984263bc
MD
62{
63 char key[8];
64 char ivec[8];
65 char *buf;
66 int err;
67 int len;
68
69 len = strlen(secret) / 2;
ce0e08e2
PA
70 if ((buf = malloc((unsigned)len)) == NULL)
71 return(0);
984263bc
MD
72
73 hex2bin(len, secret, buf);
74 passwd2des(passwd, key);
75 bzero(ivec, 8);
76
77 err = cbc_crypt(key, buf, len, DES_ENCRYPT | DES_HW, ivec);
ce0e08e2 78 if (DES_FAILED(err)) {
984263bc
MD
79 free(buf);
80 return (0);
81 }
82 bin2hex(len, (unsigned char *) buf, secret);
83 free(buf);
84 return (1);
85}
86
87/*
88 * Decrypt secret key using passwd
89 * The secret key is passed and returned in hex notation.
90 * Once again, the length is a multiple of 16 hex digits
91 */
92int
58f0ca6c 93xdecrypt(char *secret, char *passwd)
984263bc
MD
94{
95 char key[8];
96 char ivec[8];
97 char *buf;
98 int err;
99 int len;
100
101 len = strlen(secret) / 2;
ce0e08e2
PA
102 if ((buf = malloc((unsigned)len)) == NULL)
103 return(0);
984263bc
MD
104
105 hex2bin(len, secret, buf);
ce0e08e2 106 passwd2des(passwd, key);
984263bc
MD
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
58f0ca6c 124passwd2des(char *pw, char *key)
984263bc
MD
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
58f0ca6c 141hex2bin(int len, char *hexnum, char *binnum)
984263bc
MD
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
58f0ca6c 154bin2hex(int len, unsigned char *binnum, char *hexnum)
984263bc
MD
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
984263bc 167static char
58f0ca6c 168hexval(char c)
984263bc
MD
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}