xref: /illumos-gate/usr/src/lib/pkcs11/pkcs11_kernel/common/kernelDecrypt.c (revision fb2612809ed5f2cb9109db768e63d61f6659f71b)
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 /*
226d2259e1SDan OpenSolaris Anderson  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
24*fb261280SJason King  * Copyright 2018, Joyent, Inc.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <pthread.h>
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <errno.h>
307c478bd9Sstevel@tonic-gate #include <sys/crypto/ioctl.h>
317c478bd9Sstevel@tonic-gate #include <security/cryptoki.h>
327c478bd9Sstevel@tonic-gate #include "kernelGlobal.h"
337c478bd9Sstevel@tonic-gate #include "kernelSession.h"
347c478bd9Sstevel@tonic-gate #include "kernelObject.h"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate /*
387c478bd9Sstevel@tonic-gate  * Real decryptInit work. The caller doesn't hold the session lock.
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate CK_RV
kernel_decrypt_init(kernel_session_t * session_p,kernel_object_t * key_p,CK_MECHANISM_PTR pMechanism)417c478bd9Sstevel@tonic-gate kernel_decrypt_init(kernel_session_t *session_p, kernel_object_t *key_p,
427c478bd9Sstevel@tonic-gate     CK_MECHANISM_PTR pMechanism)
437c478bd9Sstevel@tonic-gate {
447c478bd9Sstevel@tonic-gate 	CK_RV rv;
457c478bd9Sstevel@tonic-gate 	crypto_decrypt_init_t decrypt_init;
467c478bd9Sstevel@tonic-gate 	crypto_mech_type_t k_mech_type;
477c478bd9Sstevel@tonic-gate 	boolean_t ses_lock_held = B_FALSE;
487c478bd9Sstevel@tonic-gate 	int r;
49*fb261280SJason King 	CK_AES_CCM_PARAMS ccm_params = { 0 };
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate 	/* Check to see if key object allows for decryption. */
527c478bd9Sstevel@tonic-gate 	if (key_p->is_lib_obj && !(key_p->bool_attr_mask & DECRYPT_BOOL_ON)) {
537c478bd9Sstevel@tonic-gate 		return (CKR_KEY_TYPE_INCONSISTENT);
547c478bd9Sstevel@tonic-gate 	}
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 	/* Get the kernel's internal mechanism number. */
577c478bd9Sstevel@tonic-gate 	rv = kernel_mech(pMechanism->mechanism, &k_mech_type);
587c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK)
597c478bd9Sstevel@tonic-gate 		return (rv);
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&session_p->session_mutex);
627c478bd9Sstevel@tonic-gate 	ses_lock_held = B_TRUE;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	/*
657c478bd9Sstevel@tonic-gate 	 * This active flag will remain ON until application calls either
667c478bd9Sstevel@tonic-gate 	 * C_Decrypt or C_DecryptFinal to actually obtain the final piece
677c478bd9Sstevel@tonic-gate 	 * of plaintext.
687c478bd9Sstevel@tonic-gate 	 */
697c478bd9Sstevel@tonic-gate 	session_p->decrypt.flags = CRYPTO_OPERATION_ACTIVE;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	/* set up key data */
727c478bd9Sstevel@tonic-gate 	if (!key_p->is_lib_obj) {
737c478bd9Sstevel@tonic-gate 		decrypt_init.di_key.ck_format = CRYPTO_KEY_REFERENCE;
747c478bd9Sstevel@tonic-gate 		decrypt_init.di_key.ck_obj_id = key_p->k_handle;
757c478bd9Sstevel@tonic-gate 	} else {
767c478bd9Sstevel@tonic-gate 		if (key_p->class == CKO_SECRET_KEY) {
777c478bd9Sstevel@tonic-gate 			decrypt_init.di_key.ck_format = CRYPTO_KEY_RAW;
787c478bd9Sstevel@tonic-gate 			decrypt_init.di_key.ck_data =
797c478bd9Sstevel@tonic-gate 			    get_symmetric_key_value(key_p);
807c478bd9Sstevel@tonic-gate 			if (decrypt_init.di_key.ck_data == NULL) {
817c478bd9Sstevel@tonic-gate 				rv = CKR_HOST_MEMORY;
827c478bd9Sstevel@tonic-gate 				goto clean_exit;
837c478bd9Sstevel@tonic-gate 			}
847c478bd9Sstevel@tonic-gate 			/* KEF key lengths are expressed in bits */
857c478bd9Sstevel@tonic-gate 			decrypt_init.di_key.ck_length =
867c478bd9Sstevel@tonic-gate 			    OBJ_SEC(key_p)->sk_value_len << 3;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 		} else if (key_p->key_type == CKK_RSA) {
897c478bd9Sstevel@tonic-gate 			if (get_rsa_private_key(key_p, &decrypt_init.di_key) !=
907c478bd9Sstevel@tonic-gate 			    CKR_OK) {
917c478bd9Sstevel@tonic-gate 				rv = CKR_HOST_MEMORY;
927c478bd9Sstevel@tonic-gate 				goto clean_exit;
937c478bd9Sstevel@tonic-gate 			}
947c478bd9Sstevel@tonic-gate 		} else {
957c478bd9Sstevel@tonic-gate 			rv = CKR_KEY_TYPE_INCONSISTENT;
967c478bd9Sstevel@tonic-gate 			goto clean_exit;
977c478bd9Sstevel@tonic-gate 		}
987c478bd9Sstevel@tonic-gate 	}
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	decrypt_init.di_session = session_p->k_session;
10187fa5c53Smcpowers 	session_p->decrypt.mech = *pMechanism;
1026d2259e1SDan OpenSolaris Anderson 
1036d2259e1SDan OpenSolaris Anderson 	/* Cache this capability value for efficiency */
1046d2259e1SDan OpenSolaris Anderson 	if (INPLACE_MECHANISM(session_p->decrypt.mech.mechanism)) {
1056d2259e1SDan OpenSolaris Anderson 		session_p->decrypt.flags |= CRYPTO_OPERATION_INPLACE_OK;
1066d2259e1SDan OpenSolaris Anderson 	}
1077c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&session_p->session_mutex);
1086d2259e1SDan OpenSolaris Anderson 
1097c478bd9Sstevel@tonic-gate 	ses_lock_held = B_FALSE;
1107c478bd9Sstevel@tonic-gate 	decrypt_init.di_mech.cm_type = k_mech_type;
1117c478bd9Sstevel@tonic-gate 	decrypt_init.di_mech.cm_param = pMechanism->pParameter;
1127c478bd9Sstevel@tonic-gate 	decrypt_init.di_mech.cm_param_len = pMechanism->ulParameterLen;
1137c478bd9Sstevel@tonic-gate 
114*fb261280SJason King 	/*
115*fb261280SJason King 	 * PKCS#11 uses CK_CCM_PARAMS as its mechanism parameter, while the
116*fb261280SJason King 	 * kernel uses CK_AES_CCM_PARAMS.  Unlike
117*fb261280SJason King 	 * CK_GCM_PARAMS / CK_AES_GCM_PARAMS, the two definitions are not
118*fb261280SJason King 	 * equivalent -- the fields are defined in different orders, so
119*fb261280SJason King 	 * we must translate.
120*fb261280SJason King 	 */
121*fb261280SJason King 	if (session_p->decrypt.mech.mechanism == CKM_AES_CCM) {
122*fb261280SJason King 		if (pMechanism->ulParameterLen != sizeof (CK_CCM_PARAMS)) {
123*fb261280SJason King 			rv = CKR_MECHANISM_PARAM_INVALID;
124*fb261280SJason King 			goto clean_exit;
125*fb261280SJason King 		}
126*fb261280SJason King 		p11_to_kernel_ccm_params(pMechanism->pParameter, &ccm_params);
127*fb261280SJason King 		decrypt_init.di_mech.cm_param = (caddr_t)&ccm_params;
128*fb261280SJason King 		decrypt_init.di_mech.cm_param_len = sizeof (ccm_params);
129*fb261280SJason King 	}
130*fb261280SJason King 
1317c478bd9Sstevel@tonic-gate 	while ((r = ioctl(kernel_fd, CRYPTO_DECRYPT_INIT, &decrypt_init)) < 0) {
1327c478bd9Sstevel@tonic-gate 		if (errno != EINTR)
1337c478bd9Sstevel@tonic-gate 			break;
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 	if (r < 0) {
1367c478bd9Sstevel@tonic-gate 		rv = CKR_FUNCTION_FAILED;
1377c478bd9Sstevel@tonic-gate 	} else {
1387c478bd9Sstevel@tonic-gate 		rv = crypto2pkcs11_error_number(decrypt_init.di_return_value);
1397c478bd9Sstevel@tonic-gate 	}
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	/* Free memory allocated for decrypt_init.di_key */
1427c478bd9Sstevel@tonic-gate 	if (key_p->is_lib_obj) {
1437c478bd9Sstevel@tonic-gate 		if (key_p->class == CKO_SECRET_KEY) {
1447c478bd9Sstevel@tonic-gate 			free(decrypt_init.di_key.ck_data);
1457c478bd9Sstevel@tonic-gate 		} else if (key_p->key_type == CKK_RSA) {
1467c478bd9Sstevel@tonic-gate 			free_key_attributes(&decrypt_init.di_key);
1477c478bd9Sstevel@tonic-gate 		}
1487c478bd9Sstevel@tonic-gate 	}
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate clean_exit:
151*fb261280SJason King 	/*
152*fb261280SJason King 	 * ccm_params does not contain any key material -- just lengths and
153*fb261280SJason King 	 * pointers, therefore it does not need to be zeroed on exit.
154*fb261280SJason King 	 */
1557c478bd9Sstevel@tonic-gate 	if (!ses_lock_held) {
1567c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_lock(&session_p->session_mutex);
1577c478bd9Sstevel@tonic-gate 		ses_lock_held = B_TRUE;
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK)
1617c478bd9Sstevel@tonic-gate 		session_p->decrypt.flags &= ~CRYPTO_OPERATION_ACTIVE;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	if (ses_lock_held) {
1647c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&session_p->session_mutex);
1657c478bd9Sstevel@tonic-gate 		ses_lock_held = B_FALSE;
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	return (rv);
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate CK_RV
C_DecryptInit(CK_SESSION_HANDLE hSession,CK_MECHANISM_PTR pMechanism,CK_OBJECT_HANDLE hKey)1727c478bd9Sstevel@tonic-gate C_DecryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
1737c478bd9Sstevel@tonic-gate     CK_OBJECT_HANDLE hKey)
1747c478bd9Sstevel@tonic-gate {
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	CK_RV rv;
1777c478bd9Sstevel@tonic-gate 	kernel_session_t *session_p;
1787c478bd9Sstevel@tonic-gate 	kernel_object_t	*key_p;
1797c478bd9Sstevel@tonic-gate 	boolean_t ses_lock_held = B_FALSE;
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	if (!kernel_initialized)
1827c478bd9Sstevel@tonic-gate 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	if (pMechanism == NULL) {
1857c478bd9Sstevel@tonic-gate 		return (CKR_ARGUMENTS_BAD);
1867c478bd9Sstevel@tonic-gate 	}
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	/* Obtain the session pointer. */
1897c478bd9Sstevel@tonic-gate 	rv = handle2session(hSession, &session_p);
1907c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK)
1917c478bd9Sstevel@tonic-gate 		return (rv);
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	/* Obtain the object pointer. */
1947c478bd9Sstevel@tonic-gate 	HANDLE2OBJECT(hKey, key_p, rv);
1957c478bd9Sstevel@tonic-gate 	if (rv == CKR_OK) {
1967c478bd9Sstevel@tonic-gate 		rv = kernel_decrypt_init(session_p, key_p, pMechanism);
19701223cbaSmcpowers 		OBJ_REFRELE(key_p);
1987c478bd9Sstevel@tonic-gate 	}
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	REFRELE(session_p, ses_lock_held);
2017c478bd9Sstevel@tonic-gate 	return (rv);
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate /*
2077c478bd9Sstevel@tonic-gate  * Real decrypt work. The caller doesn't hold the session lock.
2087c478bd9Sstevel@tonic-gate  */
2097c478bd9Sstevel@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)2107c478bd9Sstevel@tonic-gate kernel_decrypt(kernel_session_t *session_p, CK_BYTE_PTR pEncryptedData,
2117c478bd9Sstevel@tonic-gate     CK_ULONG ulEncryptedData, CK_BYTE_PTR pData, CK_ULONG_PTR pulDataLen)
2127c478bd9Sstevel@tonic-gate {
2137c478bd9Sstevel@tonic-gate 	crypto_decrypt_t decrypt;
2147c478bd9Sstevel@tonic-gate 	boolean_t ses_lock_held = B_FALSE;
21587fa5c53Smcpowers 	boolean_t inplace;
2167c478bd9Sstevel@tonic-gate 	CK_RV rv;
2177c478bd9Sstevel@tonic-gate 	int r;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&session_p->session_mutex);
2207c478bd9Sstevel@tonic-gate 	ses_lock_held = B_TRUE;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	/* Application must call C_DecryptInit before calling C_Decrypt. */
2237c478bd9Sstevel@tonic-gate 	if (!(session_p->decrypt.flags & CRYPTO_OPERATION_ACTIVE)) {
2247c478bd9Sstevel@tonic-gate 		rv = CKR_OPERATION_NOT_INITIALIZED;
2257c478bd9Sstevel@tonic-gate 		goto clean_exit;
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	/*
2297c478bd9Sstevel@tonic-gate 	 * C_Decrypt must be called without intervening C_DecryptUpdate
2307c478bd9Sstevel@tonic-gate 	 * calls.
2317c478bd9Sstevel@tonic-gate 	 */
2327c478bd9Sstevel@tonic-gate 	if (session_p->decrypt.flags & CRYPTO_OPERATION_UPDATE) {
2337c478bd9Sstevel@tonic-gate 		/*
2347c478bd9Sstevel@tonic-gate 		 * C_Decrypt cannot be used to terminate a multiple-part
2357c478bd9Sstevel@tonic-gate 		 * operation, so we'll leave the active decrypt operation
2367c478bd9Sstevel@tonic-gate 		 * flag on and let the application continue with the
2377c478bd9Sstevel@tonic-gate 		 * decrypt update operation.
2387c478bd9Sstevel@tonic-gate 		 */
2397c478bd9Sstevel@tonic-gate 		rv = CKR_FUNCTION_FAILED;
2407c478bd9Sstevel@tonic-gate 		goto clean_exit;
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	decrypt.cd_session = session_p->k_session;
24487fa5c53Smcpowers 
24587fa5c53Smcpowers 	/*
24687fa5c53Smcpowers 	 * Certain mechanisms, where the length of the plaintext is
24787fa5c53Smcpowers 	 * same as the transformed ciphertext, can be optimized
24887fa5c53Smcpowers 	 * by the kernel into an in-place operation. Unfortunately,
24987fa5c53Smcpowers 	 * some applications use a plaintext buffer that is larger
25087fa5c53Smcpowers 	 * than it needs to be. We fix that here.
25187fa5c53Smcpowers 	 */
2526d2259e1SDan OpenSolaris Anderson 	inplace = (session_p->decrypt.flags & CRYPTO_OPERATION_INPLACE_OK) != 0;
2536d2259e1SDan OpenSolaris Anderson 
25487fa5c53Smcpowers 	if (ulEncryptedData < *pulDataLen && inplace) {
25587fa5c53Smcpowers 		decrypt.cd_datalen = ulEncryptedData;
25687fa5c53Smcpowers 	} else {
25787fa5c53Smcpowers 		decrypt.cd_datalen = *pulDataLen;
25887fa5c53Smcpowers 	}
2597c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&session_p->session_mutex);
2607c478bd9Sstevel@tonic-gate 	ses_lock_held = B_FALSE;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	decrypt.cd_databuf = (char *)pData;
2637c478bd9Sstevel@tonic-gate 	decrypt.cd_encrlen = ulEncryptedData;
2647c478bd9Sstevel@tonic-gate 	decrypt.cd_encrbuf = (char *)pEncryptedData;
2656d2259e1SDan OpenSolaris Anderson 	decrypt.cd_flags =
2666d2259e1SDan OpenSolaris Anderson 	    ((inplace && (pData != NULL)) || (pData == pEncryptedData)) &&
2676d2259e1SDan OpenSolaris Anderson 	    (decrypt.cd_datalen == decrypt.cd_encrlen) ?
26887fa5c53Smcpowers 	    CRYPTO_INPLACE_OPERATION : 0;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	while ((r = ioctl(kernel_fd, CRYPTO_DECRYPT, &decrypt)) < 0) {
2717c478bd9Sstevel@tonic-gate 		if (errno != EINTR)
2727c478bd9Sstevel@tonic-gate 			break;
2737c478bd9Sstevel@tonic-gate 	}
2747c478bd9Sstevel@tonic-gate 	if (r < 0) {
2757c478bd9Sstevel@tonic-gate 		rv = CKR_FUNCTION_FAILED;
2767c478bd9Sstevel@tonic-gate 	} else {
2777c478bd9Sstevel@tonic-gate 		rv = crypto2pkcs11_error_number(decrypt.cd_return_value);
2787c478bd9Sstevel@tonic-gate 	}
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	if (rv == CKR_OK || rv == CKR_BUFFER_TOO_SMALL)
2817c478bd9Sstevel@tonic-gate 		*pulDataLen = decrypt.cd_datalen;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate clean_exit:
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	if (ses_lock_held)
2867c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&session_p->session_mutex);
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	return (rv);
2897c478bd9Sstevel@tonic-gate }
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@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)2927c478bd9Sstevel@tonic-gate C_Decrypt(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pEncryptedData,
2937c478bd9Sstevel@tonic-gate     CK_ULONG ulEncryptedData, CK_BYTE_PTR pData, CK_ULONG_PTR pulDataLen)
2947c478bd9Sstevel@tonic-gate {
2957c478bd9Sstevel@tonic-gate 	CK_RV rv;
2967c478bd9Sstevel@tonic-gate 	kernel_session_t *session_p;
2977c478bd9Sstevel@tonic-gate 	boolean_t ses_lock_held = B_FALSE;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	if (!kernel_initialized)
3007c478bd9Sstevel@tonic-gate 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	/* Obtain the session pointer. */
3037c478bd9Sstevel@tonic-gate 	rv = handle2session(hSession, &session_p);
3047c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK)
3057c478bd9Sstevel@tonic-gate 		return (rv);
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	/*
3087c478bd9Sstevel@tonic-gate 	 * No need to check pData because application might
3097c478bd9Sstevel@tonic-gate 	 * just want to know the length of decrypted data.
3107c478bd9Sstevel@tonic-gate 	 */
3117c478bd9Sstevel@tonic-gate 	if (pulDataLen == NULL) {
3127c478bd9Sstevel@tonic-gate 		rv = CKR_ARGUMENTS_BAD;
3137c478bd9Sstevel@tonic-gate 		goto clean_exit;
3147c478bd9Sstevel@tonic-gate 	}
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	rv = kernel_decrypt(session_p, pEncryptedData, ulEncryptedData, pData,
3177c478bd9Sstevel@tonic-gate 	    pulDataLen);
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	if ((rv == CKR_BUFFER_TOO_SMALL) ||
3207c478bd9Sstevel@tonic-gate 	    (rv == CKR_OK && pData == NULL)) {
3217c478bd9Sstevel@tonic-gate 		/*
3227c478bd9Sstevel@tonic-gate 		 * We will not terminate the active decrypt operation flag,
3237c478bd9Sstevel@tonic-gate 		 * when the application-supplied buffer is too small, or
3247c478bd9Sstevel@tonic-gate 		 * the application asks for the length of buffer to hold
3257c478bd9Sstevel@tonic-gate 		 * the plaintext.
3267c478bd9Sstevel@tonic-gate 		 */
3277c478bd9Sstevel@tonic-gate 		REFRELE(session_p, ses_lock_held);
3287c478bd9Sstevel@tonic-gate 		return (rv);
3297c478bd9Sstevel@tonic-gate 	}
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate clean_exit:
3327c478bd9Sstevel@tonic-gate 	/*
3337c478bd9Sstevel@tonic-gate 	 * Terminates the active decrypt operation.
3347c478bd9Sstevel@tonic-gate 	 * Application needs to call C_DecryptInit again for next
3357c478bd9Sstevel@tonic-gate 	 * decrypt operation.
3367c478bd9Sstevel@tonic-gate 	 */
3377c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&session_p->session_mutex);
3387c478bd9Sstevel@tonic-gate 	session_p->decrypt.flags = 0;
3397c478bd9Sstevel@tonic-gate 	ses_lock_held = B_TRUE;
3407c478bd9Sstevel@tonic-gate 	REFRELE(session_p, ses_lock_held);
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	return (rv);
3437c478bd9Sstevel@tonic-gate }
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@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)3477c478bd9Sstevel@tonic-gate C_DecryptUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pEncryptedPart,
3487c478bd9Sstevel@tonic-gate     CK_ULONG ulEncryptedPartLen, CK_BYTE_PTR pPart,
3497c478bd9Sstevel@tonic-gate     CK_ULONG_PTR pulPartLen)
3507c478bd9Sstevel@tonic-gate {
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	CK_RV rv;
3537c478bd9Sstevel@tonic-gate 	kernel_session_t *session_p;
3547c478bd9Sstevel@tonic-gate 	boolean_t ses_lock_held = B_FALSE;
3556d2259e1SDan OpenSolaris Anderson 	boolean_t inplace;
3567c478bd9Sstevel@tonic-gate 	crypto_decrypt_update_t decrypt_update;
3577c478bd9Sstevel@tonic-gate 	int r;
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	if (!kernel_initialized)
3607c478bd9Sstevel@tonic-gate 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	/* Obtain the session pointer. */
3637c478bd9Sstevel@tonic-gate 	rv = handle2session(hSession, &session_p);
3647c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK)
3657c478bd9Sstevel@tonic-gate 		return (rv);
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	if (pEncryptedPart == NULL) {
3687c478bd9Sstevel@tonic-gate 		rv = CKR_ARGUMENTS_BAD;
3697c478bd9Sstevel@tonic-gate 		goto clean_exit;
3707c478bd9Sstevel@tonic-gate 	}
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	/*
3737c478bd9Sstevel@tonic-gate 	 * Only check if pulPartLen is NULL.
3747c478bd9Sstevel@tonic-gate 	 * No need to check if pPart is NULL because application
3757c478bd9Sstevel@tonic-gate 	 * might just ask for the length of buffer to hold the
3767c478bd9Sstevel@tonic-gate 	 * recovered data.
3777c478bd9Sstevel@tonic-gate 	 */
3787c478bd9Sstevel@tonic-gate 	if (pulPartLen == NULL) {
3797c478bd9Sstevel@tonic-gate 		rv = CKR_ARGUMENTS_BAD;
3807c478bd9Sstevel@tonic-gate 		goto clean_exit;
3817c478bd9Sstevel@tonic-gate 	}
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&session_p->session_mutex);
3847c478bd9Sstevel@tonic-gate 	ses_lock_held = B_TRUE;
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	/*
3877c478bd9Sstevel@tonic-gate 	 * Application must call C_DecryptInit before calling
3887c478bd9Sstevel@tonic-gate 	 * C_DecryptUpdate.
3897c478bd9Sstevel@tonic-gate 	 */
3907c478bd9Sstevel@tonic-gate 	if (!(session_p->decrypt.flags & CRYPTO_OPERATION_ACTIVE)) {
3917c478bd9Sstevel@tonic-gate 		REFRELE(session_p, ses_lock_held);
3927c478bd9Sstevel@tonic-gate 		return (CKR_OPERATION_NOT_INITIALIZED);
3937c478bd9Sstevel@tonic-gate 	}
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	session_p->decrypt.flags |= CRYPTO_OPERATION_UPDATE;
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	decrypt_update.du_session = session_p->k_session;
3987c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&session_p->session_mutex);
3997c478bd9Sstevel@tonic-gate 	ses_lock_held = B_FALSE;
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	decrypt_update.du_datalen = *pulPartLen;
4027c478bd9Sstevel@tonic-gate 	decrypt_update.du_databuf = (char *)pPart;
4037c478bd9Sstevel@tonic-gate 	decrypt_update.du_encrlen = ulEncryptedPartLen;
4047c478bd9Sstevel@tonic-gate 	decrypt_update.du_encrbuf = (char *)pEncryptedPart;
4057c478bd9Sstevel@tonic-gate 
4066d2259e1SDan OpenSolaris Anderson 	inplace = (session_p->decrypt.flags & CRYPTO_OPERATION_INPLACE_OK) != 0;
4076d2259e1SDan OpenSolaris Anderson 	decrypt_update.du_flags =
4086d2259e1SDan OpenSolaris Anderson 	    ((inplace && (pPart != NULL)) || (pPart == pEncryptedPart)) &&
4096d2259e1SDan OpenSolaris Anderson 	    (decrypt_update.du_datalen == decrypt_update.du_encrlen) ?
4106d2259e1SDan OpenSolaris Anderson 	    CRYPTO_INPLACE_OPERATION : 0;
4116d2259e1SDan OpenSolaris Anderson 
4127c478bd9Sstevel@tonic-gate 	while ((r = ioctl(kernel_fd, CRYPTO_DECRYPT_UPDATE,
4137c478bd9Sstevel@tonic-gate 	    &decrypt_update)) < 0) {
4147c478bd9Sstevel@tonic-gate 		if (errno != EINTR)
4157c478bd9Sstevel@tonic-gate 			break;
4167c478bd9Sstevel@tonic-gate 	}
4177c478bd9Sstevel@tonic-gate 	if (r < 0) {
4187c478bd9Sstevel@tonic-gate 		rv = CKR_FUNCTION_FAILED;
4197c478bd9Sstevel@tonic-gate 	} else {
4207c478bd9Sstevel@tonic-gate 		rv = crypto2pkcs11_error_number(
4217c478bd9Sstevel@tonic-gate 		    decrypt_update.du_return_value);
4227c478bd9Sstevel@tonic-gate 	}
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	/*
4257c478bd9Sstevel@tonic-gate 	 * If CKR_OK or CKR_BUFFER_TOO_SMALL, set the output length.
4267c478bd9Sstevel@tonic-gate 	 * We don't terminate the current decryption operation.
4277c478bd9Sstevel@tonic-gate 	 */
4287c478bd9Sstevel@tonic-gate 	if (rv == CKR_OK || rv == CKR_BUFFER_TOO_SMALL) {
4297c478bd9Sstevel@tonic-gate 		*pulPartLen = decrypt_update.du_datalen;
4307c478bd9Sstevel@tonic-gate 		REFRELE(session_p, ses_lock_held);
4317c478bd9Sstevel@tonic-gate 		return (rv);
4327c478bd9Sstevel@tonic-gate 	}
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate clean_exit:
4357c478bd9Sstevel@tonic-gate 	/*
4367c478bd9Sstevel@tonic-gate 	 * After an error occurred, terminate the current decrypt
4377c478bd9Sstevel@tonic-gate 	 * operation by resetting the active and update flags.
4387c478bd9Sstevel@tonic-gate 	 */
4397c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&session_p->session_mutex);
4407c478bd9Sstevel@tonic-gate 	session_p->decrypt.flags = 0;
4417c478bd9Sstevel@tonic-gate 	ses_lock_held = B_TRUE;
4427c478bd9Sstevel@tonic-gate 	REFRELE(session_p, ses_lock_held);
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	return (rv);
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate CK_RV
C_DecryptFinal(CK_SESSION_HANDLE hSession,CK_BYTE_PTR pLastPart,CK_ULONG_PTR pulLastPartLen)4497c478bd9Sstevel@tonic-gate C_DecryptFinal(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pLastPart,
4507c478bd9Sstevel@tonic-gate     CK_ULONG_PTR pulLastPartLen)
4517c478bd9Sstevel@tonic-gate {
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	CK_RV rv;
4547c478bd9Sstevel@tonic-gate 	kernel_session_t *session_p;
4557c478bd9Sstevel@tonic-gate 	boolean_t ses_lock_held = B_FALSE;
4567c478bd9Sstevel@tonic-gate 	crypto_decrypt_final_t decrypt_final;
4577c478bd9Sstevel@tonic-gate 	int r;
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	if (!kernel_initialized)
4607c478bd9Sstevel@tonic-gate 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	/* Obtain the session pointer. */
4637c478bd9Sstevel@tonic-gate 	rv = handle2session(hSession, &session_p);
4647c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK)
4657c478bd9Sstevel@tonic-gate 		return (rv);
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	if (pulLastPartLen == NULL) {
4687c478bd9Sstevel@tonic-gate 		rv = CKR_ARGUMENTS_BAD;
4697c478bd9Sstevel@tonic-gate 		goto clean_exit;
4707c478bd9Sstevel@tonic-gate 	}
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&session_p->session_mutex);
4737c478bd9Sstevel@tonic-gate 	ses_lock_held = B_TRUE;
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	/*
4767c478bd9Sstevel@tonic-gate 	 * Application must call C_DecryptInit before calling
4777c478bd9Sstevel@tonic-gate 	 * C_DecryptFinal.
4787c478bd9Sstevel@tonic-gate 	 */
4797c478bd9Sstevel@tonic-gate 	if (!(session_p->decrypt.flags & CRYPTO_OPERATION_ACTIVE)) {
4807c478bd9Sstevel@tonic-gate 		REFRELE(session_p, ses_lock_held);
4817c478bd9Sstevel@tonic-gate 		return (CKR_OPERATION_NOT_INITIALIZED);
4827c478bd9Sstevel@tonic-gate 	}
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	decrypt_final.df_session = session_p->k_session;
4857c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&session_p->session_mutex);
4867c478bd9Sstevel@tonic-gate 	ses_lock_held = B_FALSE;
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	decrypt_final.df_datalen = *pulLastPartLen;
4897c478bd9Sstevel@tonic-gate 	decrypt_final.df_databuf = (char *)pLastPart;
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	while ((r = ioctl(kernel_fd, CRYPTO_DECRYPT_FINAL,
4927c478bd9Sstevel@tonic-gate 	    &decrypt_final)) < 0) {
4937c478bd9Sstevel@tonic-gate 		if (errno != EINTR)
4947c478bd9Sstevel@tonic-gate 			break;
4957c478bd9Sstevel@tonic-gate 	}
4967c478bd9Sstevel@tonic-gate 	if (r < 0) {
4977c478bd9Sstevel@tonic-gate 		rv = CKR_FUNCTION_FAILED;
4987c478bd9Sstevel@tonic-gate 	} else {
4997c478bd9Sstevel@tonic-gate 		rv = crypto2pkcs11_error_number(decrypt_final.df_return_value);
5007c478bd9Sstevel@tonic-gate 	}
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	if (rv == CKR_OK || rv == CKR_BUFFER_TOO_SMALL)
5037c478bd9Sstevel@tonic-gate 		*pulLastPartLen = decrypt_final.df_datalen;
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	if (rv == CKR_BUFFER_TOO_SMALL ||
5067c478bd9Sstevel@tonic-gate 	    (rv == CKR_OK && pLastPart == NULL)) {
5077c478bd9Sstevel@tonic-gate 		/*
5087c478bd9Sstevel@tonic-gate 		 * We will not terminate the active decrypt operation flag,
5097c478bd9Sstevel@tonic-gate 		 * when the application-supplied buffer is too small, or
5107c478bd9Sstevel@tonic-gate 		 * the application asks for the length of buffer to hold
5117c478bd9Sstevel@tonic-gate 		 * the plaintext.
5127c478bd9Sstevel@tonic-gate 		 */
5137c478bd9Sstevel@tonic-gate 		REFRELE(session_p, ses_lock_held);
5147c478bd9Sstevel@tonic-gate 		return (rv);
5157c478bd9Sstevel@tonic-gate 	}
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate clean_exit:
5187c478bd9Sstevel@tonic-gate 	/* Terminates the active decrypt operation */
5197c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&session_p->session_mutex);
5207c478bd9Sstevel@tonic-gate 	session_p->decrypt.flags = 0;
5217c478bd9Sstevel@tonic-gate 	ses_lock_held = B_TRUE;
5227c478bd9Sstevel@tonic-gate 	REFRELE(session_p, ses_lock_held);
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	return (rv);
5257c478bd9Sstevel@tonic-gate }
526