Merge remote-tracking branch 'origin/master'
[unleashed/lotheac.git] / usr / src / lib / pkcs11 / pkcs11_softtoken / common / softEncrypt.c
blob69e7817a465c8303c9cba5d525f9b5491a78f69f
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 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
25 * Copyright (c) 2018, Joyent, Inc.
28 #include <pthread.h>
29 #include <security/cryptoki.h>
30 #include "softGlobal.h"
31 #include "softSession.h"
32 #include "softObject.h"
33 #include "softOps.h"
36 CK_RV
37 C_EncryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
38 CK_OBJECT_HANDLE hKey)
41 CK_RV rv;
42 soft_session_t *session_p;
43 soft_object_t *key_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);
51 if (rv != CKR_OK)
52 return (rv);
54 if (pMechanism == NULL) {
55 rv = CKR_ARGUMENTS_BAD;
56 goto clean_exit;
59 /* Obtain the object pointer. */
60 HANDLE2OBJECT(hKey, key_p, rv);
61 if (rv != CKR_OK)
62 goto clean_exit;
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;
67 goto clean_exit1;
70 (void) pthread_mutex_lock(&session_p->session_mutex);
71 lock_held = B_TRUE;
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
82 * of ciphertext.
84 session_p->encrypt.flags = CRYPTO_OPERATION_ACTIVE;
86 (void) pthread_mutex_unlock(&session_p->session_mutex);
87 lock_held = B_FALSE;
89 rv = soft_encrypt_init(session_p, pMechanism, key_p);
91 if (rv != CKR_OK) {
92 (void) pthread_mutex_lock(&session_p->session_mutex);
93 session_p->encrypt.flags &= ~CRYPTO_OPERATION_ACTIVE;
94 lock_held = B_TRUE;
97 clean_exit1:
98 OBJ_REFRELE(key_p);
99 clean_exit:
100 SES_REFRELE(session_p, lock_held);
101 return (rv);
105 CK_RV
106 C_Encrypt(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, CK_ULONG ulDataLen,
107 CK_BYTE_PTR pEncryptedData, CK_ULONG_PTR pulEncryptedDataLen)
110 CK_RV rv;
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);
119 if (rv != CKR_OK)
120 return (rv);
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;
129 goto clean_exit;
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
136 * the ciphertext.
138 if (pulEncryptedDataLen == NULL) {
139 rv = CKR_ARGUMENTS_BAD;
140 goto clean_exit;
143 (void) pthread_mutex_lock(&session_p->session_mutex);
144 lock_held = B_TRUE;
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
154 * calls.
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);
168 lock_held = B_FALSE;
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
179 * the ciphertext.
181 SES_REFRELE(session_p, lock_held);
182 return (rv);
185 clean_exit:
186 /* Clear context, free key, and release session counter */
187 soft_crypt_cleanup(session_p, B_TRUE, B_FALSE);
188 return (rv);
192 CK_RV
193 C_EncryptUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart,
194 CK_ULONG ulPartLen, CK_BYTE_PTR pEncryptedPart,
195 CK_ULONG_PTR pulEncryptedPartLen)
198 CK_RV rv;
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);
207 if (rv != CKR_OK)
208 return (rv);
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.
216 if (pPart == NULL) {
217 rv = CKR_ARGUMENTS_BAD;
218 goto clean_exit;
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
225 * the ciphertext.
227 if (pulEncryptedPartLen == NULL) {
228 rv = CKR_ARGUMENTS_BAD;
229 goto clean_exit;
232 (void) pthread_mutex_lock(&session_p->session_mutex);
233 lock_held = B_TRUE;
236 * Application must call C_EncryptInit before calling
237 * C_EncryptUpdate.
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);
247 lock_held = B_FALSE;
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);
258 return (rv);
261 clean_exit:
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);
268 return (rv);
272 CK_RV
273 C_EncryptFinal(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pLastEncryptedPart,
274 CK_ULONG_PTR pulLastEncryptedPartLen)
277 CK_RV rv;
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);
286 if (rv != CKR_OK)
287 return (rv);
289 if (pulLastEncryptedPartLen == NULL) {
290 rv = CKR_ARGUMENTS_BAD;
291 goto clean_exit;
294 (void) pthread_mutex_lock(&session_p->session_mutex);
295 lock_held = B_TRUE;
298 * Application must call C_EncryptInit before calling
299 * C_EncryptFinal.
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);
307 lock_held = B_FALSE;
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
318 * the ciphertext.
320 SES_REFRELE(session_p, lock_held);
321 return (rv);
324 /* Terminates the active encrypt operation. */
325 (void) pthread_mutex_lock(&session_p->session_mutex);
326 session_p->encrypt.flags = 0;
327 lock_held = B_TRUE;
328 SES_REFRELE(session_p, lock_held);
330 return (rv);
332 clean_exit:
333 /* Terminates the active encrypt operation. */
334 soft_crypt_cleanup(session_p, B_TRUE, lock_held);
336 return (rv);