xref: /freebsd/lib/libc/rpc/des_crypt.c (revision 74bf4e164ba5851606a27d4feff27717452583e5)
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
28  */
29 /*
30  * des_crypt.c, DES encryption library routines
31  * Copyright (C) 1986, Sun Microsystems, Inc.
32  */
33 
34 #include <sys/types.h>
35 #include <rpc/des_crypt.h>
36 #include <rpc/des.h>
37 
38 #if 0
39 #ifndef lint
40 static char sccsid[] = "@(#)des_crypt.c	2.2 88/08/10 4.0 RPCSRC; from 1.13 88/02/08 SMI";
41 #endif
42 #endif
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
45 
46 static int common_crypt( char *, char *, unsigned, unsigned, struct desparams * );
47 int (*__des_crypt_LOCAL)() = 0;
48 extern int _des_crypt_call(char *, int, struct desparams *);
49 /*
50  * Copy 8 bytes
51  */
52 #define COPY8(src, dst) { \
53 	char *a = (char *) dst; \
54 	char *b = (char *) src; \
55 	*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
56 	*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
57 }
58 
59 /*
60  * Copy multiple of 8 bytes
61  */
62 #define DESCOPY(src, dst, len) { \
63 	char *a = (char *) dst; \
64 	char *b = (char *) src; \
65 	int i; \
66 	for (i = (int) len; i > 0; i -= 8) { \
67 		*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
68 		*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
69 	} \
70 }
71 
72 /*
73  * CBC mode encryption
74  */
75 int
76 cbc_crypt(key, buf, len, mode, ivec)
77 	char *key;
78 	char *buf;
79 	unsigned len;
80 	unsigned mode;
81 	char *ivec;
82 {
83 	int err;
84 	struct desparams dp;
85 
86 #ifdef BROKEN_DES
87 	dp.UDES.UDES_buf = buf;
88 	dp.des_mode = ECB;
89 #else
90 	dp.des_mode = CBC;
91 #endif
92 	COPY8(ivec, dp.des_ivec);
93 	err = common_crypt(key, buf, len, mode, &dp);
94 	COPY8(dp.des_ivec, ivec);
95 	return(err);
96 }
97 
98 
99 /*
100  * ECB mode encryption
101  */
102 int
103 ecb_crypt(key, buf, len, mode)
104 	char *key;
105 	char *buf;
106 	unsigned len;
107 	unsigned mode;
108 {
109 	struct desparams dp;
110 
111 #ifdef BROKEN_DES
112 	dp.UDES.UDES_buf = buf;
113 	dp.des_mode = CBC;
114 #else
115 	dp.des_mode = ECB;
116 #endif
117 	return(common_crypt(key, buf, len, mode, &dp));
118 }
119 
120 
121 
122 /*
123  * Common code to cbc_crypt() & ecb_crypt()
124  */
125 static int
126 common_crypt(key, buf, len, mode, desp)
127 	char *key;
128 	char *buf;
129 	unsigned len;
130 	unsigned mode;
131 	struct desparams *desp;
132 {
133 	int desdev;
134 
135 	if ((len % 8) != 0 || len > DES_MAXDATA) {
136 		return(DESERR_BADPARAM);
137 	}
138 	desp->des_dir =
139 		((mode & DES_DIRMASK) == DES_ENCRYPT) ? ENCRYPT : DECRYPT;
140 
141 	desdev = mode & DES_DEVMASK;
142 	COPY8(key, desp->des_key);
143 	/*
144 	 * software
145 	 */
146 	if (__des_crypt_LOCAL != NULL) {
147 		if (!__des_crypt_LOCAL(buf, len, desp)) {
148 			return (DESERR_HWERROR);
149 		}
150 	} else {
151 		if (!_des_crypt_call(buf, len, desp)) {
152 			return (DESERR_HWERROR);
153 		}
154 	}
155 	return(desdev == DES_SW ? DESERR_NONE : DESERR_NOHWDEVICE);
156 }
157