All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bouncycastle.crypto.fips.SelfTestExecutor Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 2.0.0
Show newest version
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
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy