curve25519.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2019 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. // Package curve25519 provides an implementation of the X25519 function, which
  5. // performs scalar multiplication on the elliptic curve known as Curve25519.
  6. // See RFC 7748.
  7. //
  8. // This package is a wrapper for the X25519 implementation
  9. // in the crypto/ecdh package.
  10. package curve25519
  11. import "crypto/ecdh"
  12. // ScalarMult sets dst to the product scalar * point.
  13. //
  14. // Deprecated: when provided a low-order point, ScalarMult will set dst to all
  15. // zeroes, irrespective of the scalar. Instead, use the X25519 function, which
  16. // will return an error.
  17. func ScalarMult(dst, scalar, point *[32]byte) {
  18. if _, err := x25519(dst, scalar[:], point[:]); err != nil {
  19. // The only error condition for x25519 when the inputs are 32 bytes long
  20. // is if the output would have been the all-zero value.
  21. for i := range dst {
  22. dst[i] = 0
  23. }
  24. }
  25. }
  26. // ScalarBaseMult sets dst to the product scalar * base where base is the
  27. // standard generator.
  28. //
  29. // It is recommended to use the X25519 function with Basepoint instead, as
  30. // copying into fixed size arrays can lead to unexpected bugs.
  31. func ScalarBaseMult(dst, scalar *[32]byte) {
  32. curve := ecdh.X25519()
  33. priv, err := curve.NewPrivateKey(scalar[:])
  34. if err != nil {
  35. panic("curve25519: internal error: scalarBaseMult was not 32 bytes")
  36. }
  37. copy(dst[:], priv.PublicKey().Bytes())
  38. }
  39. const (
  40. // ScalarSize is the size of the scalar input to X25519.
  41. ScalarSize = 32
  42. // PointSize is the size of the point input to X25519.
  43. PointSize = 32
  44. )
  45. // Basepoint is the canonical Curve25519 generator.
  46. var Basepoint []byte
  47. var basePoint = [32]byte{9}
  48. func init() { Basepoint = basePoint[:] }
  49. // X25519 returns the result of the scalar multiplication (scalar * point),
  50. // according to RFC 7748, Section 5. scalar, point and the return value are
  51. // slices of 32 bytes.
  52. //
  53. // scalar can be generated at random, for example with crypto/rand. point should
  54. // be either Basepoint or the output of another X25519 call.
  55. //
  56. // If point is Basepoint (but not if it's a different slice with the same
  57. // contents) a precomputed implementation might be used for performance.
  58. func X25519(scalar, point []byte) ([]byte, error) {
  59. // Outline the body of function, to let the allocation be inlined in the
  60. // caller, and possibly avoid escaping to the heap.
  61. var dst [32]byte
  62. return x25519(&dst, scalar, point)
  63. }
  64. func x25519(dst *[32]byte, scalar, point []byte) ([]byte, error) {
  65. curve := ecdh.X25519()
  66. pub, err := curve.NewPublicKey(point)
  67. if err != nil {
  68. return nil, err
  69. }
  70. priv, err := curve.NewPrivateKey(scalar)
  71. if err != nil {
  72. return nil, err
  73. }
  74. out, err := priv.ECDH(pub)
  75. if err != nil {
  76. return nil, err
  77. }
  78. copy(dst[:], out)
  79. return dst[:], nil
  80. }