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