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.

166 lines
5.4 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 skein
  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 testVectors = []struct {
  18. hashsize int
  19. conf *Config
  20. msg, hash string
  21. }{
  22. {
  23. hashsize: 64,
  24. conf: nil,
  25. msg: "",
  26. hash: "BC5B4C50925519C290CC634277AE3D6257212395CBA733BBAD37A4AF0FA06AF4" +
  27. "1FCA7903D06564FEA7A2D3730DBDB80C1F85562DFCC070334EA4D1D9E72CBA7A",
  28. },
  29. {
  30. hashsize: 64,
  31. conf: nil,
  32. msg: "FBD17C26B61A82E12E125F0D459B96C91AB4837DFF22B39B78439430CDFC5DC8" +
  33. "78BB393A1A5F79BEF30995A85A12923339BA8AB7D8FC6DC5FEC6F4ED22C122BB",
  34. hash: "02D01535C2DF280FDE92146DF054B0609273C73056C93B94B82F5E7DCC5BE697" +
  35. "9978C4BE24331CAA85D892D2E710C6C9B4904CD056A53547B866BEE097C0FB17",
  36. },
  37. {
  38. hashsize: 20,
  39. conf: nil,
  40. msg: "FBD17C26B61A82E12E125F0D459B96C91AB4837DFF22B39B78439430CDFC5DC8" +
  41. "78BB393A1A5F79BEF30995A85A12923339BA8AB7D8FC6DC5FEC6F4ED22C122BB" +
  42. "E7EB61981892966DE5CEF576F71FC7A80D14DAB2D0C03940B95B9FB3A727C66A" +
  43. "6E1FF0DC311B9AA21A3054484802154C1826C2A27A0914152AEB76F1168D4410",
  44. hash: "EF03079D61B57C6047E15FA2B35B46FA24279539",
  45. },
  46. {
  47. hashsize: 32,
  48. conf: nil,
  49. msg: "FBD17C26B61A82E12E125F0D459B96C91AB4837DFF22B39B78439430CDFC5DC8" +
  50. "78BB393A1A5F79BEF30995A85A12923339BA8AB7D8FC6DC5FEC6F4ED22C122BB" +
  51. "E7EB61981892966DE5CEF576F71FC7A80D14DAB2D0C03940B95B9FB3A727C66A" +
  52. "6E1FF0DC311B9AA21A3054484802154C1826C2A27A0914152AEB76F1168D4410",
  53. hash: "809DD3F763A11AF90912BBB92BC0D94361CBADAB10142992000C88B4CEB88648",
  54. },
  55. {
  56. hashsize: 48,
  57. conf: nil,
  58. msg: "FBD17C26B61A82E12E125F0D459B96C91AB4837DFF22B39B78439430CDFC5DC8" +
  59. "78BB393A1A5F79BEF30995A85A12923339BA8AB7D8FC6DC5FEC6F4ED22C122BB" +
  60. "E7EB61981892966DE5CEF576F71FC7A80D14DAB2D0C03940B95B9FB3A727C66A" +
  61. "6E1FF0DC311B9AA21A3054484802154C1826C2A27A0914152AEB76F1168D4410",
  62. hash: "825F5CBD5DA8807A7B4D3E7BD9CD089CA3A256BCC064CD73A9355BF3AE67F2BF" +
  63. "93AC7074B3B19907A0665BA3A878B262",
  64. },
  65. {
  66. hashsize: 64,
  67. conf: nil,
  68. msg: "FBD17C26B61A82E12E125F0D459B96C91AB4837DFF22B39B78439430CDFC5DC8" +
  69. "78BB393A1A5F79BEF30995A85A12923339BA8AB7D8FC6DC5FEC6F4ED22C122BB" +
  70. "E7EB61981892966DE5CEF576F71FC7A80D14DAB2D0C03940B95B9FB3A727C66A" +
  71. "6E1FF0DC311B9AA21A3054484802154C1826C2A27A0914152AEB76F1168D4410",
  72. hash: "1A0D5ABF4432E7C612D658F8DCFA35B0D1AB68B8D6BD4DD115C23CC57B5C5BCD" +
  73. "DE9BFF0ECE4208596E499F211BC07594D0CB6F3C12B0E110174B2A9B4B2CB6A9",
  74. },
  75. {
  76. hashsize: 128,
  77. conf: nil,
  78. msg: "FBD17C26B61A82E12E125F0D459B96C91AB4837DFF22B39B78439430CDFC5DC8" +
  79. "78BB393A1A5F79BEF30995A85A12923339BA8AB7D8FC6DC5FEC6F4ED22C122BB" +
  80. "E7EB61981892966DE5CEF576F71FC7A80D14DAB2D0C03940B95B9FB3A727C66A" +
  81. "6E1FF0DC311B9AA21A3054484802154C1826C2A27A0914152AEB76F1168D4410",
  82. hash: "8C25D314110D1C0D58054C96A19D571E26A45D5362AA8F47547E53E0BE4A830A" +
  83. "5F2C29CCD88E2185FEBAD024A4696F2DBE8307DC150E7A58B3793B1A93FAE252" +
  84. "3E2D239C59A23A1CC127B3C481A9809162E60B4CB01C011B9630322C8FE9745D" +
  85. "56D0F3AED54B3490578DB4692901EAFC1960C15359176A9C0990B32B8CA8F94B",
  86. },
  87. {
  88. hashsize: 64,
  89. conf: &Config{Key: fromHex("")},
  90. msg: "D3090C72",
  91. hash: "1259AFC2CB025EEF2F681E128F889BBCE57F9A502D57D1A17239A12E71603559" +
  92. "16B72223790FD9A8B367EC96212A3ED239331ED72EF3DEB17685A8D5FD75158D",
  93. },
  94. {
  95. hashsize: 64,
  96. conf: &Config{Key: fromHex("CB41F1706CDE09651203C2D0EFBADDF847A0D315CB2E53FF8BAC41DA0002672E" +
  97. "920244C66E02D5F0DAD3E94C42BB65F0D14157DECF4105EF5609D5B0984457C1")},
  98. msg: "D3090C72167517F7C7AD82A70C2FD3F6",
  99. hash: "478D7B6C0CC6E35D9EBBDEDF39128E5A36585DB6222891692D1747D401DE34CE" +
  100. "3DB6FCBAB6C968B7F2620F4A844A2903B547775579993736D2493A75FF6752A1",
  101. },
  102. }
  103. func TestVectors(t *testing.T) {
  104. for i, v := range testVectors {
  105. conf, msg, ref := v.conf, fromHex(v.msg), fromHex(v.hash)
  106. h := New(v.hashsize, conf)
  107. h.Write(msg)
  108. sum := h.Sum(nil)
  109. if !bytes.Equal(sum, ref) {
  110. t.Fatalf("Test vector %d : Hash does not match:\nFound: %s\nExpected: %s", i, hex.EncodeToString(sum), hex.EncodeToString(ref))
  111. }
  112. sum = Sum(msg, v.hashsize, conf)
  113. if !bytes.Equal(sum, ref) {
  114. t.Fatalf("Test vector %d : Hash does not match:\nFound: %s\nExpected: %s", i, hex.EncodeToString(sum), hex.EncodeToString(ref))
  115. }
  116. var key []byte
  117. if conf != nil {
  118. key = conf.Key
  119. }
  120. switch v.hashsize {
  121. case 64:
  122. {
  123. var out [64]byte
  124. Sum512(&out, msg, key)
  125. if !bytes.Equal(out[:], ref) {
  126. t.Fatalf("Test vector %d : Hash does not match:\nFound: %s\nExpected: %s", i, hex.EncodeToString(out[:]), hex.EncodeToString(ref))
  127. }
  128. }
  129. case 48:
  130. {
  131. var out [48]byte
  132. Sum384(&out, msg, key)
  133. if !bytes.Equal(out[:], ref) {
  134. t.Fatalf("Test vector %d : Hash does not match:\nFound: %s\nExpected: %s", i, hex.EncodeToString(out[:]), hex.EncodeToString(ref))
  135. }
  136. }
  137. case 32:
  138. {
  139. var out [32]byte
  140. Sum256(&out, msg, key)
  141. if !bytes.Equal(out[:], ref) {
  142. t.Fatalf("Test vector %d : Hash does not match:\nFound: %s\nExpected: %s", i, hex.EncodeToString(out[:]), hex.EncodeToString(ref))
  143. }
  144. }
  145. case 20:
  146. {
  147. var out [20]byte
  148. Sum160(&out, msg, key)
  149. if !bytes.Equal(out[:], ref) {
  150. t.Fatalf("Test vector %d : Hash does not match:\nFound: %s\nExpected: %s", i, hex.EncodeToString(out[:]), hex.EncodeToString(ref))
  151. }
  152. }
  153. }
  154. }
  155. }