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.

181 lines
5.8 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 skein256
  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: 32,
  25. conf: nil,
  26. msg: "",
  27. hash: "C8877087DA56E072870DAA843F176E9453115929094C3A40C463A196C29BF7BA",
  28. },
  29. {
  30. hashsize: 32,
  31. conf: nil,
  32. msg: "FF",
  33. hash: "0B98DCD198EA0E50A7A244C444E25C23DA30C10FC9A1F270A6637F1F34E67ED2",
  34. },
  35. {
  36. hashsize: 32,
  37. conf: nil,
  38. msg: "FFFEFDFCFBFAF9F8F7F6F5F4F3F2F1F0EFEEEDECEBEAE9E8E7E6E5E4E3E2E1E0",
  39. hash: "8D0FA4EF777FD759DFD4044E6F6A5AC3C774AEC943DCFC07927B723B5DBF408B",
  40. },
  41. {
  42. hashsize: 32,
  43. conf: nil,
  44. msg: "FFFEFDFCFBFAF9F8F7F6F5F4F3F2F1F0EFEEEDECEBEAE9E8E7E6E5E4E3E2E1E0" +
  45. "DFDEDDDCDBDAD9D8D7D6D5D4D3D2D1D0CFCECDCCCBCAC9C8C7C6C5C4C3C2C1C0",
  46. hash: "DF28E916630D0B44C4A849DC9A02F07A07CB30F732318256B15D865AC4AE162F",
  47. },
  48. {
  49. hashsize: 20,
  50. conf: nil,
  51. msg: "FBD17C26B61A82E12E125F0D459B96C91AB4837DFF22B39B78439430CDFC5DC8" +
  52. "78BB393A1A5F79BEF30995A85A12923339BA8AB7D8FC6DC5FEC6F4ED22C122BB" +
  53. "E7EB61981892966DE5CEF576F71FC7A80D14DAB2D0C03940B95B9FB3A727C66A" +
  54. "6E1FF0DC311B9AA21A3054484802154C1826C2A27A0914152AEB76F1168D4410",
  55. hash: "0CD491B7715704C3A15A45A1CA8D93F8F646D3A1",
  56. },
  57. {
  58. hashsize: 28,
  59. conf: nil,
  60. msg: "FBD17C26B61A82E12E125F0D459B96C91AB4837DFF22B39B78439430CDFC5DC8" +
  61. "78BB393A1A5F79BEF30995A85A12923339BA8AB7D8FC6DC5FEC6F4ED22C122BB" +
  62. "E7EB61981892966DE5CEF576F71FC7A80D14DAB2D0C03940B95B9FB3A727C66A" +
  63. "6E1FF0DC311B9AA21A3054484802154C1826C2A27A0914152AEB76F1168D4410",
  64. hash: "AFD1E2D0F5B6CD4E1F8B3935FA2497D27EE97E72060ADAC099543487",
  65. },
  66. {
  67. hashsize: 32,
  68. conf: nil,
  69. msg: "FBD17C26B61A82E12E125F0D459B96C91AB4837DFF22B39B78439430CDFC5DC8" +
  70. "78BB393A1A5F79BEF30995A85A12923339BA8AB7D8FC6DC5FEC6F4ED22C122BB" +
  71. "E7EB61981892966DE5CEF576F71FC7A80D14DAB2D0C03940B95B9FB3A727C66A" +
  72. "6E1FF0DC311B9AA21A3054484802154C1826C2A27A0914152AEB76F1168D4410",
  73. hash: "4DE6FE2BFDAA3717A4261030EF0E044CED9225D066354610842A24A3EAFD1DCF",
  74. },
  75. {
  76. hashsize: 48,
  77. conf: nil,
  78. msg: "FBD17C26B61A82E12E125F0D459B96C91AB4837DFF22B39B78439430CDFC5DC8" +
  79. "78BB393A1A5F79BEF30995A85A12923339BA8AB7D8FC6DC5FEC6F4ED22C122BB" +
  80. "E7EB61981892966DE5CEF576F71FC7A80D14DAB2D0C03940B95B9FB3A727C66A" +
  81. "6E1FF0DC311B9AA21A3054484802154C1826C2A27A0914152AEB76F1168D4410",
  82. hash: "954620FB31E8B782A2794C6542827026FE069D715DF04261629FCBE81D7D529B" +
  83. "95BA021FA4239FB00AFAA75F5FD8E78B",
  84. },
  85. {
  86. hashsize: 64,
  87. conf: nil,
  88. msg: "FBD17C26B61A82E12E125F0D459B96C91AB4837DFF22B39B78439430CDFC5DC8" +
  89. "78BB393A1A5F79BEF30995A85A12923339BA8AB7D8FC6DC5FEC6F4ED22C122BB" +
  90. "E7EB61981892966DE5CEF576F71FC7A80D14DAB2D0C03940B95B9FB3A727C66A" +
  91. "6E1FF0DC311B9AA21A3054484802154C1826C2A27A0914152AEB76F1168D4410",
  92. hash: "51347E27C7EABBA514959F899A6715EF6AD5CF01C23170590E6A8AF399470BF9" +
  93. "0EA7409960A708C1DBAA90E86389DF254ABC763639BB8CDF7FB663B29D9557C3",
  94. },
  95. {
  96. hashsize: 32,
  97. conf: &skein.Config{Key: fromHex("CB41F1706CDE09651203C2D0EFBADDF8")},
  98. msg: "",
  99. hash: "886E4EFEFC15F06AA298963971D7A25398FFFE5681C84DB39BD00851F64AE29D",
  100. },
  101. {
  102. hashsize: 32,
  103. conf: &skein.Config{Key: fromHex("")},
  104. msg: "D3090C72167517F7C7AD82A70C2FD3F6443F608301591E59",
  105. hash: "DCBD5C8BD09021A840B0EA4AAA2F06E67D7EEBE882B49DE6B74BDC56B60CC48F",
  106. },
  107. {
  108. hashsize: 48,
  109. conf: &skein.Config{Key: fromHex("CB41F1706CDE09651203C2D0EFBADDF847A0D315CB2E53FF8BAC41DA0002672E92")},
  110. msg: "D3090C72167517F7C7AD82A70C2FD3F6443F608301591E598EADB195E8357135" +
  111. "BA26FEDE2EE187417F816048D00FC23512737A2113709A77E4170C49A94B7FDF" +
  112. "F45FF579A72287743102E7766C35CA5ABC5DFE2F63A1E726CE5FBD2926DB03A2" +
  113. "DD18B03FC1508A9AAC45EB362440203A323E09EDEE6324EE2E37B4432C1867ED",
  114. hash: "96E6CEBB23573D0A70CE36A67AA05D2403148093F25C695E1254887CC97F9771" +
  115. "D2518413AF4286BF2A06B61A53F7FCEC",
  116. },
  117. }
  118. func TestVectors(t *testing.T) {
  119. for i, v := range testVectors {
  120. conf, msg, ref := v.conf, fromHex(v.msg), fromHex(v.hash)
  121. h := New(v.hashsize, conf)
  122. h.Write(msg)
  123. sum := h.Sum(nil)
  124. if !bytes.Equal(sum, ref) {
  125. t.Fatalf("Test vector %d : Hash does not match:\nFound: %s\nExpected: %s", i, hex.EncodeToString(sum), hex.EncodeToString(ref))
  126. }
  127. sum = Sum(msg, v.hashsize, conf)
  128. if !bytes.Equal(sum, ref) {
  129. t.Fatalf("Test vector %d : Hash does not match:\nFound: %s\nExpected: %s", i, hex.EncodeToString(sum), hex.EncodeToString(ref))
  130. }
  131. var key []byte
  132. if conf != nil {
  133. key = conf.Key
  134. }
  135. switch v.hashsize {
  136. case 64:
  137. {
  138. var out [64]byte
  139. Sum512(&out, msg, key)
  140. if !bytes.Equal(out[:], ref) {
  141. t.Fatalf("Test vector %d : Hash does not match:\nFound: %s\nExpected: %s", i, hex.EncodeToString(out[:]), hex.EncodeToString(ref))
  142. }
  143. }
  144. case 48:
  145. {
  146. var out [48]byte
  147. Sum384(&out, msg, key)
  148. if !bytes.Equal(out[:], ref) {
  149. t.Fatalf("Test vector %d : Hash does not match:\nFound: %s\nExpected: %s", i, hex.EncodeToString(out[:]), hex.EncodeToString(ref))
  150. }
  151. }
  152. case 32:
  153. {
  154. var out [32]byte
  155. Sum256(&out, msg, key)
  156. if !bytes.Equal(out[:], ref) {
  157. t.Fatalf("Test vector %d : Hash does not match:\nFound: %s\nExpected: %s", i, hex.EncodeToString(out[:]), hex.EncodeToString(ref))
  158. }
  159. }
  160. case 20:
  161. {
  162. var out [20]byte
  163. Sum160(&out, msg, key)
  164. if !bytes.Equal(out[:], ref) {
  165. t.Fatalf("Test vector %d : Hash does not match:\nFound: %s\nExpected: %s", i, hex.EncodeToString(out[:]), hex.EncodeToString(ref))
  166. }
  167. }
  168. }
  169. }
  170. }