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