dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / pkcs11 / libpkcs11 / common / metaCrypt.c
blob272469b54e419752a8c6344523de9cf2e92af9fc
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
29 * Encryption and Decryption Functions
30 * (as defined in PKCS#11 spec sections 11.8 and 11.9)
33 #include "metaGlobal.h"
37 * meta_EncryptInit
40 CK_RV
41 meta_EncryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
42 CK_OBJECT_HANDLE hKey)
44 CK_RV rv;
45 meta_session_t *session;
46 meta_object_t *key;
48 if (pMechanism == NULL)
49 return (CKR_ARGUMENTS_BAD);
51 rv = meta_handle2session(hSession, &session);
52 if (rv != CKR_OK)
53 return (rv);
55 rv = meta_handle2object(hKey, &key);
56 if (rv != CKR_OK) {
57 REFRELEASE(session);
58 return (rv);
61 rv = meta_operation_init_defer(CKF_ENCRYPT, session, pMechanism, key);
63 OBJRELEASE(key);
64 REFRELEASE(session);
66 return (rv);
71 * meta_Encrypt
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)
79 CK_RV rv;
80 meta_session_t *session;
82 rv = meta_handle2session(hSession, &session);
83 if (rv != CKR_OK)
84 return (rv);
86 if (pData == NULL || pulEncryptedDataLen == NULL) {
87 meta_operation_cleanup(session, CKF_ENCRYPT, FALSE);
88 REFRELEASE(session);
89 return (CKR_ARGUMENTS_BAD);
92 rv = meta_do_operation(CKF_ENCRYPT, MODE_SINGLE, session, NULL,
93 pData, ulDataLen, pEncryptedData, pulEncryptedDataLen);
95 REFRELEASE(session);
97 return (rv);
102 * meta_EncryptUpdate
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)
110 CK_RV rv;
111 meta_session_t *session;
113 rv = meta_handle2session(hSession, &session);
114 if (rv != CKR_OK)
115 return (rv);
117 if (pPart == NULL || pulEncryptedPartLen == NULL) {
118 meta_operation_cleanup(session, CKF_ENCRYPT, FALSE);
119 REFRELEASE(session);
120 return (CKR_ARGUMENTS_BAD);
123 rv = meta_do_operation(CKF_ENCRYPT, MODE_UPDATE, session, NULL,
124 pPart, ulPartLen, pEncryptedPart, pulEncryptedPartLen);
126 REFRELEASE(session);
128 return (rv);
133 * meta_EncryptFinal
136 CK_RV
137 meta_EncryptFinal(CK_SESSION_HANDLE hSession,
138 CK_BYTE_PTR pLastEncryptedPart, CK_ULONG_PTR pulLastEncryptedPartLen)
140 CK_RV rv;
141 meta_session_t *session;
143 rv = meta_handle2session(hSession, &session);
144 if (rv != CKR_OK)
145 return (rv);
147 if (pulLastEncryptedPartLen == NULL) {
148 meta_operation_cleanup(session, CKF_ENCRYPT, FALSE);
149 REFRELEASE(session);
150 return (CKR_ARGUMENTS_BAD);
153 rv = meta_do_operation(CKF_ENCRYPT, MODE_FINAL, session, NULL,
154 NULL, 0, pLastEncryptedPart, pulLastEncryptedPartLen);
156 REFRELEASE(session);
158 return (rv);
163 * meta_DecryptInit
166 CK_RV
167 meta_DecryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
168 CK_OBJECT_HANDLE hKey)
170 CK_RV rv;
171 meta_session_t *session;
172 meta_object_t *key;
174 if (pMechanism == NULL)
175 return (CKR_ARGUMENTS_BAD);
177 rv = meta_handle2session(hSession, &session);
178 if (rv != CKR_OK)
179 return (rv);
181 rv = meta_handle2object(hKey, &key);
182 if (rv != CKR_OK) {
183 REFRELEASE(session);
184 return (rv);
187 rv = meta_operation_init_defer(CKF_DECRYPT, session, pMechanism, key);
189 OBJRELEASE(key);
190 REFRELEASE(session);
192 return (rv);
197 * meta_Decrypt
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)
205 CK_RV rv;
206 meta_session_t *session;
208 rv = meta_handle2session(hSession, &session);
209 if (rv != CKR_OK)
210 return (rv);
212 if (pEncryptedData == NULL || pulDataLen == NULL) {
213 meta_operation_cleanup(session, CKF_DECRYPT, FALSE);
214 REFRELEASE(session);
215 return (CKR_ARGUMENTS_BAD);
218 rv = meta_do_operation(CKF_DECRYPT, MODE_SINGLE, session, NULL,
219 pEncryptedData, ulEncryptedDataLen, pData, pulDataLen);
221 REFRELEASE(session);
223 return (rv);
228 * meta_DecryptUpdate
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)
236 CK_RV rv;
237 meta_session_t *session;
239 rv = meta_handle2session(hSession, &session);
240 if (rv != CKR_OK)
241 return (rv);
243 if (pEncryptedPart == NULL || pulPartLen == NULL) {
244 meta_operation_cleanup(session, CKF_DECRYPT, FALSE);
245 REFRELEASE(session);
246 return (CKR_ARGUMENTS_BAD);
249 rv = meta_do_operation(CKF_DECRYPT, MODE_UPDATE, session, NULL,
250 pEncryptedPart, ulEncryptedPartLen, pPart, pulPartLen);
252 REFRELEASE(session);
254 return (rv);
259 * meta_DecryptFinal
262 CK_RV
263 meta_DecryptFinal(CK_SESSION_HANDLE hSession,
264 CK_BYTE_PTR pLastPart, CK_ULONG_PTR pulLastPartLen)
266 CK_RV rv;
267 meta_session_t *session;
269 rv = meta_handle2session(hSession, &session);
270 if (rv != CKR_OK)
271 return (rv);
273 if (pulLastPartLen == NULL) {
274 meta_operation_cleanup(session, CKF_DECRYPT, FALSE);
275 REFRELEASE(session);
276 return (CKR_ARGUMENTS_BAD);
279 rv = meta_do_operation(CKF_DECRYPT, MODE_FINAL, session, NULL,
280 NULL, 0, pLastPart, pulLastPartLen);
282 REFRELEASE(session);
284 return (rv);