curve25519.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /* SPDX-License-Identifier: GPL-2.0 OR MIT */
  2. /*
  3. * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
  4. */
  5. #ifndef CURVE25519_H
  6. #define CURVE25519_H
  7. #include <linux/types.h>
  8. #include <linux/random.h>
  9. enum curve25519_lengths {
  10. CURVE25519_KEY_SIZE = 32
  11. };
  12. void curve25519_generic(u8 out[at_least CURVE25519_KEY_SIZE],
  13. const u8 scalar[at_least CURVE25519_KEY_SIZE],
  14. const u8 point[at_least CURVE25519_KEY_SIZE]);
  15. bool __must_check
  16. curve25519(u8 mypublic[at_least CURVE25519_KEY_SIZE],
  17. const u8 secret[at_least CURVE25519_KEY_SIZE],
  18. const u8 basepoint[at_least CURVE25519_KEY_SIZE]);
  19. bool __must_check
  20. curve25519_generate_public(u8 pub[at_least CURVE25519_KEY_SIZE],
  21. const u8 secret[at_least CURVE25519_KEY_SIZE]);
  22. static inline void
  23. curve25519_clamp_secret(u8 secret[at_least CURVE25519_KEY_SIZE])
  24. {
  25. secret[0] &= 248;
  26. secret[31] = (secret[31] & 127) | 64;
  27. }
  28. static inline void
  29. curve25519_generate_secret(u8 secret[at_least CURVE25519_KEY_SIZE])
  30. {
  31. get_random_bytes_wait(secret, CURVE25519_KEY_SIZE);
  32. curve25519_clamp_secret(secret);
  33. }
  34. #endif /* CURVE25519_H */