sha3_s390x.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // Copyright 2017 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. // +build !gccgo,!appengine
  5. package sha3
  6. // This file contains code for using the 'compute intermediate
  7. // message digest' (KIMD) and 'compute last message digest' (KLMD)
  8. // instructions to compute SHA-3 and SHAKE hashes on IBM Z.
  9. import (
  10. "hash"
  11. "golang.org/x/sys/cpu"
  12. )
  13. // codes represent 7-bit KIMD/KLMD function codes as defined in
  14. // the Principles of Operation.
  15. type code uint64
  16. const (
  17. // function codes for KIMD/KLMD
  18. sha3_224 code = 32
  19. sha3_256 = 33
  20. sha3_384 = 34
  21. sha3_512 = 35
  22. shake_128 = 36
  23. shake_256 = 37
  24. nopad = 0x100
  25. )
  26. // kimd is a wrapper for the 'compute intermediate message digest' instruction.
  27. // src must be a multiple of the rate for the given function code.
  28. //go:noescape
  29. func kimd(function code, chain *[200]byte, src []byte)
  30. // klmd is a wrapper for the 'compute last message digest' instruction.
  31. // src padding is handled by the instruction.
  32. //go:noescape
  33. func klmd(function code, chain *[200]byte, dst, src []byte)
  34. type asmState struct {
  35. a [200]byte // 1600 bit state
  36. buf []byte // care must be taken to ensure cap(buf) is a multiple of rate
  37. rate int // equivalent to block size
  38. storage [3072]byte // underlying storage for buf
  39. outputLen int // output length if fixed, 0 if not
  40. function code // KIMD/KLMD function code
  41. state spongeDirection // whether the sponge is absorbing or squeezing
  42. }
  43. func newAsmState(function code) *asmState {
  44. var s asmState
  45. s.function = function
  46. switch function {
  47. case sha3_224:
  48. s.rate = 144
  49. s.outputLen = 28
  50. case sha3_256:
  51. s.rate = 136
  52. s.outputLen = 32
  53. case sha3_384:
  54. s.rate = 104
  55. s.outputLen = 48
  56. case sha3_512:
  57. s.rate = 72
  58. s.outputLen = 64
  59. case shake_128:
  60. s.rate = 168
  61. case shake_256:
  62. s.rate = 136
  63. default:
  64. panic("sha3: unrecognized function code")
  65. }
  66. // limit s.buf size to a multiple of s.rate
  67. s.resetBuf()
  68. return &s
  69. }
  70. func (s *asmState) clone() *asmState {
  71. c := *s
  72. c.buf = c.storage[:len(s.buf):cap(s.buf)]
  73. return &c
  74. }
  75. // copyIntoBuf copies b into buf. It will panic if there is not enough space to
  76. // store all of b.
  77. func (s *asmState) copyIntoBuf(b []byte) {
  78. bufLen := len(s.buf)
  79. s.buf = s.buf[:len(s.buf)+len(b)]
  80. copy(s.buf[bufLen:], b)
  81. }
  82. // resetBuf points buf at storage, sets the length to 0 and sets cap to be a
  83. // multiple of the rate.
  84. func (s *asmState) resetBuf() {
  85. max := (cap(s.storage) / s.rate) * s.rate
  86. s.buf = s.storage[:0:max]
  87. }
  88. // Write (via the embedded io.Writer interface) adds more data to the running hash.
  89. // It never returns an error.
  90. func (s *asmState) Write(b []byte) (int, error) {
  91. if s.state != spongeAbsorbing {
  92. panic("sha3: write to sponge after read")
  93. }
  94. length := len(b)
  95. for len(b) > 0 {
  96. if len(s.buf) == 0 && len(b) >= cap(s.buf) {
  97. // Hash the data directly and push any remaining bytes
  98. // into the buffer.
  99. remainder := len(s.buf) % s.rate
  100. kimd(s.function, &s.a, b[:len(b)-remainder])
  101. if remainder != 0 {
  102. s.copyIntoBuf(b[len(b)-remainder:])
  103. }
  104. return length, nil
  105. }
  106. if len(s.buf) == cap(s.buf) {
  107. // flush the buffer
  108. kimd(s.function, &s.a, s.buf)
  109. s.buf = s.buf[:0]
  110. }
  111. // copy as much as we can into the buffer
  112. n := len(b)
  113. if len(b) > cap(s.buf)-len(s.buf) {
  114. n = cap(s.buf) - len(s.buf)
  115. }
  116. s.copyIntoBuf(b[:n])
  117. b = b[n:]
  118. }
  119. return length, nil
  120. }
  121. // Read squeezes an arbitrary number of bytes from the sponge.
  122. func (s *asmState) Read(out []byte) (n int, err error) {
  123. n = len(out)
  124. // need to pad if we were absorbing
  125. if s.state == spongeAbsorbing {
  126. s.state = spongeSqueezing
  127. // write hash directly into out if possible
  128. if len(out)%s.rate == 0 {
  129. klmd(s.function, &s.a, out, s.buf) // len(out) may be 0
  130. s.buf = s.buf[:0]
  131. return
  132. }
  133. // write hash into buffer
  134. max := cap(s.buf)
  135. if max > len(out) {
  136. max = (len(out)/s.rate)*s.rate + s.rate
  137. }
  138. klmd(s.function, &s.a, s.buf[:max], s.buf)
  139. s.buf = s.buf[:max]
  140. }
  141. for len(out) > 0 {
  142. // flush the buffer
  143. if len(s.buf) != 0 {
  144. c := copy(out, s.buf)
  145. out = out[c:]
  146. s.buf = s.buf[c:]
  147. continue
  148. }
  149. // write hash directly into out if possible
  150. if len(out)%s.rate == 0 {
  151. klmd(s.function|nopad, &s.a, out, nil)
  152. return
  153. }
  154. // write hash into buffer
  155. s.resetBuf()
  156. if cap(s.buf) > len(out) {
  157. s.buf = s.buf[:(len(out)/s.rate)*s.rate+s.rate]
  158. }
  159. klmd(s.function|nopad, &s.a, s.buf, nil)
  160. }
  161. return
  162. }
  163. // Sum appends the current hash to b and returns the resulting slice.
  164. // It does not change the underlying hash state.
  165. func (s *asmState) Sum(b []byte) []byte {
  166. if s.outputLen == 0 {
  167. panic("sha3: cannot call Sum on SHAKE functions")
  168. }
  169. // Copy the state to preserve the original.
  170. a := s.a
  171. // Hash the buffer. Note that we don't clear it because we
  172. // aren't updating the state.
  173. klmd(s.function, &a, nil, s.buf)
  174. return append(b, a[:s.outputLen]...)
  175. }
  176. // Reset resets the Hash to its initial state.
  177. func (s *asmState) Reset() {
  178. for i := range s.a {
  179. s.a[i] = 0
  180. }
  181. s.resetBuf()
  182. s.state = spongeAbsorbing
  183. }
  184. // Size returns the number of bytes Sum will return.
  185. func (s *asmState) Size() int {
  186. return s.outputLen
  187. }
  188. // BlockSize returns the hash's underlying block size.
  189. // The Write method must be able to accept any amount
  190. // of data, but it may operate more efficiently if all writes
  191. // are a multiple of the block size.
  192. func (s *asmState) BlockSize() int {
  193. return s.rate
  194. }
  195. // Clone returns a copy of the ShakeHash in its current state.
  196. func (s *asmState) Clone() ShakeHash {
  197. return s.clone()
  198. }
  199. // new224Asm returns an assembly implementation of SHA3-224 if available,
  200. // otherwise it returns nil.
  201. func new224Asm() hash.Hash {
  202. if cpu.S390X.HasSHA3 {
  203. return newAsmState(sha3_224)
  204. }
  205. return nil
  206. }
  207. // new256Asm returns an assembly implementation of SHA3-256 if available,
  208. // otherwise it returns nil.
  209. func new256Asm() hash.Hash {
  210. if cpu.S390X.HasSHA3 {
  211. return newAsmState(sha3_256)
  212. }
  213. return nil
  214. }
  215. // new384Asm returns an assembly implementation of SHA3-384 if available,
  216. // otherwise it returns nil.
  217. func new384Asm() hash.Hash {
  218. if cpu.S390X.HasSHA3 {
  219. return newAsmState(sha3_384)
  220. }
  221. return nil
  222. }
  223. // new512Asm returns an assembly implementation of SHA3-512 if available,
  224. // otherwise it returns nil.
  225. func new512Asm() hash.Hash {
  226. if cpu.S390X.HasSHA3 {
  227. return newAsmState(sha3_512)
  228. }
  229. return nil
  230. }
  231. // newShake128Asm returns an assembly implementation of SHAKE-128 if available,
  232. // otherwise it returns nil.
  233. func newShake128Asm() ShakeHash {
  234. if cpu.S390X.HasSHA3 {
  235. return newAsmState(shake_128)
  236. }
  237. return nil
  238. }
  239. // newShake256Asm returns an assembly implementation of SHAKE-256 if available,
  240. // otherwise it returns nil.
  241. func newShake256Asm() ShakeHash {
  242. if cpu.S390X.HasSHA3 {
  243. return newAsmState(shake_256)
  244. }
  245. return nil
  246. }