Package org.bouncycastle.crypto.engines

Examples of org.bouncycastle.crypto.engines.ThreefishCipher


    byte[] result;

    boolean basicTest256() {

        ThreefishCipher tfc = new ThreefishCipher();
        int stateSize = 256;

        // Key must match Threefish state size
        key = new byte[stateSize / 8];

        // For simple ECB mode length matches state size
        dataIn = new byte[stateSize / 8];
        dataOut = new byte[stateSize / 8];
        result = new byte[stateSize / 8];

        // Prepare first vector
        ByteLong.PutBytes(three_256_00_input, dataIn, 0, dataIn.length);
        ByteLong.PutBytes(three_256_00_key, key, 0, key.length);
        ByteLong.PutBytes(three_256_00_result, result, 0, result.length);

        ParametersForThreefish pft = new ParametersForThreefish(
                new KeyParameter(key), stateSize, three_256_00_tweak);
       
        // Encrypt and check
        tfc.init(true, pft);
        tfc.processBlock(dataIn, 0, dataOut, 0);

        if (!Arrays.equals(result, dataOut)) {
            hexdump("Wrong cipher text 256 00", dataOut, dataOut.length);
            return false;
        }
        // Decrypt and check
        tfc.init(false, pft);
        tfc.processBlock(dataOut, 0, result, 0);
        if (!Arrays.equals(dataIn, result)) {
            hexdump("Decrypt failed 256 00", result, result.length);
            return false;
        }
        // Next Vector
        ByteLong.PutBytes(three_256_01_input, dataIn, 0, dataIn.length);
        ByteLong.PutBytes(three_256_01_key, key, 0, key.length);
        ByteLong.PutBytes(three_256_01_result, result, 0, result.length);

        pft = new ParametersForThreefish(new KeyParameter(key), stateSize,
                three_256_01_tweak);

        // Encrypt and check
        tfc.init(true, pft);
        tfc.processBlock(dataIn, 0, dataOut, 0);

        // plaintext feed forward
        for (int i = 0; i < dataIn.length; i++)
            dataOut[i] ^= dataIn[i];

        if (!Arrays.equals(result, dataOut)) {
            hexdump("Wrong cipher text 256 01", dataOut, dataOut.length);
            return false;
        }
        // Decrypt and check
        // plaintext feed backward :-)
        for (int i = 0; i < dataIn.length; i++)
            dataOut[i] ^= dataIn[i];

        tfc.init(false, pft);
        tfc.processBlock(dataOut, 0, result, 0);
        if (!Arrays.equals(dataIn, result)) {
            hexdump("Decrypt failed 256 01", result, result.length);
            return false;
        }
        return true;
View Full Code Here


        return true;
    }

    boolean basicTest512() {

        ThreefishCipher tfc = new ThreefishCipher();
        int stateSize = 512;

        // Key must match Threefish state size
        key = new byte[stateSize / 8];

        // For simple ECB mode length matches state size
        dataIn = new byte[stateSize / 8];
        dataOut = new byte[stateSize / 8];
        result = new byte[stateSize / 8];

        // Prepare first vector
        ByteLong.PutBytes(three_512_00_input, dataIn, 0, dataIn.length);
        ByteLong.PutBytes(three_512_00_key, key, 0, key.length);
        ByteLong.PutBytes(three_512_00_result, result, 0, result.length);

        ParametersForThreefish pft = new ParametersForThreefish(
                new KeyParameter(key), stateSize, three_512_00_tweak);

        // Encrypt and check
        tfc.init(true, pft);
        tfc.processBlock(dataIn, 0, dataOut, 0);

        if (!Arrays.equals(result, dataOut)) {
            hexdump("Wrong cipher text 512 00", dataOut, dataOut.length);
            return false;
        }
        // Decrypt and check
        tfc.init(false, pft);
        tfc.processBlock(dataOut, 0, result, 0);
        if (!Arrays.equals(dataIn, result)) {
            hexdump("Decrypt failed 512 00", result, result.length);
            return false;
        }
        // Next vector
        ByteLong.PutBytes(three_512_01_input, dataIn, 0, dataIn.length);
        ByteLong.PutBytes(three_512_01_key, key, 0, key.length);
        ByteLong.PutBytes(three_512_01_result, result, 0, result.length);

        pft = new ParametersForThreefish(new KeyParameter(key), stateSize,
                three_512_01_tweak);
        tfc.init(true, pft);
        tfc.processBlock(dataIn, 0, dataOut, 0);

        // plaintext feed forward
        for (int i = 0; i < dataIn.length; i++)
            dataOut[i] ^= dataIn[i];

        if (!Arrays.equals(result, dataOut)) {
            hexdump("Wrong cipher text 512 01", dataOut, dataOut.length);
            return false;
        }
        // Decrypt and check
        // plaintext feed backward :-)
        for (int i = 0; i < dataIn.length; i++)
            dataOut[i] ^= dataIn[i];

        tfc.init(false, pft);
        tfc.processBlock(dataOut, 0, result, 0);
        if (!Arrays.equals(dataIn, result)) {
            hexdump("Decrypt failed 512 01", result, result.length);
            return false;
        }
        return true;
View Full Code Here

        return true;
    }

    boolean basicTest1024() {

        ThreefishCipher tfc = new ThreefishCipher();
        int stateSize = 1024;

        // Key must match Threefish state size
        key = new byte[stateSize / 8];

        // For simple ECB mode length matches state size
        dataIn = new byte[stateSize / 8];
        dataOut = new byte[stateSize / 8];
        result = new byte[stateSize / 8];

        // Prepare first vector
        ByteLong.PutBytes(three_1024_00_input, dataIn, 0, dataIn.length);
        ByteLong.PutBytes(three_1024_00_key, key, 0, key.length);
        ByteLong.PutBytes(three_1024_00_result, result, 0, result.length);

        ParametersForThreefish pft = new ParametersForThreefish(
                new KeyParameter(key), stateSize, three_1024_00_tweak);

        // Encrypt and check
        tfc.init(true, pft);
        tfc.processBlock(dataIn, 0, dataOut, 0);

        if (!Arrays.equals(result, dataOut)) {
            hexdump("Wrong cipher text 1024 00", dataOut, dataOut.length);
            return false;
        }
        // Decrypt and check
        tfc.init(false, pft);
        tfc.processBlock(dataOut, 0, result, 0);
        if (!Arrays.equals(dataIn, result)) {
            hexdump("Decrypt failed 1024 00", result, result.length);
            return false;
        }
        // Next vector
        ByteLong.PutBytes(three_1024_01_input, dataIn, 0, dataIn.length);
        ByteLong.PutBytes(three_1024_01_key, key, 0, key.length);
        ByteLong.PutBytes(three_1024_01_result, result, 0, result.length);

        pft = new ParametersForThreefish(new KeyParameter(key), stateSize,
                three_1024_01_tweak);
        tfc.init(true, pft);
        tfc.processBlock(dataIn, 0, dataOut, 0);

        // plaintext feed forward
        for (int i = 0; i < dataIn.length; i++)
            dataOut[i] ^= dataIn[i];

        if (!Arrays.equals(result, dataOut)) {
            hexdump("Wrong cipher text 1024 01", dataOut, dataOut.length);
            return false;
        }
        // Decrypt and check
        // plaintext feed backward :-)
        for (int i = 0; i < dataIn.length; i++)
            dataOut[i] ^= dataIn[i];

        tfc.init(false, pft);
        tfc.processBlock(dataOut, 0, result, 0);
        if (!Arrays.equals(dataIn, result)) {
            hexdump("Decrypt failed 1024 01", result, result.length);
            return false;
        }
        return true;
View Full Code Here

            ConfigString[1] = sourceHash.getHashSize();
        }

        void generateConfiguration()
        {
            ThreefishCipher cipher = ThreefishCipher.createCipher(stateSize);
            UbiTweak tweak = new UbiTweak();

            // Initialize the tweak value
            tweak.startNewBlockType(UbiTweak.Config);
            tweak.setFinalBlock(true);
            tweak.setBitsProcessed(32);

            cipher.setTweak(tweak.getTweak());
            cipher.encrypt(ConfigString, ConfigValue);

            ConfigValue[0] ^= ConfigString[0];
            ConfigValue[1] ^= ConfigString[1];
            ConfigValue[2] ^= ConfigString[2];
        }
View Full Code Here

            ConfigValue[2] ^= ConfigString[2];
        }

        void generateConfiguration(long[] initialState)
        {
            ThreefishCipher cipher = ThreefishCipher.createCipher(stateSize);
            UbiTweak tweak = new UbiTweak();

            // Initialize the tweak value
            tweak.startNewBlockType(UbiTweak.Config);
            tweak.setFinalBlock(true);
            tweak.setBitsProcessed(32);

            cipher.setKey(initialState);
            cipher.setTweak(tweak.getTweak());
            cipher.encrypt(ConfigString, ConfigValue);

            ConfigValue[0] ^= ConfigString[0];
            ConfigValue[1] ^= ConfigString[1];
            ConfigValue[2] ^= ConfigString[2];
        }
View Full Code Here

TOP

Related Classes of org.bouncycastle.crypto.engines.ThreefishCipher

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.