xref: /onnv-gate/usr/src/uts/common/des/des_crypt.c (revision 7188:b5eb53fb3905)
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*7188Smcpowers  * Copyright 2008 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>
50*7188Smcpowers #include <modes/modes.h>
51*7188Smcpowers #include <des/des_impl.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,
895072Smcpowers 	"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;
465*7188Smcpowers 	des_ctx_t *des_ctx = NULL;
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 
476*7188Smcpowers 	kmflag = crypto_kmflag(req);
4770Sstevel@tonic-gate 	/* Check mechanism type and parameter length */
4780Sstevel@tonic-gate 	switch (mechanism->cm_type) {
4790Sstevel@tonic-gate 	case DES_ECB_MECH_INFO_TYPE:
480*7188Smcpowers 		des_ctx = ecb_alloc_ctx(kmflag);
481*7188Smcpowers 		/* FALLTHRU */
4820Sstevel@tonic-gate 	case DES_CBC_MECH_INFO_TYPE:
4830Sstevel@tonic-gate 		if (mechanism->cm_param != NULL &&
4840Sstevel@tonic-gate 		    mechanism->cm_param_len != DES_BLOCK_LEN)
4850Sstevel@tonic-gate 			return (CRYPTO_MECHANISM_PARAM_INVALID);
4860Sstevel@tonic-gate 		if (key->ck_length != DES_MINBITS)
4870Sstevel@tonic-gate 			return (CRYPTO_KEY_SIZE_RANGE);
4880Sstevel@tonic-gate 		strength = DES;
489*7188Smcpowers 		if (des_ctx == NULL)
490*7188Smcpowers 			des_ctx = cbc_alloc_ctx(kmflag);
4910Sstevel@tonic-gate 		break;
4920Sstevel@tonic-gate 	case DES3_ECB_MECH_INFO_TYPE:
493*7188Smcpowers 		des_ctx = ecb_alloc_ctx(kmflag);
494*7188Smcpowers 		/* FALLTHRU */
4950Sstevel@tonic-gate 	case DES3_CBC_MECH_INFO_TYPE:
4960Sstevel@tonic-gate 		if (mechanism->cm_param != NULL &&
4970Sstevel@tonic-gate 		    mechanism->cm_param_len != DES_BLOCK_LEN)
4980Sstevel@tonic-gate 			return (CRYPTO_MECHANISM_PARAM_INVALID);
4990Sstevel@tonic-gate 		if (key->ck_length != DES3_MINBITS)
5000Sstevel@tonic-gate 			return (CRYPTO_KEY_SIZE_RANGE);
5010Sstevel@tonic-gate 		strength = DES3;
502*7188Smcpowers 		if (des_ctx == NULL)
503*7188Smcpowers 			des_ctx = cbc_alloc_ctx(kmflag);
5040Sstevel@tonic-gate 		break;
5050Sstevel@tonic-gate 	default:
5060Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
5070Sstevel@tonic-gate 	}
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 	if ((rv = des_common_init_ctx(des_ctx, template, mechanism, key,
5100Sstevel@tonic-gate 	    strength, kmflag)) != CRYPTO_SUCCESS) {
511*7188Smcpowers 		crypto_free_mode_ctx(des_ctx);
5120Sstevel@tonic-gate 		return (rv);
5130Sstevel@tonic-gate 	}
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	ctx->cc_provider_private = des_ctx;
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate /* EXPORT DELETE END */
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
5200Sstevel@tonic-gate }
5210Sstevel@tonic-gate 
522*7188Smcpowers static void
523*7188Smcpowers des_copy_block64(uint8_t *in, uint64_t *out)
5240Sstevel@tonic-gate {
525*7188Smcpowers 	if (IS_P2ALIGNED(in, sizeof (uint64_t))) {
526*7188Smcpowers 		/* LINTED: pointer alignment */
527*7188Smcpowers 		out[0] = *(uint64_t *)&in[0];
528*7188Smcpowers 	} else {
529*7188Smcpowers 		uint64_t tmp64;
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate #ifdef _BIG_ENDIAN
532*7188Smcpowers 		tmp64 = (((uint64_t)in[0] << 56) |
533*7188Smcpowers 		    ((uint64_t)in[1] << 48) |
534*7188Smcpowers 		    ((uint64_t)in[2] << 40) |
535*7188Smcpowers 		    ((uint64_t)in[3] << 32) |
536*7188Smcpowers 		    ((uint64_t)in[4] << 24) |
537*7188Smcpowers 		    ((uint64_t)in[5] << 16) |
538*7188Smcpowers 		    ((uint64_t)in[6] << 8) |
539*7188Smcpowers 		    (uint64_t)in[7]);
5400Sstevel@tonic-gate #else
541*7188Smcpowers 		tmp64 = (((uint64_t)in[7] << 56) |
542*7188Smcpowers 		    ((uint64_t)in[6] << 48) |
543*7188Smcpowers 		    ((uint64_t)in[5] << 40) |
544*7188Smcpowers 		    ((uint64_t)in[4] << 32) |
545*7188Smcpowers 		    ((uint64_t)in[3] << 24) |
546*7188Smcpowers 		    ((uint64_t)in[2] << 16) |
547*7188Smcpowers 		    ((uint64_t)in[1] << 8) |
548*7188Smcpowers 		    (uint64_t)in[0]);
5490Sstevel@tonic-gate #endif /* _BIG_ENDIAN */
5500Sstevel@tonic-gate 
551*7188Smcpowers 		out[0] = tmp64;
5520Sstevel@tonic-gate 	}
5530Sstevel@tonic-gate }
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate /* ARGSUSED */
5560Sstevel@tonic-gate static int
5570Sstevel@tonic-gate des_encrypt(crypto_ctx_t *ctx, crypto_data_t *plaintext,
5580Sstevel@tonic-gate     crypto_data_t *ciphertext, crypto_req_handle_t req)
5590Sstevel@tonic-gate {
5600Sstevel@tonic-gate 	int ret;
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate /* EXPORT DELETE START */
5630Sstevel@tonic-gate 	des_ctx_t *des_ctx;
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 	/*
5660Sstevel@tonic-gate 	 * Plaintext must be a multiple of the block size.
5670Sstevel@tonic-gate 	 * This test only works for non-padded mechanisms
5680Sstevel@tonic-gate 	 * when blocksize is 2^N.
5690Sstevel@tonic-gate 	 */
5700Sstevel@tonic-gate 	if ((plaintext->cd_length & (DES_BLOCK_LEN - 1)) != 0)
5710Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
5740Sstevel@tonic-gate 	des_ctx = ctx->cc_provider_private;
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	DES_ARG_INPLACE(plaintext, ciphertext);
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 	/*
5790Sstevel@tonic-gate 	 * We need to just return the length needed to store the output.
5800Sstevel@tonic-gate 	 * We should not destroy the context for the following case.
5810Sstevel@tonic-gate 	 */
5820Sstevel@tonic-gate 	if (ciphertext->cd_length < plaintext->cd_length) {
5830Sstevel@tonic-gate 		ciphertext->cd_length = plaintext->cd_length;
5840Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
5850Sstevel@tonic-gate 	}
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	/*
5880Sstevel@tonic-gate 	 * Do an update on the specified input data.
5890Sstevel@tonic-gate 	 */
5900Sstevel@tonic-gate 	ret = des_encrypt_update(ctx, plaintext, ciphertext, req);
5910Sstevel@tonic-gate 	ASSERT(des_ctx->dc_remainder_len == 0);
5920Sstevel@tonic-gate 	(void) des_free_context(ctx);
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate /* EXPORT DELETE END */
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 	/* LINTED */
5970Sstevel@tonic-gate 	return (ret);
5980Sstevel@tonic-gate }
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate /* ARGSUSED */
6010Sstevel@tonic-gate static int
6020Sstevel@tonic-gate des_decrypt(crypto_ctx_t *ctx, crypto_data_t *ciphertext,
6030Sstevel@tonic-gate     crypto_data_t *plaintext, crypto_req_handle_t req)
6040Sstevel@tonic-gate {
6050Sstevel@tonic-gate 	int ret;
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate /* EXPORT DELETE START */
6080Sstevel@tonic-gate 	des_ctx_t *des_ctx;
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	/*
6110Sstevel@tonic-gate 	 * Ciphertext must be a multiple of the block size.
6120Sstevel@tonic-gate 	 * This test only works for non-padded mechanisms
6130Sstevel@tonic-gate 	 * when blocksize is 2^N.
6140Sstevel@tonic-gate 	 */
6150Sstevel@tonic-gate 	if ((ciphertext->cd_length & (DES_BLOCK_LEN - 1)) != 0)
6160Sstevel@tonic-gate 		return (CRYPTO_ENCRYPTED_DATA_LEN_RANGE);
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
6190Sstevel@tonic-gate 	des_ctx = ctx->cc_provider_private;
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 	DES_ARG_INPLACE(ciphertext, plaintext);
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	/*
6240Sstevel@tonic-gate 	 * We need to just return the length needed to store the output.
6250Sstevel@tonic-gate 	 * We should not destroy the context for the following case.
6260Sstevel@tonic-gate 	 */
6270Sstevel@tonic-gate 	if (plaintext->cd_length < ciphertext->cd_length) {
6280Sstevel@tonic-gate 		plaintext->cd_length = ciphertext->cd_length;
6290Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
6300Sstevel@tonic-gate 	}
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 	/*
6330Sstevel@tonic-gate 	 * Do an update on the specified input data.
6340Sstevel@tonic-gate 	 */
6350Sstevel@tonic-gate 	ret = des_decrypt_update(ctx, ciphertext, plaintext, req);
6360Sstevel@tonic-gate 	ASSERT(des_ctx->dc_remainder_len == 0);
6370Sstevel@tonic-gate 	(void) des_free_context(ctx);
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate /* EXPORT DELETE END */
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	/* LINTED */
6420Sstevel@tonic-gate 	return (ret);
6430Sstevel@tonic-gate }
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate /* ARGSUSED */
6460Sstevel@tonic-gate static int
6470Sstevel@tonic-gate des_encrypt_update(crypto_ctx_t *ctx, crypto_data_t *plaintext,
6480Sstevel@tonic-gate     crypto_data_t *ciphertext, crypto_req_handle_t req)
6490Sstevel@tonic-gate {
6500Sstevel@tonic-gate 	off_t saved_offset;
6510Sstevel@tonic-gate 	size_t saved_length, out_len;
6520Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate /* EXPORT DELETE START */
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	DES_ARG_INPLACE(plaintext, ciphertext);
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	/* compute number of bytes that will hold the ciphertext */
6610Sstevel@tonic-gate 	out_len = ((des_ctx_t *)ctx->cc_provider_private)->dc_remainder_len;
6620Sstevel@tonic-gate 	out_len += plaintext->cd_length;
6630Sstevel@tonic-gate 	out_len &= ~(DES_BLOCK_LEN - 1);
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 	/* return length needed to store the output */
6660Sstevel@tonic-gate 	if (ciphertext->cd_length < out_len) {
6670Sstevel@tonic-gate 		ciphertext->cd_length = out_len;
6680Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
6690Sstevel@tonic-gate 	}
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 	saved_offset = ciphertext->cd_offset;
6720Sstevel@tonic-gate 	saved_length = ciphertext->cd_length;
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 	/*
6750Sstevel@tonic-gate 	 * Do the DES update on the specified input data.
6760Sstevel@tonic-gate 	 */
6770Sstevel@tonic-gate 	switch (plaintext->cd_format) {
6780Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
679*7188Smcpowers 		ret = crypto_update_iov(ctx->cc_provider_private,
680*7188Smcpowers 		    plaintext, ciphertext, des_encrypt_contiguous_blocks,
681*7188Smcpowers 		    des_copy_block64);
6820Sstevel@tonic-gate 		break;
6830Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
684*7188Smcpowers 		ret = crypto_update_uio(ctx->cc_provider_private,
685*7188Smcpowers 		    plaintext, ciphertext, des_encrypt_contiguous_blocks,
686*7188Smcpowers 		    des_copy_block64);
6870Sstevel@tonic-gate 		break;
6880Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
689*7188Smcpowers 		ret = crypto_update_mp(ctx->cc_provider_private,
690*7188Smcpowers 		    plaintext, ciphertext, des_encrypt_contiguous_blocks,
691*7188Smcpowers 		    des_copy_block64);
6920Sstevel@tonic-gate 		break;
6930Sstevel@tonic-gate 	default:
6940Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
6950Sstevel@tonic-gate 	}
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
6980Sstevel@tonic-gate 		if (plaintext != ciphertext)
6990Sstevel@tonic-gate 			ciphertext->cd_length =
7000Sstevel@tonic-gate 			    ciphertext->cd_offset - saved_offset;
7010Sstevel@tonic-gate 	} else {
7020Sstevel@tonic-gate 		ciphertext->cd_length = saved_length;
7030Sstevel@tonic-gate 	}
7040Sstevel@tonic-gate 	ciphertext->cd_offset = saved_offset;
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate /* EXPORT DELETE END */
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 	return (ret);
7090Sstevel@tonic-gate }
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate /* ARGSUSED */
7120Sstevel@tonic-gate static int
7130Sstevel@tonic-gate des_decrypt_update(crypto_ctx_t *ctx, crypto_data_t *ciphertext,
7140Sstevel@tonic-gate     crypto_data_t *plaintext, crypto_req_handle_t req)
7150Sstevel@tonic-gate {
7160Sstevel@tonic-gate 	off_t saved_offset;
7170Sstevel@tonic-gate 	size_t saved_length, out_len;
7180Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate /* EXPORT DELETE START */
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 	DES_ARG_INPLACE(ciphertext, plaintext);
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 	/* compute number of bytes that will hold the plaintext */
7270Sstevel@tonic-gate 	out_len = ((des_ctx_t *)ctx->cc_provider_private)->dc_remainder_len;
7280Sstevel@tonic-gate 	out_len += ciphertext->cd_length;
7290Sstevel@tonic-gate 	out_len &= ~(DES_BLOCK_LEN - 1);
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 	/* return length needed to store the output */
7320Sstevel@tonic-gate 	if (plaintext->cd_length < out_len) {
7330Sstevel@tonic-gate 		plaintext->cd_length = out_len;
7340Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
7350Sstevel@tonic-gate 	}
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 	saved_offset = plaintext->cd_offset;
7380Sstevel@tonic-gate 	saved_length = plaintext->cd_length;
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 	/*
7410Sstevel@tonic-gate 	 * Do the DES update on the specified input data.
7420Sstevel@tonic-gate 	 */
7430Sstevel@tonic-gate 	switch (ciphertext->cd_format) {
7440Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
745*7188Smcpowers 		ret = crypto_update_iov(ctx->cc_provider_private,
746*7188Smcpowers 		    ciphertext, plaintext, des_decrypt_contiguous_blocks,
747*7188Smcpowers 		    des_copy_block64);
7480Sstevel@tonic-gate 		break;
7490Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
750*7188Smcpowers 		ret = crypto_update_uio(ctx->cc_provider_private,
751*7188Smcpowers 		    ciphertext, plaintext, des_decrypt_contiguous_blocks,
752*7188Smcpowers 		    des_copy_block64);
7530Sstevel@tonic-gate 		break;
7540Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
755*7188Smcpowers 		ret = crypto_update_mp(ctx->cc_provider_private,
756*7188Smcpowers 		    ciphertext, plaintext, des_decrypt_contiguous_blocks,
757*7188Smcpowers 		    des_copy_block64);
7580Sstevel@tonic-gate 		break;
7590Sstevel@tonic-gate 	default:
7600Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
7610Sstevel@tonic-gate 	}
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
7640Sstevel@tonic-gate 		if (ciphertext != plaintext)
7650Sstevel@tonic-gate 			plaintext->cd_length =
7660Sstevel@tonic-gate 			    plaintext->cd_offset - saved_offset;
7670Sstevel@tonic-gate 	} else {
7680Sstevel@tonic-gate 		plaintext->cd_length = saved_length;
7690Sstevel@tonic-gate 	}
7700Sstevel@tonic-gate 	plaintext->cd_offset = saved_offset;
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate /* EXPORT DELETE END */
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 	return (ret);
7750Sstevel@tonic-gate }
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate /* ARGSUSED */
7780Sstevel@tonic-gate static int
7790Sstevel@tonic-gate des_encrypt_final(crypto_ctx_t *ctx, crypto_data_t *ciphertext,
7800Sstevel@tonic-gate     crypto_req_handle_t req)
7810Sstevel@tonic-gate {
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate /* EXPORT DELETE START */
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 	des_ctx_t *des_ctx;
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
7880Sstevel@tonic-gate 	des_ctx = ctx->cc_provider_private;
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 	/*
7910Sstevel@tonic-gate 	 * There must be no unprocessed plaintext.
7920Sstevel@tonic-gate 	 * This happens if the length of the last data is
7930Sstevel@tonic-gate 	 * not a multiple of the DES block length.
7940Sstevel@tonic-gate 	 */
7950Sstevel@tonic-gate 	if (des_ctx->dc_remainder_len > 0)
7960Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	(void) des_free_context(ctx);
7990Sstevel@tonic-gate 	ciphertext->cd_length = 0;
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate /* EXPORT DELETE END */
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
8040Sstevel@tonic-gate }
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate /* ARGSUSED */
8070Sstevel@tonic-gate static int
8080Sstevel@tonic-gate des_decrypt_final(crypto_ctx_t *ctx, crypto_data_t *plaintext,
8090Sstevel@tonic-gate     crypto_req_handle_t req)
8100Sstevel@tonic-gate {
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate /* EXPORT DELETE START */
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 	des_ctx_t *des_ctx;
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
8170Sstevel@tonic-gate 	des_ctx = ctx->cc_provider_private;
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 	/*
8200Sstevel@tonic-gate 	 * There must be no unprocessed ciphertext.
8210Sstevel@tonic-gate 	 * This happens if the length of the last ciphertext is
8220Sstevel@tonic-gate 	 * not a multiple of the DES block length.
8230Sstevel@tonic-gate 	 */
8240Sstevel@tonic-gate 	if (des_ctx->dc_remainder_len > 0)
8250Sstevel@tonic-gate 		return (CRYPTO_ENCRYPTED_DATA_LEN_RANGE);
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 	(void) des_free_context(ctx);
8280Sstevel@tonic-gate 	plaintext->cd_length = 0;
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate /* EXPORT DELETE END */
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
8330Sstevel@tonic-gate }
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate /* ARGSUSED */
8360Sstevel@tonic-gate static int
8370Sstevel@tonic-gate des_encrypt_atomic(crypto_provider_handle_t provider,
8380Sstevel@tonic-gate     crypto_session_id_t session_id, crypto_mechanism_t *mechanism,
8390Sstevel@tonic-gate     crypto_key_t *key, crypto_data_t *plaintext, crypto_data_t *ciphertext,
8400Sstevel@tonic-gate     crypto_spi_ctx_template_t template, crypto_req_handle_t req)
8410Sstevel@tonic-gate {
8420Sstevel@tonic-gate 	int ret;
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate /* EXPORT DELETE START */
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 	des_ctx_t des_ctx;		/* on the stack */
8470Sstevel@tonic-gate 	des_strength_t strength;
8480Sstevel@tonic-gate 	off_t saved_offset;
8490Sstevel@tonic-gate 	size_t saved_length;
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 	DES_ARG_INPLACE(plaintext, ciphertext);
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 	/*
8540Sstevel@tonic-gate 	 * Plaintext must be a multiple of the block size.
8550Sstevel@tonic-gate 	 * This test only works for non-padded mechanisms
8560Sstevel@tonic-gate 	 * when blocksize is 2^N.
8570Sstevel@tonic-gate 	 */
8580Sstevel@tonic-gate 	if ((plaintext->cd_length & (DES_BLOCK_LEN - 1)) != 0)
8590Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 	/* return length needed to store the output */
8620Sstevel@tonic-gate 	if (ciphertext->cd_length < plaintext->cd_length) {
8630Sstevel@tonic-gate 		ciphertext->cd_length = plaintext->cd_length;
8640Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
8650Sstevel@tonic-gate 	}
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 	/* Check mechanism type and parameter length */
8680Sstevel@tonic-gate 	switch (mechanism->cm_type) {
8690Sstevel@tonic-gate 	case DES_ECB_MECH_INFO_TYPE:
8700Sstevel@tonic-gate 	case DES_CBC_MECH_INFO_TYPE:
8710Sstevel@tonic-gate 		if (mechanism->cm_param_len > 0 &&
8720Sstevel@tonic-gate 		    mechanism->cm_param_len != DES_BLOCK_LEN)
8730Sstevel@tonic-gate 			return (CRYPTO_MECHANISM_PARAM_INVALID);
8740Sstevel@tonic-gate 		if (key->ck_length != DES_MINBITS)
8750Sstevel@tonic-gate 			return (CRYPTO_KEY_SIZE_RANGE);
8760Sstevel@tonic-gate 		strength = DES;
8770Sstevel@tonic-gate 		break;
8780Sstevel@tonic-gate 	case DES3_ECB_MECH_INFO_TYPE:
8790Sstevel@tonic-gate 	case DES3_CBC_MECH_INFO_TYPE:
8800Sstevel@tonic-gate 		if (mechanism->cm_param_len > 0 &&
8810Sstevel@tonic-gate 		    mechanism->cm_param_len != DES_BLOCK_LEN)
8820Sstevel@tonic-gate 			return (CRYPTO_MECHANISM_PARAM_INVALID);
8830Sstevel@tonic-gate 		if (key->ck_length != DES3_MINBITS)
8840Sstevel@tonic-gate 			return (CRYPTO_KEY_SIZE_RANGE);
8850Sstevel@tonic-gate 		strength = DES3;
8860Sstevel@tonic-gate 		break;
8870Sstevel@tonic-gate 	default:
8880Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
8890Sstevel@tonic-gate 	}
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 	bzero(&des_ctx, sizeof (des_ctx_t));
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate 	if ((ret = des_common_init_ctx(&des_ctx, template, mechanism, key,
8940Sstevel@tonic-gate 	    strength, crypto_kmflag(req))) != CRYPTO_SUCCESS) {
8950Sstevel@tonic-gate 		return (ret);
8960Sstevel@tonic-gate 	}
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 	saved_offset = ciphertext->cd_offset;
8990Sstevel@tonic-gate 	saved_length = ciphertext->cd_length;
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 	/*
9020Sstevel@tonic-gate 	 * Do the update on the specified input data.
9030Sstevel@tonic-gate 	 */
9040Sstevel@tonic-gate 	switch (plaintext->cd_format) {
9050Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
906*7188Smcpowers 		ret = crypto_update_iov(&des_ctx, plaintext, ciphertext,
907*7188Smcpowers 		    des_encrypt_contiguous_blocks, des_copy_block64);
9080Sstevel@tonic-gate 		break;
9090Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
910*7188Smcpowers 		ret = crypto_update_uio(&des_ctx, plaintext, ciphertext,
911*7188Smcpowers 		    des_encrypt_contiguous_blocks, des_copy_block64);
9120Sstevel@tonic-gate 		break;
9130Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
914*7188Smcpowers 		ret = crypto_update_mp(&des_ctx, plaintext, ciphertext,
915*7188Smcpowers 		    des_encrypt_contiguous_blocks, des_copy_block64);
9160Sstevel@tonic-gate 		break;
9170Sstevel@tonic-gate 	default:
9180Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
9190Sstevel@tonic-gate 	}
9200Sstevel@tonic-gate 
921*7188Smcpowers 	if (des_ctx.dc_flags & PROVIDER_OWNS_KEY_SCHEDULE) {
9220Sstevel@tonic-gate 		bzero(des_ctx.dc_keysched, des_ctx.dc_keysched_len);
9230Sstevel@tonic-gate 		kmem_free(des_ctx.dc_keysched, des_ctx.dc_keysched_len);
9240Sstevel@tonic-gate 	}
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
9270Sstevel@tonic-gate 		ASSERT(des_ctx.dc_remainder_len == 0);
9280Sstevel@tonic-gate 		if (plaintext != ciphertext)
9290Sstevel@tonic-gate 			ciphertext->cd_length =
9300Sstevel@tonic-gate 			    ciphertext->cd_offset - saved_offset;
9310Sstevel@tonic-gate 	} else {
9320Sstevel@tonic-gate 		ciphertext->cd_length = saved_length;
9330Sstevel@tonic-gate 	}
9340Sstevel@tonic-gate 	ciphertext->cd_offset = saved_offset;
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate /* EXPORT DELETE END */
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 	/* LINTED */
9390Sstevel@tonic-gate 	return (ret);
9400Sstevel@tonic-gate }
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate /* ARGSUSED */
9430Sstevel@tonic-gate static int
9440Sstevel@tonic-gate des_decrypt_atomic(crypto_provider_handle_t provider,
9450Sstevel@tonic-gate     crypto_session_id_t session_id, crypto_mechanism_t *mechanism,
9460Sstevel@tonic-gate     crypto_key_t *key, crypto_data_t *ciphertext, crypto_data_t *plaintext,
9470Sstevel@tonic-gate     crypto_spi_ctx_template_t template, crypto_req_handle_t req)
9480Sstevel@tonic-gate {
9490Sstevel@tonic-gate 	int ret;
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate /* EXPORT DELETE START */
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate 	des_ctx_t des_ctx;	/* on the stack */
9540Sstevel@tonic-gate 	des_strength_t strength;
9550Sstevel@tonic-gate 	off_t saved_offset;
9560Sstevel@tonic-gate 	size_t saved_length;
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate 	DES_ARG_INPLACE(ciphertext, plaintext);
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 	/*
9610Sstevel@tonic-gate 	 * Ciphertext must be a multiple of the block size.
9620Sstevel@tonic-gate 	 * This test only works for non-padded mechanisms
9630Sstevel@tonic-gate 	 * when blocksize is 2^N.
9640Sstevel@tonic-gate 	 */
9650Sstevel@tonic-gate 	if ((ciphertext->cd_length & (DES_BLOCK_LEN - 1)) != 0)
9660Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 	/* return length needed to store the output */
9690Sstevel@tonic-gate 	if (plaintext->cd_length < ciphertext->cd_length) {
9700Sstevel@tonic-gate 		plaintext->cd_length = ciphertext->cd_length;
9710Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
9720Sstevel@tonic-gate 	}
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 	/* Check mechanism type and parameter length */
9750Sstevel@tonic-gate 	switch (mechanism->cm_type) {
9760Sstevel@tonic-gate 	case DES_ECB_MECH_INFO_TYPE:
9770Sstevel@tonic-gate 	case DES_CBC_MECH_INFO_TYPE:
9780Sstevel@tonic-gate 		if (mechanism->cm_param_len > 0 &&
9790Sstevel@tonic-gate 		    mechanism->cm_param_len != DES_BLOCK_LEN)
9800Sstevel@tonic-gate 			return (CRYPTO_MECHANISM_PARAM_INVALID);
9810Sstevel@tonic-gate 		if (key->ck_length != DES_MINBITS)
9820Sstevel@tonic-gate 			return (CRYPTO_KEY_SIZE_RANGE);
9830Sstevel@tonic-gate 		strength = DES;
9840Sstevel@tonic-gate 		break;
9850Sstevel@tonic-gate 	case DES3_ECB_MECH_INFO_TYPE:
9860Sstevel@tonic-gate 	case DES3_CBC_MECH_INFO_TYPE:
9870Sstevel@tonic-gate 		if (mechanism->cm_param_len > 0 &&
9880Sstevel@tonic-gate 		    mechanism->cm_param_len != DES_BLOCK_LEN)
9890Sstevel@tonic-gate 			return (CRYPTO_MECHANISM_PARAM_INVALID);
9900Sstevel@tonic-gate 		if (key->ck_length != DES3_MINBITS)
9910Sstevel@tonic-gate 			return (CRYPTO_KEY_SIZE_RANGE);
9920Sstevel@tonic-gate 		strength = DES3;
9930Sstevel@tonic-gate 		break;
9940Sstevel@tonic-gate 	default:
9950Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
9960Sstevel@tonic-gate 	}
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 	bzero(&des_ctx, sizeof (des_ctx_t));
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate 	if ((ret = des_common_init_ctx(&des_ctx, template, mechanism, key,
10010Sstevel@tonic-gate 	    strength, crypto_kmflag(req))) != CRYPTO_SUCCESS) {
10020Sstevel@tonic-gate 		return (ret);
10030Sstevel@tonic-gate 	}
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate 	saved_offset = plaintext->cd_offset;
10060Sstevel@tonic-gate 	saved_length = plaintext->cd_length;
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 	/*
10090Sstevel@tonic-gate 	 * Do the update on the specified input data.
10100Sstevel@tonic-gate 	 */
10110Sstevel@tonic-gate 	switch (ciphertext->cd_format) {
10120Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
1013*7188Smcpowers 		ret = crypto_update_iov(&des_ctx, ciphertext, plaintext,
1014*7188Smcpowers 		    des_decrypt_contiguous_blocks, des_copy_block64);
10150Sstevel@tonic-gate 		break;
10160Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
1017*7188Smcpowers 		ret = crypto_update_uio(&des_ctx, ciphertext, plaintext,
1018*7188Smcpowers 		    des_decrypt_contiguous_blocks, des_copy_block64);
10190Sstevel@tonic-gate 		break;
10200Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
1021*7188Smcpowers 		ret = crypto_update_mp(&des_ctx, ciphertext, plaintext,
1022*7188Smcpowers 		    des_decrypt_contiguous_blocks, des_copy_block64);
10230Sstevel@tonic-gate 		break;
10240Sstevel@tonic-gate 	default:
10250Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
10260Sstevel@tonic-gate 	}
10270Sstevel@tonic-gate 
1028*7188Smcpowers 	if (des_ctx.dc_flags & PROVIDER_OWNS_KEY_SCHEDULE) {
10290Sstevel@tonic-gate 		bzero(des_ctx.dc_keysched, des_ctx.dc_keysched_len);
10300Sstevel@tonic-gate 		kmem_free(des_ctx.dc_keysched, des_ctx.dc_keysched_len);
10310Sstevel@tonic-gate 	}
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
10340Sstevel@tonic-gate 		ASSERT(des_ctx.dc_remainder_len == 0);
10350Sstevel@tonic-gate 		if (ciphertext != plaintext)
10360Sstevel@tonic-gate 			plaintext->cd_length =
10370Sstevel@tonic-gate 			    plaintext->cd_offset - saved_offset;
10380Sstevel@tonic-gate 	} else {
10390Sstevel@tonic-gate 		plaintext->cd_length = saved_length;
10400Sstevel@tonic-gate 	}
10410Sstevel@tonic-gate 	plaintext->cd_offset = saved_offset;
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate /* EXPORT DELETE END */
10440Sstevel@tonic-gate 
10450Sstevel@tonic-gate 	/* LINTED */
10460Sstevel@tonic-gate 	return (ret);
10470Sstevel@tonic-gate }
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate /*
10500Sstevel@tonic-gate  * KCF software provider context template entry points.
10510Sstevel@tonic-gate  */
10520Sstevel@tonic-gate /* ARGSUSED */
10530Sstevel@tonic-gate static int
10540Sstevel@tonic-gate des_create_ctx_template(crypto_provider_handle_t provider,
10550Sstevel@tonic-gate     crypto_mechanism_t *mechanism, crypto_key_t *key,
10560Sstevel@tonic-gate     crypto_spi_ctx_template_t *tmpl, size_t *tmpl_size, crypto_req_handle_t req)
10570Sstevel@tonic-gate {
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate /* EXPORT DELETE START */
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate 	des_strength_t strength;
10620Sstevel@tonic-gate 	void *keysched;
10630Sstevel@tonic-gate 	size_t size;
10640Sstevel@tonic-gate 	int rv;
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate 	switch (mechanism->cm_type) {
10670Sstevel@tonic-gate 	case DES_ECB_MECH_INFO_TYPE:
10680Sstevel@tonic-gate 		strength = DES;
10690Sstevel@tonic-gate 		break;
10700Sstevel@tonic-gate 	case DES_CBC_MECH_INFO_TYPE:
10710Sstevel@tonic-gate 		strength = DES;
10720Sstevel@tonic-gate 		break;
10730Sstevel@tonic-gate 	case DES3_ECB_MECH_INFO_TYPE:
10740Sstevel@tonic-gate 		strength = DES3;
10750Sstevel@tonic-gate 		break;
10760Sstevel@tonic-gate 	case DES3_CBC_MECH_INFO_TYPE:
10770Sstevel@tonic-gate 		strength = DES3;
10780Sstevel@tonic-gate 		break;
10790Sstevel@tonic-gate 	default:
10800Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
10810Sstevel@tonic-gate 	}
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate 	if ((keysched = des_alloc_keysched(&size, strength,
10840Sstevel@tonic-gate 	    crypto_kmflag(req))) == NULL) {
10850Sstevel@tonic-gate 		return (CRYPTO_HOST_MEMORY);
10860Sstevel@tonic-gate 	}
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 	/*
10890Sstevel@tonic-gate 	 * Initialize key schedule.  Key length information is stored
10900Sstevel@tonic-gate 	 * in the key.
10910Sstevel@tonic-gate 	 */
10920Sstevel@tonic-gate 	if ((rv = init_keysched(key, keysched, strength)) != CRYPTO_SUCCESS) {
10930Sstevel@tonic-gate 		bzero(keysched, size);
10940Sstevel@tonic-gate 		kmem_free(keysched, size);
10950Sstevel@tonic-gate 		return (rv);
10960Sstevel@tonic-gate 	}
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 	*tmpl = keysched;
10990Sstevel@tonic-gate 	*tmpl_size = size;
11000Sstevel@tonic-gate 
11010Sstevel@tonic-gate /* EXPORT DELETE END */
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
11040Sstevel@tonic-gate }
11050Sstevel@tonic-gate 
11060Sstevel@tonic-gate /* ARGSUSED */
11070Sstevel@tonic-gate static int
11080Sstevel@tonic-gate des_free_context(crypto_ctx_t *ctx)
11090Sstevel@tonic-gate {
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate /* EXPORT DELETE START */
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate 	des_ctx_t *des_ctx = ctx->cc_provider_private;
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate 	if (des_ctx != NULL) {
1116*7188Smcpowers 		if (des_ctx->dc_flags & PROVIDER_OWNS_KEY_SCHEDULE) {
11170Sstevel@tonic-gate 			ASSERT(des_ctx->dc_keysched_len != 0);
11180Sstevel@tonic-gate 			bzero(des_ctx->dc_keysched, des_ctx->dc_keysched_len);
11190Sstevel@tonic-gate 			kmem_free(des_ctx->dc_keysched,
11200Sstevel@tonic-gate 			    des_ctx->dc_keysched_len);
11210Sstevel@tonic-gate 		}
1122*7188Smcpowers 		crypto_free_mode_ctx(des_ctx);
11230Sstevel@tonic-gate 		ctx->cc_provider_private = NULL;
11240Sstevel@tonic-gate 	}
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate /* EXPORT DELETE END */
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
11290Sstevel@tonic-gate }
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate /*
11320Sstevel@tonic-gate  * Pass it to des_keycheck() which will
11330Sstevel@tonic-gate  * fix it (parity bits), and check if the fixed key is weak.
11340Sstevel@tonic-gate  */
11350Sstevel@tonic-gate /* ARGSUSED */
11360Sstevel@tonic-gate static int
11370Sstevel@tonic-gate des_key_check(crypto_provider_handle_t pd, crypto_mechanism_t *mech,
11380Sstevel@tonic-gate     crypto_key_t *key)
11390Sstevel@tonic-gate {
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate /* EXPORT DELETE START */
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate 	int expectedkeylen;
11440Sstevel@tonic-gate 	des_strength_t strength;
11450Sstevel@tonic-gate 	uint8_t keydata[DES3_MAX_KEY_LEN];
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate 	if ((mech == NULL) || (key == NULL))
11480Sstevel@tonic-gate 		return (CRYPTO_ARGUMENTS_BAD);
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate 	switch (mech->cm_type) {
11510Sstevel@tonic-gate 	case DES_ECB_MECH_INFO_TYPE:
11520Sstevel@tonic-gate 	case DES_CBC_MECH_INFO_TYPE:
11530Sstevel@tonic-gate 		expectedkeylen = DES_MINBITS;
11540Sstevel@tonic-gate 		strength = DES;
11550Sstevel@tonic-gate 		break;
11560Sstevel@tonic-gate 	case DES3_ECB_MECH_INFO_TYPE:
11570Sstevel@tonic-gate 	case DES3_CBC_MECH_INFO_TYPE:
11580Sstevel@tonic-gate 		expectedkeylen = DES3_MINBITS;
11590Sstevel@tonic-gate 		strength = DES3;
11600Sstevel@tonic-gate 		break;
11610Sstevel@tonic-gate 	default:
11620Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
11630Sstevel@tonic-gate 	}
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate 	if (key->ck_format != CRYPTO_KEY_RAW)
11660Sstevel@tonic-gate 		return (CRYPTO_KEY_TYPE_INCONSISTENT);
11670Sstevel@tonic-gate 
11680Sstevel@tonic-gate 	if (key->ck_length != expectedkeylen)
11690Sstevel@tonic-gate 		return (CRYPTO_KEY_SIZE_RANGE);
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate 	bcopy(key->ck_data, keydata, CRYPTO_BITS2BYTES(expectedkeylen));
11720Sstevel@tonic-gate 
11730Sstevel@tonic-gate 	if (des_keycheck(keydata, strength, key->ck_data) == B_FALSE)
11740Sstevel@tonic-gate 		return (CRYPTO_WEAK_KEY);
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate /* EXPORT DELETE END */
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
11790Sstevel@tonic-gate }
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate /* ARGSUSED */
11820Sstevel@tonic-gate static int
11830Sstevel@tonic-gate des_common_init_ctx(des_ctx_t *des_ctx, crypto_spi_ctx_template_t *template,
11840Sstevel@tonic-gate     crypto_mechanism_t *mechanism, crypto_key_t *key, des_strength_t strength,
11850Sstevel@tonic-gate     int kmflag)
11860Sstevel@tonic-gate {
11870Sstevel@tonic-gate 	int rv = CRYPTO_SUCCESS;
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate /* EXPORT DELETE START */
11900Sstevel@tonic-gate 
11910Sstevel@tonic-gate 	void *keysched;
11920Sstevel@tonic-gate 	size_t size;
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate 	if (template == NULL) {
11950Sstevel@tonic-gate 		if ((keysched = des_alloc_keysched(&size, strength,
11960Sstevel@tonic-gate 		    kmflag)) == NULL)
11970Sstevel@tonic-gate 			return (CRYPTO_HOST_MEMORY);
11980Sstevel@tonic-gate 		/*
11990Sstevel@tonic-gate 		 * Initialize key schedule.
12000Sstevel@tonic-gate 		 * Key length is stored in the key.
12010Sstevel@tonic-gate 		 */
12020Sstevel@tonic-gate 		if ((rv = init_keysched(key, keysched,
12030Sstevel@tonic-gate 		    strength)) != CRYPTO_SUCCESS)
12040Sstevel@tonic-gate 			kmem_free(keysched, size);
12050Sstevel@tonic-gate 
1206*7188Smcpowers 		des_ctx->dc_flags |= PROVIDER_OWNS_KEY_SCHEDULE;
12070Sstevel@tonic-gate 		des_ctx->dc_keysched_len = size;
12080Sstevel@tonic-gate 	} else {
12090Sstevel@tonic-gate 		keysched = template;
12100Sstevel@tonic-gate 	}
1211*7188Smcpowers 	des_ctx->dc_keysched = keysched;
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate 	if (strength == DES3) {
12140Sstevel@tonic-gate 		des_ctx->dc_flags |= DES3_STRENGTH;
12150Sstevel@tonic-gate 	}
12160Sstevel@tonic-gate 
1217*7188Smcpowers 	switch (mechanism->cm_type) {
1218*7188Smcpowers 	case DES_CBC_MECH_INFO_TYPE:
1219*7188Smcpowers 	case DES3_CBC_MECH_INFO_TYPE:
1220*7188Smcpowers 		rv = cbc_init_ctx((cbc_ctx_t *)des_ctx, mechanism->cm_param,
1221*7188Smcpowers 		    mechanism->cm_param_len, DES_BLOCK_LEN, des_copy_block64);
1222*7188Smcpowers 		break;
1223*7188Smcpowers 	case DES_ECB_MECH_INFO_TYPE:
1224*7188Smcpowers 	case DES3_ECB_MECH_INFO_TYPE:
1225*7188Smcpowers 		des_ctx->dc_flags |= ECB_MODE;
1226*7188Smcpowers 	}
12270Sstevel@tonic-gate 
1228*7188Smcpowers 	if (rv != CRYPTO_SUCCESS) {
1229*7188Smcpowers 		if (des_ctx->dc_flags & PROVIDER_OWNS_KEY_SCHEDULE) {
1230*7188Smcpowers 			bzero(keysched, size);
1231*7188Smcpowers 			kmem_free(keysched, size);
12320Sstevel@tonic-gate 		}
12330Sstevel@tonic-gate 	}
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate /* EXPORT DELETE END */
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate 	return (rv);
12380Sstevel@tonic-gate }
1239