xref: /titanic_52/usr/src/lib/pkcs11/libpkcs11/common/metaCrypt.c (revision b6c3f7863936abeae522e48a13887dddeb691a45)
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  * Encryption and Decryption Functions
30  * (as defined in PKCS#11 spec sections 11.8 and 11.9)
31  */
32 
33 #include "metaGlobal.h"
34 
35 
36 /*
37  * meta_EncryptInit
38  *
39  */
40 CK_RV
41 meta_EncryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
42     CK_OBJECT_HANDLE hKey)
43 {
44 	CK_RV rv;
45 	meta_session_t *session;
46 	meta_object_t *key;
47 
48 	if (pMechanism == NULL)
49 		return (CKR_ARGUMENTS_BAD);
50 
51 	rv = meta_handle2session(hSession, &session);
52 	if (rv != CKR_OK)
53 		return (rv);
54 
55 	rv = meta_handle2object(hKey, &key);
56 	if (rv != CKR_OK) {
57 		REFRELEASE(session);
58 		return (rv);
59 	}
60 
61 	rv = meta_operation_init_defer(CKF_ENCRYPT, session, pMechanism, key);
62 
63 	OBJRELEASE(key);
64 	REFRELEASE(session);
65 
66 	return (rv);
67 }
68 
69 
70 /*
71  * meta_Encrypt
72  *
73  */
74 CK_RV
75 meta_Encrypt(CK_SESSION_HANDLE hSession,
76     CK_BYTE_PTR pData, CK_ULONG ulDataLen,
77     CK_BYTE_PTR pEncryptedData, CK_ULONG_PTR pulEncryptedDataLen)
78 {
79 	CK_RV rv;
80 	meta_session_t *session;
81 
82 	rv = meta_handle2session(hSession, &session);
83 	if (rv != CKR_OK)
84 		return (rv);
85 
86 	if (pData == NULL || pulEncryptedDataLen == NULL) {
87 		meta_operation_cleanup(session, CKF_ENCRYPT, FALSE);
88 		REFRELEASE(session);
89 		return (CKR_ARGUMENTS_BAD);
90 	}
91 
92 	rv = meta_do_operation(CKF_ENCRYPT, MODE_SINGLE, session, NULL,
93 	    pData, ulDataLen, pEncryptedData, pulEncryptedDataLen);
94 
95 	REFRELEASE(session);
96 
97 	return (rv);
98 }
99 
100 
101 /*
102  * meta_EncryptUpdate
103  *
104  */
105 CK_RV
106 meta_EncryptUpdate(CK_SESSION_HANDLE hSession,
107     CK_BYTE_PTR pPart, CK_ULONG ulPartLen,
108     CK_BYTE_PTR pEncryptedPart, CK_ULONG_PTR pulEncryptedPartLen)
109 {
110 	CK_RV rv;
111 	meta_session_t *session;
112 
113 	rv = meta_handle2session(hSession, &session);
114 	if (rv != CKR_OK)
115 		return (rv);
116 
117 	if (pPart == NULL || pulEncryptedPartLen == NULL) {
118 		meta_operation_cleanup(session, CKF_ENCRYPT, FALSE);
119 		REFRELEASE(session);
120 		return (CKR_ARGUMENTS_BAD);
121 	}
122 
123 	rv = meta_do_operation(CKF_ENCRYPT, MODE_UPDATE, session, NULL,
124 	    pPart, ulPartLen, pEncryptedPart, pulEncryptedPartLen);
125 
126 	REFRELEASE(session);
127 
128 	return (rv);
129 }
130 
131 
132 /*
133  * meta_EncryptFinal
134  *
135  */
136 CK_RV
137 meta_EncryptFinal(CK_SESSION_HANDLE hSession,
138     CK_BYTE_PTR pLastEncryptedPart, CK_ULONG_PTR pulLastEncryptedPartLen)
139 {
140 	CK_RV rv;
141 	meta_session_t *session;
142 
143 	rv = meta_handle2session(hSession, &session);
144 	if (rv != CKR_OK)
145 		return (rv);
146 
147 	if (pulLastEncryptedPartLen == NULL) {
148 		meta_operation_cleanup(session, CKF_ENCRYPT, FALSE);
149 		REFRELEASE(session);
150 		return (CKR_ARGUMENTS_BAD);
151 	}
152 
153 	rv = meta_do_operation(CKF_ENCRYPT, MODE_FINAL, session, NULL,
154 	    NULL, 0, pLastEncryptedPart, pulLastEncryptedPartLen);
155 
156 	REFRELEASE(session);
157 
158 	return (rv);
159 }
160 
161 
162 /*
163  * meta_DecryptInit
164  *
165  */
166 CK_RV
167 meta_DecryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
168     CK_OBJECT_HANDLE hKey)
169 {
170 	CK_RV rv;
171 	meta_session_t *session;
172 	meta_object_t *key;
173 
174 	if (pMechanism == NULL)
175 		return (CKR_ARGUMENTS_BAD);
176 
177 	rv = meta_handle2session(hSession, &session);
178 	if (rv != CKR_OK)
179 		return (rv);
180 
181 	rv = meta_handle2object(hKey, &key);
182 	if (rv != CKR_OK) {
183 		REFRELEASE(session);
184 		return (rv);
185 	}
186 
187 	rv = meta_operation_init_defer(CKF_DECRYPT, session, pMechanism, key);
188 
189 	OBJRELEASE(key);
190 	REFRELEASE(session);
191 
192 	return (rv);
193 }
194 
195 
196 /*
197  * meta_Decrypt
198  *
199  */
200 CK_RV
201 meta_Decrypt(CK_SESSION_HANDLE hSession,
202     CK_BYTE_PTR pEncryptedData, CK_ULONG ulEncryptedDataLen,
203     CK_BYTE_PTR pData, CK_ULONG_PTR pulDataLen)
204 {
205 	CK_RV rv;
206 	meta_session_t *session;
207 
208 	rv = meta_handle2session(hSession, &session);
209 	if (rv != CKR_OK)
210 		return (rv);
211 
212 	if (pEncryptedData == NULL || pulDataLen == NULL) {
213 		meta_operation_cleanup(session, CKF_DECRYPT, FALSE);
214 		REFRELEASE(session);
215 		return (CKR_ARGUMENTS_BAD);
216 	}
217 
218 	rv = meta_do_operation(CKF_DECRYPT, MODE_SINGLE, session, NULL,
219 	    pEncryptedData, ulEncryptedDataLen, pData, pulDataLen);
220 
221 	REFRELEASE(session);
222 
223 	return (rv);
224 }
225 
226 
227 /*
228  * meta_DecryptUpdate
229  *
230  */
231 CK_RV
232 meta_DecryptUpdate(CK_SESSION_HANDLE hSession,
233     CK_BYTE_PTR pEncryptedPart, CK_ULONG ulEncryptedPartLen,
234     CK_BYTE_PTR pPart, CK_ULONG_PTR pulPartLen)
235 {
236 	CK_RV rv;
237 	meta_session_t *session;
238 
239 	rv = meta_handle2session(hSession, &session);
240 	if (rv != CKR_OK)
241 		return (rv);
242 
243 	if (pEncryptedPart == NULL || pulPartLen == NULL) {
244 		meta_operation_cleanup(session, CKF_DECRYPT, FALSE);
245 		REFRELEASE(session);
246 		return (CKR_ARGUMENTS_BAD);
247 	}
248 
249 	rv = meta_do_operation(CKF_DECRYPT, MODE_UPDATE, session, NULL,
250 	    pEncryptedPart, ulEncryptedPartLen, pPart, pulPartLen);
251 
252 	REFRELEASE(session);
253 
254 	return (rv);
255 }
256 
257 
258 /*
259  * meta_DecryptFinal
260  *
261  */
262 CK_RV
263 meta_DecryptFinal(CK_SESSION_HANDLE hSession,
264     CK_BYTE_PTR pLastPart, CK_ULONG_PTR pulLastPartLen)
265 {
266 	CK_RV rv;
267 	meta_session_t *session;
268 
269 	rv = meta_handle2session(hSession, &session);
270 	if (rv != CKR_OK)
271 		return (rv);
272 
273 	if (pulLastPartLen == NULL) {
274 		meta_operation_cleanup(session, CKF_DECRYPT, FALSE);
275 		REFRELEASE(session);
276 		return (CKR_ARGUMENTS_BAD);
277 	}
278 
279 	rv = meta_do_operation(CKF_DECRYPT, MODE_FINAL, session, NULL,
280 	    NULL, 0, pLastPart, pulLastPartLen);
281 
282 	REFRELEASE(session);
283 
284 	return (rv);
285 }
286