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