p2p: add SharedKey func

This commit is contained in:
rkonfj 2024-04-29 21:24:32 +08:00
parent 99e2fba7e2
commit 571871e35a
4 changed files with 17 additions and 0 deletions

View File

@ -240,6 +240,14 @@ func (c *PeerPacketConn) runControlEventLoop(wsConn *disco.WSConn, udpConn *disc
}
}
// SharedKey get the key shared with the peer
func (c *PeerPacketConn) SharedKey(peerID peer.ID) ([]byte, error) {
if c.cfg.SymmAlgo == nil {
return nil, errors.New("get shared key from plain conn")
}
return c.cfg.SymmAlgo.SecretKey()(peerID.String())
}
// ListenPacket listen the p2p network for read/write packets
func ListenPacket(peermap *peermap.Peermap, opts ...Option) (*PeerPacketConn, error) {
id := make([]byte, 16)

View File

@ -124,6 +124,10 @@ func (s *AESCBC) Decrypt(b []byte, pubKey string) ([]byte, error) {
return PKCS7UnPadding(plainBytes)
}
func (s *AESCBC) SecretKey() secure.ProvideSecretKey {
return s.provideSecretKey
}
func (s *AESCBC) ensureChiperBlock(pubKey string) (cipher.Block, error) {
s.mut.RLock()
block, ok := s.cipher.Get(pubKey)

View File

@ -62,6 +62,10 @@ func (s *Chacha20Poly1305) Decrypt(data []byte, pubKey string) ([]byte, error) {
return plain, nil
}
func (s *Chacha20Poly1305) SecretKey() secure.ProvideSecretKey {
return s.provideSecretKey
}
func (s *Chacha20Poly1305) ensureChiperAEAD(pubKey string) (cipher.AEAD, error) {
s.mut.RLock()
aead, ok := s.cipher.Get(pubKey)

View File

@ -5,4 +5,5 @@ type ProvideSecretKey func(pubKey string) ([]byte, error)
type SymmAlgo interface {
Encrypt(data []byte, pubKey string) ([]byte, error)
Decrypt(data []byte, pubKey string) ([]byte, error)
SecretKey() ProvideSecretKey
}