private void doTestDH( int size, BigInteger g, BigInteger p) { DHKeyPairGenerator kpGen = getDHKeyPairGenerator(g, p); // // generate first pair // AsymmetricCipherKeyPair pair = kpGen.GenerateKeyPair(); DHPublicKeyParameters pu1 = (DHPublicKeyParameters)pair.Public; DHPrivateKeyParameters pv1 = (DHPrivateKeyParameters)pair.Private; // // generate second pair // pair = kpGen.GenerateKeyPair(); DHPublicKeyParameters pu2 = (DHPublicKeyParameters)pair.Public; DHPrivateKeyParameters pv2 = (DHPrivateKeyParameters)pair.Private; // // two way // DHAgreement e1 = new DHAgreement(); DHAgreement e2 = new DHAgreement(); e1.Init(pv1); e2.Init(pv2); 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(size + " bit 2-way test failed"); } }
public override void PerformTest() { doTestDHBasic(512, 0, g512, p512); doTestDHBasic(768, 0, g768, p768); doTestDHBasic(1024, 0, g1024, p1024); doTestDHBasic(512, 64, g512, p512); doTestDHBasic(768, 128, g768, p768); doTestDHBasic(1024, 256, g1024, p1024); doTestDH(512, g512, p512); doTestDH(768, g768, p768); doTestDH(1024, g1024, p1024); // // generation test. // doTestGeneration(256); // // with random test // DHBasicKeyPairGenerator kpBasicGen = getDHBasicKeyPairGenerator(g512, p512, 0); doTestSimpleWithRandom(kpBasicGen); DHKeyPairGenerator kpGen = getDHKeyPairGenerator(g512, p512); doTestGPWithRandom(kpGen); // // parameter tests // DHAgreement dh = new DHAgreement(); AsymmetricCipherKeyPair dhPair = kpGen.GenerateKeyPair(); try { dh.Init(dhPair.Public); Fail("DHAgreement key check failed"); } catch (ArgumentException) { // ignore } DHKeyPairGenerator kpGen768 = getDHKeyPairGenerator(g768, p768); try { dh.Init(dhPair.Private); dh.CalculateAgreement((DHPublicKeyParameters)kpGen768.GenerateKeyPair().Public, BigInteger.ValueOf(100)); Fail("DHAgreement agreement check failed"); } catch (ArgumentException) { // ignore } DHBasicAgreement dhBasic = new DHBasicAgreement(); AsymmetricCipherKeyPair dhBasicPair = kpBasicGen.GenerateKeyPair(); try { dhBasic.Init(dhBasicPair.Public); Fail("DHBasicAgreement key check failed"); } catch (ArgumentException) { // expected } DHBasicKeyPairGenerator kpBasicGen768 = getDHBasicKeyPairGenerator(g768, p768, 0); try { dhBasic.Init(dhPair.Private); dhBasic.CalculateAgreement((DHPublicKeyParameters)kpBasicGen768.GenerateKeyPair().Public); Fail("DHBasicAgreement agreement check failed"); } catch (ArgumentException) { // expected } }
private void doTestGPWithRandom( DHKeyPairGenerator kpGen) { // // generate first pair // AsymmetricCipherKeyPair pair = kpGen.GenerateKeyPair(); DHPublicKeyParameters pu1 = (DHPublicKeyParameters)pair.Public; DHPrivateKeyParameters pv1 = (DHPrivateKeyParameters)pair.Private; // // generate second pair // pair = kpGen.GenerateKeyPair(); DHPublicKeyParameters pu2 = (DHPublicKeyParameters)pair.Public; DHPrivateKeyParameters pv2 = (DHPrivateKeyParameters)pair.Private; // // 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"); } }