-
Notifications
You must be signed in to change notification settings - Fork 22
/
curve25519.go
105 lines (80 loc) · 1.87 KB
/
curve25519.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package curve25519
import (
"io"
"errors"
"crypto"
cryptorand "crypto/rand"
"golang.org/x/crypto/curve25519"
)
// 公钥
type PublicKey struct {
Y []byte
}
// 检测
func (this *PublicKey) Check() (err error) {
if len(this.Y) != 32 {
err = errors.New("peers public key is not 32 byte")
}
return
}
// 私钥
type PrivateKey struct {
PublicKey
X []byte
}
func (this *PrivateKey) Public() crypto.PublicKey {
return &this.PublicKey
}
// 生成密码
func (this *PrivateKey) ComputeSecret(peersPublic *PublicKey) (secret []byte) {
return ComputeSecret(this, peersPublic)
}
// 生成密钥对
func GenerateKey(rand io.Reader) (*PrivateKey, *PublicKey, error) {
if rand == nil {
rand = cryptorand.Reader
}
var err error
var pri, pub [32]byte
_, err = io.ReadFull(rand, pri[:])
if err != nil {
return nil, nil, err
}
pri[0] &= 248
pri[31] &= 127
pri[31] |= 64
curve25519.ScalarBaseMult(&pub, &pri)
public := &PublicKey{
Y: pub[:],
}
private := &PrivateKey{
X: pri[:],
PublicKey: *public,
}
return private, public, nil
}
// 从私钥获取公钥
func GeneratePublicKey(private *PrivateKey) (*PublicKey, error) {
var pri, pub [32]byte
copy(pri[:], private.X)
curve25519.ScalarBaseMult(&pub, &pri)
public := &PublicKey{
Y: pub[:],
}
return public, nil
}
// 生成密码
func ComputeSecret(private *PrivateKey, peersPublic *PublicKey) (secret []byte) {
if len(private.X) != 32 {
panic("ecdh: private key is not 32 byte")
}
if len(peersPublic.Y) != 32 {
panic("ecdh: peers public key is not 32 byte")
}
var sec, pri, pub [32]byte
copy(pri[:], private.X)
copy(pub[:], peersPublic.Y)
curve25519.ScalarMult(&sec, &pri, &pub)
secret = sec[:]
return
}