Search in sources :

Example 26 with DHParameterSpec

use of javax.crypto.spec.DHParameterSpec in project wycheproof by google.

the class DhiesTest method testDhiesBasic.

/**
   * WARNING: This test uses weak crypto (i.e. DHIESWithAES), if supported. Checks that key
   * agreement using DHIES works in the sense that it can decrypt what it encrypts. Unfortunately it
   * seems that there is no secure mode using AES.
   */
@SuppressWarnings("InsecureCryptoUsage")
public void testDhiesBasic() throws Exception {
    DHParameterSpec params = ike2048();
    KeyPairGenerator kf = KeyPairGenerator.getInstance("DH");
    kf.initialize(params);
    KeyPair keyPair = kf.generateKeyPair();
    PrivateKey priv = keyPair.getPrivate();
    PublicKey pub = keyPair.getPublic();
    byte[] message = "Hello".getBytes("UTF-8");
    Cipher dhies;
    try {
        dhies = Cipher.getInstance("DHIESwithAES");
    } catch (NoSuchAlgorithmException ex) {
        // The algorithm isn't supported - even better!
        return;
    }
    dhies.init(Cipher.ENCRYPT_MODE, pub);
    byte[] ciphertext = dhies.doFinal(message);
    System.out.println("testDhiesBasic:" + TestUtil.bytesToHex(ciphertext));
    dhies.init(Cipher.DECRYPT_MODE, priv);
    byte[] decrypted = dhies.doFinal(ciphertext);
    assertEquals(TestUtil.bytesToHex(message), TestUtil.bytesToHex(decrypted));
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) DHParameterSpec(javax.crypto.spec.DHParameterSpec) KeyPairGenerator(java.security.KeyPairGenerator) Cipher(javax.crypto.Cipher) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 27 with DHParameterSpec

use of javax.crypto.spec.DHParameterSpec in project robovm by robovm.

the class BCDHPrivateKey method readObject.

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    in.defaultReadObject();
    this.dhSpec = new DHParameterSpec((BigInteger) in.readObject(), (BigInteger) in.readObject(), in.readInt());
    this.info = null;
    this.attrCarrier = new PKCS12BagAttributeCarrierImpl();
}
Also used : PKCS12BagAttributeCarrierImpl(org.bouncycastle.jcajce.provider.asymmetric.util.PKCS12BagAttributeCarrierImpl) BigInteger(java.math.BigInteger) DHParameterSpec(javax.crypto.spec.DHParameterSpec)

Example 28 with DHParameterSpec

use of javax.crypto.spec.DHParameterSpec in project robovm by robovm.

the class KeyAgreementSpi method engineInit.

protected void engineInit(Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    if (!(key instanceof DHPrivateKey)) {
        throw new InvalidKeyException("DHKeyAgreement requires DHPrivateKey for initialisation");
    }
    DHPrivateKey privKey = (DHPrivateKey) key;
    if (params != null) {
        if (!(params instanceof DHParameterSpec)) {
            throw new InvalidAlgorithmParameterException("DHKeyAgreement only accepts DHParameterSpec");
        }
        DHParameterSpec p = (DHParameterSpec) params;
        this.p = p.getP();
        this.g = p.getG();
    } else {
        this.p = privKey.getParams().getP();
        this.g = privKey.getParams().getG();
    }
    this.x = this.result = privKey.getX();
}
Also used : DHPrivateKey(javax.crypto.interfaces.DHPrivateKey) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) DHParameterSpec(javax.crypto.spec.DHParameterSpec) InvalidKeyException(java.security.InvalidKeyException)

Example 29 with DHParameterSpec

use of javax.crypto.spec.DHParameterSpec in project robovm by robovm.

the class KeyPairGeneratorSpi method initialize.

public void initialize(AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException {
    if (!(params instanceof DHParameterSpec)) {
        throw new InvalidAlgorithmParameterException("parameter object not a DHParameterSpec");
    }
    DHParameterSpec dhParams = (DHParameterSpec) params;
    param = new DHKeyGenerationParameters(random, new DHParameters(dhParams.getP(), dhParams.getG(), null, dhParams.getL()));
    engine.init(param);
    initialised = true;
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) DHParameters(org.bouncycastle.crypto.params.DHParameters) DHKeyGenerationParameters(org.bouncycastle.crypto.params.DHKeyGenerationParameters) DHParameterSpec(javax.crypto.spec.DHParameterSpec)

Example 30 with DHParameterSpec

use of javax.crypto.spec.DHParameterSpec in project robovm by robovm.

the class KeyPairGeneratorSpi method generateKeyPair.

public KeyPair generateKeyPair() {
    if (!initialised) {
        Integer paramStrength = Integers.valueOf(strength);
        if (params.containsKey(paramStrength)) {
            param = (DHKeyGenerationParameters) params.get(paramStrength);
        } else {
            DHParameterSpec dhParams = BouncyCastleProvider.CONFIGURATION.getDHDefaultParameters(strength);
            if (dhParams != null) {
                param = new DHKeyGenerationParameters(random, new DHParameters(dhParams.getP(), dhParams.getG(), null, dhParams.getL()));
            } else {
                synchronized (lock) {
                    // our key size.
                    if (params.containsKey(paramStrength)) {
                        param = (DHKeyGenerationParameters) params.get(paramStrength);
                    } else {
                        DHParametersGenerator pGen = new DHParametersGenerator();
                        pGen.init(strength, certainty, random);
                        param = new DHKeyGenerationParameters(random, pGen.generateParameters());
                        params.put(paramStrength, param);
                    }
                }
            }
        }
        engine.init(param);
        initialised = true;
    }
    AsymmetricCipherKeyPair pair = engine.generateKeyPair();
    DHPublicKeyParameters pub = (DHPublicKeyParameters) pair.getPublic();
    DHPrivateKeyParameters priv = (DHPrivateKeyParameters) pair.getPrivate();
    return new KeyPair(new BCDHPublicKey(pub), new BCDHPrivateKey(priv));
}
Also used : KeyPair(java.security.KeyPair) AsymmetricCipherKeyPair(org.bouncycastle.crypto.AsymmetricCipherKeyPair) DHPublicKeyParameters(org.bouncycastle.crypto.params.DHPublicKeyParameters) DHParameters(org.bouncycastle.crypto.params.DHParameters) DHPrivateKeyParameters(org.bouncycastle.crypto.params.DHPrivateKeyParameters) DHKeyGenerationParameters(org.bouncycastle.crypto.params.DHKeyGenerationParameters) DHParameterSpec(javax.crypto.spec.DHParameterSpec) DHParametersGenerator(org.bouncycastle.crypto.generators.DHParametersGenerator) AsymmetricCipherKeyPair(org.bouncycastle.crypto.AsymmetricCipherKeyPair)

Aggregations

DHParameterSpec (javax.crypto.spec.DHParameterSpec)44 BigInteger (java.math.BigInteger)18 KeyPair (java.security.KeyPair)13 KeyPairGenerator (java.security.KeyPairGenerator)13 KeyAgreement (javax.crypto.KeyAgreement)10 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)7 SecureRandom (java.security.SecureRandom)7 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)6 InvalidKeyException (java.security.InvalidKeyException)6 KeyFactory (java.security.KeyFactory)6 PublicKey (java.security.PublicKey)6 DHPrivateKey (javax.crypto.interfaces.DHPrivateKey)6 DHPublicKeySpec (javax.crypto.spec.DHPublicKeySpec)6 DHPublicKey (javax.crypto.interfaces.DHPublicKey)5 IOException (java.io.IOException)4 PrivateKey (java.security.PrivateKey)4 CertificateException (java.security.cert.CertificateException)4 X509Certificate (java.security.cert.X509Certificate)4 X509ExtendedKeyManager (javax.net.ssl.X509ExtendedKeyManager)4 X509KeyManager (javax.net.ssl.X509KeyManager)4