Java Code Examples for java.security.DrbgParameters#Instantiation

The following examples show how to use java.security.DrbgParameters#Instantiation . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DrbgParametersSpec.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String args[]) throws Exception {

        byte[] p, np1, np2;

        // Capability
        Asserts.assertTrue(PR_AND_RESEED.supportsPredictionResistance());
        Asserts.assertTrue(PR_AND_RESEED.supportsReseeding());
        Asserts.assertFalse(RESEED_ONLY.supportsPredictionResistance());
        Asserts.assertTrue(RESEED_ONLY.supportsReseeding());
        Asserts.assertFalse(NONE.supportsPredictionResistance());
        Asserts.assertFalse(NONE.supportsReseeding());

        // Instantiation
        p = "Instantiation".getBytes();
        DrbgParameters.Instantiation ins = DrbgParameters
                .instantiation(192, RESEED_ONLY, p);
        Asserts.assertTrue(ins.getStrength() == 192);
        Asserts.assertTrue(ins.getCapability() == RESEED_ONLY);
        np1 = ins.getPersonalizationString();
        np2 = ins.getPersonalizationString();
        // Getter outputs have same content but not the same object
        Asserts.assertTrue(Arrays.equals(np1, p));
        Asserts.assertTrue(Arrays.equals(np2, p));
        Asserts.assertNE(np1, np2);
        // Changes to original input has no affect on object
        p[0] = 'X';
        np2 = ins.getPersonalizationString();
        Asserts.assertTrue(Arrays.equals(np1, np2));

        ins = DrbgParameters.instantiation(-1, NONE, null);
        Asserts.assertNull(ins.getPersonalizationString());

        iae(() -> DrbgParameters.instantiation(-2, NONE, null));
        npe(() -> DrbgParameters.instantiation(-1, null, null));

        // NextBytes
        p = "NextBytes".getBytes();
        DrbgParameters.NextBytes nb = DrbgParameters
                .nextBytes(192, true, p);
        Asserts.assertTrue(nb.getStrength() == 192);
        Asserts.assertTrue(nb.getPredictionResistance());
        np1 = nb.getAdditionalInput();
        np2 = nb.getAdditionalInput();
        // Getter outputs have same content but not the same object
        Asserts.assertTrue(Arrays.equals(np1, p));
        Asserts.assertTrue(Arrays.equals(np2, p));
        Asserts.assertNE(np1, np2);
        // Changes to original input has no affect on object
        p[0] = 'X';
        np2 = nb.getAdditionalInput();
        Asserts.assertTrue(Arrays.equals(np1, np2));

        iae(() -> DrbgParameters.nextBytes(-2, false, null));

        // Reseed
        p = "Reseed".getBytes();
        DrbgParameters.Reseed rs = DrbgParameters
                .reseed(true, p);
        Asserts.assertTrue(rs.getPredictionResistance());
        np1 = rs.getAdditionalInput();
        np2 = rs.getAdditionalInput();
        // Getter outputs have same content but not the same object
        Asserts.assertTrue(Arrays.equals(np1, p));
        Asserts.assertTrue(Arrays.equals(np2, p));
        Asserts.assertNE(np1, np2);
        // Changes to original input has no affect on object
        p[0] = 'X';
        np2 = rs.getAdditionalInput();
        Asserts.assertTrue(Arrays.equals(np1, np2));
    }
 
Example 2
Source File: GetInstanceTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static boolean isValidDRBGParam(SecureRandomParameters param) {
    return (param instanceof DrbgParameters.Instantiation);
}
 
Example 3
Source File: MoreDrbgParameters.java    From Bytecoder with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a new {@code MoreDrbgParameters} object.
 *
 * @param es the {@link EntropySource} to use. If set to {@code null},
 *           a default entropy source will be used.
 * @param mech mech name. If set to {@code null}, the one in
 *             securerandom.drbg.config is used. This argument is ignored
 *             when passing to HashDrbg/HmacDrbg/CtrDrbg.
 * @param algorithm the requested algorithm to use. If set to {@code null},
 *                  the algorithm will be decided by strength.
 * @param nonce the nonce to use. If set to {@code null},
 *              a nonce will be assigned.
 * @param usedf whether a derivation function should be used
 * @param config a {@link DrbgParameters.Instantiation} object
 */
public MoreDrbgParameters(EntropySource es, String mech,
                          String algorithm, byte[] nonce, boolean usedf,
                          DrbgParameters.Instantiation config) {
    this.mech = mech;
    this.algorithm = algorithm;
    this.es = es;
    this.nonce = (nonce == null) ? null : nonce.clone();
    this.usedf = usedf;

    this.strength = config.getStrength();
    this.capability = config.getCapability();
    this.personalizationString = config.getPersonalizationString();
}
 
Example 4
Source File: MoreDrbgParameters.java    From openjdk-jdk9 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Creates a new {@code MoreDrbgParameters} object.
 *
 * @param es the {@link EntropySource} to use. If set to {@code null},
 *           a default entropy source will be used.
 * @param mech mech name. If set to {@code null}, the one in
 *             securerandom.drbg.config is used. This argument is ignored
 *             when passing to HashDrbg/HmacDrbg/CtrDrbg.
 * @param algorithm the requested algorithm to use. If set to {@code null},
 *                  the algorithm will be decided by strength.
 * @param nonce the nonce to use. If set to {@code null},
 *              a nonce will be assigned.
 * @param usedf whether a derivation function should be used
 * @param config a {@link DrbgParameters.Instantiation} object
 */
public MoreDrbgParameters(EntropySource es, String mech,
                          String algorithm, byte[] nonce, boolean usedf,
                          DrbgParameters.Instantiation config) {
    this.mech = mech;
    this.algorithm = algorithm;
    this.es = es;
    this.nonce = (nonce == null) ? null : nonce.clone();
    this.usedf = usedf;

    this.strength = config.getStrength();
    this.capability = config.getCapability();
    this.personalizationString = config.getPersonalizationString();
}