cfb_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 cipher_test
  5. import (
  6. "bytes"
  7. "crypto/aes"
  8. "crypto/cipher"
  9. "crypto/rand"
  10. "encoding/hex"
  11. "testing"
  12. )
  13. // cfbTests contains the test vectors from
  14. // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf, section
  15. // F.3.13.
  16. var cfbTests = []struct {
  17. key, iv, plaintext, ciphertext string
  18. }{
  19. {
  20. "2b7e151628aed2a6abf7158809cf4f3c",
  21. "000102030405060708090a0b0c0d0e0f",
  22. "6bc1bee22e409f96e93d7e117393172a",
  23. "3b3fd92eb72dad20333449f8e83cfb4a",
  24. },
  25. {
  26. "2b7e151628aed2a6abf7158809cf4f3c",
  27. "3B3FD92EB72DAD20333449F8E83CFB4A",
  28. "ae2d8a571e03ac9c9eb76fac45af8e51",
  29. "c8a64537a0b3a93fcde3cdad9f1ce58b",
  30. },
  31. {
  32. "2b7e151628aed2a6abf7158809cf4f3c",
  33. "C8A64537A0B3A93FCDE3CDAD9F1CE58B",
  34. "30c81c46a35ce411e5fbc1191a0a52ef",
  35. "26751f67a3cbb140b1808cf187a4f4df",
  36. },
  37. {
  38. "2b7e151628aed2a6abf7158809cf4f3c",
  39. "26751F67A3CBB140B1808CF187A4F4DF",
  40. "f69f2445df4f9b17ad2b417be66c3710",
  41. "c04b05357c5d1c0eeac4c66f9ff7f2e6",
  42. },
  43. }
  44. func TestCFBVectors(t *testing.T) {
  45. for i, test := range cfbTests {
  46. key, err := hex.DecodeString(test.key)
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. iv, err := hex.DecodeString(test.iv)
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. plaintext, err := hex.DecodeString(test.plaintext)
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. expected, err := hex.DecodeString(test.ciphertext)
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. block, err := aes.NewCipher(key)
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. ciphertext := make([]byte, len(plaintext))
  67. cfb := cipher.NewCFBEncrypter(block, iv)
  68. cfb.XORKeyStream(ciphertext, plaintext)
  69. if !bytes.Equal(ciphertext, expected) {
  70. t.Errorf("#%d: wrong output: got %x, expected %x", i, ciphertext, expected)
  71. }
  72. cfbdec := cipher.NewCFBDecrypter(block, iv)
  73. plaintextCopy := make([]byte, len(ciphertext))
  74. cfbdec.XORKeyStream(plaintextCopy, ciphertext)
  75. if !bytes.Equal(plaintextCopy, plaintextCopy) {
  76. t.Errorf("#%d: wrong plaintext: got %x, expected %x", i, plaintextCopy, plaintext)
  77. }
  78. }
  79. }
  80. func TestCFBInverse(t *testing.T) {
  81. block, err := aes.NewCipher(commonKey128)
  82. if err != nil {
  83. t.Error(err)
  84. return
  85. }
  86. plaintext := []byte("this is the plaintext. this is the plaintext.")
  87. iv := make([]byte, block.BlockSize())
  88. rand.Reader.Read(iv)
  89. cfb := cipher.NewCFBEncrypter(block, iv)
  90. ciphertext := make([]byte, len(plaintext))
  91. copy(ciphertext, plaintext)
  92. cfb.XORKeyStream(ciphertext, ciphertext)
  93. cfbdec := cipher.NewCFBDecrypter(block, iv)
  94. plaintextCopy := make([]byte, len(plaintext))
  95. copy(plaintextCopy, ciphertext)
  96. cfbdec.XORKeyStream(plaintextCopy, plaintextCopy)
  97. if !bytes.Equal(plaintextCopy, plaintext) {
  98. t.Errorf("got: %x, want: %x", plaintextCopy, plaintext)
  99. }
  100. }