...
1
2
3
4
5
6
7 package curve25519
8
9 import "crypto/ecdh"
10
11 func x25519(dst *[32]byte, scalar, point []byte) ([]byte, error) {
12 curve := ecdh.X25519()
13 pub, err := curve.NewPublicKey(point)
14 if err != nil {
15 return nil, err
16 }
17 priv, err := curve.NewPrivateKey(scalar)
18 if err != nil {
19 return nil, err
20 }
21 out, err := priv.ECDH(pub)
22 if err != nil {
23 return nil, err
24 }
25 copy(dst[:], out)
26 return dst[:], nil
27 }
28
29 func scalarMult(dst, scalar, point *[32]byte) {
30 if _, err := x25519(dst, scalar[:], point[:]); err != nil {
31
32
33 for i := range dst {
34 dst[i] = 0
35 }
36 }
37 }
38
39 func scalarBaseMult(dst, scalar *[32]byte) {
40 curve := ecdh.X25519()
41 priv, err := curve.NewPrivateKey(scalar[:])
42 if err != nil {
43 panic("curve25519: internal error: scalarBaseMult was not 32 bytes")
44 }
45 copy(dst[:], priv.PublicKey().Bytes())
46 }
47
View as plain text