From f126c719c0ba03e3a35d2c73e7012c18b7ea0f8c Mon Sep 17 00:00:00 2001 From: number571 Date: Fri, 2 Jan 2026 00:25:12 +0700 Subject: [PATCH] update --- pkg/client/client.go | 118 +++++++++++++++++++++---------------------- 1 file changed, 59 insertions(+), 59 deletions(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index 1cfb4858..c5ab9504 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -77,70 +77,15 @@ func (p *sClient) GetPayloadSize() uint64 { // The message can be decrypted only if private key is known. func (p *sClient) EncryptMessage(pRecv asymmetric.IPubKey, pMsg []byte) ([]byte, error) { var ( - payloadLimit = p.fPayloadSize - resultSize = uint64(len(pMsg)) + payloadSize = p.fPayloadSize + resultSize = uint64(len(pMsg)) ) - if resultSize > payloadLimit { + if resultSize > payloadSize { return nil, ErrLimitMessageSize } - return p.encryptWithPadding(pRecv, pMsg, payloadLimit-resultSize) -} - -func (p *sClient) encryptWithPadding( - pRecv asymmetric.IPubKey, - pMsg []byte, - pPadd uint64, -) ([]byte, error) { - var ( - rand = random.NewRandom() - salt = rand.GetBytes(cSaltSize) - pkey = p.fPrivKey.GetPubKey() - ) - - data := joiner.NewBytesJoiner32([][]byte{pMsg, rand.GetBytes(pPadd)}) - hash := hashing.NewHMACHasher(salt, bytes.Join( - [][]byte{pkey.ToBytes(), pRecv.ToBytes(), data}, - []byte{}, - )).ToBytes() - - ct, sk, err := pRecv.GetKEMPubKey().Encapsulate() - if err != nil { - return nil, ErrEncryptSymmetricKey - } - - cipher := symmetric.NewCipher(sk) - return layer2.NewMessage( - ct, - cipher.EncryptBytes(joiner.NewBytesJoiner32([][]byte{ - pkey.GetHasher().ToBytes(), - salt, - data, - hash, - p.fPrivKey.GetDSAPrivKey().SignBytes(hash), - })), - ).ToBytes(), nil -} - -func (p *sClient) withStaticDecryptValues() *sClient { - var ( - pubKey = p.fPrivKey.GetPubKey() - encMsg, _ = p.EncryptMessage(pubKey, []byte{}) - msg, _ = layer2.LoadMessage(p.fMessageSize, encMsg) - skey, _ = p.fPrivKey.GetKEMPrivKey().Decapsulate(msg.GetEnck()) - decJoiner = symmetric.NewCipher(skey).DecryptBytes(msg.GetEncd()) - decSlice, _ = joiner.LoadBytesJoiner32(decJoiner) - payloadWrapper, _ = joiner.LoadBytesJoiner32(decSlice[2]) - ) - p.fStaticDecryptValues = &sStaticDecryptValues{ - fEncMsg: msg, - fSKey: skey, - fDecSlice: decSlice, - fPubKey: pubKey, - fPayloadWrapper: payloadWrapper, - } - return p + return p.encryptWithPadding(pRecv, pMsg, payloadSize-resultSize) } // Decrypt message with private key of receiver. @@ -227,3 +172,58 @@ func (p *sClient) DecryptMessage(pMapPubKeys asymmetric.IMapPubKeys, pMsg []byte // Return public key of sender with payload. return sPubKey, payloadWrapper[0], nil } + +func (p *sClient) encryptWithPadding( + pRecv asymmetric.IPubKey, + pMsg []byte, + pPadd uint64, +) ([]byte, error) { + var ( + rand = random.NewRandom() + salt = rand.GetBytes(cSaltSize) + pkey = p.fPrivKey.GetPubKey() + ) + + data := joiner.NewBytesJoiner32([][]byte{pMsg, rand.GetBytes(pPadd)}) + hash := hashing.NewHMACHasher(salt, bytes.Join( + [][]byte{pkey.ToBytes(), pRecv.ToBytes(), data}, + []byte{}, + )).ToBytes() + + ct, sk, err := pRecv.GetKEMPubKey().Encapsulate() + if err != nil { + return nil, ErrEncryptSymmetricKey + } + + cipher := symmetric.NewCipher(sk) + return layer2.NewMessage( + ct, + cipher.EncryptBytes(joiner.NewBytesJoiner32([][]byte{ + pkey.GetHasher().ToBytes(), + salt, + data, + hash, + p.fPrivKey.GetDSAPrivKey().SignBytes(hash), + })), + ).ToBytes(), nil +} + +func (p *sClient) withStaticDecryptValues() *sClient { + var ( + pubKey = p.fPrivKey.GetPubKey() + encMsg, _ = p.EncryptMessage(pubKey, []byte{}) + msg, _ = layer2.LoadMessage(p.fMessageSize, encMsg) + skey, _ = p.fPrivKey.GetKEMPrivKey().Decapsulate(msg.GetEnck()) + decJoiner = symmetric.NewCipher(skey).DecryptBytes(msg.GetEncd()) + decSlice, _ = joiner.LoadBytesJoiner32(decJoiner) + payloadWrapper, _ = joiner.LoadBytesJoiner32(decSlice[2]) + ) + p.fStaticDecryptValues = &sStaticDecryptValues{ + fEncMsg: msg, + fSKey: skey, + fDecSlice: decSlice, + fPubKey: pubKey, + fPayloadWrapper: payloadWrapper, + } + return p +}