Search in sources :

Example 1 with EphemeralKeyPair

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

the class EthereumIESEngine method processBlock.

public byte[] processBlock(byte[] in, int inOff, int inLen) throws InvalidCipherTextException {
    if (forEncryption) {
        if (keyPairGenerator != null) {
            EphemeralKeyPair ephKeyPair = keyPairGenerator.generate();
            this.privParam = ephKeyPair.getKeyPair().getPrivate();
            this.V = ephKeyPair.getEncodedPublicKey();
        }
    } else {
        if (keyParser != null) {
            ByteArrayInputStream bIn = new ByteArrayInputStream(in, inOff, inLen);
            try {
                this.pubParam = keyParser.readKey(bIn);
            } catch (IOException e) {
                throw new InvalidCipherTextException("unable to recover ephemeral public key: " + e.getMessage(), e);
            } catch (IllegalArgumentException e) {
                throw new InvalidCipherTextException("unable to recover ephemeral public key: " + e.getMessage(), e);
            }
            int encLength = (inLen - bIn.available());
            this.V = Arrays.copyOfRange(in, inOff, inOff + encLength);
        }
    }
    // Compute the common value and convert to byte array.
    agree.init(privParam);
    BigInteger z = agree.calculateAgreement(pubParam);
    byte[] Z = BigIntegers.asUnsignedByteArray(agree.getFieldSize(), z);
    // Create input to KDF.
    if (V.length != 0) {
        byte[] VZ = Arrays.concatenate(V, Z);
        Arrays.fill(Z, (byte) 0);
        Z = VZ;
    }
    try {
        // Initialise the KDF.
        KDFParameters kdfParam = new KDFParameters(Z, param.getDerivationV());
        kdf.init(kdfParam);
        return forEncryption ? encryptBlock(in, inOff, inLen) : decryptBlock(in, inOff, inLen);
    } finally {
        Arrays.fill(Z, (byte) 0);
    }
}
Also used : InvalidCipherTextException(com.github.zhenwei.core.crypto.InvalidCipherTextException) ISO18033KDFParameters(com.github.zhenwei.core.crypto.params.ISO18033KDFParameters) KDFParameters(com.github.zhenwei.core.crypto.params.KDFParameters) ByteArrayInputStream(java.io.ByteArrayInputStream) EphemeralKeyPair(com.github.zhenwei.core.crypto.EphemeralKeyPair) BigInteger(java.math.BigInteger) IOException(java.io.IOException)

Example 2 with EphemeralKeyPair

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

the class IESEngine method processBlock.

public byte[] processBlock(byte[] in, int inOff, int inLen) throws InvalidCipherTextException {
    if (forEncryption) {
        if (keyPairGenerator != null) {
            EphemeralKeyPair ephKeyPair = keyPairGenerator.generate();
            this.privParam = ephKeyPair.getKeyPair().getPrivate();
            this.V = ephKeyPair.getEncodedPublicKey();
        }
    } else {
        if (keyParser != null) {
            ByteArrayInputStream bIn = new ByteArrayInputStream(in, inOff, inLen);
            try {
                this.pubParam = keyParser.readKey(bIn);
            } catch (IOException e) {
                throw new InvalidCipherTextException("unable to recover ephemeral public key: " + e.getMessage(), e);
            } catch (IllegalArgumentException e) {
                throw new InvalidCipherTextException("unable to recover ephemeral public key: " + e.getMessage(), e);
            }
            int encLength = (inLen - bIn.available());
            this.V = Arrays.copyOfRange(in, inOff, inOff + encLength);
        }
    }
    // Compute the common value and convert to byte array.
    agree.init(privParam);
    BigInteger z = agree.calculateAgreement(pubParam);
    byte[] Z = BigIntegers.asUnsignedByteArray(agree.getFieldSize(), z);
    // Create input to KDF.
    if (V.length != 0) {
        byte[] VZ = Arrays.concatenate(V, Z);
        Arrays.fill(Z, (byte) 0);
        Z = VZ;
    }
    try {
        // Initialise the KDF.
        KDFParameters kdfParam = new KDFParameters(Z, param.getDerivationV());
        kdf.init(kdfParam);
        return forEncryption ? encryptBlock(in, inOff, inLen) : decryptBlock(in, inOff, inLen);
    } finally {
        Arrays.fill(Z, (byte) 0);
    }
}
Also used : InvalidCipherTextException(com.github.zhenwei.core.crypto.InvalidCipherTextException) KDFParameters(com.github.zhenwei.core.crypto.params.KDFParameters) ByteArrayInputStream(java.io.ByteArrayInputStream) EphemeralKeyPair(com.github.zhenwei.core.crypto.EphemeralKeyPair) BigInteger(java.math.BigInteger) IOException(java.io.IOException)

Aggregations

EphemeralKeyPair (com.github.zhenwei.core.crypto.EphemeralKeyPair)2 InvalidCipherTextException (com.github.zhenwei.core.crypto.InvalidCipherTextException)2 KDFParameters (com.github.zhenwei.core.crypto.params.KDFParameters)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 BigInteger (java.math.BigInteger)2 ISO18033KDFParameters (com.github.zhenwei.core.crypto.params.ISO18033KDFParameters)1