FileDocCategorySizeDatePackage
IntegrityHmac.javaAPI DocJava SE 6 API15701Tue Jun 10 00:23:00 BST 2008com.sun.org.apache.xml.internal.security.algorithms.implementations

IntegrityHmac

public abstract class IntegrityHmac extends SignatureAlgorithmSpi
author
$Author: raul $

Fields Summary
static Logger
log
{@link java.util.logging} logging facility
private Mac
_macAlgorithm
Field _macAlgorithm
int
_HMACOutputLength
Field _HMACOutputLength
Constructors Summary
public IntegrityHmac()
Method IntegrityHmacSHA1das

throws
XMLSignatureException


           
       

      String algorithmID = JCEMapper.translateURItoJCEID(this.engineGetURI());
      if (true)
      	if (log.isLoggable(java.util.logging.Level.FINE))                                     log.log(java.util.logging.Level.FINE, "Created IntegrityHmacSHA1 using " + algorithmID);

      try {
         this._macAlgorithm = Mac.getInstance(algorithmID);
      } catch (java.security.NoSuchAlgorithmException ex) {
         Object[] exArgs = { algorithmID,
                             ex.getLocalizedMessage() };

         throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs);
      }
   
Methods Summary
public voidengineAddContextToElement(org.w3c.dom.Element element)
Method engineAddContextToElement

param
element


      if (element == null) {
         throw new IllegalArgumentException("null element");
      }

      if (this._HMACOutputLength != 0) {
         Document doc = element.getOwnerDocument();
         Element HMElem = XMLUtils.createElementInSignatureSpace(doc,
                             Constants._TAG_HMACOUTPUTLENGTH);
         Text HMText =
            doc.createTextNode(new Integer(this._HMACOutputLength).toString());

         HMElem.appendChild(HMText);
         XMLUtils.addReturnToElement(element);
         element.appendChild(HMElem);
         XMLUtils.addReturnToElement(element);
      }
   
protected voidengineGetContextFromElement(org.w3c.dom.Element element)
Method engineGetContextFromElement

param
element


      super.engineGetContextFromElement(element);

      if (element == null) {
         throw new IllegalArgumentException("element null");
      }

             Text hmaclength =XMLUtils.selectDsNodeText(element.getFirstChild(),
                    Constants._TAG_HMACOUTPUTLENGTH,0);               

            if (hmaclength != null) {
               this._HMACOutputLength = Integer.parseInt(hmaclength.getData());
            }
      
   
protected java.lang.StringengineGetJCEAlgorithmString()
Method engineGetJCEAlgorithmString

inheritDoc


      if (log.isLoggable(java.util.logging.Level.FINE))                                     log.log(java.util.logging.Level.FINE, "engineGetJCEAlgorithmString()");

      return this._macAlgorithm.getAlgorithm();
   
protected java.lang.StringengineGetJCEProviderName()
Method engineGetJCEAlgorithmString

inheritDoc

      return this._macAlgorithm.getProvider().getName();
   
public abstract java.lang.StringengineGetURI()
Method engineGetURI

inheritDoc

protected voidengineInitSign(java.security.Key secretKey, java.security.SecureRandom secureRandom)
Method engineInitSign

param
secretKey
param
secureRandom
throws
XMLSignatureException

      throw new XMLSignatureException("algorithms.CannotUseSecureRandomOnMAC");
   
protected voidengineInitSign(java.security.Key secretKey)
Method engineInitSign

param
secretKey
throws
XMLSignatureException


      if (!(secretKey instanceof SecretKey)) {
         String supplied = secretKey.getClass().getName();
         String needed = SecretKey.class.getName();
         Object exArgs[] = { supplied, needed };

         throw new XMLSignatureException("algorithms.WrongKeyForThisOperation",
                                         exArgs);
      }

      try {
         this._macAlgorithm.init(secretKey);
      } catch (InvalidKeyException ex) {
         throw new XMLSignatureException("empty", ex);
      }
   
protected voidengineInitSign(java.security.Key secretKey, java.security.spec.AlgorithmParameterSpec algorithmParameterSpec)
Method engineInitSign

param
secretKey
param
algorithmParameterSpec
throws
XMLSignatureException


      if (!(secretKey instanceof SecretKey)) {
         String supplied = secretKey.getClass().getName();
         String needed = SecretKey.class.getName();
         Object exArgs[] = { supplied, needed };

         throw new XMLSignatureException("algorithms.WrongKeyForThisOperation",
                                         exArgs);
      }

      try {
         this._macAlgorithm.init(secretKey, algorithmParameterSpec);
      } catch (InvalidKeyException ex) {
         throw new XMLSignatureException("empty", ex);
      } catch (InvalidAlgorithmParameterException ex) {
         throw new XMLSignatureException("empty", ex);
      }
   
protected voidengineInitVerify(java.security.Key secretKey)
Proxy method for {@link java.security.Signature#initVerify(java.security.PublicKey)} which is executed on the internal {@link java.security.Signature} object.

param
secretKey
throws
XMLSignatureException


      if (!(secretKey instanceof SecretKey)) {
         String supplied = secretKey.getClass().getName();
         String needed = SecretKey.class.getName();
         Object exArgs[] = { supplied, needed };

         throw new XMLSignatureException("algorithms.WrongKeyForThisOperation",
                                         exArgs);
      }

      try {
         this._macAlgorithm.init(secretKey);
      } catch (InvalidKeyException ex) {
         throw new XMLSignatureException("empty", ex);
      }
   
protected voidengineSetHMACOutputLength(int HMACOutputLength)
Method engineSetHMACOutputLength

param
HMACOutputLength

      this._HMACOutputLength = HMACOutputLength;
   
protected voidengineSetParameter(java.security.spec.AlgorithmParameterSpec params)
Proxy method for {@link java.security.Signature#setParameter(java.security.spec.AlgorithmParameterSpec)} which is executed on the internal {@link java.security.Signature} object.

param
params
throws
XMLSignatureException

      throw new XMLSignatureException("empty");
   
protected byte[]engineSign()
Proxy method for {@link java.security.Signature#sign()} which is executed on the internal {@link java.security.Signature} object.

return
the result of the {@link java.security.Signature#sign()} method
throws
XMLSignatureException


      try {
         byte[] completeResult = this._macAlgorithm.doFinal();

         if ((this._HMACOutputLength == 0) || (this._HMACOutputLength >= 160)) {
            return completeResult;
         } 
          return IntegrityHmac.reduceBitLength(completeResult,
                                                 this._HMACOutputLength);
         
      } catch (IllegalStateException ex) {
         throw new XMLSignatureException("empty", ex);
      }
   
protected voidengineUpdate(byte[] input)
Proxy method for {@link java.security.Signature#update(byte[])} which is executed on the internal {@link java.security.Signature} object.

param
input
throws
XMLSignatureException


      try {
         this._macAlgorithm.update(input);
      } catch (IllegalStateException ex) {
         throw new XMLSignatureException("empty", ex);
      }
   
protected voidengineUpdate(byte input)
Proxy method for {@link java.security.Signature#update(byte)} which is executed on the internal {@link java.security.Signature} object.

param
input
throws
XMLSignatureException


      try {
         this._macAlgorithm.update(input);
      } catch (IllegalStateException ex) {
         throw new XMLSignatureException("empty", ex);
      }
   
protected voidengineUpdate(byte[] buf, int offset, int len)
Proxy method for {@link java.security.Signature#update(byte[], int, int)} which is executed on the internal {@link java.security.Signature} object.

param
buf
param
offset
param
len
throws
XMLSignatureException


      try {
         this._macAlgorithm.update(buf, offset, len);
      } catch (IllegalStateException ex) {
         throw new XMLSignatureException("empty", ex);
      }
   
protected booleanengineVerify(byte[] signature)
Proxy method for {@link java.security.Signature#verify(byte[])} which is executed on the internal {@link java.security.Signature} object.

param
signature
return
true if the signature is correct
throws
XMLSignatureException


      try {
         byte[] completeResult = this._macAlgorithm.doFinal();

         if ((this._HMACOutputLength == 0) || (this._HMACOutputLength >= 160)) {
            return MessageDigestAlgorithm.isEqual(completeResult, signature);
         }
         byte[] stripped = IntegrityHmac.reduceBitLength(completeResult,
                                 this._HMACOutputLength);
         return MessageDigestAlgorithm.isEqual(stripped, signature);         
      } catch (IllegalStateException ex) {
         throw new XMLSignatureException("empty", ex);
      }
   
private static byte[]reduceBitLength(byte[] completeResult, int length)
Method reduceBitLength

param
completeResult
return
the reduced bits.
param
length


      int bytes = length / 8;
      int abits = length % 8;
      byte[] strippedResult = new byte[bytes + ((abits == 0)
                                                ? 0
                                                : 1)];

      System.arraycopy(completeResult, 0, strippedResult, 0, bytes);

      if (abits > 0) {
         byte[] MASK = { (byte) 0x00, (byte) 0x80, (byte) 0xC0, (byte) 0xE0,
                         (byte) 0xF0, (byte) 0xF8, (byte) 0xFC, (byte) 0xFE };

         strippedResult[bytes] = (byte) (completeResult[bytes] & MASK[abits]);
      }

      return strippedResult;