-
Notifications
You must be signed in to change notification settings - Fork 0
/
key_info.go
57 lines (48 loc) · 1.79 KB
/
key_info.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
package mcrypto_dh
import (
"bytes"
"encoding/base64"
"github.com/MarconiProtocol/kyber"
mlog "github.com/MarconiProtocol/log"
)
type DHKeyInfo struct {
PrivateKey kyber.Scalar
PublicKey kyber.Point
PeerPublicKey kyber.Point
SymmetricKey kyber.Point
SymmetricKeyBytes *bytes.Buffer
KeySignal *chan *bytes.Buffer
}
func (dhKeyInfo *DHKeyInfo) EncodePublicKeyToString() string {
buffer := new(bytes.Buffer)
dhKeyInfo.PublicKey.MarshalTo(buffer)
encodedPublicKey := base64.StdEncoding.EncodeToString(buffer.Bytes())
return encodedPublicKey
}
func (dhKeyInfo *DHKeyInfo) DecodeStringToPeerPublicKey(encodedPublicKey string) error {
decodedBytes, b64decodeErr := base64.StdEncoding.DecodeString(encodedPublicKey)
if b64decodeErr != nil {
mlog.GetLogger().Errorf("Error base64 decoding the string payload : %v", encodedPublicKey)
return b64decodeErr
}
buffer := bytes.NewReader(decodedBytes)
publicKey := DHExchangeManagerInstance().suite.Point()
_, unmarshallErr := publicKey.UnmarshalFrom(buffer)
if unmarshallErr != nil {
mlog.GetLogger().Error("Error unmarshalling bytes from buffer into Point obj")
return unmarshallErr
}
dhKeyInfo.PeerPublicKey = publicKey
return nil
}
func (dhKeyInfo *DHKeyInfo) GenDHSymmetricKey() {
dhKeyInfo.SymmetricKey = DHExchangeManagerInstance().suite.Point().Mul(dhKeyInfo.PrivateKey, dhKeyInfo.PeerPublicKey)
dhKeyInfo.SymmetricKeyBytes.Reset()
dhKeyInfo.SymmetricKey.MarshalTo(dhKeyInfo.SymmetricKeyBytes)
// NOTE: we are using the goroutine system as a pseudo buffer to avoid blocking...
// This guarentees delivery without blocking the main goroutine
go func() {
*dhKeyInfo.KeySignal <- dhKeyInfo.SymmetricKeyBytes
mlog.GetLogger().Debug("New Symmetric Key, pushed to KeySignal")
}()
}