xref: /onnv-gate/usr/src/uts/common/des/des_crypt.c (revision 2439:40447ed85d31)
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
5*2439Sizick  * Common Development and Distribution License (the "License").
6*2439Sizick  * 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*2439Sizick  * Copyright 2006 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,
890Sstevel@tonic-gate 	"DES Kernel SW Provider %I%"
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 	 */
433*2439Sizick 	if (key->ck_data == NULL)
434*2439Sizick 		return (CRYPTO_ARGUMENTS_BAD);
4350Sstevel@tonic-gate 
436*2439Sizick 	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;
6230Sstevel@tonic-gate 	    offset -= uiop->uio_iov[vec_idx++].iov_len);
6240Sstevel@tonic-gate 	if (vec_idx == uiop->uio_iovcnt) {
6250Sstevel@tonic-gate 		/*
6260Sstevel@tonic-gate 		 * The caller specified an offset that is larger than the
6270Sstevel@tonic-gate 		 * total size of the buffers it provided.
6280Sstevel@tonic-gate 		 */
6290Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
6300Sstevel@tonic-gate 	}
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 	/*
6330Sstevel@tonic-gate 	 * Now process the iovecs.
6340Sstevel@tonic-gate 	 */
6350Sstevel@tonic-gate 	while (vec_idx < uiop->uio_iovcnt && length > 0) {
6360Sstevel@tonic-gate 		cur_len = MIN(uiop->uio_iov[vec_idx].iov_len -
6370Sstevel@tonic-gate 		    offset, length);
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 		(cipher)(des_ctx, uiop->uio_iov[vec_idx].iov_base + offset,
6400Sstevel@tonic-gate 		    cur_len, (input == output) ? NULL : output);
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 		length -= cur_len;
6430Sstevel@tonic-gate 		vec_idx++;
6440Sstevel@tonic-gate 		offset = 0;
6450Sstevel@tonic-gate 	}
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 	if (vec_idx == uiop->uio_iovcnt && length > 0) {
6480Sstevel@tonic-gate 		/*
6490Sstevel@tonic-gate 		 * The end of the specified iovec's was reached but
6500Sstevel@tonic-gate 		 * the length requested could not be processed, i.e.
6510Sstevel@tonic-gate 		 * The caller requested to digest more data than it provided.
6520Sstevel@tonic-gate 		 */
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
6550Sstevel@tonic-gate 	}
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
6580Sstevel@tonic-gate }
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate /*
6610Sstevel@tonic-gate  * Helper DES encrypt update function for mblk input data.
6620Sstevel@tonic-gate  */
6630Sstevel@tonic-gate static int
6640Sstevel@tonic-gate des_cipher_update_mp(des_ctx_t *des_ctx, crypto_data_t *input,
6650Sstevel@tonic-gate     crypto_data_t *output, int (*cipher)(des_ctx_t *, caddr_t, size_t,
6660Sstevel@tonic-gate     crypto_data_t *))
6670Sstevel@tonic-gate {
6680Sstevel@tonic-gate 	off_t offset = input->cd_offset;
6690Sstevel@tonic-gate 	size_t length = input->cd_length;
6700Sstevel@tonic-gate 	mblk_t *mp;
6710Sstevel@tonic-gate 	size_t cur_len;
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	if (input->cd_miscdata != NULL) {
6740Sstevel@tonic-gate 		if (IS_P2ALIGNED(input->cd_miscdata, sizeof (uint64_t))) {
6750Sstevel@tonic-gate 			/* LINTED: pointer alignment */
6760Sstevel@tonic-gate 			des_ctx->dc_iv = *(uint64_t *)input->cd_miscdata;
6770Sstevel@tonic-gate 		} else {
6780Sstevel@tonic-gate 			uint64_t tmp64;
6790Sstevel@tonic-gate 			uint8_t *tmp = (uint8_t *)input->cd_miscdata;
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate #ifdef _BIG_ENDIAN
6820Sstevel@tonic-gate 			tmp64 = (((uint64_t)tmp[0] << 56) |
6830Sstevel@tonic-gate 			    ((uint64_t)tmp[1] << 48) |
6840Sstevel@tonic-gate 			    ((uint64_t)tmp[2] << 40) |
6850Sstevel@tonic-gate 			    ((uint64_t)tmp[3] << 32) |
6860Sstevel@tonic-gate 			    ((uint64_t)tmp[4] << 24) |
6870Sstevel@tonic-gate 			    ((uint64_t)tmp[5] << 16) |
6880Sstevel@tonic-gate 			    ((uint64_t)tmp[6] << 8) |
6890Sstevel@tonic-gate 			    (uint64_t)tmp[7]);
6900Sstevel@tonic-gate #else
6910Sstevel@tonic-gate 			tmp64 = (((uint64_t)tmp[7] << 56) |
6920Sstevel@tonic-gate 			    ((uint64_t)tmp[6] << 48) |
6930Sstevel@tonic-gate 			    ((uint64_t)tmp[5] << 40) |
6940Sstevel@tonic-gate 			    ((uint64_t)tmp[4] << 32) |
6950Sstevel@tonic-gate 			    ((uint64_t)tmp[3] << 24) |
6960Sstevel@tonic-gate 			    ((uint64_t)tmp[2] << 16) |
6970Sstevel@tonic-gate 			    ((uint64_t)tmp[1] << 8) |
6980Sstevel@tonic-gate 			    (uint64_t)tmp[0]);
6990Sstevel@tonic-gate #endif /* _BIG_ENDIAN */
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 			des_ctx->dc_iv = tmp64;
7020Sstevel@tonic-gate 		}
7030Sstevel@tonic-gate 	}
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 	/*
7060Sstevel@tonic-gate 	 * Jump to the first mblk_t containing data to be processed.
7070Sstevel@tonic-gate 	 */
7080Sstevel@tonic-gate 	for (mp = input->cd_mp; mp != NULL && offset >= MBLKL(mp);
7090Sstevel@tonic-gate 	    offset -= MBLKL(mp), mp = mp->b_cont);
7100Sstevel@tonic-gate 	if (mp == NULL) {
7110Sstevel@tonic-gate 		/*
7120Sstevel@tonic-gate 		 * The caller specified an offset that is larger than the
7130Sstevel@tonic-gate 		 * total size of the buffers it provided.
7140Sstevel@tonic-gate 		 */
7150Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
7160Sstevel@tonic-gate 	}
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	/*
7190Sstevel@tonic-gate 	 * Now do the processing on the mblk chain.
7200Sstevel@tonic-gate 	 */
7210Sstevel@tonic-gate 	while (mp != NULL && length > 0) {
7220Sstevel@tonic-gate 		cur_len = MIN(MBLKL(mp) - offset, length);
7230Sstevel@tonic-gate 		(cipher)(des_ctx, (char *)(mp->b_rptr + offset), cur_len,
7240Sstevel@tonic-gate 		    (input == output) ? NULL : output);
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 		length -= cur_len;
7270Sstevel@tonic-gate 		offset = 0;
7280Sstevel@tonic-gate 		mp = mp->b_cont;
7290Sstevel@tonic-gate 	}
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 	if (mp == NULL && length > 0) {
7320Sstevel@tonic-gate 		/*
7330Sstevel@tonic-gate 		 * The end of the mblk was reached but the length requested
7340Sstevel@tonic-gate 		 * could not be processed, i.e. The caller requested
7350Sstevel@tonic-gate 		 * to digest more data than it provided.
7360Sstevel@tonic-gate 		 */
7370Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
7380Sstevel@tonic-gate 	}
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
7410Sstevel@tonic-gate }
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate /* ARGSUSED */
7440Sstevel@tonic-gate static int
7450Sstevel@tonic-gate des_encrypt(crypto_ctx_t *ctx, crypto_data_t *plaintext,
7460Sstevel@tonic-gate     crypto_data_t *ciphertext, crypto_req_handle_t req)
7470Sstevel@tonic-gate {
7480Sstevel@tonic-gate 	int ret;
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate /* EXPORT DELETE START */
7510Sstevel@tonic-gate 	des_ctx_t *des_ctx;
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	/*
7540Sstevel@tonic-gate 	 * Plaintext must be a multiple of the block size.
7550Sstevel@tonic-gate 	 * This test only works for non-padded mechanisms
7560Sstevel@tonic-gate 	 * when blocksize is 2^N.
7570Sstevel@tonic-gate 	 */
7580Sstevel@tonic-gate 	if ((plaintext->cd_length & (DES_BLOCK_LEN - 1)) != 0)
7590Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
7620Sstevel@tonic-gate 	des_ctx = ctx->cc_provider_private;
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	DES_ARG_INPLACE(plaintext, ciphertext);
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 	/*
7670Sstevel@tonic-gate 	 * We need to just return the length needed to store the output.
7680Sstevel@tonic-gate 	 * We should not destroy the context for the following case.
7690Sstevel@tonic-gate 	 */
7700Sstevel@tonic-gate 	if (ciphertext->cd_length < plaintext->cd_length) {
7710Sstevel@tonic-gate 		ciphertext->cd_length = plaintext->cd_length;
7720Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
7730Sstevel@tonic-gate 	}
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	/*
7760Sstevel@tonic-gate 	 * Do an update on the specified input data.
7770Sstevel@tonic-gate 	 */
7780Sstevel@tonic-gate 	ret = des_encrypt_update(ctx, plaintext, ciphertext, req);
7790Sstevel@tonic-gate 	ASSERT(des_ctx->dc_remainder_len == 0);
7800Sstevel@tonic-gate 	(void) des_free_context(ctx);
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate /* EXPORT DELETE END */
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 	/* LINTED */
7850Sstevel@tonic-gate 	return (ret);
7860Sstevel@tonic-gate }
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate /* ARGSUSED */
7890Sstevel@tonic-gate static int
7900Sstevel@tonic-gate des_decrypt(crypto_ctx_t *ctx, crypto_data_t *ciphertext,
7910Sstevel@tonic-gate     crypto_data_t *plaintext, crypto_req_handle_t req)
7920Sstevel@tonic-gate {
7930Sstevel@tonic-gate 	int ret;
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate /* EXPORT DELETE START */
7960Sstevel@tonic-gate 	des_ctx_t *des_ctx;
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	/*
7990Sstevel@tonic-gate 	 * Ciphertext must be a multiple of the block size.
8000Sstevel@tonic-gate 	 * This test only works for non-padded mechanisms
8010Sstevel@tonic-gate 	 * when blocksize is 2^N.
8020Sstevel@tonic-gate 	 */
8030Sstevel@tonic-gate 	if ((ciphertext->cd_length & (DES_BLOCK_LEN - 1)) != 0)
8040Sstevel@tonic-gate 		return (CRYPTO_ENCRYPTED_DATA_LEN_RANGE);
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
8070Sstevel@tonic-gate 	des_ctx = ctx->cc_provider_private;
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	DES_ARG_INPLACE(ciphertext, plaintext);
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 	/*
8120Sstevel@tonic-gate 	 * We need to just return the length needed to store the output.
8130Sstevel@tonic-gate 	 * We should not destroy the context for the following case.
8140Sstevel@tonic-gate 	 */
8150Sstevel@tonic-gate 	if (plaintext->cd_length < ciphertext->cd_length) {
8160Sstevel@tonic-gate 		plaintext->cd_length = ciphertext->cd_length;
8170Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
8180Sstevel@tonic-gate 	}
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 	/*
8210Sstevel@tonic-gate 	 * Do an update on the specified input data.
8220Sstevel@tonic-gate 	 */
8230Sstevel@tonic-gate 	ret = des_decrypt_update(ctx, ciphertext, plaintext, req);
8240Sstevel@tonic-gate 	ASSERT(des_ctx->dc_remainder_len == 0);
8250Sstevel@tonic-gate 	(void) des_free_context(ctx);
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate /* EXPORT DELETE END */
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 	/* LINTED */
8300Sstevel@tonic-gate 	return (ret);
8310Sstevel@tonic-gate }
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate /* ARGSUSED */
8340Sstevel@tonic-gate static int
8350Sstevel@tonic-gate des_encrypt_update(crypto_ctx_t *ctx, crypto_data_t *plaintext,
8360Sstevel@tonic-gate     crypto_data_t *ciphertext, crypto_req_handle_t req)
8370Sstevel@tonic-gate {
8380Sstevel@tonic-gate 	off_t saved_offset;
8390Sstevel@tonic-gate 	size_t saved_length, out_len;
8400Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate /* EXPORT DELETE START */
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 	DES_ARG_INPLACE(plaintext, ciphertext);
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	/* compute number of bytes that will hold the ciphertext */
8490Sstevel@tonic-gate 	out_len = ((des_ctx_t *)ctx->cc_provider_private)->dc_remainder_len;
8500Sstevel@tonic-gate 	out_len += plaintext->cd_length;
8510Sstevel@tonic-gate 	out_len &= ~(DES_BLOCK_LEN - 1);
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 	/* return length needed to store the output */
8540Sstevel@tonic-gate 	if (ciphertext->cd_length < out_len) {
8550Sstevel@tonic-gate 		ciphertext->cd_length = out_len;
8560Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
8570Sstevel@tonic-gate 	}
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 	saved_offset = ciphertext->cd_offset;
8600Sstevel@tonic-gate 	saved_length = ciphertext->cd_length;
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 	/*
8630Sstevel@tonic-gate 	 * Do the DES update on the specified input data.
8640Sstevel@tonic-gate 	 */
8650Sstevel@tonic-gate 	switch (plaintext->cd_format) {
8660Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
8670Sstevel@tonic-gate 		ret = des_cipher_update_iov(ctx->cc_provider_private,
8680Sstevel@tonic-gate 		    plaintext, ciphertext, des_encrypt_contiguous_blocks);
8690Sstevel@tonic-gate 		break;
8700Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
8710Sstevel@tonic-gate 		ret = des_cipher_update_uio(ctx->cc_provider_private,
8720Sstevel@tonic-gate 		    plaintext, ciphertext, des_encrypt_contiguous_blocks);
8730Sstevel@tonic-gate 		break;
8740Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
8750Sstevel@tonic-gate 		ret = des_cipher_update_mp(ctx->cc_provider_private,
8760Sstevel@tonic-gate 		    plaintext, ciphertext, des_encrypt_contiguous_blocks);
8770Sstevel@tonic-gate 		break;
8780Sstevel@tonic-gate 	default:
8790Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
8800Sstevel@tonic-gate 	}
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
8830Sstevel@tonic-gate 		if (plaintext != ciphertext)
8840Sstevel@tonic-gate 			ciphertext->cd_length =
8850Sstevel@tonic-gate 			    ciphertext->cd_offset - saved_offset;
8860Sstevel@tonic-gate 	} else {
8870Sstevel@tonic-gate 		ciphertext->cd_length = saved_length;
8880Sstevel@tonic-gate 	}
8890Sstevel@tonic-gate 	ciphertext->cd_offset = saved_offset;
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate /* EXPORT DELETE END */
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate 	return (ret);
8940Sstevel@tonic-gate }
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate /* ARGSUSED */
8970Sstevel@tonic-gate static int
8980Sstevel@tonic-gate des_decrypt_update(crypto_ctx_t *ctx, crypto_data_t *ciphertext,
8990Sstevel@tonic-gate     crypto_data_t *plaintext, crypto_req_handle_t req)
9000Sstevel@tonic-gate {
9010Sstevel@tonic-gate 	off_t saved_offset;
9020Sstevel@tonic-gate 	size_t saved_length, out_len;
9030Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate /* EXPORT DELETE START */
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 	DES_ARG_INPLACE(ciphertext, plaintext);
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate 	/* compute number of bytes that will hold the plaintext */
9120Sstevel@tonic-gate 	out_len = ((des_ctx_t *)ctx->cc_provider_private)->dc_remainder_len;
9130Sstevel@tonic-gate 	out_len += ciphertext->cd_length;
9140Sstevel@tonic-gate 	out_len &= ~(DES_BLOCK_LEN - 1);
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 	/* return length needed to store the output */
9170Sstevel@tonic-gate 	if (plaintext->cd_length < out_len) {
9180Sstevel@tonic-gate 		plaintext->cd_length = out_len;
9190Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
9200Sstevel@tonic-gate 	}
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate 	saved_offset = plaintext->cd_offset;
9230Sstevel@tonic-gate 	saved_length = plaintext->cd_length;
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate 	/*
9260Sstevel@tonic-gate 	 * Do the DES update on the specified input data.
9270Sstevel@tonic-gate 	 */
9280Sstevel@tonic-gate 	switch (ciphertext->cd_format) {
9290Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
9300Sstevel@tonic-gate 		ret = des_cipher_update_iov(ctx->cc_provider_private,
9310Sstevel@tonic-gate 		    ciphertext, plaintext, des_decrypt_contiguous_blocks);
9320Sstevel@tonic-gate 		break;
9330Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
9340Sstevel@tonic-gate 		ret = des_cipher_update_uio(ctx->cc_provider_private,
9350Sstevel@tonic-gate 		    ciphertext, plaintext, des_decrypt_contiguous_blocks);
9360Sstevel@tonic-gate 		break;
9370Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
9380Sstevel@tonic-gate 		ret = des_cipher_update_mp(ctx->cc_provider_private,
9390Sstevel@tonic-gate 		    ciphertext, plaintext, des_decrypt_contiguous_blocks);
9400Sstevel@tonic-gate 		break;
9410Sstevel@tonic-gate 	default:
9420Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
9430Sstevel@tonic-gate 	}
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
9460Sstevel@tonic-gate 		if (ciphertext != plaintext)
9470Sstevel@tonic-gate 			plaintext->cd_length =
9480Sstevel@tonic-gate 			    plaintext->cd_offset - saved_offset;
9490Sstevel@tonic-gate 	} else {
9500Sstevel@tonic-gate 		plaintext->cd_length = saved_length;
9510Sstevel@tonic-gate 	}
9520Sstevel@tonic-gate 	plaintext->cd_offset = saved_offset;
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate /* EXPORT DELETE END */
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 	return (ret);
9570Sstevel@tonic-gate }
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate /* ARGSUSED */
9600Sstevel@tonic-gate static int
9610Sstevel@tonic-gate des_encrypt_final(crypto_ctx_t *ctx, crypto_data_t *ciphertext,
9620Sstevel@tonic-gate     crypto_req_handle_t req)
9630Sstevel@tonic-gate {
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate /* EXPORT DELETE START */
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate 	des_ctx_t *des_ctx;
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
9700Sstevel@tonic-gate 	des_ctx = ctx->cc_provider_private;
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 	/*
9730Sstevel@tonic-gate 	 * There must be no unprocessed plaintext.
9740Sstevel@tonic-gate 	 * This happens if the length of the last data is
9750Sstevel@tonic-gate 	 * not a multiple of the DES block length.
9760Sstevel@tonic-gate 	 */
9770Sstevel@tonic-gate 	if (des_ctx->dc_remainder_len > 0)
9780Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 	(void) des_free_context(ctx);
9810Sstevel@tonic-gate 	ciphertext->cd_length = 0;
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate /* EXPORT DELETE END */
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
9860Sstevel@tonic-gate }
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate /* ARGSUSED */
9890Sstevel@tonic-gate static int
9900Sstevel@tonic-gate des_decrypt_final(crypto_ctx_t *ctx, crypto_data_t *plaintext,
9910Sstevel@tonic-gate     crypto_req_handle_t req)
9920Sstevel@tonic-gate {
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate /* EXPORT DELETE START */
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 	des_ctx_t *des_ctx;
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
9990Sstevel@tonic-gate 	des_ctx = ctx->cc_provider_private;
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate 	/*
10020Sstevel@tonic-gate 	 * There must be no unprocessed ciphertext.
10030Sstevel@tonic-gate 	 * This happens if the length of the last ciphertext is
10040Sstevel@tonic-gate 	 * not a multiple of the DES block length.
10050Sstevel@tonic-gate 	 */
10060Sstevel@tonic-gate 	if (des_ctx->dc_remainder_len > 0)
10070Sstevel@tonic-gate 		return (CRYPTO_ENCRYPTED_DATA_LEN_RANGE);
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate 	(void) des_free_context(ctx);
10100Sstevel@tonic-gate 	plaintext->cd_length = 0;
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate /* EXPORT DELETE END */
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
10150Sstevel@tonic-gate }
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate /* ARGSUSED */
10180Sstevel@tonic-gate static int
10190Sstevel@tonic-gate des_encrypt_atomic(crypto_provider_handle_t provider,
10200Sstevel@tonic-gate     crypto_session_id_t session_id, crypto_mechanism_t *mechanism,
10210Sstevel@tonic-gate     crypto_key_t *key, crypto_data_t *plaintext, crypto_data_t *ciphertext,
10220Sstevel@tonic-gate     crypto_spi_ctx_template_t template, crypto_req_handle_t req)
10230Sstevel@tonic-gate {
10240Sstevel@tonic-gate 	int ret;
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate /* EXPORT DELETE START */
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 	des_ctx_t des_ctx;		/* on the stack */
10290Sstevel@tonic-gate 	des_strength_t strength;
10300Sstevel@tonic-gate 	off_t saved_offset;
10310Sstevel@tonic-gate 	size_t saved_length;
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate 	DES_ARG_INPLACE(plaintext, ciphertext);
10340Sstevel@tonic-gate 
10350Sstevel@tonic-gate 	/*
10360Sstevel@tonic-gate 	 * Plaintext must be a multiple of the block size.
10370Sstevel@tonic-gate 	 * This test only works for non-padded mechanisms
10380Sstevel@tonic-gate 	 * when blocksize is 2^N.
10390Sstevel@tonic-gate 	 */
10400Sstevel@tonic-gate 	if ((plaintext->cd_length & (DES_BLOCK_LEN - 1)) != 0)
10410Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 	/* return length needed to store the output */
10440Sstevel@tonic-gate 	if (ciphertext->cd_length < plaintext->cd_length) {
10450Sstevel@tonic-gate 		ciphertext->cd_length = plaintext->cd_length;
10460Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
10470Sstevel@tonic-gate 	}
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate 	/* Check mechanism type and parameter length */
10500Sstevel@tonic-gate 	switch (mechanism->cm_type) {
10510Sstevel@tonic-gate 	case DES_ECB_MECH_INFO_TYPE:
10520Sstevel@tonic-gate 	case DES_CBC_MECH_INFO_TYPE:
10530Sstevel@tonic-gate 		if (mechanism->cm_param_len > 0 &&
10540Sstevel@tonic-gate 		    mechanism->cm_param_len != DES_BLOCK_LEN)
10550Sstevel@tonic-gate 			return (CRYPTO_MECHANISM_PARAM_INVALID);
10560Sstevel@tonic-gate 		if (key->ck_length != DES_MINBITS)
10570Sstevel@tonic-gate 			return (CRYPTO_KEY_SIZE_RANGE);
10580Sstevel@tonic-gate 		strength = DES;
10590Sstevel@tonic-gate 		break;
10600Sstevel@tonic-gate 	case DES3_ECB_MECH_INFO_TYPE:
10610Sstevel@tonic-gate 	case DES3_CBC_MECH_INFO_TYPE:
10620Sstevel@tonic-gate 		if (mechanism->cm_param_len > 0 &&
10630Sstevel@tonic-gate 		    mechanism->cm_param_len != DES_BLOCK_LEN)
10640Sstevel@tonic-gate 			return (CRYPTO_MECHANISM_PARAM_INVALID);
10650Sstevel@tonic-gate 		if (key->ck_length != DES3_MINBITS)
10660Sstevel@tonic-gate 			return (CRYPTO_KEY_SIZE_RANGE);
10670Sstevel@tonic-gate 		strength = DES3;
10680Sstevel@tonic-gate 		break;
10690Sstevel@tonic-gate 	default:
10700Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
10710Sstevel@tonic-gate 	}
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate 	bzero(&des_ctx, sizeof (des_ctx_t));
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 	if ((ret = des_common_init_ctx(&des_ctx, template, mechanism, key,
10760Sstevel@tonic-gate 	    strength, crypto_kmflag(req))) != CRYPTO_SUCCESS) {
10770Sstevel@tonic-gate 		return (ret);
10780Sstevel@tonic-gate 	}
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate 	saved_offset = ciphertext->cd_offset;
10810Sstevel@tonic-gate 	saved_length = ciphertext->cd_length;
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate 	/*
10840Sstevel@tonic-gate 	 * Do the update on the specified input data.
10850Sstevel@tonic-gate 	 */
10860Sstevel@tonic-gate 	switch (plaintext->cd_format) {
10870Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
10880Sstevel@tonic-gate 		ret = des_cipher_update_iov(&des_ctx, plaintext, ciphertext,
10890Sstevel@tonic-gate 		    des_encrypt_contiguous_blocks);
10900Sstevel@tonic-gate 		break;
10910Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
10920Sstevel@tonic-gate 		ret = des_cipher_update_uio(&des_ctx, plaintext, ciphertext,
10930Sstevel@tonic-gate 		    des_encrypt_contiguous_blocks);
10940Sstevel@tonic-gate 		break;
10950Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
10960Sstevel@tonic-gate 		ret = des_cipher_update_mp(&des_ctx, plaintext, ciphertext,
10970Sstevel@tonic-gate 		    des_encrypt_contiguous_blocks);
10980Sstevel@tonic-gate 		break;
10990Sstevel@tonic-gate 	default:
11000Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
11010Sstevel@tonic-gate 	}
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate 	if (des_ctx.dc_flags & DES_PROVIDER_OWNS_KEY_SCHEDULE) {
11040Sstevel@tonic-gate 		bzero(des_ctx.dc_keysched, des_ctx.dc_keysched_len);
11050Sstevel@tonic-gate 		kmem_free(des_ctx.dc_keysched, des_ctx.dc_keysched_len);
11060Sstevel@tonic-gate 	}
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
11090Sstevel@tonic-gate 		ASSERT(des_ctx.dc_remainder_len == 0);
11100Sstevel@tonic-gate 		if (plaintext != ciphertext)
11110Sstevel@tonic-gate 			ciphertext->cd_length =
11120Sstevel@tonic-gate 			    ciphertext->cd_offset - saved_offset;
11130Sstevel@tonic-gate 	} else {
11140Sstevel@tonic-gate 		ciphertext->cd_length = saved_length;
11150Sstevel@tonic-gate 	}
11160Sstevel@tonic-gate 	ciphertext->cd_offset = saved_offset;
11170Sstevel@tonic-gate 
11180Sstevel@tonic-gate /* EXPORT DELETE END */
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate 	/* LINTED */
11210Sstevel@tonic-gate 	return (ret);
11220Sstevel@tonic-gate }
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate /* ARGSUSED */
11250Sstevel@tonic-gate static int
11260Sstevel@tonic-gate des_decrypt_atomic(crypto_provider_handle_t provider,
11270Sstevel@tonic-gate     crypto_session_id_t session_id, crypto_mechanism_t *mechanism,
11280Sstevel@tonic-gate     crypto_key_t *key, crypto_data_t *ciphertext, crypto_data_t *plaintext,
11290Sstevel@tonic-gate     crypto_spi_ctx_template_t template, crypto_req_handle_t req)
11300Sstevel@tonic-gate {
11310Sstevel@tonic-gate 	int ret;
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate /* EXPORT DELETE START */
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 	des_ctx_t des_ctx;	/* on the stack */
11360Sstevel@tonic-gate 	des_strength_t strength;
11370Sstevel@tonic-gate 	off_t saved_offset;
11380Sstevel@tonic-gate 	size_t saved_length;
11390Sstevel@tonic-gate 
11400Sstevel@tonic-gate 	DES_ARG_INPLACE(ciphertext, plaintext);
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate 	/*
11430Sstevel@tonic-gate 	 * Ciphertext must be a multiple of the block size.
11440Sstevel@tonic-gate 	 * This test only works for non-padded mechanisms
11450Sstevel@tonic-gate 	 * when blocksize is 2^N.
11460Sstevel@tonic-gate 	 */
11470Sstevel@tonic-gate 	if ((ciphertext->cd_length & (DES_BLOCK_LEN - 1)) != 0)
11480Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate 	/* return length needed to store the output */
11510Sstevel@tonic-gate 	if (plaintext->cd_length < ciphertext->cd_length) {
11520Sstevel@tonic-gate 		plaintext->cd_length = ciphertext->cd_length;
11530Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
11540Sstevel@tonic-gate 	}
11550Sstevel@tonic-gate 
11560Sstevel@tonic-gate 	/* Check mechanism type and parameter length */
11570Sstevel@tonic-gate 	switch (mechanism->cm_type) {
11580Sstevel@tonic-gate 	case DES_ECB_MECH_INFO_TYPE:
11590Sstevel@tonic-gate 	case DES_CBC_MECH_INFO_TYPE:
11600Sstevel@tonic-gate 		if (mechanism->cm_param_len > 0 &&
11610Sstevel@tonic-gate 		    mechanism->cm_param_len != DES_BLOCK_LEN)
11620Sstevel@tonic-gate 			return (CRYPTO_MECHANISM_PARAM_INVALID);
11630Sstevel@tonic-gate 		if (key->ck_length != DES_MINBITS)
11640Sstevel@tonic-gate 			return (CRYPTO_KEY_SIZE_RANGE);
11650Sstevel@tonic-gate 		strength = DES;
11660Sstevel@tonic-gate 		break;
11670Sstevel@tonic-gate 	case DES3_ECB_MECH_INFO_TYPE:
11680Sstevel@tonic-gate 	case DES3_CBC_MECH_INFO_TYPE:
11690Sstevel@tonic-gate 		if (mechanism->cm_param_len > 0 &&
11700Sstevel@tonic-gate 		    mechanism->cm_param_len != DES_BLOCK_LEN)
11710Sstevel@tonic-gate 			return (CRYPTO_MECHANISM_PARAM_INVALID);
11720Sstevel@tonic-gate 		if (key->ck_length != DES3_MINBITS)
11730Sstevel@tonic-gate 			return (CRYPTO_KEY_SIZE_RANGE);
11740Sstevel@tonic-gate 		strength = DES3;
11750Sstevel@tonic-gate 		break;
11760Sstevel@tonic-gate 	default:
11770Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
11780Sstevel@tonic-gate 	}
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate 	bzero(&des_ctx, sizeof (des_ctx_t));
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 	if ((ret = des_common_init_ctx(&des_ctx, template, mechanism, key,
11830Sstevel@tonic-gate 	    strength, crypto_kmflag(req))) != CRYPTO_SUCCESS) {
11840Sstevel@tonic-gate 		return (ret);
11850Sstevel@tonic-gate 	}
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 	saved_offset = plaintext->cd_offset;
11880Sstevel@tonic-gate 	saved_length = plaintext->cd_length;
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 	/*
11910Sstevel@tonic-gate 	 * Do the update on the specified input data.
11920Sstevel@tonic-gate 	 */
11930Sstevel@tonic-gate 	switch (ciphertext->cd_format) {
11940Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
11950Sstevel@tonic-gate 		ret = des_cipher_update_iov(&des_ctx, ciphertext, plaintext,
11960Sstevel@tonic-gate 		    des_decrypt_contiguous_blocks);
11970Sstevel@tonic-gate 		break;
11980Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
11990Sstevel@tonic-gate 		ret = des_cipher_update_uio(&des_ctx, ciphertext, plaintext,
12000Sstevel@tonic-gate 		    des_decrypt_contiguous_blocks);
12010Sstevel@tonic-gate 		break;
12020Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
12030Sstevel@tonic-gate 		ret = des_cipher_update_mp(&des_ctx, ciphertext, plaintext,
12040Sstevel@tonic-gate 		    des_decrypt_contiguous_blocks);
12050Sstevel@tonic-gate 		break;
12060Sstevel@tonic-gate 	default:
12070Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
12080Sstevel@tonic-gate 	}
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate 	if (des_ctx.dc_flags & DES_PROVIDER_OWNS_KEY_SCHEDULE) {
12110Sstevel@tonic-gate 		bzero(des_ctx.dc_keysched, des_ctx.dc_keysched_len);
12120Sstevel@tonic-gate 		kmem_free(des_ctx.dc_keysched, des_ctx.dc_keysched_len);
12130Sstevel@tonic-gate 	}
12140Sstevel@tonic-gate 
12150Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
12160Sstevel@tonic-gate 		ASSERT(des_ctx.dc_remainder_len == 0);
12170Sstevel@tonic-gate 		if (ciphertext != plaintext)
12180Sstevel@tonic-gate 			plaintext->cd_length =
12190Sstevel@tonic-gate 			    plaintext->cd_offset - saved_offset;
12200Sstevel@tonic-gate 	} else {
12210Sstevel@tonic-gate 		plaintext->cd_length = saved_length;
12220Sstevel@tonic-gate 	}
12230Sstevel@tonic-gate 	plaintext->cd_offset = saved_offset;
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate /* EXPORT DELETE END */
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate 	/* LINTED */
12280Sstevel@tonic-gate 	return (ret);
12290Sstevel@tonic-gate }
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate /*
12320Sstevel@tonic-gate  * KCF software provider context template entry points.
12330Sstevel@tonic-gate  */
12340Sstevel@tonic-gate /* ARGSUSED */
12350Sstevel@tonic-gate static int
12360Sstevel@tonic-gate des_create_ctx_template(crypto_provider_handle_t provider,
12370Sstevel@tonic-gate     crypto_mechanism_t *mechanism, crypto_key_t *key,
12380Sstevel@tonic-gate     crypto_spi_ctx_template_t *tmpl, size_t *tmpl_size, crypto_req_handle_t req)
12390Sstevel@tonic-gate {
12400Sstevel@tonic-gate 
12410Sstevel@tonic-gate /* EXPORT DELETE START */
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 	des_strength_t strength;
12440Sstevel@tonic-gate 	void *keysched;
12450Sstevel@tonic-gate 	size_t size;
12460Sstevel@tonic-gate 	int rv;
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate 	switch (mechanism->cm_type) {
12490Sstevel@tonic-gate 	case DES_ECB_MECH_INFO_TYPE:
12500Sstevel@tonic-gate 		strength = DES;
12510Sstevel@tonic-gate 		break;
12520Sstevel@tonic-gate 	case DES_CBC_MECH_INFO_TYPE:
12530Sstevel@tonic-gate 		strength = DES;
12540Sstevel@tonic-gate 		break;
12550Sstevel@tonic-gate 	case DES3_ECB_MECH_INFO_TYPE:
12560Sstevel@tonic-gate 		strength = DES3;
12570Sstevel@tonic-gate 		break;
12580Sstevel@tonic-gate 	case DES3_CBC_MECH_INFO_TYPE:
12590Sstevel@tonic-gate 		strength = DES3;
12600Sstevel@tonic-gate 		break;
12610Sstevel@tonic-gate 	default:
12620Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
12630Sstevel@tonic-gate 	}
12640Sstevel@tonic-gate 
12650Sstevel@tonic-gate 	if ((keysched = des_alloc_keysched(&size, strength,
12660Sstevel@tonic-gate 	    crypto_kmflag(req))) == NULL) {
12670Sstevel@tonic-gate 		return (CRYPTO_HOST_MEMORY);
12680Sstevel@tonic-gate 	}
12690Sstevel@tonic-gate 
12700Sstevel@tonic-gate 	/*
12710Sstevel@tonic-gate 	 * Initialize key schedule.  Key length information is stored
12720Sstevel@tonic-gate 	 * in the key.
12730Sstevel@tonic-gate 	 */
12740Sstevel@tonic-gate 	if ((rv = init_keysched(key, keysched, strength)) != CRYPTO_SUCCESS) {
12750Sstevel@tonic-gate 		bzero(keysched, size);
12760Sstevel@tonic-gate 		kmem_free(keysched, size);
12770Sstevel@tonic-gate 		return (rv);
12780Sstevel@tonic-gate 	}
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate 	*tmpl = keysched;
12810Sstevel@tonic-gate 	*tmpl_size = size;
12820Sstevel@tonic-gate 
12830Sstevel@tonic-gate /* EXPORT DELETE END */
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
12860Sstevel@tonic-gate }
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate /* ARGSUSED */
12890Sstevel@tonic-gate static int
12900Sstevel@tonic-gate des_free_context(crypto_ctx_t *ctx)
12910Sstevel@tonic-gate {
12920Sstevel@tonic-gate 
12930Sstevel@tonic-gate /* EXPORT DELETE START */
12940Sstevel@tonic-gate 
12950Sstevel@tonic-gate 	des_ctx_t *des_ctx = ctx->cc_provider_private;
12960Sstevel@tonic-gate 
12970Sstevel@tonic-gate 	if (des_ctx != NULL) {
12980Sstevel@tonic-gate 		if (des_ctx->dc_flags & DES_PROVIDER_OWNS_KEY_SCHEDULE) {
12990Sstevel@tonic-gate 			ASSERT(des_ctx->dc_keysched_len != 0);
13000Sstevel@tonic-gate 			bzero(des_ctx->dc_keysched, des_ctx->dc_keysched_len);
13010Sstevel@tonic-gate 			kmem_free(des_ctx->dc_keysched,
13020Sstevel@tonic-gate 			    des_ctx->dc_keysched_len);
13030Sstevel@tonic-gate 		}
13040Sstevel@tonic-gate 		kmem_free(des_ctx, sizeof (des_ctx_t));
13050Sstevel@tonic-gate 		ctx->cc_provider_private = NULL;
13060Sstevel@tonic-gate 	}
13070Sstevel@tonic-gate 
13080Sstevel@tonic-gate /* EXPORT DELETE END */
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
13110Sstevel@tonic-gate }
13120Sstevel@tonic-gate 
13130Sstevel@tonic-gate /*
13140Sstevel@tonic-gate  * Pass it to des_keycheck() which will
13150Sstevel@tonic-gate  * fix it (parity bits), and check if the fixed key is weak.
13160Sstevel@tonic-gate  */
13170Sstevel@tonic-gate /* ARGSUSED */
13180Sstevel@tonic-gate static int
13190Sstevel@tonic-gate des_key_check(crypto_provider_handle_t pd, crypto_mechanism_t *mech,
13200Sstevel@tonic-gate     crypto_key_t *key)
13210Sstevel@tonic-gate {
13220Sstevel@tonic-gate 
13230Sstevel@tonic-gate /* EXPORT DELETE START */
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 	int expectedkeylen;
13260Sstevel@tonic-gate 	des_strength_t strength;
13270Sstevel@tonic-gate 	uint8_t keydata[DES3_MAX_KEY_LEN];
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate 	if ((mech == NULL) || (key == NULL))
13300Sstevel@tonic-gate 		return (CRYPTO_ARGUMENTS_BAD);
13310Sstevel@tonic-gate 
13320Sstevel@tonic-gate 	switch (mech->cm_type) {
13330Sstevel@tonic-gate 	case DES_ECB_MECH_INFO_TYPE:
13340Sstevel@tonic-gate 	case DES_CBC_MECH_INFO_TYPE:
13350Sstevel@tonic-gate 		expectedkeylen = DES_MINBITS;
13360Sstevel@tonic-gate 		strength = DES;
13370Sstevel@tonic-gate 		break;
13380Sstevel@tonic-gate 	case DES3_ECB_MECH_INFO_TYPE:
13390Sstevel@tonic-gate 	case DES3_CBC_MECH_INFO_TYPE:
13400Sstevel@tonic-gate 		expectedkeylen = DES3_MINBITS;
13410Sstevel@tonic-gate 		strength = DES3;
13420Sstevel@tonic-gate 		break;
13430Sstevel@tonic-gate 	default:
13440Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
13450Sstevel@tonic-gate 	}
13460Sstevel@tonic-gate 
13470Sstevel@tonic-gate 	if (key->ck_format != CRYPTO_KEY_RAW)
13480Sstevel@tonic-gate 		return (CRYPTO_KEY_TYPE_INCONSISTENT);
13490Sstevel@tonic-gate 
13500Sstevel@tonic-gate 	if (key->ck_length != expectedkeylen)
13510Sstevel@tonic-gate 		return (CRYPTO_KEY_SIZE_RANGE);
13520Sstevel@tonic-gate 
13530Sstevel@tonic-gate 	bcopy(key->ck_data, keydata, CRYPTO_BITS2BYTES(expectedkeylen));
13540Sstevel@tonic-gate 
13550Sstevel@tonic-gate 	if (des_keycheck(keydata, strength, key->ck_data) == B_FALSE)
13560Sstevel@tonic-gate 		return (CRYPTO_WEAK_KEY);
13570Sstevel@tonic-gate 
13580Sstevel@tonic-gate /* EXPORT DELETE END */
13590Sstevel@tonic-gate 
13600Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
13610Sstevel@tonic-gate }
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate /* ARGSUSED */
13640Sstevel@tonic-gate static int
13650Sstevel@tonic-gate des_common_init_ctx(des_ctx_t *des_ctx, crypto_spi_ctx_template_t *template,
13660Sstevel@tonic-gate     crypto_mechanism_t *mechanism, crypto_key_t *key, des_strength_t strength,
13670Sstevel@tonic-gate     int kmflag)
13680Sstevel@tonic-gate {
13690Sstevel@tonic-gate 	int rv = CRYPTO_SUCCESS;
13700Sstevel@tonic-gate 
13710Sstevel@tonic-gate /* EXPORT DELETE START */
13720Sstevel@tonic-gate 
13730Sstevel@tonic-gate 	void *keysched;
13740Sstevel@tonic-gate 	size_t size;
13750Sstevel@tonic-gate 
13760Sstevel@tonic-gate 	if (template == NULL) {
13770Sstevel@tonic-gate 		if ((keysched = des_alloc_keysched(&size, strength,
13780Sstevel@tonic-gate 		    kmflag)) == NULL)
13790Sstevel@tonic-gate 			return (CRYPTO_HOST_MEMORY);
13800Sstevel@tonic-gate 		/*
13810Sstevel@tonic-gate 		 * Initialize key schedule.
13820Sstevel@tonic-gate 		 * Key length is stored in the key.
13830Sstevel@tonic-gate 		 */
13840Sstevel@tonic-gate 		if ((rv = init_keysched(key, keysched,
13850Sstevel@tonic-gate 		    strength)) != CRYPTO_SUCCESS)
13860Sstevel@tonic-gate 			kmem_free(keysched, size);
13870Sstevel@tonic-gate 
13880Sstevel@tonic-gate 		des_ctx->dc_flags = DES_PROVIDER_OWNS_KEY_SCHEDULE;
13890Sstevel@tonic-gate 		des_ctx->dc_keysched_len = size;
13900Sstevel@tonic-gate 	} else {
13910Sstevel@tonic-gate 		keysched = template;
13920Sstevel@tonic-gate 	}
13930Sstevel@tonic-gate 
13940Sstevel@tonic-gate 	if (strength == DES3) {
13950Sstevel@tonic-gate 		des_ctx->dc_flags |= DES3_STRENGTH;
13960Sstevel@tonic-gate 	}
13970Sstevel@tonic-gate 
13980Sstevel@tonic-gate 	if (mechanism->cm_type == DES_CBC_MECH_INFO_TYPE ||
13990Sstevel@tonic-gate 	    mechanism->cm_type == DES3_CBC_MECH_INFO_TYPE) {
14000Sstevel@tonic-gate 		/*
14010Sstevel@tonic-gate 		 * Copy IV into DES context.
14020Sstevel@tonic-gate 		 *
14030Sstevel@tonic-gate 		 * If cm_param == NULL then the IV comes from the
14040Sstevel@tonic-gate 		 * cd_miscdata field in the crypto_data structure.
14050Sstevel@tonic-gate 		 */
14060Sstevel@tonic-gate 		if (mechanism->cm_param != NULL) {
14070Sstevel@tonic-gate 			ASSERT(mechanism->cm_param_len == DES_BLOCK_LEN);
14080Sstevel@tonic-gate 			if (IS_P2ALIGNED(mechanism->cm_param,
14090Sstevel@tonic-gate 			    sizeof (uint64_t))) {
14100Sstevel@tonic-gate 				/* LINTED: pointer alignment */
14110Sstevel@tonic-gate 				des_ctx->dc_iv =
14120Sstevel@tonic-gate 				    *(uint64_t *)mechanism->cm_param;
14130Sstevel@tonic-gate 			} else {
14140Sstevel@tonic-gate 				uint64_t tmp64;
14150Sstevel@tonic-gate 				uint8_t *tmp = (uint8_t *)mechanism->cm_param;
14160Sstevel@tonic-gate 
14170Sstevel@tonic-gate #ifdef _BIG_ENDIAN
14180Sstevel@tonic-gate 				tmp64 = (((uint64_t)tmp[0] << 56) |
14190Sstevel@tonic-gate 				    ((uint64_t)tmp[1] << 48) |
14200Sstevel@tonic-gate 				    ((uint64_t)tmp[2] << 40) |
14210Sstevel@tonic-gate 				    ((uint64_t)tmp[3] << 32) |
14220Sstevel@tonic-gate 				    ((uint64_t)tmp[4] << 24) |
14230Sstevel@tonic-gate 				    ((uint64_t)tmp[5] << 16) |
14240Sstevel@tonic-gate 				    ((uint64_t)tmp[6] << 8) |
14250Sstevel@tonic-gate 				    (uint64_t)tmp[7]);
14260Sstevel@tonic-gate #else
14270Sstevel@tonic-gate 				tmp64 = (((uint64_t)tmp[7] << 56) |
14280Sstevel@tonic-gate 				    ((uint64_t)tmp[6] << 48) |
14290Sstevel@tonic-gate 				    ((uint64_t)tmp[5] << 40) |
14300Sstevel@tonic-gate 				    ((uint64_t)tmp[4] << 32) |
14310Sstevel@tonic-gate 				    ((uint64_t)tmp[3] << 24) |
14320Sstevel@tonic-gate 				    ((uint64_t)tmp[2] << 16) |
14330Sstevel@tonic-gate 				    ((uint64_t)tmp[1] << 8) |
14340Sstevel@tonic-gate 				    (uint64_t)tmp[0]);
14350Sstevel@tonic-gate #endif /* _BIG_ENDIAN */
14360Sstevel@tonic-gate 
14370Sstevel@tonic-gate 				des_ctx->dc_iv = tmp64;
14380Sstevel@tonic-gate 			}
14390Sstevel@tonic-gate 		}
14400Sstevel@tonic-gate 
14410Sstevel@tonic-gate 		des_ctx->dc_lastp = (uint8_t *)&des_ctx->dc_iv;
14420Sstevel@tonic-gate 		des_ctx->dc_flags |= DES_CBC_MODE;
14430Sstevel@tonic-gate 	}
14440Sstevel@tonic-gate 	des_ctx->dc_keysched = keysched;
14450Sstevel@tonic-gate 
14460Sstevel@tonic-gate /* EXPORT DELETE END */
14470Sstevel@tonic-gate 
14480Sstevel@tonic-gate 	return (rv);
14490Sstevel@tonic-gate }
1450