cast5_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 cast5
  5. import (
  6. "bytes"
  7. "encoding/hex"
  8. "testing"
  9. )
  10. // This test vector is taken from RFC 2144, App B.1.
  11. // Since the other two test vectors are for reduced-round variants, we can't
  12. // use them.
  13. var basicTests = []struct {
  14. key, plainText, cipherText string
  15. }{
  16. {
  17. "0123456712345678234567893456789a",
  18. "0123456789abcdef",
  19. "238b4fe5847e44b2",
  20. },
  21. }
  22. func TestBasic(t *testing.T) {
  23. for i, test := range basicTests {
  24. key, _ := hex.DecodeString(test.key)
  25. plainText, _ := hex.DecodeString(test.plainText)
  26. expected, _ := hex.DecodeString(test.cipherText)
  27. c, err := NewCipher(key)
  28. if err != nil {
  29. t.Errorf("#%d: failed to create Cipher: %s", i, err)
  30. continue
  31. }
  32. var cipherText [BlockSize]byte
  33. c.Encrypt(cipherText[:], plainText)
  34. if !bytes.Equal(cipherText[:], expected) {
  35. t.Errorf("#%d: got:%x want:%x", i, cipherText, expected)
  36. }
  37. var plainTextAgain [BlockSize]byte
  38. c.Decrypt(plainTextAgain[:], cipherText[:])
  39. if !bytes.Equal(plainTextAgain[:], plainText) {
  40. t.Errorf("#%d: got:%x want:%x", i, plainTextAgain, plainText)
  41. }
  42. }
  43. }
  44. // TestFull performs the test specified in RFC 2144, App B.2.
  45. // However, due to the length of time taken, it's disabled here and a more
  46. // limited version is included, below.
  47. func TestFull(t *testing.T) {
  48. if testing.Short() {
  49. // This is too slow for normal testing
  50. return
  51. }
  52. a, b := iterate(1000000)
  53. const expectedA = "eea9d0a249fd3ba6b3436fb89d6dca92"
  54. const expectedB = "b2c95eb00c31ad7180ac05b8e83d696e"
  55. if hex.EncodeToString(a) != expectedA {
  56. t.Errorf("a: got:%x want:%s", a, expectedA)
  57. }
  58. if hex.EncodeToString(b) != expectedB {
  59. t.Errorf("b: got:%x want:%s", b, expectedB)
  60. }
  61. }
  62. func iterate(iterations int) ([]byte, []byte) {
  63. const initValueHex = "0123456712345678234567893456789a"
  64. initValue, _ := hex.DecodeString(initValueHex)
  65. var a, b [16]byte
  66. copy(a[:], initValue)
  67. copy(b[:], initValue)
  68. for i := 0; i < iterations; i++ {
  69. c, _ := NewCipher(b[:])
  70. c.Encrypt(a[:8], a[:8])
  71. c.Encrypt(a[8:], a[8:])
  72. c, _ = NewCipher(a[:])
  73. c.Encrypt(b[:8], b[:8])
  74. c.Encrypt(b[8:], b[8:])
  75. }
  76. return a[:], b[:]
  77. }
  78. func TestLimited(t *testing.T) {
  79. a, b := iterate(1000)
  80. const expectedA = "23f73b14b02a2ad7dfb9f2c35644798d"
  81. const expectedB = "e5bf37eff14c456a40b21ce369370a9f"
  82. if hex.EncodeToString(a) != expectedA {
  83. t.Errorf("a: got:%x want:%s", a, expectedA)
  84. }
  85. if hex.EncodeToString(b) != expectedB {
  86. t.Errorf("b: got:%x want:%s", b, expectedB)
  87. }
  88. }