ocfb_test.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright 2010 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package packet
  5. import (
  6. "bytes"
  7. "crypto/aes"
  8. "crypto/rand"
  9. "testing"
  10. )
  11. var commonKey128 = []byte{0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c}
  12. func testOCFB(t *testing.T, resync OCFBResyncOption) {
  13. block, err := aes.NewCipher(commonKey128)
  14. if err != nil {
  15. t.Error(err)
  16. return
  17. }
  18. plaintext := []byte("this is the plaintext, which is long enough to span several blocks.")
  19. randData := make([]byte, block.BlockSize())
  20. rand.Reader.Read(randData)
  21. ocfb, prefix := NewOCFBEncrypter(block, randData, resync)
  22. ciphertext := make([]byte, len(plaintext))
  23. ocfb.XORKeyStream(ciphertext, plaintext)
  24. ocfbdec := NewOCFBDecrypter(block, prefix, resync)
  25. if ocfbdec == nil {
  26. t.Errorf("NewOCFBDecrypter failed (resync: %t)", resync)
  27. return
  28. }
  29. plaintextCopy := make([]byte, len(plaintext))
  30. ocfbdec.XORKeyStream(plaintextCopy, ciphertext)
  31. if !bytes.Equal(plaintextCopy, plaintext) {
  32. t.Errorf("got: %x, want: %x (resync: %t)", plaintextCopy, plaintext, resync)
  33. }
  34. }
  35. func TestOCFB(t *testing.T) {
  36. testOCFB(t, OCFBNoResync)
  37. testOCFB(t, OCFBResync)
  38. }