ECDH public key size

Russ Beech 41 Reputation points
2022-01-06T16:15:28.3+00:00

I have an ECDiffieHellmanCng(256) object. I expect the public key to be 512 bits - 256 bits for the X value and 256 bits for the Y value - or 64 bytes. When I put the public key into a byte array I end up with 72 bytes. I need to send a 32-byte X value and a 32-byte Y value to a uC, but don't know how to do this when I have 72 bytes to work with. Can anyone explain this, or tell me what I'm doing wrong?

Russ

    class Crypto
    {
        private static byte[] publickey;
        private ECDiffieHellmanCng ECDH;

        public Crypto()
        {
            ECDH = new ECDiffieHellmanCng(256);
            ECDH.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;
            ECDH.HashAlgorithm = CngAlgorithm.Sha256;
            publickey = ECDH.PublicKey.ToByteArray();
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,362 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Russ Beech 41 Reputation points
    2022-01-06T17:21:01.09+00:00

    Answer - it appears that the ECDiffieHellmanCNG object provides the public key with 8 bytes of header information:
    UINT32 Magic
    UINT32 cbkey

    where Magic is some code, in my case, when interpreted as ASCII, "ECK1", and cbkey is the key size, in my case 20 00 00 00 - or 32 bytes. So, for my 72 bytes: the first 8 can be tossed; the next 32 are the key's X value; and the last 32 are the key's Y value. In another use case, the first 8 bytes might need to be transmitted also - depending on what's at the other end (the ECDH library used with my uC doesn't need them).

    (Clarifications, corrections, expansion to this explanation certainly welcome.)

    Russ

    0 comments No comments