xref: /titanic_44/usr/src/common/crypto/md5/md5.c (revision afd1ac7b1c9a8cdf273c865aa5e9a14620341443)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright 2005 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 */
48*afd1ac7bSwesolows #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,
88*afd1ac7bSwesolows 	"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));
6087c478bd9Sstevel@tonic-gate }
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate #ifndef	_KERNEL
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate void
6137c478bd9Sstevel@tonic-gate md5_calc(unsigned char *output, unsigned char *input, unsigned int inlen)
6147c478bd9Sstevel@tonic-gate {
6157c478bd9Sstevel@tonic-gate 	MD5_CTX context;
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	MD5Init(&context);
6187c478bd9Sstevel@tonic-gate 	MD5Update(&context, input, inlen);
6197c478bd9Sstevel@tonic-gate 	MD5Final(output, &context);
6207c478bd9Sstevel@tonic-gate }
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate #endif	/* !_KERNEL */
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate /*
6257c478bd9Sstevel@tonic-gate  * sparc register window optimization:
6267c478bd9Sstevel@tonic-gate  *
6277c478bd9Sstevel@tonic-gate  * `a', `b', `c', and `d' are passed into MD5Transform explicitly
6287c478bd9Sstevel@tonic-gate  * since it increases the number of registers available to the
6297c478bd9Sstevel@tonic-gate  * compiler.  under this scheme, these variables can be held in
6307c478bd9Sstevel@tonic-gate  * %i0 - %i3, which leaves more local and out registers available.
6317c478bd9Sstevel@tonic-gate  */
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate /*
6347c478bd9Sstevel@tonic-gate  * MD5Transform()
6357c478bd9Sstevel@tonic-gate  *
6367c478bd9Sstevel@tonic-gate  * purpose: md5 transformation -- updates the digest based on `block'
6377c478bd9Sstevel@tonic-gate  *   input: uint32_t	: bytes  1 -  4 of the digest
6387c478bd9Sstevel@tonic-gate  *          uint32_t	: bytes  5 -  8 of the digest
6397c478bd9Sstevel@tonic-gate  *          uint32_t	: bytes  9 - 12 of the digest
6407c478bd9Sstevel@tonic-gate  *          uint32_t	: bytes 12 - 16 of the digest
6417c478bd9Sstevel@tonic-gate  *          MD5_CTX *   : the context to update
6427c478bd9Sstevel@tonic-gate  *          uint8_t [64]: the block to use to update the digest
6437c478bd9Sstevel@tonic-gate  *  output: void
6447c478bd9Sstevel@tonic-gate  */
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate static void
6477c478bd9Sstevel@tonic-gate MD5Transform(uint32_t a, uint32_t b, uint32_t c, uint32_t d,
6487c478bd9Sstevel@tonic-gate     MD5_CTX *ctx, const uint8_t block[64])
6497c478bd9Sstevel@tonic-gate {
6507c478bd9Sstevel@tonic-gate 	/*
6517c478bd9Sstevel@tonic-gate 	 * general optimization:
6527c478bd9Sstevel@tonic-gate 	 *
6537c478bd9Sstevel@tonic-gate 	 * use individual integers instead of using an array.  this is a
6547c478bd9Sstevel@tonic-gate 	 * win, although the amount it wins by seems to vary quite a bit.
6557c478bd9Sstevel@tonic-gate 	 */
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	register uint32_t	x_0, x_1, x_2,  x_3,  x_4,  x_5,  x_6,  x_7;
6587c478bd9Sstevel@tonic-gate 	register uint32_t	x_8, x_9, x_10, x_11, x_12, x_13, x_14, x_15;
659554ff184Skais #ifdef sun4v
660554ff184Skais 	unsigned long long 	*md5_consts64;
661554ff184Skais 
662554ff184Skais 	md5_consts64 = (unsigned long long *) md5_consts;
663554ff184Skais #endif	/* sun4v */
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	/*
6667c478bd9Sstevel@tonic-gate 	 * general optimization:
6677c478bd9Sstevel@tonic-gate 	 *
6687c478bd9Sstevel@tonic-gate 	 * the compiler (at least SC4.2/5.x) generates better code if
6697c478bd9Sstevel@tonic-gate 	 * variable use is localized.  in this case, swapping the integers in
6707c478bd9Sstevel@tonic-gate 	 * this order allows `x_0 'to be swapped nearest to its first use in
6717c478bd9Sstevel@tonic-gate 	 * FF(), and likewise for `x_1' and up.  note that the compiler
6727c478bd9Sstevel@tonic-gate 	 * prefers this to doing each swap right before the FF() that
6737c478bd9Sstevel@tonic-gate 	 * uses it.
6747c478bd9Sstevel@tonic-gate 	 */
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 	/*
6777c478bd9Sstevel@tonic-gate 	 * sparc v9/v8plus optimization:
6787c478bd9Sstevel@tonic-gate 	 *
6797c478bd9Sstevel@tonic-gate 	 * if `block' is already aligned on a 4-byte boundary, use the
6807c478bd9Sstevel@tonic-gate 	 * optimized load_little_32() directly.  otherwise, bcopy()
6817c478bd9Sstevel@tonic-gate 	 * into a buffer that *is* aligned on a 4-byte boundary and
6827c478bd9Sstevel@tonic-gate 	 * then do the load_little_32() on that buffer.  benchmarks
6837c478bd9Sstevel@tonic-gate 	 * have shown that using the bcopy() is better than loading
6847c478bd9Sstevel@tonic-gate 	 * the bytes individually and doing the endian-swap by hand.
6857c478bd9Sstevel@tonic-gate 	 *
6867c478bd9Sstevel@tonic-gate 	 * even though it's quite tempting to assign to do:
6877c478bd9Sstevel@tonic-gate 	 *
6887c478bd9Sstevel@tonic-gate 	 * blk = bcopy(blk, ctx->buf_un.buf32, sizeof (ctx->buf_un.buf32));
6897c478bd9Sstevel@tonic-gate 	 *
6907c478bd9Sstevel@tonic-gate 	 * and only have one set of LOAD_LITTLE_32()'s, the compiler (at least
6917c478bd9Sstevel@tonic-gate 	 * SC4.2/5.x) *does not* like that, so please resist the urge.
6927c478bd9Sstevel@tonic-gate 	 */
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate #ifdef _MD5_CHECK_ALIGNMENT
6957c478bd9Sstevel@tonic-gate 	if ((uintptr_t)block & 0x3) {		/* not 4-byte aligned? */
6967c478bd9Sstevel@tonic-gate 		bcopy(block, ctx->buf_un.buf32, sizeof (ctx->buf_un.buf32));
697554ff184Skais 
698554ff184Skais #ifdef sun4v
699554ff184Skais 		x_15 = LOAD_LITTLE_32_f(ctx->buf_un.buf32);
700554ff184Skais 		x_14 = LOAD_LITTLE_32_e(ctx->buf_un.buf32);
701554ff184Skais 		x_13 = LOAD_LITTLE_32_d(ctx->buf_un.buf32);
702554ff184Skais 		x_12 = LOAD_LITTLE_32_c(ctx->buf_un.buf32);
703554ff184Skais 		x_11 = LOAD_LITTLE_32_b(ctx->buf_un.buf32);
704554ff184Skais 		x_10 = LOAD_LITTLE_32_a(ctx->buf_un.buf32);
705554ff184Skais 		x_9  = LOAD_LITTLE_32_9(ctx->buf_un.buf32);
706554ff184Skais 		x_8  = LOAD_LITTLE_32_8(ctx->buf_un.buf32);
707554ff184Skais 		x_7  = LOAD_LITTLE_32_7(ctx->buf_un.buf32);
708554ff184Skais 		x_6  = LOAD_LITTLE_32_6(ctx->buf_un.buf32);
709554ff184Skais 		x_5  = LOAD_LITTLE_32_5(ctx->buf_un.buf32);
710554ff184Skais 		x_4  = LOAD_LITTLE_32_4(ctx->buf_un.buf32);
711554ff184Skais 		x_3  = LOAD_LITTLE_32_3(ctx->buf_un.buf32);
712554ff184Skais 		x_2  = LOAD_LITTLE_32_2(ctx->buf_un.buf32);
713554ff184Skais 		x_1  = LOAD_LITTLE_32_1(ctx->buf_un.buf32);
714554ff184Skais 		x_0  = LOAD_LITTLE_32_0(ctx->buf_un.buf32);
715554ff184Skais #else
7167c478bd9Sstevel@tonic-gate 		x_15 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 15);
7177c478bd9Sstevel@tonic-gate 		x_14 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 14);
7187c478bd9Sstevel@tonic-gate 		x_13 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 13);
7197c478bd9Sstevel@tonic-gate 		x_12 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 12);
7207c478bd9Sstevel@tonic-gate 		x_11 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 11);
7217c478bd9Sstevel@tonic-gate 		x_10 = LOAD_LITTLE_32(ctx->buf_un.buf32 + 10);
7227c478bd9Sstevel@tonic-gate 		x_9  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  9);
7237c478bd9Sstevel@tonic-gate 		x_8  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  8);
7247c478bd9Sstevel@tonic-gate 		x_7  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  7);
7257c478bd9Sstevel@tonic-gate 		x_6  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  6);
7267c478bd9Sstevel@tonic-gate 		x_5  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  5);
7277c478bd9Sstevel@tonic-gate 		x_4  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  4);
7287c478bd9Sstevel@tonic-gate 		x_3  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  3);
7297c478bd9Sstevel@tonic-gate 		x_2  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  2);
7307c478bd9Sstevel@tonic-gate 		x_1  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  1);
7317c478bd9Sstevel@tonic-gate 		x_0  = LOAD_LITTLE_32(ctx->buf_un.buf32 +  0);
732554ff184Skais #endif /* sun4v */
7337c478bd9Sstevel@tonic-gate 	} else
7347c478bd9Sstevel@tonic-gate #endif
7357c478bd9Sstevel@tonic-gate 	{
736554ff184Skais 
737554ff184Skais #ifdef sun4v
738554ff184Skais 		x_15 = LOAD_LITTLE_32_f(block);
739554ff184Skais 		x_14 = LOAD_LITTLE_32_e(block);
740554ff184Skais 		x_13 = LOAD_LITTLE_32_d(block);
741554ff184Skais 		x_12 = LOAD_LITTLE_32_c(block);
742554ff184Skais 		x_11 = LOAD_LITTLE_32_b(block);
743554ff184Skais 		x_10 = LOAD_LITTLE_32_a(block);
744554ff184Skais 		x_9  = LOAD_LITTLE_32_9(block);
745554ff184Skais 		x_8  = LOAD_LITTLE_32_8(block);
746554ff184Skais 		x_7  = LOAD_LITTLE_32_7(block);
747554ff184Skais 		x_6  = LOAD_LITTLE_32_6(block);
748554ff184Skais 		x_5  = LOAD_LITTLE_32_5(block);
749554ff184Skais 		x_4  = LOAD_LITTLE_32_4(block);
750554ff184Skais 		x_3  = LOAD_LITTLE_32_3(block);
751554ff184Skais 		x_2  = LOAD_LITTLE_32_2(block);
752554ff184Skais 		x_1  = LOAD_LITTLE_32_1(block);
753554ff184Skais 		x_0  = LOAD_LITTLE_32_0(block);
754554ff184Skais #else
7557c478bd9Sstevel@tonic-gate 		x_15 = LOAD_LITTLE_32(block + 60);
7567c478bd9Sstevel@tonic-gate 		x_14 = LOAD_LITTLE_32(block + 56);
7577c478bd9Sstevel@tonic-gate 		x_13 = LOAD_LITTLE_32(block + 52);
7587c478bd9Sstevel@tonic-gate 		x_12 = LOAD_LITTLE_32(block + 48);
7597c478bd9Sstevel@tonic-gate 		x_11 = LOAD_LITTLE_32(block + 44);
7607c478bd9Sstevel@tonic-gate 		x_10 = LOAD_LITTLE_32(block + 40);
7617c478bd9Sstevel@tonic-gate 		x_9  = LOAD_LITTLE_32(block + 36);
7627c478bd9Sstevel@tonic-gate 		x_8  = LOAD_LITTLE_32(block + 32);
7637c478bd9Sstevel@tonic-gate 		x_7  = LOAD_LITTLE_32(block + 28);
7647c478bd9Sstevel@tonic-gate 		x_6  = LOAD_LITTLE_32(block + 24);
7657c478bd9Sstevel@tonic-gate 		x_5  = LOAD_LITTLE_32(block + 20);
7667c478bd9Sstevel@tonic-gate 		x_4  = LOAD_LITTLE_32(block + 16);
7677c478bd9Sstevel@tonic-gate 		x_3  = LOAD_LITTLE_32(block + 12);
7687c478bd9Sstevel@tonic-gate 		x_2  = LOAD_LITTLE_32(block +  8);
7697c478bd9Sstevel@tonic-gate 		x_1  = LOAD_LITTLE_32(block +  4);
7707c478bd9Sstevel@tonic-gate 		x_0  = LOAD_LITTLE_32(block +  0);
771554ff184Skais #endif /* sun4v */
7727c478bd9Sstevel@tonic-gate 	}
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 	/* round 1 */
775554ff184Skais 	FF(a, b, c, d, 	x_0, MD5_SHIFT_11, MD5_CONST_e(0));  /* 1 */
776554ff184Skais 	FF(d, a, b, c, 	x_1, MD5_SHIFT_12, MD5_CONST_o(1));  /* 2 */
777554ff184Skais 	FF(c, d, a, b, 	x_2, MD5_SHIFT_13, MD5_CONST_e(2));  /* 3 */
778554ff184Skais 	FF(b, c, d, a, 	x_3, MD5_SHIFT_14, MD5_CONST_o(3));  /* 4 */
779554ff184Skais 	FF(a, b, c, d, 	x_4, MD5_SHIFT_11, MD5_CONST_e(4));  /* 5 */
780554ff184Skais 	FF(d, a, b, c, 	x_5, MD5_SHIFT_12, MD5_CONST_o(5));  /* 6 */
781554ff184Skais 	FF(c, d, a, b, 	x_6, MD5_SHIFT_13, MD5_CONST_e(6));  /* 7 */
782554ff184Skais 	FF(b, c, d, a, 	x_7, MD5_SHIFT_14, MD5_CONST_o(7));  /* 8 */
783554ff184Skais 	FF(a, b, c, d, 	x_8, MD5_SHIFT_11, MD5_CONST_e(8));  /* 9 */
784554ff184Skais 	FF(d, a, b, c, 	x_9, MD5_SHIFT_12, MD5_CONST_o(9));  /* 10 */
785554ff184Skais 	FF(c, d, a, b, x_10, MD5_SHIFT_13, MD5_CONST_e(10)); /* 11 */
786554ff184Skais 	FF(b, c, d, a, x_11, MD5_SHIFT_14, MD5_CONST_o(11)); /* 12 */
787554ff184Skais 	FF(a, b, c, d, x_12, MD5_SHIFT_11, MD5_CONST_e(12)); /* 13 */
788554ff184Skais 	FF(d, a, b, c, x_13, MD5_SHIFT_12, MD5_CONST_o(13)); /* 14 */
789554ff184Skais 	FF(c, d, a, b, x_14, MD5_SHIFT_13, MD5_CONST_e(14)); /* 15 */
790554ff184Skais 	FF(b, c, d, a, x_15, MD5_SHIFT_14, MD5_CONST_o(15)); /* 16 */
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	/* round 2 */
793554ff184Skais 	GG(a, b, c, d,  x_1, MD5_SHIFT_21, MD5_CONST_e(16)); /* 17 */
794554ff184Skais 	GG(d, a, b, c,  x_6, MD5_SHIFT_22, MD5_CONST_o(17)); /* 18 */
795554ff184Skais 	GG(c, d, a, b, x_11, MD5_SHIFT_23, MD5_CONST_e(18)); /* 19 */
796554ff184Skais 	GG(b, c, d, a,  x_0, MD5_SHIFT_24, MD5_CONST_o(19)); /* 20 */
797554ff184Skais 	GG(a, b, c, d,  x_5, MD5_SHIFT_21, MD5_CONST_e(20)); /* 21 */
798554ff184Skais 	GG(d, a, b, c, x_10, MD5_SHIFT_22, MD5_CONST_o(21)); /* 22 */
799554ff184Skais 	GG(c, d, a, b, x_15, MD5_SHIFT_23, MD5_CONST_e(22)); /* 23 */
800554ff184Skais 	GG(b, c, d, a,  x_4, MD5_SHIFT_24, MD5_CONST_o(23)); /* 24 */
801554ff184Skais 	GG(a, b, c, d,  x_9, MD5_SHIFT_21, MD5_CONST_e(24)); /* 25 */
802554ff184Skais 	GG(d, a, b, c, x_14, MD5_SHIFT_22, MD5_CONST_o(25)); /* 26 */
803554ff184Skais 	GG(c, d, a, b,  x_3, MD5_SHIFT_23, MD5_CONST_e(26)); /* 27 */
804554ff184Skais 	GG(b, c, d, a,  x_8, MD5_SHIFT_24, MD5_CONST_o(27)); /* 28 */
805554ff184Skais 	GG(a, b, c, d, x_13, MD5_SHIFT_21, MD5_CONST_e(28)); /* 29 */
806554ff184Skais 	GG(d, a, b, c,  x_2, MD5_SHIFT_22, MD5_CONST_o(29)); /* 30 */
807554ff184Skais 	GG(c, d, a, b,  x_7, MD5_SHIFT_23, MD5_CONST_e(30)); /* 31 */
808554ff184Skais 	GG(b, c, d, a, x_12, MD5_SHIFT_24, MD5_CONST_o(31)); /* 32 */
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 	/* round 3 */
811554ff184Skais 	HH(a, b, c, d,  x_5, MD5_SHIFT_31, MD5_CONST_e(32)); /* 33 */
812554ff184Skais 	HH(d, a, b, c,  x_8, MD5_SHIFT_32, MD5_CONST_o(33)); /* 34 */
813554ff184Skais 	HH(c, d, a, b, x_11, MD5_SHIFT_33, MD5_CONST_e(34)); /* 35 */
814554ff184Skais 	HH(b, c, d, a, x_14, MD5_SHIFT_34, MD5_CONST_o(35)); /* 36 */
815554ff184Skais 	HH(a, b, c, d,  x_1, MD5_SHIFT_31, MD5_CONST_e(36)); /* 37 */
816554ff184Skais 	HH(d, a, b, c,  x_4, MD5_SHIFT_32, MD5_CONST_o(37)); /* 38 */
817554ff184Skais 	HH(c, d, a, b,  x_7, MD5_SHIFT_33, MD5_CONST_e(38)); /* 39 */
818554ff184Skais 	HH(b, c, d, a, x_10, MD5_SHIFT_34, MD5_CONST_o(39)); /* 40 */
819554ff184Skais 	HH(a, b, c, d, x_13, MD5_SHIFT_31, MD5_CONST_e(40)); /* 41 */
820554ff184Skais 	HH(d, a, b, c,  x_0, MD5_SHIFT_32, MD5_CONST_o(41)); /* 42 */
821554ff184Skais 	HH(c, d, a, b,  x_3, MD5_SHIFT_33, MD5_CONST_e(42)); /* 43 */
822554ff184Skais 	HH(b, c, d, a,  x_6, MD5_SHIFT_34, MD5_CONST_o(43)); /* 44 */
823554ff184Skais 	HH(a, b, c, d,  x_9, MD5_SHIFT_31, MD5_CONST_e(44)); /* 45 */
824554ff184Skais 	HH(d, a, b, c, x_12, MD5_SHIFT_32, MD5_CONST_o(45)); /* 46 */
825554ff184Skais 	HH(c, d, a, b, x_15, MD5_SHIFT_33, MD5_CONST_e(46)); /* 47 */
826554ff184Skais 	HH(b, c, d, a,  x_2, MD5_SHIFT_34, MD5_CONST_o(47)); /* 48 */
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 	/* round 4 */
829554ff184Skais 	II(a, b, c, d,  x_0, MD5_SHIFT_41, MD5_CONST_e(48)); /* 49 */
830554ff184Skais 	II(d, a, b, c,  x_7, MD5_SHIFT_42, MD5_CONST_o(49)); /* 50 */
831554ff184Skais 	II(c, d, a, b, x_14, MD5_SHIFT_43, MD5_CONST_e(50)); /* 51 */
832554ff184Skais 	II(b, c, d, a,  x_5, MD5_SHIFT_44, MD5_CONST_o(51)); /* 52 */
833554ff184Skais 	II(a, b, c, d, x_12, MD5_SHIFT_41, MD5_CONST_e(52)); /* 53 */
834554ff184Skais 	II(d, a, b, c,  x_3, MD5_SHIFT_42, MD5_CONST_o(53)); /* 54 */
835554ff184Skais 	II(c, d, a, b, x_10, MD5_SHIFT_43, MD5_CONST_e(54)); /* 55 */
836554ff184Skais 	II(b, c, d, a,  x_1, MD5_SHIFT_44, MD5_CONST_o(55)); /* 56 */
837554ff184Skais 	II(a, b, c, d,  x_8, MD5_SHIFT_41, MD5_CONST_e(56)); /* 57 */
838554ff184Skais 	II(d, a, b, c, x_15, MD5_SHIFT_42, MD5_CONST_o(57)); /* 58 */
839554ff184Skais 	II(c, d, a, b,  x_6, MD5_SHIFT_43, MD5_CONST_e(58)); /* 59 */
840554ff184Skais 	II(b, c, d, a, x_13, MD5_SHIFT_44, MD5_CONST_o(59)); /* 60 */
841554ff184Skais 	II(a, b, c, d,  x_4, MD5_SHIFT_41, MD5_CONST_e(60)); /* 61 */
842554ff184Skais 	II(d, a, b, c, x_11, MD5_SHIFT_42, MD5_CONST_o(61)); /* 62 */
843554ff184Skais 	II(c, d, a, b,  x_2, MD5_SHIFT_43, MD5_CONST_e(62)); /* 63 */
844554ff184Skais 	II(b, c, d, a,  x_9, MD5_SHIFT_44, MD5_CONST_o(63)); /* 64 */
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 	ctx->state[0] += a;
8477c478bd9Sstevel@tonic-gate 	ctx->state[1] += b;
8487c478bd9Sstevel@tonic-gate 	ctx->state[2] += c;
8497c478bd9Sstevel@tonic-gate 	ctx->state[3] += d;
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 	/*
8527c478bd9Sstevel@tonic-gate 	 * zeroize sensitive information -- compiler will optimize
8537c478bd9Sstevel@tonic-gate 	 * this out if everything is kept in registers
8547c478bd9Sstevel@tonic-gate 	 */
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	x_0 = x_1  = x_2  = x_3  = x_4  = x_5  = x_6  = x_7 = x_8 = 0;
8577c478bd9Sstevel@tonic-gate 	x_9 = x_10 = x_11 = x_12 = x_13 = x_14 = x_15 = 0;
8587c478bd9Sstevel@tonic-gate }
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate /*
8617c478bd9Sstevel@tonic-gate  * devpro compiler optimization:
8627c478bd9Sstevel@tonic-gate  *
8637c478bd9Sstevel@tonic-gate  * the compiler can generate better code if it knows that `input' and
8647c478bd9Sstevel@tonic-gate  * `output' do not point to the same source.  there is no portable
8657c478bd9Sstevel@tonic-gate  * way to tell the compiler this, but the devpro compiler recognizes the
8667c478bd9Sstevel@tonic-gate  * `_Restrict' keyword to indicate this condition.  use it if possible.
8677c478bd9Sstevel@tonic-gate  */
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate #if defined(__RESTRICT) && !defined(__GNUC__)
8707c478bd9Sstevel@tonic-gate #define	restrict	_Restrict
8717c478bd9Sstevel@tonic-gate #else
8727c478bd9Sstevel@tonic-gate #define	restrict	/* nothing */
8737c478bd9Sstevel@tonic-gate #endif
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate /*
8767c478bd9Sstevel@tonic-gate  * Encode()
8777c478bd9Sstevel@tonic-gate  *
8787c478bd9Sstevel@tonic-gate  * purpose: to convert a list of numbers from big endian to little endian
8797c478bd9Sstevel@tonic-gate  *   input: uint8_t *	: place to store the converted little endian numbers
8807c478bd9Sstevel@tonic-gate  *	    uint32_t *	: place to get numbers to convert from
8817c478bd9Sstevel@tonic-gate  *          size_t	: the length of the input in bytes
8827c478bd9Sstevel@tonic-gate  *  output: void
8837c478bd9Sstevel@tonic-gate  */
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate static void
8867c478bd9Sstevel@tonic-gate Encode(uint8_t *restrict output, uint32_t *restrict input, size_t input_len)
8877c478bd9Sstevel@tonic-gate {
8887c478bd9Sstevel@tonic-gate 	size_t		i, j;
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 	for (i = 0, j = 0; j < input_len; i++, j += sizeof (uint32_t)) {
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate #ifdef _LITTLE_ENDIAN
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate #ifdef _MD5_CHECK_ALIGNMENT
8957c478bd9Sstevel@tonic-gate 		if ((uintptr_t)output & 0x3)	/* Not 4-byte aligned */
8967c478bd9Sstevel@tonic-gate 			bcopy(input + i, output + j, 4);
8977c478bd9Sstevel@tonic-gate 		else *(uint32_t *)(output + j) = input[i];
8987c478bd9Sstevel@tonic-gate #else
8997c478bd9Sstevel@tonic-gate 		*(uint32_t *)(output + j) = input[i];
9007c478bd9Sstevel@tonic-gate #endif /* _MD5_CHECK_ALIGNMENT */
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate #else	/* big endian -- will work on little endian, but slowly */
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 		output[j] = input[i] & 0xff;
9057c478bd9Sstevel@tonic-gate 		output[j + 1] = (input[i] >> 8)  & 0xff;
9067c478bd9Sstevel@tonic-gate 		output[j + 2] = (input[i] >> 16) & 0xff;
9077c478bd9Sstevel@tonic-gate 		output[j + 3] = (input[i] >> 24) & 0xff;
9087c478bd9Sstevel@tonic-gate #endif
9097c478bd9Sstevel@tonic-gate 	}
9107c478bd9Sstevel@tonic-gate }
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate #if	defined(_KERNEL) && !defined(_BOOT)
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate /*
9157c478bd9Sstevel@tonic-gate  * KCF software provider control entry points.
9167c478bd9Sstevel@tonic-gate  */
9177c478bd9Sstevel@tonic-gate /* ARGSUSED */
9187c478bd9Sstevel@tonic-gate static void
9197c478bd9Sstevel@tonic-gate md5_provider_status(crypto_provider_handle_t provider, uint_t *status)
9207c478bd9Sstevel@tonic-gate {
9217c478bd9Sstevel@tonic-gate 	*status = CRYPTO_PROVIDER_READY;
9227c478bd9Sstevel@tonic-gate }
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate /*
9257c478bd9Sstevel@tonic-gate  * KCF software provider digest entry points.
9267c478bd9Sstevel@tonic-gate  */
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate static int
9297c478bd9Sstevel@tonic-gate md5_digest_init(crypto_ctx_t *ctx, crypto_mechanism_t *mechanism,
9307c478bd9Sstevel@tonic-gate     crypto_req_handle_t req)
9317c478bd9Sstevel@tonic-gate {
9327c478bd9Sstevel@tonic-gate 	if (mechanism->cm_type != MD5_MECH_INFO_TYPE)
9337c478bd9Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 	/*
9367c478bd9Sstevel@tonic-gate 	 * Allocate and initialize MD5 context.
9377c478bd9Sstevel@tonic-gate 	 */
9387c478bd9Sstevel@tonic-gate 	ctx->cc_provider_private = kmem_alloc(sizeof (md5_ctx_t),
9397c478bd9Sstevel@tonic-gate 	    crypto_kmflag(req));
9407c478bd9Sstevel@tonic-gate 	if (ctx->cc_provider_private == NULL)
9417c478bd9Sstevel@tonic-gate 		return (CRYPTO_HOST_MEMORY);
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 	PROV_MD5_CTX(ctx)->mc_mech_type = MD5_MECH_INFO_TYPE;
9447c478bd9Sstevel@tonic-gate 	MD5Init(&PROV_MD5_CTX(ctx)->mc_md5_ctx);
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
9477c478bd9Sstevel@tonic-gate }
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate /*
9507c478bd9Sstevel@tonic-gate  * Helper MD5 digest update function for uio data.
9517c478bd9Sstevel@tonic-gate  */
9527c478bd9Sstevel@tonic-gate static int
9537c478bd9Sstevel@tonic-gate md5_digest_update_uio(MD5_CTX *md5_ctx, crypto_data_t *data)
9547c478bd9Sstevel@tonic-gate {
9557c478bd9Sstevel@tonic-gate 	off_t offset = data->cd_offset;
9567c478bd9Sstevel@tonic-gate 	size_t length = data->cd_length;
9577c478bd9Sstevel@tonic-gate 	uint_t vec_idx;
9587c478bd9Sstevel@tonic-gate 	size_t cur_len;
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	/* we support only kernel buffer */
9617c478bd9Sstevel@tonic-gate 	if (data->cd_uio->uio_segflg != UIO_SYSSPACE)
9627c478bd9Sstevel@tonic-gate 		return (CRYPTO_ARGUMENTS_BAD);
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 	/*
9657c478bd9Sstevel@tonic-gate 	 * Jump to the first iovec containing data to be
9667c478bd9Sstevel@tonic-gate 	 * digested.
9677c478bd9Sstevel@tonic-gate 	 */
9687c478bd9Sstevel@tonic-gate 	for (vec_idx = 0; vec_idx < data->cd_uio->uio_iovcnt &&
9697c478bd9Sstevel@tonic-gate 	    offset >= data->cd_uio->uio_iov[vec_idx].iov_len;
9707c478bd9Sstevel@tonic-gate 	    offset -= data->cd_uio->uio_iov[vec_idx++].iov_len);
9717c478bd9Sstevel@tonic-gate 	if (vec_idx == data->cd_uio->uio_iovcnt) {
9727c478bd9Sstevel@tonic-gate 		/*
9737c478bd9Sstevel@tonic-gate 		 * The caller specified an offset that is larger than the
9747c478bd9Sstevel@tonic-gate 		 * total size of the buffers it provided.
9757c478bd9Sstevel@tonic-gate 		 */
9767c478bd9Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
9777c478bd9Sstevel@tonic-gate 	}
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 	/*
9807c478bd9Sstevel@tonic-gate 	 * Now do the digesting on the iovecs.
9817c478bd9Sstevel@tonic-gate 	 */
9827c478bd9Sstevel@tonic-gate 	while (vec_idx < data->cd_uio->uio_iovcnt && length > 0) {
9837c478bd9Sstevel@tonic-gate 		cur_len = MIN(data->cd_uio->uio_iov[vec_idx].iov_len -
9847c478bd9Sstevel@tonic-gate 		    offset, length);
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 		MD5Update(md5_ctx, data->cd_uio->uio_iov[vec_idx].iov_base +
9877c478bd9Sstevel@tonic-gate 		    offset, cur_len);
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 		length -= cur_len;
9907c478bd9Sstevel@tonic-gate 		vec_idx++;
9917c478bd9Sstevel@tonic-gate 		offset = 0;
9927c478bd9Sstevel@tonic-gate 	}
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate 	if (vec_idx == data->cd_uio->uio_iovcnt && length > 0) {
9957c478bd9Sstevel@tonic-gate 		/*
9967c478bd9Sstevel@tonic-gate 		 * The end of the specified iovec's was reached but
9977c478bd9Sstevel@tonic-gate 		 * the length requested could not be processed, i.e.
9987c478bd9Sstevel@tonic-gate 		 * The caller requested to digest more data than it provided.
9997c478bd9Sstevel@tonic-gate 		 */
10007c478bd9Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
10017c478bd9Sstevel@tonic-gate 	}
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
10047c478bd9Sstevel@tonic-gate }
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate /*
10077c478bd9Sstevel@tonic-gate  * Helper MD5 digest final function for uio data.
10087c478bd9Sstevel@tonic-gate  * digest_len is the length of the desired digest. If digest_len
10097c478bd9Sstevel@tonic-gate  * is smaller than the default MD5 digest length, the caller
10107c478bd9Sstevel@tonic-gate  * must pass a scratch buffer, digest_scratch, which must
10117c478bd9Sstevel@tonic-gate  * be at least MD5_DIGEST_LENGTH bytes.
10127c478bd9Sstevel@tonic-gate  */
10137c478bd9Sstevel@tonic-gate static int
10147c478bd9Sstevel@tonic-gate md5_digest_final_uio(MD5_CTX *md5_ctx, crypto_data_t *digest,
10157c478bd9Sstevel@tonic-gate     ulong_t digest_len, uchar_t *digest_scratch)
10167c478bd9Sstevel@tonic-gate {
10177c478bd9Sstevel@tonic-gate 	off_t offset = digest->cd_offset;
10187c478bd9Sstevel@tonic-gate 	uint_t vec_idx;
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 	/* we support only kernel buffer */
10217c478bd9Sstevel@tonic-gate 	if (digest->cd_uio->uio_segflg != UIO_SYSSPACE)
10227c478bd9Sstevel@tonic-gate 		return (CRYPTO_ARGUMENTS_BAD);
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 	/*
10257c478bd9Sstevel@tonic-gate 	 * Jump to the first iovec containing ptr to the digest to
10267c478bd9Sstevel@tonic-gate 	 * be returned.
10277c478bd9Sstevel@tonic-gate 	 */
10287c478bd9Sstevel@tonic-gate 	for (vec_idx = 0; offset >= digest->cd_uio->uio_iov[vec_idx].iov_len &&
10297c478bd9Sstevel@tonic-gate 	    vec_idx < digest->cd_uio->uio_iovcnt;
10307c478bd9Sstevel@tonic-gate 	    offset -= digest->cd_uio->uio_iov[vec_idx++].iov_len);
10317c478bd9Sstevel@tonic-gate 	if (vec_idx == digest->cd_uio->uio_iovcnt) {
10327c478bd9Sstevel@tonic-gate 		/*
10337c478bd9Sstevel@tonic-gate 		 * The caller specified an offset that is
10347c478bd9Sstevel@tonic-gate 		 * larger than the total size of the buffers
10357c478bd9Sstevel@tonic-gate 		 * it provided.
10367c478bd9Sstevel@tonic-gate 		 */
10377c478bd9Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
10387c478bd9Sstevel@tonic-gate 	}
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 	if (offset + digest_len <=
10417c478bd9Sstevel@tonic-gate 	    digest->cd_uio->uio_iov[vec_idx].iov_len) {
10427c478bd9Sstevel@tonic-gate 		/*
10437c478bd9Sstevel@tonic-gate 		 * The computed MD5 digest will fit in the current
10447c478bd9Sstevel@tonic-gate 		 * iovec.
10457c478bd9Sstevel@tonic-gate 		 */
10467c478bd9Sstevel@tonic-gate 		if (digest_len != MD5_DIGEST_LENGTH) {
10477c478bd9Sstevel@tonic-gate 			/*
10487c478bd9Sstevel@tonic-gate 			 * The caller requested a short digest. Digest
10497c478bd9Sstevel@tonic-gate 			 * into a scratch buffer and return to
10507c478bd9Sstevel@tonic-gate 			 * the user only what was requested.
10517c478bd9Sstevel@tonic-gate 			 */
10527c478bd9Sstevel@tonic-gate 			MD5Final(digest_scratch, md5_ctx);
10537c478bd9Sstevel@tonic-gate 			bcopy(digest_scratch, (uchar_t *)digest->
10547c478bd9Sstevel@tonic-gate 			    cd_uio->uio_iov[vec_idx].iov_base + offset,
10557c478bd9Sstevel@tonic-gate 			    digest_len);
10567c478bd9Sstevel@tonic-gate 		} else {
10577c478bd9Sstevel@tonic-gate 			MD5Final((uchar_t *)digest->
10587c478bd9Sstevel@tonic-gate 			    cd_uio->uio_iov[vec_idx].iov_base + offset,
10597c478bd9Sstevel@tonic-gate 			    md5_ctx);
10607c478bd9Sstevel@tonic-gate 		}
10617c478bd9Sstevel@tonic-gate 	} else {
10627c478bd9Sstevel@tonic-gate 		/*
10637c478bd9Sstevel@tonic-gate 		 * The computed digest will be crossing one or more iovec's.
10647c478bd9Sstevel@tonic-gate 		 * This is bad performance-wise but we need to support it.
10657c478bd9Sstevel@tonic-gate 		 * Allocate a small scratch buffer on the stack and
10667c478bd9Sstevel@tonic-gate 		 * copy it piece meal to the specified digest iovec's.
10677c478bd9Sstevel@tonic-gate 		 */
10687c478bd9Sstevel@tonic-gate 		uchar_t digest_tmp[MD5_DIGEST_LENGTH];
10697c478bd9Sstevel@tonic-gate 		off_t scratch_offset = 0;
10707c478bd9Sstevel@tonic-gate 		size_t length = digest_len;
10717c478bd9Sstevel@tonic-gate 		size_t cur_len;
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 		MD5Final(digest_tmp, md5_ctx);
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 		while (vec_idx < digest->cd_uio->uio_iovcnt && length > 0) {
10767c478bd9Sstevel@tonic-gate 			cur_len = MIN(digest->cd_uio->uio_iov[vec_idx].iov_len -
10777c478bd9Sstevel@tonic-gate 			    offset, length);
10787c478bd9Sstevel@tonic-gate 			bcopy(digest_tmp + scratch_offset,
10797c478bd9Sstevel@tonic-gate 			    digest->cd_uio->uio_iov[vec_idx].iov_base + offset,
10807c478bd9Sstevel@tonic-gate 			    cur_len);
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 			length -= cur_len;
10837c478bd9Sstevel@tonic-gate 			vec_idx++;
10847c478bd9Sstevel@tonic-gate 			scratch_offset += cur_len;
10857c478bd9Sstevel@tonic-gate 			offset = 0;
10867c478bd9Sstevel@tonic-gate 		}
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 		if (vec_idx == digest->cd_uio->uio_iovcnt && length > 0) {
10897c478bd9Sstevel@tonic-gate 			/*
10907c478bd9Sstevel@tonic-gate 			 * The end of the specified iovec's was reached but
10917c478bd9Sstevel@tonic-gate 			 * the length requested could not be processed, i.e.
10927c478bd9Sstevel@tonic-gate 			 * The caller requested to digest more data than it
10937c478bd9Sstevel@tonic-gate 			 * provided.
10947c478bd9Sstevel@tonic-gate 			 */
10957c478bd9Sstevel@tonic-gate 			return (CRYPTO_DATA_LEN_RANGE);
10967c478bd9Sstevel@tonic-gate 		}
10977c478bd9Sstevel@tonic-gate 	}
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
11007c478bd9Sstevel@tonic-gate }
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate /*
11037c478bd9Sstevel@tonic-gate  * Helper MD5 digest update for mblk's.
11047c478bd9Sstevel@tonic-gate  */
11057c478bd9Sstevel@tonic-gate static int
11067c478bd9Sstevel@tonic-gate md5_digest_update_mblk(MD5_CTX *md5_ctx, crypto_data_t *data)
11077c478bd9Sstevel@tonic-gate {
11087c478bd9Sstevel@tonic-gate 	off_t offset = data->cd_offset;
11097c478bd9Sstevel@tonic-gate 	size_t length = data->cd_length;
11107c478bd9Sstevel@tonic-gate 	mblk_t *mp;
11117c478bd9Sstevel@tonic-gate 	size_t cur_len;
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate 	/*
11147c478bd9Sstevel@tonic-gate 	 * Jump to the first mblk_t containing data to be digested.
11157c478bd9Sstevel@tonic-gate 	 */
11167c478bd9Sstevel@tonic-gate 	for (mp = data->cd_mp; mp != NULL && offset >= MBLKL(mp);
11177c478bd9Sstevel@tonic-gate 	    offset -= MBLKL(mp), mp = mp->b_cont);
11187c478bd9Sstevel@tonic-gate 	if (mp == NULL) {
11197c478bd9Sstevel@tonic-gate 		/*
11207c478bd9Sstevel@tonic-gate 		 * The caller specified an offset that is larger than the
11217c478bd9Sstevel@tonic-gate 		 * total size of the buffers it provided.
11227c478bd9Sstevel@tonic-gate 		 */
11237c478bd9Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
11247c478bd9Sstevel@tonic-gate 	}
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 	/*
11277c478bd9Sstevel@tonic-gate 	 * Now do the digesting on the mblk chain.
11287c478bd9Sstevel@tonic-gate 	 */
11297c478bd9Sstevel@tonic-gate 	while (mp != NULL && length > 0) {
11307c478bd9Sstevel@tonic-gate 		cur_len = MIN(MBLKL(mp) - offset, length);
11317c478bd9Sstevel@tonic-gate 		MD5Update(md5_ctx, mp->b_rptr + offset, cur_len);
11327c478bd9Sstevel@tonic-gate 		length -= cur_len;
11337c478bd9Sstevel@tonic-gate 		offset = 0;
11347c478bd9Sstevel@tonic-gate 		mp = mp->b_cont;
11357c478bd9Sstevel@tonic-gate 	}
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 	if (mp == NULL && length > 0) {
11387c478bd9Sstevel@tonic-gate 		/*
11397c478bd9Sstevel@tonic-gate 		 * The end of the mblk was reached but the length requested
11407c478bd9Sstevel@tonic-gate 		 * could not be processed, i.e. The caller requested
11417c478bd9Sstevel@tonic-gate 		 * to digest more data than it provided.
11427c478bd9Sstevel@tonic-gate 		 */
11437c478bd9Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
11447c478bd9Sstevel@tonic-gate 	}
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
11477c478bd9Sstevel@tonic-gate }
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate /*
11507c478bd9Sstevel@tonic-gate  * Helper MD5 digest final for mblk's.
11517c478bd9Sstevel@tonic-gate  * digest_len is the length of the desired digest. If digest_len
11527c478bd9Sstevel@tonic-gate  * is smaller than the default MD5 digest length, the caller
11537c478bd9Sstevel@tonic-gate  * must pass a scratch buffer, digest_scratch, which must
11547c478bd9Sstevel@tonic-gate  * be at least MD5_DIGEST_LENGTH bytes.
11557c478bd9Sstevel@tonic-gate  */
11567c478bd9Sstevel@tonic-gate static int
11577c478bd9Sstevel@tonic-gate md5_digest_final_mblk(MD5_CTX *md5_ctx, crypto_data_t *digest,
11587c478bd9Sstevel@tonic-gate     ulong_t digest_len, uchar_t *digest_scratch)
11597c478bd9Sstevel@tonic-gate {
11607c478bd9Sstevel@tonic-gate 	off_t offset = digest->cd_offset;
11617c478bd9Sstevel@tonic-gate 	mblk_t *mp;
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	/*
11647c478bd9Sstevel@tonic-gate 	 * Jump to the first mblk_t that will be used to store the digest.
11657c478bd9Sstevel@tonic-gate 	 */
11667c478bd9Sstevel@tonic-gate 	for (mp = digest->cd_mp; mp != NULL && offset >= MBLKL(mp);
11677c478bd9Sstevel@tonic-gate 	    offset -= MBLKL(mp), mp = mp->b_cont);
11687c478bd9Sstevel@tonic-gate 	if (mp == NULL) {
11697c478bd9Sstevel@tonic-gate 		/*
11707c478bd9Sstevel@tonic-gate 		 * The caller specified an offset that is larger than the
11717c478bd9Sstevel@tonic-gate 		 * total size of the buffers it provided.
11727c478bd9Sstevel@tonic-gate 		 */
11737c478bd9Sstevel@tonic-gate 		return (CRYPTO_DATA_LEN_RANGE);
11747c478bd9Sstevel@tonic-gate 	}
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 	if (offset + digest_len <= MBLKL(mp)) {
11777c478bd9Sstevel@tonic-gate 		/*
11787c478bd9Sstevel@tonic-gate 		 * The computed MD5 digest will fit in the current mblk.
11797c478bd9Sstevel@tonic-gate 		 * Do the MD5Final() in-place.
11807c478bd9Sstevel@tonic-gate 		 */
11817c478bd9Sstevel@tonic-gate 		if (digest_len != MD5_DIGEST_LENGTH) {
11827c478bd9Sstevel@tonic-gate 			/*
11837c478bd9Sstevel@tonic-gate 			 * The caller requested a short digest. Digest
11847c478bd9Sstevel@tonic-gate 			 * into a scratch buffer and return to
11857c478bd9Sstevel@tonic-gate 			 * the user only what was requested.
11867c478bd9Sstevel@tonic-gate 			 */
11877c478bd9Sstevel@tonic-gate 			MD5Final(digest_scratch, md5_ctx);
11887c478bd9Sstevel@tonic-gate 			bcopy(digest_scratch, mp->b_rptr + offset, digest_len);
11897c478bd9Sstevel@tonic-gate 		} else {
11907c478bd9Sstevel@tonic-gate 			MD5Final(mp->b_rptr + offset, md5_ctx);
11917c478bd9Sstevel@tonic-gate 		}
11927c478bd9Sstevel@tonic-gate 	} else {
11937c478bd9Sstevel@tonic-gate 		/*
11947c478bd9Sstevel@tonic-gate 		 * The computed digest will be crossing one or more mblk's.
11957c478bd9Sstevel@tonic-gate 		 * This is bad performance-wise but we need to support it.
11967c478bd9Sstevel@tonic-gate 		 * Allocate a small scratch buffer on the stack and
11977c478bd9Sstevel@tonic-gate 		 * copy it piece meal to the specified digest iovec's.
11987c478bd9Sstevel@tonic-gate 		 */
11997c478bd9Sstevel@tonic-gate 		uchar_t digest_tmp[MD5_DIGEST_LENGTH];
12007c478bd9Sstevel@tonic-gate 		off_t scratch_offset = 0;
12017c478bd9Sstevel@tonic-gate 		size_t length = digest_len;
12027c478bd9Sstevel@tonic-gate 		size_t cur_len;
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate 		MD5Final(digest_tmp, md5_ctx);
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate 		while (mp != NULL && length > 0) {
12077c478bd9Sstevel@tonic-gate 			cur_len = MIN(MBLKL(mp) - offset, length);
12087c478bd9Sstevel@tonic-gate 			bcopy(digest_tmp + scratch_offset,
12097c478bd9Sstevel@tonic-gate 			    mp->b_rptr + offset, cur_len);
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate 			length -= cur_len;
12127c478bd9Sstevel@tonic-gate 			mp = mp->b_cont;
12137c478bd9Sstevel@tonic-gate 			scratch_offset += cur_len;
12147c478bd9Sstevel@tonic-gate 			offset = 0;
12157c478bd9Sstevel@tonic-gate 		}
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate 		if (mp == NULL && length > 0) {
12187c478bd9Sstevel@tonic-gate 			/*
12197c478bd9Sstevel@tonic-gate 			 * The end of the specified mblk was reached but
12207c478bd9Sstevel@tonic-gate 			 * the length requested could not be processed, i.e.
12217c478bd9Sstevel@tonic-gate 			 * The caller requested to digest more data than it
12227c478bd9Sstevel@tonic-gate 			 * provided.
12237c478bd9Sstevel@tonic-gate 			 */
12247c478bd9Sstevel@tonic-gate 			return (CRYPTO_DATA_LEN_RANGE);
12257c478bd9Sstevel@tonic-gate 		}
12267c478bd9Sstevel@tonic-gate 	}
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
12297c478bd9Sstevel@tonic-gate }
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate /* ARGSUSED */
12327c478bd9Sstevel@tonic-gate static int
12337c478bd9Sstevel@tonic-gate md5_digest(crypto_ctx_t *ctx, crypto_data_t *data, crypto_data_t *digest,
12347c478bd9Sstevel@tonic-gate     crypto_req_handle_t req)
12357c478bd9Sstevel@tonic-gate {
12367c478bd9Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate 	/*
12417c478bd9Sstevel@tonic-gate 	 * We need to just return the length needed to store the output.
12427c478bd9Sstevel@tonic-gate 	 * We should not destroy the context for the following cases.
12437c478bd9Sstevel@tonic-gate 	 */
12447c478bd9Sstevel@tonic-gate 	if ((digest->cd_length == 0) ||
12457c478bd9Sstevel@tonic-gate 	    (digest->cd_length < MD5_DIGEST_LENGTH)) {
12467c478bd9Sstevel@tonic-gate 		digest->cd_length = MD5_DIGEST_LENGTH;
12477c478bd9Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
12487c478bd9Sstevel@tonic-gate 	}
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 	/*
12517c478bd9Sstevel@tonic-gate 	 * Do the MD5 update on the specified input data.
12527c478bd9Sstevel@tonic-gate 	 */
12537c478bd9Sstevel@tonic-gate 	switch (data->cd_format) {
12547c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
12557c478bd9Sstevel@tonic-gate 		MD5Update(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
12567c478bd9Sstevel@tonic-gate 		    data->cd_raw.iov_base + data->cd_offset,
12577c478bd9Sstevel@tonic-gate 		    data->cd_length);
12587c478bd9Sstevel@tonic-gate 		break;
12597c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
12607c478bd9Sstevel@tonic-gate 		ret = md5_digest_update_uio(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
12617c478bd9Sstevel@tonic-gate 		    data);
12627c478bd9Sstevel@tonic-gate 		break;
12637c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
12647c478bd9Sstevel@tonic-gate 		ret = md5_digest_update_mblk(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
12657c478bd9Sstevel@tonic-gate 		    data);
12667c478bd9Sstevel@tonic-gate 		break;
12677c478bd9Sstevel@tonic-gate 	default:
12687c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
12697c478bd9Sstevel@tonic-gate 	}
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 	if (ret != CRYPTO_SUCCESS) {
12727c478bd9Sstevel@tonic-gate 		/* the update failed, free context and bail */
12737c478bd9Sstevel@tonic-gate 		kmem_free(ctx->cc_provider_private, sizeof (md5_ctx_t));
12747c478bd9Sstevel@tonic-gate 		ctx->cc_provider_private = NULL;
12757c478bd9Sstevel@tonic-gate 		digest->cd_length = 0;
12767c478bd9Sstevel@tonic-gate 		return (ret);
12777c478bd9Sstevel@tonic-gate 	}
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate 	/*
12807c478bd9Sstevel@tonic-gate 	 * Do an MD5 final, must be done separately since the digest
12817c478bd9Sstevel@tonic-gate 	 * type can be different than the input data type.
12827c478bd9Sstevel@tonic-gate 	 */
12837c478bd9Sstevel@tonic-gate 	switch (digest->cd_format) {
12847c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
12857c478bd9Sstevel@tonic-gate 		MD5Final((unsigned char *)digest->cd_raw.iov_base +
12867c478bd9Sstevel@tonic-gate 		    digest->cd_offset, &PROV_MD5_CTX(ctx)->mc_md5_ctx);
12877c478bd9Sstevel@tonic-gate 		break;
12887c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
12897c478bd9Sstevel@tonic-gate 		ret = md5_digest_final_uio(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
12907c478bd9Sstevel@tonic-gate 		    digest, MD5_DIGEST_LENGTH, NULL);
12917c478bd9Sstevel@tonic-gate 		break;
12927c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
12937c478bd9Sstevel@tonic-gate 		ret = md5_digest_final_mblk(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
12947c478bd9Sstevel@tonic-gate 		    digest, MD5_DIGEST_LENGTH, NULL);
12957c478bd9Sstevel@tonic-gate 		break;
12967c478bd9Sstevel@tonic-gate 	default:
12977c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
12987c478bd9Sstevel@tonic-gate 	}
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate 	/* all done, free context and return */
13017c478bd9Sstevel@tonic-gate 
13027c478bd9Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
13037c478bd9Sstevel@tonic-gate 		digest->cd_length = MD5_DIGEST_LENGTH;
13047c478bd9Sstevel@tonic-gate 	} else {
13057c478bd9Sstevel@tonic-gate 		digest->cd_length = 0;
13067c478bd9Sstevel@tonic-gate 	}
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 	kmem_free(ctx->cc_provider_private, sizeof (md5_ctx_t));
13097c478bd9Sstevel@tonic-gate 	ctx->cc_provider_private = NULL;
13107c478bd9Sstevel@tonic-gate 	return (ret);
13117c478bd9Sstevel@tonic-gate }
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate /* ARGSUSED */
13147c478bd9Sstevel@tonic-gate static int
13157c478bd9Sstevel@tonic-gate md5_digest_update(crypto_ctx_t *ctx, crypto_data_t *data,
13167c478bd9Sstevel@tonic-gate     crypto_req_handle_t req)
13177c478bd9Sstevel@tonic-gate {
13187c478bd9Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate 	/*
13237c478bd9Sstevel@tonic-gate 	 * Do the MD5 update on the specified input data.
13247c478bd9Sstevel@tonic-gate 	 */
13257c478bd9Sstevel@tonic-gate 	switch (data->cd_format) {
13267c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
13277c478bd9Sstevel@tonic-gate 		MD5Update(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
13287c478bd9Sstevel@tonic-gate 		    data->cd_raw.iov_base + data->cd_offset,
13297c478bd9Sstevel@tonic-gate 		    data->cd_length);
13307c478bd9Sstevel@tonic-gate 		break;
13317c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
13327c478bd9Sstevel@tonic-gate 		ret = md5_digest_update_uio(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
13337c478bd9Sstevel@tonic-gate 		    data);
13347c478bd9Sstevel@tonic-gate 		break;
13357c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
13367c478bd9Sstevel@tonic-gate 		ret = md5_digest_update_mblk(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
13377c478bd9Sstevel@tonic-gate 		    data);
13387c478bd9Sstevel@tonic-gate 		break;
13397c478bd9Sstevel@tonic-gate 	default:
13407c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
13417c478bd9Sstevel@tonic-gate 	}
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate 	return (ret);
13447c478bd9Sstevel@tonic-gate }
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate /* ARGSUSED */
13477c478bd9Sstevel@tonic-gate static int
13487c478bd9Sstevel@tonic-gate md5_digest_final(crypto_ctx_t *ctx, crypto_data_t *digest,
13497c478bd9Sstevel@tonic-gate     crypto_req_handle_t req)
13507c478bd9Sstevel@tonic-gate {
13517c478bd9Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
13527c478bd9Sstevel@tonic-gate 
13537c478bd9Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate 	/*
13567c478bd9Sstevel@tonic-gate 	 * We need to just return the length needed to store the output.
13577c478bd9Sstevel@tonic-gate 	 * We should not destroy the context for the following cases.
13587c478bd9Sstevel@tonic-gate 	 */
13597c478bd9Sstevel@tonic-gate 	if ((digest->cd_length == 0) ||
13607c478bd9Sstevel@tonic-gate 	    (digest->cd_length < MD5_DIGEST_LENGTH)) {
13617c478bd9Sstevel@tonic-gate 		digest->cd_length = MD5_DIGEST_LENGTH;
13627c478bd9Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
13637c478bd9Sstevel@tonic-gate 	}
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 	/*
13667c478bd9Sstevel@tonic-gate 	 * Do an MD5 final.
13677c478bd9Sstevel@tonic-gate 	 */
13687c478bd9Sstevel@tonic-gate 	switch (digest->cd_format) {
13697c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
13707c478bd9Sstevel@tonic-gate 		MD5Final((unsigned char *)digest->cd_raw.iov_base +
13717c478bd9Sstevel@tonic-gate 		    digest->cd_offset, &PROV_MD5_CTX(ctx)->mc_md5_ctx);
13727c478bd9Sstevel@tonic-gate 		break;
13737c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
13747c478bd9Sstevel@tonic-gate 		ret = md5_digest_final_uio(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
13757c478bd9Sstevel@tonic-gate 		    digest, MD5_DIGEST_LENGTH, NULL);
13767c478bd9Sstevel@tonic-gate 		break;
13777c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
13787c478bd9Sstevel@tonic-gate 		ret = md5_digest_final_mblk(&PROV_MD5_CTX(ctx)->mc_md5_ctx,
13797c478bd9Sstevel@tonic-gate 		    digest, MD5_DIGEST_LENGTH, NULL);
13807c478bd9Sstevel@tonic-gate 		break;
13817c478bd9Sstevel@tonic-gate 	default:
13827c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
13837c478bd9Sstevel@tonic-gate 	}
13847c478bd9Sstevel@tonic-gate 
13857c478bd9Sstevel@tonic-gate 	/* all done, free context and return */
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
13887c478bd9Sstevel@tonic-gate 		digest->cd_length = MD5_DIGEST_LENGTH;
13897c478bd9Sstevel@tonic-gate 	} else {
13907c478bd9Sstevel@tonic-gate 		digest->cd_length = 0;
13917c478bd9Sstevel@tonic-gate 	}
13927c478bd9Sstevel@tonic-gate 
13937c478bd9Sstevel@tonic-gate 	kmem_free(ctx->cc_provider_private, sizeof (md5_ctx_t));
13947c478bd9Sstevel@tonic-gate 	ctx->cc_provider_private = NULL;
13957c478bd9Sstevel@tonic-gate 
13967c478bd9Sstevel@tonic-gate 	return (ret);
13977c478bd9Sstevel@tonic-gate }
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate /* ARGSUSED */
14007c478bd9Sstevel@tonic-gate static int
14017c478bd9Sstevel@tonic-gate md5_digest_atomic(crypto_provider_handle_t provider,
14027c478bd9Sstevel@tonic-gate     crypto_session_id_t session_id, crypto_mechanism_t *mechanism,
14037c478bd9Sstevel@tonic-gate     crypto_data_t *data, crypto_data_t *digest,
14047c478bd9Sstevel@tonic-gate     crypto_req_handle_t req)
14057c478bd9Sstevel@tonic-gate {
14067c478bd9Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
14077c478bd9Sstevel@tonic-gate 	MD5_CTX md5_ctx;
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate 	if (mechanism->cm_type != MD5_MECH_INFO_TYPE)
14107c478bd9Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate 	/*
14137c478bd9Sstevel@tonic-gate 	 * Do the MD5 init.
14147c478bd9Sstevel@tonic-gate 	 */
14157c478bd9Sstevel@tonic-gate 	MD5Init(&md5_ctx);
14167c478bd9Sstevel@tonic-gate 
14177c478bd9Sstevel@tonic-gate 	/*
14187c478bd9Sstevel@tonic-gate 	 * Do the MD5 update on the specified input data.
14197c478bd9Sstevel@tonic-gate 	 */
14207c478bd9Sstevel@tonic-gate 	switch (data->cd_format) {
14217c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
14227c478bd9Sstevel@tonic-gate 		MD5Update(&md5_ctx, data->cd_raw.iov_base + data->cd_offset,
14237c478bd9Sstevel@tonic-gate 		    data->cd_length);
14247c478bd9Sstevel@tonic-gate 		break;
14257c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
14267c478bd9Sstevel@tonic-gate 		ret = md5_digest_update_uio(&md5_ctx, data);
14277c478bd9Sstevel@tonic-gate 		break;
14287c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
14297c478bd9Sstevel@tonic-gate 		ret = md5_digest_update_mblk(&md5_ctx, data);
14307c478bd9Sstevel@tonic-gate 		break;
14317c478bd9Sstevel@tonic-gate 	default:
14327c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
14337c478bd9Sstevel@tonic-gate 	}
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate 	if (ret != CRYPTO_SUCCESS) {
14367c478bd9Sstevel@tonic-gate 		/* the update failed, bail */
14377c478bd9Sstevel@tonic-gate 		digest->cd_length = 0;
14387c478bd9Sstevel@tonic-gate 		return (ret);
14397c478bd9Sstevel@tonic-gate 	}
14407c478bd9Sstevel@tonic-gate 
14417c478bd9Sstevel@tonic-gate 	/*
14427c478bd9Sstevel@tonic-gate 	 * Do an MD5 final, must be done separately since the digest
14437c478bd9Sstevel@tonic-gate 	 * type can be different than the input data type.
14447c478bd9Sstevel@tonic-gate 	 */
14457c478bd9Sstevel@tonic-gate 	switch (digest->cd_format) {
14467c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
14477c478bd9Sstevel@tonic-gate 		MD5Final((unsigned char *)digest->cd_raw.iov_base +
14487c478bd9Sstevel@tonic-gate 		    digest->cd_offset, &md5_ctx);
14497c478bd9Sstevel@tonic-gate 		break;
14507c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
14517c478bd9Sstevel@tonic-gate 		ret = md5_digest_final_uio(&md5_ctx, digest,
14527c478bd9Sstevel@tonic-gate 		    MD5_DIGEST_LENGTH, NULL);
14537c478bd9Sstevel@tonic-gate 		break;
14547c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
14557c478bd9Sstevel@tonic-gate 		ret = md5_digest_final_mblk(&md5_ctx, digest,
14567c478bd9Sstevel@tonic-gate 		    MD5_DIGEST_LENGTH, NULL);
14577c478bd9Sstevel@tonic-gate 		break;
14587c478bd9Sstevel@tonic-gate 	default:
14597c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
14607c478bd9Sstevel@tonic-gate 	}
14617c478bd9Sstevel@tonic-gate 
14627c478bd9Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
14637c478bd9Sstevel@tonic-gate 		digest->cd_length = MD5_DIGEST_LENGTH;
14647c478bd9Sstevel@tonic-gate 	} else {
14657c478bd9Sstevel@tonic-gate 		digest->cd_length = 0;
14667c478bd9Sstevel@tonic-gate 	}
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate 	return (ret);
14697c478bd9Sstevel@tonic-gate }
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate /*
14727c478bd9Sstevel@tonic-gate  * KCF software provider mac entry points.
14737c478bd9Sstevel@tonic-gate  *
14747c478bd9Sstevel@tonic-gate  * MD5 HMAC is: MD5(key XOR opad, MD5(key XOR ipad, text))
14757c478bd9Sstevel@tonic-gate  *
14767c478bd9Sstevel@tonic-gate  * Init:
14777c478bd9Sstevel@tonic-gate  * The initialization routine initializes what we denote
14787c478bd9Sstevel@tonic-gate  * as the inner and outer contexts by doing
14797c478bd9Sstevel@tonic-gate  * - for inner context: MD5(key XOR ipad)
14807c478bd9Sstevel@tonic-gate  * - for outer context: MD5(key XOR opad)
14817c478bd9Sstevel@tonic-gate  *
14827c478bd9Sstevel@tonic-gate  * Update:
14837c478bd9Sstevel@tonic-gate  * Each subsequent MD5 HMAC update will result in an
14847c478bd9Sstevel@tonic-gate  * update of the inner context with the specified data.
14857c478bd9Sstevel@tonic-gate  *
14867c478bd9Sstevel@tonic-gate  * Final:
14877c478bd9Sstevel@tonic-gate  * The MD5 HMAC final will do a MD5 final operation on the
14887c478bd9Sstevel@tonic-gate  * inner context, and the resulting digest will be used
14897c478bd9Sstevel@tonic-gate  * as the data for an update on the outer context. Last
14907c478bd9Sstevel@tonic-gate  * but not least, an MD5 final on the outer context will
14917c478bd9Sstevel@tonic-gate  * be performed to obtain the MD5 HMAC digest to return
14927c478bd9Sstevel@tonic-gate  * to the user.
14937c478bd9Sstevel@tonic-gate  */
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate /*
14967c478bd9Sstevel@tonic-gate  * Initialize a MD5-HMAC context.
14977c478bd9Sstevel@tonic-gate  */
14987c478bd9Sstevel@tonic-gate static void
14997c478bd9Sstevel@tonic-gate md5_mac_init_ctx(md5_hmac_ctx_t *ctx, void *keyval, uint_t length_in_bytes)
15007c478bd9Sstevel@tonic-gate {
15017c478bd9Sstevel@tonic-gate 	uint32_t ipad[MD5_HMAC_INTS_PER_BLOCK];
15027c478bd9Sstevel@tonic-gate 	uint32_t opad[MD5_HMAC_INTS_PER_BLOCK];
15037c478bd9Sstevel@tonic-gate 	uint_t i;
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate 	bzero(ipad, MD5_HMAC_BLOCK_SIZE);
15067c478bd9Sstevel@tonic-gate 	bzero(opad, MD5_HMAC_BLOCK_SIZE);
15077c478bd9Sstevel@tonic-gate 
15087c478bd9Sstevel@tonic-gate 	bcopy(keyval, ipad, length_in_bytes);
15097c478bd9Sstevel@tonic-gate 	bcopy(keyval, opad, length_in_bytes);
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate 	/* XOR key with ipad (0x36) and opad (0x5c) */
15127c478bd9Sstevel@tonic-gate 	for (i = 0; i < MD5_HMAC_INTS_PER_BLOCK; i++) {
15137c478bd9Sstevel@tonic-gate 		ipad[i] ^= 0x36363636;
15147c478bd9Sstevel@tonic-gate 		opad[i] ^= 0x5c5c5c5c;
15157c478bd9Sstevel@tonic-gate 	}
15167c478bd9Sstevel@tonic-gate 
15177c478bd9Sstevel@tonic-gate 	/* perform MD5 on ipad */
15187c478bd9Sstevel@tonic-gate 	MD5Init(&ctx->hc_icontext);
15197c478bd9Sstevel@tonic-gate 	MD5Update(&ctx->hc_icontext, ipad, MD5_HMAC_BLOCK_SIZE);
15207c478bd9Sstevel@tonic-gate 
15217c478bd9Sstevel@tonic-gate 	/* perform MD5 on opad */
15227c478bd9Sstevel@tonic-gate 	MD5Init(&ctx->hc_ocontext);
15237c478bd9Sstevel@tonic-gate 	MD5Update(&ctx->hc_ocontext, opad, MD5_HMAC_BLOCK_SIZE);
15247c478bd9Sstevel@tonic-gate }
15257c478bd9Sstevel@tonic-gate 
15267c478bd9Sstevel@tonic-gate /*
15277c478bd9Sstevel@tonic-gate  * Initializes a multi-part MAC operation.
15287c478bd9Sstevel@tonic-gate  */
15297c478bd9Sstevel@tonic-gate static int
15307c478bd9Sstevel@tonic-gate md5_mac_init(crypto_ctx_t *ctx, crypto_mechanism_t *mechanism,
15317c478bd9Sstevel@tonic-gate     crypto_key_t *key, crypto_spi_ctx_template_t ctx_template,
15327c478bd9Sstevel@tonic-gate     crypto_req_handle_t req)
15337c478bd9Sstevel@tonic-gate {
15347c478bd9Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
15357c478bd9Sstevel@tonic-gate 	uint_t keylen_in_bytes = CRYPTO_BITS2BYTES(key->ck_length);
15367c478bd9Sstevel@tonic-gate 
15377c478bd9Sstevel@tonic-gate 	if (mechanism->cm_type != MD5_HMAC_MECH_INFO_TYPE &&
15387c478bd9Sstevel@tonic-gate 	    mechanism->cm_type != MD5_HMAC_GEN_MECH_INFO_TYPE)
15397c478bd9Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate 	/* Add support for key by attributes (RFE 4706552) */
15427c478bd9Sstevel@tonic-gate 	if (key->ck_format != CRYPTO_KEY_RAW)
15437c478bd9Sstevel@tonic-gate 		return (CRYPTO_ARGUMENTS_BAD);
15447c478bd9Sstevel@tonic-gate 
15457c478bd9Sstevel@tonic-gate 	ctx->cc_provider_private = kmem_alloc(sizeof (md5_hmac_ctx_t),
15467c478bd9Sstevel@tonic-gate 	    crypto_kmflag(req));
15477c478bd9Sstevel@tonic-gate 	if (ctx->cc_provider_private == NULL)
15487c478bd9Sstevel@tonic-gate 		return (CRYPTO_HOST_MEMORY);
15497c478bd9Sstevel@tonic-gate 
15507c478bd9Sstevel@tonic-gate 	if (ctx_template != NULL) {
15517c478bd9Sstevel@tonic-gate 		/* reuse context template */
15527c478bd9Sstevel@tonic-gate 		bcopy(ctx_template, PROV_MD5_HMAC_CTX(ctx),
15537c478bd9Sstevel@tonic-gate 		    sizeof (md5_hmac_ctx_t));
15547c478bd9Sstevel@tonic-gate 	} else {
15557c478bd9Sstevel@tonic-gate 		/* no context template, compute context */
15567c478bd9Sstevel@tonic-gate 		if (keylen_in_bytes > MD5_HMAC_BLOCK_SIZE) {
15577c478bd9Sstevel@tonic-gate 			uchar_t digested_key[MD5_DIGEST_LENGTH];
15587c478bd9Sstevel@tonic-gate 			md5_hmac_ctx_t *hmac_ctx = ctx->cc_provider_private;
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate 			/*
15617c478bd9Sstevel@tonic-gate 			 * Hash the passed-in key to get a smaller key.
15627c478bd9Sstevel@tonic-gate 			 * The inner context is used since it hasn't been
15637c478bd9Sstevel@tonic-gate 			 * initialized yet.
15647c478bd9Sstevel@tonic-gate 			 */
15657c478bd9Sstevel@tonic-gate 			PROV_MD5_DIGEST_KEY(&hmac_ctx->hc_icontext,
15667c478bd9Sstevel@tonic-gate 			    key->ck_data, keylen_in_bytes, digested_key);
15677c478bd9Sstevel@tonic-gate 			md5_mac_init_ctx(PROV_MD5_HMAC_CTX(ctx),
15687c478bd9Sstevel@tonic-gate 			    digested_key, MD5_DIGEST_LENGTH);
15697c478bd9Sstevel@tonic-gate 		} else {
15707c478bd9Sstevel@tonic-gate 			md5_mac_init_ctx(PROV_MD5_HMAC_CTX(ctx),
15717c478bd9Sstevel@tonic-gate 			    key->ck_data, keylen_in_bytes);
15727c478bd9Sstevel@tonic-gate 		}
15737c478bd9Sstevel@tonic-gate 	}
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate 	/*
15767c478bd9Sstevel@tonic-gate 	 * Get the mechanism parameters, if applicable.
15777c478bd9Sstevel@tonic-gate 	 */
15787c478bd9Sstevel@tonic-gate 	PROV_MD5_HMAC_CTX(ctx)->hc_mech_type = mechanism->cm_type;
15797c478bd9Sstevel@tonic-gate 	if (mechanism->cm_type == MD5_HMAC_GEN_MECH_INFO_TYPE) {
15807c478bd9Sstevel@tonic-gate 		if (mechanism->cm_param == NULL ||
15817c478bd9Sstevel@tonic-gate 		    mechanism->cm_param_len != sizeof (ulong_t))
15827c478bd9Sstevel@tonic-gate 			ret = CRYPTO_MECHANISM_PARAM_INVALID;
15837c478bd9Sstevel@tonic-gate 		PROV_MD5_GET_DIGEST_LEN(mechanism,
15847c478bd9Sstevel@tonic-gate 		    PROV_MD5_HMAC_CTX(ctx)->hc_digest_len);
15857c478bd9Sstevel@tonic-gate 		if (PROV_MD5_HMAC_CTX(ctx)->hc_digest_len >
15867c478bd9Sstevel@tonic-gate 		    MD5_DIGEST_LENGTH)
15877c478bd9Sstevel@tonic-gate 			ret = CRYPTO_MECHANISM_PARAM_INVALID;
15887c478bd9Sstevel@tonic-gate 	}
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate 	if (ret != CRYPTO_SUCCESS) {
15917c478bd9Sstevel@tonic-gate 		bzero(ctx->cc_provider_private, sizeof (md5_hmac_ctx_t));
15927c478bd9Sstevel@tonic-gate 		kmem_free(ctx->cc_provider_private, sizeof (md5_hmac_ctx_t));
15937c478bd9Sstevel@tonic-gate 		ctx->cc_provider_private = NULL;
15947c478bd9Sstevel@tonic-gate 	}
15957c478bd9Sstevel@tonic-gate 
15967c478bd9Sstevel@tonic-gate 	return (ret);
15977c478bd9Sstevel@tonic-gate }
15987c478bd9Sstevel@tonic-gate 
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate /* ARGSUSED */
16017c478bd9Sstevel@tonic-gate static int
16027c478bd9Sstevel@tonic-gate md5_mac_update(crypto_ctx_t *ctx, crypto_data_t *data, crypto_req_handle_t req)
16037c478bd9Sstevel@tonic-gate {
16047c478bd9Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
16077c478bd9Sstevel@tonic-gate 
16087c478bd9Sstevel@tonic-gate 	/*
16097c478bd9Sstevel@tonic-gate 	 * Do an MD5 update of the inner context using the specified
16107c478bd9Sstevel@tonic-gate 	 * data.
16117c478bd9Sstevel@tonic-gate 	 */
16127c478bd9Sstevel@tonic-gate 	switch (data->cd_format) {
16137c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
16147c478bd9Sstevel@tonic-gate 		MD5Update(&PROV_MD5_HMAC_CTX(ctx)->hc_icontext,
16157c478bd9Sstevel@tonic-gate 		    data->cd_raw.iov_base + data->cd_offset,
16167c478bd9Sstevel@tonic-gate 		    data->cd_length);
16177c478bd9Sstevel@tonic-gate 		break;
16187c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
16197c478bd9Sstevel@tonic-gate 		ret = md5_digest_update_uio(
16207c478bd9Sstevel@tonic-gate 		    &PROV_MD5_HMAC_CTX(ctx)->hc_icontext, data);
16217c478bd9Sstevel@tonic-gate 		break;
16227c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
16237c478bd9Sstevel@tonic-gate 		ret = md5_digest_update_mblk(
16247c478bd9Sstevel@tonic-gate 		    &PROV_MD5_HMAC_CTX(ctx)->hc_icontext, data);
16257c478bd9Sstevel@tonic-gate 		break;
16267c478bd9Sstevel@tonic-gate 	default:
16277c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
16287c478bd9Sstevel@tonic-gate 	}
16297c478bd9Sstevel@tonic-gate 
16307c478bd9Sstevel@tonic-gate 	return (ret);
16317c478bd9Sstevel@tonic-gate }
16327c478bd9Sstevel@tonic-gate 
16337c478bd9Sstevel@tonic-gate /* ARGSUSED */
16347c478bd9Sstevel@tonic-gate static int
16357c478bd9Sstevel@tonic-gate md5_mac_final(crypto_ctx_t *ctx, crypto_data_t *mac, crypto_req_handle_t req)
16367c478bd9Sstevel@tonic-gate {
16377c478bd9Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
16387c478bd9Sstevel@tonic-gate 	uchar_t digest[MD5_DIGEST_LENGTH];
16397c478bd9Sstevel@tonic-gate 	uint32_t digest_len = MD5_DIGEST_LENGTH;
16407c478bd9Sstevel@tonic-gate 
16417c478bd9Sstevel@tonic-gate 	ASSERT(ctx->cc_provider_private != NULL);
16427c478bd9Sstevel@tonic-gate 
16437c478bd9Sstevel@tonic-gate 	if (PROV_MD5_HMAC_CTX(ctx)->hc_mech_type == MD5_HMAC_GEN_MECH_INFO_TYPE)
16447c478bd9Sstevel@tonic-gate 	    digest_len = PROV_MD5_HMAC_CTX(ctx)->hc_digest_len;
16457c478bd9Sstevel@tonic-gate 
16467c478bd9Sstevel@tonic-gate 	/*
16477c478bd9Sstevel@tonic-gate 	 * We need to just return the length needed to store the output.
16487c478bd9Sstevel@tonic-gate 	 * We should not destroy the context for the following cases.
16497c478bd9Sstevel@tonic-gate 	 */
16507c478bd9Sstevel@tonic-gate 	if ((mac->cd_length == 0) || (mac->cd_length < digest_len)) {
16517c478bd9Sstevel@tonic-gate 		mac->cd_length = digest_len;
16527c478bd9Sstevel@tonic-gate 		return (CRYPTO_BUFFER_TOO_SMALL);
16537c478bd9Sstevel@tonic-gate 	}
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate 	/*
16567c478bd9Sstevel@tonic-gate 	 * Do an MD5 final on the inner context.
16577c478bd9Sstevel@tonic-gate 	 */
16587c478bd9Sstevel@tonic-gate 	MD5Final(digest, &PROV_MD5_HMAC_CTX(ctx)->hc_icontext);
16597c478bd9Sstevel@tonic-gate 
16607c478bd9Sstevel@tonic-gate 	/*
16617c478bd9Sstevel@tonic-gate 	 * Do an MD5 update on the outer context, feeding the inner
16627c478bd9Sstevel@tonic-gate 	 * digest as data.
16637c478bd9Sstevel@tonic-gate 	 */
16647c478bd9Sstevel@tonic-gate 	MD5Update(&PROV_MD5_HMAC_CTX(ctx)->hc_ocontext, digest,
16657c478bd9Sstevel@tonic-gate 	    MD5_DIGEST_LENGTH);
16667c478bd9Sstevel@tonic-gate 
16677c478bd9Sstevel@tonic-gate 	/*
16687c478bd9Sstevel@tonic-gate 	 * Do an MD5 final on the outer context, storing the computing
16697c478bd9Sstevel@tonic-gate 	 * digest in the users buffer.
16707c478bd9Sstevel@tonic-gate 	 */
16717c478bd9Sstevel@tonic-gate 	switch (mac->cd_format) {
16727c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
16737c478bd9Sstevel@tonic-gate 		if (digest_len != MD5_DIGEST_LENGTH) {
16747c478bd9Sstevel@tonic-gate 			/*
16757c478bd9Sstevel@tonic-gate 			 * The caller requested a short digest. Digest
16767c478bd9Sstevel@tonic-gate 			 * into a scratch buffer and return to
16777c478bd9Sstevel@tonic-gate 			 * the user only what was requested.
16787c478bd9Sstevel@tonic-gate 			 */
16797c478bd9Sstevel@tonic-gate 			MD5Final(digest,
16807c478bd9Sstevel@tonic-gate 			    &PROV_MD5_HMAC_CTX(ctx)->hc_ocontext);
16817c478bd9Sstevel@tonic-gate 			bcopy(digest, (unsigned char *)mac->cd_raw.iov_base +
16827c478bd9Sstevel@tonic-gate 			    mac->cd_offset, digest_len);
16837c478bd9Sstevel@tonic-gate 		} else {
16847c478bd9Sstevel@tonic-gate 			MD5Final((unsigned char *)mac->cd_raw.iov_base +
16857c478bd9Sstevel@tonic-gate 			    mac->cd_offset,
16867c478bd9Sstevel@tonic-gate 			    &PROV_MD5_HMAC_CTX(ctx)->hc_ocontext);
16877c478bd9Sstevel@tonic-gate 		}
16887c478bd9Sstevel@tonic-gate 		break;
16897c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
16907c478bd9Sstevel@tonic-gate 		ret = md5_digest_final_uio(
16917c478bd9Sstevel@tonic-gate 		    &PROV_MD5_HMAC_CTX(ctx)->hc_ocontext, mac,
16927c478bd9Sstevel@tonic-gate 		    digest_len, digest);
16937c478bd9Sstevel@tonic-gate 		break;
16947c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
16957c478bd9Sstevel@tonic-gate 		ret = md5_digest_final_mblk(
16967c478bd9Sstevel@tonic-gate 		    &PROV_MD5_HMAC_CTX(ctx)->hc_ocontext, mac,
16977c478bd9Sstevel@tonic-gate 		    digest_len, digest);
16987c478bd9Sstevel@tonic-gate 		break;
16997c478bd9Sstevel@tonic-gate 	default:
17007c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
17017c478bd9Sstevel@tonic-gate 	}
17027c478bd9Sstevel@tonic-gate 
17037c478bd9Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
17047c478bd9Sstevel@tonic-gate 		mac->cd_length = digest_len;
17057c478bd9Sstevel@tonic-gate 	} else {
17067c478bd9Sstevel@tonic-gate 		mac->cd_length = 0;
17077c478bd9Sstevel@tonic-gate 	}
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate 	bzero(ctx->cc_provider_private, sizeof (md5_hmac_ctx_t));
17107c478bd9Sstevel@tonic-gate 	kmem_free(ctx->cc_provider_private, sizeof (md5_hmac_ctx_t));
17117c478bd9Sstevel@tonic-gate 	ctx->cc_provider_private = NULL;
17127c478bd9Sstevel@tonic-gate 
17137c478bd9Sstevel@tonic-gate 	return (ret);
17147c478bd9Sstevel@tonic-gate }
17157c478bd9Sstevel@tonic-gate 
17167c478bd9Sstevel@tonic-gate #define	MD5_MAC_UPDATE(data, ctx, ret) {				\
17177c478bd9Sstevel@tonic-gate 	switch (data->cd_format) {					\
17187c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:						\
17197c478bd9Sstevel@tonic-gate 		MD5Update(&(ctx).hc_icontext,				\
17207c478bd9Sstevel@tonic-gate 		    data->cd_raw.iov_base + data->cd_offset,		\
17217c478bd9Sstevel@tonic-gate 		    data->cd_length);					\
17227c478bd9Sstevel@tonic-gate 		break;							\
17237c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:						\
17247c478bd9Sstevel@tonic-gate 		ret = md5_digest_update_uio(&(ctx).hc_icontext,	data);	\
17257c478bd9Sstevel@tonic-gate 		break;							\
17267c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:						\
17277c478bd9Sstevel@tonic-gate 		ret = md5_digest_update_mblk(&(ctx).hc_icontext,	\
17287c478bd9Sstevel@tonic-gate 		    data);						\
17297c478bd9Sstevel@tonic-gate 		break;							\
17307c478bd9Sstevel@tonic-gate 	default:							\
17317c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;				\
17327c478bd9Sstevel@tonic-gate 	}								\
17337c478bd9Sstevel@tonic-gate }
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate 
17367c478bd9Sstevel@tonic-gate /* ARGSUSED */
17377c478bd9Sstevel@tonic-gate static int
17387c478bd9Sstevel@tonic-gate md5_mac_atomic(crypto_provider_handle_t provider,
17397c478bd9Sstevel@tonic-gate     crypto_session_id_t session_id, crypto_mechanism_t *mechanism,
17407c478bd9Sstevel@tonic-gate     crypto_key_t *key, crypto_data_t *data, crypto_data_t *mac,
17417c478bd9Sstevel@tonic-gate     crypto_spi_ctx_template_t ctx_template, crypto_req_handle_t req)
17427c478bd9Sstevel@tonic-gate {
17437c478bd9Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
17447c478bd9Sstevel@tonic-gate 	uchar_t digest[MD5_DIGEST_LENGTH];
17457c478bd9Sstevel@tonic-gate 	md5_hmac_ctx_t md5_hmac_ctx;
17467c478bd9Sstevel@tonic-gate 	uint32_t digest_len = MD5_DIGEST_LENGTH;
17477c478bd9Sstevel@tonic-gate 	uint_t keylen_in_bytes = CRYPTO_BITS2BYTES(key->ck_length);
17487c478bd9Sstevel@tonic-gate 
17497c478bd9Sstevel@tonic-gate 	if (mechanism->cm_type != MD5_HMAC_MECH_INFO_TYPE &&
17507c478bd9Sstevel@tonic-gate 	    mechanism->cm_type != MD5_HMAC_GEN_MECH_INFO_TYPE)
17517c478bd9Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate 	/* Add support for key by attributes (RFE 4706552) */
17547c478bd9Sstevel@tonic-gate 	if (key->ck_format != CRYPTO_KEY_RAW)
17557c478bd9Sstevel@tonic-gate 		return (CRYPTO_ARGUMENTS_BAD);
17567c478bd9Sstevel@tonic-gate 
17577c478bd9Sstevel@tonic-gate 	if (ctx_template != NULL) {
17587c478bd9Sstevel@tonic-gate 		/* reuse context template */
17597c478bd9Sstevel@tonic-gate 		bcopy(ctx_template, &md5_hmac_ctx, sizeof (md5_hmac_ctx_t));
17607c478bd9Sstevel@tonic-gate 	} else {
17617c478bd9Sstevel@tonic-gate 		/* no context template, compute context */
17627c478bd9Sstevel@tonic-gate 		if (keylen_in_bytes > MD5_HMAC_BLOCK_SIZE) {
17637c478bd9Sstevel@tonic-gate 			/*
17647c478bd9Sstevel@tonic-gate 			 * Hash the passed-in key to get a smaller key.
17657c478bd9Sstevel@tonic-gate 			 * The inner context is used since it hasn't been
17667c478bd9Sstevel@tonic-gate 			 * initialized yet.
17677c478bd9Sstevel@tonic-gate 			 */
17687c478bd9Sstevel@tonic-gate 			PROV_MD5_DIGEST_KEY(&md5_hmac_ctx.hc_icontext,
17697c478bd9Sstevel@tonic-gate 			    key->ck_data, keylen_in_bytes, digest);
17707c478bd9Sstevel@tonic-gate 			md5_mac_init_ctx(&md5_hmac_ctx, digest,
17717c478bd9Sstevel@tonic-gate 			    MD5_DIGEST_LENGTH);
17727c478bd9Sstevel@tonic-gate 		} else {
17737c478bd9Sstevel@tonic-gate 			md5_mac_init_ctx(&md5_hmac_ctx, key->ck_data,
17747c478bd9Sstevel@tonic-gate 			    keylen_in_bytes);
17757c478bd9Sstevel@tonic-gate 		}
17767c478bd9Sstevel@tonic-gate 	}
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate 	/*
17797c478bd9Sstevel@tonic-gate 	 * Get the mechanism parameters, if applicable.
17807c478bd9Sstevel@tonic-gate 	 */
17817c478bd9Sstevel@tonic-gate 	if (mechanism->cm_type == MD5_HMAC_GEN_MECH_INFO_TYPE) {
17827c478bd9Sstevel@tonic-gate 		if (mechanism->cm_param == NULL ||
17837c478bd9Sstevel@tonic-gate 		    mechanism->cm_param_len != sizeof (ulong_t)) {
17847c478bd9Sstevel@tonic-gate 			ret = CRYPTO_MECHANISM_PARAM_INVALID;
17857c478bd9Sstevel@tonic-gate 			goto bail;
17867c478bd9Sstevel@tonic-gate 		}
17877c478bd9Sstevel@tonic-gate 		PROV_MD5_GET_DIGEST_LEN(mechanism, digest_len);
17887c478bd9Sstevel@tonic-gate 		if (digest_len > MD5_DIGEST_LENGTH) {
17897c478bd9Sstevel@tonic-gate 			ret = CRYPTO_MECHANISM_PARAM_INVALID;
17907c478bd9Sstevel@tonic-gate 			goto bail;
17917c478bd9Sstevel@tonic-gate 		}
17927c478bd9Sstevel@tonic-gate 	}
17937c478bd9Sstevel@tonic-gate 
17947c478bd9Sstevel@tonic-gate 	/* do an MD5 update of the inner context using the specified data */
17957c478bd9Sstevel@tonic-gate 	MD5_MAC_UPDATE(data, md5_hmac_ctx, ret);
17967c478bd9Sstevel@tonic-gate 	if (ret != CRYPTO_SUCCESS)
17977c478bd9Sstevel@tonic-gate 		/* the update failed, free context and bail */
17987c478bd9Sstevel@tonic-gate 		goto bail;
17997c478bd9Sstevel@tonic-gate 
18007c478bd9Sstevel@tonic-gate 	/* do an MD5 final on the inner context */
18017c478bd9Sstevel@tonic-gate 	MD5Final(digest, &md5_hmac_ctx.hc_icontext);
18027c478bd9Sstevel@tonic-gate 
18037c478bd9Sstevel@tonic-gate 	/*
18047c478bd9Sstevel@tonic-gate 	 * Do an MD5 update on the outer context, feeding the inner
18057c478bd9Sstevel@tonic-gate 	 * digest as data.
18067c478bd9Sstevel@tonic-gate 	 */
18077c478bd9Sstevel@tonic-gate 	MD5Update(&md5_hmac_ctx.hc_ocontext, digest, MD5_DIGEST_LENGTH);
18087c478bd9Sstevel@tonic-gate 
18097c478bd9Sstevel@tonic-gate 	/*
18107c478bd9Sstevel@tonic-gate 	 * Do an MD5 final on the outer context, storing the computed
18117c478bd9Sstevel@tonic-gate 	 * digest in the users buffer.
18127c478bd9Sstevel@tonic-gate 	 */
18137c478bd9Sstevel@tonic-gate 	switch (mac->cd_format) {
18147c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
18157c478bd9Sstevel@tonic-gate 		if (digest_len != MD5_DIGEST_LENGTH) {
18167c478bd9Sstevel@tonic-gate 			/*
18177c478bd9Sstevel@tonic-gate 			 * The caller requested a short digest. Digest
18187c478bd9Sstevel@tonic-gate 			 * into a scratch buffer and return to
18197c478bd9Sstevel@tonic-gate 			 * the user only what was requested.
18207c478bd9Sstevel@tonic-gate 			 */
18217c478bd9Sstevel@tonic-gate 			MD5Final(digest, &md5_hmac_ctx.hc_ocontext);
18227c478bd9Sstevel@tonic-gate 			bcopy(digest, (unsigned char *)mac->cd_raw.iov_base +
18237c478bd9Sstevel@tonic-gate 			    mac->cd_offset, digest_len);
18247c478bd9Sstevel@tonic-gate 		} else {
18257c478bd9Sstevel@tonic-gate 			MD5Final((unsigned char *)mac->cd_raw.iov_base +
18267c478bd9Sstevel@tonic-gate 			    mac->cd_offset, &md5_hmac_ctx.hc_ocontext);
18277c478bd9Sstevel@tonic-gate 		}
18287c478bd9Sstevel@tonic-gate 		break;
18297c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO:
18307c478bd9Sstevel@tonic-gate 		ret = md5_digest_final_uio(&md5_hmac_ctx.hc_ocontext, mac,
18317c478bd9Sstevel@tonic-gate 		    digest_len, digest);
18327c478bd9Sstevel@tonic-gate 		break;
18337c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK:
18347c478bd9Sstevel@tonic-gate 		ret = md5_digest_final_mblk(&md5_hmac_ctx.hc_ocontext, mac,
18357c478bd9Sstevel@tonic-gate 		    digest_len, digest);
18367c478bd9Sstevel@tonic-gate 		break;
18377c478bd9Sstevel@tonic-gate 	default:
18387c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
18397c478bd9Sstevel@tonic-gate 	}
18407c478bd9Sstevel@tonic-gate 
18417c478bd9Sstevel@tonic-gate 	if (ret == CRYPTO_SUCCESS) {
18427c478bd9Sstevel@tonic-gate 		mac->cd_length = digest_len;
18437c478bd9Sstevel@tonic-gate 	} else {
18447c478bd9Sstevel@tonic-gate 		mac->cd_length = 0;
18457c478bd9Sstevel@tonic-gate 	}
18467c478bd9Sstevel@tonic-gate 	/* Extra paranoia: zeroizing the local context on the stack */
18477c478bd9Sstevel@tonic-gate 	bzero(&md5_hmac_ctx, sizeof (md5_hmac_ctx_t));
18487c478bd9Sstevel@tonic-gate 
18497c478bd9Sstevel@tonic-gate 	return (ret);
18507c478bd9Sstevel@tonic-gate bail:
18517c478bd9Sstevel@tonic-gate 	bzero(&md5_hmac_ctx, sizeof (md5_hmac_ctx_t));
18527c478bd9Sstevel@tonic-gate 	mac->cd_length = 0;
18537c478bd9Sstevel@tonic-gate 	return (ret);
18547c478bd9Sstevel@tonic-gate }
18557c478bd9Sstevel@tonic-gate 
18567c478bd9Sstevel@tonic-gate /* ARGSUSED */
18577c478bd9Sstevel@tonic-gate static int
18587c478bd9Sstevel@tonic-gate md5_mac_verify_atomic(crypto_provider_handle_t provider,
18597c478bd9Sstevel@tonic-gate     crypto_session_id_t session_id, crypto_mechanism_t *mechanism,
18607c478bd9Sstevel@tonic-gate     crypto_key_t *key, crypto_data_t *data, crypto_data_t *mac,
18617c478bd9Sstevel@tonic-gate     crypto_spi_ctx_template_t ctx_template, crypto_req_handle_t req)
18627c478bd9Sstevel@tonic-gate {
18637c478bd9Sstevel@tonic-gate 	int ret = CRYPTO_SUCCESS;
18647c478bd9Sstevel@tonic-gate 	uchar_t digest[MD5_DIGEST_LENGTH];
18657c478bd9Sstevel@tonic-gate 	md5_hmac_ctx_t md5_hmac_ctx;
18667c478bd9Sstevel@tonic-gate 	uint32_t digest_len = MD5_DIGEST_LENGTH;
18677c478bd9Sstevel@tonic-gate 	uint_t keylen_in_bytes = CRYPTO_BITS2BYTES(key->ck_length);
18687c478bd9Sstevel@tonic-gate 
18697c478bd9Sstevel@tonic-gate 	if (mechanism->cm_type != MD5_HMAC_MECH_INFO_TYPE &&
18707c478bd9Sstevel@tonic-gate 	    mechanism->cm_type != MD5_HMAC_GEN_MECH_INFO_TYPE)
18717c478bd9Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
18727c478bd9Sstevel@tonic-gate 
18737c478bd9Sstevel@tonic-gate 	/* Add support for key by attributes (RFE 4706552) */
18747c478bd9Sstevel@tonic-gate 	if (key->ck_format != CRYPTO_KEY_RAW)
18757c478bd9Sstevel@tonic-gate 		return (CRYPTO_ARGUMENTS_BAD);
18767c478bd9Sstevel@tonic-gate 
18777c478bd9Sstevel@tonic-gate 	if (ctx_template != NULL) {
18787c478bd9Sstevel@tonic-gate 		/* reuse context template */
18797c478bd9Sstevel@tonic-gate 		bcopy(ctx_template, &md5_hmac_ctx, sizeof (md5_hmac_ctx_t));
18807c478bd9Sstevel@tonic-gate 	} else {
18817c478bd9Sstevel@tonic-gate 		/* no context template, compute context */
18827c478bd9Sstevel@tonic-gate 		if (keylen_in_bytes > MD5_HMAC_BLOCK_SIZE) {
18837c478bd9Sstevel@tonic-gate 			/*
18847c478bd9Sstevel@tonic-gate 			 * Hash the passed-in key to get a smaller key.
18857c478bd9Sstevel@tonic-gate 			 * The inner context is used since it hasn't been
18867c478bd9Sstevel@tonic-gate 			 * initialized yet.
18877c478bd9Sstevel@tonic-gate 			 */
18887c478bd9Sstevel@tonic-gate 			PROV_MD5_DIGEST_KEY(&md5_hmac_ctx.hc_icontext,
18897c478bd9Sstevel@tonic-gate 			    key->ck_data, keylen_in_bytes, digest);
18907c478bd9Sstevel@tonic-gate 			md5_mac_init_ctx(&md5_hmac_ctx, digest,
18917c478bd9Sstevel@tonic-gate 			    MD5_DIGEST_LENGTH);
18927c478bd9Sstevel@tonic-gate 		} else {
18937c478bd9Sstevel@tonic-gate 			md5_mac_init_ctx(&md5_hmac_ctx, key->ck_data,
18947c478bd9Sstevel@tonic-gate 			    keylen_in_bytes);
18957c478bd9Sstevel@tonic-gate 		}
18967c478bd9Sstevel@tonic-gate 	}
18977c478bd9Sstevel@tonic-gate 
18987c478bd9Sstevel@tonic-gate 	/*
18997c478bd9Sstevel@tonic-gate 	 * Get the mechanism parameters, if applicable.
19007c478bd9Sstevel@tonic-gate 	 */
19017c478bd9Sstevel@tonic-gate 	if (mechanism->cm_type == MD5_HMAC_GEN_MECH_INFO_TYPE) {
19027c478bd9Sstevel@tonic-gate 		if (mechanism->cm_param == NULL ||
19037c478bd9Sstevel@tonic-gate 		    mechanism->cm_param_len != sizeof (ulong_t)) {
19047c478bd9Sstevel@tonic-gate 			ret = CRYPTO_MECHANISM_PARAM_INVALID;
19057c478bd9Sstevel@tonic-gate 			goto bail;
19067c478bd9Sstevel@tonic-gate 		}
19077c478bd9Sstevel@tonic-gate 		PROV_MD5_GET_DIGEST_LEN(mechanism, digest_len);
19087c478bd9Sstevel@tonic-gate 		if (digest_len > MD5_DIGEST_LENGTH) {
19097c478bd9Sstevel@tonic-gate 			ret = CRYPTO_MECHANISM_PARAM_INVALID;
19107c478bd9Sstevel@tonic-gate 			goto bail;
19117c478bd9Sstevel@tonic-gate 		}
19127c478bd9Sstevel@tonic-gate 	}
19137c478bd9Sstevel@tonic-gate 
19147c478bd9Sstevel@tonic-gate 	if (mac->cd_length != digest_len) {
19157c478bd9Sstevel@tonic-gate 		ret = CRYPTO_INVALID_MAC;
19167c478bd9Sstevel@tonic-gate 		goto bail;
19177c478bd9Sstevel@tonic-gate 	}
19187c478bd9Sstevel@tonic-gate 
19197c478bd9Sstevel@tonic-gate 	/* do an MD5 update of the inner context using the specified data */
19207c478bd9Sstevel@tonic-gate 	MD5_MAC_UPDATE(data, md5_hmac_ctx, ret);
19217c478bd9Sstevel@tonic-gate 	if (ret != CRYPTO_SUCCESS)
19227c478bd9Sstevel@tonic-gate 		/* the update failed, free context and bail */
19237c478bd9Sstevel@tonic-gate 		goto bail;
19247c478bd9Sstevel@tonic-gate 
19257c478bd9Sstevel@tonic-gate 	/* do an MD5 final on the inner context */
19267c478bd9Sstevel@tonic-gate 	MD5Final(digest, &md5_hmac_ctx.hc_icontext);
19277c478bd9Sstevel@tonic-gate 
19287c478bd9Sstevel@tonic-gate 	/*
19297c478bd9Sstevel@tonic-gate 	 * Do an MD5 update on the outer context, feeding the inner
19307c478bd9Sstevel@tonic-gate 	 * digest as data.
19317c478bd9Sstevel@tonic-gate 	 */
19327c478bd9Sstevel@tonic-gate 	MD5Update(&md5_hmac_ctx.hc_ocontext, digest, MD5_DIGEST_LENGTH);
19337c478bd9Sstevel@tonic-gate 
19347c478bd9Sstevel@tonic-gate 	/*
19357c478bd9Sstevel@tonic-gate 	 * Do an MD5 final on the outer context, storing the computed
19367c478bd9Sstevel@tonic-gate 	 * digest in the local digest buffer.
19377c478bd9Sstevel@tonic-gate 	 */
19387c478bd9Sstevel@tonic-gate 	MD5Final(digest, &md5_hmac_ctx.hc_ocontext);
19397c478bd9Sstevel@tonic-gate 
19407c478bd9Sstevel@tonic-gate 	/*
19417c478bd9Sstevel@tonic-gate 	 * Compare the computed digest against the expected digest passed
19427c478bd9Sstevel@tonic-gate 	 * as argument.
19437c478bd9Sstevel@tonic-gate 	 */
19447c478bd9Sstevel@tonic-gate 	switch (mac->cd_format) {
19457c478bd9Sstevel@tonic-gate 
19467c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_RAW:
19477c478bd9Sstevel@tonic-gate 		if (bcmp(digest, (unsigned char *)mac->cd_raw.iov_base +
19487c478bd9Sstevel@tonic-gate 		    mac->cd_offset, digest_len) != 0)
19497c478bd9Sstevel@tonic-gate 			ret = CRYPTO_INVALID_MAC;
19507c478bd9Sstevel@tonic-gate 		break;
19517c478bd9Sstevel@tonic-gate 
19527c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_UIO: {
19537c478bd9Sstevel@tonic-gate 		off_t offset = mac->cd_offset;
19547c478bd9Sstevel@tonic-gate 		uint_t vec_idx;
19557c478bd9Sstevel@tonic-gate 		off_t scratch_offset = 0;
19567c478bd9Sstevel@tonic-gate 		size_t length = digest_len;
19577c478bd9Sstevel@tonic-gate 		size_t cur_len;
19587c478bd9Sstevel@tonic-gate 
19597c478bd9Sstevel@tonic-gate 		/* we support only kernel buffer */
19607c478bd9Sstevel@tonic-gate 		if (mac->cd_uio->uio_segflg != UIO_SYSSPACE)
19617c478bd9Sstevel@tonic-gate 			return (CRYPTO_ARGUMENTS_BAD);
19627c478bd9Sstevel@tonic-gate 
19637c478bd9Sstevel@tonic-gate 		/* jump to the first iovec containing the expected digest */
19647c478bd9Sstevel@tonic-gate 		for (vec_idx = 0;
19657c478bd9Sstevel@tonic-gate 		    offset >= mac->cd_uio->uio_iov[vec_idx].iov_len &&
19667c478bd9Sstevel@tonic-gate 		    vec_idx < mac->cd_uio->uio_iovcnt;
19677c478bd9Sstevel@tonic-gate 		    offset -= mac->cd_uio->uio_iov[vec_idx++].iov_len);
19687c478bd9Sstevel@tonic-gate 		if (vec_idx == mac->cd_uio->uio_iovcnt) {
19697c478bd9Sstevel@tonic-gate 			/*
19707c478bd9Sstevel@tonic-gate 			 * The caller specified an offset that is
19717c478bd9Sstevel@tonic-gate 			 * larger than the total size of the buffers
19727c478bd9Sstevel@tonic-gate 			 * it provided.
19737c478bd9Sstevel@tonic-gate 			 */
19747c478bd9Sstevel@tonic-gate 			ret = CRYPTO_DATA_LEN_RANGE;
19757c478bd9Sstevel@tonic-gate 			break;
19767c478bd9Sstevel@tonic-gate 		}
19777c478bd9Sstevel@tonic-gate 
19787c478bd9Sstevel@tonic-gate 		/* do the comparison of computed digest vs specified one */
19797c478bd9Sstevel@tonic-gate 		while (vec_idx < mac->cd_uio->uio_iovcnt && length > 0) {
19807c478bd9Sstevel@tonic-gate 			cur_len = MIN(mac->cd_uio->uio_iov[vec_idx].iov_len -
19817c478bd9Sstevel@tonic-gate 			    offset, length);
19827c478bd9Sstevel@tonic-gate 
19837c478bd9Sstevel@tonic-gate 			if (bcmp(digest + scratch_offset,
19847c478bd9Sstevel@tonic-gate 			    mac->cd_uio->uio_iov[vec_idx].iov_base + offset,
19857c478bd9Sstevel@tonic-gate 			    cur_len) != 0) {
19867c478bd9Sstevel@tonic-gate 				ret = CRYPTO_INVALID_MAC;
19877c478bd9Sstevel@tonic-gate 				break;
19887c478bd9Sstevel@tonic-gate 			}
19897c478bd9Sstevel@tonic-gate 
19907c478bd9Sstevel@tonic-gate 			length -= cur_len;
19917c478bd9Sstevel@tonic-gate 			vec_idx++;
19927c478bd9Sstevel@tonic-gate 			scratch_offset += cur_len;
19937c478bd9Sstevel@tonic-gate 			offset = 0;
19947c478bd9Sstevel@tonic-gate 		}
19957c478bd9Sstevel@tonic-gate 		break;
19967c478bd9Sstevel@tonic-gate 	}
19977c478bd9Sstevel@tonic-gate 
19987c478bd9Sstevel@tonic-gate 	case CRYPTO_DATA_MBLK: {
19997c478bd9Sstevel@tonic-gate 		off_t offset = mac->cd_offset;
20007c478bd9Sstevel@tonic-gate 		mblk_t *mp;
20017c478bd9Sstevel@tonic-gate 		off_t scratch_offset = 0;
20027c478bd9Sstevel@tonic-gate 		size_t length = digest_len;
20037c478bd9Sstevel@tonic-gate 		size_t cur_len;
20047c478bd9Sstevel@tonic-gate 
20057c478bd9Sstevel@tonic-gate 		/* jump to the first mblk_t containing the expected digest */
20067c478bd9Sstevel@tonic-gate 		for (mp = mac->cd_mp; mp != NULL && offset >= MBLKL(mp);
20077c478bd9Sstevel@tonic-gate 		    offset -= MBLKL(mp), mp = mp->b_cont);
20087c478bd9Sstevel@tonic-gate 		if (mp == NULL) {
20097c478bd9Sstevel@tonic-gate 			/*
20107c478bd9Sstevel@tonic-gate 			 * The caller specified an offset that is larger than
20117c478bd9Sstevel@tonic-gate 			 * the total size of the buffers it provided.
20127c478bd9Sstevel@tonic-gate 			 */
20137c478bd9Sstevel@tonic-gate 			ret = CRYPTO_DATA_LEN_RANGE;
20147c478bd9Sstevel@tonic-gate 			break;
20157c478bd9Sstevel@tonic-gate 		}
20167c478bd9Sstevel@tonic-gate 
20177c478bd9Sstevel@tonic-gate 		while (mp != NULL && length > 0) {
20187c478bd9Sstevel@tonic-gate 			cur_len = MIN(MBLKL(mp) - offset, length);
20197c478bd9Sstevel@tonic-gate 			if (bcmp(digest + scratch_offset,
20207c478bd9Sstevel@tonic-gate 			    mp->b_rptr + offset, cur_len) != 0) {
20217c478bd9Sstevel@tonic-gate 				ret = CRYPTO_INVALID_MAC;
20227c478bd9Sstevel@tonic-gate 				break;
20237c478bd9Sstevel@tonic-gate 			}
20247c478bd9Sstevel@tonic-gate 
20257c478bd9Sstevel@tonic-gate 			length -= cur_len;
20267c478bd9Sstevel@tonic-gate 			mp = mp->b_cont;
20277c478bd9Sstevel@tonic-gate 			scratch_offset += cur_len;
20287c478bd9Sstevel@tonic-gate 			offset = 0;
20297c478bd9Sstevel@tonic-gate 		}
20307c478bd9Sstevel@tonic-gate 		break;
20317c478bd9Sstevel@tonic-gate 	}
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate 	default:
20347c478bd9Sstevel@tonic-gate 		ret = CRYPTO_ARGUMENTS_BAD;
20357c478bd9Sstevel@tonic-gate 	}
20367c478bd9Sstevel@tonic-gate 
20377c478bd9Sstevel@tonic-gate 	bzero(&md5_hmac_ctx, sizeof (md5_hmac_ctx_t));
20387c478bd9Sstevel@tonic-gate 	return (ret);
20397c478bd9Sstevel@tonic-gate bail:
20407c478bd9Sstevel@tonic-gate 	bzero(&md5_hmac_ctx, sizeof (md5_hmac_ctx_t));
20417c478bd9Sstevel@tonic-gate 	mac->cd_length = 0;
20427c478bd9Sstevel@tonic-gate 	return (ret);
20437c478bd9Sstevel@tonic-gate }
20447c478bd9Sstevel@tonic-gate 
20457c478bd9Sstevel@tonic-gate /*
20467c478bd9Sstevel@tonic-gate  * KCF software provider context management entry points.
20477c478bd9Sstevel@tonic-gate  */
20487c478bd9Sstevel@tonic-gate 
20497c478bd9Sstevel@tonic-gate /* ARGSUSED */
20507c478bd9Sstevel@tonic-gate static int
20517c478bd9Sstevel@tonic-gate md5_create_ctx_template(crypto_provider_handle_t provider,
20527c478bd9Sstevel@tonic-gate     crypto_mechanism_t *mechanism, crypto_key_t *key,
20537c478bd9Sstevel@tonic-gate     crypto_spi_ctx_template_t *ctx_template, size_t *ctx_template_size,
20547c478bd9Sstevel@tonic-gate     crypto_req_handle_t req)
20557c478bd9Sstevel@tonic-gate {
20567c478bd9Sstevel@tonic-gate 	md5_hmac_ctx_t *md5_hmac_ctx_tmpl;
20577c478bd9Sstevel@tonic-gate 	uint_t keylen_in_bytes = CRYPTO_BITS2BYTES(key->ck_length);
20587c478bd9Sstevel@tonic-gate 
20597c478bd9Sstevel@tonic-gate 	if ((mechanism->cm_type != MD5_HMAC_MECH_INFO_TYPE) &&
20607c478bd9Sstevel@tonic-gate 	    (mechanism->cm_type != MD5_HMAC_GEN_MECH_INFO_TYPE))
20617c478bd9Sstevel@tonic-gate 		return (CRYPTO_MECHANISM_INVALID);
20627c478bd9Sstevel@tonic-gate 
20637c478bd9Sstevel@tonic-gate 	/* Add support for key by attributes (RFE 4706552) */
20647c478bd9Sstevel@tonic-gate 	if (key->ck_format != CRYPTO_KEY_RAW)
20657c478bd9Sstevel@tonic-gate 		return (CRYPTO_ARGUMENTS_BAD);
20667c478bd9Sstevel@tonic-gate 
20677c478bd9Sstevel@tonic-gate 	/*
20687c478bd9Sstevel@tonic-gate 	 * Allocate and initialize MD5 context.
20697c478bd9Sstevel@tonic-gate 	 */
20707c478bd9Sstevel@tonic-gate 	md5_hmac_ctx_tmpl = kmem_alloc(sizeof (md5_hmac_ctx_t),
20717c478bd9Sstevel@tonic-gate 	    crypto_kmflag(req));
20727c478bd9Sstevel@tonic-gate 	if (md5_hmac_ctx_tmpl == NULL)
20737c478bd9Sstevel@tonic-gate 		return (CRYPTO_HOST_MEMORY);
20747c478bd9Sstevel@tonic-gate 
20757c478bd9Sstevel@tonic-gate 	if (keylen_in_bytes > MD5_HMAC_BLOCK_SIZE) {
20767c478bd9Sstevel@tonic-gate 		uchar_t digested_key[MD5_DIGEST_LENGTH];
20777c478bd9Sstevel@tonic-gate 
20787c478bd9Sstevel@tonic-gate 		/*
20797c478bd9Sstevel@tonic-gate 		 * Hash the passed-in key to get a smaller key.
20807c478bd9Sstevel@tonic-gate 		 * The inner context is used since it hasn't been
20817c478bd9Sstevel@tonic-gate 		 * initialized yet.
20827c478bd9Sstevel@tonic-gate 		 */
20837c478bd9Sstevel@tonic-gate 		PROV_MD5_DIGEST_KEY(&md5_hmac_ctx_tmpl->hc_icontext,
20847c478bd9Sstevel@tonic-gate 		    key->ck_data, keylen_in_bytes, digested_key);
20857c478bd9Sstevel@tonic-gate 		md5_mac_init_ctx(md5_hmac_ctx_tmpl, digested_key,
20867c478bd9Sstevel@tonic-gate 		    MD5_DIGEST_LENGTH);
20877c478bd9Sstevel@tonic-gate 	} else {
20887c478bd9Sstevel@tonic-gate 		md5_mac_init_ctx(md5_hmac_ctx_tmpl, key->ck_data,
20897c478bd9Sstevel@tonic-gate 		    keylen_in_bytes);
20907c478bd9Sstevel@tonic-gate 	}
20917c478bd9Sstevel@tonic-gate 
20927c478bd9Sstevel@tonic-gate 	md5_hmac_ctx_tmpl->hc_mech_type = mechanism->cm_type;
20937c478bd9Sstevel@tonic-gate 	*ctx_template = (crypto_spi_ctx_template_t)md5_hmac_ctx_tmpl;
20947c478bd9Sstevel@tonic-gate 	*ctx_template_size = sizeof (md5_hmac_ctx_t);
20957c478bd9Sstevel@tonic-gate 
20967c478bd9Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
20977c478bd9Sstevel@tonic-gate }
20987c478bd9Sstevel@tonic-gate 
20997c478bd9Sstevel@tonic-gate static int
21007c478bd9Sstevel@tonic-gate md5_free_context(crypto_ctx_t *ctx)
21017c478bd9Sstevel@tonic-gate {
21027c478bd9Sstevel@tonic-gate 	uint_t ctx_len;
21037c478bd9Sstevel@tonic-gate 	md5_mech_type_t mech_type;
21047c478bd9Sstevel@tonic-gate 
21057c478bd9Sstevel@tonic-gate 	if (ctx->cc_provider_private == NULL)
21067c478bd9Sstevel@tonic-gate 		return (CRYPTO_SUCCESS);
21077c478bd9Sstevel@tonic-gate 
21087c478bd9Sstevel@tonic-gate 	/*
21097c478bd9Sstevel@tonic-gate 	 * We have to free either MD5 or MD5-HMAC contexts, which
21107c478bd9Sstevel@tonic-gate 	 * have different lengths.
21117c478bd9Sstevel@tonic-gate 	 */
21127c478bd9Sstevel@tonic-gate 
21137c478bd9Sstevel@tonic-gate 	mech_type = PROV_MD5_CTX(ctx)->mc_mech_type;
21147c478bd9Sstevel@tonic-gate 	if (mech_type == MD5_MECH_INFO_TYPE)
21157c478bd9Sstevel@tonic-gate 		ctx_len = sizeof (md5_ctx_t);
21167c478bd9Sstevel@tonic-gate 	else {
21177c478bd9Sstevel@tonic-gate 		ASSERT(mech_type == MD5_HMAC_MECH_INFO_TYPE ||
21187c478bd9Sstevel@tonic-gate 		    mech_type == MD5_HMAC_GEN_MECH_INFO_TYPE);
21197c478bd9Sstevel@tonic-gate 		ctx_len = sizeof (md5_hmac_ctx_t);
21207c478bd9Sstevel@tonic-gate 	}
21217c478bd9Sstevel@tonic-gate 
21227c478bd9Sstevel@tonic-gate 	bzero(ctx->cc_provider_private, ctx_len);
21237c478bd9Sstevel@tonic-gate 	kmem_free(ctx->cc_provider_private, ctx_len);
21247c478bd9Sstevel@tonic-gate 	ctx->cc_provider_private = NULL;
21257c478bd9Sstevel@tonic-gate 
21267c478bd9Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
21277c478bd9Sstevel@tonic-gate }
21287c478bd9Sstevel@tonic-gate 
21297c478bd9Sstevel@tonic-gate #endif	/* _KERNEL && !_BOOT */
2130