xref: /titanic_53/usr/src/common/crypto/md5/md5.c (revision 554ff184129088135ad2643c1c9832174a17be88)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * Cleaned-up and optimized version of MD5, based on the reference
297c478bd9Sstevel@tonic-gate  * implementation provided in RFC 1321.  See RSA Copyright information
307c478bd9Sstevel@tonic-gate  * below.
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  * NOTE:  All compiler data was gathered with SC4.2, and verified with SC5.x,
337c478bd9Sstevel@tonic-gate  *	  as used to build Solaris 2.7.  Hopefully the compiler behavior won't
347c478bd9Sstevel@tonic-gate  *	  change for the worse in subsequent Solaris builds.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  * MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
417c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate  * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
457c478bd9Sstevel@tonic-gate  * rights reserved.
467c478bd9Sstevel@tonic-gate  *
477c478bd9Sstevel@tonic-gate  * License to copy and use this software is granted provided that it
487c478bd9Sstevel@tonic-gate  * is identified as the "RSA Data Security, Inc. MD5 Message-Digest
497c478bd9Sstevel@tonic-gate  * Algorithm" in all material mentioning or referencing this software
507c478bd9Sstevel@tonic-gate  * or this function.
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  * License is also granted to make and use derivative works provided
537c478bd9Sstevel@tonic-gate  * that such works are identified as "derived from the RSA Data
547c478bd9Sstevel@tonic-gate  * Security, Inc. MD5 Message-Digest Algorithm" in all material
557c478bd9Sstevel@tonic-gate  * mentioning or referencing the derived work.
567c478bd9Sstevel@tonic-gate  *
577c478bd9Sstevel@tonic-gate  * RSA Data Security, Inc. makes no representations concerning either
587c478bd9Sstevel@tonic-gate  * the merchantability of this software or the suitability of this
597c478bd9Sstevel@tonic-gate  * software for any particular purpose. It is provided "as is"
607c478bd9Sstevel@tonic-gate  * without express or implied warranty of any kind.
617c478bd9Sstevel@tonic-gate  *
627c478bd9Sstevel@tonic-gate  * These notices must be retained in any copies of any part of this
637c478bd9Sstevel@tonic-gate  * documentation and/or software.
647c478bd9Sstevel@tonic-gate  */
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #include <sys/types.h>
677c478bd9Sstevel@tonic-gate #include <sys/md5.h>
687c478bd9Sstevel@tonic-gate #include <sys/md5_consts.h>	/* MD5_CONST() optimization */
697c478bd9Sstevel@tonic-gate #if	!defined(_KERNEL) || defined(_BOOT)
707c478bd9Sstevel@tonic-gate #include <strings.h>
717c478bd9Sstevel@tonic-gate #endif /* !_KERNEL || _BOOT */
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #if	defined(_KERNEL) && !defined(_BOOT)
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * In kernel module, the md5 module is created with two modlinkages:
777c478bd9Sstevel@tonic-gate  * - a modlmisc that allows consumers to directly call the entry points
787c478bd9Sstevel@tonic-gate  *   MD5Init, MD5Update, and MD5Final.
797c478bd9Sstevel@tonic-gate  * - a modlcrypto that allows the module to register with the Kernel
807c478bd9Sstevel@tonic-gate  *   Cryptographic Framework (KCF) as a software provider for the MD5
817c478bd9Sstevel@tonic-gate  *   mechanisms.
827c478bd9Sstevel@tonic-gate  */
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate #include <sys/systm.h>
857c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
867c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
877c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
887c478bd9Sstevel@tonic-gate #include <sys/crypto/common.h>
897c478bd9Sstevel@tonic-gate #include <sys/crypto/spi.h>
907c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
917c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
927c478bd9Sstevel@tonic-gate #include <sys/note.h>
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate extern struct mod_ops mod_miscops;
957c478bd9Sstevel@tonic-gate extern struct mod_ops mod_cryptoops;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate /*
987c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
997c478bd9Sstevel@tonic-gate  */
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate static struct modlmisc modlmisc = {
1027c478bd9Sstevel@tonic-gate 	&mod_miscops,
1037c478bd9Sstevel@tonic-gate 	"MD5 Message-Digest Algorithm"
1047c478bd9Sstevel@tonic-gate };
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate static struct modlcrypto modlcrypto = {
1077c478bd9Sstevel@tonic-gate 	&mod_cryptoops,
108*554ff184Skais 	"MD5 Kernel SW Provider 1.17"
1097c478bd9Sstevel@tonic-gate };
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1127c478bd9Sstevel@tonic-gate 	MODREV_1,
1137c478bd9Sstevel@tonic-gate 	(void *)&modlmisc,
1147c478bd9Sstevel@tonic-gate 	(void *)&modlcrypto,
1157c478bd9Sstevel@tonic-gate 	NULL
1167c478bd9Sstevel@tonic-gate };
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate /*
1197c478bd9Sstevel@tonic-gate  * CSPI information (entry points, provider info, etc.)
1207c478bd9Sstevel@tonic-gate  */
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate typedef enum md5_mech_type {
1237c478bd9Sstevel@tonic-gate 	MD5_MECH_INFO_TYPE,		/* SUN_CKM_MD5 */
1247c478bd9Sstevel@tonic-gate 	MD5_HMAC_MECH_INFO_TYPE,	/* SUN_CKM_MD5_HMAC */
1257c478bd9Sstevel@tonic-gate 	MD5_HMAC_GEN_MECH_INFO_TYPE	/* SUN_CKM_MD5_HMAC_GENERAL */
1267c478bd9Sstevel@tonic-gate } md5_mech_type_t;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate #define	MD5_DIGEST_LENGTH	16	/* MD5 digest length in bytes */
1297c478bd9Sstevel@tonic-gate #define	MD5_HMAC_BLOCK_SIZE	64	/* MD5 block size */
1307c478bd9Sstevel@tonic-gate #define	MD5_HMAC_MIN_KEY_LEN	8	/* MD5-HMAC min key length in bits */
1317c478bd9Sstevel@tonic-gate #define	MD5_HMAC_MAX_KEY_LEN	INT_MAX	/* MD5-HMAC max key length in bits */
1327c478bd9Sstevel@tonic-gate #define	MD5_HMAC_INTS_PER_BLOCK	(MD5_HMAC_BLOCK_SIZE/sizeof (uint32_t))
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /*
1357c478bd9Sstevel@tonic-gate  * Context for MD5 mechanism.
1367c478bd9Sstevel@tonic-gate  */
1377c478bd9Sstevel@tonic-gate typedef struct md5_ctx {
1387c478bd9Sstevel@tonic-gate 	md5_mech_type_t		mc_mech_type;	/* type of context */
1397c478bd9Sstevel@tonic-gate 	MD5_CTX			mc_md5_ctx;	/* MD5 context */
1407c478bd9Sstevel@tonic-gate } md5_ctx_t;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate /*
1437c478bd9Sstevel@tonic-gate  * Context for MD5-HMAC and MD5-HMAC-GENERAL mechanisms.
1447c478bd9Sstevel@tonic-gate  */
1457c478bd9Sstevel@tonic-gate typedef struct md5_hmac_ctx {
1467c478bd9Sstevel@tonic-gate 	md5_mech_type_t		hc_mech_type;	/* type of context */
1477c478bd9Sstevel@tonic-gate 	uint32_t		hc_digest_len;	/* digest len in bytes */
1487c478bd9Sstevel@tonic-gate 	MD5_CTX			hc_icontext;	/* inner MD5 context */
1497c478bd9Sstevel@tonic-gate 	MD5_CTX			hc_ocontext;	/* outer MD5 context */
1507c478bd9Sstevel@tonic-gate } md5_hmac_ctx_t;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate /*
1537c478bd9Sstevel@tonic-gate  * Macros to access the MD5 or MD5-HMAC contexts from a context passed
1547c478bd9Sstevel@tonic-gate  * by KCF to one of the entry points.
1557c478bd9Sstevel@tonic-gate  */
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate #define	PROV_MD5_CTX(ctx)	((md5_ctx_t *)(ctx)->cc_provider_private)
1587c478bd9Sstevel@tonic-gate #define	PROV_MD5_HMAC_CTX(ctx)	((md5_hmac_ctx_t *)(ctx)->cc_provider_private)
1597c478bd9Sstevel@tonic-gate /* to extract the digest length passed as mechanism parameter */
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate #define	PROV_MD5_GET_DIGEST_LEN(m, len) {				\
1627c478bd9Sstevel@tonic-gate 	if (IS_P2ALIGNED((m)->cm_param, sizeof (ulong_t)))		\
1637c478bd9Sstevel@tonic-gate 		(len) = (uint32_t)*((ulong_t *)mechanism->cm_param);	\
1647c478bd9Sstevel@tonic-gate 	else {								\
1657c478bd9Sstevel@tonic-gate 		ulong_t tmp_ulong;					\
1667c478bd9Sstevel@tonic-gate 		bcopy((m)->cm_param, &tmp_ulong, sizeof (ulong_t));	\
1677c478bd9Sstevel@tonic-gate 		(len) = (uint32_t)tmp_ulong;				\
1687c478bd9Sstevel@tonic-gate 	}								\
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate #define	PROV_MD5_DIGEST_KEY(ctx, key, len, digest) {	\
1727c478bd9Sstevel@tonic-gate 	MD5Init(ctx);					\
1737c478bd9Sstevel@tonic-gate 	MD5Update(ctx, key, len);			\
1747c478bd9Sstevel@tonic-gate 	MD5Final(digest, ctx);				\
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate /*
1787c478bd9Sstevel@tonic-gate  * Mechanism info structure passed to KCF during registration.
1797c478bd9Sstevel@tonic-gate  */
1807c478bd9Sstevel@tonic-gate static crypto_mech_info_t md5_mech_info_tab[] = {
1817c478bd9Sstevel@tonic-gate 	/* MD5 */
1827c478bd9Sstevel@tonic-gate 	{SUN_CKM_MD5, MD5_MECH_INFO_TYPE,
1837c478bd9Sstevel@tonic-gate 	    CRYPTO_FG_DIGEST | CRYPTO_FG_DIGEST_ATOMIC,
1847c478bd9Sstevel@tonic-gate 	    0, 0, CRYPTO_KEYSIZE_UNIT_IN_BITS},
1857c478bd9Sstevel@tonic-gate 	/* MD5-HMAC */
1867c478bd9Sstevel@tonic-gate 	{SUN_CKM_MD5_HMAC, MD5_HMAC_MECH_INFO_TYPE,
1877c478bd9Sstevel@tonic-gate 	    CRYPTO_FG_MAC | CRYPTO_FG_MAC_ATOMIC,
1887c478bd9Sstevel@tonic-gate 	    MD5_HMAC_MIN_KEY_LEN, MD5_HMAC_MAX_KEY_LEN,
1897c478bd9Sstevel@tonic-gate 	    CRYPTO_KEYSIZE_UNIT_IN_BITS},
1907c478bd9Sstevel@tonic-gate 	/* MD5-HMAC GENERAL */
1917c478bd9Sstevel@tonic-gate 	{SUN_CKM_MD5_HMAC_GENERAL, MD5_HMAC_GEN_MECH_INFO_TYPE,
1927c478bd9Sstevel@tonic-gate 	    CRYPTO_FG_MAC | CRYPTO_FG_MAC_ATOMIC,
1937c478bd9Sstevel@tonic-gate 	    MD5_HMAC_MIN_KEY_LEN, MD5_HMAC_MAX_KEY_LEN,
1947c478bd9Sstevel@tonic-gate 	    CRYPTO_KEYSIZE_UNIT_IN_BITS}
1957c478bd9Sstevel@tonic-gate };
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate static void md5_provider_status(crypto_provider_handle_t, uint_t *);
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate static crypto_control_ops_t md5_control_ops = {
2007c478bd9Sstevel@tonic-gate 	md5_provider_status
2017c478bd9Sstevel@tonic-gate };
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate static int md5_digest_init(crypto_ctx_t *, crypto_mechanism_t *,
2047c478bd9Sstevel@tonic-gate     crypto_req_handle_t);
2057c478bd9Sstevel@tonic-gate static int md5_digest(crypto_ctx_t *, crypto_data_t *, crypto_data_t *,
2067c478bd9Sstevel@tonic-gate     crypto_req_handle_t);
2077c478bd9Sstevel@tonic-gate static int md5_digest_update(crypto_ctx_t *, crypto_data_t *,
2087c478bd9Sstevel@tonic-gate     crypto_req_handle_t);
2097c478bd9Sstevel@tonic-gate static int md5_digest_final(crypto_ctx_t *, crypto_data_t *,
2107c478bd9Sstevel@tonic-gate     crypto_req_handle_t);
2117c478bd9Sstevel@tonic-gate static int md5_digest_atomic(crypto_provider_handle_t, crypto_session_id_t,
2127c478bd9Sstevel@tonic-gate     crypto_mechanism_t *, crypto_data_t *, crypto_data_t *,
2137c478bd9Sstevel@tonic-gate     crypto_req_handle_t);
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate static crypto_digest_ops_t md5_digest_ops = {
2167c478bd9Sstevel@tonic-gate 	md5_digest_init,
2177c478bd9Sstevel@tonic-gate 	md5_digest,
2187c478bd9Sstevel@tonic-gate 	md5_digest_update,
2197c478bd9Sstevel@tonic-gate 	NULL,
2207c478bd9Sstevel@tonic-gate 	md5_digest_final,
2217c478bd9Sstevel@tonic-gate 	md5_digest_atomic
2227c478bd9Sstevel@tonic-gate };
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate static int md5_mac_init(crypto_ctx_t *, crypto_mechanism_t *, crypto_key_t *,
2257c478bd9Sstevel@tonic-gate     crypto_spi_ctx_template_t, crypto_req_handle_t);
2267c478bd9Sstevel@tonic-gate static int md5_mac_update(crypto_ctx_t *, crypto_data_t *, crypto_req_handle_t);
2277c478bd9Sstevel@tonic-gate static int md5_mac_final(crypto_ctx_t *, crypto_data_t *, crypto_req_handle_t);
2287c478bd9Sstevel@tonic-gate static int md5_mac_atomic(crypto_provider_handle_t, crypto_session_id_t,
2297c478bd9Sstevel@tonic-gate     crypto_mechanism_t *, crypto_key_t *, crypto_data_t *, crypto_data_t *,
2307c478bd9Sstevel@tonic-gate     crypto_spi_ctx_template_t, crypto_req_handle_t);
2317c478bd9Sstevel@tonic-gate static int md5_mac_verify_atomic(crypto_provider_handle_t, crypto_session_id_t,
2327c478bd9Sstevel@tonic-gate     crypto_mechanism_t *, crypto_key_t *, crypto_data_t *, crypto_data_t *,
2337c478bd9Sstevel@tonic-gate     crypto_spi_ctx_template_t, crypto_req_handle_t);
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate static crypto_mac_ops_t md5_mac_ops = {
2367c478bd9Sstevel@tonic-gate 	md5_mac_init,
2377c478bd9Sstevel@tonic-gate 	NULL,
2387c478bd9Sstevel@tonic-gate 	md5_mac_update,
2397c478bd9Sstevel@tonic-gate 	md5_mac_final,
2407c478bd9Sstevel@tonic-gate 	md5_mac_atomic,
2417c478bd9Sstevel@tonic-gate 	md5_mac_verify_atomic
2427c478bd9Sstevel@tonic-gate };
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate static int md5_create_ctx_template(crypto_provider_handle_t,
2457c478bd9Sstevel@tonic-gate     crypto_mechanism_t *, crypto_key_t *, crypto_spi_ctx_template_t *,
2467c478bd9Sstevel@tonic-gate     size_t *, crypto_req_handle_t);
2477c478bd9Sstevel@tonic-gate static int md5_free_context(crypto_ctx_t *);
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate static crypto_ctx_ops_t md5_ctx_ops = {
2507c478bd9Sstevel@tonic-gate 	md5_create_ctx_template,
2517c478bd9Sstevel@tonic-gate 	md5_free_context
2527c478bd9Sstevel@tonic-gate };
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate static crypto_ops_t md5_crypto_ops = {
2557c478bd9Sstevel@tonic-gate 	&md5_control_ops,
2567c478bd9Sstevel@tonic-gate 	&md5_digest_ops,
2577c478bd9Sstevel@tonic-gate 	NULL,
2587c478bd9Sstevel@tonic-gate 	&md5_mac_ops,
2597c478bd9Sstevel@tonic-gate 	NULL,
2607c478bd9Sstevel@tonic-gate 	NULL,
2617c478bd9Sstevel@tonic-gate 	NULL,
2627c478bd9Sstevel@tonic-gate 	NULL,
2637c478bd9Sstevel@tonic-gate 	NULL,
2647c478bd9Sstevel@tonic-gate 	NULL,
2657c478bd9Sstevel@tonic-gate 	NULL,
2667c478bd9Sstevel@tonic-gate 	NULL,
2677c478bd9Sstevel@tonic-gate 	NULL,
2687c478bd9Sstevel@tonic-gate 	&md5_ctx_ops
2697c478bd9Sstevel@tonic-gate };
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate static crypto_provider_info_t md5_prov_info = {
2727c478bd9Sstevel@tonic-gate 	CRYPTO_SPI_VERSION_1,
2737c478bd9Sstevel@tonic-gate 	"MD5 Software Provider",
2747c478bd9Sstevel@tonic-gate 	CRYPTO_SW_PROVIDER,
2757c478bd9Sstevel@tonic-gate 	{&modlinkage},
2767c478bd9Sstevel@tonic-gate 	NULL,
2777c478bd9Sstevel@tonic-gate 	&md5_crypto_ops,
2787c478bd9Sstevel@tonic-gate 	sizeof (md5_mech_info_tab)/sizeof (crypto_mech_info_t),
2797c478bd9Sstevel@tonic-gate 	md5_mech_info_tab
2807c478bd9Sstevel@tonic-gate };
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate static crypto_kcf_provider_handle_t md5_prov_handle = NULL;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate int
2857c478bd9Sstevel@tonic-gate _init(void)
2867c478bd9Sstevel@tonic-gate {
2877c478bd9Sstevel@tonic-gate 	int ret;
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	if ((ret = mod_install(&modlinkage)) != 0)
2907c478bd9Sstevel@tonic-gate 		return (ret);
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	/*
2937c478bd9Sstevel@tonic-gate 	 * Register with KCF. If the registration fails, log an
2947c478bd9Sstevel@tonic-gate 	 * error but do not uninstall the module, since the functionality
2957c478bd9Sstevel@tonic-gate 	 * provided by misc/md5 should still be available.
2967c478bd9Sstevel@tonic-gate 	 */
2977c478bd9Sstevel@tonic-gate 	if ((ret = crypto_register_provider(&md5_prov_info,
2987c478bd9Sstevel@tonic-gate 	    &md5_prov_handle)) != CRYPTO_SUCCESS)
2997c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "md5 _init: "
3007c478bd9Sstevel@tonic-gate 		    "crypto_register_provider() failed (0x%x)", ret);
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	return (0);
3037c478bd9Sstevel@tonic-gate }
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate int
3067c478bd9Sstevel@tonic-gate _fini(void)
3077c478bd9Sstevel@tonic-gate {
3087c478bd9Sstevel@tonic-gate 	int ret;
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	/*
3117c478bd9Sstevel@tonic-gate 	 * Unregister from KCF if previous registration succeeded.
3127c478bd9Sstevel@tonic-gate 	 */
3137c478bd9Sstevel@tonic-gate 	if (md5_prov_handle != NULL) {
3147c478bd9Sstevel@tonic-gate 		if ((ret = crypto_unregister_provider(md5_prov_handle)) !=
3157c478bd9Sstevel@tonic-gate 		    CRYPTO_SUCCESS) {
3167c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "md5 _fini: "
3177c478bd9Sstevel@tonic-gate 			    "crypto_unregister_provider() failed (0x%x)", ret);
3187c478bd9Sstevel@tonic-gate 			return (EBUSY);
3197c478bd9Sstevel@tonic-gate 		}
3207c478bd9Sstevel@tonic-gate 		md5_prov_handle = NULL;
3217c478bd9Sstevel@tonic-gate 	}
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
3247c478bd9Sstevel@tonic-gate }
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate int
3277c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
3287c478bd9Sstevel@tonic-gate {
3297c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
3307c478bd9Sstevel@tonic-gate }
3317c478bd9Sstevel@tonic-gate #endif	/* _KERNEL && !_BOOT */
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate static void Encode(uint8_t *, uint32_t *, size_t);
3347c478bd9Sstevel@tonic-gate static void MD5Transform(uint32_t, uint32_t, uint32_t, uint32_t, MD5_CTX *,
3357c478bd9Sstevel@tonic-gate     const uint8_t [64]);
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate static uint8_t PADDING[64] = { 0x80, /* all zeros */ };
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate /*
3407c478bd9Sstevel@tonic-gate  * F, G, H and I are the basic MD5 functions.
3417c478bd9Sstevel@tonic-gate  */
3427c478bd9Sstevel@tonic-gate #define	F(b, c, d)	(((b) & (c)) | ((~b) & (d)))
3437c478bd9Sstevel@tonic-gate #define	G(b, c, d)	(((b) & (d)) | ((c) & (~d)))
3447c478bd9Sstevel@tonic-gate #define	H(b, c, d)	((b) ^ (c) ^ (d))
3457c478bd9Sstevel@tonic-gate #define	I(b, c, d)	((c) ^ ((b) | (~d)))
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate /*
3487c478bd9Sstevel@tonic-gate  * ROTATE_LEFT rotates x left n bits.
3497c478bd9Sstevel@tonic-gate  */
3507c478bd9Sstevel@tonic-gate #define	ROTATE_LEFT(x, n)	\
3517c478bd9Sstevel@tonic-gate 	(((x) << (n)) | ((x) >> ((sizeof (x) << 3) - (n))))
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate /*
3547c478bd9Sstevel@tonic-gate  * FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
3557c478bd9Sstevel@tonic-gate  * Rotation is separate from addition to prevent recomputation.
3567c478bd9Sstevel@tonic-gate  */
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate #define	FF(a, b, c, d, x, s, ac) { \
359*554ff184Skais 	(a) += F((b), (c), (d)) + (x) + ((unsigned long long)(ac)); \
3607c478bd9Sstevel@tonic-gate 	(a) = ROTATE_LEFT((a), (s)); \
3617c478bd9Sstevel@tonic-gate 	(a) += (b); \
3627c478bd9Sstevel@tonic-gate 	}
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate #define	GG(a, b, c, d, x, s, ac) { \
365*554ff184Skais 	(a) += G((b), (c), (d)) + (x) + ((unsigned long long)(ac)); \
3667c478bd9Sstevel@tonic-gate 	(a) = ROTATE_LEFT((a), (s)); \
3677c478bd9Sstevel@tonic-gate 	(a) += (b); \
3687c478bd9Sstevel@tonic-gate 	}
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate #define	HH(a, b, c, d, x, s, ac) { \
371*554ff184Skais 	(a) += H((b), (c), (d)) + (x) + ((unsigned long long)(ac)); \
3727c478bd9Sstevel@tonic-gate 	(a) = ROTATE_LEFT((a), (s)); \
3737c478bd9Sstevel@tonic-gate 	(a) += (b); \
3747c478bd9Sstevel@tonic-gate 	}
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate #define	II(a, b, c, d, x, s, ac) { \
377*554ff184Skais 	(a) += I((b), (c), (d)) + (x) + ((unsigned long long)(ac)); \
3787c478bd9Sstevel@tonic-gate 	(a) = ROTATE_LEFT((a), (s)); \
3797c478bd9Sstevel@tonic-gate 	(a) += (b); \
3807c478bd9Sstevel@tonic-gate 	}
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate /*
3837c478bd9Sstevel@tonic-gate  * Loading 32-bit constants on a RISC is expensive since it involves both a
3847c478bd9Sstevel@tonic-gate  * `sethi' and an `or'.  thus, we instead have the compiler generate `ld's to
3857c478bd9Sstevel@tonic-gate  * load the constants from an array called `md5_consts'.  however, on intel
3867c478bd9Sstevel@tonic-gate  * (and other CISC processors), it is cheaper to load the constant
3877c478bd9Sstevel@tonic-gate  * directly.  thus, the c code in MD5Transform() uses the macro MD5_CONST()
3887c478bd9Sstevel@tonic-gate  * which either expands to a constant or an array reference, depending on the
3897c478bd9Sstevel@tonic-gate  * architecture the code is being compiled for.
3907c478bd9Sstevel@tonic-gate  *
3917c478bd9Sstevel@tonic-gate  * Right now, i386 and amd64 are the CISC exceptions.
3927c478bd9Sstevel@tonic-gate  * If we get another CISC ISA, we'll have to change the ifdef.
3937c478bd9Sstevel@tonic-gate  */
3947c478bd9Sstevel@tonic-gate 
395*554ff184Skais /*
396*554ff184Skais  * Using the %asi register to achieve little endian loads - register
397*554ff184Skais  * is set using a inline template.
398*554ff184Skais  *
399*554ff184Skais  * Saves a few arithmetic ops as can now use an immediate offset with the
400*554ff184Skais  * lduwa instructions.
401*554ff184Skais  */
402*554ff184Skais 
403*554ff184Skais extern void set_little(uint32_t);
404*554ff184Skais extern uint32_t get_little();
405*554ff184Skais 
4067c478bd9Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate #define	MD5_CONST(x)		(MD5_CONST_ ## x)
409*554ff184Skais #define	MD5_CONST_e(x)		MD5_CONST(x)
410*554ff184Skais #define	MD5_CONST_o(x)		MD5_CONST(x)
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate #else
4137c478bd9Sstevel@tonic-gate /*
4147c478bd9Sstevel@tonic-gate  * sparc/RISC optimization:
4157c478bd9Sstevel@tonic-gate  *
4167c478bd9Sstevel@tonic-gate  * while it is somewhat counter-intuitive, on sparc (and presumably other RISC
4177c478bd9Sstevel@tonic-gate  * machines), it is more efficient to place all the constants used in this
4187c478bd9Sstevel@tonic-gate  * function in an array and load the values out of the array than to manually
4197c478bd9Sstevel@tonic-gate  * load the constants.  this is because setting a register to a 32-bit value
4207c478bd9Sstevel@tonic-gate  * takes two ops in most cases: a `sethi' and an `or', but loading a 32-bit
4217c478bd9Sstevel@tonic-gate  * value from memory only takes one `ld' (or `lduw' on v9).  while this
4227c478bd9Sstevel@tonic-gate  * increases memory usage, the compiler can find enough other things to do
4237c478bd9Sstevel@tonic-gate  * while waiting to keep the pipeline does not stall.  additionally, it is
4247c478bd9Sstevel@tonic-gate  * likely that many of these constants are cached so that later accesses do
4257c478bd9Sstevel@tonic-gate  * not even go out to the bus.
4267c478bd9Sstevel@tonic-gate  *
4277c478bd9Sstevel@tonic-gate  * this array is declared `static' to keep the compiler from having to
4287c478bd9Sstevel@tonic-gate  * bcopy() this array onto the stack frame of MD5Transform() each time it is
4297c478bd9Sstevel@tonic-gate  * called -- which is unacceptably expensive.
4307c478bd9Sstevel@tonic-gate  *
4317c478bd9Sstevel@tonic-gate  * the `const' is to ensure that callers are good citizens and do not try to
4327c478bd9Sstevel@tonic-gate  * munge the array.  since these routines are going to be called from inside
4337c478bd9Sstevel@tonic-gate  * multithreaded kernelland, this is a good safety check. -- `constants' will
4347c478bd9Sstevel@tonic-gate  * end up in .rodata.
4357c478bd9Sstevel@tonic-gate  *
4367c478bd9Sstevel@tonic-gate  * unfortunately, loading from an array in this manner hurts performance under
4377c478bd9Sstevel@tonic-gate  * intel (and presumably other CISC machines).  so, there is a macro,
4387c478bd9Sstevel@tonic-gate  * MD5_CONST(), used in MD5Transform(), that either expands to a reference to
4397c478bd9Sstevel@tonic-gate  * this array, or to the actual constant, depending on what platform this code
4407c478bd9Sstevel@tonic-gate  * is compiled for.
4417c478bd9Sstevel@tonic-gate  */
4427c478bd9Sstevel@tonic-gate 
443*554ff184Skais #ifdef sun4v
444*554ff184Skais 
445*554ff184Skais /*
446*554ff184Skais  * Going to load these consts in 8B chunks, so need to enforce 8B alignment
447*554ff184Skais  */
448*554ff184Skais 
449*554ff184Skais /* CSTYLED */
450*554ff184Skais #pragma align 64 (md5_consts)
451*554ff184Skais 
452*554ff184Skais #endif /* sun4v */
453*554ff184Skais 
4547c478bd9Sstevel@tonic-gate static const uint32_t md5_consts[] = {
4557c478bd9Sstevel@tonic-gate 	MD5_CONST_0,	MD5_CONST_1,	MD5_CONST_2,	MD5_CONST_3,
4567c478bd9Sstevel@tonic-gate 	MD5_CONST_4,	MD5_CONST_5,	MD5_CONST_6,	MD5_CONST_7,
4577c478bd9Sstevel@tonic-gate 	MD5_CONST_8,	MD5_CONST_9,	MD5_CONST_10,	MD5_CONST_11,
4587c478bd9Sstevel@tonic-gate 	MD5_CONST_12,	MD5_CONST_13,	MD5_CONST_14,	MD5_CONST_15,
4597c478bd9Sstevel@tonic-gate 	MD5_CONST_16,	MD5_CONST_17,	MD5_CONST_18,	MD5_CONST_19,
4607c478bd9Sstevel@tonic-gate 	MD5_CONST_20,	MD5_CONST_21,	MD5_CONST_22,	MD5_CONST_23,
4617c478bd9Sstevel@tonic-gate 	MD5_CONST_24,	MD5_CONST_25,	MD5_CONST_26,	MD5_CONST_27,
4627c478bd9Sstevel@tonic-gate 	MD5_CONST_28,	MD5_CONST_29,	MD5_CONST_30,	MD5_CONST_31,
4637c478bd9Sstevel@tonic-gate 	MD5_CONST_32,	MD5_CONST_33,	MD5_CONST_34,	MD5_CONST_35,
4647c478bd9Sstevel@tonic-gate 	MD5_CONST_36,	MD5_CONST_37,	MD5_CONST_38,	MD5_CONST_39,
4657c478bd9Sstevel@tonic-gate 	MD5_CONST_40,	MD5_CONST_41,	MD5_CONST_42,	MD5_CONST_43,
4667c478bd9Sstevel@tonic-gate 	MD5_CONST_44,	MD5_CONST_45,	MD5_CONST_46,	MD5_CONST_47,
4677c478bd9Sstevel@tonic-gate 	MD5_CONST_48,	MD5_CONST_49,	MD5_CONST_50,	MD5_CONST_51,
4687c478bd9Sstevel@tonic-gate 	MD5_CONST_52,	MD5_CONST_53,	MD5_CONST_54,	MD5_CONST_55,
4697c478bd9Sstevel@tonic-gate 	MD5_CONST_56,	MD5_CONST_57,	MD5_CONST_58,	MD5_CONST_59,
4707c478bd9Sstevel@tonic-gate 	MD5_CONST_60,	MD5_CONST_61,	MD5_CONST_62,	MD5_CONST_63
4717c478bd9Sstevel@tonic-gate };
4727c478bd9Sstevel@tonic-gate 
473*554ff184Skais 
474*554ff184Skais #ifdef sun4v
475*554ff184Skais /*
476*554ff184Skais  * To reduce the number of loads, load consts in 64-bit
477*554ff184Skais  * chunks and then split.
478*554ff184Skais  *
479*554ff184Skais  * No need to mask upper 32-bits, as just interested in
480*554ff184Skais  * low 32-bits (saves an & operation and means that this
481*554ff184Skais  * optimization doesn't increases the icount.
482*554ff184Skais  */
483*554ff184Skais #define	MD5_CONST_e(x)		(md5_consts64[x/2] >> 32)
484*554ff184Skais #define	MD5_CONST_o(x)		(md5_consts64[x/2])
485*554ff184Skais 
486*554ff184Skais #else
487*554ff184Skais 
488*554ff184Skais #define	MD5_CONST_e(x)		(md5_consts[x])
489*554ff184Skais #define	MD5_CONST_o(x)		(md5_consts[x])
490*554ff184Skais 
491*554ff184Skais #endif /* sun4v */
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate #endif
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate /*
4967c478bd9Sstevel@tonic-gate  * MD5Init()
4977c478bd9Sstevel@tonic-gate  *
4987c478bd9Sstevel@tonic-gate  * purpose: initializes the md5 context and begins and md5 digest operation
4997c478bd9Sstevel@tonic-gate  *   input: MD5_CTX *	: the context to initialize.
5007c478bd9Sstevel@tonic-gate  *  output: void
5017c478bd9Sstevel@tonic-gate  */
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate void
5047c478bd9Sstevel@tonic-gate MD5Init(MD5_CTX *ctx)
5057c478bd9Sstevel@tonic-gate {
5067c478bd9Sstevel@tonic-gate 	ctx->count[0] = ctx->count[1] = 0;
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	/* load magic initialization constants */
5097c478bd9Sstevel@tonic-gate 	ctx->state[0] = MD5_INIT_CONST_1;
5107c478bd9Sstevel@tonic-gate 	ctx->state[1] = MD5_INIT_CONST_2;
5117c478bd9Sstevel@tonic-gate 	ctx->state[2] = MD5_INIT_CONST_3;
5127c478bd9Sstevel@tonic-gate 	ctx->state[3] = MD5_INIT_CONST_4;
5137c478bd9Sstevel@tonic-gate }
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate /*
5167c478bd9Sstevel@tonic-gate  * MD5Update()
5177c478bd9Sstevel@tonic-gate  *
5187c478bd9Sstevel@tonic-gate  * purpose: continues an md5 digest operation, using the message block
5197c478bd9Sstevel@tonic-gate  *          to update the context.
5207c478bd9Sstevel@tonic-gate  *   input: MD5_CTX *	: the context to update
5217c478bd9Sstevel@tonic-gate  *          uint8_t *	: the message block
5227c478bd9Sstevel@tonic-gate  *          uint32_t    : the length of the message block in bytes
5237c478bd9Sstevel@tonic-gate  *  output: void
5247c478bd9Sstevel@tonic-gate  *
5257c478bd9Sstevel@tonic-gate  * MD5 crunches in 64-byte blocks.  All numeric constants here are related to
5267c478bd9Sstevel@tonic-gate  * that property of MD5.
5277c478bd9Sstevel@tonic-gate  */
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate void
5307c478bd9Sstevel@tonic-gate MD5Update(MD5_CTX *ctx, const void *inpp, unsigned int input_len)
5317c478bd9Sstevel@tonic-gate {
5327c478bd9Sstevel@tonic-gate 	uint32_t		i, buf_index, buf_len;
533*554ff184Skais #ifdef	sun4v
534*554ff184Skais 	uint32_t		old_asi;
535*554ff184Skais #endif	/* sun4v */
5367c478bd9Sstevel@tonic-gate 	const unsigned char 	*input = (const unsigned char *)inpp;
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	/* compute (number of bytes computed so far) mod 64 */
5397c478bd9Sstevel@tonic-gate 	buf_index = (ctx->count[0] >> 3) & 0x3F;
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	/* update number of bits hashed into this MD5 computation so far */
5427c478bd9Sstevel@tonic-gate 	if ((ctx->count[0] += (input_len << 3)) < (input_len << 3))
5437c478bd9Sstevel@tonic-gate 	    ctx->count[1]++;
5447c478bd9Sstevel@tonic-gate 	ctx->count[1] += (input_len >> 29);
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	buf_len = 64 - buf_index;
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	/* transform as many times as possible */
5497c478bd9Sstevel@tonic-gate 	i = 0;
5507c478bd9Sstevel@tonic-gate 	if (input_len >= buf_len) {
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 		/*
5537c478bd9Sstevel@tonic-gate 		 * general optimization:
5547c478bd9Sstevel@tonic-gate 		 *
5557c478bd9Sstevel@tonic-gate 		 * only do initial bcopy() and MD5Transform() if
5567c478bd9Sstevel@tonic-gate 		 * buf_index != 0.  if buf_index == 0, we're just
5577c478bd9Sstevel@tonic-gate 		 * wasting our time doing the bcopy() since there
5587c478bd9Sstevel@tonic-gate 		 * wasn't any data left over from a previous call to
5597c478bd9Sstevel@tonic-gate 		 * MD5Update().
5607c478bd9Sstevel@tonic-gate 		 */
5617c478bd9Sstevel@tonic-gate 
562*554ff184Skais #ifdef sun4v
563*554ff184Skais 		/*
564*554ff184Skais 		 * For N1 use %asi register. However, costly to repeatedly set
565*554ff184Skais 		 * in MD5Transform. Therefore, set once here.
566*554ff184Skais 		 * Should probably restore the old value afterwards...
567*554ff184Skais 		 */
568*554ff184Skais 		old_asi = get_little();
569*554ff184Skais 		set_little(0x88);
570*554ff184Skais #endif /* sun4v */
571*554ff184Skais 
5727c478bd9Sstevel@tonic-gate 		if (buf_index) {
5737c478bd9Sstevel@tonic-gate 			bcopy(input, &ctx->buf_un.buf8[buf_index], buf_len);
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 			MD5Transform(ctx->state[0], ctx->state[1],
5767c478bd9Sstevel@tonic-gate 			    ctx->state[2], ctx->state[3], ctx,
5777c478bd9Sstevel@tonic-gate 			    ctx->buf_un.buf8);
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 			i = buf_len;
5807c478bd9Sstevel@tonic-gate 		}
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 		for (; i + 63 < input_len; i += 64)
5837c478bd9Sstevel@tonic-gate 			MD5Transform(ctx->state[0], ctx->state[1],
5847c478bd9Sstevel@tonic-gate 			    ctx->state[2], ctx->state[3], ctx, &input[i]);
5857c478bd9Sstevel@tonic-gate 
586*554ff184Skais 
587*554ff184Skais #ifdef sun4v
588*554ff184Skais 		/*
589*554ff184Skais 		 * Restore old %ASI value
590*554ff184Skais 		 */
591*554ff184Skais 		set_little(old_asi);
592*554ff184Skais #endif /* sun4v */
593*554ff184Skais 
5947c478bd9Sstevel@tonic-gate 		/*
5957c478bd9Sstevel@tonic-gate 		 * general optimization:
5967c478bd9Sstevel@tonic-gate 		 *
5977c478bd9Sstevel@tonic-gate 		 * if i and input_len are the same, return now instead
5987c478bd9Sstevel@tonic-gate 		 * of calling bcopy(), since the bcopy() in this
5997c478bd9Sstevel@tonic-gate 		 * case will be an expensive nop.
6007c478bd9Sstevel@tonic-gate 		 */
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 		if (input_len == i)
6037c478bd9Sstevel@tonic-gate 			return;
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 		buf_index = 0;
6067c478bd9Sstevel@tonic-gate 	}
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	/* buffer remaining input */
6097c478bd9Sstevel@tonic-gate 	bcopy(&input[i], &ctx->buf_un.buf8[buf_index], input_len - i);
6107c478bd9Sstevel@tonic-gate }
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate /*
6137c478bd9Sstevel@tonic-gate  * MD5Final()
6147c478bd9Sstevel@tonic-gate  *
6157c478bd9Sstevel@tonic-gate  * purpose: ends an md5 digest operation, finalizing the message digest and
6167c478bd9Sstevel@tonic-gate  *          zeroing the context.
6177c478bd9Sstevel@tonic-gate  *   input: uint8_t *	: a buffer to store the digest in
6187c478bd9Sstevel@tonic-gate  *          MD5_CTX *   : the context to finalize, save, and zero
6197c478bd9Sstevel@tonic-gate  *  output: void
6207c478bd9Sstevel@tonic-gate  */
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate void
6237c478bd9Sstevel@tonic-gate MD5Final(unsigned char *digest, MD5_CTX *ctx)
6247c478bd9Sstevel@tonic-gate {
6257c478bd9Sstevel@tonic-gate 	uint8_t		bitcount_le[sizeof (ctx->count)];
6267c478bd9Sstevel@tonic-gate 	uint32_t	index = (ctx->count[0] >> 3) & 0x3f;
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	/* store bit count, little endian */
6297c478bd9Sstevel@tonic-gate 	Encode(bitcount_le, ctx->count, sizeof (bitcount_le));
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	/* pad out to 56 mod 64 */
6327c478bd9Sstevel@tonic-gate 	MD5Update(ctx, PADDING, ((index < 56) ? 56 : 120) - index);
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	/* append length (before padding) */
6357c478bd9Sstevel@tonic-gate 	MD5Update(ctx, bitcount_le, sizeof (bitcount_le));
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	/* store state in digest */
6387c478bd9Sstevel@tonic-gate 	Encode(digest, ctx->state, sizeof (ctx->state));
6397c478bd9Sstevel@tonic-gate }
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate #ifndef	_KERNEL
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate void
6447c478bd9Sstevel@tonic-gate md5_calc(unsigned char *output, unsigned char *input, unsigned int inlen)
6457c478bd9Sstevel@tonic-gate {
6467c478bd9Sstevel@tonic-gate 	MD5_CTX context;
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	MD5Init(&context);
6497c478bd9Sstevel@tonic-gate 	MD5Update(&context, input, inlen);
6507c478bd9Sstevel@tonic-gate 	MD5Final(output, &context);
6517c478bd9Sstevel@tonic-gate }
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate #endif	/* !_KERNEL */
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate /*
6567c478bd9Sstevel@tonic-gate  * Little-endian optimization:  I don't need to do any weirdness.   On
6577c478bd9Sstevel@tonic-gate  * some little-endian boxen, I'll have to do alignment checks, but I can do
6587c478bd9Sstevel@tonic-gate  * that below.
6597c478bd9Sstevel@tonic-gate  */
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate #ifdef _LITTLE_ENDIAN
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate #if !defined(__i386) && !defined(__amd64)
6647c478bd9Sstevel@tonic-gate /*
6657c478bd9Sstevel@tonic-gate  * i386 and amd64 don't require aligned 4-byte loads.  The symbol
6667c478bd9Sstevel@tonic-gate  * _MD5_CHECK_ALIGNMENT indicates below whether the MD5Transform function
6677c478bd9Sstevel@tonic-gate  * requires alignment checking.
6687c478bd9Sstevel@tonic-gate  */
6697c478bd9Sstevel@tonic-gate #define	_MD5_CHECK_ALIGNMENT
6707c478bd9Sstevel@tonic-gate #endif /* !__i386 && !__amd64 */
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate #define	LOAD_LITTLE_32(addr)	(*(uint32_t *)(addr))
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate /*
6757c478bd9Sstevel@tonic-gate  * sparc v9/v8plus optimization:
6767c478bd9Sstevel@tonic-gate  *
6777c478bd9Sstevel@tonic-gate  * on the sparc v9/v8plus, we can load data little endian.  however, since
6787c478bd9Sstevel@tonic-gate  * the compiler doesn't have direct support for little endian, we
6797c478bd9Sstevel@tonic-gate  * link to an assembly-language routine `load_little_32' to do
6807c478bd9Sstevel@tonic-gate  * the magic.  note that special care must be taken to ensure the
6817c478bd9Sstevel@tonic-gate  * address is 32-bit aligned -- in the interest of speed, we don't
6827c478bd9Sstevel@tonic-gate  * check to make sure, since careful programming can guarantee this
6837c478bd9Sstevel@tonic-gate  * for us.
6847c478bd9Sstevel@tonic-gate  */
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate #elif	defined(sun4u)
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate /* Define alignment check because we can 4-byte load as little endian. */
6897c478bd9Sstevel@tonic-gate #define	_MD5_CHECK_ALIGNMENT
690*554ff184Skais 
6917c478bd9Sstevel@tonic-gate extern  uint32_t load_little_32(uint32_t *);
6927c478bd9Sstevel@tonic-gate #define	LOAD_LITTLE_32(addr)    load_little_32((uint32_t *)(addr))
6937c478bd9Sstevel@tonic-gate 
694*554ff184Skais #ifdef sun4v
695*554ff184Skais 
696*554ff184Skais /*
697*554ff184Skais  * For N1 want to minimize number of arithmetic operations. This is best
698*554ff184Skais  * achieved by using the %asi register to specify ASI for the lduwa operations.
699*554ff184Skais  * Also, have a separate inline template for each word, so can utilize the
700*554ff184Skais  * immediate offset in lduwa, without relying on the compiler to do the right
701*554ff184Skais  * thing.
702*554ff184Skais  *
703*554ff184Skais  * Moving to 64-bit loads might also be beneficial.
704*554ff184Skais  */
705*554ff184Skais 
706*554ff184Skais extern	uint32_t load_little_32_0(uint32_t *);
707*554ff184Skais extern	uint32_t load_little_32_1(uint32_t *);
708*554ff184Skais extern	uint32_t load_little_32_2(uint32_t *);
709*554ff184Skais extern	uint32_t load_little_32_3(uint32_t *);
710*554ff184Skais extern	uint32_t load_little_32_4(uint32_t *);
711*554ff184Skais extern	uint32_t load_little_32_5(uint32_t *);
712*554ff184Skais extern	uint32_t load_little_32_6(uint32_t *);
713*554ff184Skais extern	uint32_t load_little_32_7(uint32_t *);
714*554ff184Skais extern	uint32_t load_little_32_8(uint32_t *);
715*554ff184Skais extern	uint32_t load_little_32_9(uint32_t *);
716*554ff184Skais extern	uint32_t load_little_32_a(uint32_t *);
717*554ff184Skais extern	uint32_t load_little_32_b(uint32_t *);
718*554ff184Skais extern	uint32_t load_little_32_c(uint32_t *);
719*554ff184Skais extern	uint32_t load_little_32_d(uint32_t *);
720*554ff184Skais extern	uint32_t load_little_32_e(uint32_t *);
721*554ff184Skais extern	uint32_t load_little_32_f(uint32_t *);
722*554ff184Skais #define	LOAD_LITTLE_32_0(addr)	load_little_32_0((uint32_t *)(addr))
723*554ff184Skais #define	LOAD_LITTLE_32_1(addr)	load_little_32_1((uint32_t *)(addr))
724*554ff184Skais #define	LOAD_LITTLE_32_2(addr)	load_little_32_2((uint32_t *)(addr))
725*554ff184Skais #define	LOAD_LITTLE_32_3(addr)	load_little_32_3((uint32_t *)(addr))
726*554ff184Skais #define	LOAD_LITTLE_32_4(addr)	load_little_32_4((uint32_t *)(addr))
727*554ff184Skais #define	LOAD_LITTLE_32_5(addr)	load_little_32_5((uint32_t *)(addr))
728*554ff184Skais #define	LOAD_LITTLE_32_6(addr)	load_little_32_6((uint32_t *)(addr))
729*554ff184Skais #define	LOAD_LITTLE_32_7(addr)	load_little_32_7((uint32_t *)(addr))
730*554ff184Skais #define	LOAD_LITTLE_32_8(addr)	load_little_32_8((uint32_t *)(addr))
731*554ff184Skais #define	LOAD_LITTLE_32_9(addr)	load_little_32_9((uint32_t *)(addr))
732*554ff184Skais #define	LOAD_LITTLE_32_a(addr)	load_little_32_a((uint32_t *)(addr))
733*554ff184Skais #define	LOAD_LITTLE_32_b(addr)	load_little_32_b((uint32_t *)(addr))
734*554ff184Skais #define	LOAD_LITTLE_32_c(addr)	load_little_32_c((uint32_t *)(addr))
735*554ff184Skais #define	LOAD_LITTLE_32_d(addr)	load_little_32_d((uint32_t *)(addr))
736*554ff184Skais #define	LOAD_LITTLE_32_e(addr)	load_little_32_e((uint32_t *)(addr))
737*554ff184Skais #define	LOAD_LITTLE_32_f(addr)	load_little_32_f((uint32_t *)(addr))
738*554ff184Skais #endif /* sun4v */
739*554ff184Skais 
7407c478bd9Sstevel@tonic-gate /* Placate lint */
7417c478bd9Sstevel@tonic-gate #if	defined(__lint)
7427c478bd9Sstevel@tonic-gate uint32_t
7437c478bd9Sstevel@tonic-gate load_little_32(uint32_t *addr)
7447c478bd9Sstevel@tonic-gate {
7457c478bd9Sstevel@tonic-gate 	return (*addr);
7467c478bd9Sstevel@tonic-gate }
7477c478bd9Sstevel@tonic-gate #endif
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate #else	/* big endian -- will work on little endian, but slowly */
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate /* Since we do byte operations, we don't have to check for alignment. */
7527c478bd9Sstevel@tonic-gate #define	LOAD_LITTLE_32(addr)	\
7537c478bd9Sstevel@tonic-gate 	((addr)[0] | ((addr)[1] << 8) | ((addr)[2] << 16) | ((addr)[3] << 24))
7547c478bd9Sstevel@tonic-gate #endif
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate /*
7577c478bd9Sstevel@tonic-gate  * sparc register window optimization:
7587c478bd9Sstevel@tonic-gate  *
7597c478bd9Sstevel@tonic-gate  * `a', `b', `c', and `d' are passed into MD5Transform explicitly
7607c478bd9Sstevel@tonic-gate  * since it increases the number of registers available to the
7617c478bd9Sstevel@tonic-gate  * compiler.  under this scheme, these variables can be held in
7627c478bd9Sstevel@tonic-gate  * %i0 - %i3, which leaves more local and out registers available.
7637c478bd9Sstevel@tonic-gate  */
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate /*
7667c478bd9Sstevel@tonic-gate  * MD5Transform()
7677c478bd9Sstevel@tonic-gate  *
7687c478bd9Sstevel@tonic-gate  * purpose: md5 transformation -- updates the digest based on `block'
7697c478bd9Sstevel@tonic-gate  *   input: uint32_t	: bytes  1 -  4 of the digest
7707c478bd9Sstevel@tonic-gate  *          uint32_t	: bytes  5 -  8 of the digest
7717c478bd9Sstevel@tonic-gate  *          uint32_t	: bytes  9 - 12 of the digest
7727c478bd9Sstevel@tonic-gate  *          uint32_t	: bytes 12 - 16 of the digest
7737c478bd9Sstevel@tonic-gate  *          MD5_CTX *   : the context to update
7747c478bd9Sstevel@tonic-gate  *          uint8_t [64]: the block to use to update the digest
7757c478bd9Sstevel@tonic-gate  *  output: void
7767c478bd9Sstevel@tonic-gate  */
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate static void
7797c478bd9Sstevel@tonic-gate MD5Transform(uint32_t a, uint32_t b, uint32_t c, uint32_t d,
7807c478bd9Sstevel@tonic-gate     MD5_CTX *ctx, const uint8_t block[64])
7817c478bd9Sstevel@tonic-gate {
7827c478bd9Sstevel@tonic-gate 	/*
7837c478bd9Sstevel@tonic-gate 	 * general optimization:
7847c478bd9Sstevel@tonic-gate 	 *
7857c478bd9Sstevel@tonic-gate 	 * use individual integers instead of using an array.  this is a
7867c478bd9Sstevel@tonic-gate 	 * win, although the amount it wins by seems to vary quite a bit.
7877c478bd9Sstevel@tonic-gate 	 */
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 	register uint32_t	x_0, x_1, x_2,  x_3,  x_4,  x_5,  x_6,  x_7;
7907c478bd9Sstevel@tonic-gate 	register uint32_t	x_8, x_9, x_10, x_11, x_12, x_13, x_14, x_15;
791*554ff184Skais #ifdef sun4v
792*554ff184Skais 	unsigned long long 	*md5_consts64;
793*554ff184Skais 
794*554ff184Skais 	md5_consts64 = (unsigned long long *) md5_consts;
795*554ff184Skais #endif	/* sun4v */
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 	/*
7987c478bd9Sstevel@tonic-gate 	 * general optimization:
7997c478bd9Sstevel@tonic-gate 	 *
8007c478bd9Sstevel@tonic-gate 	 * the compiler (at least SC4.2/5.x) generates better code if
8017c478bd9Sstevel@tonic-gate 	 * variable use is localized.  in this case, swapping the integers in
8027c478bd9Sstevel@tonic-gate 	 * this order allows `x_0 'to be swapped nearest to its first use in
8037c478bd9Sstevel@tonic-gate 	 * FF(), and likewise for `x_1' and up.  note that the compiler
8047c478bd9Sstevel@tonic-gate 	 * prefers this to doing each swap right before the FF() that
8057c478bd9Sstevel@tonic-gate 	 * uses it.
8067c478bd9Sstevel@tonic-gate 	 */
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	/*
8097c478bd9Sstevel@tonic-gate 	 * sparc v9/v8plus optimization:
8107c478bd9Sstevel@tonic-gate 	 *
8117c478bd9Sstevel@tonic-gate 	 * if `block' is already aligned on a 4-byte boundary, use the
8127c478bd9Sstevel@tonic-gate 	 * optimized load_little_32() directly.  otherwise, bcopy()
8137c478bd9Sstevel@tonic-gate 	 * into a buffer that *is* aligned on a 4-byte boundary and
8147c478bd9Sstevel@tonic-gate 	 * then do the load_little_32() on that buffer.  benchmarks
8157c478bd9Sstevel@tonic-gate 	 * have shown that using the bcopy() is better than loading
8167c478bd9Sstevel@tonic-gate 	 * the bytes individually and doing the endian-swap by hand.
8177c478bd9Sstevel@tonic-gate 	 *
8187c478bd9Sstevel@tonic-gate 	 * even though it's quite tempting to assign to do:
8197c478bd9Sstevel@tonic-gate 	 *
8207c478bd9Sstevel@tonic-gate 	 * blk = bcopy(blk, ctx->buf_un.buf32, sizeof (ctx->buf_un.buf32));
8217c478bd9Sstevel@tonic-gate 	 *
8227c478bd9Sstevel@tonic-gate 	 * and only have one set of LOAD_LITTLE_32()'s, the compiler (at least
8237c478bd9Sstevel@tonic-gate 	 * SC4.2/5.x) *does not* like that, so please resist the urge.
8247c478bd9Sstevel@tonic-gate 	 */
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate #ifdef _MD5_CHECK_ALIGNMENT
8277c478bd9Sstevel@tonic-gate 	if ((uintptr_t)block & 0x3) {		/* not 4-byte aligned? */
8287c478bd9Sstevel@tonic-gate 		bcopy(block, ctx->buf_un.buf32, sizeof (ctx->buf_un.buf32));
829*554ff184Skais 
830*554ff184Skais #ifdef sun4v
831*554ff184Skais 		x_15 = LOAD_LITTLE_32_f(ctx->buf_un.buf32);
832*554ff184Skais 		x_14 = LOAD_LITTLE_32_e(ctx->buf_un.buf32);
833*554ff184Skais 		x_13 = LOAD_LITTLE_32_d(ctx->buf_un.buf32);
834*554ff184Skais 		x_12 = LOAD_LITTLE_32_c(ctx->buf_un.buf32);
835*554ff184Skais 		x_11 = LOAD_LITTLE_32_b(ctx->buf_un.buf32);
836*554ff184Skais 		x_10 = LOAD_LITTLE_32_a(ctx->buf_un.buf32);
837*554ff184Skais 		x_9  = LOAD_LITTLE_32_9(ctx->buf_un.buf32);
838*554ff184Skais 		x_8  = LOAD_LITTLE_32_8(ctx->buf_un.buf32);
839*554ff184Skais 		x_7  = LOAD_LITTLE_32_7(ctx->buf_un.buf32);
840*554ff184Skais 		x_6  = LOAD_LITTLE_32_6(ctx->buf_un.buf32);
841*554ff184Skais 		x_5  = LOAD_LITTLE_32_5(ctx->buf_un.buf32);
842*554ff184Skais 		x_4  = LOAD_LITTLE_32_4(ctx->buf_un.buf32);
843*554ff184Skais 		x_3  = LOAD_LITTLE_32_3(ctx->buf_un.buf32);
844*554ff184Skais 		x_2  = LOAD_LITTLE_32_2(ctx->buf_un.buf32);
845*554ff184Skais 		x_1  = LOAD_LITTLE_32_1(ctx->buf_un.buf32);
846*554ff184Skais 		x_0  = LOAD_LITTLE_32_0(ctx->buf_un.buf32);
847*554ff184Skais #else
8487c478bd9Sstevel@tonic-gate 		x_15 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 15);
8497c478bd9Sstevel@tonic-gate 		x_14 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 14);
8507c478bd9Sstevel@tonic-gate 		x_13 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 13);
8517c478bd9Sstevel@tonic-gate 		x_12 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 12);
8527c478bd9Sstevel@tonic-gate 		x_11 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 11);
8537c478bd9Sstevel@tonic-gate 		x_10 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 10);
8547c478bd9Sstevel@tonic-gate 		x_9  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  9);
8557c478bd9Sstevel@tonic-gate 		x_8  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  8);
8567c478bd9Sstevel@tonic-gate 		x_7  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  7);
8577c478bd9Sstevel@tonic-gate 		x_6  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  6);
8587c478bd9Sstevel@tonic-gate 		x_5  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  5);
8597c478bd9Sstevel@tonic-gate 		x_4  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  4);
8607c478bd9Sstevel@tonic-gate 		x_3  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  3);
8617c478bd9Sstevel@tonic-gate 		x_2  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  2);
8627c478bd9Sstevel@tonic-gate 		x_1  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  1);
8637c478bd9Sstevel@tonic-gate 		x_0  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  0);
864*554ff184Skais #endif /* sun4v */
8657c478bd9Sstevel@tonic-gate 	} else
8667c478bd9Sstevel@tonic-gate #endif
8677c478bd9Sstevel@tonic-gate 	{
868*554ff184Skais 
869*554ff184Skais #ifdef sun4v
870*554ff184Skais 		x_15 = LOAD_LITTLE_32_f(block);
871*554ff184Skais 		x_14 = LOAD_LITTLE_32_e(block);
872*554ff184Skais 		x_13 = LOAD_LITTLE_32_d(block);
873*554ff184Skais 		x_12 = LOAD_LITTLE_32_c(block);
874*554ff184Skais 		x_11 = LOAD_LITTLE_32_b(block);
875*554ff184Skais 		x_10 = LOAD_LITTLE_32_a(block);
876*554ff184Skais 		x_9  = LOAD_LITTLE_32_9(block);
877*554ff184Skais 		x_8  = LOAD_LITTLE_32_8(block);
878*554ff184Skais 		x_7  = LOAD_LITTLE_32_7(block);
879*554ff184Skais 		x_6  = LOAD_LITTLE_32_6(block);
880*554ff184Skais 		x_5  = LOAD_LITTLE_32_5(block);
881*554ff184Skais 		x_4  = LOAD_LITTLE_32_4(block);
882*554ff184Skais 		x_3  = LOAD_LITTLE_32_3(block);
883*554ff184Skais 		x_2  = LOAD_LITTLE_32_2(block);
884*554ff184Skais 		x_1  = LOAD_LITTLE_32_1(block);
885*554ff184Skais 		x_0  = LOAD_LITTLE_32_0(block);
886*554ff184Skais #else
8877c478bd9Sstevel@tonic-gate 		x_15 = LOAD_LITTLE_32(block + 60);
8887c478bd9Sstevel@tonic-gate 		x_14 = LOAD_LITTLE_32(block + 56);
8897c478bd9Sstevel@tonic-gate 		x_13 = LOAD_LITTLE_32(block + 52);
8907c478bd9Sstevel@tonic-gate 		x_12 = LOAD_LITTLE_32(block + 48);
8917c478bd9Sstevel@tonic-gate 		x_11 = LOAD_LITTLE_32(block + 44);
8927c478bd9Sstevel@tonic-gate 		x_10 = LOAD_LITTLE_32(block + 40);
8937c478bd9Sstevel@tonic-gate 		x_9  = LOAD_LITTLE_32(block + 36);
8947c478bd9Sstevel@tonic-gate 		x_8  = LOAD_LITTLE_32(block + 32);
8957c478bd9Sstevel@tonic-gate 		x_7  = LOAD_LITTLE_32(block + 28);
8967c478bd9Sstevel@tonic-gate 		x_6  = LOAD_LITTLE_32(block + 24);
8977c478bd9Sstevel@tonic-gate 		x_5  = LOAD_LITTLE_32(block + 20);
8987c478bd9Sstevel@tonic-gate 		x_4  = LOAD_LITTLE_32(block + 16);
8997c478bd9Sstevel@tonic-gate 		x_3  = LOAD_LITTLE_32(block + 12);
9007c478bd9Sstevel@tonic-gate 		x_2  = LOAD_LITTLE_32(block +  8);
9017c478bd9Sstevel@tonic-gate 		x_1  = LOAD_LITTLE_32(block +  4);
9027c478bd9Sstevel@tonic-gate 		x_0  = LOAD_LITTLE_32(block +  0);
903*554ff184Skais #endif /* sun4v */
9047c478bd9Sstevel@tonic-gate 	}
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	/* round 1 */
907*554ff184Skais 	FF(a, b, c, d, 	x_0, MD5_SHIFT_11, MD5_CONST_e(0));  /* 1 */
908*554ff184Skais 	FF(d, a, b, c, 	x_1, MD5_SHIFT_12, MD5_CONST_o(1));  /* 2 */
909*554ff184Skais 	FF(c, d, a, b, 	x_2, MD5_SHIFT_13, MD5_CONST_e(2));  /* 3 */
910*554ff184Skais 	FF(b, c, d, a, 	x_3, MD5_SHIFT_14, MD5_CONST_o(3));  /* 4 */
911*554ff184Skais 	FF(a, b, c, d, 	x_4, MD5_SHIFT_11, MD5_CONST_e(4));  /* 5 */
912*554ff184Skais 	FF(d, a, b, c, 	x_5, MD5_SHIFT_12, MD5_CONST_o(5));  /* 6 */
913*554ff184Skais 	FF(c, d, a, b, 	x_6, MD5_SHIFT_13, MD5_CONST_e(6));  /* 7 */
914*554ff184Skais 	FF(b, c, d, a, 	x_7, MD5_SHIFT_14, MD5_CONST_o(7));  /* 8 */
915*554ff184Skais 	FF(a, b, c, d, 	x_8, MD5_SHIFT_11, MD5_CONST_e(8));  /* 9 */
916*554ff184Skais 	FF(d, a, b, c, 	x_9, MD5_SHIFT_12, MD5_CONST_o(9));  /* 10 */
917*554ff184Skais 	FF(c, d, a, b, x_10, MD5_SHIFT_13, MD5_CONST_e(10)); /* 11 */
918*554ff184Skais 	FF(b, c, d, a, x_11, MD5_SHIFT_14, MD5_CONST_o(11)); /* 12 */
919*554ff184Skais 	FF(a, b, c, d, x_12, MD5_SHIFT_11, MD5_CONST_e(12)); /* 13 */
920*554ff184Skais 	FF(d, a, b, c, x_13, MD5_SHIFT_12, MD5_CONST_o(13)); /* 14 */
921*554ff184Skais 	FF(c, d, a, b, x_14, MD5_SHIFT_13, MD5_CONST_e(14)); /* 15 */
922*554ff184Skais 	FF(b, c, d, a, x_15, MD5_SHIFT_14, MD5_CONST_o(15)); /* 16 */
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	/* round 2 */
925*554ff184Skais 	GG(a, b, c, d,  x_1, MD5_SHIFT_21, MD5_CONST_e(16)); /* 17 */
926*554ff184Skais 	GG(d, a, b, c,  x_6, MD5_SHIFT_22, MD5_CONST_o(17)); /* 18 */
927*554ff184Skais 	GG(c, d, a, b, x_11, MD5_SHIFT_23, MD5_CONST_e(18)); /* 19 */
928*554ff184Skais 	GG(b, c, d, a,  x_0, MD5_SHIFT_24, MD5_CONST_o(19)); /* 20 */
929*554ff184Skais 	GG(a, b, c, d,  x_5, MD5_SHIFT_21, MD5_CONST_e(20)); /* 21 */
930*554ff184Skais 	GG(d, a, b, c, x_10, MD5_SHIFT_22, MD5_CONST_o(21)); /* 22 */
931*554ff184Skais 	GG(c, d, a, b, x_15, MD5_SHIFT_23, MD5_CONST_e(22)); /* 23 */
932*554ff184Skais 	GG(b, c, d, a,  x_4, MD5_SHIFT_24, MD5_CONST_o(23)); /* 24 */
933*554ff184Skais 	GG(a, b, c, d,  x_9, MD5_SHIFT_21, MD5_CONST_e(24)); /* 25 */
934*554ff184Skais 	GG(d, a, b, c, x_14, MD5_SHIFT_22, MD5_CONST_o(25)); /* 26 */
935*554ff184Skais 	GG(c, d, a, b,  x_3, MD5_SHIFT_23, MD5_CONST_e(26)); /* 27 */
936*554ff184Skais 	GG(b, c, d, a,  x_8, MD5_SHIFT_24, MD5_CONST_o(27)); /* 28 */
937*554ff184Skais 	GG(a, b, c, d, x_13, MD5_SHIFT_21, MD5_CONST_e(28)); /* 29 */
938*554ff184Skais 	GG(d, a, b, c,  x_2, MD5_SHIFT_22, MD5_CONST_o(29)); /* 30 */
939*554ff184Skais 	GG(c, d, a, b,  x_7, MD5_SHIFT_23, MD5_CONST_e(30)); /* 31 */
940*554ff184Skais 	GG(b, c, d, a, x_12, MD5_SHIFT_24, MD5_CONST_o(31)); /* 32 */
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 	/* round 3 */
943*554ff184Skais 	HH(a, b, c, d,  x_5, MD5_SHIFT_31, MD5_CONST_e(32)); /* 33 */
944*554ff184Skais 	HH(d, a, b, c,  x_8, MD5_SHIFT_32, MD5_CONST_o(33)); /* 34 */
945*554ff184Skais 	HH(c, d, a, b, x_11, MD5_SHIFT_33, MD5_CONST_e(34)); /* 35 */
946*554ff184Skais 	HH(b, c, d, a, x_14, MD5_SHIFT_34, MD5_CONST_o(35)); /* 36 */
947*554ff184Skais 	HH(a, b, c, d,  x_1, MD5_SHIFT_31, MD5_CONST_e(36)); /* 37 */
948*554ff184Skais 	HH(d, a, b, c,  x_4, MD5_SHIFT_32, MD5_CONST_o(37)); /* 38 */
949*554ff184Skais 	HH(c, d, a, b,  x_7, MD5_SHIFT_33, MD5_CONST_e(38)); /* 39 */
950*554ff184Skais 	HH(b, c, d, a, x_10, MD5_SHIFT_34, MD5_CONST_o(39)); /* 40 */
951*554ff184Skais 	HH(a, b, c, d, x_13, MD5_SHIFT_31, MD5_CONST_e(40)); /* 41 */
952*554ff184Skais 	HH(d, a, b, c,  x_0, MD5_SHIFT_32, MD5_CONST_o(41)); /* 42 */
953*554ff184Skais 	HH(c, d, a, b,  x_3, MD5_SHIFT_33, MD5_CONST_e(42)); /* 43 */
954*554ff184Skais 	HH(b, c, d, a,  x_6, MD5_SHIFT_34, MD5_CONST_o(43)); /* 44 */
955*554ff184Skais 	HH(a, b, c, d,  x_9, MD5_SHIFT_31, MD5_CONST_e(44)); /* 45 */
956*554ff184Skais 	HH(d, a, b, c, x_12, MD5_SHIFT_32, MD5_CONST_o(45)); /* 46 */
957*554ff184Skais 	HH(c, d, a, b, x_15, MD5_SHIFT_33, MD5_CONST_e(46)); /* 47 */
958*554ff184Skais 	HH(b, c, d, a,  x_2, MD5_SHIFT_34, MD5_CONST_o(47)); /* 48 */
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	/* round 4 */
961*554ff184Skais 	II(a, b, c, d,  x_0, MD5_SHIFT_41, MD5_CONST_e(48)); /* 49 */
962*554ff184Skais 	II(d, a, b, c,  x_7, MD5_SHIFT_42, MD5_CONST_o(49)); /* 50 */
963*554ff184Skais 	II(c, d, a, b, x_14, MD5_SHIFT_43, MD5_CONST_e(50)); /* 51 */
964*554ff184Skais 	II(b, c, d, a,  x_5, MD5_SHIFT_44, MD5_CONST_o(51)); /* 52 */
965*554ff184Skais 	II(a, b, c, d, x_12, MD5_SHIFT_41, MD5_CONST_e(52)); /* 53 */
966*554ff184Skais 	II(d, a, b, c,  x_3, MD5_SHIFT_42, MD5_CONST_o(53)); /* 54 */
967*554ff184Skais 	II(c, d, a, b, x_10, MD5_SHIFT_43, MD5_CONST_e(54)); /* 55 */
968*554ff184Skais 	II(b, c, d, a,  x_1, MD5_SHIFT_44, MD5_CONST_o(55)); /* 56 */
969*554ff184Skais 	II(a, b, c, d,  x_8, MD5_SHIFT_41, MD5_CONST_e(56)); /* 57 */
970*554ff184Skais 	II(d, a, b, c, x_15, MD5_SHIFT_42, MD5_CONST_o(57)); /* 58 */
971*554ff184Skais 	II(c, d, a, b,  x_6, MD5_SHIFT_43, MD5_CONST_e(58)); /* 59 */
972*554ff184Skais 	II(b, c, d, a, x_13, MD5_SHIFT_44, MD5_CONST_o(59)); /* 60 */
973*554ff184Skais 	II(a, b, c, d,  x_4, MD5_SHIFT_41, MD5_CONST_e(60)); /* 61 */
974*554ff184Skais 	II(d, a, b, c, x_11, MD5_SHIFT_42, MD5_CONST_o(61)); /* 62 */
975*554ff184Skais 	II(c, d, a, b,  x_2, MD5_SHIFT_43, MD5_CONST_e(62)); /* 63 */
976*554ff184Skais 	II(b, c, d, a,  x_9, MD5_SHIFT_44, MD5_CONST_o(63)); /* 64 */
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 	ctx->state[0] += a;
9797c478bd9Sstevel@tonic-gate 	ctx->state[1] += b;
9807c478bd9Sstevel@tonic-gate 	ctx->state[2] += c;
9817c478bd9Sstevel@tonic-gate 	ctx->state[3] += d;
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 	/*
9847c478bd9Sstevel@tonic-gate 	 * zeroize sensitive information -- compiler will optimize
9857c478bd9Sstevel@tonic-gate 	 * this out if everything is kept in registers
9867c478bd9Sstevel@tonic-gate 	 */
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate 	x_0 = x_1  = x_2  = x_3  = x_4  = x_5  = x_6  = x_7 = x_8 = 0;
9897c478bd9Sstevel@tonic-gate 	x_9 = x_10 = x_11 = x_12 = x_13 = x_14 = x_15 = 0;
9907c478bd9Sstevel@tonic-gate }
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate /*
9937c478bd9Sstevel@tonic-gate  * devpro compiler optimization:
9947c478bd9Sstevel@tonic-gate  *
9957c478bd9Sstevel@tonic-gate  * the compiler can generate better code if it knows that `input' and
9967c478bd9Sstevel@tonic-gate  * `output' do not point to the same source.  there is no portable
9977c478bd9Sstevel@tonic-gate  * way to tell the compiler this, but the devpro compiler recognizes the
9987c478bd9Sstevel@tonic-gate  * `_Restrict' keyword to indicate this condition.  use it if possible.
9997c478bd9Sstevel@tonic-gate  */
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate #if defined(__RESTRICT) && !defined(__GNUC__)
10027c478bd9Sstevel@tonic-gate #define	restrict	_Restrict
10037c478bd9Sstevel@tonic-gate #else
10047c478bd9Sstevel@tonic-gate #define	restrict	/* nothing */
10057c478bd9Sstevel@tonic-gate #endif
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate /*
10087c478bd9Sstevel@tonic-gate  * Encode()
10097c478bd9Sstevel@tonic-gate  *
10107c478bd9Sstevel@tonic-gate  * purpose: to convert a list of numbers from big endian to little endian
10117c478bd9Sstevel@tonic-gate  *   input: uint8_t *	: place to store the converted little endian numbers
10127c478bd9Sstevel@tonic-gate  *	    uint32_t *	: place to get numbers to convert from
10137c478bd9Sstevel@tonic-gate  *          size_t	: the length of the input in bytes
10147c478bd9Sstevel@tonic-gate  *  output: void
10157c478bd9Sstevel@tonic-gate  */
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate static void
10187c478bd9Sstevel@tonic-gate Encode(uint8_t *restrict output, uint32_t *restrict input, size_t input_len)
10197c478bd9Sstevel@tonic-gate {
10207c478bd9Sstevel@tonic-gate 	size_t		i, j;
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	for (i = 0, j = 0; j < input_len; i++, j += sizeof (uint32_t)) {
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate #ifdef _LITTLE_ENDIAN
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate #ifdef _MD5_CHECK_ALIGNMENT
10277c478bd9Sstevel@tonic-gate 		if ((uintptr_t)output & 0x3)	/* Not 4-byte aligned */
10287c478bd9Sstevel@tonic-gate 			bcopy(input + i, output + j, 4);
10297c478bd9Sstevel@tonic-gate 		else *(uint32_t *)(output + j) = input[i];
10307c478bd9Sstevel@tonic-gate #else
10317c478bd9Sstevel@tonic-gate 		*(uint32_t *)(output + j) = input[i];
10327c478bd9Sstevel@tonic-gate #endif /* _MD5_CHECK_ALIGNMENT */
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate #else	/* big endian -- will work on little endian, but slowly */
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate 		output[j] = input[i] & 0xff;
10377c478bd9Sstevel@tonic-gate 		output[j + 1] = (input[i] >> 8)  & 0xff;
10387c478bd9Sstevel@tonic-gate 		output[j + 2] = (input[i] >> 16) & 0xff;
10397c478bd9Sstevel@tonic-gate 		output[j + 3] = (input[i] >> 24) & 0xff;
10407c478bd9Sstevel@tonic-gate #endif
10417c478bd9Sstevel@tonic-gate 	}
10427c478bd9Sstevel@tonic-gate }
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate #if	defined(_KERNEL) && !defined(_BOOT)
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate /*
10477c478bd9Sstevel@tonic-gate  * KCF software provider control entry points.
10487c478bd9Sstevel@tonic-gate  */
10497c478bd9Sstevel@tonic-gate /* ARGSUSED */
10507c478bd9Sstevel@tonic-gate static void
10517c478bd9Sstevel@tonic-gate md5_provider_status(crypto_provider_handle_t provider, uint_t *status)
10527c478bd9Sstevel@tonic-gate {
10537c478bd9Sstevel@tonic-gate 	*status = CRYPTO_PROVIDER_READY;
10547c478bd9Sstevel@tonic-gate }
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate /*
10577c478bd9Sstevel@tonic-gate  * KCF software provider digest entry points.
10587c478bd9Sstevel@tonic-gate  */
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate static int
10617c478bd9Sstevel@tonic-gate md5_digest_init(crypto_ctx_t *ctx, crypto_mechanism_t *mechanism,
10627c478bd9Sstevel@tonic-gate     crypto_req_handle_t req)
10637c478bd9Sstevel@tonic-gate {
10647c478bd9Sstevel@tonic-gate 	if (mechanism->cm_type != MD5_MECH_INFO_TYPE)
10657c478bd9Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 	/*
10687c478bd9Sstevel@tonic-gate 	 * Allocate and initialize MD5 context.
10697c478bd9Sstevel@tonic-gate 	 */
10707c478bd9Sstevel@tonic-gate 	ctx->cc_provider_private = kmem_alloc(sizeof (md5_ctx_t),
10717c478bd9Sstevel@tonic-gate 	    crypto_kmflag(req));
10727c478bd9Sstevel@tonic-gate 	if (ctx->cc_provider_private == NULL)
10737c478bd9Sstevel@tonic-gate 		return (CRYPTO_HOST_MEMORY);
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 	PROV_MD5_CTX(ctx)->mc_mech_type = MD5_MECH_INFO_TYPE;
10767c478bd9Sstevel@tonic-gate 	MD5Init(&PROV_MD5_CTX(ctx)->mc_md5_ctx);
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
10797c478bd9Sstevel@tonic-gate }
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate /*
10827c478bd9Sstevel@tonic-gate  * Helper MD5 digest update function for uio data.
10837c478bd9Sstevel@tonic-gate  */
10847c478bd9Sstevel@tonic-gate static int
10857c478bd9Sstevel@tonic-gate md5_digest_update_uio(MD5_CTX *md5_ctx, crypto_data_t *data)
10867c478bd9Sstevel@tonic-gate {
10877c478bd9Sstevel@tonic-gate 	off_t offset = data->cd_offset;
10887c478bd9Sstevel@tonic-gate 	size_t length = data->cd_length;
10897c478bd9Sstevel@tonic-gate 	uint_t vec_idx;
10907c478bd9Sstevel@tonic-gate 	size_t cur_len;
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 	/* we support only kernel buffer */
10937c478bd9Sstevel@tonic-gate 	if (data->cd_uio->uio_segflg != UIO_SYSSPACE)
10947c478bd9Sstevel@tonic-gate 		return (CRYPTO_ARGUMENTS_BAD);
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 	/*
10977c478bd9Sstevel@tonic-gate 	 * Jump to the first iovec containing data to be
10987c478bd9Sstevel@tonic-gate 	 * digested.
10997c478bd9Sstevel@tonic-gate 	 */
11007c478bd9Sstevel@tonic-gate 	for (vec_idx = 0; vec_idx < data->cd_uio->uio_iovcnt &&
11017c478bd9Sstevel@tonic-gate 	    offset >= data->cd_uio->uio_iov[vec_idx].iov_len;
11027c478bd9Sstevel@tonic-gate 	    offset -= data->cd_uio->uio_iov[vec_idx++].iov_len);
11037c478bd9Sstevel@tonic-gate 	if (vec_idx == data->cd_uio->uio_iovcnt) {
11047c478bd9Sstevel@tonic-gate 		/*
11057c478bd9Sstevel@tonic-gate 		 * The caller specified an offset that is larger than the
11067c478bd9Sstevel@tonic-gate 		 * total size of the buffers it provided.
11077c478bd9Sstevel@tonic-gate 		 */
11087c478bd9Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
11097c478bd9Sstevel@tonic-gate 	}
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 	/*
11127c478bd9Sstevel@tonic-gate 	 * Now do the digesting on the iovecs.
11137c478bd9Sstevel@tonic-gate 	 */
11147c478bd9Sstevel@tonic-gate 	while (vec_idx < data->cd_uio->uio_iovcnt && length > 0) {
11157c478bd9Sstevel@tonic-gate 		cur_len = MIN(data->cd_uio->uio_iov[vec_idx].iov_len -
11167c478bd9Sstevel@tonic-gate 		    offset, length);
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate 		MD5Update(md5_ctx, data->cd_uio->uio_iov[vec_idx].iov_base +
11197c478bd9Sstevel@tonic-gate 		    offset, cur_len);
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 		length -= cur_len;
11227c478bd9Sstevel@tonic-gate 		vec_idx++;
11237c478bd9Sstevel@tonic-gate 		offset = 0;
11247c478bd9Sstevel@tonic-gate 	}
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 	if (vec_idx == data->cd_uio->uio_iovcnt && length > 0) {
11277c478bd9Sstevel@tonic-gate 		/*
11287c478bd9Sstevel@tonic-gate 		 * The end of the specified iovec's was reached but
11297c478bd9Sstevel@tonic-gate 		 * the length requested could not be processed, i.e.
11307c478bd9Sstevel@tonic-gate 		 * The caller requested to digest more data than it provided.
11317c478bd9Sstevel@tonic-gate 		 */
11327c478bd9Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
11337c478bd9Sstevel@tonic-gate 	}
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
11367c478bd9Sstevel@tonic-gate }
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate /*
11397c478bd9Sstevel@tonic-gate  * Helper MD5 digest final function for uio data.
11407c478bd9Sstevel@tonic-gate  * digest_len is the length of the desired digest. If digest_len
11417c478bd9Sstevel@tonic-gate  * is smaller than the default MD5 digest length, the caller
11427c478bd9Sstevel@tonic-gate  * must pass a scratch buffer, digest_scratch, which must
11437c478bd9Sstevel@tonic-gate  * be at least MD5_DIGEST_LENGTH bytes.
11447c478bd9Sstevel@tonic-gate  */
11457c478bd9Sstevel@tonic-gate static int
11467c478bd9Sstevel@tonic-gate md5_digest_final_uio(MD5_CTX *md5_ctx, crypto_data_t *digest,
11477c478bd9Sstevel@tonic-gate     ulong_t digest_len, uchar_t *digest_scratch)
11487c478bd9Sstevel@tonic-gate {
11497c478bd9Sstevel@tonic-gate 	off_t offset = digest->cd_offset;
11507c478bd9Sstevel@tonic-gate 	uint_t vec_idx;
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate 	/* we support only kernel buffer */
11537c478bd9Sstevel@tonic-gate 	if (digest->cd_uio->uio_segflg != UIO_SYSSPACE)
11547c478bd9Sstevel@tonic-gate 		return (CRYPTO_ARGUMENTS_BAD);
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate 	/*
11577c478bd9Sstevel@tonic-gate 	 * Jump to the first iovec containing ptr to the digest to
11587c478bd9Sstevel@tonic-gate 	 * be returned.
11597c478bd9Sstevel@tonic-gate 	 */
11607c478bd9Sstevel@tonic-gate 	for (vec_idx = 0; offset >= digest->cd_uio->uio_iov[vec_idx].iov_len &&
11617c478bd9Sstevel@tonic-gate 	    vec_idx < digest->cd_uio->uio_iovcnt;
11627c478bd9Sstevel@tonic-gate 	    offset -= digest->cd_uio->uio_iov[vec_idx++].iov_len);
11637c478bd9Sstevel@tonic-gate 	if (vec_idx == digest->cd_uio->uio_iovcnt) {
11647c478bd9Sstevel@tonic-gate 		/*
11657c478bd9Sstevel@tonic-gate 		 * The caller specified an offset that is
11667c478bd9Sstevel@tonic-gate 		 * larger than the total size of the buffers
11677c478bd9Sstevel@tonic-gate 		 * it provided.
11687c478bd9Sstevel@tonic-gate 		 */
11697c478bd9Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
11707c478bd9Sstevel@tonic-gate 	}
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 	if (offset + digest_len <=
11737c478bd9Sstevel@tonic-gate 	    digest->cd_uio->uio_iov[vec_idx].iov_len) {
11747c478bd9Sstevel@tonic-gate 		/*
11757c478bd9Sstevel@tonic-gate 		 * The computed MD5 digest will fit in the current
11767c478bd9Sstevel@tonic-gate 		 * iovec.
11777c478bd9Sstevel@tonic-gate 		 */
11787c478bd9Sstevel@tonic-gate 		if (digest_len != MD5_DIGEST_LENGTH) {
11797c478bd9Sstevel@tonic-gate 			/*
11807c478bd9Sstevel@tonic-gate 			 * The caller requested a short digest. Digest
11817c478bd9Sstevel@tonic-gate 			 * into a scratch buffer and return to
11827c478bd9Sstevel@tonic-gate 			 * the user only what was requested.
11837c478bd9Sstevel@tonic-gate 			 */
11847c478bd9Sstevel@tonic-gate 			MD5Final(digest_scratch, md5_ctx);
11857c478bd9Sstevel@tonic-gate 			bcopy(digest_scratch, (uchar_t *)digest->
11867c478bd9Sstevel@tonic-gate 			    cd_uio->uio_iov[vec_idx].iov_base + offset,
11877c478bd9Sstevel@tonic-gate 			    digest_len);
11887c478bd9Sstevel@tonic-gate 		} else {
11897c478bd9Sstevel@tonic-gate 			MD5Final((uchar_t *)digest->
11907c478bd9Sstevel@tonic-gate 			    cd_uio->uio_iov[vec_idx].iov_base + offset,
11917c478bd9Sstevel@tonic-gate 			    md5_ctx);
11927c478bd9Sstevel@tonic-gate 		}
11937c478bd9Sstevel@tonic-gate 	} else {
11947c478bd9Sstevel@tonic-gate 		/*
11957c478bd9Sstevel@tonic-gate 		 * The computed digest will be crossing one or more iovec's.
11967c478bd9Sstevel@tonic-gate 		 * This is bad performance-wise but we need to support it.
11977c478bd9Sstevel@tonic-gate 		 * Allocate a small scratch buffer on the stack and
11987c478bd9Sstevel@tonic-gate 		 * copy it piece meal to the specified digest iovec's.
11997c478bd9Sstevel@tonic-gate 		 */
12007c478bd9Sstevel@tonic-gate 		uchar_t digest_tmp[MD5_DIGEST_LENGTH];
12017c478bd9Sstevel@tonic-gate 		off_t scratch_offset = 0;
12027c478bd9Sstevel@tonic-gate 		size_t length = digest_len;
12037c478bd9Sstevel@tonic-gate 		size_t cur_len;
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate 		MD5Final(digest_tmp, md5_ctx);
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 		while (vec_idx < digest->cd_uio->uio_iovcnt && length > 0) {
12087c478bd9Sstevel@tonic-gate 			cur_len = MIN(digest->cd_uio->uio_iov[vec_idx].iov_len -
12097c478bd9Sstevel@tonic-gate 			    offset, length);
12107c478bd9Sstevel@tonic-gate 			bcopy(digest_tmp + scratch_offset,
12117c478bd9Sstevel@tonic-gate 			    digest->cd_uio->uio_iov[vec_idx].iov_base + offset,
12127c478bd9Sstevel@tonic-gate 			    cur_len);
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 			length -= cur_len;
12157c478bd9Sstevel@tonic-gate 			vec_idx++;
12167c478bd9Sstevel@tonic-gate 			scratch_offset += cur_len;
12177c478bd9Sstevel@tonic-gate 			offset = 0;
12187c478bd9Sstevel@tonic-gate 		}
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 		if (vec_idx == digest->cd_uio->uio_iovcnt && length > 0) {
12217c478bd9Sstevel@tonic-gate 			/*
12227c478bd9Sstevel@tonic-gate 			 * The end of the specified iovec's was reached but
12237c478bd9Sstevel@tonic-gate 			 * the length requested could not be processed, i.e.
12247c478bd9Sstevel@tonic-gate 			 * The caller requested to digest more data than it
12257c478bd9Sstevel@tonic-gate 			 * provided.
12267c478bd9Sstevel@tonic-gate 			 */
12277c478bd9Sstevel@tonic-gate 			return (CRYPTO_DATA_LEN_RANGE);
12287c478bd9Sstevel@tonic-gate 		}
12297c478bd9Sstevel@tonic-gate 	}
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
12327c478bd9Sstevel@tonic-gate }
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate /*
12357c478bd9Sstevel@tonic-gate  * Helper MD5 digest update for mblk's.
12367c478bd9Sstevel@tonic-gate  */
12377c478bd9Sstevel@tonic-gate static int
12387c478bd9Sstevel@tonic-gate md5_digest_update_mblk(MD5_CTX *md5_ctx, crypto_data_t *data)
12397c478bd9Sstevel@tonic-gate {
12407c478bd9Sstevel@tonic-gate 	off_t offset = data->cd_offset;
12417c478bd9Sstevel@tonic-gate 	size_t length = data->cd_length;
12427c478bd9Sstevel@tonic-gate 	mblk_t *mp;
12437c478bd9Sstevel@tonic-gate 	size_t cur_len;
12447c478bd9Sstevel@tonic-gate 
12457c478bd9Sstevel@tonic-gate 	/*
12467c478bd9Sstevel@tonic-gate 	 * Jump to the first mblk_t containing data to be digested.
12477c478bd9Sstevel@tonic-gate 	 */
12487c478bd9Sstevel@tonic-gate 	for (mp = data->cd_mp; mp != NULL && offset >= MBLKL(mp);
12497c478bd9Sstevel@tonic-gate 	    offset -= MBLKL(mp), mp = mp->b_cont);
12507c478bd9Sstevel@tonic-gate 	if (mp == NULL) {
12517c478bd9Sstevel@tonic-gate 		/*
12527c478bd9Sstevel@tonic-gate 		 * The caller specified an offset that is larger than the
12537c478bd9Sstevel@tonic-gate 		 * total size of the buffers it provided.
12547c478bd9Sstevel@tonic-gate 		 */
12557c478bd9Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
12567c478bd9Sstevel@tonic-gate 	}
12577c478bd9Sstevel@tonic-gate 
12587c478bd9Sstevel@tonic-gate 	/*
12597c478bd9Sstevel@tonic-gate 	 * Now do the digesting on the mblk chain.
12607c478bd9Sstevel@tonic-gate 	 */
12617c478bd9Sstevel@tonic-gate 	while (mp != NULL && length > 0) {
12627c478bd9Sstevel@tonic-gate 		cur_len = MIN(MBLKL(mp) - offset, length);
12637c478bd9Sstevel@tonic-gate 		MD5Update(md5_ctx, mp->b_rptr + offset, cur_len);
12647c478bd9Sstevel@tonic-gate 		length -= cur_len;
12657c478bd9Sstevel@tonic-gate 		offset = 0;
12667c478bd9Sstevel@tonic-gate 		mp = mp->b_cont;
12677c478bd9Sstevel@tonic-gate 	}
12687c478bd9Sstevel@tonic-gate 
12697c478bd9Sstevel@tonic-gate 	if (mp == NULL && length > 0) {
12707c478bd9Sstevel@tonic-gate 		/*
12717c478bd9Sstevel@tonic-gate 		 * The end of the mblk was reached but the length requested
12727c478bd9Sstevel@tonic-gate 		 * could not be processed, i.e. The caller requested
12737c478bd9Sstevel@tonic-gate 		 * to digest more data than it provided.
12747c478bd9Sstevel@tonic-gate 		 */
12757c478bd9Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
12767c478bd9Sstevel@tonic-gate 	}
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
12797c478bd9Sstevel@tonic-gate }
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate /*
12827c478bd9Sstevel@tonic-gate  * Helper MD5 digest final for mblk's.
12837c478bd9Sstevel@tonic-gate  * digest_len is the length of the desired digest. If digest_len
12847c478bd9Sstevel@tonic-gate  * is smaller than the default MD5 digest length, the caller
12857c478bd9Sstevel@tonic-gate  * must pass a scratch buffer, digest_scratch, which must
12867c478bd9Sstevel@tonic-gate  * be at least MD5_DIGEST_LENGTH bytes.
12877c478bd9Sstevel@tonic-gate  */
12887c478bd9Sstevel@tonic-gate static int
12897c478bd9Sstevel@tonic-gate md5_digest_final_mblk(MD5_CTX *md5_ctx, crypto_data_t *digest,
12907c478bd9Sstevel@tonic-gate     ulong_t digest_len, uchar_t *digest_scratch)
12917c478bd9Sstevel@tonic-gate {
12927c478bd9Sstevel@tonic-gate 	off_t offset = digest->cd_offset;
12937c478bd9Sstevel@tonic-gate 	mblk_t *mp;
12947c478bd9Sstevel@tonic-gate 
12957c478bd9Sstevel@tonic-gate 	/*
12967c478bd9Sstevel@tonic-gate 	 * Jump to the first mblk_t that will be used to store the digest.
12977c478bd9Sstevel@tonic-gate 	 */
12987c478bd9Sstevel@tonic-gate 	for (mp = digest->cd_mp; mp != NULL && offset >= MBLKL(mp);
12997c478bd9Sstevel@tonic-gate 	    offset -= MBLKL(mp), mp = mp->b_cont);
13007c478bd9Sstevel@tonic-gate 	if (mp == NULL) {
13017c478bd9Sstevel@tonic-gate 		/*
13027c478bd9Sstevel@tonic-gate 		 * The caller specified an offset that is larger than the
13037c478bd9Sstevel@tonic-gate 		 * total size of the buffers it provided.
13047c478bd9Sstevel@tonic-gate 		 */
13057c478bd9Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
13067c478bd9Sstevel@tonic-gate 	}
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 	if (offset + digest_len <= MBLKL(mp)) {
13097c478bd9Sstevel@tonic-gate 		/*
13107c478bd9Sstevel@tonic-gate 		 * The computed MD5 digest will fit in the current mblk.
13117c478bd9Sstevel@tonic-gate 		 * Do the MD5Final() in-place.
13127c478bd9Sstevel@tonic-gate 		 */
13137c478bd9Sstevel@tonic-gate 		if (digest_len != MD5_DIGEST_LENGTH) {
13147c478bd9Sstevel@tonic-gate 			/*
13157c478bd9Sstevel@tonic-gate 			 * The caller requested a short digest. Digest
13167c478bd9Sstevel@tonic-gate 			 * into a scratch buffer and return to
13177c478bd9Sstevel@tonic-gate 			 * the user only what was requested.
13187c478bd9Sstevel@tonic-gate 			 */
13197c478bd9Sstevel@tonic-gate 			MD5Final(digest_scratch, md5_ctx);
13207c478bd9Sstevel@tonic-gate 			bcopy(digest_scratch, mp->b_rptr + offset, digest_len);
13217c478bd9Sstevel@tonic-gate 		} else {
13227c478bd9Sstevel@tonic-gate 			MD5Final(mp->b_rptr + offset, md5_ctx);
13237c478bd9Sstevel@tonic-gate 		}
13247c478bd9Sstevel@tonic-gate 	} else {
13257c478bd9Sstevel@tonic-gate 		/*
13267c478bd9Sstevel@tonic-gate 		 * The computed digest will be crossing one or more mblk's.
13277c478bd9Sstevel@tonic-gate 		 * This is bad performance-wise but we need to support it.
13287c478bd9Sstevel@tonic-gate 		 * Allocate a small scratch buffer on the stack and
13297c478bd9Sstevel@tonic-gate 		 * copy it piece meal to the specified digest iovec's.
13307c478bd9Sstevel@tonic-gate 		 */
13317c478bd9Sstevel@tonic-gate 		uchar_t digest_tmp[MD5_DIGEST_LENGTH];
13327c478bd9Sstevel@tonic-gate 		off_t scratch_offset = 0;
13337c478bd9Sstevel@tonic-gate 		size_t length = digest_len;
13347c478bd9Sstevel@tonic-gate 		size_t cur_len;
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate 		MD5Final(digest_tmp, md5_ctx);
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate 		while (mp != NULL && length > 0) {
13397c478bd9Sstevel@tonic-gate 			cur_len = MIN(MBLKL(mp) - offset, length);
13407c478bd9Sstevel@tonic-gate 			bcopy(digest_tmp + scratch_offset,
13417c478bd9Sstevel@tonic-gate 			    mp->b_rptr + offset, cur_len);
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate 			length -= cur_len;
13447c478bd9Sstevel@tonic-gate 			mp = mp->b_cont;
13457c478bd9Sstevel@tonic-gate 			scratch_offset += cur_len;
13467c478bd9Sstevel@tonic-gate 			offset = 0;
13477c478bd9Sstevel@tonic-gate 		}
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate 		if (mp == NULL && length > 0) {
13507c478bd9Sstevel@tonic-gate 			/*
13517c478bd9Sstevel@tonic-gate 			 * The end of the specified mblk was reached but
13527c478bd9Sstevel@tonic-gate 			 * the length requested could not be processed, i.e.
13537c478bd9Sstevel@tonic-gate 			 * The caller requested to digest more data than it
13547c478bd9Sstevel@tonic-gate 			 * provided.
13557c478bd9Sstevel@tonic-gate 			 */
13567c478bd9Sstevel@tonic-gate 			return (CRYPTO_DATA_LEN_RANGE);
13577c478bd9Sstevel@tonic-gate 		}
13587c478bd9Sstevel@tonic-gate 	}
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
13617c478bd9Sstevel@tonic-gate }
13627c478bd9Sstevel@tonic-gate 
13637c478bd9Sstevel@tonic-gate /* ARGSUSED */
13647c478bd9Sstevel@tonic-gate static int
13657c478bd9Sstevel@tonic-gate md5_digest(crypto_ctx_t *ctx, crypto_data_t *data, crypto_data_t *digest,
13667c478bd9Sstevel@tonic-gate     crypto_req_handle_t req)
13677c478bd9Sstevel@tonic-gate {
13687c478bd9Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
13697c478bd9Sstevel@tonic-gate 
13707c478bd9Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate 	/*
13737c478bd9Sstevel@tonic-gate 	 * We need to just return the length needed to store the output.
13747c478bd9Sstevel@tonic-gate 	 * We should not destroy the context for the following cases.
13757c478bd9Sstevel@tonic-gate 	 */
13767c478bd9Sstevel@tonic-gate 	if ((digest->cd_length == 0) ||
13777c478bd9Sstevel@tonic-gate 	    (digest->cd_length < MD5_DIGEST_LENGTH)) {
13787c478bd9Sstevel@tonic-gate 		digest->cd_length = MD5_DIGEST_LENGTH;
13797c478bd9Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
13807c478bd9Sstevel@tonic-gate 	}
13817c478bd9Sstevel@tonic-gate 
13827c478bd9Sstevel@tonic-gate 	/*
13837c478bd9Sstevel@tonic-gate 	 * Do the MD5 update on the specified input data.
13847c478bd9Sstevel@tonic-gate 	 */
13857c478bd9Sstevel@tonic-gate 	switch (data->cd_format) {
13867c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
13877c478bd9Sstevel@tonic-gate 		MD5Update(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
13887c478bd9Sstevel@tonic-gate 		    data->cd_raw.iov_base + data->cd_offset,
13897c478bd9Sstevel@tonic-gate 		    data->cd_length);
13907c478bd9Sstevel@tonic-gate 		break;
13917c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
13927c478bd9Sstevel@tonic-gate 		ret = md5_digest_update_uio(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
13937c478bd9Sstevel@tonic-gate 		    data);
13947c478bd9Sstevel@tonic-gate 		break;
13957c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
13967c478bd9Sstevel@tonic-gate 		ret = md5_digest_update_mblk(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
13977c478bd9Sstevel@tonic-gate 		    data);
13987c478bd9Sstevel@tonic-gate 		break;
13997c478bd9Sstevel@tonic-gate 	default:
14007c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
14017c478bd9Sstevel@tonic-gate 	}
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate 	if (ret != CRYPTO_SUCCESS) {
14047c478bd9Sstevel@tonic-gate 		/* the update failed, free context and bail */
14057c478bd9Sstevel@tonic-gate 		kmem_free(ctx->cc_provider_private, sizeof (md5_ctx_t));
14067c478bd9Sstevel@tonic-gate 		ctx->cc_provider_private = NULL;
14077c478bd9Sstevel@tonic-gate 		digest->cd_length = 0;
14087c478bd9Sstevel@tonic-gate 		return (ret);
14097c478bd9Sstevel@tonic-gate 	}
14107c478bd9Sstevel@tonic-gate 
14117c478bd9Sstevel@tonic-gate 	/*
14127c478bd9Sstevel@tonic-gate 	 * Do an MD5 final, must be done separately since the digest
14137c478bd9Sstevel@tonic-gate 	 * type can be different than the input data type.
14147c478bd9Sstevel@tonic-gate 	 */
14157c478bd9Sstevel@tonic-gate 	switch (digest->cd_format) {
14167c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
14177c478bd9Sstevel@tonic-gate 		MD5Final((unsigned char *)digest->cd_raw.iov_base +
14187c478bd9Sstevel@tonic-gate 		    digest->cd_offset, &PROV_MD5_CTX(ctx)->mc_md5_ctx);
14197c478bd9Sstevel@tonic-gate 		break;
14207c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
14217c478bd9Sstevel@tonic-gate 		ret = md5_digest_final_uio(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
14227c478bd9Sstevel@tonic-gate 		    digest, MD5_DIGEST_LENGTH, NULL);
14237c478bd9Sstevel@tonic-gate 		break;
14247c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
14257c478bd9Sstevel@tonic-gate 		ret = md5_digest_final_mblk(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
14267c478bd9Sstevel@tonic-gate 		    digest, MD5_DIGEST_LENGTH, NULL);
14277c478bd9Sstevel@tonic-gate 		break;
14287c478bd9Sstevel@tonic-gate 	default:
14297c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
14307c478bd9Sstevel@tonic-gate 	}
14317c478bd9Sstevel@tonic-gate 
14327c478bd9Sstevel@tonic-gate 	/* all done, free context and return */
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
14357c478bd9Sstevel@tonic-gate 		digest->cd_length = MD5_DIGEST_LENGTH;
14367c478bd9Sstevel@tonic-gate 	} else {
14377c478bd9Sstevel@tonic-gate 		digest->cd_length = 0;
14387c478bd9Sstevel@tonic-gate 	}
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate 	kmem_free(ctx->cc_provider_private, sizeof (md5_ctx_t));
14417c478bd9Sstevel@tonic-gate 	ctx->cc_provider_private = NULL;
14427c478bd9Sstevel@tonic-gate 	return (ret);
14437c478bd9Sstevel@tonic-gate }
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate /* ARGSUSED */
14467c478bd9Sstevel@tonic-gate static int
14477c478bd9Sstevel@tonic-gate md5_digest_update(crypto_ctx_t *ctx, crypto_data_t *data,
14487c478bd9Sstevel@tonic-gate     crypto_req_handle_t req)
14497c478bd9Sstevel@tonic-gate {
14507c478bd9Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
14517c478bd9Sstevel@tonic-gate 
14527c478bd9Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
14537c478bd9Sstevel@tonic-gate 
14547c478bd9Sstevel@tonic-gate 	/*
14557c478bd9Sstevel@tonic-gate 	 * Do the MD5 update on the specified input data.
14567c478bd9Sstevel@tonic-gate 	 */
14577c478bd9Sstevel@tonic-gate 	switch (data->cd_format) {
14587c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
14597c478bd9Sstevel@tonic-gate 		MD5Update(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
14607c478bd9Sstevel@tonic-gate 		    data->cd_raw.iov_base + data->cd_offset,
14617c478bd9Sstevel@tonic-gate 		    data->cd_length);
14627c478bd9Sstevel@tonic-gate 		break;
14637c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
14647c478bd9Sstevel@tonic-gate 		ret = md5_digest_update_uio(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
14657c478bd9Sstevel@tonic-gate 		    data);
14667c478bd9Sstevel@tonic-gate 		break;
14677c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
14687c478bd9Sstevel@tonic-gate 		ret = md5_digest_update_mblk(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
14697c478bd9Sstevel@tonic-gate 		    data);
14707c478bd9Sstevel@tonic-gate 		break;
14717c478bd9Sstevel@tonic-gate 	default:
14727c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
14737c478bd9Sstevel@tonic-gate 	}
14747c478bd9Sstevel@tonic-gate 
14757c478bd9Sstevel@tonic-gate 	return (ret);
14767c478bd9Sstevel@tonic-gate }
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate /* ARGSUSED */
14797c478bd9Sstevel@tonic-gate static int
14807c478bd9Sstevel@tonic-gate md5_digest_final(crypto_ctx_t *ctx, crypto_data_t *digest,
14817c478bd9Sstevel@tonic-gate     crypto_req_handle_t req)
14827c478bd9Sstevel@tonic-gate {
14837c478bd9Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate 	/*
14887c478bd9Sstevel@tonic-gate 	 * We need to just return the length needed to store the output.
14897c478bd9Sstevel@tonic-gate 	 * We should not destroy the context for the following cases.
14907c478bd9Sstevel@tonic-gate 	 */
14917c478bd9Sstevel@tonic-gate 	if ((digest->cd_length == 0) ||
14927c478bd9Sstevel@tonic-gate 	    (digest->cd_length < MD5_DIGEST_LENGTH)) {
14937c478bd9Sstevel@tonic-gate 		digest->cd_length = MD5_DIGEST_LENGTH;
14947c478bd9Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
14957c478bd9Sstevel@tonic-gate 	}
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate 	/*
14987c478bd9Sstevel@tonic-gate 	 * Do an MD5 final.
14997c478bd9Sstevel@tonic-gate 	 */
15007c478bd9Sstevel@tonic-gate 	switch (digest->cd_format) {
15017c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
15027c478bd9Sstevel@tonic-gate 		MD5Final((unsigned char *)digest->cd_raw.iov_base +
15037c478bd9Sstevel@tonic-gate 		    digest->cd_offset, &PROV_MD5_CTX(ctx)->mc_md5_ctx);
15047c478bd9Sstevel@tonic-gate 		break;
15057c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
15067c478bd9Sstevel@tonic-gate 		ret = md5_digest_final_uio(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
15077c478bd9Sstevel@tonic-gate 		    digest, MD5_DIGEST_LENGTH, NULL);
15087c478bd9Sstevel@tonic-gate 		break;
15097c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
15107c478bd9Sstevel@tonic-gate 		ret = md5_digest_final_mblk(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
15117c478bd9Sstevel@tonic-gate 		    digest, MD5_DIGEST_LENGTH, NULL);
15127c478bd9Sstevel@tonic-gate 		break;
15137c478bd9Sstevel@tonic-gate 	default:
15147c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
15157c478bd9Sstevel@tonic-gate 	}
15167c478bd9Sstevel@tonic-gate 
15177c478bd9Sstevel@tonic-gate 	/* all done, free context and return */
15187c478bd9Sstevel@tonic-gate 
15197c478bd9Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
15207c478bd9Sstevel@tonic-gate 		digest->cd_length = MD5_DIGEST_LENGTH;
15217c478bd9Sstevel@tonic-gate 	} else {
15227c478bd9Sstevel@tonic-gate 		digest->cd_length = 0;
15237c478bd9Sstevel@tonic-gate 	}
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate 	kmem_free(ctx->cc_provider_private, sizeof (md5_ctx_t));
15267c478bd9Sstevel@tonic-gate 	ctx->cc_provider_private = NULL;
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate 	return (ret);
15297c478bd9Sstevel@tonic-gate }
15307c478bd9Sstevel@tonic-gate 
15317c478bd9Sstevel@tonic-gate /* ARGSUSED */
15327c478bd9Sstevel@tonic-gate static int
15337c478bd9Sstevel@tonic-gate md5_digest_atomic(crypto_provider_handle_t provider,
15347c478bd9Sstevel@tonic-gate     crypto_session_id_t session_id, crypto_mechanism_t *mechanism,
15357c478bd9Sstevel@tonic-gate     crypto_data_t *data, crypto_data_t *digest,
15367c478bd9Sstevel@tonic-gate     crypto_req_handle_t req)
15377c478bd9Sstevel@tonic-gate {
15387c478bd9Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
15397c478bd9Sstevel@tonic-gate 	MD5_CTX md5_ctx;
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate 	if (mechanism->cm_type != MD5_MECH_INFO_TYPE)
15427c478bd9Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
15437c478bd9Sstevel@tonic-gate 
15447c478bd9Sstevel@tonic-gate 	/*
15457c478bd9Sstevel@tonic-gate 	 * Do the MD5 init.
15467c478bd9Sstevel@tonic-gate 	 */
15477c478bd9Sstevel@tonic-gate 	MD5Init(&md5_ctx);
15487c478bd9Sstevel@tonic-gate 
15497c478bd9Sstevel@tonic-gate 	/*
15507c478bd9Sstevel@tonic-gate 	 * Do the MD5 update on the specified input data.
15517c478bd9Sstevel@tonic-gate 	 */
15527c478bd9Sstevel@tonic-gate 	switch (data->cd_format) {
15537c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
15547c478bd9Sstevel@tonic-gate 		MD5Update(&md5_ctx, data->cd_raw.iov_base + data->cd_offset,
15557c478bd9Sstevel@tonic-gate 		    data->cd_length);
15567c478bd9Sstevel@tonic-gate 		break;
15577c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
15587c478bd9Sstevel@tonic-gate 		ret = md5_digest_update_uio(&md5_ctx, data);
15597c478bd9Sstevel@tonic-gate 		break;
15607c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
15617c478bd9Sstevel@tonic-gate 		ret = md5_digest_update_mblk(&md5_ctx, data);
15627c478bd9Sstevel@tonic-gate 		break;
15637c478bd9Sstevel@tonic-gate 	default:
15647c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
15657c478bd9Sstevel@tonic-gate 	}
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate 	if (ret != CRYPTO_SUCCESS) {
15687c478bd9Sstevel@tonic-gate 		/* the update failed, bail */
15697c478bd9Sstevel@tonic-gate 		digest->cd_length = 0;
15707c478bd9Sstevel@tonic-gate 		return (ret);
15717c478bd9Sstevel@tonic-gate 	}
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 	/*
15747c478bd9Sstevel@tonic-gate 	 * Do an MD5 final, must be done separately since the digest
15757c478bd9Sstevel@tonic-gate 	 * type can be different than the input data type.
15767c478bd9Sstevel@tonic-gate 	 */
15777c478bd9Sstevel@tonic-gate 	switch (digest->cd_format) {
15787c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
15797c478bd9Sstevel@tonic-gate 		MD5Final((unsigned char *)digest->cd_raw.iov_base +
15807c478bd9Sstevel@tonic-gate 		    digest->cd_offset, &md5_ctx);
15817c478bd9Sstevel@tonic-gate 		break;
15827c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
15837c478bd9Sstevel@tonic-gate 		ret = md5_digest_final_uio(&md5_ctx, digest,
15847c478bd9Sstevel@tonic-gate 		    MD5_DIGEST_LENGTH, NULL);
15857c478bd9Sstevel@tonic-gate 		break;
15867c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
15877c478bd9Sstevel@tonic-gate 		ret = md5_digest_final_mblk(&md5_ctx, digest,
15887c478bd9Sstevel@tonic-gate 		    MD5_DIGEST_LENGTH, NULL);
15897c478bd9Sstevel@tonic-gate 		break;
15907c478bd9Sstevel@tonic-gate 	default:
15917c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
15927c478bd9Sstevel@tonic-gate 	}
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
15957c478bd9Sstevel@tonic-gate 		digest->cd_length = MD5_DIGEST_LENGTH;
15967c478bd9Sstevel@tonic-gate 	} else {
15977c478bd9Sstevel@tonic-gate 		digest->cd_length = 0;
15987c478bd9Sstevel@tonic-gate 	}
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate 	return (ret);
16017c478bd9Sstevel@tonic-gate }
16027c478bd9Sstevel@tonic-gate 
16037c478bd9Sstevel@tonic-gate /*
16047c478bd9Sstevel@tonic-gate  * KCF software provider mac entry points.
16057c478bd9Sstevel@tonic-gate  *
16067c478bd9Sstevel@tonic-gate  * MD5 HMAC is: MD5(key XOR opad, MD5(key XOR ipad, text))
16077c478bd9Sstevel@tonic-gate  *
16087c478bd9Sstevel@tonic-gate  * Init:
16097c478bd9Sstevel@tonic-gate  * The initialization routine initializes what we denote
16107c478bd9Sstevel@tonic-gate  * as the inner and outer contexts by doing
16117c478bd9Sstevel@tonic-gate  * - for inner context: MD5(key XOR ipad)
16127c478bd9Sstevel@tonic-gate  * - for outer context: MD5(key XOR opad)
16137c478bd9Sstevel@tonic-gate  *
16147c478bd9Sstevel@tonic-gate  * Update:
16157c478bd9Sstevel@tonic-gate  * Each subsequent MD5 HMAC update will result in an
16167c478bd9Sstevel@tonic-gate  * update of the inner context with the specified data.
16177c478bd9Sstevel@tonic-gate  *
16187c478bd9Sstevel@tonic-gate  * Final:
16197c478bd9Sstevel@tonic-gate  * The MD5 HMAC final will do a MD5 final operation on the
16207c478bd9Sstevel@tonic-gate  * inner context, and the resulting digest will be used
16217c478bd9Sstevel@tonic-gate  * as the data for an update on the outer context. Last
16227c478bd9Sstevel@tonic-gate  * but not least, an MD5 final on the outer context will
16237c478bd9Sstevel@tonic-gate  * be performed to obtain the MD5 HMAC digest to return
16247c478bd9Sstevel@tonic-gate  * to the user.
16257c478bd9Sstevel@tonic-gate  */
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate /*
16287c478bd9Sstevel@tonic-gate  * Initialize a MD5-HMAC context.
16297c478bd9Sstevel@tonic-gate  */
16307c478bd9Sstevel@tonic-gate static void
16317c478bd9Sstevel@tonic-gate md5_mac_init_ctx(md5_hmac_ctx_t *ctx, void *keyval, uint_t length_in_bytes)
16327c478bd9Sstevel@tonic-gate {
16337c478bd9Sstevel@tonic-gate 	uint32_t ipad[MD5_HMAC_INTS_PER_BLOCK];
16347c478bd9Sstevel@tonic-gate 	uint32_t opad[MD5_HMAC_INTS_PER_BLOCK];
16357c478bd9Sstevel@tonic-gate 	uint_t i;
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate 	bzero(ipad, MD5_HMAC_BLOCK_SIZE);
16387c478bd9Sstevel@tonic-gate 	bzero(opad, MD5_HMAC_BLOCK_SIZE);
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate 	bcopy(keyval, ipad, length_in_bytes);
16417c478bd9Sstevel@tonic-gate 	bcopy(keyval, opad, length_in_bytes);
16427c478bd9Sstevel@tonic-gate 
16437c478bd9Sstevel@tonic-gate 	/* XOR key with ipad (0x36) and opad (0x5c) */
16447c478bd9Sstevel@tonic-gate 	for (i = 0; i < MD5_HMAC_INTS_PER_BLOCK; i++) {
16457c478bd9Sstevel@tonic-gate 		ipad[i] ^= 0x36363636;
16467c478bd9Sstevel@tonic-gate 		opad[i] ^= 0x5c5c5c5c;
16477c478bd9Sstevel@tonic-gate 	}
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate 	/* perform MD5 on ipad */
16507c478bd9Sstevel@tonic-gate 	MD5Init(&ctx->hc_icontext);
16517c478bd9Sstevel@tonic-gate 	MD5Update(&ctx->hc_icontext, ipad, MD5_HMAC_BLOCK_SIZE);
16527c478bd9Sstevel@tonic-gate 
16537c478bd9Sstevel@tonic-gate 	/* perform MD5 on opad */
16547c478bd9Sstevel@tonic-gate 	MD5Init(&ctx->hc_ocontext);
16557c478bd9Sstevel@tonic-gate 	MD5Update(&ctx->hc_ocontext, opad, MD5_HMAC_BLOCK_SIZE);
16567c478bd9Sstevel@tonic-gate }
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate /*
16597c478bd9Sstevel@tonic-gate  * Initializes a multi-part MAC operation.
16607c478bd9Sstevel@tonic-gate  */
16617c478bd9Sstevel@tonic-gate static int
16627c478bd9Sstevel@tonic-gate md5_mac_init(crypto_ctx_t *ctx, crypto_mechanism_t *mechanism,
16637c478bd9Sstevel@tonic-gate     crypto_key_t *key, crypto_spi_ctx_template_t ctx_template,
16647c478bd9Sstevel@tonic-gate     crypto_req_handle_t req)
16657c478bd9Sstevel@tonic-gate {
16667c478bd9Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
16677c478bd9Sstevel@tonic-gate 	uint_t keylen_in_bytes = CRYPTO_BITS2BYTES(key->ck_length);
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate 	if (mechanism->cm_type != MD5_HMAC_MECH_INFO_TYPE &&
16707c478bd9Sstevel@tonic-gate 	    mechanism->cm_type != MD5_HMAC_GEN_MECH_INFO_TYPE)
16717c478bd9Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate 	/* Add support for key by attributes (RFE 4706552) */
16747c478bd9Sstevel@tonic-gate 	if (key->ck_format != CRYPTO_KEY_RAW)
16757c478bd9Sstevel@tonic-gate 		return (CRYPTO_ARGUMENTS_BAD);
16767c478bd9Sstevel@tonic-gate 
16777c478bd9Sstevel@tonic-gate 	ctx->cc_provider_private = kmem_alloc(sizeof (md5_hmac_ctx_t),
16787c478bd9Sstevel@tonic-gate 	    crypto_kmflag(req));
16797c478bd9Sstevel@tonic-gate 	if (ctx->cc_provider_private == NULL)
16807c478bd9Sstevel@tonic-gate 		return (CRYPTO_HOST_MEMORY);
16817c478bd9Sstevel@tonic-gate 
16827c478bd9Sstevel@tonic-gate 	if (ctx_template != NULL) {
16837c478bd9Sstevel@tonic-gate 		/* reuse context template */
16847c478bd9Sstevel@tonic-gate 		bcopy(ctx_template, PROV_MD5_HMAC_CTX(ctx),
16857c478bd9Sstevel@tonic-gate 		    sizeof (md5_hmac_ctx_t));
16867c478bd9Sstevel@tonic-gate 	} else {
16877c478bd9Sstevel@tonic-gate 		/* no context template, compute context */
16887c478bd9Sstevel@tonic-gate 		if (keylen_in_bytes > MD5_HMAC_BLOCK_SIZE) {
16897c478bd9Sstevel@tonic-gate 			uchar_t digested_key[MD5_DIGEST_LENGTH];
16907c478bd9Sstevel@tonic-gate 			md5_hmac_ctx_t *hmac_ctx = ctx->cc_provider_private;
16917c478bd9Sstevel@tonic-gate 
16927c478bd9Sstevel@tonic-gate 			/*
16937c478bd9Sstevel@tonic-gate 			 * Hash the passed-in key to get a smaller key.
16947c478bd9Sstevel@tonic-gate 			 * The inner context is used since it hasn't been
16957c478bd9Sstevel@tonic-gate 			 * initialized yet.
16967c478bd9Sstevel@tonic-gate 			 */
16977c478bd9Sstevel@tonic-gate 			PROV_MD5_DIGEST_KEY(&hmac_ctx->hc_icontext,
16987c478bd9Sstevel@tonic-gate 			    key->ck_data, keylen_in_bytes, digested_key);
16997c478bd9Sstevel@tonic-gate 			md5_mac_init_ctx(PROV_MD5_HMAC_CTX(ctx),
17007c478bd9Sstevel@tonic-gate 			    digested_key, MD5_DIGEST_LENGTH);
17017c478bd9Sstevel@tonic-gate 		} else {
17027c478bd9Sstevel@tonic-gate 			md5_mac_init_ctx(PROV_MD5_HMAC_CTX(ctx),
17037c478bd9Sstevel@tonic-gate 			    key->ck_data, keylen_in_bytes);
17047c478bd9Sstevel@tonic-gate 		}
17057c478bd9Sstevel@tonic-gate 	}
17067c478bd9Sstevel@tonic-gate 
17077c478bd9Sstevel@tonic-gate 	/*
17087c478bd9Sstevel@tonic-gate 	 * Get the mechanism parameters, if applicable.
17097c478bd9Sstevel@tonic-gate 	 */
17107c478bd9Sstevel@tonic-gate 	PROV_MD5_HMAC_CTX(ctx)->hc_mech_type = mechanism->cm_type;
17117c478bd9Sstevel@tonic-gate 	if (mechanism->cm_type == MD5_HMAC_GEN_MECH_INFO_TYPE) {
17127c478bd9Sstevel@tonic-gate 		if (mechanism->cm_param == NULL ||
17137c478bd9Sstevel@tonic-gate 		    mechanism->cm_param_len != sizeof (ulong_t))
17147c478bd9Sstevel@tonic-gate 			ret = CRYPTO_MECHANISM_PARAM_INVALID;
17157c478bd9Sstevel@tonic-gate 		PROV_MD5_GET_DIGEST_LEN(mechanism,
17167c478bd9Sstevel@tonic-gate 		    PROV_MD5_HMAC_CTX(ctx)->hc_digest_len);
17177c478bd9Sstevel@tonic-gate 		if (PROV_MD5_HMAC_CTX(ctx)->hc_digest_len >
17187c478bd9Sstevel@tonic-gate 		    MD5_DIGEST_LENGTH)
17197c478bd9Sstevel@tonic-gate 			ret = CRYPTO_MECHANISM_PARAM_INVALID;
17207c478bd9Sstevel@tonic-gate 	}
17217c478bd9Sstevel@tonic-gate 
17227c478bd9Sstevel@tonic-gate 	if (ret != CRYPTO_SUCCESS) {
17237c478bd9Sstevel@tonic-gate 		bzero(ctx->cc_provider_private, sizeof (md5_hmac_ctx_t));
17247c478bd9Sstevel@tonic-gate 		kmem_free(ctx->cc_provider_private, sizeof (md5_hmac_ctx_t));
17257c478bd9Sstevel@tonic-gate 		ctx->cc_provider_private = NULL;
17267c478bd9Sstevel@tonic-gate 	}
17277c478bd9Sstevel@tonic-gate 
17287c478bd9Sstevel@tonic-gate 	return (ret);
17297c478bd9Sstevel@tonic-gate }
17307c478bd9Sstevel@tonic-gate 
17317c478bd9Sstevel@tonic-gate 
17327c478bd9Sstevel@tonic-gate /* ARGSUSED */
17337c478bd9Sstevel@tonic-gate static int
17347c478bd9Sstevel@tonic-gate md5_mac_update(crypto_ctx_t *ctx, crypto_data_t *data, crypto_req_handle_t req)
17357c478bd9Sstevel@tonic-gate {
17367c478bd9Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
17377c478bd9Sstevel@tonic-gate 
17387c478bd9Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
17397c478bd9Sstevel@tonic-gate 
17407c478bd9Sstevel@tonic-gate 	/*
17417c478bd9Sstevel@tonic-gate 	 * Do an MD5 update of the inner context using the specified
17427c478bd9Sstevel@tonic-gate 	 * data.
17437c478bd9Sstevel@tonic-gate 	 */
17447c478bd9Sstevel@tonic-gate 	switch (data->cd_format) {
17457c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
17467c478bd9Sstevel@tonic-gate 		MD5Update(&PROV_MD5_HMAC_CTX(ctx)->hc_icontext,
17477c478bd9Sstevel@tonic-gate 		    data->cd_raw.iov_base + data->cd_offset,
17487c478bd9Sstevel@tonic-gate 		    data->cd_length);
17497c478bd9Sstevel@tonic-gate 		break;
17507c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
17517c478bd9Sstevel@tonic-gate 		ret = md5_digest_update_uio(
17527c478bd9Sstevel@tonic-gate 		    &PROV_MD5_HMAC_CTX(ctx)->hc_icontext, data);
17537c478bd9Sstevel@tonic-gate 		break;
17547c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
17557c478bd9Sstevel@tonic-gate 		ret = md5_digest_update_mblk(
17567c478bd9Sstevel@tonic-gate 		    &PROV_MD5_HMAC_CTX(ctx)->hc_icontext, data);
17577c478bd9Sstevel@tonic-gate 		break;
17587c478bd9Sstevel@tonic-gate 	default:
17597c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
17607c478bd9Sstevel@tonic-gate 	}
17617c478bd9Sstevel@tonic-gate 
17627c478bd9Sstevel@tonic-gate 	return (ret);
17637c478bd9Sstevel@tonic-gate }
17647c478bd9Sstevel@tonic-gate 
17657c478bd9Sstevel@tonic-gate /* ARGSUSED */
17667c478bd9Sstevel@tonic-gate static int
17677c478bd9Sstevel@tonic-gate md5_mac_final(crypto_ctx_t *ctx, crypto_data_t *mac, crypto_req_handle_t req)
17687c478bd9Sstevel@tonic-gate {
17697c478bd9Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
17707c478bd9Sstevel@tonic-gate 	uchar_t digest[MD5_DIGEST_LENGTH];
17717c478bd9Sstevel@tonic-gate 	uint32_t digest_len = MD5_DIGEST_LENGTH;
17727c478bd9Sstevel@tonic-gate 
17737c478bd9Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
17747c478bd9Sstevel@tonic-gate 
17757c478bd9Sstevel@tonic-gate 	if (PROV_MD5_HMAC_CTX(ctx)->hc_mech_type == MD5_HMAC_GEN_MECH_INFO_TYPE)
17767c478bd9Sstevel@tonic-gate 	    digest_len = PROV_MD5_HMAC_CTX(ctx)->hc_digest_len;
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate 	/*
17797c478bd9Sstevel@tonic-gate 	 * We need to just return the length needed to store the output.
17807c478bd9Sstevel@tonic-gate 	 * We should not destroy the context for the following cases.
17817c478bd9Sstevel@tonic-gate 	 */
17827c478bd9Sstevel@tonic-gate 	if ((mac->cd_length == 0) || (mac->cd_length < digest_len)) {
17837c478bd9Sstevel@tonic-gate 		mac->cd_length = digest_len;
17847c478bd9Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
17857c478bd9Sstevel@tonic-gate 	}
17867c478bd9Sstevel@tonic-gate 
17877c478bd9Sstevel@tonic-gate 	/*
17887c478bd9Sstevel@tonic-gate 	 * Do an MD5 final on the inner context.
17897c478bd9Sstevel@tonic-gate 	 */
17907c478bd9Sstevel@tonic-gate 	MD5Final(digest, &PROV_MD5_HMAC_CTX(ctx)->hc_icontext);
17917c478bd9Sstevel@tonic-gate 
17927c478bd9Sstevel@tonic-gate 	/*
17937c478bd9Sstevel@tonic-gate 	 * Do an MD5 update on the outer context, feeding the inner
17947c478bd9Sstevel@tonic-gate 	 * digest as data.
17957c478bd9Sstevel@tonic-gate 	 */
17967c478bd9Sstevel@tonic-gate 	MD5Update(&PROV_MD5_HMAC_CTX(ctx)->hc_ocontext, digest,
17977c478bd9Sstevel@tonic-gate 	    MD5_DIGEST_LENGTH);
17987c478bd9Sstevel@tonic-gate 
17997c478bd9Sstevel@tonic-gate 	/*
18007c478bd9Sstevel@tonic-gate 	 * Do an MD5 final on the outer context, storing the computing
18017c478bd9Sstevel@tonic-gate 	 * digest in the users buffer.
18027c478bd9Sstevel@tonic-gate 	 */
18037c478bd9Sstevel@tonic-gate 	switch (mac->cd_format) {
18047c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
18057c478bd9Sstevel@tonic-gate 		if (digest_len != MD5_DIGEST_LENGTH) {
18067c478bd9Sstevel@tonic-gate 			/*
18077c478bd9Sstevel@tonic-gate 			 * The caller requested a short digest. Digest
18087c478bd9Sstevel@tonic-gate 			 * into a scratch buffer and return to
18097c478bd9Sstevel@tonic-gate 			 * the user only what was requested.
18107c478bd9Sstevel@tonic-gate 			 */
18117c478bd9Sstevel@tonic-gate 			MD5Final(digest,
18127c478bd9Sstevel@tonic-gate 			    &PROV_MD5_HMAC_CTX(ctx)->hc_ocontext);
18137c478bd9Sstevel@tonic-gate 			bcopy(digest, (unsigned char *)mac->cd_raw.iov_base +
18147c478bd9Sstevel@tonic-gate 			    mac->cd_offset, digest_len);
18157c478bd9Sstevel@tonic-gate 		} else {
18167c478bd9Sstevel@tonic-gate 			MD5Final((unsigned char *)mac->cd_raw.iov_base +
18177c478bd9Sstevel@tonic-gate 			    mac->cd_offset,
18187c478bd9Sstevel@tonic-gate 			    &PROV_MD5_HMAC_CTX(ctx)->hc_ocontext);
18197c478bd9Sstevel@tonic-gate 		}
18207c478bd9Sstevel@tonic-gate 		break;
18217c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
18227c478bd9Sstevel@tonic-gate 		ret = md5_digest_final_uio(
18237c478bd9Sstevel@tonic-gate 		    &PROV_MD5_HMAC_CTX(ctx)->hc_ocontext, mac,
18247c478bd9Sstevel@tonic-gate 		    digest_len, digest);
18257c478bd9Sstevel@tonic-gate 		break;
18267c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
18277c478bd9Sstevel@tonic-gate 		ret = md5_digest_final_mblk(
18287c478bd9Sstevel@tonic-gate 		    &PROV_MD5_HMAC_CTX(ctx)->hc_ocontext, mac,
18297c478bd9Sstevel@tonic-gate 		    digest_len, digest);
18307c478bd9Sstevel@tonic-gate 		break;
18317c478bd9Sstevel@tonic-gate 	default:
18327c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
18337c478bd9Sstevel@tonic-gate 	}
18347c478bd9Sstevel@tonic-gate 
18357c478bd9Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
18367c478bd9Sstevel@tonic-gate 		mac->cd_length = digest_len;
18377c478bd9Sstevel@tonic-gate 	} else {
18387c478bd9Sstevel@tonic-gate 		mac->cd_length = 0;
18397c478bd9Sstevel@tonic-gate 	}
18407c478bd9Sstevel@tonic-gate 
18417c478bd9Sstevel@tonic-gate 	bzero(ctx->cc_provider_private, sizeof (md5_hmac_ctx_t));
18427c478bd9Sstevel@tonic-gate 	kmem_free(ctx->cc_provider_private, sizeof (md5_hmac_ctx_t));
18437c478bd9Sstevel@tonic-gate 	ctx->cc_provider_private = NULL;
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate 	return (ret);
18467c478bd9Sstevel@tonic-gate }
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate #define	MD5_MAC_UPDATE(data, ctx, ret) {				\
18497c478bd9Sstevel@tonic-gate 	switch (data->cd_format) {					\
18507c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:						\
18517c478bd9Sstevel@tonic-gate 		MD5Update(&(ctx).hc_icontext,				\
18527c478bd9Sstevel@tonic-gate 		    data->cd_raw.iov_base + data->cd_offset,		\
18537c478bd9Sstevel@tonic-gate 		    data->cd_length);					\
18547c478bd9Sstevel@tonic-gate 		break;							\
18557c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:						\
18567c478bd9Sstevel@tonic-gate 		ret = md5_digest_update_uio(&(ctx).hc_icontext,	data);	\
18577c478bd9Sstevel@tonic-gate 		break;							\
18587c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:						\
18597c478bd9Sstevel@tonic-gate 		ret = md5_digest_update_mblk(&(ctx).hc_icontext,	\
18607c478bd9Sstevel@tonic-gate 		    data);						\
18617c478bd9Sstevel@tonic-gate 		break;							\
18627c478bd9Sstevel@tonic-gate 	default:							\
18637c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;				\
18647c478bd9Sstevel@tonic-gate 	}								\
18657c478bd9Sstevel@tonic-gate }
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate 
18687c478bd9Sstevel@tonic-gate /* ARGSUSED */
18697c478bd9Sstevel@tonic-gate static int
18707c478bd9Sstevel@tonic-gate md5_mac_atomic(crypto_provider_handle_t provider,
18717c478bd9Sstevel@tonic-gate     crypto_session_id_t session_id, crypto_mechanism_t *mechanism,
18727c478bd9Sstevel@tonic-gate     crypto_key_t *key, crypto_data_t *data, crypto_data_t *mac,
18737c478bd9Sstevel@tonic-gate     crypto_spi_ctx_template_t ctx_template, crypto_req_handle_t req)
18747c478bd9Sstevel@tonic-gate {
18757c478bd9Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
18767c478bd9Sstevel@tonic-gate 	uchar_t digest[MD5_DIGEST_LENGTH];
18777c478bd9Sstevel@tonic-gate 	md5_hmac_ctx_t md5_hmac_ctx;
18787c478bd9Sstevel@tonic-gate 	uint32_t digest_len = MD5_DIGEST_LENGTH;
18797c478bd9Sstevel@tonic-gate 	uint_t keylen_in_bytes = CRYPTO_BITS2BYTES(key->ck_length);
18807c478bd9Sstevel@tonic-gate 
18817c478bd9Sstevel@tonic-gate 	if (mechanism->cm_type != MD5_HMAC_MECH_INFO_TYPE &&
18827c478bd9Sstevel@tonic-gate 	    mechanism->cm_type != MD5_HMAC_GEN_MECH_INFO_TYPE)
18837c478bd9Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
18847c478bd9Sstevel@tonic-gate 
18857c478bd9Sstevel@tonic-gate 	/* Add support for key by attributes (RFE 4706552) */
18867c478bd9Sstevel@tonic-gate 	if (key->ck_format != CRYPTO_KEY_RAW)
18877c478bd9Sstevel@tonic-gate 		return (CRYPTO_ARGUMENTS_BAD);
18887c478bd9Sstevel@tonic-gate 
18897c478bd9Sstevel@tonic-gate 	if (ctx_template != NULL) {
18907c478bd9Sstevel@tonic-gate 		/* reuse context template */
18917c478bd9Sstevel@tonic-gate 		bcopy(ctx_template, &md5_hmac_ctx, sizeof (md5_hmac_ctx_t));
18927c478bd9Sstevel@tonic-gate 	} else {
18937c478bd9Sstevel@tonic-gate 		/* no context template, compute context */
18947c478bd9Sstevel@tonic-gate 		if (keylen_in_bytes > MD5_HMAC_BLOCK_SIZE) {
18957c478bd9Sstevel@tonic-gate 			/*
18967c478bd9Sstevel@tonic-gate 			 * Hash the passed-in key to get a smaller key.
18977c478bd9Sstevel@tonic-gate 			 * The inner context is used since it hasn't been
18987c478bd9Sstevel@tonic-gate 			 * initialized yet.
18997c478bd9Sstevel@tonic-gate 			 */
19007c478bd9Sstevel@tonic-gate 			PROV_MD5_DIGEST_KEY(&md5_hmac_ctx.hc_icontext,
19017c478bd9Sstevel@tonic-gate 			    key->ck_data, keylen_in_bytes, digest);
19027c478bd9Sstevel@tonic-gate 			md5_mac_init_ctx(&md5_hmac_ctx, digest,
19037c478bd9Sstevel@tonic-gate 			    MD5_DIGEST_LENGTH);
19047c478bd9Sstevel@tonic-gate 		} else {
19057c478bd9Sstevel@tonic-gate 			md5_mac_init_ctx(&md5_hmac_ctx, key->ck_data,
19067c478bd9Sstevel@tonic-gate 			    keylen_in_bytes);
19077c478bd9Sstevel@tonic-gate 		}
19087c478bd9Sstevel@tonic-gate 	}
19097c478bd9Sstevel@tonic-gate 
19107c478bd9Sstevel@tonic-gate 	/*
19117c478bd9Sstevel@tonic-gate 	 * Get the mechanism parameters, if applicable.
19127c478bd9Sstevel@tonic-gate 	 */
19137c478bd9Sstevel@tonic-gate 	if (mechanism->cm_type == MD5_HMAC_GEN_MECH_INFO_TYPE) {
19147c478bd9Sstevel@tonic-gate 		if (mechanism->cm_param == NULL ||
19157c478bd9Sstevel@tonic-gate 		    mechanism->cm_param_len != sizeof (ulong_t)) {
19167c478bd9Sstevel@tonic-gate 			ret = CRYPTO_MECHANISM_PARAM_INVALID;
19177c478bd9Sstevel@tonic-gate 			goto bail;
19187c478bd9Sstevel@tonic-gate 		}
19197c478bd9Sstevel@tonic-gate 		PROV_MD5_GET_DIGEST_LEN(mechanism, digest_len);
19207c478bd9Sstevel@tonic-gate 		if (digest_len > MD5_DIGEST_LENGTH) {
19217c478bd9Sstevel@tonic-gate 			ret = CRYPTO_MECHANISM_PARAM_INVALID;
19227c478bd9Sstevel@tonic-gate 			goto bail;
19237c478bd9Sstevel@tonic-gate 		}
19247c478bd9Sstevel@tonic-gate 	}
19257c478bd9Sstevel@tonic-gate 
19267c478bd9Sstevel@tonic-gate 	/* do an MD5 update of the inner context using the specified data */
19277c478bd9Sstevel@tonic-gate 	MD5_MAC_UPDATE(data, md5_hmac_ctx, ret);
19287c478bd9Sstevel@tonic-gate 	if (ret != CRYPTO_SUCCESS)
19297c478bd9Sstevel@tonic-gate 		/* the update failed, free context and bail */
19307c478bd9Sstevel@tonic-gate 		goto bail;
19317c478bd9Sstevel@tonic-gate 
19327c478bd9Sstevel@tonic-gate 	/* do an MD5 final on the inner context */
19337c478bd9Sstevel@tonic-gate 	MD5Final(digest, &md5_hmac_ctx.hc_icontext);
19347c478bd9Sstevel@tonic-gate 
19357c478bd9Sstevel@tonic-gate 	/*
19367c478bd9Sstevel@tonic-gate 	 * Do an MD5 update on the outer context, feeding the inner
19377c478bd9Sstevel@tonic-gate 	 * digest as data.
19387c478bd9Sstevel@tonic-gate 	 */
19397c478bd9Sstevel@tonic-gate 	MD5Update(&md5_hmac_ctx.hc_ocontext, digest, MD5_DIGEST_LENGTH);
19407c478bd9Sstevel@tonic-gate 
19417c478bd9Sstevel@tonic-gate 	/*
19427c478bd9Sstevel@tonic-gate 	 * Do an MD5 final on the outer context, storing the computed
19437c478bd9Sstevel@tonic-gate 	 * digest in the users buffer.
19447c478bd9Sstevel@tonic-gate 	 */
19457c478bd9Sstevel@tonic-gate 	switch (mac->cd_format) {
19467c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
19477c478bd9Sstevel@tonic-gate 		if (digest_len != MD5_DIGEST_LENGTH) {
19487c478bd9Sstevel@tonic-gate 			/*
19497c478bd9Sstevel@tonic-gate 			 * The caller requested a short digest. Digest
19507c478bd9Sstevel@tonic-gate 			 * into a scratch buffer and return to
19517c478bd9Sstevel@tonic-gate 			 * the user only what was requested.
19527c478bd9Sstevel@tonic-gate 			 */
19537c478bd9Sstevel@tonic-gate 			MD5Final(digest, &md5_hmac_ctx.hc_ocontext);
19547c478bd9Sstevel@tonic-gate 			bcopy(digest, (unsigned char *)mac->cd_raw.iov_base +
19557c478bd9Sstevel@tonic-gate 			    mac->cd_offset, digest_len);
19567c478bd9Sstevel@tonic-gate 		} else {
19577c478bd9Sstevel@tonic-gate 			MD5Final((unsigned char *)mac->cd_raw.iov_base +
19587c478bd9Sstevel@tonic-gate 			    mac->cd_offset, &md5_hmac_ctx.hc_ocontext);
19597c478bd9Sstevel@tonic-gate 		}
19607c478bd9Sstevel@tonic-gate 		break;
19617c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
19627c478bd9Sstevel@tonic-gate 		ret = md5_digest_final_uio(&md5_hmac_ctx.hc_ocontext, mac,
19637c478bd9Sstevel@tonic-gate 		    digest_len, digest);
19647c478bd9Sstevel@tonic-gate 		break;
19657c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
19667c478bd9Sstevel@tonic-gate 		ret = md5_digest_final_mblk(&md5_hmac_ctx.hc_ocontext, mac,
19677c478bd9Sstevel@tonic-gate 		    digest_len, digest);
19687c478bd9Sstevel@tonic-gate 		break;
19697c478bd9Sstevel@tonic-gate 	default:
19707c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
19717c478bd9Sstevel@tonic-gate 	}
19727c478bd9Sstevel@tonic-gate 
19737c478bd9Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
19747c478bd9Sstevel@tonic-gate 		mac->cd_length = digest_len;
19757c478bd9Sstevel@tonic-gate 	} else {
19767c478bd9Sstevel@tonic-gate 		mac->cd_length = 0;
19777c478bd9Sstevel@tonic-gate 	}
19787c478bd9Sstevel@tonic-gate 	/* Extra paranoia: zeroizing the local context on the stack */
19797c478bd9Sstevel@tonic-gate 	bzero(&md5_hmac_ctx, sizeof (md5_hmac_ctx_t));
19807c478bd9Sstevel@tonic-gate 
19817c478bd9Sstevel@tonic-gate 	return (ret);
19827c478bd9Sstevel@tonic-gate bail:
19837c478bd9Sstevel@tonic-gate 	bzero(&md5_hmac_ctx, sizeof (md5_hmac_ctx_t));
19847c478bd9Sstevel@tonic-gate 	mac->cd_length = 0;
19857c478bd9Sstevel@tonic-gate 	return (ret);
19867c478bd9Sstevel@tonic-gate }
19877c478bd9Sstevel@tonic-gate 
19887c478bd9Sstevel@tonic-gate /* ARGSUSED */
19897c478bd9Sstevel@tonic-gate static int
19907c478bd9Sstevel@tonic-gate md5_mac_verify_atomic(crypto_provider_handle_t provider,
19917c478bd9Sstevel@tonic-gate     crypto_session_id_t session_id, crypto_mechanism_t *mechanism,
19927c478bd9Sstevel@tonic-gate     crypto_key_t *key, crypto_data_t *data, crypto_data_t *mac,
19937c478bd9Sstevel@tonic-gate     crypto_spi_ctx_template_t ctx_template, crypto_req_handle_t req)
19947c478bd9Sstevel@tonic-gate {
19957c478bd9Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
19967c478bd9Sstevel@tonic-gate 	uchar_t digest[MD5_DIGEST_LENGTH];
19977c478bd9Sstevel@tonic-gate 	md5_hmac_ctx_t md5_hmac_ctx;
19987c478bd9Sstevel@tonic-gate 	uint32_t digest_len = MD5_DIGEST_LENGTH;
19997c478bd9Sstevel@tonic-gate 	uint_t keylen_in_bytes = CRYPTO_BITS2BYTES(key->ck_length);
20007c478bd9Sstevel@tonic-gate 
20017c478bd9Sstevel@tonic-gate 	if (mechanism->cm_type != MD5_HMAC_MECH_INFO_TYPE &&
20027c478bd9Sstevel@tonic-gate 	    mechanism->cm_type != MD5_HMAC_GEN_MECH_INFO_TYPE)
20037c478bd9Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
20047c478bd9Sstevel@tonic-gate 
20057c478bd9Sstevel@tonic-gate 	/* Add support for key by attributes (RFE 4706552) */
20067c478bd9Sstevel@tonic-gate 	if (key->ck_format != CRYPTO_KEY_RAW)
20077c478bd9Sstevel@tonic-gate 		return (CRYPTO_ARGUMENTS_BAD);
20087c478bd9Sstevel@tonic-gate 
20097c478bd9Sstevel@tonic-gate 	if (ctx_template != NULL) {
20107c478bd9Sstevel@tonic-gate 		/* reuse context template */
20117c478bd9Sstevel@tonic-gate 		bcopy(ctx_template, &md5_hmac_ctx, sizeof (md5_hmac_ctx_t));
20127c478bd9Sstevel@tonic-gate 	} else {
20137c478bd9Sstevel@tonic-gate 		/* no context template, compute context */
20147c478bd9Sstevel@tonic-gate 		if (keylen_in_bytes > MD5_HMAC_BLOCK_SIZE) {
20157c478bd9Sstevel@tonic-gate 			/*
20167c478bd9Sstevel@tonic-gate 			 * Hash the passed-in key to get a smaller key.
20177c478bd9Sstevel@tonic-gate 			 * The inner context is used since it hasn't been
20187c478bd9Sstevel@tonic-gate 			 * initialized yet.
20197c478bd9Sstevel@tonic-gate 			 */
20207c478bd9Sstevel@tonic-gate 			PROV_MD5_DIGEST_KEY(&md5_hmac_ctx.hc_icontext,
20217c478bd9Sstevel@tonic-gate 			    key->ck_data, keylen_in_bytes, digest);
20227c478bd9Sstevel@tonic-gate 			md5_mac_init_ctx(&md5_hmac_ctx, digest,
20237c478bd9Sstevel@tonic-gate 			    MD5_DIGEST_LENGTH);
20247c478bd9Sstevel@tonic-gate 		} else {
20257c478bd9Sstevel@tonic-gate 			md5_mac_init_ctx(&md5_hmac_ctx, key->ck_data,
20267c478bd9Sstevel@tonic-gate 			    keylen_in_bytes);
20277c478bd9Sstevel@tonic-gate 		}
20287c478bd9Sstevel@tonic-gate 	}
20297c478bd9Sstevel@tonic-gate 
20307c478bd9Sstevel@tonic-gate 	/*
20317c478bd9Sstevel@tonic-gate 	 * Get the mechanism parameters, if applicable.
20327c478bd9Sstevel@tonic-gate 	 */
20337c478bd9Sstevel@tonic-gate 	if (mechanism->cm_type == MD5_HMAC_GEN_MECH_INFO_TYPE) {
20347c478bd9Sstevel@tonic-gate 		if (mechanism->cm_param == NULL ||
20357c478bd9Sstevel@tonic-gate 		    mechanism->cm_param_len != sizeof (ulong_t)) {
20367c478bd9Sstevel@tonic-gate 			ret = CRYPTO_MECHANISM_PARAM_INVALID;
20377c478bd9Sstevel@tonic-gate 			goto bail;
20387c478bd9Sstevel@tonic-gate 		}
20397c478bd9Sstevel@tonic-gate 		PROV_MD5_GET_DIGEST_LEN(mechanism, digest_len);
20407c478bd9Sstevel@tonic-gate 		if (digest_len > MD5_DIGEST_LENGTH) {
20417c478bd9Sstevel@tonic-gate 			ret = CRYPTO_MECHANISM_PARAM_INVALID;
20427c478bd9Sstevel@tonic-gate 			goto bail;
20437c478bd9Sstevel@tonic-gate 		}
20447c478bd9Sstevel@tonic-gate 	}
20457c478bd9Sstevel@tonic-gate 
20467c478bd9Sstevel@tonic-gate 	if (mac->cd_length != digest_len) {
20477c478bd9Sstevel@tonic-gate 		ret = CRYPTO_INVALID_MAC;
20487c478bd9Sstevel@tonic-gate 		goto bail;
20497c478bd9Sstevel@tonic-gate 	}
20507c478bd9Sstevel@tonic-gate 
20517c478bd9Sstevel@tonic-gate 	/* do an MD5 update of the inner context using the specified data */
20527c478bd9Sstevel@tonic-gate 	MD5_MAC_UPDATE(data, md5_hmac_ctx, ret);
20537c478bd9Sstevel@tonic-gate 	if (ret != CRYPTO_SUCCESS)
20547c478bd9Sstevel@tonic-gate 		/* the update failed, free context and bail */
20557c478bd9Sstevel@tonic-gate 		goto bail;
20567c478bd9Sstevel@tonic-gate 
20577c478bd9Sstevel@tonic-gate 	/* do an MD5 final on the inner context */
20587c478bd9Sstevel@tonic-gate 	MD5Final(digest, &md5_hmac_ctx.hc_icontext);
20597c478bd9Sstevel@tonic-gate 
20607c478bd9Sstevel@tonic-gate 	/*
20617c478bd9Sstevel@tonic-gate 	 * Do an MD5 update on the outer context, feeding the inner
20627c478bd9Sstevel@tonic-gate 	 * digest as data.
20637c478bd9Sstevel@tonic-gate 	 */
20647c478bd9Sstevel@tonic-gate 	MD5Update(&md5_hmac_ctx.hc_ocontext, digest, MD5_DIGEST_LENGTH);
20657c478bd9Sstevel@tonic-gate 
20667c478bd9Sstevel@tonic-gate 	/*
20677c478bd9Sstevel@tonic-gate 	 * Do an MD5 final on the outer context, storing the computed
20687c478bd9Sstevel@tonic-gate 	 * digest in the local digest buffer.
20697c478bd9Sstevel@tonic-gate 	 */
20707c478bd9Sstevel@tonic-gate 	MD5Final(digest, &md5_hmac_ctx.hc_ocontext);
20717c478bd9Sstevel@tonic-gate 
20727c478bd9Sstevel@tonic-gate 	/*
20737c478bd9Sstevel@tonic-gate 	 * Compare the computed digest against the expected digest passed
20747c478bd9Sstevel@tonic-gate 	 * as argument.
20757c478bd9Sstevel@tonic-gate 	 */
20767c478bd9Sstevel@tonic-gate 	switch (mac->cd_format) {
20777c478bd9Sstevel@tonic-gate 
20787c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
20797c478bd9Sstevel@tonic-gate 		if (bcmp(digest, (unsigned char *)mac->cd_raw.iov_base +
20807c478bd9Sstevel@tonic-gate 		    mac->cd_offset, digest_len) != 0)
20817c478bd9Sstevel@tonic-gate 			ret = CRYPTO_INVALID_MAC;
20827c478bd9Sstevel@tonic-gate 		break;
20837c478bd9Sstevel@tonic-gate 
20847c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO: {
20857c478bd9Sstevel@tonic-gate 		off_t offset = mac->cd_offset;
20867c478bd9Sstevel@tonic-gate 		uint_t vec_idx;
20877c478bd9Sstevel@tonic-gate 		off_t scratch_offset = 0;
20887c478bd9Sstevel@tonic-gate 		size_t length = digest_len;
20897c478bd9Sstevel@tonic-gate 		size_t cur_len;
20907c478bd9Sstevel@tonic-gate 
20917c478bd9Sstevel@tonic-gate 		/* we support only kernel buffer */
20927c478bd9Sstevel@tonic-gate 		if (mac->cd_uio->uio_segflg != UIO_SYSSPACE)
20937c478bd9Sstevel@tonic-gate 			return (CRYPTO_ARGUMENTS_BAD);
20947c478bd9Sstevel@tonic-gate 
20957c478bd9Sstevel@tonic-gate 		/* jump to the first iovec containing the expected digest */
20967c478bd9Sstevel@tonic-gate 		for (vec_idx = 0;
20977c478bd9Sstevel@tonic-gate 		    offset >= mac->cd_uio->uio_iov[vec_idx].iov_len &&
20987c478bd9Sstevel@tonic-gate 		    vec_idx < mac->cd_uio->uio_iovcnt;
20997c478bd9Sstevel@tonic-gate 		    offset -= mac->cd_uio->uio_iov[vec_idx++].iov_len);
21007c478bd9Sstevel@tonic-gate 		if (vec_idx == mac->cd_uio->uio_iovcnt) {
21017c478bd9Sstevel@tonic-gate 			/*
21027c478bd9Sstevel@tonic-gate 			 * The caller specified an offset that is
21037c478bd9Sstevel@tonic-gate 			 * larger than the total size of the buffers
21047c478bd9Sstevel@tonic-gate 			 * it provided.
21057c478bd9Sstevel@tonic-gate 			 */
21067c478bd9Sstevel@tonic-gate 			ret = CRYPTO_DATA_LEN_RANGE;
21077c478bd9Sstevel@tonic-gate 			break;
21087c478bd9Sstevel@tonic-gate 		}
21097c478bd9Sstevel@tonic-gate 
21107c478bd9Sstevel@tonic-gate 		/* do the comparison of computed digest vs specified one */
21117c478bd9Sstevel@tonic-gate 		while (vec_idx < mac->cd_uio->uio_iovcnt && length > 0) {
21127c478bd9Sstevel@tonic-gate 			cur_len = MIN(mac->cd_uio->uio_iov[vec_idx].iov_len -
21137c478bd9Sstevel@tonic-gate 			    offset, length);
21147c478bd9Sstevel@tonic-gate 
21157c478bd9Sstevel@tonic-gate 			if (bcmp(digest + scratch_offset,
21167c478bd9Sstevel@tonic-gate 			    mac->cd_uio->uio_iov[vec_idx].iov_base + offset,
21177c478bd9Sstevel@tonic-gate 			    cur_len) != 0) {
21187c478bd9Sstevel@tonic-gate 				ret = CRYPTO_INVALID_MAC;
21197c478bd9Sstevel@tonic-gate 				break;
21207c478bd9Sstevel@tonic-gate 			}
21217c478bd9Sstevel@tonic-gate 
21227c478bd9Sstevel@tonic-gate 			length -= cur_len;
21237c478bd9Sstevel@tonic-gate 			vec_idx++;
21247c478bd9Sstevel@tonic-gate 			scratch_offset += cur_len;
21257c478bd9Sstevel@tonic-gate 			offset = 0;
21267c478bd9Sstevel@tonic-gate 		}
21277c478bd9Sstevel@tonic-gate 		break;
21287c478bd9Sstevel@tonic-gate 	}
21297c478bd9Sstevel@tonic-gate 
21307c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK: {
21317c478bd9Sstevel@tonic-gate 		off_t offset = mac->cd_offset;
21327c478bd9Sstevel@tonic-gate 		mblk_t *mp;
21337c478bd9Sstevel@tonic-gate 		off_t scratch_offset = 0;
21347c478bd9Sstevel@tonic-gate 		size_t length = digest_len;
21357c478bd9Sstevel@tonic-gate 		size_t cur_len;
21367c478bd9Sstevel@tonic-gate 
21377c478bd9Sstevel@tonic-gate 		/* jump to the first mblk_t containing the expected digest */
21387c478bd9Sstevel@tonic-gate 		for (mp = mac->cd_mp; mp != NULL && offset >= MBLKL(mp);
21397c478bd9Sstevel@tonic-gate 		    offset -= MBLKL(mp), mp = mp->b_cont);
21407c478bd9Sstevel@tonic-gate 		if (mp == NULL) {
21417c478bd9Sstevel@tonic-gate 			/*
21427c478bd9Sstevel@tonic-gate 			 * The caller specified an offset that is larger than
21437c478bd9Sstevel@tonic-gate 			 * the total size of the buffers it provided.
21447c478bd9Sstevel@tonic-gate 			 */
21457c478bd9Sstevel@tonic-gate 			ret = CRYPTO_DATA_LEN_RANGE;
21467c478bd9Sstevel@tonic-gate 			break;
21477c478bd9Sstevel@tonic-gate 		}
21487c478bd9Sstevel@tonic-gate 
21497c478bd9Sstevel@tonic-gate 		while (mp != NULL && length > 0) {
21507c478bd9Sstevel@tonic-gate 			cur_len = MIN(MBLKL(mp) - offset, length);
21517c478bd9Sstevel@tonic-gate 			if (bcmp(digest + scratch_offset,
21527c478bd9Sstevel@tonic-gate 			    mp->b_rptr + offset, cur_len) != 0) {
21537c478bd9Sstevel@tonic-gate 				ret = CRYPTO_INVALID_MAC;
21547c478bd9Sstevel@tonic-gate 				break;
21557c478bd9Sstevel@tonic-gate 			}
21567c478bd9Sstevel@tonic-gate 
21577c478bd9Sstevel@tonic-gate 			length -= cur_len;
21587c478bd9Sstevel@tonic-gate 			mp = mp->b_cont;
21597c478bd9Sstevel@tonic-gate 			scratch_offset += cur_len;
21607c478bd9Sstevel@tonic-gate 			offset = 0;
21617c478bd9Sstevel@tonic-gate 		}
21627c478bd9Sstevel@tonic-gate 		break;
21637c478bd9Sstevel@tonic-gate 	}
21647c478bd9Sstevel@tonic-gate 
21657c478bd9Sstevel@tonic-gate 	default:
21667c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
21677c478bd9Sstevel@tonic-gate 	}
21687c478bd9Sstevel@tonic-gate 
21697c478bd9Sstevel@tonic-gate 	bzero(&md5_hmac_ctx, sizeof (md5_hmac_ctx_t));
21707c478bd9Sstevel@tonic-gate 	return (ret);
21717c478bd9Sstevel@tonic-gate bail:
21727c478bd9Sstevel@tonic-gate 	bzero(&md5_hmac_ctx, sizeof (md5_hmac_ctx_t));
21737c478bd9Sstevel@tonic-gate 	mac->cd_length = 0;
21747c478bd9Sstevel@tonic-gate 	return (ret);
21757c478bd9Sstevel@tonic-gate }
21767c478bd9Sstevel@tonic-gate 
21777c478bd9Sstevel@tonic-gate /*
21787c478bd9Sstevel@tonic-gate  * KCF software provider context management entry points.
21797c478bd9Sstevel@tonic-gate  */
21807c478bd9Sstevel@tonic-gate 
21817c478bd9Sstevel@tonic-gate /* ARGSUSED */
21827c478bd9Sstevel@tonic-gate static int
21837c478bd9Sstevel@tonic-gate md5_create_ctx_template(crypto_provider_handle_t provider,
21847c478bd9Sstevel@tonic-gate     crypto_mechanism_t *mechanism, crypto_key_t *key,
21857c478bd9Sstevel@tonic-gate     crypto_spi_ctx_template_t *ctx_template, size_t *ctx_template_size,
21867c478bd9Sstevel@tonic-gate     crypto_req_handle_t req)
21877c478bd9Sstevel@tonic-gate {
21887c478bd9Sstevel@tonic-gate 	md5_hmac_ctx_t *md5_hmac_ctx_tmpl;
21897c478bd9Sstevel@tonic-gate 	uint_t keylen_in_bytes = CRYPTO_BITS2BYTES(key->ck_length);
21907c478bd9Sstevel@tonic-gate 
21917c478bd9Sstevel@tonic-gate 	if ((mechanism->cm_type != MD5_HMAC_MECH_INFO_TYPE) &&
21927c478bd9Sstevel@tonic-gate 	    (mechanism->cm_type != MD5_HMAC_GEN_MECH_INFO_TYPE))
21937c478bd9Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
21947c478bd9Sstevel@tonic-gate 
21957c478bd9Sstevel@tonic-gate 	/* Add support for key by attributes (RFE 4706552) */
21967c478bd9Sstevel@tonic-gate 	if (key->ck_format != CRYPTO_KEY_RAW)
21977c478bd9Sstevel@tonic-gate 		return (CRYPTO_ARGUMENTS_BAD);
21987c478bd9Sstevel@tonic-gate 
21997c478bd9Sstevel@tonic-gate 	/*
22007c478bd9Sstevel@tonic-gate 	 * Allocate and initialize MD5 context.
22017c478bd9Sstevel@tonic-gate 	 */
22027c478bd9Sstevel@tonic-gate 	md5_hmac_ctx_tmpl = kmem_alloc(sizeof (md5_hmac_ctx_t),
22037c478bd9Sstevel@tonic-gate 	    crypto_kmflag(req));
22047c478bd9Sstevel@tonic-gate 	if (md5_hmac_ctx_tmpl == NULL)
22057c478bd9Sstevel@tonic-gate 		return (CRYPTO_HOST_MEMORY);
22067c478bd9Sstevel@tonic-gate 
22077c478bd9Sstevel@tonic-gate 	if (keylen_in_bytes > MD5_HMAC_BLOCK_SIZE) {
22087c478bd9Sstevel@tonic-gate 		uchar_t digested_key[MD5_DIGEST_LENGTH];
22097c478bd9Sstevel@tonic-gate 
22107c478bd9Sstevel@tonic-gate 		/*
22117c478bd9Sstevel@tonic-gate 		 * Hash the passed-in key to get a smaller key.
22127c478bd9Sstevel@tonic-gate 		 * The inner context is used since it hasn't been
22137c478bd9Sstevel@tonic-gate 		 * initialized yet.
22147c478bd9Sstevel@tonic-gate 		 */
22157c478bd9Sstevel@tonic-gate 		PROV_MD5_DIGEST_KEY(&md5_hmac_ctx_tmpl->hc_icontext,
22167c478bd9Sstevel@tonic-gate 		    key->ck_data, keylen_in_bytes, digested_key);
22177c478bd9Sstevel@tonic-gate 		md5_mac_init_ctx(md5_hmac_ctx_tmpl, digested_key,
22187c478bd9Sstevel@tonic-gate 		    MD5_DIGEST_LENGTH);
22197c478bd9Sstevel@tonic-gate 	} else {
22207c478bd9Sstevel@tonic-gate 		md5_mac_init_ctx(md5_hmac_ctx_tmpl, key->ck_data,
22217c478bd9Sstevel@tonic-gate 		    keylen_in_bytes);
22227c478bd9Sstevel@tonic-gate 	}
22237c478bd9Sstevel@tonic-gate 
22247c478bd9Sstevel@tonic-gate 	md5_hmac_ctx_tmpl->hc_mech_type = mechanism->cm_type;
22257c478bd9Sstevel@tonic-gate 	*ctx_template = (crypto_spi_ctx_template_t)md5_hmac_ctx_tmpl;
22267c478bd9Sstevel@tonic-gate 	*ctx_template_size = sizeof (md5_hmac_ctx_t);
22277c478bd9Sstevel@tonic-gate 
22287c478bd9Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
22297c478bd9Sstevel@tonic-gate }
22307c478bd9Sstevel@tonic-gate 
22317c478bd9Sstevel@tonic-gate static int
22327c478bd9Sstevel@tonic-gate md5_free_context(crypto_ctx_t *ctx)
22337c478bd9Sstevel@tonic-gate {
22347c478bd9Sstevel@tonic-gate 	uint_t ctx_len;
22357c478bd9Sstevel@tonic-gate 	md5_mech_type_t mech_type;
22367c478bd9Sstevel@tonic-gate 
22377c478bd9Sstevel@tonic-gate 	if (ctx->cc_provider_private == NULL)
22387c478bd9Sstevel@tonic-gate 		return (CRYPTO_SUCCESS);
22397c478bd9Sstevel@tonic-gate 
22407c478bd9Sstevel@tonic-gate 	/*
22417c478bd9Sstevel@tonic-gate 	 * We have to free either MD5 or MD5-HMAC contexts, which
22427c478bd9Sstevel@tonic-gate 	 * have different lengths.
22437c478bd9Sstevel@tonic-gate 	 */
22447c478bd9Sstevel@tonic-gate 
22457c478bd9Sstevel@tonic-gate 	mech_type = PROV_MD5_CTX(ctx)->mc_mech_type;
22467c478bd9Sstevel@tonic-gate 	if (mech_type == MD5_MECH_INFO_TYPE)
22477c478bd9Sstevel@tonic-gate 		ctx_len = sizeof (md5_ctx_t);
22487c478bd9Sstevel@tonic-gate 	else {
22497c478bd9Sstevel@tonic-gate 		ASSERT(mech_type == MD5_HMAC_MECH_INFO_TYPE ||
22507c478bd9Sstevel@tonic-gate 		    mech_type == MD5_HMAC_GEN_MECH_INFO_TYPE);
22517c478bd9Sstevel@tonic-gate 		ctx_len = sizeof (md5_hmac_ctx_t);
22527c478bd9Sstevel@tonic-gate 	}
22537c478bd9Sstevel@tonic-gate 
22547c478bd9Sstevel@tonic-gate 	bzero(ctx->cc_provider_private, ctx_len);
22557c478bd9Sstevel@tonic-gate 	kmem_free(ctx->cc_provider_private, ctx_len);
22567c478bd9Sstevel@tonic-gate 	ctx->cc_provider_private = NULL;
22577c478bd9Sstevel@tonic-gate 
22587c478bd9Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
22597c478bd9Sstevel@tonic-gate }
22607c478bd9Sstevel@tonic-gate 
22617c478bd9Sstevel@tonic-gate #endif	/* _KERNEL && !_BOOT */
2262