This commit is contained in:
number571 2024-04-16 10:26:05 +07:00
parent d48330e5b8
commit 2a1b381f8a
8 changed files with 48176 additions and 48177 deletions

File diff suppressed because it is too large Load Diff

View File

@ -130,12 +130,12 @@ func (p *sClient) encryptWithParams(pRecv asymmetric.IPubKey, pPld payload.IPayl
cipher := symmetric.NewAESCipher(session)
return &message.SMessage{
FPubKey: encoding.HexEncode(cipher.EncryptBytes(p.GetPubKey().ToBytes())),
FEncKey: encoding.HexEncode(encKey),
FSalt: encoding.HexEncode(cipher.EncryptBytes(salt)),
FHash: encoding.HexEncode(cipher.EncryptBytes(hash)),
FSign: encoding.HexEncode(cipher.EncryptBytes(p.fPrivKey.SignBytes(hash))),
FPayload: cipher.EncryptBytes(payloadWrapper.ToBytes()), // JSON field to raw Body (no need HEX encode)
FPubk: encoding.HexEncode(cipher.EncryptBytes(p.GetPubKey().ToBytes())),
FEnck: encoding.HexEncode(encKey),
FSalt: encoding.HexEncode(cipher.EncryptBytes(salt)),
FHash: encoding.HexEncode(cipher.EncryptBytes(hash)),
FSign: encoding.HexEncode(cipher.EncryptBytes(p.fPrivKey.SignBytes(hash))),
FData: cipher.EncryptBytes(payloadWrapper.ToBytes()), // JSON field to raw Body (no need HEX encode)
}, nil
}
@ -148,14 +148,14 @@ func (p *sClient) DecryptMessage(pMsg message.IMessage) (asymmetric.IPubKey, pay
}
// Decrypt session key by private key of receiver.
session := p.fPrivKey.DecryptBytes(pMsg.GetEncKey())
session := p.fPrivKey.DecryptBytes(pMsg.GetEnck())
if session == nil {
return nil, nil, ErrDecryptCipherKey
}
// Decrypt public key of sender by decrypted session key.
cipher := symmetric.NewAESCipher(session)
publicBytes := cipher.DecryptBytes(pMsg.GetPubKey())
publicBytes := cipher.DecryptBytes(pMsg.GetPubk())
// Load public key and check standart size.
pubKey := asymmetric.LoadRSAPubKey(publicBytes)
@ -167,7 +167,7 @@ func (p *sClient) DecryptMessage(pMsg message.IMessage) (asymmetric.IPubKey, pay
}
// Decrypt main data of message by session key.
payloadWrapperBytes := cipher.DecryptBytes(pMsg.GetPayload())
payloadWrapperBytes := cipher.DecryptBytes(pMsg.GetData())
payloadWrapper := payload.LoadPayload(payloadWrapperBytes)
if payloadWrapper == nil {
return nil, nil, ErrDecodePayloadWrapper

View File

@ -182,14 +182,14 @@ func TestDecrypt(t *testing.T) {
}
sMsg3 := *sMsg
sMsg3.FEncKey = "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
sMsg3.FEnck = "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
if _, _, err := client1.DecryptMessage(&sMsg3); err == nil {
t.Error("success decrypt message with incorrect session key")
return
}
sMsg4 := *sMsg
sMsg4.FPubKey = "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
sMsg4.FPubk = "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
if _, _, err := client1.DecryptMessage(&sMsg4); err == nil {
t.Error("success decrypt message with incorrect sender key (incorrect)")
return
@ -207,14 +207,14 @@ func TestDecrypt(t *testing.T) {
}
sMsg6 := *sMsg
sMsg6.FPayload = []byte("111")
sMsg6.FData = []byte("111")
if _, _, err := client1.DecryptMessage(&sMsg6); err == nil {
t.Error("success decrypt message with incorrect payload (iv block)")
return
}
sMsg7 := *sMsg
sMsg7.FPayload = random.NewStdPRNG().GetBytes(948)
sMsg7.FData = random.NewStdPRNG().GetBytes(948)
if _, _, err := client1.DecryptMessage(&sMsg7); err == nil {
t.Error("success decrypt message with incorrect payload (payload is nil)")
return
@ -358,11 +358,11 @@ func (p *sClient) tInvalidEncryptWithParams(pRecv asymmetric.IPubKey, pPld paylo
cipher := symmetric.NewAESCipher(session)
return &message.SMessage{
FPubKey: encoding.HexEncode(cipher.EncryptBytes(p.GetPubKey().ToBytes())),
FEncKey: encoding.HexEncode(pRecv.EncryptBytes(session)),
FSalt: encoding.HexEncode(cipher.EncryptBytes(salt)),
FHash: encoding.HexEncode(cipher.EncryptBytes(hash)),
FSign: encoding.HexEncode(cipher.EncryptBytes(p.fPrivKey.SignBytes(hash))),
FPayload: cipher.EncryptBytes(doublePayload.ToBytes()), // JSON field to raw Body (no need HEX encode)
FPubk: encoding.HexEncode(cipher.EncryptBytes(p.GetPubKey().ToBytes())),
FEnck: encoding.HexEncode(pRecv.EncryptBytes(session)),
FSalt: encoding.HexEncode(cipher.EncryptBytes(salt)),
FHash: encoding.HexEncode(cipher.EncryptBytes(hash)),
FSign: encoding.HexEncode(cipher.EncryptBytes(p.fPrivKey.SignBytes(hash))),
FData: cipher.EncryptBytes(doublePayload.ToBytes()), // JSON field to raw Body (no need HEX encode)
}
}

View File

@ -25,12 +25,12 @@ var (
// Basic structure of transport package.
type SMessage struct {
FPubKey string `json:"pubk"`
FEncKey string `json:"enck"`
FSalt string `json:"salt"`
FHash string `json:"hash"`
FSign string `json:"sign"`
FPayload []byte `json:"-"`
FPubk string `json:"pubk"`
FEnck string `json:"enck"`
FSalt string `json:"salt"`
FHash string `json:"hash"`
FSign string `json:"sign"`
FData []byte `json:"-"`
}
// Message can be created only with client module.
@ -58,11 +58,11 @@ func LoadMessage(psett ISettings, pMsg interface{}) (IMessage, error) {
switch x := pMsg.(type) {
case []byte:
msg.FPayload = x[i+cSeparatorLen:]
msg.FData = x[i+cSeparatorLen:]
case string:
encStr := strings.TrimSpace(x[i+cSeparatorLen:])
msg.FPayload = encoding.HexDecode(encStr)
if msg.FPayload == nil {
msg.FData = encoding.HexDecode(encStr)
if msg.FData == nil {
return nil, ErrDecodePayload
}
}
@ -74,12 +74,12 @@ func LoadMessage(psett ISettings, pMsg interface{}) (IMessage, error) {
return msg, nil
}
func (p *SMessage) GetPubKey() []byte {
return encoding.HexDecode(p.FPubKey)
func (p *SMessage) GetPubk() []byte {
return encoding.HexDecode(p.FPubk)
}
func (p *SMessage) GetEncKey() []byte {
return encoding.HexDecode(p.FEncKey)
func (p *SMessage) GetEnck() []byte {
return encoding.HexDecode(p.FEnck)
}
func (p *SMessage) GetSalt() []byte {
@ -94,15 +94,15 @@ func (p *SMessage) GetSign() []byte {
return encoding.HexDecode(p.FSign)
}
func (p *SMessage) GetPayload() []byte {
return p.FPayload
func (p *SMessage) GetData() []byte {
return p.FData
}
func (p *SMessage) ToBytes() []byte {
return bytes.Join(
[][]byte{
encoding.SerializeJSON(p),
p.FPayload,
p.FData,
},
[]byte(CSeparator),
)
@ -112,7 +112,7 @@ func (p *SMessage) ToString() string {
return strings.Join(
[]string{
string(encoding.SerializeJSON(p)),
encoding.HexEncode(p.FPayload),
encoding.HexEncode(p.FData),
},
CSeparator,
)
@ -124,12 +124,12 @@ func (p *SMessage) IsValid(psett ISettings) bool {
switch {
case
uint64(len(p.ToBytes())) != msgSizeBytes,
uint64(len(p.GetEncKey())) != keySizeBytes,
uint64(len(p.GetEnck())) != keySizeBytes,
uint64(len(p.GetSign())) != symmetric.CAESBlockSize+keySizeBytes,
uint64(len(p.GetPubKey())) < symmetric.CAESBlockSize+keySizeBytes,
uint64(len(p.GetPubk())) < symmetric.CAESBlockSize+keySizeBytes,
len(p.GetHash()) != symmetric.CAESBlockSize+hashing.CSHA256Size,
len(p.GetSalt()) != symmetric.CAESBlockSize+symmetric.CAESKeySize,
len(p.GetPayload()) < symmetric.CAESBlockSize:
len(p.GetData()) < symmetric.CAESBlockSize:
return false
default:
return true

View File

@ -135,12 +135,12 @@ func testMessage(t *testing.T, msg IMessage) {
return
}
if !bytes.Equal(msg.GetEncKey(), encoding.HexDecode(tcSession)) {
if !bytes.Equal(msg.GetEnck(), encoding.HexDecode(tcSession)) {
t.Error("incorrect session value")
return
}
if !bytes.Equal(msg.GetPubKey(), encoding.HexDecode(tcSender)) {
if !bytes.Equal(msg.GetPubk(), encoding.HexDecode(tcSender)) {
t.Error("incorrect sender value")
return
}
@ -155,14 +155,14 @@ func testMessage(t *testing.T, msg IMessage) {
return
}
if msg.GetPayload() == nil {
if msg.GetData() == nil {
t.Error("failed get encrypted payload")
return
}
msg1 := msg.(*SMessage)
msg1.FPayload = []byte{123}
if !bytes.Equal(msg.GetPayload(), []byte{123}) {
msg1.FData = []byte{123}
if !bytes.Equal(msg.GetData(), []byte{123}) {
t.Error("success got incorrect payload")
return
}

View File

@ -6,14 +6,14 @@ import (
type IMessage interface {
types.IConverter
IsValid(ISettings) bool
GetPubKey() []byte // Public key of the sender.
GetEncKey() []byte // One-time key of encryption data.
GetSalt() []byte // Random bytes for hide data of the hash.
GetHash() []byte // HMAC of the (salt, sender + receiver + payload).
GetSign() []byte // Sign of the hash.
GetPayload() []byte // Main data in the ecnrypted bytes format.
GetPubk() []byte // Public key of the sender.
GetEnck() []byte // One-time key of encryption data.
GetSalt() []byte // Random bytes for hide data of the hash.
GetHash() []byte // HMAC of the (salt, sender + receiver + payload).
GetSign() []byte // Sign of the hash.
GetData() []byte // Main data in the ecnrypted bytes format.
}
type ISettings interface {

View File

@ -8,12 +8,11 @@ import (
type IClient interface {
GetSettings() message.ISettings
GetMessageLimit() uint64
GetPubKey() asymmetric.IPubKey
GetPrivKey() asymmetric.IPrivKey
GetMessageLimit() uint64
EncryptPayload(asymmetric.IPubKey, payload.IPayload) (message.IMessage, error)
DecryptMessage(message.IMessage) (asymmetric.IPubKey, payload.IPayload, error)
}

File diff suppressed because it is too large Load Diff