10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22*1219Sraf 
230Sstevel@tonic-gate /*
24*1219Sraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
290Sstevel@tonic-gate /*	  All Rights Reserved  	*/
300Sstevel@tonic-gate 
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
330Sstevel@tonic-gate  * under license from the Regents of the University of California.
340Sstevel@tonic-gate  */
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
370Sstevel@tonic-gate /*LINTLIBRARY*/
380Sstevel@tonic-gate 
390Sstevel@tonic-gate /*
400Sstevel@tonic-gate  * DES encryption library routines
410Sstevel@tonic-gate  */
420Sstevel@tonic-gate 
43*1219Sraf #include "des_synonyms.h"
440Sstevel@tonic-gate #include <sys/types.h>
450Sstevel@tonic-gate #include <rpc/des_crypt.h>
460Sstevel@tonic-gate #include <sys/stat.h>
470Sstevel@tonic-gate #include <fcntl.h>
480Sstevel@tonic-gate #include <unistd.h>
490Sstevel@tonic-gate #include <stropts.h>
500Sstevel@tonic-gate #ifdef sun
510Sstevel@tonic-gate #include <sys/ioctl.h>
520Sstevel@tonic-gate #include <sys/des.h>
530Sstevel@tonic-gate #ifdef _KERNEL
540Sstevel@tonic-gate #include <sys/conf.h>
550Sstevel@tonic-gate #define	getdesfd() (cdevsw[11].d_open(0, 0) ? -1 : 0)
560Sstevel@tonic-gate #define	ioctl(a, b, c) (cdevsw[11].d_ioctl(0, b, c, 0) ? -1 : 0)
570Sstevel@tonic-gate #ifndef CRYPT
580Sstevel@tonic-gate #define	__des_crypt(a, b, c) 0
590Sstevel@tonic-gate #endif
600Sstevel@tonic-gate #else
610Sstevel@tonic-gate #define	getdesfd()	(open("/dev/des", 0, 0))
620Sstevel@tonic-gate #endif
630Sstevel@tonic-gate #else
640Sstevel@tonic-gate #include <des/des.h>
650Sstevel@tonic-gate #endif
660Sstevel@tonic-gate 
670Sstevel@tonic-gate #include "des_soft.h"
680Sstevel@tonic-gate 
690Sstevel@tonic-gate /*
700Sstevel@tonic-gate  * To see if chip is installed
710Sstevel@tonic-gate  */
720Sstevel@tonic-gate #define	UNOPENED (-2)
730Sstevel@tonic-gate 
740Sstevel@tonic-gate /*
750Sstevel@tonic-gate  * Copy 8 bytes
760Sstevel@tonic-gate  */
770Sstevel@tonic-gate #define	COPY8(src, dst) { \
780Sstevel@tonic-gate 	char *a = (char *) dst; \
790Sstevel@tonic-gate 	char *b = (char *) src; \
800Sstevel@tonic-gate 	*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
810Sstevel@tonic-gate 	*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
820Sstevel@tonic-gate }
830Sstevel@tonic-gate 
840Sstevel@tonic-gate /*
850Sstevel@tonic-gate  * Copy multiple of 8 bytes
860Sstevel@tonic-gate  */
870Sstevel@tonic-gate #define	DESCOPY(src, dst, len) { \
880Sstevel@tonic-gate 	char *a = (char *) dst; \
890Sstevel@tonic-gate 	char *b = (char *) src; \
900Sstevel@tonic-gate 	int i; \
910Sstevel@tonic-gate 	for (i = (int) len; i > 0; i -= 8) { \
920Sstevel@tonic-gate 		*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
930Sstevel@tonic-gate 		*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
940Sstevel@tonic-gate 	} \
950Sstevel@tonic-gate }
960Sstevel@tonic-gate static int common_crypt(char *, char *, unsigned, unsigned, struct desparams *);
970Sstevel@tonic-gate 
980Sstevel@tonic-gate /*
990Sstevel@tonic-gate  * CBC mode encryption
1000Sstevel@tonic-gate  */
1010Sstevel@tonic-gate int
1020Sstevel@tonic-gate cbc_crypt(char *key, char *buf, size_t len, unsigned int mode, char *ivec)
1030Sstevel@tonic-gate {
1040Sstevel@tonic-gate 	int err = 0;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate /* EXPORT DELETE START */
1070Sstevel@tonic-gate 	struct desparams dp;
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	dp.des_mode = CBC;
1100Sstevel@tonic-gate 	COPY8(ivec, dp.des_ivec);
1110Sstevel@tonic-gate 	err = common_crypt(key, buf, len, mode, &dp);
1120Sstevel@tonic-gate 	COPY8(dp.des_ivec, ivec);
1130Sstevel@tonic-gate /* EXPORT DELETE END */
1140Sstevel@tonic-gate 	return (err);
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate /*
1190Sstevel@tonic-gate  * ECB mode encryption
1200Sstevel@tonic-gate  */
1210Sstevel@tonic-gate int
1220Sstevel@tonic-gate ecb_crypt(char *key, char *buf, size_t len, unsigned int mode)
1230Sstevel@tonic-gate {
1240Sstevel@tonic-gate 	int ret = 0;
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate /* EXPORT DELETE START */
1270Sstevel@tonic-gate 	struct desparams dp;
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	dp.des_mode = ECB;
1300Sstevel@tonic-gate 	ret = common_crypt(key, buf, len, mode, &dp);
1310Sstevel@tonic-gate /* EXPORT DELETE END */
1320Sstevel@tonic-gate 	return (ret);
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate /* EXPORT DELETE START */
1370Sstevel@tonic-gate /*
1380Sstevel@tonic-gate  * Common code to cbc_crypt() & ecb_crypt()
1390Sstevel@tonic-gate  */
1400Sstevel@tonic-gate static int
1410Sstevel@tonic-gate common_crypt(char *key, char *buf, unsigned len, unsigned mode, struct desparams *desp)
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate 	int desdev;
1440Sstevel@tonic-gate 	int res;
1450Sstevel@tonic-gate 	int g_desfd = UNOPENED;
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	if ((len % 8) != 0 || len > DES_MAXDATA) {
1480Sstevel@tonic-gate 		return (DESERR_BADPARAM);
1490Sstevel@tonic-gate 	}
1500Sstevel@tonic-gate 	desp->des_dir =
1510Sstevel@tonic-gate 		((mode & DES_DIRMASK) == DES_ENCRYPT) ? ENCRYPT : DECRYPT;
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	desdev = mode & DES_DEVMASK;
1540Sstevel@tonic-gate 	COPY8(key, desp->des_key);
1550Sstevel@tonic-gate #ifdef sun
1560Sstevel@tonic-gate 	if (desdev == DES_HW) {
1570Sstevel@tonic-gate 		if (g_desfd < 0) {
1580Sstevel@tonic-gate 			if (g_desfd == -1 || (g_desfd = getdesfd()) < 0) {
1590Sstevel@tonic-gate 				goto software;	/* no hardware device */
1600Sstevel@tonic-gate 			}
1610Sstevel@tonic-gate 		}
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 		/*
1640Sstevel@tonic-gate 		 * hardware
1650Sstevel@tonic-gate 		 */
1660Sstevel@tonic-gate 		desp->des_len = len;
1670Sstevel@tonic-gate 		if (len <= DES_QUICKLEN) {
1680Sstevel@tonic-gate 			DESCOPY(buf, desp->des_data, len);
1690Sstevel@tonic-gate 			res = ioctl(g_desfd, (int)DESIOCQUICK, (char *) desp);
1700Sstevel@tonic-gate 			DESCOPY(desp->des_data, buf, len);
1710Sstevel@tonic-gate 		} else {
1720Sstevel@tonic-gate 			desp->des_buf = (u_char *) buf;
1730Sstevel@tonic-gate 			res = ioctl(g_desfd, (int)DESIOCBLOCK, (char *) desp);
1740Sstevel@tonic-gate 		}
1750Sstevel@tonic-gate 		return (res == 0 ? DESERR_NONE : DESERR_HWERROR);
1760Sstevel@tonic-gate 	}
1770Sstevel@tonic-gate software:
1780Sstevel@tonic-gate #endif
1790Sstevel@tonic-gate 	/*
1800Sstevel@tonic-gate 	 * software
1810Sstevel@tonic-gate 	 */
1820Sstevel@tonic-gate 	if (!__des_crypt(buf, len, desp)) {
1830Sstevel@tonic-gate 		return (DESERR_HWERROR);
1840Sstevel@tonic-gate 	}
1850Sstevel@tonic-gate 	return (desdev == DES_SW ? DESERR_NONE : DESERR_NOHWDEVICE);
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate /* EXPORT DELETE END */
188