crypto_passwd_to_dh.c 916 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include <stdint.h>
  2. #include <string.h>
  3. #include "crypto_dh.h"
  4. #include "ctassert.h"
  5. #include "crypto.h"
  6. /* We use HMAC-SHA256 to generate a DH private key; so the size must match. */
  7. CTASSERT(CRYPTO_DH_PRIVLEN == 32);
  8. /**
  9. * crypto_passwd_to_dh(passwd, salt, pub, priv):
  10. * Generate a Diffie-Hellman pair (${priv}, ${pub}), with ${pub} equal to
  11. * 2^(2^258 + ${priv}) modulo the group #14 modulus, and ${priv} equal to
  12. * HMAC(${salt}, ${passwd}), where ${passwd} is a NUL-terminated string.
  13. */
  14. int
  15. crypto_passwd_to_dh(const char * passwd, const uint8_t salt[32],
  16. uint8_t pub[CRYPTO_DH_PUBLEN], uint8_t priv[CRYPTO_DH_PRIVLEN])
  17. {
  18. /* Generate private key via HMAC. */
  19. crypto_hash_data_key(salt, 32,
  20. (const uint8_t *)passwd, strlen(passwd), priv);
  21. /* Generate public part. */
  22. if (crypto_dh_generate_pub(pub, priv))
  23. goto err0;
  24. /* Success! */
  25. return (0);
  26. err0:
  27. /* Failure! */
  28. return (-1);
  29. }