xref: /onnv-gate/usr/src/uts/common/des/des_crypt.c (revision 11751:58c0c8f4305f)
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 /*
23*11751SAnthony.Scarpino@Sun.COM  * Copyright 2010 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>
5110500SHai-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 
12510500SHai-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 
22410732SAnthony.Scarpino@Sun.COM static void des_POST(int *);
22510732SAnthony.Scarpino@Sun.COM 
22610732SAnthony.Scarpino@Sun.COM static crypto_fips140_ops_t des_fips140_ops = {
22710732SAnthony.Scarpino@Sun.COM 	des_POST
22810732SAnthony.Scarpino@Sun.COM };
22910732SAnthony.Scarpino@Sun.COM 
2300Sstevel@tonic-gate static crypto_ops_t des_crypto_ops = {
2310Sstevel@tonic-gate 	&des_control_ops,
2320Sstevel@tonic-gate 	NULL,
2330Sstevel@tonic-gate 	&des_cipher_ops,
2340Sstevel@tonic-gate 	NULL,
2350Sstevel@tonic-gate 	NULL,
2360Sstevel@tonic-gate 	NULL,
2370Sstevel@tonic-gate 	NULL,
2380Sstevel@tonic-gate 	NULL,
2390Sstevel@tonic-gate 	NULL,
2400Sstevel@tonic-gate 	NULL,
2410Sstevel@tonic-gate 	NULL,
2420Sstevel@tonic-gate 	&des_key_ops,
2430Sstevel@tonic-gate 	NULL,
24410732SAnthony.Scarpino@Sun.COM 	&des_ctx_ops,
24510732SAnthony.Scarpino@Sun.COM 	NULL,
24610732SAnthony.Scarpino@Sun.COM 	NULL,
24710732SAnthony.Scarpino@Sun.COM 	&des_fips140_ops
2480Sstevel@tonic-gate };
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate static crypto_provider_info_t des_prov_info = {
25110732SAnthony.Scarpino@Sun.COM 	CRYPTO_SPI_VERSION_4,
2520Sstevel@tonic-gate 	"DES Software Provider",
2530Sstevel@tonic-gate 	CRYPTO_SW_PROVIDER,
2540Sstevel@tonic-gate 	{&modlinkage},
2550Sstevel@tonic-gate 	NULL,
2560Sstevel@tonic-gate 	&des_crypto_ops,
2570Sstevel@tonic-gate 	sizeof (des_mech_info_tab)/sizeof (crypto_mech_info_t),
2580Sstevel@tonic-gate 	des_mech_info_tab
2590Sstevel@tonic-gate };
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate static crypto_kcf_provider_handle_t des_prov_handle = NULL;
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate int
_init(void)2640Sstevel@tonic-gate _init(void)
2650Sstevel@tonic-gate {
2660Sstevel@tonic-gate 	int ret;
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	if ((ret = mod_install(&modlinkage)) != 0)
2690Sstevel@tonic-gate 		return (ret);
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	/*
272*11751SAnthony.Scarpino@Sun.COM 	 * Register with KCF. If the registration fails, kcf will log an
2730Sstevel@tonic-gate 	 * error but do not uninstall the module, since the functionality
2740Sstevel@tonic-gate 	 * provided by misc/des should still be available.
275*11751SAnthony.Scarpino@Sun.COM 	 *
2760Sstevel@tonic-gate 	 */
277*11751SAnthony.Scarpino@Sun.COM 	(void) crypto_register_provider(&des_prov_info, &des_prov_handle);
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	return (0);
2800Sstevel@tonic-gate }
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2840Sstevel@tonic-gate _info(struct modinfo *modinfop)
2850Sstevel@tonic-gate {
2860Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2870Sstevel@tonic-gate }
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate /*
2900Sstevel@tonic-gate  * Copy 8 bytes
2910Sstevel@tonic-gate  */
2920Sstevel@tonic-gate #define	COPY8(src, dst) { \
2930Sstevel@tonic-gate 	char *a = (char *)dst; \
2940Sstevel@tonic-gate 	char *b = (char *)src; \
2950Sstevel@tonic-gate 	*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
2960Sstevel@tonic-gate 	*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
2970Sstevel@tonic-gate }
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate /*
3000Sstevel@tonic-gate  * Copy multiple of 8 bytes
3010Sstevel@tonic-gate  */
3020Sstevel@tonic-gate #define	DESCOPY(src, dst, len) { \
3030Sstevel@tonic-gate 	char *a = (char *)dst; \
3040Sstevel@tonic-gate 	char *b = (char *)src; \
3050Sstevel@tonic-gate 	int i; \
3060Sstevel@tonic-gate 	for (i = (size_t)len; i > 0; i -= 8) { \
3070Sstevel@tonic-gate 		*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
3080Sstevel@tonic-gate 		*a++ = *b++; *a++ = *b++; *a++ = *b++; *a++ = *b++; \
3090Sstevel@tonic-gate 	} \
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate /*
3130Sstevel@tonic-gate  * CBC mode encryption
3140Sstevel@tonic-gate  */
3150Sstevel@tonic-gate /* ARGSUSED */
3160Sstevel@tonic-gate int
cbc_crypt(char * key,char * buf,size_t len,unsigned int mode,char * ivec)3170Sstevel@tonic-gate cbc_crypt(char *key, char *buf, size_t len, unsigned int mode, char *ivec)
3180Sstevel@tonic-gate {
3190Sstevel@tonic-gate 	int err = 0;
3200Sstevel@tonic-gate /* EXPORT DELETE START */
3210Sstevel@tonic-gate 	struct desparams dp;
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	dp.des_mode = CBC;
3240Sstevel@tonic-gate 	COPY8(ivec, dp.des_ivec);
3250Sstevel@tonic-gate 	err = common_crypt(key, buf, len, mode, &dp);
3260Sstevel@tonic-gate 	COPY8(dp.des_ivec, ivec);
3270Sstevel@tonic-gate /* EXPORT DELETE END */
3280Sstevel@tonic-gate 	return (err);
3290Sstevel@tonic-gate }
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate /*
3330Sstevel@tonic-gate  * ECB mode encryption
3340Sstevel@tonic-gate  */
3350Sstevel@tonic-gate /* ARGSUSED */
3360Sstevel@tonic-gate int
ecb_crypt(char * key,char * buf,size_t len,unsigned int mode)3370Sstevel@tonic-gate ecb_crypt(char *key, char *buf, size_t len, unsigned int mode)
3380Sstevel@tonic-gate {
3390Sstevel@tonic-gate 	int err = 0;
3400Sstevel@tonic-gate /* EXPORT DELETE START */
3410Sstevel@tonic-gate 	struct desparams dp;
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	dp.des_mode = ECB;
3440Sstevel@tonic-gate 	err = common_crypt(key, buf, len, mode, &dp);
3450Sstevel@tonic-gate /* EXPORT DELETE END */
3460Sstevel@tonic-gate 	return (err);
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate /* EXPORT DELETE START */
3520Sstevel@tonic-gate /*
3530Sstevel@tonic-gate  * Common code to cbc_crypt() & ecb_crypt()
3540Sstevel@tonic-gate  */
3550Sstevel@tonic-gate static int
common_crypt(char * key,char * buf,size_t len,unsigned int mode,struct desparams * desp)3560Sstevel@tonic-gate common_crypt(char *key, char *buf, size_t len, unsigned int mode,
3570Sstevel@tonic-gate     struct desparams *desp)
3580Sstevel@tonic-gate {
3590Sstevel@tonic-gate 	int desdev;
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	if ((len % 8) != 0 || len > DES_MAXDATA)
3620Sstevel@tonic-gate 		return (DESERR_BADPARAM);
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	desp->des_dir =
3650Sstevel@tonic-gate 	    ((mode & DES_DIRMASK) == DES_ENCRYPT) ? ENCRYPT : DECRYPT;
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	desdev = mode & DES_DEVMASK;
3680Sstevel@tonic-gate 	COPY8(key, desp->des_key);
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate #ifdef sun_hardware
3710Sstevel@tonic-gate 	if (desdev == DES_HW) {
3720Sstevel@tonic-gate 		int res;
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 		if (g_desfd < 0 &&
3750Sstevel@tonic-gate 		    (g_desfd == -1 || (g_desfd = getdesfd()) < 0))
3760Sstevel@tonic-gate 				goto software;	/* no hardware device */
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 		/*
3790Sstevel@tonic-gate 		 * hardware
3800Sstevel@tonic-gate 		 */
3810Sstevel@tonic-gate 		desp->des_len = len;
3820Sstevel@tonic-gate 		if (len <= DES_QUICKLEN) {
3830Sstevel@tonic-gate 			DESCOPY(buf, desp->des_data, len);
3840Sstevel@tonic-gate 			res = ioctl(g_desfd, DESIOCQUICK, (char *)desp);
3850Sstevel@tonic-gate 			DESCOPY(desp->des_data, buf, len);
3860Sstevel@tonic-gate 		} else {
3870Sstevel@tonic-gate 			desp->des_buf = (uchar_t *)buf;
3880Sstevel@tonic-gate 			res = ioctl(g_desfd, DESIOCBLOCK, (char *)desp);
3890Sstevel@tonic-gate 		}
3900Sstevel@tonic-gate 		return (res == 0 ? DESERR_NONE : DESERR_HWERROR);
3910Sstevel@tonic-gate 	}
3920Sstevel@tonic-gate software:
3930Sstevel@tonic-gate #endif
3940Sstevel@tonic-gate 	/*
3950Sstevel@tonic-gate 	 * software
3960Sstevel@tonic-gate 	 */
3970Sstevel@tonic-gate 	if (!_des_crypt(buf, len, desp))
3980Sstevel@tonic-gate 		return (DESERR_HWERROR);
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	return (desdev == DES_SW ? DESERR_NONE : DESERR_NOHWDEVICE);
4010Sstevel@tonic-gate }
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate /*
4040Sstevel@tonic-gate  * Initialize key schedules for DES and DES3
4050Sstevel@tonic-gate  */
4060Sstevel@tonic-gate static int
init_keysched(crypto_key_t * key,void * newbie,des_strength_t strength)4070Sstevel@tonic-gate init_keysched(crypto_key_t *key, void *newbie, des_strength_t strength)
4080Sstevel@tonic-gate {
4090Sstevel@tonic-gate 	uint8_t corrected_key[DES3_KEYSIZE];
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	/*
4120Sstevel@tonic-gate 	 * Only keys by value are supported by this module.
4130Sstevel@tonic-gate 	 */
4140Sstevel@tonic-gate 	switch (key->ck_format) {
4150Sstevel@tonic-gate 	case CRYPTO_KEY_RAW:
41610444SVladimir.Kotal@Sun.COM 		if (strength == DES && key->ck_length != DES_MAXBITS)
4170Sstevel@tonic-gate 			return (CRYPTO_KEY_SIZE_RANGE);
41810444SVladimir.Kotal@Sun.COM 		if (strength == DES3 && key->ck_length != DES3_MAXBITS)
4190Sstevel@tonic-gate 			return (CRYPTO_KEY_SIZE_RANGE);
4200Sstevel@tonic-gate 		break;
4210Sstevel@tonic-gate 	default:
4220Sstevel@tonic-gate 		return (CRYPTO_KEY_TYPE_INCONSISTENT);
4230Sstevel@tonic-gate 	}
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	/*
4260Sstevel@tonic-gate 	 * Fix parity bits.
4270Sstevel@tonic-gate 	 * Initialize key schedule even if key is weak.
4280Sstevel@tonic-gate 	 */
4292439Sizick 	if (key->ck_data == NULL)
4302439Sizick 		return (CRYPTO_ARGUMENTS_BAD);
4310Sstevel@tonic-gate 
4322439Sizick 	des_parity_fix(key->ck_data, strength, corrected_key);
4330Sstevel@tonic-gate 	des_init_keysched(corrected_key, strength, newbie);
4340Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
4350Sstevel@tonic-gate }
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate /* EXPORT DELETE END */
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate /*
4400Sstevel@tonic-gate  * KCF software provider control entry points.
4410Sstevel@tonic-gate  */
4420Sstevel@tonic-gate /* ARGSUSED */
4430Sstevel@tonic-gate static void
des_provider_status(crypto_provider_handle_t provider,uint_t * status)4440Sstevel@tonic-gate des_provider_status(crypto_provider_handle_t provider, uint_t *status)
4450Sstevel@tonic-gate {
4460Sstevel@tonic-gate 	*status = CRYPTO_PROVIDER_READY;
4470Sstevel@tonic-gate }
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate /*
4500Sstevel@tonic-gate  * KCF software provider encrypt entry points.
4510Sstevel@tonic-gate  */
4520Sstevel@tonic-gate static int
des_common_init(crypto_ctx_t * ctx,crypto_mechanism_t * mechanism,crypto_key_t * key,crypto_spi_ctx_template_t template,crypto_req_handle_t req)4530Sstevel@tonic-gate des_common_init(crypto_ctx_t *ctx, crypto_mechanism_t *mechanism,
4540Sstevel@tonic-gate     crypto_key_t *key, crypto_spi_ctx_template_t template,
4550Sstevel@tonic-gate     crypto_req_handle_t req)
4560Sstevel@tonic-gate {
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate /* EXPORT DELETE START */
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	des_strength_t strength;
4617188Smcpowers 	des_ctx_t *des_ctx = NULL;
4620Sstevel@tonic-gate 	int rv;
4630Sstevel@tonic-gate 	int kmflag;
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	/*
4660Sstevel@tonic-gate 	 * Only keys by value are supported by this module.
4670Sstevel@tonic-gate 	 */
4680Sstevel@tonic-gate 	if (key->ck_format != CRYPTO_KEY_RAW) {
4690Sstevel@tonic-gate 		return (CRYPTO_KEY_TYPE_INCONSISTENT);
4700Sstevel@tonic-gate 	}
4710Sstevel@tonic-gate 
4727188Smcpowers 	kmflag = crypto_kmflag(req);
4730Sstevel@tonic-gate 	/* Check mechanism type and parameter length */
4740Sstevel@tonic-gate 	switch (mechanism->cm_type) {
4750Sstevel@tonic-gate 	case DES_ECB_MECH_INFO_TYPE:
4767188Smcpowers 		des_ctx = ecb_alloc_ctx(kmflag);
4777188Smcpowers 		/* FALLTHRU */
4780Sstevel@tonic-gate 	case DES_CBC_MECH_INFO_TYPE:
4790Sstevel@tonic-gate 		if (mechanism->cm_param != NULL &&
4800Sstevel@tonic-gate 		    mechanism->cm_param_len != DES_BLOCK_LEN)
4810Sstevel@tonic-gate 			return (CRYPTO_MECHANISM_PARAM_INVALID);
48210444SVladimir.Kotal@Sun.COM 		if (key->ck_length != DES_MAXBITS)
4830Sstevel@tonic-gate 			return (CRYPTO_KEY_SIZE_RANGE);
4840Sstevel@tonic-gate 		strength = DES;
4857188Smcpowers 		if (des_ctx == NULL)
4867188Smcpowers 			des_ctx = cbc_alloc_ctx(kmflag);
4870Sstevel@tonic-gate 		break;
4880Sstevel@tonic-gate 	case DES3_ECB_MECH_INFO_TYPE:
4897188Smcpowers 		des_ctx = ecb_alloc_ctx(kmflag);
4907188Smcpowers 		/* FALLTHRU */
4910Sstevel@tonic-gate 	case DES3_CBC_MECH_INFO_TYPE:
4920Sstevel@tonic-gate 		if (mechanism->cm_param != NULL &&
4930Sstevel@tonic-gate 		    mechanism->cm_param_len != DES_BLOCK_LEN)
4940Sstevel@tonic-gate 			return (CRYPTO_MECHANISM_PARAM_INVALID);
49510444SVladimir.Kotal@Sun.COM 		if (key->ck_length != DES3_MAXBITS)
4960Sstevel@tonic-gate 			return (CRYPTO_KEY_SIZE_RANGE);
4970Sstevel@tonic-gate 		strength = DES3;
4987188Smcpowers 		if (des_ctx == NULL)
4997188Smcpowers 			des_ctx = cbc_alloc_ctx(kmflag);
5000Sstevel@tonic-gate 		break;
5010Sstevel@tonic-gate 	default:
5020Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
5030Sstevel@tonic-gate 	}
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 	if ((rv = des_common_init_ctx(des_ctx, template, mechanism, key,
5060Sstevel@tonic-gate 	    strength, kmflag)) != CRYPTO_SUCCESS) {
5077188Smcpowers 		crypto_free_mode_ctx(des_ctx);
5080Sstevel@tonic-gate 		return (rv);
5090Sstevel@tonic-gate 	}
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	ctx->cc_provider_private = des_ctx;
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate /* EXPORT DELETE END */
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
5160Sstevel@tonic-gate }
5170Sstevel@tonic-gate 
5187188Smcpowers static void
des_copy_block64(uint8_t * in,uint64_t * out)5197188Smcpowers des_copy_block64(uint8_t *in, uint64_t *out)
5200Sstevel@tonic-gate {
5217188Smcpowers 	if (IS_P2ALIGNED(in, sizeof (uint64_t))) {
5227188Smcpowers 		/* LINTED: pointer alignment */
5237188Smcpowers 		out[0] = *(uint64_t *)&in[0];
5247188Smcpowers 	} else {
5257188Smcpowers 		uint64_t tmp64;
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate #ifdef _BIG_ENDIAN
5287188Smcpowers 		tmp64 = (((uint64_t)in[0] << 56) |
5297188Smcpowers 		    ((uint64_t)in[1] << 48) |
5307188Smcpowers 		    ((uint64_t)in[2] << 40) |
5317188Smcpowers 		    ((uint64_t)in[3] << 32) |
5327188Smcpowers 		    ((uint64_t)in[4] << 24) |
5337188Smcpowers 		    ((uint64_t)in[5] << 16) |
5347188Smcpowers 		    ((uint64_t)in[6] << 8) |
5357188Smcpowers 		    (uint64_t)in[7]);
5360Sstevel@tonic-gate #else
5377188Smcpowers 		tmp64 = (((uint64_t)in[7] << 56) |
5387188Smcpowers 		    ((uint64_t)in[6] << 48) |
5397188Smcpowers 		    ((uint64_t)in[5] << 40) |
5407188Smcpowers 		    ((uint64_t)in[4] << 32) |
5417188Smcpowers 		    ((uint64_t)in[3] << 24) |
5427188Smcpowers 		    ((uint64_t)in[2] << 16) |
5437188Smcpowers 		    ((uint64_t)in[1] << 8) |
5447188Smcpowers 		    (uint64_t)in[0]);
5450Sstevel@tonic-gate #endif /* _BIG_ENDIAN */
5460Sstevel@tonic-gate 
5477188Smcpowers 		out[0] = tmp64;
5480Sstevel@tonic-gate 	}
5490Sstevel@tonic-gate }
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate /* ARGSUSED */
5520Sstevel@tonic-gate static int
des_encrypt(crypto_ctx_t * ctx,crypto_data_t * plaintext,crypto_data_t * ciphertext,crypto_req_handle_t req)5530Sstevel@tonic-gate des_encrypt(crypto_ctx_t *ctx, crypto_data_t *plaintext,
5540Sstevel@tonic-gate     crypto_data_t *ciphertext, crypto_req_handle_t req)
5550Sstevel@tonic-gate {
5560Sstevel@tonic-gate 	int ret;
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate /* EXPORT DELETE START */
5590Sstevel@tonic-gate 	des_ctx_t *des_ctx;
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 	/*
5620Sstevel@tonic-gate 	 * Plaintext must be a multiple of the block size.
5630Sstevel@tonic-gate 	 * This test only works for non-padded mechanisms
5640Sstevel@tonic-gate 	 * when blocksize is 2^N.
5650Sstevel@tonic-gate 	 */
5660Sstevel@tonic-gate 	if ((plaintext->cd_length & (DES_BLOCK_LEN - 1)) != 0)
5670Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
5700Sstevel@tonic-gate 	des_ctx = ctx->cc_provider_private;
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	DES_ARG_INPLACE(plaintext, ciphertext);
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 	/*
5750Sstevel@tonic-gate 	 * We need to just return the length needed to store the output.
5760Sstevel@tonic-gate 	 * We should not destroy the context for the following case.
5770Sstevel@tonic-gate 	 */
5780Sstevel@tonic-gate 	if (ciphertext->cd_length < plaintext->cd_length) {
5790Sstevel@tonic-gate 		ciphertext->cd_length = plaintext->cd_length;
5800Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
5810Sstevel@tonic-gate 	}
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	/*
5840Sstevel@tonic-gate 	 * Do an update on the specified input data.
5850Sstevel@tonic-gate 	 */
5860Sstevel@tonic-gate 	ret = des_encrypt_update(ctx, plaintext, ciphertext, req);
5870Sstevel@tonic-gate 	ASSERT(des_ctx->dc_remainder_len == 0);
5880Sstevel@tonic-gate 	(void) des_free_context(ctx);
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate /* EXPORT DELETE END */
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 	/* LINTED */
5930Sstevel@tonic-gate 	return (ret);
5940Sstevel@tonic-gate }
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate /* ARGSUSED */
5970Sstevel@tonic-gate static int
des_decrypt(crypto_ctx_t * ctx,crypto_data_t * ciphertext,crypto_data_t * plaintext,crypto_req_handle_t req)5980Sstevel@tonic-gate des_decrypt(crypto_ctx_t *ctx, crypto_data_t *ciphertext,
5990Sstevel@tonic-gate     crypto_data_t *plaintext, crypto_req_handle_t req)
6000Sstevel@tonic-gate {
6010Sstevel@tonic-gate 	int ret;
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate /* EXPORT DELETE START */
6040Sstevel@tonic-gate 	des_ctx_t *des_ctx;
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 	/*
6070Sstevel@tonic-gate 	 * Ciphertext must be a multiple of the block size.
6080Sstevel@tonic-gate 	 * This test only works for non-padded mechanisms
6090Sstevel@tonic-gate 	 * when blocksize is 2^N.
6100Sstevel@tonic-gate 	 */
6110Sstevel@tonic-gate 	if ((ciphertext->cd_length & (DES_BLOCK_LEN - 1)) != 0)
6120Sstevel@tonic-gate 		return (CRYPTO_ENCRYPTED_DATA_LEN_RANGE);
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
6150Sstevel@tonic-gate 	des_ctx = ctx->cc_provider_private;
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 	DES_ARG_INPLACE(ciphertext, plaintext);
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	/*
6200Sstevel@tonic-gate 	 * We need to just return the length needed to store the output.
6210Sstevel@tonic-gate 	 * We should not destroy the context for the following case.
6220Sstevel@tonic-gate 	 */
6230Sstevel@tonic-gate 	if (plaintext->cd_length < ciphertext->cd_length) {
6240Sstevel@tonic-gate 		plaintext->cd_length = ciphertext->cd_length;
6250Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
6260Sstevel@tonic-gate 	}
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	/*
6290Sstevel@tonic-gate 	 * Do an update on the specified input data.
6300Sstevel@tonic-gate 	 */
6310Sstevel@tonic-gate 	ret = des_decrypt_update(ctx, ciphertext, plaintext, req);
6320Sstevel@tonic-gate 	ASSERT(des_ctx->dc_remainder_len == 0);
6330Sstevel@tonic-gate 	(void) des_free_context(ctx);
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate /* EXPORT DELETE END */
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 	/* LINTED */
6380Sstevel@tonic-gate 	return (ret);
6390Sstevel@tonic-gate }
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate /* ARGSUSED */
6420Sstevel@tonic-gate static int
des_encrypt_update(crypto_ctx_t * ctx,crypto_data_t * plaintext,crypto_data_t * ciphertext,crypto_req_handle_t req)6430Sstevel@tonic-gate des_encrypt_update(crypto_ctx_t *ctx, crypto_data_t *plaintext,
6440Sstevel@tonic-gate     crypto_data_t *ciphertext, crypto_req_handle_t req)
6450Sstevel@tonic-gate {
6460Sstevel@tonic-gate 	off_t saved_offset;
6470Sstevel@tonic-gate 	size_t saved_length, out_len;
6480Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate /* EXPORT DELETE START */
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	DES_ARG_INPLACE(plaintext, ciphertext);
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 	/* compute number of bytes that will hold the ciphertext */
6570Sstevel@tonic-gate 	out_len = ((des_ctx_t *)ctx->cc_provider_private)->dc_remainder_len;
6580Sstevel@tonic-gate 	out_len += plaintext->cd_length;
6590Sstevel@tonic-gate 	out_len &= ~(DES_BLOCK_LEN - 1);
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	/* return length needed to store the output */
6620Sstevel@tonic-gate 	if (ciphertext->cd_length < out_len) {
6630Sstevel@tonic-gate 		ciphertext->cd_length = out_len;
6640Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
6650Sstevel@tonic-gate 	}
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 	saved_offset = ciphertext->cd_offset;
6680Sstevel@tonic-gate 	saved_length = ciphertext->cd_length;
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	/*
6710Sstevel@tonic-gate 	 * Do the DES update on the specified input data.
6720Sstevel@tonic-gate 	 */
6730Sstevel@tonic-gate 	switch (plaintext->cd_format) {
6740Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
6757188Smcpowers 		ret = crypto_update_iov(ctx->cc_provider_private,
6767188Smcpowers 		    plaintext, ciphertext, des_encrypt_contiguous_blocks,
6777188Smcpowers 		    des_copy_block64);
6780Sstevel@tonic-gate 		break;
6790Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
6807188Smcpowers 		ret = crypto_update_uio(ctx->cc_provider_private,
6817188Smcpowers 		    plaintext, ciphertext, des_encrypt_contiguous_blocks,
6827188Smcpowers 		    des_copy_block64);
6830Sstevel@tonic-gate 		break;
6840Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
6857188Smcpowers 		ret = crypto_update_mp(ctx->cc_provider_private,
6867188Smcpowers 		    plaintext, ciphertext, des_encrypt_contiguous_blocks,
6877188Smcpowers 		    des_copy_block64);
6880Sstevel@tonic-gate 		break;
6890Sstevel@tonic-gate 	default:
6900Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
6910Sstevel@tonic-gate 	}
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
6940Sstevel@tonic-gate 		if (plaintext != ciphertext)
6950Sstevel@tonic-gate 			ciphertext->cd_length =
6960Sstevel@tonic-gate 			    ciphertext->cd_offset - saved_offset;
6970Sstevel@tonic-gate 	} else {
6980Sstevel@tonic-gate 		ciphertext->cd_length = saved_length;
6990Sstevel@tonic-gate 	}
7000Sstevel@tonic-gate 	ciphertext->cd_offset = saved_offset;
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate /* EXPORT DELETE END */
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 	return (ret);
7050Sstevel@tonic-gate }
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate /* ARGSUSED */
7080Sstevel@tonic-gate static int
des_decrypt_update(crypto_ctx_t * ctx,crypto_data_t * ciphertext,crypto_data_t * plaintext,crypto_req_handle_t req)7090Sstevel@tonic-gate des_decrypt_update(crypto_ctx_t *ctx, crypto_data_t *ciphertext,
7100Sstevel@tonic-gate     crypto_data_t *plaintext, crypto_req_handle_t req)
7110Sstevel@tonic-gate {
7120Sstevel@tonic-gate 	off_t saved_offset;
7130Sstevel@tonic-gate 	size_t saved_length, out_len;
7140Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate /* EXPORT DELETE START */
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	DES_ARG_INPLACE(ciphertext, plaintext);
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 	/* compute number of bytes that will hold the plaintext */
7230Sstevel@tonic-gate 	out_len = ((des_ctx_t *)ctx->cc_provider_private)->dc_remainder_len;
7240Sstevel@tonic-gate 	out_len += ciphertext->cd_length;
7250Sstevel@tonic-gate 	out_len &= ~(DES_BLOCK_LEN - 1);
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 	/* return length needed to store the output */
7280Sstevel@tonic-gate 	if (plaintext->cd_length < out_len) {
7290Sstevel@tonic-gate 		plaintext->cd_length = out_len;
7300Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
7310Sstevel@tonic-gate 	}
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 	saved_offset = plaintext->cd_offset;
7340Sstevel@tonic-gate 	saved_length = plaintext->cd_length;
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	/*
7370Sstevel@tonic-gate 	 * Do the DES update on the specified input data.
7380Sstevel@tonic-gate 	 */
7390Sstevel@tonic-gate 	switch (ciphertext->cd_format) {
7400Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
7417188Smcpowers 		ret = crypto_update_iov(ctx->cc_provider_private,
7427188Smcpowers 		    ciphertext, plaintext, des_decrypt_contiguous_blocks,
7437188Smcpowers 		    des_copy_block64);
7440Sstevel@tonic-gate 		break;
7450Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
7467188Smcpowers 		ret = crypto_update_uio(ctx->cc_provider_private,
7477188Smcpowers 		    ciphertext, plaintext, des_decrypt_contiguous_blocks,
7487188Smcpowers 		    des_copy_block64);
7490Sstevel@tonic-gate 		break;
7500Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
7517188Smcpowers 		ret = crypto_update_mp(ctx->cc_provider_private,
7527188Smcpowers 		    ciphertext, plaintext, des_decrypt_contiguous_blocks,
7537188Smcpowers 		    des_copy_block64);
7540Sstevel@tonic-gate 		break;
7550Sstevel@tonic-gate 	default:
7560Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
7570Sstevel@tonic-gate 	}
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
7600Sstevel@tonic-gate 		if (ciphertext != plaintext)
7610Sstevel@tonic-gate 			plaintext->cd_length =
7620Sstevel@tonic-gate 			    plaintext->cd_offset - saved_offset;
7630Sstevel@tonic-gate 	} else {
7640Sstevel@tonic-gate 		plaintext->cd_length = saved_length;
7650Sstevel@tonic-gate 	}
7660Sstevel@tonic-gate 	plaintext->cd_offset = saved_offset;
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate /* EXPORT DELETE END */
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	return (ret);
7710Sstevel@tonic-gate }
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate /* ARGSUSED */
7740Sstevel@tonic-gate static int
des_encrypt_final(crypto_ctx_t * ctx,crypto_data_t * ciphertext,crypto_req_handle_t req)7750Sstevel@tonic-gate des_encrypt_final(crypto_ctx_t *ctx, crypto_data_t *ciphertext,
7760Sstevel@tonic-gate     crypto_req_handle_t req)
7770Sstevel@tonic-gate {
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate /* EXPORT DELETE START */
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	des_ctx_t *des_ctx;
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
7840Sstevel@tonic-gate 	des_ctx = ctx->cc_provider_private;
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate 	/*
7870Sstevel@tonic-gate 	 * There must be no unprocessed plaintext.
7880Sstevel@tonic-gate 	 * This happens if the length of the last data is
7890Sstevel@tonic-gate 	 * not a multiple of the DES block length.
7900Sstevel@tonic-gate 	 */
7910Sstevel@tonic-gate 	if (des_ctx->dc_remainder_len > 0)
7920Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 	(void) des_free_context(ctx);
7950Sstevel@tonic-gate 	ciphertext->cd_length = 0;
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate /* EXPORT DELETE END */
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
8000Sstevel@tonic-gate }
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate /* ARGSUSED */
8030Sstevel@tonic-gate static int
des_decrypt_final(crypto_ctx_t * ctx,crypto_data_t * plaintext,crypto_req_handle_t req)8040Sstevel@tonic-gate des_decrypt_final(crypto_ctx_t *ctx, crypto_data_t *plaintext,
8050Sstevel@tonic-gate     crypto_req_handle_t req)
8060Sstevel@tonic-gate {
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate /* EXPORT DELETE START */
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 	des_ctx_t *des_ctx;
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
8130Sstevel@tonic-gate 	des_ctx = ctx->cc_provider_private;
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	/*
8160Sstevel@tonic-gate 	 * There must be no unprocessed ciphertext.
8170Sstevel@tonic-gate 	 * This happens if the length of the last ciphertext is
8180Sstevel@tonic-gate 	 * not a multiple of the DES block length.
8190Sstevel@tonic-gate 	 */
8200Sstevel@tonic-gate 	if (des_ctx->dc_remainder_len > 0)
8210Sstevel@tonic-gate 		return (CRYPTO_ENCRYPTED_DATA_LEN_RANGE);
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate 	(void) des_free_context(ctx);
8240Sstevel@tonic-gate 	plaintext->cd_length = 0;
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate /* EXPORT DELETE END */
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
8290Sstevel@tonic-gate }
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate /* ARGSUSED */
8320Sstevel@tonic-gate static int
des_encrypt_atomic(crypto_provider_handle_t provider,crypto_session_id_t session_id,crypto_mechanism_t * mechanism,crypto_key_t * key,crypto_data_t * plaintext,crypto_data_t * ciphertext,crypto_spi_ctx_template_t template,crypto_req_handle_t req)8330Sstevel@tonic-gate des_encrypt_atomic(crypto_provider_handle_t provider,
8340Sstevel@tonic-gate     crypto_session_id_t session_id, crypto_mechanism_t *mechanism,
8350Sstevel@tonic-gate     crypto_key_t *key, crypto_data_t *plaintext, crypto_data_t *ciphertext,
8360Sstevel@tonic-gate     crypto_spi_ctx_template_t template, crypto_req_handle_t req)
8370Sstevel@tonic-gate {
8380Sstevel@tonic-gate 	int ret;
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate /* EXPORT DELETE START */
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate 	des_ctx_t des_ctx;		/* on the stack */
8430Sstevel@tonic-gate 	des_strength_t strength;
8440Sstevel@tonic-gate 	off_t saved_offset;
8450Sstevel@tonic-gate 	size_t saved_length;
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate 	DES_ARG_INPLACE(plaintext, ciphertext);
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 	/*
8500Sstevel@tonic-gate 	 * Plaintext must be a multiple of the block size.
8510Sstevel@tonic-gate 	 * This test only works for non-padded mechanisms
8520Sstevel@tonic-gate 	 * when blocksize is 2^N.
8530Sstevel@tonic-gate 	 */
8540Sstevel@tonic-gate 	if ((plaintext->cd_length & (DES_BLOCK_LEN - 1)) != 0)
8550Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 	/* return length needed to store the output */
8580Sstevel@tonic-gate 	if (ciphertext->cd_length < plaintext->cd_length) {
8590Sstevel@tonic-gate 		ciphertext->cd_length = plaintext->cd_length;
8600Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
8610Sstevel@tonic-gate 	}
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 	/* Check mechanism type and parameter length */
8640Sstevel@tonic-gate 	switch (mechanism->cm_type) {
8650Sstevel@tonic-gate 	case DES_ECB_MECH_INFO_TYPE:
8660Sstevel@tonic-gate 	case DES_CBC_MECH_INFO_TYPE:
8670Sstevel@tonic-gate 		if (mechanism->cm_param_len > 0 &&
8680Sstevel@tonic-gate 		    mechanism->cm_param_len != DES_BLOCK_LEN)
8690Sstevel@tonic-gate 			return (CRYPTO_MECHANISM_PARAM_INVALID);
8700Sstevel@tonic-gate 		if (key->ck_length != DES_MINBITS)
8710Sstevel@tonic-gate 			return (CRYPTO_KEY_SIZE_RANGE);
8720Sstevel@tonic-gate 		strength = DES;
8730Sstevel@tonic-gate 		break;
8740Sstevel@tonic-gate 	case DES3_ECB_MECH_INFO_TYPE:
8750Sstevel@tonic-gate 	case DES3_CBC_MECH_INFO_TYPE:
8760Sstevel@tonic-gate 		if (mechanism->cm_param_len > 0 &&
8770Sstevel@tonic-gate 		    mechanism->cm_param_len != DES_BLOCK_LEN)
8780Sstevel@tonic-gate 			return (CRYPTO_MECHANISM_PARAM_INVALID);
87910444SVladimir.Kotal@Sun.COM 		if (key->ck_length != DES3_MAXBITS)
8800Sstevel@tonic-gate 			return (CRYPTO_KEY_SIZE_RANGE);
8810Sstevel@tonic-gate 		strength = DES3;
8820Sstevel@tonic-gate 		break;
8830Sstevel@tonic-gate 	default:
8840Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
8850Sstevel@tonic-gate 	}
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 	bzero(&des_ctx, sizeof (des_ctx_t));
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 	if ((ret = des_common_init_ctx(&des_ctx, template, mechanism, key,
8900Sstevel@tonic-gate 	    strength, crypto_kmflag(req))) != CRYPTO_SUCCESS) {
8910Sstevel@tonic-gate 		return (ret);
8920Sstevel@tonic-gate 	}
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 	saved_offset = ciphertext->cd_offset;
8950Sstevel@tonic-gate 	saved_length = ciphertext->cd_length;
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 	/*
8980Sstevel@tonic-gate 	 * Do the update on the specified input data.
8990Sstevel@tonic-gate 	 */
9000Sstevel@tonic-gate 	switch (plaintext->cd_format) {
9010Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
9027188Smcpowers 		ret = crypto_update_iov(&des_ctx, plaintext, ciphertext,
9037188Smcpowers 		    des_encrypt_contiguous_blocks, des_copy_block64);
9040Sstevel@tonic-gate 		break;
9050Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
9067188Smcpowers 		ret = crypto_update_uio(&des_ctx, plaintext, ciphertext,
9077188Smcpowers 		    des_encrypt_contiguous_blocks, des_copy_block64);
9080Sstevel@tonic-gate 		break;
9090Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
9107188Smcpowers 		ret = crypto_update_mp(&des_ctx, plaintext, ciphertext,
9117188Smcpowers 		    des_encrypt_contiguous_blocks, des_copy_block64);
9120Sstevel@tonic-gate 		break;
9130Sstevel@tonic-gate 	default:
9140Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
9150Sstevel@tonic-gate 	}
9160Sstevel@tonic-gate 
9177188Smcpowers 	if (des_ctx.dc_flags & PROVIDER_OWNS_KEY_SCHEDULE) {
9180Sstevel@tonic-gate 		bzero(des_ctx.dc_keysched, des_ctx.dc_keysched_len);
9190Sstevel@tonic-gate 		kmem_free(des_ctx.dc_keysched, des_ctx.dc_keysched_len);
9200Sstevel@tonic-gate 	}
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
9230Sstevel@tonic-gate 		ASSERT(des_ctx.dc_remainder_len == 0);
9240Sstevel@tonic-gate 		if (plaintext != ciphertext)
9250Sstevel@tonic-gate 			ciphertext->cd_length =
9260Sstevel@tonic-gate 			    ciphertext->cd_offset - saved_offset;
9270Sstevel@tonic-gate 	} else {
9280Sstevel@tonic-gate 		ciphertext->cd_length = saved_length;
9290Sstevel@tonic-gate 	}
9300Sstevel@tonic-gate 	ciphertext->cd_offset = saved_offset;
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate /* EXPORT DELETE END */
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 	/* LINTED */
9350Sstevel@tonic-gate 	return (ret);
9360Sstevel@tonic-gate }
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate /* ARGSUSED */
9390Sstevel@tonic-gate static int
des_decrypt_atomic(crypto_provider_handle_t provider,crypto_session_id_t session_id,crypto_mechanism_t * mechanism,crypto_key_t * key,crypto_data_t * ciphertext,crypto_data_t * plaintext,crypto_spi_ctx_template_t template,crypto_req_handle_t req)9400Sstevel@tonic-gate des_decrypt_atomic(crypto_provider_handle_t provider,
9410Sstevel@tonic-gate     crypto_session_id_t session_id, crypto_mechanism_t *mechanism,
9420Sstevel@tonic-gate     crypto_key_t *key, crypto_data_t *ciphertext, crypto_data_t *plaintext,
9430Sstevel@tonic-gate     crypto_spi_ctx_template_t template, crypto_req_handle_t req)
9440Sstevel@tonic-gate {
9450Sstevel@tonic-gate 	int ret;
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate /* EXPORT DELETE START */
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate 	des_ctx_t des_ctx;	/* on the stack */
9500Sstevel@tonic-gate 	des_strength_t strength;
9510Sstevel@tonic-gate 	off_t saved_offset;
9520Sstevel@tonic-gate 	size_t saved_length;
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 	DES_ARG_INPLACE(ciphertext, plaintext);
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 	/*
9570Sstevel@tonic-gate 	 * Ciphertext must be a multiple of the block size.
9580Sstevel@tonic-gate 	 * This test only works for non-padded mechanisms
9590Sstevel@tonic-gate 	 * when blocksize is 2^N.
9600Sstevel@tonic-gate 	 */
9610Sstevel@tonic-gate 	if ((ciphertext->cd_length & (DES_BLOCK_LEN - 1)) != 0)
9620Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 	/* return length needed to store the output */
9650Sstevel@tonic-gate 	if (plaintext->cd_length < ciphertext->cd_length) {
9660Sstevel@tonic-gate 		plaintext->cd_length = ciphertext->cd_length;
9670Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
9680Sstevel@tonic-gate 	}
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 	/* Check mechanism type and parameter length */
9710Sstevel@tonic-gate 	switch (mechanism->cm_type) {
9720Sstevel@tonic-gate 	case DES_ECB_MECH_INFO_TYPE:
9730Sstevel@tonic-gate 	case DES_CBC_MECH_INFO_TYPE:
9740Sstevel@tonic-gate 		if (mechanism->cm_param_len > 0 &&
9750Sstevel@tonic-gate 		    mechanism->cm_param_len != DES_BLOCK_LEN)
9760Sstevel@tonic-gate 			return (CRYPTO_MECHANISM_PARAM_INVALID);
9770Sstevel@tonic-gate 		if (key->ck_length != DES_MINBITS)
9780Sstevel@tonic-gate 			return (CRYPTO_KEY_SIZE_RANGE);
9790Sstevel@tonic-gate 		strength = DES;
9800Sstevel@tonic-gate 		break;
9810Sstevel@tonic-gate 	case DES3_ECB_MECH_INFO_TYPE:
9820Sstevel@tonic-gate 	case DES3_CBC_MECH_INFO_TYPE:
9830Sstevel@tonic-gate 		if (mechanism->cm_param_len > 0 &&
9840Sstevel@tonic-gate 		    mechanism->cm_param_len != DES_BLOCK_LEN)
9850Sstevel@tonic-gate 			return (CRYPTO_MECHANISM_PARAM_INVALID);
98610444SVladimir.Kotal@Sun.COM 		if (key->ck_length != DES3_MAXBITS)
9870Sstevel@tonic-gate 			return (CRYPTO_KEY_SIZE_RANGE);
9880Sstevel@tonic-gate 		strength = DES3;
9890Sstevel@tonic-gate 		break;
9900Sstevel@tonic-gate 	default:
9910Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
9920Sstevel@tonic-gate 	}
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 	bzero(&des_ctx, sizeof (des_ctx_t));
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 	if ((ret = des_common_init_ctx(&des_ctx, template, mechanism, key,
9970Sstevel@tonic-gate 	    strength, crypto_kmflag(req))) != CRYPTO_SUCCESS) {
9980Sstevel@tonic-gate 		return (ret);
9990Sstevel@tonic-gate 	}
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate 	saved_offset = plaintext->cd_offset;
10020Sstevel@tonic-gate 	saved_length = plaintext->cd_length;
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate 	/*
10050Sstevel@tonic-gate 	 * Do the update on the specified input data.
10060Sstevel@tonic-gate 	 */
10070Sstevel@tonic-gate 	switch (ciphertext->cd_format) {
10080Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
10097188Smcpowers 		ret = crypto_update_iov(&des_ctx, ciphertext, plaintext,
10107188Smcpowers 		    des_decrypt_contiguous_blocks, des_copy_block64);
10110Sstevel@tonic-gate 		break;
10120Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
10137188Smcpowers 		ret = crypto_update_uio(&des_ctx, ciphertext, plaintext,
10147188Smcpowers 		    des_decrypt_contiguous_blocks, des_copy_block64);
10150Sstevel@tonic-gate 		break;
10160Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
10177188Smcpowers 		ret = crypto_update_mp(&des_ctx, ciphertext, plaintext,
10187188Smcpowers 		    des_decrypt_contiguous_blocks, des_copy_block64);
10190Sstevel@tonic-gate 		break;
10200Sstevel@tonic-gate 	default:
10210Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
10220Sstevel@tonic-gate 	}
10230Sstevel@tonic-gate 
10247188Smcpowers 	if (des_ctx.dc_flags & PROVIDER_OWNS_KEY_SCHEDULE) {
10250Sstevel@tonic-gate 		bzero(des_ctx.dc_keysched, des_ctx.dc_keysched_len);
10260Sstevel@tonic-gate 		kmem_free(des_ctx.dc_keysched, des_ctx.dc_keysched_len);
10270Sstevel@tonic-gate 	}
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
10300Sstevel@tonic-gate 		ASSERT(des_ctx.dc_remainder_len == 0);
10310Sstevel@tonic-gate 		if (ciphertext != plaintext)
10320Sstevel@tonic-gate 			plaintext->cd_length =
10330Sstevel@tonic-gate 			    plaintext->cd_offset - saved_offset;
10340Sstevel@tonic-gate 	} else {
10350Sstevel@tonic-gate 		plaintext->cd_length = saved_length;
10360Sstevel@tonic-gate 	}
10370Sstevel@tonic-gate 	plaintext->cd_offset = saved_offset;
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate /* EXPORT DELETE END */
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate 	/* LINTED */
10420Sstevel@tonic-gate 	return (ret);
10430Sstevel@tonic-gate }
10440Sstevel@tonic-gate 
10450Sstevel@tonic-gate /*
10460Sstevel@tonic-gate  * KCF software provider context template entry points.
10470Sstevel@tonic-gate  */
10480Sstevel@tonic-gate /* ARGSUSED */
10490Sstevel@tonic-gate static int
des_create_ctx_template(crypto_provider_handle_t provider,crypto_mechanism_t * mechanism,crypto_key_t * key,crypto_spi_ctx_template_t * tmpl,size_t * tmpl_size,crypto_req_handle_t req)10500Sstevel@tonic-gate des_create_ctx_template(crypto_provider_handle_t provider,
10510Sstevel@tonic-gate     crypto_mechanism_t *mechanism, crypto_key_t *key,
10520Sstevel@tonic-gate     crypto_spi_ctx_template_t *tmpl, size_t *tmpl_size, crypto_req_handle_t req)
10530Sstevel@tonic-gate {
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate /* EXPORT DELETE START */
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate 	des_strength_t strength;
10580Sstevel@tonic-gate 	void *keysched;
10590Sstevel@tonic-gate 	size_t size;
10600Sstevel@tonic-gate 	int rv;
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate 	switch (mechanism->cm_type) {
10630Sstevel@tonic-gate 	case DES_ECB_MECH_INFO_TYPE:
10640Sstevel@tonic-gate 		strength = DES;
10650Sstevel@tonic-gate 		break;
10660Sstevel@tonic-gate 	case DES_CBC_MECH_INFO_TYPE:
10670Sstevel@tonic-gate 		strength = DES;
10680Sstevel@tonic-gate 		break;
10690Sstevel@tonic-gate 	case DES3_ECB_MECH_INFO_TYPE:
10700Sstevel@tonic-gate 		strength = DES3;
10710Sstevel@tonic-gate 		break;
10720Sstevel@tonic-gate 	case DES3_CBC_MECH_INFO_TYPE:
10730Sstevel@tonic-gate 		strength = DES3;
10740Sstevel@tonic-gate 		break;
10750Sstevel@tonic-gate 	default:
10760Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
10770Sstevel@tonic-gate 	}
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate 	if ((keysched = des_alloc_keysched(&size, strength,
10800Sstevel@tonic-gate 	    crypto_kmflag(req))) == NULL) {
10810Sstevel@tonic-gate 		return (CRYPTO_HOST_MEMORY);
10820Sstevel@tonic-gate 	}
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate 	/*
10850Sstevel@tonic-gate 	 * Initialize key schedule.  Key length information is stored
10860Sstevel@tonic-gate 	 * in the key.
10870Sstevel@tonic-gate 	 */
10880Sstevel@tonic-gate 	if ((rv = init_keysched(key, keysched, strength)) != CRYPTO_SUCCESS) {
10890Sstevel@tonic-gate 		bzero(keysched, size);
10900Sstevel@tonic-gate 		kmem_free(keysched, size);
10910Sstevel@tonic-gate 		return (rv);
10920Sstevel@tonic-gate 	}
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate 	*tmpl = keysched;
10950Sstevel@tonic-gate 	*tmpl_size = size;
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate /* EXPORT DELETE END */
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
11000Sstevel@tonic-gate }
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate /* ARGSUSED */
11030Sstevel@tonic-gate static int
des_free_context(crypto_ctx_t * ctx)11040Sstevel@tonic-gate des_free_context(crypto_ctx_t *ctx)
11050Sstevel@tonic-gate {
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate /* EXPORT DELETE START */
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 	des_ctx_t *des_ctx = ctx->cc_provider_private;
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 	if (des_ctx != NULL) {
11127188Smcpowers 		if (des_ctx->dc_flags & PROVIDER_OWNS_KEY_SCHEDULE) {
11130Sstevel@tonic-gate 			ASSERT(des_ctx->dc_keysched_len != 0);
11140Sstevel@tonic-gate 			bzero(des_ctx->dc_keysched, des_ctx->dc_keysched_len);
11150Sstevel@tonic-gate 			kmem_free(des_ctx->dc_keysched,
11160Sstevel@tonic-gate 			    des_ctx->dc_keysched_len);
11170Sstevel@tonic-gate 		}
11187188Smcpowers 		crypto_free_mode_ctx(des_ctx);
11190Sstevel@tonic-gate 		ctx->cc_provider_private = NULL;
11200Sstevel@tonic-gate 	}
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate /* EXPORT DELETE END */
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
11250Sstevel@tonic-gate }
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate /*
11280Sstevel@tonic-gate  * Pass it to des_keycheck() which will
11290Sstevel@tonic-gate  * fix it (parity bits), and check if the fixed key is weak.
11300Sstevel@tonic-gate  */
11310Sstevel@tonic-gate /* ARGSUSED */
11320Sstevel@tonic-gate static int
des_key_check(crypto_provider_handle_t pd,crypto_mechanism_t * mech,crypto_key_t * key)11330Sstevel@tonic-gate des_key_check(crypto_provider_handle_t pd, crypto_mechanism_t *mech,
11340Sstevel@tonic-gate     crypto_key_t *key)
11350Sstevel@tonic-gate {
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate /* EXPORT DELETE START */
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate 	int expectedkeylen;
11400Sstevel@tonic-gate 	des_strength_t strength;
11410Sstevel@tonic-gate 	uint8_t keydata[DES3_MAX_KEY_LEN];
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate 	if ((mech == NULL) || (key == NULL))
11440Sstevel@tonic-gate 		return (CRYPTO_ARGUMENTS_BAD);
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate 	switch (mech->cm_type) {
11470Sstevel@tonic-gate 	case DES_ECB_MECH_INFO_TYPE:
11480Sstevel@tonic-gate 	case DES_CBC_MECH_INFO_TYPE:
11490Sstevel@tonic-gate 		expectedkeylen = DES_MINBITS;
11500Sstevel@tonic-gate 		strength = DES;
11510Sstevel@tonic-gate 		break;
11520Sstevel@tonic-gate 	case DES3_ECB_MECH_INFO_TYPE:
11530Sstevel@tonic-gate 	case DES3_CBC_MECH_INFO_TYPE:
115410444SVladimir.Kotal@Sun.COM 		expectedkeylen = DES3_MAXBITS;
11550Sstevel@tonic-gate 		strength = DES3;
11560Sstevel@tonic-gate 		break;
11570Sstevel@tonic-gate 	default:
11580Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
11590Sstevel@tonic-gate 	}
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate 	if (key->ck_format != CRYPTO_KEY_RAW)
11620Sstevel@tonic-gate 		return (CRYPTO_KEY_TYPE_INCONSISTENT);
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate 	if (key->ck_length != expectedkeylen)
11650Sstevel@tonic-gate 		return (CRYPTO_KEY_SIZE_RANGE);
11660Sstevel@tonic-gate 
11670Sstevel@tonic-gate 	bcopy(key->ck_data, keydata, CRYPTO_BITS2BYTES(expectedkeylen));
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 	if (des_keycheck(keydata, strength, key->ck_data) == B_FALSE)
11700Sstevel@tonic-gate 		return (CRYPTO_WEAK_KEY);
11710Sstevel@tonic-gate 
11720Sstevel@tonic-gate /* EXPORT DELETE END */
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
11750Sstevel@tonic-gate }
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate /* ARGSUSED */
11780Sstevel@tonic-gate static int
des_common_init_ctx(des_ctx_t * des_ctx,crypto_spi_ctx_template_t * template,crypto_mechanism_t * mechanism,crypto_key_t * key,des_strength_t strength,int kmflag)11790Sstevel@tonic-gate des_common_init_ctx(des_ctx_t *des_ctx, crypto_spi_ctx_template_t *template,
11800Sstevel@tonic-gate     crypto_mechanism_t *mechanism, crypto_key_t *key, des_strength_t strength,
11810Sstevel@tonic-gate     int kmflag)
11820Sstevel@tonic-gate {
11830Sstevel@tonic-gate 	int rv = CRYPTO_SUCCESS;
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate /* EXPORT DELETE START */
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 	void *keysched;
11880Sstevel@tonic-gate 	size_t size;
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 	if (template == NULL) {
11910Sstevel@tonic-gate 		if ((keysched = des_alloc_keysched(&size, strength,
11920Sstevel@tonic-gate 		    kmflag)) == NULL)
11930Sstevel@tonic-gate 			return (CRYPTO_HOST_MEMORY);
11940Sstevel@tonic-gate 		/*
11950Sstevel@tonic-gate 		 * Initialize key schedule.
11960Sstevel@tonic-gate 		 * Key length is stored in the key.
11970Sstevel@tonic-gate 		 */
11980Sstevel@tonic-gate 		if ((rv = init_keysched(key, keysched,
11990Sstevel@tonic-gate 		    strength)) != CRYPTO_SUCCESS)
12000Sstevel@tonic-gate 			kmem_free(keysched, size);
12010Sstevel@tonic-gate 
12027188Smcpowers 		des_ctx->dc_flags |= PROVIDER_OWNS_KEY_SCHEDULE;
12030Sstevel@tonic-gate 		des_ctx->dc_keysched_len = size;
12040Sstevel@tonic-gate 	} else {
12050Sstevel@tonic-gate 		keysched = template;
12060Sstevel@tonic-gate 	}
12077188Smcpowers 	des_ctx->dc_keysched = keysched;
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate 	if (strength == DES3) {
12100Sstevel@tonic-gate 		des_ctx->dc_flags |= DES3_STRENGTH;
12110Sstevel@tonic-gate 	}
12120Sstevel@tonic-gate 
12137188Smcpowers 	switch (mechanism->cm_type) {
12147188Smcpowers 	case DES_CBC_MECH_INFO_TYPE:
12157188Smcpowers 	case DES3_CBC_MECH_INFO_TYPE:
12167188Smcpowers 		rv = cbc_init_ctx((cbc_ctx_t *)des_ctx, mechanism->cm_param,
12177188Smcpowers 		    mechanism->cm_param_len, DES_BLOCK_LEN, des_copy_block64);
12187188Smcpowers 		break;
12197188Smcpowers 	case DES_ECB_MECH_INFO_TYPE:
12207188Smcpowers 	case DES3_ECB_MECH_INFO_TYPE:
12217188Smcpowers 		des_ctx->dc_flags |= ECB_MODE;
12227188Smcpowers 	}
12230Sstevel@tonic-gate 
12247188Smcpowers 	if (rv != CRYPTO_SUCCESS) {
12257188Smcpowers 		if (des_ctx->dc_flags & PROVIDER_OWNS_KEY_SCHEDULE) {
12267188Smcpowers 			bzero(keysched, size);
12277188Smcpowers 			kmem_free(keysched, size);
12280Sstevel@tonic-gate 		}
12290Sstevel@tonic-gate 	}
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate /* EXPORT DELETE END */
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 	return (rv);
12340Sstevel@tonic-gate }
123510500SHai-May.Chao@Sun.COM 
123610500SHai-May.Chao@Sun.COM /*
123710500SHai-May.Chao@Sun.COM  * Triple DES Power-Up Self-Test
123810500SHai-May.Chao@Sun.COM  */
123910500SHai-May.Chao@Sun.COM void
des_POST(int * rc)124010500SHai-May.Chao@Sun.COM des_POST(int *rc)
124110500SHai-May.Chao@Sun.COM {
124210500SHai-May.Chao@Sun.COM 
124310500SHai-May.Chao@Sun.COM 	*rc = fips_des3_post();
124410500SHai-May.Chao@Sun.COM 
124510500SHai-May.Chao@Sun.COM }
1246