2

I'm trying to use HKDF after Diffie-Hellman key exchange. However, I got stuck on the HKDF part.

from Crypto.Protocol import HKDF
from Crypto.Hash import SHA512
from Crypto.Random import get_random_bytes

salt = get_random_bytes(16) key1, key2 = HKDF(master_secret, 32, salt, SHA512, 2)

From my understanding, I only need one key for ChaCha20 or AES-256-GCM. Is it a good idea if I only generate one key by HKDF?

One more thing, how can I generate a good salt for this case?

forest
  • 15,626
  • 2
  • 49
  • 103
Lyf Lan
  • 21
  • 1

1 Answers1

2

From my understanding, I only need one key for CHACHA20 or AES-256-GCM. Is it a good idea if I only generate one key by HKDF?

In one direction yes, as the MAC and encryption part only requires one key for ChaCha20/Poly1305 and AES-GCM. If you need multiple key parts you can either increase output size and split the result, or you can call HKDF twice using different labels (in the Info component).

Note that you may want to explicitly verify that you generated the same master key on both sides, preferably that should be performed with a separate derived key (and, e.g., HMAC).

One more thing, how can I generate a good salt for this case?

A cryptographic 128 bit salt is fine, as your code seems to be doing at this time.

forest
  • 15,626
  • 2
  • 49
  • 103
Maarten Bodewes
  • 96,351
  • 14
  • 169
  • 323