Path: blob/master/src/java.base/share/classes/sun/security/provider/AbstractHashDrbg.java
41159 views
/*1* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.provider;2627import sun.security.util.HexDumpEncoder;2829import java.util.ArrayList;30import java.util.List;31import java.util.Locale;3233public abstract class AbstractHashDrbg extends AbstractDrbg {3435protected int outLen;36protected int seedLen;3738private static int alg2strength(String algorithm) {39switch (algorithm.toUpperCase(Locale.ROOT)) {40case "SHA-224":41case "SHA-512/224":42return 192;43case "SHA-256":44case "SHA-512/256":45case "SHA-384":46case "SHA-512":47return 256;48default:49throw new IllegalArgumentException(algorithm +50" not supported in Hash_DBRG");51}52}5354protected void chooseAlgorithmAndStrength() {55if (requestedAlgorithm != null) {56algorithm = requestedAlgorithm.toUpperCase(Locale.ROOT);57int supportedStrength = alg2strength(algorithm);58if (requestedInstantiationSecurityStrength >= 0) {59int tryStrength = getStandardStrength(60requestedInstantiationSecurityStrength);61if (tryStrength > supportedStrength) {62throw new IllegalArgumentException(algorithm +63" does not support strength " +64requestedInstantiationSecurityStrength);65}66this.securityStrength = tryStrength;67} else {68this.securityStrength = DEFAULT_STRENGTH > supportedStrength ?69supportedStrength : DEFAULT_STRENGTH;70}71} else {72int tryStrength = (requestedInstantiationSecurityStrength < 0) ?73DEFAULT_STRENGTH : requestedInstantiationSecurityStrength;74tryStrength = getStandardStrength(tryStrength);75// The default algorithm which is enough for all strengths.76// Remember to sync with "securerandom.drbg.config" in java.security77algorithm = "SHA-256";78this.securityStrength = tryStrength;79}80switch (algorithm.toUpperCase(Locale.ROOT)) {81case "SHA-224":82case "SHA-512/224":83this.seedLen = 440 / 8;84this.outLen = 224 / 8;85break;86case "SHA-256":87case "SHA-512/256":88this.seedLen = 440 / 8;89this.outLen = 256 / 8;90break;91case "SHA-384":92this.seedLen = 888 / 8;93this.outLen = 384 / 8;94break;95case "SHA-512":96this.seedLen = 888 / 8;97this.outLen = 512 / 8;98break;99default:100throw new IllegalArgumentException(algorithm +101" not supported in Hash_DBRG");102}103this.minLength = this.securityStrength / 8;104}105106@Override107public void instantiateAlgorithm(byte[] entropy) {108if (debug != null) {109debug.println(this, "instantiate");110}111112// 800-90Ar1 10.1.1.2: Hash_DRBG Instantiate Process.113// 800-90Ar1 10.1.2.3: Hmac_DRBG Instantiate Process.114115// Step 1: entropy_input || nonce || personalization_string.116List<byte[]> inputs = new ArrayList<>(3);117inputs.add(entropy);118inputs.add(nonce);119if (personalizationString != null) {120inputs.add(personalizationString);121}122hashReseedInternal(inputs);123}124125@Override126protected void reseedAlgorithm(127byte[] ei,128byte[] additionalInput) {129if (debug != null) {130debug.println(this, "reseedAlgorithm\n" +131new HexDumpEncoder().encodeBuffer(ei) + "\n" +132((additionalInput == null) ? "" :133new HexDumpEncoder().encodeBuffer(additionalInput)));134}135136// 800-90Ar1 10.1.1.3: Hash_DRBG Reseed Process.137// 800-90Ar1 10.1.2.4: Hmac_DRBG Reseed Process.138139// Step 1: entropy_input || additional_input.140List<byte[]> inputs = new ArrayList<>(2);141inputs.add(ei);142if (additionalInput != null) {143inputs.add(additionalInput);144}145hashReseedInternal(inputs);146}147148/**149* Operates on multiple inputs.150* @param inputs not null, each element neither null151*/152protected abstract void hashReseedInternal(List<byte[]> inputs);153}154155156