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