private static BigInteger calculateAgreement( AsymmetricCipherKeyPair U1, AsymmetricCipherKeyPair U2, AsymmetricCipherKeyPair V1, AsymmetricCipherKeyPair V2) { ECMqvBasicAgreement u = new ECMqvBasicAgreement(); u.Init(new MqvPrivateParameters( (ECPrivateKeyParameters)U1.Private, (ECPrivateKeyParameters)U2.Private, (ECPublicKeyParameters)U2.Public)); BigInteger ux = u.CalculateAgreement(new MqvPublicParameters( (ECPublicKeyParameters)V1.Public, (ECPublicKeyParameters)V2.Public)); ECMqvBasicAgreement v = new ECMqvBasicAgreement(); v.Init(new MqvPrivateParameters( (ECPrivateKeyParameters)V1.Private, (ECPrivateKeyParameters)V2.Private, (ECPublicKeyParameters)V2.Public)); BigInteger vx = v.CalculateAgreement(new MqvPublicParameters( (ECPublicKeyParameters)U1.Public, (ECPublicKeyParameters)U2.Public)); if (ux.Equals(vx)) { return(ux); } return(null); }
/** * X9.62 - 1998,<br/> * J.3.1, Page 152, ECDSA over the field Fp<br/> * an example with 192 bit prime */ private static IBigInteger CalculateAgreement( IAsymmetricCipherKeyPair u1, IAsymmetricCipherKeyPair u2, IAsymmetricCipherKeyPair v1, IAsymmetricCipherKeyPair v2) { var u = new ECMqvBasicAgreement(); u.Init(new MqvPrivateParameters( (ECPrivateKeyParameters)u1.Private, (ECPrivateKeyParameters)u2.Private, (ECPublicKeyParameters)u2.Public)); IBigInteger ux = u.CalculateAgreement(new MqvPublicParameters( (ECPublicKeyParameters)v1.Public, (ECPublicKeyParameters)v2.Public)); var v = new ECMqvBasicAgreement(); v.Init(new MqvPrivateParameters( (ECPrivateKeyParameters)v1.Private, (ECPrivateKeyParameters)v2.Private, (ECPublicKeyParameters)v2.Public)); IBigInteger vx = v.CalculateAgreement(new MqvPublicParameters( (ECPublicKeyParameters)u1.Public, (ECPublicKeyParameters)u2.Public)); if (ux.Equals(vx)) { return(ux); } return(null); }
/// <summary> /// Derives a shared secret key from a private key and another persons public key /// </summary> /// <param name="myPrivateKey">the private key which is used</param> /// <param name="otherPartyPublicKey">the public key of the other person</param> /// <returns></returns> public byte[] DeriveKey(byte[] myPrivateKey, byte[] otherPartyPublicKey) { ECPrivateKeyParameters privKey = null; try { privKey = (ECPrivateKeyParameters)CreateAsymmetricKeyParameterFromPrivateKeyInfo(myPrivateKey); } catch (InvalidCastException exception) { string message = "Private Key Import Failed!\n" + $"{exception.Message}.\n" + "The contents of the source do not represent a valid EC private key parameter\n" + "Verify that the public key is not corrupted.\n" + "- or - Verify that the correct key is selected."; throw new CryptoException(message, exception); } var mqvParameters = new MqvPrivateParameters(privKey, privKey); var a1 = new ECMqvBasicAgreement(); a1.Init(mqvParameters); ECPublicKeyParameters pubKey = null; try { pubKey = (ECPublicKeyParameters)CreateAsymmetricKeyParameterFromPublicKeyInfo(otherPartyPublicKey); } catch (InvalidCastException exception) { string message = "Public Key Import Failed!\n" + $"{exception.Message}.\n" + "The contents of the source do not represent a valid EC public key parameter\n" + "Verify that the public key is not corrupted.\n" + "- or - Verify that the correct key is selected."; throw new CryptoException(message, exception); } var mqvPubParameters = new MqvPublicParameters(pubKey, pubKey); BigInteger k = null; try { k = a1.CalculateAgreement(mqvPubParameters); } catch (InvalidOperationException exception) { string message = "Key Deriviation Failed!\n" + $"{exception.Message}.\n" + "The public key does not use the same domain parameters as the private key.\n" + "Verify that the correct public key is selected."; throw new CryptoException(message, exception); } return(k.ToByteArrayUnsigned()); }