Source for gnu.javax.crypto.jce.PBKDF2SecretKeyFactory

   1: /* PBKDF2SecretKeyFactory.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.jce;
  40: 
  41: import java.security.spec.InvalidKeySpecException;
  42: import java.security.spec.KeySpec;
  43: 
  44: import java.util.HashMap;
  45: 
  46: import javax.crypto.SecretKey;
  47: import javax.crypto.SecretKeyFactorySpi;
  48: import javax.crypto.spec.PBEKeySpec;
  49: import javax.crypto.spec.SecretKeySpec;
  50: 
  51: import gnu.javax.crypto.prng.IPBE;
  52: import gnu.java.security.prng.IRandom;
  53: import gnu.java.security.prng.LimitReachedException;
  54: import gnu.javax.crypto.prng.PRNGFactory;
  55: 
  56: public abstract class PBKDF2SecretKeyFactory
  57:     extends SecretKeyFactorySpi
  58: {
  59:   protected String macName;
  60:   private static final int DEFAULT_ITERATION_COUNT = 1000;
  61:   private static final int DEFAULT_KEY_LEN = 32;
  62: 
  63:   protected PBKDF2SecretKeyFactory(String macName)
  64:   {
  65:     this.macName = macName;
  66:   }
  67: 
  68:   protected SecretKey engineGenerateSecret(KeySpec spec)
  69:       throws InvalidKeySpecException
  70:   {
  71:     if (! (spec instanceof PBEKeySpec))
  72:       throw new InvalidKeySpecException("not a PBEKeySpec");
  73:     IRandom kdf = PRNGFactory.getInstance("PBKDF2-" + macName);
  74:     HashMap attr = new HashMap();
  75:     attr.put(IPBE.PASSWORD, ((PBEKeySpec) spec).getPassword());
  76:     byte[] salt = ((PBEKeySpec) spec).getSalt();
  77:     if (salt == null)
  78:       salt = new byte[0];
  79:     attr.put(IPBE.SALT, salt);
  80:     int ic = ((PBEKeySpec) spec).getIterationCount();
  81:     if (ic <= 0)
  82:       ic = DEFAULT_ITERATION_COUNT;
  83:     attr.put(IPBE.ITERATION_COUNT, Integer.valueOf(ic));
  84:     kdf.init(attr);
  85:     int len = ((PBEKeySpec) spec).getKeyLength();
  86:     if (len <= 0)
  87:       len = DEFAULT_KEY_LEN;
  88:     byte[] dk = new byte[len];
  89:     try
  90:       {
  91:         kdf.nextBytes(dk, 0, len);
  92:       }
  93:     catch (LimitReachedException lre)
  94:       {
  95:         throw new IllegalArgumentException(lre.toString());
  96:       }
  97:     return new SecretKeySpec(dk, "PBKDF2");
  98:   }
  99: 
 100:   protected KeySpec engineGetKeySpec(SecretKey key, Class clazz)
 101:       throws InvalidKeySpecException
 102:   {
 103:     throw new InvalidKeySpecException("not supported");
 104:   }
 105: 
 106:   protected SecretKey engineTranslateKey(SecretKey key)
 107:   {
 108:     return new SecretKeySpec(key.getEncoded(), key.getAlgorithm());
 109:   }
 110: 
 111:   public static class HMacHaval
 112:       extends PBKDF2SecretKeyFactory
 113:   {
 114:     public HMacHaval()
 115:     {
 116:       super("HMAC-HAVAL");
 117:     }
 118:   }
 119: 
 120:   public static class HMacMD2
 121:       extends PBKDF2SecretKeyFactory
 122:   {
 123:     public HMacMD2()
 124:     {
 125:       super("HMAC-MD2");
 126:     }
 127:   }
 128: 
 129:   public static class HMacMD4
 130:       extends PBKDF2SecretKeyFactory
 131:   {
 132:     public HMacMD4()
 133:     {
 134:       super("HMAC-MD4");
 135:     }
 136:   }
 137: 
 138:   public static class HMacMD5
 139:       extends PBKDF2SecretKeyFactory
 140:   {
 141:     public HMacMD5()
 142:     {
 143:       super("HMAC-MD5");
 144:     }
 145:   }
 146: 
 147:   public static class HMacRipeMD128
 148:       extends PBKDF2SecretKeyFactory
 149:   {
 150:     public HMacRipeMD128()
 151:     {
 152:       super("HMAC-RIPEMD128");
 153:     }
 154:   }
 155: 
 156:   public static class HMacRipeMD160
 157:       extends PBKDF2SecretKeyFactory
 158:   {
 159:     public HMacRipeMD160()
 160:     {
 161:       super("HMAC-RIPEMD160");
 162:     }
 163:   }
 164: 
 165:   public static class HMacSHA1
 166:       extends PBKDF2SecretKeyFactory
 167:   {
 168:     public HMacSHA1()
 169:     {
 170:       super("HMAC-SHA1");
 171:     }
 172:   }
 173: 
 174:   public static class HMacSHA256
 175:       extends PBKDF2SecretKeyFactory
 176:   {
 177:     public HMacSHA256()
 178:     {
 179:       super("HMAC-SHA256");
 180:     }
 181:   }
 182: 
 183:   public static class HMacSHA384
 184:       extends PBKDF2SecretKeyFactory
 185:   {
 186:     public HMacSHA384()
 187:     {
 188:       super("HMAC-SHA384");
 189:     }
 190:   }
 191: 
 192:   public static class HMacSHA512
 193:       extends PBKDF2SecretKeyFactory
 194:   {
 195:     public HMacSHA512()
 196:     {
 197:       super("HMAC-SHA512");
 198:     }
 199:   }
 200: 
 201:   public static class HMacTiger
 202:       extends PBKDF2SecretKeyFactory
 203:   {
 204:     public HMacTiger()
 205:     {
 206:       super("HMAC-TIGER");
 207:     }
 208:   }
 209: 
 210:   public static class HMacWhirlpool
 211:       extends PBKDF2SecretKeyFactory
 212:   {
 213:     public HMacWhirlpool()
 214:     {
 215:       super("HMAC-WHIRLPOOL");
 216:     }
 217:   }
 218: }