2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
2N/A/* All Rights Reserved */
2N/A
2N/A/*
2N/A * Portions of this source code were derived from Berkeley 4.3 BSD
2N/A * under license from the Regents of the University of California.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/*
2N/A * DES encryption library routines
2N/A */
2N/A
2N/A#include <sys/types.h>
2N/A#include <rpc/des_crypt.h>
2N/A#include <sys/stat.h>
2N/A#include <fcntl.h>
2N/A#include <unistd.h>
2N/A#include <stropts.h>
2N/A#ifdef sun
2N/A#include <sys/ioctl.h>
2N/A#include <sys/des.h>
2N/A#ifdef _KERNEL
2N/A#include <sys/conf.h>
2N/A#define getdesfd() (cdevsw[11].d_open(0, 0) ? -1 : 0)
2N/A#define ioctl(a, b, c) (cdevsw[11].d_ioctl(0, b, c, 0) ? -1 : 0)
2N/A#ifndef CRYPT
2N/A#define __des_crypt(a, b, c) 0
2N/A#endif
2N/A#else
2N/A#define getdesfd() (open("/dev/des", 0, 0))
2N/A#endif
2N/A#else
2N/A#include <des/des.h>
2N/A#endif
2N/A
2N/A#include "des_soft.h"
2N/A
2N/A/*
2N/A * To see if chip is installed
2N/A */
2N/A#define UNOPENED (-2)
2N/A
2N/A/*
2N/A * Copy 8 bytes
2N/A */
2N/A#define COPY8(src, dst) { \
2N/A char *a = (char *)dst; \
2N/A char *b = (char *)src; \
2N/A *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
2N/A *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
2N/A}
2N/A
2N/A/*
2N/A * Copy multiple of 8 bytes
2N/A */
2N/A#define DESCOPY(src, dst, len) { \
2N/A char *a = (char *)dst; \
2N/A char *b = (char *)src; \
2N/A int i; \
2N/A for (i = (int)len; i > 0; i -= 8) { \
2N/A *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
2N/A *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
2N/A } \
2N/A}
2N/Astatic int common_crypt(char *, char *, unsigned, unsigned, struct desparams *);
2N/A
2N/A/*
2N/A * CBC mode encryption
2N/A */
2N/Aint
2N/Acbc_crypt(char *key, char *buf, size_t len, unsigned int mode, char *ivec)
2N/A{
2N/A int err = 0;
2N/A
2N/A/* EXPORT DELETE START */
2N/A struct desparams dp;
2N/A
2N/A dp.des_mode = CBC;
2N/A COPY8(ivec, dp.des_ivec);
2N/A err = common_crypt(key, buf, len, mode, &dp);
2N/A COPY8(dp.des_ivec, ivec);
2N/A/* EXPORT DELETE END */
2N/A return (err);
2N/A}
2N/A
2N/A
2N/A/*
2N/A * ECB mode encryption
2N/A */
2N/Aint
2N/Aecb_crypt(char *key, char *buf, size_t len, unsigned int mode)
2N/A{
2N/A int ret = 0;
2N/A
2N/A/* EXPORT DELETE START */
2N/A struct desparams dp;
2N/A
2N/A dp.des_mode = ECB;
2N/A ret = common_crypt(key, buf, len, mode, &dp);
2N/A/* EXPORT DELETE END */
2N/A return (ret);
2N/A}
2N/A
2N/A
2N/A/* EXPORT DELETE START */
2N/A/*
2N/A * Common code to cbc_crypt() & ecb_crypt()
2N/A */
2N/Astatic int
2N/Acommon_crypt(char *key, char *buf, unsigned len,
2N/A unsigned mode, struct desparams *desp)
2N/A{
2N/A int desdev;
2N/A int res;
2N/A int g_desfd = UNOPENED;
2N/A
2N/A if ((len % 8) != 0 || len > DES_MAXDATA) {
2N/A return (DESERR_BADPARAM);
2N/A }
2N/A desp->des_dir =
2N/A ((mode & DES_DIRMASK) == DES_ENCRYPT) ? ENCRYPT : DECRYPT;
2N/A
2N/A desdev = mode & DES_DEVMASK;
2N/A COPY8(key, desp->des_key);
2N/A#ifdef sun
2N/A if (desdev == DES_HW) {
2N/A if (g_desfd < 0) {
2N/A if (g_desfd == -1 || (g_desfd = getdesfd()) < 0) {
2N/A goto software; /* no hardware device */
2N/A }
2N/A }
2N/A
2N/A /*
2N/A * hardware
2N/A */
2N/A desp->des_len = len;
2N/A if (len <= DES_QUICKLEN) {
2N/A DESCOPY(buf, desp->des_data, len);
2N/A res = ioctl(g_desfd, (int)DESIOCQUICK, (char *)desp);
2N/A DESCOPY(desp->des_data, buf, len);
2N/A } else {
2N/A desp->des_buf = (uchar_t *)buf;
2N/A res = ioctl(g_desfd, (int)DESIOCBLOCK, (char *)desp);
2N/A }
2N/A return (res == 0 ? DESERR_NONE : DESERR_HWERROR);
2N/A }
2N/Asoftware:
2N/A#endif
2N/A /*
2N/A * software
2N/A */
2N/A if (!__des_crypt(buf, len, desp)) {
2N/A return (DESERR_HWERROR);
2N/A }
2N/A return (desdev == DES_SW ? DESERR_NONE : DESERR_NOHWDEVICE);
2N/A}
2N/A/* EXPORT DELETE END */