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.

196 lines
7.2 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 skein1024
  5. import (
  6. "bytes"
  7. "encoding/hex"
  8. "testing"
  9. "github.com/aead/skein"
  10. )
  11. func fromHex(s string) []byte {
  12. b, err := hex.DecodeString(s)
  13. if err != nil {
  14. panic(err)
  15. }
  16. return b
  17. }
  18. var testVectors = []struct {
  19. hashsize int
  20. conf *skein.Config
  21. msg, hash string
  22. }{
  23. {
  24. hashsize: 128,
  25. conf: nil,
  26. msg: "",
  27. hash: "0FFF9563BB3279289227AC77D319B6FFF8D7E9F09DA1247B72A0A265CD6D2A62" +
  28. "645AD547ED8193DB48CFF847C06494A03F55666D3B47EB4C20456C9373C86297" +
  29. "D630D5578EBD34CB40991578F9F52B18003EFA35D3DA6553FF35DB91B81AB890" +
  30. "BEC1B189B7F52CB2A783EBB7D823D725B0B4A71F6824E88F68F982EEFC6D19C6",
  31. },
  32. {
  33. hashsize: 20,
  34. conf: nil,
  35. msg: "FBD17C26B61A82E12E125F0D459B96C91AB4837DFF22B39B78439430CDFC5DC8" +
  36. "78BB393A1A5F79BEF30995A85A12923339BA8AB7D8FC6DC5FEC6F4ED22C122BB" +
  37. "E7EB61981892966DE5CEF576F71FC7A80D14DAB2D0C03940B95B9FB3A727C66A" +
  38. "6E1FF0DC311B9AA21A3054484802154C1826C2A27A0914152AEB76F1168D4410",
  39. hash: "2E6A4CBF2EF05EA9C24B93E8D1DE732DDF2739EB",
  40. },
  41. {
  42. hashsize: 28,
  43. conf: nil,
  44. msg: "FBD17C26B61A82E12E125F0D459B96C91AB4837DFF22B39B78439430CDFC5DC8" +
  45. "78BB393A1A5F79BEF30995A85A12923339BA8AB7D8FC6DC5FEC6F4ED22C122BB" +
  46. "E7EB61981892966DE5CEF576F71FC7A80D14DAB2D0C03940B95B9FB3A727C66A" +
  47. "6E1FF0DC311B9AA21A3054484802154C1826C2A27A0914152AEB76F1168D4410",
  48. hash: "1D6DE19F37F7A3C265440EECB4B9FBD3300BB5AC60895CFC0D4D3C72",
  49. },
  50. {
  51. hashsize: 32,
  52. conf: nil,
  53. msg: "FBD17C26B61A82E12E125F0D459B96C91AB4837DFF22B39B78439430CDFC5DC8" +
  54. "78BB393A1A5F79BEF30995A85A12923339BA8AB7D8FC6DC5FEC6F4ED22C122BB" +
  55. "E7EB61981892966DE5CEF576F71FC7A80D14DAB2D0C03940B95B9FB3A727C66A" +
  56. "6E1FF0DC311B9AA21A3054484802154C1826C2A27A0914152AEB76F1168D4410",
  57. hash: "986A4D472B123E8148731A8EAC9DB23325F0058C4CCBC44A5BB6FE3A8DB672D7",
  58. },
  59. {
  60. hashsize: 48,
  61. conf: nil,
  62. msg: "FBD17C26B61A82E12E125F0D459B96C91AB4837DFF22B39B78439430CDFC5DC8" +
  63. "78BB393A1A5F79BEF30995A85A12923339BA8AB7D8FC6DC5FEC6F4ED22C122BB" +
  64. "E7EB61981892966DE5CEF576F71FC7A80D14DAB2D0C03940B95B9FB3A727C66A" +
  65. "6E1FF0DC311B9AA21A3054484802154C1826C2A27A0914152AEB76F1168D4410",
  66. hash: "9C3D0648C11F31C18395D5E6C8EBD73F43D189843FC45235E2C35E345E12D62B" +
  67. "C21A41F65896DDC6A04969654C2E2CE9",
  68. },
  69. {
  70. hashsize: 128,
  71. conf: nil,
  72. msg: "FBD17C26B61A82E12E125F0D459B96C91AB4837DFF22B39B78439430CDFC5DC8" +
  73. "78BB393A1A5F79BEF30995A85A12923339BA8AB7D8FC6DC5FEC6F4ED22C122BB" +
  74. "E7EB61981892966DE5CEF576F71FC7A80D14DAB2D0C03940B95B9FB3A727C66A" +
  75. "6E1FF0DC311B9AA21A3054484802154C1826C2A27A0914152AEB76F1168D4410",
  76. hash: "96CA81F586C825D0360AEF5ACAEC49AD55289E1797072EEE198B64F349CE65B6" +
  77. "E6ED804FE38F05135FE769CC56240DDDA5098F620865CE4A4278C77FA2EC6BC3" +
  78. "1C0F354CA78C7CA81665BFCC5DC54258C3B8310ED421D9157F36C093814D9B25" +
  79. "103D83E0DDD89C52D0050E13A64C6140E6388431961685734B1F138FE2243086",
  80. },
  81. {
  82. hashsize: 257,
  83. conf: nil,
  84. msg: "FBD17C26B61A82E12E125F0D459B96C91AB4837DFF22B39B78439430CDFC5DC8" +
  85. "78BB393A1A5F79BEF30995A85A12923339BA8AB7D8FC6DC5FEC6F4ED22C122BB" +
  86. "E7EB61981892966DE5CEF576F71FC7A80D14DAB2D0C03940B95B9FB3A727C66A" +
  87. "6E1FF0DC311B9AA21A3054484802154C1826C2A27A0914152AEB76F1168D4410",
  88. hash: "56A0CAB1AD315859DA7A6CFC35807CBFE039AF06CA4B8671C053360BDA0B17C1" +
  89. "4A9EB5EB2ABB01B0DB3F45C03CD30C69D7C1B70C5C9EF74C06FB3AEF0C843CE9" +
  90. "B4C1BA2294DDB5C71CAB692CEDC1E6F908C471B38C0C583418B55AEFDDFE08AB" +
  91. "A4055D0D19EDB5CCBA16C3E288471EFE463E6BF6CC346CC74F6C013E0293E6DF" +
  92. "D2E4AA66A92242FD395B6D91AAAD5A071C449D77EA00E44ECC75073890AC50D4" +
  93. "F4210E8C9DA45385A46D214A0FCCC131DB3F842F955E6E76AC311B3BF439DD51" +
  94. "9BEDD691785ADF7540F3163AD1216CF2ADB7D4BF40D93BE3184AEF51B651CA26" +
  95. "C7EC44073F43AD689D269EA9FF02F8D2C8932FE6CED0292F97FB5F07CA276D6B" +
  96. "43",
  97. },
  98. {
  99. hashsize: 128,
  100. conf: &skein.Config{Key: fromHex("CB41F1706CDE09651203C2D0EFBADDF847A0D315CB2E53FF8BAC41DA0002672E" +
  101. "920244C66E02D5F0DAD3E94C42BB65F0D14157DECF4105EF5609D5B0984457C1" +
  102. "935DF3061FF06E9F204192BA11E5BB2CAC0430C1C370CB3D113FEA5EC1021EB8" +
  103. "75E5946D7A96AC69A1626C6206B7252736F24253C9EE9B85EB852DFC81463134")},
  104. msg: "",
  105. hash: "BCF37B3459C88959D6B6B58B2BFE142CEF60C6F4EC56B0702480D7893A2B0595" +
  106. "AA354E87102A788B61996B9CBC1EADE7DAFBF6581135572C09666D844C90F066" +
  107. "B800FC4F5FD1737644894EF7D588AFC5C38F5D920BDBD3B738AEA3A3267D161E" +
  108. "D65284D1F57DA73B68817E17E381CA169115152B869C66B812BB9A84275303F0",
  109. },
  110. {
  111. hashsize: 128,
  112. conf: &skein.Config{Key: fromHex("CB41F1706CDE09651203C2D0EFBADDF847A0D315CB2E53FF8BAC41DA0002672E" +
  113. "920244C66E02D5F0DAD3E94C42BB65F0D14157DECF4105EF5609D5B0984457C1" +
  114. "935DF3061FF06E9F204192BA11E5BB2CAC0430C1C370CB3D113FEA5EC1021EB8" +
  115. "75E5946D7A96AC69A1626C6206B7252736F24253C9EE9B85EB852DFC81463134" +
  116. "6C")},
  117. msg: "D3090C72",
  118. hash: "DF0596E5808835A3E304AA27923DB05F61DAC57C0696A1D19ABF188E70AA9DBC" +
  119. "C659E9510F7C9A37FBC025BD4E5EA293E78ED7838DD0B08864E8AD40DDB3A880" +
  120. "31EBEFC21572A89960D1916107A7DA7AC0C067E34EC46A86A29CA63FA250BD39" +
  121. "8EB32EC1ED0F8AC8329F26DA018B029E41E2E58D1DFC44DE81615E6C987ED9C9",
  122. },
  123. {
  124. hashsize: 128,
  125. conf: &skein.Config{Key: fromHex("")},
  126. msg: "D3",
  127. hash: "F1FBB54F260D0FB9D49A29EEC184B265EDC663668A9720AA61661E43659B3CD6" +
  128. "97C700CE1E3E535E0C69801220B5DA975138E7CB1EC8D8E3018F078A32CAE28B" +
  129. "C189350B68EE67785623B372EF7811BB06BA6C67E5847596FB72F2B51994EB8E" +
  130. "E079B960E228F7026E1BFE8CEA0877496F986FD13DB82E132CC45F70BB010F27",
  131. },
  132. }
  133. func TestVectors(t *testing.T) {
  134. for i, v := range testVectors {
  135. conf, msg, ref := v.conf, fromHex(v.msg), fromHex(v.hash)
  136. h := New(v.hashsize, conf)
  137. h.Write(msg)
  138. sum := h.Sum(nil)
  139. if !bytes.Equal(sum, ref) {
  140. t.Fatalf("Test vector %d : Hash does not match:\nFound: %s\nExpected: %s", i, hex.EncodeToString(sum), hex.EncodeToString(ref))
  141. }
  142. sum = Sum(msg, v.hashsize, conf)
  143. if !bytes.Equal(sum, ref) {
  144. t.Fatalf("Test vector %d : Hash does not match:\nFound: %s\nExpected: %s", i, hex.EncodeToString(sum), hex.EncodeToString(ref))
  145. }
  146. var key []byte
  147. if conf != nil {
  148. key = conf.Key
  149. }
  150. switch v.hashsize {
  151. case 64:
  152. {
  153. var out [64]byte
  154. Sum512(&out, msg, key)
  155. if !bytes.Equal(out[:], ref) {
  156. t.Fatalf("Test vector %d : Hash does not match:\nFound: %s\nExpected: %s", i, hex.EncodeToString(out[:]), hex.EncodeToString(ref))
  157. }
  158. }
  159. case 48:
  160. {
  161. var out [48]byte
  162. Sum384(&out, msg, key)
  163. if !bytes.Equal(out[:], ref) {
  164. t.Fatalf("Test vector %d : Hash does not match:\nFound: %s\nExpected: %s", i, hex.EncodeToString(out[:]), hex.EncodeToString(ref))
  165. }
  166. }
  167. case 32:
  168. {
  169. var out [32]byte
  170. Sum256(&out, msg, key)
  171. if !bytes.Equal(out[:], ref) {
  172. t.Fatalf("Test vector %d : Hash does not match:\nFound: %s\nExpected: %s", i, hex.EncodeToString(out[:]), hex.EncodeToString(ref))
  173. }
  174. }
  175. case 20:
  176. {
  177. var out [20]byte
  178. Sum160(&out, msg, key)
  179. if !bytes.Equal(out[:], ref) {
  180. t.Fatalf("Test vector %d : Hash does not match:\nFound: %s\nExpected: %s", i, hex.EncodeToString(out[:]), hex.EncodeToString(ref))
  181. }
  182. }
  183. }
  184. }
  185. }