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
54c21f043Sizick  * Common Development and Distribution License (the "License").
64c21f043Sizick  * 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 /*
22f9fbec18Smcpowers  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
24cd964fceSMatt Barden  * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
25a8793c76SJason King  * Copyright (c) 2018, Joyent, Inc.
267c478bd9Sstevel@tonic-gate  */
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"
38f9fbec18Smcpowers #include "softEC.h"
397c478bd9Sstevel@tonic-gate #include "softCrypt.h"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * soft_sign_init()
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  * Arguments:
457c478bd9Sstevel@tonic-gate  *	session_p:	pointer to soft_session_t struct
467c478bd9Sstevel@tonic-gate  *	pMechanism:	pointer to CK_MECHANISM struct provided by application
477c478bd9Sstevel@tonic-gate  *	key_p:		pointer to key soft_object_t struct
487c478bd9Sstevel@tonic-gate  *
497c478bd9Sstevel@tonic-gate  * Description:
507c478bd9Sstevel@tonic-gate  *	called by C_SignInit(). This function calls the corresponding
517c478bd9Sstevel@tonic-gate  *	sign init routine based on the mechanism.
527c478bd9Sstevel@tonic-gate  *
537c478bd9Sstevel@tonic-gate  */
547c478bd9Sstevel@tonic-gate CK_RV
soft_sign_init(soft_session_t * session_p,CK_MECHANISM_PTR pMechanism,soft_object_t * key_p)557c478bd9Sstevel@tonic-gate soft_sign_init(soft_session_t *session_p, CK_MECHANISM_PTR pMechanism,
567c478bd9Sstevel@tonic-gate     soft_object_t *key_p)
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	switch (pMechanism->mechanism) {
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	case CKM_SSL3_MD5_MAC:
627c478bd9Sstevel@tonic-gate 	case CKM_SSL3_SHA1_MAC:
637c478bd9Sstevel@tonic-gate 	case CKM_MD5_HMAC_GENERAL:
647c478bd9Sstevel@tonic-gate 	case CKM_MD5_HMAC:
657c478bd9Sstevel@tonic-gate 	case CKM_SHA_1_HMAC_GENERAL:
667c478bd9Sstevel@tonic-gate 	case CKM_SHA_1_HMAC:
67f66d273dSizick 	case CKM_SHA256_HMAC_GENERAL:
68f66d273dSizick 	case CKM_SHA256_HMAC:
69f66d273dSizick 	case CKM_SHA384_HMAC_GENERAL:
70f66d273dSizick 	case CKM_SHA384_HMAC:
71f66d273dSizick 	case CKM_SHA512_HMAC_GENERAL:
72f66d273dSizick 	case CKM_SHA512_HMAC:
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 		return (soft_hmac_sign_verify_init_common(session_p,
757c478bd9Sstevel@tonic-gate 		    pMechanism, key_p, B_TRUE));
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	case CKM_RSA_X_509:
787c478bd9Sstevel@tonic-gate 	case CKM_RSA_PKCS:
797c478bd9Sstevel@tonic-gate 	case CKM_MD5_RSA_PKCS:
807c478bd9Sstevel@tonic-gate 	case CKM_SHA1_RSA_PKCS:
81f66d273dSizick 	case CKM_SHA256_RSA_PKCS:
82f66d273dSizick 	case CKM_SHA384_RSA_PKCS:
83f66d273dSizick 	case CKM_SHA512_RSA_PKCS:
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 		return (soft_rsa_sign_verify_init_common(session_p, pMechanism,
867c478bd9Sstevel@tonic-gate 		    key_p, B_TRUE));
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	case CKM_DSA:
897c478bd9Sstevel@tonic-gate 	case CKM_DSA_SHA1:
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 		return (soft_dsa_sign_verify_init_common(session_p, pMechanism,
927c478bd9Sstevel@tonic-gate 		    key_p, B_TRUE));
937c478bd9Sstevel@tonic-gate 
94f9fbec18Smcpowers 	case CKM_ECDSA:
95f9fbec18Smcpowers 	case CKM_ECDSA_SHA1:
96f9fbec18Smcpowers 
97f9fbec18Smcpowers 		return (soft_ecc_sign_verify_init_common(session_p, pMechanism,
98f9fbec18Smcpowers 		    key_p, B_TRUE));
99f9fbec18Smcpowers 
1007c478bd9Sstevel@tonic-gate 	case CKM_DES_MAC_GENERAL:
1017c478bd9Sstevel@tonic-gate 	case CKM_DES_MAC:
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 		return (soft_des_sign_verify_init_common(session_p, pMechanism,
1047c478bd9Sstevel@tonic-gate 		    key_p, B_TRUE));
1057c478bd9Sstevel@tonic-gate 
106cd964fceSMatt Barden 	case CKM_AES_CMAC_GENERAL:
107cd964fceSMatt Barden 	case CKM_AES_CMAC:
108cd964fceSMatt Barden 
109cd964fceSMatt Barden 		return (soft_aes_sign_verify_init_common(session_p, pMechanism,
110cd964fceSMatt Barden 		    key_p, B_TRUE));
111cd964fceSMatt Barden 
1127c478bd9Sstevel@tonic-gate 	default:
1137c478bd9Sstevel@tonic-gate 		return (CKR_MECHANISM_INVALID);
1147c478bd9Sstevel@tonic-gate 	}
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate /*
1207c478bd9Sstevel@tonic-gate  * soft_sign()
1217c478bd9Sstevel@tonic-gate  *
1227c478bd9Sstevel@tonic-gate  * Arguments:
1237c478bd9Sstevel@tonic-gate  *      session_p:	pointer to soft_session_t struct
1247c478bd9Sstevel@tonic-gate  *	pData:		pointer to the input data to be signed
1257c478bd9Sstevel@tonic-gate  *	ulDataLen:	length of the input data
1267c478bd9Sstevel@tonic-gate  *	pSignature:	pointer to the signature after signing
1277c478bd9Sstevel@tonic-gate  *	pulSignatureLen: pointer to the length of the signature
1287c478bd9Sstevel@tonic-gate  *
1297c478bd9Sstevel@tonic-gate  * Description:
1307c478bd9Sstevel@tonic-gate  *      called by C_Sign(). This function calls the corresponding
1317c478bd9Sstevel@tonic-gate  *	sign routine based on the mechanism.
1327c478bd9Sstevel@tonic-gate  *
1337c478bd9Sstevel@tonic-gate  */
1347c478bd9Sstevel@tonic-gate CK_RV
soft_sign(soft_session_t * session_p,CK_BYTE_PTR pData,CK_ULONG ulDataLen,CK_BYTE_PTR pSignature,CK_ULONG_PTR pulSignatureLen)1357c478bd9Sstevel@tonic-gate soft_sign(soft_session_t *session_p, CK_BYTE_PTR pData,
1367c478bd9Sstevel@tonic-gate     CK_ULONG ulDataLen, CK_BYTE_PTR pSignature,
1377c478bd9Sstevel@tonic-gate     CK_ULONG_PTR pulSignatureLen)
1387c478bd9Sstevel@tonic-gate {
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	CK_MECHANISM_TYPE mechanism = session_p->sign.mech.mechanism;
1417c478bd9Sstevel@tonic-gate 	CK_RV rv = CKR_OK;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	switch (mechanism) {
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	case CKM_SSL3_MD5_MAC:
1467c478bd9Sstevel@tonic-gate 	case CKM_SSL3_SHA1_MAC:
1477c478bd9Sstevel@tonic-gate 	case CKM_MD5_HMAC_GENERAL:
1487c478bd9Sstevel@tonic-gate 	case CKM_MD5_HMAC:
1497c478bd9Sstevel@tonic-gate 	case CKM_SHA_1_HMAC_GENERAL:
1507c478bd9Sstevel@tonic-gate 	case CKM_SHA_1_HMAC:
151f66d273dSizick 	case CKM_SHA256_HMAC_GENERAL:
152f66d273dSizick 	case CKM_SHA256_HMAC:
153f66d273dSizick 	case CKM_SHA384_HMAC_GENERAL:
154f66d273dSizick 	case CKM_SHA384_HMAC:
155f66d273dSizick 	case CKM_SHA512_HMAC_GENERAL:
156f66d273dSizick 	case CKM_SHA512_HMAC:
1577c478bd9Sstevel@tonic-gate 	{
158f66d273dSizick 		CK_BYTE hmac[SHA512_DIGEST_LENGTH]; /* use the maximum size */
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 		if (pSignature != NULL) {
1617c478bd9Sstevel@tonic-gate 			/* Pass local buffer to avoid overflow. */
1627c478bd9Sstevel@tonic-gate 			rv = soft_hmac_sign_verify_common(session_p, pData,
1637c478bd9Sstevel@tonic-gate 			    ulDataLen, hmac, pulSignatureLen, B_TRUE);
1647c478bd9Sstevel@tonic-gate 		} else {
1657c478bd9Sstevel@tonic-gate 			/* Pass original pSignature, let callee to handle it. */
1667c478bd9Sstevel@tonic-gate 			rv = soft_hmac_sign_verify_common(session_p, pData,
1677c478bd9Sstevel@tonic-gate 			    ulDataLen, pSignature, pulSignatureLen, B_TRUE);
1687c478bd9Sstevel@tonic-gate 		}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 		if ((rv == CKR_OK) && (pSignature != NULL))
1717c478bd9Sstevel@tonic-gate 			(void) memcpy(pSignature, hmac, *pulSignatureLen);
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 		return (rv);
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 	case CKM_DES_MAC_GENERAL:
1767c478bd9Sstevel@tonic-gate 	case CKM_DES_MAC:
1777c478bd9Sstevel@tonic-gate 	{
1787c478bd9Sstevel@tonic-gate 		CK_BYTE signature[DES_BLOCK_LEN]; /* use the maximum size */
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 		if (pSignature != NULL) {
1817c478bd9Sstevel@tonic-gate 			/* Pass local buffer to avoid overflow. */
1827c478bd9Sstevel@tonic-gate 			rv = soft_des_sign_verify_common(session_p, pData,
183f9fbec18Smcpowers 			    ulDataLen, signature, pulSignatureLen, B_TRUE,
184f9fbec18Smcpowers 			    B_FALSE);
1857c478bd9Sstevel@tonic-gate 		} else {
1867c478bd9Sstevel@tonic-gate 			/* Pass NULL, let callee to handle it. */
1877c478bd9Sstevel@tonic-gate 			rv = soft_des_sign_verify_common(session_p, pData,
188f9fbec18Smcpowers 			    ulDataLen, NULL, pulSignatureLen, B_TRUE, B_FALSE);
1897c478bd9Sstevel@tonic-gate 		}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 		if ((rv == CKR_OK) && (pSignature != NULL))
1927c478bd9Sstevel@tonic-gate 			(void) memcpy(pSignature, signature, *pulSignatureLen);
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 		return (rv);
1957c478bd9Sstevel@tonic-gate 	}
196cd964fceSMatt Barden 	case CKM_AES_CMAC_GENERAL:
197cd964fceSMatt Barden 	case CKM_AES_CMAC:
198cd964fceSMatt Barden 	{
199cd964fceSMatt Barden 		CK_BYTE signature[AES_BLOCK_LEN];
200cd964fceSMatt Barden 
201cd964fceSMatt Barden 		if (pSignature != NULL) {
202cd964fceSMatt Barden 			/* Pass local buffer to avoid overflow. */
203cd964fceSMatt Barden 			rv = soft_aes_sign_verify_common(session_p, pData,
204cd964fceSMatt Barden 			    ulDataLen, signature, pulSignatureLen, B_TRUE,
205cd964fceSMatt Barden 			    B_FALSE);
206cd964fceSMatt Barden 		} else {
207cd964fceSMatt Barden 			/* Pass NULL, let callee handle it. */
208cd964fceSMatt Barden 			rv = soft_aes_sign_verify_common(session_p, pData,
209cd964fceSMatt Barden 			    ulDataLen, NULL, pulSignatureLen, B_TRUE, B_FALSE);
210cd964fceSMatt Barden 		}
211cd964fceSMatt Barden 
212cd964fceSMatt Barden 		if ((rv == CKR_OK) && (pSignature != NULL))
213cd964fceSMatt Barden 			(void) memcpy(pSignature, signature, *pulSignatureLen);
214cd964fceSMatt Barden 
215cd964fceSMatt Barden 		return (rv);
216cd964fceSMatt Barden 	}
2177c478bd9Sstevel@tonic-gate 	case CKM_RSA_X_509:
2187c478bd9Sstevel@tonic-gate 	case CKM_RSA_PKCS:
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 		return (soft_rsa_sign_common(session_p, pData, ulDataLen,
2217c478bd9Sstevel@tonic-gate 		    pSignature, pulSignatureLen, mechanism));
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	case CKM_MD5_RSA_PKCS:
2247c478bd9Sstevel@tonic-gate 	case CKM_SHA1_RSA_PKCS:
225f66d273dSizick 	case CKM_SHA256_RSA_PKCS:
226f66d273dSizick 	case CKM_SHA384_RSA_PKCS:
227f66d273dSizick 	case CKM_SHA512_RSA_PKCS:
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 		return (soft_rsa_digest_sign_common(session_p, pData, ulDataLen,
2307c478bd9Sstevel@tonic-gate 		    pSignature, pulSignatureLen, mechanism, B_FALSE));
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	case CKM_DSA:
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 		return (soft_dsa_sign(session_p, pData, ulDataLen,
2357c478bd9Sstevel@tonic-gate 		    pSignature, pulSignatureLen));
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	case CKM_DSA_SHA1:
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 		return (soft_dsa_digest_sign_common(session_p, pData, ulDataLen,
2407c478bd9Sstevel@tonic-gate 		    pSignature, pulSignatureLen, B_FALSE));
2417c478bd9Sstevel@tonic-gate 
242f9fbec18Smcpowers 	case CKM_ECDSA:
243f9fbec18Smcpowers 
244f9fbec18Smcpowers 		return (soft_ecc_sign(session_p, pData, ulDataLen,
245f9fbec18Smcpowers 		    pSignature, pulSignatureLen));
246f9fbec18Smcpowers 
247f9fbec18Smcpowers 	case CKM_ECDSA_SHA1:
248f9fbec18Smcpowers 
249f9fbec18Smcpowers 		return (soft_ecc_digest_sign_common(session_p, pData, ulDataLen,
250f9fbec18Smcpowers 		    pSignature, pulSignatureLen, B_FALSE));
251f9fbec18Smcpowers 
2527c478bd9Sstevel@tonic-gate 	default:
2537c478bd9Sstevel@tonic-gate 		return (CKR_MECHANISM_INVALID);
2547c478bd9Sstevel@tonic-gate 	}
2557c478bd9Sstevel@tonic-gate }
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate /*
2597c478bd9Sstevel@tonic-gate  * soft_sign_update()
2607c478bd9Sstevel@tonic-gate  *
2617c478bd9Sstevel@tonic-gate  * Arguments:
2627c478bd9Sstevel@tonic-gate  *      session_p:	pointer to soft_session_t struct
2637c478bd9Sstevel@tonic-gate  *      pPart:		pointer to the input data to be signed
2647c478bd9Sstevel@tonic-gate  *      ulPartLen:	length of the input data
2657c478bd9Sstevel@tonic-gate  *
2667c478bd9Sstevel@tonic-gate  * Description:
2677c478bd9Sstevel@tonic-gate  *      called by C_SignUpdate(). This function calls the corresponding
2687c478bd9Sstevel@tonic-gate  *	sign update routine based on the mechanism.
2697c478bd9Sstevel@tonic-gate  *
2707c478bd9Sstevel@tonic-gate  */
2717c478bd9Sstevel@tonic-gate CK_RV
soft_sign_update(soft_session_t * session_p,CK_BYTE_PTR pPart,CK_ULONG ulPartLen)2727c478bd9Sstevel@tonic-gate soft_sign_update(soft_session_t *session_p, CK_BYTE_PTR pPart,
2737c478bd9Sstevel@tonic-gate     CK_ULONG ulPartLen)
2747c478bd9Sstevel@tonic-gate {
2757c478bd9Sstevel@tonic-gate 	CK_MECHANISM_TYPE	mechanism = session_p->sign.mech.mechanism;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	switch (mechanism) {
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	case CKM_SSL3_MD5_MAC:
2807c478bd9Sstevel@tonic-gate 	case CKM_SSL3_SHA1_MAC:
2817c478bd9Sstevel@tonic-gate 	case CKM_MD5_HMAC_GENERAL:
2827c478bd9Sstevel@tonic-gate 	case CKM_MD5_HMAC:
2837c478bd9Sstevel@tonic-gate 	case CKM_SHA_1_HMAC_GENERAL:
2847c478bd9Sstevel@tonic-gate 	case CKM_SHA_1_HMAC:
285f66d273dSizick 	case CKM_SHA256_HMAC_GENERAL:
286f66d273dSizick 	case CKM_SHA256_HMAC:
287f66d273dSizick 	case CKM_SHA384_HMAC_GENERAL:
288f66d273dSizick 	case CKM_SHA384_HMAC:
289f66d273dSizick 	case CKM_SHA512_HMAC_GENERAL:
290f66d273dSizick 	case CKM_SHA512_HMAC:
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 		return (soft_hmac_sign_verify_update(session_p, pPart,
2937c478bd9Sstevel@tonic-gate 		    ulPartLen, B_TRUE));
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	case CKM_DES_MAC_GENERAL:
2967c478bd9Sstevel@tonic-gate 	case CKM_DES_MAC:
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 		return (soft_des_mac_sign_verify_update(session_p, pPart,
2997c478bd9Sstevel@tonic-gate 		    ulPartLen));
3007c478bd9Sstevel@tonic-gate 
301cd964fceSMatt Barden 	case CKM_AES_CMAC_GENERAL:
302cd964fceSMatt Barden 	case CKM_AES_CMAC:
303cd964fceSMatt Barden 
304cd964fceSMatt Barden 		return (soft_aes_mac_sign_verify_update(session_p, pPart,
305cd964fceSMatt Barden 		    ulPartLen));
306cd964fceSMatt Barden 
3077c478bd9Sstevel@tonic-gate 	case CKM_MD5_RSA_PKCS:
3087c478bd9Sstevel@tonic-gate 	case CKM_SHA1_RSA_PKCS:
309f66d273dSizick 	case CKM_SHA256_RSA_PKCS:
310f66d273dSizick 	case CKM_SHA384_RSA_PKCS:
311f66d273dSizick 	case CKM_SHA512_RSA_PKCS:
3127c478bd9Sstevel@tonic-gate 		/*
3137c478bd9Sstevel@tonic-gate 		 * The MD5/SHA1 digest value is accumulated in the context
3147c478bd9Sstevel@tonic-gate 		 * of the multiple-part digesting operation. In the final
3157c478bd9Sstevel@tonic-gate 		 * operation, the digest is encoded and then perform RSA
3167c478bd9Sstevel@tonic-gate 		 * signing.
3177c478bd9Sstevel@tonic-gate 		 */
3187c478bd9Sstevel@tonic-gate 	case CKM_DSA_SHA1:
319f9fbec18Smcpowers 	case CKM_ECDSA_SHA1:
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 		return (soft_digest_update(session_p, pPart, ulPartLen));
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	default:
3247c478bd9Sstevel@tonic-gate 		/* PKCS11: The mechanism only supports single-part operation. */
3257c478bd9Sstevel@tonic-gate 		return (CKR_MECHANISM_INVALID);
3267c478bd9Sstevel@tonic-gate 	}
3277c478bd9Sstevel@tonic-gate }
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate /*
3317c478bd9Sstevel@tonic-gate  * soft_sign_final()
3327c478bd9Sstevel@tonic-gate  *
3337c478bd9Sstevel@tonic-gate  * Arguments:
3347c478bd9Sstevel@tonic-gate  *      session_p:	pointer to soft_session_t struct
3357c478bd9Sstevel@tonic-gate  *      pSignature:	pointer to the signature after signing
3367c478bd9Sstevel@tonic-gate  *      pulSignatureLen: pointer to the	length of the signature
3377c478bd9Sstevel@tonic-gate  *
3387c478bd9Sstevel@tonic-gate  * Description:
3397c478bd9Sstevel@tonic-gate  *      called by C_SignFinal(). This function calls the corresponding
3407c478bd9Sstevel@tonic-gate  *	sign final routine based on the mechanism.
3417c478bd9Sstevel@tonic-gate  *
3427c478bd9Sstevel@tonic-gate  */
3437c478bd9Sstevel@tonic-gate CK_RV
soft_sign_final(soft_session_t * session_p,CK_BYTE_PTR pSignature,CK_ULONG_PTR pulSignatureLen)3447c478bd9Sstevel@tonic-gate soft_sign_final(soft_session_t *session_p, CK_BYTE_PTR pSignature,
3457c478bd9Sstevel@tonic-gate     CK_ULONG_PTR pulSignatureLen)
3467c478bd9Sstevel@tonic-gate {
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	CK_MECHANISM_TYPE mechanism = session_p->sign.mech.mechanism;
3497c478bd9Sstevel@tonic-gate 	CK_RV rv = CKR_OK;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	switch (mechanism) {
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	case CKM_SSL3_MD5_MAC:
3547c478bd9Sstevel@tonic-gate 	case CKM_SSL3_SHA1_MAC:
3557c478bd9Sstevel@tonic-gate 	case CKM_MD5_HMAC_GENERAL:
3567c478bd9Sstevel@tonic-gate 	case CKM_MD5_HMAC:
3577c478bd9Sstevel@tonic-gate 	case CKM_SHA_1_HMAC_GENERAL:
3587c478bd9Sstevel@tonic-gate 	case CKM_SHA_1_HMAC:
359f66d273dSizick 	case CKM_SHA256_HMAC_GENERAL:
360f66d273dSizick 	case CKM_SHA256_HMAC:
361f66d273dSizick 	case CKM_SHA384_HMAC_GENERAL:
362f66d273dSizick 	case CKM_SHA384_HMAC:
363f66d273dSizick 	case CKM_SHA512_HMAC_GENERAL:
364f66d273dSizick 	case CKM_SHA512_HMAC:
3657c478bd9Sstevel@tonic-gate 	{
366f66d273dSizick 		CK_BYTE hmac[SHA512_DIGEST_LENGTH]; /* use the maximum size */
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 		if (pSignature != NULL) {
3697c478bd9Sstevel@tonic-gate 			/* Pass local buffer to avoid overflow */
3707c478bd9Sstevel@tonic-gate 			rv = soft_hmac_sign_verify_common(session_p, NULL,
3717c478bd9Sstevel@tonic-gate 			    0, hmac, pulSignatureLen, B_TRUE);
3727c478bd9Sstevel@tonic-gate 		} else {
3737c478bd9Sstevel@tonic-gate 			/* Pass original pSignature, let callee to handle it. */
3747c478bd9Sstevel@tonic-gate 			rv = soft_hmac_sign_verify_common(session_p, NULL,
3757c478bd9Sstevel@tonic-gate 			    0, pSignature, pulSignatureLen, B_TRUE);
3767c478bd9Sstevel@tonic-gate 		}
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 		if ((rv == CKR_OK) && (pSignature != NULL))
3797c478bd9Sstevel@tonic-gate 			(void) memcpy(pSignature, hmac, *pulSignatureLen);
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 		return (rv);
3827c478bd9Sstevel@tonic-gate 	}
3837c478bd9Sstevel@tonic-gate 	case CKM_DES_MAC_GENERAL:
3847c478bd9Sstevel@tonic-gate 	case CKM_DES_MAC:
3857c478bd9Sstevel@tonic-gate 	{
3867c478bd9Sstevel@tonic-gate 		CK_BYTE signature[DES_BLOCK_LEN]; /* use the maximum size */
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 		if (pSignature != NULL) {
3897c478bd9Sstevel@tonic-gate 			/* Pass local buffer to avoid overflow. */
3907c478bd9Sstevel@tonic-gate 			rv = soft_des_sign_verify_common(session_p, NULL, 0,
391f9fbec18Smcpowers 			    signature, pulSignatureLen, B_TRUE, B_TRUE);
3927c478bd9Sstevel@tonic-gate 		} else {
3937c478bd9Sstevel@tonic-gate 			/* Pass NULL, let callee to handle it. */
3947c478bd9Sstevel@tonic-gate 			rv = soft_des_sign_verify_common(session_p, NULL, 0,
395f9fbec18Smcpowers 			    NULL, pulSignatureLen, B_TRUE, B_TRUE);
3967c478bd9Sstevel@tonic-gate 		}
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 		if ((rv == CKR_OK) && (pSignature != NULL))
3997c478bd9Sstevel@tonic-gate 			(void) memcpy(pSignature, signature, *pulSignatureLen);
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 		return (rv);
4027c478bd9Sstevel@tonic-gate 	}
403cd964fceSMatt Barden 	case CKM_AES_CMAC_GENERAL:
404cd964fceSMatt Barden 	case CKM_AES_CMAC:
405cd964fceSMatt Barden 	{
406cd964fceSMatt Barden 		CK_BYTE signature[AES_BLOCK_LEN]; /* use the maximum size */
407cd964fceSMatt Barden 
408cd964fceSMatt Barden 		if (pSignature != NULL) {
409cd964fceSMatt Barden 			/* Pass local buffer to avoid overflow. */
410cd964fceSMatt Barden 			rv = soft_aes_sign_verify_common(session_p, NULL, 0,
411cd964fceSMatt Barden 			    signature, pulSignatureLen, B_TRUE, B_TRUE);
412cd964fceSMatt Barden 		} else {
413cd964fceSMatt Barden 			/* Pass NULL, let callee handle it. */
414cd964fceSMatt Barden 			rv = soft_aes_sign_verify_common(session_p, NULL, 0,
415cd964fceSMatt Barden 			    NULL, pulSignatureLen, B_TRUE, B_TRUE);
416cd964fceSMatt Barden 		}
417cd964fceSMatt Barden 
418cd964fceSMatt Barden 		if ((rv == CKR_OK) && (pSignature != NULL))
419cd964fceSMatt Barden 			(void) memcpy(pSignature, signature, *pulSignatureLen);
420cd964fceSMatt Barden 
421cd964fceSMatt Barden 		return (rv);
422cd964fceSMatt Barden 	}
4237c478bd9Sstevel@tonic-gate 	case CKM_MD5_RSA_PKCS:
4247c478bd9Sstevel@tonic-gate 	case CKM_SHA1_RSA_PKCS:
425f66d273dSizick 	case CKM_SHA256_RSA_PKCS:
426f66d273dSizick 	case CKM_SHA384_RSA_PKCS:
427f66d273dSizick 	case CKM_SHA512_RSA_PKCS:
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 		return (soft_rsa_digest_sign_common(session_p, NULL, 0,
4307c478bd9Sstevel@tonic-gate 		    pSignature, pulSignatureLen, mechanism, B_TRUE));
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	case CKM_DSA_SHA1:
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 		return (soft_dsa_digest_sign_common(session_p, NULL, 0,
4357c478bd9Sstevel@tonic-gate 		    pSignature, pulSignatureLen, B_TRUE));
4367c478bd9Sstevel@tonic-gate 
437f9fbec18Smcpowers 	case CKM_ECDSA_SHA1:
438f9fbec18Smcpowers 
439f9fbec18Smcpowers 		return (soft_ecc_digest_sign_common(session_p, NULL, 0,
440f9fbec18Smcpowers 		    pSignature, pulSignatureLen, B_TRUE));
441f9fbec18Smcpowers 
4427c478bd9Sstevel@tonic-gate 	default:
4437c478bd9Sstevel@tonic-gate 		/* PKCS11: The mechanism only supports single-part operation. */
4447c478bd9Sstevel@tonic-gate 		return (CKR_MECHANISM_INVALID);
4457c478bd9Sstevel@tonic-gate 	}
4467c478bd9Sstevel@tonic-gate }
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate CK_RV
soft_sign_recover_init(soft_session_t * session_p,CK_MECHANISM_PTR pMechanism,soft_object_t * key_p)4507c478bd9Sstevel@tonic-gate soft_sign_recover_init(soft_session_t *session_p, CK_MECHANISM_PTR pMechanism,
4517c478bd9Sstevel@tonic-gate     soft_object_t *key_p)
4527c478bd9Sstevel@tonic-gate {
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	switch (pMechanism->mechanism) {
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	case CKM_RSA_X_509:
4577c478bd9Sstevel@tonic-gate 	case CKM_RSA_PKCS:
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 		return (soft_rsa_sign_verify_init_common(session_p, pMechanism,
4607c478bd9Sstevel@tonic-gate 		    key_p, B_TRUE));
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	default:
4637c478bd9Sstevel@tonic-gate 		return (CKR_MECHANISM_INVALID);
4647c478bd9Sstevel@tonic-gate 	}
4657c478bd9Sstevel@tonic-gate }
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate CK_RV
soft_sign_recover(soft_session_t * session_p,CK_BYTE_PTR pData,CK_ULONG ulDataLen,CK_BYTE_PTR pSignature,CK_ULONG_PTR pulSignatureLen)4697c478bd9Sstevel@tonic-gate soft_sign_recover(soft_session_t *session_p, CK_BYTE_PTR pData,
4707c478bd9Sstevel@tonic-gate     CK_ULONG ulDataLen, CK_BYTE_PTR pSignature,
4717c478bd9Sstevel@tonic-gate     CK_ULONG_PTR pulSignatureLen)
4727c478bd9Sstevel@tonic-gate {
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	CK_MECHANISM_TYPE mechanism = session_p->sign.mech.mechanism;
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	switch (mechanism) {
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	case CKM_RSA_X_509:
4797c478bd9Sstevel@tonic-gate 	case CKM_RSA_PKCS:
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 		return (soft_rsa_sign_common(session_p, pData, ulDataLen,
4827c478bd9Sstevel@tonic-gate 		    pSignature, pulSignatureLen, mechanism));
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	default:
4857c478bd9Sstevel@tonic-gate 		return (CKR_MECHANISM_INVALID);
4867c478bd9Sstevel@tonic-gate 	}
4877c478bd9Sstevel@tonic-gate }
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate /*
4907c478bd9Sstevel@tonic-gate  * This function frees the allocated active crypto context.
4917c478bd9Sstevel@tonic-gate  * It is only called by the first tier of sign/verify routines
4927c478bd9Sstevel@tonic-gate  * and the caller of this function may or may not hold the session mutex.
4937c478bd9Sstevel@tonic-gate  */
4947c478bd9Sstevel@tonic-gate void
soft_sign_verify_cleanup(soft_session_t * session_p,boolean_t sign,boolean_t lock_held)4957c478bd9Sstevel@tonic-gate soft_sign_verify_cleanup(soft_session_t *session_p, boolean_t sign,
4967c478bd9Sstevel@tonic-gate     boolean_t lock_held)
4977c478bd9Sstevel@tonic-gate {
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	crypto_active_op_t *active_op;
5007c478bd9Sstevel@tonic-gate 	boolean_t lock_true = B_TRUE;
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	if (!lock_held)
5037c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_lock(&session_p->session_mutex);
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	active_op = (sign) ? &(session_p->sign) : &(session_p->verify);
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	switch (active_op->mech.mechanism) {
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	case CKM_MD5_RSA_PKCS:
5107c478bd9Sstevel@tonic-gate 	case CKM_SHA1_RSA_PKCS:
511f66d273dSizick 	case CKM_SHA256_RSA_PKCS:
512f66d273dSizick 	case CKM_SHA384_RSA_PKCS:
513f66d273dSizick 	case CKM_SHA512_RSA_PKCS:
5147c478bd9Sstevel@tonic-gate 		if (session_p->digest.context != NULL) {
5157c478bd9Sstevel@tonic-gate 			free(session_p->digest.context);
5167c478bd9Sstevel@tonic-gate 			session_p->digest.context = NULL;
5177c478bd9Sstevel@tonic-gate 			session_p->digest.flags = 0;
5187c478bd9Sstevel@tonic-gate 		}
5194c21f043Sizick 		/* FALLTHRU */
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	case CKM_RSA_PKCS:
5227c478bd9Sstevel@tonic-gate 	case CKM_RSA_X_509:
5234c21f043Sizick 	{
5244c21f043Sizick 		soft_rsa_ctx_t *rsa_ctx =
5254c21f043Sizick 		    (soft_rsa_ctx_t *)active_op->context;
5264c21f043Sizick 
5274c21f043Sizick 		if (rsa_ctx != NULL && rsa_ctx->key != NULL) {
5284c21f043Sizick 			soft_cleanup_object(rsa_ctx->key);
5294c21f043Sizick 			free(rsa_ctx->key);
5304c21f043Sizick 		}
5314c21f043Sizick 		break;
5324c21f043Sizick 
5334c21f043Sizick 	}
5344c21f043Sizick 	case CKM_DSA_SHA1:
5354c21f043Sizick 		if (session_p->digest.context != NULL) {
5364c21f043Sizick 			free(session_p->digest.context);
5374c21f043Sizick 			session_p->digest.context = NULL;
5384c21f043Sizick 			session_p->digest.flags = 0;
5394c21f043Sizick 		}
5404c21f043Sizick 
5414c21f043Sizick 		/* FALLTHRU */
5427c478bd9Sstevel@tonic-gate 	case CKM_DSA:
5434c21f043Sizick 	{
5444c21f043Sizick 		soft_dsa_ctx_t *dsa_ctx =
5454c21f043Sizick 		    (soft_dsa_ctx_t *)active_op->context;
5464c21f043Sizick 
5474c21f043Sizick 		if (dsa_ctx != NULL && dsa_ctx->key != NULL) {
5484c21f043Sizick 			soft_cleanup_object(dsa_ctx->key);
5494c21f043Sizick 			free(dsa_ctx->key);
5504c21f043Sizick 		}
5517c478bd9Sstevel@tonic-gate 		break;
5527c478bd9Sstevel@tonic-gate 
5534c21f043Sizick 	}
5547c478bd9Sstevel@tonic-gate 	case CKM_SSL3_MD5_MAC:
5557c478bd9Sstevel@tonic-gate 	case CKM_SSL3_SHA1_MAC:
5567c478bd9Sstevel@tonic-gate 	case CKM_MD5_HMAC_GENERAL:
5577c478bd9Sstevel@tonic-gate 	case CKM_MD5_HMAC:
5587c478bd9Sstevel@tonic-gate 	case CKM_SHA_1_HMAC_GENERAL:
5597c478bd9Sstevel@tonic-gate 	case CKM_SHA_1_HMAC:
560f66d273dSizick 	case CKM_SHA256_HMAC_GENERAL:
561f66d273dSizick 	case CKM_SHA256_HMAC:
562f66d273dSizick 	case CKM_SHA384_HMAC_GENERAL:
563f66d273dSizick 	case CKM_SHA384_HMAC:
564f66d273dSizick 	case CKM_SHA512_HMAC_GENERAL:
565f66d273dSizick 	case CKM_SHA512_HMAC:
566a8793c76SJason King 		if (active_op->context != NULL) {
567a8793c76SJason King 			explicit_bzero(active_op->context,
568a8793c76SJason King 			    sizeof (soft_hmac_ctx_t));
569a8793c76SJason King 		}
5707c478bd9Sstevel@tonic-gate 		break;
5717c478bd9Sstevel@tonic-gate 	case CKM_DES_MAC_GENERAL:
5727c478bd9Sstevel@tonic-gate 	case CKM_DES_MAC:
5737c478bd9Sstevel@tonic-gate 		if (session_p->encrypt.context != NULL) {
5747c478bd9Sstevel@tonic-gate 			free(session_p->encrypt.context);
5757c478bd9Sstevel@tonic-gate 			session_p->encrypt.context = NULL;
5767c478bd9Sstevel@tonic-gate 			session_p->encrypt.flags = 0;
5777c478bd9Sstevel@tonic-gate 		}
578a8793c76SJason King 		if (active_op->context != NULL) {
579a8793c76SJason King 			explicit_bzero(active_op->context,
580a8793c76SJason King 			    sizeof (soft_des_ctx_t));
581a8793c76SJason King 		}
5827c478bd9Sstevel@tonic-gate 		break;
5837c478bd9Sstevel@tonic-gate 
584cd964fceSMatt Barden 	case CKM_AES_CMAC_GENERAL:
585cd964fceSMatt Barden 	case CKM_AES_CMAC:
586cd964fceSMatt Barden 		if (session_p->encrypt.context != NULL) {
587*fb261280SJason King 			soft_aes_free_ctx(session_p->encrypt.context);
588cd964fceSMatt Barden 			session_p->encrypt.context = NULL;
589cd964fceSMatt Barden 			session_p->encrypt.flags = 0;
590cd964fceSMatt Barden 		}
591a8793c76SJason King 		if (active_op->context != NULL) {
592a8793c76SJason King 			explicit_bzero(active_op->context,
593*fb261280SJason King 			    sizeof (soft_aes_sign_ctx_t));
594a8793c76SJason King 		}
595cd964fceSMatt Barden 		break;
596cd964fceSMatt Barden 
5977c478bd9Sstevel@tonic-gate 	}
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 	if (active_op->context != NULL) {
6007c478bd9Sstevel@tonic-gate 		free(active_op->context);
6017c478bd9Sstevel@tonic-gate 		active_op->context = NULL;
6027c478bd9Sstevel@tonic-gate 	}
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	active_op->flags = 0;
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	if (!lock_held)
6077c478bd9Sstevel@tonic-gate 		SES_REFRELE(session_p, lock_true);
6087c478bd9Sstevel@tonic-gate }
609