/* * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/* * @test * @bug 8141039 * @library /lib/testlibrary * @summary This test do API coverage for SecureRandom. It covers most of * supported operations along with possible positive and negative * parameters for DRBG mechanism. * @run main/othervm ApiTest Hash_DRBG * @run main/othervm ApiTest HMAC_DRBG * @run main/othervm ApiTest CTR_DRBG * @run main/othervm ApiTest SHA1PRNG * @run main/othervm ApiTest NATIVE
*/ import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.Security; import java.security.SecureRandomParameters; import java.security.DrbgParameters; import java.security.DrbgParameters.Instantiation; import java.security.DrbgParameters.Capability; import javax.crypto.Cipher;
if (!success) { thrownew RuntimeException("At least one test failed.");
}
}
/** * Run the test for a DRBG mechanism with a possible set of parameter * combination. * @param mech DRBG mechanism name * @param algs Algorithm supported by each mechanism * @throws Exception
*/ privatestaticvoid runForEachMech(String mech, String[] algs) throws Exception { for (String alg : algs) {
runForEachAlg(mech, alg);
}
}
for (boolean df : newBoolean[]{true, false}) { try {
Security.setProperty(DRBG_CONFIG, mech + "," + alg + ","
+ (df ? "use_df" : "no_df"));
System.out.printf("%nParameter for SecureRandom "
+ "mechanism: %s is (param:%s, algo:%s, df:%s)",
mech, param, alg, df);
SecureRandom sr = SecureRandom.getInstance("DRBG", param);
verifyAPI(sr, mech);
} catch (NoSuchAlgorithmException e) { // Verify exception status for current test.
checkException(getDefaultAlg(mech, alg), param, e);
} finally {
Security.setProperty(DRBG_CONFIG, DRBG_CONFIG_VALUE);
}
}
}
/** * Returns the algorithm supported for input mechanism. * @param mech Mechanism name * @param alg Algorithm name * @return Algorithm name
*/ privatestatic String getDefaultAlg(String mech, String alg) throws NoSuchAlgorithmException { if (alg == null) { switch (mech) { case"Hash_DRBG": case"HMAC_DRBG": return"SHA-256"; case"CTR_DRBG": return (Cipher.getMaxAllowedKeyLength("AES") < 256)
? "AES-128" : "AES-256"; default: thrownew RuntimeException("Mechanism not supported");
}
} return alg;
}
/** * Verify the exception type either it is expected to occur or not. * @param alg Algorithm name * @param param DRBG parameter * @param e Exception to verify * @throws NoSuchAlgorithmException
*/ privatestaticvoid checkException(String alg, SecureRandomParameters param,
NoSuchAlgorithmException e) throws NoSuchAlgorithmException {
int strength = ((Instantiation) param).getStrength(); boolean error = true; switch (alg) { case INVALID_ALGO:
error = false; break; case"SHA-224": case"SHA-512/224": if (strength > 192) {
error = false;
} break; case"SHA-256": case"SHA-512/256": case"SHA-384": case"SHA-512": if (strength > 256) {
error = false;
} break; case"AES-128": case"AES-192": case"AES-256": int algoStrength = Integer.parseInt(alg.substring("AES-".length())); int maxAESStrength = Cipher.getMaxAllowedKeyLength("AES"); if (strength > algoStrength
|| algoStrength > maxAESStrength) {
error = false;
} break;
} if (error) { thrownew RuntimeException("Unknown :", e);
}
}
/** * Find if the mechanism is a DRBG mechanism. * @param mech Mechanism name * @return True for DRBG mechanism else False
*/ privatestaticboolean isDRBG(String mech) { return mech.contains("_DRBG");
}
/** * Find the name of supported native mechanism name for current platform.
*/ privatestatic String supportedNativeAlgo() {
String nativeSr = "Windows-PRNG"; try {
SecureRandom.getInstance(nativeSr);
} catch (NoSuchAlgorithmException e) {
nativeSr = "NativePRNG";
} return nativeSr;
}
/** * Test a possible set of SecureRandom API for a SecureRandom instance. * @param random SecureRandom instance * @param mech Mechanism used to create SecureRandom instance
*/ privatestaticvoid verifyAPI(SecureRandom random, String mech) throws Exception {
// Generate random number.
random.nextBytes(output);
// Seed the SecureRandom with a generated seed value of lesser size. byte[] seed = random.generateSeed(1);
random.setSeed(seed);
random.nextBytes(output);
// Seed the SecureRandom with a fixed seed value.
random.setSeed(SEED);
random.nextBytes(output);
// Seed the SecureRandom with a larger seed value.
seed = random.generateSeed(128);
random.setSeed(seed);
random.nextBytes(output);
// Additional operation only supported for DRBG based SecureRandom. // Execute the code block and expect to pass for DRBG. If it will fail // then it should fail with specified exception type. Else the case // will be considered as a test case failure.
matchExc(() -> {
random.reseed();
random.nextBytes(output);
},
isDRBG(mech),
UnsupportedOperationException.class,
String.format("PASS - Unsupported reseed() method for "
+ "SecureRandom Algorithm %s ", mech));
matchExc(() -> {
random.reseed(null);
random.nextBytes(output);
},
!SHOULD_PASS,
IllegalArgumentException.class, "PASS - Test is expected to fail when parameter for reseed() "
+ "is null");
matchExc(() -> random.nextBytes(output, null),
!SHOULD_PASS,
IllegalArgumentException.class, "PASS - Test is expected to fail when parameter for nextBytes()"
+ " is null");
/** * Execute a given code block and verify, if the exception type is expected. * @param r Code block to run * @param ex Expected exception type * @param shouldPass If the code execution expected to pass without failure * @param msg Message to log in case of expected failure
*/ privatestaticvoid matchExc(RunnableCode r, boolean shouldPass, Class ex,
String msg) { try {
r.run(); if (!shouldPass) { thrownew RuntimeException("Excecution should fail here.");
}
} catch (Exception e) {
System.out.printf("%nOccured exception: %s - Expected exception: "
+ "%s : ", e.getClass(), ex.getCanonicalName()); if (ex.isAssignableFrom(e.getClass())) {
System.out.printf("%n%s : Expected Exception occured: %s : ",
e.getClass(), msg);
} elseif (shouldPass) { thrownew RuntimeException(e);
} else {
System.out.printf("Ignore the following exception: %s%n",
e.getMessage());
}
}
}
}
Messung V0.5
¤ Dauer der Verarbeitung: 0.2 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.