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.

158 lines
4.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 skein
  5. import (
  6. "bytes"
  7. "encoding/hex"
  8. "hash"
  9. "testing"
  10. )
  11. func testWrite(msg string, t *testing.T, h hash.Hash, c *Config) {
  12. var msg1 []byte
  13. msg0 := make([]byte, 64)
  14. for i := range msg0 {
  15. h.Write(msg0[:i])
  16. msg1 = append(msg1, msg0[:i]...)
  17. }
  18. tag0 := h.Sum(nil)
  19. tag1 := Sum(msg1, h.Size(), c)
  20. if !bytes.Equal(tag0, tag1) {
  21. t.Fatalf("%s\nSum differ from Sum\n Sum: %s \n skein.Sum: %s", msg, hex.EncodeToString(tag0), hex.EncodeToString(tag1))
  22. }
  23. }
  24. func TestWrite(t *testing.T) {
  25. testWrite("testWrite(t, New256(nil), nil)", t, New256(nil), nil)
  26. testWrite("testWrite(t, New256(make([]byte, 16)), &Config{Key: make([]byte, 16)})", t, New256(make([]byte, 16)), &Config{Key: make([]byte, 16)})
  27. testWrite("testWrite(t, New512(nil), nil)", t, New512(nil), nil)
  28. testWrite("testWrite(t, New512(make([]byte, 16)), &Config{Key: make([]byte, 16)})", t, New512(make([]byte, 16)), &Config{Key: make([]byte, 16)})
  29. testWrite("testWrite(t, New(128, nil), nil)", t, New(128, nil), nil)
  30. testWrite("testWrite(t, New(128, &Config{Key: make([]byte, 16)}), &Config{Key: make([]byte, 16)})", t, New(128, &Config{Key: make([]byte, 16)}), &Config{Key: make([]byte, 16)})
  31. }
  32. func TestBlockSize(t *testing.T) {
  33. h := New(64, nil)
  34. if bs := h.BlockSize(); bs != BlockSize {
  35. t.Fatalf("BlockSize() returned: %d - but expected: %d", bs, BlockSize)
  36. }
  37. }
  38. func TestSum(t *testing.T) {
  39. sizes := []int{20, 32, 48, 64}
  40. key := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
  41. msg := make([]byte, 512)
  42. for i := range msg {
  43. msg[i] = byte(i) + key[i%len(key)]
  44. }
  45. c := &Config{Key: key}
  46. for _, hashsize := range sizes {
  47. switch hashsize {
  48. case 20:
  49. {
  50. var sum0 [20]byte
  51. Sum160(&sum0, msg, key)
  52. sum1 := Sum(msg, hashsize, c)
  53. if !bytes.Equal(sum0[:], sum1) {
  54. t.Fatalf("Sum160 differ from Sum: Sum160: %s\n Sum: %s", hex.EncodeToString(sum0[:]), hex.EncodeToString(sum1))
  55. }
  56. }
  57. case 32:
  58. {
  59. var sum0 [32]byte
  60. Sum256(&sum0, msg, key)
  61. sum1 := Sum(msg, hashsize, c)
  62. if !bytes.Equal(sum0[:], sum1) {
  63. t.Fatalf("Sum256 differ from Sum: Sum256: %s\n Sum: %s", hex.EncodeToString(sum0[:]), hex.EncodeToString(sum1))
  64. }
  65. }
  66. case 48:
  67. {
  68. var sum0 [48]byte
  69. Sum384(&sum0, msg, key)
  70. sum1 := Sum(msg, hashsize, c)
  71. if !bytes.Equal(sum0[:], sum1) {
  72. t.Fatalf("Sum384 differ from Sum: Sum384: %s\n Sum: %s", hex.EncodeToString(sum0[:]), hex.EncodeToString(sum1))
  73. }
  74. }
  75. case 64:
  76. {
  77. var sum0 [64]byte
  78. Sum512(&sum0, msg, key)
  79. sum1 := Sum(msg, hashsize, c)
  80. if !bytes.Equal(sum0[:], sum1) {
  81. t.Fatalf("Sum512 differ from Sum: Sum512: %s\n Sum: %s", hex.EncodeToString(sum0[:]), hex.EncodeToString(sum1))
  82. }
  83. }
  84. }
  85. }
  86. }
  87. func TestInitialize(t *testing.T) {
  88. rec := func() {
  89. if err := recover(); err == nil {
  90. t.Fatal("Recover expected error, but no one occured")
  91. }
  92. }
  93. mustFail := func() {
  94. defer rec()
  95. s := new(hashFunc)
  96. s.initialize(0, nil)
  97. }
  98. mustFail()
  99. c := &Config{
  100. Key: make([]byte, 16),
  101. KeyID: make([]byte, 16),
  102. Personal: make([]byte, 8),
  103. Nonce: make([]byte, 12),
  104. PublicKey: make([]byte, 128),
  105. }
  106. testWrite("testWrite(t, New(64, c), c)", t, New(64, c), c)
  107. }
  108. // Benchmarks
  109. func benchmarkSum(b *testing.B, size int) {
  110. msg := make([]byte, size)
  111. b.SetBytes(int64(size))
  112. b.ResetTimer()
  113. for i := 0; i < b.N; i++ {
  114. Sum(msg, 64, nil)
  115. }
  116. }
  117. func benchmarkSum512(b *testing.B, size int) {
  118. var sum [64]byte
  119. msg := make([]byte, size)
  120. b.SetBytes(int64(size))
  121. b.ResetTimer()
  122. for i := 0; i < b.N; i++ {
  123. Sum512(&sum, msg, nil)
  124. }
  125. }
  126. func BenchmarkSum_64(b *testing.B) { benchmarkSum(b, 64) }
  127. func BenchmarkSum_1K(b *testing.B) { benchmarkSum(b, 1024) }
  128. func BenchmarkSum512_64(b *testing.B) { benchmarkSum512(b, 64) }
  129. func BenchmarkSum512_1K(b *testing.B) { benchmarkSum512(b, 1024) }
  130. func benchmarkWrite(b *testing.B, size int) {
  131. h := New512(nil)
  132. msg := make([]byte, size)
  133. b.SetBytes(int64(size))
  134. b.ResetTimer()
  135. for i := 0; i < b.N; i++ {
  136. h.Write(msg)
  137. }
  138. }
  139. func BenchmarkWrite_64(b *testing.B) { benchmarkWrite(b, 64) }
  140. func BenchmarkWrite_1K(b *testing.B) { benchmarkWrite(b, 1024) }