Skip to content

Commit

Permalink
Merge pull request #2 from ericlagergren/fuzz
Browse files Browse the repository at this point in the history
add fuzz and other testing
  • Loading branch information
ericlagergren authored Apr 15, 2022
2 parents 6ca8133 + 3df9a0c commit d0bf8db
Show file tree
Hide file tree
Showing 5 changed files with 584 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: CI
on: ['push', 'pull_request']
on: ['pull_request']

jobs:
ci:
Expand Down
107 changes: 107 additions & 0 deletions fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package siv

import (
"bytes"
"os"
"testing"
"time"

rand "github.com/ericlagergren/saferand"
tink "github.com/google/tink/go/aead/subtle"
)

// TestTink tests against Google Tink.
func TestTink(t *testing.T) {
runTests(t, func(t *testing.T) {
t.Run("128", func(t *testing.T) {
testTink(t, 16)
})
t.Run("256", func(t *testing.T) {
testTink(t, 32)
})
})
}

func testTink(t *testing.T, keySize int) {
d := 2 * time.Second
if testing.Short() {
d = 10 * time.Millisecond
}
if s := os.Getenv("SIV_FUZZ_TIMEOUT"); s != "" {
var err error
d, err = time.ParseDuration(s)
if err != nil {
t.Fatal(err)
}
}
tm := time.NewTimer(d)
t.Cleanup(func() {
tm.Stop()
})

key := make([]byte, keySize)
plaintext := make([]byte, 1*1024*1024) // 1 MB
aad := make([]byte, 1*1024) // 1 KiB
for i := 0; ; i++ {
select {
case <-tm.C:
t.Logf("iters: %d", i)
return
default:
}

if _, err := rand.Read(key); err != nil {
t.Fatal(err)
}
n := rand.Intn(len(plaintext))
if _, err := rand.Read(plaintext[:n]); err != nil {
t.Fatal(err)
}
plaintext := plaintext[:n]

n = rand.Intn(len(aad))
if _, err := rand.Read(aad[:n]); err != nil {
t.Fatal(err)
}
aad := aad[:n]

refAead, err := tink.NewAESGCMSIV(key)
if err != nil {
t.Fatal(err)
}
nonceAndCt, err := refAead.Encrypt(plaintext, aad)
if err != nil {
t.Fatal(err)
}
nonce := nonceAndCt[:NonceSize]
wantCt := nonceAndCt[NonceSize:]

gotAead, err := NewGCM(key)
if err != nil {
t.Fatal(err)
}

gotCt := gotAead.Seal(nil, nonce, plaintext, aad)
if !bytes.Equal(wantCt, gotCt) {
for i, c := range gotCt {
if c != wantCt[i] {
t.Fatalf("bad value at index %d of %d (%d): %#x",
i, len(wantCt), len(wantCt)-i, c)
}
}
t.Fatalf("expected %#x, got %#x", wantCt, gotCt)
}

wantPt, err := refAead.Decrypt(nonceAndCt, aad)
if err != nil {
t.Fatal(err)
}
gotPt, err := gotAead.Open(nil, nonce, wantCt, aad)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(wantPt, gotPt) {
t.Fatalf("expected %#x, got %#x", wantPt, gotPt)
}
}
}
11 changes: 10 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,13 @@ go 1.17

require github.com/ericlagergren/polyval v0.0.0-20220411101811-e25bc10ba391

require golang.org/x/sys v0.0.0-20220408201424-a24fb2fb8a0f
require (
github.com/ericlagergren/saferand v0.0.0-20220206064634-960a4dd2bc5c
github.com/google/tink/go v1.6.1
golang.org/x/sys v0.0.0-20220408201424-a24fb2fb8a0f
)

require (
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect
golang.org/x/exp v0.0.0-20220128181451-c853b6ddb95e // indirect
)
Loading

0 comments on commit d0bf8db

Please sign in to comment.