Add PCICAP_{ID,NEXTPTR} to avoid using magic number
[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.
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.
12 *
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.
16 *
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.
20 *
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.
24 *
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
30 * $FreeBSD: src/lib/librpcsvc/xcrypt.c,v 1.2 1999/08/28 00:05:24 peter 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 *
36 * Copyright (C) 1986, Sun Microsystems, Inc.
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
984263bc 45static char hex[]; /* forward */
064e1fb3
EN
46static char hexval ( char );
47static void bin2hex ( int, unsigned char *, char * );
48static void hex2bin ( int, char *, char * );
49void passwd2des ( char *, char * );
984263bc
MD
50
51/*
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).
55 */
56int
58f0ca6c 57xencrypt(char *secret, char *passwd)
984263bc
MD
58{
59 char key[8];
60 char ivec[8];
61 char *buf;
62 int err;
63 int len;
64
65 len = strlen(secret) / 2;
66 buf = malloc((unsigned)len);
67
68 hex2bin(len, secret, buf);
69 passwd2des(passwd, key);
70 bzero(ivec, 8);
71
72 err = cbc_crypt(key, buf, len, DES_ENCRYPT | DES_HW, ivec);
73 if (DES_FAILED(err)) {
74 free(buf);
75 return (0);
76 }
77 bin2hex(len, (unsigned char *) buf, secret);
78 free(buf);
79 return (1);
80}
81
82/*
83 * Decrypt secret key using passwd
84 * The secret key is passed and returned in hex notation.
85 * Once again, the length is a multiple of 16 hex digits
86 */
87int
58f0ca6c 88xdecrypt(char *secret, char *passwd)
984263bc
MD
89{
90 char key[8];
91 char ivec[8];
92 char *buf;
93 int err;
94 int len;
95
96 len = strlen(secret) / 2;
97 buf = malloc((unsigned)len);
98
99 hex2bin(len, secret, buf);
100 passwd2des(passwd, key);
101 bzero(ivec, 8);
102
103 err = cbc_crypt(key, buf, len, DES_DECRYPT | DES_HW, ivec);
104 if (DES_FAILED(err)) {
105 free(buf);
106 return (0);
107 }
108 bin2hex(len, (unsigned char *) buf, secret);
109 free(buf);
110 return (1);
111}
112
113
114/*
115 * Turn password into DES key
116 */
117void
58f0ca6c 118passwd2des(char *pw, char *key)
984263bc
MD
119{
120 int i;
121
122 bzero(key, 8);
123 for (i = 0; *pw; i = (i+1)%8) {
124 key[i] ^= *pw++ << 1;
125 }
126 des_setparity(key);
127}
128
129
130
131/*
132 * Hex to binary conversion
133 */
134static void
58f0ca6c 135hex2bin(int len, char *hexnum, char *binnum)
984263bc
MD
136{
137 int i;
138
139 for (i = 0; i < len; i++) {
140 *binnum++ = 16 * hexval(hexnum[2*i]) + hexval(hexnum[2*i+1]);
141 }
142}
143
144/*
145 * Binary to hex conversion
146 */
147static void
58f0ca6c 148bin2hex(int len, unsigned char *binnum, char *hexnum)
984263bc
MD
149{
150 int i;
151 unsigned val;
152
153 for (i = 0; i < len; i++) {
154 val = binnum[i];
155 hexnum[i*2] = hex[val >> 4];
156 hexnum[i*2+1] = hex[val & 0xf];
157 }
158 hexnum[len*2] = 0;
159}
160
161static char hex[16] = {
162 '0', '1', '2', '3', '4', '5', '6', '7',
163 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
164};
165
166static char
58f0ca6c 167hexval(char c)
984263bc
MD
168{
169 if (c >= '0' && c <= '9') {
170 return (c - '0');
171 } else if (c >= 'a' && c <= 'z') {
172 return (c - 'a' + 10);
173 } else if (c >= 'A' && c <= 'Z') {
174 return (c - 'A' + 10);
175 } else {
176 return (-1);
177 }
178}