< prev index next >

src/java.base/share/classes/sun/security/provider/HashDrbg.java

Print this page

 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package sun.security.provider;
 27 
 28 import java.math.BigInteger;
 29 import java.security.DigestException;
 30 import java.security.MessageDigest;
 31 import java.security.NoSuchAlgorithmException;
 32 import java.security.NoSuchProviderException;
 33 import java.security.SecureRandomParameters;
 34 import java.util.ArrayList;
 35 import java.util.Arrays;

 36 import java.util.List;
 37 
 38 public class HashDrbg extends AbstractHashDrbg {
 39 
 40     private static final byte[] ZERO = new byte[1];
 41     private static final byte[] ONE = new byte[]{1};
 42 
 43     private MessageDigest digest;
 44 
 45     private byte[] v;
 46     private byte[] c;
 47 
 48     public HashDrbg(SecureRandomParameters params) {
 49         mechName = "Hash_DRBG";
 50         configure(params);
 51     }
 52 
 53     /**
 54      * This call, used by the constructors, instantiates the digest.
 55      */

143         }
144 
145         // Step 3. V = seed.
146         v = seed;
147 
148         // Step 4. C = Hash_df ((0x00 || V), seedlen).
149         inputs = new ArrayList<>(2);
150         inputs.add(ZERO);
151         inputs.add(v);
152         c = hashDf(seedLen, inputs);
153 
154         // Step 5. reseed_counter = 1.
155         reseedCounter = 1;
156 
157         //status();
158 
159         // Step 6: Return
160     }
161 
162     private void status() {

163         if (debug != null) {
164             debug.println(this, "V = " + hex(v));
165             debug.println(this, "C = " + hex(c));
166             debug.println(this, "reseed counter = " + reseedCounter);
167         }
168     }
169 
170     /**
171      * Adds byte arrays into an existing one.
172      *
173      * @param out existing array
174      * @param data more arrays, can be of different length
175      */
176     private static void addBytes(byte[] out, int len, byte[]... data) {
177         for (byte[] d: data) {
178             int dlen = d.length;
179             int carry = 0;
180             for (int i = 0; i < len; i++) {
181                 int sum = (out[len - i - 1] & 0xff) + carry;
182                 if (i < dlen) {
183                     sum += (d[dlen - i - 1] & 0xff);
184                 }
185                 out[len - i - 1] = (byte) sum;

 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package sun.security.provider;
 27 
 28 import java.math.BigInteger;
 29 import java.security.DigestException;
 30 import java.security.MessageDigest;
 31 import java.security.NoSuchAlgorithmException;
 32 import java.security.NoSuchProviderException;
 33 import java.security.SecureRandomParameters;
 34 import java.util.ArrayList;
 35 import java.util.Arrays;
 36 import java.util.Hex;
 37 import java.util.List;
 38 
 39 public class HashDrbg extends AbstractHashDrbg {
 40 
 41     private static final byte[] ZERO = new byte[1];
 42     private static final byte[] ONE = new byte[]{1};
 43 
 44     private MessageDigest digest;
 45 
 46     private byte[] v;
 47     private byte[] c;
 48 
 49     public HashDrbg(SecureRandomParameters params) {
 50         mechName = "Hash_DRBG";
 51         configure(params);
 52     }
 53 
 54     /**
 55      * This call, used by the constructors, instantiates the digest.
 56      */

144         }
145 
146         // Step 3. V = seed.
147         v = seed;
148 
149         // Step 4. C = Hash_df ((0x00 || V), seedlen).
150         inputs = new ArrayList<>(2);
151         inputs.add(ZERO);
152         inputs.add(v);
153         c = hashDf(seedLen, inputs);
154 
155         // Step 5. reseed_counter = 1.
156         reseedCounter = 1;
157 
158         //status();
159 
160         // Step 6: Return
161     }
162 
163     private void status() {
164 
165         if (debug != null) {
166             debug.println(this, "V = " + Hex.encoder().encode(v));
167             debug.println(this, "C = " + Hex.encoder().encode(c));
168             debug.println(this, "reseed counter = " + reseedCounter);
169         }
170     }
171 
172     /**
173      * Adds byte arrays into an existing one.
174      *
175      * @param out existing array
176      * @param data more arrays, can be of different length
177      */
178     private static void addBytes(byte[] out, int len, byte[]... data) {
179         for (byte[] d: data) {
180             int dlen = d.length;
181             int carry = 0;
182             for (int i = 0; i < len; i++) {
183                 int sum = (out[len - i - 1] & 0xff) + carry;
184                 if (i < dlen) {
185                     sum += (d[dlen - i - 1] & 0xff);
186                 }
187                 out[len - i - 1] = (byte) sum;
< prev index next >