| |
| |
| |
|
|
| package cryptotest |
|
|
| import ( |
| "bytes" |
| "crypto/cipher" |
| "testing" |
| ) |
|
|
| type MakeBlock func(key []byte) (cipher.Block, error) |
|
|
| |
| |
| func TestBlock(t *testing.T, keySize int, mb MakeBlock) { |
| |
| key := make([]byte, keySize) |
| newRandReader(t).Read(key) |
| t.Logf("Cipher key: 0x%x", key) |
|
|
| block, err := mb(key) |
| if err != nil { |
| t.Fatal(err) |
| } |
|
|
| blockSize := block.BlockSize() |
|
|
| t.Run("Encryption", func(t *testing.T) { |
| testCipher(t, block.Encrypt, blockSize) |
| }) |
|
|
| t.Run("Decryption", func(t *testing.T) { |
| testCipher(t, block.Decrypt, blockSize) |
| }) |
|
|
| |
| |
| |
| t.Run("Roundtrip", func(t *testing.T) { |
| rng := newRandReader(t) |
|
|
| |
| before, ciphertext, after := make([]byte, blockSize), make([]byte, blockSize), make([]byte, blockSize) |
|
|
| rng.Read(before) |
|
|
| block.Encrypt(ciphertext, before) |
| block.Decrypt(after, ciphertext) |
|
|
| if !bytes.Equal(after, before) { |
| t.Errorf("plaintext is different after an encrypt/decrypt cycle; got %x, want %x", after, before) |
| } |
|
|
| |
| before, plaintext, after := make([]byte, blockSize), make([]byte, blockSize), make([]byte, blockSize) |
|
|
| rng.Read(before) |
|
|
| block.Decrypt(plaintext, before) |
| block.Encrypt(after, plaintext) |
|
|
| if !bytes.Equal(after, before) { |
| t.Errorf("ciphertext is different after a decrypt/encrypt cycle; got %x, want %x", after, before) |
| } |
| }) |
|
|
| } |
|
|
| func testCipher(t *testing.T, cipher func(dst, src []byte), blockSize int) { |
| t.Run("AlterInput", func(t *testing.T) { |
| rng := newRandReader(t) |
|
|
| |
| |
| src, before := make([]byte, blockSize*2), make([]byte, blockSize*2) |
| rng.Read(src) |
| copy(before, src) |
|
|
| dst := make([]byte, blockSize) |
|
|
| cipher(dst, src) |
| if !bytes.Equal(src, before) { |
| t.Errorf("block cipher modified src; got %x, want %x", src, before) |
| } |
| }) |
|
|
| t.Run("Aliasing", func(t *testing.T) { |
| rng := newRandReader(t) |
|
|
| buff, expectedOutput := make([]byte, blockSize), make([]byte, blockSize) |
|
|
| |
| rng.Read(buff) |
| cipher(expectedOutput, buff) |
|
|
| |
| |
| cipher(buff, buff) |
| if !bytes.Equal(buff, expectedOutput) { |
| t.Errorf("block cipher produced different output when dst = src; got %x, want %x", buff, expectedOutput) |
| } |
| }) |
|
|
| t.Run("OutOfBoundsWrite", func(t *testing.T) { |
| rng := newRandReader(t) |
|
|
| src := make([]byte, blockSize) |
| rng.Read(src) |
|
|
| |
| buff := make([]byte, blockSize*3) |
| endOfPrefix, startOfSuffix := blockSize, blockSize*2 |
| rng.Read(buff[:endOfPrefix]) |
| rng.Read(buff[startOfSuffix:]) |
| dst := buff[endOfPrefix:startOfSuffix] |
|
|
| |
| initPrefix, initSuffix := make([]byte, blockSize), make([]byte, blockSize) |
| copy(initPrefix, buff[:endOfPrefix]) |
| copy(initSuffix, buff[startOfSuffix:]) |
|
|
| |
| |
| cipher(dst, src) |
| if !bytes.Equal(buff[startOfSuffix:], initSuffix) { |
| t.Errorf("block cipher did out of bounds write after end of dst slice; got %x, want %x", buff[startOfSuffix:], initSuffix) |
| } |
| if !bytes.Equal(buff[:endOfPrefix], initPrefix) { |
| t.Errorf("block cipher did out of bounds write before beginning of dst slice; got %x, want %x", buff[:endOfPrefix], initPrefix) |
| } |
|
|
| |
| |
| dst = buff[endOfPrefix:] |
| cipher(dst, src) |
| if !bytes.Equal(buff[startOfSuffix:], initSuffix) { |
| t.Errorf("block cipher modified dst past BlockSize bytes; got %x, want %x", buff[startOfSuffix:], initSuffix) |
| } |
| }) |
|
|
| |
| |
| |
| t.Run("OutOfBoundsRead", func(t *testing.T) { |
| rng := newRandReader(t) |
|
|
| src := make([]byte, blockSize) |
| rng.Read(src) |
| expectedDst := make([]byte, blockSize) |
| cipher(expectedDst, src) |
|
|
| |
| buff := make([]byte, blockSize*3) |
| endOfPrefix, startOfSuffix := blockSize, blockSize*2 |
|
|
| copy(buff[endOfPrefix:startOfSuffix], src) |
| rng.Read(buff[:endOfPrefix]) |
| rng.Read(buff[startOfSuffix:]) |
|
|
| testDst := make([]byte, blockSize) |
| cipher(testDst, buff[endOfPrefix:startOfSuffix]) |
| if !bytes.Equal(testDst, expectedDst) { |
| t.Errorf("block cipher affected by data outside of src slice bounds; got %x, want %x", testDst, expectedDst) |
| } |
|
|
| |
| |
| cipher(testDst, buff[endOfPrefix:]) |
| if !bytes.Equal(testDst, expectedDst) { |
| t.Errorf("block cipher affected by src data beyond BlockSize bytes; got %x, want %x", buff[startOfSuffix:], expectedDst) |
| } |
| }) |
|
|
| t.Run("NonZeroDst", func(t *testing.T) { |
| rng := newRandReader(t) |
|
|
| |
| src := make([]byte, blockSize) |
| rng.Read(src) |
| expectedDst := make([]byte, blockSize) |
|
|
| cipher(expectedDst, src) |
|
|
| |
| dst := make([]byte, blockSize*2) |
| rng.Read(dst) |
|
|
| |
| expectedDst = append(expectedDst, dst[blockSize:]...) |
|
|
| cipher(dst, src) |
| if !bytes.Equal(dst, expectedDst) { |
| t.Errorf("block cipher behavior differs when given non-zero dst; got %x, want %x", dst, expectedDst) |
| } |
| }) |
|
|
| t.Run("BufferOverlap", func(t *testing.T) { |
| rng := newRandReader(t) |
|
|
| buff := make([]byte, blockSize*2) |
| rng.Read((buff)) |
|
|
| |
| src := buff[:blockSize] |
| dst := buff[1 : blockSize+1] |
| mustPanic(t, "invalid buffer overlap", func() { cipher(dst, src) }) |
|
|
| |
| src = buff[:blockSize] |
| dst = buff[blockSize-1 : 2*blockSize-1] |
| mustPanic(t, "invalid buffer overlap", func() { cipher(dst, src) }) |
|
|
| |
| src = buff[blockSize-1 : 2*blockSize-1] |
| dst = buff[:blockSize] |
| mustPanic(t, "invalid buffer overlap", func() { cipher(dst, src) }) |
| }) |
|
|
| |
| |
| |
| t.Run("ShortBlock", func(t *testing.T) { |
| |
| |
| |
| byteSlice := func(n int) []byte { return make([]byte, n+1)[0:n] } |
|
|
| |
| mustPanic(t, "input not full block", func() { cipher(byteSlice(blockSize), byteSlice(blockSize-1)) }) |
| mustPanic(t, "output not full block", func() { cipher(byteSlice(blockSize-1), byteSlice(blockSize)) }) |
|
|
| |
| mustPanic(t, "input not full block", func() { cipher(byteSlice(1), byteSlice(1)) }) |
| mustPanic(t, "input not full block", func() { cipher(byteSlice(100), byteSlice(1)) }) |
| mustPanic(t, "output not full block", func() { cipher(byteSlice(1), byteSlice(100)) }) |
| }) |
| } |
|
|
| func mustPanic(t *testing.T, msg string, f func()) { |
| t.Helper() |
|
|
| defer func() { |
| t.Helper() |
|
|
| err := recover() |
|
|
| if err == nil { |
| t.Errorf("function did not panic for %q", msg) |
| } |
| }() |
| f() |
| } |
|
|