< prev index next >

src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11Cipher.java

Print this page

        

*** 392,462 **** } } this.iv = iv; p11Key = P11SecretKeyFactory.convertKey(token, key, keyAlgorithm); try { ! initialize(); } catch (PKCS11Exception e) { throw new InvalidKeyException("Could not initialize cipher", e); } } ! private void cancelOperation() { ! if (initialized == false) { return; } ! ! if ((session == null) || (token.explicitCancel == false)) { return; } ! try { if (session.hasObjects() == false) { session = token.killSession(session); return; } else { // cancel operation by finishing it int bufLen = doFinalLength(0); byte[] buffer = new byte[bufLen]; if (encrypt) { token.p11.C_EncryptFinal(session.id(), 0, buffer, 0, bufLen); } else { token.p11.C_DecryptFinal(session.id(), 0, buffer, 0, bufLen); } - } } catch (PKCS11Exception e) { throw new ProviderException("Cancel failed", e); } } private void ensureInitialized() throws PKCS11Exception { ! if (initialized == false) { ! initialize(); } } ! ! private void initialize() throws PKCS11Exception { if (session == null) { session = token.getOpSession(); } CK_MECHANISM mechParams = (blockMode == MODE_CTR? new CK_MECHANISM(mechanism, new CK_AES_CTR_PARAMS(iv)) : new CK_MECHANISM(mechanism, iv)); - - try { if (encrypt) { token.p11.C_EncryptInit(session.id(), mechParams, p11Key.keyID); } else { token.p11.C_DecryptInit(session.id(), mechParams, p11Key.keyID); } ! } catch (PKCS11Exception ex) { ! // release session when initialization failed session = token.releaseSession(session); ! throw ex; } bytesBuffered = 0; padBufferLen = 0; - initialized = true; } // if update(inLen) is called, how big does the output buffer have to be? private int updateLength(int inLen) { if (inLen <= 0) { --- 392,479 ---- } } this.iv = iv; p11Key = P11SecretKeyFactory.convertKey(token, key, keyAlgorithm); try { ! ensureInitialized(); } catch (PKCS11Exception e) { throw new InvalidKeyException("Could not initialize cipher", e); } } ! // reset the states to the pre-initialized values ! private void reset(boolean doCancel) { ! if (!initialized) { return; } ! initialized = false; ! try { ! if (session == null) { return; } ! if (doCancel && token.explicitCancel) { ! cancelOperation(); ! } ! } finally { ! p11Key.decNativeKeyRef(); ! session = token.releaseSession(session); ! bytesBuffered = 0; ! padBufferLen = 0; ! } ! } ! ! private void cancelOperation() { ! token.ensureValid(); if (session.hasObjects() == false) { session = token.killSession(session); return; } else { + try { // cancel operation by finishing it int bufLen = doFinalLength(0); byte[] buffer = new byte[bufLen]; if (encrypt) { token.p11.C_EncryptFinal(session.id(), 0, buffer, 0, bufLen); } else { token.p11.C_DecryptFinal(session.id(), 0, buffer, 0, bufLen); } } catch (PKCS11Exception e) { throw new ProviderException("Cancel failed", e); } } + } private void ensureInitialized() throws PKCS11Exception { ! if (initialized) { ! return; } + if (p11Key == null) { + throw new ProviderException( + "Operation cannot be performed without calling engineInit first"); } ! token.ensureValid(); ! p11Key.incNativeKeyRef(); ! try { if (session == null) { session = token.getOpSession(); } CK_MECHANISM mechParams = (blockMode == MODE_CTR? new CK_MECHANISM(mechanism, new CK_AES_CTR_PARAMS(iv)) : new CK_MECHANISM(mechanism, iv)); if (encrypt) { token.p11.C_EncryptInit(session.id(), mechParams, p11Key.keyID); } else { token.p11.C_DecryptInit(session.id(), mechParams, p11Key.keyID); } ! } catch (Throwable t) { ! p11Key.decNativeKeyRef(); session = token.releaseSession(session); ! throw t; } + initialized = true; bytesBuffered = 0; padBufferLen = 0; } // if update(inLen) is called, how big does the output buffer have to be? private int updateLength(int inLen) { if (inLen <= 0) {
*** 483,504 **** result += (blockSize - (result & (blockSize - 1))); } return result; } - // reset the states to the pre-initialized values - private void reset(boolean doCancel) { - if (doCancel) cancelOperation(); - - initialized = false; - bytesBuffered = 0; - padBufferLen = 0; - if (session != null) { - session = token.releaseSession(session); - } - } - // see JCE spec protected byte[] engineUpdate(byte[] in, int inOfs, int inLen) { try { byte[] out = new byte[updateLength(inLen)]; int n = engineUpdate(in, inOfs, inLen, out, 0); --- 500,509 ----
< prev index next >