Search in sources :

Example 1 with GCFBBlockCipher

use of com.github.zhenwei.core.crypto.modes.GCFBBlockCipher in project LinLong-Java by zhenwei1108.

the class BaseBlockCipher method engineSetMode.

protected void engineSetMode(String mode) throws NoSuchAlgorithmException {
    if (baseEngine == null) {
        throw new NoSuchAlgorithmException("no mode supported for this algorithm");
    }
    modeName = Strings.toUpperCase(mode);
    if (modeName.equals("ECB")) {
        ivLength = 0;
        cipher = new BufferedGenericBlockCipher(baseEngine);
    } else if (modeName.equals("CBC")) {
        ivLength = baseEngine.getBlockSize();
        cipher = new BufferedGenericBlockCipher(new CBCBlockCipher(baseEngine));
    } else if (modeName.startsWith("OFB")) {
        ivLength = baseEngine.getBlockSize();
        if (modeName.length() != 3) {
            int wordSize = Integer.parseInt(modeName.substring(3));
            cipher = new BufferedGenericBlockCipher(new OFBBlockCipher(baseEngine, wordSize));
        } else {
            cipher = new BufferedGenericBlockCipher(new OFBBlockCipher(baseEngine, 8 * baseEngine.getBlockSize()));
        }
    } else if (modeName.startsWith("CFB")) {
        ivLength = baseEngine.getBlockSize();
        if (modeName.length() != 3) {
            int wordSize = Integer.parseInt(modeName.substring(3));
            cipher = new BufferedGenericBlockCipher(new CFBBlockCipher(baseEngine, wordSize));
        } else {
            cipher = new BufferedGenericBlockCipher(new CFBBlockCipher(baseEngine, 8 * baseEngine.getBlockSize()));
        }
    } else if (modeName.startsWith("PGPCFB")) {
        boolean inlineIV = modeName.equals("PGPCFBWITHIV");
        if (!inlineIV && modeName.length() != 6) {
            throw new NoSuchAlgorithmException("no mode support for " + modeName);
        }
        ivLength = baseEngine.getBlockSize();
        cipher = new BufferedGenericBlockCipher(new PGPCFBBlockCipher(baseEngine, inlineIV));
    } else if (modeName.equals("OPENPGPCFB")) {
        ivLength = 0;
        cipher = new BufferedGenericBlockCipher(new OpenPGPCFBBlockCipher(baseEngine));
    } else if (modeName.equals("FF1")) {
        ivLength = 0;
        cipher = new BufferedFPEBlockCipher(new FPEFF1Engine(baseEngine));
    } else if (modeName.equals("FF3-1")) {
        ivLength = 0;
        cipher = new BufferedFPEBlockCipher(new FPEFF3_1Engine(baseEngine));
    } else if (modeName.equals("SIC")) {
        ivLength = baseEngine.getBlockSize();
        if (ivLength < 16) {
            throw new IllegalArgumentException("Warning: SIC-Mode can become a twotime-pad if the blocksize of the cipher is too small. Use a cipher with a block size of at least 128 bits (e.g. AES)");
        }
        fixedIv = false;
        cipher = new BufferedGenericBlockCipher(new BufferedBlockCipher(new SICBlockCipher(baseEngine)));
    } else if (modeName.equals("CTR")) {
        ivLength = baseEngine.getBlockSize();
        fixedIv = false;
        if (baseEngine instanceof DSTU7624Engine) {
            cipher = new BufferedGenericBlockCipher(new BufferedBlockCipher(new KCTRBlockCipher(baseEngine)));
        } else {
            cipher = new BufferedGenericBlockCipher(new BufferedBlockCipher(new SICBlockCipher(baseEngine)));
        }
    } else if (modeName.equals("GOFB")) {
        ivLength = baseEngine.getBlockSize();
        cipher = new BufferedGenericBlockCipher(new BufferedBlockCipher(new GOFBBlockCipher(baseEngine)));
    } else if (modeName.equals("GCFB")) {
        ivLength = baseEngine.getBlockSize();
        cipher = new BufferedGenericBlockCipher(new BufferedBlockCipher(new GCFBBlockCipher(baseEngine)));
    } else if (modeName.equals("CTS")) {
        ivLength = baseEngine.getBlockSize();
        cipher = new BufferedGenericBlockCipher(new CTSBlockCipher(new CBCBlockCipher(baseEngine)));
    } else if (modeName.equals("CCM")) {
        // CCM nonce 7..13 bytes
        ivLength = 12;
        if (baseEngine instanceof DSTU7624Engine) {
            cipher = new AEADGenericBlockCipher(new KCCMBlockCipher(baseEngine));
        } else {
            cipher = new AEADGenericBlockCipher(new CCMBlockCipher(baseEngine));
        }
    } else if (modeName.equals("OCB")) {
        if (engineProvider != null) {
            /*
         * RFC 7253 4.2. Nonce is a string of no more than 120 bits
         */
            ivLength = 15;
            cipher = new AEADGenericBlockCipher(new OCBBlockCipher(baseEngine, engineProvider.get()));
        } else {
            throw new NoSuchAlgorithmException("can't support mode " + mode);
        }
    } else if (modeName.equals("EAX")) {
        ivLength = baseEngine.getBlockSize();
        cipher = new AEADGenericBlockCipher(new EAXBlockCipher(baseEngine));
    } else if (modeName.equals("GCM-SIV")) {
        ivLength = 12;
        cipher = new AEADGenericBlockCipher(new GCMSIVBlockCipher(baseEngine));
    } else if (modeName.equals("GCM")) {
        if (baseEngine instanceof DSTU7624Engine) {
            ivLength = baseEngine.getBlockSize();
            cipher = new AEADGenericBlockCipher(new KGCMBlockCipher(baseEngine));
        } else {
            ivLength = 12;
            cipher = new AEADGenericBlockCipher(new GCMBlockCipher(baseEngine));
        }
    } else {
        throw new NoSuchAlgorithmException("can't support mode " + mode);
    }
}
Also used : DSTU7624Engine(com.github.zhenwei.core.crypto.engines.DSTU7624Engine) GCFBBlockCipher(com.github.zhenwei.core.crypto.modes.GCFBBlockCipher) EAXBlockCipher(com.github.zhenwei.core.crypto.modes.EAXBlockCipher) CCMBlockCipher(com.github.zhenwei.core.crypto.modes.CCMBlockCipher) KCCMBlockCipher(com.github.zhenwei.core.crypto.modes.KCCMBlockCipher) SICBlockCipher(com.github.zhenwei.core.crypto.modes.SICBlockCipher) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KGCMBlockCipher(com.github.zhenwei.core.crypto.modes.KGCMBlockCipher) OpenPGPCFBBlockCipher(com.github.zhenwei.core.crypto.modes.OpenPGPCFBBlockCipher) CFBBlockCipher(com.github.zhenwei.core.crypto.modes.CFBBlockCipher) GCFBBlockCipher(com.github.zhenwei.core.crypto.modes.GCFBBlockCipher) PGPCFBBlockCipher(com.github.zhenwei.core.crypto.modes.PGPCFBBlockCipher) FPEFF1Engine(com.github.zhenwei.core.crypto.fpe.FPEFF1Engine) CBCBlockCipher(com.github.zhenwei.core.crypto.modes.CBCBlockCipher) GOFBBlockCipher(com.github.zhenwei.core.crypto.modes.GOFBBlockCipher) KCCMBlockCipher(com.github.zhenwei.core.crypto.modes.KCCMBlockCipher) OFBBlockCipher(com.github.zhenwei.core.crypto.modes.OFBBlockCipher) GOFBBlockCipher(com.github.zhenwei.core.crypto.modes.GOFBBlockCipher) OpenPGPCFBBlockCipher(com.github.zhenwei.core.crypto.modes.OpenPGPCFBBlockCipher) OCBBlockCipher(com.github.zhenwei.core.crypto.modes.OCBBlockCipher) GCMSIVBlockCipher(com.github.zhenwei.core.crypto.modes.GCMSIVBlockCipher) KCTRBlockCipher(com.github.zhenwei.core.crypto.modes.KCTRBlockCipher) BufferedBlockCipher(com.github.zhenwei.core.crypto.BufferedBlockCipher) PaddedBufferedBlockCipher(com.github.zhenwei.core.crypto.paddings.PaddedBufferedBlockCipher) CTSBlockCipher(com.github.zhenwei.core.crypto.modes.CTSBlockCipher) OpenPGPCFBBlockCipher(com.github.zhenwei.core.crypto.modes.OpenPGPCFBBlockCipher) PGPCFBBlockCipher(com.github.zhenwei.core.crypto.modes.PGPCFBBlockCipher) FPEFF3_1Engine(com.github.zhenwei.core.crypto.fpe.FPEFF3_1Engine) GCMBlockCipher(com.github.zhenwei.core.crypto.modes.GCMBlockCipher) KGCMBlockCipher(com.github.zhenwei.core.crypto.modes.KGCMBlockCipher)

Example 2 with GCFBBlockCipher

use of com.github.zhenwei.core.crypto.modes.GCFBBlockCipher in project LinLong-Java by zhenwei1108.

the class CryptoProWrapEngine method cryptoProDiversify.

/*
       RFC 4357 6.5.  CryptoPro KEK Diversification Algorithm

       Given a random 64-bit UKM and a GOST 28147-89 key K, this algorithm
       creates a new GOST 28147-89 key K(UKM).

        1) Let K[0] = K;
        2) UKM is split into components a[i,j]:
           UKM = a[0]|..|a[7] (a[i] - byte, a[i,0]..a[i,7] - it's bits)
        3) Let i be 0.
        4) K[1]..K[8] are calculated by repeating the following algorithm
           eight times:
         A) K[i] is split into components k[i,j]:
            K[i] = k[i,0]|k[i,1]|..|k[i,7] (k[i,j] - 32-bit integer)
         B) Vector S[i] is calculated:
            S[i] = ((a[i,0]*k[i,0] + ... + a[i,7]*k[i,7]) mod 2^32) |
            (((~a[i,0])*k[i,0] + ... + (~a[i,7])*k[i,7]) mod 2^32);
         C) K[i+1] = encryptCFB (S[i], K[i], K[i])
         D) i = i + 1
        5) Let K(UKM) be K[8].
   */
private static byte[] cryptoProDiversify(byte[] K, byte[] ukm, byte[] sBox) {
    for (int i = 0; i != 8; i++) {
        int sOn = 0;
        int sOff = 0;
        for (int j = 0; j != 8; j++) {
            int kj = Pack.littleEndianToInt(K, j * 4);
            if (bitSet(ukm[i], j)) {
                sOn += kj;
            } else {
                sOff += kj;
            }
        }
        byte[] s = new byte[8];
        Pack.intToLittleEndian(sOn, s, 0);
        Pack.intToLittleEndian(sOff, s, 4);
        GCFBBlockCipher c = new GCFBBlockCipher(new GOST28147Engine());
        c.init(true, new ParametersWithIV(new ParametersWithSBox(new KeyParameter(K), sBox), s));
        c.processBlock(K, 0, K, 0);
        c.processBlock(K, 8, K, 8);
        c.processBlock(K, 16, K, 16);
        c.processBlock(K, 24, K, 24);
    }
    return K;
}
Also used : ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) GCFBBlockCipher(com.github.zhenwei.core.crypto.modes.GCFBBlockCipher) ParametersWithSBox(com.github.zhenwei.core.crypto.params.ParametersWithSBox) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Aggregations

GCFBBlockCipher (com.github.zhenwei.core.crypto.modes.GCFBBlockCipher)2 BufferedBlockCipher (com.github.zhenwei.core.crypto.BufferedBlockCipher)1 DSTU7624Engine (com.github.zhenwei.core.crypto.engines.DSTU7624Engine)1 FPEFF1Engine (com.github.zhenwei.core.crypto.fpe.FPEFF1Engine)1 FPEFF3_1Engine (com.github.zhenwei.core.crypto.fpe.FPEFF3_1Engine)1 CBCBlockCipher (com.github.zhenwei.core.crypto.modes.CBCBlockCipher)1 CCMBlockCipher (com.github.zhenwei.core.crypto.modes.CCMBlockCipher)1 CFBBlockCipher (com.github.zhenwei.core.crypto.modes.CFBBlockCipher)1 CTSBlockCipher (com.github.zhenwei.core.crypto.modes.CTSBlockCipher)1 EAXBlockCipher (com.github.zhenwei.core.crypto.modes.EAXBlockCipher)1 GCMBlockCipher (com.github.zhenwei.core.crypto.modes.GCMBlockCipher)1 GCMSIVBlockCipher (com.github.zhenwei.core.crypto.modes.GCMSIVBlockCipher)1 GOFBBlockCipher (com.github.zhenwei.core.crypto.modes.GOFBBlockCipher)1 KCCMBlockCipher (com.github.zhenwei.core.crypto.modes.KCCMBlockCipher)1 KCTRBlockCipher (com.github.zhenwei.core.crypto.modes.KCTRBlockCipher)1 KGCMBlockCipher (com.github.zhenwei.core.crypto.modes.KGCMBlockCipher)1 OCBBlockCipher (com.github.zhenwei.core.crypto.modes.OCBBlockCipher)1 OFBBlockCipher (com.github.zhenwei.core.crypto.modes.OFBBlockCipher)1 OpenPGPCFBBlockCipher (com.github.zhenwei.core.crypto.modes.OpenPGPCFBBlockCipher)1 PGPCFBBlockCipher (com.github.zhenwei.core.crypto.modes.PGPCFBBlockCipher)1