MCPcopy Index your code
hub / github.com/CovenantSQL/CovenantSQL / GetSharedSecretWith

Function GetSharedSecretWith

naconn/secret.go:38–86  ·  view source on GitHub ↗

GetSharedSecretWith gets shared symmetric key with ECDH.

(resolver Resolver, nodeID *proto.RawNodeID, isAnonymous bool)

Source from the content-addressed store, hash-verified

36
37// GetSharedSecretWith gets shared symmetric key with ECDH.
38func GetSharedSecretWith(resolver Resolver, nodeID *proto.RawNodeID, isAnonymous bool) (symmetricKey []byte, err error) {
39 if isAnonymous {
40 //log.Debug("using anonymous ETLS")
41 return []byte(sharedSecret), nil
42 }
43
44 symmetricKeyI, ok := symmetricKeyCache.Load(nodeID.String())
45 if ok {
46 symmetricKey, _ = symmetricKeyI.([]byte)
47 } else {
48 var remotePublicKey *asymmetric.PublicKey
49 if route.IsBPNodeID(nodeID) {
50 remotePublicKey = kms.BP.PublicKey
51 } else if conf.RoleTag[0] == conf.BlockProducerBuildTag[0] {
52 remotePublicKey, err = kms.GetPublicKey(proto.NodeID(nodeID.String()))
53 if err != nil {
54 err = errors.Wrapf(err, "get public key locally failed, node: %s", nodeID)
55 return
56 }
57 } else {
58 // if non BP running and key not found, ask BlockProducer
59 var nodeInfo *proto.Node
60 nodeInfo, err = resolver.ResolveEx(nodeID)
61 if err != nil {
62 err = errors.Wrapf(err, "get public key failed, node: %s", nodeID)
63 return
64 }
65 remotePublicKey = nodeInfo.PublicKey
66 }
67
68 var localPrivateKey *asymmetric.PrivateKey
69 localPrivateKey, err = kms.GetLocalPrivateKey()
70 if err != nil {
71 err = errors.Wrap(err, "get local private key failed")
72 return
73 }
74
75 symmetricKey = asymmetric.GenECDHSharedSecret(localPrivateKey, remotePublicKey)
76 symmetricKeyCache.Store(nodeID.String(), symmetricKey)
77 //log.WithFields(log.Fields{
78 // "node": nodeID.String(),
79 // "remotePub": fmt.Sprintf("%#x", remotePublicKey.Serialize()),
80 // "sessionKey": fmt.Sprintf("%#x", symmetricKey),
81 //}).Debug("generated shared secret")
82 }
83 //log.Debugf("ECDH for %s Public Key: %x, Private Key: %x Session Key: %x",
84 // nodeID.ToNodeID(), remotePublicKey.Serialize(), localPrivateKey.Serialize(), symmetricKey)
85 return
86}

Callers 2

serverHandshakeMethod · 0.85
DialExFunction · 0.85

Calls 7

IsBPNodeIDFunction · 0.92
GetPublicKeyFunction · 0.92
NodeIDTypeAlias · 0.92
GetLocalPrivateKeyFunction · 0.92
GenECDHSharedSecretFunction · 0.92
ResolveExMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected