Score:0

Use Diffie Hellman to share a secret

br flag

I was trying to use Diffie Hellman to first agree on a key and then use that key to symmetrically share a secret. The python3 code using cryptography library look like

#Generate some parameters. These can be reused.
parameters = dh.generate_parameters(generator=2, key_size=2048)
# Generate a private key for use in the exchange.
server_key = parameters.generate_private_key()
n=1
client_key=[parameters.generate_private_key() for i in range(n)]

#shared key for each client
shared_key = [server_key.exchange(client_key[i].public_key()) for i in range(n)]

Now the size of the shared key is 256 bytes so when I am trying to use AES wrap and unwrap or any symmetric encryption scheme, I am getting errors related to key sizes. Can anyone please help me with what scheme to use of maybe format DH key to be used with different encryption scheme in the crypto library. Thanks

Maarten Bodewes avatar
in flag
You would normally use a KDF one or more times on the *statically sized* output to derive keys. As such, the `shared_key` is probably better named `shared_secret` because, as you found out, it isn't directly usable as a key. For a modern KDF, you can use HKDF. For an quick hack, just use SHA-256.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.