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