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 */
22132Srobinson
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) 1984, 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
380Sstevel@tonic-gate /*
39*1219Sraf * DES encryption library routines
400Sstevel@tonic-gate */
410Sstevel@tonic-gate
42*1219Sraf #include "mt.h"
430Sstevel@tonic-gate #include <unistd.h>
440Sstevel@tonic-gate #include <fcntl.h>
450Sstevel@tonic-gate #include <sys/types.h>
460Sstevel@tonic-gate #include <rpc/des_crypt.h>
470Sstevel@tonic-gate /* EXPORT DELETE START */
480Sstevel@tonic-gate #ifdef sun
490Sstevel@tonic-gate #include <sys/ioctl.h>
500Sstevel@tonic-gate #include <sys/des.h>
510Sstevel@tonic-gate #define getdesfd() (open("/dev/des", 0, 0))
520Sstevel@tonic-gate #else
530Sstevel@tonic-gate #include <des/des.h>
540Sstevel@tonic-gate #endif
550Sstevel@tonic-gate /* EXPORT DELETE END */
560Sstevel@tonic-gate #include <rpc/rpc.h>
570Sstevel@tonic-gate /* EXPORT DELETE START */
580Sstevel@tonic-gate
59132Srobinson extern int __des_crypt(char *, unsigned, struct desparams *);
600Sstevel@tonic-gate
61132Srobinson static int common_crypt(char *, char *, unsigned, unsigned, struct desparams *);
620Sstevel@tonic-gate
630Sstevel@tonic-gate /*
640Sstevel@tonic-gate * To see if chip is installed
650Sstevel@tonic-gate */
660Sstevel@tonic-gate #define UNOPENED (-2)
670Sstevel@tonic-gate static int g_desfd = UNOPENED;
680Sstevel@tonic-gate
690Sstevel@tonic-gate
700Sstevel@tonic-gate /*
710Sstevel@tonic-gate * Copy 8 bytes
720Sstevel@tonic-gate */
730Sstevel@tonic-gate #define COPY8(src, dst) { \
74132Srobinson char *a = (char *)dst; \
75132Srobinson char *b = (char *)src; \
760Sstevel@tonic-gate *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
770Sstevel@tonic-gate *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
780Sstevel@tonic-gate }
790Sstevel@tonic-gate
800Sstevel@tonic-gate /*
810Sstevel@tonic-gate * Copy multiple of 8 bytes
820Sstevel@tonic-gate */
830Sstevel@tonic-gate #define DESCOPY(src, dst, len) { \
84132Srobinson char *a = (char *)dst; \
85132Srobinson char *b = (char *)src; \
86132Srobinson int i; \
87132Srobinson for (i = (int)len; i > 0; i -= 8) { \
880Sstevel@tonic-gate *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
890Sstevel@tonic-gate *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
900Sstevel@tonic-gate } \
910Sstevel@tonic-gate }
920Sstevel@tonic-gate /* EXPORT DELETE END */
930Sstevel@tonic-gate
940Sstevel@tonic-gate /*
950Sstevel@tonic-gate * CBC mode encryption
960Sstevel@tonic-gate */
970Sstevel@tonic-gate int
cbc_crypt(char * key,char * buf,size_t len,unsigned int mode,char * ivec)980Sstevel@tonic-gate cbc_crypt(char *key, char *buf, size_t len, unsigned int mode, char *ivec)
990Sstevel@tonic-gate {
1000Sstevel@tonic-gate /* EXPORT DELETE START */
1010Sstevel@tonic-gate int err;
1020Sstevel@tonic-gate struct desparams dp;
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate dp.des_mode = CBC;
1050Sstevel@tonic-gate COPY8(ivec, dp.des_ivec);
1060Sstevel@tonic-gate err = common_crypt(key, buf, len, mode, &dp);
1070Sstevel@tonic-gate COPY8(dp.des_ivec, ivec);
1080Sstevel@tonic-gate return (err);
1090Sstevel@tonic-gate #if 0
1100Sstevel@tonic-gate /* EXPORT DELETE END */
1110Sstevel@tonic-gate return (DESERR_HWERROR);
1120Sstevel@tonic-gate /* EXPORT DELETE START */
1130Sstevel@tonic-gate #endif
1140Sstevel@tonic-gate /* EXPORT DELETE END */
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
ecb_crypt(char * key,char * buf,size_t len,unsigned int mode)1220Sstevel@tonic-gate ecb_crypt(char *key, char *buf, size_t len, unsigned int mode)
1230Sstevel@tonic-gate {
1240Sstevel@tonic-gate /* EXPORT DELETE START */
1250Sstevel@tonic-gate struct desparams dp;
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate dp.des_mode = ECB;
128132Srobinson return (common_crypt(key, buf, len, mode, &dp));
1290Sstevel@tonic-gate #if 0
1300Sstevel@tonic-gate /* EXPORT DELETE END */
1310Sstevel@tonic-gate return (DESERR_HWERROR);
1320Sstevel@tonic-gate /* EXPORT DELETE START */
1330Sstevel@tonic-gate #endif
1340Sstevel@tonic-gate /* EXPORT DELETE END */
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate /* EXPORT DELETE START */
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate /*
1410Sstevel@tonic-gate * Common code to cbc_crypt() & ecb_crypt()
1420Sstevel@tonic-gate */
1430Sstevel@tonic-gate static int
common_crypt(char * key,char * buf,unsigned len,unsigned mode,struct desparams * desp)144132Srobinson common_crypt(char *key, char *buf, unsigned len, unsigned mode,
145132Srobinson struct desparams *desp)
1460Sstevel@tonic-gate {
147132Srobinson int desdev;
148132Srobinson int res;
1490Sstevel@tonic-gate
150132Srobinson if ((len % 8) != 0 || len > DES_MAXDATA)
1510Sstevel@tonic-gate return (DESERR_BADPARAM);
1520Sstevel@tonic-gate desp->des_dir =
1530Sstevel@tonic-gate ((mode & DES_DIRMASK) == DES_ENCRYPT) ? ENCRYPT : DECRYPT;
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate desdev = mode & DES_DEVMASK;
1560Sstevel@tonic-gate COPY8(key, desp->des_key);
1570Sstevel@tonic-gate #ifdef sun
1580Sstevel@tonic-gate if (desdev == DES_HW) {
1590Sstevel@tonic-gate if (g_desfd < 0) {
1600Sstevel@tonic-gate if (g_desfd == -1 || (g_desfd = getdesfd()) < 0) {
1610Sstevel@tonic-gate goto software; /* no hardware device */
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate /*
1660Sstevel@tonic-gate * hardware
1670Sstevel@tonic-gate */
1680Sstevel@tonic-gate desp->des_len = len;
1690Sstevel@tonic-gate if (len <= DES_QUICKLEN) {
1700Sstevel@tonic-gate DESCOPY(buf, desp->des_data, len);
171132Srobinson res = ioctl(g_desfd, DESIOCQUICK, (char *)desp);
1720Sstevel@tonic-gate DESCOPY(desp->des_data, buf, len);
1730Sstevel@tonic-gate } else {
174132Srobinson desp->des_buf = (uchar_t *)buf;
175132Srobinson res = ioctl(g_desfd, DESIOCBLOCK, (char *)desp);
1760Sstevel@tonic-gate }
1770Sstevel@tonic-gate return (res == 0 ? DESERR_NONE : DESERR_HWERROR);
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate software:
1800Sstevel@tonic-gate #endif
1810Sstevel@tonic-gate /*
1820Sstevel@tonic-gate * software
1830Sstevel@tonic-gate */
184132Srobinson if (!__des_crypt(buf, len, desp))
1850Sstevel@tonic-gate return (DESERR_HWERROR);
1860Sstevel@tonic-gate return (desdev == DES_SW ? DESERR_NONE : DESERR_NOHWDEVICE);
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate /* EXPORT DELETE END */
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate /* EXPORT DELETE START */
1910Sstevel@tonic-gate static int
desN_crypt(des_block keys[],int keynum,char * buf,unsigned int len,unsigned int mode,char * ivec)1920Sstevel@tonic-gate desN_crypt(des_block keys[], int keynum, char *buf, unsigned int len,
1930Sstevel@tonic-gate unsigned int mode, char *ivec)
1940Sstevel@tonic-gate {
1950Sstevel@tonic-gate unsigned int m = mode & (DES_ENCRYPT | DES_DECRYPT);
1960Sstevel@tonic-gate unsigned int flags = mode & ~(DES_ENCRYPT | DES_DECRYPT);
1970Sstevel@tonic-gate des_block svec, dvec;
1980Sstevel@tonic-gate int i, j, stat;
1990Sstevel@tonic-gate
2000Sstevel@tonic-gate if (keynum < 1)
2010Sstevel@tonic-gate return (DESERR_BADPARAM);
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate (void) memcpy(svec.c, ivec, sizeof (des_block));
2040Sstevel@tonic-gate for (i = 0; i < keynum; i++) {
2050Sstevel@tonic-gate j = (mode & DES_DECRYPT) ? keynum - 1 - i : i;
2060Sstevel@tonic-gate stat = cbc_crypt(keys[j].c, buf, len, m | flags, ivec);
2070Sstevel@tonic-gate if (mode & DES_DECRYPT && i == 0)
2080Sstevel@tonic-gate (void) memcpy(dvec.c, ivec, sizeof (des_block));
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate if (DES_FAILED(stat))
2110Sstevel@tonic-gate return (stat);
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate m = (m == DES_ENCRYPT ? DES_DECRYPT : DES_ENCRYPT);
2140Sstevel@tonic-gate
2150Sstevel@tonic-gate if ((mode & DES_DECRYPT) || i != keynum - 1 || i%2)
2160Sstevel@tonic-gate (void) memcpy(ivec, svec.c, sizeof (des_block));
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate if (keynum % 2 == 0)
2190Sstevel@tonic-gate stat = cbc_crypt(keys[0].c, buf, len, mode, ivec);
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate if (mode & DES_DECRYPT)
2220Sstevel@tonic-gate (void) memcpy(ivec, dvec.c, sizeof (des_block));
2230Sstevel@tonic-gate
2240Sstevel@tonic-gate return (stat);
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate /* EXPORT DELETE END */
2270Sstevel@tonic-gate
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate
2300Sstevel@tonic-gate int
__cbc_triple_crypt(des_block keys[],char * buf,uint_t len,uint_t mode,char * ivec)231132Srobinson __cbc_triple_crypt(des_block keys[], char *buf, uint_t len,
232132Srobinson uint_t mode, char *ivec)
2330Sstevel@tonic-gate {
2340Sstevel@tonic-gate /* EXPORT DELETE START */
2350Sstevel@tonic-gate return (desN_crypt(keys, 3, buf, len, mode, ivec));
2360Sstevel@tonic-gate #if 0
2370Sstevel@tonic-gate /* EXPORT DELETE END */
2380Sstevel@tonic-gate return (DESERR_HWERROR);
2390Sstevel@tonic-gate /* EXPORT DELETE START */
2400Sstevel@tonic-gate #endif
2410Sstevel@tonic-gate /* EXPORT DELETE END */
2420Sstevel@tonic-gate }
243