--- /dev/null 2016-04-01 19:18:02.000000000 +0800 +++ new/src/java.base/share/classes/sun/security/provider/AbstractHashDrbg.java 2016-04-01 19:18:02.000000000 +0800 @@ -0,0 +1,144 @@ +/* + * 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 sun.security.util.HexDumpEncoder; + +import java.util.Arrays; +import java.util.Locale; + +public abstract class AbstractHashDrbg extends AbstractDrbg { + + private static final long serialVersionUID = 9L; + + protected int outLen; + protected int seedLen; + + private static int alg2strength(String algorithm) { + switch (algorithm.toUpperCase(Locale.ROOT)) { + case "SHA-1": + return 128; + case "SHA-224": + case "SHA-512/224": + return 192; + case "SHA-256": + case "SHA-512/256": + case "SHA-384": + case "SHA-512": + return 256; + default: + throw new IllegalArgumentException(algorithm + + " not supported in Hash_DBRG"); + } + } + + protected void chooseAlgorithmAndStrength() { + if (requestedAlgorithm != null) { + algorithm = requestedAlgorithm.toUpperCase(Locale.ROOT); + int supportedStrength = alg2strength(algorithm); + if (requestedStrength >= 0) { + int tryStrength = getStandardStrength(requestedStrength); + if (tryStrength > supportedStrength) { + throw new IllegalArgumentException( + algorithm + " does not support strength " + requestedStrength); + } + this.strength = tryStrength; + } else { + this.strength = defaultStrength > supportedStrength? + supportedStrength: defaultStrength; + } + } else { + int tryStrength = + requestedStrength<0? defaultStrength: requestedStrength; + tryStrength = getStandardStrength(tryStrength); + // The default algorithm which is enough for all strengths. + // Also described in comments of the "drbg" security property. + algorithm = "SHA-256"; + this.strength = tryStrength; + } + switch (algorithm.toUpperCase(Locale.ROOT)) { + case "SHA-1": + this.seedLen = 440/8; + this.outLen = 160/8; + break; + case "SHA-224": + case "SHA-512/224": + this.seedLen = 440/8; + this.outLen = 224/8; + break; + case "SHA-256": + case "SHA-512/256": + this.seedLen = 440/8; + this.outLen = 256/8; + break; + case "SHA-384": + this.seedLen = 888/8; + this.outLen = 384/8; + break; + case "SHA-512": + this.seedLen = 888/8; + this.outLen = 512/8; + break; + default: + throw new IllegalArgumentException(algorithm + + " not supported in Hash_DBRG"); + } + this.minLength = this.strength/8; + } + + @Override + public void instantiateAlgorithm(byte[] entropy) { + byte[] seed = Arrays.copyOf(entropy, entropy.length + nonce.length + + (ps == null? 0: ps.length)); + System.arraycopy(nonce, 0, seed, entropy.length, nonce.length); + if (ps != null) { + System.arraycopy(ps, 0, seed, entropy.length + nonce.length, + ps.length); + } + hashReseedInternal(seed); + } + + @Override + protected void reseedAlgorithm( + byte[] ei, + byte[] additionalInput) { + if (debug != null) { + debug.println("reseedAlgorithm"); + debug.println(new HexDumpEncoder().encodeBuffer(ei)); + if (additionalInput != null) { + debug.println(new HexDumpEncoder().encodeBuffer(additionalInput)); + } + } + if (additionalInput != null) { + ei = Arrays.copyOf(ei, ei.length + additionalInput.length); + System.arraycopy(additionalInput, 0, ei, + ei.length - additionalInput.length, additionalInput.length); + } + hashReseedInternal(ei); + } + + protected abstract void hashReseedInternal(byte[] seed); +}