--- old/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11Digest.java 2018-06-04 19:27:19.828674724 -0300 +++ new/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11Digest.java 2018-06-04 19:27:19.599673096 -0300 @@ -143,7 +143,8 @@ token.ensureValid(); if (session != null) { - if (state == S_INIT && token.explicitCancel == true) { + if (state == S_INIT && token.explicitCancel == true + && session.hasObjects() == false) { session = token.killSession(session); } else { session = token.releaseSession(session); @@ -264,7 +265,12 @@ token.p11.C_DigestUpdate(session.id(), 0, buffer, 0, bufOfs); bufOfs = 0; } - token.p11.C_DigestKey(session.id(), p11Key.keyID); + p11Key.incNativeKeyRef(); + try { + token.p11.C_DigestKey(session.id(), p11Key.keyID); + } finally { + p11Key.decNativeKeyRef(); + } } catch (PKCS11Exception e) { engineReset(); throw new ProviderException("update(SecretKey) failed", e);