xref: /onnv-gate/usr/src/uts/common/des/des_crypt.c (revision 5072:a2b6f90cc61e)
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
52439Sizick  * Common Development and Distribution License (the "License").
62439Sizick  * 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  *
21*5072Smcpowers  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
220Sstevel@tonic-gate  * Use is subject to license terms.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
260Sstevel@tonic-gate /*	  All Rights Reserved  	*/
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
300Sstevel@tonic-gate  * under license from the Regents of the University of California.
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
340Sstevel@tonic-gate 
350Sstevel@tonic-gate /*
360Sstevel@tonic-gate  * des_crypt.c, DES encryption library routines
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #include <sys/errno.h>
400Sstevel@tonic-gate #include <sys/modctl.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #include <sys/systm.h>
430Sstevel@tonic-gate #include <sys/cmn_err.h>
440Sstevel@tonic-gate #include <sys/ddi.h>
450Sstevel@tonic-gate #include <sys/crypto/common.h>
460Sstevel@tonic-gate #include <sys/crypto/spi.h>
470Sstevel@tonic-gate #include <sys/sysmacros.h>
480Sstevel@tonic-gate #include <sys/strsun.h>
490Sstevel@tonic-gate #include <sys/note.h>
500Sstevel@tonic-gate #include <des_impl.h>
510Sstevel@tonic-gate #include <des_cbc_crypt.h>
520Sstevel@tonic-gate 
530Sstevel@tonic-gate /* EXPORT DELETE START */
540Sstevel@tonic-gate #include <sys/types.h>
550Sstevel@tonic-gate #include <rpc/des_crypt.h>
560Sstevel@tonic-gate #include <des/des.h>
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #ifdef sun_hardware
590Sstevel@tonic-gate #include <sys/ioctl.h>
600Sstevel@tonic-gate #ifdef _KERNEL
610Sstevel@tonic-gate #include <sys/conf.h>
620Sstevel@tonic-gate static int g_desfd = -1;
630Sstevel@tonic-gate #define	getdesfd()	(cdevsw[11].d_open(0, 0) ? -1 : 0)
640Sstevel@tonic-gate #define	ioctl(a, b, c)	(cdevsw[11].d_ioctl(0, b, c, 0) ? -1 : 0)
650Sstevel@tonic-gate #else
660Sstevel@tonic-gate #define	getdesfd()	(open("/dev/des", 0, 0))
670Sstevel@tonic-gate #endif	/* _KERNEL */
680Sstevel@tonic-gate #endif	/* sun */
690Sstevel@tonic-gate 
700Sstevel@tonic-gate static int common_crypt(char *key, char *buf, size_t len,
710Sstevel@tonic-gate     unsigned int mode, struct desparams *desp);
720Sstevel@tonic-gate 
730Sstevel@tonic-gate extern int _des_crypt(char *buf, size_t len, struct desparams *desp);
740Sstevel@tonic-gate 
750Sstevel@tonic-gate /* EXPORT DELETE END */
760Sstevel@tonic-gate 
770Sstevel@tonic-gate extern struct mod_ops mod_cryptoops;
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /*
800Sstevel@tonic-gate  * Module linkage information for the kernel.
810Sstevel@tonic-gate  */
820Sstevel@tonic-gate static struct modlmisc modlmisc = {
830Sstevel@tonic-gate 	&mod_miscops,
840Sstevel@tonic-gate 	"des encryption",
850Sstevel@tonic-gate };
860Sstevel@tonic-gate 
870Sstevel@tonic-gate static struct modlcrypto modlcrypto = {
880Sstevel@tonic-gate 	&mod_cryptoops,
89*5072Smcpowers 	"DES Kernel SW Provider"
900Sstevel@tonic-gate };
910Sstevel@tonic-gate 
920Sstevel@tonic-gate static struct modlinkage modlinkage = {
930Sstevel@tonic-gate 	MODREV_1,
940Sstevel@tonic-gate 	&modlmisc,
950Sstevel@tonic-gate 	&modlcrypto,
960Sstevel@tonic-gate 	NULL
970Sstevel@tonic-gate };
980Sstevel@tonic-gate 
990Sstevel@tonic-gate /*
1000Sstevel@tonic-gate  * CSPI information (entry points, provider info, etc.)
1010Sstevel@tonic-gate  */
1020Sstevel@tonic-gate typedef enum des_mech_type {
1030Sstevel@tonic-gate 	DES_ECB_MECH_INFO_TYPE,		/* SUN_CKM_DES_ECB */
1040Sstevel@tonic-gate 	DES_CBC_MECH_INFO_TYPE,		/* SUN_CKM_DES_CBC */
1050Sstevel@tonic-gate 	DES_CFB_MECH_INFO_TYPE,		/* SUN_CKM_DES_CFB */
1060Sstevel@tonic-gate 	DES3_ECB_MECH_INFO_TYPE,	/* SUN_CKM_DES3_ECB */
1070Sstevel@tonic-gate 	DES3_CBC_MECH_INFO_TYPE,	/* SUN_CKM_DES3_CBC */
1080Sstevel@tonic-gate 	DES3_CFB_MECH_INFO_TYPE		/* SUN_CKM_DES3_CFB */
1090Sstevel@tonic-gate } des_mech_type_t;
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate /* EXPORT DELETE START */
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate #define	DES_MIN_KEY_LEN		DES_MINBYTES
1140Sstevel@tonic-gate #define	DES_MAX_KEY_LEN		DES_MAXBYTES
1150Sstevel@tonic-gate #define	DES3_MIN_KEY_LEN	DES3_MINBYTES
1160Sstevel@tonic-gate #define	DES3_MAX_KEY_LEN	DES3_MAXBYTES
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate /* EXPORT DELETE END */
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate #ifndef DES_MIN_KEY_LEN
1210Sstevel@tonic-gate #define	DES_MIN_KEY_LEN		0
1220Sstevel@tonic-gate #endif
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate #ifndef DES_MAX_KEY_LEN
1250Sstevel@tonic-gate #define	DES_MAX_KEY_LEN		0
1260Sstevel@tonic-gate #endif
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate #ifndef DES3_MIN_KEY_LEN
1290Sstevel@tonic-gate #define	DES3_MIN_KEY_LEN	0
1300Sstevel@tonic-gate #endif
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate #ifndef DES3_MAX_KEY_LEN
1330Sstevel@tonic-gate #define	DES3_MAX_KEY_LEN	0
1340Sstevel@tonic-gate #endif
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate /*
1370Sstevel@tonic-gate  * Mechanism info structure passed to KCF during registration.
1380Sstevel@tonic-gate  */
1390Sstevel@tonic-gate static crypto_mech_info_t des_mech_info_tab[] = {
1400Sstevel@tonic-gate 	/* DES_ECB */
1410Sstevel@tonic-gate 	{SUN_CKM_DES_ECB, DES_ECB_MECH_INFO_TYPE,
1420Sstevel@tonic-gate 	    CRYPTO_FG_ENCRYPT | CRYPTO_FG_ENCRYPT_ATOMIC |
1430Sstevel@tonic-gate 	    CRYPTO_FG_DECRYPT | CRYPTO_FG_DECRYPT_ATOMIC,
1440Sstevel@tonic-gate 	    DES_MIN_KEY_LEN, DES_MAX_KEY_LEN, CRYPTO_KEYSIZE_UNIT_IN_BYTES},
1450Sstevel@tonic-gate 	/* DES_CBC */
1460Sstevel@tonic-gate 	{SUN_CKM_DES_CBC, DES_CBC_MECH_INFO_TYPE,
1470Sstevel@tonic-gate 	    CRYPTO_FG_ENCRYPT | CRYPTO_FG_ENCRYPT_ATOMIC |
1480Sstevel@tonic-gate 	    CRYPTO_FG_DECRYPT | CRYPTO_FG_DECRYPT_ATOMIC,
1490Sstevel@tonic-gate 	    DES_MIN_KEY_LEN, DES_MAX_KEY_LEN, CRYPTO_KEYSIZE_UNIT_IN_BYTES},
1500Sstevel@tonic-gate 	/* DES3_ECB */
1510Sstevel@tonic-gate 	{SUN_CKM_DES3_ECB, DES3_ECB_MECH_INFO_TYPE,
1520Sstevel@tonic-gate 	    CRYPTO_FG_ENCRYPT | CRYPTO_FG_ENCRYPT_ATOMIC |
1530Sstevel@tonic-gate 	    CRYPTO_FG_DECRYPT | CRYPTO_FG_DECRYPT_ATOMIC,
1540Sstevel@tonic-gate 	    DES3_MIN_KEY_LEN, DES3_MAX_KEY_LEN, CRYPTO_KEYSIZE_UNIT_IN_BYTES},
1550Sstevel@tonic-gate 	/* DES3_CBC */
1560Sstevel@tonic-gate 	{SUN_CKM_DES3_CBC, DES3_CBC_MECH_INFO_TYPE,
1570Sstevel@tonic-gate 	    CRYPTO_FG_ENCRYPT | CRYPTO_FG_ENCRYPT_ATOMIC |
1580Sstevel@tonic-gate 	    CRYPTO_FG_DECRYPT | CRYPTO_FG_DECRYPT_ATOMIC,
1590Sstevel@tonic-gate 	    DES3_MIN_KEY_LEN, DES3_MAX_KEY_LEN, CRYPTO_KEYSIZE_UNIT_IN_BYTES}
1600Sstevel@tonic-gate };
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate /* operations are in-place if the output buffer is NULL */
1630Sstevel@tonic-gate #define	DES_ARG_INPLACE(input, output)				\
1640Sstevel@tonic-gate 	if ((output) == NULL)					\
1650Sstevel@tonic-gate 		(output) = (input);
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate static void des_provider_status(crypto_provider_handle_t, uint_t *);
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate static crypto_control_ops_t des_control_ops = {
1700Sstevel@tonic-gate 	des_provider_status
1710Sstevel@tonic-gate };
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate static int
1740Sstevel@tonic-gate des_common_init(crypto_ctx_t *, crypto_mechanism_t *, crypto_key_t *,
1750Sstevel@tonic-gate     crypto_spi_ctx_template_t, crypto_req_handle_t);
1760Sstevel@tonic-gate static int des_common_init_ctx(des_ctx_t *, crypto_spi_ctx_template_t *,
1770Sstevel@tonic-gate     crypto_mechanism_t *, crypto_key_t *, des_strength_t, int);
1780Sstevel@tonic-gate static int des_encrypt_final(crypto_ctx_t *, crypto_data_t *,
1790Sstevel@tonic-gate     crypto_req_handle_t);
1800Sstevel@tonic-gate static int des_decrypt_final(crypto_ctx_t *, crypto_data_t *,
1810Sstevel@tonic-gate     crypto_req_handle_t);
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate static int des_encrypt(crypto_ctx_t *, crypto_data_t *, crypto_data_t *,
1840Sstevel@tonic-gate     crypto_req_handle_t);
1850Sstevel@tonic-gate static int des_encrypt_update(crypto_ctx_t *, crypto_data_t *,
1860Sstevel@tonic-gate     crypto_data_t *, crypto_req_handle_t);
1870Sstevel@tonic-gate static int des_encrypt_atomic(crypto_provider_handle_t, crypto_session_id_t,
1880Sstevel@tonic-gate     crypto_mechanism_t *, crypto_key_t *, crypto_data_t *,
1890Sstevel@tonic-gate     crypto_data_t *, crypto_spi_ctx_template_t, crypto_req_handle_t);
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate static int des_decrypt(crypto_ctx_t *, crypto_data_t *, crypto_data_t *,
1920Sstevel@tonic-gate     crypto_req_handle_t);
1930Sstevel@tonic-gate static int des_decrypt_update(crypto_ctx_t *, crypto_data_t *,
1940Sstevel@tonic-gate     crypto_data_t *, crypto_req_handle_t);
1950Sstevel@tonic-gate static int des_decrypt_atomic(crypto_provider_handle_t, crypto_session_id_t,
1960Sstevel@tonic-gate     crypto_mechanism_t *, crypto_key_t *, crypto_data_t *,
1970Sstevel@tonic-gate     crypto_data_t *, crypto_spi_ctx_template_t, crypto_req_handle_t);
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate static crypto_cipher_ops_t des_cipher_ops = {
2000Sstevel@tonic-gate 	des_common_init,
2010Sstevel@tonic-gate 	des_encrypt,
2020Sstevel@tonic-gate 	des_encrypt_update,
2030Sstevel@tonic-gate 	des_encrypt_final,
2040Sstevel@tonic-gate 	des_encrypt_atomic,
2050Sstevel@tonic-gate 	des_common_init,
2060Sstevel@tonic-gate 	des_decrypt,
2070Sstevel@tonic-gate 	des_decrypt_update,
2080Sstevel@tonic-gate 	des_decrypt_final,
2090Sstevel@tonic-gate 	des_decrypt_atomic
2100Sstevel@tonic-gate };
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate static int des_create_ctx_template(crypto_provider_handle_t,
2130Sstevel@tonic-gate     crypto_mechanism_t *, crypto_key_t *, crypto_spi_ctx_template_t *,
2140Sstevel@tonic-gate     size_t *, crypto_req_handle_t);
2150Sstevel@tonic-gate static int des_free_context(crypto_ctx_t *);
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate static crypto_ctx_ops_t des_ctx_ops = {
2180Sstevel@tonic-gate 	des_create_ctx_template,
2190Sstevel@tonic-gate 	des_free_context
2200Sstevel@tonic-gate };
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate static int des_key_check(crypto_provider_handle_t, crypto_mechanism_t *,
2230Sstevel@tonic-gate     crypto_key_t *);
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate static crypto_key_ops_t des_key_ops = {
2260Sstevel@tonic-gate 	NULL,
2270Sstevel@tonic-gate 	NULL,
2280Sstevel@tonic-gate 	NULL,
2290Sstevel@tonic-gate 	NULL,
2300Sstevel@tonic-gate 	NULL,
2310Sstevel@tonic-gate 	des_key_check
2320Sstevel@tonic-gate };
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate static crypto_ops_t des_crypto_ops = {
2350Sstevel@tonic-gate 	&des_control_ops,
2360Sstevel@tonic-gate 	NULL,
2370Sstevel@tonic-gate 	&des_cipher_ops,
2380Sstevel@tonic-gate 	NULL,
2390Sstevel@tonic-gate 	NULL,
2400Sstevel@tonic-gate 	NULL,
2410Sstevel@tonic-gate 	NULL,
2420Sstevel@tonic-gate 	NULL,
2430Sstevel@tonic-gate 	NULL,
2440Sstevel@tonic-gate 	NULL,
2450Sstevel@tonic-gate 	NULL,
2460Sstevel@tonic-gate 	&des_key_ops,
2470Sstevel@tonic-gate 	NULL,
2480Sstevel@tonic-gate 	&des_ctx_ops
2490Sstevel@tonic-gate };
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate static crypto_provider_info_t des_prov_info = {
2520Sstevel@tonic-gate 	CRYPTO_SPI_VERSION_1,
2530Sstevel@tonic-gate 	"DES Software Provider",
2540Sstevel@tonic-gate 	CRYPTO_SW_PROVIDER,
2550Sstevel@tonic-gate 	{&modlinkage},
2560Sstevel@tonic-gate 	NULL,
2570Sstevel@tonic-gate 	&des_crypto_ops,
2580Sstevel@tonic-gate 	sizeof (des_mech_info_tab)/sizeof (crypto_mech_info_t),
2590Sstevel@tonic-gate 	des_mech_info_tab
2600Sstevel@tonic-gate };
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate static crypto_kcf_provider_handle_t des_prov_handle = NULL;
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate int
2650Sstevel@tonic-gate _init(void)
2660Sstevel@tonic-gate {
2670Sstevel@tonic-gate 	int ret;
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	if ((ret = mod_install(&modlinkage)) != 0)
2700Sstevel@tonic-gate 		return (ret);
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	/*
2730Sstevel@tonic-gate 	 * Register with KCF. If the registration fails, log an
2740Sstevel@tonic-gate 	 * error but do not uninstall the module, since the functionality
2750Sstevel@tonic-gate 	 * provided by misc/des should still be available.
2760Sstevel@tonic-gate 	 */
2770Sstevel@tonic-gate 	if ((ret = crypto_register_provider(&des_prov_info,
2780Sstevel@tonic-gate 	    &des_prov_handle)) != CRYPTO_SUCCESS) {
2790Sstevel@tonic-gate 		cmn_err(CE_WARN, "des _init: crypto_register_provider() "
2800Sstevel@tonic-gate 		    "failed (0x%x)", ret);
2810Sstevel@tonic-gate 	}
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	return (0);
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate int
2880Sstevel@tonic-gate _info(struct modinfo *modinfop)
2890Sstevel@tonic-gate {
2900Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2910Sstevel@tonic-gate }
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate /*
2940Sstevel@tonic-gate  * Copy 8 bytes
2950Sstevel@tonic-gate  */
2960Sstevel@tonic-gate #define	COPY8(src, dst) { \
2970Sstevel@tonic-gate 	char *a = (char *)dst; \
2980Sstevel@tonic-gate 	char *b = (char *)src; \
2990Sstevel@tonic-gate 	*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
3000Sstevel@tonic-gate 	*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate /*
3040Sstevel@tonic-gate  * Copy multiple of 8 bytes
3050Sstevel@tonic-gate  */
3060Sstevel@tonic-gate #define	DESCOPY(src, dst, len) { \
3070Sstevel@tonic-gate 	char *a = (char *)dst; \
3080Sstevel@tonic-gate 	char *b = (char *)src; \
3090Sstevel@tonic-gate 	int i; \
3100Sstevel@tonic-gate 	for (i = (size_t)len; i > 0; i -= 8) { \
3110Sstevel@tonic-gate 		*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
3120Sstevel@tonic-gate 		*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
3130Sstevel@tonic-gate 	} \
3140Sstevel@tonic-gate }
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate /*
3170Sstevel@tonic-gate  * CBC mode encryption
3180Sstevel@tonic-gate  */
3190Sstevel@tonic-gate /* ARGSUSED */
3200Sstevel@tonic-gate int
3210Sstevel@tonic-gate cbc_crypt(char *key, char *buf, size_t len, unsigned int mode, char *ivec)
3220Sstevel@tonic-gate {
3230Sstevel@tonic-gate 	int err = 0;
3240Sstevel@tonic-gate /* EXPORT DELETE START */
3250Sstevel@tonic-gate 	struct desparams dp;
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	dp.des_mode = CBC;
3280Sstevel@tonic-gate 	COPY8(ivec, dp.des_ivec);
3290Sstevel@tonic-gate 	err = common_crypt(key, buf, len, mode, &dp);
3300Sstevel@tonic-gate 	COPY8(dp.des_ivec, ivec);
3310Sstevel@tonic-gate /* EXPORT DELETE END */
3320Sstevel@tonic-gate 	return (err);
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate /*
3370Sstevel@tonic-gate  * ECB mode encryption
3380Sstevel@tonic-gate  */
3390Sstevel@tonic-gate /* ARGSUSED */
3400Sstevel@tonic-gate int
3410Sstevel@tonic-gate ecb_crypt(char *key, char *buf, size_t len, unsigned int mode)
3420Sstevel@tonic-gate {
3430Sstevel@tonic-gate 	int err = 0;
3440Sstevel@tonic-gate /* EXPORT DELETE START */
3450Sstevel@tonic-gate 	struct desparams dp;
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	dp.des_mode = ECB;
3480Sstevel@tonic-gate 	err = common_crypt(key, buf, len, mode, &dp);
3490Sstevel@tonic-gate /* EXPORT DELETE END */
3500Sstevel@tonic-gate 	return (err);
3510Sstevel@tonic-gate }
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate /* EXPORT DELETE START */
3560Sstevel@tonic-gate /*
3570Sstevel@tonic-gate  * Common code to cbc_crypt() & ecb_crypt()
3580Sstevel@tonic-gate  */
3590Sstevel@tonic-gate static int
3600Sstevel@tonic-gate common_crypt(char *key, char *buf, size_t len, unsigned int mode,
3610Sstevel@tonic-gate     struct desparams *desp)
3620Sstevel@tonic-gate {
3630Sstevel@tonic-gate 	int desdev;
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	if ((len % 8) != 0 || len > DES_MAXDATA)
3660Sstevel@tonic-gate 		return (DESERR_BADPARAM);
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	desp->des_dir =
3690Sstevel@tonic-gate 	    ((mode & DES_DIRMASK) == DES_ENCRYPT) ? ENCRYPT : DECRYPT;
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	desdev = mode & DES_DEVMASK;
3720Sstevel@tonic-gate 	COPY8(key, desp->des_key);
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate #ifdef sun_hardware
3750Sstevel@tonic-gate 	if (desdev == DES_HW) {
3760Sstevel@tonic-gate 		int res;
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 		if (g_desfd < 0 &&
3790Sstevel@tonic-gate 		    (g_desfd == -1 || (g_desfd = getdesfd()) < 0))
3800Sstevel@tonic-gate 				goto software;	/* no hardware device */
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 		/*
3830Sstevel@tonic-gate 		 * hardware
3840Sstevel@tonic-gate 		 */
3850Sstevel@tonic-gate 		desp->des_len = len;
3860Sstevel@tonic-gate 		if (len <= DES_QUICKLEN) {
3870Sstevel@tonic-gate 			DESCOPY(buf, desp->des_data, len);
3880Sstevel@tonic-gate 			res = ioctl(g_desfd, DESIOCQUICK, (char *)desp);
3890Sstevel@tonic-gate 			DESCOPY(desp->des_data, buf, len);
3900Sstevel@tonic-gate 		} else {
3910Sstevel@tonic-gate 			desp->des_buf = (uchar_t *)buf;
3920Sstevel@tonic-gate 			res = ioctl(g_desfd, DESIOCBLOCK, (char *)desp);
3930Sstevel@tonic-gate 		}
3940Sstevel@tonic-gate 		return (res == 0 ? DESERR_NONE : DESERR_HWERROR);
3950Sstevel@tonic-gate 	}
3960Sstevel@tonic-gate software:
3970Sstevel@tonic-gate #endif
3980Sstevel@tonic-gate 	/*
3990Sstevel@tonic-gate 	 * software
4000Sstevel@tonic-gate 	 */
4010Sstevel@tonic-gate 	if (!_des_crypt(buf, len, desp))
4020Sstevel@tonic-gate 		return (DESERR_HWERROR);
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 	return (desdev == DES_SW ? DESERR_NONE : DESERR_NOHWDEVICE);
4050Sstevel@tonic-gate }
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate /*
4080Sstevel@tonic-gate  * Initialize key schedules for DES and DES3
4090Sstevel@tonic-gate  */
4100Sstevel@tonic-gate static int
4110Sstevel@tonic-gate init_keysched(crypto_key_t *key, void *newbie, des_strength_t strength)
4120Sstevel@tonic-gate {
4130Sstevel@tonic-gate 	uint8_t corrected_key[DES3_KEYSIZE];
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	/*
4160Sstevel@tonic-gate 	 * Only keys by value are supported by this module.
4170Sstevel@tonic-gate 	 */
4180Sstevel@tonic-gate 	switch (key->ck_format) {
4190Sstevel@tonic-gate 	case CRYPTO_KEY_RAW:
4200Sstevel@tonic-gate 		if (strength == DES && key->ck_length != DES_MINBITS)
4210Sstevel@tonic-gate 			return (CRYPTO_KEY_SIZE_RANGE);
4220Sstevel@tonic-gate 		if (strength == DES3 && key->ck_length != DES3_MINBITS)
4230Sstevel@tonic-gate 			return (CRYPTO_KEY_SIZE_RANGE);
4240Sstevel@tonic-gate 		break;
4250Sstevel@tonic-gate 	default:
4260Sstevel@tonic-gate 		return (CRYPTO_KEY_TYPE_INCONSISTENT);
4270Sstevel@tonic-gate 	}
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	/*
4300Sstevel@tonic-gate 	 * Fix parity bits.
4310Sstevel@tonic-gate 	 * Initialize key schedule even if key is weak.
4320Sstevel@tonic-gate 	 */
4332439Sizick 	if (key->ck_data == NULL)
4342439Sizick 		return (CRYPTO_ARGUMENTS_BAD);
4350Sstevel@tonic-gate 
4362439Sizick 	des_parity_fix(key->ck_data, strength, corrected_key);
4370Sstevel@tonic-gate 	des_init_keysched(corrected_key, strength, newbie);
4380Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
4390Sstevel@tonic-gate }
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate /* EXPORT DELETE END */
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate /*
4440Sstevel@tonic-gate  * KCF software provider control entry points.
4450Sstevel@tonic-gate  */
4460Sstevel@tonic-gate /* ARGSUSED */
4470Sstevel@tonic-gate static void
4480Sstevel@tonic-gate des_provider_status(crypto_provider_handle_t provider, uint_t *status)
4490Sstevel@tonic-gate {
4500Sstevel@tonic-gate 	*status = CRYPTO_PROVIDER_READY;
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate /*
4540Sstevel@tonic-gate  * KCF software provider encrypt entry points.
4550Sstevel@tonic-gate  */
4560Sstevel@tonic-gate static int
4570Sstevel@tonic-gate des_common_init(crypto_ctx_t *ctx, crypto_mechanism_t *mechanism,
4580Sstevel@tonic-gate     crypto_key_t *key, crypto_spi_ctx_template_t template,
4590Sstevel@tonic-gate     crypto_req_handle_t req)
4600Sstevel@tonic-gate {
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate /* EXPORT DELETE START */
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	des_strength_t strength;
4650Sstevel@tonic-gate 	des_ctx_t *des_ctx;
4660Sstevel@tonic-gate 	int rv;
4670Sstevel@tonic-gate 	int kmflag;
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	/*
4700Sstevel@tonic-gate 	 * Only keys by value are supported by this module.
4710Sstevel@tonic-gate 	 */
4720Sstevel@tonic-gate 	if (key->ck_format != CRYPTO_KEY_RAW) {
4730Sstevel@tonic-gate 		return (CRYPTO_KEY_TYPE_INCONSISTENT);
4740Sstevel@tonic-gate 	}
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	/* Check mechanism type and parameter length */
4770Sstevel@tonic-gate 	switch (mechanism->cm_type) {
4780Sstevel@tonic-gate 	case DES_ECB_MECH_INFO_TYPE:
4790Sstevel@tonic-gate 	case DES_CBC_MECH_INFO_TYPE:
4800Sstevel@tonic-gate 		if (mechanism->cm_param != NULL &&
4810Sstevel@tonic-gate 		    mechanism->cm_param_len != DES_BLOCK_LEN)
4820Sstevel@tonic-gate 			return (CRYPTO_MECHANISM_PARAM_INVALID);
4830Sstevel@tonic-gate 		if (key->ck_length != DES_MINBITS)
4840Sstevel@tonic-gate 			return (CRYPTO_KEY_SIZE_RANGE);
4850Sstevel@tonic-gate 		strength = DES;
4860Sstevel@tonic-gate 		break;
4870Sstevel@tonic-gate 	case DES3_ECB_MECH_INFO_TYPE:
4880Sstevel@tonic-gate 	case DES3_CBC_MECH_INFO_TYPE:
4890Sstevel@tonic-gate 		if (mechanism->cm_param != NULL &&
4900Sstevel@tonic-gate 		    mechanism->cm_param_len != DES_BLOCK_LEN)
4910Sstevel@tonic-gate 			return (CRYPTO_MECHANISM_PARAM_INVALID);
4920Sstevel@tonic-gate 		if (key->ck_length != DES3_MINBITS)
4930Sstevel@tonic-gate 			return (CRYPTO_KEY_SIZE_RANGE);
4940Sstevel@tonic-gate 		strength = DES3;
4950Sstevel@tonic-gate 		break;
4960Sstevel@tonic-gate 	default:
4970Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
4980Sstevel@tonic-gate 	}
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	/*
5010Sstevel@tonic-gate 	 * Allocate a context.  Same context is used for DES and DES3.
5020Sstevel@tonic-gate 	 */
5030Sstevel@tonic-gate 	kmflag = crypto_kmflag(req);
5040Sstevel@tonic-gate 	if ((des_ctx = kmem_zalloc(sizeof (des_ctx_t), kmflag)) == NULL)
5050Sstevel@tonic-gate 		return (CRYPTO_HOST_MEMORY);
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	if ((rv = des_common_init_ctx(des_ctx, template, mechanism, key,
5080Sstevel@tonic-gate 	    strength, kmflag)) != CRYPTO_SUCCESS) {
5090Sstevel@tonic-gate 		kmem_free(des_ctx, sizeof (des_ctx_t));
5100Sstevel@tonic-gate 		return (rv);
5110Sstevel@tonic-gate 	}
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	ctx->cc_provider_private = des_ctx;
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate /* EXPORT DELETE END */
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
5180Sstevel@tonic-gate }
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate /*
5210Sstevel@tonic-gate  * Helper DES encrypt update function for iov input data.
5220Sstevel@tonic-gate  */
5230Sstevel@tonic-gate static int
5240Sstevel@tonic-gate des_cipher_update_iov(des_ctx_t *des_ctx, crypto_data_t *input,
5250Sstevel@tonic-gate     crypto_data_t *output, int (*cipher)(des_ctx_t *, caddr_t, size_t,
5260Sstevel@tonic-gate     crypto_data_t *))
5270Sstevel@tonic-gate {
5280Sstevel@tonic-gate 	if (input->cd_miscdata != NULL) {
5290Sstevel@tonic-gate 		if (IS_P2ALIGNED(input->cd_miscdata, sizeof (uint64_t))) {
5300Sstevel@tonic-gate 			/* LINTED: pointer alignment */
5310Sstevel@tonic-gate 			des_ctx->dc_iv = *(uint64_t *)input->cd_miscdata;
5320Sstevel@tonic-gate 		} else {
5330Sstevel@tonic-gate 			uint64_t tmp64;
5340Sstevel@tonic-gate 			uint8_t *tmp = (uint8_t *)input->cd_miscdata;
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate #ifdef _BIG_ENDIAN
5370Sstevel@tonic-gate 			tmp64 = (((uint64_t)tmp[0] << 56) |
5380Sstevel@tonic-gate 			    ((uint64_t)tmp[1] << 48) |
5390Sstevel@tonic-gate 			    ((uint64_t)tmp[2] << 40) |
5400Sstevel@tonic-gate 			    ((uint64_t)tmp[3] << 32) |
5410Sstevel@tonic-gate 			    ((uint64_t)tmp[4] << 24) |
5420Sstevel@tonic-gate 			    ((uint64_t)tmp[5] << 16) |
5430Sstevel@tonic-gate 			    ((uint64_t)tmp[6] << 8) |
5440Sstevel@tonic-gate 			    (uint64_t)tmp[7]);
5450Sstevel@tonic-gate #else
5460Sstevel@tonic-gate 			tmp64 = (((uint64_t)tmp[7] << 56) |
5470Sstevel@tonic-gate 			    ((uint64_t)tmp[6] << 48) |
5480Sstevel@tonic-gate 			    ((uint64_t)tmp[5] << 40) |
5490Sstevel@tonic-gate 			    ((uint64_t)tmp[4] << 32) |
5500Sstevel@tonic-gate 			    ((uint64_t)tmp[3] << 24) |
5510Sstevel@tonic-gate 			    ((uint64_t)tmp[2] << 16) |
5520Sstevel@tonic-gate 			    ((uint64_t)tmp[1] << 8) |
5530Sstevel@tonic-gate 			    (uint64_t)tmp[0]);
5540Sstevel@tonic-gate #endif /* _BIG_ENDIAN */
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 			des_ctx->dc_iv = tmp64;
5570Sstevel@tonic-gate 		}
5580Sstevel@tonic-gate 	}
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	if (input->cd_raw.iov_len < input->cd_length)
5610Sstevel@tonic-gate 		return (CRYPTO_ARGUMENTS_BAD);
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	return ((cipher)(des_ctx, input->cd_raw.iov_base + input->cd_offset,
5640Sstevel@tonic-gate 	    input->cd_length, (input == output) ? NULL : output));
5650Sstevel@tonic-gate }
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate /*
5680Sstevel@tonic-gate  * Helper DES encrypt update function for uio input data.
5690Sstevel@tonic-gate  */
5700Sstevel@tonic-gate static int
5710Sstevel@tonic-gate des_cipher_update_uio(des_ctx_t *des_ctx, crypto_data_t *input,
5720Sstevel@tonic-gate     crypto_data_t *output, int (*cipher)(des_ctx_t *, caddr_t, size_t,
5730Sstevel@tonic-gate     crypto_data_t *))
5740Sstevel@tonic-gate {
5750Sstevel@tonic-gate 	uio_t *uiop = input->cd_uio;
5760Sstevel@tonic-gate 	off_t offset = input->cd_offset;
5770Sstevel@tonic-gate 	size_t length = input->cd_length;
5780Sstevel@tonic-gate 	uint_t vec_idx;
5790Sstevel@tonic-gate 	size_t cur_len;
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 	if (input->cd_miscdata != NULL) {
5820Sstevel@tonic-gate 		if (IS_P2ALIGNED(input->cd_miscdata, sizeof (uint64_t))) {
5830Sstevel@tonic-gate 			/* LINTED: pointer alignment */
5840Sstevel@tonic-gate 			des_ctx->dc_iv = *(uint64_t *)input->cd_miscdata;
5850Sstevel@tonic-gate 		} else {
5860Sstevel@tonic-gate 			uint64_t tmp64;
5870Sstevel@tonic-gate 			uint8_t *tmp = (uint8_t *)input->cd_miscdata;
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate #ifdef _BIG_ENDIAN
5900Sstevel@tonic-gate 			tmp64 = (((uint64_t)tmp[0] << 56) |
5910Sstevel@tonic-gate 			    ((uint64_t)tmp[1] << 48) |
5920Sstevel@tonic-gate 			    ((uint64_t)tmp[2] << 40) |
5930Sstevel@tonic-gate 			    ((uint64_t)tmp[3] << 32) |
5940Sstevel@tonic-gate 			    ((uint64_t)tmp[4] << 24) |
5950Sstevel@tonic-gate 			    ((uint64_t)tmp[5] << 16) |
5960Sstevel@tonic-gate 			    ((uint64_t)tmp[6] << 8) |
5970Sstevel@tonic-gate 			    (uint64_t)tmp[7]);
5980Sstevel@tonic-gate #else
5990Sstevel@tonic-gate 			tmp64 = (((uint64_t)tmp[7] << 56) |
6000Sstevel@tonic-gate 			    ((uint64_t)tmp[6] << 48) |
6010Sstevel@tonic-gate 			    ((uint64_t)tmp[5] << 40) |
6020Sstevel@tonic-gate 			    ((uint64_t)tmp[4] << 32) |
6030Sstevel@tonic-gate 			    ((uint64_t)tmp[3] << 24) |
6040Sstevel@tonic-gate 			    ((uint64_t)tmp[2] << 16) |
6050Sstevel@tonic-gate 			    ((uint64_t)tmp[1] << 8) |
6060Sstevel@tonic-gate 			    (uint64_t)tmp[0]);
6070Sstevel@tonic-gate #endif /* _BIG_ENDIAN */
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 			des_ctx->dc_iv = tmp64;
6100Sstevel@tonic-gate 		}
6110Sstevel@tonic-gate 	}
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	if (input->cd_uio->uio_segflg != UIO_SYSSPACE) {
6140Sstevel@tonic-gate 		return (CRYPTO_ARGUMENTS_BAD);
6150Sstevel@tonic-gate 	}
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 	/*
6180Sstevel@tonic-gate 	 * Jump to the first iovec containing data to be
6190Sstevel@tonic-gate 	 * processed.
6200Sstevel@tonic-gate 	 */
6210Sstevel@tonic-gate 	for (vec_idx = 0; vec_idx < uiop->uio_iovcnt &&
6220Sstevel@tonic-gate 	    offset >= uiop->uio_iov[vec_idx].iov_len;
623*5072Smcpowers 	    offset -= uiop->uio_iov[vec_idx++].iov_len)
624*5072Smcpowers 		;
6250Sstevel@tonic-gate 	if (vec_idx == uiop->uio_iovcnt) {
6260Sstevel@tonic-gate 		/*
6270Sstevel@tonic-gate 		 * The caller specified an offset that is larger than the
6280Sstevel@tonic-gate 		 * total size of the buffers it provided.
6290Sstevel@tonic-gate 		 */
6300Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
6310Sstevel@tonic-gate 	}
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	/*
6340Sstevel@tonic-gate 	 * Now process the iovecs.
6350Sstevel@tonic-gate 	 */
6360Sstevel@tonic-gate 	while (vec_idx < uiop->uio_iovcnt && length > 0) {
6370Sstevel@tonic-gate 		cur_len = MIN(uiop->uio_iov[vec_idx].iov_len -
6380Sstevel@tonic-gate 		    offset, length);
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 		(cipher)(des_ctx, uiop->uio_iov[vec_idx].iov_base + offset,
6410Sstevel@tonic-gate 		    cur_len, (input == output) ? NULL : output);
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 		length -= cur_len;
6440Sstevel@tonic-gate 		vec_idx++;
6450Sstevel@tonic-gate 		offset = 0;
6460Sstevel@tonic-gate 	}
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 	if (vec_idx == uiop->uio_iovcnt && length > 0) {
6490Sstevel@tonic-gate 		/*
6500Sstevel@tonic-gate 		 * The end of the specified iovec's was reached but
6510Sstevel@tonic-gate 		 * the length requested could not be processed, i.e.
6520Sstevel@tonic-gate 		 * The caller requested to digest more data than it provided.
6530Sstevel@tonic-gate 		 */
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
6560Sstevel@tonic-gate 	}
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
6590Sstevel@tonic-gate }
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate /*
6620Sstevel@tonic-gate  * Helper DES encrypt update function for mblk input data.
6630Sstevel@tonic-gate  */
6640Sstevel@tonic-gate static int
6650Sstevel@tonic-gate des_cipher_update_mp(des_ctx_t *des_ctx, crypto_data_t *input,
6660Sstevel@tonic-gate     crypto_data_t *output, int (*cipher)(des_ctx_t *, caddr_t, size_t,
6670Sstevel@tonic-gate     crypto_data_t *))
6680Sstevel@tonic-gate {
6690Sstevel@tonic-gate 	off_t offset = input->cd_offset;
6700Sstevel@tonic-gate 	size_t length = input->cd_length;
6710Sstevel@tonic-gate 	mblk_t *mp;
6720Sstevel@tonic-gate 	size_t cur_len;
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 	if (input->cd_miscdata != NULL) {
6750Sstevel@tonic-gate 		if (IS_P2ALIGNED(input->cd_miscdata, sizeof (uint64_t))) {
6760Sstevel@tonic-gate 			/* LINTED: pointer alignment */
6770Sstevel@tonic-gate 			des_ctx->dc_iv = *(uint64_t *)input->cd_miscdata;
6780Sstevel@tonic-gate 		} else {
6790Sstevel@tonic-gate 			uint64_t tmp64;
6800Sstevel@tonic-gate 			uint8_t *tmp = (uint8_t *)input->cd_miscdata;
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate #ifdef _BIG_ENDIAN
6830Sstevel@tonic-gate 			tmp64 = (((uint64_t)tmp[0] << 56) |
6840Sstevel@tonic-gate 			    ((uint64_t)tmp[1] << 48) |
6850Sstevel@tonic-gate 			    ((uint64_t)tmp[2] << 40) |
6860Sstevel@tonic-gate 			    ((uint64_t)tmp[3] << 32) |
6870Sstevel@tonic-gate 			    ((uint64_t)tmp[4] << 24) |
6880Sstevel@tonic-gate 			    ((uint64_t)tmp[5] << 16) |
6890Sstevel@tonic-gate 			    ((uint64_t)tmp[6] << 8) |
6900Sstevel@tonic-gate 			    (uint64_t)tmp[7]);
6910Sstevel@tonic-gate #else
6920Sstevel@tonic-gate 			tmp64 = (((uint64_t)tmp[7] << 56) |
6930Sstevel@tonic-gate 			    ((uint64_t)tmp[6] << 48) |
6940Sstevel@tonic-gate 			    ((uint64_t)tmp[5] << 40) |
6950Sstevel@tonic-gate 			    ((uint64_t)tmp[4] << 32) |
6960Sstevel@tonic-gate 			    ((uint64_t)tmp[3] << 24) |
6970Sstevel@tonic-gate 			    ((uint64_t)tmp[2] << 16) |
6980Sstevel@tonic-gate 			    ((uint64_t)tmp[1] << 8) |
6990Sstevel@tonic-gate 			    (uint64_t)tmp[0]);
7000Sstevel@tonic-gate #endif /* _BIG_ENDIAN */
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 			des_ctx->dc_iv = tmp64;
7030Sstevel@tonic-gate 		}
7040Sstevel@tonic-gate 	}
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	/*
7070Sstevel@tonic-gate 	 * Jump to the first mblk_t containing data to be processed.
7080Sstevel@tonic-gate 	 */
7090Sstevel@tonic-gate 	for (mp = input->cd_mp; mp != NULL && offset >= MBLKL(mp);
710*5072Smcpowers 	    offset -= MBLKL(mp), mp = mp->b_cont)
711*5072Smcpowers 		;
7120Sstevel@tonic-gate 	if (mp == NULL) {
7130Sstevel@tonic-gate 		/*
7140Sstevel@tonic-gate 		 * The caller specified an offset that is larger than the
7150Sstevel@tonic-gate 		 * total size of the buffers it provided.
7160Sstevel@tonic-gate 		 */
7170Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
7180Sstevel@tonic-gate 	}
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	/*
7210Sstevel@tonic-gate 	 * Now do the processing on the mblk chain.
7220Sstevel@tonic-gate 	 */
7230Sstevel@tonic-gate 	while (mp != NULL && length > 0) {
7240Sstevel@tonic-gate 		cur_len = MIN(MBLKL(mp) - offset, length);
7250Sstevel@tonic-gate 		(cipher)(des_ctx, (char *)(mp->b_rptr + offset), cur_len,
7260Sstevel@tonic-gate 		    (input == output) ? NULL : output);
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 		length -= cur_len;
7290Sstevel@tonic-gate 		offset = 0;
7300Sstevel@tonic-gate 		mp = mp->b_cont;
7310Sstevel@tonic-gate 	}
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 	if (mp == NULL && length > 0) {
7340Sstevel@tonic-gate 		/*
7350Sstevel@tonic-gate 		 * The end of the mblk was reached but the length requested
7360Sstevel@tonic-gate 		 * could not be processed, i.e. The caller requested
7370Sstevel@tonic-gate 		 * to digest more data than it provided.
7380Sstevel@tonic-gate 		 */
7390Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
7400Sstevel@tonic-gate 	}
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
7430Sstevel@tonic-gate }
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate /* ARGSUSED */
7460Sstevel@tonic-gate static int
7470Sstevel@tonic-gate des_encrypt(crypto_ctx_t *ctx, crypto_data_t *plaintext,
7480Sstevel@tonic-gate     crypto_data_t *ciphertext, crypto_req_handle_t req)
7490Sstevel@tonic-gate {
7500Sstevel@tonic-gate 	int ret;
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate /* EXPORT DELETE START */
7530Sstevel@tonic-gate 	des_ctx_t *des_ctx;
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 	/*
7560Sstevel@tonic-gate 	 * Plaintext must be a multiple of the block size.
7570Sstevel@tonic-gate 	 * This test only works for non-padded mechanisms
7580Sstevel@tonic-gate 	 * when blocksize is 2^N.
7590Sstevel@tonic-gate 	 */
7600Sstevel@tonic-gate 	if ((plaintext->cd_length & (DES_BLOCK_LEN - 1)) != 0)
7610Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
7640Sstevel@tonic-gate 	des_ctx = ctx->cc_provider_private;
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 	DES_ARG_INPLACE(plaintext, ciphertext);
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 	/*
7690Sstevel@tonic-gate 	 * We need to just return the length needed to store the output.
7700Sstevel@tonic-gate 	 * We should not destroy the context for the following case.
7710Sstevel@tonic-gate 	 */
7720Sstevel@tonic-gate 	if (ciphertext->cd_length < plaintext->cd_length) {
7730Sstevel@tonic-gate 		ciphertext->cd_length = plaintext->cd_length;
7740Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
7750Sstevel@tonic-gate 	}
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate 	/*
7780Sstevel@tonic-gate 	 * Do an update on the specified input data.
7790Sstevel@tonic-gate 	 */
7800Sstevel@tonic-gate 	ret = des_encrypt_update(ctx, plaintext, ciphertext, req);
7810Sstevel@tonic-gate 	ASSERT(des_ctx->dc_remainder_len == 0);
7820Sstevel@tonic-gate 	(void) des_free_context(ctx);
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate /* EXPORT DELETE END */
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate 	/* LINTED */
7870Sstevel@tonic-gate 	return (ret);
7880Sstevel@tonic-gate }
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate /* ARGSUSED */
7910Sstevel@tonic-gate static int
7920Sstevel@tonic-gate des_decrypt(crypto_ctx_t *ctx, crypto_data_t *ciphertext,
7930Sstevel@tonic-gate     crypto_data_t *plaintext, crypto_req_handle_t req)
7940Sstevel@tonic-gate {
7950Sstevel@tonic-gate 	int ret;
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate /* EXPORT DELETE START */
7980Sstevel@tonic-gate 	des_ctx_t *des_ctx;
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 	/*
8010Sstevel@tonic-gate 	 * Ciphertext must be a multiple of the block size.
8020Sstevel@tonic-gate 	 * This test only works for non-padded mechanisms
8030Sstevel@tonic-gate 	 * when blocksize is 2^N.
8040Sstevel@tonic-gate 	 */
8050Sstevel@tonic-gate 	if ((ciphertext->cd_length & (DES_BLOCK_LEN - 1)) != 0)
8060Sstevel@tonic-gate 		return (CRYPTO_ENCRYPTED_DATA_LEN_RANGE);
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
8090Sstevel@tonic-gate 	des_ctx = ctx->cc_provider_private;
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 	DES_ARG_INPLACE(ciphertext, plaintext);
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 	/*
8140Sstevel@tonic-gate 	 * We need to just return the length needed to store the output.
8150Sstevel@tonic-gate 	 * We should not destroy the context for the following case.
8160Sstevel@tonic-gate 	 */
8170Sstevel@tonic-gate 	if (plaintext->cd_length < ciphertext->cd_length) {
8180Sstevel@tonic-gate 		plaintext->cd_length = ciphertext->cd_length;
8190Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
8200Sstevel@tonic-gate 	}
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	/*
8230Sstevel@tonic-gate 	 * Do an update on the specified input data.
8240Sstevel@tonic-gate 	 */
8250Sstevel@tonic-gate 	ret = des_decrypt_update(ctx, ciphertext, plaintext, req);
8260Sstevel@tonic-gate 	ASSERT(des_ctx->dc_remainder_len == 0);
8270Sstevel@tonic-gate 	(void) des_free_context(ctx);
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate /* EXPORT DELETE END */
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 	/* LINTED */
8320Sstevel@tonic-gate 	return (ret);
8330Sstevel@tonic-gate }
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate /* ARGSUSED */
8360Sstevel@tonic-gate static int
8370Sstevel@tonic-gate des_encrypt_update(crypto_ctx_t *ctx, crypto_data_t *plaintext,
8380Sstevel@tonic-gate     crypto_data_t *ciphertext, crypto_req_handle_t req)
8390Sstevel@tonic-gate {
8400Sstevel@tonic-gate 	off_t saved_offset;
8410Sstevel@tonic-gate 	size_t saved_length, out_len;
8420Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate /* EXPORT DELETE START */
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	DES_ARG_INPLACE(plaintext, ciphertext);
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 	/* compute number of bytes that will hold the ciphertext */
8510Sstevel@tonic-gate 	out_len = ((des_ctx_t *)ctx->cc_provider_private)->dc_remainder_len;
8520Sstevel@tonic-gate 	out_len += plaintext->cd_length;
8530Sstevel@tonic-gate 	out_len &= ~(DES_BLOCK_LEN - 1);
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 	/* return length needed to store the output */
8560Sstevel@tonic-gate 	if (ciphertext->cd_length < out_len) {
8570Sstevel@tonic-gate 		ciphertext->cd_length = out_len;
8580Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
8590Sstevel@tonic-gate 	}
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 	saved_offset = ciphertext->cd_offset;
8620Sstevel@tonic-gate 	saved_length = ciphertext->cd_length;
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 	/*
8650Sstevel@tonic-gate 	 * Do the DES update on the specified input data.
8660Sstevel@tonic-gate 	 */
8670Sstevel@tonic-gate 	switch (plaintext->cd_format) {
8680Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
8690Sstevel@tonic-gate 		ret = des_cipher_update_iov(ctx->cc_provider_private,
8700Sstevel@tonic-gate 		    plaintext, ciphertext, des_encrypt_contiguous_blocks);
8710Sstevel@tonic-gate 		break;
8720Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
8730Sstevel@tonic-gate 		ret = des_cipher_update_uio(ctx->cc_provider_private,
8740Sstevel@tonic-gate 		    plaintext, ciphertext, des_encrypt_contiguous_blocks);
8750Sstevel@tonic-gate 		break;
8760Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
8770Sstevel@tonic-gate 		ret = des_cipher_update_mp(ctx->cc_provider_private,
8780Sstevel@tonic-gate 		    plaintext, ciphertext, des_encrypt_contiguous_blocks);
8790Sstevel@tonic-gate 		break;
8800Sstevel@tonic-gate 	default:
8810Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
8820Sstevel@tonic-gate 	}
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
8850Sstevel@tonic-gate 		if (plaintext != ciphertext)
8860Sstevel@tonic-gate 			ciphertext->cd_length =
8870Sstevel@tonic-gate 			    ciphertext->cd_offset - saved_offset;
8880Sstevel@tonic-gate 	} else {
8890Sstevel@tonic-gate 		ciphertext->cd_length = saved_length;
8900Sstevel@tonic-gate 	}
8910Sstevel@tonic-gate 	ciphertext->cd_offset = saved_offset;
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate /* EXPORT DELETE END */
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate 	return (ret);
8960Sstevel@tonic-gate }
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate /* ARGSUSED */
8990Sstevel@tonic-gate static int
9000Sstevel@tonic-gate des_decrypt_update(crypto_ctx_t *ctx, crypto_data_t *ciphertext,
9010Sstevel@tonic-gate     crypto_data_t *plaintext, crypto_req_handle_t req)
9020Sstevel@tonic-gate {
9030Sstevel@tonic-gate 	off_t saved_offset;
9040Sstevel@tonic-gate 	size_t saved_length, out_len;
9050Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate /* EXPORT DELETE START */
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate 	DES_ARG_INPLACE(ciphertext, plaintext);
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate 	/* compute number of bytes that will hold the plaintext */
9140Sstevel@tonic-gate 	out_len = ((des_ctx_t *)ctx->cc_provider_private)->dc_remainder_len;
9150Sstevel@tonic-gate 	out_len += ciphertext->cd_length;
9160Sstevel@tonic-gate 	out_len &= ~(DES_BLOCK_LEN - 1);
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 	/* return length needed to store the output */
9190Sstevel@tonic-gate 	if (plaintext->cd_length < out_len) {
9200Sstevel@tonic-gate 		plaintext->cd_length = out_len;
9210Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
9220Sstevel@tonic-gate 	}
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate 	saved_offset = plaintext->cd_offset;
9250Sstevel@tonic-gate 	saved_length = plaintext->cd_length;
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate 	/*
9280Sstevel@tonic-gate 	 * Do the DES update on the specified input data.
9290Sstevel@tonic-gate 	 */
9300Sstevel@tonic-gate 	switch (ciphertext->cd_format) {
9310Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
9320Sstevel@tonic-gate 		ret = des_cipher_update_iov(ctx->cc_provider_private,
9330Sstevel@tonic-gate 		    ciphertext, plaintext, des_decrypt_contiguous_blocks);
9340Sstevel@tonic-gate 		break;
9350Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
9360Sstevel@tonic-gate 		ret = des_cipher_update_uio(ctx->cc_provider_private,
9370Sstevel@tonic-gate 		    ciphertext, plaintext, des_decrypt_contiguous_blocks);
9380Sstevel@tonic-gate 		break;
9390Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
9400Sstevel@tonic-gate 		ret = des_cipher_update_mp(ctx->cc_provider_private,
9410Sstevel@tonic-gate 		    ciphertext, plaintext, des_decrypt_contiguous_blocks);
9420Sstevel@tonic-gate 		break;
9430Sstevel@tonic-gate 	default:
9440Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
9450Sstevel@tonic-gate 	}
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
9480Sstevel@tonic-gate 		if (ciphertext != plaintext)
9490Sstevel@tonic-gate 			plaintext->cd_length =
9500Sstevel@tonic-gate 			    plaintext->cd_offset - saved_offset;
9510Sstevel@tonic-gate 	} else {
9520Sstevel@tonic-gate 		plaintext->cd_length = saved_length;
9530Sstevel@tonic-gate 	}
9540Sstevel@tonic-gate 	plaintext->cd_offset = saved_offset;
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate /* EXPORT DELETE END */
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate 	return (ret);
9590Sstevel@tonic-gate }
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate /* ARGSUSED */
9620Sstevel@tonic-gate static int
9630Sstevel@tonic-gate des_encrypt_final(crypto_ctx_t *ctx, crypto_data_t *ciphertext,
9640Sstevel@tonic-gate     crypto_req_handle_t req)
9650Sstevel@tonic-gate {
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate /* EXPORT DELETE START */
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate 	des_ctx_t *des_ctx;
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
9720Sstevel@tonic-gate 	des_ctx = ctx->cc_provider_private;
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 	/*
9750Sstevel@tonic-gate 	 * There must be no unprocessed plaintext.
9760Sstevel@tonic-gate 	 * This happens if the length of the last data is
9770Sstevel@tonic-gate 	 * not a multiple of the DES block length.
9780Sstevel@tonic-gate 	 */
9790Sstevel@tonic-gate 	if (des_ctx->dc_remainder_len > 0)
9800Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
9810Sstevel@tonic-gate 
9820Sstevel@tonic-gate 	(void) des_free_context(ctx);
9830Sstevel@tonic-gate 	ciphertext->cd_length = 0;
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate /* EXPORT DELETE END */
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
9880Sstevel@tonic-gate }
9890Sstevel@tonic-gate 
9900Sstevel@tonic-gate /* ARGSUSED */
9910Sstevel@tonic-gate static int
9920Sstevel@tonic-gate des_decrypt_final(crypto_ctx_t *ctx, crypto_data_t *plaintext,
9930Sstevel@tonic-gate     crypto_req_handle_t req)
9940Sstevel@tonic-gate {
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate /* EXPORT DELETE START */
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 	des_ctx_t *des_ctx;
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
10010Sstevel@tonic-gate 	des_ctx = ctx->cc_provider_private;
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 	/*
10040Sstevel@tonic-gate 	 * There must be no unprocessed ciphertext.
10050Sstevel@tonic-gate 	 * This happens if the length of the last ciphertext is
10060Sstevel@tonic-gate 	 * not a multiple of the DES block length.
10070Sstevel@tonic-gate 	 */
10080Sstevel@tonic-gate 	if (des_ctx->dc_remainder_len > 0)
10090Sstevel@tonic-gate 		return (CRYPTO_ENCRYPTED_DATA_LEN_RANGE);
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 	(void) des_free_context(ctx);
10120Sstevel@tonic-gate 	plaintext->cd_length = 0;
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate /* EXPORT DELETE END */
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
10170Sstevel@tonic-gate }
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate /* ARGSUSED */
10200Sstevel@tonic-gate static int
10210Sstevel@tonic-gate des_encrypt_atomic(crypto_provider_handle_t provider,
10220Sstevel@tonic-gate     crypto_session_id_t session_id, crypto_mechanism_t *mechanism,
10230Sstevel@tonic-gate     crypto_key_t *key, crypto_data_t *plaintext, crypto_data_t *ciphertext,
10240Sstevel@tonic-gate     crypto_spi_ctx_template_t template, crypto_req_handle_t req)
10250Sstevel@tonic-gate {
10260Sstevel@tonic-gate 	int ret;
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate /* EXPORT DELETE START */
10290Sstevel@tonic-gate 
10300Sstevel@tonic-gate 	des_ctx_t des_ctx;		/* on the stack */
10310Sstevel@tonic-gate 	des_strength_t strength;
10320Sstevel@tonic-gate 	off_t saved_offset;
10330Sstevel@tonic-gate 	size_t saved_length;
10340Sstevel@tonic-gate 
10350Sstevel@tonic-gate 	DES_ARG_INPLACE(plaintext, ciphertext);
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 	/*
10380Sstevel@tonic-gate 	 * Plaintext must be a multiple of the block size.
10390Sstevel@tonic-gate 	 * This test only works for non-padded mechanisms
10400Sstevel@tonic-gate 	 * when blocksize is 2^N.
10410Sstevel@tonic-gate 	 */
10420Sstevel@tonic-gate 	if ((plaintext->cd_length & (DES_BLOCK_LEN - 1)) != 0)
10430Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
10440Sstevel@tonic-gate 
10450Sstevel@tonic-gate 	/* return length needed to store the output */
10460Sstevel@tonic-gate 	if (ciphertext->cd_length < plaintext->cd_length) {
10470Sstevel@tonic-gate 		ciphertext->cd_length = plaintext->cd_length;
10480Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
10490Sstevel@tonic-gate 	}
10500Sstevel@tonic-gate 
10510Sstevel@tonic-gate 	/* Check mechanism type and parameter length */
10520Sstevel@tonic-gate 	switch (mechanism->cm_type) {
10530Sstevel@tonic-gate 	case DES_ECB_MECH_INFO_TYPE:
10540Sstevel@tonic-gate 	case DES_CBC_MECH_INFO_TYPE:
10550Sstevel@tonic-gate 		if (mechanism->cm_param_len > 0 &&
10560Sstevel@tonic-gate 		    mechanism->cm_param_len != DES_BLOCK_LEN)
10570Sstevel@tonic-gate 			return (CRYPTO_MECHANISM_PARAM_INVALID);
10580Sstevel@tonic-gate 		if (key->ck_length != DES_MINBITS)
10590Sstevel@tonic-gate 			return (CRYPTO_KEY_SIZE_RANGE);
10600Sstevel@tonic-gate 		strength = DES;
10610Sstevel@tonic-gate 		break;
10620Sstevel@tonic-gate 	case DES3_ECB_MECH_INFO_TYPE:
10630Sstevel@tonic-gate 	case DES3_CBC_MECH_INFO_TYPE:
10640Sstevel@tonic-gate 		if (mechanism->cm_param_len > 0 &&
10650Sstevel@tonic-gate 		    mechanism->cm_param_len != DES_BLOCK_LEN)
10660Sstevel@tonic-gate 			return (CRYPTO_MECHANISM_PARAM_INVALID);
10670Sstevel@tonic-gate 		if (key->ck_length != DES3_MINBITS)
10680Sstevel@tonic-gate 			return (CRYPTO_KEY_SIZE_RANGE);
10690Sstevel@tonic-gate 		strength = DES3;
10700Sstevel@tonic-gate 		break;
10710Sstevel@tonic-gate 	default:
10720Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
10730Sstevel@tonic-gate 	}
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 	bzero(&des_ctx, sizeof (des_ctx_t));
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 	if ((ret = des_common_init_ctx(&des_ctx, template, mechanism, key,
10780Sstevel@tonic-gate 	    strength, crypto_kmflag(req))) != CRYPTO_SUCCESS) {
10790Sstevel@tonic-gate 		return (ret);
10800Sstevel@tonic-gate 	}
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate 	saved_offset = ciphertext->cd_offset;
10830Sstevel@tonic-gate 	saved_length = ciphertext->cd_length;
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 	/*
10860Sstevel@tonic-gate 	 * Do the update on the specified input data.
10870Sstevel@tonic-gate 	 */
10880Sstevel@tonic-gate 	switch (plaintext->cd_format) {
10890Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
10900Sstevel@tonic-gate 		ret = des_cipher_update_iov(&des_ctx, plaintext, ciphertext,
10910Sstevel@tonic-gate 		    des_encrypt_contiguous_blocks);
10920Sstevel@tonic-gate 		break;
10930Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
10940Sstevel@tonic-gate 		ret = des_cipher_update_uio(&des_ctx, plaintext, ciphertext,
10950Sstevel@tonic-gate 		    des_encrypt_contiguous_blocks);
10960Sstevel@tonic-gate 		break;
10970Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
10980Sstevel@tonic-gate 		ret = des_cipher_update_mp(&des_ctx, plaintext, ciphertext,
10990Sstevel@tonic-gate 		    des_encrypt_contiguous_blocks);
11000Sstevel@tonic-gate 		break;
11010Sstevel@tonic-gate 	default:
11020Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
11030Sstevel@tonic-gate 	}
11040Sstevel@tonic-gate 
11050Sstevel@tonic-gate 	if (des_ctx.dc_flags & DES_PROVIDER_OWNS_KEY_SCHEDULE) {
11060Sstevel@tonic-gate 		bzero(des_ctx.dc_keysched, des_ctx.dc_keysched_len);
11070Sstevel@tonic-gate 		kmem_free(des_ctx.dc_keysched, des_ctx.dc_keysched_len);
11080Sstevel@tonic-gate 	}
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
11110Sstevel@tonic-gate 		ASSERT(des_ctx.dc_remainder_len == 0);
11120Sstevel@tonic-gate 		if (plaintext != ciphertext)
11130Sstevel@tonic-gate 			ciphertext->cd_length =
11140Sstevel@tonic-gate 			    ciphertext->cd_offset - saved_offset;
11150Sstevel@tonic-gate 	} else {
11160Sstevel@tonic-gate 		ciphertext->cd_length = saved_length;
11170Sstevel@tonic-gate 	}
11180Sstevel@tonic-gate 	ciphertext->cd_offset = saved_offset;
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate /* EXPORT DELETE END */
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 	/* LINTED */
11230Sstevel@tonic-gate 	return (ret);
11240Sstevel@tonic-gate }
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate /* ARGSUSED */
11270Sstevel@tonic-gate static int
11280Sstevel@tonic-gate des_decrypt_atomic(crypto_provider_handle_t provider,
11290Sstevel@tonic-gate     crypto_session_id_t session_id, crypto_mechanism_t *mechanism,
11300Sstevel@tonic-gate     crypto_key_t *key, crypto_data_t *ciphertext, crypto_data_t *plaintext,
11310Sstevel@tonic-gate     crypto_spi_ctx_template_t template, crypto_req_handle_t req)
11320Sstevel@tonic-gate {
11330Sstevel@tonic-gate 	int ret;
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate /* EXPORT DELETE START */
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate 	des_ctx_t des_ctx;	/* on the stack */
11380Sstevel@tonic-gate 	des_strength_t strength;
11390Sstevel@tonic-gate 	off_t saved_offset;
11400Sstevel@tonic-gate 	size_t saved_length;
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate 	DES_ARG_INPLACE(ciphertext, plaintext);
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 	/*
11450Sstevel@tonic-gate 	 * Ciphertext must be a multiple of the block size.
11460Sstevel@tonic-gate 	 * This test only works for non-padded mechanisms
11470Sstevel@tonic-gate 	 * when blocksize is 2^N.
11480Sstevel@tonic-gate 	 */
11490Sstevel@tonic-gate 	if ((ciphertext->cd_length & (DES_BLOCK_LEN - 1)) != 0)
11500Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 	/* return length needed to store the output */
11530Sstevel@tonic-gate 	if (plaintext->cd_length < ciphertext->cd_length) {
11540Sstevel@tonic-gate 		plaintext->cd_length = ciphertext->cd_length;
11550Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
11560Sstevel@tonic-gate 	}
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 	/* Check mechanism type and parameter length */
11590Sstevel@tonic-gate 	switch (mechanism->cm_type) {
11600Sstevel@tonic-gate 	case DES_ECB_MECH_INFO_TYPE:
11610Sstevel@tonic-gate 	case DES_CBC_MECH_INFO_TYPE:
11620Sstevel@tonic-gate 		if (mechanism->cm_param_len > 0 &&
11630Sstevel@tonic-gate 		    mechanism->cm_param_len != DES_BLOCK_LEN)
11640Sstevel@tonic-gate 			return (CRYPTO_MECHANISM_PARAM_INVALID);
11650Sstevel@tonic-gate 		if (key->ck_length != DES_MINBITS)
11660Sstevel@tonic-gate 			return (CRYPTO_KEY_SIZE_RANGE);
11670Sstevel@tonic-gate 		strength = DES;
11680Sstevel@tonic-gate 		break;
11690Sstevel@tonic-gate 	case DES3_ECB_MECH_INFO_TYPE:
11700Sstevel@tonic-gate 	case DES3_CBC_MECH_INFO_TYPE:
11710Sstevel@tonic-gate 		if (mechanism->cm_param_len > 0 &&
11720Sstevel@tonic-gate 		    mechanism->cm_param_len != DES_BLOCK_LEN)
11730Sstevel@tonic-gate 			return (CRYPTO_MECHANISM_PARAM_INVALID);
11740Sstevel@tonic-gate 		if (key->ck_length != DES3_MINBITS)
11750Sstevel@tonic-gate 			return (CRYPTO_KEY_SIZE_RANGE);
11760Sstevel@tonic-gate 		strength = DES3;
11770Sstevel@tonic-gate 		break;
11780Sstevel@tonic-gate 	default:
11790Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
11800Sstevel@tonic-gate 	}
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 	bzero(&des_ctx, sizeof (des_ctx_t));
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 	if ((ret = des_common_init_ctx(&des_ctx, template, mechanism, key,
11850Sstevel@tonic-gate 	    strength, crypto_kmflag(req))) != CRYPTO_SUCCESS) {
11860Sstevel@tonic-gate 		return (ret);
11870Sstevel@tonic-gate 	}
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate 	saved_offset = plaintext->cd_offset;
11900Sstevel@tonic-gate 	saved_length = plaintext->cd_length;
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 	/*
11930Sstevel@tonic-gate 	 * Do the update on the specified input data.
11940Sstevel@tonic-gate 	 */
11950Sstevel@tonic-gate 	switch (ciphertext->cd_format) {
11960Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
11970Sstevel@tonic-gate 		ret = des_cipher_update_iov(&des_ctx, ciphertext, plaintext,
11980Sstevel@tonic-gate 		    des_decrypt_contiguous_blocks);
11990Sstevel@tonic-gate 		break;
12000Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
12010Sstevel@tonic-gate 		ret = des_cipher_update_uio(&des_ctx, ciphertext, plaintext,
12020Sstevel@tonic-gate 		    des_decrypt_contiguous_blocks);
12030Sstevel@tonic-gate 		break;
12040Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
12050Sstevel@tonic-gate 		ret = des_cipher_update_mp(&des_ctx, ciphertext, plaintext,
12060Sstevel@tonic-gate 		    des_decrypt_contiguous_blocks);
12070Sstevel@tonic-gate 		break;
12080Sstevel@tonic-gate 	default:
12090Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
12100Sstevel@tonic-gate 	}
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate 	if (des_ctx.dc_flags & DES_PROVIDER_OWNS_KEY_SCHEDULE) {
12130Sstevel@tonic-gate 		bzero(des_ctx.dc_keysched, des_ctx.dc_keysched_len);
12140Sstevel@tonic-gate 		kmem_free(des_ctx.dc_keysched, des_ctx.dc_keysched_len);
12150Sstevel@tonic-gate 	}
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
12180Sstevel@tonic-gate 		ASSERT(des_ctx.dc_remainder_len == 0);
12190Sstevel@tonic-gate 		if (ciphertext != plaintext)
12200Sstevel@tonic-gate 			plaintext->cd_length =
12210Sstevel@tonic-gate 			    plaintext->cd_offset - saved_offset;
12220Sstevel@tonic-gate 	} else {
12230Sstevel@tonic-gate 		plaintext->cd_length = saved_length;
12240Sstevel@tonic-gate 	}
12250Sstevel@tonic-gate 	plaintext->cd_offset = saved_offset;
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate /* EXPORT DELETE END */
12280Sstevel@tonic-gate 
12290Sstevel@tonic-gate 	/* LINTED */
12300Sstevel@tonic-gate 	return (ret);
12310Sstevel@tonic-gate }
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate /*
12340Sstevel@tonic-gate  * KCF software provider context template entry points.
12350Sstevel@tonic-gate  */
12360Sstevel@tonic-gate /* ARGSUSED */
12370Sstevel@tonic-gate static int
12380Sstevel@tonic-gate des_create_ctx_template(crypto_provider_handle_t provider,
12390Sstevel@tonic-gate     crypto_mechanism_t *mechanism, crypto_key_t *key,
12400Sstevel@tonic-gate     crypto_spi_ctx_template_t *tmpl, size_t *tmpl_size, crypto_req_handle_t req)
12410Sstevel@tonic-gate {
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate /* EXPORT DELETE START */
12440Sstevel@tonic-gate 
12450Sstevel@tonic-gate 	des_strength_t strength;
12460Sstevel@tonic-gate 	void *keysched;
12470Sstevel@tonic-gate 	size_t size;
12480Sstevel@tonic-gate 	int rv;
12490Sstevel@tonic-gate 
12500Sstevel@tonic-gate 	switch (mechanism->cm_type) {
12510Sstevel@tonic-gate 	case DES_ECB_MECH_INFO_TYPE:
12520Sstevel@tonic-gate 		strength = DES;
12530Sstevel@tonic-gate 		break;
12540Sstevel@tonic-gate 	case DES_CBC_MECH_INFO_TYPE:
12550Sstevel@tonic-gate 		strength = DES;
12560Sstevel@tonic-gate 		break;
12570Sstevel@tonic-gate 	case DES3_ECB_MECH_INFO_TYPE:
12580Sstevel@tonic-gate 		strength = DES3;
12590Sstevel@tonic-gate 		break;
12600Sstevel@tonic-gate 	case DES3_CBC_MECH_INFO_TYPE:
12610Sstevel@tonic-gate 		strength = DES3;
12620Sstevel@tonic-gate 		break;
12630Sstevel@tonic-gate 	default:
12640Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
12650Sstevel@tonic-gate 	}
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 	if ((keysched = des_alloc_keysched(&size, strength,
12680Sstevel@tonic-gate 	    crypto_kmflag(req))) == NULL) {
12690Sstevel@tonic-gate 		return (CRYPTO_HOST_MEMORY);
12700Sstevel@tonic-gate 	}
12710Sstevel@tonic-gate 
12720Sstevel@tonic-gate 	/*
12730Sstevel@tonic-gate 	 * Initialize key schedule.  Key length information is stored
12740Sstevel@tonic-gate 	 * in the key.
12750Sstevel@tonic-gate 	 */
12760Sstevel@tonic-gate 	if ((rv = init_keysched(key, keysched, strength)) != CRYPTO_SUCCESS) {
12770Sstevel@tonic-gate 		bzero(keysched, size);
12780Sstevel@tonic-gate 		kmem_free(keysched, size);
12790Sstevel@tonic-gate 		return (rv);
12800Sstevel@tonic-gate 	}
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate 	*tmpl = keysched;
12830Sstevel@tonic-gate 	*tmpl_size = size;
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate /* EXPORT DELETE END */
12860Sstevel@tonic-gate 
12870Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
12880Sstevel@tonic-gate }
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate /* ARGSUSED */
12910Sstevel@tonic-gate static int
12920Sstevel@tonic-gate des_free_context(crypto_ctx_t *ctx)
12930Sstevel@tonic-gate {
12940Sstevel@tonic-gate 
12950Sstevel@tonic-gate /* EXPORT DELETE START */
12960Sstevel@tonic-gate 
12970Sstevel@tonic-gate 	des_ctx_t *des_ctx = ctx->cc_provider_private;
12980Sstevel@tonic-gate 
12990Sstevel@tonic-gate 	if (des_ctx != NULL) {
13000Sstevel@tonic-gate 		if (des_ctx->dc_flags & DES_PROVIDER_OWNS_KEY_SCHEDULE) {
13010Sstevel@tonic-gate 			ASSERT(des_ctx->dc_keysched_len != 0);
13020Sstevel@tonic-gate 			bzero(des_ctx->dc_keysched, des_ctx->dc_keysched_len);
13030Sstevel@tonic-gate 			kmem_free(des_ctx->dc_keysched,
13040Sstevel@tonic-gate 			    des_ctx->dc_keysched_len);
13050Sstevel@tonic-gate 		}
13060Sstevel@tonic-gate 		kmem_free(des_ctx, sizeof (des_ctx_t));
13070Sstevel@tonic-gate 		ctx->cc_provider_private = NULL;
13080Sstevel@tonic-gate 	}
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate /* EXPORT DELETE END */
13110Sstevel@tonic-gate 
13120Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
13130Sstevel@tonic-gate }
13140Sstevel@tonic-gate 
13150Sstevel@tonic-gate /*
13160Sstevel@tonic-gate  * Pass it to des_keycheck() which will
13170Sstevel@tonic-gate  * fix it (parity bits), and check if the fixed key is weak.
13180Sstevel@tonic-gate  */
13190Sstevel@tonic-gate /* ARGSUSED */
13200Sstevel@tonic-gate static int
13210Sstevel@tonic-gate des_key_check(crypto_provider_handle_t pd, crypto_mechanism_t *mech,
13220Sstevel@tonic-gate     crypto_key_t *key)
13230Sstevel@tonic-gate {
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate /* EXPORT DELETE START */
13260Sstevel@tonic-gate 
13270Sstevel@tonic-gate 	int expectedkeylen;
13280Sstevel@tonic-gate 	des_strength_t strength;
13290Sstevel@tonic-gate 	uint8_t keydata[DES3_MAX_KEY_LEN];
13300Sstevel@tonic-gate 
13310Sstevel@tonic-gate 	if ((mech == NULL) || (key == NULL))
13320Sstevel@tonic-gate 		return (CRYPTO_ARGUMENTS_BAD);
13330Sstevel@tonic-gate 
13340Sstevel@tonic-gate 	switch (mech->cm_type) {
13350Sstevel@tonic-gate 	case DES_ECB_MECH_INFO_TYPE:
13360Sstevel@tonic-gate 	case DES_CBC_MECH_INFO_TYPE:
13370Sstevel@tonic-gate 		expectedkeylen = DES_MINBITS;
13380Sstevel@tonic-gate 		strength = DES;
13390Sstevel@tonic-gate 		break;
13400Sstevel@tonic-gate 	case DES3_ECB_MECH_INFO_TYPE:
13410Sstevel@tonic-gate 	case DES3_CBC_MECH_INFO_TYPE:
13420Sstevel@tonic-gate 		expectedkeylen = DES3_MINBITS;
13430Sstevel@tonic-gate 		strength = DES3;
13440Sstevel@tonic-gate 		break;
13450Sstevel@tonic-gate 	default:
13460Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
13470Sstevel@tonic-gate 	}
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate 	if (key->ck_format != CRYPTO_KEY_RAW)
13500Sstevel@tonic-gate 		return (CRYPTO_KEY_TYPE_INCONSISTENT);
13510Sstevel@tonic-gate 
13520Sstevel@tonic-gate 	if (key->ck_length != expectedkeylen)
13530Sstevel@tonic-gate 		return (CRYPTO_KEY_SIZE_RANGE);
13540Sstevel@tonic-gate 
13550Sstevel@tonic-gate 	bcopy(key->ck_data, keydata, CRYPTO_BITS2BYTES(expectedkeylen));
13560Sstevel@tonic-gate 
13570Sstevel@tonic-gate 	if (des_keycheck(keydata, strength, key->ck_data) == B_FALSE)
13580Sstevel@tonic-gate 		return (CRYPTO_WEAK_KEY);
13590Sstevel@tonic-gate 
13600Sstevel@tonic-gate /* EXPORT DELETE END */
13610Sstevel@tonic-gate 
13620Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
13630Sstevel@tonic-gate }
13640Sstevel@tonic-gate 
13650Sstevel@tonic-gate /* ARGSUSED */
13660Sstevel@tonic-gate static int
13670Sstevel@tonic-gate des_common_init_ctx(des_ctx_t *des_ctx, crypto_spi_ctx_template_t *template,
13680Sstevel@tonic-gate     crypto_mechanism_t *mechanism, crypto_key_t *key, des_strength_t strength,
13690Sstevel@tonic-gate     int kmflag)
13700Sstevel@tonic-gate {
13710Sstevel@tonic-gate 	int rv = CRYPTO_SUCCESS;
13720Sstevel@tonic-gate 
13730Sstevel@tonic-gate /* EXPORT DELETE START */
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate 	void *keysched;
13760Sstevel@tonic-gate 	size_t size;
13770Sstevel@tonic-gate 
13780Sstevel@tonic-gate 	if (template == NULL) {
13790Sstevel@tonic-gate 		if ((keysched = des_alloc_keysched(&size, strength,
13800Sstevel@tonic-gate 		    kmflag)) == NULL)
13810Sstevel@tonic-gate 			return (CRYPTO_HOST_MEMORY);
13820Sstevel@tonic-gate 		/*
13830Sstevel@tonic-gate 		 * Initialize key schedule.
13840Sstevel@tonic-gate 		 * Key length is stored in the key.
13850Sstevel@tonic-gate 		 */
13860Sstevel@tonic-gate 		if ((rv = init_keysched(key, keysched,
13870Sstevel@tonic-gate 		    strength)) != CRYPTO_SUCCESS)
13880Sstevel@tonic-gate 			kmem_free(keysched, size);
13890Sstevel@tonic-gate 
13900Sstevel@tonic-gate 		des_ctx->dc_flags = DES_PROVIDER_OWNS_KEY_SCHEDULE;
13910Sstevel@tonic-gate 		des_ctx->dc_keysched_len = size;
13920Sstevel@tonic-gate 	} else {
13930Sstevel@tonic-gate 		keysched = template;
13940Sstevel@tonic-gate 	}
13950Sstevel@tonic-gate 
13960Sstevel@tonic-gate 	if (strength == DES3) {
13970Sstevel@tonic-gate 		des_ctx->dc_flags |= DES3_STRENGTH;
13980Sstevel@tonic-gate 	}
13990Sstevel@tonic-gate 
14000Sstevel@tonic-gate 	if (mechanism->cm_type == DES_CBC_MECH_INFO_TYPE ||
14010Sstevel@tonic-gate 	    mechanism->cm_type == DES3_CBC_MECH_INFO_TYPE) {
14020Sstevel@tonic-gate 		/*
14030Sstevel@tonic-gate 		 * Copy IV into DES context.
14040Sstevel@tonic-gate 		 *
14050Sstevel@tonic-gate 		 * If cm_param == NULL then the IV comes from the
14060Sstevel@tonic-gate 		 * cd_miscdata field in the crypto_data structure.
14070Sstevel@tonic-gate 		 */
14080Sstevel@tonic-gate 		if (mechanism->cm_param != NULL) {
14090Sstevel@tonic-gate 			ASSERT(mechanism->cm_param_len == DES_BLOCK_LEN);
14100Sstevel@tonic-gate 			if (IS_P2ALIGNED(mechanism->cm_param,
14110Sstevel@tonic-gate 			    sizeof (uint64_t))) {
14120Sstevel@tonic-gate 				/* LINTED: pointer alignment */
14130Sstevel@tonic-gate 				des_ctx->dc_iv =
14140Sstevel@tonic-gate 				    *(uint64_t *)mechanism->cm_param;
14150Sstevel@tonic-gate 			} else {
14160Sstevel@tonic-gate 				uint64_t tmp64;
14170Sstevel@tonic-gate 				uint8_t *tmp = (uint8_t *)mechanism->cm_param;
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate #ifdef _BIG_ENDIAN
14200Sstevel@tonic-gate 				tmp64 = (((uint64_t)tmp[0] << 56) |
14210Sstevel@tonic-gate 				    ((uint64_t)tmp[1] << 48) |
14220Sstevel@tonic-gate 				    ((uint64_t)tmp[2] << 40) |
14230Sstevel@tonic-gate 				    ((uint64_t)tmp[3] << 32) |
14240Sstevel@tonic-gate 				    ((uint64_t)tmp[4] << 24) |
14250Sstevel@tonic-gate 				    ((uint64_t)tmp[5] << 16) |
14260Sstevel@tonic-gate 				    ((uint64_t)tmp[6] << 8) |
14270Sstevel@tonic-gate 				    (uint64_t)tmp[7]);
14280Sstevel@tonic-gate #else
14290Sstevel@tonic-gate 				tmp64 = (((uint64_t)tmp[7] << 56) |
14300Sstevel@tonic-gate 				    ((uint64_t)tmp[6] << 48) |
14310Sstevel@tonic-gate 				    ((uint64_t)tmp[5] << 40) |
14320Sstevel@tonic-gate 				    ((uint64_t)tmp[4] << 32) |
14330Sstevel@tonic-gate 				    ((uint64_t)tmp[3] << 24) |
14340Sstevel@tonic-gate 				    ((uint64_t)tmp[2] << 16) |
14350Sstevel@tonic-gate 				    ((uint64_t)tmp[1] << 8) |
14360Sstevel@tonic-gate 				    (uint64_t)tmp[0]);
14370Sstevel@tonic-gate #endif /* _BIG_ENDIAN */
14380Sstevel@tonic-gate 
14390Sstevel@tonic-gate 				des_ctx->dc_iv = tmp64;
14400Sstevel@tonic-gate 			}
14410Sstevel@tonic-gate 		}
14420Sstevel@tonic-gate 
14430Sstevel@tonic-gate 		des_ctx->dc_lastp = (uint8_t *)&des_ctx->dc_iv;
14440Sstevel@tonic-gate 		des_ctx->dc_flags |= DES_CBC_MODE;
14450Sstevel@tonic-gate 	}
14460Sstevel@tonic-gate 	des_ctx->dc_keysched = keysched;
14470Sstevel@tonic-gate 
14480Sstevel@tonic-gate /* EXPORT DELETE END */
14490Sstevel@tonic-gate 
14500Sstevel@tonic-gate 	return (rv);
14510Sstevel@tonic-gate }
1452