org.bouncycastle.crypto.fips.SelfTestExecutor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bc-fips Show documentation
Show all versions of bc-fips Show documentation
The FIPS 140-3 Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms certified to FIPS 140-3 level 1. This jar contains JCE provider and low-level API for the BC-FJA version 2.0.0, FIPS Certificate #4743. Please see certificate for certified platform details.
package org.bouncycastle.crypto.fips;
import org.bouncycastle.crypto.internal.test.BasicKatTest;
import org.bouncycastle.crypto.internal.test.ConsistencyTest;
class SelfTestExecutor
{
static T validate(FipsAlgorithm algorithm, T engine, BasicKatTest test)
{
try
{
if (!test.hasTestPassed(engine))
{
FipsStatus.moveToErrorStatus(new FipsSelfTestFailedError("Self test failed", algorithm));
}
return engine;
}
catch (Exception e)
{
FipsStatus.moveToErrorStatus(new FipsSelfTestFailedError("Exception on self test: " + e.getMessage(), algorithm));
}
return null; // we'll never get this far
}
static T validate(FipsAlgorithm algorithm, T engine, VariantKatTest test)
{
try
{
test.evaluate(engine);
return engine;
}
catch (TestFailedException e)
{
FipsStatus.moveToErrorStatus(new FipsSelfTestFailedError(e.getMessage(), algorithm));
}
catch (Exception e)
{
FipsStatus.moveToErrorStatus(new FipsSelfTestFailedError("Exception on self test: " + e.getMessage(), algorithm));
}
return null; // we'll never get this far
}
static void validate(FipsAlgorithm algorithm, VariantInternalKatTest test)
{
try
{
if (!algorithm.equals(test.getAlgorithm()))
{
throw new TestFailedException("Inconsistent algorithm tag for " + algorithm);
}
test.evaluate();
}
catch (TestFailedException e)
{
FipsStatus.moveToErrorStatus(new FipsSelfTestFailedError(e.getMessage(), algorithm));
}
catch (Exception e)
{
FipsStatus.moveToErrorStatus(new FipsSelfTestFailedError("Exception on self test: " + e.getMessage(), algorithm));
}
}
static T validate(FipsAlgorithm algorithm, T parameters, ConsistencyTest test)
{
try
{
if (!test.hasTestPassed(parameters))
{
FipsStatus.moveToErrorStatus(new FipsConsistencyTestFailedError("Consistency test failed", algorithm));
}
return parameters;
}
catch (Exception e)
{
FipsStatus.moveToErrorStatus(new FipsConsistencyTestFailedError("Exception on consistency test: " + e.getMessage(), algorithm));
}
return null; // we'll never get this far
}
static class TestFailedException
extends RuntimeException
{
public TestFailedException(String message)
{
super(message);
}
}
//
// unused - for validation testing purposes only
//
static T fail(FipsAlgorithm algorithm, T engine, BasicKatTest test)
{
FipsStatus.moveToErrorStatus(new FipsSelfTestFailedError("Self test failed", algorithm));
return null; // we'll never get this far
}
static T fail(FipsAlgorithm algorithm, T engine, VariantKatTest test)
{
FipsStatus.moveToErrorStatus(new FipsSelfTestFailedError("Kat test failed", algorithm));
return null; // we'll never get this far
}
static void fail(FipsAlgorithm algorithm, VariantInternalKatTest test)
{
FipsStatus.moveToErrorStatus(new FipsSelfTestFailedError("Internal kat test failed", algorithm));
}
static T fail(FipsAlgorithm algorithm, T parameters, ConsistencyTest test)
{
FipsStatus.moveToErrorStatus(new FipsConsistencyTestFailedError("Consistency test failed", algorithm));
return null; // we'll never get this far
}
}