xref: /illumos-gate/usr/src/lib/pkcs11/pkcs11_softtoken/common/softSignUtil.c (revision 4c21f043137d037cfab505df3956f56ba521b170)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*4c21f043Sizick  * Common Development and Distribution License (the "License").
6*4c21f043Sizick  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*4c21f043Sizick  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <strings.h>
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <security/cryptoki.h>
327c478bd9Sstevel@tonic-gate #include "softObject.h"
337c478bd9Sstevel@tonic-gate #include "softOps.h"
347c478bd9Sstevel@tonic-gate #include "softSession.h"
357c478bd9Sstevel@tonic-gate #include "softMAC.h"
367c478bd9Sstevel@tonic-gate #include "softRSA.h"
377c478bd9Sstevel@tonic-gate #include "softDSA.h"
387c478bd9Sstevel@tonic-gate #include "softCrypt.h"
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * soft_sign_init()
427c478bd9Sstevel@tonic-gate  *
437c478bd9Sstevel@tonic-gate  * Arguments:
447c478bd9Sstevel@tonic-gate  *	session_p:	pointer to soft_session_t struct
457c478bd9Sstevel@tonic-gate  *	pMechanism:	pointer to CK_MECHANISM struct provided by application
467c478bd9Sstevel@tonic-gate  *	key_p:		pointer to key soft_object_t struct
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  * Description:
497c478bd9Sstevel@tonic-gate  *	called by C_SignInit(). This function calls the corresponding
507c478bd9Sstevel@tonic-gate  *	sign init routine based on the mechanism.
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate CK_RV
547c478bd9Sstevel@tonic-gate soft_sign_init(soft_session_t *session_p, CK_MECHANISM_PTR pMechanism,
557c478bd9Sstevel@tonic-gate     soft_object_t *key_p)
567c478bd9Sstevel@tonic-gate {
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 	switch (pMechanism->mechanism) {
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	case CKM_SSL3_MD5_MAC:
617c478bd9Sstevel@tonic-gate 	case CKM_SSL3_SHA1_MAC:
627c478bd9Sstevel@tonic-gate 	case CKM_MD5_HMAC_GENERAL:
637c478bd9Sstevel@tonic-gate 	case CKM_MD5_HMAC:
647c478bd9Sstevel@tonic-gate 	case CKM_SHA_1_HMAC_GENERAL:
657c478bd9Sstevel@tonic-gate 	case CKM_SHA_1_HMAC:
66f66d273dSizick 	case CKM_SHA256_HMAC_GENERAL:
67f66d273dSizick 	case CKM_SHA256_HMAC:
68f66d273dSizick 	case CKM_SHA384_HMAC_GENERAL:
69f66d273dSizick 	case CKM_SHA384_HMAC:
70f66d273dSizick 	case CKM_SHA512_HMAC_GENERAL:
71f66d273dSizick 	case CKM_SHA512_HMAC:
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 		return (soft_hmac_sign_verify_init_common(session_p,
747c478bd9Sstevel@tonic-gate 		    pMechanism, key_p, B_TRUE));
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	case CKM_RSA_X_509:
777c478bd9Sstevel@tonic-gate 	case CKM_RSA_PKCS:
787c478bd9Sstevel@tonic-gate 	case CKM_MD5_RSA_PKCS:
797c478bd9Sstevel@tonic-gate 	case CKM_SHA1_RSA_PKCS:
80f66d273dSizick 	case CKM_SHA256_RSA_PKCS:
81f66d273dSizick 	case CKM_SHA384_RSA_PKCS:
82f66d273dSizick 	case CKM_SHA512_RSA_PKCS:
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 		return (soft_rsa_sign_verify_init_common(session_p, pMechanism,
857c478bd9Sstevel@tonic-gate 		    key_p, B_TRUE));
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	case CKM_DSA:
887c478bd9Sstevel@tonic-gate 	case CKM_DSA_SHA1:
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 		return (soft_dsa_sign_verify_init_common(session_p, pMechanism,
917c478bd9Sstevel@tonic-gate 		    key_p, B_TRUE));
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	case CKM_DES_MAC_GENERAL:
947c478bd9Sstevel@tonic-gate 	case CKM_DES_MAC:
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 		return (soft_des_sign_verify_init_common(session_p, pMechanism,
977c478bd9Sstevel@tonic-gate 		    key_p, B_TRUE));
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	default:
1007c478bd9Sstevel@tonic-gate 		return (CKR_MECHANISM_INVALID);
1017c478bd9Sstevel@tonic-gate 	}
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate  * soft_sign()
1087c478bd9Sstevel@tonic-gate  *
1097c478bd9Sstevel@tonic-gate  * Arguments:
1107c478bd9Sstevel@tonic-gate  *      session_p:	pointer to soft_session_t struct
1117c478bd9Sstevel@tonic-gate  *	pData:		pointer to the input data to be signed
1127c478bd9Sstevel@tonic-gate  *	ulDataLen:	length of the input data
1137c478bd9Sstevel@tonic-gate  *	pSignature:	pointer to the signature after signing
1147c478bd9Sstevel@tonic-gate  *	pulSignatureLen: pointer to the length of the signature
1157c478bd9Sstevel@tonic-gate  *
1167c478bd9Sstevel@tonic-gate  * Description:
1177c478bd9Sstevel@tonic-gate  *      called by C_Sign(). This function calls the corresponding
1187c478bd9Sstevel@tonic-gate  *	sign routine based on the mechanism.
1197c478bd9Sstevel@tonic-gate  *
1207c478bd9Sstevel@tonic-gate  */
1217c478bd9Sstevel@tonic-gate CK_RV
1227c478bd9Sstevel@tonic-gate soft_sign(soft_session_t *session_p, CK_BYTE_PTR pData,
1237c478bd9Sstevel@tonic-gate     CK_ULONG ulDataLen, CK_BYTE_PTR pSignature,
1247c478bd9Sstevel@tonic-gate     CK_ULONG_PTR pulSignatureLen)
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	CK_MECHANISM_TYPE mechanism = session_p->sign.mech.mechanism;
1287c478bd9Sstevel@tonic-gate 	CK_RV rv = CKR_OK;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	switch (mechanism) {
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	case CKM_SSL3_MD5_MAC:
1337c478bd9Sstevel@tonic-gate 	case CKM_SSL3_SHA1_MAC:
1347c478bd9Sstevel@tonic-gate 	case CKM_MD5_HMAC_GENERAL:
1357c478bd9Sstevel@tonic-gate 	case CKM_MD5_HMAC:
1367c478bd9Sstevel@tonic-gate 	case CKM_SHA_1_HMAC_GENERAL:
1377c478bd9Sstevel@tonic-gate 	case CKM_SHA_1_HMAC:
138f66d273dSizick 	case CKM_SHA256_HMAC_GENERAL:
139f66d273dSizick 	case CKM_SHA256_HMAC:
140f66d273dSizick 	case CKM_SHA384_HMAC_GENERAL:
141f66d273dSizick 	case CKM_SHA384_HMAC:
142f66d273dSizick 	case CKM_SHA512_HMAC_GENERAL:
143f66d273dSizick 	case CKM_SHA512_HMAC:
1447c478bd9Sstevel@tonic-gate 	{
145f66d273dSizick 		CK_BYTE hmac[SHA512_DIGEST_LENGTH]; /* use the maximum size */
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 		if (pSignature != NULL) {
1487c478bd9Sstevel@tonic-gate 			/* Pass local buffer to avoid overflow. */
1497c478bd9Sstevel@tonic-gate 			rv = soft_hmac_sign_verify_common(session_p, pData,
1507c478bd9Sstevel@tonic-gate 			    ulDataLen, hmac, pulSignatureLen, B_TRUE);
1517c478bd9Sstevel@tonic-gate 		} else {
1527c478bd9Sstevel@tonic-gate 			/* Pass original pSignature, let callee to handle it. */
1537c478bd9Sstevel@tonic-gate 			rv = soft_hmac_sign_verify_common(session_p, pData,
1547c478bd9Sstevel@tonic-gate 			    ulDataLen, pSignature, pulSignatureLen, B_TRUE);
1557c478bd9Sstevel@tonic-gate 		}
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 		if ((rv == CKR_OK) && (pSignature != NULL))
1587c478bd9Sstevel@tonic-gate 			(void) memcpy(pSignature, hmac, *pulSignatureLen);
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 		return (rv);
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate 	case CKM_DES_MAC_GENERAL:
1637c478bd9Sstevel@tonic-gate 	case CKM_DES_MAC:
1647c478bd9Sstevel@tonic-gate 	{
1657c478bd9Sstevel@tonic-gate 		CK_BYTE signature[DES_BLOCK_LEN]; /* use the maximum size */
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 		if (pSignature != NULL) {
1687c478bd9Sstevel@tonic-gate 			/* Pass local buffer to avoid overflow. */
1697c478bd9Sstevel@tonic-gate 			rv = soft_des_sign_verify_common(session_p, pData,
1707c478bd9Sstevel@tonic-gate 				ulDataLen, signature, pulSignatureLen, B_TRUE,
1717c478bd9Sstevel@tonic-gate 				B_FALSE);
1727c478bd9Sstevel@tonic-gate 		} else {
1737c478bd9Sstevel@tonic-gate 			/* Pass NULL, let callee to handle it. */
1747c478bd9Sstevel@tonic-gate 			rv = soft_des_sign_verify_common(session_p, pData,
1757c478bd9Sstevel@tonic-gate 				ulDataLen, NULL, pulSignatureLen, B_TRUE,
1767c478bd9Sstevel@tonic-gate 				B_FALSE);
1777c478bd9Sstevel@tonic-gate 		}
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 		if ((rv == CKR_OK) && (pSignature != NULL))
1807c478bd9Sstevel@tonic-gate 			(void) memcpy(pSignature, signature, *pulSignatureLen);
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 		return (rv);
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate 	case CKM_RSA_X_509:
1857c478bd9Sstevel@tonic-gate 	case CKM_RSA_PKCS:
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 		return (soft_rsa_sign_common(session_p, pData, ulDataLen,
1887c478bd9Sstevel@tonic-gate 		    pSignature, pulSignatureLen, mechanism));
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	case CKM_MD5_RSA_PKCS:
1917c478bd9Sstevel@tonic-gate 	case CKM_SHA1_RSA_PKCS:
192f66d273dSizick 	case CKM_SHA256_RSA_PKCS:
193f66d273dSizick 	case CKM_SHA384_RSA_PKCS:
194f66d273dSizick 	case CKM_SHA512_RSA_PKCS:
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 		return (soft_rsa_digest_sign_common(session_p, pData, ulDataLen,
1977c478bd9Sstevel@tonic-gate 		    pSignature, pulSignatureLen, mechanism, B_FALSE));
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	case CKM_DSA:
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 		return (soft_dsa_sign(session_p, pData, ulDataLen,
2027c478bd9Sstevel@tonic-gate 		    pSignature, pulSignatureLen));
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	case CKM_DSA_SHA1:
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 		return (soft_dsa_digest_sign_common(session_p, pData, ulDataLen,
2077c478bd9Sstevel@tonic-gate 		    pSignature, pulSignatureLen, B_FALSE));
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	default:
2107c478bd9Sstevel@tonic-gate 		return (CKR_MECHANISM_INVALID);
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate /*
2167c478bd9Sstevel@tonic-gate  * soft_sign_update()
2177c478bd9Sstevel@tonic-gate  *
2187c478bd9Sstevel@tonic-gate  * Arguments:
2197c478bd9Sstevel@tonic-gate  *      session_p:	pointer to soft_session_t struct
2207c478bd9Sstevel@tonic-gate  *      pPart:		pointer to the input data to be signed
2217c478bd9Sstevel@tonic-gate  *      ulPartLen:	length of the input data
2227c478bd9Sstevel@tonic-gate  *
2237c478bd9Sstevel@tonic-gate  * Description:
2247c478bd9Sstevel@tonic-gate  *      called by C_SignUpdate(). This function calls the corresponding
2257c478bd9Sstevel@tonic-gate  *	sign update routine based on the mechanism.
2267c478bd9Sstevel@tonic-gate  *
2277c478bd9Sstevel@tonic-gate  */
2287c478bd9Sstevel@tonic-gate CK_RV
2297c478bd9Sstevel@tonic-gate soft_sign_update(soft_session_t *session_p, CK_BYTE_PTR pPart,
2307c478bd9Sstevel@tonic-gate     CK_ULONG ulPartLen)
2317c478bd9Sstevel@tonic-gate {
2327c478bd9Sstevel@tonic-gate 	CK_MECHANISM_TYPE	mechanism = session_p->sign.mech.mechanism;
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	switch (mechanism) {
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	case CKM_SSL3_MD5_MAC:
2377c478bd9Sstevel@tonic-gate 	case CKM_SSL3_SHA1_MAC:
2387c478bd9Sstevel@tonic-gate 	case CKM_MD5_HMAC_GENERAL:
2397c478bd9Sstevel@tonic-gate 	case CKM_MD5_HMAC:
2407c478bd9Sstevel@tonic-gate 	case CKM_SHA_1_HMAC_GENERAL:
2417c478bd9Sstevel@tonic-gate 	case CKM_SHA_1_HMAC:
242f66d273dSizick 	case CKM_SHA256_HMAC_GENERAL:
243f66d273dSizick 	case CKM_SHA256_HMAC:
244f66d273dSizick 	case CKM_SHA384_HMAC_GENERAL:
245f66d273dSizick 	case CKM_SHA384_HMAC:
246f66d273dSizick 	case CKM_SHA512_HMAC_GENERAL:
247f66d273dSizick 	case CKM_SHA512_HMAC:
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 		return (soft_hmac_sign_verify_update(session_p, pPart,
2507c478bd9Sstevel@tonic-gate 		    ulPartLen, B_TRUE));
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	case CKM_DES_MAC_GENERAL:
2537c478bd9Sstevel@tonic-gate 	case CKM_DES_MAC:
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 		return (soft_des_mac_sign_verify_update(session_p, pPart,
2567c478bd9Sstevel@tonic-gate 		    ulPartLen));
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	case CKM_MD5_RSA_PKCS:
2597c478bd9Sstevel@tonic-gate 	case CKM_SHA1_RSA_PKCS:
260f66d273dSizick 	case CKM_SHA256_RSA_PKCS:
261f66d273dSizick 	case CKM_SHA384_RSA_PKCS:
262f66d273dSizick 	case CKM_SHA512_RSA_PKCS:
2637c478bd9Sstevel@tonic-gate 		/*
2647c478bd9Sstevel@tonic-gate 		 * The MD5/SHA1 digest value is accumulated in the context
2657c478bd9Sstevel@tonic-gate 		 * of the multiple-part digesting operation. In the final
2667c478bd9Sstevel@tonic-gate 		 * operation, the digest is encoded and then perform RSA
2677c478bd9Sstevel@tonic-gate 		 * signing.
2687c478bd9Sstevel@tonic-gate 		 */
2697c478bd9Sstevel@tonic-gate 	case CKM_DSA_SHA1:
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 		return (soft_digest_update(session_p, pPart, ulPartLen));
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	default:
2747c478bd9Sstevel@tonic-gate 		/* PKCS11: The mechanism only supports single-part operation. */
2757c478bd9Sstevel@tonic-gate 		return (CKR_MECHANISM_INVALID);
2767c478bd9Sstevel@tonic-gate 	}
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate /*
2817c478bd9Sstevel@tonic-gate  * soft_sign_final()
2827c478bd9Sstevel@tonic-gate  *
2837c478bd9Sstevel@tonic-gate  * Arguments:
2847c478bd9Sstevel@tonic-gate  *      session_p:	pointer to soft_session_t struct
2857c478bd9Sstevel@tonic-gate  *      pSignature:	pointer to the signature after signing
2867c478bd9Sstevel@tonic-gate  *      pulSignatureLen: pointer to the	length of the signature
2877c478bd9Sstevel@tonic-gate  *
2887c478bd9Sstevel@tonic-gate  * Description:
2897c478bd9Sstevel@tonic-gate  *      called by C_SignFinal(). This function calls the corresponding
2907c478bd9Sstevel@tonic-gate  *	sign final routine based on the mechanism.
2917c478bd9Sstevel@tonic-gate  *
2927c478bd9Sstevel@tonic-gate  */
2937c478bd9Sstevel@tonic-gate CK_RV
2947c478bd9Sstevel@tonic-gate soft_sign_final(soft_session_t *session_p, CK_BYTE_PTR pSignature,
2957c478bd9Sstevel@tonic-gate     CK_ULONG_PTR pulSignatureLen)
2967c478bd9Sstevel@tonic-gate {
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	CK_MECHANISM_TYPE mechanism = session_p->sign.mech.mechanism;
2997c478bd9Sstevel@tonic-gate 	CK_RV rv = CKR_OK;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	switch (mechanism) {
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	case CKM_SSL3_MD5_MAC:
3047c478bd9Sstevel@tonic-gate 	case CKM_SSL3_SHA1_MAC:
3057c478bd9Sstevel@tonic-gate 	case CKM_MD5_HMAC_GENERAL:
3067c478bd9Sstevel@tonic-gate 	case CKM_MD5_HMAC:
3077c478bd9Sstevel@tonic-gate 	case CKM_SHA_1_HMAC_GENERAL:
3087c478bd9Sstevel@tonic-gate 	case CKM_SHA_1_HMAC:
309f66d273dSizick 	case CKM_SHA256_HMAC_GENERAL:
310f66d273dSizick 	case CKM_SHA256_HMAC:
311f66d273dSizick 	case CKM_SHA384_HMAC_GENERAL:
312f66d273dSizick 	case CKM_SHA384_HMAC:
313f66d273dSizick 	case CKM_SHA512_HMAC_GENERAL:
314f66d273dSizick 	case CKM_SHA512_HMAC:
3157c478bd9Sstevel@tonic-gate 	{
316f66d273dSizick 		CK_BYTE hmac[SHA512_DIGEST_LENGTH]; /* use the maximum size */
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 		if (pSignature != NULL) {
3197c478bd9Sstevel@tonic-gate 			/* Pass local buffer to avoid overflow */
3207c478bd9Sstevel@tonic-gate 			rv = soft_hmac_sign_verify_common(session_p, NULL,
3217c478bd9Sstevel@tonic-gate 			    0, hmac, pulSignatureLen, B_TRUE);
3227c478bd9Sstevel@tonic-gate 		} else {
3237c478bd9Sstevel@tonic-gate 			/* Pass original pSignature, let callee to handle it. */
3247c478bd9Sstevel@tonic-gate 			rv = soft_hmac_sign_verify_common(session_p, NULL,
3257c478bd9Sstevel@tonic-gate 			    0, pSignature, pulSignatureLen, B_TRUE);
3267c478bd9Sstevel@tonic-gate 		}
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 		if ((rv == CKR_OK) && (pSignature != NULL))
3297c478bd9Sstevel@tonic-gate 			(void) memcpy(pSignature, hmac, *pulSignatureLen);
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 		return (rv);
3327c478bd9Sstevel@tonic-gate 	}
3337c478bd9Sstevel@tonic-gate 	case CKM_DES_MAC_GENERAL:
3347c478bd9Sstevel@tonic-gate 	case CKM_DES_MAC:
3357c478bd9Sstevel@tonic-gate 	{
3367c478bd9Sstevel@tonic-gate 		CK_BYTE signature[DES_BLOCK_LEN]; /* use the maximum size */
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 		if (pSignature != NULL) {
3397c478bd9Sstevel@tonic-gate 			/* Pass local buffer to avoid overflow. */
3407c478bd9Sstevel@tonic-gate 			rv = soft_des_sign_verify_common(session_p, NULL, 0,
3417c478bd9Sstevel@tonic-gate 				signature, pulSignatureLen, B_TRUE, B_TRUE);
3427c478bd9Sstevel@tonic-gate 		} else {
3437c478bd9Sstevel@tonic-gate 			/* Pass NULL, let callee to handle it. */
3447c478bd9Sstevel@tonic-gate 			rv = soft_des_sign_verify_common(session_p, NULL, 0,
3457c478bd9Sstevel@tonic-gate 				NULL, pulSignatureLen, B_TRUE, B_TRUE);
3467c478bd9Sstevel@tonic-gate 		}
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 		if ((rv == CKR_OK) && (pSignature != NULL))
3497c478bd9Sstevel@tonic-gate 			(void) memcpy(pSignature, signature, *pulSignatureLen);
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 		return (rv);
3527c478bd9Sstevel@tonic-gate 	}
3537c478bd9Sstevel@tonic-gate 	case CKM_MD5_RSA_PKCS:
3547c478bd9Sstevel@tonic-gate 	case CKM_SHA1_RSA_PKCS:
355f66d273dSizick 	case CKM_SHA256_RSA_PKCS:
356f66d273dSizick 	case CKM_SHA384_RSA_PKCS:
357f66d273dSizick 	case CKM_SHA512_RSA_PKCS:
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 		return (soft_rsa_digest_sign_common(session_p, NULL, 0,
3607c478bd9Sstevel@tonic-gate 		    pSignature, pulSignatureLen, mechanism, B_TRUE));
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	case CKM_DSA_SHA1:
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 		return (soft_dsa_digest_sign_common(session_p, NULL, 0,
3657c478bd9Sstevel@tonic-gate 		    pSignature, pulSignatureLen, B_TRUE));
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	default:
3687c478bd9Sstevel@tonic-gate 		/* PKCS11: The mechanism only supports single-part operation. */
3697c478bd9Sstevel@tonic-gate 		return (CKR_MECHANISM_INVALID);
3707c478bd9Sstevel@tonic-gate 	}
3717c478bd9Sstevel@tonic-gate }
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate CK_RV
3757c478bd9Sstevel@tonic-gate soft_sign_recover_init(soft_session_t *session_p, CK_MECHANISM_PTR pMechanism,
3767c478bd9Sstevel@tonic-gate     soft_object_t *key_p)
3777c478bd9Sstevel@tonic-gate {
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	switch (pMechanism->mechanism) {
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	case CKM_RSA_X_509:
3827c478bd9Sstevel@tonic-gate 	case CKM_RSA_PKCS:
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 		return (soft_rsa_sign_verify_init_common(session_p, pMechanism,
3857c478bd9Sstevel@tonic-gate 		    key_p, B_TRUE));
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	default:
3887c478bd9Sstevel@tonic-gate 		return (CKR_MECHANISM_INVALID);
3897c478bd9Sstevel@tonic-gate 	}
3907c478bd9Sstevel@tonic-gate }
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate CK_RV
3947c478bd9Sstevel@tonic-gate soft_sign_recover(soft_session_t *session_p, CK_BYTE_PTR pData,
3957c478bd9Sstevel@tonic-gate     CK_ULONG ulDataLen, CK_BYTE_PTR pSignature,
3967c478bd9Sstevel@tonic-gate     CK_ULONG_PTR pulSignatureLen)
3977c478bd9Sstevel@tonic-gate {
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	CK_MECHANISM_TYPE mechanism = session_p->sign.mech.mechanism;
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	switch (mechanism) {
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	case CKM_RSA_X_509:
4047c478bd9Sstevel@tonic-gate 	case CKM_RSA_PKCS:
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 		return (soft_rsa_sign_common(session_p, pData, ulDataLen,
4077c478bd9Sstevel@tonic-gate 		    pSignature, pulSignatureLen, mechanism));
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	default:
4107c478bd9Sstevel@tonic-gate 		return (CKR_MECHANISM_INVALID);
4117c478bd9Sstevel@tonic-gate 	}
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate /*
4157c478bd9Sstevel@tonic-gate  * This function frees the allocated active crypto context.
4167c478bd9Sstevel@tonic-gate  * It is only called by the first tier of sign/verify routines
4177c478bd9Sstevel@tonic-gate  * and the caller of this function may or may not hold the session mutex.
4187c478bd9Sstevel@tonic-gate  */
4197c478bd9Sstevel@tonic-gate void
4207c478bd9Sstevel@tonic-gate soft_sign_verify_cleanup(soft_session_t *session_p, boolean_t sign,
4217c478bd9Sstevel@tonic-gate     boolean_t lock_held)
4227c478bd9Sstevel@tonic-gate {
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	crypto_active_op_t *active_op;
4257c478bd9Sstevel@tonic-gate 	boolean_t lock_true = B_TRUE;
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	if (!lock_held)
4287c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_lock(&session_p->session_mutex);
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	active_op = (sign) ? &(session_p->sign) : &(session_p->verify);
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	switch (active_op->mech.mechanism) {
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	case CKM_MD5_RSA_PKCS:
4357c478bd9Sstevel@tonic-gate 	case CKM_SHA1_RSA_PKCS:
436f66d273dSizick 	case CKM_SHA256_RSA_PKCS:
437f66d273dSizick 	case CKM_SHA384_RSA_PKCS:
438f66d273dSizick 	case CKM_SHA512_RSA_PKCS:
4397c478bd9Sstevel@tonic-gate 		if (session_p->digest.context != NULL) {
4407c478bd9Sstevel@tonic-gate 			free(session_p->digest.context);
4417c478bd9Sstevel@tonic-gate 			session_p->digest.context = NULL;
4427c478bd9Sstevel@tonic-gate 			session_p->digest.flags = 0;
4437c478bd9Sstevel@tonic-gate 		}
444*4c21f043Sizick 		/* FALLTHRU */
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	case CKM_RSA_PKCS:
4477c478bd9Sstevel@tonic-gate 	case CKM_RSA_X_509:
448*4c21f043Sizick 	{
449*4c21f043Sizick 		soft_rsa_ctx_t *rsa_ctx =
450*4c21f043Sizick 		    (soft_rsa_ctx_t *)active_op->context;
451*4c21f043Sizick 
452*4c21f043Sizick 		if (rsa_ctx != NULL && rsa_ctx->key != NULL) {
453*4c21f043Sizick 			soft_cleanup_object(rsa_ctx->key);
454*4c21f043Sizick 			free(rsa_ctx->key);
455*4c21f043Sizick 		}
456*4c21f043Sizick 		break;
457*4c21f043Sizick 
458*4c21f043Sizick 	}
459*4c21f043Sizick 	case CKM_DSA_SHA1:
460*4c21f043Sizick 		if (session_p->digest.context != NULL) {
461*4c21f043Sizick 			free(session_p->digest.context);
462*4c21f043Sizick 			session_p->digest.context = NULL;
463*4c21f043Sizick 			session_p->digest.flags = 0;
464*4c21f043Sizick 		}
465*4c21f043Sizick 
466*4c21f043Sizick 		/* FALLTHRU */
4677c478bd9Sstevel@tonic-gate 	case CKM_DSA:
468*4c21f043Sizick 	{
469*4c21f043Sizick 		soft_dsa_ctx_t *dsa_ctx =
470*4c21f043Sizick 		    (soft_dsa_ctx_t *)active_op->context;
471*4c21f043Sizick 
472*4c21f043Sizick 		if (dsa_ctx != NULL && dsa_ctx->key != NULL) {
473*4c21f043Sizick 			soft_cleanup_object(dsa_ctx->key);
474*4c21f043Sizick 			free(dsa_ctx->key);
475*4c21f043Sizick 		}
4767c478bd9Sstevel@tonic-gate 		break;
4777c478bd9Sstevel@tonic-gate 
478*4c21f043Sizick 	}
4797c478bd9Sstevel@tonic-gate 	case CKM_SSL3_MD5_MAC:
4807c478bd9Sstevel@tonic-gate 	case CKM_SSL3_SHA1_MAC:
4817c478bd9Sstevel@tonic-gate 	case CKM_MD5_HMAC_GENERAL:
4827c478bd9Sstevel@tonic-gate 	case CKM_MD5_HMAC:
4837c478bd9Sstevel@tonic-gate 	case CKM_SHA_1_HMAC_GENERAL:
4847c478bd9Sstevel@tonic-gate 	case CKM_SHA_1_HMAC:
485f66d273dSizick 	case CKM_SHA256_HMAC_GENERAL:
486f66d273dSizick 	case CKM_SHA256_HMAC:
487f66d273dSizick 	case CKM_SHA384_HMAC_GENERAL:
488f66d273dSizick 	case CKM_SHA384_HMAC:
489f66d273dSizick 	case CKM_SHA512_HMAC_GENERAL:
490f66d273dSizick 	case CKM_SHA512_HMAC:
4917c478bd9Sstevel@tonic-gate 		if (active_op->context != NULL)
4927c478bd9Sstevel@tonic-gate 			bzero(active_op->context, sizeof (soft_hmac_ctx_t));
4937c478bd9Sstevel@tonic-gate 		break;
4947c478bd9Sstevel@tonic-gate 	case CKM_DES_MAC_GENERAL:
4957c478bd9Sstevel@tonic-gate 	case CKM_DES_MAC:
4967c478bd9Sstevel@tonic-gate 		if (session_p->encrypt.context != NULL) {
4977c478bd9Sstevel@tonic-gate 			free(session_p->encrypt.context);
4987c478bd9Sstevel@tonic-gate 			session_p->encrypt.context = NULL;
4997c478bd9Sstevel@tonic-gate 			session_p->encrypt.flags = 0;
5007c478bd9Sstevel@tonic-gate 		}
5017c478bd9Sstevel@tonic-gate 		if (active_op->context != NULL)
5027c478bd9Sstevel@tonic-gate 			bzero(active_op->context, sizeof (soft_des_ctx_t));
5037c478bd9Sstevel@tonic-gate 		break;
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	}
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	if (active_op->context != NULL) {
5087c478bd9Sstevel@tonic-gate 		free(active_op->context);
5097c478bd9Sstevel@tonic-gate 		active_op->context = NULL;
5107c478bd9Sstevel@tonic-gate 	}
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	active_op->flags = 0;
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	if (!lock_held)
5157c478bd9Sstevel@tonic-gate 		SES_REFRELE(session_p, lock_true);
5167c478bd9Sstevel@tonic-gate }
517