xref: /onnv-gate/usr/src/lib/pkcs11/libpkcs11/common/metaDigest.c (revision 4841:72ccafc74ba8)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
54338Sdinak  * Common Development and Distribution License (the "License").
64338Sdinak  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
224338Sdinak  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * Message Digesting Functions
300Sstevel@tonic-gate  * (as defined in PKCS#11 spec section 11.10)
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include "metaGlobal.h"
340Sstevel@tonic-gate 
350Sstevel@tonic-gate 
360Sstevel@tonic-gate /*
370Sstevel@tonic-gate  * meta_DigestInit
380Sstevel@tonic-gate  *
390Sstevel@tonic-gate  */
400Sstevel@tonic-gate CK_RV
meta_DigestInit(CK_SESSION_HANDLE hSession,CK_MECHANISM_PTR pMechanism)410Sstevel@tonic-gate meta_DigestInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism)
420Sstevel@tonic-gate {
430Sstevel@tonic-gate 	CK_RV rv;
440Sstevel@tonic-gate 	meta_session_t *session;
450Sstevel@tonic-gate 
460Sstevel@tonic-gate 	if (pMechanism == NULL)
470Sstevel@tonic-gate 		return (CKR_ARGUMENTS_BAD);
480Sstevel@tonic-gate 
490Sstevel@tonic-gate 	rv = meta_handle2session(hSession, &session);
500Sstevel@tonic-gate 	if (rv != CKR_OK)
510Sstevel@tonic-gate 		return (rv);
520Sstevel@tonic-gate 
53*4841Shaimay 	rv = meta_operation_init_defer(CKF_DIGEST, session, pMechanism, NULL);
540Sstevel@tonic-gate 
550Sstevel@tonic-gate 	REFRELEASE(session);
560Sstevel@tonic-gate 
570Sstevel@tonic-gate 	return (rv);
580Sstevel@tonic-gate }
590Sstevel@tonic-gate 
600Sstevel@tonic-gate 
610Sstevel@tonic-gate /*
620Sstevel@tonic-gate  * meta_Digest
630Sstevel@tonic-gate  *
640Sstevel@tonic-gate  */
650Sstevel@tonic-gate CK_RV
meta_Digest(CK_SESSION_HANDLE hSession,CK_BYTE_PTR pData,CK_ULONG ulDataLen,CK_BYTE_PTR pDigest,CK_ULONG_PTR pulDigestLen)660Sstevel@tonic-gate meta_Digest(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, CK_ULONG ulDataLen,
670Sstevel@tonic-gate     CK_BYTE_PTR pDigest, CK_ULONG_PTR pulDigestLen)
680Sstevel@tonic-gate {
690Sstevel@tonic-gate 	CK_RV rv;
700Sstevel@tonic-gate 	meta_session_t *session;
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	if (pData == NULL || pulDigestLen == NULL)
740Sstevel@tonic-gate 		return (CKR_ARGUMENTS_BAD);
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	rv = meta_handle2session(hSession, &session);
770Sstevel@tonic-gate 	if (rv != CKR_OK)
780Sstevel@tonic-gate 		return (rv);
790Sstevel@tonic-gate 
804338Sdinak 	rv = meta_do_operation(CKF_DIGEST, MODE_SINGLE, session, NULL,
814338Sdinak 	    pData, ulDataLen, pDigest, pulDigestLen);
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 	REFRELEASE(session);
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	return (rv);
860Sstevel@tonic-gate }
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 
890Sstevel@tonic-gate /*
900Sstevel@tonic-gate  * meta_DigestUpdate
910Sstevel@tonic-gate  *
920Sstevel@tonic-gate  */
930Sstevel@tonic-gate CK_RV
meta_DigestUpdate(CK_SESSION_HANDLE hSession,CK_BYTE_PTR pPart,CK_ULONG ulPartLen)940Sstevel@tonic-gate meta_DigestUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart,
950Sstevel@tonic-gate     CK_ULONG ulPartLen)
960Sstevel@tonic-gate {
970Sstevel@tonic-gate 	CK_RV rv;
980Sstevel@tonic-gate 	meta_session_t *session;
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	if (pPart == NULL)
1020Sstevel@tonic-gate 		return (CKR_ARGUMENTS_BAD);
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	rv = meta_handle2session(hSession, &session);
1050Sstevel@tonic-gate 	if (rv != CKR_OK)
1060Sstevel@tonic-gate 		return (rv);
1070Sstevel@tonic-gate 
1084338Sdinak 	rv = meta_do_operation(CKF_DIGEST, MODE_UPDATE, session, NULL,
1094338Sdinak 	    pPart, ulPartLen, NULL, NULL);
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	REFRELEASE(session);
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	return (rv);
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate /*
1180Sstevel@tonic-gate  * meta_DigestKey
1190Sstevel@tonic-gate  *
1200Sstevel@tonic-gate  * NOTE: This function can fail under certain circumstances!
1210Sstevel@tonic-gate  * Unlike the other crypto functions, we didn't get the key object
1220Sstevel@tonic-gate  * when the operation was initialized with C_DigestInit().
1230Sstevel@tonic-gate  * Thus, the slot we're using for the digest operation may
1240Sstevel@tonic-gate  * not be the slot containing the key -- if the key is extractible we can
1250Sstevel@tonic-gate  * deal with it, but if it's not the operation will FAIL.
1260Sstevel@tonic-gate  */
1270Sstevel@tonic-gate CK_RV
meta_DigestKey(CK_SESSION_HANDLE hSession,CK_OBJECT_HANDLE hKey)1280Sstevel@tonic-gate meta_DigestKey(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hKey)
1290Sstevel@tonic-gate {
1300Sstevel@tonic-gate 	CK_RV rv;
1310Sstevel@tonic-gate 	meta_session_t *session;
1320Sstevel@tonic-gate 	meta_object_t *key;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	rv = meta_handle2session(hSession, &session);
1350Sstevel@tonic-gate 	if (rv != CKR_OK)
1360Sstevel@tonic-gate 		return (rv);
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	rv = meta_handle2object(hKey, &key);
1390Sstevel@tonic-gate 	if (rv != CKR_OK) {
1400Sstevel@tonic-gate 		REFRELEASE(session);
1410Sstevel@tonic-gate 		return (rv);
1420Sstevel@tonic-gate 	}
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	/* meta_do_operation() will clone the key, if needed. */
1454338Sdinak 	rv = meta_do_operation(CKF_DIGEST, MODE_UPDATE_WITHKEY, session, key,
1460Sstevel@tonic-gate 	    NULL, 0, NULL, NULL);
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	OBJRELEASE(key);
1490Sstevel@tonic-gate 	REFRELEASE(session);
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	return (rv);
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate /*
1560Sstevel@tonic-gate  * meta_DigestFinal
1570Sstevel@tonic-gate  *
1580Sstevel@tonic-gate  */
1590Sstevel@tonic-gate CK_RV
meta_DigestFinal(CK_SESSION_HANDLE hSession,CK_BYTE_PTR pDigest,CK_ULONG_PTR pulDigestLen)1600Sstevel@tonic-gate meta_DigestFinal(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pDigest,
1610Sstevel@tonic-gate     CK_ULONG_PTR pulDigestLen)
1620Sstevel@tonic-gate {
1630Sstevel@tonic-gate 	CK_RV rv;
1640Sstevel@tonic-gate 	meta_session_t *session;
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	if (pulDigestLen == NULL)
1670Sstevel@tonic-gate 		return (CKR_ARGUMENTS_BAD);
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	rv = meta_handle2session(hSession, &session);
1700Sstevel@tonic-gate 	if (rv != CKR_OK)
1710Sstevel@tonic-gate 		return (rv);
1720Sstevel@tonic-gate 
1734338Sdinak 	rv = meta_do_operation(CKF_DIGEST, MODE_FINAL, session, NULL,
1744338Sdinak 	    NULL, 0, pDigest, pulDigestLen);
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	REFRELEASE(session);
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	return (rv);
1790Sstevel@tonic-gate }
180