1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43:
44: import ;
45:
46: import ;
47: import ;
48: import ;
49: import ;
50:
51: import ;
52: import ;
53: import ;
54: import ;
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: }