unsigned char newdeskey[8];
unsigned char newdeskey[24];
pk11_CryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,		 CK_OBJECT_HANDLE hKey, CK_ATTRIBUTE_TYPE etype,		 PK11ContextType contextType, PRBool isEncrypt){    PK11Session *session;    PK11Object *key;    PK11SessionContext *context;    PK11Attribute *att;    CK_RC2_CBC_PARAMS *rc2_param;#if NSS_SOFTOKEN_DOES_RC5    CK_RC5_CBC_PARAMS *rc5_param;    SECItem rc5Key;#endif    CK_KEY_TYPE key_type;    CK_RV crv = CKR_OK;    unsigned effectiveKeyLength;    unsigned char newdeskey[8];    PRBool useNewKey=PR_FALSE;    int t;    crv = pk11_MechAllowsOperation(pMechanism->mechanism, etype);    if (crv != CKR_OK)     	return crv;    session = pk11_SessionFromHandle(hSession);    if (session == NULL) return CKR_SESSION_HANDLE_INVALID;    crv = pk11_InitGeneric(session,&context,contextType,&key,hKey,&key_type,    			isEncrypt ?CKO_PUBLIC_KEY:CKO_PRIVATE_KEY, etype);						    if (crv != CKR_OK) {	pk11_FreeSession(session);	return crv;    }    context->doPad = PR_FALSE;    switch(pMechanism->mechanism) {    case CKM_RSA_PKCS:    case CKM_RSA_X_509:	if (key_type != CKK_RSA) {	    crv = CKR_KEY_TYPE_INCONSISTENT;	    break;	}	context->multi = PR_FALSE;	context->cipherInfo =  isEncrypt ? 			(void *)pk11_GetPubKey(key,CKK_RSA,&crv) :				(void *)pk11_GetPrivKey(key,CKK_RSA,&crv);	if (context->cipherInfo == NULL) {	    break;	}	if (isEncrypt) {	    context->update = (PK11Cipher) 		(pMechanism->mechanism == CKM_RSA_X_509					? RSA_EncryptRaw : RSA_EncryptBlock);	} else {	    context->update = (PK11Cipher) 		(pMechanism->mechanism == CKM_RSA_X_509					? RSA_DecryptRaw : RSA_DecryptBlock);	}	context->destroy = pk11_Null;	break;    case CKM_RC2_CBC_PAD:	context->doPad = PR_TRUE;	/* fall thru */    case CKM_RC2_ECB:    case CKM_RC2_CBC:	context->blockSize = 8;	if (key_type != CKK_RC2) {	    crv = CKR_KEY_TYPE_INCONSISTENT;	    break;	}	att = pk11_FindAttribute(key,CKA_VALUE);	if (att == NULL) {	    crv = CKR_KEY_HANDLE_INVALID;	    break;	}	rc2_param = (CK_RC2_CBC_PARAMS *)pMechanism->pParameter;	effectiveKeyLength = (rc2_param->ulEffectiveBits+7)/8;	context->cipherInfo = 	    RC2_CreateContext((unsigned char*)att->attrib.pValue,			      att->attrib.ulValueLen, rc2_param->iv,			      pMechanism->mechanism == CKM_RC2_ECB ? NSS_RC2 :			      NSS_RC2_CBC,effectiveKeyLength);	pk11_FreeAttribute(att);	if (context->cipherInfo == NULL) {	    crv = CKR_HOST_MEMORY;	    break;	}	context->update = (PK11Cipher) (isEncrypt ? RC2_Encrypt : RC2_Decrypt);	context->destroy = (PK11Destroy) RC2_DestroyContext;	break;#if NSS_SOFTOKEN_DOES_RC5    case CKM_RC5_CBC_PAD:	context->doPad = PR_TRUE;	/* fall thru */    case CKM_RC5_ECB:    case CKM_RC5_CBC:	if (key_type != CKK_RC5) {	    crv = CKR_KEY_TYPE_INCONSISTENT;	    break;	}	att = pk11_FindAttribute(key,CKA_VALUE);	if (att == NULL) {	    crv = CKR_KEY_HANDLE_INVALID;	    break;	}	rc5_param = (CK_RC5_CBC_PARAMS *)pMechanism->pParameter;	context->blockSize = rc5_param->ulWordsize*2;	rc5Key.data = (unsigned char*)att->attrib.pValue;	rc5Key.len = att->attrib.ulValueLen;	context->cipherInfo = RC5_CreateContext(&rc5Key,rc5_param->ulRounds,	   rc5_param->ulWordsize,rc5_param->pIv,		 pMechanism->mechanism == CKM_RC5_ECB ? NSS_RC5 : NSS_RC5_CBC);	pk11_FreeAttribute(att);	if (context->cipherInfo == NULL) {	    crv = CKR_HOST_MEMORY;	    break;	}	context->update = (PK11Cipher) (isEncrypt ? RC5_Encrypt : RC5_Decrypt);	context->destroy = (PK11Destroy) RC5_DestroyContext;	break;#endif    case CKM_RC4:	if (key_type != CKK_RC4) {	    crv = CKR_KEY_TYPE_INCONSISTENT;	    break;	}	att = pk11_FindAttribute(key,CKA_VALUE);	if (att == NULL) {	    crv = CKR_KEY_HANDLE_INVALID;	    break;	}	context->cipherInfo = 	    RC4_CreateContext((unsigned char*)att->attrib.pValue,			      att->attrib.ulValueLen);	pk11_FreeAttribute(att);	if (context->cipherInfo == NULL) {	    crv = CKR_HOST_MEMORY;  /* WRONG !!! */	    break;	}	context->update = (PK11Cipher) (isEncrypt ? RC4_Encrypt : RC4_Decrypt);	context->destroy = (PK11Destroy) RC4_DestroyContext;	break;    case CKM_CDMF_CBC_PAD:	context->doPad = PR_TRUE;	/* fall thru */    case CKM_CDMF_ECB:    case CKM_CDMF_CBC:	if (key_type != CKK_CDMF) {	    crv = CKR_KEY_TYPE_INCONSISTENT;	    break;	}	t = (pMechanism->mechanism == CKM_CDMF_ECB) ? NSS_DES : NSS_DES_CBC;	useNewKey=PR_TRUE;	if (crv != CKR_OK) break;	goto finish_des;    case CKM_DES_ECB:	if (key_type != CKK_DES) {	    crv = CKR_KEY_TYPE_INCONSISTENT;	    break;	}	t = NSS_DES;	goto finish_des;    case CKM_DES_CBC_PAD:	context->doPad = PR_TRUE;	/* fall thru */    case CKM_DES_CBC:	if (key_type != CKK_DES) {	    crv = CKR_KEY_TYPE_INCONSISTENT;	    break;	}	t = NSS_DES_CBC;	goto finish_des;    case CKM_DES3_ECB:	if ((key_type != CKK_DES2) && (key_type != CKK_DES3)) {	    crv = CKR_KEY_TYPE_INCONSISTENT;	    break;	}	t = NSS_DES_EDE3;	goto finish_des;    case CKM_DES3_CBC_PAD:	context->doPad = PR_TRUE;	/* fall thru */    case CKM_DES3_CBC:	if ((key_type != CKK_DES2) && (key_type != CKK_DES3)) {	    crv = CKR_KEY_TYPE_INCONSISTENT;	    break;	}	t = NSS_DES_EDE3_CBC;finish_des:	context->blockSize = 8;	att = pk11_FindAttribute(key,CKA_VALUE);	if (att == NULL) {	    crv = CKR_KEY_HANDLE_INVALID;	    break;	}	if (useNewKey) {	    crv = pk11_cdmf2des((unsigned char*)att->attrib.pValue,newdeskey);	    if (crv != CKR_OK) {		pk11_FreeAttribute(att);		break;	     }	}	context->cipherInfo = DES_CreateContext(		useNewKey ? newdeskey : (unsigned char*)att->attrib.pValue,		(unsigned char*)pMechanism->pParameter,t, isEncrypt);	pk11_FreeAttribute(att);	if (context->cipherInfo == NULL) {	    crv = CKR_HOST_MEMORY;	    break;	}	context->update = (PK11Cipher) (isEncrypt ? DES_Encrypt : DES_Decrypt);	context->destroy = (PK11Destroy) DES_DestroyContext;	break;    case CKM_AES_CBC_PAD:	context->doPad = PR_TRUE;	/* fall thru */    case CKM_AES_ECB:    case CKM_AES_CBC:	context->blockSize = 16;	if (key_type != CKK_AES) {	    crv = CKR_KEY_TYPE_INCONSISTENT;	    break;	}	att = pk11_FindAttribute(key,CKA_VALUE);	if (att == NULL) {	    crv = CKR_KEY_HANDLE_INVALID;	    break;	}	context->cipherInfo = AES_CreateContext(	    (unsigned char*)att->attrib.pValue,	    (unsigned char*)pMechanism->pParameter,	    pMechanism->mechanism == CKM_AES_ECB ? NSS_AES : NSS_AES_CBC,	    isEncrypt, att->attrib.ulValueLen, 16);	pk11_FreeAttribute(att);	if (context->cipherInfo == NULL) {	    crv = CKR_HOST_MEMORY;	    break;	}	context->update = (PK11Cipher) (isEncrypt ? AES_Encrypt : AES_Decrypt);	context->destroy = (PK11Destroy) AES_DestroyContext;	break;    case CKM_NETSCAPE_AES_KEY_WRAP_PAD:    	context->doPad = PR_TRUE;	/* fall thru */    case CKM_NETSCAPE_AES_KEY_WRAP:	context->multi = PR_FALSE;	context->blockSize = 8;	if (key_type != CKK_AES) {	    crv = CKR_KEY_TYPE_INCONSISTENT;	    break;	}	att = pk11_FindAttribute(key,CKA_VALUE);	if (att == NULL) {	    crv = CKR_KEY_HANDLE_INVALID;	    break;	}	context->cipherInfo = AESKeyWrap_CreateContext(	    (unsigned char*)att->attrib.pValue,	    (unsigned char*)pMechanism->pParameter,	    isEncrypt, att->attrib.ulValueLen);	pk11_FreeAttribute(att);	if (context->cipherInfo == NULL) {	    crv = CKR_HOST_MEMORY;	    break;	}	context->update = (PK11Cipher) (isEncrypt ? AESKeyWrap_Encrypt 	                                          : AESKeyWrap_Decrypt);	context->destroy = (PK11Destroy) AESKeyWrap_DestroyContext;	break;    default:	crv = CKR_MECHANISM_INVALID;	break;    }    if (crv != CKR_OK) {        pk11_FreeContext(context);	pk11_FreeSession(session);	return crv;    }    pk11_SetContextByType(session, contextType, context);    pk11_FreeSession(session);    return CKR_OK;}