Search in sources :

Example 11 with SHA256Digest

use of com.github.zhenwei.core.crypto.digests.SHA256Digest 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 12 with SHA256Digest

use of com.github.zhenwei.core.crypto.digests.SHA256Digest in project LinLong-Java by zhenwei1108.

the class JPAKEExample method deriveSessionKey.

private static BigInteger deriveSessionKey(BigInteger keyingMaterial) {
    /*
     * You should use a secure key derivation function (KDF) to derive the session key.
     *
     * For the purposes of this example, I'm just going to use a hash of the keying material.
     */
    SHA256Digest digest = new SHA256Digest();
    byte[] keyByteArray = keyingMaterial.toByteArray();
    byte[] output = new byte[digest.getDigestSize()];
    digest.update(keyByteArray, 0, keyByteArray.length);
    digest.doFinal(output, 0);
    return new BigInteger(output);
}
Also used : SHA256Digest(com.github.zhenwei.core.crypto.digests.SHA256Digest) BigInteger(java.math.BigInteger)

Example 13 with SHA256Digest

use of com.github.zhenwei.core.crypto.digests.SHA256Digest in project LinLong-Java by zhenwei1108.

the class CramerShoupParametersGenerator method generateParameters.

public CramerShoupParameters generateParameters(DHParameters dhParams) {
    BigInteger p = dhParams.getP();
    BigInteger g1 = dhParams.getG();
    // now we just need a second generator
    BigInteger g2 = ParametersHelper.selectGenerator(p, random);
    while (g1.equals(g2)) {
        g2 = ParametersHelper.selectGenerator(p, random);
    }
    return new CramerShoupParameters(p, g1, g2, new SHA256Digest());
}
Also used : CramerShoupParameters(com.github.zhenwei.core.crypto.params.CramerShoupParameters) SHA256Digest(com.github.zhenwei.core.crypto.digests.SHA256Digest) BigInteger(java.math.BigInteger)

Aggregations

SHA256Digest (com.github.zhenwei.core.crypto.digests.SHA256Digest)13 SHA512Digest (com.github.zhenwei.core.crypto.digests.SHA512Digest)4 BigInteger (java.math.BigInteger)4 Digest (com.github.zhenwei.core.crypto.Digest)3 SHAKEDigest (com.github.zhenwei.core.crypto.digests.SHAKEDigest)3 KeyParameter (com.github.zhenwei.core.crypto.params.KeyParameter)3 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)3 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)2 ExtendedDigest (com.github.zhenwei.core.crypto.ExtendedDigest)2 SHA1Digest (com.github.zhenwei.core.crypto.digests.SHA1Digest)2 SHA224Digest (com.github.zhenwei.core.crypto.digests.SHA224Digest)2 SHA384Digest (com.github.zhenwei.core.crypto.digests.SHA384Digest)2 DSAParametersGenerator (com.github.zhenwei.core.crypto.generators.DSAParametersGenerator)2 AsymmetricKeyParameter (com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter)2 CramerShoupParameters (com.github.zhenwei.core.crypto.params.CramerShoupParameters)2 DSAParameterGenerationParameters (com.github.zhenwei.core.crypto.params.DSAParameterGenerationParameters)2 IESWithCipherParameters (com.github.zhenwei.core.crypto.params.IESWithCipherParameters)2 ParametersWithIV (com.github.zhenwei.core.crypto.params.ParametersWithIV)2 HashMap (java.util.HashMap)2 AsymmetricCipherKeyPair (com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)1