Skip to content

Commit

Permalink
feat: Improve error handling in ECIES encryption and decryption funct…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
rafagomes committed Aug 10, 2024
1 parent 173a31b commit e4b399d
Showing 1 changed file with 80 additions and 2 deletions.
82 changes: 80 additions & 2 deletions eciesbls12381_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ package eciesbls12381

import (
"bytes"
"errors"
"testing"

"github.com/drand/kyber"
bls "github.com/drand/kyber-bls12381"
)

// TestGenerateECPrivateKey tests the GenerateECPrivateKey function.
Expand All @@ -24,10 +28,15 @@ func TestGenerateECKeypair(t *testing.T) {
// TestGetECPublicKeyFromPrivateKey tests the GetECPublicKeyFromPrivateKey function.
func TestGetECPublicKeyFromPrivateKey(t *testing.T) {
_, privateKey := GenerateECKeypair()
publicKey := GetECPublicKeyFromPrivateKey(privateKey)
if publicKey == nil {
publicKey, err := GetECPublicKeyFromPrivateKey(privateKey)
if err != nil || publicKey == nil {
t.Error("Failed to derive public key from private key")
}

_, err = GetECPublicKeyFromPrivateKey(nil)
if err == nil {
t.Error("Expected an error when private key is nil")
}
}

// TestEncryptWithECAndDecryptWithEC tests the EncryptWithEC and DecryptWithEC functions.
Expand Down Expand Up @@ -70,3 +79,72 @@ func TestInvalidDecryptWithEC(t *testing.T) {
t.Errorf("Decryption should have returned nil, got %s", string(plaintext))
}
}

// TestEncryptWithECError tests EncryptWithEC to cover error handling.
func TestEncryptWithECError(t *testing.T) {
// Test with nil public key
message := []byte("Hello, BLS12381!")
_, err := EncryptWithEC(nil, message)
if err == nil {
t.Error("Expected an error when public key is nil")
}

// Test with nil message
publicKey, _ := GenerateECKeypair()
_, err = EncryptWithEC(publicKey, nil)
if err == nil {
t.Error("Expected an error when message is nil")
}
}

// TestDecryptWithECError tests DecryptWithEC to cover error handling.
func TestDecryptWithECError(t *testing.T) {
// Test with nil private key
ciphertext := []byte("encrypted message")
_, err := DecryptWithEC(nil, ciphertext)
if err == nil {
t.Error("Expected an error when private key is nil")
}

// Test with nil ciphertext
_, privateKey := GenerateECKeypair()
_, err = DecryptWithEC(privateKey, nil)
if err == nil {
t.Error("Expected an error when ciphertext is nil")
}
}

// Mocking a faulty encrypt function to force an error
type faultyEncryptSuite struct {
kyber.Group
}

func (f *faultyEncryptSuite) Point() kyber.Point {
return &faultyPoint{}
}

type faultyPoint struct {
kyber.Point
}

func (p *faultyPoint) Mul(s kyber.Scalar, q kyber.Point) kyber.Point {
return p
}

func (p *faultyPoint) MarshalBinary() ([]byte, error) {
return nil, errors.New("forced error")
}

func TestEncryptWithECForcedError(t *testing.T) {
oldSuite := suite
defer func() { suite = oldSuite }()
suite = &faultyEncryptSuite{Group: bls.NewBLS12381Suite().G2()}

publicKey, _ := GenerateECKeypair()
message := []byte("Hello, BLS12381!")

_, err := EncryptWithEC(publicKey, message)
if err == nil {
t.Error("Encryption should have failed with a forced error")
}
}

0 comments on commit e4b399d

Please sign in to comment.