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]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 #include <security/cryptoki.h>
28 #include "softGlobal.h"
29 #include "softSession.h"
30 #include "softObject.h"
35 C_EncryptInit(CK_SESSION_HANDLE hSession
, CK_MECHANISM_PTR pMechanism
,
36 CK_OBJECT_HANDLE hKey
)
40 soft_session_t
*session_p
;
42 boolean_t lock_held
= B_FALSE
;
44 if (!softtoken_initialized
)
45 return (CKR_CRYPTOKI_NOT_INITIALIZED
);
47 /* Obtain the session pointer. */
48 rv
= handle2session(hSession
, &session_p
);
52 if (pMechanism
== NULL
) {
53 rv
= CKR_ARGUMENTS_BAD
;
57 /* Obtain the object pointer. */
58 HANDLE2OBJECT(hKey
, key_p
, rv
);
62 /* Check to see if key object allows for encryption. */
63 if (!(key_p
->bool_attr_mask
& ENCRYPT_BOOL_ON
)) {
64 rv
= CKR_KEY_FUNCTION_NOT_PERMITTED
;
68 (void) pthread_mutex_lock(&session_p
->session_mutex
);
71 /* Check to see if encrypt operation is already active. */
72 if (session_p
->encrypt
.flags
& CRYPTO_OPERATION_ACTIVE
) {
73 /* free the memory to avoid memory leak */
74 soft_crypt_cleanup(session_p
, B_TRUE
, lock_held
);
78 * This active flag will remain ON until application calls either
79 * C_Encrypt or C_EncryptFinal to actually obtain the final piece
82 session_p
->encrypt
.flags
= CRYPTO_OPERATION_ACTIVE
;
84 (void) pthread_mutex_unlock(&session_p
->session_mutex
);
87 rv
= soft_encrypt_init(session_p
, pMechanism
, key_p
);
90 (void) pthread_mutex_lock(&session_p
->session_mutex
);
91 session_p
->encrypt
.flags
&= ~CRYPTO_OPERATION_ACTIVE
;
98 SES_REFRELE(session_p
, lock_held
);
104 C_Encrypt(CK_SESSION_HANDLE hSession
, CK_BYTE_PTR pData
, CK_ULONG ulDataLen
,
105 CK_BYTE_PTR pEncryptedData
, CK_ULONG_PTR pulEncryptedDataLen
)
109 soft_session_t
*session_p
;
110 boolean_t lock_held
= B_FALSE
;
112 if (!softtoken_initialized
)
113 return (CKR_CRYPTOKI_NOT_INITIALIZED
);
115 /* Obtain the session pointer. */
116 rv
= handle2session(hSession
, &session_p
);
121 * Only check if input buffer is null. How to handle zero input
122 * length depends on the mechanism in use. For secret key mechanisms,
123 * unpadded ones yield zero length output, but padded ones always
124 * result in greater than zero length output.
127 rv
= CKR_ARGUMENTS_BAD
;
132 * Only check if pulEncryptedDataLen is NULL.
133 * No need to check if pEncryptedData is NULL because
134 * application might just ask for the length of buffer to hold
137 if (pulEncryptedDataLen
== NULL
) {
138 rv
= CKR_ARGUMENTS_BAD
;
142 (void) pthread_mutex_lock(&session_p
->session_mutex
);
145 /* Application must call C_EncryptInit before calling C_Encrypt. */
146 if (!(session_p
->encrypt
.flags
& CRYPTO_OPERATION_ACTIVE
)) {
147 SES_REFRELE(session_p
, lock_held
);
148 return (CKR_OPERATION_NOT_INITIALIZED
);
152 * C_Encrypt must be called without intervening C_EncryptUpdate
155 if (session_p
->encrypt
.flags
& CRYPTO_OPERATION_UPDATE
) {
157 * C_Encrypt can not be used to terminate a multi-part
158 * operation, so we'll leave the active encrypt operation
159 * flag on and let the application continue with the
160 * encrypt update operation.
162 SES_REFRELE(session_p
, lock_held
);
163 return (CKR_FUNCTION_FAILED
);
166 (void) pthread_mutex_unlock(&session_p
->session_mutex
);
169 rv
= soft_encrypt(session_p
, pData
, ulDataLen
, pEncryptedData
,
170 pulEncryptedDataLen
);
172 if ((rv
== CKR_BUFFER_TOO_SMALL
) ||
173 (pEncryptedData
== NULL
&& rv
== CKR_OK
)) {
175 * We will not terminate the active encrypt operation flag,
176 * when the application-supplied buffer is too small, or
177 * the application asks for the length of buffer to hold
180 SES_REFRELE(session_p
, lock_held
);
185 /* Clear context, free key, and release session counter */
186 soft_crypt_cleanup(session_p
, B_TRUE
, B_FALSE
);
192 C_EncryptUpdate(CK_SESSION_HANDLE hSession
, CK_BYTE_PTR pPart
,
193 CK_ULONG ulPartLen
, CK_BYTE_PTR pEncryptedPart
,
194 CK_ULONG_PTR pulEncryptedPartLen
)
198 soft_session_t
*session_p
;
199 boolean_t lock_held
= B_FALSE
;
201 if (!softtoken_initialized
)
202 return (CKR_CRYPTOKI_NOT_INITIALIZED
);
204 /* Obtain the session pointer. */
205 rv
= handle2session(hSession
, &session_p
);
210 * Only check if input buffer is null. How to handle zero input
211 * length depends on the mechanism in use. For secret key mechanisms,
212 * unpadded ones yeild zero length output, but padded ones always
213 * result in greater than zero length output.
216 rv
= CKR_ARGUMENTS_BAD
;
221 * Only check if pulEncryptedPartLen is NULL.
222 * No need to check if pEncryptedPart is NULL because
223 * application might just ask for the length of buffer to hold
226 if (pulEncryptedPartLen
== NULL
) {
227 rv
= CKR_ARGUMENTS_BAD
;
231 (void) pthread_mutex_lock(&session_p
->session_mutex
);
235 * Application must call C_EncryptInit before calling
238 if (!(session_p
->encrypt
.flags
& CRYPTO_OPERATION_ACTIVE
)) {
239 SES_REFRELE(session_p
, lock_held
);
240 return (CKR_OPERATION_NOT_INITIALIZED
);
243 session_p
->encrypt
.flags
|= CRYPTO_OPERATION_UPDATE
;
245 (void) pthread_mutex_unlock(&session_p
->session_mutex
);
248 rv
= soft_encrypt_update(session_p
, pPart
, ulPartLen
,
249 pEncryptedPart
, pulEncryptedPartLen
);
252 * If CKR_OK or CKR_BUFFER_TOO_SMALL, don't terminate the
253 * current encryption operation.
255 if ((rv
== CKR_OK
) || (rv
== CKR_BUFFER_TOO_SMALL
)) {
256 SES_REFRELE(session_p
, lock_held
);
262 * After an error occurred, terminate the current encrypt
263 * operation by resetting the active and update flags.
265 soft_crypt_cleanup(session_p
, B_TRUE
, lock_held
);
272 C_EncryptFinal(CK_SESSION_HANDLE hSession
, CK_BYTE_PTR pLastEncryptedPart
,
273 CK_ULONG_PTR pulLastEncryptedPartLen
)
277 soft_session_t
*session_p
;
278 boolean_t lock_held
= B_FALSE
;
280 if (!softtoken_initialized
)
281 return (CKR_CRYPTOKI_NOT_INITIALIZED
);
283 /* Obtain the session pointer. */
284 rv
= handle2session(hSession
, &session_p
);
288 if (pulLastEncryptedPartLen
== NULL
) {
289 rv
= CKR_ARGUMENTS_BAD
;
293 (void) pthread_mutex_lock(&session_p
->session_mutex
);
297 * Application must call C_EncryptInit before calling
300 if (!(session_p
->encrypt
.flags
& CRYPTO_OPERATION_ACTIVE
)) {
301 SES_REFRELE(session_p
, lock_held
);
302 return (CKR_OPERATION_NOT_INITIALIZED
);
305 (void) pthread_mutex_unlock(&session_p
->session_mutex
);
308 rv
= soft_encrypt_final(session_p
, pLastEncryptedPart
,
309 pulLastEncryptedPartLen
);
311 if ((rv
== CKR_BUFFER_TOO_SMALL
) ||
312 (pLastEncryptedPart
== NULL
&& rv
== CKR_OK
)) {
314 * We will not terminate the active encrypt operation flag,
315 * when the application-supplied buffer is too small, or
316 * the application asks for the length of buffer to hold
319 SES_REFRELE(session_p
, lock_held
);
323 /* Terminates the active encrypt operation. */
324 (void) pthread_mutex_lock(&session_p
->session_mutex
);
325 session_p
->encrypt
.flags
= 0;
327 SES_REFRELE(session_p
, lock_held
);
332 /* Terminates the active encrypt operation. */
333 soft_crypt_cleanup(session_p
, B_TRUE
, lock_held
);