/// <summary> /// Derives the key material that is generated from the secret agreement between two parties, given an ECDiffieHellmanPublicKey object that contains the second party's public key. /// (Secret Prepend/Append Not Implemented yet) /// </summary> /// <param name="otherPartyPublicKey">A byte array that contains the public part of the Elliptic Curve Diffie-Hellman (ECDH) key from the other party in the key exchange.</param> /// <returns>A byte array that contains the key material. This information is generated from the secret agreement that is calculated from the current object's private key and the specified public key.</returns> public CryptoKey DeriveKeyMaterial(byte[] otherPartyPublicKey) { if (otherPartyPublicKey == null) { throw new ArgumentNullException(); } ECDH1_PARAMS ecData = new ECDH1_PARAMS(); ecData.PeerPublicData = otherPartyPublicKey; if (KeyDerivationFunction == ECDiffieHellmanKeyDerivationFunction.Hash) { //byte[] secretAppend = SecretAppend == null ? null : SecretAppend.Clone() as byte[]; //byte[] secretPrepend = SecretPrepend == null ? null : SecretPrepend.Clone() as byte[]; switch (m_hashAlgorithm) { case MechanismType.SHA_1: case MechanismType.SHA1_KEY_DERIVATION: ecData.KDF = MechanismType.SHA1_KEY_DERIVATION; break; case MechanismType.SHA256_KEY_DERIVATION: case MechanismType.SHA256: ecData.KDF = MechanismType.SHA256_KEY_DERIVATION; break; case MechanismType.SHA384_KEY_DERIVATION: case MechanismType.SHA384: ecData.KDF = MechanismType.SHA256_KEY_DERIVATION; break; case MechanismType.SHA512_KEY_DERIVATION: case MechanismType.SHA512: ecData.KDF = MechanismType.SHA512_KEY_DERIVATION; break; case MechanismType.MD5_KEY_DERIVATION: case MechanismType.MD5: ecData.KDF = MechanismType.MD5_KEY_DERIVATION; break; default: ecData.KDF = m_hashAlgorithm; break; } } else if (KeyDerivationFunction == ECDiffieHellmanKeyDerivationFunction.Hmac) { //byte[] hmacKey = HmacKey == null ? null : HmacKey.Clone() as byte[]; //byte[] secretAppend = SecretAppend == null ? null : SecretAppend.Clone() as byte[]; //byte[] secretPrepend = SecretPrepend == null ? null : SecretPrepend.Clone() as byte[]; ecData.KDF = MechanismType.SHA256_HMAC; } else if (KeyDerivationFunction == ECDiffieHellmanKeyDerivationFunction.Tls) { //byte[] label = Label == null ? null : Label.Clone() as byte[]; //byte[] seed = Seed == null ? null : Seed.Clone() as byte[]; //if (label == null || seed == null) //{ // throw new InvalidOperationException(); //} ecData.KDF = MechanismType.TLS_MASTER_KEY_DERIVE_DH; } else { ecData.KDF = MechanismType.NULL_KEY_DERIVATION; } return(KeyPair.DeriveKey(new Mechanism(MechanismType.ECDH1_DERIVE, ecData.ToArray()), null)); }
/// <summary> /// Derives the key material that is generated from the secret agreement between two parties, given an ECDiffieHellmanPublicKey object that contains the second party's public key. /// (Secret Prepend/Append Not Implemented yet) /// </summary> /// <param name="otherPartyPublicKey">A byte array that contains the public part of the Elliptic Curve Diffie-Hellman (ECDH) key from the other party in the key exchange.</param> /// <returns>A byte array that contains the key material. This information is generated from the secret agreement that is calculated from the current object's private key and the specified public key.</returns> public CryptoKey DeriveKeyMaterial(byte[] otherPartyPublicKey) { if (otherPartyPublicKey == null) { throw new ArgumentNullException(); } ECDH1_PARAMS ecData = new ECDH1_PARAMS(); ecData.PeerPublicData = otherPartyPublicKey; if (KeyDerivationFunction == ECDiffieHellmanKeyDerivationFunction.Hash) { //byte[] secretAppend = SecretAppend == null ? null : SecretAppend.Clone() as byte[]; //byte[] secretPrepend = SecretPrepend == null ? null : SecretPrepend.Clone() as byte[]; switch(m_hashAlgorithm) { case MechanismType.SHA_1: case MechanismType.SHA1_KEY_DERIVATION: ecData.KDF = MechanismType.SHA1_KEY_DERIVATION; break; case MechanismType.SHA256_KEY_DERIVATION: case MechanismType.SHA256: ecData.KDF = MechanismType.SHA256_KEY_DERIVATION; break; case MechanismType.SHA384_KEY_DERIVATION: case MechanismType.SHA384: ecData.KDF = MechanismType.SHA256_KEY_DERIVATION; break; case MechanismType.SHA512_KEY_DERIVATION: case MechanismType.SHA512: ecData.KDF = MechanismType.SHA512_KEY_DERIVATION; break; case MechanismType.MD5_KEY_DERIVATION: case MechanismType.MD5: ecData.KDF = MechanismType.MD5_KEY_DERIVATION; break; default: ecData.KDF = m_hashAlgorithm; break; } } else if (KeyDerivationFunction == ECDiffieHellmanKeyDerivationFunction.Hmac) { //byte[] hmacKey = HmacKey == null ? null : HmacKey.Clone() as byte[]; //byte[] secretAppend = SecretAppend == null ? null : SecretAppend.Clone() as byte[]; //byte[] secretPrepend = SecretPrepend == null ? null : SecretPrepend.Clone() as byte[]; ecData.KDF = MechanismType.SHA256_HMAC; } else if(KeyDerivationFunction == ECDiffieHellmanKeyDerivationFunction.Tls) { //byte[] label = Label == null ? null : Label.Clone() as byte[]; //byte[] seed = Seed == null ? null : Seed.Clone() as byte[]; //if (label == null || seed == null) //{ // throw new InvalidOperationException(); //} ecData.KDF = MechanismType.TLS_MASTER_KEY_DERIVE_DH; } else { ecData.KDF = MechanismType.NULL_KEY_DERIVATION; } return KeyPair.DeriveKey(new Mechanism(MechanismType.ECDH1_DERIVE, ecData.ToArray()), null); }