I am communicating with a device that uses AES-CCM encryption and I need to set that up using a key derived by ECDH. My machine cert has an ECC private key in the TPM.
I'm somewhat new to this so please bear with me.
Here's the code I'm looking at now. Is this the correct way to sign the key with my certificate?
//this will actually be loaded by another method. Only here for demo purposes;
X509Certificate2 masterEndEntityCert;
//we are using the NST p-256 curve
using (var ecdh = new ECDiffieHellmanCng(ECCurve.NamedCurves.nistP256))
{
//our HKDF should be HMAC
ecdh.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hmac;
ecdh.HashAlgorithm = CngAlgorithm.Sha256;
//get the ephemeral key to send to the other device
var ephemeralKey = ecdh.PublicKey.ToByteArray();
//get the ecdsa private key from my cert for signing
using (var alg = masterEndEntityCert.GetECDsaPrivateKey() as ECDsaCng)
{
//sign the sha256 hash of the key
var sig = alg.SignData(ephemeralKey, HashAlgorithmName.SHA256);
//concat the ephemeral key and the signed hash together for transmission
this.SignedEphemeralPublicKey = ephemeralKey.Concat(sig).ToArray();
}
}