CK_MECHANISM_TYPE mech; PK11SymKey *key=NULL; SECItem *param=NULL; SECItem *iv=NULL; PK11Context *context=NULL; CK_ATTRIBUTE_TYPE op; jobject contextObj;  PR_ASSERT(env!=NULL && clazz!=NULL && keyObj!=NULL && algObj!=NULL);   mech = JSS_getPK11MechFromAlg(env, algObj); if(mech == CKM_INVALID_MECHANISM) { JSS_throwMsg(env, TOKEN_EXCEPTION, "Unable to resolve algorithm to" " PKCS #11 mechanism"); goto finish; }   if( encrypt ) { op = CKA_ENCRYPT; } else { op = CKA_DECRYPT; }   if( JSS_PK11_getSymKeyPtr(env, keyObj, &key) != PR_SUCCESS) { goto finish; }   if( ivBA != NULL ) { iv = JSS_ByteArrayToSECItem(env, ivBA); if( iv == NULL ) {  goto finish; } } param = PK11_ParamFromIV(mech, iv);   if( mech == CKM_RC2_CBC || mech == CKM_RC2_CBC_PAD ) { ((CK_RC2_CBC_PARAMS*)param->data)->ulEffectiveBits = PK11_GetKeyStrength(key, NULL); }    context = PK11_CreateContextBySymKey(mech, op, key, param); if(context == NULL ) { JSS_throwMsg(env, TOKEN_EXCEPTION, "Failed to generate crypto context"); goto finish; }   contextObj = JSS_PK11_wrapCipherContextProxy(env, &context);  finish: if( param != NULL ) { SECITEM_FreeItem(param, PR_TRUE ); } if(iv) { SECITEM_FreeItem(iv, PR_TRUE ); } if(context != NULL) {  PK11_DestroyContext(context, PR_TRUE ); } PR_ASSERT( contextObj || (*env)->ExceptionOccurred(env) ); return contextObj;
return Java_org_mozilla_jss_pkcs11_PK11Cipher_initContextWithKeyBits ( env, clazz, encrypt, keyObj, algObj, ivBA, 0);
Java_org_mozilla_jss_pkcs11_PK11Cipher_initContext    (JNIEnv *env, jclass clazz, jboolean encrypt, jobject keyObj,        jobject algObj, jbyteArray ivBA){    CK_MECHANISM_TYPE mech;    PK11SymKey *key=NULL;    SECItem *param=NULL;    SECItem *iv=NULL;    PK11Context *context=NULL;    CK_ATTRIBUTE_TYPE op;    jobject contextObj;    PR_ASSERT(env!=NULL && clazz!=NULL && keyObj!=NULL && algObj!=NULL);    /* get mechanism */    mech = JSS_getPK11MechFromAlg(env, algObj);    if(mech == CKM_INVALID_MECHANISM) {        JSS_throwMsg(env, TOKEN_EXCEPTION, "Unable to resolve algorithm to"            " PKCS #11 mechanism");        goto finish;    }    /* get operation type */    if( encrypt ) {        op = CKA_ENCRYPT;    } else {        op = CKA_DECRYPT;    }    /* get key */    if( JSS_PK11_getSymKeyPtr(env, keyObj, &key) != PR_SUCCESS) {        goto finish;    }    /* get param, if there is one */    if( ivBA != NULL ) {        iv = JSS_ByteArrayToSECItem(env, ivBA);        if( iv == NULL ) {            /* exception was thrown */            goto finish;        }    }    param = PK11_ParamFromIV(mech, iv);    /* HACK! The previous function doesn't know the key size, so it can't     * set the RC2 effective key length correctly.  We have to set it by     * hand in this case. */    if( mech == CKM_RC2_CBC || mech == CKM_RC2_CBC_PAD ) {        ((CK_RC2_CBC_PARAMS*)param->data)->ulEffectiveBits =                PK11_GetKeyStrength(key, NULL);    }            /* create crypto context */    context = PK11_CreateContextBySymKey(mech, op, key, param);    if(context == NULL ) {        JSS_throwMsg(env, TOKEN_EXCEPTION,            "Failed to generate crypto context");        goto finish;    }    /* wrap crypto context. This sets context to NULL. */    contextObj = JSS_PK11_wrapCipherContextProxy(env, &context);finish:    if( param != NULL ) {        SECITEM_FreeItem(param, PR_TRUE /*freeit*/);    }    if(iv) {        SECITEM_FreeItem(iv, PR_TRUE /*freeit*/);    }    if(context != NULL) {        /* if the function succeeded, context would be NULL */        PK11_DestroyContext(context, PR_TRUE /*freeit*/);    }    PR_ASSERT( contextObj || (*env)->ExceptionOccurred(env) );    return contextObj;}