Source for gnu.javax.crypto.sasl.srp.IALG

   1: /* IALG.java --
   2:    Copyright (C) 2003, 2006 Free Software Foundation, Inc.
   3: 
   4: This file is a part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2 of the License, or (at
   9: your option) any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; if not, write to the Free Software
  18: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  19: USA
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version.  */
  37: 
  38: 
  39: package gnu.javax.crypto.sasl.srp;
  40: 
  41: import gnu.javax.crypto.mac.IMac;
  42: import gnu.javax.crypto.mac.MacFactory;
  43: 
  44: import java.security.InvalidKeyException;
  45: import java.security.NoSuchAlgorithmException;
  46: import java.util.HashMap;
  47: 
  48: import javax.security.sasl.SaslException;
  49: 
  50: /**
  51:  * A Factory class that returns IALG (Integrity Algorithm) instances that
  52:  * operate as described in the draft-burdis-cat-sasl-srp-04 and later.
  53:  */
  54: public final class IALG
  55:     implements Cloneable
  56: {
  57:   private IMac hmac;
  58: 
  59:   /** Private constructor to enforce instantiation through Factory method. */
  60:   private IALG(final IMac hmac)
  61:   {
  62:     super();
  63: 
  64:     this.hmac = hmac;
  65:   }
  66: 
  67:   /**
  68:    * Returns an instance of a SASL-SRP IALG implementation.
  69:    *
  70:    * @param algorithm the name of the HMAC algorithm.
  71:    * @return an instance of this object.
  72:    */
  73:   static synchronized IALG getInstance(final String algorithm)
  74:       throws SaslException
  75:   {
  76:     final IMac hmac;
  77:     hmac = MacFactory.getInstance(algorithm);
  78:     if (hmac == null)
  79:       throw new SaslException("getInstance()",
  80:                               new NoSuchAlgorithmException(algorithm));
  81:     return new IALG(hmac);
  82:   }
  83: 
  84:   public Object clone() throws CloneNotSupportedException
  85:   {
  86:     return new IALG((IMac) hmac.clone());
  87:   }
  88: 
  89:   public void init(final KDF kdf) throws SaslException
  90:   {
  91:     try
  92:       {
  93:         final byte[] sk = kdf.derive(hmac.macSize());
  94:         final HashMap map = new HashMap();
  95:         map.put(IMac.MAC_KEY_MATERIAL, sk);
  96:         hmac.init(map);
  97:       }
  98:     catch (InvalidKeyException x)
  99:       {
 100:         throw new SaslException("getInstance()", x);
 101:       }
 102:   }
 103: 
 104:   public void update(final byte[] data)
 105:   {
 106:     hmac.update(data, 0, data.length);
 107:   }
 108: 
 109:   public void update(final byte[] data, final int offset, final int length)
 110:   {
 111:     hmac.update(data, offset, length);
 112:   }
 113: 
 114:   public byte[] doFinal()
 115:   {
 116:     return hmac.digest();
 117:   }
 118: 
 119:   /**
 120:    * Returns the length (in bytes) of this SASL SRP Integrity Algorithm.
 121:    *
 122:    * @return the length, in bytes, of this integrity protection algorithm.
 123:    */
 124:   public int length()
 125:   {
 126:     return hmac.macSize();
 127:   }
 128: }