curve25519_go120.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright 2022 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //go:build go1.20
  5. package curve25519
  6. import "crypto/ecdh"
  7. func x25519(dst *[32]byte, scalar, point []byte) ([]byte, error) {
  8. curve := ecdh.X25519()
  9. pub, err := curve.NewPublicKey(point)
  10. if err != nil {
  11. return nil, err
  12. }
  13. priv, err := curve.NewPrivateKey(scalar)
  14. if err != nil {
  15. return nil, err
  16. }
  17. out, err := priv.ECDH(pub)
  18. if err != nil {
  19. return nil, err
  20. }
  21. copy(dst[:], out)
  22. return dst[:], nil
  23. }
  24. func scalarMult(dst, scalar, point *[32]byte) {
  25. if _, err := x25519(dst, scalar[:], point[:]); err != nil {
  26. // The only error condition for x25519 when the inputs are 32 bytes long
  27. // is if the output would have been the all-zero value.
  28. for i := range dst {
  29. dst[i] = 0
  30. }
  31. }
  32. }
  33. func scalarBaseMult(dst, scalar *[32]byte) {
  34. curve := ecdh.X25519()
  35. priv, err := curve.NewPrivateKey(scalar[:])
  36. if err != nil {
  37. panic("curve25519: internal error: scalarBaseMult was not 32 bytes")
  38. }
  39. copy(dst[:], priv.PublicKey().Bytes())
  40. }