summaryrefslogtreecommitdiff
path: root/usr/src/lib/libcrypt/common
diff options
context:
space:
mode:
authorstevel@tonic-gate <none@none>2005-06-14 00:00:00 -0700
committerstevel@tonic-gate <none@none>2005-06-14 00:00:00 -0700
commit7c478bd95313f5f23a4c958a745db2134aa03244 (patch)
treec871e58545497667cbb4b0a4f2daf204743e1fe7 /usr/src/lib/libcrypt/common
downloadillumos-gate-7c478bd95313f5f23a4c958a745db2134aa03244.tar.gz
OpenSolaris Launch
Diffstat (limited to 'usr/src/lib/libcrypt/common')
-rw-r--r--usr/src/lib/libcrypt/common/cryptio.c206
-rw-r--r--usr/src/lib/libcrypt/common/des.c186
-rw-r--r--usr/src/lib/libcrypt/common/des_crypt.c422
-rw-r--r--usr/src/lib/libcrypt/common/des_decrypt.c119
-rw-r--r--usr/src/lib/libcrypt/common/des_encrypt.c121
-rw-r--r--usr/src/lib/libcrypt/common/des_soft.c446
-rw-r--r--usr/src/lib/libcrypt/common/llib-lcrypt83
7 files changed, 1583 insertions, 0 deletions
diff --git a/usr/src/lib/libcrypt/common/cryptio.c b/usr/src/lib/libcrypt/common/cryptio.c
new file mode 100644
index 0000000000..cc1758d967
--- /dev/null
+++ b/usr/src/lib/libcrypt/common/cryptio.c
@@ -0,0 +1,206 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License, Version 1.0 only
+ * (the "License"). You may not use this file except in compliance
+ * with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+/*
+ * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
+ * Use is subject to license terms.
+ */
+
+/* Copyright (c) 1988 AT&T */
+/* All Rights Reserved */
+
+
+
+#pragma ident "%Z%%M% %I% %E% SMI"
+
+#pragma weak run_setkey = _run_setkey
+#pragma weak run_crypt = _run_crypt
+#pragma weak crypt_close = _crypt_close
+#pragma weak makekey = _makekey
+
+#include "synonyms.h"
+#include "mtlib.h"
+#include <stdio.h>
+#include <signal.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <thread.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <strings.h>
+#include <crypt.h>
+#include "des_soft.h"
+#include "lib_gen.h"
+
+#define READER 0
+#define WRITER 1
+#define KSIZE 8
+
+/* Global Variables */
+static char key[KSIZE+1];
+struct header {
+ long offset;
+ unsigned int count;
+};
+
+static mutex_t lock = DEFAULTMUTEX;
+
+static int cryptopen();
+static int writekey();
+
+void _exit();
+
+int
+run_setkey(int p[2], const char *keyparam)
+{
+ (void) mutex_lock(&lock);
+ if (cryptopen(p) == -1) {
+ (void) mutex_unlock(&lock);
+ return (-1);
+ }
+ (void) strncpy(key, keyparam, KSIZE);
+ if (*key == 0) {
+ (void) crypt_close_nolock(p);
+ (void) mutex_unlock(&lock);
+ return (0);
+ }
+ if (writekey(p, key) == -1) {
+ (void) mutex_unlock(&lock);
+ return (-1);
+ }
+ (void) mutex_unlock(&lock);
+ return (1);
+}
+
+static char cmd[] = "exec /usr/bin/crypt -p 2>/dev/null";
+static int
+cryptopen(int p[2])
+{
+ char c;
+
+ if (__p2open(cmd, p) < 0)
+ return (-1);
+ if (read(p[WRITER], &c, 1) != 1) { /* check that crypt is working on */
+ /* other end */
+ (void) crypt_close(p); /* remove defunct process */
+ return (-1);
+ }
+ return (1);
+}
+
+static int
+writekey(int p[2], char *keyarg)
+{
+ void (*pstat) ();
+ pstat = signal(SIGPIPE, SIG_IGN); /* don't want pipe errors to cause */
+ /* death */
+ if (write(p[READER], keyarg, KSIZE) != KSIZE) {
+ (void) crypt_close(p); /* remove defunct process */
+ (void) signal(SIGPIPE, pstat);
+ return (-1);
+ }
+ (void) signal(SIGPIPE, pstat);
+ return (1);
+}
+
+
+int
+run_crypt(long offset, char *buffer, unsigned int count, int p[2])
+{
+ struct header header;
+ void (*pstat) ();
+
+ (void) mutex_lock(&lock);
+ header.count = count;
+ header.offset = offset;
+ pstat = signal(SIGPIPE, SIG_IGN);
+ if (write(p[READER], (char *)&header, sizeof (header))
+ != sizeof (header)) {
+ (void) crypt_close_nolock(p);
+ (void) signal(SIGPIPE, pstat);
+ (void) mutex_unlock(&lock);
+ return (-1);
+ }
+ if (write(p[READER], buffer, count) < count) {
+ (void) crypt_close_nolock(p);
+ (void) signal(SIGPIPE, pstat);
+ (void) mutex_unlock(&lock);
+ return (-1);
+ }
+ if (read(p[WRITER], buffer, count) < count) {
+ (void) crypt_close_nolock(p);
+ (void) signal(SIGPIPE, pstat);
+ (void) mutex_unlock(&lock);
+ return (-1);
+ }
+ (void) signal(SIGPIPE, pstat);
+ (void) mutex_unlock(&lock);
+ return (0);
+}
+
+int
+makekey(int b[2])
+{
+ int i;
+ long gorp;
+ char tempbuf[KSIZE], *a, *temp;
+
+ (void) mutex_lock(&lock);
+ a = key;
+ temp = tempbuf;
+ for (i = 0; i < KSIZE; i++)
+ temp[i] = *a++;
+ gorp = getuid() + getgid();
+
+ for (i = 0; i < 4; i++)
+ temp[i] ^= (char)((gorp>>(8*i))&0377);
+
+ if (cryptopen(b) == -1) {
+ (void) mutex_unlock(&lock);
+ return (-1);
+ }
+ if (writekey(b, temp) == -1) {
+ (void) mutex_unlock(&lock);
+ return (-1);
+ }
+ (void) mutex_unlock(&lock);
+ return (0);
+}
+
+int
+crypt_close_nolock(int p[2])
+{
+
+ if (p[0] == 0 && p[1] == 0 || p[0] < 0 || p[1] < 0) {
+ return (-1);
+ }
+
+ return (__p2close(p, NULL, SIGKILL));
+}
+
+int
+crypt_close(int p[2])
+{
+ (void) mutex_lock(&lock);
+ (void) crypt_close_nolock(p);
+ (void) mutex_unlock(&lock);
+ return (0);
+}
diff --git a/usr/src/lib/libcrypt/common/des.c b/usr/src/lib/libcrypt/common/des.c
new file mode 100644
index 0000000000..61fcd3c407
--- /dev/null
+++ b/usr/src/lib/libcrypt/common/des.c
@@ -0,0 +1,186 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License, Version 1.0 only
+ * (the "License"). You may not use this file except in compliance
+ * with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+/*
+ * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
+ * Use is subject to license terms.
+ */
+
+/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
+/* All Rights Reserved */
+
+/*
+ * Portions of this source code were derived from Berkeley 4.3 BSD
+ * under license from the Regents of the University of California.
+ */
+
+#pragma ident "%Z%%M% %I% %E% SMI"
+/*LINTLIBRARY*/
+
+/*
+ * DES encryption library routines
+ */
+
+#include <sys/types.h>
+
+#include <rpc/des_crypt.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stropts.h>
+#ifdef sun
+#include <sys/ioctl.h>
+#include <sys/des.h>
+#ifdef _KERNEL
+#include <sys/conf.h>
+#define getdesfd() (cdevsw[11].d_open(0, 0) ? -1 : 0)
+#define ioctl(a, b, c) (cdevsw[11].d_ioctl(0, b, c, 0) ? -1 : 0)
+#ifndef CRYPT
+#define __des_crypt(a, b, c) 0
+#endif
+#else
+#define getdesfd() (open("/dev/des", 0, 0))
+#endif
+#else
+#include <des/des.h>
+#endif
+
+#include "des_soft.h"
+
+/*
+ * To see if chip is installed
+ */
+#define UNOPENED (-2)
+
+/*
+ * Copy 8 bytes
+ */
+#define COPY8(src, dst) { \
+ char *a = (char *) dst; \
+ char *b = (char *) src; \
+ *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
+ *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
+}
+
+/*
+ * Copy multiple of 8 bytes
+ */
+#define DESCOPY(src, dst, len) { \
+ char *a = (char *) dst; \
+ char *b = (char *) src; \
+ int i; \
+ for (i = (int) len; i > 0; i -= 8) { \
+ *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
+ *a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
+ } \
+}
+static int common_crypt(char *, char *, unsigned, unsigned, struct desparams *);
+
+/*
+ * CBC mode encryption
+ */
+int
+cbc_crypt(char *key, char *buf, size_t len, unsigned int mode, char *ivec)
+{
+ int err = 0;
+
+/* EXPORT DELETE START */
+ struct desparams dp;
+
+ dp.des_mode = CBC;
+ COPY8(ivec, dp.des_ivec);
+ err = common_crypt(key, buf, len, mode, &dp);
+ COPY8(dp.des_ivec, ivec);
+/* EXPORT DELETE END */
+ return (err);
+}
+
+
+/*
+ * ECB mode encryption
+ */
+int
+ecb_crypt(char *key, char *buf, size_t len, unsigned int mode)
+{
+ int ret = 0;
+
+/* EXPORT DELETE START */
+ struct desparams dp;
+
+ dp.des_mode = ECB;
+ ret = common_crypt(key, buf, len, mode, &dp);
+/* EXPORT DELETE END */
+ return (ret);
+}
+
+
+/* EXPORT DELETE START */
+/*
+ * Common code to cbc_crypt() & ecb_crypt()
+ */
+static int
+common_crypt(char *key, char *buf, unsigned len, unsigned mode, struct desparams *desp)
+{
+ int desdev;
+ int res;
+ int g_desfd = UNOPENED;
+
+ if ((len % 8) != 0 || len > DES_MAXDATA) {
+ return (DESERR_BADPARAM);
+ }
+ desp->des_dir =
+ ((mode & DES_DIRMASK) == DES_ENCRYPT) ? ENCRYPT : DECRYPT;
+
+ desdev = mode & DES_DEVMASK;
+ COPY8(key, desp->des_key);
+#ifdef sun
+ if (desdev == DES_HW) {
+ if (g_desfd < 0) {
+ if (g_desfd == -1 || (g_desfd = getdesfd()) < 0) {
+ goto software; /* no hardware device */
+ }
+ }
+
+ /*
+ * hardware
+ */
+ desp->des_len = len;
+ if (len <= DES_QUICKLEN) {
+ DESCOPY(buf, desp->des_data, len);
+ res = ioctl(g_desfd, (int)DESIOCQUICK, (char *) desp);
+ DESCOPY(desp->des_data, buf, len);
+ } else {
+ desp->des_buf = (u_char *) buf;
+ res = ioctl(g_desfd, (int)DESIOCBLOCK, (char *) desp);
+ }
+ return (res == 0 ? DESERR_NONE : DESERR_HWERROR);
+ }
+software:
+#endif
+ /*
+ * software
+ */
+ if (!__des_crypt(buf, len, desp)) {
+ return (DESERR_HWERROR);
+ }
+ return (desdev == DES_SW ? DESERR_NONE : DESERR_NOHWDEVICE);
+}
+/* EXPORT DELETE END */
diff --git a/usr/src/lib/libcrypt/common/des_crypt.c b/usr/src/lib/libcrypt/common/des_crypt.c
new file mode 100644
index 0000000000..b7fd085065
--- /dev/null
+++ b/usr/src/lib/libcrypt/common/des_crypt.c
@@ -0,0 +1,422 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License, Version 1.0 only
+ * (the "License"). You may not use this file except in compliance
+ * with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+/* Copyright (c) 1988 AT&T */
+/* All Rights Reserved */
+
+
+/*
+ * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
+ * Use is subject to license terms.
+ */
+
+#pragma ident "%Z%%M% %I% %E% SMI"
+/*LINTLIBRARY*/
+
+#pragma weak des_crypt = _des_crypt
+#pragma weak des_encrypt = _des_encrypt
+#pragma weak des_setkey = _des_setkey
+
+#include "synonyms.h"
+#include "mtlib.h"
+#include <sys/types.h>
+#include <crypt.h>
+#include "des_soft.h"
+
+#include <stdlib.h>
+#include <thread.h>
+#include <synch.h>
+#include <sys/types.h>
+
+/* EXPORT DELETE START */
+/*
+ * This program implements the
+ * Proposed Federal Information Processing
+ * Data Encryption Standard.
+ * See Federal Register, March 17, 1975 (40FR12134)
+ */
+
+/*
+ * Initial permutation,
+ */
+static char IP[] = {
+ 58, 50, 42, 34, 26, 18, 10, 2,
+ 60, 52, 44, 36, 28, 20, 12, 4,
+ 62, 54, 46, 38, 30, 22, 14, 6,
+ 64, 56, 48, 40, 32, 24, 16, 8,
+ 57, 49, 41, 33, 25, 17, 9, 1,
+ 59, 51, 43, 35, 27, 19, 11, 3,
+ 61, 53, 45, 37, 29, 21, 13, 5,
+ 63, 55, 47, 39, 31, 23, 15, 7,
+};
+
+/*
+ * Final permutation, FP = IP^(-1)
+ */
+static char FP[] = {
+ 40, 8, 48, 16, 56, 24, 64, 32,
+ 39, 7, 47, 15, 55, 23, 63, 31,
+ 38, 6, 46, 14, 54, 22, 62, 30,
+ 37, 5, 45, 13, 53, 21, 61, 29,
+ 36, 4, 44, 12, 52, 20, 60, 28,
+ 35, 3, 43, 11, 51, 19, 59, 27,
+ 34, 2, 42, 10, 50, 18, 58, 26,
+ 33, 1, 41, 9, 49, 17, 57, 25,
+};
+
+/*
+ * Permuted-choice 1 from the key bits
+ * to yield C and D.
+ * Note that bits 8, 16... are left out:
+ * They are intended for a parity check.
+ */
+static char PC1_C[] = {
+ 57, 49, 41, 33, 25, 17, 9,
+ 1, 58, 50, 42, 34, 26, 18,
+ 10, 2, 59, 51, 43, 35, 27,
+ 19, 11, 3, 60, 52, 44, 36,
+};
+
+static char PC1_D[] = {
+ 63, 55, 47, 39, 31, 23, 15,
+ 7, 62, 54, 46, 38, 30, 22,
+ 14, 6, 61, 53, 45, 37, 29,
+ 21, 13, 5, 28, 20, 12, 4,
+};
+
+/*
+ * Sequence of shifts used for the key schedule.
+ */
+static char shifts[] = {1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1, };
+
+/*
+ * Permuted-choice 2, to pick out the bits from
+ * the CD array that generate the key schedule.
+ */
+static char PC2_C[] = {
+ 14, 17, 11, 24, 1, 5,
+ 3, 28, 15, 6, 21, 10,
+ 23, 19, 12, 4, 26, 8,
+ 16, 7, 27, 20, 13, 2,
+};
+
+static char PC2_D[] = {
+ 41, 52, 31, 37, 47, 55,
+ 30, 40, 51, 45, 33, 48,
+ 44, 49, 39, 56, 34, 53,
+ 46, 42, 50, 36, 29, 32,
+};
+
+/*
+ * The C and D arrays used to calculate the key schedule.
+ */
+
+static char C[28];
+static char D[28];
+/*
+ * The key schedule.
+ * Generated from the key.
+ */
+static char KS[16][48];
+
+/*
+ * The E bit-selection table.
+ */
+static char E[48];
+static char e2[] = {
+ 32, 1, 2, 3, 4, 5,
+ 4, 5, 6, 7, 8, 9,
+ 8, 9, 10, 11, 12, 13,
+ 12, 13, 14, 15, 16, 17,
+ 16, 17, 18, 19, 20, 21,
+ 20, 21, 22, 23, 24, 25,
+ 24, 25, 26, 27, 28, 29,
+ 28, 29, 30, 31, 32, 1,
+};
+
+/*
+ * Set up the key schedule from the key.
+ */
+
+static mutex_t lock = DEFAULTMUTEX;
+
+/* EXPORT DELETE END */
+
+
+static void
+des_setkey_nolock(const char *key)
+{
+/* EXPORT DELETE START */
+ int i, j, k;
+ char t;
+
+ /*
+ * First, generate C and D by permuting
+ * the key. The low order bit of each
+ * 8-bit char is not used, so C and D are only 28
+ * bits apiece.
+ */
+ for (i = 0; i < 28; i++) {
+ C[i] = key[PC1_C[i]-1];
+ D[i] = key[PC1_D[i]-1];
+ }
+ /*
+ * To generate Ki, rotate C and D according
+ * to schedule and pick up a permutation
+ * using PC2.
+ */
+ for (i = 0; i < 16; i++) {
+ /*
+ * rotate.
+ */
+ for (k = 0; k < shifts[i]; k++) {
+ t = C[0];
+ for (j = 0; j < 28-1; j++)
+ C[j] = C[j+1];
+ C[27] = (char)t;
+ t = D[0];
+ for (j = 0; j < 28-1; j++)
+ D[j] = D[j+1];
+ D[27] = (char)t;
+ }
+ /*
+ * get Ki. Note C and D are concatenated.
+ */
+ for (j = 0; j < 24; j++) {
+ KS[i][j] = C[PC2_C[j]-1];
+ KS[i][j+24] = D[PC2_D[j]-28-1];
+ }
+ }
+
+ for (i = 0; i < 48; i++)
+ E[i] = e2[i];
+/* EXPORT DELETE END */
+}
+
+void
+des_setkey(const char *key)
+{
+/* EXPORT DELETE START */
+ (void) mutex_lock(&lock);
+ des_setkey_nolock(key);
+ (void) mutex_unlock(&lock);
+/* EXPORT DELETE END */
+}
+
+/* EXPORT DELETE START */
+/*
+ * The 8 selection functions.
+ * For some reason, they give a 0-origin
+ * index, unlike everything else.
+ */
+static char S[8][64] = {
+ 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
+ 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
+ 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
+ 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13,
+
+ 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
+ 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
+ 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
+ 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9,
+
+ 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
+ 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
+ 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
+ 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12,
+
+ 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
+ 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
+ 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
+ 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14,
+
+ 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
+ 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
+ 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
+ 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3,
+
+ 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
+ 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
+ 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
+ 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13,
+
+ 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
+ 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
+ 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
+ 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12,
+
+ 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
+ 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
+ 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
+ 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11,
+};
+
+/*
+ * P is a permutation on the selected combination
+ * of the current L and key.
+ */
+static char P[] = {
+ 16, 7, 20, 21,
+ 29, 12, 28, 17,
+ 1, 15, 23, 26,
+ 5, 18, 31, 10,
+ 2, 8, 24, 14,
+ 32, 27, 3, 9,
+ 19, 13, 30, 6,
+ 22, 11, 4, 25,
+};
+
+/*
+ * The current block, divided into 2 halves.
+ */
+static char L[64];
+static char tempL[32];
+static char f[32];
+
+/*
+ * The combination of the key and the input, before selection.
+ */
+static char preS[48];
+
+/*
+ * The payoff: encrypt a block.
+ */
+/* EXPORT DELETE END */
+
+static void
+des_encrypt_nolock(char *block, int edflag)
+{
+/* EXPORT DELETE START */
+
+ if (edflag)
+ (void) des_decrypt1(block, L, IP, &L[32],
+ preS, E, KS, S, f, tempL, P, FP);
+ else
+ (void) des_encrypt1(block, L, IP, &L[32],
+ preS, E, KS, S, f, tempL, P, FP);
+
+/* EXPORT DELETE END */
+}
+
+void
+des_encrypt(char *block, int edflag)
+{
+/* EXPORT DELETE START */
+ (void) mutex_lock(&lock);
+ des_encrypt_nolock(block, edflag);
+ (void) mutex_unlock(&lock);
+/* EXPORT DELETE END */
+}
+
+
+
+#define IOBUF_SIZE 16
+
+static char *
+_get_iobuf(thread_key_t *key, unsigned size)
+{
+ char *iobuf = NULL;
+
+ if (thr_getspecific(*key, (void **)&iobuf) != 0) {
+ if (_thr_keycreate(key, free) != 0) {
+ return (NULL);
+ }
+ }
+
+ if (!iobuf) {
+ if (_thr_setspecific(*key, (void *)(iobuf = malloc(size)))
+ != 0) {
+ if (iobuf)
+ (void) free(iobuf);
+ return (NULL);
+ }
+ }
+ return (iobuf);
+}
+
+char *
+des_crypt(const char *pw, const char *salt)
+{
+/* EXPORT DELETE START */
+ int i, j;
+ char c, temp;
+ static thread_key_t key = 0;
+ char block[66], *iobuf = _get_iobuf(&key, IOBUF_SIZE);
+
+ (void) mutex_lock(&lock);
+ for (i = 0; i < 66; i++)
+ block[i] = 0;
+ for (i = 0; (c = *pw) && (i < 64); pw++) {
+ for (j = 0; j < 7; j++, i++)
+ block[i] = (c>>(6-j)) & 01;
+ i++;
+ }
+
+ des_setkey_nolock(block);
+
+ for (i = 0; i < 66; i++)
+ block[i] = 0;
+
+ for (i = 0; i < 2; i++) {
+ c = *salt++;
+ iobuf[i] = (char)c;
+ if (c > 'Z')
+ c -= 6;
+ if (c > '9')
+ c -= 7;
+ c -= '.';
+ for (j = 0; j < 6; j++) {
+ if ((c>>j) & 01) {
+ temp = E[6*i+j];
+ E[6*i+j] = E[6*i+j+24];
+ E[6*i+j+24] = (char)temp;
+ }
+ }
+ }
+
+ for (i = 0; i < 25; i++)
+ (void) des_encrypt_nolock(block, 0);
+
+ for (i = 0; i < 11; i++) {
+ c = 0;
+ for (j = 0; j < 6; j++) {
+ c <<= 1;
+ c |= block[6*i+j];
+ }
+ c += '.';
+ if (c > '9')
+ c += 7;
+ if (c > 'Z')
+ c += 6;
+ iobuf[i+2] = (char)c;
+ }
+ iobuf[i+2] = 0;
+ if (iobuf[1] == 0)
+ iobuf[1] = iobuf[0];
+ (void) mutex_unlock(&lock);
+ return (iobuf);
+#if 0
+/* EXPORT DELETE END */
+ return (0);
+/* EXPORT DELETE START */
+#endif
+/* EXPORT DELETE END */
+}
diff --git a/usr/src/lib/libcrypt/common/des_decrypt.c b/usr/src/lib/libcrypt/common/des_decrypt.c
new file mode 100644
index 0000000000..ac8fb8291f
--- /dev/null
+++ b/usr/src/lib/libcrypt/common/des_decrypt.c
@@ -0,0 +1,119 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License, Version 1.0 only
+ * (the "License"). You may not use this file except in compliance
+ * with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+/* Copyright (c) 1988 AT&T */
+/* All Rights Reserved */
+
+
+/*
+ * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
+ * Use is subject to license terms.
+ */
+
+#pragma ident "%Z%%M% %I% %E% SMI"
+/*LINTLIBRARY*/
+
+#include <sys/types.h>
+
+void
+_des_decrypt1(char *block, char *L, char *IP, char *R, char *preS, char *E, char KS[][48], char S[][64], char *f, char *tempL, char *P, char *FP)
+{
+/* EXPORT DELETE START */
+ int i, ii;
+ int t, j, k;
+ char t2;
+
+ /*
+ * First, permute the bits in the input
+ */
+ for (j = 0; j < 64; j++)
+ L[j] = block[IP[j]-1];
+ /*
+ * Perform a decryption operation 16 times.
+ */
+ for (ii = 0; ii < 16; ii++) {
+ i = 15-ii;
+ /*
+ * Save the R array,
+ * which will be the new L.
+ */
+ for (j = 0; j < 32; j++)
+ tempL[j] = R[j];
+ /*
+ * Expand R to 48 bits using the E selector;
+ * exclusive-or with the current key bits.
+ */
+ for (j = 0; j < 48; j++)
+ preS[j] = R[E[j]-1] ^ KS[i][j];
+ /*
+ * The pre-select bits are now considered
+ * in 8 groups of 6 bits each.
+ * The 8 selection functions map these
+ * 6-bit quantities into 4-bit quantities
+ * and the results permuted
+ * to make an f(R, K).
+ * The indexing into the selection functions
+ * is peculiar; it could be simplified by
+ * rewriting the tables.
+ */
+ for (j = 0; j < 8; j++) {
+ t = 6*j;
+ k = S[j][(preS[t+0]<<5)+
+ (preS[t+1]<<3)+
+ (preS[t+2]<<2)+
+ (preS[t+3]<<1)+
+ (preS[t+4]<<0)+
+ (preS[t+5]<<4)];
+ t = 4*j;
+ f[t+0] = (k>>3)&01;
+ f[t+1] = (k>>2)&01;
+ f[t+2] = (k>>1)&01;
+ f[t+3] = (k>>0)&01;
+ }
+ /*
+ * The new R is L ^ f(R, K).
+ * The f here has to be permuted first, though.
+ */
+ for (j = 0; j < 32; j++)
+ R[j] = L[j] ^ f[P[j]-1];
+ /*
+ * Finally, the new L (the original R)
+ * is copied back.
+ */
+ for (j = 0; j < 32; j++)
+ L[j] = tempL[j];
+ }
+ /*
+ * The output L and R are reversed.
+ */
+ for (j = 0; j < 32; j++) {
+ t2 = L[j];
+ L[j] = R[j];
+ R[j] = t2;
+ }
+ /*
+ * The final output
+ * gets the inverse permutation of the very original.
+ */
+ for (j = 0; j < 64; j++)
+ block[j] = L[FP[j]-1];
+/* EXPORT DELETE END */
+}
diff --git a/usr/src/lib/libcrypt/common/des_encrypt.c b/usr/src/lib/libcrypt/common/des_encrypt.c
new file mode 100644
index 0000000000..ff5b88cb3b
--- /dev/null
+++ b/usr/src/lib/libcrypt/common/des_encrypt.c
@@ -0,0 +1,121 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License, Version 1.0 only
+ * (the "License"). You may not use this file except in compliance
+ * with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+/* Copyright (c) 1988 AT&T */
+/* All Rights Reserved */
+
+
+/*
+ * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
+ * Use is subject to license terms.
+ */
+
+#pragma ident "%Z%%M% %I% %E% SMI"
+/*LINTLIBRARY*/
+
+#pragma weak des_encrypt1 = _des_encrypt1
+
+#include "synonyms.h"
+#include <sys/types.h>
+
+void
+des_encrypt1(char *block, char *L, char *IP, char *R, char *preS, char *E, char KS[][48], char S[][64], char *f, char *tempL, char *P, char *FP)
+{
+/* EXPORT DELETE START */
+ int i;
+ int t, j, k;
+ char t2;
+
+ /*
+ * First, permute the bits in the input
+ */
+ for (j = 0; j < 64; j++)
+ L[j] = block[IP[j]-1];
+ /*
+ * Perform an encryption operation 16 times.
+ */
+ for (i = 0; i < 16; i++) {
+ /*
+ * Save the R array,
+ * which will be the new L.
+ */
+ for (j = 0; j < 32; j++)
+ tempL[j] = R[j];
+ /*
+ * Expand R to 48 bits using the E selector;
+ * exclusive-or with the current key bits.
+ */
+ for (j = 0; j < 48; j++)
+ preS[j] = R[E[j]-1] ^ KS[i][j];
+ /*
+ * The pre-select bits are now considered
+ * in 8 groups of 6 bits each.
+ * The 8 selection functions map these
+ * 6-bit quantities into 4-bit quantities
+ * and the results permuted
+ * to make an f(R, K).
+ * The indexing into the selection functions
+ * is peculiar; it could be simplified by
+ * rewriting the tables.
+ */
+ for (j = 0; j < 8; j++) {
+ t = 6*j;
+ k = S[j][(preS[t+0]<<5)+
+ (preS[t+1]<<3)+
+ (preS[t+2]<<2)+
+ (preS[t+3]<<1)+
+ (preS[t+4]<<0)+
+ (preS[t+5]<<4)];
+ t = 4*j;
+ f[t+0] = (k>>3)&01;
+ f[t+1] = (k>>2)&01;
+ f[t+2] = (k>>1)&01;
+ f[t+3] = (k>>0)&01;
+ }
+ /*
+ * The new R is L ^ f(R, K).
+ * The f here has to be permuted first, though.
+ */
+ for (j = 0; j < 32; j++)
+ R[j] = L[j] ^ f[P[j]-1];
+ /*
+ * Finally, the new L (the original R)
+ * is copied back.
+ */
+ for (j = 0; j < 32; j++)
+ L[j] = tempL[j];
+ }
+ /*
+ * The output L and R are reversed.
+ */
+ for (j = 0; j < 32; j++) {
+ t2 = L[j];
+ L[j] = R[j];
+ R[j] = t2;
+ }
+ /*
+ * The final output
+ * gets the inverse permutation of the very original.
+ */
+ for (j = 0; j < 64; j++)
+ block[j] = L[FP[j]-1];
+/* EXPORT DELETE END */
+}
diff --git a/usr/src/lib/libcrypt/common/des_soft.c b/usr/src/lib/libcrypt/common/des_soft.c
new file mode 100644
index 0000000000..b560c2dd65
--- /dev/null
+++ b/usr/src/lib/libcrypt/common/des_soft.c
@@ -0,0 +1,446 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License, Version 1.0 only
+ * (the "License"). You may not use this file except in compliance
+ * with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+/*
+ * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
+ * Use is subject to license terms.
+ */
+
+/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
+/* All Rights Reserved */
+
+/*
+ * Portions of this source code were derived from Berkeley 4.3 BSD
+ * under license from the Regents of the University of California.
+ */
+
+#pragma ident "%Z%%M% %I% %E% SMI"
+/*LINTLIBRARY*/
+
+/*
+ * Warning! Things are arranged very carefully in this file to
+ * allow read-only data to be moved to the text segment. The
+ * various DES tables must appear before any function definitions
+ * (this is arranged by including them immediately below) and partab
+ * must also appear before and function definitions
+ * This arrangement allows all data up through the first text to
+ * be moved to text.
+ */
+
+#ifndef _KERNEL
+#define CRYPT /* cannot configure out of user-level code */
+#endif
+
+#include "synonyms.h"
+#ifdef CRYPT
+#include <sys/types.h>
+#include <des/softdes.h>
+#include <des/desdata.h>
+
+#ifdef sun
+#include <sys/ioctl.h>
+#include <sys/des.h>
+#else
+#include <des/des.h>
+#endif
+
+#include "des_soft.h"
+
+/*
+ * Fast (?) software implementation of DES
+ * Has been seen going at 2000 bytes/sec on a Sun-2
+ * Works on a VAX too.
+ * Won't work without 8 bit chars and 32 bit longs
+ */
+
+#define btst(k, b) (k[b >> 3] & (0x80 >> (b & 07)))
+#define BIT28 (1<<28)
+
+
+#endif /* def CRYPT */
+
+static void des_setkey(u_char [8], struct deskeydata *, unsigned);
+static void des_encrypt(u_char *, struct deskeydata *);
+
+#ifndef _KERNEL
+/*
+ * Table giving odd parity in the low bit for ASCII characters
+ */
+static char partab[128] = {
+ 0x01, 0x01, 0x02, 0x02, 0x04, 0x04, 0x07, 0x07,
+ 0x08, 0x08, 0x0b, 0x0b, 0x0d, 0x0d, 0x0e, 0x0e,
+ 0x10, 0x10, 0x13, 0x13, 0x15, 0x15, 0x16, 0x16,
+ 0x19, 0x19, 0x1a, 0x1a, 0x1c, 0x1c, 0x1f, 0x1f,
+ 0x20, 0x20, 0x23, 0x23, 0x25, 0x25, 0x26, 0x26,
+ 0x29, 0x29, 0x2a, 0x2a, 0x2c, 0x2c, 0x2f, 0x2f,
+ 0x31, 0x31, 0x32, 0x32, 0x34, 0x34, 0x37, 0x37,
+ 0x38, 0x38, 0x3b, 0x3b, 0x3d, 0x3d, 0x3e, 0x3e,
+ 0x40, 0x40, 0x43, 0x43, 0x45, 0x45, 0x46, 0x46,
+ 0x49, 0x49, 0x4a, 0x4a, 0x4c, 0x4c, 0x4f, 0x4f,
+ 0x51, 0x51, 0x52, 0x52, 0x54, 0x54, 0x57, 0x57,
+ 0x58, 0x58, 0x5b, 0x5b, 0x5d, 0x5d, 0x5e, 0x5e,
+ 0x61, 0x61, 0x62, 0x62, 0x64, 0x64, 0x67, 0x67,
+ 0x68, 0x68, 0x6b, 0x6b, 0x6d, 0x6d, 0x6e, 0x6e,
+ 0x70, 0x70, 0x73, 0x73, 0x75, 0x75, 0x76, 0x76,
+ 0x79, 0x79, 0x7a, 0x7a, 0x7c, 0x7c, 0x7f, 0x7f,
+};
+
+
+
+/*
+ * Add odd parity to low bit of 8 byte key
+ */
+void
+des_setparity(char *p)
+{
+ int i;
+
+ for (i = 0; i < 8; i++) {
+ *p = partab[*p & 0x7f];
+ p++;
+ }
+}
+#endif /* def _KERNEL */
+
+#ifdef CRYPT
+/*
+ * Software encrypt or decrypt a block of data (multiple of 8 bytes)
+ * Do the CBC ourselves if needed.
+ */
+int
+__des_crypt(char *buf, unsigned int len, struct desparams *desp)
+{
+/* EXPORT DELETE START */
+ short i;
+ unsigned mode;
+ unsigned dir;
+ char nextiv[8];
+ struct deskeydata softkey;
+
+ mode = (unsigned) desp->des_mode;
+ dir = (unsigned) desp->des_dir;
+ des_setkey(desp->des_key, &softkey, dir);
+ while (len != 0) {
+ switch (mode) {
+ case CBC:
+ switch (dir) {
+ case ENCRYPT:
+ for (i = 0; i < 8; i++)
+ buf[i] ^= desp->des_ivec[i];
+ des_encrypt((u_char *)buf, &softkey);
+ for (i = 0; i < 8; i++)
+ desp->des_ivec[i] = buf[i];
+ break;
+ case DECRYPT:
+ for (i = 0; i < 8; i++)
+ nextiv[i] = buf[i];
+ des_encrypt((u_char *)buf, &softkey);
+ for (i = 0; i < 8; i++) {
+ buf[i] ^= desp->des_ivec[i];
+ desp->des_ivec[i] = nextiv[i];
+ }
+ break;
+ }
+ break;
+ case ECB:
+ des_encrypt((u_char *)buf, &softkey);
+ break;
+ }
+ buf += 8;
+ len -= 8;
+ }
+/* EXPORT DELETE END */
+ return (1);
+}
+
+
+/*
+ * Set the key and direction for an encryption operation
+ * We build the 16 key entries here
+ */
+static void
+des_setkey(u_char userkey[8], struct deskeydata *kd, unsigned int dir)
+{
+/* EXPORT DELETE START */
+ long C, D;
+ short i;
+
+ /*
+ * First, generate C and D by permuting
+ * the key. The low order bit of each
+ * 8-bit char is not used, so C and D are only 28
+ * bits apiece.
+ */
+ {
+ short bit;
+ const short *pcc = PC1_C, *pcd = PC1_D;
+
+ C = D = 0;
+ for (i = 0; i < 28; i++) {
+ C <<= 1;
+ D <<= 1;
+ bit = *pcc++;
+ if (btst(userkey, bit))
+ C |= 1;
+ bit = *pcd++;
+ if (btst(userkey, bit))
+ D |= 1;
+ }
+ }
+ /*
+ * To generate Ki, rotate C and D according
+ * to schedule and pick up a permutation
+ * using PC2.
+ */
+ for (i = 0; i < 16; i++) {
+ chunk_t *c;
+ short j, k, bit;
+ long bbit;
+
+ /*
+ * Do the "left shift" (rotate)
+ * We know we always rotate by either 1 or 2 bits
+ * the shifts table tells us if its 2
+ */
+ C <<= 1;
+ if (C & BIT28)
+ C |= 1;
+ D <<= 1;
+ if (D & BIT28)
+ D |= 1;
+ if (shifts[i]) {
+ C <<= 1;
+ if (C & BIT28)
+ C |= 1;
+ D <<= 1;
+ if (D & BIT28)
+ D |= 1;
+ }
+ /*
+ * get Ki. Note C and D are concatenated.
+ */
+ bit = 0;
+ switch (dir) {
+ case ENCRYPT:
+ c = &kd->keyval[i]; break;
+ case DECRYPT:
+ c = &kd->keyval[15 - i]; break;
+ }
+ c->long0 = 0;
+ c->long1 = 0;
+ bbit = (1 << 5) << 24;
+ for (j = 0; j < 4; j++) {
+ for (k = 0; k < 6; k++) {
+ if (C & (BIT28 >> PC2_C[bit]))
+ c->long0 |= bbit >> k;
+ if (D & (BIT28 >> PC2_D[bit]))
+ c->long1 |= bbit >> k;
+ bit++;
+ }
+ bbit >>= 8;
+ }
+
+ }
+/* EXPORT DELETE END */
+ return;
+}
+
+
+
+/*
+ * Do an encryption operation
+ * Much pain is taken (with preprocessor) to avoid loops so the compiler
+ * can do address arithmetic instead of doing it at runtime.
+ * Note that the byte-to-chunk conversion is necessary to guarantee
+ * processor byte-order independence.
+ */
+static void
+des_encrypt(u_char *data, struct deskeydata *kd)
+{
+/* EXPORT DELETE START */
+ chunk_t work1, work2;
+
+ /*
+ * Initial permutation
+ * and byte to chunk conversion
+ */
+ {
+ const uint32_t *lp;
+ uint32_t l0, l1, w;
+ short i, pbit;
+
+ work1.byte0 = data[0];
+ work1.byte1 = data[1];
+ work1.byte2 = data[2];
+ work1.byte3 = data[3];
+ work1.byte4 = data[4];
+ work1.byte5 = data[5];
+ work1.byte6 = data[6];
+ work1.byte7 = data[7];
+ l0 = l1 = 0;
+ w = work1.long0;
+ for (lp = &longtab[0], i = 0; i < 32; i++) {
+ if (w & *lp++) {
+ pbit = IPtab[i];
+ if (pbit < 32)
+ l0 |= longtab[pbit];
+ else
+ l1 |= longtab[pbit-32];
+ }
+ }
+ w = work1.long1;
+ for (lp = &longtab[0], i = 32; i < 64; i++) {
+ if (w & *lp++) {
+ pbit = IPtab[i];
+ if (pbit < 32)
+ l0 |= longtab[pbit];
+ else
+ l1 |= longtab[pbit-32];
+ }
+ }
+ work2.long0 = l0;
+ work2.long1 = l1;
+ }
+
+/*
+ * Expand 8 bits of 32 bit R to 48 bit R
+ */
+#define do_R_to_ER(op, b) { \
+ const struct R_to_ER *p = &R_to_ER_tab[b][R.byte##b]; \
+ e0 op p->l0; \
+ e1 op p->l1; \
+}
+
+/*
+ * Inner part of the algorithm:
+ * Expand R from 32 to 48 bits; xor key value;
+ * apply S boxes; permute 32 bits of output
+ */
+#define do_F(iter, inR, outR) { \
+ chunk_t R, ER; \
+ uint32_t e0, e1; \
+ R.long0 = inR; \
+ do_R_to_ER(=, 0); \
+ do_R_to_ER(|=, 1); \
+ do_R_to_ER(|=, 2); \
+ do_R_to_ER(|=, 3); \
+ ER.long0 = e0 ^ kd->keyval[iter].long0; \
+ ER.long1 = e1 ^ kd->keyval[iter].long1; \
+ R.long0 = \
+ S_tab[0][ER.byte0] + \
+ S_tab[1][ER.byte1] + \
+ S_tab[2][ER.byte2] + \
+ S_tab[3][ER.byte3] + \
+ S_tab[4][ER.byte4] + \
+ S_tab[5][ER.byte5] + \
+ S_tab[6][ER.byte6] + \
+ S_tab[7][ER.byte7]; \
+ outR = \
+ P_tab[0][R.byte0] + \
+ P_tab[1][R.byte1] + \
+ P_tab[2][R.byte2] + \
+ P_tab[3][R.byte3]; \
+}
+
+/*
+ * Do a cipher step
+ * Apply inner part; do xor and exchange of 32 bit parts
+ */
+#define cipher(iter, inR, inL, outR, outL) { \
+ do_F(iter, inR, outR); \
+ outR ^= inL; \
+ outL = inR; \
+}
+
+ /*
+ * Apply the 16 ciphering steps
+ */
+ {
+ uint32_t r0, l0, r1, l1;
+
+ l0 = work2.long0;
+ r0 = work2.long1;
+ cipher(0, r0, l0, r1, l1);
+ cipher(1, r1, l1, r0, l0);
+ cipher(2, r0, l0, r1, l1);
+ cipher(3, r1, l1, r0, l0);
+ cipher(4, r0, l0, r1, l1);
+ cipher(5, r1, l1, r0, l0);
+ cipher(6, r0, l0, r1, l1);
+ cipher(7, r1, l1, r0, l0);
+ cipher(8, r0, l0, r1, l1);
+ cipher(9, r1, l1, r0, l0);
+ cipher(10, r0, l0, r1, l1);
+ cipher(11, r1, l1, r0, l0);
+ cipher(12, r0, l0, r1, l1);
+ cipher(13, r1, l1, r0, l0);
+ cipher(14, r0, l0, r1, l1);
+ cipher(15, r1, l1, r0, l0);
+ work1.long0 = r0;
+ work1.long1 = l0;
+ }
+
+ /*
+ * Final permutation
+ * and chunk to byte conversion
+ */
+ {
+ const uint32_t *lp;
+ uint32_t l0, l1, w;
+ short i, pbit;
+
+ l0 = l1 = 0;
+ w = work1.long0;
+ for (lp = &longtab[0], i = 0; i < 32; i++) {
+ if (w & *lp++) {
+ pbit = FPtab[i];
+ if (pbit < 32)
+ l0 |= longtab[pbit];
+ else
+ l1 |= longtab[pbit-32];
+ }
+ }
+ w = work1.long1;
+ for (lp = &longtab[0], i = 32; i < 64; i++) {
+ if (w & *lp++) {
+ pbit = FPtab[i];
+ if (pbit < 32)
+ l0 |= longtab[pbit];
+ else
+ l1 |= longtab[pbit-32];
+ }
+ }
+ work2.long0 = l0;
+ work2.long1 = l1;
+ }
+ data[0] = work2.byte0;
+ data[1] = work2.byte1;
+ data[2] = work2.byte2;
+ data[3] = work2.byte3;
+ data[4] = work2.byte4;
+ data[5] = work2.byte5;
+ data[6] = work2.byte6;
+ data[7] = work2.byte7;
+
+/* EXPORT DELETE END */
+ return;
+}
+#endif /* def CRYPT */
diff --git a/usr/src/lib/libcrypt/common/llib-lcrypt b/usr/src/lib/libcrypt/common/llib-lcrypt
new file mode 100644
index 0000000000..5063b0a32e
--- /dev/null
+++ b/usr/src/lib/libcrypt/common/llib-lcrypt
@@ -0,0 +1,83 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License, Version 1.0 only
+ * (the "License"). You may not use this file except in compliance
+ * with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+/* LINTLIBRARY */
+/* PROTOLIB1 */
+
+/*
+ * Copyright (c) 1997, 2000 by Sun Microsystems, Inc.
+ * All rights reserved.
+ */
+#pragma ident "%Z%%M% %I% %E% SMI"
+
+#include <stdio.h>
+#include <sys/types.h>
+#include <des/softdes.h>
+#include <des/desdata.h>
+#include <sys/des.h>
+
+/*
+ * usr/src/lib/libcrypt
+ */
+
+/* crypt.c */
+void setkey(const char *key);
+void encrypt(char *block, int edflag);
+char *crypt(const char *pw, const char *salt);
+
+/* cryptio.c */
+int _run_setkey(int p[2], const char *keyparam);
+int run_setkey(int p[2], const char *keyparam);
+int _run_crypt(long offset, char *buffer, unsigned int count, int p[2]);
+int run_crypt(long offset, char *buffer, unsigned int count, int p[2]);
+int _makekey(int b[2]);
+int makekey(int b[2]);
+int crypt_close_nolock(int p[2]);
+int _crypt_close(int p[2]);
+int crypt_close(int p[2]);
+
+/* des.c */
+int cbc_crypt(char *key, char *buf, size_t len, unsigned int mode, char *ivec);
+int ecb_crypt(char *key, char *buf, size_t len, unsigned int mode);
+
+/* des_crypt.c */
+void _des_setkey(const char *key);
+void des_setkey(const char *key);
+void _des_encrypt(char *block, int edflag);
+void des_encrypt(char *block, int edflag);
+char *des_crypt(const char *pw, const char *salt);
+
+/* des_decrypt.c */
+void _des_decrypt1(char *block, char *L, char *IP, char *R, char *preS,
+ char *E, char KS[][48], char S[][64], char *f,
+ char *tempL, char *P, char *FP);
+
+/* des_encrypt.c */
+void des_encrypt1(char *block, char *L, char *IP, char *R, char *preS,
+ char *E, char KS[][48], char S[][64], char *f,
+ char *tempL, char *P, char *FP);
+void _des_encrypt1(char *block, char *L, char *IP, char *R, char *preS,
+ char *E, char KS[][48], char S[][64], char *f,
+ char *tempL, char *P, char *FP);
+
+/* des_soft.c */
+void des_setparity(char *p);
+int __des_crypt(char *buf, unsigned len, struct desparams *desp);