Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
number571 committed Oct 23, 2024
1 parent d28cef1 commit 449d984
Show file tree
Hide file tree
Showing 14 changed files with 2,955 additions and 2,958 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
- Update `cmd/tools`: add keysplit app
- Update `pkg/crypto/asymmetric`: add NewPrivKeyFromSeed

### CHANGES

- Update `pkg/client`: update interface IClient: DecryptMessage

<!-- ... -->

## v1.7.0
Expand Down
20 changes: 10 additions & 10 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ func (p *sClient) encryptWithParams(
var (
rand = random.NewRandom()
salt = rand.GetBytes(cSaltSize)
sign = p.fPrivKey.GetDSAPrivKey()
pkey = p.fPrivKey.GetPubKey().ToBytes()
)

data := joiner.NewBytesJoiner32([][]byte{pMsg, rand.GetBytes(pPadd)})
hash := hashing.NewHMACHasher(salt, bytes.Join(
[][]byte{
sign.GetPubKey().ToBytes(),
pkey,
pRecv.ToBytes(),
data,
},
Expand All @@ -111,18 +111,18 @@ func (p *sClient) encryptWithParams(
return message.NewMessage(
ct,
cipher.EncryptBytes(joiner.NewBytesJoiner32([][]byte{
sign.GetPubKey().ToBytes(),
pkey,
salt,
hash,
sign.SignBytes(hash),
p.fPrivKey.GetDSAPrivKey().SignBytes(hash),
data,
})),
).ToBytes(), nil
}

// Decrypt message with private key of receiver.
// No one else except the sender will be able to decrypt the message.
func (p *sClient) DecryptMessage(pMsg []byte) (asymmetric.IDSAPubKey, []byte, error) {
func (p *sClient) DecryptMessage(pMsg []byte) (asymmetric.IPubKey, []byte, error) {
msg, err := message.LoadMessage(p.fMessageSize, pMsg)
if err != nil {
return nil, nil, ErrInitCheckMessage
Expand Down Expand Up @@ -152,15 +152,15 @@ func (p *sClient) DecryptMessage(pMsg []byte) (asymmetric.IDSAPubKey, []byte, er
)

// Load public key and check standart size.
signerPubKey := asymmetric.LoadDSAPubKey(pkey)
if signerPubKey == nil {
pubKey := asymmetric.LoadPubKey(pkey)
if pubKey == nil {
return nil, nil, ErrDecodePublicKey
}

// Validate received hash with generated hash.
check := hashing.NewHMACHasher(salt, bytes.Join(
[][]byte{
signerPubKey.ToBytes(),
pubKey.ToBytes(),
kemPrivKey.GetPubKey().ToBytes(),
data,
},
Expand All @@ -171,7 +171,7 @@ func (p *sClient) DecryptMessage(pMsg []byte) (asymmetric.IDSAPubKey, []byte, er
}

// Verify sign by public key of sender and hash of message.
if !signerPubKey.VerifyBytes(hash, sign) {
if !pubKey.GetDSAPubKey().VerifyBytes(hash, sign) {
return nil, nil, ErrInvalidHashSign
}

Expand All @@ -182,5 +182,5 @@ func (p *sClient) DecryptMessage(pMsg []byte) (asymmetric.IDSAPubKey, []byte, er
}

// Return public key of sender with payload.
return signerPubKey, payloadWrapper[0], nil
return pubKey, payloadWrapper[0], nil
}
8 changes: 4 additions & 4 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,14 @@ func TestClient(t *testing.T) {
// _ = os.WriteFile("message/test_binary.msg", enc, 0600)
// _ = os.WriteFile("message/test_string.msg", []byte(encoding.HexEncode(enc)), 0600)

signerPubKey := client.GetPrivKey().GetDSAPrivKey().GetPubKey()
gotDSAPubKey, dec, err := client.DecryptMessage(enc)
pubKey := client.GetPrivKey().GetPubKey()
gotPubKey, dec, err := client.DecryptMessage(enc)
if err != nil {
t.Error(err)
return
}
if !bytes.Equal(signerPubKey.ToBytes(), gotDSAPubKey.ToBytes()) {
t.Error("invalid decrypt signer key")
if !bytes.Equal(pubKey.ToBytes(), gotPubKey.ToBytes()) {
t.Error("invalid decrypt key")
return
}
if !bytes.Equal(msg, dec) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ type IClient interface {
GetPrivKey() asymmetric.IPrivKey

EncryptMessage(asymmetric.IKEMPubKey, []byte) ([]byte, error)
DecryptMessage([]byte) (asymmetric.IDSAPubKey, []byte, error)
DecryptMessage([]byte) (asymmetric.IPubKey, []byte, error)
}
20 changes: 10 additions & 10 deletions pkg/crypto/asymmetric/map_pubkeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,40 @@ var (
// F2F connection mode.
type sMapPubKeys struct {
fMutex sync.RWMutex
fMapping map[string]IKEMPubKey
fMapping map[string]struct{}
}

func NewMapPubKeys() IMapPubKeys {
return &sMapPubKeys{
fMapping: make(map[string]IKEMPubKey),
fMapping: make(map[string]struct{}),
}
}

// Add public key to list of friends.
func (p *sMapPubKeys) SetPubKey(pDSAPubKey IDSAPubKey, pKEMPubKey IKEMPubKey) {
func (p *sMapPubKeys) SetPubKey(pPubKey IPubKey) {
p.fMutex.Lock()
defer p.fMutex.Unlock()

p.fMapping[hashkey(pDSAPubKey)] = pKEMPubKey
p.fMapping[hashkey(pPubKey)] = struct{}{}
}

// Check the existence of a friend in the list by the public key.
func (p *sMapPubKeys) GetPubKey(pDSAPubKey IDSAPubKey) (IKEMPubKey, bool) {
func (p *sMapPubKeys) InPubKeys(pPubKey IPubKey) bool {
p.fMutex.RLock()
defer p.fMutex.RUnlock()

kemPubKey, ok := p.fMapping[hashkey(pDSAPubKey)]
return kemPubKey, ok
_, ok := p.fMapping[hashkey(pPubKey)]
return ok
}

// Delete public key from list of friends.
func (p *sMapPubKeys) DelPubKey(pPubKey IDSAPubKey) {
func (p *sMapPubKeys) DelPubKey(pPubKey IPubKey) {
p.fMutex.Lock()
defer p.fMutex.Unlock()

delete(p.fMapping, hashkey(pPubKey))
}

func hashkey(pDSAPubKey IDSAPubKey) string {
return hashing.NewHasher(pDSAPubKey.ToBytes()).ToString()
func hashkey(pPubKey IPubKey) string {
return hashing.NewHasher(pPubKey.ToBytes()).ToString()
}
11 changes: 4 additions & 7 deletions pkg/crypto/asymmetric/map_pubkeys_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package asymmetric

import (
"bytes"
"testing"
)

Expand All @@ -17,18 +16,16 @@ func TestMapPubKeys(t *testing.T) {
}

for _, pk := range pubKeys {
list.SetPubKey(pk.GetDSAPubKey(), pk.GetKEMPubKey())
list.SetPubKey(pk)
}

dsaPubKey := pubKeys[1].GetDSAPubKey()
pk, ok := list.GetPubKey(dsaPubKey)
if !ok || !bytes.Equal(pk.ToBytes(), pubKeys[1].GetKEMPubKey().ToBytes()) {
if ok := list.InPubKeys(pubKeys[1]); !ok {
t.Error("get invalid pub key")
return
}

list.DelPubKey(dsaPubKey)
if _, ok := list.GetPubKey(dsaPubKey); ok {
list.DelPubKey(pubKeys[1])
if ok := list.InPubKeys(pubKeys[1]); ok {
t.Error("get success deleted pub key")
return
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/crypto/asymmetric/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
)

type IMapPubKeys interface {
SetPubKey(IDSAPubKey, IKEMPubKey)
GetPubKey(IDSAPubKey) (IKEMPubKey, bool)
DelPubKey(IDSAPubKey)
InPubKeys(IPubKey) bool
SetPubKey(IPubKey)
DelPubKey(IPubKey)
}

type IPrivKey interface {
Expand Down
10 changes: 4 additions & 6 deletions pkg/network/anonymity/anonymity.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,18 +237,17 @@ func (p *sNode) networkHandler(
}

// try decrypt message
dsaPubKey, decMsg, err := client.DecryptMessage(encMsg)
pubKey, decMsg, err := client.DecryptMessage(encMsg)
if err != nil {
p.fLogger.PushInfo(logBuilder.WithType(anon_logger.CLogInfoUndecryptable))
return nil
}

// enrich logger
logBuilder.WithPubKey(dsaPubKey)
logBuilder.WithPubKey(pubKey.GetDSAPubKey())

// check sender's public key in f2f list
kemPubKey, ok := p.fFriends.GetPubKey(dsaPubKey)
if !ok {
if ok := p.fFriends.InPubKeys(pubKey); !ok {
// ignore reading message from unknown public key
p.fLogger.PushWarn(logBuilder.WithType(anon_logger.CLogWarnNotFriend))
return nil
Expand All @@ -263,8 +262,7 @@ func (p *sNode) networkHandler(
}

// do request or response action
keychain := asymmetric.NewPubKey(kemPubKey, dsaPubKey)
return p.handleDoAction(pCtx, logBuilder, keychain, pld)
return p.handleDoAction(pCtx, logBuilder, pubKey, pld)
}

func (p *sNode) handleDoAction(
Expand Down
12 changes: 5 additions & 7 deletions pkg/network/anonymity/anonymity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ func TestF2FWithoutFriends(t *testing.T) {
}
defer testFreeNodes(nodes[:], cancels[:], 1)

nodes[0].GetMapPubKeys().DelPubKey(nodes[1].GetMessageQueue().GetClient().GetPrivKey().GetPubKey().GetDSAPubKey())
nodes[1].GetMapPubKeys().DelPubKey(nodes[0].GetMessageQueue().GetClient().GetPrivKey().GetPubKey().GetDSAPubKey())
nodes[0].GetMapPubKeys().DelPubKey(nodes[1].GetMessageQueue().GetClient().GetPrivKey().GetPubKey())
nodes[1].GetMapPubKeys().DelPubKey(nodes[0].GetMessageQueue().GetClient().GetPrivKey().GetPubKey())

ctx := context.Background()

Expand Down Expand Up @@ -339,9 +339,7 @@ func TestHandleWrapper(t *testing.T) {

privKey := client.GetPrivKey()
kemPubKey := privKey.GetKEMPrivKey().GetPubKey()
dsaPubKey := privKey.GetDSAPrivKey().GetPubKey()

node.GetMapPubKeys().SetPubKey(dsaPubKey, kemPubKey)
node.GetMapPubKeys().SetPubKey(privKey.GetPubKey())

ctx := context.Background()
sett := net_message.NewConstructSettings(&net_message.SConstructSettings{
Expand Down Expand Up @@ -608,8 +606,8 @@ func testNewNodes(t *testing.T, timeWait time.Duration, addresses [2]string, typ
pubKey1 := nodes[1].GetMessageQueue().GetClient().GetPrivKey().GetPubKey()
pubKey0 := nodes[0].GetMessageQueue().GetClient().GetPrivKey().GetPubKey()

nodes[0].GetMapPubKeys().SetPubKey(pubKey1.GetDSAPubKey(), pubKey1.GetKEMPubKey())
nodes[1].GetMapPubKeys().SetPubKey(pubKey0.GetDSAPubKey(), pubKey0.GetKEMPubKey())
nodes[0].GetMapPubKeys().SetPubKey(pubKey1)
nodes[1].GetMapPubKeys().SetPubKey(pubKey0)

for _, node := range nodes {
node.HandleFunc(
Expand Down
4 changes: 2 additions & 2 deletions pkg/network/anonymity/examples/echo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ func exchangeKeys(node1, node2 anonymity.INode) (asymmetric.IKEMPubKey, asymmetr
pubKey1 := node1.GetMessageQueue().GetClient().GetPrivKey().GetPubKey()
pubKey2 := node2.GetMessageQueue().GetClient().GetPrivKey().GetPubKey()

node1.GetMapPubKeys().SetPubKey(pubKey2.GetDSAPubKey(), pubKey1.GetKEMPubKey())
node2.GetMapPubKeys().SetPubKey(pubKey1.GetDSAPubKey(), pubKey1.GetKEMPubKey())
node1.GetMapPubKeys().SetPubKey(pubKey2)
node2.GetMapPubKeys().SetPubKey(pubKey1)

return pubKey1.GetKEMPubKey(), pubKey2.GetKEMPubKey()
}
4 changes: 2 additions & 2 deletions pkg/network/anonymity/examples/ping-pong/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ func exchangeKeys(node1, node2 anonymity.INode) (asymmetric.IKEMPubKey, asymmetr
pubKey1 := node1.GetMessageQueue().GetClient().GetPrivKey().GetPubKey()
pubKey2 := node2.GetMessageQueue().GetClient().GetPrivKey().GetPubKey()

node1.GetMapPubKeys().SetPubKey(pubKey2.GetDSAPubKey(), pubKey1.GetKEMPubKey())
node2.GetMapPubKeys().SetPubKey(pubKey1.GetDSAPubKey(), pubKey1.GetKEMPubKey())
node1.GetMapPubKeys().SetPubKey(pubKey2)
node2.GetMapPubKeys().SetPubKey(pubKey1)

return pubKey1.GetKEMPubKey(), pubKey2.GetKEMPubKey()
}
2 changes: 1 addition & 1 deletion test/result/badge_codelines.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 449d984

Please sign in to comment.