xref: /titanic_41/usr/src/lib/pkcs11/libpkcs11/common/metaDigest.c (revision c40d696f8f0e05103b3795dd37198e00ae7ef955)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * Message Digesting Functions
30  * (as defined in PKCS#11 spec section 11.10)
31  */
32 
33 #include "metaGlobal.h"
34 
35 
36 /*
37  * meta_DigestInit
38  *
39  */
40 CK_RV
41 meta_DigestInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism)
42 {
43 	CK_RV rv;
44 	meta_session_t *session;
45 
46 	if (pMechanism == NULL)
47 		return (CKR_ARGUMENTS_BAD);
48 
49 	rv = meta_handle2session(hSession, &session);
50 	if (rv != CKR_OK)
51 		return (rv);
52 
53 	rv = meta_operation_init_defer(CKF_DIGEST, session, pMechanism, NULL);
54 
55 	REFRELEASE(session);
56 
57 	return (rv);
58 }
59 
60 
61 /*
62  * meta_Digest
63  *
64  */
65 CK_RV
66 meta_Digest(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, CK_ULONG ulDataLen,
67     CK_BYTE_PTR pDigest, CK_ULONG_PTR pulDigestLen)
68 {
69 	CK_RV rv;
70 	meta_session_t *session;
71 
72 
73 	if (pData == NULL || pulDigestLen == NULL)
74 		return (CKR_ARGUMENTS_BAD);
75 
76 	rv = meta_handle2session(hSession, &session);
77 	if (rv != CKR_OK)
78 		return (rv);
79 
80 	rv = meta_do_operation(CKF_DIGEST, MODE_SINGLE, session, NULL,
81 	    pData, ulDataLen, pDigest, pulDigestLen);
82 
83 	REFRELEASE(session);
84 
85 	return (rv);
86 }
87 
88 
89 /*
90  * meta_DigestUpdate
91  *
92  */
93 CK_RV
94 meta_DigestUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart,
95     CK_ULONG ulPartLen)
96 {
97 	CK_RV rv;
98 	meta_session_t *session;
99 
100 
101 	if (pPart == NULL)
102 		return (CKR_ARGUMENTS_BAD);
103 
104 	rv = meta_handle2session(hSession, &session);
105 	if (rv != CKR_OK)
106 		return (rv);
107 
108 	rv = meta_do_operation(CKF_DIGEST, MODE_UPDATE, session, NULL,
109 	    pPart, ulPartLen, NULL, NULL);
110 
111 	REFRELEASE(session);
112 
113 	return (rv);
114 }
115 
116 
117 /*
118  * meta_DigestKey
119  *
120  * NOTE: This function can fail under certain circumstances!
121  * Unlike the other crypto functions, we didn't get the key object
122  * when the operation was initialized with C_DigestInit().
123  * Thus, the slot we're using for the digest operation may
124  * not be the slot containing the key -- if the key is extractible we can
125  * deal with it, but if it's not the operation will FAIL.
126  */
127 CK_RV
128 meta_DigestKey(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hKey)
129 {
130 	CK_RV rv;
131 	meta_session_t *session;
132 	meta_object_t *key;
133 
134 	rv = meta_handle2session(hSession, &session);
135 	if (rv != CKR_OK)
136 		return (rv);
137 
138 	rv = meta_handle2object(hKey, &key);
139 	if (rv != CKR_OK) {
140 		REFRELEASE(session);
141 		return (rv);
142 	}
143 
144 	/* meta_do_operation() will clone the key, if needed. */
145 	rv = meta_do_operation(CKF_DIGEST, MODE_UPDATE_WITHKEY, session, key,
146 	    NULL, 0, NULL, NULL);
147 
148 	OBJRELEASE(key);
149 	REFRELEASE(session);
150 
151 	return (rv);
152 }
153 
154 
155 /*
156  * meta_DigestFinal
157  *
158  */
159 CK_RV
160 meta_DigestFinal(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pDigest,
161     CK_ULONG_PTR pulDigestLen)
162 {
163 	CK_RV rv;
164 	meta_session_t *session;
165 
166 	if (pulDigestLen == NULL)
167 		return (CKR_ARGUMENTS_BAD);
168 
169 	rv = meta_handle2session(hSession, &session);
170 	if (rv != CKR_OK)
171 		return (rv);
172 
173 	rv = meta_do_operation(CKF_DIGEST, MODE_FINAL, session, NULL,
174 	    NULL, 0, pDigest, pulDigestLen);
175 
176 	REFRELEASE(session);
177 
178 	return (rv);
179 }
180