xref: /titanic_51/usr/src/common/crypto/md5/md5.c (revision 673007c6bdf2eee5b45916949d1b2e7ed00efbe3)
17c478bd9Sstevel@tonic-gate /*
2*673007c6Sdarrenm  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate  * Cleaned-up and optimized version of MD5, based on the reference
87c478bd9Sstevel@tonic-gate  * implementation provided in RFC 1321.  See RSA Copyright information
97c478bd9Sstevel@tonic-gate  * below.
107c478bd9Sstevel@tonic-gate  *
117c478bd9Sstevel@tonic-gate  * NOTE:  All compiler data was gathered with SC4.2, and verified with SC5.x,
127c478bd9Sstevel@tonic-gate  *	  as used to build Solaris 2.7.  Hopefully the compiler behavior won't
137c478bd9Sstevel@tonic-gate  *	  change for the worse in subsequent Solaris builds.
147c478bd9Sstevel@tonic-gate  */
157c478bd9Sstevel@tonic-gate 
167c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate /*
197c478bd9Sstevel@tonic-gate  * MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
247c478bd9Sstevel@tonic-gate  * rights reserved.
257c478bd9Sstevel@tonic-gate  *
267c478bd9Sstevel@tonic-gate  * License to copy and use this software is granted provided that it
277c478bd9Sstevel@tonic-gate  * is identified as the "RSA Data Security, Inc. MD5 Message-Digest
287c478bd9Sstevel@tonic-gate  * Algorithm" in all material mentioning or referencing this software
297c478bd9Sstevel@tonic-gate  * or this function.
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * License is also granted to make and use derivative works provided
327c478bd9Sstevel@tonic-gate  * that such works are identified as "derived from the RSA Data
337c478bd9Sstevel@tonic-gate  * Security, Inc. MD5 Message-Digest Algorithm" in all material
347c478bd9Sstevel@tonic-gate  * mentioning or referencing the derived work.
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * RSA Data Security, Inc. makes no representations concerning either
377c478bd9Sstevel@tonic-gate  * the merchantability of this software or the suitability of this
387c478bd9Sstevel@tonic-gate  * software for any particular purpose. It is provided "as is"
397c478bd9Sstevel@tonic-gate  * without express or implied warranty of any kind.
407c478bd9Sstevel@tonic-gate  *
417c478bd9Sstevel@tonic-gate  * These notices must be retained in any copies of any part of this
427c478bd9Sstevel@tonic-gate  * documentation and/or software.
437c478bd9Sstevel@tonic-gate  */
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include <sys/types.h>
467c478bd9Sstevel@tonic-gate #include <sys/md5.h>
477c478bd9Sstevel@tonic-gate #include <sys/md5_consts.h>	/* MD5_CONST() optimization */
48afd1ac7bSwesolows #include "md5_byteswap.h"
497c478bd9Sstevel@tonic-gate #if	!defined(_KERNEL) || defined(_BOOT)
507c478bd9Sstevel@tonic-gate #include <strings.h>
517c478bd9Sstevel@tonic-gate #endif /* !_KERNEL || _BOOT */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #if	defined(_KERNEL) && !defined(_BOOT)
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /*
567c478bd9Sstevel@tonic-gate  * In kernel module, the md5 module is created with two modlinkages:
577c478bd9Sstevel@tonic-gate  * - a modlmisc that allows consumers to directly call the entry points
587c478bd9Sstevel@tonic-gate  *   MD5Init, MD5Update, and MD5Final.
597c478bd9Sstevel@tonic-gate  * - a modlcrypto that allows the module to register with the Kernel
607c478bd9Sstevel@tonic-gate  *   Cryptographic Framework (KCF) as a software provider for the MD5
617c478bd9Sstevel@tonic-gate  *   mechanisms.
627c478bd9Sstevel@tonic-gate  */
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #include <sys/systm.h>
657c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
667c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
677c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
687c478bd9Sstevel@tonic-gate #include <sys/crypto/common.h>
697c478bd9Sstevel@tonic-gate #include <sys/crypto/spi.h>
707c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
717c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
727c478bd9Sstevel@tonic-gate #include <sys/note.h>
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate extern struct mod_ops mod_miscops;
757c478bd9Sstevel@tonic-gate extern struct mod_ops mod_cryptoops;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /*
787c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
797c478bd9Sstevel@tonic-gate  */
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate static struct modlmisc modlmisc = {
827c478bd9Sstevel@tonic-gate 	&mod_miscops,
837c478bd9Sstevel@tonic-gate 	"MD5 Message-Digest Algorithm"
847c478bd9Sstevel@tonic-gate };
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate static struct modlcrypto modlcrypto = {
877c478bd9Sstevel@tonic-gate 	&mod_cryptoops,
88afd1ac7bSwesolows 	"MD5 Kernel SW Provider 1.23"
897c478bd9Sstevel@tonic-gate };
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
927c478bd9Sstevel@tonic-gate 	MODREV_1,
937c478bd9Sstevel@tonic-gate 	(void *)&modlmisc,
947c478bd9Sstevel@tonic-gate 	(void *)&modlcrypto,
957c478bd9Sstevel@tonic-gate 	NULL
967c478bd9Sstevel@tonic-gate };
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate  * CSPI information (entry points, provider info, etc.)
1007c478bd9Sstevel@tonic-gate  */
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate typedef enum md5_mech_type {
1037c478bd9Sstevel@tonic-gate 	MD5_MECH_INFO_TYPE,		/* SUN_CKM_MD5 */
1047c478bd9Sstevel@tonic-gate 	MD5_HMAC_MECH_INFO_TYPE,	/* SUN_CKM_MD5_HMAC */
1057c478bd9Sstevel@tonic-gate 	MD5_HMAC_GEN_MECH_INFO_TYPE	/* SUN_CKM_MD5_HMAC_GENERAL */
1067c478bd9Sstevel@tonic-gate } md5_mech_type_t;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate #define	MD5_DIGEST_LENGTH	16	/* MD5 digest length in bytes */
1097c478bd9Sstevel@tonic-gate #define	MD5_HMAC_BLOCK_SIZE	64	/* MD5 block size */
1107c478bd9Sstevel@tonic-gate #define	MD5_HMAC_MIN_KEY_LEN	8	/* MD5-HMAC min key length in bits */
1117c478bd9Sstevel@tonic-gate #define	MD5_HMAC_MAX_KEY_LEN	INT_MAX	/* MD5-HMAC max key length in bits */
1127c478bd9Sstevel@tonic-gate #define	MD5_HMAC_INTS_PER_BLOCK	(MD5_HMAC_BLOCK_SIZE/sizeof (uint32_t))
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /*
1157c478bd9Sstevel@tonic-gate  * Context for MD5 mechanism.
1167c478bd9Sstevel@tonic-gate  */
1177c478bd9Sstevel@tonic-gate typedef struct md5_ctx {
1187c478bd9Sstevel@tonic-gate 	md5_mech_type_t		mc_mech_type;	/* type of context */
1197c478bd9Sstevel@tonic-gate 	MD5_CTX			mc_md5_ctx;	/* MD5 context */
1207c478bd9Sstevel@tonic-gate } md5_ctx_t;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate /*
1237c478bd9Sstevel@tonic-gate  * Context for MD5-HMAC and MD5-HMAC-GENERAL mechanisms.
1247c478bd9Sstevel@tonic-gate  */
1257c478bd9Sstevel@tonic-gate typedef struct md5_hmac_ctx {
1267c478bd9Sstevel@tonic-gate 	md5_mech_type_t		hc_mech_type;	/* type of context */
1277c478bd9Sstevel@tonic-gate 	uint32_t		hc_digest_len;	/* digest len in bytes */
1287c478bd9Sstevel@tonic-gate 	MD5_CTX			hc_icontext;	/* inner MD5 context */
1297c478bd9Sstevel@tonic-gate 	MD5_CTX			hc_ocontext;	/* outer MD5 context */
1307c478bd9Sstevel@tonic-gate } md5_hmac_ctx_t;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate /*
1337c478bd9Sstevel@tonic-gate  * Macros to access the MD5 or MD5-HMAC contexts from a context passed
1347c478bd9Sstevel@tonic-gate  * by KCF to one of the entry points.
1357c478bd9Sstevel@tonic-gate  */
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate #define	PROV_MD5_CTX(ctx)	((md5_ctx_t *)(ctx)->cc_provider_private)
1387c478bd9Sstevel@tonic-gate #define	PROV_MD5_HMAC_CTX(ctx)	((md5_hmac_ctx_t *)(ctx)->cc_provider_private)
1397c478bd9Sstevel@tonic-gate /* to extract the digest length passed as mechanism parameter */
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate #define	PROV_MD5_GET_DIGEST_LEN(m, len) {				\
1427c478bd9Sstevel@tonic-gate 	if (IS_P2ALIGNED((m)->cm_param, sizeof (ulong_t)))		\
1437c478bd9Sstevel@tonic-gate 		(len) = (uint32_t)*((ulong_t *)mechanism->cm_param);	\
1447c478bd9Sstevel@tonic-gate 	else {								\
1457c478bd9Sstevel@tonic-gate 		ulong_t tmp_ulong;					\
1467c478bd9Sstevel@tonic-gate 		bcopy((m)->cm_param, &tmp_ulong, sizeof (ulong_t));	\
1477c478bd9Sstevel@tonic-gate 		(len) = (uint32_t)tmp_ulong;				\
1487c478bd9Sstevel@tonic-gate 	}								\
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate #define	PROV_MD5_DIGEST_KEY(ctx, key, len, digest) {	\
1527c478bd9Sstevel@tonic-gate 	MD5Init(ctx);					\
1537c478bd9Sstevel@tonic-gate 	MD5Update(ctx, key, len);			\
1547c478bd9Sstevel@tonic-gate 	MD5Final(digest, ctx);				\
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate /*
1587c478bd9Sstevel@tonic-gate  * Mechanism info structure passed to KCF during registration.
1597c478bd9Sstevel@tonic-gate  */
1607c478bd9Sstevel@tonic-gate static crypto_mech_info_t md5_mech_info_tab[] = {
1617c478bd9Sstevel@tonic-gate 	/* MD5 */
1627c478bd9Sstevel@tonic-gate 	{SUN_CKM_MD5, MD5_MECH_INFO_TYPE,
1637c478bd9Sstevel@tonic-gate 	    CRYPTO_FG_DIGEST | CRYPTO_FG_DIGEST_ATOMIC,
1647c478bd9Sstevel@tonic-gate 	    0, 0, CRYPTO_KEYSIZE_UNIT_IN_BITS},
1657c478bd9Sstevel@tonic-gate 	/* MD5-HMAC */
1667c478bd9Sstevel@tonic-gate 	{SUN_CKM_MD5_HMAC, MD5_HMAC_MECH_INFO_TYPE,
1677c478bd9Sstevel@tonic-gate 	    CRYPTO_FG_MAC | CRYPTO_FG_MAC_ATOMIC,
1687c478bd9Sstevel@tonic-gate 	    MD5_HMAC_MIN_KEY_LEN, MD5_HMAC_MAX_KEY_LEN,
1697c478bd9Sstevel@tonic-gate 	    CRYPTO_KEYSIZE_UNIT_IN_BITS},
1707c478bd9Sstevel@tonic-gate 	/* MD5-HMAC GENERAL */
1717c478bd9Sstevel@tonic-gate 	{SUN_CKM_MD5_HMAC_GENERAL, MD5_HMAC_GEN_MECH_INFO_TYPE,
1727c478bd9Sstevel@tonic-gate 	    CRYPTO_FG_MAC | CRYPTO_FG_MAC_ATOMIC,
1737c478bd9Sstevel@tonic-gate 	    MD5_HMAC_MIN_KEY_LEN, MD5_HMAC_MAX_KEY_LEN,
1747c478bd9Sstevel@tonic-gate 	    CRYPTO_KEYSIZE_UNIT_IN_BITS}
1757c478bd9Sstevel@tonic-gate };
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate static void md5_provider_status(crypto_provider_handle_t, uint_t *);
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate static crypto_control_ops_t md5_control_ops = {
1807c478bd9Sstevel@tonic-gate 	md5_provider_status
1817c478bd9Sstevel@tonic-gate };
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate static int md5_digest_init(crypto_ctx_t *, crypto_mechanism_t *,
1847c478bd9Sstevel@tonic-gate     crypto_req_handle_t);
1857c478bd9Sstevel@tonic-gate static int md5_digest(crypto_ctx_t *, crypto_data_t *, crypto_data_t *,
1867c478bd9Sstevel@tonic-gate     crypto_req_handle_t);
1877c478bd9Sstevel@tonic-gate static int md5_digest_update(crypto_ctx_t *, crypto_data_t *,
1887c478bd9Sstevel@tonic-gate     crypto_req_handle_t);
1897c478bd9Sstevel@tonic-gate static int md5_digest_final(crypto_ctx_t *, crypto_data_t *,
1907c478bd9Sstevel@tonic-gate     crypto_req_handle_t);
1917c478bd9Sstevel@tonic-gate static int md5_digest_atomic(crypto_provider_handle_t, crypto_session_id_t,
1927c478bd9Sstevel@tonic-gate     crypto_mechanism_t *, crypto_data_t *, crypto_data_t *,
1937c478bd9Sstevel@tonic-gate     crypto_req_handle_t);
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate static crypto_digest_ops_t md5_digest_ops = {
1967c478bd9Sstevel@tonic-gate 	md5_digest_init,
1977c478bd9Sstevel@tonic-gate 	md5_digest,
1987c478bd9Sstevel@tonic-gate 	md5_digest_update,
1997c478bd9Sstevel@tonic-gate 	NULL,
2007c478bd9Sstevel@tonic-gate 	md5_digest_final,
2017c478bd9Sstevel@tonic-gate 	md5_digest_atomic
2027c478bd9Sstevel@tonic-gate };
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate static int md5_mac_init(crypto_ctx_t *, crypto_mechanism_t *, crypto_key_t *,
2057c478bd9Sstevel@tonic-gate     crypto_spi_ctx_template_t, crypto_req_handle_t);
2067c478bd9Sstevel@tonic-gate static int md5_mac_update(crypto_ctx_t *, crypto_data_t *, crypto_req_handle_t);
2077c478bd9Sstevel@tonic-gate static int md5_mac_final(crypto_ctx_t *, crypto_data_t *, crypto_req_handle_t);
2087c478bd9Sstevel@tonic-gate static int md5_mac_atomic(crypto_provider_handle_t, crypto_session_id_t,
2097c478bd9Sstevel@tonic-gate     crypto_mechanism_t *, crypto_key_t *, crypto_data_t *, crypto_data_t *,
2107c478bd9Sstevel@tonic-gate     crypto_spi_ctx_template_t, crypto_req_handle_t);
2117c478bd9Sstevel@tonic-gate static int md5_mac_verify_atomic(crypto_provider_handle_t, crypto_session_id_t,
2127c478bd9Sstevel@tonic-gate     crypto_mechanism_t *, crypto_key_t *, crypto_data_t *, crypto_data_t *,
2137c478bd9Sstevel@tonic-gate     crypto_spi_ctx_template_t, crypto_req_handle_t);
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate static crypto_mac_ops_t md5_mac_ops = {
2167c478bd9Sstevel@tonic-gate 	md5_mac_init,
2177c478bd9Sstevel@tonic-gate 	NULL,
2187c478bd9Sstevel@tonic-gate 	md5_mac_update,
2197c478bd9Sstevel@tonic-gate 	md5_mac_final,
2207c478bd9Sstevel@tonic-gate 	md5_mac_atomic,
2217c478bd9Sstevel@tonic-gate 	md5_mac_verify_atomic
2227c478bd9Sstevel@tonic-gate };
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate static int md5_create_ctx_template(crypto_provider_handle_t,
2257c478bd9Sstevel@tonic-gate     crypto_mechanism_t *, crypto_key_t *, crypto_spi_ctx_template_t *,
2267c478bd9Sstevel@tonic-gate     size_t *, crypto_req_handle_t);
2277c478bd9Sstevel@tonic-gate static int md5_free_context(crypto_ctx_t *);
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate static crypto_ctx_ops_t md5_ctx_ops = {
2307c478bd9Sstevel@tonic-gate 	md5_create_ctx_template,
2317c478bd9Sstevel@tonic-gate 	md5_free_context
2327c478bd9Sstevel@tonic-gate };
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate static crypto_ops_t md5_crypto_ops = {
2357c478bd9Sstevel@tonic-gate 	&md5_control_ops,
2367c478bd9Sstevel@tonic-gate 	&md5_digest_ops,
2377c478bd9Sstevel@tonic-gate 	NULL,
2387c478bd9Sstevel@tonic-gate 	&md5_mac_ops,
2397c478bd9Sstevel@tonic-gate 	NULL,
2407c478bd9Sstevel@tonic-gate 	NULL,
2417c478bd9Sstevel@tonic-gate 	NULL,
2427c478bd9Sstevel@tonic-gate 	NULL,
2437c478bd9Sstevel@tonic-gate 	NULL,
2447c478bd9Sstevel@tonic-gate 	NULL,
2457c478bd9Sstevel@tonic-gate 	NULL,
2467c478bd9Sstevel@tonic-gate 	NULL,
2477c478bd9Sstevel@tonic-gate 	NULL,
2487c478bd9Sstevel@tonic-gate 	&md5_ctx_ops
2497c478bd9Sstevel@tonic-gate };
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate static crypto_provider_info_t md5_prov_info = {
2527c478bd9Sstevel@tonic-gate 	CRYPTO_SPI_VERSION_1,
2537c478bd9Sstevel@tonic-gate 	"MD5 Software Provider",
2547c478bd9Sstevel@tonic-gate 	CRYPTO_SW_PROVIDER,
2557c478bd9Sstevel@tonic-gate 	{&modlinkage},
2567c478bd9Sstevel@tonic-gate 	NULL,
2577c478bd9Sstevel@tonic-gate 	&md5_crypto_ops,
2587c478bd9Sstevel@tonic-gate 	sizeof (md5_mech_info_tab)/sizeof (crypto_mech_info_t),
2597c478bd9Sstevel@tonic-gate 	md5_mech_info_tab
2607c478bd9Sstevel@tonic-gate };
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate static crypto_kcf_provider_handle_t md5_prov_handle = NULL;
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate int
2657c478bd9Sstevel@tonic-gate _init(void)
2667c478bd9Sstevel@tonic-gate {
2677c478bd9Sstevel@tonic-gate 	int ret;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	if ((ret = mod_install(&modlinkage)) != 0)
2707c478bd9Sstevel@tonic-gate 		return (ret);
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	/*
2737c478bd9Sstevel@tonic-gate 	 * Register with KCF. If the registration fails, log an
2747c478bd9Sstevel@tonic-gate 	 * error but do not uninstall the module, since the functionality
2757c478bd9Sstevel@tonic-gate 	 * provided by misc/md5 should still be available.
2767c478bd9Sstevel@tonic-gate 	 */
2777c478bd9Sstevel@tonic-gate 	if ((ret = crypto_register_provider(&md5_prov_info,
2787c478bd9Sstevel@tonic-gate 	    &md5_prov_handle)) != CRYPTO_SUCCESS)
2797c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "md5 _init: "
2807c478bd9Sstevel@tonic-gate 		    "crypto_register_provider() failed (0x%x)", ret);
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	return (0);
2837c478bd9Sstevel@tonic-gate }
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate int
2867c478bd9Sstevel@tonic-gate _fini(void)
2877c478bd9Sstevel@tonic-gate {
2887c478bd9Sstevel@tonic-gate 	int ret;
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	/*
2917c478bd9Sstevel@tonic-gate 	 * Unregister from KCF if previous registration succeeded.
2927c478bd9Sstevel@tonic-gate 	 */
2937c478bd9Sstevel@tonic-gate 	if (md5_prov_handle != NULL) {
2947c478bd9Sstevel@tonic-gate 		if ((ret = crypto_unregister_provider(md5_prov_handle)) !=
2957c478bd9Sstevel@tonic-gate 		    CRYPTO_SUCCESS) {
2967c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "md5 _fini: "
2977c478bd9Sstevel@tonic-gate 			    "crypto_unregister_provider() failed (0x%x)", ret);
2987c478bd9Sstevel@tonic-gate 			return (EBUSY);
2997c478bd9Sstevel@tonic-gate 		}
3007c478bd9Sstevel@tonic-gate 		md5_prov_handle = NULL;
3017c478bd9Sstevel@tonic-gate 	}
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate int
3077c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
3087c478bd9Sstevel@tonic-gate {
3097c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate #endif	/* _KERNEL && !_BOOT */
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate static void Encode(uint8_t *, uint32_t *, size_t);
3147c478bd9Sstevel@tonic-gate static void MD5Transform(uint32_t, uint32_t, uint32_t, uint32_t, MD5_CTX *,
3157c478bd9Sstevel@tonic-gate     const uint8_t [64]);
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate static uint8_t PADDING[64] = { 0x80, /* all zeros */ };
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate /*
3207c478bd9Sstevel@tonic-gate  * F, G, H and I are the basic MD5 functions.
3217c478bd9Sstevel@tonic-gate  */
3227c478bd9Sstevel@tonic-gate #define	F(b, c, d)	(((b) & (c)) | ((~b) & (d)))
3237c478bd9Sstevel@tonic-gate #define	G(b, c, d)	(((b) & (d)) | ((c) & (~d)))
3247c478bd9Sstevel@tonic-gate #define	H(b, c, d)	((b) ^ (c) ^ (d))
3257c478bd9Sstevel@tonic-gate #define	I(b, c, d)	((c) ^ ((b) | (~d)))
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate /*
3287c478bd9Sstevel@tonic-gate  * ROTATE_LEFT rotates x left n bits.
3297c478bd9Sstevel@tonic-gate  */
3307c478bd9Sstevel@tonic-gate #define	ROTATE_LEFT(x, n)	\
3317c478bd9Sstevel@tonic-gate 	(((x) << (n)) | ((x) >> ((sizeof (x) << 3) - (n))))
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate /*
3347c478bd9Sstevel@tonic-gate  * FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
3357c478bd9Sstevel@tonic-gate  * Rotation is separate from addition to prevent recomputation.
3367c478bd9Sstevel@tonic-gate  */
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate #define	FF(a, b, c, d, x, s, ac) { \
339554ff184Skais 	(a) += F((b), (c), (d)) + (x) + ((unsigned long long)(ac)); \
3407c478bd9Sstevel@tonic-gate 	(a) = ROTATE_LEFT((a), (s)); \
3417c478bd9Sstevel@tonic-gate 	(a) += (b); \
3427c478bd9Sstevel@tonic-gate 	}
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate #define	GG(a, b, c, d, x, s, ac) { \
345554ff184Skais 	(a) += G((b), (c), (d)) + (x) + ((unsigned long long)(ac)); \
3467c478bd9Sstevel@tonic-gate 	(a) = ROTATE_LEFT((a), (s)); \
3477c478bd9Sstevel@tonic-gate 	(a) += (b); \
3487c478bd9Sstevel@tonic-gate 	}
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate #define	HH(a, b, c, d, x, s, ac) { \
351554ff184Skais 	(a) += H((b), (c), (d)) + (x) + ((unsigned long long)(ac)); \
3527c478bd9Sstevel@tonic-gate 	(a) = ROTATE_LEFT((a), (s)); \
3537c478bd9Sstevel@tonic-gate 	(a) += (b); \
3547c478bd9Sstevel@tonic-gate 	}
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate #define	II(a, b, c, d, x, s, ac) { \
357554ff184Skais 	(a) += I((b), (c), (d)) + (x) + ((unsigned long long)(ac)); \
3587c478bd9Sstevel@tonic-gate 	(a) = ROTATE_LEFT((a), (s)); \
3597c478bd9Sstevel@tonic-gate 	(a) += (b); \
3607c478bd9Sstevel@tonic-gate 	}
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate /*
3637c478bd9Sstevel@tonic-gate  * Loading 32-bit constants on a RISC is expensive since it involves both a
3647c478bd9Sstevel@tonic-gate  * `sethi' and an `or'.  thus, we instead have the compiler generate `ld's to
3657c478bd9Sstevel@tonic-gate  * load the constants from an array called `md5_consts'.  however, on intel
3667c478bd9Sstevel@tonic-gate  * (and other CISC processors), it is cheaper to load the constant
3677c478bd9Sstevel@tonic-gate  * directly.  thus, the c code in MD5Transform() uses the macro MD5_CONST()
3687c478bd9Sstevel@tonic-gate  * which either expands to a constant or an array reference, depending on the
3697c478bd9Sstevel@tonic-gate  * architecture the code is being compiled for.
3707c478bd9Sstevel@tonic-gate  *
3717c478bd9Sstevel@tonic-gate  * Right now, i386 and amd64 are the CISC exceptions.
3727c478bd9Sstevel@tonic-gate  * If we get another CISC ISA, we'll have to change the ifdef.
3737c478bd9Sstevel@tonic-gate  */
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate #define	MD5_CONST(x)		(MD5_CONST_ ## x)
378554ff184Skais #define	MD5_CONST_e(x)		MD5_CONST(x)
379554ff184Skais #define	MD5_CONST_o(x)		MD5_CONST(x)
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate #else
3827c478bd9Sstevel@tonic-gate /*
3837c478bd9Sstevel@tonic-gate  * sparc/RISC optimization:
3847c478bd9Sstevel@tonic-gate  *
3857c478bd9Sstevel@tonic-gate  * while it is somewhat counter-intuitive, on sparc (and presumably other RISC
3867c478bd9Sstevel@tonic-gate  * machines), it is more efficient to place all the constants used in this
3877c478bd9Sstevel@tonic-gate  * function in an array and load the values out of the array than to manually
3887c478bd9Sstevel@tonic-gate  * load the constants.  this is because setting a register to a 32-bit value
3897c478bd9Sstevel@tonic-gate  * takes two ops in most cases: a `sethi' and an `or', but loading a 32-bit
3907c478bd9Sstevel@tonic-gate  * value from memory only takes one `ld' (or `lduw' on v9).  while this
3917c478bd9Sstevel@tonic-gate  * increases memory usage, the compiler can find enough other things to do
3927c478bd9Sstevel@tonic-gate  * while waiting to keep the pipeline does not stall.  additionally, it is
3937c478bd9Sstevel@tonic-gate  * likely that many of these constants are cached so that later accesses do
3947c478bd9Sstevel@tonic-gate  * not even go out to the bus.
3957c478bd9Sstevel@tonic-gate  *
3967c478bd9Sstevel@tonic-gate  * this array is declared `static' to keep the compiler from having to
3977c478bd9Sstevel@tonic-gate  * bcopy() this array onto the stack frame of MD5Transform() each time it is
3987c478bd9Sstevel@tonic-gate  * called -- which is unacceptably expensive.
3997c478bd9Sstevel@tonic-gate  *
4007c478bd9Sstevel@tonic-gate  * the `const' is to ensure that callers are good citizens and do not try to
4017c478bd9Sstevel@tonic-gate  * munge the array.  since these routines are going to be called from inside
4027c478bd9Sstevel@tonic-gate  * multithreaded kernelland, this is a good safety check. -- `constants' will
4037c478bd9Sstevel@tonic-gate  * end up in .rodata.
4047c478bd9Sstevel@tonic-gate  *
4057c478bd9Sstevel@tonic-gate  * unfortunately, loading from an array in this manner hurts performance under
4067c478bd9Sstevel@tonic-gate  * intel (and presumably other CISC machines).  so, there is a macro,
4077c478bd9Sstevel@tonic-gate  * MD5_CONST(), used in MD5Transform(), that either expands to a reference to
4087c478bd9Sstevel@tonic-gate  * this array, or to the actual constant, depending on what platform this code
4097c478bd9Sstevel@tonic-gate  * is compiled for.
4107c478bd9Sstevel@tonic-gate  */
4117c478bd9Sstevel@tonic-gate 
412554ff184Skais #ifdef sun4v
413554ff184Skais 
414554ff184Skais /*
415554ff184Skais  * Going to load these consts in 8B chunks, so need to enforce 8B alignment
416554ff184Skais  */
417554ff184Skais 
418554ff184Skais /* CSTYLED */
419554ff184Skais #pragma align 64 (md5_consts)
420554ff184Skais 
421554ff184Skais #endif /* sun4v */
422554ff184Skais 
4237c478bd9Sstevel@tonic-gate static const uint32_t md5_consts[] = {
4247c478bd9Sstevel@tonic-gate 	MD5_CONST_0,	MD5_CONST_1,	MD5_CONST_2,	MD5_CONST_3,
4257c478bd9Sstevel@tonic-gate 	MD5_CONST_4,	MD5_CONST_5,	MD5_CONST_6,	MD5_CONST_7,
4267c478bd9Sstevel@tonic-gate 	MD5_CONST_8,	MD5_CONST_9,	MD5_CONST_10,	MD5_CONST_11,
4277c478bd9Sstevel@tonic-gate 	MD5_CONST_12,	MD5_CONST_13,	MD5_CONST_14,	MD5_CONST_15,
4287c478bd9Sstevel@tonic-gate 	MD5_CONST_16,	MD5_CONST_17,	MD5_CONST_18,	MD5_CONST_19,
4297c478bd9Sstevel@tonic-gate 	MD5_CONST_20,	MD5_CONST_21,	MD5_CONST_22,	MD5_CONST_23,
4307c478bd9Sstevel@tonic-gate 	MD5_CONST_24,	MD5_CONST_25,	MD5_CONST_26,	MD5_CONST_27,
4317c478bd9Sstevel@tonic-gate 	MD5_CONST_28,	MD5_CONST_29,	MD5_CONST_30,	MD5_CONST_31,
4327c478bd9Sstevel@tonic-gate 	MD5_CONST_32,	MD5_CONST_33,	MD5_CONST_34,	MD5_CONST_35,
4337c478bd9Sstevel@tonic-gate 	MD5_CONST_36,	MD5_CONST_37,	MD5_CONST_38,	MD5_CONST_39,
4347c478bd9Sstevel@tonic-gate 	MD5_CONST_40,	MD5_CONST_41,	MD5_CONST_42,	MD5_CONST_43,
4357c478bd9Sstevel@tonic-gate 	MD5_CONST_44,	MD5_CONST_45,	MD5_CONST_46,	MD5_CONST_47,
4367c478bd9Sstevel@tonic-gate 	MD5_CONST_48,	MD5_CONST_49,	MD5_CONST_50,	MD5_CONST_51,
4377c478bd9Sstevel@tonic-gate 	MD5_CONST_52,	MD5_CONST_53,	MD5_CONST_54,	MD5_CONST_55,
4387c478bd9Sstevel@tonic-gate 	MD5_CONST_56,	MD5_CONST_57,	MD5_CONST_58,	MD5_CONST_59,
4397c478bd9Sstevel@tonic-gate 	MD5_CONST_60,	MD5_CONST_61,	MD5_CONST_62,	MD5_CONST_63
4407c478bd9Sstevel@tonic-gate };
4417c478bd9Sstevel@tonic-gate 
442554ff184Skais 
443554ff184Skais #ifdef sun4v
444554ff184Skais /*
445554ff184Skais  * To reduce the number of loads, load consts in 64-bit
446554ff184Skais  * chunks and then split.
447554ff184Skais  *
448554ff184Skais  * No need to mask upper 32-bits, as just interested in
449554ff184Skais  * low 32-bits (saves an & operation and means that this
450554ff184Skais  * optimization doesn't increases the icount.
451554ff184Skais  */
452554ff184Skais #define	MD5_CONST_e(x)		(md5_consts64[x/2] >> 32)
453554ff184Skais #define	MD5_CONST_o(x)		(md5_consts64[x/2])
454554ff184Skais 
455554ff184Skais #else
456554ff184Skais 
457554ff184Skais #define	MD5_CONST_e(x)		(md5_consts[x])
458554ff184Skais #define	MD5_CONST_o(x)		(md5_consts[x])
459554ff184Skais 
460554ff184Skais #endif /* sun4v */
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate #endif
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate /*
4657c478bd9Sstevel@tonic-gate  * MD5Init()
4667c478bd9Sstevel@tonic-gate  *
4677c478bd9Sstevel@tonic-gate  * purpose: initializes the md5 context and begins and md5 digest operation
4687c478bd9Sstevel@tonic-gate  *   input: MD5_CTX *	: the context to initialize.
4697c478bd9Sstevel@tonic-gate  *  output: void
4707c478bd9Sstevel@tonic-gate  */
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate void
4737c478bd9Sstevel@tonic-gate MD5Init(MD5_CTX *ctx)
4747c478bd9Sstevel@tonic-gate {
4757c478bd9Sstevel@tonic-gate 	ctx->count[0] = ctx->count[1] = 0;
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	/* load magic initialization constants */
4787c478bd9Sstevel@tonic-gate 	ctx->state[0] = MD5_INIT_CONST_1;
4797c478bd9Sstevel@tonic-gate 	ctx->state[1] = MD5_INIT_CONST_2;
4807c478bd9Sstevel@tonic-gate 	ctx->state[2] = MD5_INIT_CONST_3;
4817c478bd9Sstevel@tonic-gate 	ctx->state[3] = MD5_INIT_CONST_4;
4827c478bd9Sstevel@tonic-gate }
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate /*
4857c478bd9Sstevel@tonic-gate  * MD5Update()
4867c478bd9Sstevel@tonic-gate  *
4877c478bd9Sstevel@tonic-gate  * purpose: continues an md5 digest operation, using the message block
4887c478bd9Sstevel@tonic-gate  *          to update the context.
4897c478bd9Sstevel@tonic-gate  *   input: MD5_CTX *	: the context to update
4907c478bd9Sstevel@tonic-gate  *          uint8_t *	: the message block
4917c478bd9Sstevel@tonic-gate  *          uint32_t    : the length of the message block in bytes
4927c478bd9Sstevel@tonic-gate  *  output: void
4937c478bd9Sstevel@tonic-gate  *
4947c478bd9Sstevel@tonic-gate  * MD5 crunches in 64-byte blocks.  All numeric constants here are related to
4957c478bd9Sstevel@tonic-gate  * that property of MD5.
4967c478bd9Sstevel@tonic-gate  */
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate void
4997c478bd9Sstevel@tonic-gate MD5Update(MD5_CTX *ctx, const void *inpp, unsigned int input_len)
5007c478bd9Sstevel@tonic-gate {
5017c478bd9Sstevel@tonic-gate 	uint32_t		i, buf_index, buf_len;
502554ff184Skais #ifdef	sun4v
503554ff184Skais 	uint32_t		old_asi;
504554ff184Skais #endif	/* sun4v */
5057c478bd9Sstevel@tonic-gate 	const unsigned char 	*input = (const unsigned char *)inpp;
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	/* compute (number of bytes computed so far) mod 64 */
5087c478bd9Sstevel@tonic-gate 	buf_index = (ctx->count[0] >> 3) & 0x3F;
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	/* update number of bits hashed into this MD5 computation so far */
5117c478bd9Sstevel@tonic-gate 	if ((ctx->count[0] += (input_len << 3)) < (input_len << 3))
5127c478bd9Sstevel@tonic-gate 	    ctx->count[1]++;
5137c478bd9Sstevel@tonic-gate 	ctx->count[1] += (input_len >> 29);
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	buf_len = 64 - buf_index;
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	/* transform as many times as possible */
5187c478bd9Sstevel@tonic-gate 	i = 0;
5197c478bd9Sstevel@tonic-gate 	if (input_len >= buf_len) {
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 		/*
5227c478bd9Sstevel@tonic-gate 		 * general optimization:
5237c478bd9Sstevel@tonic-gate 		 *
5247c478bd9Sstevel@tonic-gate 		 * only do initial bcopy() and MD5Transform() if
5257c478bd9Sstevel@tonic-gate 		 * buf_index != 0.  if buf_index == 0, we're just
5267c478bd9Sstevel@tonic-gate 		 * wasting our time doing the bcopy() since there
5277c478bd9Sstevel@tonic-gate 		 * wasn't any data left over from a previous call to
5287c478bd9Sstevel@tonic-gate 		 * MD5Update().
5297c478bd9Sstevel@tonic-gate 		 */
5307c478bd9Sstevel@tonic-gate 
531554ff184Skais #ifdef sun4v
532554ff184Skais 		/*
533554ff184Skais 		 * For N1 use %asi register. However, costly to repeatedly set
534554ff184Skais 		 * in MD5Transform. Therefore, set once here.
535554ff184Skais 		 * Should probably restore the old value afterwards...
536554ff184Skais 		 */
537554ff184Skais 		old_asi = get_little();
538554ff184Skais 		set_little(0x88);
539554ff184Skais #endif /* sun4v */
540554ff184Skais 
5417c478bd9Sstevel@tonic-gate 		if (buf_index) {
5427c478bd9Sstevel@tonic-gate 			bcopy(input, &ctx->buf_un.buf8[buf_index], buf_len);
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 			MD5Transform(ctx->state[0], ctx->state[1],
5457c478bd9Sstevel@tonic-gate 			    ctx->state[2], ctx->state[3], ctx,
5467c478bd9Sstevel@tonic-gate 			    ctx->buf_un.buf8);
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 			i = buf_len;
5497c478bd9Sstevel@tonic-gate 		}
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 		for (; i + 63 < input_len; i += 64)
5527c478bd9Sstevel@tonic-gate 			MD5Transform(ctx->state[0], ctx->state[1],
5537c478bd9Sstevel@tonic-gate 			    ctx->state[2], ctx->state[3], ctx, &input[i]);
5547c478bd9Sstevel@tonic-gate 
555554ff184Skais 
556554ff184Skais #ifdef sun4v
557554ff184Skais 		/*
558554ff184Skais 		 * Restore old %ASI value
559554ff184Skais 		 */
560554ff184Skais 		set_little(old_asi);
561554ff184Skais #endif /* sun4v */
562554ff184Skais 
5637c478bd9Sstevel@tonic-gate 		/*
5647c478bd9Sstevel@tonic-gate 		 * general optimization:
5657c478bd9Sstevel@tonic-gate 		 *
5667c478bd9Sstevel@tonic-gate 		 * if i and input_len are the same, return now instead
5677c478bd9Sstevel@tonic-gate 		 * of calling bcopy(), since the bcopy() in this
5687c478bd9Sstevel@tonic-gate 		 * case will be an expensive nop.
5697c478bd9Sstevel@tonic-gate 		 */
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 		if (input_len == i)
5727c478bd9Sstevel@tonic-gate 			return;
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 		buf_index = 0;
5757c478bd9Sstevel@tonic-gate 	}
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	/* buffer remaining input */
5787c478bd9Sstevel@tonic-gate 	bcopy(&input[i], &ctx->buf_un.buf8[buf_index], input_len - i);
5797c478bd9Sstevel@tonic-gate }
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate /*
5827c478bd9Sstevel@tonic-gate  * MD5Final()
5837c478bd9Sstevel@tonic-gate  *
5847c478bd9Sstevel@tonic-gate  * purpose: ends an md5 digest operation, finalizing the message digest and
5857c478bd9Sstevel@tonic-gate  *          zeroing the context.
5867c478bd9Sstevel@tonic-gate  *   input: uint8_t *	: a buffer to store the digest in
5877c478bd9Sstevel@tonic-gate  *          MD5_CTX *   : the context to finalize, save, and zero
5887c478bd9Sstevel@tonic-gate  *  output: void
5897c478bd9Sstevel@tonic-gate  */
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate void
5927c478bd9Sstevel@tonic-gate MD5Final(unsigned char *digest, MD5_CTX *ctx)
5937c478bd9Sstevel@tonic-gate {
5947c478bd9Sstevel@tonic-gate 	uint8_t		bitcount_le[sizeof (ctx->count)];
5957c478bd9Sstevel@tonic-gate 	uint32_t	index = (ctx->count[0] >> 3) & 0x3f;
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	/* store bit count, little endian */
5987c478bd9Sstevel@tonic-gate 	Encode(bitcount_le, ctx->count, sizeof (bitcount_le));
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	/* pad out to 56 mod 64 */
6017c478bd9Sstevel@tonic-gate 	MD5Update(ctx, PADDING, ((index < 56) ? 56 : 120) - index);
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	/* append length (before padding) */
6047c478bd9Sstevel@tonic-gate 	MD5Update(ctx, bitcount_le, sizeof (bitcount_le));
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	/* store state in digest */
6077c478bd9Sstevel@tonic-gate 	Encode(digest, ctx->state, sizeof (ctx->state));
608*673007c6Sdarrenm 
609*673007c6Sdarrenm 	/* zeroize sensitive information */
610*673007c6Sdarrenm 	bzero(ctx, sizeof (*ctx));
6117c478bd9Sstevel@tonic-gate }
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate #ifndef	_KERNEL
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate void
6167c478bd9Sstevel@tonic-gate md5_calc(unsigned char *output, unsigned char *input, unsigned int inlen)
6177c478bd9Sstevel@tonic-gate {
6187c478bd9Sstevel@tonic-gate 	MD5_CTX context;
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	MD5Init(&context);
6217c478bd9Sstevel@tonic-gate 	MD5Update(&context, input, inlen);
6227c478bd9Sstevel@tonic-gate 	MD5Final(output, &context);
6237c478bd9Sstevel@tonic-gate }
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate #endif	/* !_KERNEL */
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate /*
6287c478bd9Sstevel@tonic-gate  * sparc register window optimization:
6297c478bd9Sstevel@tonic-gate  *
6307c478bd9Sstevel@tonic-gate  * `a', `b', `c', and `d' are passed into MD5Transform explicitly
6317c478bd9Sstevel@tonic-gate  * since it increases the number of registers available to the
6327c478bd9Sstevel@tonic-gate  * compiler.  under this scheme, these variables can be held in
6337c478bd9Sstevel@tonic-gate  * %i0 - %i3, which leaves more local and out registers available.
6347c478bd9Sstevel@tonic-gate  */
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate /*
6377c478bd9Sstevel@tonic-gate  * MD5Transform()
6387c478bd9Sstevel@tonic-gate  *
6397c478bd9Sstevel@tonic-gate  * purpose: md5 transformation -- updates the digest based on `block'
6407c478bd9Sstevel@tonic-gate  *   input: uint32_t	: bytes  1 -  4 of the digest
6417c478bd9Sstevel@tonic-gate  *          uint32_t	: bytes  5 -  8 of the digest
6427c478bd9Sstevel@tonic-gate  *          uint32_t	: bytes  9 - 12 of the digest
6437c478bd9Sstevel@tonic-gate  *          uint32_t	: bytes 12 - 16 of the digest
6447c478bd9Sstevel@tonic-gate  *          MD5_CTX *   : the context to update
6457c478bd9Sstevel@tonic-gate  *          uint8_t [64]: the block to use to update the digest
6467c478bd9Sstevel@tonic-gate  *  output: void
6477c478bd9Sstevel@tonic-gate  */
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate static void
6507c478bd9Sstevel@tonic-gate MD5Transform(uint32_t a, uint32_t b, uint32_t c, uint32_t d,
6517c478bd9Sstevel@tonic-gate     MD5_CTX *ctx, const uint8_t block[64])
6527c478bd9Sstevel@tonic-gate {
6537c478bd9Sstevel@tonic-gate 	/*
6547c478bd9Sstevel@tonic-gate 	 * general optimization:
6557c478bd9Sstevel@tonic-gate 	 *
6567c478bd9Sstevel@tonic-gate 	 * use individual integers instead of using an array.  this is a
6577c478bd9Sstevel@tonic-gate 	 * win, although the amount it wins by seems to vary quite a bit.
6587c478bd9Sstevel@tonic-gate 	 */
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	register uint32_t	x_0, x_1, x_2,  x_3,  x_4,  x_5,  x_6,  x_7;
6617c478bd9Sstevel@tonic-gate 	register uint32_t	x_8, x_9, x_10, x_11, x_12, x_13, x_14, x_15;
662554ff184Skais #ifdef sun4v
663554ff184Skais 	unsigned long long 	*md5_consts64;
664554ff184Skais 
665554ff184Skais 	md5_consts64 = (unsigned long long *) md5_consts;
666554ff184Skais #endif	/* sun4v */
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 	/*
6697c478bd9Sstevel@tonic-gate 	 * general optimization:
6707c478bd9Sstevel@tonic-gate 	 *
6717c478bd9Sstevel@tonic-gate 	 * the compiler (at least SC4.2/5.x) generates better code if
6727c478bd9Sstevel@tonic-gate 	 * variable use is localized.  in this case, swapping the integers in
6737c478bd9Sstevel@tonic-gate 	 * this order allows `x_0 'to be swapped nearest to its first use in
6747c478bd9Sstevel@tonic-gate 	 * FF(), and likewise for `x_1' and up.  note that the compiler
6757c478bd9Sstevel@tonic-gate 	 * prefers this to doing each swap right before the FF() that
6767c478bd9Sstevel@tonic-gate 	 * uses it.
6777c478bd9Sstevel@tonic-gate 	 */
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	/*
6807c478bd9Sstevel@tonic-gate 	 * sparc v9/v8plus optimization:
6817c478bd9Sstevel@tonic-gate 	 *
6827c478bd9Sstevel@tonic-gate 	 * if `block' is already aligned on a 4-byte boundary, use the
6837c478bd9Sstevel@tonic-gate 	 * optimized load_little_32() directly.  otherwise, bcopy()
6847c478bd9Sstevel@tonic-gate 	 * into a buffer that *is* aligned on a 4-byte boundary and
6857c478bd9Sstevel@tonic-gate 	 * then do the load_little_32() on that buffer.  benchmarks
6867c478bd9Sstevel@tonic-gate 	 * have shown that using the bcopy() is better than loading
6877c478bd9Sstevel@tonic-gate 	 * the bytes individually and doing the endian-swap by hand.
6887c478bd9Sstevel@tonic-gate 	 *
6897c478bd9Sstevel@tonic-gate 	 * even though it's quite tempting to assign to do:
6907c478bd9Sstevel@tonic-gate 	 *
6917c478bd9Sstevel@tonic-gate 	 * blk = bcopy(blk, ctx->buf_un.buf32, sizeof (ctx->buf_un.buf32));
6927c478bd9Sstevel@tonic-gate 	 *
6937c478bd9Sstevel@tonic-gate 	 * and only have one set of LOAD_LITTLE_32()'s, the compiler (at least
6947c478bd9Sstevel@tonic-gate 	 * SC4.2/5.x) *does not* like that, so please resist the urge.
6957c478bd9Sstevel@tonic-gate 	 */
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate #ifdef _MD5_CHECK_ALIGNMENT
6987c478bd9Sstevel@tonic-gate 	if ((uintptr_t)block & 0x3) {		/* not 4-byte aligned? */
6997c478bd9Sstevel@tonic-gate 		bcopy(block, ctx->buf_un.buf32, sizeof (ctx->buf_un.buf32));
700554ff184Skais 
701554ff184Skais #ifdef sun4v
702554ff184Skais 		x_15 = LOAD_LITTLE_32_f(ctx->buf_un.buf32);
703554ff184Skais 		x_14 = LOAD_LITTLE_32_e(ctx->buf_un.buf32);
704554ff184Skais 		x_13 = LOAD_LITTLE_32_d(ctx->buf_un.buf32);
705554ff184Skais 		x_12 = LOAD_LITTLE_32_c(ctx->buf_un.buf32);
706554ff184Skais 		x_11 = LOAD_LITTLE_32_b(ctx->buf_un.buf32);
707554ff184Skais 		x_10 = LOAD_LITTLE_32_a(ctx->buf_un.buf32);
708554ff184Skais 		x_9  = LOAD_LITTLE_32_9(ctx->buf_un.buf32);
709554ff184Skais 		x_8  = LOAD_LITTLE_32_8(ctx->buf_un.buf32);
710554ff184Skais 		x_7  = LOAD_LITTLE_32_7(ctx->buf_un.buf32);
711554ff184Skais 		x_6  = LOAD_LITTLE_32_6(ctx->buf_un.buf32);
712554ff184Skais 		x_5  = LOAD_LITTLE_32_5(ctx->buf_un.buf32);
713554ff184Skais 		x_4  = LOAD_LITTLE_32_4(ctx->buf_un.buf32);
714554ff184Skais 		x_3  = LOAD_LITTLE_32_3(ctx->buf_un.buf32);
715554ff184Skais 		x_2  = LOAD_LITTLE_32_2(ctx->buf_un.buf32);
716554ff184Skais 		x_1  = LOAD_LITTLE_32_1(ctx->buf_un.buf32);
717554ff184Skais 		x_0  = LOAD_LITTLE_32_0(ctx->buf_un.buf32);
718554ff184Skais #else
7197c478bd9Sstevel@tonic-gate 		x_15 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 15);
7207c478bd9Sstevel@tonic-gate 		x_14 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 14);
7217c478bd9Sstevel@tonic-gate 		x_13 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 13);
7227c478bd9Sstevel@tonic-gate 		x_12 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 12);
7237c478bd9Sstevel@tonic-gate 		x_11 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 11);
7247c478bd9Sstevel@tonic-gate 		x_10 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 10);
7257c478bd9Sstevel@tonic-gate 		x_9  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  9);
7267c478bd9Sstevel@tonic-gate 		x_8  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  8);
7277c478bd9Sstevel@tonic-gate 		x_7  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  7);
7287c478bd9Sstevel@tonic-gate 		x_6  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  6);
7297c478bd9Sstevel@tonic-gate 		x_5  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  5);
7307c478bd9Sstevel@tonic-gate 		x_4  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  4);
7317c478bd9Sstevel@tonic-gate 		x_3  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  3);
7327c478bd9Sstevel@tonic-gate 		x_2  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  2);
7337c478bd9Sstevel@tonic-gate 		x_1  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  1);
7347c478bd9Sstevel@tonic-gate 		x_0  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  0);
735554ff184Skais #endif /* sun4v */
7367c478bd9Sstevel@tonic-gate 	} else
7377c478bd9Sstevel@tonic-gate #endif
7387c478bd9Sstevel@tonic-gate 	{
739554ff184Skais 
740554ff184Skais #ifdef sun4v
741554ff184Skais 		x_15 = LOAD_LITTLE_32_f(block);
742554ff184Skais 		x_14 = LOAD_LITTLE_32_e(block);
743554ff184Skais 		x_13 = LOAD_LITTLE_32_d(block);
744554ff184Skais 		x_12 = LOAD_LITTLE_32_c(block);
745554ff184Skais 		x_11 = LOAD_LITTLE_32_b(block);
746554ff184Skais 		x_10 = LOAD_LITTLE_32_a(block);
747554ff184Skais 		x_9  = LOAD_LITTLE_32_9(block);
748554ff184Skais 		x_8  = LOAD_LITTLE_32_8(block);
749554ff184Skais 		x_7  = LOAD_LITTLE_32_7(block);
750554ff184Skais 		x_6  = LOAD_LITTLE_32_6(block);
751554ff184Skais 		x_5  = LOAD_LITTLE_32_5(block);
752554ff184Skais 		x_4  = LOAD_LITTLE_32_4(block);
753554ff184Skais 		x_3  = LOAD_LITTLE_32_3(block);
754554ff184Skais 		x_2  = LOAD_LITTLE_32_2(block);
755554ff184Skais 		x_1  = LOAD_LITTLE_32_1(block);
756554ff184Skais 		x_0  = LOAD_LITTLE_32_0(block);
757554ff184Skais #else
7587c478bd9Sstevel@tonic-gate 		x_15 = LOAD_LITTLE_32(block + 60);
7597c478bd9Sstevel@tonic-gate 		x_14 = LOAD_LITTLE_32(block + 56);
7607c478bd9Sstevel@tonic-gate 		x_13 = LOAD_LITTLE_32(block + 52);
7617c478bd9Sstevel@tonic-gate 		x_12 = LOAD_LITTLE_32(block + 48);
7627c478bd9Sstevel@tonic-gate 		x_11 = LOAD_LITTLE_32(block + 44);
7637c478bd9Sstevel@tonic-gate 		x_10 = LOAD_LITTLE_32(block + 40);
7647c478bd9Sstevel@tonic-gate 		x_9  = LOAD_LITTLE_32(block + 36);
7657c478bd9Sstevel@tonic-gate 		x_8  = LOAD_LITTLE_32(block + 32);
7667c478bd9Sstevel@tonic-gate 		x_7  = LOAD_LITTLE_32(block + 28);
7677c478bd9Sstevel@tonic-gate 		x_6  = LOAD_LITTLE_32(block + 24);
7687c478bd9Sstevel@tonic-gate 		x_5  = LOAD_LITTLE_32(block + 20);
7697c478bd9Sstevel@tonic-gate 		x_4  = LOAD_LITTLE_32(block + 16);
7707c478bd9Sstevel@tonic-gate 		x_3  = LOAD_LITTLE_32(block + 12);
7717c478bd9Sstevel@tonic-gate 		x_2  = LOAD_LITTLE_32(block +  8);
7727c478bd9Sstevel@tonic-gate 		x_1  = LOAD_LITTLE_32(block +  4);
7737c478bd9Sstevel@tonic-gate 		x_0  = LOAD_LITTLE_32(block +  0);
774554ff184Skais #endif /* sun4v */
7757c478bd9Sstevel@tonic-gate 	}
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 	/* round 1 */
778554ff184Skais 	FF(a, b, c, d, 	x_0, MD5_SHIFT_11, MD5_CONST_e(0));  /* 1 */
779554ff184Skais 	FF(d, a, b, c, 	x_1, MD5_SHIFT_12, MD5_CONST_o(1));  /* 2 */
780554ff184Skais 	FF(c, d, a, b, 	x_2, MD5_SHIFT_13, MD5_CONST_e(2));  /* 3 */
781554ff184Skais 	FF(b, c, d, a, 	x_3, MD5_SHIFT_14, MD5_CONST_o(3));  /* 4 */
782554ff184Skais 	FF(a, b, c, d, 	x_4, MD5_SHIFT_11, MD5_CONST_e(4));  /* 5 */
783554ff184Skais 	FF(d, a, b, c, 	x_5, MD5_SHIFT_12, MD5_CONST_o(5));  /* 6 */
784554ff184Skais 	FF(c, d, a, b, 	x_6, MD5_SHIFT_13, MD5_CONST_e(6));  /* 7 */
785554ff184Skais 	FF(b, c, d, a, 	x_7, MD5_SHIFT_14, MD5_CONST_o(7));  /* 8 */
786554ff184Skais 	FF(a, b, c, d, 	x_8, MD5_SHIFT_11, MD5_CONST_e(8));  /* 9 */
787554ff184Skais 	FF(d, a, b, c, 	x_9, MD5_SHIFT_12, MD5_CONST_o(9));  /* 10 */
788554ff184Skais 	FF(c, d, a, b, x_10, MD5_SHIFT_13, MD5_CONST_e(10)); /* 11 */
789554ff184Skais 	FF(b, c, d, a, x_11, MD5_SHIFT_14, MD5_CONST_o(11)); /* 12 */
790554ff184Skais 	FF(a, b, c, d, x_12, MD5_SHIFT_11, MD5_CONST_e(12)); /* 13 */
791554ff184Skais 	FF(d, a, b, c, x_13, MD5_SHIFT_12, MD5_CONST_o(13)); /* 14 */
792554ff184Skais 	FF(c, d, a, b, x_14, MD5_SHIFT_13, MD5_CONST_e(14)); /* 15 */
793554ff184Skais 	FF(b, c, d, a, x_15, MD5_SHIFT_14, MD5_CONST_o(15)); /* 16 */
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	/* round 2 */
796554ff184Skais 	GG(a, b, c, d,  x_1, MD5_SHIFT_21, MD5_CONST_e(16)); /* 17 */
797554ff184Skais 	GG(d, a, b, c,  x_6, MD5_SHIFT_22, MD5_CONST_o(17)); /* 18 */
798554ff184Skais 	GG(c, d, a, b, x_11, MD5_SHIFT_23, MD5_CONST_e(18)); /* 19 */
799554ff184Skais 	GG(b, c, d, a,  x_0, MD5_SHIFT_24, MD5_CONST_o(19)); /* 20 */
800554ff184Skais 	GG(a, b, c, d,  x_5, MD5_SHIFT_21, MD5_CONST_e(20)); /* 21 */
801554ff184Skais 	GG(d, a, b, c, x_10, MD5_SHIFT_22, MD5_CONST_o(21)); /* 22 */
802554ff184Skais 	GG(c, d, a, b, x_15, MD5_SHIFT_23, MD5_CONST_e(22)); /* 23 */
803554ff184Skais 	GG(b, c, d, a,  x_4, MD5_SHIFT_24, MD5_CONST_o(23)); /* 24 */
804554ff184Skais 	GG(a, b, c, d,  x_9, MD5_SHIFT_21, MD5_CONST_e(24)); /* 25 */
805554ff184Skais 	GG(d, a, b, c, x_14, MD5_SHIFT_22, MD5_CONST_o(25)); /* 26 */
806554ff184Skais 	GG(c, d, a, b,  x_3, MD5_SHIFT_23, MD5_CONST_e(26)); /* 27 */
807554ff184Skais 	GG(b, c, d, a,  x_8, MD5_SHIFT_24, MD5_CONST_o(27)); /* 28 */
808554ff184Skais 	GG(a, b, c, d, x_13, MD5_SHIFT_21, MD5_CONST_e(28)); /* 29 */
809554ff184Skais 	GG(d, a, b, c,  x_2, MD5_SHIFT_22, MD5_CONST_o(29)); /* 30 */
810554ff184Skais 	GG(c, d, a, b,  x_7, MD5_SHIFT_23, MD5_CONST_e(30)); /* 31 */
811554ff184Skais 	GG(b, c, d, a, x_12, MD5_SHIFT_24, MD5_CONST_o(31)); /* 32 */
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	/* round 3 */
814554ff184Skais 	HH(a, b, c, d,  x_5, MD5_SHIFT_31, MD5_CONST_e(32)); /* 33 */
815554ff184Skais 	HH(d, a, b, c,  x_8, MD5_SHIFT_32, MD5_CONST_o(33)); /* 34 */
816554ff184Skais 	HH(c, d, a, b, x_11, MD5_SHIFT_33, MD5_CONST_e(34)); /* 35 */
817554ff184Skais 	HH(b, c, d, a, x_14, MD5_SHIFT_34, MD5_CONST_o(35)); /* 36 */
818554ff184Skais 	HH(a, b, c, d,  x_1, MD5_SHIFT_31, MD5_CONST_e(36)); /* 37 */
819554ff184Skais 	HH(d, a, b, c,  x_4, MD5_SHIFT_32, MD5_CONST_o(37)); /* 38 */
820554ff184Skais 	HH(c, d, a, b,  x_7, MD5_SHIFT_33, MD5_CONST_e(38)); /* 39 */
821554ff184Skais 	HH(b, c, d, a, x_10, MD5_SHIFT_34, MD5_CONST_o(39)); /* 40 */
822554ff184Skais 	HH(a, b, c, d, x_13, MD5_SHIFT_31, MD5_CONST_e(40)); /* 41 */
823554ff184Skais 	HH(d, a, b, c,  x_0, MD5_SHIFT_32, MD5_CONST_o(41)); /* 42 */
824554ff184Skais 	HH(c, d, a, b,  x_3, MD5_SHIFT_33, MD5_CONST_e(42)); /* 43 */
825554ff184Skais 	HH(b, c, d, a,  x_6, MD5_SHIFT_34, MD5_CONST_o(43)); /* 44 */
826554ff184Skais 	HH(a, b, c, d,  x_9, MD5_SHIFT_31, MD5_CONST_e(44)); /* 45 */
827554ff184Skais 	HH(d, a, b, c, x_12, MD5_SHIFT_32, MD5_CONST_o(45)); /* 46 */
828554ff184Skais 	HH(c, d, a, b, x_15, MD5_SHIFT_33, MD5_CONST_e(46)); /* 47 */
829554ff184Skais 	HH(b, c, d, a,  x_2, MD5_SHIFT_34, MD5_CONST_o(47)); /* 48 */
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 	/* round 4 */
832554ff184Skais 	II(a, b, c, d,  x_0, MD5_SHIFT_41, MD5_CONST_e(48)); /* 49 */
833554ff184Skais 	II(d, a, b, c,  x_7, MD5_SHIFT_42, MD5_CONST_o(49)); /* 50 */
834554ff184Skais 	II(c, d, a, b, x_14, MD5_SHIFT_43, MD5_CONST_e(50)); /* 51 */
835554ff184Skais 	II(b, c, d, a,  x_5, MD5_SHIFT_44, MD5_CONST_o(51)); /* 52 */
836554ff184Skais 	II(a, b, c, d, x_12, MD5_SHIFT_41, MD5_CONST_e(52)); /* 53 */
837554ff184Skais 	II(d, a, b, c,  x_3, MD5_SHIFT_42, MD5_CONST_o(53)); /* 54 */
838554ff184Skais 	II(c, d, a, b, x_10, MD5_SHIFT_43, MD5_CONST_e(54)); /* 55 */
839554ff184Skais 	II(b, c, d, a,  x_1, MD5_SHIFT_44, MD5_CONST_o(55)); /* 56 */
840554ff184Skais 	II(a, b, c, d,  x_8, MD5_SHIFT_41, MD5_CONST_e(56)); /* 57 */
841554ff184Skais 	II(d, a, b, c, x_15, MD5_SHIFT_42, MD5_CONST_o(57)); /* 58 */
842554ff184Skais 	II(c, d, a, b,  x_6, MD5_SHIFT_43, MD5_CONST_e(58)); /* 59 */
843554ff184Skais 	II(b, c, d, a, x_13, MD5_SHIFT_44, MD5_CONST_o(59)); /* 60 */
844554ff184Skais 	II(a, b, c, d,  x_4, MD5_SHIFT_41, MD5_CONST_e(60)); /* 61 */
845554ff184Skais 	II(d, a, b, c, x_11, MD5_SHIFT_42, MD5_CONST_o(61)); /* 62 */
846554ff184Skais 	II(c, d, a, b,  x_2, MD5_SHIFT_43, MD5_CONST_e(62)); /* 63 */
847554ff184Skais 	II(b, c, d, a,  x_9, MD5_SHIFT_44, MD5_CONST_o(63)); /* 64 */
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 	ctx->state[0] += a;
8507c478bd9Sstevel@tonic-gate 	ctx->state[1] += b;
8517c478bd9Sstevel@tonic-gate 	ctx->state[2] += c;
8527c478bd9Sstevel@tonic-gate 	ctx->state[3] += d;
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 	/*
8557c478bd9Sstevel@tonic-gate 	 * zeroize sensitive information -- compiler will optimize
8567c478bd9Sstevel@tonic-gate 	 * this out if everything is kept in registers
8577c478bd9Sstevel@tonic-gate 	 */
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 	x_0 = x_1  = x_2  = x_3  = x_4  = x_5  = x_6  = x_7 = x_8 = 0;
8607c478bd9Sstevel@tonic-gate 	x_9 = x_10 = x_11 = x_12 = x_13 = x_14 = x_15 = 0;
8617c478bd9Sstevel@tonic-gate }
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate /*
8647c478bd9Sstevel@tonic-gate  * devpro compiler optimization:
8657c478bd9Sstevel@tonic-gate  *
8667c478bd9Sstevel@tonic-gate  * the compiler can generate better code if it knows that `input' and
8677c478bd9Sstevel@tonic-gate  * `output' do not point to the same source.  there is no portable
8687c478bd9Sstevel@tonic-gate  * way to tell the compiler this, but the devpro compiler recognizes the
8697c478bd9Sstevel@tonic-gate  * `_Restrict' keyword to indicate this condition.  use it if possible.
8707c478bd9Sstevel@tonic-gate  */
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate #if defined(__RESTRICT) && !defined(__GNUC__)
8737c478bd9Sstevel@tonic-gate #define	restrict	_Restrict
8747c478bd9Sstevel@tonic-gate #else
8757c478bd9Sstevel@tonic-gate #define	restrict	/* nothing */
8767c478bd9Sstevel@tonic-gate #endif
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate /*
8797c478bd9Sstevel@tonic-gate  * Encode()
8807c478bd9Sstevel@tonic-gate  *
8817c478bd9Sstevel@tonic-gate  * purpose: to convert a list of numbers from big endian to little endian
8827c478bd9Sstevel@tonic-gate  *   input: uint8_t *	: place to store the converted little endian numbers
8837c478bd9Sstevel@tonic-gate  *	    uint32_t *	: place to get numbers to convert from
8847c478bd9Sstevel@tonic-gate  *          size_t	: the length of the input in bytes
8857c478bd9Sstevel@tonic-gate  *  output: void
8867c478bd9Sstevel@tonic-gate  */
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate static void
8897c478bd9Sstevel@tonic-gate Encode(uint8_t *restrict output, uint32_t *restrict input, size_t input_len)
8907c478bd9Sstevel@tonic-gate {
8917c478bd9Sstevel@tonic-gate 	size_t		i, j;
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 	for (i = 0, j = 0; j < input_len; i++, j += sizeof (uint32_t)) {
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate #ifdef _LITTLE_ENDIAN
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate #ifdef _MD5_CHECK_ALIGNMENT
8987c478bd9Sstevel@tonic-gate 		if ((uintptr_t)output & 0x3)	/* Not 4-byte aligned */
8997c478bd9Sstevel@tonic-gate 			bcopy(input + i, output + j, 4);
9007c478bd9Sstevel@tonic-gate 		else *(uint32_t *)(output + j) = input[i];
9017c478bd9Sstevel@tonic-gate #else
9027c478bd9Sstevel@tonic-gate 		*(uint32_t *)(output + j) = input[i];
9037c478bd9Sstevel@tonic-gate #endif /* _MD5_CHECK_ALIGNMENT */
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate #else	/* big endian -- will work on little endian, but slowly */
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 		output[j] = input[i] & 0xff;
9087c478bd9Sstevel@tonic-gate 		output[j + 1] = (input[i] >> 8)  & 0xff;
9097c478bd9Sstevel@tonic-gate 		output[j + 2] = (input[i] >> 16) & 0xff;
9107c478bd9Sstevel@tonic-gate 		output[j + 3] = (input[i] >> 24) & 0xff;
9117c478bd9Sstevel@tonic-gate #endif
9127c478bd9Sstevel@tonic-gate 	}
9137c478bd9Sstevel@tonic-gate }
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate #if	defined(_KERNEL) && !defined(_BOOT)
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate /*
9187c478bd9Sstevel@tonic-gate  * KCF software provider control entry points.
9197c478bd9Sstevel@tonic-gate  */
9207c478bd9Sstevel@tonic-gate /* ARGSUSED */
9217c478bd9Sstevel@tonic-gate static void
9227c478bd9Sstevel@tonic-gate md5_provider_status(crypto_provider_handle_t provider, uint_t *status)
9237c478bd9Sstevel@tonic-gate {
9247c478bd9Sstevel@tonic-gate 	*status = CRYPTO_PROVIDER_READY;
9257c478bd9Sstevel@tonic-gate }
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate /*
9287c478bd9Sstevel@tonic-gate  * KCF software provider digest entry points.
9297c478bd9Sstevel@tonic-gate  */
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate static int
9327c478bd9Sstevel@tonic-gate md5_digest_init(crypto_ctx_t *ctx, crypto_mechanism_t *mechanism,
9337c478bd9Sstevel@tonic-gate     crypto_req_handle_t req)
9347c478bd9Sstevel@tonic-gate {
9357c478bd9Sstevel@tonic-gate 	if (mechanism->cm_type != MD5_MECH_INFO_TYPE)
9367c478bd9Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 	/*
9397c478bd9Sstevel@tonic-gate 	 * Allocate and initialize MD5 context.
9407c478bd9Sstevel@tonic-gate 	 */
9417c478bd9Sstevel@tonic-gate 	ctx->cc_provider_private = kmem_alloc(sizeof (md5_ctx_t),
9427c478bd9Sstevel@tonic-gate 	    crypto_kmflag(req));
9437c478bd9Sstevel@tonic-gate 	if (ctx->cc_provider_private == NULL)
9447c478bd9Sstevel@tonic-gate 		return (CRYPTO_HOST_MEMORY);
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 	PROV_MD5_CTX(ctx)->mc_mech_type = MD5_MECH_INFO_TYPE;
9477c478bd9Sstevel@tonic-gate 	MD5Init(&PROV_MD5_CTX(ctx)->mc_md5_ctx);
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
9507c478bd9Sstevel@tonic-gate }
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate /*
9537c478bd9Sstevel@tonic-gate  * Helper MD5 digest update function for uio data.
9547c478bd9Sstevel@tonic-gate  */
9557c478bd9Sstevel@tonic-gate static int
9567c478bd9Sstevel@tonic-gate md5_digest_update_uio(MD5_CTX *md5_ctx, crypto_data_t *data)
9577c478bd9Sstevel@tonic-gate {
9587c478bd9Sstevel@tonic-gate 	off_t offset = data->cd_offset;
9597c478bd9Sstevel@tonic-gate 	size_t length = data->cd_length;
9607c478bd9Sstevel@tonic-gate 	uint_t vec_idx;
9617c478bd9Sstevel@tonic-gate 	size_t cur_len;
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 	/* we support only kernel buffer */
9647c478bd9Sstevel@tonic-gate 	if (data->cd_uio->uio_segflg != UIO_SYSSPACE)
9657c478bd9Sstevel@tonic-gate 		return (CRYPTO_ARGUMENTS_BAD);
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 	/*
9687c478bd9Sstevel@tonic-gate 	 * Jump to the first iovec containing data to be
9697c478bd9Sstevel@tonic-gate 	 * digested.
9707c478bd9Sstevel@tonic-gate 	 */
9717c478bd9Sstevel@tonic-gate 	for (vec_idx = 0; vec_idx < data->cd_uio->uio_iovcnt &&
9727c478bd9Sstevel@tonic-gate 	    offset >= data->cd_uio->uio_iov[vec_idx].iov_len;
9737c478bd9Sstevel@tonic-gate 	    offset -= data->cd_uio->uio_iov[vec_idx++].iov_len);
9747c478bd9Sstevel@tonic-gate 	if (vec_idx == data->cd_uio->uio_iovcnt) {
9757c478bd9Sstevel@tonic-gate 		/*
9767c478bd9Sstevel@tonic-gate 		 * The caller specified an offset that is larger than the
9777c478bd9Sstevel@tonic-gate 		 * total size of the buffers it provided.
9787c478bd9Sstevel@tonic-gate 		 */
9797c478bd9Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
9807c478bd9Sstevel@tonic-gate 	}
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate 	/*
9837c478bd9Sstevel@tonic-gate 	 * Now do the digesting on the iovecs.
9847c478bd9Sstevel@tonic-gate 	 */
9857c478bd9Sstevel@tonic-gate 	while (vec_idx < data->cd_uio->uio_iovcnt && length > 0) {
9867c478bd9Sstevel@tonic-gate 		cur_len = MIN(data->cd_uio->uio_iov[vec_idx].iov_len -
9877c478bd9Sstevel@tonic-gate 		    offset, length);
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 		MD5Update(md5_ctx, data->cd_uio->uio_iov[vec_idx].iov_base +
9907c478bd9Sstevel@tonic-gate 		    offset, cur_len);
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 		length -= cur_len;
9937c478bd9Sstevel@tonic-gate 		vec_idx++;
9947c478bd9Sstevel@tonic-gate 		offset = 0;
9957c478bd9Sstevel@tonic-gate 	}
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 	if (vec_idx == data->cd_uio->uio_iovcnt && length > 0) {
9987c478bd9Sstevel@tonic-gate 		/*
9997c478bd9Sstevel@tonic-gate 		 * The end of the specified iovec's was reached but
10007c478bd9Sstevel@tonic-gate 		 * the length requested could not be processed, i.e.
10017c478bd9Sstevel@tonic-gate 		 * The caller requested to digest more data than it provided.
10027c478bd9Sstevel@tonic-gate 		 */
10037c478bd9Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
10047c478bd9Sstevel@tonic-gate 	}
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
10077c478bd9Sstevel@tonic-gate }
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate /*
10107c478bd9Sstevel@tonic-gate  * Helper MD5 digest final function for uio data.
10117c478bd9Sstevel@tonic-gate  * digest_len is the length of the desired digest. If digest_len
10127c478bd9Sstevel@tonic-gate  * is smaller than the default MD5 digest length, the caller
10137c478bd9Sstevel@tonic-gate  * must pass a scratch buffer, digest_scratch, which must
10147c478bd9Sstevel@tonic-gate  * be at least MD5_DIGEST_LENGTH bytes.
10157c478bd9Sstevel@tonic-gate  */
10167c478bd9Sstevel@tonic-gate static int
10177c478bd9Sstevel@tonic-gate md5_digest_final_uio(MD5_CTX *md5_ctx, crypto_data_t *digest,
10187c478bd9Sstevel@tonic-gate     ulong_t digest_len, uchar_t *digest_scratch)
10197c478bd9Sstevel@tonic-gate {
10207c478bd9Sstevel@tonic-gate 	off_t offset = digest->cd_offset;
10217c478bd9Sstevel@tonic-gate 	uint_t vec_idx;
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 	/* we support only kernel buffer */
10247c478bd9Sstevel@tonic-gate 	if (digest->cd_uio->uio_segflg != UIO_SYSSPACE)
10257c478bd9Sstevel@tonic-gate 		return (CRYPTO_ARGUMENTS_BAD);
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 	/*
10287c478bd9Sstevel@tonic-gate 	 * Jump to the first iovec containing ptr to the digest to
10297c478bd9Sstevel@tonic-gate 	 * be returned.
10307c478bd9Sstevel@tonic-gate 	 */
10317c478bd9Sstevel@tonic-gate 	for (vec_idx = 0; offset >= digest->cd_uio->uio_iov[vec_idx].iov_len &&
10327c478bd9Sstevel@tonic-gate 	    vec_idx < digest->cd_uio->uio_iovcnt;
10337c478bd9Sstevel@tonic-gate 	    offset -= digest->cd_uio->uio_iov[vec_idx++].iov_len);
10347c478bd9Sstevel@tonic-gate 	if (vec_idx == digest->cd_uio->uio_iovcnt) {
10357c478bd9Sstevel@tonic-gate 		/*
10367c478bd9Sstevel@tonic-gate 		 * The caller specified an offset that is
10377c478bd9Sstevel@tonic-gate 		 * larger than the total size of the buffers
10387c478bd9Sstevel@tonic-gate 		 * it provided.
10397c478bd9Sstevel@tonic-gate 		 */
10407c478bd9Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
10417c478bd9Sstevel@tonic-gate 	}
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 	if (offset + digest_len <=
10447c478bd9Sstevel@tonic-gate 	    digest->cd_uio->uio_iov[vec_idx].iov_len) {
10457c478bd9Sstevel@tonic-gate 		/*
10467c478bd9Sstevel@tonic-gate 		 * The computed MD5 digest will fit in the current
10477c478bd9Sstevel@tonic-gate 		 * iovec.
10487c478bd9Sstevel@tonic-gate 		 */
10497c478bd9Sstevel@tonic-gate 		if (digest_len != MD5_DIGEST_LENGTH) {
10507c478bd9Sstevel@tonic-gate 			/*
10517c478bd9Sstevel@tonic-gate 			 * The caller requested a short digest. Digest
10527c478bd9Sstevel@tonic-gate 			 * into a scratch buffer and return to
10537c478bd9Sstevel@tonic-gate 			 * the user only what was requested.
10547c478bd9Sstevel@tonic-gate 			 */
10557c478bd9Sstevel@tonic-gate 			MD5Final(digest_scratch, md5_ctx);
10567c478bd9Sstevel@tonic-gate 			bcopy(digest_scratch, (uchar_t *)digest->
10577c478bd9Sstevel@tonic-gate 			    cd_uio->uio_iov[vec_idx].iov_base + offset,
10587c478bd9Sstevel@tonic-gate 			    digest_len);
10597c478bd9Sstevel@tonic-gate 		} else {
10607c478bd9Sstevel@tonic-gate 			MD5Final((uchar_t *)digest->
10617c478bd9Sstevel@tonic-gate 			    cd_uio->uio_iov[vec_idx].iov_base + offset,
10627c478bd9Sstevel@tonic-gate 			    md5_ctx);
10637c478bd9Sstevel@tonic-gate 		}
10647c478bd9Sstevel@tonic-gate 	} else {
10657c478bd9Sstevel@tonic-gate 		/*
10667c478bd9Sstevel@tonic-gate 		 * The computed digest will be crossing one or more iovec's.
10677c478bd9Sstevel@tonic-gate 		 * This is bad performance-wise but we need to support it.
10687c478bd9Sstevel@tonic-gate 		 * Allocate a small scratch buffer on the stack and
10697c478bd9Sstevel@tonic-gate 		 * copy it piece meal to the specified digest iovec's.
10707c478bd9Sstevel@tonic-gate 		 */
10717c478bd9Sstevel@tonic-gate 		uchar_t digest_tmp[MD5_DIGEST_LENGTH];
10727c478bd9Sstevel@tonic-gate 		off_t scratch_offset = 0;
10737c478bd9Sstevel@tonic-gate 		size_t length = digest_len;
10747c478bd9Sstevel@tonic-gate 		size_t cur_len;
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate 		MD5Final(digest_tmp, md5_ctx);
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 		while (vec_idx < digest->cd_uio->uio_iovcnt && length > 0) {
10797c478bd9Sstevel@tonic-gate 			cur_len = MIN(digest->cd_uio->uio_iov[vec_idx].iov_len -
10807c478bd9Sstevel@tonic-gate 			    offset, length);
10817c478bd9Sstevel@tonic-gate 			bcopy(digest_tmp + scratch_offset,
10827c478bd9Sstevel@tonic-gate 			    digest->cd_uio->uio_iov[vec_idx].iov_base + offset,
10837c478bd9Sstevel@tonic-gate 			    cur_len);
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 			length -= cur_len;
10867c478bd9Sstevel@tonic-gate 			vec_idx++;
10877c478bd9Sstevel@tonic-gate 			scratch_offset += cur_len;
10887c478bd9Sstevel@tonic-gate 			offset = 0;
10897c478bd9Sstevel@tonic-gate 		}
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 		if (vec_idx == digest->cd_uio->uio_iovcnt && length > 0) {
10927c478bd9Sstevel@tonic-gate 			/*
10937c478bd9Sstevel@tonic-gate 			 * The end of the specified iovec's was reached but
10947c478bd9Sstevel@tonic-gate 			 * the length requested could not be processed, i.e.
10957c478bd9Sstevel@tonic-gate 			 * The caller requested to digest more data than it
10967c478bd9Sstevel@tonic-gate 			 * provided.
10977c478bd9Sstevel@tonic-gate 			 */
10987c478bd9Sstevel@tonic-gate 			return (CRYPTO_DATA_LEN_RANGE);
10997c478bd9Sstevel@tonic-gate 		}
11007c478bd9Sstevel@tonic-gate 	}
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
11037c478bd9Sstevel@tonic-gate }
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate /*
11067c478bd9Sstevel@tonic-gate  * Helper MD5 digest update for mblk's.
11077c478bd9Sstevel@tonic-gate  */
11087c478bd9Sstevel@tonic-gate static int
11097c478bd9Sstevel@tonic-gate md5_digest_update_mblk(MD5_CTX *md5_ctx, crypto_data_t *data)
11107c478bd9Sstevel@tonic-gate {
11117c478bd9Sstevel@tonic-gate 	off_t offset = data->cd_offset;
11127c478bd9Sstevel@tonic-gate 	size_t length = data->cd_length;
11137c478bd9Sstevel@tonic-gate 	mblk_t *mp;
11147c478bd9Sstevel@tonic-gate 	size_t cur_len;
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate 	/*
11177c478bd9Sstevel@tonic-gate 	 * Jump to the first mblk_t containing data to be digested.
11187c478bd9Sstevel@tonic-gate 	 */
11197c478bd9Sstevel@tonic-gate 	for (mp = data->cd_mp; mp != NULL && offset >= MBLKL(mp);
11207c478bd9Sstevel@tonic-gate 	    offset -= MBLKL(mp), mp = mp->b_cont);
11217c478bd9Sstevel@tonic-gate 	if (mp == NULL) {
11227c478bd9Sstevel@tonic-gate 		/*
11237c478bd9Sstevel@tonic-gate 		 * The caller specified an offset that is larger than the
11247c478bd9Sstevel@tonic-gate 		 * total size of the buffers it provided.
11257c478bd9Sstevel@tonic-gate 		 */
11267c478bd9Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
11277c478bd9Sstevel@tonic-gate 	}
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 	/*
11307c478bd9Sstevel@tonic-gate 	 * Now do the digesting on the mblk chain.
11317c478bd9Sstevel@tonic-gate 	 */
11327c478bd9Sstevel@tonic-gate 	while (mp != NULL && length > 0) {
11337c478bd9Sstevel@tonic-gate 		cur_len = MIN(MBLKL(mp) - offset, length);
11347c478bd9Sstevel@tonic-gate 		MD5Update(md5_ctx, mp->b_rptr + offset, cur_len);
11357c478bd9Sstevel@tonic-gate 		length -= cur_len;
11367c478bd9Sstevel@tonic-gate 		offset = 0;
11377c478bd9Sstevel@tonic-gate 		mp = mp->b_cont;
11387c478bd9Sstevel@tonic-gate 	}
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate 	if (mp == NULL && length > 0) {
11417c478bd9Sstevel@tonic-gate 		/*
11427c478bd9Sstevel@tonic-gate 		 * The end of the mblk was reached but the length requested
11437c478bd9Sstevel@tonic-gate 		 * could not be processed, i.e. The caller requested
11447c478bd9Sstevel@tonic-gate 		 * to digest more data than it provided.
11457c478bd9Sstevel@tonic-gate 		 */
11467c478bd9Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
11477c478bd9Sstevel@tonic-gate 	}
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
11507c478bd9Sstevel@tonic-gate }
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate /*
11537c478bd9Sstevel@tonic-gate  * Helper MD5 digest final for mblk's.
11547c478bd9Sstevel@tonic-gate  * digest_len is the length of the desired digest. If digest_len
11557c478bd9Sstevel@tonic-gate  * is smaller than the default MD5 digest length, the caller
11567c478bd9Sstevel@tonic-gate  * must pass a scratch buffer, digest_scratch, which must
11577c478bd9Sstevel@tonic-gate  * be at least MD5_DIGEST_LENGTH bytes.
11587c478bd9Sstevel@tonic-gate  */
11597c478bd9Sstevel@tonic-gate static int
11607c478bd9Sstevel@tonic-gate md5_digest_final_mblk(MD5_CTX *md5_ctx, crypto_data_t *digest,
11617c478bd9Sstevel@tonic-gate     ulong_t digest_len, uchar_t *digest_scratch)
11627c478bd9Sstevel@tonic-gate {
11637c478bd9Sstevel@tonic-gate 	off_t offset = digest->cd_offset;
11647c478bd9Sstevel@tonic-gate 	mblk_t *mp;
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate 	/*
11677c478bd9Sstevel@tonic-gate 	 * Jump to the first mblk_t that will be used to store the digest.
11687c478bd9Sstevel@tonic-gate 	 */
11697c478bd9Sstevel@tonic-gate 	for (mp = digest->cd_mp; mp != NULL && offset >= MBLKL(mp);
11707c478bd9Sstevel@tonic-gate 	    offset -= MBLKL(mp), mp = mp->b_cont);
11717c478bd9Sstevel@tonic-gate 	if (mp == NULL) {
11727c478bd9Sstevel@tonic-gate 		/*
11737c478bd9Sstevel@tonic-gate 		 * The caller specified an offset that is larger than the
11747c478bd9Sstevel@tonic-gate 		 * total size of the buffers it provided.
11757c478bd9Sstevel@tonic-gate 		 */
11767c478bd9Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
11777c478bd9Sstevel@tonic-gate 	}
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 	if (offset + digest_len <= MBLKL(mp)) {
11807c478bd9Sstevel@tonic-gate 		/*
11817c478bd9Sstevel@tonic-gate 		 * The computed MD5 digest will fit in the current mblk.
11827c478bd9Sstevel@tonic-gate 		 * Do the MD5Final() in-place.
11837c478bd9Sstevel@tonic-gate 		 */
11847c478bd9Sstevel@tonic-gate 		if (digest_len != MD5_DIGEST_LENGTH) {
11857c478bd9Sstevel@tonic-gate 			/*
11867c478bd9Sstevel@tonic-gate 			 * The caller requested a short digest. Digest
11877c478bd9Sstevel@tonic-gate 			 * into a scratch buffer and return to
11887c478bd9Sstevel@tonic-gate 			 * the user only what was requested.
11897c478bd9Sstevel@tonic-gate 			 */
11907c478bd9Sstevel@tonic-gate 			MD5Final(digest_scratch, md5_ctx);
11917c478bd9Sstevel@tonic-gate 			bcopy(digest_scratch, mp->b_rptr + offset, digest_len);
11927c478bd9Sstevel@tonic-gate 		} else {
11937c478bd9Sstevel@tonic-gate 			MD5Final(mp->b_rptr + offset, md5_ctx);
11947c478bd9Sstevel@tonic-gate 		}
11957c478bd9Sstevel@tonic-gate 	} else {
11967c478bd9Sstevel@tonic-gate 		/*
11977c478bd9Sstevel@tonic-gate 		 * The computed digest will be crossing one or more mblk's.
11987c478bd9Sstevel@tonic-gate 		 * This is bad performance-wise but we need to support it.
11997c478bd9Sstevel@tonic-gate 		 * Allocate a small scratch buffer on the stack and
12007c478bd9Sstevel@tonic-gate 		 * copy it piece meal to the specified digest iovec's.
12017c478bd9Sstevel@tonic-gate 		 */
12027c478bd9Sstevel@tonic-gate 		uchar_t digest_tmp[MD5_DIGEST_LENGTH];
12037c478bd9Sstevel@tonic-gate 		off_t scratch_offset = 0;
12047c478bd9Sstevel@tonic-gate 		size_t length = digest_len;
12057c478bd9Sstevel@tonic-gate 		size_t cur_len;
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 		MD5Final(digest_tmp, md5_ctx);
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate 		while (mp != NULL && length > 0) {
12107c478bd9Sstevel@tonic-gate 			cur_len = MIN(MBLKL(mp) - offset, length);
12117c478bd9Sstevel@tonic-gate 			bcopy(digest_tmp + scratch_offset,
12127c478bd9Sstevel@tonic-gate 			    mp->b_rptr + offset, cur_len);
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 			length -= cur_len;
12157c478bd9Sstevel@tonic-gate 			mp = mp->b_cont;
12167c478bd9Sstevel@tonic-gate 			scratch_offset += cur_len;
12177c478bd9Sstevel@tonic-gate 			offset = 0;
12187c478bd9Sstevel@tonic-gate 		}
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 		if (mp == NULL && length > 0) {
12217c478bd9Sstevel@tonic-gate 			/*
12227c478bd9Sstevel@tonic-gate 			 * The end of the specified mblk 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 /* ARGSUSED */
12357c478bd9Sstevel@tonic-gate static int
12367c478bd9Sstevel@tonic-gate md5_digest(crypto_ctx_t *ctx, crypto_data_t *data, crypto_data_t *digest,
12377c478bd9Sstevel@tonic-gate     crypto_req_handle_t req)
12387c478bd9Sstevel@tonic-gate {
12397c478bd9Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate 	/*
12447c478bd9Sstevel@tonic-gate 	 * We need to just return the length needed to store the output.
12457c478bd9Sstevel@tonic-gate 	 * We should not destroy the context for the following cases.
12467c478bd9Sstevel@tonic-gate 	 */
12477c478bd9Sstevel@tonic-gate 	if ((digest->cd_length == 0) ||
12487c478bd9Sstevel@tonic-gate 	    (digest->cd_length < MD5_DIGEST_LENGTH)) {
12497c478bd9Sstevel@tonic-gate 		digest->cd_length = MD5_DIGEST_LENGTH;
12507c478bd9Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
12517c478bd9Sstevel@tonic-gate 	}
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 	/*
12547c478bd9Sstevel@tonic-gate 	 * Do the MD5 update on the specified input data.
12557c478bd9Sstevel@tonic-gate 	 */
12567c478bd9Sstevel@tonic-gate 	switch (data->cd_format) {
12577c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
12587c478bd9Sstevel@tonic-gate 		MD5Update(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
12597c478bd9Sstevel@tonic-gate 		    data->cd_raw.iov_base + data->cd_offset,
12607c478bd9Sstevel@tonic-gate 		    data->cd_length);
12617c478bd9Sstevel@tonic-gate 		break;
12627c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
12637c478bd9Sstevel@tonic-gate 		ret = md5_digest_update_uio(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
12647c478bd9Sstevel@tonic-gate 		    data);
12657c478bd9Sstevel@tonic-gate 		break;
12667c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
12677c478bd9Sstevel@tonic-gate 		ret = md5_digest_update_mblk(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
12687c478bd9Sstevel@tonic-gate 		    data);
12697c478bd9Sstevel@tonic-gate 		break;
12707c478bd9Sstevel@tonic-gate 	default:
12717c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
12727c478bd9Sstevel@tonic-gate 	}
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate 	if (ret != CRYPTO_SUCCESS) {
12757c478bd9Sstevel@tonic-gate 		/* the update failed, free context and bail */
12767c478bd9Sstevel@tonic-gate 		kmem_free(ctx->cc_provider_private, sizeof (md5_ctx_t));
12777c478bd9Sstevel@tonic-gate 		ctx->cc_provider_private = NULL;
12787c478bd9Sstevel@tonic-gate 		digest->cd_length = 0;
12797c478bd9Sstevel@tonic-gate 		return (ret);
12807c478bd9Sstevel@tonic-gate 	}
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate 	/*
12837c478bd9Sstevel@tonic-gate 	 * Do an MD5 final, must be done separately since the digest
12847c478bd9Sstevel@tonic-gate 	 * type can be different than the input data type.
12857c478bd9Sstevel@tonic-gate 	 */
12867c478bd9Sstevel@tonic-gate 	switch (digest->cd_format) {
12877c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
12887c478bd9Sstevel@tonic-gate 		MD5Final((unsigned char *)digest->cd_raw.iov_base +
12897c478bd9Sstevel@tonic-gate 		    digest->cd_offset, &PROV_MD5_CTX(ctx)->mc_md5_ctx);
12907c478bd9Sstevel@tonic-gate 		break;
12917c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
12927c478bd9Sstevel@tonic-gate 		ret = md5_digest_final_uio(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
12937c478bd9Sstevel@tonic-gate 		    digest, MD5_DIGEST_LENGTH, NULL);
12947c478bd9Sstevel@tonic-gate 		break;
12957c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
12967c478bd9Sstevel@tonic-gate 		ret = md5_digest_final_mblk(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
12977c478bd9Sstevel@tonic-gate 		    digest, MD5_DIGEST_LENGTH, NULL);
12987c478bd9Sstevel@tonic-gate 		break;
12997c478bd9Sstevel@tonic-gate 	default:
13007c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
13017c478bd9Sstevel@tonic-gate 	}
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate 	/* all done, free context and return */
13047c478bd9Sstevel@tonic-gate 
13057c478bd9Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
13067c478bd9Sstevel@tonic-gate 		digest->cd_length = MD5_DIGEST_LENGTH;
13077c478bd9Sstevel@tonic-gate 	} else {
13087c478bd9Sstevel@tonic-gate 		digest->cd_length = 0;
13097c478bd9Sstevel@tonic-gate 	}
13107c478bd9Sstevel@tonic-gate 
13117c478bd9Sstevel@tonic-gate 	kmem_free(ctx->cc_provider_private, sizeof (md5_ctx_t));
13127c478bd9Sstevel@tonic-gate 	ctx->cc_provider_private = NULL;
13137c478bd9Sstevel@tonic-gate 	return (ret);
13147c478bd9Sstevel@tonic-gate }
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate /* ARGSUSED */
13177c478bd9Sstevel@tonic-gate static int
13187c478bd9Sstevel@tonic-gate md5_digest_update(crypto_ctx_t *ctx, crypto_data_t *data,
13197c478bd9Sstevel@tonic-gate     crypto_req_handle_t req)
13207c478bd9Sstevel@tonic-gate {
13217c478bd9Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
13227c478bd9Sstevel@tonic-gate 
13237c478bd9Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
13247c478bd9Sstevel@tonic-gate 
13257c478bd9Sstevel@tonic-gate 	/*
13267c478bd9Sstevel@tonic-gate 	 * Do the MD5 update on the specified input data.
13277c478bd9Sstevel@tonic-gate 	 */
13287c478bd9Sstevel@tonic-gate 	switch (data->cd_format) {
13297c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
13307c478bd9Sstevel@tonic-gate 		MD5Update(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
13317c478bd9Sstevel@tonic-gate 		    data->cd_raw.iov_base + data->cd_offset,
13327c478bd9Sstevel@tonic-gate 		    data->cd_length);
13337c478bd9Sstevel@tonic-gate 		break;
13347c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
13357c478bd9Sstevel@tonic-gate 		ret = md5_digest_update_uio(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
13367c478bd9Sstevel@tonic-gate 		    data);
13377c478bd9Sstevel@tonic-gate 		break;
13387c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
13397c478bd9Sstevel@tonic-gate 		ret = md5_digest_update_mblk(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
13407c478bd9Sstevel@tonic-gate 		    data);
13417c478bd9Sstevel@tonic-gate 		break;
13427c478bd9Sstevel@tonic-gate 	default:
13437c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
13447c478bd9Sstevel@tonic-gate 	}
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate 	return (ret);
13477c478bd9Sstevel@tonic-gate }
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate /* ARGSUSED */
13507c478bd9Sstevel@tonic-gate static int
13517c478bd9Sstevel@tonic-gate md5_digest_final(crypto_ctx_t *ctx, crypto_data_t *digest,
13527c478bd9Sstevel@tonic-gate     crypto_req_handle_t req)
13537c478bd9Sstevel@tonic-gate {
13547c478bd9Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
13557c478bd9Sstevel@tonic-gate 
13567c478bd9Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate 	/*
13597c478bd9Sstevel@tonic-gate 	 * We need to just return the length needed to store the output.
13607c478bd9Sstevel@tonic-gate 	 * We should not destroy the context for the following cases.
13617c478bd9Sstevel@tonic-gate 	 */
13627c478bd9Sstevel@tonic-gate 	if ((digest->cd_length == 0) ||
13637c478bd9Sstevel@tonic-gate 	    (digest->cd_length < MD5_DIGEST_LENGTH)) {
13647c478bd9Sstevel@tonic-gate 		digest->cd_length = MD5_DIGEST_LENGTH;
13657c478bd9Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
13667c478bd9Sstevel@tonic-gate 	}
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate 	/*
13697c478bd9Sstevel@tonic-gate 	 * Do an MD5 final.
13707c478bd9Sstevel@tonic-gate 	 */
13717c478bd9Sstevel@tonic-gate 	switch (digest->cd_format) {
13727c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
13737c478bd9Sstevel@tonic-gate 		MD5Final((unsigned char *)digest->cd_raw.iov_base +
13747c478bd9Sstevel@tonic-gate 		    digest->cd_offset, &PROV_MD5_CTX(ctx)->mc_md5_ctx);
13757c478bd9Sstevel@tonic-gate 		break;
13767c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
13777c478bd9Sstevel@tonic-gate 		ret = md5_digest_final_uio(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
13787c478bd9Sstevel@tonic-gate 		    digest, MD5_DIGEST_LENGTH, NULL);
13797c478bd9Sstevel@tonic-gate 		break;
13807c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
13817c478bd9Sstevel@tonic-gate 		ret = md5_digest_final_mblk(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
13827c478bd9Sstevel@tonic-gate 		    digest, MD5_DIGEST_LENGTH, NULL);
13837c478bd9Sstevel@tonic-gate 		break;
13847c478bd9Sstevel@tonic-gate 	default:
13857c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
13867c478bd9Sstevel@tonic-gate 	}
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 	/* all done, free context and return */
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
13917c478bd9Sstevel@tonic-gate 		digest->cd_length = MD5_DIGEST_LENGTH;
13927c478bd9Sstevel@tonic-gate 	} else {
13937c478bd9Sstevel@tonic-gate 		digest->cd_length = 0;
13947c478bd9Sstevel@tonic-gate 	}
13957c478bd9Sstevel@tonic-gate 
13967c478bd9Sstevel@tonic-gate 	kmem_free(ctx->cc_provider_private, sizeof (md5_ctx_t));
13977c478bd9Sstevel@tonic-gate 	ctx->cc_provider_private = NULL;
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate 	return (ret);
14007c478bd9Sstevel@tonic-gate }
14017c478bd9Sstevel@tonic-gate 
14027c478bd9Sstevel@tonic-gate /* ARGSUSED */
14037c478bd9Sstevel@tonic-gate static int
14047c478bd9Sstevel@tonic-gate md5_digest_atomic(crypto_provider_handle_t provider,
14057c478bd9Sstevel@tonic-gate     crypto_session_id_t session_id, crypto_mechanism_t *mechanism,
14067c478bd9Sstevel@tonic-gate     crypto_data_t *data, crypto_data_t *digest,
14077c478bd9Sstevel@tonic-gate     crypto_req_handle_t req)
14087c478bd9Sstevel@tonic-gate {
14097c478bd9Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
14107c478bd9Sstevel@tonic-gate 	MD5_CTX md5_ctx;
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate 	if (mechanism->cm_type != MD5_MECH_INFO_TYPE)
14137c478bd9Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate 	/*
14167c478bd9Sstevel@tonic-gate 	 * Do the MD5 init.
14177c478bd9Sstevel@tonic-gate 	 */
14187c478bd9Sstevel@tonic-gate 	MD5Init(&md5_ctx);
14197c478bd9Sstevel@tonic-gate 
14207c478bd9Sstevel@tonic-gate 	/*
14217c478bd9Sstevel@tonic-gate 	 * Do the MD5 update on the specified input data.
14227c478bd9Sstevel@tonic-gate 	 */
14237c478bd9Sstevel@tonic-gate 	switch (data->cd_format) {
14247c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
14257c478bd9Sstevel@tonic-gate 		MD5Update(&md5_ctx, data->cd_raw.iov_base + data->cd_offset,
14267c478bd9Sstevel@tonic-gate 		    data->cd_length);
14277c478bd9Sstevel@tonic-gate 		break;
14287c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
14297c478bd9Sstevel@tonic-gate 		ret = md5_digest_update_uio(&md5_ctx, data);
14307c478bd9Sstevel@tonic-gate 		break;
14317c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
14327c478bd9Sstevel@tonic-gate 		ret = md5_digest_update_mblk(&md5_ctx, data);
14337c478bd9Sstevel@tonic-gate 		break;
14347c478bd9Sstevel@tonic-gate 	default:
14357c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
14367c478bd9Sstevel@tonic-gate 	}
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate 	if (ret != CRYPTO_SUCCESS) {
14397c478bd9Sstevel@tonic-gate 		/* the update failed, bail */
14407c478bd9Sstevel@tonic-gate 		digest->cd_length = 0;
14417c478bd9Sstevel@tonic-gate 		return (ret);
14427c478bd9Sstevel@tonic-gate 	}
14437c478bd9Sstevel@tonic-gate 
14447c478bd9Sstevel@tonic-gate 	/*
14457c478bd9Sstevel@tonic-gate 	 * Do an MD5 final, must be done separately since the digest
14467c478bd9Sstevel@tonic-gate 	 * type can be different than the input data type.
14477c478bd9Sstevel@tonic-gate 	 */
14487c478bd9Sstevel@tonic-gate 	switch (digest->cd_format) {
14497c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
14507c478bd9Sstevel@tonic-gate 		MD5Final((unsigned char *)digest->cd_raw.iov_base +
14517c478bd9Sstevel@tonic-gate 		    digest->cd_offset, &md5_ctx);
14527c478bd9Sstevel@tonic-gate 		break;
14537c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
14547c478bd9Sstevel@tonic-gate 		ret = md5_digest_final_uio(&md5_ctx, digest,
14557c478bd9Sstevel@tonic-gate 		    MD5_DIGEST_LENGTH, NULL);
14567c478bd9Sstevel@tonic-gate 		break;
14577c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
14587c478bd9Sstevel@tonic-gate 		ret = md5_digest_final_mblk(&md5_ctx, digest,
14597c478bd9Sstevel@tonic-gate 		    MD5_DIGEST_LENGTH, NULL);
14607c478bd9Sstevel@tonic-gate 		break;
14617c478bd9Sstevel@tonic-gate 	default:
14627c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
14637c478bd9Sstevel@tonic-gate 	}
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
14667c478bd9Sstevel@tonic-gate 		digest->cd_length = MD5_DIGEST_LENGTH;
14677c478bd9Sstevel@tonic-gate 	} else {
14687c478bd9Sstevel@tonic-gate 		digest->cd_length = 0;
14697c478bd9Sstevel@tonic-gate 	}
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate 	return (ret);
14727c478bd9Sstevel@tonic-gate }
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate /*
14757c478bd9Sstevel@tonic-gate  * KCF software provider mac entry points.
14767c478bd9Sstevel@tonic-gate  *
14777c478bd9Sstevel@tonic-gate  * MD5 HMAC is: MD5(key XOR opad, MD5(key XOR ipad, text))
14787c478bd9Sstevel@tonic-gate  *
14797c478bd9Sstevel@tonic-gate  * Init:
14807c478bd9Sstevel@tonic-gate  * The initialization routine initializes what we denote
14817c478bd9Sstevel@tonic-gate  * as the inner and outer contexts by doing
14827c478bd9Sstevel@tonic-gate  * - for inner context: MD5(key XOR ipad)
14837c478bd9Sstevel@tonic-gate  * - for outer context: MD5(key XOR opad)
14847c478bd9Sstevel@tonic-gate  *
14857c478bd9Sstevel@tonic-gate  * Update:
14867c478bd9Sstevel@tonic-gate  * Each subsequent MD5 HMAC update will result in an
14877c478bd9Sstevel@tonic-gate  * update of the inner context with the specified data.
14887c478bd9Sstevel@tonic-gate  *
14897c478bd9Sstevel@tonic-gate  * Final:
14907c478bd9Sstevel@tonic-gate  * The MD5 HMAC final will do a MD5 final operation on the
14917c478bd9Sstevel@tonic-gate  * inner context, and the resulting digest will be used
14927c478bd9Sstevel@tonic-gate  * as the data for an update on the outer context. Last
14937c478bd9Sstevel@tonic-gate  * but not least, an MD5 final on the outer context will
14947c478bd9Sstevel@tonic-gate  * be performed to obtain the MD5 HMAC digest to return
14957c478bd9Sstevel@tonic-gate  * to the user.
14967c478bd9Sstevel@tonic-gate  */
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate /*
14997c478bd9Sstevel@tonic-gate  * Initialize a MD5-HMAC context.
15007c478bd9Sstevel@tonic-gate  */
15017c478bd9Sstevel@tonic-gate static void
15027c478bd9Sstevel@tonic-gate md5_mac_init_ctx(md5_hmac_ctx_t *ctx, void *keyval, uint_t length_in_bytes)
15037c478bd9Sstevel@tonic-gate {
15047c478bd9Sstevel@tonic-gate 	uint32_t ipad[MD5_HMAC_INTS_PER_BLOCK];
15057c478bd9Sstevel@tonic-gate 	uint32_t opad[MD5_HMAC_INTS_PER_BLOCK];
15067c478bd9Sstevel@tonic-gate 	uint_t i;
15077c478bd9Sstevel@tonic-gate 
15087c478bd9Sstevel@tonic-gate 	bzero(ipad, MD5_HMAC_BLOCK_SIZE);
15097c478bd9Sstevel@tonic-gate 	bzero(opad, MD5_HMAC_BLOCK_SIZE);
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate 	bcopy(keyval, ipad, length_in_bytes);
15127c478bd9Sstevel@tonic-gate 	bcopy(keyval, opad, length_in_bytes);
15137c478bd9Sstevel@tonic-gate 
15147c478bd9Sstevel@tonic-gate 	/* XOR key with ipad (0x36) and opad (0x5c) */
15157c478bd9Sstevel@tonic-gate 	for (i = 0; i < MD5_HMAC_INTS_PER_BLOCK; i++) {
15167c478bd9Sstevel@tonic-gate 		ipad[i] ^= 0x36363636;
15177c478bd9Sstevel@tonic-gate 		opad[i] ^= 0x5c5c5c5c;
15187c478bd9Sstevel@tonic-gate 	}
15197c478bd9Sstevel@tonic-gate 
15207c478bd9Sstevel@tonic-gate 	/* perform MD5 on ipad */
15217c478bd9Sstevel@tonic-gate 	MD5Init(&ctx->hc_icontext);
15227c478bd9Sstevel@tonic-gate 	MD5Update(&ctx->hc_icontext, ipad, MD5_HMAC_BLOCK_SIZE);
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate 	/* perform MD5 on opad */
15257c478bd9Sstevel@tonic-gate 	MD5Init(&ctx->hc_ocontext);
15267c478bd9Sstevel@tonic-gate 	MD5Update(&ctx->hc_ocontext, opad, MD5_HMAC_BLOCK_SIZE);
15277c478bd9Sstevel@tonic-gate }
15287c478bd9Sstevel@tonic-gate 
15297c478bd9Sstevel@tonic-gate /*
15307c478bd9Sstevel@tonic-gate  * Initializes a multi-part MAC operation.
15317c478bd9Sstevel@tonic-gate  */
15327c478bd9Sstevel@tonic-gate static int
15337c478bd9Sstevel@tonic-gate md5_mac_init(crypto_ctx_t *ctx, crypto_mechanism_t *mechanism,
15347c478bd9Sstevel@tonic-gate     crypto_key_t *key, crypto_spi_ctx_template_t ctx_template,
15357c478bd9Sstevel@tonic-gate     crypto_req_handle_t req)
15367c478bd9Sstevel@tonic-gate {
15377c478bd9Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
15387c478bd9Sstevel@tonic-gate 	uint_t keylen_in_bytes = CRYPTO_BITS2BYTES(key->ck_length);
15397c478bd9Sstevel@tonic-gate 
15407c478bd9Sstevel@tonic-gate 	if (mechanism->cm_type != MD5_HMAC_MECH_INFO_TYPE &&
15417c478bd9Sstevel@tonic-gate 	    mechanism->cm_type != MD5_HMAC_GEN_MECH_INFO_TYPE)
15427c478bd9Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
15437c478bd9Sstevel@tonic-gate 
15447c478bd9Sstevel@tonic-gate 	/* Add support for key by attributes (RFE 4706552) */
15457c478bd9Sstevel@tonic-gate 	if (key->ck_format != CRYPTO_KEY_RAW)
15467c478bd9Sstevel@tonic-gate 		return (CRYPTO_ARGUMENTS_BAD);
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate 	ctx->cc_provider_private = kmem_alloc(sizeof (md5_hmac_ctx_t),
15497c478bd9Sstevel@tonic-gate 	    crypto_kmflag(req));
15507c478bd9Sstevel@tonic-gate 	if (ctx->cc_provider_private == NULL)
15517c478bd9Sstevel@tonic-gate 		return (CRYPTO_HOST_MEMORY);
15527c478bd9Sstevel@tonic-gate 
15537c478bd9Sstevel@tonic-gate 	if (ctx_template != NULL) {
15547c478bd9Sstevel@tonic-gate 		/* reuse context template */
15557c478bd9Sstevel@tonic-gate 		bcopy(ctx_template, PROV_MD5_HMAC_CTX(ctx),
15567c478bd9Sstevel@tonic-gate 		    sizeof (md5_hmac_ctx_t));
15577c478bd9Sstevel@tonic-gate 	} else {
15587c478bd9Sstevel@tonic-gate 		/* no context template, compute context */
15597c478bd9Sstevel@tonic-gate 		if (keylen_in_bytes > MD5_HMAC_BLOCK_SIZE) {
15607c478bd9Sstevel@tonic-gate 			uchar_t digested_key[MD5_DIGEST_LENGTH];
15617c478bd9Sstevel@tonic-gate 			md5_hmac_ctx_t *hmac_ctx = ctx->cc_provider_private;
15627c478bd9Sstevel@tonic-gate 
15637c478bd9Sstevel@tonic-gate 			/*
15647c478bd9Sstevel@tonic-gate 			 * Hash the passed-in key to get a smaller key.
15657c478bd9Sstevel@tonic-gate 			 * The inner context is used since it hasn't been
15667c478bd9Sstevel@tonic-gate 			 * initialized yet.
15677c478bd9Sstevel@tonic-gate 			 */
15687c478bd9Sstevel@tonic-gate 			PROV_MD5_DIGEST_KEY(&hmac_ctx->hc_icontext,
15697c478bd9Sstevel@tonic-gate 			    key->ck_data, keylen_in_bytes, digested_key);
15707c478bd9Sstevel@tonic-gate 			md5_mac_init_ctx(PROV_MD5_HMAC_CTX(ctx),
15717c478bd9Sstevel@tonic-gate 			    digested_key, MD5_DIGEST_LENGTH);
15727c478bd9Sstevel@tonic-gate 		} else {
15737c478bd9Sstevel@tonic-gate 			md5_mac_init_ctx(PROV_MD5_HMAC_CTX(ctx),
15747c478bd9Sstevel@tonic-gate 			    key->ck_data, keylen_in_bytes);
15757c478bd9Sstevel@tonic-gate 		}
15767c478bd9Sstevel@tonic-gate 	}
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate 	/*
15797c478bd9Sstevel@tonic-gate 	 * Get the mechanism parameters, if applicable.
15807c478bd9Sstevel@tonic-gate 	 */
15817c478bd9Sstevel@tonic-gate 	PROV_MD5_HMAC_CTX(ctx)->hc_mech_type = mechanism->cm_type;
15827c478bd9Sstevel@tonic-gate 	if (mechanism->cm_type == MD5_HMAC_GEN_MECH_INFO_TYPE) {
15837c478bd9Sstevel@tonic-gate 		if (mechanism->cm_param == NULL ||
15847c478bd9Sstevel@tonic-gate 		    mechanism->cm_param_len != sizeof (ulong_t))
15857c478bd9Sstevel@tonic-gate 			ret = CRYPTO_MECHANISM_PARAM_INVALID;
15867c478bd9Sstevel@tonic-gate 		PROV_MD5_GET_DIGEST_LEN(mechanism,
15877c478bd9Sstevel@tonic-gate 		    PROV_MD5_HMAC_CTX(ctx)->hc_digest_len);
15887c478bd9Sstevel@tonic-gate 		if (PROV_MD5_HMAC_CTX(ctx)->hc_digest_len >
15897c478bd9Sstevel@tonic-gate 		    MD5_DIGEST_LENGTH)
15907c478bd9Sstevel@tonic-gate 			ret = CRYPTO_MECHANISM_PARAM_INVALID;
15917c478bd9Sstevel@tonic-gate 	}
15927c478bd9Sstevel@tonic-gate 
15937c478bd9Sstevel@tonic-gate 	if (ret != CRYPTO_SUCCESS) {
15947c478bd9Sstevel@tonic-gate 		bzero(ctx->cc_provider_private, sizeof (md5_hmac_ctx_t));
15957c478bd9Sstevel@tonic-gate 		kmem_free(ctx->cc_provider_private, sizeof (md5_hmac_ctx_t));
15967c478bd9Sstevel@tonic-gate 		ctx->cc_provider_private = NULL;
15977c478bd9Sstevel@tonic-gate 	}
15987c478bd9Sstevel@tonic-gate 
15997c478bd9Sstevel@tonic-gate 	return (ret);
16007c478bd9Sstevel@tonic-gate }
16017c478bd9Sstevel@tonic-gate 
16027c478bd9Sstevel@tonic-gate 
16037c478bd9Sstevel@tonic-gate /* ARGSUSED */
16047c478bd9Sstevel@tonic-gate static int
16057c478bd9Sstevel@tonic-gate md5_mac_update(crypto_ctx_t *ctx, crypto_data_t *data, crypto_req_handle_t req)
16067c478bd9Sstevel@tonic-gate {
16077c478bd9Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate 	/*
16127c478bd9Sstevel@tonic-gate 	 * Do an MD5 update of the inner context using the specified
16137c478bd9Sstevel@tonic-gate 	 * data.
16147c478bd9Sstevel@tonic-gate 	 */
16157c478bd9Sstevel@tonic-gate 	switch (data->cd_format) {
16167c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
16177c478bd9Sstevel@tonic-gate 		MD5Update(&PROV_MD5_HMAC_CTX(ctx)->hc_icontext,
16187c478bd9Sstevel@tonic-gate 		    data->cd_raw.iov_base + data->cd_offset,
16197c478bd9Sstevel@tonic-gate 		    data->cd_length);
16207c478bd9Sstevel@tonic-gate 		break;
16217c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
16227c478bd9Sstevel@tonic-gate 		ret = md5_digest_update_uio(
16237c478bd9Sstevel@tonic-gate 		    &PROV_MD5_HMAC_CTX(ctx)->hc_icontext, data);
16247c478bd9Sstevel@tonic-gate 		break;
16257c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
16267c478bd9Sstevel@tonic-gate 		ret = md5_digest_update_mblk(
16277c478bd9Sstevel@tonic-gate 		    &PROV_MD5_HMAC_CTX(ctx)->hc_icontext, data);
16287c478bd9Sstevel@tonic-gate 		break;
16297c478bd9Sstevel@tonic-gate 	default:
16307c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
16317c478bd9Sstevel@tonic-gate 	}
16327c478bd9Sstevel@tonic-gate 
16337c478bd9Sstevel@tonic-gate 	return (ret);
16347c478bd9Sstevel@tonic-gate }
16357c478bd9Sstevel@tonic-gate 
16367c478bd9Sstevel@tonic-gate /* ARGSUSED */
16377c478bd9Sstevel@tonic-gate static int
16387c478bd9Sstevel@tonic-gate md5_mac_final(crypto_ctx_t *ctx, crypto_data_t *mac, crypto_req_handle_t req)
16397c478bd9Sstevel@tonic-gate {
16407c478bd9Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
16417c478bd9Sstevel@tonic-gate 	uchar_t digest[MD5_DIGEST_LENGTH];
16427c478bd9Sstevel@tonic-gate 	uint32_t digest_len = MD5_DIGEST_LENGTH;
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
16457c478bd9Sstevel@tonic-gate 
16467c478bd9Sstevel@tonic-gate 	if (PROV_MD5_HMAC_CTX(ctx)->hc_mech_type == MD5_HMAC_GEN_MECH_INFO_TYPE)
16477c478bd9Sstevel@tonic-gate 	    digest_len = PROV_MD5_HMAC_CTX(ctx)->hc_digest_len;
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate 	/*
16507c478bd9Sstevel@tonic-gate 	 * We need to just return the length needed to store the output.
16517c478bd9Sstevel@tonic-gate 	 * We should not destroy the context for the following cases.
16527c478bd9Sstevel@tonic-gate 	 */
16537c478bd9Sstevel@tonic-gate 	if ((mac->cd_length == 0) || (mac->cd_length < digest_len)) {
16547c478bd9Sstevel@tonic-gate 		mac->cd_length = digest_len;
16557c478bd9Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
16567c478bd9Sstevel@tonic-gate 	}
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate 	/*
16597c478bd9Sstevel@tonic-gate 	 * Do an MD5 final on the inner context.
16607c478bd9Sstevel@tonic-gate 	 */
16617c478bd9Sstevel@tonic-gate 	MD5Final(digest, &PROV_MD5_HMAC_CTX(ctx)->hc_icontext);
16627c478bd9Sstevel@tonic-gate 
16637c478bd9Sstevel@tonic-gate 	/*
16647c478bd9Sstevel@tonic-gate 	 * Do an MD5 update on the outer context, feeding the inner
16657c478bd9Sstevel@tonic-gate 	 * digest as data.
16667c478bd9Sstevel@tonic-gate 	 */
16677c478bd9Sstevel@tonic-gate 	MD5Update(&PROV_MD5_HMAC_CTX(ctx)->hc_ocontext, digest,
16687c478bd9Sstevel@tonic-gate 	    MD5_DIGEST_LENGTH);
16697c478bd9Sstevel@tonic-gate 
16707c478bd9Sstevel@tonic-gate 	/*
16717c478bd9Sstevel@tonic-gate 	 * Do an MD5 final on the outer context, storing the computing
16727c478bd9Sstevel@tonic-gate 	 * digest in the users buffer.
16737c478bd9Sstevel@tonic-gate 	 */
16747c478bd9Sstevel@tonic-gate 	switch (mac->cd_format) {
16757c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
16767c478bd9Sstevel@tonic-gate 		if (digest_len != MD5_DIGEST_LENGTH) {
16777c478bd9Sstevel@tonic-gate 			/*
16787c478bd9Sstevel@tonic-gate 			 * The caller requested a short digest. Digest
16797c478bd9Sstevel@tonic-gate 			 * into a scratch buffer and return to
16807c478bd9Sstevel@tonic-gate 			 * the user only what was requested.
16817c478bd9Sstevel@tonic-gate 			 */
16827c478bd9Sstevel@tonic-gate 			MD5Final(digest,
16837c478bd9Sstevel@tonic-gate 			    &PROV_MD5_HMAC_CTX(ctx)->hc_ocontext);
16847c478bd9Sstevel@tonic-gate 			bcopy(digest, (unsigned char *)mac->cd_raw.iov_base +
16857c478bd9Sstevel@tonic-gate 			    mac->cd_offset, digest_len);
16867c478bd9Sstevel@tonic-gate 		} else {
16877c478bd9Sstevel@tonic-gate 			MD5Final((unsigned char *)mac->cd_raw.iov_base +
16887c478bd9Sstevel@tonic-gate 			    mac->cd_offset,
16897c478bd9Sstevel@tonic-gate 			    &PROV_MD5_HMAC_CTX(ctx)->hc_ocontext);
16907c478bd9Sstevel@tonic-gate 		}
16917c478bd9Sstevel@tonic-gate 		break;
16927c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
16937c478bd9Sstevel@tonic-gate 		ret = md5_digest_final_uio(
16947c478bd9Sstevel@tonic-gate 		    &PROV_MD5_HMAC_CTX(ctx)->hc_ocontext, mac,
16957c478bd9Sstevel@tonic-gate 		    digest_len, digest);
16967c478bd9Sstevel@tonic-gate 		break;
16977c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
16987c478bd9Sstevel@tonic-gate 		ret = md5_digest_final_mblk(
16997c478bd9Sstevel@tonic-gate 		    &PROV_MD5_HMAC_CTX(ctx)->hc_ocontext, mac,
17007c478bd9Sstevel@tonic-gate 		    digest_len, digest);
17017c478bd9Sstevel@tonic-gate 		break;
17027c478bd9Sstevel@tonic-gate 	default:
17037c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
17047c478bd9Sstevel@tonic-gate 	}
17057c478bd9Sstevel@tonic-gate 
17067c478bd9Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
17077c478bd9Sstevel@tonic-gate 		mac->cd_length = digest_len;
17087c478bd9Sstevel@tonic-gate 	} else {
17097c478bd9Sstevel@tonic-gate 		mac->cd_length = 0;
17107c478bd9Sstevel@tonic-gate 	}
17117c478bd9Sstevel@tonic-gate 
17127c478bd9Sstevel@tonic-gate 	bzero(ctx->cc_provider_private, sizeof (md5_hmac_ctx_t));
17137c478bd9Sstevel@tonic-gate 	kmem_free(ctx->cc_provider_private, sizeof (md5_hmac_ctx_t));
17147c478bd9Sstevel@tonic-gate 	ctx->cc_provider_private = NULL;
17157c478bd9Sstevel@tonic-gate 
17167c478bd9Sstevel@tonic-gate 	return (ret);
17177c478bd9Sstevel@tonic-gate }
17187c478bd9Sstevel@tonic-gate 
17197c478bd9Sstevel@tonic-gate #define	MD5_MAC_UPDATE(data, ctx, ret) {				\
17207c478bd9Sstevel@tonic-gate 	switch (data->cd_format) {					\
17217c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:						\
17227c478bd9Sstevel@tonic-gate 		MD5Update(&(ctx).hc_icontext,				\
17237c478bd9Sstevel@tonic-gate 		    data->cd_raw.iov_base + data->cd_offset,		\
17247c478bd9Sstevel@tonic-gate 		    data->cd_length);					\
17257c478bd9Sstevel@tonic-gate 		break;							\
17267c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:						\
17277c478bd9Sstevel@tonic-gate 		ret = md5_digest_update_uio(&(ctx).hc_icontext,	data);	\
17287c478bd9Sstevel@tonic-gate 		break;							\
17297c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:						\
17307c478bd9Sstevel@tonic-gate 		ret = md5_digest_update_mblk(&(ctx).hc_icontext,	\
17317c478bd9Sstevel@tonic-gate 		    data);						\
17327c478bd9Sstevel@tonic-gate 		break;							\
17337c478bd9Sstevel@tonic-gate 	default:							\
17347c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;				\
17357c478bd9Sstevel@tonic-gate 	}								\
17367c478bd9Sstevel@tonic-gate }
17377c478bd9Sstevel@tonic-gate 
17387c478bd9Sstevel@tonic-gate 
17397c478bd9Sstevel@tonic-gate /* ARGSUSED */
17407c478bd9Sstevel@tonic-gate static int
17417c478bd9Sstevel@tonic-gate md5_mac_atomic(crypto_provider_handle_t provider,
17427c478bd9Sstevel@tonic-gate     crypto_session_id_t session_id, crypto_mechanism_t *mechanism,
17437c478bd9Sstevel@tonic-gate     crypto_key_t *key, crypto_data_t *data, crypto_data_t *mac,
17447c478bd9Sstevel@tonic-gate     crypto_spi_ctx_template_t ctx_template, crypto_req_handle_t req)
17457c478bd9Sstevel@tonic-gate {
17467c478bd9Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
17477c478bd9Sstevel@tonic-gate 	uchar_t digest[MD5_DIGEST_LENGTH];
17487c478bd9Sstevel@tonic-gate 	md5_hmac_ctx_t md5_hmac_ctx;
17497c478bd9Sstevel@tonic-gate 	uint32_t digest_len = MD5_DIGEST_LENGTH;
17507c478bd9Sstevel@tonic-gate 	uint_t keylen_in_bytes = CRYPTO_BITS2BYTES(key->ck_length);
17517c478bd9Sstevel@tonic-gate 
17527c478bd9Sstevel@tonic-gate 	if (mechanism->cm_type != MD5_HMAC_MECH_INFO_TYPE &&
17537c478bd9Sstevel@tonic-gate 	    mechanism->cm_type != MD5_HMAC_GEN_MECH_INFO_TYPE)
17547c478bd9Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
17557c478bd9Sstevel@tonic-gate 
17567c478bd9Sstevel@tonic-gate 	/* Add support for key by attributes (RFE 4706552) */
17577c478bd9Sstevel@tonic-gate 	if (key->ck_format != CRYPTO_KEY_RAW)
17587c478bd9Sstevel@tonic-gate 		return (CRYPTO_ARGUMENTS_BAD);
17597c478bd9Sstevel@tonic-gate 
17607c478bd9Sstevel@tonic-gate 	if (ctx_template != NULL) {
17617c478bd9Sstevel@tonic-gate 		/* reuse context template */
17627c478bd9Sstevel@tonic-gate 		bcopy(ctx_template, &md5_hmac_ctx, sizeof (md5_hmac_ctx_t));
17637c478bd9Sstevel@tonic-gate 	} else {
17647c478bd9Sstevel@tonic-gate 		/* no context template, compute context */
17657c478bd9Sstevel@tonic-gate 		if (keylen_in_bytes > MD5_HMAC_BLOCK_SIZE) {
17667c478bd9Sstevel@tonic-gate 			/*
17677c478bd9Sstevel@tonic-gate 			 * Hash the passed-in key to get a smaller key.
17687c478bd9Sstevel@tonic-gate 			 * The inner context is used since it hasn't been
17697c478bd9Sstevel@tonic-gate 			 * initialized yet.
17707c478bd9Sstevel@tonic-gate 			 */
17717c478bd9Sstevel@tonic-gate 			PROV_MD5_DIGEST_KEY(&md5_hmac_ctx.hc_icontext,
17727c478bd9Sstevel@tonic-gate 			    key->ck_data, keylen_in_bytes, digest);
17737c478bd9Sstevel@tonic-gate 			md5_mac_init_ctx(&md5_hmac_ctx, digest,
17747c478bd9Sstevel@tonic-gate 			    MD5_DIGEST_LENGTH);
17757c478bd9Sstevel@tonic-gate 		} else {
17767c478bd9Sstevel@tonic-gate 			md5_mac_init_ctx(&md5_hmac_ctx, key->ck_data,
17777c478bd9Sstevel@tonic-gate 			    keylen_in_bytes);
17787c478bd9Sstevel@tonic-gate 		}
17797c478bd9Sstevel@tonic-gate 	}
17807c478bd9Sstevel@tonic-gate 
17817c478bd9Sstevel@tonic-gate 	/*
17827c478bd9Sstevel@tonic-gate 	 * Get the mechanism parameters, if applicable.
17837c478bd9Sstevel@tonic-gate 	 */
17847c478bd9Sstevel@tonic-gate 	if (mechanism->cm_type == MD5_HMAC_GEN_MECH_INFO_TYPE) {
17857c478bd9Sstevel@tonic-gate 		if (mechanism->cm_param == NULL ||
17867c478bd9Sstevel@tonic-gate 		    mechanism->cm_param_len != sizeof (ulong_t)) {
17877c478bd9Sstevel@tonic-gate 			ret = CRYPTO_MECHANISM_PARAM_INVALID;
17887c478bd9Sstevel@tonic-gate 			goto bail;
17897c478bd9Sstevel@tonic-gate 		}
17907c478bd9Sstevel@tonic-gate 		PROV_MD5_GET_DIGEST_LEN(mechanism, digest_len);
17917c478bd9Sstevel@tonic-gate 		if (digest_len > MD5_DIGEST_LENGTH) {
17927c478bd9Sstevel@tonic-gate 			ret = CRYPTO_MECHANISM_PARAM_INVALID;
17937c478bd9Sstevel@tonic-gate 			goto bail;
17947c478bd9Sstevel@tonic-gate 		}
17957c478bd9Sstevel@tonic-gate 	}
17967c478bd9Sstevel@tonic-gate 
17977c478bd9Sstevel@tonic-gate 	/* do an MD5 update of the inner context using the specified data */
17987c478bd9Sstevel@tonic-gate 	MD5_MAC_UPDATE(data, md5_hmac_ctx, ret);
17997c478bd9Sstevel@tonic-gate 	if (ret != CRYPTO_SUCCESS)
18007c478bd9Sstevel@tonic-gate 		/* the update failed, free context and bail */
18017c478bd9Sstevel@tonic-gate 		goto bail;
18027c478bd9Sstevel@tonic-gate 
18037c478bd9Sstevel@tonic-gate 	/* do an MD5 final on the inner context */
18047c478bd9Sstevel@tonic-gate 	MD5Final(digest, &md5_hmac_ctx.hc_icontext);
18057c478bd9Sstevel@tonic-gate 
18067c478bd9Sstevel@tonic-gate 	/*
18077c478bd9Sstevel@tonic-gate 	 * Do an MD5 update on the outer context, feeding the inner
18087c478bd9Sstevel@tonic-gate 	 * digest as data.
18097c478bd9Sstevel@tonic-gate 	 */
18107c478bd9Sstevel@tonic-gate 	MD5Update(&md5_hmac_ctx.hc_ocontext, digest, MD5_DIGEST_LENGTH);
18117c478bd9Sstevel@tonic-gate 
18127c478bd9Sstevel@tonic-gate 	/*
18137c478bd9Sstevel@tonic-gate 	 * Do an MD5 final on the outer context, storing the computed
18147c478bd9Sstevel@tonic-gate 	 * digest in the users buffer.
18157c478bd9Sstevel@tonic-gate 	 */
18167c478bd9Sstevel@tonic-gate 	switch (mac->cd_format) {
18177c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
18187c478bd9Sstevel@tonic-gate 		if (digest_len != MD5_DIGEST_LENGTH) {
18197c478bd9Sstevel@tonic-gate 			/*
18207c478bd9Sstevel@tonic-gate 			 * The caller requested a short digest. Digest
18217c478bd9Sstevel@tonic-gate 			 * into a scratch buffer and return to
18227c478bd9Sstevel@tonic-gate 			 * the user only what was requested.
18237c478bd9Sstevel@tonic-gate 			 */
18247c478bd9Sstevel@tonic-gate 			MD5Final(digest, &md5_hmac_ctx.hc_ocontext);
18257c478bd9Sstevel@tonic-gate 			bcopy(digest, (unsigned char *)mac->cd_raw.iov_base +
18267c478bd9Sstevel@tonic-gate 			    mac->cd_offset, digest_len);
18277c478bd9Sstevel@tonic-gate 		} else {
18287c478bd9Sstevel@tonic-gate 			MD5Final((unsigned char *)mac->cd_raw.iov_base +
18297c478bd9Sstevel@tonic-gate 			    mac->cd_offset, &md5_hmac_ctx.hc_ocontext);
18307c478bd9Sstevel@tonic-gate 		}
18317c478bd9Sstevel@tonic-gate 		break;
18327c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
18337c478bd9Sstevel@tonic-gate 		ret = md5_digest_final_uio(&md5_hmac_ctx.hc_ocontext, mac,
18347c478bd9Sstevel@tonic-gate 		    digest_len, digest);
18357c478bd9Sstevel@tonic-gate 		break;
18367c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
18377c478bd9Sstevel@tonic-gate 		ret = md5_digest_final_mblk(&md5_hmac_ctx.hc_ocontext, mac,
18387c478bd9Sstevel@tonic-gate 		    digest_len, digest);
18397c478bd9Sstevel@tonic-gate 		break;
18407c478bd9Sstevel@tonic-gate 	default:
18417c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
18427c478bd9Sstevel@tonic-gate 	}
18437c478bd9Sstevel@tonic-gate 
18447c478bd9Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
18457c478bd9Sstevel@tonic-gate 		mac->cd_length = digest_len;
18467c478bd9Sstevel@tonic-gate 	} else {
18477c478bd9Sstevel@tonic-gate 		mac->cd_length = 0;
18487c478bd9Sstevel@tonic-gate 	}
18497c478bd9Sstevel@tonic-gate 	/* Extra paranoia: zeroizing the local context on the stack */
18507c478bd9Sstevel@tonic-gate 	bzero(&md5_hmac_ctx, sizeof (md5_hmac_ctx_t));
18517c478bd9Sstevel@tonic-gate 
18527c478bd9Sstevel@tonic-gate 	return (ret);
18537c478bd9Sstevel@tonic-gate bail:
18547c478bd9Sstevel@tonic-gate 	bzero(&md5_hmac_ctx, sizeof (md5_hmac_ctx_t));
18557c478bd9Sstevel@tonic-gate 	mac->cd_length = 0;
18567c478bd9Sstevel@tonic-gate 	return (ret);
18577c478bd9Sstevel@tonic-gate }
18587c478bd9Sstevel@tonic-gate 
18597c478bd9Sstevel@tonic-gate /* ARGSUSED */
18607c478bd9Sstevel@tonic-gate static int
18617c478bd9Sstevel@tonic-gate md5_mac_verify_atomic(crypto_provider_handle_t provider,
18627c478bd9Sstevel@tonic-gate     crypto_session_id_t session_id, crypto_mechanism_t *mechanism,
18637c478bd9Sstevel@tonic-gate     crypto_key_t *key, crypto_data_t *data, crypto_data_t *mac,
18647c478bd9Sstevel@tonic-gate     crypto_spi_ctx_template_t ctx_template, crypto_req_handle_t req)
18657c478bd9Sstevel@tonic-gate {
18667c478bd9Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
18677c478bd9Sstevel@tonic-gate 	uchar_t digest[MD5_DIGEST_LENGTH];
18687c478bd9Sstevel@tonic-gate 	md5_hmac_ctx_t md5_hmac_ctx;
18697c478bd9Sstevel@tonic-gate 	uint32_t digest_len = MD5_DIGEST_LENGTH;
18707c478bd9Sstevel@tonic-gate 	uint_t keylen_in_bytes = CRYPTO_BITS2BYTES(key->ck_length);
18717c478bd9Sstevel@tonic-gate 
18727c478bd9Sstevel@tonic-gate 	if (mechanism->cm_type != MD5_HMAC_MECH_INFO_TYPE &&
18737c478bd9Sstevel@tonic-gate 	    mechanism->cm_type != MD5_HMAC_GEN_MECH_INFO_TYPE)
18747c478bd9Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
18757c478bd9Sstevel@tonic-gate 
18767c478bd9Sstevel@tonic-gate 	/* Add support for key by attributes (RFE 4706552) */
18777c478bd9Sstevel@tonic-gate 	if (key->ck_format != CRYPTO_KEY_RAW)
18787c478bd9Sstevel@tonic-gate 		return (CRYPTO_ARGUMENTS_BAD);
18797c478bd9Sstevel@tonic-gate 
18807c478bd9Sstevel@tonic-gate 	if (ctx_template != NULL) {
18817c478bd9Sstevel@tonic-gate 		/* reuse context template */
18827c478bd9Sstevel@tonic-gate 		bcopy(ctx_template, &md5_hmac_ctx, sizeof (md5_hmac_ctx_t));
18837c478bd9Sstevel@tonic-gate 	} else {
18847c478bd9Sstevel@tonic-gate 		/* no context template, compute context */
18857c478bd9Sstevel@tonic-gate 		if (keylen_in_bytes > MD5_HMAC_BLOCK_SIZE) {
18867c478bd9Sstevel@tonic-gate 			/*
18877c478bd9Sstevel@tonic-gate 			 * Hash the passed-in key to get a smaller key.
18887c478bd9Sstevel@tonic-gate 			 * The inner context is used since it hasn't been
18897c478bd9Sstevel@tonic-gate 			 * initialized yet.
18907c478bd9Sstevel@tonic-gate 			 */
18917c478bd9Sstevel@tonic-gate 			PROV_MD5_DIGEST_KEY(&md5_hmac_ctx.hc_icontext,
18927c478bd9Sstevel@tonic-gate 			    key->ck_data, keylen_in_bytes, digest);
18937c478bd9Sstevel@tonic-gate 			md5_mac_init_ctx(&md5_hmac_ctx, digest,
18947c478bd9Sstevel@tonic-gate 			    MD5_DIGEST_LENGTH);
18957c478bd9Sstevel@tonic-gate 		} else {
18967c478bd9Sstevel@tonic-gate 			md5_mac_init_ctx(&md5_hmac_ctx, key->ck_data,
18977c478bd9Sstevel@tonic-gate 			    keylen_in_bytes);
18987c478bd9Sstevel@tonic-gate 		}
18997c478bd9Sstevel@tonic-gate 	}
19007c478bd9Sstevel@tonic-gate 
19017c478bd9Sstevel@tonic-gate 	/*
19027c478bd9Sstevel@tonic-gate 	 * Get the mechanism parameters, if applicable.
19037c478bd9Sstevel@tonic-gate 	 */
19047c478bd9Sstevel@tonic-gate 	if (mechanism->cm_type == MD5_HMAC_GEN_MECH_INFO_TYPE) {
19057c478bd9Sstevel@tonic-gate 		if (mechanism->cm_param == NULL ||
19067c478bd9Sstevel@tonic-gate 		    mechanism->cm_param_len != sizeof (ulong_t)) {
19077c478bd9Sstevel@tonic-gate 			ret = CRYPTO_MECHANISM_PARAM_INVALID;
19087c478bd9Sstevel@tonic-gate 			goto bail;
19097c478bd9Sstevel@tonic-gate 		}
19107c478bd9Sstevel@tonic-gate 		PROV_MD5_GET_DIGEST_LEN(mechanism, digest_len);
19117c478bd9Sstevel@tonic-gate 		if (digest_len > MD5_DIGEST_LENGTH) {
19127c478bd9Sstevel@tonic-gate 			ret = CRYPTO_MECHANISM_PARAM_INVALID;
19137c478bd9Sstevel@tonic-gate 			goto bail;
19147c478bd9Sstevel@tonic-gate 		}
19157c478bd9Sstevel@tonic-gate 	}
19167c478bd9Sstevel@tonic-gate 
19177c478bd9Sstevel@tonic-gate 	if (mac->cd_length != digest_len) {
19187c478bd9Sstevel@tonic-gate 		ret = CRYPTO_INVALID_MAC;
19197c478bd9Sstevel@tonic-gate 		goto bail;
19207c478bd9Sstevel@tonic-gate 	}
19217c478bd9Sstevel@tonic-gate 
19227c478bd9Sstevel@tonic-gate 	/* do an MD5 update of the inner context using the specified data */
19237c478bd9Sstevel@tonic-gate 	MD5_MAC_UPDATE(data, md5_hmac_ctx, ret);
19247c478bd9Sstevel@tonic-gate 	if (ret != CRYPTO_SUCCESS)
19257c478bd9Sstevel@tonic-gate 		/* the update failed, free context and bail */
19267c478bd9Sstevel@tonic-gate 		goto bail;
19277c478bd9Sstevel@tonic-gate 
19287c478bd9Sstevel@tonic-gate 	/* do an MD5 final on the inner context */
19297c478bd9Sstevel@tonic-gate 	MD5Final(digest, &md5_hmac_ctx.hc_icontext);
19307c478bd9Sstevel@tonic-gate 
19317c478bd9Sstevel@tonic-gate 	/*
19327c478bd9Sstevel@tonic-gate 	 * Do an MD5 update on the outer context, feeding the inner
19337c478bd9Sstevel@tonic-gate 	 * digest as data.
19347c478bd9Sstevel@tonic-gate 	 */
19357c478bd9Sstevel@tonic-gate 	MD5Update(&md5_hmac_ctx.hc_ocontext, digest, MD5_DIGEST_LENGTH);
19367c478bd9Sstevel@tonic-gate 
19377c478bd9Sstevel@tonic-gate 	/*
19387c478bd9Sstevel@tonic-gate 	 * Do an MD5 final on the outer context, storing the computed
19397c478bd9Sstevel@tonic-gate 	 * digest in the local digest buffer.
19407c478bd9Sstevel@tonic-gate 	 */
19417c478bd9Sstevel@tonic-gate 	MD5Final(digest, &md5_hmac_ctx.hc_ocontext);
19427c478bd9Sstevel@tonic-gate 
19437c478bd9Sstevel@tonic-gate 	/*
19447c478bd9Sstevel@tonic-gate 	 * Compare the computed digest against the expected digest passed
19457c478bd9Sstevel@tonic-gate 	 * as argument.
19467c478bd9Sstevel@tonic-gate 	 */
19477c478bd9Sstevel@tonic-gate 	switch (mac->cd_format) {
19487c478bd9Sstevel@tonic-gate 
19497c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
19507c478bd9Sstevel@tonic-gate 		if (bcmp(digest, (unsigned char *)mac->cd_raw.iov_base +
19517c478bd9Sstevel@tonic-gate 		    mac->cd_offset, digest_len) != 0)
19527c478bd9Sstevel@tonic-gate 			ret = CRYPTO_INVALID_MAC;
19537c478bd9Sstevel@tonic-gate 		break;
19547c478bd9Sstevel@tonic-gate 
19557c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO: {
19567c478bd9Sstevel@tonic-gate 		off_t offset = mac->cd_offset;
19577c478bd9Sstevel@tonic-gate 		uint_t vec_idx;
19587c478bd9Sstevel@tonic-gate 		off_t scratch_offset = 0;
19597c478bd9Sstevel@tonic-gate 		size_t length = digest_len;
19607c478bd9Sstevel@tonic-gate 		size_t cur_len;
19617c478bd9Sstevel@tonic-gate 
19627c478bd9Sstevel@tonic-gate 		/* we support only kernel buffer */
19637c478bd9Sstevel@tonic-gate 		if (mac->cd_uio->uio_segflg != UIO_SYSSPACE)
19647c478bd9Sstevel@tonic-gate 			return (CRYPTO_ARGUMENTS_BAD);
19657c478bd9Sstevel@tonic-gate 
19667c478bd9Sstevel@tonic-gate 		/* jump to the first iovec containing the expected digest */
19677c478bd9Sstevel@tonic-gate 		for (vec_idx = 0;
19687c478bd9Sstevel@tonic-gate 		    offset >= mac->cd_uio->uio_iov[vec_idx].iov_len &&
19697c478bd9Sstevel@tonic-gate 		    vec_idx < mac->cd_uio->uio_iovcnt;
19707c478bd9Sstevel@tonic-gate 		    offset -= mac->cd_uio->uio_iov[vec_idx++].iov_len);
19717c478bd9Sstevel@tonic-gate 		if (vec_idx == mac->cd_uio->uio_iovcnt) {
19727c478bd9Sstevel@tonic-gate 			/*
19737c478bd9Sstevel@tonic-gate 			 * The caller specified an offset that is
19747c478bd9Sstevel@tonic-gate 			 * larger than the total size of the buffers
19757c478bd9Sstevel@tonic-gate 			 * it provided.
19767c478bd9Sstevel@tonic-gate 			 */
19777c478bd9Sstevel@tonic-gate 			ret = CRYPTO_DATA_LEN_RANGE;
19787c478bd9Sstevel@tonic-gate 			break;
19797c478bd9Sstevel@tonic-gate 		}
19807c478bd9Sstevel@tonic-gate 
19817c478bd9Sstevel@tonic-gate 		/* do the comparison of computed digest vs specified one */
19827c478bd9Sstevel@tonic-gate 		while (vec_idx < mac->cd_uio->uio_iovcnt && length > 0) {
19837c478bd9Sstevel@tonic-gate 			cur_len = MIN(mac->cd_uio->uio_iov[vec_idx].iov_len -
19847c478bd9Sstevel@tonic-gate 			    offset, length);
19857c478bd9Sstevel@tonic-gate 
19867c478bd9Sstevel@tonic-gate 			if (bcmp(digest + scratch_offset,
19877c478bd9Sstevel@tonic-gate 			    mac->cd_uio->uio_iov[vec_idx].iov_base + offset,
19887c478bd9Sstevel@tonic-gate 			    cur_len) != 0) {
19897c478bd9Sstevel@tonic-gate 				ret = CRYPTO_INVALID_MAC;
19907c478bd9Sstevel@tonic-gate 				break;
19917c478bd9Sstevel@tonic-gate 			}
19927c478bd9Sstevel@tonic-gate 
19937c478bd9Sstevel@tonic-gate 			length -= cur_len;
19947c478bd9Sstevel@tonic-gate 			vec_idx++;
19957c478bd9Sstevel@tonic-gate 			scratch_offset += cur_len;
19967c478bd9Sstevel@tonic-gate 			offset = 0;
19977c478bd9Sstevel@tonic-gate 		}
19987c478bd9Sstevel@tonic-gate 		break;
19997c478bd9Sstevel@tonic-gate 	}
20007c478bd9Sstevel@tonic-gate 
20017c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK: {
20027c478bd9Sstevel@tonic-gate 		off_t offset = mac->cd_offset;
20037c478bd9Sstevel@tonic-gate 		mblk_t *mp;
20047c478bd9Sstevel@tonic-gate 		off_t scratch_offset = 0;
20057c478bd9Sstevel@tonic-gate 		size_t length = digest_len;
20067c478bd9Sstevel@tonic-gate 		size_t cur_len;
20077c478bd9Sstevel@tonic-gate 
20087c478bd9Sstevel@tonic-gate 		/* jump to the first mblk_t containing the expected digest */
20097c478bd9Sstevel@tonic-gate 		for (mp = mac->cd_mp; mp != NULL && offset >= MBLKL(mp);
20107c478bd9Sstevel@tonic-gate 		    offset -= MBLKL(mp), mp = mp->b_cont);
20117c478bd9Sstevel@tonic-gate 		if (mp == NULL) {
20127c478bd9Sstevel@tonic-gate 			/*
20137c478bd9Sstevel@tonic-gate 			 * The caller specified an offset that is larger than
20147c478bd9Sstevel@tonic-gate 			 * the total size of the buffers it provided.
20157c478bd9Sstevel@tonic-gate 			 */
20167c478bd9Sstevel@tonic-gate 			ret = CRYPTO_DATA_LEN_RANGE;
20177c478bd9Sstevel@tonic-gate 			break;
20187c478bd9Sstevel@tonic-gate 		}
20197c478bd9Sstevel@tonic-gate 
20207c478bd9Sstevel@tonic-gate 		while (mp != NULL && length > 0) {
20217c478bd9Sstevel@tonic-gate 			cur_len = MIN(MBLKL(mp) - offset, length);
20227c478bd9Sstevel@tonic-gate 			if (bcmp(digest + scratch_offset,
20237c478bd9Sstevel@tonic-gate 			    mp->b_rptr + offset, cur_len) != 0) {
20247c478bd9Sstevel@tonic-gate 				ret = CRYPTO_INVALID_MAC;
20257c478bd9Sstevel@tonic-gate 				break;
20267c478bd9Sstevel@tonic-gate 			}
20277c478bd9Sstevel@tonic-gate 
20287c478bd9Sstevel@tonic-gate 			length -= cur_len;
20297c478bd9Sstevel@tonic-gate 			mp = mp->b_cont;
20307c478bd9Sstevel@tonic-gate 			scratch_offset += cur_len;
20317c478bd9Sstevel@tonic-gate 			offset = 0;
20327c478bd9Sstevel@tonic-gate 		}
20337c478bd9Sstevel@tonic-gate 		break;
20347c478bd9Sstevel@tonic-gate 	}
20357c478bd9Sstevel@tonic-gate 
20367c478bd9Sstevel@tonic-gate 	default:
20377c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
20387c478bd9Sstevel@tonic-gate 	}
20397c478bd9Sstevel@tonic-gate 
20407c478bd9Sstevel@tonic-gate 	bzero(&md5_hmac_ctx, sizeof (md5_hmac_ctx_t));
20417c478bd9Sstevel@tonic-gate 	return (ret);
20427c478bd9Sstevel@tonic-gate bail:
20437c478bd9Sstevel@tonic-gate 	bzero(&md5_hmac_ctx, sizeof (md5_hmac_ctx_t));
20447c478bd9Sstevel@tonic-gate 	mac->cd_length = 0;
20457c478bd9Sstevel@tonic-gate 	return (ret);
20467c478bd9Sstevel@tonic-gate }
20477c478bd9Sstevel@tonic-gate 
20487c478bd9Sstevel@tonic-gate /*
20497c478bd9Sstevel@tonic-gate  * KCF software provider context management entry points.
20507c478bd9Sstevel@tonic-gate  */
20517c478bd9Sstevel@tonic-gate 
20527c478bd9Sstevel@tonic-gate /* ARGSUSED */
20537c478bd9Sstevel@tonic-gate static int
20547c478bd9Sstevel@tonic-gate md5_create_ctx_template(crypto_provider_handle_t provider,
20557c478bd9Sstevel@tonic-gate     crypto_mechanism_t *mechanism, crypto_key_t *key,
20567c478bd9Sstevel@tonic-gate     crypto_spi_ctx_template_t *ctx_template, size_t *ctx_template_size,
20577c478bd9Sstevel@tonic-gate     crypto_req_handle_t req)
20587c478bd9Sstevel@tonic-gate {
20597c478bd9Sstevel@tonic-gate 	md5_hmac_ctx_t *md5_hmac_ctx_tmpl;
20607c478bd9Sstevel@tonic-gate 	uint_t keylen_in_bytes = CRYPTO_BITS2BYTES(key->ck_length);
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate 	if ((mechanism->cm_type != MD5_HMAC_MECH_INFO_TYPE) &&
20637c478bd9Sstevel@tonic-gate 	    (mechanism->cm_type != MD5_HMAC_GEN_MECH_INFO_TYPE))
20647c478bd9Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
20657c478bd9Sstevel@tonic-gate 
20667c478bd9Sstevel@tonic-gate 	/* Add support for key by attributes (RFE 4706552) */
20677c478bd9Sstevel@tonic-gate 	if (key->ck_format != CRYPTO_KEY_RAW)
20687c478bd9Sstevel@tonic-gate 		return (CRYPTO_ARGUMENTS_BAD);
20697c478bd9Sstevel@tonic-gate 
20707c478bd9Sstevel@tonic-gate 	/*
20717c478bd9Sstevel@tonic-gate 	 * Allocate and initialize MD5 context.
20727c478bd9Sstevel@tonic-gate 	 */
20737c478bd9Sstevel@tonic-gate 	md5_hmac_ctx_tmpl = kmem_alloc(sizeof (md5_hmac_ctx_t),
20747c478bd9Sstevel@tonic-gate 	    crypto_kmflag(req));
20757c478bd9Sstevel@tonic-gate 	if (md5_hmac_ctx_tmpl == NULL)
20767c478bd9Sstevel@tonic-gate 		return (CRYPTO_HOST_MEMORY);
20777c478bd9Sstevel@tonic-gate 
20787c478bd9Sstevel@tonic-gate 	if (keylen_in_bytes > MD5_HMAC_BLOCK_SIZE) {
20797c478bd9Sstevel@tonic-gate 		uchar_t digested_key[MD5_DIGEST_LENGTH];
20807c478bd9Sstevel@tonic-gate 
20817c478bd9Sstevel@tonic-gate 		/*
20827c478bd9Sstevel@tonic-gate 		 * Hash the passed-in key to get a smaller key.
20837c478bd9Sstevel@tonic-gate 		 * The inner context is used since it hasn't been
20847c478bd9Sstevel@tonic-gate 		 * initialized yet.
20857c478bd9Sstevel@tonic-gate 		 */
20867c478bd9Sstevel@tonic-gate 		PROV_MD5_DIGEST_KEY(&md5_hmac_ctx_tmpl->hc_icontext,
20877c478bd9Sstevel@tonic-gate 		    key->ck_data, keylen_in_bytes, digested_key);
20887c478bd9Sstevel@tonic-gate 		md5_mac_init_ctx(md5_hmac_ctx_tmpl, digested_key,
20897c478bd9Sstevel@tonic-gate 		    MD5_DIGEST_LENGTH);
20907c478bd9Sstevel@tonic-gate 	} else {
20917c478bd9Sstevel@tonic-gate 		md5_mac_init_ctx(md5_hmac_ctx_tmpl, key->ck_data,
20927c478bd9Sstevel@tonic-gate 		    keylen_in_bytes);
20937c478bd9Sstevel@tonic-gate 	}
20947c478bd9Sstevel@tonic-gate 
20957c478bd9Sstevel@tonic-gate 	md5_hmac_ctx_tmpl->hc_mech_type = mechanism->cm_type;
20967c478bd9Sstevel@tonic-gate 	*ctx_template = (crypto_spi_ctx_template_t)md5_hmac_ctx_tmpl;
20977c478bd9Sstevel@tonic-gate 	*ctx_template_size = sizeof (md5_hmac_ctx_t);
20987c478bd9Sstevel@tonic-gate 
20997c478bd9Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
21007c478bd9Sstevel@tonic-gate }
21017c478bd9Sstevel@tonic-gate 
21027c478bd9Sstevel@tonic-gate static int
21037c478bd9Sstevel@tonic-gate md5_free_context(crypto_ctx_t *ctx)
21047c478bd9Sstevel@tonic-gate {
21057c478bd9Sstevel@tonic-gate 	uint_t ctx_len;
21067c478bd9Sstevel@tonic-gate 	md5_mech_type_t mech_type;
21077c478bd9Sstevel@tonic-gate 
21087c478bd9Sstevel@tonic-gate 	if (ctx->cc_provider_private == NULL)
21097c478bd9Sstevel@tonic-gate 		return (CRYPTO_SUCCESS);
21107c478bd9Sstevel@tonic-gate 
21117c478bd9Sstevel@tonic-gate 	/*
21127c478bd9Sstevel@tonic-gate 	 * We have to free either MD5 or MD5-HMAC contexts, which
21137c478bd9Sstevel@tonic-gate 	 * have different lengths.
21147c478bd9Sstevel@tonic-gate 	 */
21157c478bd9Sstevel@tonic-gate 
21167c478bd9Sstevel@tonic-gate 	mech_type = PROV_MD5_CTX(ctx)->mc_mech_type;
21177c478bd9Sstevel@tonic-gate 	if (mech_type == MD5_MECH_INFO_TYPE)
21187c478bd9Sstevel@tonic-gate 		ctx_len = sizeof (md5_ctx_t);
21197c478bd9Sstevel@tonic-gate 	else {
21207c478bd9Sstevel@tonic-gate 		ASSERT(mech_type == MD5_HMAC_MECH_INFO_TYPE ||
21217c478bd9Sstevel@tonic-gate 		    mech_type == MD5_HMAC_GEN_MECH_INFO_TYPE);
21227c478bd9Sstevel@tonic-gate 		ctx_len = sizeof (md5_hmac_ctx_t);
21237c478bd9Sstevel@tonic-gate 	}
21247c478bd9Sstevel@tonic-gate 
21257c478bd9Sstevel@tonic-gate 	bzero(ctx->cc_provider_private, ctx_len);
21267c478bd9Sstevel@tonic-gate 	kmem_free(ctx->cc_provider_private, ctx_len);
21277c478bd9Sstevel@tonic-gate 	ctx->cc_provider_private = NULL;
21287c478bd9Sstevel@tonic-gate 
21297c478bd9Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
21307c478bd9Sstevel@tonic-gate }
21317c478bd9Sstevel@tonic-gate 
21327c478bd9Sstevel@tonic-gate #endif	/* _KERNEL && !_BOOT */
2133