-
Notifications
You must be signed in to change notification settings - Fork 2
/
ecdh.go
290 lines (235 loc) · 6.78 KB
/
ecdh.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// SPDX-FileCopyrightText: © 2023 David Stainton and Yawning Angel
// SPDX-License-Identifier: AGPL-3.0-only
package x25519
import (
"encoding/base64"
"errors"
"io"
"golang.org/x/crypto/curve25519"
"github.com/katzenpost/hpqc/nike"
"github.com/katzenpost/hpqc/util"
)
const (
// GroupElementLength is the length of a ECDH group element in bytes.
GroupElementLength = 32
// PublicKeySize is the size of a serialized PublicKey in bytes.
PublicKeySize = GroupElementLength
// PrivateKeySize is the size of a serialized PrivateKey in bytes.
PrivateKeySize = GroupElementLength
)
var (
// ErrBlindDataSizeInvalid indicates that the blinding data size was invalid.
ErrBlindDataSizeInvalid error = errors.New("ecdh: blinding data size invalid")
errInvalidKey = errors.New("ecdh: invalid key")
)
var _ nike.PrivateKey = (*PrivateKey)(nil)
var _ nike.PublicKey = (*PublicKey)(nil)
var _ nike.Scheme = (*scheme)(nil)
// EcdhNike implements the Nike interface using our ecdh module.
type scheme struct {
rng io.Reader
}
// Scheme instantiates a new X25519 scheme given a CSPRNG.
func Scheme(rng io.Reader) *scheme {
return &scheme{
rng: rng,
}
}
type PrivateKey struct {
pubKey PublicKey
privBytes [GroupElementLength]byte
}
func (p *PrivateKey) Public() nike.PublicKey {
return &p.pubKey
}
func (p *PrivateKey) Reset() {
p.pubKey.Reset()
util.ExplicitBzero(p.privBytes[:])
}
func (p *PrivateKey) Bytes() []byte {
return p.privBytes[:]
}
func (p *PrivateKey) FromBytes(data []byte) error {
if len(data) != PrivateKeySize {
return errInvalidKey
}
copy(p.privBytes[:], data)
expG(&p.pubKey.pubBytes, &p.privBytes)
p.pubKey.rebuildB64String()
return nil
}
func (p *PrivateKey) MarshalBinary() ([]byte, error) {
return p.Bytes(), nil
}
func (p *PrivateKey) MarshalText() ([]byte, error) {
return []byte(base64.StdEncoding.EncodeToString(p.Bytes())), nil
}
func (p *PrivateKey) UnmarshalBinary(data []byte) error {
return p.FromBytes(data)
}
func (p *PrivateKey) UnmarshalText(data []byte) error {
raw, err := base64.StdEncoding.DecodeString(string(data))
if err != nil {
return err
}
return p.FromBytes(raw)
}
// Exp calculates the shared secret with the provided public key.
func (k *PrivateKey) Exp(publicKey *PublicKey) []byte {
return Exp(publicKey.pubBytes[:], k.privBytes[:])
}
type PublicKey struct {
pubBytes [GroupElementLength]byte
b64String string
}
func (p *PublicKey) Blind(blindingFactor nike.PrivateKey) error {
if len(blindingFactor.Bytes()) != GroupElementLength {
return ErrBlindDataSizeInvalid
}
pubBytes := Exp(p.pubBytes[:], blindingFactor.Bytes())
copy(p.pubBytes[:], pubBytes)
util.ExplicitBzero(pubBytes)
return nil
}
func (p *PublicKey) Reset() {
util.ExplicitBzero(p.pubBytes[:])
p.b64String = "[scrubbed]"
}
func (p *PublicKey) Bytes() []byte {
return p.pubBytes[:]
}
func (p *PublicKey) rebuildB64String() {
p.b64String = base64.StdEncoding.EncodeToString(p.Bytes())
}
func (p *PublicKey) FromBytes(data []byte) error {
if len(data) != PublicKeySize {
return errInvalidKey
}
copy(p.pubBytes[:], data)
p.rebuildB64String()
return nil
}
func (p *PublicKey) MarshalBinary() ([]byte, error) {
return p.Bytes(), nil
}
func (p *PublicKey) MarshalText() ([]byte, error) {
return []byte(base64.StdEncoding.EncodeToString(p.Bytes())), nil
}
func (p *PublicKey) UnmarshalBinary(data []byte) error {
return p.FromBytes(data)
}
func (p *PublicKey) UnmarshalText(data []byte) error {
raw, err := base64.StdEncoding.DecodeString(string(data))
if err != nil {
return err
}
return p.FromBytes(raw)
}
func (e *scheme) GeneratePrivateKey(rng io.Reader) nike.PrivateKey {
privKey, err := NewKeypair(rng)
if err != nil {
panic(err)
}
return privKey
}
func (e *scheme) GenerateKeyPairFromEntropy(rng io.Reader) (nike.PublicKey, nike.PrivateKey, error) {
privKey, err := NewKeypair(rng)
if err != nil {
return nil, nil, err
}
return privKey.Public(), privKey, nil
}
func (e *scheme) GenerateKeyPair() (nike.PublicKey, nike.PrivateKey, error) {
return e.GenerateKeyPairFromEntropy(e.rng)
}
func (e *scheme) Name() string {
return "x25519"
}
// PublicKeySize returns the size in bytes of the public key.
func (e *scheme) PublicKeySize() int {
return PublicKeySize
}
// PrivateKeySize returns the size in bytes of the private key.
func (e *scheme) PrivateKeySize() int {
return PublicKeySize
}
// NewEmptyPublicKey returns an uninitialized
// PublicKey which is suitable to be loaded
// via some serialization format via FromBytes
// or FromPEMFile methods.
func (e *scheme) NewEmptyPublicKey() nike.PublicKey {
return new(PublicKey)
}
// NewEmptyPrivateKey returns an uninitialized
// PrivateKey which is suitable to be loaded
// via some serialization format via FromBytes
// or FromPEMFile methods.
func (e *scheme) NewEmptyPrivateKey() nike.PrivateKey {
return new(PrivateKey)
}
// DeriveSecret derives a shared secret given a private key
// from one party and a public key from another.
func (e *scheme) DeriveSecret(privKey nike.PrivateKey, pubKey nike.PublicKey) []byte {
sharedSecret := privKey.(*PrivateKey).Exp(pubKey.(*PublicKey))
return sharedSecret[:]
}
// DerivePublicKey derives a public key given a private key.
func (e *scheme) DerivePublicKey(privKey nike.PrivateKey) nike.PublicKey {
return privKey.(*PrivateKey).Public()
}
func (e *scheme) Blind(groupMember nike.PublicKey, blindingFactor nike.PrivateKey) nike.PublicKey {
sharedSecret := Exp(groupMember.Bytes(), blindingFactor.Bytes())
pubKey := new(PublicKey)
err := pubKey.FromBytes(sharedSecret)
if err != nil {
panic(err)
}
return pubKey
}
// UnmarshalBinaryPublicKey loads a public key from byte slice.
func (e *scheme) UnmarshalBinaryPublicKey(b []byte) (nike.PublicKey, error) {
pubKey := new(PublicKey)
err := pubKey.FromBytes(b)
if err != nil {
return nil, err
}
return pubKey, nil
}
// UnmarshalBinaryPrivateKey loads a private key from byte slice.
func (e *scheme) UnmarshalBinaryPrivateKey(b []byte) (nike.PrivateKey, error) {
privKey := new(PrivateKey)
err := privKey.FromBytes(b)
if err != nil {
return nil, err
}
return privKey, err
}
// Exp returns the group element, the result of x^y, over the ECDH group.
func Exp(x, y []byte) []byte {
var err error
if len(x) != GroupElementLength {
panic(errInvalidKey)
}
if len(y) != GroupElementLength {
panic(errInvalidKey)
}
sharedSecret, err := curve25519.X25519(y, x)
if err != nil {
panic(err)
}
return sharedSecret
}
func expG(dst, y *[GroupElementLength]byte) {
curve25519.ScalarBaseMult(dst, y)
}
// NewKeypair generates a new PrivateKey sampled from the provided entropy
// source.
func NewKeypair(r io.Reader) (*PrivateKey, error) {
k := new(PrivateKey)
if _, err := io.ReadFull(r, k.privBytes[:]); err != nil {
return nil, err
}
expG(&k.pubKey.pubBytes, &k.privBytes)
k.pubKey.rebuildB64String()
return k, nil
}