NULL should never be passed to ether_poll_deregister(). Try catching any
[dragonfly/port-amd64.git] / lib / librpcsvc / xcrypt.c
blobc156b4e5e4be55a28519405996e77e9037db197c
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.
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.
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.
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.
25 * Sun Microsystems, Inc.
26 * 2550 Garcia Avenue
27 * Mountain View, California 94043
29 * @(#)xcrypt.c 2.2 88/08/10 4.0 RPCSRC
30 * $FreeBSD: src/lib/librpcsvc/xcrypt.c,v 1.2 1999/08/28 00:05:24 peter Exp $
31 * $DragonFly: src/lib/librpcsvc/xcrypt.c,v 1.3 2003/11/12 20:21:31 eirikn Exp $
34 * Hex encryption/decryption and utility routines
36 * Copyright (C) 1986, Sun Microsystems, Inc.
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <sys/cdefs.h>
43 #include <rpc/des_crypt.h>
45 static char hex[]; /* forward */
46 static char hexval ( char );
47 static void bin2hex ( int, unsigned char *, char * );
48 static void hex2bin ( int, char *, char * );
49 void passwd2des ( char *, char * );
52 * Encrypt a secret key given passwd
53 * The secret key is passed and returned in hex notation.
54 * Its length must be a multiple of 16 hex digits (64 bits).
56 int
57 xencrypt(secret, passwd)
58 char *secret;
59 char *passwd;
61 char key[8];
62 char ivec[8];
63 char *buf;
64 int err;
65 int len;
67 len = strlen(secret) / 2;
68 buf = malloc((unsigned)len);
70 hex2bin(len, secret, buf);
71 passwd2des(passwd, key);
72 bzero(ivec, 8);
74 err = cbc_crypt(key, buf, len, DES_ENCRYPT | DES_HW, ivec);
75 if (DES_FAILED(err)) {
76 free(buf);
77 return (0);
79 bin2hex(len, (unsigned char *) buf, secret);
80 free(buf);
81 return (1);
85 * Decrypt secret key using passwd
86 * The secret key is passed and returned in hex notation.
87 * Once again, the length is a multiple of 16 hex digits
89 int
90 xdecrypt(secret, passwd)
91 char *secret;
92 char *passwd;
94 char key[8];
95 char ivec[8];
96 char *buf;
97 int err;
98 int len;
100 len = strlen(secret) / 2;
101 buf = malloc((unsigned)len);
103 hex2bin(len, secret, buf);
104 passwd2des(passwd, key);
105 bzero(ivec, 8);
107 err = cbc_crypt(key, buf, len, DES_DECRYPT | DES_HW, ivec);
108 if (DES_FAILED(err)) {
109 free(buf);
110 return (0);
112 bin2hex(len, (unsigned char *) buf, secret);
113 free(buf);
114 return (1);
119 * Turn password into DES key
121 void
122 passwd2des(pw, key)
123 char *pw;
124 char *key;
126 int i;
128 bzero(key, 8);
129 for (i = 0; *pw; i = (i+1)%8) {
130 key[i] ^= *pw++ << 1;
132 des_setparity(key);
138 * Hex to binary conversion
140 static void
141 hex2bin(len, hexnum, binnum)
142 int len;
143 char *hexnum;
144 char *binnum;
146 int i;
148 for (i = 0; i < len; i++) {
149 *binnum++ = 16 * hexval(hexnum[2*i]) + hexval(hexnum[2*i+1]);
154 * Binary to hex conversion
156 static void
157 bin2hex(len, binnum, hexnum)
158 int len;
159 unsigned char *binnum;
160 char *hexnum;
162 int i;
163 unsigned val;
165 for (i = 0; i < len; i++) {
166 val = binnum[i];
167 hexnum[i*2] = hex[val >> 4];
168 hexnum[i*2+1] = hex[val & 0xf];
170 hexnum[len*2] = 0;
173 static char hex[16] = {
174 '0', '1', '2', '3', '4', '5', '6', '7',
175 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
178 static char
179 hexval(c)
180 char c;
182 if (c >= '0' && c <= '9') {
183 return (c - '0');
184 } else if (c >= 'a' && c <= 'z') {
185 return (c - 'a' + 10);
186 } else if (c >= 'A' && c <= 'Z') {
187 return (c - 'A' + 10);
188 } else {
189 return (-1);