I am trying to implement the internal primitives of ECDH. Currently I'm able to multiply the receiver's public EC point with the sender's private key to arrive at the shared EC point. Next step is to input the x-coordinate of the shared point which is a bignum to a hashing function. The curve is secp521r1 so the shared x-coordinate is of size 66 bytes.
Question 1
What format must the x-coordinate bignum be converted to before hashing it? The other party is openssl.
Is something like below sufficient...
...
print_hex(hash_input_buffer, shared_x_coordinate_bignum_bytes, 66);
...
void print_hex(unsigned char* to, unsigned char* from, int len)
{
for (int i = 0; i < len; ++i)
{
sprintf((char*)to + (i * 2), "%02x", from[i]);
}
}
OR
sprintf(hash_input_buffer, "%64x", shared_x_coordinate_bignum_bytes);
Question 2
If the hashing function is SHA-512 and I want to derive an AES-256 key from it, should I just take the first 32 bytes of the 64 byte hash output?
> openssl cms -encrypt -aes256 ecdh_kdf_md:sha512 -in ... -out ...
– sce Sep 04 '15 at 22:27