Path: blob/master/test/jdk/sun/security/provider/SecureRandom/CommonSeeder.java
41154 views
/*1* Copyright (c) 2016, 2018, 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*/2223import java.security.DrbgParameters;24import java.security.SecureRandom;25import java.security.Security;2627import sun.security.provider.SeedGenerator;28/**29* @test30* @bug 8051408 820260831* @modules java.base/sun.security.provider32* @build java.base/sun.security.provider.SeedGenerator33* @run main/othervm CommonSeeder34* @summary check entropy reading of DRBGs35*/36public class CommonSeeder {3738public static void main(String[] args) throws Exception {3940byte[] result = new byte[10];4142// Use patched SeedGenerator in java.base/sun/security/provider/.4344// Nothing happened yet45SeedGenerator.checkUsage(0);4647SecureRandom sr;48sr = SecureRandom.getInstance("DRBG");4950// No entropy reading if only getInstance51SeedGenerator.checkUsage(0);5253// Entropy is read at 1st nextBytes of the 1st DRBG54sr.nextInt();55SeedGenerator.checkUsage(1);5657for (String mech : new String[]{"Hash_DRBG", "HMAC_DRBG", "CTR_DRBG"}) {58System.out.println("Testing " + mech + "...");5960// DRBG with pr_false will never read entropy again no matter61// if nextBytes or reseed is called.6263Security.setProperty("securerandom.drbg.config", mech);64sr = SecureRandom.getInstance("DRBG");65sr.nextInt();66sr.reseed();67SeedGenerator.checkUsage(0);6869// DRBG with pr_true always read from default entropy, and70// its nextBytes always reseed itself7172Security.setProperty("securerandom.drbg.config",73mech + ",pr_and_reseed");74sr = SecureRandom.getInstance("DRBG");7576sr.nextInt();77SeedGenerator.checkUsage(2); // one instantiate, one reseed78sr.nextInt();79SeedGenerator.checkUsage(1); // one reseed in nextBytes80sr.reseed();81SeedGenerator.checkUsage(1); // one reseed82sr.nextBytes(result, DrbgParameters.nextBytes(-1, false, null));83SeedGenerator.checkUsage(0); // pr_false for this call84sr.nextBytes(result, DrbgParameters.nextBytes(-1, true, null));85SeedGenerator.checkUsage(1); // pr_true for this call86sr.reseed(DrbgParameters.reseed(true, null));87SeedGenerator.checkUsage(1); // reseed from es88sr.reseed(DrbgParameters.reseed(false, null));89SeedGenerator.checkUsage(0); // reseed from AbstractDrbg.SeederHolder.seeder90}91}92}939495