xref: /onnv-gate/usr/src/uts/common/des/des_crypt.c (revision 10500:a10fbcfc2f21)
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  *
2110444SVladimir.Kotal@Sun.COM  */
2210444SVladimir.Kotal@Sun.COM /*
2310444SVladimir.Kotal@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
320Sstevel@tonic-gate  * under license from the Regents of the University of California.
330Sstevel@tonic-gate  */
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>
507188Smcpowers #include <modes/modes.h>
51*10500SHai-May.Chao@Sun.COM #define	_DES_FIPS_POST
527188Smcpowers #include <des/des_impl.h>
530Sstevel@tonic-gate 
540Sstevel@tonic-gate /* EXPORT DELETE START */
550Sstevel@tonic-gate #include <sys/types.h>
560Sstevel@tonic-gate #include <rpc/des_crypt.h>
570Sstevel@tonic-gate #include <des/des.h>
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #ifdef sun_hardware
600Sstevel@tonic-gate #include <sys/ioctl.h>
610Sstevel@tonic-gate #ifdef _KERNEL
620Sstevel@tonic-gate #include <sys/conf.h>
630Sstevel@tonic-gate static int g_desfd = -1;
640Sstevel@tonic-gate #define	getdesfd()	(cdevsw[11].d_open(0, 0) ? -1 : 0)
650Sstevel@tonic-gate #define	ioctl(a, b, c)	(cdevsw[11].d_ioctl(0, b, c, 0) ? -1 : 0)
660Sstevel@tonic-gate #else
670Sstevel@tonic-gate #define	getdesfd()	(open("/dev/des", 0, 0))
680Sstevel@tonic-gate #endif	/* _KERNEL */
690Sstevel@tonic-gate #endif	/* sun */
700Sstevel@tonic-gate 
710Sstevel@tonic-gate static int common_crypt(char *key, char *buf, size_t len,
720Sstevel@tonic-gate     unsigned int mode, struct desparams *desp);
730Sstevel@tonic-gate 
740Sstevel@tonic-gate extern int _des_crypt(char *buf, size_t len, struct desparams *desp);
750Sstevel@tonic-gate 
760Sstevel@tonic-gate /* EXPORT DELETE END */
770Sstevel@tonic-gate 
780Sstevel@tonic-gate extern struct mod_ops mod_cryptoops;
790Sstevel@tonic-gate 
800Sstevel@tonic-gate /*
810Sstevel@tonic-gate  * Module linkage information for the kernel.
820Sstevel@tonic-gate  */
830Sstevel@tonic-gate static struct modlmisc modlmisc = {
840Sstevel@tonic-gate 	&mod_miscops,
850Sstevel@tonic-gate 	"des encryption",
860Sstevel@tonic-gate };
870Sstevel@tonic-gate 
880Sstevel@tonic-gate static struct modlcrypto modlcrypto = {
890Sstevel@tonic-gate 	&mod_cryptoops,
905072Smcpowers 	"DES Kernel SW Provider"
910Sstevel@tonic-gate };
920Sstevel@tonic-gate 
930Sstevel@tonic-gate static struct modlinkage modlinkage = {
940Sstevel@tonic-gate 	MODREV_1,
950Sstevel@tonic-gate 	&modlmisc,
960Sstevel@tonic-gate 	&modlcrypto,
970Sstevel@tonic-gate 	NULL
980Sstevel@tonic-gate };
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate /* EXPORT DELETE START */
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate #define	DES_MIN_KEY_LEN		DES_MINBYTES
1030Sstevel@tonic-gate #define	DES_MAX_KEY_LEN		DES_MAXBYTES
10410444SVladimir.Kotal@Sun.COM #define	DES3_MIN_KEY_LEN	DES3_MAXBYTES	/* no CKK_DES2 support */
1050Sstevel@tonic-gate #define	DES3_MAX_KEY_LEN	DES3_MAXBYTES
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate /* EXPORT DELETE END */
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate #ifndef DES_MIN_KEY_LEN
1100Sstevel@tonic-gate #define	DES_MIN_KEY_LEN		0
1110Sstevel@tonic-gate #endif
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate #ifndef DES_MAX_KEY_LEN
1140Sstevel@tonic-gate #define	DES_MAX_KEY_LEN		0
1150Sstevel@tonic-gate #endif
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate #ifndef DES3_MIN_KEY_LEN
1180Sstevel@tonic-gate #define	DES3_MIN_KEY_LEN	0
1190Sstevel@tonic-gate #endif
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate #ifndef DES3_MAX_KEY_LEN
1220Sstevel@tonic-gate #define	DES3_MAX_KEY_LEN	0
1230Sstevel@tonic-gate #endif
1240Sstevel@tonic-gate 
125*10500SHai-May.Chao@Sun.COM 
1260Sstevel@tonic-gate /*
1270Sstevel@tonic-gate  * Mechanism info structure passed to KCF during registration.
1280Sstevel@tonic-gate  */
1290Sstevel@tonic-gate static crypto_mech_info_t des_mech_info_tab[] = {
1300Sstevel@tonic-gate 	/* DES_ECB */
1310Sstevel@tonic-gate 	{SUN_CKM_DES_ECB, DES_ECB_MECH_INFO_TYPE,
1320Sstevel@tonic-gate 	    CRYPTO_FG_ENCRYPT | CRYPTO_FG_ENCRYPT_ATOMIC |
1330Sstevel@tonic-gate 	    CRYPTO_FG_DECRYPT | CRYPTO_FG_DECRYPT_ATOMIC,
1340Sstevel@tonic-gate 	    DES_MIN_KEY_LEN, DES_MAX_KEY_LEN, CRYPTO_KEYSIZE_UNIT_IN_BYTES},
1350Sstevel@tonic-gate 	/* DES_CBC */
1360Sstevel@tonic-gate 	{SUN_CKM_DES_CBC, DES_CBC_MECH_INFO_TYPE,
1370Sstevel@tonic-gate 	    CRYPTO_FG_ENCRYPT | CRYPTO_FG_ENCRYPT_ATOMIC |
1380Sstevel@tonic-gate 	    CRYPTO_FG_DECRYPT | CRYPTO_FG_DECRYPT_ATOMIC,
1390Sstevel@tonic-gate 	    DES_MIN_KEY_LEN, DES_MAX_KEY_LEN, CRYPTO_KEYSIZE_UNIT_IN_BYTES},
1400Sstevel@tonic-gate 	/* DES3_ECB */
1410Sstevel@tonic-gate 	{SUN_CKM_DES3_ECB, DES3_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 	    DES3_MIN_KEY_LEN, DES3_MAX_KEY_LEN, CRYPTO_KEYSIZE_UNIT_IN_BYTES},
1450Sstevel@tonic-gate 	/* DES3_CBC */
1460Sstevel@tonic-gate 	{SUN_CKM_DES3_CBC, DES3_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 	    DES3_MIN_KEY_LEN, DES3_MAX_KEY_LEN, CRYPTO_KEYSIZE_UNIT_IN_BYTES}
1500Sstevel@tonic-gate };
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate /* operations are in-place if the output buffer is NULL */
1530Sstevel@tonic-gate #define	DES_ARG_INPLACE(input, output)				\
1540Sstevel@tonic-gate 	if ((output) == NULL)					\
1550Sstevel@tonic-gate 		(output) = (input);
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate static void des_provider_status(crypto_provider_handle_t, uint_t *);
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate static crypto_control_ops_t des_control_ops = {
1600Sstevel@tonic-gate 	des_provider_status
1610Sstevel@tonic-gate };
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate static int
1640Sstevel@tonic-gate des_common_init(crypto_ctx_t *, crypto_mechanism_t *, crypto_key_t *,
1650Sstevel@tonic-gate     crypto_spi_ctx_template_t, crypto_req_handle_t);
1660Sstevel@tonic-gate static int des_common_init_ctx(des_ctx_t *, crypto_spi_ctx_template_t *,
1670Sstevel@tonic-gate     crypto_mechanism_t *, crypto_key_t *, des_strength_t, int);
1680Sstevel@tonic-gate static int des_encrypt_final(crypto_ctx_t *, crypto_data_t *,
1690Sstevel@tonic-gate     crypto_req_handle_t);
1700Sstevel@tonic-gate static int des_decrypt_final(crypto_ctx_t *, crypto_data_t *,
1710Sstevel@tonic-gate     crypto_req_handle_t);
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate static int des_encrypt(crypto_ctx_t *, crypto_data_t *, crypto_data_t *,
1740Sstevel@tonic-gate     crypto_req_handle_t);
1750Sstevel@tonic-gate static int des_encrypt_update(crypto_ctx_t *, crypto_data_t *,
1760Sstevel@tonic-gate     crypto_data_t *, crypto_req_handle_t);
1770Sstevel@tonic-gate static int des_encrypt_atomic(crypto_provider_handle_t, crypto_session_id_t,
1780Sstevel@tonic-gate     crypto_mechanism_t *, crypto_key_t *, crypto_data_t *,
1790Sstevel@tonic-gate     crypto_data_t *, crypto_spi_ctx_template_t, crypto_req_handle_t);
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate static int des_decrypt(crypto_ctx_t *, crypto_data_t *, crypto_data_t *,
1820Sstevel@tonic-gate     crypto_req_handle_t);
1830Sstevel@tonic-gate static int des_decrypt_update(crypto_ctx_t *, crypto_data_t *,
1840Sstevel@tonic-gate     crypto_data_t *, crypto_req_handle_t);
1850Sstevel@tonic-gate static int des_decrypt_atomic(crypto_provider_handle_t, crypto_session_id_t,
1860Sstevel@tonic-gate     crypto_mechanism_t *, crypto_key_t *, crypto_data_t *,
1870Sstevel@tonic-gate     crypto_data_t *, crypto_spi_ctx_template_t, crypto_req_handle_t);
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate static crypto_cipher_ops_t des_cipher_ops = {
1900Sstevel@tonic-gate 	des_common_init,
1910Sstevel@tonic-gate 	des_encrypt,
1920Sstevel@tonic-gate 	des_encrypt_update,
1930Sstevel@tonic-gate 	des_encrypt_final,
1940Sstevel@tonic-gate 	des_encrypt_atomic,
1950Sstevel@tonic-gate 	des_common_init,
1960Sstevel@tonic-gate 	des_decrypt,
1970Sstevel@tonic-gate 	des_decrypt_update,
1980Sstevel@tonic-gate 	des_decrypt_final,
1990Sstevel@tonic-gate 	des_decrypt_atomic
2000Sstevel@tonic-gate };
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate static int des_create_ctx_template(crypto_provider_handle_t,
2030Sstevel@tonic-gate     crypto_mechanism_t *, crypto_key_t *, crypto_spi_ctx_template_t *,
2040Sstevel@tonic-gate     size_t *, crypto_req_handle_t);
2050Sstevel@tonic-gate static int des_free_context(crypto_ctx_t *);
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate static crypto_ctx_ops_t des_ctx_ops = {
2080Sstevel@tonic-gate 	des_create_ctx_template,
2090Sstevel@tonic-gate 	des_free_context
2100Sstevel@tonic-gate };
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate static int des_key_check(crypto_provider_handle_t, crypto_mechanism_t *,
2130Sstevel@tonic-gate     crypto_key_t *);
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate static crypto_key_ops_t des_key_ops = {
2160Sstevel@tonic-gate 	NULL,
2170Sstevel@tonic-gate 	NULL,
2180Sstevel@tonic-gate 	NULL,
2190Sstevel@tonic-gate 	NULL,
2200Sstevel@tonic-gate 	NULL,
2210Sstevel@tonic-gate 	des_key_check
2220Sstevel@tonic-gate };
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate static crypto_ops_t des_crypto_ops = {
2250Sstevel@tonic-gate 	&des_control_ops,
2260Sstevel@tonic-gate 	NULL,
2270Sstevel@tonic-gate 	&des_cipher_ops,
2280Sstevel@tonic-gate 	NULL,
2290Sstevel@tonic-gate 	NULL,
2300Sstevel@tonic-gate 	NULL,
2310Sstevel@tonic-gate 	NULL,
2320Sstevel@tonic-gate 	NULL,
2330Sstevel@tonic-gate 	NULL,
2340Sstevel@tonic-gate 	NULL,
2350Sstevel@tonic-gate 	NULL,
2360Sstevel@tonic-gate 	&des_key_ops,
2370Sstevel@tonic-gate 	NULL,
2380Sstevel@tonic-gate 	&des_ctx_ops
2390Sstevel@tonic-gate };
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate static crypto_provider_info_t des_prov_info = {
2420Sstevel@tonic-gate 	CRYPTO_SPI_VERSION_1,
2430Sstevel@tonic-gate 	"DES Software Provider",
2440Sstevel@tonic-gate 	CRYPTO_SW_PROVIDER,
2450Sstevel@tonic-gate 	{&modlinkage},
2460Sstevel@tonic-gate 	NULL,
2470Sstevel@tonic-gate 	&des_crypto_ops,
2480Sstevel@tonic-gate 	sizeof (des_mech_info_tab)/sizeof (crypto_mech_info_t),
2490Sstevel@tonic-gate 	des_mech_info_tab
2500Sstevel@tonic-gate };
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate static crypto_kcf_provider_handle_t des_prov_handle = NULL;
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate int
2550Sstevel@tonic-gate _init(void)
2560Sstevel@tonic-gate {
2570Sstevel@tonic-gate 	int ret;
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	if ((ret = mod_install(&modlinkage)) != 0)
2600Sstevel@tonic-gate 		return (ret);
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	/*
2630Sstevel@tonic-gate 	 * Register with KCF. If the registration fails, log an
2640Sstevel@tonic-gate 	 * error but do not uninstall the module, since the functionality
2650Sstevel@tonic-gate 	 * provided by misc/des should still be available.
2660Sstevel@tonic-gate 	 */
2670Sstevel@tonic-gate 	if ((ret = crypto_register_provider(&des_prov_info,
2680Sstevel@tonic-gate 	    &des_prov_handle)) != CRYPTO_SUCCESS) {
2690Sstevel@tonic-gate 		cmn_err(CE_WARN, "des _init: crypto_register_provider() "
2700Sstevel@tonic-gate 		    "failed (0x%x)", ret);
2710Sstevel@tonic-gate 	}
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	return (0);
2740Sstevel@tonic-gate }
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate int
2780Sstevel@tonic-gate _info(struct modinfo *modinfop)
2790Sstevel@tonic-gate {
2800Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2810Sstevel@tonic-gate }
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate /*
2840Sstevel@tonic-gate  * Copy 8 bytes
2850Sstevel@tonic-gate  */
2860Sstevel@tonic-gate #define	COPY8(src, dst) { \
2870Sstevel@tonic-gate 	char *a = (char *)dst; \
2880Sstevel@tonic-gate 	char *b = (char *)src; \
2890Sstevel@tonic-gate 	*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
2900Sstevel@tonic-gate 	*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
2910Sstevel@tonic-gate }
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate /*
2940Sstevel@tonic-gate  * Copy multiple of 8 bytes
2950Sstevel@tonic-gate  */
2960Sstevel@tonic-gate #define	DESCOPY(src, dst, len) { \
2970Sstevel@tonic-gate 	char *a = (char *)dst; \
2980Sstevel@tonic-gate 	char *b = (char *)src; \
2990Sstevel@tonic-gate 	int i; \
3000Sstevel@tonic-gate 	for (i = (size_t)len; i > 0; i -= 8) { \
3010Sstevel@tonic-gate 		*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
3020Sstevel@tonic-gate 		*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
3030Sstevel@tonic-gate 	} \
3040Sstevel@tonic-gate }
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate /*
3070Sstevel@tonic-gate  * CBC mode encryption
3080Sstevel@tonic-gate  */
3090Sstevel@tonic-gate /* ARGSUSED */
3100Sstevel@tonic-gate int
3110Sstevel@tonic-gate cbc_crypt(char *key, char *buf, size_t len, unsigned int mode, char *ivec)
3120Sstevel@tonic-gate {
3130Sstevel@tonic-gate 	int err = 0;
3140Sstevel@tonic-gate /* EXPORT DELETE START */
3150Sstevel@tonic-gate 	struct desparams dp;
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	dp.des_mode = CBC;
3180Sstevel@tonic-gate 	COPY8(ivec, dp.des_ivec);
3190Sstevel@tonic-gate 	err = common_crypt(key, buf, len, mode, &dp);
3200Sstevel@tonic-gate 	COPY8(dp.des_ivec, ivec);
3210Sstevel@tonic-gate /* EXPORT DELETE END */
3220Sstevel@tonic-gate 	return (err);
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate /*
3270Sstevel@tonic-gate  * ECB mode encryption
3280Sstevel@tonic-gate  */
3290Sstevel@tonic-gate /* ARGSUSED */
3300Sstevel@tonic-gate int
3310Sstevel@tonic-gate ecb_crypt(char *key, char *buf, size_t len, unsigned int mode)
3320Sstevel@tonic-gate {
3330Sstevel@tonic-gate 	int err = 0;
3340Sstevel@tonic-gate /* EXPORT DELETE START */
3350Sstevel@tonic-gate 	struct desparams dp;
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	dp.des_mode = ECB;
3380Sstevel@tonic-gate 	err = common_crypt(key, buf, len, mode, &dp);
3390Sstevel@tonic-gate /* EXPORT DELETE END */
3400Sstevel@tonic-gate 	return (err);
3410Sstevel@tonic-gate }
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate /* EXPORT DELETE START */
3460Sstevel@tonic-gate /*
3470Sstevel@tonic-gate  * Common code to cbc_crypt() & ecb_crypt()
3480Sstevel@tonic-gate  */
3490Sstevel@tonic-gate static int
3500Sstevel@tonic-gate common_crypt(char *key, char *buf, size_t len, unsigned int mode,
3510Sstevel@tonic-gate     struct desparams *desp)
3520Sstevel@tonic-gate {
3530Sstevel@tonic-gate 	int desdev;
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 	if ((len % 8) != 0 || len > DES_MAXDATA)
3560Sstevel@tonic-gate 		return (DESERR_BADPARAM);
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	desp->des_dir =
3590Sstevel@tonic-gate 	    ((mode & DES_DIRMASK) == DES_ENCRYPT) ? ENCRYPT : DECRYPT;
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	desdev = mode & DES_DEVMASK;
3620Sstevel@tonic-gate 	COPY8(key, desp->des_key);
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate #ifdef sun_hardware
3650Sstevel@tonic-gate 	if (desdev == DES_HW) {
3660Sstevel@tonic-gate 		int res;
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 		if (g_desfd < 0 &&
3690Sstevel@tonic-gate 		    (g_desfd == -1 || (g_desfd = getdesfd()) < 0))
3700Sstevel@tonic-gate 				goto software;	/* no hardware device */
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 		/*
3730Sstevel@tonic-gate 		 * hardware
3740Sstevel@tonic-gate 		 */
3750Sstevel@tonic-gate 		desp->des_len = len;
3760Sstevel@tonic-gate 		if (len <= DES_QUICKLEN) {
3770Sstevel@tonic-gate 			DESCOPY(buf, desp->des_data, len);
3780Sstevel@tonic-gate 			res = ioctl(g_desfd, DESIOCQUICK, (char *)desp);
3790Sstevel@tonic-gate 			DESCOPY(desp->des_data, buf, len);
3800Sstevel@tonic-gate 		} else {
3810Sstevel@tonic-gate 			desp->des_buf = (uchar_t *)buf;
3820Sstevel@tonic-gate 			res = ioctl(g_desfd, DESIOCBLOCK, (char *)desp);
3830Sstevel@tonic-gate 		}
3840Sstevel@tonic-gate 		return (res == 0 ? DESERR_NONE : DESERR_HWERROR);
3850Sstevel@tonic-gate 	}
3860Sstevel@tonic-gate software:
3870Sstevel@tonic-gate #endif
3880Sstevel@tonic-gate 	/*
3890Sstevel@tonic-gate 	 * software
3900Sstevel@tonic-gate 	 */
3910Sstevel@tonic-gate 	if (!_des_crypt(buf, len, desp))
3920Sstevel@tonic-gate 		return (DESERR_HWERROR);
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 	return (desdev == DES_SW ? DESERR_NONE : DESERR_NOHWDEVICE);
3950Sstevel@tonic-gate }
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate /*
3980Sstevel@tonic-gate  * Initialize key schedules for DES and DES3
3990Sstevel@tonic-gate  */
4000Sstevel@tonic-gate static int
4010Sstevel@tonic-gate init_keysched(crypto_key_t *key, void *newbie, des_strength_t strength)
4020Sstevel@tonic-gate {
4030Sstevel@tonic-gate 	uint8_t corrected_key[DES3_KEYSIZE];
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	/*
4060Sstevel@tonic-gate 	 * Only keys by value are supported by this module.
4070Sstevel@tonic-gate 	 */
4080Sstevel@tonic-gate 	switch (key->ck_format) {
4090Sstevel@tonic-gate 	case CRYPTO_KEY_RAW:
41010444SVladimir.Kotal@Sun.COM 		if (strength == DES && key->ck_length != DES_MAXBITS)
4110Sstevel@tonic-gate 			return (CRYPTO_KEY_SIZE_RANGE);
41210444SVladimir.Kotal@Sun.COM 		if (strength == DES3 && key->ck_length != DES3_MAXBITS)
4130Sstevel@tonic-gate 			return (CRYPTO_KEY_SIZE_RANGE);
4140Sstevel@tonic-gate 		break;
4150Sstevel@tonic-gate 	default:
4160Sstevel@tonic-gate 		return (CRYPTO_KEY_TYPE_INCONSISTENT);
4170Sstevel@tonic-gate 	}
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 	/*
4200Sstevel@tonic-gate 	 * Fix parity bits.
4210Sstevel@tonic-gate 	 * Initialize key schedule even if key is weak.
4220Sstevel@tonic-gate 	 */
4232439Sizick 	if (key->ck_data == NULL)
4242439Sizick 		return (CRYPTO_ARGUMENTS_BAD);
4250Sstevel@tonic-gate 
4262439Sizick 	des_parity_fix(key->ck_data, strength, corrected_key);
4270Sstevel@tonic-gate 	des_init_keysched(corrected_key, strength, newbie);
4280Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
4290Sstevel@tonic-gate }
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate /* EXPORT DELETE END */
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate /*
4340Sstevel@tonic-gate  * KCF software provider control entry points.
4350Sstevel@tonic-gate  */
4360Sstevel@tonic-gate /* ARGSUSED */
4370Sstevel@tonic-gate static void
4380Sstevel@tonic-gate des_provider_status(crypto_provider_handle_t provider, uint_t *status)
4390Sstevel@tonic-gate {
4400Sstevel@tonic-gate 	*status = CRYPTO_PROVIDER_READY;
4410Sstevel@tonic-gate }
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate /*
4440Sstevel@tonic-gate  * KCF software provider encrypt entry points.
4450Sstevel@tonic-gate  */
4460Sstevel@tonic-gate static int
4470Sstevel@tonic-gate des_common_init(crypto_ctx_t *ctx, crypto_mechanism_t *mechanism,
4480Sstevel@tonic-gate     crypto_key_t *key, crypto_spi_ctx_template_t template,
4490Sstevel@tonic-gate     crypto_req_handle_t req)
4500Sstevel@tonic-gate {
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate /* EXPORT DELETE START */
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 	des_strength_t strength;
4557188Smcpowers 	des_ctx_t *des_ctx = NULL;
4560Sstevel@tonic-gate 	int rv;
4570Sstevel@tonic-gate 	int kmflag;
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	/*
4600Sstevel@tonic-gate 	 * Only keys by value are supported by this module.
4610Sstevel@tonic-gate 	 */
4620Sstevel@tonic-gate 	if (key->ck_format != CRYPTO_KEY_RAW) {
4630Sstevel@tonic-gate 		return (CRYPTO_KEY_TYPE_INCONSISTENT);
4640Sstevel@tonic-gate 	}
4650Sstevel@tonic-gate 
4667188Smcpowers 	kmflag = crypto_kmflag(req);
4670Sstevel@tonic-gate 	/* Check mechanism type and parameter length */
4680Sstevel@tonic-gate 	switch (mechanism->cm_type) {
4690Sstevel@tonic-gate 	case DES_ECB_MECH_INFO_TYPE:
4707188Smcpowers 		des_ctx = ecb_alloc_ctx(kmflag);
4717188Smcpowers 		/* FALLTHRU */
4720Sstevel@tonic-gate 	case DES_CBC_MECH_INFO_TYPE:
4730Sstevel@tonic-gate 		if (mechanism->cm_param != NULL &&
4740Sstevel@tonic-gate 		    mechanism->cm_param_len != DES_BLOCK_LEN)
4750Sstevel@tonic-gate 			return (CRYPTO_MECHANISM_PARAM_INVALID);
47610444SVladimir.Kotal@Sun.COM 		if (key->ck_length != DES_MAXBITS)
4770Sstevel@tonic-gate 			return (CRYPTO_KEY_SIZE_RANGE);
4780Sstevel@tonic-gate 		strength = DES;
4797188Smcpowers 		if (des_ctx == NULL)
4807188Smcpowers 			des_ctx = cbc_alloc_ctx(kmflag);
4810Sstevel@tonic-gate 		break;
4820Sstevel@tonic-gate 	case DES3_ECB_MECH_INFO_TYPE:
4837188Smcpowers 		des_ctx = ecb_alloc_ctx(kmflag);
4847188Smcpowers 		/* FALLTHRU */
4850Sstevel@tonic-gate 	case DES3_CBC_MECH_INFO_TYPE:
4860Sstevel@tonic-gate 		if (mechanism->cm_param != NULL &&
4870Sstevel@tonic-gate 		    mechanism->cm_param_len != DES_BLOCK_LEN)
4880Sstevel@tonic-gate 			return (CRYPTO_MECHANISM_PARAM_INVALID);
48910444SVladimir.Kotal@Sun.COM 		if (key->ck_length != DES3_MAXBITS)
4900Sstevel@tonic-gate 			return (CRYPTO_KEY_SIZE_RANGE);
4910Sstevel@tonic-gate 		strength = DES3;
4927188Smcpowers 		if (des_ctx == NULL)
4937188Smcpowers 			des_ctx = cbc_alloc_ctx(kmflag);
4940Sstevel@tonic-gate 		break;
4950Sstevel@tonic-gate 	default:
4960Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
4970Sstevel@tonic-gate 	}
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	if ((rv = des_common_init_ctx(des_ctx, template, mechanism, key,
5000Sstevel@tonic-gate 	    strength, kmflag)) != CRYPTO_SUCCESS) {
5017188Smcpowers 		crypto_free_mode_ctx(des_ctx);
5020Sstevel@tonic-gate 		return (rv);
5030Sstevel@tonic-gate 	}
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 	ctx->cc_provider_private = des_ctx;
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate /* EXPORT DELETE END */
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
5100Sstevel@tonic-gate }
5110Sstevel@tonic-gate 
5127188Smcpowers static void
5137188Smcpowers des_copy_block64(uint8_t *in, uint64_t *out)
5140Sstevel@tonic-gate {
5157188Smcpowers 	if (IS_P2ALIGNED(in, sizeof (uint64_t))) {
5167188Smcpowers 		/* LINTED: pointer alignment */
5177188Smcpowers 		out[0] = *(uint64_t *)&in[0];
5187188Smcpowers 	} else {
5197188Smcpowers 		uint64_t tmp64;
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate #ifdef _BIG_ENDIAN
5227188Smcpowers 		tmp64 = (((uint64_t)in[0] << 56) |
5237188Smcpowers 		    ((uint64_t)in[1] << 48) |
5247188Smcpowers 		    ((uint64_t)in[2] << 40) |
5257188Smcpowers 		    ((uint64_t)in[3] << 32) |
5267188Smcpowers 		    ((uint64_t)in[4] << 24) |
5277188Smcpowers 		    ((uint64_t)in[5] << 16) |
5287188Smcpowers 		    ((uint64_t)in[6] << 8) |
5297188Smcpowers 		    (uint64_t)in[7]);
5300Sstevel@tonic-gate #else
5317188Smcpowers 		tmp64 = (((uint64_t)in[7] << 56) |
5327188Smcpowers 		    ((uint64_t)in[6] << 48) |
5337188Smcpowers 		    ((uint64_t)in[5] << 40) |
5347188Smcpowers 		    ((uint64_t)in[4] << 32) |
5357188Smcpowers 		    ((uint64_t)in[3] << 24) |
5367188Smcpowers 		    ((uint64_t)in[2] << 16) |
5377188Smcpowers 		    ((uint64_t)in[1] << 8) |
5387188Smcpowers 		    (uint64_t)in[0]);
5390Sstevel@tonic-gate #endif /* _BIG_ENDIAN */
5400Sstevel@tonic-gate 
5417188Smcpowers 		out[0] = tmp64;
5420Sstevel@tonic-gate 	}
5430Sstevel@tonic-gate }
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate /* ARGSUSED */
5460Sstevel@tonic-gate static int
5470Sstevel@tonic-gate des_encrypt(crypto_ctx_t *ctx, crypto_data_t *plaintext,
5480Sstevel@tonic-gate     crypto_data_t *ciphertext, crypto_req_handle_t req)
5490Sstevel@tonic-gate {
5500Sstevel@tonic-gate 	int ret;
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate /* EXPORT DELETE START */
5530Sstevel@tonic-gate 	des_ctx_t *des_ctx;
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	/*
5560Sstevel@tonic-gate 	 * Plaintext must be a multiple of the block size.
5570Sstevel@tonic-gate 	 * This test only works for non-padded mechanisms
5580Sstevel@tonic-gate 	 * when blocksize is 2^N.
5590Sstevel@tonic-gate 	 */
5600Sstevel@tonic-gate 	if ((plaintext->cd_length & (DES_BLOCK_LEN - 1)) != 0)
5610Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
5640Sstevel@tonic-gate 	des_ctx = ctx->cc_provider_private;
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	DES_ARG_INPLACE(plaintext, ciphertext);
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	/*
5690Sstevel@tonic-gate 	 * We need to just return the length needed to store the output.
5700Sstevel@tonic-gate 	 * We should not destroy the context for the following case.
5710Sstevel@tonic-gate 	 */
5720Sstevel@tonic-gate 	if (ciphertext->cd_length < plaintext->cd_length) {
5730Sstevel@tonic-gate 		ciphertext->cd_length = plaintext->cd_length;
5740Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
5750Sstevel@tonic-gate 	}
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 	/*
5780Sstevel@tonic-gate 	 * Do an update on the specified input data.
5790Sstevel@tonic-gate 	 */
5800Sstevel@tonic-gate 	ret = des_encrypt_update(ctx, plaintext, ciphertext, req);
5810Sstevel@tonic-gate 	ASSERT(des_ctx->dc_remainder_len == 0);
5820Sstevel@tonic-gate 	(void) des_free_context(ctx);
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate /* EXPORT DELETE END */
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	/* LINTED */
5870Sstevel@tonic-gate 	return (ret);
5880Sstevel@tonic-gate }
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate /* ARGSUSED */
5910Sstevel@tonic-gate static int
5920Sstevel@tonic-gate des_decrypt(crypto_ctx_t *ctx, crypto_data_t *ciphertext,
5930Sstevel@tonic-gate     crypto_data_t *plaintext, crypto_req_handle_t req)
5940Sstevel@tonic-gate {
5950Sstevel@tonic-gate 	int ret;
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate /* EXPORT DELETE START */
5980Sstevel@tonic-gate 	des_ctx_t *des_ctx;
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	/*
6010Sstevel@tonic-gate 	 * Ciphertext must be a multiple of the block size.
6020Sstevel@tonic-gate 	 * This test only works for non-padded mechanisms
6030Sstevel@tonic-gate 	 * when blocksize is 2^N.
6040Sstevel@tonic-gate 	 */
6050Sstevel@tonic-gate 	if ((ciphertext->cd_length & (DES_BLOCK_LEN - 1)) != 0)
6060Sstevel@tonic-gate 		return (CRYPTO_ENCRYPTED_DATA_LEN_RANGE);
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
6090Sstevel@tonic-gate 	des_ctx = ctx->cc_provider_private;
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	DES_ARG_INPLACE(ciphertext, plaintext);
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	/*
6140Sstevel@tonic-gate 	 * We need to just return the length needed to store the output.
6150Sstevel@tonic-gate 	 * We should not destroy the context for the following case.
6160Sstevel@tonic-gate 	 */
6170Sstevel@tonic-gate 	if (plaintext->cd_length < ciphertext->cd_length) {
6180Sstevel@tonic-gate 		plaintext->cd_length = ciphertext->cd_length;
6190Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
6200Sstevel@tonic-gate 	}
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	/*
6230Sstevel@tonic-gate 	 * Do an update on the specified input data.
6240Sstevel@tonic-gate 	 */
6250Sstevel@tonic-gate 	ret = des_decrypt_update(ctx, ciphertext, plaintext, req);
6260Sstevel@tonic-gate 	ASSERT(des_ctx->dc_remainder_len == 0);
6270Sstevel@tonic-gate 	(void) des_free_context(ctx);
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate /* EXPORT DELETE END */
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 	/* LINTED */
6320Sstevel@tonic-gate 	return (ret);
6330Sstevel@tonic-gate }
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate /* ARGSUSED */
6360Sstevel@tonic-gate static int
6370Sstevel@tonic-gate des_encrypt_update(crypto_ctx_t *ctx, crypto_data_t *plaintext,
6380Sstevel@tonic-gate     crypto_data_t *ciphertext, crypto_req_handle_t req)
6390Sstevel@tonic-gate {
6400Sstevel@tonic-gate 	off_t saved_offset;
6410Sstevel@tonic-gate 	size_t saved_length, out_len;
6420Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate /* EXPORT DELETE START */
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 	DES_ARG_INPLACE(plaintext, ciphertext);
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 	/* compute number of bytes that will hold the ciphertext */
6510Sstevel@tonic-gate 	out_len = ((des_ctx_t *)ctx->cc_provider_private)->dc_remainder_len;
6520Sstevel@tonic-gate 	out_len += plaintext->cd_length;
6530Sstevel@tonic-gate 	out_len &= ~(DES_BLOCK_LEN - 1);
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 	/* return length needed to store the output */
6560Sstevel@tonic-gate 	if (ciphertext->cd_length < out_len) {
6570Sstevel@tonic-gate 		ciphertext->cd_length = out_len;
6580Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
6590Sstevel@tonic-gate 	}
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	saved_offset = ciphertext->cd_offset;
6620Sstevel@tonic-gate 	saved_length = ciphertext->cd_length;
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	/*
6650Sstevel@tonic-gate 	 * Do the DES update on the specified input data.
6660Sstevel@tonic-gate 	 */
6670Sstevel@tonic-gate 	switch (plaintext->cd_format) {
6680Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
6697188Smcpowers 		ret = crypto_update_iov(ctx->cc_provider_private,
6707188Smcpowers 		    plaintext, ciphertext, des_encrypt_contiguous_blocks,
6717188Smcpowers 		    des_copy_block64);
6720Sstevel@tonic-gate 		break;
6730Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
6747188Smcpowers 		ret = crypto_update_uio(ctx->cc_provider_private,
6757188Smcpowers 		    plaintext, ciphertext, des_encrypt_contiguous_blocks,
6767188Smcpowers 		    des_copy_block64);
6770Sstevel@tonic-gate 		break;
6780Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
6797188Smcpowers 		ret = crypto_update_mp(ctx->cc_provider_private,
6807188Smcpowers 		    plaintext, ciphertext, des_encrypt_contiguous_blocks,
6817188Smcpowers 		    des_copy_block64);
6820Sstevel@tonic-gate 		break;
6830Sstevel@tonic-gate 	default:
6840Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
6850Sstevel@tonic-gate 	}
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
6880Sstevel@tonic-gate 		if (plaintext != ciphertext)
6890Sstevel@tonic-gate 			ciphertext->cd_length =
6900Sstevel@tonic-gate 			    ciphertext->cd_offset - saved_offset;
6910Sstevel@tonic-gate 	} else {
6920Sstevel@tonic-gate 		ciphertext->cd_length = saved_length;
6930Sstevel@tonic-gate 	}
6940Sstevel@tonic-gate 	ciphertext->cd_offset = saved_offset;
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate /* EXPORT DELETE END */
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 	return (ret);
6990Sstevel@tonic-gate }
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate /* ARGSUSED */
7020Sstevel@tonic-gate static int
7030Sstevel@tonic-gate des_decrypt_update(crypto_ctx_t *ctx, crypto_data_t *ciphertext,
7040Sstevel@tonic-gate     crypto_data_t *plaintext, crypto_req_handle_t req)
7050Sstevel@tonic-gate {
7060Sstevel@tonic-gate 	off_t saved_offset;
7070Sstevel@tonic-gate 	size_t saved_length, out_len;
7080Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate /* EXPORT DELETE START */
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 	DES_ARG_INPLACE(ciphertext, plaintext);
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 	/* compute number of bytes that will hold the plaintext */
7170Sstevel@tonic-gate 	out_len = ((des_ctx_t *)ctx->cc_provider_private)->dc_remainder_len;
7180Sstevel@tonic-gate 	out_len += ciphertext->cd_length;
7190Sstevel@tonic-gate 	out_len &= ~(DES_BLOCK_LEN - 1);
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 	/* return length needed to store the output */
7220Sstevel@tonic-gate 	if (plaintext->cd_length < out_len) {
7230Sstevel@tonic-gate 		plaintext->cd_length = out_len;
7240Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
7250Sstevel@tonic-gate 	}
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 	saved_offset = plaintext->cd_offset;
7280Sstevel@tonic-gate 	saved_length = plaintext->cd_length;
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 	/*
7310Sstevel@tonic-gate 	 * Do the DES update on the specified input data.
7320Sstevel@tonic-gate 	 */
7330Sstevel@tonic-gate 	switch (ciphertext->cd_format) {
7340Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
7357188Smcpowers 		ret = crypto_update_iov(ctx->cc_provider_private,
7367188Smcpowers 		    ciphertext, plaintext, des_decrypt_contiguous_blocks,
7377188Smcpowers 		    des_copy_block64);
7380Sstevel@tonic-gate 		break;
7390Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
7407188Smcpowers 		ret = crypto_update_uio(ctx->cc_provider_private,
7417188Smcpowers 		    ciphertext, plaintext, des_decrypt_contiguous_blocks,
7427188Smcpowers 		    des_copy_block64);
7430Sstevel@tonic-gate 		break;
7440Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
7457188Smcpowers 		ret = crypto_update_mp(ctx->cc_provider_private,
7467188Smcpowers 		    ciphertext, plaintext, des_decrypt_contiguous_blocks,
7477188Smcpowers 		    des_copy_block64);
7480Sstevel@tonic-gate 		break;
7490Sstevel@tonic-gate 	default:
7500Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
7510Sstevel@tonic-gate 	}
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
7540Sstevel@tonic-gate 		if (ciphertext != plaintext)
7550Sstevel@tonic-gate 			plaintext->cd_length =
7560Sstevel@tonic-gate 			    plaintext->cd_offset - saved_offset;
7570Sstevel@tonic-gate 	} else {
7580Sstevel@tonic-gate 		plaintext->cd_length = saved_length;
7590Sstevel@tonic-gate 	}
7600Sstevel@tonic-gate 	plaintext->cd_offset = saved_offset;
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate /* EXPORT DELETE END */
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	return (ret);
7650Sstevel@tonic-gate }
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate /* ARGSUSED */
7680Sstevel@tonic-gate static int
7690Sstevel@tonic-gate des_encrypt_final(crypto_ctx_t *ctx, crypto_data_t *ciphertext,
7700Sstevel@tonic-gate     crypto_req_handle_t req)
7710Sstevel@tonic-gate {
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate /* EXPORT DELETE START */
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	des_ctx_t *des_ctx;
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
7780Sstevel@tonic-gate 	des_ctx = ctx->cc_provider_private;
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 	/*
7810Sstevel@tonic-gate 	 * There must be no unprocessed plaintext.
7820Sstevel@tonic-gate 	 * This happens if the length of the last data is
7830Sstevel@tonic-gate 	 * not a multiple of the DES block length.
7840Sstevel@tonic-gate 	 */
7850Sstevel@tonic-gate 	if (des_ctx->dc_remainder_len > 0)
7860Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 	(void) des_free_context(ctx);
7890Sstevel@tonic-gate 	ciphertext->cd_length = 0;
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate /* EXPORT DELETE END */
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
7940Sstevel@tonic-gate }
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate /* ARGSUSED */
7970Sstevel@tonic-gate static int
7980Sstevel@tonic-gate des_decrypt_final(crypto_ctx_t *ctx, crypto_data_t *plaintext,
7990Sstevel@tonic-gate     crypto_req_handle_t req)
8000Sstevel@tonic-gate {
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate /* EXPORT DELETE START */
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 	des_ctx_t *des_ctx;
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 	/*
8100Sstevel@tonic-gate 	 * There must be no unprocessed ciphertext.
8110Sstevel@tonic-gate 	 * This happens if the length of the last ciphertext is
8120Sstevel@tonic-gate 	 * not a multiple of the DES block length.
8130Sstevel@tonic-gate 	 */
8140Sstevel@tonic-gate 	if (des_ctx->dc_remainder_len > 0)
8150Sstevel@tonic-gate 		return (CRYPTO_ENCRYPTED_DATA_LEN_RANGE);
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	(void) des_free_context(ctx);
8180Sstevel@tonic-gate 	plaintext->cd_length = 0;
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate /* EXPORT DELETE END */
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
8230Sstevel@tonic-gate }
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate /* ARGSUSED */
8260Sstevel@tonic-gate static int
8270Sstevel@tonic-gate des_encrypt_atomic(crypto_provider_handle_t provider,
8280Sstevel@tonic-gate     crypto_session_id_t session_id, crypto_mechanism_t *mechanism,
8290Sstevel@tonic-gate     crypto_key_t *key, crypto_data_t *plaintext, crypto_data_t *ciphertext,
8300Sstevel@tonic-gate     crypto_spi_ctx_template_t template, crypto_req_handle_t req)
8310Sstevel@tonic-gate {
8320Sstevel@tonic-gate 	int ret;
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate /* EXPORT DELETE START */
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	des_ctx_t des_ctx;		/* on the stack */
8370Sstevel@tonic-gate 	des_strength_t strength;
8380Sstevel@tonic-gate 	off_t saved_offset;
8390Sstevel@tonic-gate 	size_t saved_length;
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 	DES_ARG_INPLACE(plaintext, ciphertext);
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 	/*
8440Sstevel@tonic-gate 	 * Plaintext must be a multiple of the block size.
8450Sstevel@tonic-gate 	 * This test only works for non-padded mechanisms
8460Sstevel@tonic-gate 	 * when blocksize is 2^N.
8470Sstevel@tonic-gate 	 */
8480Sstevel@tonic-gate 	if ((plaintext->cd_length & (DES_BLOCK_LEN - 1)) != 0)
8490Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 	/* return length needed to store the output */
8520Sstevel@tonic-gate 	if (ciphertext->cd_length < plaintext->cd_length) {
8530Sstevel@tonic-gate 		ciphertext->cd_length = plaintext->cd_length;
8540Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
8550Sstevel@tonic-gate 	}
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 	/* Check mechanism type and parameter length */
8580Sstevel@tonic-gate 	switch (mechanism->cm_type) {
8590Sstevel@tonic-gate 	case DES_ECB_MECH_INFO_TYPE:
8600Sstevel@tonic-gate 	case DES_CBC_MECH_INFO_TYPE:
8610Sstevel@tonic-gate 		if (mechanism->cm_param_len > 0 &&
8620Sstevel@tonic-gate 		    mechanism->cm_param_len != DES_BLOCK_LEN)
8630Sstevel@tonic-gate 			return (CRYPTO_MECHANISM_PARAM_INVALID);
8640Sstevel@tonic-gate 		if (key->ck_length != DES_MINBITS)
8650Sstevel@tonic-gate 			return (CRYPTO_KEY_SIZE_RANGE);
8660Sstevel@tonic-gate 		strength = DES;
8670Sstevel@tonic-gate 		break;
8680Sstevel@tonic-gate 	case DES3_ECB_MECH_INFO_TYPE:
8690Sstevel@tonic-gate 	case DES3_CBC_MECH_INFO_TYPE:
8700Sstevel@tonic-gate 		if (mechanism->cm_param_len > 0 &&
8710Sstevel@tonic-gate 		    mechanism->cm_param_len != DES_BLOCK_LEN)
8720Sstevel@tonic-gate 			return (CRYPTO_MECHANISM_PARAM_INVALID);
87310444SVladimir.Kotal@Sun.COM 		if (key->ck_length != DES3_MAXBITS)
8740Sstevel@tonic-gate 			return (CRYPTO_KEY_SIZE_RANGE);
8750Sstevel@tonic-gate 		strength = DES3;
8760Sstevel@tonic-gate 		break;
8770Sstevel@tonic-gate 	default:
8780Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
8790Sstevel@tonic-gate 	}
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 	bzero(&des_ctx, sizeof (des_ctx_t));
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	if ((ret = des_common_init_ctx(&des_ctx, template, mechanism, key,
8840Sstevel@tonic-gate 	    strength, crypto_kmflag(req))) != CRYPTO_SUCCESS) {
8850Sstevel@tonic-gate 		return (ret);
8860Sstevel@tonic-gate 	}
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 	saved_offset = ciphertext->cd_offset;
8890Sstevel@tonic-gate 	saved_length = ciphertext->cd_length;
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 	/*
8920Sstevel@tonic-gate 	 * Do the update on the specified input data.
8930Sstevel@tonic-gate 	 */
8940Sstevel@tonic-gate 	switch (plaintext->cd_format) {
8950Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
8967188Smcpowers 		ret = crypto_update_iov(&des_ctx, plaintext, ciphertext,
8977188Smcpowers 		    des_encrypt_contiguous_blocks, des_copy_block64);
8980Sstevel@tonic-gate 		break;
8990Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
9007188Smcpowers 		ret = crypto_update_uio(&des_ctx, plaintext, ciphertext,
9017188Smcpowers 		    des_encrypt_contiguous_blocks, des_copy_block64);
9020Sstevel@tonic-gate 		break;
9030Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
9047188Smcpowers 		ret = crypto_update_mp(&des_ctx, plaintext, ciphertext,
9057188Smcpowers 		    des_encrypt_contiguous_blocks, des_copy_block64);
9060Sstevel@tonic-gate 		break;
9070Sstevel@tonic-gate 	default:
9080Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
9090Sstevel@tonic-gate 	}
9100Sstevel@tonic-gate 
9117188Smcpowers 	if (des_ctx.dc_flags & PROVIDER_OWNS_KEY_SCHEDULE) {
9120Sstevel@tonic-gate 		bzero(des_ctx.dc_keysched, des_ctx.dc_keysched_len);
9130Sstevel@tonic-gate 		kmem_free(des_ctx.dc_keysched, des_ctx.dc_keysched_len);
9140Sstevel@tonic-gate 	}
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
9170Sstevel@tonic-gate 		ASSERT(des_ctx.dc_remainder_len == 0);
9180Sstevel@tonic-gate 		if (plaintext != ciphertext)
9190Sstevel@tonic-gate 			ciphertext->cd_length =
9200Sstevel@tonic-gate 			    ciphertext->cd_offset - saved_offset;
9210Sstevel@tonic-gate 	} else {
9220Sstevel@tonic-gate 		ciphertext->cd_length = saved_length;
9230Sstevel@tonic-gate 	}
9240Sstevel@tonic-gate 	ciphertext->cd_offset = saved_offset;
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate /* EXPORT DELETE END */
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 	/* LINTED */
9290Sstevel@tonic-gate 	return (ret);
9300Sstevel@tonic-gate }
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate /* ARGSUSED */
9330Sstevel@tonic-gate static int
9340Sstevel@tonic-gate des_decrypt_atomic(crypto_provider_handle_t provider,
9350Sstevel@tonic-gate     crypto_session_id_t session_id, crypto_mechanism_t *mechanism,
9360Sstevel@tonic-gate     crypto_key_t *key, crypto_data_t *ciphertext, crypto_data_t *plaintext,
9370Sstevel@tonic-gate     crypto_spi_ctx_template_t template, crypto_req_handle_t req)
9380Sstevel@tonic-gate {
9390Sstevel@tonic-gate 	int ret;
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate /* EXPORT DELETE START */
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate 	des_ctx_t des_ctx;	/* on the stack */
9440Sstevel@tonic-gate 	des_strength_t strength;
9450Sstevel@tonic-gate 	off_t saved_offset;
9460Sstevel@tonic-gate 	size_t saved_length;
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 	DES_ARG_INPLACE(ciphertext, plaintext);
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate 	/*
9510Sstevel@tonic-gate 	 * Ciphertext must be a multiple of the block size.
9520Sstevel@tonic-gate 	 * This test only works for non-padded mechanisms
9530Sstevel@tonic-gate 	 * when blocksize is 2^N.
9540Sstevel@tonic-gate 	 */
9550Sstevel@tonic-gate 	if ((ciphertext->cd_length & (DES_BLOCK_LEN - 1)) != 0)
9560Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate 	/* return length needed to store the output */
9590Sstevel@tonic-gate 	if (plaintext->cd_length < ciphertext->cd_length) {
9600Sstevel@tonic-gate 		plaintext->cd_length = ciphertext->cd_length;
9610Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
9620Sstevel@tonic-gate 	}
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 	/* Check mechanism type and parameter length */
9650Sstevel@tonic-gate 	switch (mechanism->cm_type) {
9660Sstevel@tonic-gate 	case DES_ECB_MECH_INFO_TYPE:
9670Sstevel@tonic-gate 	case DES_CBC_MECH_INFO_TYPE:
9680Sstevel@tonic-gate 		if (mechanism->cm_param_len > 0 &&
9690Sstevel@tonic-gate 		    mechanism->cm_param_len != DES_BLOCK_LEN)
9700Sstevel@tonic-gate 			return (CRYPTO_MECHANISM_PARAM_INVALID);
9710Sstevel@tonic-gate 		if (key->ck_length != DES_MINBITS)
9720Sstevel@tonic-gate 			return (CRYPTO_KEY_SIZE_RANGE);
9730Sstevel@tonic-gate 		strength = DES;
9740Sstevel@tonic-gate 		break;
9750Sstevel@tonic-gate 	case DES3_ECB_MECH_INFO_TYPE:
9760Sstevel@tonic-gate 	case DES3_CBC_MECH_INFO_TYPE:
9770Sstevel@tonic-gate 		if (mechanism->cm_param_len > 0 &&
9780Sstevel@tonic-gate 		    mechanism->cm_param_len != DES_BLOCK_LEN)
9790Sstevel@tonic-gate 			return (CRYPTO_MECHANISM_PARAM_INVALID);
98010444SVladimir.Kotal@Sun.COM 		if (key->ck_length != DES3_MAXBITS)
9810Sstevel@tonic-gate 			return (CRYPTO_KEY_SIZE_RANGE);
9820Sstevel@tonic-gate 		strength = DES3;
9830Sstevel@tonic-gate 		break;
9840Sstevel@tonic-gate 	default:
9850Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
9860Sstevel@tonic-gate 	}
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate 	bzero(&des_ctx, sizeof (des_ctx_t));
9890Sstevel@tonic-gate 
9900Sstevel@tonic-gate 	if ((ret = des_common_init_ctx(&des_ctx, template, mechanism, key,
9910Sstevel@tonic-gate 	    strength, crypto_kmflag(req))) != CRYPTO_SUCCESS) {
9920Sstevel@tonic-gate 		return (ret);
9930Sstevel@tonic-gate 	}
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate 	saved_offset = plaintext->cd_offset;
9960Sstevel@tonic-gate 	saved_length = plaintext->cd_length;
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 	/*
9990Sstevel@tonic-gate 	 * Do the update on the specified input data.
10000Sstevel@tonic-gate 	 */
10010Sstevel@tonic-gate 	switch (ciphertext->cd_format) {
10020Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
10037188Smcpowers 		ret = crypto_update_iov(&des_ctx, ciphertext, plaintext,
10047188Smcpowers 		    des_decrypt_contiguous_blocks, des_copy_block64);
10050Sstevel@tonic-gate 		break;
10060Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
10077188Smcpowers 		ret = crypto_update_uio(&des_ctx, ciphertext, plaintext,
10087188Smcpowers 		    des_decrypt_contiguous_blocks, des_copy_block64);
10090Sstevel@tonic-gate 		break;
10100Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
10117188Smcpowers 		ret = crypto_update_mp(&des_ctx, ciphertext, plaintext,
10127188Smcpowers 		    des_decrypt_contiguous_blocks, des_copy_block64);
10130Sstevel@tonic-gate 		break;
10140Sstevel@tonic-gate 	default:
10150Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
10160Sstevel@tonic-gate 	}
10170Sstevel@tonic-gate 
10187188Smcpowers 	if (des_ctx.dc_flags & PROVIDER_OWNS_KEY_SCHEDULE) {
10190Sstevel@tonic-gate 		bzero(des_ctx.dc_keysched, des_ctx.dc_keysched_len);
10200Sstevel@tonic-gate 		kmem_free(des_ctx.dc_keysched, des_ctx.dc_keysched_len);
10210Sstevel@tonic-gate 	}
10220Sstevel@tonic-gate 
10230Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
10240Sstevel@tonic-gate 		ASSERT(des_ctx.dc_remainder_len == 0);
10250Sstevel@tonic-gate 		if (ciphertext != plaintext)
10260Sstevel@tonic-gate 			plaintext->cd_length =
10270Sstevel@tonic-gate 			    plaintext->cd_offset - saved_offset;
10280Sstevel@tonic-gate 	} else {
10290Sstevel@tonic-gate 		plaintext->cd_length = saved_length;
10300Sstevel@tonic-gate 	}
10310Sstevel@tonic-gate 	plaintext->cd_offset = saved_offset;
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate /* EXPORT DELETE END */
10340Sstevel@tonic-gate 
10350Sstevel@tonic-gate 	/* LINTED */
10360Sstevel@tonic-gate 	return (ret);
10370Sstevel@tonic-gate }
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate /*
10400Sstevel@tonic-gate  * KCF software provider context template entry points.
10410Sstevel@tonic-gate  */
10420Sstevel@tonic-gate /* ARGSUSED */
10430Sstevel@tonic-gate static int
10440Sstevel@tonic-gate des_create_ctx_template(crypto_provider_handle_t provider,
10450Sstevel@tonic-gate     crypto_mechanism_t *mechanism, crypto_key_t *key,
10460Sstevel@tonic-gate     crypto_spi_ctx_template_t *tmpl, size_t *tmpl_size, crypto_req_handle_t req)
10470Sstevel@tonic-gate {
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate /* EXPORT DELETE START */
10500Sstevel@tonic-gate 
10510Sstevel@tonic-gate 	des_strength_t strength;
10520Sstevel@tonic-gate 	void *keysched;
10530Sstevel@tonic-gate 	size_t size;
10540Sstevel@tonic-gate 	int rv;
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate 	switch (mechanism->cm_type) {
10570Sstevel@tonic-gate 	case DES_ECB_MECH_INFO_TYPE:
10580Sstevel@tonic-gate 		strength = DES;
10590Sstevel@tonic-gate 		break;
10600Sstevel@tonic-gate 	case DES_CBC_MECH_INFO_TYPE:
10610Sstevel@tonic-gate 		strength = DES;
10620Sstevel@tonic-gate 		break;
10630Sstevel@tonic-gate 	case DES3_ECB_MECH_INFO_TYPE:
10640Sstevel@tonic-gate 		strength = DES3;
10650Sstevel@tonic-gate 		break;
10660Sstevel@tonic-gate 	case DES3_CBC_MECH_INFO_TYPE:
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 	if ((keysched = des_alloc_keysched(&size, strength,
10740Sstevel@tonic-gate 	    crypto_kmflag(req))) == NULL) {
10750Sstevel@tonic-gate 		return (CRYPTO_HOST_MEMORY);
10760Sstevel@tonic-gate 	}
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate 	/*
10790Sstevel@tonic-gate 	 * Initialize key schedule.  Key length information is stored
10800Sstevel@tonic-gate 	 * in the key.
10810Sstevel@tonic-gate 	 */
10820Sstevel@tonic-gate 	if ((rv = init_keysched(key, keysched, strength)) != CRYPTO_SUCCESS) {
10830Sstevel@tonic-gate 		bzero(keysched, size);
10840Sstevel@tonic-gate 		kmem_free(keysched, size);
10850Sstevel@tonic-gate 		return (rv);
10860Sstevel@tonic-gate 	}
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 	*tmpl = keysched;
10890Sstevel@tonic-gate 	*tmpl_size = size;
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate /* EXPORT DELETE END */
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
10940Sstevel@tonic-gate }
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate /* ARGSUSED */
10970Sstevel@tonic-gate static int
10980Sstevel@tonic-gate des_free_context(crypto_ctx_t *ctx)
10990Sstevel@tonic-gate {
11000Sstevel@tonic-gate 
11010Sstevel@tonic-gate /* EXPORT DELETE START */
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate 	des_ctx_t *des_ctx = ctx->cc_provider_private;
11040Sstevel@tonic-gate 
11050Sstevel@tonic-gate 	if (des_ctx != NULL) {
11067188Smcpowers 		if (des_ctx->dc_flags & PROVIDER_OWNS_KEY_SCHEDULE) {
11070Sstevel@tonic-gate 			ASSERT(des_ctx->dc_keysched_len != 0);
11080Sstevel@tonic-gate 			bzero(des_ctx->dc_keysched, des_ctx->dc_keysched_len);
11090Sstevel@tonic-gate 			kmem_free(des_ctx->dc_keysched,
11100Sstevel@tonic-gate 			    des_ctx->dc_keysched_len);
11110Sstevel@tonic-gate 		}
11127188Smcpowers 		crypto_free_mode_ctx(des_ctx);
11130Sstevel@tonic-gate 		ctx->cc_provider_private = NULL;
11140Sstevel@tonic-gate 	}
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate /* EXPORT DELETE END */
11170Sstevel@tonic-gate 
11180Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
11190Sstevel@tonic-gate }
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate /*
11220Sstevel@tonic-gate  * Pass it to des_keycheck() which will
11230Sstevel@tonic-gate  * fix it (parity bits), and check if the fixed key is weak.
11240Sstevel@tonic-gate  */
11250Sstevel@tonic-gate /* ARGSUSED */
11260Sstevel@tonic-gate static int
11270Sstevel@tonic-gate des_key_check(crypto_provider_handle_t pd, crypto_mechanism_t *mech,
11280Sstevel@tonic-gate     crypto_key_t *key)
11290Sstevel@tonic-gate {
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate /* EXPORT DELETE START */
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate 	int expectedkeylen;
11340Sstevel@tonic-gate 	des_strength_t strength;
11350Sstevel@tonic-gate 	uint8_t keydata[DES3_MAX_KEY_LEN];
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate 	if ((mech == NULL) || (key == NULL))
11380Sstevel@tonic-gate 		return (CRYPTO_ARGUMENTS_BAD);
11390Sstevel@tonic-gate 
11400Sstevel@tonic-gate 	switch (mech->cm_type) {
11410Sstevel@tonic-gate 	case DES_ECB_MECH_INFO_TYPE:
11420Sstevel@tonic-gate 	case DES_CBC_MECH_INFO_TYPE:
11430Sstevel@tonic-gate 		expectedkeylen = DES_MINBITS;
11440Sstevel@tonic-gate 		strength = DES;
11450Sstevel@tonic-gate 		break;
11460Sstevel@tonic-gate 	case DES3_ECB_MECH_INFO_TYPE:
11470Sstevel@tonic-gate 	case DES3_CBC_MECH_INFO_TYPE:
114810444SVladimir.Kotal@Sun.COM 		expectedkeylen = DES3_MAXBITS;
11490Sstevel@tonic-gate 		strength = DES3;
11500Sstevel@tonic-gate 		break;
11510Sstevel@tonic-gate 	default:
11520Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
11530Sstevel@tonic-gate 	}
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 	if (key->ck_format != CRYPTO_KEY_RAW)
11560Sstevel@tonic-gate 		return (CRYPTO_KEY_TYPE_INCONSISTENT);
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 	if (key->ck_length != expectedkeylen)
11590Sstevel@tonic-gate 		return (CRYPTO_KEY_SIZE_RANGE);
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate 	bcopy(key->ck_data, keydata, CRYPTO_BITS2BYTES(expectedkeylen));
11620Sstevel@tonic-gate 
11630Sstevel@tonic-gate 	if (des_keycheck(keydata, strength, key->ck_data) == B_FALSE)
11640Sstevel@tonic-gate 		return (CRYPTO_WEAK_KEY);
11650Sstevel@tonic-gate 
11660Sstevel@tonic-gate /* EXPORT DELETE END */
11670Sstevel@tonic-gate 
11680Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
11690Sstevel@tonic-gate }
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate /* ARGSUSED */
11720Sstevel@tonic-gate static int
11730Sstevel@tonic-gate des_common_init_ctx(des_ctx_t *des_ctx, crypto_spi_ctx_template_t *template,
11740Sstevel@tonic-gate     crypto_mechanism_t *mechanism, crypto_key_t *key, des_strength_t strength,
11750Sstevel@tonic-gate     int kmflag)
11760Sstevel@tonic-gate {
11770Sstevel@tonic-gate 	int rv = CRYPTO_SUCCESS;
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate /* EXPORT DELETE START */
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate 	void *keysched;
11820Sstevel@tonic-gate 	size_t size;
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 	if (template == NULL) {
11850Sstevel@tonic-gate 		if ((keysched = des_alloc_keysched(&size, strength,
11860Sstevel@tonic-gate 		    kmflag)) == NULL)
11870Sstevel@tonic-gate 			return (CRYPTO_HOST_MEMORY);
11880Sstevel@tonic-gate 		/*
11890Sstevel@tonic-gate 		 * Initialize key schedule.
11900Sstevel@tonic-gate 		 * Key length is stored in the key.
11910Sstevel@tonic-gate 		 */
11920Sstevel@tonic-gate 		if ((rv = init_keysched(key, keysched,
11930Sstevel@tonic-gate 		    strength)) != CRYPTO_SUCCESS)
11940Sstevel@tonic-gate 			kmem_free(keysched, size);
11950Sstevel@tonic-gate 
11967188Smcpowers 		des_ctx->dc_flags |= PROVIDER_OWNS_KEY_SCHEDULE;
11970Sstevel@tonic-gate 		des_ctx->dc_keysched_len = size;
11980Sstevel@tonic-gate 	} else {
11990Sstevel@tonic-gate 		keysched = template;
12000Sstevel@tonic-gate 	}
12017188Smcpowers 	des_ctx->dc_keysched = keysched;
12020Sstevel@tonic-gate 
12030Sstevel@tonic-gate 	if (strength == DES3) {
12040Sstevel@tonic-gate 		des_ctx->dc_flags |= DES3_STRENGTH;
12050Sstevel@tonic-gate 	}
12060Sstevel@tonic-gate 
12077188Smcpowers 	switch (mechanism->cm_type) {
12087188Smcpowers 	case DES_CBC_MECH_INFO_TYPE:
12097188Smcpowers 	case DES3_CBC_MECH_INFO_TYPE:
12107188Smcpowers 		rv = cbc_init_ctx((cbc_ctx_t *)des_ctx, mechanism->cm_param,
12117188Smcpowers 		    mechanism->cm_param_len, DES_BLOCK_LEN, des_copy_block64);
12127188Smcpowers 		break;
12137188Smcpowers 	case DES_ECB_MECH_INFO_TYPE:
12147188Smcpowers 	case DES3_ECB_MECH_INFO_TYPE:
12157188Smcpowers 		des_ctx->dc_flags |= ECB_MODE;
12167188Smcpowers 	}
12170Sstevel@tonic-gate 
12187188Smcpowers 	if (rv != CRYPTO_SUCCESS) {
12197188Smcpowers 		if (des_ctx->dc_flags & PROVIDER_OWNS_KEY_SCHEDULE) {
12207188Smcpowers 			bzero(keysched, size);
12217188Smcpowers 			kmem_free(keysched, size);
12220Sstevel@tonic-gate 		}
12230Sstevel@tonic-gate 	}
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate /* EXPORT DELETE END */
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate 	return (rv);
12280Sstevel@tonic-gate }
1229*10500SHai-May.Chao@Sun.COM 
1230*10500SHai-May.Chao@Sun.COM /*
1231*10500SHai-May.Chao@Sun.COM  * Triple DES Power-Up Self-Test
1232*10500SHai-May.Chao@Sun.COM  */
1233*10500SHai-May.Chao@Sun.COM void
1234*10500SHai-May.Chao@Sun.COM des_POST(int *rc)
1235*10500SHai-May.Chao@Sun.COM {
1236*10500SHai-May.Chao@Sun.COM 
1237*10500SHai-May.Chao@Sun.COM 	*rc = fips_des3_post();
1238*10500SHai-May.Chao@Sun.COM 
1239*10500SHai-May.Chao@Sun.COM }
1240