Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Fuzz test for RSA #549

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 29 additions & 24 deletions cipher/rsa/rsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,47 +33,52 @@ var rsaTestData = []struct {
"Encrypt full sentence from rsacipher.go main function",
"I think RSA is really great",
},
{
"Encrypt uncommon unicode",
string("\x82"),
},
}

func TestEncryptDecrypt(t *testing.T) {
func FuzzEncryptDecrypt(f *testing.F) {
// Both prime numbers
p := int64(61)
q := int64(53)
p := int64(3079)
q := int64(983)

n := p * q

delta := lcm.Lcm(p-1, q-1)

e := int64(17) // Coprime with delta
e := int64(2837) // Coprime with delta

if gcd.Recursive(e, delta) != 1 {
t.Fatal("Algorithm failed in preamble stage:\n\tPrime numbers are chosen statically and it shouldn't fail at this stage")
f.Fatal("Algorithm failed in preamble stage:\n\tPrime numbers are chosen statically and it shouldn't fail at this stage")
}

d, err := modular.Inverse(e, delta)

if err != nil {
t.Fatalf("Algorithm failed in preamble stage:\n\tProblem with a modular directory dependency: %v", err)
f.Fatalf("Algorithm failed in preamble stage:\n\tProblem with a modular directory dependency: %v", err)
}

for _, test := range rsaTestData {
t.Run(test.description, func(t *testing.T) {

message := []rune(test.input)
encrypted, err := Encrypt(message, e, n)
if err != nil {
t.Fatalf("Failed to Encrypt test string:\n\tDescription: %v\n\tErrMessage: %v", test.description, err)
}

decrypted, err := Decrypt(encrypted, d, n)
if err != nil {
t.Fatalf("Failed to Decrypt test message:\n\tDescription: %v\n\tErrMessage: %v", test.description, err)
}

if actual := test.input; actual != decrypted {
t.Logf("FAIL: %s", test.description)
t.Fatalf("Expecting %v, actual %v", decrypted, actual)
}
})
f.Add(test.input)
}

f.Fuzz(func(t *testing.T, input string) {
message := []rune(input)
encrypted, err := Encrypt(message, e, n)
if err != nil {
t.Fatalf("Failed to Encrypt test string:\n\tInput:%s\n\tErrMessage: %v", input, err)
}

decrypted, err := Decrypt(encrypted, d, n)
if err != nil {
t.Fatalf("Failed to Decrypt test message:\n\tInput:%s\n\tErrMessage: %v", input, err)
}

if string(message) != decrypted {
t.Fatalf("Decrypted ciphertext does not match input: Expecting %v, actual %v", []byte(input), []byte(decrypted))
}
})

}