Path: blob/master/test/jdk/sun/security/provider/SecureRandom/DRBGAlg.java
41154 views
/*1* Copyright (c) 2016, 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.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22import sun.security.provider.MoreDrbgParameters;2324import java.security.DrbgParameters;25import java.security.NoSuchAlgorithmException;26import java.security.SecureRandom;27import java.security.SecureRandomParameters;28import java.security.Security;29import java.util.ArrayList;30import java.util.Arrays;31import java.util.Collections;32import java.util.List;33import java.util.stream.Collectors;3435import static java.security.DrbgParameters.Capability.*;3637/**38* @test39* @bug 805140840* @modules java.base/sun.security.provider41* @summary make sure DRBG alg can be defined and instantiated freely42*/43public class DRBGAlg {4445public static void main(String[] args) throws Exception {4647check(null, "Hash_DRBG", "SHA-256", "reseed_only", ",128");48check("", "Hash_DRBG", "SHA-256", "reseed_only", ",128");49check("sha-256", "Hash_DRBG", "SHA-256", "reseed_only", ",128");50check("SHA-3");51check("hash_drbg", "Hash_DRBG", "SHA-256", "reseed_only", ",128");52check("hmac_drbg", "HMAC_DRBG", "SHA-256", "reseed_only", ",128");53check("ctr_drbg", "CTR_DRBG", "AES-", "reseed_only", ",128", "use_df");5455// trying all permutations56checkPermutations(57Collections.emptyList(),58Arrays.asList("hash_drbg","sha-512","Pr_and_Reseed","192"),59"Hash_DRBG", "SHA-512", "pr_and_reseed", ",192");6061check("Hash_DRBG,Hmac_DRBG");62check("SHA-224,SHA-256");63check("128,256");64check("none,reseed_only");65check("use_df,no_df");66check("Hash_DRBG,,SHA-256");6768check(null, DrbgParameters.instantiation(112, PR_AND_RESEED, null),69"Hash_DRBG", "SHA-256", "pr_and_reseed", ",112");70check(null, DrbgParameters.instantiation(256, PR_AND_RESEED, null),71"Hash_DRBG", "SHA-256", "pr_and_reseed", ",256");72check(null, DrbgParameters.instantiation(384, PR_AND_RESEED, null));73check("sha-224", DrbgParameters.instantiation(112, PR_AND_RESEED, null),74"Hash_DRBG", "SHA-224", "pr_and_reseed", ",112");75check("sha-224", DrbgParameters.instantiation(256, PR_AND_RESEED, null));76check("hash_drbg,sha-512,Pr_and_Reseed,192",77DrbgParameters.instantiation(112, NONE, null),78"Hash_DRBG", "SHA-512", "reseed_only", ",112");79check("hash_drbg,sha-512,Pr_and_Reseed,192",80DrbgParameters.instantiation(-1, NONE, null),81"Hash_DRBG", "SHA-512", "reseed_only", ",192");82// getInstance params can be stronger than definition83check("hash_drbg,sha-256,None,112",84DrbgParameters.instantiation(192, PR_AND_RESEED, null),85"Hash_DRBG", "SHA-256", "pr_and_reseed", ",192");8687check("hash_drbg,sha-224", new MoreDrbgParameters(88null, null, "sha-512", null, false,89DrbgParameters.instantiation(-1, NONE, null)),90"Hash_DRBG", "SHA-512");91check("hash_drbg,sha-224", new MoreDrbgParameters(92null, null, null, null, false,93DrbgParameters.instantiation(-1, NONE, null)),94"Hash_DRBG", "SHA-224");95check("hash_drbg", new MoreDrbgParameters(96null, "hmac_drbg", null, null, false,97DrbgParameters.instantiation(-1, NONE, null)),98"HMAC_DRBG", "SHA-256");99100check("hash_drbg,sha-224", new MoreDrbgParameters(101null, null, "sha-3", null, false,102DrbgParameters.instantiation(-1, NONE, null)));103check("hash_drbg,sha-224", new MoreDrbgParameters(104null, "Unknown_DRBG", null, null, false,105DrbgParameters.instantiation(-1, NONE, null)));106}107108/**109* Checks all permutatins of a config. This is a recursive method and110* should be called with checkPermutations(empty,config,expected).111*112* @param current the current chosen aspects113* @param remains the remaining114* @param expected the expected effective config115* @throws Exception when check fails116*/117private static void checkPermutations(List<String> current,118List<String> remains, String... expected) throws Exception {119if (remains.isEmpty()) {120check(current.stream().collect(Collectors.joining(",")), expected);121} else {122for (String r : remains) {123List<String> newCurrent = new ArrayList<>(current);124newCurrent.add(r);125List<String> newRemains = new ArrayList<>(remains);126newRemains.remove(r);127checkPermutations(newCurrent, newRemains, expected);128}129}130}131132/**133* Checks DRBG definition for getInstance(alg, params).134*135* @param define DRBG136* @param params getInstance request (null if none)137* @param expected expected actual instantiate params, empty if should fail138*/139static void check(String define, SecureRandomParameters params,140String... expected) throws Exception {141System.out.println("Testing " + define + " with " + params + "...");142String old = Security.getProperty("securerandom.drbg.config");143if (define != null) {144Security.setProperty("securerandom.drbg.config", define);145}146try {147String result = params != null ?148SecureRandom.getInstance("DRBG", params).toString() :149SecureRandom.getInstance("DRBG").toString();150System.out.println("Result " + result);151if (expected.length == 0) {152throw new Exception("should fail");153}154for (String s : expected) {155if (!result.contains(s)) {156throw new Exception(result);157}158}159} catch (NoSuchAlgorithmException e) {160System.out.println("Result NSAE");161if (expected.length > 0) {162throw e;163}164} finally {165Security.setProperty("securerandom.drbg.config", old);166}167}168169/**170* Checks DRBG definition for getInstance(alg).171*172* @param define DRBG173* @param expected expected actual instantiate params, empty if should fail174*/175static void check(String define, String... expected) throws Exception {176check(define, null, expected);177}178}179180181