src/share/classes/sun/security/pkcs11/P11Digest.java

Print this page




  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.security.pkcs11;
  27 
  28 import java.util.*;
  29 import java.nio.ByteBuffer;
  30 
  31 import java.security.*;
  32 
  33 import javax.crypto.SecretKey;
  34 
  35 import sun.nio.ch.DirectBuffer;
  36 
  37 import sun.security.pkcs11.wrapper.*;
  38 import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
  39 
  40 /**
  41  * MessageDigest implementation class. This class currently supports
  42  * MD2, MD5, SHA-1, SHA-256, SHA-384, and SHA-512.
  43  *
  44  * Note that many digest operations are on fairly small amounts of data
  45  * (less than 100 bytes total). For example, the 2nd hashing in HMAC or
  46  * the PRF in TLS. In order to speed those up, we use some buffering to
  47  * minimize number of the Java->native transitions.
  48  *
  49  * @author  Andreas Sterbenz
  50  * @since   1.5
  51  */
  52 final class P11Digest extends MessageDigestSpi {
  53 
  54     /* unitialized, fields uninitialized, no session acquired */
  55     private final static int S_BLANK    = 1;
  56 
  57     // data in buffer, all fields valid, session acquired
  58     // but digest not initialized
  59     private final static int S_BUFFERED = 2;
  60 
  61     /* session initialized for digesting */
  62     private final static int S_INIT     = 3;


  85     private byte[] oneByte;
  86 
  87     // buffer to reduce number of JNI calls
  88     private final byte[] buffer;
  89 
  90     // offset into the buffer
  91     private int bufOfs;
  92 
  93     P11Digest(Token token, String algorithm, long mechanism) {
  94         super();
  95         this.token = token;
  96         this.algorithm = algorithm;
  97         this.mechanism = mechanism;
  98         switch ((int)mechanism) {
  99         case (int)CKM_MD2:
 100         case (int)CKM_MD5:
 101             digestLength = 16;
 102             break;
 103         case (int)CKM_SHA_1:
 104             digestLength = 20;



 105             break;
 106         case (int)CKM_SHA256:
 107             digestLength = 32;
 108             break;
 109         case (int)CKM_SHA384:
 110             digestLength = 48;
 111             break;
 112         case (int)CKM_SHA512:
 113             digestLength = 64;
 114             break;
 115         default:
 116             throw new ProviderException("Unknown mechanism: " + mechanism);
 117         }
 118         buffer = new byte[BUFFER_SIZE];
 119         state = S_BLANK;
 120         engineReset();
 121     }
 122 
 123     // see JCA spec
 124     protected int engineGetDigestLength() {




  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.security.pkcs11;
  27 
  28 import java.util.*;
  29 import java.nio.ByteBuffer;
  30 
  31 import java.security.*;
  32 
  33 import javax.crypto.SecretKey;
  34 
  35 import sun.nio.ch.DirectBuffer;
  36 
  37 import sun.security.pkcs11.wrapper.*;
  38 import static sun.security.pkcs11.wrapper.PKCS11Constants.*;
  39 
  40 /**
  41  * MessageDigest implementation class. This class currently supports
  42  * MD2, MD5, SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512.
  43  *
  44  * Note that many digest operations are on fairly small amounts of data
  45  * (less than 100 bytes total). For example, the 2nd hashing in HMAC or
  46  * the PRF in TLS. In order to speed those up, we use some buffering to
  47  * minimize number of the Java->native transitions.
  48  *
  49  * @author  Andreas Sterbenz
  50  * @since   1.5
  51  */
  52 final class P11Digest extends MessageDigestSpi {
  53 
  54     /* unitialized, fields uninitialized, no session acquired */
  55     private final static int S_BLANK    = 1;
  56 
  57     // data in buffer, all fields valid, session acquired
  58     // but digest not initialized
  59     private final static int S_BUFFERED = 2;
  60 
  61     /* session initialized for digesting */
  62     private final static int S_INIT     = 3;


  85     private byte[] oneByte;
  86 
  87     // buffer to reduce number of JNI calls
  88     private final byte[] buffer;
  89 
  90     // offset into the buffer
  91     private int bufOfs;
  92 
  93     P11Digest(Token token, String algorithm, long mechanism) {
  94         super();
  95         this.token = token;
  96         this.algorithm = algorithm;
  97         this.mechanism = mechanism;
  98         switch ((int)mechanism) {
  99         case (int)CKM_MD2:
 100         case (int)CKM_MD5:
 101             digestLength = 16;
 102             break;
 103         case (int)CKM_SHA_1:
 104             digestLength = 20;
 105             break;
 106         case (int)CKM_SHA224:
 107             digestLength = 28;
 108             break;
 109         case (int)CKM_SHA256:
 110             digestLength = 32;
 111             break;
 112         case (int)CKM_SHA384:
 113             digestLength = 48;
 114             break;
 115         case (int)CKM_SHA512:
 116             digestLength = 64;
 117             break;
 118         default:
 119             throw new ProviderException("Unknown mechanism: " + mechanism);
 120         }
 121         buffer = new byte[BUFFER_SIZE];
 122         state = S_BLANK;
 123         engineReset();
 124     }
 125 
 126     // see JCA spec
 127     protected int engineGetDigestLength() {