Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

sha3_s390x.go 6.8KB

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