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.
25 * Copyright (c) 2018, Joyent, Inc.
29 #include <security/cryptoki.h>
30 #include "softGlobal.h"
31 #include "softSession.h"
32 #include "softObject.h"
37 C_EncryptInit(CK_SESSION_HANDLE hSession
, CK_MECHANISM_PTR pMechanism
,
38 CK_OBJECT_HANDLE hKey
)
42 soft_session_t
*session_p
;
44 boolean_t lock_held
= B_FALSE
;
46 if (!softtoken_initialized
)
47 return (CKR_CRYPTOKI_NOT_INITIALIZED
);
49 /* Obtain the session pointer. */
50 rv
= handle2session(hSession
, &session_p
);
54 if (pMechanism
== NULL
) {
55 rv
= CKR_ARGUMENTS_BAD
;
59 /* Obtain the object pointer. */
60 HANDLE2OBJECT(hKey
, key_p
, rv
);
64 /* Check to see if key object allows for encryption. */
65 if (!(key_p
->bool_attr_mask
& ENCRYPT_BOOL_ON
)) {
66 rv
= CKR_KEY_FUNCTION_NOT_PERMITTED
;
70 (void) pthread_mutex_lock(&session_p
->session_mutex
);
73 /* Check to see if encrypt operation is already active. */
74 if (session_p
->encrypt
.flags
& CRYPTO_OPERATION_ACTIVE
) {
75 /* free the memory to avoid memory leak */
76 soft_crypt_cleanup(session_p
, B_TRUE
, lock_held
);
80 * This active flag will remain ON until application calls either
81 * C_Encrypt or C_EncryptFinal to actually obtain the final piece
84 session_p
->encrypt
.flags
= CRYPTO_OPERATION_ACTIVE
;
86 (void) pthread_mutex_unlock(&session_p
->session_mutex
);
89 rv
= soft_encrypt_init(session_p
, pMechanism
, key_p
);
92 (void) pthread_mutex_lock(&session_p
->session_mutex
);
93 session_p
->encrypt
.flags
&= ~CRYPTO_OPERATION_ACTIVE
;
100 SES_REFRELE(session_p
, lock_held
);
106 C_Encrypt(CK_SESSION_HANDLE hSession
, CK_BYTE_PTR pData
, CK_ULONG ulDataLen
,
107 CK_BYTE_PTR pEncryptedData
, CK_ULONG_PTR pulEncryptedDataLen
)
111 soft_session_t
*session_p
;
112 boolean_t lock_held
= B_FALSE
;
114 if (!softtoken_initialized
)
115 return (CKR_CRYPTOKI_NOT_INITIALIZED
);
117 /* Obtain the session pointer. */
118 rv
= handle2session(hSession
, &session_p
);
123 * How to handle zero input length depends on the mechanism in use.
124 * For secret key mechanisms, unpadded ones yield zero length output,
125 * but padded ones always result in greater than zero length output.
127 if (pData
== NULL
&& ulDataLen
!= 0) {
128 rv
= CKR_ARGUMENTS_BAD
;
133 * Only check if pulEncryptedDataLen is NULL.
134 * No need to check if pEncryptedData is NULL because
135 * application might just ask for the length of buffer to hold
138 if (pulEncryptedDataLen
== NULL
) {
139 rv
= CKR_ARGUMENTS_BAD
;
143 (void) pthread_mutex_lock(&session_p
->session_mutex
);
146 /* Application must call C_EncryptInit before calling C_Encrypt. */
147 if (!(session_p
->encrypt
.flags
& CRYPTO_OPERATION_ACTIVE
)) {
148 SES_REFRELE(session_p
, lock_held
);
149 return (CKR_OPERATION_NOT_INITIALIZED
);
153 * C_Encrypt must be called without intervening C_EncryptUpdate
156 if (session_p
->encrypt
.flags
& CRYPTO_OPERATION_UPDATE
) {
158 * C_Encrypt can not be used to terminate a multi-part
159 * operation, so we'll leave the active encrypt operation
160 * flag on and let the application continue with the
161 * encrypt update operation.
163 SES_REFRELE(session_p
, lock_held
);
164 return (CKR_FUNCTION_FAILED
);
167 (void) pthread_mutex_unlock(&session_p
->session_mutex
);
170 rv
= soft_encrypt(session_p
, pData
, ulDataLen
, pEncryptedData
,
171 pulEncryptedDataLen
);
173 if ((rv
== CKR_BUFFER_TOO_SMALL
) ||
174 (pEncryptedData
== NULL
&& rv
== CKR_OK
)) {
176 * We will not terminate the active encrypt operation flag,
177 * when the application-supplied buffer is too small, or
178 * the application asks for the length of buffer to hold
181 SES_REFRELE(session_p
, lock_held
);
186 /* Clear context, free key, and release session counter */
187 soft_crypt_cleanup(session_p
, B_TRUE
, B_FALSE
);
193 C_EncryptUpdate(CK_SESSION_HANDLE hSession
, CK_BYTE_PTR pPart
,
194 CK_ULONG ulPartLen
, CK_BYTE_PTR pEncryptedPart
,
195 CK_ULONG_PTR pulEncryptedPartLen
)
199 soft_session_t
*session_p
;
200 boolean_t lock_held
= B_FALSE
;
202 if (!softtoken_initialized
)
203 return (CKR_CRYPTOKI_NOT_INITIALIZED
);
205 /* Obtain the session pointer. */
206 rv
= handle2session(hSession
, &session_p
);
211 * Only check if input buffer is null. How to handle zero input
212 * length depends on the mechanism in use. For secret key mechanisms,
213 * unpadded ones yeild zero length output, but padded ones always
214 * result in greater than zero length output.
217 rv
= CKR_ARGUMENTS_BAD
;
222 * Only check if pulEncryptedPartLen is NULL.
223 * No need to check if pEncryptedPart is NULL because
224 * application might just ask for the length of buffer to hold
227 if (pulEncryptedPartLen
== NULL
) {
228 rv
= CKR_ARGUMENTS_BAD
;
232 (void) pthread_mutex_lock(&session_p
->session_mutex
);
236 * Application must call C_EncryptInit before calling
239 if (!(session_p
->encrypt
.flags
& CRYPTO_OPERATION_ACTIVE
)) {
240 SES_REFRELE(session_p
, lock_held
);
241 return (CKR_OPERATION_NOT_INITIALIZED
);
244 session_p
->encrypt
.flags
|= CRYPTO_OPERATION_UPDATE
;
246 (void) pthread_mutex_unlock(&session_p
->session_mutex
);
249 rv
= soft_encrypt_update(session_p
, pPart
, ulPartLen
,
250 pEncryptedPart
, pulEncryptedPartLen
);
253 * If CKR_OK or CKR_BUFFER_TOO_SMALL, don't terminate the
254 * current encryption operation.
256 if ((rv
== CKR_OK
) || (rv
== CKR_BUFFER_TOO_SMALL
)) {
257 SES_REFRELE(session_p
, lock_held
);
263 * After an error occurred, terminate the current encrypt
264 * operation by resetting the active and update flags.
266 soft_crypt_cleanup(session_p
, B_TRUE
, lock_held
);
273 C_EncryptFinal(CK_SESSION_HANDLE hSession
, CK_BYTE_PTR pLastEncryptedPart
,
274 CK_ULONG_PTR pulLastEncryptedPartLen
)
278 soft_session_t
*session_p
;
279 boolean_t lock_held
= B_FALSE
;
281 if (!softtoken_initialized
)
282 return (CKR_CRYPTOKI_NOT_INITIALIZED
);
284 /* Obtain the session pointer. */
285 rv
= handle2session(hSession
, &session_p
);
289 if (pulLastEncryptedPartLen
== NULL
) {
290 rv
= CKR_ARGUMENTS_BAD
;
294 (void) pthread_mutex_lock(&session_p
->session_mutex
);
298 * Application must call C_EncryptInit before calling
301 if (!(session_p
->encrypt
.flags
& CRYPTO_OPERATION_ACTIVE
)) {
302 SES_REFRELE(session_p
, lock_held
);
303 return (CKR_OPERATION_NOT_INITIALIZED
);
306 (void) pthread_mutex_unlock(&session_p
->session_mutex
);
309 rv
= soft_encrypt_final(session_p
, pLastEncryptedPart
,
310 pulLastEncryptedPartLen
);
312 if ((rv
== CKR_BUFFER_TOO_SMALL
) ||
313 (pLastEncryptedPart
== NULL
&& rv
== CKR_OK
)) {
315 * We will not terminate the active encrypt operation flag,
316 * when the application-supplied buffer is too small, or
317 * the application asks for the length of buffer to hold
320 SES_REFRELE(session_p
, lock_held
);
324 /* Terminates the active encrypt operation. */
325 (void) pthread_mutex_lock(&session_p
->session_mutex
);
326 session_p
->encrypt
.flags
= 0;
328 SES_REFRELE(session_p
, lock_held
);
333 /* Terminates the active encrypt operation. */
334 soft_crypt_cleanup(session_p
, B_TRUE
, lock_held
);