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 /* @test
  25  * @bug 8051408 8158534
  26  * @summary Make sure DrbgParameters coded as specified
  27  * @library /test/lib
  28  * @build jdk.test.lib.Asserts
  29  * @run main DrbgParametersSpec
  30  */
  31 
  32 import jdk.test.lib.Asserts;
  33 
  34 import java.security.DrbgParameters;
  35 import java.util.Arrays;
  36 
  37 import static java.security.DrbgParameters.Capability.*;
  38 
  39 public class DrbgParametersSpec {
  40 
  41     public static void main(String args[]) throws Exception {
  42 
  43         byte[] p, np1, np2;
  44 
  45         // Capability
  46         Asserts.assertTrue(PR_AND_RESEED.supportsPredictionResistance());
  47         Asserts.assertTrue(PR_AND_RESEED.supportsReseeding());
  48         Asserts.assertFalse(RESEED_ONLY.supportsPredictionResistance());
  49         Asserts.assertTrue(RESEED_ONLY.supportsReseeding());
  50         Asserts.assertFalse(NONE.supportsPredictionResistance());
  51         Asserts.assertFalse(NONE.supportsReseeding());
  52 
  53         // Instantiation
  54         p = "Instantiation".getBytes();
  55         DrbgParameters.Instantiation ins = DrbgParameters
  56                 .instantiation(192, RESEED_ONLY, p);
  57         Asserts.assertTrue(ins.getStrength() == 192);
  58         Asserts.assertTrue(ins.getCapability() == RESEED_ONLY);
  59         np1 = ins.getPersonalizationString();
  60         np2 = ins.getPersonalizationString();
  61         // Getter outputs have same content but not the same object
  62         Asserts.assertTrue(Arrays.equals(np1, p));
  63         Asserts.assertTrue(Arrays.equals(np2, p));
  64         Asserts.assertNE(np1, np2);
  65         // Changes to original input has no affect on object
  66         p[0] = 'X';
  67         np2 = ins.getPersonalizationString();
  68         Asserts.assertTrue(Arrays.equals(np1, np2));
  69 
  70         ins = DrbgParameters.instantiation(-1, NONE, null);
  71         Asserts.assertNull(ins.getPersonalizationString());
  72 
  73         iae(() -> DrbgParameters.instantiation(-2, NONE, null));
  74         npe(() -> DrbgParameters.instantiation(-1, null, null));
  75 
  76         // NextBytes
  77         p = "NextBytes".getBytes();
  78         DrbgParameters.NextBytes nb = DrbgParameters
  79                 .nextBytes(192, true, p);
  80         Asserts.assertTrue(nb.getStrength() == 192);
  81         Asserts.assertTrue(nb.getPredictionResistance());
  82         np1 = nb.getAdditionalInput();
  83         np2 = nb.getAdditionalInput();
  84         // Getter outputs have same content but not the same object
  85         Asserts.assertTrue(Arrays.equals(np1, p));
  86         Asserts.assertTrue(Arrays.equals(np2, p));
  87         Asserts.assertNE(np1, np2);
  88         // Changes to original input has no affect on object
  89         p[0] = 'X';
  90         np2 = nb.getAdditionalInput();
  91         Asserts.assertTrue(Arrays.equals(np1, np2));
  92 
  93         iae(() -> DrbgParameters.nextBytes(-2, false, null));
  94 
  95         // Reseed
  96         p = "Reseed".getBytes();
  97         DrbgParameters.Reseed rs = DrbgParameters
  98                 .reseed(true, p);
  99         Asserts.assertTrue(rs.getPredictionResistance());
 100         np1 = rs.getAdditionalInput();
 101         np2 = rs.getAdditionalInput();
 102         // Getter outputs have same content but not the same object
 103         Asserts.assertTrue(Arrays.equals(np1, p));
 104         Asserts.assertTrue(Arrays.equals(np2, p));
 105         Asserts.assertNE(np1, np2);
 106         // Changes to original input has no affect on object
 107         p[0] = 'X';
 108         np2 = rs.getAdditionalInput();
 109         Asserts.assertTrue(Arrays.equals(np1, np2));
 110     }
 111 
 112     static void iae(RunnableWithException r) throws Exception {
 113         checkException(r, IllegalArgumentException.class);
 114     }
 115 
 116     static void npe(RunnableWithException r) throws Exception {
 117         checkException(r, NullPointerException.class);
 118     }
 119 
 120     interface RunnableWithException {
 121         void run() throws Exception;
 122     }
 123 
 124     static void checkException(RunnableWithException r, Class ex)
 125             throws Exception {
 126         try {
 127             r.run();
 128         } catch (Exception e) {
 129             if (ex.isAssignableFrom(e.getClass())) {
 130                 return;
 131             }
 132             throw e;
 133         }
 134         throw new Exception("No exception thrown");
 135     }
 136 }