Skip to content

Commit

Permalink
generateMac bug fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Dharitri-org committed Jan 31, 2024
1 parent d35e667 commit 2d69e7a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 29 deletions.
30 changes: 14 additions & 16 deletions encryption/x25519/pubKeyEncryptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,28 @@ const EncryptionVersion = 1
const EncryptionCipher = "x25519-xsalsa20-poly1305"

// EncryptedDataIdentities holds the data associated with the identities involved
//
// in the encryption process - who is able to decrypt, the ephemeral public key
// used to encrypt, and the address of the originator of the encryption
// used to authenticate that indeed a message was encrypted by Bob
// for Alice (remember that the private key used for encryption
// is ephemeral - in order to avoid nonce reuses and
// multipurpose use of the same secret)
// in the encryption process - who is able to decrypt, the ephemeral public key
// used to encrypt, and the address of the originator of the encryption
// used to authenticate that indeed a message was encrypted by Bob
// for Alice (remember that the private key used for encryption
// is ephemeral - in order to avoid nonce reuses and
// multipurpose use of the same secret)
type EncryptedDataIdentities struct {
Recipient string `json:"recipient"`
EphemeralPubKey string `json:"ephemeralPubKey"`
OriginatorPubKey string `json:"originatorPubKey"`
}

// EncryptedCryptoData holds crypto information such as the cipher used, the ciphertext itself
//
// and the authentication code
// and the authentication code
type EncryptedCryptoData struct {
Cipher string `json:"cipher"`
Ciphertext string `json:"ciphertext"`
MAC string `json:"mac"`
}

// EncryptedData holds the needed information of an encrypted
//
// message required to correctly be decrypted by the recipient
// message required to correctly be decrypted by the recipient
type EncryptedData struct {
Nonce string `json:"nonce"`
Version uint8 `json:"version"`
Expand All @@ -53,9 +50,8 @@ type EncryptedData struct {
}

// Encrypt generates a public key encryption for a message using a recipient edwards public key and an ephemeral
//
// private key generated on the spot. The senderPrivateKey param is used to authenticate the encryption
// that normally should happen between two edwards curve identities.
// private key generated on the spot. The senderPrivateKey param is used to authenticate the encryption
// that normally should happen between two edwards curve identities.
func (ed *EncryptedData) Encrypt(data []byte, recipientPubKey crypto.PublicKey, senderPrivateKey crypto.PrivateKey) error {
suite := ed25519.NewEd25519()
ephemeralEdScalar, ephemeralEdPoint := suite.CreateKeyPair()
Expand All @@ -80,6 +76,9 @@ func (ed *EncryptedData) Encrypt(data []byte, recipientPubKey crypto.PublicKey,
return err
}
mac, err := ed.generateMAC(senderPrivateKey, append(ciphertext, ephemeralEdPointBytes...))
if err != nil {
return err
}

senderPubKey, err := senderPrivateKey.GeneratePublic().ToByteArray()
if err != nil {
Expand All @@ -99,8 +98,7 @@ func (ed *EncryptedData) Encrypt(data []byte, recipientPubKey crypto.PublicKey,
}

// Decrypt returns the plain text associated to a ciphertext that was previously encrypted
//
// using the public key of the recipient
// using the public key of the recipient
func (ed *EncryptedData) Decrypt(recipientPrivateKey crypto.PrivateKey) ([]byte, error) {
encryptedMessage, err := hex.DecodeString(ed.Crypto.Ciphertext)
if err != nil {
Expand Down
58 changes: 45 additions & 13 deletions signing/mcl/multisig/bls_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,35 @@ func benchmarkConcatPubKeys(nPubKeys int, b *testing.B) {
}

func Benchmark_AggregatedSig63(b *testing.B) {
benchmarkAggregatedSig(63, b)
}
hasher, err := blake2b.NewBlake2bWithSize(blsHashSize)
require.Nil(b, err)
llSig := &multisig.BlsMultiSigner{Hasher: hasher}

func Benchmark_AggregatedSig400(b *testing.B) {
benchmarkAggregatedSig(400, b)
benchmarkAggregatedSig(63, llSig, b)
}

func benchmarkAggregatedSig(nPubKeys uint16, b *testing.B) {
msg := []byte(testMessage)
func Benchmark_AggregatedSigKOSK63(b *testing.B) {
llSig := &multisig.BlsMultiSignerKOSK{}

benchmarkAggregatedSig(63, llSig, b)
}

func Benchmark_AggregatedSig400(b *testing.B) {
hasher, err := blake2b.NewBlake2bWithSize(blsHashSize)
require.Nil(b, err)
llSig := &multisig.BlsMultiSigner{Hasher: hasher}

benchmarkAggregatedSig(400, llSig, b)
}

func Benchmark_AggregatedSigKOSK400(b *testing.B) {
llSig := &multisig.BlsMultiSignerKOSK{}

benchmarkAggregatedSig(400, llSig, b)
}

func benchmarkAggregatedSig(nPubKeys uint16, llSig crypto.LowLevelSignerBLS, b *testing.B) {
msg := []byte(testMessage)
pubKeys, sigShares := createSigSharesBLS(nPubKeys, msg, llSig)

b.ResetTimer()
Expand All @@ -78,20 +94,36 @@ func benchmarkAggregatedSig(nPubKeys uint16, b *testing.B) {
}

func Benchmark_VerifyAggregatedSig63(b *testing.B) {
benchmarkVerifyAggregatedSig(63, b)
}
hasher, err := blake2b.NewBlake2bWithSize(blsHashSize)
require.Nil(b, err)
llSig := &multisig.BlsMultiSigner{Hasher: hasher}

func Benchmark_VerifyAggregatedSig400(b *testing.B) {
benchmarkVerifyAggregatedSig(400, b)
benchmarkVerifyAggregatedSig(63, llSig, b)
}

func benchmarkVerifyAggregatedSig(nPubKeys uint16, b *testing.B) {
msg := []byte(testMessage)
func Benchmark_VerifyAggregatedSigKOSK63(b *testing.B) {
llSig := &multisig.BlsMultiSignerKOSK{}

benchmarkVerifyAggregatedSig(63, llSig, b)
}

func Benchmark_VerifyAggregatedSig400(b *testing.B) {
hasher, err := blake2b.NewBlake2bWithSize(blsHashSize)
require.Nil(b, err)

llSig := &multisig.BlsMultiSigner{Hasher: hasher}

benchmarkVerifyAggregatedSig(400, llSig, b)
}

func Benchmark_VerifyAggregatedSigKOSK400(b *testing.B) {
llSig := &multisig.BlsMultiSignerKOSK{}

benchmarkVerifyAggregatedSig(400, llSig, b)
}

func benchmarkVerifyAggregatedSig(nPubKeys uint16, llSig crypto.LowLevelSignerBLS, b *testing.B) {
msg := []byte(testMessage)

pubKeys, sigShares := createSigSharesBLS(nPubKeys, msg, llSig)
aggSigBytes, err := llSig.AggregateSignatures(pubKeys[0].Suite(), sigShares, pubKeys)
require.Nil(b, err)
Expand Down

0 comments on commit 2d69e7a

Please sign in to comment.