Search in sources :

Example 81 with KeyParameter

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

the class EthereumIESEngine method decryptBlock.

private byte[] decryptBlock(byte[] in_enc, int inOff, int inLen) throws InvalidCipherTextException {
    byte[] M, K, K1, K2;
    int len = 0;
    // Ensure that the length of the input is greater than the MAC in bytes
    if (inLen < V.length + mac.getMacSize()) {
        throw new InvalidCipherTextException("length of input must be greater than the MAC and V combined");
    }
    // note order is important: set up keys, do simple encryptions, check mac, do final encryption.
    if (cipher == null) {
        // Streaming mode.
        K1 = new byte[inLen - V.length - mac.getMacSize()];
        K2 = new byte[param.getMacKeySize() / 8];
        K = new byte[K1.length + K2.length];
        kdf.generateBytes(K, 0, K.length);
        if (V.length != 0) {
            System.arraycopy(K, 0, K2, 0, K2.length);
            System.arraycopy(K, K2.length, K1, 0, K1.length);
        } else {
            System.arraycopy(K, 0, K1, 0, K1.length);
            System.arraycopy(K, K1.length, K2, 0, K2.length);
        }
        // process the message
        M = new byte[K1.length];
        for (int i = 0; i != K1.length; i++) {
            M[i] = (byte) (in_enc[inOff + V.length + i] ^ K1[i]);
        }
    } else {
        // Block cipher mode.
        K1 = new byte[((IESWithCipherParameters) param).getCipherKeySize() / 8];
        K2 = new byte[param.getMacKeySize() / 8];
        K = new byte[K1.length + K2.length];
        kdf.generateBytes(K, 0, K.length);
        System.arraycopy(K, 0, K1, 0, K1.length);
        System.arraycopy(K, K1.length, K2, 0, K2.length);
        CipherParameters cp = new KeyParameter(K1);
        // If IV provide use it to initialize the cipher
        if (IV != null) {
            cp = new ParametersWithIV(cp, IV);
        }
        cipher.init(false, cp);
        M = new byte[cipher.getOutputSize(inLen - V.length - mac.getMacSize())];
        // do initial processing
        len = cipher.processBytes(in_enc, inOff + V.length, inLen - V.length - mac.getMacSize(), M, 0);
    }
    // Convert the length of the encoding vector into a byte array.
    byte[] P2 = param.getEncodingV();
    byte[] L2 = null;
    if (V.length != 0) {
        L2 = getLengthTag(P2);
    }
    // Verify the MAC.
    int end = inOff + inLen;
    byte[] T1 = Arrays.copyOfRange(in_enc, end - mac.getMacSize(), end);
    byte[] T2 = new byte[T1.length];
    // Ethereum change:
    // Instead of initializing the mac with the bytes, we initialize with the hash of the bytes.
    // Old code: mac.init(new KeyParameter(K2));
    Digest hash = new SHA256Digest();
    byte[] K2hash = new byte[hash.getDigestSize()];
    hash.reset();
    hash.update(K2, 0, K2.length);
    hash.doFinal(K2hash, 0);
    mac.init(new KeyParameter(K2hash));
    // we also update the mac with the IV:
    mac.update(IV, 0, IV.length);
    // end of Ethereum change.
    mac.update(in_enc, inOff + V.length, inLen - V.length - T2.length);
    if (P2 != null) {
        mac.update(P2, 0, P2.length);
    }
    if (V.length != 0) {
        mac.update(L2, 0, L2.length);
    }
    // Ethereum change
    mac.update(commonMac, 0, commonMac.length);
    mac.doFinal(T2, 0);
    if (!Arrays.constantTimeAreEqual(T1, T2)) {
        throw new InvalidCipherTextException("invalid MAC");
    }
    if (cipher == null) {
        return M;
    } else {
        len += cipher.doFinal(M, len);
        return Arrays.copyOfRange(M, 0, len);
    }
}
Also used : IESWithCipherParameters(com.github.zhenwei.core.crypto.params.IESWithCipherParameters) CipherParameters(com.github.zhenwei.core.crypto.CipherParameters) ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) InvalidCipherTextException(com.github.zhenwei.core.crypto.InvalidCipherTextException) SHA256Digest(com.github.zhenwei.core.crypto.digests.SHA256Digest) Digest(com.github.zhenwei.core.crypto.Digest) SHA256Digest(com.github.zhenwei.core.crypto.digests.SHA256Digest) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter) AsymmetricKeyParameter(com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter) IESWithCipherParameters(com.github.zhenwei.core.crypto.params.IESWithCipherParameters)

Example 82 with KeyParameter

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

the class VMPCEngine method init.

/**
 * initialise a VMPC 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 ParametersWithIV)) {
        throw new IllegalArgumentException("VMPC init parameters must include an IV");
    }
    ParametersWithIV ivParams = (ParametersWithIV) params;
    if (!(ivParams.getParameters() instanceof KeyParameter)) {
        throw new IllegalArgumentException("VMPC init parameters must include a key");
    }
    KeyParameter key = (KeyParameter) ivParams.getParameters();
    this.workingIV = ivParams.getIV();
    if (workingIV == null || workingIV.length < 1 || workingIV.length > 768) {
        throw new IllegalArgumentException("VMPC requires 1 to 768 bytes of IV");
    }
    this.workingKey = key.getKey();
    initKey(this.workingKey, this.workingIV);
}
Also used : ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 83 with KeyParameter

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

the class XTEAEngine 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 TEA init - " + params.getClass().getName());
    }
    _forEncryption = forEncryption;
    _initialised = true;
    KeyParameter p = (KeyParameter) params;
    setKey(p.getKey());
}
Also used : KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 84 with KeyParameter

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

the class Zuc128CoreEngine method init.

/**
 * initialise a Snow3G 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(final boolean forEncryption, final CipherParameters params) {
    /*
     * encryption and decryption is completely symmetrical, so the 'forEncryption' is
     * irrelevant. (Like 90% of stream ciphers)
     */
    /* Determine parameters */
    CipherParameters myParams = params;
    byte[] newKey = null;
    byte[] newIV = null;
    if ((myParams instanceof ParametersWithIV)) {
        final ParametersWithIV ivParams = (ParametersWithIV) myParams;
        newIV = ivParams.getIV();
        myParams = ivParams.getParameters();
    }
    if (myParams instanceof KeyParameter) {
        final KeyParameter keyParam = (KeyParameter) myParams;
        newKey = keyParam.getKey();
    }
    /* Initialise engine and mark as initialised */
    theIndex = 0;
    theIterations = 0;
    setKeyAndIV(newKey, newIV);
    /* Save reset state */
    theResetState = (Zuc128CoreEngine) copy();
}
Also used : CipherParameters(com.github.zhenwei.core.crypto.CipherParameters) ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 85 with KeyParameter

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

the class DESExample method performEncrypt.

/*
   * This method performs all the encryption and writes
   * the cipher text to the buffered output stream created
   * previously.
   */
private void performEncrypt(byte[] key) {
    // initialise the cipher with the key bytes, for encryption
    cipher.init(true, new KeyParameter(key));
    /*
     * Create some temporary byte arrays for use in
     * encryption, make them a reasonable size so that
     * we don't spend forever reading small chunks from
     * a file.
     *
     * There is no particular reason for using getBlockSize()
     * to determine the size of the input chunk.  It just
     * was a convenient number for the example.
     */
    // int inBlockSize = cipher.getBlockSize() * 5;
    int inBlockSize = 47;
    int outBlockSize = cipher.getOutputSize(inBlockSize);
    byte[] inblock = new byte[inBlockSize];
    byte[] outblock = new byte[outBlockSize];
    /*
     * now, read the file, and output the chunks
     */
    try {
        int inL;
        int outL;
        byte[] rv = null;
        while ((inL = in.read(inblock, 0, inBlockSize)) > 0) {
            outL = cipher.processBytes(inblock, 0, inL, outblock, 0);
            /*
         * Before we write anything out, we need to make sure
         * that we've got something to write out.
         */
            if (outL > 0) {
                rv = Hex.encode(outblock, 0, outL);
                out.write(rv, 0, rv.length);
                out.write('\n');
            }
        }
        try {
            /*
         * Now, process the bytes that are still buffered
         * within the cipher.
         */
            outL = cipher.doFinal(outblock, 0);
            if (outL > 0) {
                rv = Hex.encode(outblock, 0, outL);
                out.write(rv, 0, rv.length);
                out.write('\n');
            }
        } catch (CryptoException ce) {
        }
    } catch (IOException ioeread) {
        ioeread.printStackTrace();
    }
}
Also used : KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter) IOException(java.io.IOException) CryptoException(com.github.zhenwei.core.crypto.CryptoException)

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