-
Notifications
You must be signed in to change notification settings - Fork 23
/
dh.go
157 lines (125 loc) · 3.16 KB
/
dh.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package dh
import (
"io"
"errors"
"crypto"
"math/big"
)
var zero *big.Int = big.NewInt(0)
var one *big.Int = big.NewInt(1)
var two *big.Int = big.NewInt(2)
// 分组 id
type GroupID uint
const (
P1001 GroupID = 1 + iota
P1002
P1536
P2048
P3072
P4096
P6144
P8192
)
// 公用参数
type Parameters struct {
// The prime
P *big.Int
// The generator
G *big.Int
}
// 公钥
type PublicKey struct {
Parameters
// 公钥
Y *big.Int
}
// 检测
// public key is < 0 or > g.P.
func (this *PublicKey) Check() (err error) {
if !((*this.Y).Cmp(zero) >= 0 && (*this.Y).Cmp(this.P) == -1) {
err = errors.New("peer's public is not a possible group element")
}
return
}
// 私钥
type PrivateKey struct {
PublicKey
// 私钥
X *big.Int
}
func (this *PrivateKey) Public() crypto.PublicKey {
return &this.PublicKey
}
// 生成密钥
func (this *PrivateKey) ComputeSecret(peersPublic *PublicKey) (secret []byte) {
return ComputeSecret(this, peersPublic)
}
// 生成证书
func GenerateKey(groupID GroupID, rand io.Reader) (*PrivateKey, *PublicKey, error) {
param, err := GetMODGroup(groupID)
if err != nil {
return nil, nil, err
}
return GenerateKeyWithGroup(param, rand)
}
// 生成证书
func GenerateKeyWithGroup(param *Group, rand io.Reader) (*PrivateKey, *PublicKey, error) {
if param.P == nil {
err := errors.New("crypto/dh: prime is nil")
return nil, nil, err
}
if param.G == nil {
err := errors.New("crypto/dh: generator is nil")
return nil, nil, err
}
min := big.NewInt(int64(param.P.BitLen() + 1))
bytes := make([]byte, (param.P.BitLen()+7)/8)
private := &PrivateKey{}
private.PublicKey.Parameters = Parameters{
P: param.P,
G: param.G,
}
for private.X == nil {
_, err := io.ReadFull(rand, bytes)
if err != nil {
private.X = nil
return nil, nil, errors.New("private x is nil")
}
// Clear bits in the first byte to increase
// the probability that the candidate is < g.P.
bytes[0] = 0
if private.X == nil {
private.X = new(big.Int)
}
(*private.X).SetBytes(bytes)
if (*private.X).Cmp(min) < 0 {
private.X = nil
}
}
private.PublicKey.Y = new(big.Int).Exp(param.G, private.X, param.P)
public := &private.PublicKey
return private, public, nil
}
// 从私钥获取公钥
func GeneratePublicKey(private *PrivateKey) (*PublicKey, error) {
pub := new(big.Int).Exp(private.G, private.X, private.P)
public := &PublicKey{
Y: pub,
Parameters: Parameters{
P: private.P,
G: private.G,
},
}
return public, nil
}
// 生成密钥
func ComputeSecret(private *PrivateKey, peersPublic *PublicKey) []byte {
secret := new(big.Int).Exp(peersPublic.Y, private.X, private.P)
return secret.Bytes()
}
// 判断是否为安全数据
func IsSafePrimeParameters(param Parameters, n int) bool {
q := new(big.Int).Sub(param.P, one)
q = q.Div(q, two)
return q.ProbablyPrime(n)
}