1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8141039
  27  * @library /lib/testlibrary
  28  * @summary Check SecureRandom generate expected seed counts what the caller
  29  *          asked for.
  30  * @run main EnoughSeedTest
  31  */
  32 import java.security.SecureRandom;
  33 import java.security.Security;
  34 import static java.lang.Math.*;
  35 
  36 public class EnoughSeedTest {
  37 
  38     private static final String DRBG_CONFIG = "securerandom.drbg.config";
  39     private static final String DRBG_CONFIG_VALUE
  40             = Security.getProperty(DRBG_CONFIG);
  41 
  42     public static void main(String[] args) {
  43 
  44         boolean success = true;
  45         for (String mech : new String[]{
  46             "SHA1PRNG", "Hash_DRBG", "HMAC_DRBG", "CTR_DRBG"}) {
  47             System.out.printf("%nTest for SecureRandom algorithm: '%s'", mech);
  48             try {
  49                 SecureRandom sr = null;
  50                 if (!mech.contains("_DRBG")) {
  51                     sr = SecureRandom.getInstance(mech);
  52                 } else {
  53                     Security.setProperty(DRBG_CONFIG, mech);
  54                     sr = SecureRandom.getInstance("DRBG");
  55                 }
  56 
  57                 success &= forEachSeedBytes(sr);
  58                 System.out.printf("%nCompleted test for SecureRandom "
  59                         + "mechanism: '%s'", mech);
  60             } catch (Exception e) {
  61                 success &= false;
  62                 e.printStackTrace(System.out);
  63             } finally {
  64                 Security.setProperty(DRBG_CONFIG, DRBG_CONFIG_VALUE);
  65             }
  66         }
  67         if (!success) {
  68             throw new RuntimeException("At least one test failed.");
  69         }
  70     }
  71 
  72     /**
  73      * Generates fixed number of seed bytes through a SecureRandom instance
  74      * to verify it's seed generation status.
  75      * @param sr SecureRandom instance
  76      * @return The test success indicator
  77      */
  78     private static boolean forEachSeedBytes(SecureRandom sr) {
  79         boolean success = true;
  80         sr.setSeed(1l);
  81         for (int seedByte : new int[]{Integer.MIN_VALUE, -1, 0, 1, 256, 1024,
  82             Short.MAX_VALUE, (int) pow(2, 20)}) {
  83             try {
  84                 byte[] seed = sr.generateSeed(seedByte);
  85                 if (seed.length != seedByte) {
  86                     throw new RuntimeException("Not able to produce expected "
  87                             + "seed size.");
  88                 }
  89             } catch (IllegalArgumentException e) {
  90                 if (seedByte >= 0) {
  91                     throw new RuntimeException("Unknown Exception occured.", e);
  92                 }
  93                 System.out.printf("%nPASS - Exception expected when required "
  94                         + "seed size requested is negative: %s", seedByte);
  95             }
  96         }
  97         return success;
  98     }
  99 
 100 }