xref: /freebsd/lib/libc/rpc/des_crypt.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2009, Sun Microsystems, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  * - Redistributions of source code must retain the above copyright notice,
10  *   this list of conditions and the following disclaimer.
11  * - Redistributions in binary form must reproduce the above copyright notice,
12  *   this list of conditions and the following disclaimer in the documentation
13  *   and/or other materials provided with the distribution.
14  * - Neither the name of Sun Microsystems, Inc. nor the names of its
15  *   contributors may be used to endorse or promote products derived
16  *   from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 /*
31  * des_crypt.c, DES encryption library routines
32  * Copyright (C) 1986, Sun Microsystems, Inc.
33  */
34 
35 #include <sys/types.h>
36 #include <rpc/des_crypt.h>
37 #include <rpc/des.h>
38 
39 #if defined(LIBC_SCCS) && !defined(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 static int common_crypt( char *, char *, unsigned, unsigned, struct desparams * );
43 int (*__des_crypt_LOCAL)(char *, unsigned, struct desparams *) = 0;
44 extern int _des_crypt_call(char *, int, struct desparams *);
45 /*
46  * Copy 8 bytes
47  */
48 #define COPY8(src, dst) { \
49 	char *a = (char *) dst; \
50 	char *b = (char *) src; \
51 	*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
52 	*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
53 }
54 
55 /*
56  * Copy multiple of 8 bytes
57  */
58 #define DESCOPY(src, dst, len) { \
59 	char *a = (char *) dst; \
60 	char *b = (char *) src; \
61 	int i; \
62 	for (i = (int) len; i > 0; i -= 8) { \
63 		*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
64 		*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
65 	} \
66 }
67 
68 /*
69  * CBC mode encryption
70  */
71 int
72 cbc_crypt(char *key, char *buf, unsigned len, unsigned mode, char *ivec)
73 {
74 	int err;
75 	struct desparams dp;
76 
77 #ifdef BROKEN_DES
78 	dp.UDES.UDES_buf = buf;
79 	dp.des_mode = ECB;
80 #else
81 	dp.des_mode = CBC;
82 #endif
83 	COPY8(ivec, dp.des_ivec);
84 	err = common_crypt(key, buf, len, mode, &dp);
85 	COPY8(dp.des_ivec, ivec);
86 	return(err);
87 }
88 
89 
90 /*
91  * ECB mode encryption
92  */
93 int
94 ecb_crypt(char *key, char *buf, unsigned len, unsigned mode)
95 {
96 	struct desparams dp;
97 
98 #ifdef BROKEN_DES
99 	dp.UDES.UDES_buf = buf;
100 	dp.des_mode = CBC;
101 #else
102 	dp.des_mode = ECB;
103 #endif
104 	return(common_crypt(key, buf, len, mode, &dp));
105 }
106 
107 
108 
109 /*
110  * Common code to cbc_crypt() & ecb_crypt()
111  */
112 static int
113 common_crypt(char *key, char *buf, unsigned len, unsigned mode,
114     struct desparams *desp)
115 {
116 	int desdev;
117 
118 	if ((len % 8) != 0 || len > DES_MAXDATA) {
119 		return(DESERR_BADPARAM);
120 	}
121 	desp->des_dir =
122 		((mode & DES_DIRMASK) == DES_ENCRYPT) ? ENCRYPT : DECRYPT;
123 
124 	desdev = mode & DES_DEVMASK;
125 	COPY8(key, desp->des_key);
126 	/*
127 	 * software
128 	 */
129 	if (__des_crypt_LOCAL != NULL) {
130 		if (!__des_crypt_LOCAL(buf, len, desp)) {
131 			return (DESERR_HWERROR);
132 		}
133 	} else {
134 		if (!_des_crypt_call(buf, len, desp)) {
135 			return (DESERR_HWERROR);
136 		}
137 	}
138 	return(desdev == DES_SW ? DESERR_NONE : DESERR_NOHWDEVICE);
139 }
140