--- /dev/null 2016-04-01 19:18:08.000000000 +0800 +++ new/src/java.base/share/classes/sun/security/provider/HmacDrbg.java 2016-04-01 19:18:08.000000000 +0800 @@ -0,0 +1,177 @@ +/* + * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package sun.security.provider; + +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; +import java.io.IOException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.SecureRandomInstantiateParameters; +import java.util.Arrays; + +public class HmacDrbg extends AbstractHashDrbg { + + private static final long serialVersionUID = 9L; + + private transient Mac mac; + + private String macAlg; + + private byte[] v; + private byte[] k; + + public HmacDrbg(SecureRandomInstantiateParameters params) { + mechName = "HMAC_DRBG"; + configureInternal(params); + } + + private void status() { + if (debug != null) { + debug.println("V = " + hex(v)); + debug.println("Key = " + hex(k)); + debug.println("reseed counter = " + reseedCounter); + } + } + + private void update(byte[]... inputs) { + try { + // Step 1. + mac.init(new SecretKeySpec(k, macAlg)); + mac.update(v); + mac.update((byte) 0); + for (byte[] input: inputs) { + mac.update(input); + } + k = mac.doFinal(); + // Step 2. + mac.init(new SecretKeySpec(k, macAlg)); + v = mac.doFinal(v); + // Step 4. + if (inputs.length != 0) { + mac.update(v); + mac.update((byte) 1); + for (byte[] input: inputs) { + mac.update(input); + } + k = mac.doFinal(); + mac.init(new SecretKeySpec(k, macAlg)); + v = mac.doFinal(v); + } + } catch (InvalidKeyException e) { + throw new InternalError(e); + } + } + + /** + * This call, used by the constructors, instantiates the digest. + */ + @Override + protected void initEngine() { + if (algorithm == null) { + return; + } + macAlg = "HmacSHA" + algorithm.substring(4); + try { + mac = Mac.getInstance(macAlg, "SunJCE"); + } catch (NoSuchProviderException | NoSuchAlgorithmException e) { + // Fallback to any available. + try { + mac = Mac.getInstance(macAlg); + } catch (NoSuchAlgorithmException exc) { + throw new InternalError( + "internal error: " + macAlg + " not available.", exc); + } + } + } + + // For seeding, input is entropy_input || nonce || personalization_string; + // for reseeding, input is entropy_input || additional_input + @Override + protected final void hashReseedInternal(byte[] input) { + if (v == null) { + k = new byte[outLen]; + v = new byte[outLen]; + Arrays.fill(v, (byte) 1); + } + update(input); + reseedCounter = 1; + status(); + } + + /** + * Generates a user-specified number of random bytes. + * + * @param result the array to be filled in with random bytes. + */ + @Override + public synchronized void generateAlgorithm( + byte[] result, byte[] additionalInput) { + + // Step 2. + if (additionalInput != null) { + update(additionalInput); + } + + // Step 3. + int pos = 0; + + // Step 4. + while (pos < result.length) { + int tailLen = result.length - pos; + // Step 4.1. + try { + mac.init(new SecretKeySpec(k, macAlg)); + } catch (InvalidKeyException e) { + throw new InternalError(e); + } + v = mac.doFinal(v); + // Step 4.2 and 5. + System.arraycopy(v, 0, result, pos, + tailLen > outLen ? outLen : tailLen); + pos += outLen; + } + + // Step 6. + if (additionalInput != null) { + update(additionalInput); + } else { + update(); + } + + // Step 7. + reseedCounter++; + + status(); + } + + private void readObject(java.io.ObjectInputStream s) + throws IOException, ClassNotFoundException { + s.defaultReadObject (); + initEngine(); + } +}