1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 28*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 29*7c478bd9Sstevel@tonic-gate 30*7c478bd9Sstevel@tonic-gate /* 31*7c478bd9Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 4.3 BSD 32*7c478bd9Sstevel@tonic-gate * under license from the Regents of the University of California. 33*7c478bd9Sstevel@tonic-gate */ 34*7c478bd9Sstevel@tonic-gate 35*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 36*7c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/ 37*7c478bd9Sstevel@tonic-gate 38*7c478bd9Sstevel@tonic-gate /* 39*7c478bd9Sstevel@tonic-gate * DES encryption library routines 40*7c478bd9Sstevel@tonic-gate */ 41*7c478bd9Sstevel@tonic-gate 42*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 43*7c478bd9Sstevel@tonic-gate 44*7c478bd9Sstevel@tonic-gate #include <rpc/des_crypt.h> 45*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> 46*7c478bd9Sstevel@tonic-gate #include <fcntl.h> 47*7c478bd9Sstevel@tonic-gate #include <unistd.h> 48*7c478bd9Sstevel@tonic-gate #include <stropts.h> 49*7c478bd9Sstevel@tonic-gate #ifdef sun 50*7c478bd9Sstevel@tonic-gate #include <sys/ioctl.h> 51*7c478bd9Sstevel@tonic-gate #include <sys/des.h> 52*7c478bd9Sstevel@tonic-gate #ifdef _KERNEL 53*7c478bd9Sstevel@tonic-gate #include <sys/conf.h> 54*7c478bd9Sstevel@tonic-gate #define getdesfd() (cdevsw[11].d_open(0, 0) ? -1 : 0) 55*7c478bd9Sstevel@tonic-gate #define ioctl(a, b, c) (cdevsw[11].d_ioctl(0, b, c, 0) ? -1 : 0) 56*7c478bd9Sstevel@tonic-gate #ifndef CRYPT 57*7c478bd9Sstevel@tonic-gate #define __des_crypt(a, b, c) 0 58*7c478bd9Sstevel@tonic-gate #endif 59*7c478bd9Sstevel@tonic-gate #else 60*7c478bd9Sstevel@tonic-gate #define getdesfd() (open("/dev/des", 0, 0)) 61*7c478bd9Sstevel@tonic-gate #endif 62*7c478bd9Sstevel@tonic-gate #else 63*7c478bd9Sstevel@tonic-gate #include <des/des.h> 64*7c478bd9Sstevel@tonic-gate #endif 65*7c478bd9Sstevel@tonic-gate 66*7c478bd9Sstevel@tonic-gate #include "des_soft.h" 67*7c478bd9Sstevel@tonic-gate 68*7c478bd9Sstevel@tonic-gate /* 69*7c478bd9Sstevel@tonic-gate * To see if chip is installed 70*7c478bd9Sstevel@tonic-gate */ 71*7c478bd9Sstevel@tonic-gate #define UNOPENED (-2) 72*7c478bd9Sstevel@tonic-gate 73*7c478bd9Sstevel@tonic-gate /* 74*7c478bd9Sstevel@tonic-gate * Copy 8 bytes 75*7c478bd9Sstevel@tonic-gate */ 76*7c478bd9Sstevel@tonic-gate #define COPY8(src, dst) { \ 77*7c478bd9Sstevel@tonic-gate char *a = (char *) dst; \ 78*7c478bd9Sstevel@tonic-gate char *b = (char *) src; \ 79*7c478bd9Sstevel@tonic-gate *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \ 80*7c478bd9Sstevel@tonic-gate *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \ 81*7c478bd9Sstevel@tonic-gate } 82*7c478bd9Sstevel@tonic-gate 83*7c478bd9Sstevel@tonic-gate /* 84*7c478bd9Sstevel@tonic-gate * Copy multiple of 8 bytes 85*7c478bd9Sstevel@tonic-gate */ 86*7c478bd9Sstevel@tonic-gate #define DESCOPY(src, dst, len) { \ 87*7c478bd9Sstevel@tonic-gate char *a = (char *) dst; \ 88*7c478bd9Sstevel@tonic-gate char *b = (char *) src; \ 89*7c478bd9Sstevel@tonic-gate int i; \ 90*7c478bd9Sstevel@tonic-gate for (i = (int) len; i > 0; i -= 8) { \ 91*7c478bd9Sstevel@tonic-gate *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \ 92*7c478bd9Sstevel@tonic-gate *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \ 93*7c478bd9Sstevel@tonic-gate } \ 94*7c478bd9Sstevel@tonic-gate } 95*7c478bd9Sstevel@tonic-gate static int common_crypt(char *, char *, unsigned, unsigned, struct desparams *); 96*7c478bd9Sstevel@tonic-gate 97*7c478bd9Sstevel@tonic-gate /* 98*7c478bd9Sstevel@tonic-gate * CBC mode encryption 99*7c478bd9Sstevel@tonic-gate */ 100*7c478bd9Sstevel@tonic-gate int 101*7c478bd9Sstevel@tonic-gate cbc_crypt(char *key, char *buf, size_t len, unsigned int mode, char *ivec) 102*7c478bd9Sstevel@tonic-gate { 103*7c478bd9Sstevel@tonic-gate int err = 0; 104*7c478bd9Sstevel@tonic-gate 105*7c478bd9Sstevel@tonic-gate /* EXPORT DELETE START */ 106*7c478bd9Sstevel@tonic-gate struct desparams dp; 107*7c478bd9Sstevel@tonic-gate 108*7c478bd9Sstevel@tonic-gate dp.des_mode = CBC; 109*7c478bd9Sstevel@tonic-gate COPY8(ivec, dp.des_ivec); 110*7c478bd9Sstevel@tonic-gate err = common_crypt(key, buf, len, mode, &dp); 111*7c478bd9Sstevel@tonic-gate COPY8(dp.des_ivec, ivec); 112*7c478bd9Sstevel@tonic-gate /* EXPORT DELETE END */ 113*7c478bd9Sstevel@tonic-gate return (err); 114*7c478bd9Sstevel@tonic-gate } 115*7c478bd9Sstevel@tonic-gate 116*7c478bd9Sstevel@tonic-gate 117*7c478bd9Sstevel@tonic-gate /* 118*7c478bd9Sstevel@tonic-gate * ECB mode encryption 119*7c478bd9Sstevel@tonic-gate */ 120*7c478bd9Sstevel@tonic-gate int 121*7c478bd9Sstevel@tonic-gate ecb_crypt(char *key, char *buf, size_t len, unsigned int mode) 122*7c478bd9Sstevel@tonic-gate { 123*7c478bd9Sstevel@tonic-gate int ret = 0; 124*7c478bd9Sstevel@tonic-gate 125*7c478bd9Sstevel@tonic-gate /* EXPORT DELETE START */ 126*7c478bd9Sstevel@tonic-gate struct desparams dp; 127*7c478bd9Sstevel@tonic-gate 128*7c478bd9Sstevel@tonic-gate dp.des_mode = ECB; 129*7c478bd9Sstevel@tonic-gate ret = common_crypt(key, buf, len, mode, &dp); 130*7c478bd9Sstevel@tonic-gate /* EXPORT DELETE END */ 131*7c478bd9Sstevel@tonic-gate return (ret); 132*7c478bd9Sstevel@tonic-gate } 133*7c478bd9Sstevel@tonic-gate 134*7c478bd9Sstevel@tonic-gate 135*7c478bd9Sstevel@tonic-gate /* EXPORT DELETE START */ 136*7c478bd9Sstevel@tonic-gate /* 137*7c478bd9Sstevel@tonic-gate * Common code to cbc_crypt() & ecb_crypt() 138*7c478bd9Sstevel@tonic-gate */ 139*7c478bd9Sstevel@tonic-gate static int 140*7c478bd9Sstevel@tonic-gate common_crypt(char *key, char *buf, unsigned len, unsigned mode, struct desparams *desp) 141*7c478bd9Sstevel@tonic-gate { 142*7c478bd9Sstevel@tonic-gate int desdev; 143*7c478bd9Sstevel@tonic-gate int res; 144*7c478bd9Sstevel@tonic-gate int g_desfd = UNOPENED; 145*7c478bd9Sstevel@tonic-gate 146*7c478bd9Sstevel@tonic-gate if ((len % 8) != 0 || len > DES_MAXDATA) { 147*7c478bd9Sstevel@tonic-gate return (DESERR_BADPARAM); 148*7c478bd9Sstevel@tonic-gate } 149*7c478bd9Sstevel@tonic-gate desp->des_dir = 150*7c478bd9Sstevel@tonic-gate ((mode & DES_DIRMASK) == DES_ENCRYPT) ? ENCRYPT : DECRYPT; 151*7c478bd9Sstevel@tonic-gate 152*7c478bd9Sstevel@tonic-gate desdev = mode & DES_DEVMASK; 153*7c478bd9Sstevel@tonic-gate COPY8(key, desp->des_key); 154*7c478bd9Sstevel@tonic-gate #ifdef sun 155*7c478bd9Sstevel@tonic-gate if (desdev == DES_HW) { 156*7c478bd9Sstevel@tonic-gate if (g_desfd < 0) { 157*7c478bd9Sstevel@tonic-gate if (g_desfd == -1 || (g_desfd = getdesfd()) < 0) { 158*7c478bd9Sstevel@tonic-gate goto software; /* no hardware device */ 159*7c478bd9Sstevel@tonic-gate } 160*7c478bd9Sstevel@tonic-gate } 161*7c478bd9Sstevel@tonic-gate 162*7c478bd9Sstevel@tonic-gate /* 163*7c478bd9Sstevel@tonic-gate * hardware 164*7c478bd9Sstevel@tonic-gate */ 165*7c478bd9Sstevel@tonic-gate desp->des_len = len; 166*7c478bd9Sstevel@tonic-gate if (len <= DES_QUICKLEN) { 167*7c478bd9Sstevel@tonic-gate DESCOPY(buf, desp->des_data, len); 168*7c478bd9Sstevel@tonic-gate res = ioctl(g_desfd, (int)DESIOCQUICK, (char *) desp); 169*7c478bd9Sstevel@tonic-gate DESCOPY(desp->des_data, buf, len); 170*7c478bd9Sstevel@tonic-gate } else { 171*7c478bd9Sstevel@tonic-gate desp->des_buf = (u_char *) buf; 172*7c478bd9Sstevel@tonic-gate res = ioctl(g_desfd, (int)DESIOCBLOCK, (char *) desp); 173*7c478bd9Sstevel@tonic-gate } 174*7c478bd9Sstevel@tonic-gate return (res == 0 ? DESERR_NONE : DESERR_HWERROR); 175*7c478bd9Sstevel@tonic-gate } 176*7c478bd9Sstevel@tonic-gate software: 177*7c478bd9Sstevel@tonic-gate #endif 178*7c478bd9Sstevel@tonic-gate /* 179*7c478bd9Sstevel@tonic-gate * software 180*7c478bd9Sstevel@tonic-gate */ 181*7c478bd9Sstevel@tonic-gate if (!__des_crypt(buf, len, desp)) { 182*7c478bd9Sstevel@tonic-gate return (DESERR_HWERROR); 183*7c478bd9Sstevel@tonic-gate } 184*7c478bd9Sstevel@tonic-gate return (desdev == DES_SW ? DESERR_NONE : DESERR_NOHWDEVICE); 185*7c478bd9Sstevel@tonic-gate } 186*7c478bd9Sstevel@tonic-gate /* EXPORT DELETE END */ 187