Search in sources :

Example 1 with KDFParameters

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

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

the class ECIESKeyEncapsulation method deriveKey.

protected KeyParameter deriveKey(int keyLen, byte[] C, byte[] PEH) {
    byte[] kdfInput = PEH;
    if (!SingleHashMode) {
        kdfInput = Arrays.concatenate(C, PEH);
        Arrays.fill(PEH, (byte) 0);
    }
    try {
        // Initialise the KDF
        kdf.init(new KDFParameters(kdfInput, null));
        // Generate the secret key
        byte[] K = new byte[keyLen];
        kdf.generateBytes(K, 0, K.length);
        // Return the ciphertext
        return new KeyParameter(K);
    } finally {
        Arrays.fill(kdfInput, (byte) 0);
    }
}
Also used : KDFParameters(com.github.zhenwei.core.crypto.params.KDFParameters) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 3 with KDFParameters

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

the class RSAKeyEncapsulation method generateKey.

protected KeyParameter generateKey(BigInteger n, BigInteger r, int keyLen) {
    byte[] R = BigIntegers.asUnsignedByteArray((n.bitLength() + 7) / 8, r);
    // Initialise the KDF
    kdf.init(new KDFParameters(R, null));
    // Generate the secret key
    byte[] K = new byte[keyLen];
    kdf.generateBytes(K, 0, K.length);
    return new KeyParameter(K);
}
Also used : KDFParameters(com.github.zhenwei.core.crypto.params.KDFParameters) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter)

Example 4 with KDFParameters

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

the class BrokenKDF2BytesGenerator method init.

public void init(DerivationParameters param) {
    if (!(param instanceof KDFParameters)) {
        throw new IllegalArgumentException("KDF parameters required for generator");
    }
    KDFParameters p = (KDFParameters) param;
    shared = p.getSharedSecret();
    iv = p.getIV();
}
Also used : KDFParameters(com.github.zhenwei.core.crypto.params.KDFParameters)

Example 5 with KDFParameters

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

the class BaseAgreementSpi method getSharedSecretBytes.

private byte[] getSharedSecretBytes(byte[] secret, String oidAlgorithm, int keySize) throws NoSuchAlgorithmException {
    if (kdf != null) {
        if (keySize < 0) {
            throw new NoSuchAlgorithmException("unknown algorithm encountered: " + oidAlgorithm);
        }
        byte[] keyBytes = new byte[keySize / 8];
        if (kdf instanceof DHKEKGenerator) {
            if (oidAlgorithm == null) {
                throw new NoSuchAlgorithmException("algorithm OID is null");
            }
            ASN1ObjectIdentifier oid;
            try {
                oid = new ASN1ObjectIdentifier(oidAlgorithm);
            } catch (IllegalArgumentException e) {
                throw new NoSuchAlgorithmException("no OID for algorithm: " + oidAlgorithm);
            }
            DHKDFParameters params = new DHKDFParameters(oid, keySize, secret, ukmParameters);
            kdf.init(params);
        } else {
            KDFParameters params = new KDFParameters(secret, ukmParameters);
            kdf.init(params);
        }
        kdf.generateBytes(keyBytes, 0, keyBytes.length);
        Arrays.clear(secret);
        return keyBytes;
    } else {
        if (keySize > 0) {
            byte[] keyBytes = new byte[keySize / 8];
            System.arraycopy(secret, 0, keyBytes, 0, keyBytes.length);
            Arrays.clear(secret);
            return keyBytes;
        }
        return secret;
    }
}
Also used : DHKDFParameters(com.github.zhenwei.core.crypto.agreement.kdf.DHKDFParameters) KDFParameters(com.github.zhenwei.core.crypto.params.KDFParameters) DHKEKGenerator(com.github.zhenwei.core.crypto.agreement.kdf.DHKEKGenerator) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) DHKDFParameters(com.github.zhenwei.core.crypto.agreement.kdf.DHKDFParameters) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)

Aggregations

KDFParameters (com.github.zhenwei.core.crypto.params.KDFParameters)9 IOException (java.io.IOException)3 EphemeralKeyPair (com.github.zhenwei.core.crypto.EphemeralKeyPair)2 InvalidCipherTextException (com.github.zhenwei.core.crypto.InvalidCipherTextException)2 ISO18033KDFParameters (com.github.zhenwei.core.crypto.params.ISO18033KDFParameters)2 KeyParameter (com.github.zhenwei.core.crypto.params.KeyParameter)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 BigInteger (java.math.BigInteger)2 ASN1EncodableVector (com.github.zhenwei.core.asn1.ASN1EncodableVector)1 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)1 DEROctetString (com.github.zhenwei.core.asn1.DEROctetString)1 DERSequence (com.github.zhenwei.core.asn1.DERSequence)1 DERTaggedObject (com.github.zhenwei.core.asn1.DERTaggedObject)1 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)1 DataLengthException (com.github.zhenwei.core.crypto.DataLengthException)1 DHKDFParameters (com.github.zhenwei.core.crypto.agreement.kdf.DHKDFParameters)1 DHKEKGenerator (com.github.zhenwei.core.crypto.agreement.kdf.DHKEKGenerator)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1