stupidgcm: add testWipe test

After looking at the cover profile, this was the only untested
code except panic cases.
This commit is contained in:
Jakob Unterwurzacher 2021-09-02 10:17:01 +02:00
parent 961b8ca438
commit 3ba74ac4fc
3 changed files with 30 additions and 8 deletions

View File

@ -9,12 +9,13 @@ import (
"testing" "testing"
) )
func testCiphers(t *testing.T, c1 cipher.AEAD, c2 cipher.AEAD) { func testCiphers(t *testing.T, our cipher.AEAD, ref cipher.AEAD) {
t.Run("testEncryptDecrypt", func(t *testing.T) { testEncryptDecrypt(t, c1, c2) }) t.Run("testEncryptDecrypt", func(t *testing.T) { testEncryptDecrypt(t, our, ref) })
t.Run("testInplaceSeal", func(t *testing.T) { testInplaceSeal(t, c1, c2) }) t.Run("testInplaceSeal", func(t *testing.T) { testInplaceSeal(t, our, ref) })
t.Run("testInplaceOpen", func(t *testing.T) { testInplaceOpen(t, c1, c2) }) t.Run("testInplaceOpen", func(t *testing.T) { testInplaceOpen(t, our, ref) })
t.Run("testCorruption_c1", func(t *testing.T) { testCorruption(t, c1) }) t.Run("testCorruption_c1", func(t *testing.T) { testCorruption(t, our) })
t.Run("testCorruption_c2", func(t *testing.T) { testCorruption(t, c2) }) t.Run("testCorruption_c2", func(t *testing.T) { testCorruption(t, ref) })
t.Run("testWipe", func(t *testing.T) { testWipe(t, our) })
} }
// testEncryptDecrypt encrypts and decrypts using both stupidgcm and Go's built-in // testEncryptDecrypt encrypts and decrypts using both stupidgcm and Go's built-in
@ -161,6 +162,27 @@ func testCorruption(t *testing.T, c cipher.AEAD) {
} }
} }
type Wiper interface {
Wipe()
}
func testWipe(t *testing.T, c cipher.AEAD) {
var key []byte
switch c2 := c.(type) {
case *StupidGCM:
c2.Wipe()
key = c2.key
case *stupidChacha20poly1305:
c2.Wipe()
key = c2.key
default:
t.Fatalf("BUG: unhandled type %t", c2)
}
if key != nil {
t.Fatal("key is not nil")
}
}
// Get "n" random bytes from /dev/urandom or panic // Get "n" random bytes from /dev/urandom or panic
func randBytes(n int) []byte { func randBytes(n int) []byte {
b := make([]byte, n) b := make([]byte, n)

View File

@ -213,7 +213,7 @@ func (g *stupidChacha20poly1305) Open(dst, iv, in, authData []byte) ([]byte, err
// and setting the reference to nil. // and setting the reference to nil.
// //
// This is not bulletproof due to possible GC copies, but // This is not bulletproof due to possible GC copies, but
// still raises to bar for extracting the key. // still raises the bar for extracting the key.
func (g *stupidChacha20poly1305) Wipe() { func (g *stupidChacha20poly1305) Wipe() {
for i := range g.key { for i := range g.key {
g.key[i] = 0 g.key[i] = 0

View File

@ -240,7 +240,7 @@ func (g *StupidGCM) Open(dst, iv, in, authData []byte) ([]byte, error) {
// and setting the reference to nil. // and setting the reference to nil.
// //
// This is not bulletproof due to possible GC copies, but // This is not bulletproof due to possible GC copies, but
// still raises to bar for extracting the key. // still raises the bar for extracting the key.
func (g *StupidGCM) Wipe() { func (g *StupidGCM) Wipe() {
for i := range g.key { for i := range g.key {
g.key[i] = 0 g.key[i] = 0