You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
6.5 KiB

  1. // Copyright (c) 2016 Andreas Auernhammer. All rights reserved.
  2. // Use of this source code is governed by a license that can be
  3. // found in the LICENSE file.
  4. package threefish
  5. import (
  6. "bytes"
  7. "encoding/hex"
  8. "testing"
  9. )
  10. func fromHex(s string) []byte {
  11. b, err := hex.DecodeString(s)
  12. if err != nil {
  13. panic(err)
  14. }
  15. return b
  16. }
  17. var testVectors256 = []struct {
  18. key, tweak, plaintext, ciphertext string
  19. }{
  20. {
  21. key: "0000000000000000000000000000000000000000000000000000000000000000",
  22. tweak: "00000000000000000000000000000000",
  23. plaintext: "0000000000000000000000000000000000000000000000000000000000000000",
  24. ciphertext: "84da2a1f8beaee947066ae3e3103f1ad536db1f4a1192495116b9f3ce6133fd8",
  25. },
  26. {
  27. key: "101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f",
  28. tweak: "000102030405060708090a0b0c0d0e0f",
  29. plaintext: "FFFEFDFCFBFAF9F8F7F6F5F4F3F2F1F0EFEEEDECEBEAE9E8E7E6E5E4E3E2E1E0",
  30. ciphertext: "e0d091ff0eea8fdfc98192e62ed80ad59d865d08588df476657056b5955e97df",
  31. },
  32. }
  33. func TestVectros256(t *testing.T) {
  34. for i, v := range testVectors256 {
  35. key := fromHex(v.key)
  36. tweak := fromHex(v.tweak)
  37. plaintext := fromHex(v.plaintext)
  38. ciphertext := fromHex(v.ciphertext)
  39. var Tweak [TweakSize]byte
  40. copy(Tweak[:], tweak)
  41. c, err := NewCipher(&Tweak, key)
  42. if err != nil {
  43. t.Fatal(err)
  44. }
  45. dst := make([]byte, BlockSize256)
  46. c.Encrypt(dst, plaintext)
  47. if !bytes.Equal(ciphertext, dst) {
  48. t.Fatalf("Test vector %d : Encryption failed\nFound: %s \nExpected: %s", i, hex.EncodeToString(dst), hex.EncodeToString(ciphertext))
  49. }
  50. c.Decrypt(dst, dst)
  51. if !bytes.Equal(plaintext, dst) {
  52. t.Fatalf("Test vector %d : Decryption failed\nFound: %s \nExpected: %s", i, hex.EncodeToString(dst), hex.EncodeToString(plaintext))
  53. }
  54. }
  55. }
  56. // Test vectors from:
  57. // https://github.com/bcgit/bc-java/blob/master/core/src/test/java/org/bouncycastle/crypto/test/Threefish512Test.java
  58. var testVectors512 = []struct {
  59. key, tweak, plaintext, ciphertext string
  60. }{
  61. {
  62. key: "0000000000000000000000000000000000000000000000000000000000000000" +
  63. "0000000000000000000000000000000000000000000000000000000000000000",
  64. tweak: "00000000000000000000000000000000",
  65. plaintext: "0000000000000000000000000000000000000000000000000000000000000000" +
  66. "0000000000000000000000000000000000000000000000000000000000000000",
  67. ciphertext: "b1a2bbc6ef6025bc40eb3822161f36e375d1bb0aee3186fbd19e47c5d479947b" +
  68. "7bc2f8586e35f0cff7e7f03084b0b7b1f1ab3961a580a3e97eb41ea14a6d7bbe",
  69. },
  70. {
  71. key: "101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f" +
  72. "303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f",
  73. tweak: "000102030405060708090a0b0c0d0e0f",
  74. plaintext: "fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0efeeedecebeae9e8e7e6e5e4e3e2e1e0" +
  75. "dfdedddcdbdad9d8d7d6d5d4d3d2d1d0cfcecdcccbcac9c8c7c6c5c4c3c2c1c0",
  76. ciphertext: "e304439626d45a2cb401cad8d636249a6338330eb06d45dd8b36b90e97254779" +
  77. "272a0a8d99463504784420ea18c9a725af11dffea10162348927673d5c1caf3d",
  78. },
  79. }
  80. func TestVectros512(t *testing.T) {
  81. for i, v := range testVectors512 {
  82. key := fromHex(v.key)
  83. tweak := fromHex(v.tweak)
  84. plaintext := fromHex(v.plaintext)
  85. ciphertext := fromHex(v.ciphertext)
  86. var Tweak [TweakSize]byte
  87. copy(Tweak[:], tweak)
  88. c, err := NewCipher(&Tweak, key)
  89. if err != nil {
  90. t.Fatal(err)
  91. }
  92. dst := make([]byte, BlockSize512)
  93. c.Encrypt(dst, plaintext)
  94. if !bytes.Equal(ciphertext, dst) {
  95. t.Fatalf("Test vector %d : Encryption failed\nFound: %s \nExpected: %s", i, hex.EncodeToString(dst), hex.EncodeToString(ciphertext))
  96. }
  97. c.Decrypt(dst, dst)
  98. if !bytes.Equal(plaintext, dst) {
  99. t.Fatalf("Test vector %d : Decryption failed\nFound: %s \nExpected: %s", i, hex.EncodeToString(dst), hex.EncodeToString(plaintext))
  100. }
  101. }
  102. }
  103. // Test vectors from:
  104. // https://github.com/bcgit/bc-java/blob/master/core/src/test/java/org/bouncycastle/crypto/test/Threefish1024Test.java
  105. var testVectors1024 = []struct {
  106. key, tweak, plaintext, ciphertext string
  107. }{
  108. {
  109. key: "0000000000000000000000000000000000000000000000000000000000000000" +
  110. "0000000000000000000000000000000000000000000000000000000000000000" +
  111. "0000000000000000000000000000000000000000000000000000000000000000" +
  112. "0000000000000000000000000000000000000000000000000000000000000000",
  113. tweak: "00000000000000000000000000000000",
  114. plaintext: "0000000000000000000000000000000000000000000000000000000000000000" +
  115. "0000000000000000000000000000000000000000000000000000000000000000" +
  116. "0000000000000000000000000000000000000000000000000000000000000000" +
  117. "0000000000000000000000000000000000000000000000000000000000000000",
  118. ciphertext: "f05c3d0a3d05b304f785ddc7d1e036015c8aa76e2f217b06c6e1544c0bc1a90d" +
  119. "f0accb9473c24e0fd54fea68057f43329cb454761d6df5cf7b2e9b3614fbd5a2" +
  120. "0b2e4760b40603540d82eabc5482c171c832afbe68406bc39500367a592943fa" +
  121. "9a5b4a43286ca3c4cf46104b443143d560a4b230488311df4feef7e1dfe8391e",
  122. },
  123. {
  124. key: "101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f" +
  125. "303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f" +
  126. "505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f" +
  127. "707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f",
  128. tweak: "000102030405060708090a0b0c0d0e0f",
  129. plaintext: "fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0efeeedecebeae9e8e7e6e5e4e3e2e1e0" +
  130. "dfdedddcdbdad9d8d7d6d5d4d3d2d1d0cfcecdcccbcac9c8c7c6c5c4c3c2c1c0" +
  131. "bfbebdbcbbbab9b8b7b6b5b4b3b2b1b0afaeadacabaaa9a8a7a6a5a4a3a2a1a0" +
  132. "9f9e9d9c9b9a999897969594939291908f8e8d8c8b8a89888786858483828180",
  133. ciphertext: "a6654ddbd73cc3b05dd777105aa849bce49372eaaffc5568d254771bab85531c" +
  134. "94f780e7ffaae430d5d8af8c70eebbe1760f3b42b737a89cb363490d670314bd" +
  135. "8aa41ee63c2e1f45fbd477922f8360b388d6125ea6c7af0ad7056d01796e90c8" +
  136. "3313f4150a5716b30ed5f569288ae974ce2b4347926fce57de44512177dd7cde",
  137. },
  138. }
  139. func TestVectros1024(t *testing.T) {
  140. for i, v := range testVectors1024 {
  141. key := fromHex(v.key)
  142. tweak := fromHex(v.tweak)
  143. plaintext := fromHex(v.plaintext)
  144. ciphertext := fromHex(v.ciphertext)
  145. var Tweak [TweakSize]byte
  146. copy(Tweak[:], tweak)
  147. c, err := NewCipher(&Tweak, key)
  148. if err != nil {
  149. t.Fatal(err)
  150. }
  151. dst := make([]byte, BlockSize1024)
  152. c.Encrypt(dst, plaintext)
  153. if !bytes.Equal(ciphertext, dst) {
  154. t.Fatalf("Test vector %d : Encryption failed\nFound: %s \nExpected: %s", i, hex.EncodeToString(dst), hex.EncodeToString(ciphertext))
  155. }
  156. c.Decrypt(dst, dst)
  157. if !bytes.Equal(plaintext, dst) {
  158. t.Fatalf("Test vector %d : Decryption failed\nFound: %s \nExpected: %s", i, hex.EncodeToString(dst), hex.EncodeToString(plaintext))
  159. }
  160. }
  161. }