test - burst vmpageinfo pages
[dragonfly.git] / lib / librpcsvc / xcrypt.c
blob792d62c3fe3f1d0ee75a5b4d181953ae3b745438
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.
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.6 2008/02/04 07:56:36 matteo Exp $
33 * Hex encryption/decryption and utility routines
35 * Copyright (C) 1986, Sun Microsystems, Inc.
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <rpc/des_crypt.h>
43 static char hex[16] = {
44 '0', '1', '2', '3', '4', '5', '6', '7',
45 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
48 static void bin2hex(int, unsigned char *, char *);
49 static void hex2bin(int, char *, char *);
50 static char hexval(char);
51 void passwd2des(char *, char *);
54 * Encrypt a secret key given passwd
55 * The secret key is passed and returned in hex notation.
56 * Its length must be a multiple of 16 hex digits (64 bits).
58 int
59 xencrypt(char *secret, 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 if ((buf = malloc((unsigned)len)) == NULL)
69 return(0);
71 hex2bin(len, secret, buf);
72 passwd2des(passwd, key);
73 bzero(ivec, 8);
75 err = cbc_crypt(key, buf, len, DES_ENCRYPT | DES_HW, ivec);
76 if (DES_FAILED(err)) {
77 free(buf);
78 return (0);
80 bin2hex(len, (unsigned char *) buf, secret);
81 free(buf);
82 return (1);
86 * Decrypt secret key using passwd
87 * The secret key is passed and returned in hex notation.
88 * Once again, the length is a multiple of 16 hex digits
90 int
91 xdecrypt(char *secret, char *passwd)
93 char key[8];
94 char ivec[8];
95 char *buf;
96 int err;
97 int len;
99 len = strlen(secret) / 2;
100 if ((buf = malloc((unsigned)len)) == NULL)
101 return(0);
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(char *pw, char *key)
124 int i;
126 bzero(key, 8);
127 for (i = 0; *pw; i = (i+1)%8) {
128 key[i] ^= *pw++ << 1;
130 des_setparity(key);
136 * Hex to binary conversion
138 static void
139 hex2bin(int len, char *hexnum, char *binnum)
141 int i;
143 for (i = 0; i < len; i++) {
144 *binnum++ = 16 * hexval(hexnum[2*i]) + hexval(hexnum[2*i+1]);
149 * Binary to hex conversion
151 static void
152 bin2hex(int len, unsigned char *binnum, char *hexnum)
154 int i;
155 unsigned val;
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];
162 hexnum[len*2] = 0;
165 static char
166 hexval(char c)
168 if (c >= '0' && c <= '9') {
169 return (c - '0');
170 } else if (c >= 'a' && c <= 'z') {
171 return (c - 'a' + 10);
172 } else if (c >= 'A' && c <= 'Z') {
173 return (c - 'A' + 10);
174 } else {
175 return (-1);