Search in sources :

Example 76 with KeyParameter

use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.

the class NoekeonEngine method init.

/**
 * initialise
 *
 * @param forEncryption whether or not we are for encryption.
 * @param params        the parameters required to set up the cipher.
 * @throws IllegalArgumentException if the params argument is inappropriate.
 */
public void init(boolean forEncryption, CipherParameters params) {
    if (!(params instanceof KeyParameter)) {
        throw new IllegalArgumentException("invalid parameter passed to Noekeon init - " + params.getClass().getName());
    }
    KeyParameter p = (KeyParameter) params;
    byte[] key = p.getKey();
    if (key.length != 16) {
        throw new IllegalArgumentException("Key length not 128 bits.");
    }
    Pack.bigEndianToInt(key, 0, k, 0, 4);
    if (!forEncryption) {
        // theta(k, new int[]{ 0x00, 0x00, 0x00, 0x00 });
        {
            int a0 = k[0], a1 = k[1], a2 = k[2], a3 = k[3];
            int t02 = a0 ^ a2;
            t02 ^= Integers.rotateLeft(t02, 8) ^ Integers.rotateLeft(t02, 24);
            int t13 = a1 ^ a3;
            t13 ^= Integers.rotateLeft(t13, 8) ^ Integers.rotateLeft(t13, 24);
            a0 ^= t13;
            a1 ^= t02;
            a2 ^= t13;
            a3 ^= t02;
            k[0] = a0;
            k[1] = a1;
            k[2] = a2;
            k[3] = a3;
        }
    }
    this._forEncryption = forEncryption;
    this._initialised = true;
}
Also used : KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 77 with KeyParameter

use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.

the class RC532Engine method init.

/**
 * initialise a RC5-32 cipher.
 *
 * @param forEncryption whether or not we are for encryption.
 * @param params        the parameters required to set up the cipher.
 * @throws IllegalArgumentException if the params argument is inappropriate.
 */
public void init(boolean forEncryption, CipherParameters params) {
    if (params instanceof RC5Parameters) {
        RC5Parameters p = (RC5Parameters) params;
        _noRounds = p.getRounds();
        setKey(p.getKey());
    } else if (params instanceof KeyParameter) {
        KeyParameter p = (KeyParameter) params;
        setKey(p.getKey());
    } else {
        throw new IllegalArgumentException("invalid parameter passed to RC532 init - " + params.getClass().getName());
    }
    this.forEncryption = forEncryption;
}
Also used : RC5Parameters(com.github.zhenwei.core.crypto.params.RC5Parameters) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 78 with KeyParameter

use of com.github.zhenwei.core.crypto.params.KeyParameter 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)

Example 79 with KeyParameter

use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.

the class CryptoProWrapEngine method init.

public void init(boolean forWrapping, CipherParameters param) {
    if (param instanceof ParametersWithRandom) {
        ParametersWithRandom pr = (ParametersWithRandom) param;
        param = pr.getParameters();
    }
    ParametersWithUKM pU = (ParametersWithUKM) param;
    byte[] sBox = null;
    KeyParameter kParam;
    if (pU.getParameters() instanceof ParametersWithSBox) {
        kParam = (KeyParameter) ((ParametersWithSBox) pU.getParameters()).getParameters();
        sBox = ((ParametersWithSBox) pU.getParameters()).getSBox();
    } else {
        kParam = (KeyParameter) pU.getParameters();
    }
    kParam = new KeyParameter(cryptoProDiversify(kParam.getKey(), pU.getUKM(), sBox));
    if (sBox != null) {
        super.init(forWrapping, new ParametersWithUKM(new ParametersWithSBox(kParam, sBox), pU.getUKM()));
    } else {
        super.init(forWrapping, new ParametersWithUKM(kParam, pU.getUKM()));
    }
}
Also used : ParametersWithUKM(com.github.zhenwei.core.crypto.params.ParametersWithUKM) ParametersWithSBox(com.github.zhenwei.core.crypto.params.ParametersWithSBox) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter) ParametersWithRandom(com.github.zhenwei.core.crypto.params.ParametersWithRandom)

Example 80 with KeyParameter

use of com.github.zhenwei.core.crypto.params.KeyParameter in project LinLong-Java by zhenwei1108.

the class DESedeWrapEngine method init.

/**
 * Method init
 *
 * @param forWrapping true if for wrapping, false otherwise.
 * @param param       necessary parameters, may include KeyParameter, ParametersWithRandom, and
 *                    ParametersWithIV
 */
public void init(boolean forWrapping, CipherParameters param) {
    this.forWrapping = forWrapping;
    this.engine = new CBCBlockCipher(new DESedeEngine());
    SecureRandom sr;
    if (param instanceof ParametersWithRandom) {
        ParametersWithRandom pr = (ParametersWithRandom) param;
        param = pr.getParameters();
        sr = pr.getRandom();
    } else {
        sr = CryptoServicesRegistrar.getSecureRandom();
    }
    if (param instanceof KeyParameter) {
        this.param = (KeyParameter) param;
        if (this.forWrapping) {
            // Hm, we have no IV but we want to wrap ?!?
            // well, then we have to create our own IV.
            this.iv = new byte[8];
            sr.nextBytes(iv);
            this.paramPlusIV = new ParametersWithIV(this.param, this.iv);
        }
    } else if (param instanceof ParametersWithIV) {
        this.paramPlusIV = (ParametersWithIV) param;
        this.iv = this.paramPlusIV.getIV();
        this.param = (KeyParameter) this.paramPlusIV.getParameters();
        if (this.forWrapping) {
            if ((this.iv == null) || (this.iv.length != 8)) {
                throw new IllegalArgumentException("IV is not 8 octets");
            }
        } else {
            throw new IllegalArgumentException("You should not supply an IV for unwrapping");
        }
    }
}
Also used : ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter) SecureRandom(java.security.SecureRandom) ParametersWithRandom(com.github.zhenwei.core.crypto.params.ParametersWithRandom) CBCBlockCipher(com.github.zhenwei.core.crypto.modes.CBCBlockCipher)

Aggregations

KeyParameter (com.github.zhenwei.core.crypto.params.KeyParameter)91 ParametersWithIV (com.github.zhenwei.core.crypto.params.ParametersWithIV)41 CipherParameters (com.github.zhenwei.core.crypto.CipherParameters)15 AEADParameters (com.github.zhenwei.core.crypto.params.AEADParameters)10 InvalidCipherTextException (com.github.zhenwei.core.crypto.InvalidCipherTextException)6 ParametersWithSBox (com.github.zhenwei.core.crypto.params.ParametersWithSBox)6 RC2Parameters (com.github.zhenwei.core.crypto.params.RC2Parameters)6 BigInteger (java.math.BigInteger)6 BufferedBlockCipher (com.github.zhenwei.core.crypto.BufferedBlockCipher)4 StreamCipher (com.github.zhenwei.core.crypto.StreamCipher)4 PKCS5S2ParametersGenerator (com.github.zhenwei.core.crypto.generators.PKCS5S2ParametersGenerator)4 HMac (com.github.zhenwei.core.crypto.macs.HMac)4 AsymmetricKeyParameter (com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter)4 IESWithCipherParameters (com.github.zhenwei.core.crypto.params.IESWithCipherParameters)4 ParametersWithRandom (com.github.zhenwei.core.crypto.params.ParametersWithRandom)4 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)4 IvParameterSpec (javax.crypto.spec.IvParameterSpec)4 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)3 RC5Parameters (com.github.zhenwei.core.crypto.params.RC5Parameters)3 CMSException (com.github.zhenwei.pkix.cms.CMSException)3