Search in sources :

Example 1 with DHParameters

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

the class IESCipher method engineDoFinal.

// Finalisation methods
public byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen) throws IllegalBlockSizeException, BadPaddingException {
    if (inputLen != 0) {
        buffer.write(input, inputOffset, inputLen);
    }
    byte[] in = buffer.toByteArray();
    buffer.reset();
    // Convert parameters for use in IESEngine
    CipherParameters params = new IESWithCipherParameters(engineSpec.getDerivationV(), engineSpec.getEncodingV(), engineSpec.getMacKeySize(), engineSpec.getCipherKeySize());
    if (engineSpec.getNonce() != null) {
        params = new ParametersWithIV(params, engineSpec.getNonce());
    }
    DHParameters dhParams = ((DHKeyParameters) key).getParameters();
    byte[] V;
    if (otherKeyParameter != null) {
        try {
            if (state == Cipher.ENCRYPT_MODE || state == Cipher.WRAP_MODE) {
                engine.init(true, otherKeyParameter, key, params);
            } else {
                engine.init(false, key, otherKeyParameter, params);
            }
            return engine.processBlock(in, 0, in.length);
        } catch (Exception e) {
            throw new BadBlockException("unable to process block", e);
        }
    }
    if (state == Cipher.ENCRYPT_MODE || state == Cipher.WRAP_MODE) {
        // Generate the ephemeral key pair
        DHKeyPairGenerator gen = new DHKeyPairGenerator();
        gen.init(new DHKeyGenerationParameters(random, dhParams));
        EphemeralKeyPairGenerator kGen = new EphemeralKeyPairGenerator(gen, new KeyEncoder() {

            public byte[] getEncoded(AsymmetricKeyParameter keyParameter) {
                byte[] Vloc = new byte[(((DHKeyParameters) keyParameter).getParameters().getP().bitLength() + 7) / 8];
                byte[] Vtmp = BigIntegers.asUnsignedByteArray(((DHPublicKeyParameters) keyParameter).getY());
                if (Vtmp.length > Vloc.length) {
                    throw new IllegalArgumentException("Senders's public key longer than expected.");
                } else {
                    System.arraycopy(Vtmp, 0, Vloc, Vloc.length - Vtmp.length, Vtmp.length);
                }
                return Vloc;
            }
        });
        // Encrypt the buffer
        try {
            engine.init(key, params, kGen);
            return engine.processBlock(in, 0, in.length);
        } catch (Exception e) {
            throw new BadBlockException("unable to process block", e);
        }
    } else if (state == Cipher.DECRYPT_MODE || state == Cipher.UNWRAP_MODE) {
        // Decrypt the buffer
        try {
            engine.init(key, params, new DHIESPublicKeyParser(((DHKeyParameters) key).getParameters()));
            return engine.processBlock(in, 0, in.length);
        } catch (InvalidCipherTextException e) {
            throw new BadBlockException("unable to process block", e);
        }
    } else {
        throw new IllegalStateException("IESCipher not initialised");
    }
}
Also used : EphemeralKeyPairGenerator(com.github.zhenwei.core.crypto.generators.EphemeralKeyPairGenerator) DHKeyParameters(com.github.zhenwei.core.crypto.params.DHKeyParameters) BadBlockException(com.github.zhenwei.provider.jcajce.provider.util.BadBlockException) KeyEncoder(com.github.zhenwei.core.crypto.KeyEncoder) InvalidCipherTextException(com.github.zhenwei.core.crypto.InvalidCipherTextException) DHPublicKeyParameters(com.github.zhenwei.core.crypto.params.DHPublicKeyParameters) DHParameters(com.github.zhenwei.core.crypto.params.DHParameters) DHKeyPairGenerator(com.github.zhenwei.core.crypto.generators.DHKeyPairGenerator) DHKeyGenerationParameters(com.github.zhenwei.core.crypto.params.DHKeyGenerationParameters) BadBlockException(com.github.zhenwei.provider.jcajce.provider.util.BadBlockException) InvalidCipherTextException(com.github.zhenwei.core.crypto.InvalidCipherTextException) ShortBufferException(javax.crypto.ShortBufferException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) BadPaddingException(javax.crypto.BadPaddingException) IESWithCipherParameters(com.github.zhenwei.core.crypto.params.IESWithCipherParameters) CipherParameters(com.github.zhenwei.core.crypto.CipherParameters) ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) AsymmetricKeyParameter(com.github.zhenwei.core.crypto.params.AsymmetricKeyParameter) DHIESPublicKeyParser(com.github.zhenwei.core.crypto.parsers.DHIESPublicKeyParser) IESWithCipherParameters(com.github.zhenwei.core.crypto.params.IESWithCipherParameters)

Example 2 with DHParameters

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

the class KeyAgreementSpi method generatePrivateKeyParameter.

private DHPrivateKeyParameters generatePrivateKeyParameter(PrivateKey privKey) throws InvalidKeyException {
    if (privKey instanceof DHPrivateKey) {
        if (privKey instanceof BCDHPrivateKey) {
            return ((BCDHPrivateKey) privKey).engineGetKeyParameters();
        } else {
            DHPrivateKey pub = (DHPrivateKey) privKey;
            DHParameterSpec params = pub.getParams();
            return new DHPrivateKeyParameters(pub.getX(), new DHParameters(params.getP(), params.getG(), null, params.getL()));
        }
    } else {
        throw new InvalidKeyException("private key not a DHPrivateKey");
    }
}
Also used : DHPrivateKey(javax.crypto.interfaces.DHPrivateKey) DHParameters(com.github.zhenwei.core.crypto.params.DHParameters) DHPrivateKeyParameters(com.github.zhenwei.core.crypto.params.DHPrivateKeyParameters) DHParameterSpec(javax.crypto.spec.DHParameterSpec) InvalidKeyException(java.security.InvalidKeyException)

Example 3 with DHParameters

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

the class DHKeyPairGenerator method generateKeyPair.

public AsymmetricCipherKeyPair generateKeyPair() {
    DHKeyGeneratorHelper helper = DHKeyGeneratorHelper.INSTANCE;
    DHParameters dhp = param.getParameters();
    BigInteger x = helper.calculatePrivate(dhp, param.getRandom());
    BigInteger y = helper.calculatePublic(dhp, x);
    return new AsymmetricCipherKeyPair(new DHPublicKeyParameters(y, dhp), new DHPrivateKeyParameters(x, dhp));
}
Also used : DHPublicKeyParameters(com.github.zhenwei.core.crypto.params.DHPublicKeyParameters) DHParameters(com.github.zhenwei.core.crypto.params.DHParameters) DHPrivateKeyParameters(com.github.zhenwei.core.crypto.params.DHPrivateKeyParameters) BigInteger(java.math.BigInteger) AsymmetricCipherKeyPair(com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)

Example 4 with DHParameters

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

the class DHParametersGenerator method generateParameters.

/**
 * which generates the p and g values from the given parameters, returning the DHParameters
 * object.
 * <p>
 * Note: can take a while...
 *
 * @return a generated Diffie-Hellman parameters object.
 */
public DHParameters generateParameters() {
    // 
    // find a safe prime p where p = 2*q + 1, where p and q are prime.
    // 
    BigInteger[] safePrimes = DHParametersHelper.generateSafePrimes(size, certainty, random);
    BigInteger p = safePrimes[0];
    BigInteger q = safePrimes[1];
    BigInteger g = DHParametersHelper.selectGenerator(p, q, random);
    return new DHParameters(p, g, q, TWO, null);
}
Also used : DHParameters(com.github.zhenwei.core.crypto.params.DHParameters) BigInteger(java.math.BigInteger)

Example 5 with DHParameters

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

the class ElGamalKeyPairGenerator method generateKeyPair.

public AsymmetricCipherKeyPair generateKeyPair() {
    DHKeyGeneratorHelper helper = DHKeyGeneratorHelper.INSTANCE;
    ElGamalParameters egp = param.getParameters();
    DHParameters dhp = new DHParameters(egp.getP(), egp.getG(), null, egp.getL());
    BigInteger x = helper.calculatePrivate(dhp, param.getRandom());
    BigInteger y = helper.calculatePublic(dhp, x);
    return new AsymmetricCipherKeyPair(new ElGamalPublicKeyParameters(y, egp), new ElGamalPrivateKeyParameters(x, egp));
}
Also used : ElGamalParameters(com.github.zhenwei.core.crypto.params.ElGamalParameters) DHParameters(com.github.zhenwei.core.crypto.params.DHParameters) ElGamalPrivateKeyParameters(com.github.zhenwei.core.crypto.params.ElGamalPrivateKeyParameters) BigInteger(java.math.BigInteger) ElGamalPublicKeyParameters(com.github.zhenwei.core.crypto.params.ElGamalPublicKeyParameters) AsymmetricCipherKeyPair(com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)

Aggregations

DHParameters (com.github.zhenwei.core.crypto.params.DHParameters)13 BigInteger (java.math.BigInteger)5 DHPrivateKeyParameters (com.github.zhenwei.core.crypto.params.DHPrivateKeyParameters)4 DHPublicKeyParameters (com.github.zhenwei.core.crypto.params.DHPublicKeyParameters)4 DHDomainParameterSpec (com.github.zhenwei.provider.jcajce.spec.DHDomainParameterSpec)4 DHParameterSpec (javax.crypto.spec.DHParameterSpec)4 ASN1Integer (com.github.zhenwei.core.asn1.ASN1Integer)3 DHParameter (com.github.zhenwei.core.asn1.pkcs.DHParameter)3 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)3 AsymmetricCipherKeyPair (com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)3 DHValidationParameters (com.github.zhenwei.core.crypto.params.DHValidationParameters)3 DomainParameters (com.github.zhenwei.core.asn1.x9.DomainParameters)2 ValidationParams (com.github.zhenwei.core.asn1.x9.ValidationParams)2 ElGamalParameters (com.github.zhenwei.core.crypto.params.ElGamalParameters)2 ElGamalPrivateKeyParameters (com.github.zhenwei.core.crypto.params.ElGamalPrivateKeyParameters)2 InvalidKeyException (java.security.InvalidKeyException)2 ASN1Encodable (com.github.zhenwei.core.asn1.ASN1Encodable)1 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)1 ASN1OctetString (com.github.zhenwei.core.asn1.ASN1OctetString)1 ASN1Primitive (com.github.zhenwei.core.asn1.ASN1Primitive)1