Search in sources :

Example 6 with KDFParameters

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

Example 7 with KDFParameters

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

the class BaseKDFBytesGenerator method init.

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

Example 8 with KDFParameters

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

the class ConcatenationKDFGenerator method init.

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

Example 9 with KDFParameters

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

the class ECDHKEKGenerator method generateBytes.

public int generateBytes(byte[] out, int outOff, int len) throws DataLengthException, IllegalArgumentException {
    if (outOff + len > out.length) {
        throw new DataLengthException("output buffer too small");
    }
    // TODO Create an ASN.1 class for this (RFC3278)
    // ECC-CMS-SharedInfo
    ASN1EncodableVector v = new ASN1EncodableVector();
    v.add(new AlgorithmIdentifier(algorithm, DERNull.INSTANCE));
    v.add(new DERTaggedObject(true, 2, new DEROctetString(Pack.intToBigEndian(keySize))));
    try {
        kdf.init(new KDFParameters(z, new DERSequence(v).getEncoded(ASN1Encoding.DER)));
    } catch (IOException e) {
        throw new IllegalArgumentException("unable to initialise kdf: " + e.getMessage());
    }
    return kdf.generateBytes(out, outOff, len);
}
Also used : KDFParameters(com.github.zhenwei.core.crypto.params.KDFParameters) DERSequence(com.github.zhenwei.core.asn1.DERSequence) DataLengthException(com.github.zhenwei.core.crypto.DataLengthException) DERTaggedObject(com.github.zhenwei.core.asn1.DERTaggedObject) ASN1EncodableVector(com.github.zhenwei.core.asn1.ASN1EncodableVector) IOException(java.io.IOException) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)

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