-
Notifications
You must be signed in to change notification settings - Fork 3
/
curve25519.go
94 lines (84 loc) · 2.71 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
// Copyright (c) 2015 Mute Communications Ltd.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package cipher
import (
"bytes"
"io"
"github.com/mutecomm/mute/log"
"golang.org/x/crypto/curve25519"
)
// Curve25519Key holds a Curve25519 key pair.
type Curve25519Key struct {
publicKey *[32]byte
privateKey *[32]byte
}
// Curve25519Generate generates a new Curve25519 key pair.
func Curve25519Generate(rand io.Reader) (*Curve25519Key, error) {
var c Curve25519Key
c.privateKey = new([32]byte)
if _, err := io.ReadFull(rand, c.privateKey[:]); err != nil {
return nil, err
}
c.publicKey = new([32]byte)
curve25519.ScalarBaseMult(c.publicKey, c.privateKey)
return &c, nil
}
// PublicKey returns the public key of an curve25519Key.
func (c *Curve25519Key) PublicKey() *[32]byte {
return c.publicKey
}
// PrivateKey returns the private key of an curve25519Key.
func (c *Curve25519Key) PrivateKey() *[32]byte {
return c.privateKey
}
// SetPublicKey sets the public key of curve25519Key to key.
// SetPublicKey returns an error, if len(key) != 32.
func (c *Curve25519Key) SetPublicKey(key []byte) error {
if len(key) != 32 {
return log.Errorf("cipher: Curve25519Key.SetPublicKey(): len(key) = %d != 32", len(key))
}
c.publicKey = new([32]byte)
copy(c.publicKey[:], key)
return nil
}
// SetPrivateKey sets the private key of curve25519Key to key.
// SetPrivateKey returns an error, if len(key) != 32.
func (c *Curve25519Key) SetPrivateKey(key []byte) error {
if len(key) != 32 {
return log.Errorf("cipher: Curve25519Key.SetPrivateKey(): len(key) = %d != 32", len(key))
}
c.privateKey = new([32]byte)
copy(c.privateKey[:], key)
return nil
}
// ECDH computes a Diffie-Hellman (DH) key exchange over the elliptic curve (EC)
// curve25519. If ownPublicKey is given it is used to check for the key
// reflection attack. Otherwise it is derived from privateKey.
func ECDH(privateKey, peersPublicKey, ownPublicKey *[32]byte) (*[32]byte, error) {
var (
sharedKey [32]byte
pubKey []byte
)
// check mandatory key length
if privateKey == nil {
return nil, log.Error("cipher: curve25519.ECDH(): privateKey == nil")
}
if peersPublicKey == nil {
return nil, log.Error("cipher: curve25519.ECDH(): peersPublicKey == nil")
}
// check for key reflection attack
if ownPublicKey != nil {
pubKey = ownPublicKey[:]
} else {
var publicKey [32]byte
curve25519.ScalarBaseMult(&publicKey, privateKey)
pubKey = publicKey[:]
}
if bytes.Equal(pubKey, peersPublicKey[:]) {
return nil, log.Errorf("cipher: curve25519.ECDH(): publicKey == peersPublicKey")
}
// perform Diffie-Hellman key exchange
curve25519.ScalarMult(&sharedKey, privateKey, peersPublicKey)
return &sharedKey, nil
}