curve.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. In cryptography, Curve25519 is an elliptic curve offering 128 bits of security and designed for use with the elliptic
  3. curve Diffie–Hellman (ECDH) key agreement scheme. It is one of the fastest ECC curves and is not covered by any known
  4. patents. The reference implementation is public domain software. The original Curve25519 paper defined it
  5. as a Diffie–Hellman (DH) function.
  6. */
  7. package curve25519
  8. import (
  9. "crypto/rand"
  10. "golang.org/x/crypto/curve25519"
  11. "io"
  12. )
  13. /*
  14. GenerateKey generates a public private key pair using Curve25519.
  15. */
  16. func GenerateKey() (privateKey *[32]byte, publicKey *[32]byte, err error) {
  17. var pub, priv [32]byte
  18. _, err = io.ReadFull(rand.Reader, priv[:])
  19. if err != nil {
  20. return nil, nil, err
  21. }
  22. priv[0] &= 248
  23. priv[31] &= 127
  24. priv[31] |= 64
  25. curve25519.ScalarBaseMult(&pub, &priv)
  26. return &priv, &pub, nil
  27. }
  28. /*
  29. GenerateSharedSecret generates the shared secret with a given public private key pair.
  30. */
  31. func GenerateSharedSecret(priv, pub [32]byte) []byte {
  32. var secret [32]byte
  33. curve25519.ScalarMult(&secret, &priv, &pub)
  34. return secret[:]
  35. }