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.
 
 
 

138 lines
3.2 KiB

  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package s2k
  5. import (
  6. "bytes"
  7. "crypto"
  8. _ "crypto/md5"
  9. "crypto/rand"
  10. "crypto/sha1"
  11. _ "crypto/sha256"
  12. _ "crypto/sha512"
  13. "encoding/hex"
  14. "testing"
  15. _ "golang.org/x/crypto/ripemd160"
  16. )
  17. var saltedTests = []struct {
  18. in, out string
  19. }{
  20. {"hello", "10295ac1"},
  21. {"world", "ac587a5e"},
  22. {"foo", "4dda8077"},
  23. {"bar", "bd8aac6b9ea9cae04eae6a91c6133b58b5d9a61c14f355516ed9370456"},
  24. {"x", "f1d3f289"},
  25. {"xxxxxxxxxxxxxxxxxxxxxxx", "e00d7b45"},
  26. }
  27. func TestSalted(t *testing.T) {
  28. h := sha1.New()
  29. salt := [4]byte{1, 2, 3, 4}
  30. for i, test := range saltedTests {
  31. expected, _ := hex.DecodeString(test.out)
  32. out := make([]byte, len(expected))
  33. Salted(out, h, []byte(test.in), salt[:])
  34. if !bytes.Equal(expected, out) {
  35. t.Errorf("#%d, got: %x want: %x", i, out, expected)
  36. }
  37. }
  38. }
  39. var iteratedTests = []struct {
  40. in, out string
  41. }{
  42. {"hello", "83126105"},
  43. {"world", "6fa317f9"},
  44. {"foo", "8fbc35b9"},
  45. {"bar", "2af5a99b54f093789fd657f19bd245af7604d0f6ae06f66602a46a08ae"},
  46. {"x", "5a684dfe"},
  47. {"xxxxxxxxxxxxxxxxxxxxxxx", "18955174"},
  48. }
  49. func TestIterated(t *testing.T) {
  50. h := sha1.New()
  51. salt := [4]byte{4, 3, 2, 1}
  52. for i, test := range iteratedTests {
  53. expected, _ := hex.DecodeString(test.out)
  54. out := make([]byte, len(expected))
  55. Iterated(out, h, []byte(test.in), salt[:], 31)
  56. if !bytes.Equal(expected, out) {
  57. t.Errorf("#%d, got: %x want: %x", i, out, expected)
  58. }
  59. }
  60. }
  61. var parseTests = []struct {
  62. spec, in, out string
  63. }{
  64. /* Simple with SHA1 */
  65. {"0002", "hello", "aaf4c61d"},
  66. /* Salted with SHA1 */
  67. {"01020102030405060708", "hello", "f4f7d67e"},
  68. /* Iterated with SHA1 */
  69. {"03020102030405060708f1", "hello", "f2a57b7c"},
  70. }
  71. func TestParse(t *testing.T) {
  72. for i, test := range parseTests {
  73. spec, _ := hex.DecodeString(test.spec)
  74. buf := bytes.NewBuffer(spec)
  75. f, err := Parse(buf)
  76. if err != nil {
  77. t.Errorf("%d: Parse returned error: %s", i, err)
  78. continue
  79. }
  80. expected, _ := hex.DecodeString(test.out)
  81. out := make([]byte, len(expected))
  82. f(out, []byte(test.in))
  83. if !bytes.Equal(out, expected) {
  84. t.Errorf("%d: output got: %x want: %x", i, out, expected)
  85. }
  86. if testing.Short() {
  87. break
  88. }
  89. }
  90. }
  91. func TestSerialize(t *testing.T) {
  92. hashes := []crypto.Hash{crypto.MD5, crypto.SHA1, crypto.RIPEMD160,
  93. crypto.SHA256, crypto.SHA384, crypto.SHA512, crypto.SHA224}
  94. testCounts := []int{-1, 0, 1024, 65536, 4063232, 65011712}
  95. for _, h := range hashes {
  96. for _, c := range testCounts {
  97. testSerializeConfig(t, &Config{Hash: h, S2KCount: c})
  98. }
  99. }
  100. }
  101. func testSerializeConfig(t *testing.T, c *Config) {
  102. t.Logf("Running testSerializeConfig() with config: %+v", c)
  103. buf := bytes.NewBuffer(nil)
  104. key := make([]byte, 16)
  105. passphrase := []byte("testing")
  106. err := Serialize(buf, key, rand.Reader, passphrase, c)
  107. if err != nil {
  108. t.Errorf("failed to serialize: %s", err)
  109. return
  110. }
  111. f, err := Parse(buf)
  112. if err != nil {
  113. t.Errorf("failed to reparse: %s", err)
  114. return
  115. }
  116. key2 := make([]byte, len(key))
  117. f(key2, passphrase)
  118. if !bytes.Equal(key2, key) {
  119. t.Errorf("keys don't match: %x (serialied) vs %x (parsed)", key, key2)
  120. }
  121. }