xref: /titanic_41/usr/src/lib/pkcs11/pkcs11_softtoken/common/softVerifyUtil.c (revision f9fbec18f5b458b560ecf45d3db8e8bd56bf6942)
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*f9fbec18Smcpowers  * Common Development and Distribution License (the "License").
6*f9fbec18Smcpowers  * 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*f9fbec18Smcpowers  * Copyright 2007 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 <string.h>
307c478bd9Sstevel@tonic-gate #include <strings.h>
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <security/cryptoki.h>
337c478bd9Sstevel@tonic-gate #include "softObject.h"
347c478bd9Sstevel@tonic-gate #include "softOps.h"
357c478bd9Sstevel@tonic-gate #include "softSession.h"
367c478bd9Sstevel@tonic-gate #include "softMAC.h"
377c478bd9Sstevel@tonic-gate #include "softRSA.h"
387c478bd9Sstevel@tonic-gate #include "softDSA.h"
39*f9fbec18Smcpowers #include "softEC.h"
407c478bd9Sstevel@tonic-gate #include "softCrypt.h"
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate  * soft_verify_init()
447c478bd9Sstevel@tonic-gate  *
457c478bd9Sstevel@tonic-gate  * Arguments:
467c478bd9Sstevel@tonic-gate  *	session_p:	pointer to soft_session_t struct
477c478bd9Sstevel@tonic-gate  *	pMechanism:	pointer to CK_MECHANISM struct provided by application
487c478bd9Sstevel@tonic-gate  *	key_p:		pointer to key soft_object_t struct
497c478bd9Sstevel@tonic-gate  *
507c478bd9Sstevel@tonic-gate  * Description:
517c478bd9Sstevel@tonic-gate  *	called by C_VerifyInit(). This function calls the corresponding
527c478bd9Sstevel@tonic-gate  *	verify init routine based on the mechanism.
537c478bd9Sstevel@tonic-gate  *
547c478bd9Sstevel@tonic-gate  */
557c478bd9Sstevel@tonic-gate CK_RV
soft_verify_init(soft_session_t * session_p,CK_MECHANISM_PTR pMechanism,soft_object_t * key_p)567c478bd9Sstevel@tonic-gate soft_verify_init(soft_session_t *session_p, CK_MECHANISM_PTR pMechanism,
577c478bd9Sstevel@tonic-gate     soft_object_t *key_p)
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	switch (pMechanism->mechanism) {
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	case CKM_SSL3_MD5_MAC:
637c478bd9Sstevel@tonic-gate 	case CKM_SSL3_SHA1_MAC:
647c478bd9Sstevel@tonic-gate 	case CKM_MD5_HMAC_GENERAL:
657c478bd9Sstevel@tonic-gate 	case CKM_MD5_HMAC:
667c478bd9Sstevel@tonic-gate 	case CKM_SHA_1_HMAC_GENERAL:
677c478bd9Sstevel@tonic-gate 	case CKM_SHA_1_HMAC:
68f66d273dSizick 	case CKM_SHA256_HMAC_GENERAL:
69f66d273dSizick 	case CKM_SHA256_HMAC:
70f66d273dSizick 	case CKM_SHA384_HMAC_GENERAL:
71f66d273dSizick 	case CKM_SHA384_HMAC:
72f66d273dSizick 	case CKM_SHA512_HMAC_GENERAL:
73f66d273dSizick 	case CKM_SHA512_HMAC:
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 		return (soft_hmac_sign_verify_init_common(session_p,
767c478bd9Sstevel@tonic-gate 		    pMechanism, key_p, B_FALSE));
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	case CKM_RSA_X_509:
797c478bd9Sstevel@tonic-gate 	case CKM_RSA_PKCS:
807c478bd9Sstevel@tonic-gate 	case CKM_MD5_RSA_PKCS:
817c478bd9Sstevel@tonic-gate 	case CKM_SHA1_RSA_PKCS:
82f66d273dSizick 	case CKM_SHA256_RSA_PKCS:
83f66d273dSizick 	case CKM_SHA384_RSA_PKCS:
84f66d273dSizick 	case CKM_SHA512_RSA_PKCS:
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 		return (soft_rsa_sign_verify_init_common(session_p, pMechanism,
877c478bd9Sstevel@tonic-gate 		    key_p, B_FALSE));
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	case CKM_DSA:
907c478bd9Sstevel@tonic-gate 	case CKM_DSA_SHA1:
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 		return (soft_dsa_sign_verify_init_common(session_p, pMechanism,
937c478bd9Sstevel@tonic-gate 		    key_p, B_FALSE));
947c478bd9Sstevel@tonic-gate 
95*f9fbec18Smcpowers 	case CKM_ECDSA:
96*f9fbec18Smcpowers 	case CKM_ECDSA_SHA1:
97*f9fbec18Smcpowers 
98*f9fbec18Smcpowers 		return (soft_ecc_sign_verify_init_common(session_p, pMechanism,
99*f9fbec18Smcpowers 		    key_p, B_FALSE));
100*f9fbec18Smcpowers 
1017c478bd9Sstevel@tonic-gate 	case CKM_DES_MAC_GENERAL:
1027c478bd9Sstevel@tonic-gate 	case CKM_DES_MAC:
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 		return (soft_des_sign_verify_init_common(session_p, pMechanism,
1057c478bd9Sstevel@tonic-gate 		    key_p, B_FALSE));
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	default:
1087c478bd9Sstevel@tonic-gate 		return (CKR_MECHANISM_INVALID);
1097c478bd9Sstevel@tonic-gate 	}
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /*
1157c478bd9Sstevel@tonic-gate  * soft_verify()
1167c478bd9Sstevel@tonic-gate  *
1177c478bd9Sstevel@tonic-gate  * Arguments:
1187c478bd9Sstevel@tonic-gate  *      session_p:	pointer to soft_session_t struct
1197c478bd9Sstevel@tonic-gate  *	pData:		pointer to the input data
1207c478bd9Sstevel@tonic-gate  *	ulDataLen:	length of the input data
1217c478bd9Sstevel@tonic-gate  *	pSignature:	pointer to the signature
1227c478bd9Sstevel@tonic-gate  *	ulSignatureLen:	length of the signature
1237c478bd9Sstevel@tonic-gate  *
1247c478bd9Sstevel@tonic-gate  * Description:
1257c478bd9Sstevel@tonic-gate  *      called by C_Verify(). This function calls the corresponding
1267c478bd9Sstevel@tonic-gate  *	verify routine based on the mechanism.
1277c478bd9Sstevel@tonic-gate  *
1287c478bd9Sstevel@tonic-gate  */
1297c478bd9Sstevel@tonic-gate CK_RV
soft_verify(soft_session_t * session_p,CK_BYTE_PTR pData,CK_ULONG ulDataLen,CK_BYTE_PTR pSignature,CK_ULONG ulSignatureLen)1307c478bd9Sstevel@tonic-gate soft_verify(soft_session_t *session_p, CK_BYTE_PTR pData,
1317c478bd9Sstevel@tonic-gate     CK_ULONG ulDataLen, CK_BYTE_PTR pSignature,
1327c478bd9Sstevel@tonic-gate     CK_ULONG ulSignatureLen)
1337c478bd9Sstevel@tonic-gate {
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	CK_MECHANISM_TYPE mechanism = session_p->verify.mech.mechanism;
1367c478bd9Sstevel@tonic-gate 	CK_RV rv = CKR_OK;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	switch (mechanism) {
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	case CKM_SSL3_MD5_MAC:
1417c478bd9Sstevel@tonic-gate 	case CKM_SSL3_SHA1_MAC:
1427c478bd9Sstevel@tonic-gate 	case CKM_MD5_HMAC_GENERAL:
1437c478bd9Sstevel@tonic-gate 	case CKM_MD5_HMAC:
1447c478bd9Sstevel@tonic-gate 	case CKM_SHA_1_HMAC_GENERAL:
1457c478bd9Sstevel@tonic-gate 	case CKM_SHA_1_HMAC:
146f66d273dSizick 	case CKM_SHA256_HMAC_GENERAL:
147f66d273dSizick 	case CKM_SHA256_HMAC:
148f66d273dSizick 	case CKM_SHA384_HMAC_GENERAL:
149f66d273dSizick 	case CKM_SHA384_HMAC:
150f66d273dSizick 	case CKM_SHA512_HMAC_GENERAL:
151f66d273dSizick 	case CKM_SHA512_HMAC:
1527c478bd9Sstevel@tonic-gate 	{
1537c478bd9Sstevel@tonic-gate 		CK_ULONG len;
154f66d273dSizick 		CK_BYTE hmac[SHA512_DIGEST_LENGTH]; /* use the maximum size */
1557c478bd9Sstevel@tonic-gate 		soft_hmac_ctx_t *hmac_ctx;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 		hmac_ctx = (soft_hmac_ctx_t *)session_p->verify.context;
1587c478bd9Sstevel@tonic-gate 		len = hmac_ctx->hmac_len;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 		rv = soft_hmac_sign_verify_common(session_p, pData,
1617c478bd9Sstevel@tonic-gate 		    ulDataLen, hmac, &len, B_FALSE);
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 		if (rv == CKR_OK) {
1647c478bd9Sstevel@tonic-gate 			if (len != ulSignatureLen) {
1657c478bd9Sstevel@tonic-gate 				rv = CKR_SIGNATURE_LEN_RANGE;
1667c478bd9Sstevel@tonic-gate 			}
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 			if (memcmp(hmac, pSignature, len) != 0) {
1697c478bd9Sstevel@tonic-gate 				rv = CKR_SIGNATURE_INVALID;
1707c478bd9Sstevel@tonic-gate 			}
1717c478bd9Sstevel@tonic-gate 		}
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_ULONG len;
1797c478bd9Sstevel@tonic-gate 		CK_BYTE signature[DES_BLOCK_LEN]; /* use the maximum size */
1807c478bd9Sstevel@tonic-gate 		soft_des_ctx_t *des_ctx;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 		des_ctx = (soft_des_ctx_t *)session_p->verify.context;
1837c478bd9Sstevel@tonic-gate 		len = des_ctx->mac_len;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 		/* Pass local buffer to avoid overflow. */
1867c478bd9Sstevel@tonic-gate 		rv = soft_des_sign_verify_common(session_p, pData,
1877c478bd9Sstevel@tonic-gate 		    ulDataLen, signature, &len, B_FALSE, B_FALSE);
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 		if (rv == CKR_OK) {
1907c478bd9Sstevel@tonic-gate 			if (len != ulSignatureLen) {
1917c478bd9Sstevel@tonic-gate 				rv = CKR_SIGNATURE_LEN_RANGE;
1927c478bd9Sstevel@tonic-gate 			}
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 			if (memcmp(signature, pSignature, len) != 0) {
1957c478bd9Sstevel@tonic-gate 				rv = CKR_SIGNATURE_INVALID;
1967c478bd9Sstevel@tonic-gate 			}
1977c478bd9Sstevel@tonic-gate 		}
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 		return (rv);
2007c478bd9Sstevel@tonic-gate 	}
2017c478bd9Sstevel@tonic-gate 	case CKM_RSA_X_509:
2027c478bd9Sstevel@tonic-gate 	case CKM_RSA_PKCS:
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 		return (soft_rsa_verify_common(session_p, pData, ulDataLen,
2057c478bd9Sstevel@tonic-gate 		    pSignature, ulSignatureLen, mechanism));
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	case CKM_MD5_RSA_PKCS:
2087c478bd9Sstevel@tonic-gate 	case CKM_SHA1_RSA_PKCS:
209f66d273dSizick 	case CKM_SHA256_RSA_PKCS:
210f66d273dSizick 	case CKM_SHA384_RSA_PKCS:
211f66d273dSizick 	case CKM_SHA512_RSA_PKCS:
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 		return (soft_rsa_digest_verify_common(session_p, pData,
2147c478bd9Sstevel@tonic-gate 		    ulDataLen, pSignature, ulSignatureLen, mechanism, B_FALSE));
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	case CKM_DSA:
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 		return (soft_dsa_verify(session_p, pData, ulDataLen,
2197c478bd9Sstevel@tonic-gate 		    pSignature, ulSignatureLen));
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	case CKM_DSA_SHA1:
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 		return (soft_dsa_digest_verify_common(session_p, pData,
2247c478bd9Sstevel@tonic-gate 		    ulDataLen, pSignature, ulSignatureLen, B_FALSE));
2257c478bd9Sstevel@tonic-gate 
226*f9fbec18Smcpowers 	case CKM_ECDSA:
227*f9fbec18Smcpowers 
228*f9fbec18Smcpowers 		return (soft_ecc_verify(session_p, pData, ulDataLen,
229*f9fbec18Smcpowers 		    pSignature, ulSignatureLen));
230*f9fbec18Smcpowers 
231*f9fbec18Smcpowers 	case CKM_ECDSA_SHA1:
232*f9fbec18Smcpowers 
233*f9fbec18Smcpowers 		return (soft_ecc_digest_verify_common(session_p, pData,
234*f9fbec18Smcpowers 		    ulDataLen, pSignature, ulSignatureLen, B_FALSE));
235*f9fbec18Smcpowers 
2367c478bd9Sstevel@tonic-gate 	default:
2377c478bd9Sstevel@tonic-gate 		return (CKR_MECHANISM_INVALID);
2387c478bd9Sstevel@tonic-gate 	}
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate /*
2437c478bd9Sstevel@tonic-gate  * soft_verify_update()
2447c478bd9Sstevel@tonic-gate  *
2457c478bd9Sstevel@tonic-gate  * Arguments:
2467c478bd9Sstevel@tonic-gate  *      session_p:	pointer to soft_session_t struct
2477c478bd9Sstevel@tonic-gate  *      pPart:		pointer to the input data
2487c478bd9Sstevel@tonic-gate  *      ulPartLen:	length of the input data
2497c478bd9Sstevel@tonic-gate  *
2507c478bd9Sstevel@tonic-gate  * Description:
2517c478bd9Sstevel@tonic-gate  *      called by C_VerifyUpdate(). This function calls the corresponding
2527c478bd9Sstevel@tonic-gate  *	verify update routine based on the mechanism.
2537c478bd9Sstevel@tonic-gate  *
2547c478bd9Sstevel@tonic-gate  */
2557c478bd9Sstevel@tonic-gate CK_RV
soft_verify_update(soft_session_t * session_p,CK_BYTE_PTR pPart,CK_ULONG ulPartLen)2567c478bd9Sstevel@tonic-gate soft_verify_update(soft_session_t *session_p, CK_BYTE_PTR pPart,
2577c478bd9Sstevel@tonic-gate     CK_ULONG ulPartLen)
2587c478bd9Sstevel@tonic-gate {
2597c478bd9Sstevel@tonic-gate 	CK_MECHANISM_TYPE	mechanism = session_p->verify.mech.mechanism;
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	switch (mechanism) {
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	case CKM_SSL3_MD5_MAC:
2647c478bd9Sstevel@tonic-gate 	case CKM_SSL3_SHA1_MAC:
2657c478bd9Sstevel@tonic-gate 	case CKM_MD5_HMAC_GENERAL:
2667c478bd9Sstevel@tonic-gate 	case CKM_MD5_HMAC:
2677c478bd9Sstevel@tonic-gate 	case CKM_SHA_1_HMAC_GENERAL:
2687c478bd9Sstevel@tonic-gate 	case CKM_SHA_1_HMAC:
269f66d273dSizick 	case CKM_SHA256_HMAC_GENERAL:
270f66d273dSizick 	case CKM_SHA256_HMAC:
271f66d273dSizick 	case CKM_SHA384_HMAC_GENERAL:
272f66d273dSizick 	case CKM_SHA384_HMAC:
273f66d273dSizick 	case CKM_SHA512_HMAC_GENERAL:
274f66d273dSizick 	case CKM_SHA512_HMAC:
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 		return (soft_hmac_sign_verify_update(session_p, pPart,
2777c478bd9Sstevel@tonic-gate 		    ulPartLen, B_FALSE));
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	case CKM_DES_MAC_GENERAL:
2807c478bd9Sstevel@tonic-gate 	case CKM_DES_MAC:
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 		return (soft_des_mac_sign_verify_update(session_p, pPart,
2837c478bd9Sstevel@tonic-gate 		    ulPartLen));
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	case CKM_MD5_RSA_PKCS:
2867c478bd9Sstevel@tonic-gate 	case CKM_SHA1_RSA_PKCS:
287f66d273dSizick 	case CKM_SHA256_RSA_PKCS:
288f66d273dSizick 	case CKM_SHA384_RSA_PKCS:
289f66d273dSizick 	case CKM_SHA512_RSA_PKCS:
2907c478bd9Sstevel@tonic-gate 		/*
2917c478bd9Sstevel@tonic-gate 		 * The MD5/SHA1 digest value is accumulated in the context
2927c478bd9Sstevel@tonic-gate 		 * of the multiple-part digesting operation. In the final
2937c478bd9Sstevel@tonic-gate 		 * operation, the digest is encoded and then perform RSA
2947c478bd9Sstevel@tonic-gate 		 * verification.
2957c478bd9Sstevel@tonic-gate 		 */
2967c478bd9Sstevel@tonic-gate 	case CKM_DSA_SHA1:
297*f9fbec18Smcpowers 	case CKM_ECDSA_SHA1:
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 		return (soft_digest_update(session_p, pPart, ulPartLen));
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	default:
3027c478bd9Sstevel@tonic-gate 		/* PKCS11: The mechanism only supports single-part operation. */
3037c478bd9Sstevel@tonic-gate 		return (CKR_MECHANISM_INVALID);
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate /*
3097c478bd9Sstevel@tonic-gate  * soft_verify_final()
3107c478bd9Sstevel@tonic-gate  *
3117c478bd9Sstevel@tonic-gate  * Arguments:
3127c478bd9Sstevel@tonic-gate  *      session_p:	pointer to soft_session_t struct
3137c478bd9Sstevel@tonic-gate  *      pSignature:	pointer to the signature
3147c478bd9Sstevel@tonic-gate  *      ulSignatureLen:	length of the signature
3157c478bd9Sstevel@tonic-gate  *
3167c478bd9Sstevel@tonic-gate  * Description:
3177c478bd9Sstevel@tonic-gate  *      called by C_VerifyFinal().  This function calls the corresponding
3187c478bd9Sstevel@tonic-gate  *	verify final routine based on the mechanism.
3197c478bd9Sstevel@tonic-gate  *
3207c478bd9Sstevel@tonic-gate  */
3217c478bd9Sstevel@tonic-gate CK_RV
soft_verify_final(soft_session_t * session_p,CK_BYTE_PTR pSignature,CK_ULONG ulSignatureLen)3227c478bd9Sstevel@tonic-gate soft_verify_final(soft_session_t *session_p, CK_BYTE_PTR pSignature,
3237c478bd9Sstevel@tonic-gate     CK_ULONG ulSignatureLen)
3247c478bd9Sstevel@tonic-gate {
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	CK_MECHANISM_TYPE mechanism = session_p->verify.mech.mechanism;
3277c478bd9Sstevel@tonic-gate 	CK_RV rv = CKR_OK;
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	switch (mechanism) {
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	case CKM_SSL3_MD5_MAC:
3327c478bd9Sstevel@tonic-gate 	case CKM_SSL3_SHA1_MAC:
3337c478bd9Sstevel@tonic-gate 	case CKM_MD5_HMAC_GENERAL:
3347c478bd9Sstevel@tonic-gate 	case CKM_MD5_HMAC:
3357c478bd9Sstevel@tonic-gate 	case CKM_SHA_1_HMAC_GENERAL:
3367c478bd9Sstevel@tonic-gate 	case CKM_SHA_1_HMAC:
337f66d273dSizick 	case CKM_SHA256_HMAC_GENERAL:
338f66d273dSizick 	case CKM_SHA256_HMAC:
339f66d273dSizick 	case CKM_SHA384_HMAC_GENERAL:
340f66d273dSizick 	case CKM_SHA384_HMAC:
341f66d273dSizick 	case CKM_SHA512_HMAC_GENERAL:
342f66d273dSizick 	case CKM_SHA512_HMAC:
3437c478bd9Sstevel@tonic-gate 	{
3447c478bd9Sstevel@tonic-gate 		CK_ULONG len;
345f66d273dSizick 		CK_BYTE hmac[SHA512_DIGEST_LENGTH];
3467c478bd9Sstevel@tonic-gate 		soft_hmac_ctx_t *hmac_ctx;
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 		hmac_ctx = (soft_hmac_ctx_t *)session_p->verify.context;
3497c478bd9Sstevel@tonic-gate 		len = hmac_ctx->hmac_len;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 		rv = soft_hmac_sign_verify_common(session_p, NULL, 0,
3527c478bd9Sstevel@tonic-gate 		    hmac, &len, B_FALSE);
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 		if (rv == CKR_OK) {
3557c478bd9Sstevel@tonic-gate 			if (len != ulSignatureLen) {
3567c478bd9Sstevel@tonic-gate 				rv = CKR_SIGNATURE_LEN_RANGE;
3577c478bd9Sstevel@tonic-gate 			}
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 			if (memcmp(hmac, pSignature, len) != 0) {
3607c478bd9Sstevel@tonic-gate 				rv = CKR_SIGNATURE_INVALID;
3617c478bd9Sstevel@tonic-gate 			}
3627c478bd9Sstevel@tonic-gate 		}
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 		return (rv);
3657c478bd9Sstevel@tonic-gate 	}
3667c478bd9Sstevel@tonic-gate 	case CKM_DES_MAC_GENERAL:
3677c478bd9Sstevel@tonic-gate 	case CKM_DES_MAC:
3687c478bd9Sstevel@tonic-gate 	{
3697c478bd9Sstevel@tonic-gate 		CK_ULONG len;
3707c478bd9Sstevel@tonic-gate 		CK_BYTE signature[DES_BLOCK_LEN]; /* use the maximum size */
3717c478bd9Sstevel@tonic-gate 		soft_des_ctx_t *des_ctx;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 		des_ctx = (soft_des_ctx_t *)session_p->verify.context;
3747c478bd9Sstevel@tonic-gate 		len = des_ctx->mac_len;
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 		/* Pass local buffer to avoid overflow. */
3777c478bd9Sstevel@tonic-gate 		rv = soft_des_sign_verify_common(session_p, NULL, 0,
3787c478bd9Sstevel@tonic-gate 		    signature, &len, B_FALSE, B_TRUE);
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 		if (rv == CKR_OK) {
3817c478bd9Sstevel@tonic-gate 			if (len != ulSignatureLen) {
3827c478bd9Sstevel@tonic-gate 				rv = CKR_SIGNATURE_LEN_RANGE;
3837c478bd9Sstevel@tonic-gate 			}
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 			if (memcmp(signature, pSignature, len) != 0) {
3867c478bd9Sstevel@tonic-gate 				rv = CKR_SIGNATURE_INVALID;
3877c478bd9Sstevel@tonic-gate 			}
3887c478bd9Sstevel@tonic-gate 		}
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 		return (rv);
3917c478bd9Sstevel@tonic-gate 	}
3927c478bd9Sstevel@tonic-gate 	case CKM_MD5_RSA_PKCS:
3937c478bd9Sstevel@tonic-gate 	case CKM_SHA1_RSA_PKCS:
394f66d273dSizick 	case CKM_SHA256_RSA_PKCS:
395f66d273dSizick 	case CKM_SHA384_RSA_PKCS:
396f66d273dSizick 	case CKM_SHA512_RSA_PKCS:
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 		return (soft_rsa_digest_verify_common(session_p, NULL, 0,
3997c478bd9Sstevel@tonic-gate 		    pSignature, ulSignatureLen, mechanism, B_TRUE));
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	case CKM_DSA_SHA1:
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 		return (soft_dsa_digest_verify_common(session_p, NULL, 0,
4047c478bd9Sstevel@tonic-gate 		    pSignature, ulSignatureLen, B_TRUE));
4057c478bd9Sstevel@tonic-gate 
406*f9fbec18Smcpowers 	case CKM_ECDSA_SHA1:
407*f9fbec18Smcpowers 
408*f9fbec18Smcpowers 		return (soft_ecc_digest_verify_common(session_p, NULL, 0,
409*f9fbec18Smcpowers 		    pSignature, ulSignatureLen, B_TRUE));
410*f9fbec18Smcpowers 
4117c478bd9Sstevel@tonic-gate 	default:
4127c478bd9Sstevel@tonic-gate 		/* PKCS11: The mechanism only supports single-part operation. */
4137c478bd9Sstevel@tonic-gate 		return (CKR_MECHANISM_INVALID);
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	}
4167c478bd9Sstevel@tonic-gate }
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate CK_RV
soft_verify_recover_init(soft_session_t * session_p,CK_MECHANISM_PTR pMechanism,soft_object_t * key_p)4207c478bd9Sstevel@tonic-gate soft_verify_recover_init(soft_session_t *session_p, CK_MECHANISM_PTR pMechanism,
4217c478bd9Sstevel@tonic-gate     soft_object_t *key_p)
4227c478bd9Sstevel@tonic-gate {
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	switch (pMechanism->mechanism) {
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	case CKM_RSA_X_509:
4277c478bd9Sstevel@tonic-gate 	case CKM_RSA_PKCS:
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 		return (soft_rsa_sign_verify_init_common(session_p, pMechanism,
4307c478bd9Sstevel@tonic-gate 		    key_p, B_FALSE));
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	default:
4337c478bd9Sstevel@tonic-gate 		return (CKR_MECHANISM_INVALID);
4347c478bd9Sstevel@tonic-gate 	}
4357c478bd9Sstevel@tonic-gate }
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate CK_RV
soft_verify_recover(soft_session_t * session_p,CK_BYTE_PTR pSignature,CK_ULONG ulSignatureLen,CK_BYTE_PTR pData,CK_ULONG_PTR pulDataLen)4397c478bd9Sstevel@tonic-gate soft_verify_recover(soft_session_t *session_p, CK_BYTE_PTR pSignature,
4407c478bd9Sstevel@tonic-gate     CK_ULONG ulSignatureLen, CK_BYTE_PTR pData, CK_ULONG_PTR pulDataLen)
4417c478bd9Sstevel@tonic-gate {
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	CK_MECHANISM_TYPE mechanism = session_p->verify.mech.mechanism;
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	switch (mechanism) {
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	case CKM_RSA_X_509:
4487c478bd9Sstevel@tonic-gate 	case CKM_RSA_PKCS:
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 		return (soft_rsa_verify_recover(session_p, pSignature,
4517c478bd9Sstevel@tonic-gate 		    ulSignatureLen, pData, pulDataLen));
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	default:
4547c478bd9Sstevel@tonic-gate 		return (CKR_MECHANISM_INVALID);
4557c478bd9Sstevel@tonic-gate 	}
4567c478bd9Sstevel@tonic-gate }
457