xref: /titanic_53/usr/src/lib/pkcs11/pkcs11_kernel/common/kernelDecrypt.c (revision 6d2259e1baf8d4ac11c96570f45ecdcd9771a68d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
587fa5c53Smcpowers  * Common Development and Distribution License (the "License").
687fa5c53Smcpowers  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*6d2259e1SDan OpenSolaris Anderson  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <pthread.h>
277c478bd9Sstevel@tonic-gate #include <stdlib.h>
287c478bd9Sstevel@tonic-gate #include <errno.h>
297c478bd9Sstevel@tonic-gate #include <sys/crypto/ioctl.h>
307c478bd9Sstevel@tonic-gate #include <security/cryptoki.h>
317c478bd9Sstevel@tonic-gate #include "kernelGlobal.h"
327c478bd9Sstevel@tonic-gate #include "kernelSession.h"
337c478bd9Sstevel@tonic-gate #include "kernelObject.h"
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate  * Real decryptInit work. The caller doesn't hold the session lock.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate CK_RV
kernel_decrypt_init(kernel_session_t * session_p,kernel_object_t * key_p,CK_MECHANISM_PTR pMechanism)407c478bd9Sstevel@tonic-gate kernel_decrypt_init(kernel_session_t *session_p, kernel_object_t *key_p,
417c478bd9Sstevel@tonic-gate     CK_MECHANISM_PTR pMechanism)
427c478bd9Sstevel@tonic-gate {
437c478bd9Sstevel@tonic-gate 	CK_RV rv;
447c478bd9Sstevel@tonic-gate 	crypto_decrypt_init_t decrypt_init;
457c478bd9Sstevel@tonic-gate 	crypto_mech_type_t k_mech_type;
467c478bd9Sstevel@tonic-gate 	boolean_t ses_lock_held = B_FALSE;
477c478bd9Sstevel@tonic-gate 	int r;
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate 	/* Check to see if key object allows for decryption. */
507c478bd9Sstevel@tonic-gate 	if (key_p->is_lib_obj && !(key_p->bool_attr_mask & DECRYPT_BOOL_ON)) {
517c478bd9Sstevel@tonic-gate 		return (CKR_KEY_TYPE_INCONSISTENT);
527c478bd9Sstevel@tonic-gate 	}
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 	/* Get the kernel's internal mechanism number. */
557c478bd9Sstevel@tonic-gate 	rv = kernel_mech(pMechanism->mechanism, &k_mech_type);
567c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK)
577c478bd9Sstevel@tonic-gate 		return (rv);
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&session_p->session_mutex);
607c478bd9Sstevel@tonic-gate 	ses_lock_held = B_TRUE;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	/*
637c478bd9Sstevel@tonic-gate 	 * This active flag will remain ON until application calls either
647c478bd9Sstevel@tonic-gate 	 * C_Decrypt or C_DecryptFinal to actually obtain the final piece
657c478bd9Sstevel@tonic-gate 	 * of plaintext.
667c478bd9Sstevel@tonic-gate 	 */
677c478bd9Sstevel@tonic-gate 	session_p->decrypt.flags = CRYPTO_OPERATION_ACTIVE;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	/* set up key data */
707c478bd9Sstevel@tonic-gate 	if (!key_p->is_lib_obj) {
717c478bd9Sstevel@tonic-gate 		decrypt_init.di_key.ck_format = CRYPTO_KEY_REFERENCE;
727c478bd9Sstevel@tonic-gate 		decrypt_init.di_key.ck_obj_id = key_p->k_handle;
737c478bd9Sstevel@tonic-gate 	} else {
747c478bd9Sstevel@tonic-gate 		if (key_p->class == CKO_SECRET_KEY) {
757c478bd9Sstevel@tonic-gate 			decrypt_init.di_key.ck_format = CRYPTO_KEY_RAW;
767c478bd9Sstevel@tonic-gate 			decrypt_init.di_key.ck_data =
777c478bd9Sstevel@tonic-gate 			    get_symmetric_key_value(key_p);
787c478bd9Sstevel@tonic-gate 			if (decrypt_init.di_key.ck_data == NULL) {
797c478bd9Sstevel@tonic-gate 				rv = CKR_HOST_MEMORY;
807c478bd9Sstevel@tonic-gate 				goto clean_exit;
817c478bd9Sstevel@tonic-gate 			}
827c478bd9Sstevel@tonic-gate 			/* KEF key lengths are expressed in bits */
837c478bd9Sstevel@tonic-gate 			decrypt_init.di_key.ck_length =
847c478bd9Sstevel@tonic-gate 			    OBJ_SEC(key_p)->sk_value_len << 3;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 		} else if (key_p->key_type == CKK_RSA) {
877c478bd9Sstevel@tonic-gate 			if (get_rsa_private_key(key_p, &decrypt_init.di_key) !=
887c478bd9Sstevel@tonic-gate 			    CKR_OK) {
897c478bd9Sstevel@tonic-gate 				rv = CKR_HOST_MEMORY;
907c478bd9Sstevel@tonic-gate 				goto clean_exit;
917c478bd9Sstevel@tonic-gate 			}
927c478bd9Sstevel@tonic-gate 		} else {
937c478bd9Sstevel@tonic-gate 			rv = CKR_KEY_TYPE_INCONSISTENT;
947c478bd9Sstevel@tonic-gate 			goto clean_exit;
957c478bd9Sstevel@tonic-gate 		}
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	decrypt_init.di_session = session_p->k_session;
9987fa5c53Smcpowers 	session_p->decrypt.mech = *pMechanism;
100*6d2259e1SDan OpenSolaris Anderson 
101*6d2259e1SDan OpenSolaris Anderson 	/* Cache this capability value for efficiency */
102*6d2259e1SDan OpenSolaris Anderson 	if (INPLACE_MECHANISM(session_p->decrypt.mech.mechanism)) {
103*6d2259e1SDan OpenSolaris Anderson 		session_p->decrypt.flags |= CRYPTO_OPERATION_INPLACE_OK;
104*6d2259e1SDan OpenSolaris Anderson 	}
1057c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&session_p->session_mutex);
106*6d2259e1SDan OpenSolaris Anderson 
1077c478bd9Sstevel@tonic-gate 	ses_lock_held = B_FALSE;
1087c478bd9Sstevel@tonic-gate 	decrypt_init.di_mech.cm_type = k_mech_type;
1097c478bd9Sstevel@tonic-gate 	decrypt_init.di_mech.cm_param = pMechanism->pParameter;
1107c478bd9Sstevel@tonic-gate 	decrypt_init.di_mech.cm_param_len = pMechanism->ulParameterLen;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	while ((r = ioctl(kernel_fd, CRYPTO_DECRYPT_INIT, &decrypt_init)) < 0) {
1137c478bd9Sstevel@tonic-gate 		if (errno != EINTR)
1147c478bd9Sstevel@tonic-gate 			break;
1157c478bd9Sstevel@tonic-gate 	}
1167c478bd9Sstevel@tonic-gate 	if (r < 0) {
1177c478bd9Sstevel@tonic-gate 		rv = CKR_FUNCTION_FAILED;
1187c478bd9Sstevel@tonic-gate 	} else {
1197c478bd9Sstevel@tonic-gate 		rv = crypto2pkcs11_error_number(decrypt_init.di_return_value);
1207c478bd9Sstevel@tonic-gate 	}
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	/* Free memory allocated for decrypt_init.di_key */
1237c478bd9Sstevel@tonic-gate 	if (key_p->is_lib_obj) {
1247c478bd9Sstevel@tonic-gate 		if (key_p->class == CKO_SECRET_KEY) {
1257c478bd9Sstevel@tonic-gate 			free(decrypt_init.di_key.ck_data);
1267c478bd9Sstevel@tonic-gate 		} else if (key_p->key_type == CKK_RSA) {
1277c478bd9Sstevel@tonic-gate 			free_key_attributes(&decrypt_init.di_key);
1287c478bd9Sstevel@tonic-gate 		}
1297c478bd9Sstevel@tonic-gate 	}
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate clean_exit:
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	if (!ses_lock_held) {
1347c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_lock(&session_p->session_mutex);
1357c478bd9Sstevel@tonic-gate 		ses_lock_held = B_TRUE;
1367c478bd9Sstevel@tonic-gate 	}
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK)
1397c478bd9Sstevel@tonic-gate 		session_p->decrypt.flags &= ~CRYPTO_OPERATION_ACTIVE;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	if (ses_lock_held) {
1427c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&session_p->session_mutex);
1437c478bd9Sstevel@tonic-gate 		ses_lock_held = B_FALSE;
1447c478bd9Sstevel@tonic-gate 	}
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	return (rv);
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate CK_RV
C_DecryptInit(CK_SESSION_HANDLE hSession,CK_MECHANISM_PTR pMechanism,CK_OBJECT_HANDLE hKey)1507c478bd9Sstevel@tonic-gate C_DecryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
1517c478bd9Sstevel@tonic-gate     CK_OBJECT_HANDLE hKey)
1527c478bd9Sstevel@tonic-gate {
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	CK_RV rv;
1557c478bd9Sstevel@tonic-gate 	kernel_session_t *session_p;
1567c478bd9Sstevel@tonic-gate 	kernel_object_t	*key_p;
1577c478bd9Sstevel@tonic-gate 	boolean_t ses_lock_held = B_FALSE;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	if (!kernel_initialized)
1607c478bd9Sstevel@tonic-gate 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	if (pMechanism == NULL) {
1637c478bd9Sstevel@tonic-gate 		return (CKR_ARGUMENTS_BAD);
1647c478bd9Sstevel@tonic-gate 	}
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	/* Obtain the session pointer. */
1677c478bd9Sstevel@tonic-gate 	rv = handle2session(hSession, &session_p);
1687c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK)
1697c478bd9Sstevel@tonic-gate 		return (rv);
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	/* Obtain the object pointer. */
1727c478bd9Sstevel@tonic-gate 	HANDLE2OBJECT(hKey, key_p, rv);
1737c478bd9Sstevel@tonic-gate 	if (rv == CKR_OK) {
1747c478bd9Sstevel@tonic-gate 		rv = kernel_decrypt_init(session_p, key_p, pMechanism);
17501223cbaSmcpowers 		OBJ_REFRELE(key_p);
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	REFRELE(session_p, ses_lock_held);
1797c478bd9Sstevel@tonic-gate 	return (rv);
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate /*
1857c478bd9Sstevel@tonic-gate  * Real decrypt work. The caller doesn't hold the session lock.
1867c478bd9Sstevel@tonic-gate  */
1877c478bd9Sstevel@tonic-gate CK_RV
kernel_decrypt(kernel_session_t * session_p,CK_BYTE_PTR pEncryptedData,CK_ULONG ulEncryptedData,CK_BYTE_PTR pData,CK_ULONG_PTR pulDataLen)1887c478bd9Sstevel@tonic-gate kernel_decrypt(kernel_session_t *session_p, CK_BYTE_PTR pEncryptedData,
1897c478bd9Sstevel@tonic-gate     CK_ULONG ulEncryptedData, CK_BYTE_PTR pData, CK_ULONG_PTR pulDataLen)
1907c478bd9Sstevel@tonic-gate {
1917c478bd9Sstevel@tonic-gate 	crypto_decrypt_t decrypt;
1927c478bd9Sstevel@tonic-gate 	boolean_t ses_lock_held = B_FALSE;
19387fa5c53Smcpowers 	boolean_t inplace;
1947c478bd9Sstevel@tonic-gate 	CK_RV rv;
1957c478bd9Sstevel@tonic-gate 	int r;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&session_p->session_mutex);
1987c478bd9Sstevel@tonic-gate 	ses_lock_held = B_TRUE;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	/* Application must call C_DecryptInit before calling C_Decrypt. */
2017c478bd9Sstevel@tonic-gate 	if (!(session_p->decrypt.flags & CRYPTO_OPERATION_ACTIVE)) {
2027c478bd9Sstevel@tonic-gate 		rv = CKR_OPERATION_NOT_INITIALIZED;
2037c478bd9Sstevel@tonic-gate 		goto clean_exit;
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	/*
2077c478bd9Sstevel@tonic-gate 	 * C_Decrypt must be called without intervening C_DecryptUpdate
2087c478bd9Sstevel@tonic-gate 	 * calls.
2097c478bd9Sstevel@tonic-gate 	 */
2107c478bd9Sstevel@tonic-gate 	if (session_p->decrypt.flags & CRYPTO_OPERATION_UPDATE) {
2117c478bd9Sstevel@tonic-gate 		/*
2127c478bd9Sstevel@tonic-gate 		 * C_Decrypt cannot be used to terminate a multiple-part
2137c478bd9Sstevel@tonic-gate 		 * operation, so we'll leave the active decrypt operation
2147c478bd9Sstevel@tonic-gate 		 * flag on and let the application continue with the
2157c478bd9Sstevel@tonic-gate 		 * decrypt update operation.
2167c478bd9Sstevel@tonic-gate 		 */
2177c478bd9Sstevel@tonic-gate 		rv = CKR_FUNCTION_FAILED;
2187c478bd9Sstevel@tonic-gate 		goto clean_exit;
2197c478bd9Sstevel@tonic-gate 	}
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	decrypt.cd_session = session_p->k_session;
22287fa5c53Smcpowers 
22387fa5c53Smcpowers 	/*
22487fa5c53Smcpowers 	 * Certain mechanisms, where the length of the plaintext is
22587fa5c53Smcpowers 	 * same as the transformed ciphertext, can be optimized
22687fa5c53Smcpowers 	 * by the kernel into an in-place operation. Unfortunately,
22787fa5c53Smcpowers 	 * some applications use a plaintext buffer that is larger
22887fa5c53Smcpowers 	 * than it needs to be. We fix that here.
22987fa5c53Smcpowers 	 */
230*6d2259e1SDan OpenSolaris Anderson 	inplace = (session_p->decrypt.flags & CRYPTO_OPERATION_INPLACE_OK) != 0;
231*6d2259e1SDan OpenSolaris Anderson 
23287fa5c53Smcpowers 	if (ulEncryptedData < *pulDataLen && inplace) {
23387fa5c53Smcpowers 		decrypt.cd_datalen = ulEncryptedData;
23487fa5c53Smcpowers 	} else {
23587fa5c53Smcpowers 		decrypt.cd_datalen = *pulDataLen;
23687fa5c53Smcpowers 	}
2377c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&session_p->session_mutex);
2387c478bd9Sstevel@tonic-gate 	ses_lock_held = B_FALSE;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	decrypt.cd_databuf = (char *)pData;
2417c478bd9Sstevel@tonic-gate 	decrypt.cd_encrlen = ulEncryptedData;
2427c478bd9Sstevel@tonic-gate 	decrypt.cd_encrbuf = (char *)pEncryptedData;
243*6d2259e1SDan OpenSolaris Anderson 	decrypt.cd_flags =
244*6d2259e1SDan OpenSolaris Anderson 	    ((inplace && (pData != NULL)) || (pData == pEncryptedData)) &&
245*6d2259e1SDan OpenSolaris Anderson 	    (decrypt.cd_datalen == decrypt.cd_encrlen) ?
24687fa5c53Smcpowers 	    CRYPTO_INPLACE_OPERATION : 0;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	while ((r = ioctl(kernel_fd, CRYPTO_DECRYPT, &decrypt)) < 0) {
2497c478bd9Sstevel@tonic-gate 		if (errno != EINTR)
2507c478bd9Sstevel@tonic-gate 			break;
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate 	if (r < 0) {
2537c478bd9Sstevel@tonic-gate 		rv = CKR_FUNCTION_FAILED;
2547c478bd9Sstevel@tonic-gate 	} else {
2557c478bd9Sstevel@tonic-gate 		rv = crypto2pkcs11_error_number(decrypt.cd_return_value);
2567c478bd9Sstevel@tonic-gate 	}
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	if (rv == CKR_OK || rv == CKR_BUFFER_TOO_SMALL)
2597c478bd9Sstevel@tonic-gate 		*pulDataLen = decrypt.cd_datalen;
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate clean_exit:
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	if (ses_lock_held)
2647c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&session_p->session_mutex);
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	return (rv);
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate CK_RV
C_Decrypt(CK_SESSION_HANDLE hSession,CK_BYTE_PTR pEncryptedData,CK_ULONG ulEncryptedData,CK_BYTE_PTR pData,CK_ULONG_PTR pulDataLen)2707c478bd9Sstevel@tonic-gate C_Decrypt(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pEncryptedData,
2717c478bd9Sstevel@tonic-gate     CK_ULONG ulEncryptedData, CK_BYTE_PTR pData, CK_ULONG_PTR pulDataLen)
2727c478bd9Sstevel@tonic-gate {
2737c478bd9Sstevel@tonic-gate 	CK_RV rv;
2747c478bd9Sstevel@tonic-gate 	kernel_session_t *session_p;
2757c478bd9Sstevel@tonic-gate 	boolean_t ses_lock_held = B_FALSE;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	if (!kernel_initialized)
2787c478bd9Sstevel@tonic-gate 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	/* Obtain the session pointer. */
2817c478bd9Sstevel@tonic-gate 	rv = handle2session(hSession, &session_p);
2827c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK)
2837c478bd9Sstevel@tonic-gate 		return (rv);
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	/*
2867c478bd9Sstevel@tonic-gate 	 * No need to check pData because application might
2877c478bd9Sstevel@tonic-gate 	 * just want to know the length of decrypted data.
2887c478bd9Sstevel@tonic-gate 	 */
2897c478bd9Sstevel@tonic-gate 	if (pulDataLen == NULL) {
2907c478bd9Sstevel@tonic-gate 		rv = CKR_ARGUMENTS_BAD;
2917c478bd9Sstevel@tonic-gate 		goto clean_exit;
2927c478bd9Sstevel@tonic-gate 	}
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	rv = kernel_decrypt(session_p, pEncryptedData, ulEncryptedData, pData,
2957c478bd9Sstevel@tonic-gate 	    pulDataLen);
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	if ((rv == CKR_BUFFER_TOO_SMALL) ||
2987c478bd9Sstevel@tonic-gate 	    (rv == CKR_OK && pData == NULL)) {
2997c478bd9Sstevel@tonic-gate 		/*
3007c478bd9Sstevel@tonic-gate 		 * We will not terminate the active decrypt operation flag,
3017c478bd9Sstevel@tonic-gate 		 * when the application-supplied buffer is too small, or
3027c478bd9Sstevel@tonic-gate 		 * the application asks for the length of buffer to hold
3037c478bd9Sstevel@tonic-gate 		 * the plaintext.
3047c478bd9Sstevel@tonic-gate 		 */
3057c478bd9Sstevel@tonic-gate 		REFRELE(session_p, ses_lock_held);
3067c478bd9Sstevel@tonic-gate 		return (rv);
3077c478bd9Sstevel@tonic-gate 	}
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate clean_exit:
3107c478bd9Sstevel@tonic-gate 	/*
3117c478bd9Sstevel@tonic-gate 	 * Terminates the active decrypt operation.
3127c478bd9Sstevel@tonic-gate 	 * Application needs to call C_DecryptInit again for next
3137c478bd9Sstevel@tonic-gate 	 * decrypt operation.
3147c478bd9Sstevel@tonic-gate 	 */
3157c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&session_p->session_mutex);
3167c478bd9Sstevel@tonic-gate 	session_p->decrypt.flags = 0;
3177c478bd9Sstevel@tonic-gate 	ses_lock_held = B_TRUE;
3187c478bd9Sstevel@tonic-gate 	REFRELE(session_p, ses_lock_held);
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	return (rv);
3217c478bd9Sstevel@tonic-gate }
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate CK_RV
C_DecryptUpdate(CK_SESSION_HANDLE hSession,CK_BYTE_PTR pEncryptedPart,CK_ULONG ulEncryptedPartLen,CK_BYTE_PTR pPart,CK_ULONG_PTR pulPartLen)3257c478bd9Sstevel@tonic-gate C_DecryptUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pEncryptedPart,
3267c478bd9Sstevel@tonic-gate     CK_ULONG ulEncryptedPartLen, CK_BYTE_PTR pPart,
3277c478bd9Sstevel@tonic-gate     CK_ULONG_PTR pulPartLen)
3287c478bd9Sstevel@tonic-gate {
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	CK_RV rv;
3317c478bd9Sstevel@tonic-gate 	kernel_session_t *session_p;
3327c478bd9Sstevel@tonic-gate 	boolean_t ses_lock_held = B_FALSE;
333*6d2259e1SDan OpenSolaris Anderson 	boolean_t inplace;
3347c478bd9Sstevel@tonic-gate 	crypto_decrypt_update_t decrypt_update;
3357c478bd9Sstevel@tonic-gate 	int r;
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	if (!kernel_initialized)
3387c478bd9Sstevel@tonic-gate 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	/* Obtain the session pointer. */
3417c478bd9Sstevel@tonic-gate 	rv = handle2session(hSession, &session_p);
3427c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK)
3437c478bd9Sstevel@tonic-gate 		return (rv);
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	if (pEncryptedPart == NULL) {
3467c478bd9Sstevel@tonic-gate 		rv = CKR_ARGUMENTS_BAD;
3477c478bd9Sstevel@tonic-gate 		goto clean_exit;
3487c478bd9Sstevel@tonic-gate 	}
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	/*
3517c478bd9Sstevel@tonic-gate 	 * Only check if pulPartLen is NULL.
3527c478bd9Sstevel@tonic-gate 	 * No need to check if pPart is NULL because application
3537c478bd9Sstevel@tonic-gate 	 * might just ask for the length of buffer to hold the
3547c478bd9Sstevel@tonic-gate 	 * recovered data.
3557c478bd9Sstevel@tonic-gate 	 */
3567c478bd9Sstevel@tonic-gate 	if (pulPartLen == NULL) {
3577c478bd9Sstevel@tonic-gate 		rv = CKR_ARGUMENTS_BAD;
3587c478bd9Sstevel@tonic-gate 		goto clean_exit;
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&session_p->session_mutex);
3627c478bd9Sstevel@tonic-gate 	ses_lock_held = B_TRUE;
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	/*
3657c478bd9Sstevel@tonic-gate 	 * Application must call C_DecryptInit before calling
3667c478bd9Sstevel@tonic-gate 	 * C_DecryptUpdate.
3677c478bd9Sstevel@tonic-gate 	 */
3687c478bd9Sstevel@tonic-gate 	if (!(session_p->decrypt.flags & CRYPTO_OPERATION_ACTIVE)) {
3697c478bd9Sstevel@tonic-gate 		REFRELE(session_p, ses_lock_held);
3707c478bd9Sstevel@tonic-gate 		return (CKR_OPERATION_NOT_INITIALIZED);
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	session_p->decrypt.flags |= CRYPTO_OPERATION_UPDATE;
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	decrypt_update.du_session = session_p->k_session;
3767c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&session_p->session_mutex);
3777c478bd9Sstevel@tonic-gate 	ses_lock_held = B_FALSE;
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	decrypt_update.du_datalen = *pulPartLen;
3807c478bd9Sstevel@tonic-gate 	decrypt_update.du_databuf = (char *)pPart;
3817c478bd9Sstevel@tonic-gate 	decrypt_update.du_encrlen = ulEncryptedPartLen;
3827c478bd9Sstevel@tonic-gate 	decrypt_update.du_encrbuf = (char *)pEncryptedPart;
3837c478bd9Sstevel@tonic-gate 
384*6d2259e1SDan OpenSolaris Anderson 	inplace = (session_p->decrypt.flags & CRYPTO_OPERATION_INPLACE_OK) != 0;
385*6d2259e1SDan OpenSolaris Anderson 	decrypt_update.du_flags =
386*6d2259e1SDan OpenSolaris Anderson 	    ((inplace && (pPart != NULL)) || (pPart == pEncryptedPart)) &&
387*6d2259e1SDan OpenSolaris Anderson 	    (decrypt_update.du_datalen == decrypt_update.du_encrlen) ?
388*6d2259e1SDan OpenSolaris Anderson 	    CRYPTO_INPLACE_OPERATION : 0;
389*6d2259e1SDan OpenSolaris Anderson 
3907c478bd9Sstevel@tonic-gate 	while ((r = ioctl(kernel_fd, CRYPTO_DECRYPT_UPDATE,
3917c478bd9Sstevel@tonic-gate 	    &decrypt_update)) < 0) {
3927c478bd9Sstevel@tonic-gate 		if (errno != EINTR)
3937c478bd9Sstevel@tonic-gate 			break;
3947c478bd9Sstevel@tonic-gate 	}
3957c478bd9Sstevel@tonic-gate 	if (r < 0) {
3967c478bd9Sstevel@tonic-gate 		rv = CKR_FUNCTION_FAILED;
3977c478bd9Sstevel@tonic-gate 	} else {
3987c478bd9Sstevel@tonic-gate 		rv = crypto2pkcs11_error_number(
3997c478bd9Sstevel@tonic-gate 		    decrypt_update.du_return_value);
4007c478bd9Sstevel@tonic-gate 	}
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	/*
4037c478bd9Sstevel@tonic-gate 	 * If CKR_OK or CKR_BUFFER_TOO_SMALL, set the output length.
4047c478bd9Sstevel@tonic-gate 	 * We don't terminate the current decryption operation.
4057c478bd9Sstevel@tonic-gate 	 */
4067c478bd9Sstevel@tonic-gate 	if (rv == CKR_OK || rv == CKR_BUFFER_TOO_SMALL) {
4077c478bd9Sstevel@tonic-gate 		*pulPartLen = decrypt_update.du_datalen;
4087c478bd9Sstevel@tonic-gate 		REFRELE(session_p, ses_lock_held);
4097c478bd9Sstevel@tonic-gate 		return (rv);
4107c478bd9Sstevel@tonic-gate 	}
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate clean_exit:
4137c478bd9Sstevel@tonic-gate 	/*
4147c478bd9Sstevel@tonic-gate 	 * After an error occurred, terminate the current decrypt
4157c478bd9Sstevel@tonic-gate 	 * operation by resetting the active and update flags.
4167c478bd9Sstevel@tonic-gate 	 */
4177c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&session_p->session_mutex);
4187c478bd9Sstevel@tonic-gate 	session_p->decrypt.flags = 0;
4197c478bd9Sstevel@tonic-gate 	ses_lock_held = B_TRUE;
4207c478bd9Sstevel@tonic-gate 	REFRELE(session_p, ses_lock_held);
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	return (rv);
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate CK_RV
C_DecryptFinal(CK_SESSION_HANDLE hSession,CK_BYTE_PTR pLastPart,CK_ULONG_PTR pulLastPartLen)4277c478bd9Sstevel@tonic-gate C_DecryptFinal(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pLastPart,
4287c478bd9Sstevel@tonic-gate     CK_ULONG_PTR pulLastPartLen)
4297c478bd9Sstevel@tonic-gate {
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	CK_RV rv;
4327c478bd9Sstevel@tonic-gate 	kernel_session_t *session_p;
4337c478bd9Sstevel@tonic-gate 	boolean_t ses_lock_held = B_FALSE;
4347c478bd9Sstevel@tonic-gate 	crypto_decrypt_final_t decrypt_final;
4357c478bd9Sstevel@tonic-gate 	int r;
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	if (!kernel_initialized)
4387c478bd9Sstevel@tonic-gate 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	/* Obtain the session pointer. */
4417c478bd9Sstevel@tonic-gate 	rv = handle2session(hSession, &session_p);
4427c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK)
4437c478bd9Sstevel@tonic-gate 		return (rv);
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	if (pulLastPartLen == NULL) {
4467c478bd9Sstevel@tonic-gate 		rv = CKR_ARGUMENTS_BAD;
4477c478bd9Sstevel@tonic-gate 		goto clean_exit;
4487c478bd9Sstevel@tonic-gate 	}
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&session_p->session_mutex);
4517c478bd9Sstevel@tonic-gate 	ses_lock_held = B_TRUE;
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	/*
4547c478bd9Sstevel@tonic-gate 	 * Application must call C_DecryptInit before calling
4557c478bd9Sstevel@tonic-gate 	 * C_DecryptFinal.
4567c478bd9Sstevel@tonic-gate 	 */
4577c478bd9Sstevel@tonic-gate 	if (!(session_p->decrypt.flags & CRYPTO_OPERATION_ACTIVE)) {
4587c478bd9Sstevel@tonic-gate 		REFRELE(session_p, ses_lock_held);
4597c478bd9Sstevel@tonic-gate 		return (CKR_OPERATION_NOT_INITIALIZED);
4607c478bd9Sstevel@tonic-gate 	}
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	decrypt_final.df_session = session_p->k_session;
4637c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&session_p->session_mutex);
4647c478bd9Sstevel@tonic-gate 	ses_lock_held = B_FALSE;
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	decrypt_final.df_datalen = *pulLastPartLen;
4677c478bd9Sstevel@tonic-gate 	decrypt_final.df_databuf = (char *)pLastPart;
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	while ((r = ioctl(kernel_fd, CRYPTO_DECRYPT_FINAL,
4707c478bd9Sstevel@tonic-gate 	    &decrypt_final)) < 0) {
4717c478bd9Sstevel@tonic-gate 		if (errno != EINTR)
4727c478bd9Sstevel@tonic-gate 			break;
4737c478bd9Sstevel@tonic-gate 	}
4747c478bd9Sstevel@tonic-gate 	if (r < 0) {
4757c478bd9Sstevel@tonic-gate 		rv = CKR_FUNCTION_FAILED;
4767c478bd9Sstevel@tonic-gate 	} else {
4777c478bd9Sstevel@tonic-gate 		rv = crypto2pkcs11_error_number(decrypt_final.df_return_value);
4787c478bd9Sstevel@tonic-gate 	}
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	if (rv == CKR_OK || rv == CKR_BUFFER_TOO_SMALL)
4817c478bd9Sstevel@tonic-gate 		*pulLastPartLen = decrypt_final.df_datalen;
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	if (rv == CKR_BUFFER_TOO_SMALL ||
4847c478bd9Sstevel@tonic-gate 	    (rv == CKR_OK && pLastPart == NULL)) {
4857c478bd9Sstevel@tonic-gate 		/*
4867c478bd9Sstevel@tonic-gate 		 * We will not terminate the active decrypt operation flag,
4877c478bd9Sstevel@tonic-gate 		 * when the application-supplied buffer is too small, or
4887c478bd9Sstevel@tonic-gate 		 * the application asks for the length of buffer to hold
4897c478bd9Sstevel@tonic-gate 		 * the plaintext.
4907c478bd9Sstevel@tonic-gate 		 */
4917c478bd9Sstevel@tonic-gate 		REFRELE(session_p, ses_lock_held);
4927c478bd9Sstevel@tonic-gate 		return (rv);
4937c478bd9Sstevel@tonic-gate 	}
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate clean_exit:
4967c478bd9Sstevel@tonic-gate 	/* Terminates the active decrypt operation */
4977c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&session_p->session_mutex);
4987c478bd9Sstevel@tonic-gate 	session_p->decrypt.flags = 0;
4997c478bd9Sstevel@tonic-gate 	ses_lock_held = B_TRUE;
5007c478bd9Sstevel@tonic-gate 	REFRELE(session_p, ses_lock_held);
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	return (rv);
5037c478bd9Sstevel@tonic-gate }
504