Skip to main content
2 of 2
deleted 1 character in body
forest
  • 15.5k
  • 2
  • 49
  • 103

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.

Maarten Bodewes
  • 94.5k
  • 13
  • 165
  • 319