-
Notifications
You must be signed in to change notification settings - Fork 26
/
dhgroup.go
148 lines (136 loc) · 4.3 KB
/
dhgroup.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
/*
* Copyright 2012 Nan Deng
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package dhkx
import (
"crypto/rand"
"errors"
"io"
"math/big"
)
type DHGroup struct {
p *big.Int
g *big.Int
}
func (self *DHGroup) P() *big.Int {
p := new(big.Int)
p.Set(self.p)
return p
}
func (self *DHGroup) G() *big.Int {
g := new(big.Int)
g.Set(self.g)
return g
}
func (self *DHGroup) GeneratePrivateKey(randReader io.Reader) (key *DHKey, err error) {
if randReader == nil {
randReader = rand.Reader
}
// x should be in (0, p).
// alternative approach:
// x, err := big.Add(rand.Int(randReader, big.Sub(p, big.NewInt(1))), big.NewInt(1))
//
// However, since x is highly unlikely to be zero if p is big enough,
// we would rather use an iterative approach below,
// which is more efficient in terms of exptected running time.
x, err := rand.Int(randReader, self.p)
if err != nil {
return
}
zero := big.NewInt(0)
for x.Cmp(zero) == 0 {
x, err = rand.Int(randReader, self.p)
if err != nil {
return
}
}
key = new(DHKey)
key.x = x
// y = g ^ x mod p
key.y = new(big.Int).Exp(self.g, x, self.p)
key.group = self
return
}
// This function fetches a DHGroup by its ID as defined in either RFC 2409 or
// RFC 3526.
//
// If you are unsure what to use use group ID 0 for a sensible default value
func GetGroup(groupID int) (group *DHGroup, err error) {
if groupID <= 0 {
groupID = 14
}
switch groupID {
case 1:
p, _ := new(big.Int).SetString("FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A63A3620FFFFFFFFFFFFFFFF", 16)
group = &DHGroup{
g: new(big.Int).SetInt64(2),
p: p,
}
case 2:
p, _ := new(big.Int).SetString("FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF", 16)
group = &DHGroup{
g: new(big.Int).SetInt64(2),
p: p,
}
case 14:
p, _ := new(big.Int).SetString("FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AACAA68FFFFFFFFFFFFFFFF", 16)
group = &DHGroup{
g: new(big.Int).SetInt64(2),
p: p,
}
default:
group = nil
err = errors.New("DH: Unknown group")
}
return
}
// This function enables users to create their own custom DHGroup.
// Most users will not however want to use this function, and should prefer
// the use of GetGroup which supplies DHGroups defined in RFCs 2409 and 3526
//
// WARNING! You should only use this if you know what you are doing. The
// behavior of the group returned by this function is not defined if prime is
// not in fact prime.
func CreateGroup(prime, generator *big.Int) (group *DHGroup) {
group = &DHGroup{
g: generator,
p: prime,
}
return
}
func (self *DHGroup) ComputeKey(pubkey *DHKey, privkey *DHKey) (key *DHKey, err error) {
if self.p == nil {
err = errors.New("DH: invalid group")
return
}
if pubkey.y == nil {
err = errors.New("DH: invalid public key")
return
}
if pubkey.y.Sign() <= 0 || pubkey.y.Cmp(self.p) >= 0 {
err = errors.New("DH parameter out of bounds")
return
}
if privkey.x == nil {
err = errors.New("DH: invalid private key")
return
}
k := new(big.Int).Exp(pubkey.y, privkey.x, self.p)
key = new(DHKey)
key.y = k
key.group = self
return
}