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