sha512.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // Copyright 2009 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 sha512 implements the SHA-384, SHA-512, SHA-512/224, and SHA-512/256
  5. // hash algorithms as defined in FIPS 180-4.
  6. package sha512
  7. import (
  8. "crypto"
  9. "hash"
  10. )
  11. func init() {
  12. crypto.RegisterHash(crypto.SHA384, New384)
  13. crypto.RegisterHash(crypto.SHA512, New)
  14. crypto.RegisterHash(crypto.SHA512_224, New512_224)
  15. crypto.RegisterHash(crypto.SHA512_256, New512_256)
  16. }
  17. const (
  18. // Size is the size, in bytes, of a SHA-512 checksum.
  19. Size = 64
  20. // Size224 is the size, in bytes, of a SHA-512/224 checksum.
  21. Size224 = 28
  22. // Size256 is the size, in bytes, of a SHA-512/256 checksum.
  23. Size256 = 32
  24. // Size384 is the size, in bytes, of a SHA-384 checksum.
  25. Size384 = 48
  26. // BlockSize is the block size, in bytes, of the SHA-512/224,
  27. // SHA-512/256, SHA-384 and SHA-512 hash functions.
  28. BlockSize = 128
  29. )
  30. const (
  31. chunk = 128
  32. init0 = 0x6a09e667f3bcc908
  33. init1 = 0xbb67ae8584caa73b
  34. init2 = 0x3c6ef372fe94f82b
  35. init3 = 0xa54ff53a5f1d36f1
  36. init4 = 0x510e527fade682d1
  37. init5 = 0x9b05688c2b3e6c1f
  38. init6 = 0x1f83d9abfb41bd6b
  39. init7 = 0x5be0cd19137e2179
  40. init0_224 = 0x8c3d37c819544da2
  41. init1_224 = 0x73e1996689dcd4d6
  42. init2_224 = 0x1dfab7ae32ff9c82
  43. init3_224 = 0x679dd514582f9fcf
  44. init4_224 = 0x0f6d2b697bd44da8
  45. init5_224 = 0x77e36f7304c48942
  46. init6_224 = 0x3f9d85a86a1d36c8
  47. init7_224 = 0x1112e6ad91d692a1
  48. init0_256 = 0x22312194fc2bf72c
  49. init1_256 = 0x9f555fa3c84c64c2
  50. init2_256 = 0x2393b86b6f53b151
  51. init3_256 = 0x963877195940eabd
  52. init4_256 = 0x96283ee2a88effe3
  53. init5_256 = 0xbe5e1e2553863992
  54. init6_256 = 0x2b0199fc2c85b8aa
  55. init7_256 = 0x0eb72ddc81c52ca2
  56. init0_384 = 0xcbbb9d5dc1059ed8
  57. init1_384 = 0x629a292a367cd507
  58. init2_384 = 0x9159015a3070dd17
  59. init3_384 = 0x152fecd8f70e5939
  60. init4_384 = 0x67332667ffc00b31
  61. init5_384 = 0x8eb44a8768581511
  62. init6_384 = 0xdb0c2e0d64f98fa7
  63. init7_384 = 0x47b5481dbefa4fa4
  64. )
  65. // digest represents the partial evaluation of a checksum.
  66. type digest struct {
  67. h [8]uint64
  68. x [chunk]byte
  69. nx int
  70. len uint64
  71. function crypto.Hash
  72. }
  73. func (d *digest) Reset() {
  74. switch d.function {
  75. case crypto.SHA384:
  76. d.h[0] = init0_384
  77. d.h[1] = init1_384
  78. d.h[2] = init2_384
  79. d.h[3] = init3_384
  80. d.h[4] = init4_384
  81. d.h[5] = init5_384
  82. d.h[6] = init6_384
  83. d.h[7] = init7_384
  84. case crypto.SHA512_224:
  85. d.h[0] = init0_224
  86. d.h[1] = init1_224
  87. d.h[2] = init2_224
  88. d.h[3] = init3_224
  89. d.h[4] = init4_224
  90. d.h[5] = init5_224
  91. d.h[6] = init6_224
  92. d.h[7] = init7_224
  93. case crypto.SHA512_256:
  94. d.h[0] = init0_256
  95. d.h[1] = init1_256
  96. d.h[2] = init2_256
  97. d.h[3] = init3_256
  98. d.h[4] = init4_256
  99. d.h[5] = init5_256
  100. d.h[6] = init6_256
  101. d.h[7] = init7_256
  102. default:
  103. d.h[0] = init0
  104. d.h[1] = init1
  105. d.h[2] = init2
  106. d.h[3] = init3
  107. d.h[4] = init4
  108. d.h[5] = init5
  109. d.h[6] = init6
  110. d.h[7] = init7
  111. }
  112. d.nx = 0
  113. d.len = 0
  114. }
  115. // New returns a new hash.Hash computing the SHA-512 checksum.
  116. func New() hash.Hash {
  117. d := &digest{function: crypto.SHA512}
  118. d.Reset()
  119. return d
  120. }
  121. // New512_224 returns a new hash.Hash computing the SHA-512/224 checksum.
  122. func New512_224() hash.Hash {
  123. d := &digest{function: crypto.SHA512_224}
  124. d.Reset()
  125. return d
  126. }
  127. // New512_256 returns a new hash.Hash computing the SHA-512/256 checksum.
  128. func New512_256() hash.Hash {
  129. d := &digest{function: crypto.SHA512_256}
  130. d.Reset()
  131. return d
  132. }
  133. // New384 returns a new hash.Hash computing the SHA-384 checksum.
  134. func New384() hash.Hash {
  135. d := &digest{function: crypto.SHA384}
  136. d.Reset()
  137. return d
  138. }
  139. func (d *digest) Size() int {
  140. switch d.function {
  141. case crypto.SHA512_224:
  142. return Size224
  143. case crypto.SHA512_256:
  144. return Size256
  145. case crypto.SHA384:
  146. return Size384
  147. default:
  148. return Size
  149. }
  150. }
  151. func (d *digest) BlockSize() int { return BlockSize }
  152. func (d *digest) Write(p []byte) (nn int, err error) {
  153. nn = len(p)
  154. d.len += uint64(nn)
  155. if d.nx > 0 {
  156. n := copy(d.x[d.nx:], p)
  157. d.nx += n
  158. if d.nx == chunk {
  159. block(d, d.x[:])
  160. d.nx = 0
  161. }
  162. p = p[n:]
  163. }
  164. if len(p) >= chunk {
  165. n := len(p) &^ (chunk - 1)
  166. block(d, p[:n])
  167. p = p[n:]
  168. }
  169. if len(p) > 0 {
  170. d.nx = copy(d.x[:], p)
  171. }
  172. return
  173. }
  174. func (d0 *digest) Sum(in []byte) []byte {
  175. // Make a copy of d0 so that caller can keep writing and summing.
  176. d := new(digest)
  177. *d = *d0
  178. hash := d.checkSum()
  179. switch d.function {
  180. case crypto.SHA384:
  181. return append(in, hash[:Size384]...)
  182. case crypto.SHA512_224:
  183. return append(in, hash[:Size224]...)
  184. case crypto.SHA512_256:
  185. return append(in, hash[:Size256]...)
  186. default:
  187. return append(in, hash[:]...)
  188. }
  189. }
  190. func (d *digest) checkSum() [Size]byte {
  191. // Padding. Add a 1 bit and 0 bits until 112 bytes mod 128.
  192. len := d.len
  193. var tmp [128]byte
  194. tmp[0] = 0x80
  195. if len%128 < 112 {
  196. d.Write(tmp[0 : 112-len%128])
  197. } else {
  198. d.Write(tmp[0 : 128+112-len%128])
  199. }
  200. // Length in bits.
  201. len <<= 3
  202. for i := uint(0); i < 16; i++ {
  203. tmp[i] = byte(len >> (120 - 8*i))
  204. }
  205. d.Write(tmp[0:16])
  206. if d.nx != 0 {
  207. panic("d.nx != 0")
  208. }
  209. h := d.h[:]
  210. if d.function == crypto.SHA384 {
  211. h = d.h[:6]
  212. }
  213. var digest [Size]byte
  214. for i, s := range h {
  215. digest[i*8] = byte(s >> 56)
  216. digest[i*8+1] = byte(s >> 48)
  217. digest[i*8+2] = byte(s >> 40)
  218. digest[i*8+3] = byte(s >> 32)
  219. digest[i*8+4] = byte(s >> 24)
  220. digest[i*8+5] = byte(s >> 16)
  221. digest[i*8+6] = byte(s >> 8)
  222. digest[i*8+7] = byte(s)
  223. }
  224. return digest
  225. }
  226. // Sum512 returns the SHA512 checksum of the data.
  227. func Sum512(data []byte) [Size]byte {
  228. d := digest{function: crypto.SHA512}
  229. d.Reset()
  230. d.Write(data)
  231. return d.checkSum()
  232. }
  233. // Sum384 returns the SHA384 checksum of the data.
  234. func Sum384(data []byte) (sum384 [Size384]byte) {
  235. d := digest{function: crypto.SHA384}
  236. d.Reset()
  237. d.Write(data)
  238. sum := d.checkSum()
  239. copy(sum384[:], sum[:Size384])
  240. return
  241. }
  242. // Sum512_224 returns the Sum512/224 checksum of the data.
  243. func Sum512_224(data []byte) (sum224 [Size224]byte) {
  244. d := digest{function: crypto.SHA512_224}
  245. d.Reset()
  246. d.Write(data)
  247. sum := d.checkSum()
  248. copy(sum224[:], sum[:Size224])
  249. return
  250. }
  251. // Sum512_256 returns the Sum512/256 checksum of the data.
  252. func Sum512_256(data []byte) (sum256 [Size256]byte) {
  253. d := digest{function: crypto.SHA512_256}
  254. d.Reset()
  255. d.Write(data)
  256. sum := d.checkSum()
  257. copy(sum256[:], sum[:Size256])
  258. return
  259. }