hashes.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2014 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 sha3
  5. // This file provides functions for creating instances of the SHA-3
  6. // and SHAKE hash functions, as well as utility functions for hashing
  7. // bytes.
  8. import (
  9. "crypto"
  10. "hash"
  11. )
  12. // New224 creates a new SHA3-224 hash.
  13. // Its generic security strength is 224 bits against preimage attacks,
  14. // and 112 bits against collision attacks.
  15. func New224() hash.Hash {
  16. return new224()
  17. }
  18. // New256 creates a new SHA3-256 hash.
  19. // Its generic security strength is 256 bits against preimage attacks,
  20. // and 128 bits against collision attacks.
  21. func New256() hash.Hash {
  22. return new256()
  23. }
  24. // New384 creates a new SHA3-384 hash.
  25. // Its generic security strength is 384 bits against preimage attacks,
  26. // and 192 bits against collision attacks.
  27. func New384() hash.Hash {
  28. return new384()
  29. }
  30. // New512 creates a new SHA3-512 hash.
  31. // Its generic security strength is 512 bits against preimage attacks,
  32. // and 256 bits against collision attacks.
  33. func New512() hash.Hash {
  34. return new512()
  35. }
  36. func init() {
  37. crypto.RegisterHash(crypto.SHA3_224, New224)
  38. crypto.RegisterHash(crypto.SHA3_256, New256)
  39. crypto.RegisterHash(crypto.SHA3_384, New384)
  40. crypto.RegisterHash(crypto.SHA3_512, New512)
  41. }
  42. const (
  43. dsbyteSHA3 = 0b00000110
  44. dsbyteKeccak = 0b00000001
  45. dsbyteShake = 0b00011111
  46. dsbyteCShake = 0b00000100
  47. // rateK[c] is the rate in bytes for Keccak[c] where c is the capacity in
  48. // bits. Given the sponge size is 1600 bits, the rate is 1600 - c bits.
  49. rateK256 = (1600 - 256) / 8
  50. rateK448 = (1600 - 448) / 8
  51. rateK512 = (1600 - 512) / 8
  52. rateK768 = (1600 - 768) / 8
  53. rateK1024 = (1600 - 1024) / 8
  54. )
  55. func new224Generic() *state {
  56. return &state{rate: rateK448, outputLen: 28, dsbyte: dsbyteSHA3}
  57. }
  58. func new256Generic() *state {
  59. return &state{rate: rateK512, outputLen: 32, dsbyte: dsbyteSHA3}
  60. }
  61. func new384Generic() *state {
  62. return &state{rate: rateK768, outputLen: 48, dsbyte: dsbyteSHA3}
  63. }
  64. func new512Generic() *state {
  65. return &state{rate: rateK1024, outputLen: 64, dsbyte: dsbyteSHA3}
  66. }
  67. // NewLegacyKeccak256 creates a new Keccak-256 hash.
  68. //
  69. // Only use this function if you require compatibility with an existing cryptosystem
  70. // that uses non-standard padding. All other users should use New256 instead.
  71. func NewLegacyKeccak256() hash.Hash {
  72. return &state{rate: rateK512, outputLen: 32, dsbyte: dsbyteKeccak}
  73. }
  74. // NewLegacyKeccak512 creates a new Keccak-512 hash.
  75. //
  76. // Only use this function if you require compatibility with an existing cryptosystem
  77. // that uses non-standard padding. All other users should use New512 instead.
  78. func NewLegacyKeccak512() hash.Hash {
  79. return &state{rate: rateK1024, outputLen: 64, dsbyte: dsbyteKeccak}
  80. }
  81. // Sum224 returns the SHA3-224 digest of the data.
  82. func Sum224(data []byte) (digest [28]byte) {
  83. h := New224()
  84. h.Write(data)
  85. h.Sum(digest[:0])
  86. return
  87. }
  88. // Sum256 returns the SHA3-256 digest of the data.
  89. func Sum256(data []byte) (digest [32]byte) {
  90. h := New256()
  91. h.Write(data)
  92. h.Sum(digest[:0])
  93. return
  94. }
  95. // Sum384 returns the SHA3-384 digest of the data.
  96. func Sum384(data []byte) (digest [48]byte) {
  97. h := New384()
  98. h.Write(data)
  99. h.Sum(digest[:0])
  100. return
  101. }
  102. // Sum512 returns the SHA3-512 digest of the data.
  103. func Sum512(data []byte) (digest [64]byte) {
  104. h := New512()
  105. h.Write(data)
  106. h.Sum(digest[:0])
  107. return
  108. }