Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / javax / crypto / prng / PBKDF2.java
blob5146bd4b9ab4daff10ce1891600efd9dfb4ad19e
1 /* PBKDF2.java --
2 Copyright (C) 2003, 2006 Free Software Foundation, Inc.
4 This file is a part of GNU Classpath.
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.
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.
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
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.
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. */
39 package gnu.javax.crypto.prng;
41 import gnu.java.security.prng.BasePRNG;
42 import gnu.java.security.prng.LimitReachedException;
43 import gnu.javax.crypto.mac.HMac;
44 import gnu.javax.crypto.mac.IMac;
46 import java.io.UnsupportedEncodingException;
47 import java.util.Arrays;
48 import java.util.HashMap;
49 import java.util.Map;
51 /**
52 * <p>An implementation of the <i>key derivation function</i> KDF2 from PKCS #5:
53 * Password-Based Cryptography (<b>PBE</b>). This KDF is essentially a way to
54 * transform a password and a salt into a stream of random bytes, which may then
55 * be used to initialize a cipher or a MAC.</p>
57 * <p>This version uses a MAC as its pseudo-random function, and the password is
58 * used as the key.</p>
60 * <p>References:</p>
61 * <ol>
62 * <li>B. Kaliski, <a href="http://www.ietf.org/rfc/rfc2898.txt">RFC 2898:
63 * Password-Based Cryptography Specification, Version 2.0</a></li>
64 * </ol>
66 * @version $Revision: 1.1 $
68 public class PBKDF2 extends BasePRNG implements Cloneable
71 // Contstants and variables
72 // -------------------------------------------------------------------------
74 /**
75 * The bytes fed into the MAC. This is initially the concatenation of the
76 * salt and the block number.
78 private byte[] in;
80 /** The iteration count. */
81 private int iterationCount;
83 /** The salt. */
84 private byte[] salt;
86 /** The MAC (the pseudo-random function we use). */
87 private IMac mac;
89 /** The number of hLen-sized blocks generated. */
90 private long count;
92 // Constructor(s)
93 // -------------------------------------------------------------------------
95 /**
96 * <p>Creates a new PBKDF2 object. The argument is the MAC that will serve as
97 * the pseudo-random function. The MAC does not need to be initialized.</p>
99 * @param mac The pseudo-random function.
101 public PBKDF2(IMac mac)
103 super("PBKDF2-" + mac.name());
104 this.mac = mac;
105 iterationCount = -1;
108 // Class methods
109 // -------------------------------------------------------------------------
111 // Instance methods
112 // -------------------------------------------------------------------------
114 public void setup(Map attributes)
116 Map macAttrib = new HashMap();
117 macAttrib.put(HMac.USE_WITH_PKCS5_V2, Boolean.TRUE);
119 byte[] s = (byte[]) attributes.get(IPBE.SALT);
120 if (s == null)
122 if (salt == null)
124 throw new IllegalArgumentException("no salt specified");
125 } // Otherwise re-use.
127 else
129 salt = s;
132 char[] password = (char[]) attributes.get(IPBE.PASSWORD);
133 if (password != null)
137 macAttrib.put(IMac.MAC_KEY_MATERIAL,
138 new String(password).getBytes("UTF-8"));
140 catch (UnsupportedEncodingException uee)
142 throw new Error(uee.getMessage());
145 else if (!initialised)
147 throw new IllegalArgumentException("no password specified");
148 } // otherwise re-use previous password.
152 mac.init(macAttrib);
154 catch (Exception x)
156 throw new IllegalArgumentException(x.getMessage());
159 Integer ic = (Integer) attributes.get(IPBE.ITERATION_COUNT);
160 if (ic != null)
162 iterationCount = ic.intValue();
164 if (iterationCount <= 0)
166 throw new IllegalArgumentException("bad iteration count");
169 count = 0L;
170 buffer = new byte[mac.macSize()];
173 fillBlock();
174 // } catch (Exception x) {
176 catch (LimitReachedException x)
178 // x.printStackTrace(System.err);
179 throw new Error(x.getMessage());
183 public void fillBlock() throws LimitReachedException
185 if (++count > ((1L << 32) - 1))
187 throw new LimitReachedException();
189 // for (int i = 0; i < buffer.length; i++) {
190 // buffer[i] = 0;
191 // }
192 Arrays.fill(buffer, (byte) 0x00);
193 int limit = salt.length;
194 // in = new byte[salt.length + 4];
195 in = new byte[limit + 4];
196 System.arraycopy(salt, 0, in, 0, salt.length);
197 // in[salt.length ] = (byte)(count >>> 24);
198 // in[salt.length+1] = (byte)(count >>> 16);
199 // in[salt.length+2] = (byte)(count >>> 8);
200 // in[salt.length+3] = (byte) count;
201 in[limit++] = (byte) (count >>> 24);
202 in[limit++] = (byte) (count >>> 16);
203 in[limit++] = (byte) (count >>> 8);
204 in[limit] = (byte) count;
205 for (int i = 0; i < iterationCount; i++)
207 mac.reset();
208 mac.update(in, 0, in.length);
209 in = mac.digest();
210 for (int j = 0; j < buffer.length; j++)
212 buffer[j] ^= in[j];