public AsymmetricCipherKeyPair generateKeyPair() { BigInteger p, g, x, y; int qLength = param.getStrength() - 1; DHParameters dhParams = param.getParameters(); p = dhParams.getP(); g = dhParams.getG(); // // calculate the private key // x = new BigInteger(qLength, param.getRandom()); // // calculate the public key. // y = g.modPow(x, p); return new AsymmetricCipherKeyPair( new DHPublicKeyParameters(y, dhParams), new DHPrivateKeyParameters(x, dhParams)); }
public void init( CipherParameters param) { AsymmetricKeyParameter kParam; if (param instanceof ParametersWithRandom) { ParametersWithRandom rParam = (ParametersWithRandom)param; kParam = (AsymmetricKeyParameter)rParam.getParameters(); } else { kParam = (AsymmetricKeyParameter)param; } if (!(kParam instanceof DHPrivateKeyParameters)) { throw new IllegalArgumentException("DHEngine expects DHPrivateKeyParameters"); } this.key = (DHPrivateKeyParameters)kParam; this.dhParams = key.getParameters(); }
static public AsymmetricKeyParameter generatePrivateKeyParameter( PrivateKey key) throws InvalidKeyException { if (key instanceof DHPrivateKey) { DHPrivateKey k = (DHPrivateKey)key; return new DHPrivateKeyParameters(k.getX(), new DHParameters(k.getParams().getP(), k.getParams().getG(), null, k.getParams().getL())); } throw new InvalidKeyException("can't identify DH private key."); }
public static byte[] calculateDHBasicAgreement(DHPublicKeyParameters publicKey, DHPrivateKeyParameters privateKey) { DHBasicAgreement basicAgreement = new DHBasicAgreement(); basicAgreement.init(privateKey); BigInteger agreementValue = basicAgreement.calculateAgreement(publicKey); /* * RFC 5246 8.1.2. Leading bytes of Z that contain all zero bits are stripped before it is * used as the pre_master_secret. */ return BigIntegers.asUnsignedByteArray(agreementValue); }
public static DHPrivateKeyParameters generateEphemeralClientKeyExchange(SecureRandom random, DHParameters dhParams, OutputStream output) throws IOException { AsymmetricCipherKeyPair dhAgreeClientKeyPair = generateDHKeyPair(random, dhParams); DHPrivateKeyParameters dhAgreeClientPrivateKey = (DHPrivateKeyParameters)dhAgreeClientKeyPair .getPrivate(); BigInteger Yc = ((DHPublicKeyParameters)dhAgreeClientKeyPair.getPublic()).getY(); byte[] keData = BigIntegers.asUnsignedByteArray(Yc); TlsUtils.writeOpaque16(keData, output); return dhAgreeClientPrivateKey; }
/** * calculate our initial message. */ public BigInteger calculateMessage() { DHKeyPairGenerator dhGen = new DHKeyPairGenerator(); dhGen.init(new DHKeyGenerationParameters(random, dhParams)); AsymmetricCipherKeyPair dhPair = dhGen.generateKeyPair(); this.privateValue = ((DHPrivateKeyParameters)dhPair.getPrivate()).getX(); return ((DHPublicKeyParameters)dhPair.getPublic()).getY(); }
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)); }
public static DHPrivateKeyParameters generateEphemeralClientKeyExchange(SecureRandom random, DHParameters dhParams, OutputStream output) throws IOException { AsymmetricCipherKeyPair kp = generateDHKeyPair(random, dhParams); DHPublicKeyParameters dhPublic = (DHPublicKeyParameters) kp.getPublic(); writeDHParameter(dhPublic.getY(), output); return (DHPrivateKeyParameters) kp.getPrivate(); }
public static DHPrivateKeyParameters generateEphemeralServerKeyExchange(SecureRandom random, DHParameters dhParams, OutputStream output) throws IOException { AsymmetricCipherKeyPair kp = generateDHKeyPair(random, dhParams); DHPublicKeyParameters dhPublic = (DHPublicKeyParameters)kp.getPublic(); new ServerDHParams(dhPublic).encode(output); return (DHPrivateKeyParameters)kp.getPrivate(); }
private void checkKeySize( int privateValueSize, DHPrivateKeyParameters priv) { if (privateValueSize != 0) { if (priv.getX().bitLength() != privateValueSize) { fail("limited key check failed for key size " + privateValueSize); } } }
private void testGPWithRandom( DHKeyPairGenerator kpGen) { // // generate first pair // AsymmetricCipherKeyPair pair = kpGen.generateKeyPair(); DHPublicKeyParameters pu1 = (DHPublicKeyParameters)pair.getPublic(); DHPrivateKeyParameters pv1 = (DHPrivateKeyParameters)pair.getPrivate(); // // generate second pair // pair = kpGen.generateKeyPair(); DHPublicKeyParameters pu2 = (DHPublicKeyParameters)pair.getPublic(); DHPrivateKeyParameters pv2 = (DHPrivateKeyParameters)pair.getPrivate(); // // two way // DHAgreement e1 = new DHAgreement(); DHAgreement e2 = new DHAgreement(); e1.init(new ParametersWithRandom(pv1, new SecureRandom())); e2.init(new ParametersWithRandom(pv2, new SecureRandom())); BigInteger m1 = e1.calculateMessage(); BigInteger m2 = e2.calculateMessage(); BigInteger k1 = e1.calculateAgreement(pu2, m2); BigInteger k2 = e2.calculateAgreement(pu1, m1); if (!k1.equals(k2)) { fail("basic with random 2-way test failed"); } }
private void testSimpleWithRandom( DHBasicKeyPairGenerator kpGen) { // // generate first pair // AsymmetricCipherKeyPair pair = kpGen.generateKeyPair(); DHPublicKeyParameters pu1 = (DHPublicKeyParameters)pair.getPublic(); DHPrivateKeyParameters pv1 = (DHPrivateKeyParameters)pair.getPrivate(); // // generate second pair // pair = kpGen.generateKeyPair(); DHPublicKeyParameters pu2 = (DHPublicKeyParameters)pair.getPublic(); DHPrivateKeyParameters pv2 = (DHPrivateKeyParameters)pair.getPrivate(); // // two way // DHBasicAgreement e1 = new DHBasicAgreement(); DHBasicAgreement e2 = new DHBasicAgreement(); e1.init(new ParametersWithRandom(pv1, new SecureRandom())); e2.init(new ParametersWithRandom(pv2, new SecureRandom())); BigInteger k1 = e1.calculateAgreement(pu2); BigInteger k2 = e2.calculateAgreement(pu1); if (!k1.equals(k2)) { fail("basic with random 2-way test failed"); } }
public void init( CipherParameters param) { AsymmetricKeyParameter kParam; if (param instanceof ParametersWithRandom) { ParametersWithRandom rParam = (ParametersWithRandom)param; this.random = rParam.getRandom(); kParam = (AsymmetricKeyParameter)rParam.getParameters(); } else { this.random = new SecureRandom(); kParam = (AsymmetricKeyParameter)param; } if (!(kParam instanceof DHPrivateKeyParameters)) { throw new IllegalArgumentException("DHEngine expects DHPrivateKeyParameters"); } this.key = (DHPrivateKeyParameters)kParam; this.dhParams = key.getParameters(); }