curve25519.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. package curve25519 // import "golang.org/x/crypto/curve25519"
  8. import (
  9. "crypto/subtle"
  10. "fmt"
  11. )
  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. scalarMult(dst, scalar, point)
  19. }
  20. // ScalarBaseMult sets dst to the product scalar * base where base is the
  21. // standard generator.
  22. //
  23. // It is recommended to use the X25519 function with Basepoint instead, as
  24. // copying into fixed size arrays can lead to unexpected bugs.
  25. func ScalarBaseMult(dst, scalar *[32]byte) {
  26. ScalarMult(dst, scalar, &basePoint)
  27. }
  28. const (
  29. // ScalarSize is the size of the scalar input to X25519.
  30. ScalarSize = 32
  31. // PointSize is the size of the point input to X25519.
  32. PointSize = 32
  33. )
  34. // Basepoint is the canonical Curve25519 generator.
  35. var Basepoint []byte
  36. var basePoint = [32]byte{9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
  37. func init() { Basepoint = basePoint[:] }
  38. func checkBasepoint() {
  39. if subtle.ConstantTimeCompare(Basepoint, []byte{
  40. 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  41. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  42. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  43. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  44. }) != 1 {
  45. panic("curve25519: global Basepoint value was modified")
  46. }
  47. }
  48. // X25519 returns the result of the scalar multiplication (scalar * point),
  49. // according to RFC 7748, Section 5. scalar, point and the return value are
  50. // slices of 32 bytes.
  51. //
  52. // scalar can be generated at random, for example with crypto/rand. point should
  53. // be either Basepoint or the output of another X25519 call.
  54. //
  55. // If point is Basepoint (but not if it's a different slice with the same
  56. // contents) a precomputed implementation might be used for performance.
  57. func X25519(scalar, point []byte) ([]byte, error) {
  58. // Outline the body of function, to let the allocation be inlined in the
  59. // caller, and possibly avoid escaping to the heap.
  60. var dst [32]byte
  61. return x25519(&dst, scalar, point)
  62. }
  63. func x25519(dst *[32]byte, scalar, point []byte) ([]byte, error) {
  64. var in [32]byte
  65. if l := len(scalar); l != 32 {
  66. return nil, fmt.Errorf("bad scalar length: %d, expected %d", l, 32)
  67. }
  68. if l := len(point); l != 32 {
  69. return nil, fmt.Errorf("bad point length: %d, expected %d", l, 32)
  70. }
  71. copy(in[:], scalar)
  72. if &point[0] == &Basepoint[0] {
  73. checkBasepoint()
  74. ScalarBaseMult(dst, &in)
  75. } else {
  76. var base, zero [32]byte
  77. copy(base[:], point)
  78. ScalarMult(dst, &in, &base)
  79. if subtle.ConstantTimeCompare(dst[:], zero[:]) == 1 {
  80. return nil, fmt.Errorf("bad input point: low order point")
  81. }
  82. }
  83. return dst[:], nil
  84. }