Skip to content

Commit

Permalink
Add fuzz testing for CryptoAEADAES256GCM
Browse files Browse the repository at this point in the history
Add tests for all encryption and decryption functions.
Uses random data from gofuzz (see GoKillers#1).
  • Loading branch information
silkeh committed Apr 29, 2017
1 parent 2179f68 commit 2596982
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions cryptoaead/crypto_aead_aes256gcm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package cryptoaead

import (
"testing"
"bytes"
"github.com/google/gofuzz"
)

var testCount = 1000000

type Test struct {
Message []byte
Ad []byte
Key [32]byte
Nonce [12]byte
}

func TestCryptoAEADAES256GCM(t *testing.T) {
// Skip the test if unsupported on this platform
if !CryptoAEADAES256GCMIsAvailable() {
t.Skip()
}

// Test the key generation
if len(CryptoAEADAES256GCMKeyGen()) != CryptoAEADAES256GCMKeyBytes() {
t.Error("Generated key has the wrong length")
}

// Fuzzing
f := fuzz.New()

// Run tests
for i := 0; i < testCount; i++ {
var c, m, ec, mac []byte
var err int
var test Test

// Fuzz the test struct
f.Fuzz(&test)

// Detached encryption test
c, mac, err = CryptoAEADAES256GCMEncryptDetached(test.Message, test.Ad, test.Nonce[:], test.Key[:])
if err != 0 {
t.Errorf("Detached encryption failed for %+v", test)
t.FailNow()
}

// Encryption test
ec, err = CryptoAEADAES256GCMEncrypt(test.Message, test.Ad, test.Nonce[:], test.Key[:])
if err != 0 || !bytes.Equal(ec, append(c, mac...)) {
t.Errorf("Encryption failed for %+v", test)
t.FailNow()
}

// Detached decryption test
m, err = CryptoAEADAES256GCMDecryptDetached(c, mac, test.Ad, test.Nonce[:], test.Key[:])
if err != 0 || !bytes.Equal(m, test.Message) {
t.Errorf("Detached decryption failed for %+v", test)
t.FailNow()
}

// Decryption test
m, err = CryptoAEADAES256GCMDecrypt(ec, test.Ad, test.Nonce[:], test.Key[:])
if err != 0 || !bytes.Equal(m, test.Message) {
t.Errorf("Decryption failed for %+v", test)
t.FailNow()
}
}
t.Logf("Completed %v tests", testCount)
}

0 comments on commit 2596982

Please sign in to comment.