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 0e93ce9 commit 011b403
Show file tree
Hide file tree
Showing 28 changed files with 296 additions and 316 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

*??? ??, ????*

### CHANGES

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

<!-- ... -->

## v1.7.1
Expand Down
38 changes: 18 additions & 20 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func NewClient(pPrivKey asymmetric.IPrivKey, pMessageSize uint64) IClient {
fPrivKey: pPrivKey,
}

kemPubKey := client.GetPrivKey().GetKEMPrivKey().GetPubKey()
encMsg, err := client.encryptWithParams(kemPubKey, []byte{}, 0)
pubKey := client.GetPrivKey().GetPubKey()
encMsg, err := client.encryptWithParams(pubKey, []byte{}, 0)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -68,7 +68,7 @@ func (p *sClient) GetPrivKey() asymmetric.IPrivKey {

// Encrypt message with public key of receiver.
// The message can be decrypted only if private key is known.
func (p *sClient) EncryptMessage(pRecv asymmetric.IKEMPubKey, pMsg []byte) ([]byte, error) {
func (p *sClient) EncryptMessage(pRecv asymmetric.IPubKey, pMsg []byte) ([]byte, error) {
var (
payloadLimit = p.GetPayloadLimit()
resultSize = uint64(len(pMsg))
Expand All @@ -82,27 +82,27 @@ func (p *sClient) EncryptMessage(pRecv asymmetric.IKEMPubKey, pMsg []byte) ([]by
}

func (p *sClient) encryptWithParams(
pRecv asymmetric.IKEMPubKey,
pRecv asymmetric.IPubKey,
pMsg []byte,
pPadd uint64,
) ([]byte, error) {
var (
rand = random.NewRandom()
salt = rand.GetBytes(cSaltSize)
pkey = p.fPrivKey.GetPubKey().ToBytes()
pkid = p.fPrivKey.GetPubKey().GetHasher().ToBytes()
)

data := joiner.NewBytesJoiner32([][]byte{pMsg, rand.GetBytes(pPadd)})
hash := hashing.NewHMACHasher(salt, bytes.Join(
[][]byte{
pkey,
pkid,
pRecv.ToBytes(),
data,
},
[]byte{},
)).ToBytes()

ct, sk, err := pRecv.Encapsulate()
ct, sk, err := pRecv.GetKEMPubKey().Encapsulate()
if err != nil {
return nil, ErrEncryptSymmetricKey
}
Expand All @@ -111,7 +111,7 @@ func (p *sClient) encryptWithParams(
return message.NewMessage(
ct,
cipher.EncryptBytes(joiner.NewBytesJoiner32([][]byte{
pkey,
pkid,
salt,
hash,
p.fPrivKey.GetDSAPrivKey().SignBytes(hash),
Expand All @@ -122,15 +122,14 @@ func (p *sClient) encryptWithParams(

// 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.IPubKey, []byte, error) {
func (p *sClient) DecryptMessage(pMapPubKeys asymmetric.IMapPubKeys, pMsg []byte) (asymmetric.IPubKey, []byte, error) {
msg, err := message.LoadMessage(p.fMessageSize, pMsg)
if err != nil {
return nil, nil, ErrInitCheckMessage
}

// Decrypt session key by private key of receiver.
kemPrivKey := p.fPrivKey.GetKEMPrivKey()
skey, err := kemPrivKey.Decapsulate(msg.GetEnck())
skey, err := p.fPrivKey.GetKEMPrivKey().Decapsulate(msg.GetEnck())
if err != nil {
return nil, nil, ErrDecryptCipherKey
}
Expand All @@ -144,34 +143,33 @@ func (p *sClient) DecryptMessage(pMsg []byte) (asymmetric.IPubKey, []byte, error

// Decode wrapped data.
var (
pkey = decSlice[0]
pkid = decSlice[0]
salt = decSlice[1]
hash = decSlice[2]
sign = decSlice[3]
data = decSlice[4]
)

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

// Validate received hash with generated hash.
check := hashing.NewHMACHasher(salt, bytes.Join(
[][]byte{
pubKey.ToBytes(),
kemPrivKey.GetPubKey().ToBytes(),
sPubKey.ToBytes(),
p.fPrivKey.GetPubKey().ToBytes(),
data,
},
[]byte{},
)).ToBytes()
if !bytes.Equal(check, hash) {
if bytes.Equal(check, hash) {
return nil, nil, ErrInvalidDataHash
}

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

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

// Return public key of sender with payload.
return pubKey, payloadWrapper[0], nil
return sPubKey, payloadWrapper[0], nil
}
27 changes: 14 additions & 13 deletions pkg/client/client_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,30 @@ goos: linux
goarch: amd64
pkg: github.com/number571/go-peer/pkg/client
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkClient/kyber=768-bit,dilithium=mode3-12 1000 132328 ns/op
--- BENCH: BenchmarkClient/kyber=768-bit,dilithium=mode3-12
client_bench_test.go:66: Timer_Encrypt(N=1): 280.196µs
client_bench_test.go:79: Timer_Decrypt(N=1): 155.822µs
client_bench_test.go:66: Timer_Encrypt(N=1000): 254.339142ms
client_bench_test.go:79: Timer_Decrypt(N=1000): 132.305197ms
BenchmarkClient/mlkem=768,mldsa=65-12 10000 94239 ns/op
--- BENCH: BenchmarkClient/mlkem=768,mldsa=65-12
client_bench_test.go:67: Timer_Encrypt(N=1): 207.249µs
client_bench_test.go:80: Timer_Decrypt(N=1): 131.503µs
client_bench_test.go:67: Timer_Encrypt(N=10000): 2.376859383s
client_bench_test.go:80: Timer_Decrypt(N=10000): 942.366433ms
PASS
*/

// go test -bench=BenchmarkClient -benchtime=1000x -timeout 99999s
func BenchmarkClient(b *testing.B) {
privKeyChain := asymmetric.NewPrivKey()
privKey := asymmetric.NewPrivKey()
pubKey := privKey.GetPubKey()

mapKeys := asymmetric.NewMapPubKeys()
mapKeys.SetPubKey(pubKey)

benchTable := []struct {
name string
client IClient
}{
{
name: "mlkem=768,mldsa=65",
client: NewClient(privKeyChain, (8 << 10)),
client: NewClient(privKey, (8 << 10)),
},
}

Expand All @@ -51,10 +55,7 @@ func BenchmarkClient(b *testing.B) {

nowEnc := time.Now()
for i := 0; i < b.N; i++ {
encMsg, err := t.client.EncryptMessage(
t.client.GetPrivKey().GetKEMPrivKey().GetPubKey(),
randomBytes[i],
)
encMsg, err := t.client.EncryptMessage(pubKey, randomBytes[i])
if err != nil {
b.Error(err)
return
Expand All @@ -68,7 +69,7 @@ func BenchmarkClient(b *testing.B) {

nowDec := time.Now()
for i := 0; i < b.N; i++ {
_, _, err := t.client.DecryptMessage(encMessages[i])
_, _, err := t.client.DecryptMessage(mapKeys, encMessages[i])
if err != nil {
b.Error(err)
return
Expand Down
Loading

0 comments on commit 011b403

Please sign in to comment.