Skip to content

Commit 67b1361

Browse files
mauri870gopherbot
authored andcommitted
sha3: reenable s390x assembly
Fixes golang/go#64897 Change-Id: I0c8c52d73a7d2df0f44fee36d407a87213f59bff Reviewed-on: https://go-review.googlesource.com/c/crypto/+/554435 TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Cherry Mui <[email protected]> Reviewed-by: Filippo Valsorda <[email protected]> Reviewed-by: Roland Shoemaker <[email protected]> Auto-Submit: Filippo Valsorda <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 477a5b4 commit 67b1361

7 files changed

+103
-33
lines changed

sha3/allocations_test.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package sha3_test
88

99
import (
10+
"runtime"
1011
"testing"
1112

1213
"golang.org/x/crypto/sha3"
@@ -15,6 +16,13 @@ import (
1516
var sink byte
1617

1718
func TestAllocations(t *testing.T) {
19+
want := 0.0
20+
21+
if runtime.GOARCH == "s390x" {
22+
// On s390x the returned hash.Hash is conditional so it escapes.
23+
want = 3.0
24+
}
25+
1826
t.Run("New", func(t *testing.T) {
1927
if allocs := testing.AllocsPerRun(10, func() {
2028
h := sha3.New256()
@@ -23,7 +31,7 @@ func TestAllocations(t *testing.T) {
2331
out := make([]byte, 0, 32)
2432
out = h.Sum(out)
2533
sink ^= out[0]
26-
}); allocs > 0 {
34+
}); allocs > want {
2735
t.Errorf("expected zero allocations, got %0.1f", allocs)
2836
}
2937
})
@@ -37,7 +45,7 @@ func TestAllocations(t *testing.T) {
3745
sink ^= out[0]
3846
h.Read(out)
3947
sink ^= out[0]
40-
}); allocs > 0 {
48+
}); allocs > want {
4149
t.Errorf("expected zero allocations, got %0.1f", allocs)
4250
}
4351
})
@@ -46,7 +54,7 @@ func TestAllocations(t *testing.T) {
4654
b := []byte("ABC")
4755
out := sha3.Sum256(b)
4856
sink ^= out[0]
49-
}); allocs > 0 {
57+
}); allocs > want {
5058
t.Errorf("expected zero allocations, got %0.1f", allocs)
5159
}
5260
})

sha3/hashes.go

+19-3
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,43 @@ import (
1616
// Its generic security strength is 224 bits against preimage attacks,
1717
// and 112 bits against collision attacks.
1818
func New224() hash.Hash {
19-
return &state{rate: 144, outputLen: 28, dsbyte: 0x06}
19+
return new224()
2020
}
2121

2222
// New256 creates a new SHA3-256 hash.
2323
// Its generic security strength is 256 bits against preimage attacks,
2424
// and 128 bits against collision attacks.
2525
func New256() hash.Hash {
26-
return &state{rate: 136, outputLen: 32, dsbyte: 0x06}
26+
return new256()
2727
}
2828

2929
// New384 creates a new SHA3-384 hash.
3030
// Its generic security strength is 384 bits against preimage attacks,
3131
// and 192 bits against collision attacks.
3232
func New384() hash.Hash {
33-
return &state{rate: 104, outputLen: 48, dsbyte: 0x06}
33+
return new384()
3434
}
3535

3636
// New512 creates a new SHA3-512 hash.
3737
// Its generic security strength is 512 bits against preimage attacks,
3838
// and 256 bits against collision attacks.
3939
func New512() hash.Hash {
40+
return new512()
41+
}
42+
43+
func new224Generic() *state {
44+
return &state{rate: 144, outputLen: 28, dsbyte: 0x06}
45+
}
46+
47+
func new256Generic() *state {
48+
return &state{rate: 136, outputLen: 32, dsbyte: 0x06}
49+
}
50+
51+
func new384Generic() *state {
52+
return &state{rate: 104, outputLen: 48, dsbyte: 0x06}
53+
}
54+
55+
func new512Generic() *state {
4056
return &state{rate: 72, outputLen: 64, dsbyte: 0x06}
4157
}
4258

sha3/hashes_noasm.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2023 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+
5+
//go:build !gc || purego || !s390x
6+
7+
package sha3
8+
9+
func new224() *state {
10+
return new224Generic()
11+
}
12+
13+
func new256() *state {
14+
return new256Generic()
15+
}
16+
17+
func new384() *state {
18+
return new384Generic()
19+
}
20+
21+
func new512() *state {
22+
return new512Generic()
23+
}

sha3/sha3_s390x.go

+25-25
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build gc && !purego && ignore
5+
//go:build gc && !purego
66

77
package sha3
88

@@ -248,56 +248,56 @@ func (s *asmState) Clone() ShakeHash {
248248
return s.clone()
249249
}
250250

251-
// new224Asm returns an assembly implementation of SHA3-224 if available,
252-
// otherwise it returns nil.
253-
func new224Asm() hash.Hash {
251+
// new224 returns an assembly implementation of SHA3-224 if available,
252+
// otherwise it returns a generic implementation.
253+
func new224() hash.Hash {
254254
if cpu.S390X.HasSHA3 {
255255
return newAsmState(sha3_224)
256256
}
257-
return nil
257+
return new224Generic()
258258
}
259259

260-
// new256Asm returns an assembly implementation of SHA3-256 if available,
261-
// otherwise it returns nil.
262-
func new256Asm() hash.Hash {
260+
// new256 returns an assembly implementation of SHA3-256 if available,
261+
// otherwise it returns a generic implementation.
262+
func new256() hash.Hash {
263263
if cpu.S390X.HasSHA3 {
264264
return newAsmState(sha3_256)
265265
}
266-
return nil
266+
return new256Generic()
267267
}
268268

269-
// new384Asm returns an assembly implementation of SHA3-384 if available,
270-
// otherwise it returns nil.
271-
func new384Asm() hash.Hash {
269+
// new384 returns an assembly implementation of SHA3-384 if available,
270+
// otherwise it returns a generic implementation.
271+
func new384() hash.Hash {
272272
if cpu.S390X.HasSHA3 {
273273
return newAsmState(sha3_384)
274274
}
275-
return nil
275+
return new384Generic()
276276
}
277277

278-
// new512Asm returns an assembly implementation of SHA3-512 if available,
279-
// otherwise it returns nil.
280-
func new512Asm() hash.Hash {
278+
// new512 returns an assembly implementation of SHA3-512 if available,
279+
// otherwise it returns a generic implementation.
280+
func new512() hash.Hash {
281281
if cpu.S390X.HasSHA3 {
282282
return newAsmState(sha3_512)
283283
}
284-
return nil
284+
return new512Generic()
285285
}
286286

287-
// newShake128Asm returns an assembly implementation of SHAKE-128 if available,
288-
// otherwise it returns nil.
289-
func newShake128Asm() ShakeHash {
287+
// newShake128 returns an assembly implementation of SHAKE-128 if available,
288+
// otherwise it returns a generic implementation.
289+
func newShake128() ShakeHash {
290290
if cpu.S390X.HasSHA3 {
291291
return newAsmState(shake_128)
292292
}
293-
return nil
293+
return newShake128Generic()
294294
}
295295

296-
// newShake256Asm returns an assembly implementation of SHAKE-256 if available,
297-
// otherwise it returns nil.
298-
func newShake256Asm() ShakeHash {
296+
// newShake256 returns an assembly implementation of SHAKE-256 if available,
297+
// otherwise it returns a generic implementation.
298+
func newShake256() ShakeHash {
299299
if cpu.S390X.HasSHA3 {
300300
return newAsmState(shake_256)
301301
}
302-
return nil
302+
return newShake256Generic()
303303
}

sha3/sha3_s390x.s

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build gc && !purego && ignore
5+
//go:build gc && !purego
66

77
#include "textflag.h"
88

sha3/shake.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,21 @@ func (c *state) Clone() ShakeHash {
115115
// Its generic security strength is 128 bits against all attacks if at
116116
// least 32 bytes of its output are used.
117117
func NewShake128() ShakeHash {
118-
return &state{rate: rate128, outputLen: 32, dsbyte: dsbyteShake}
118+
return newShake128()
119119
}
120120

121121
// NewShake256 creates a new SHAKE256 variable-output-length ShakeHash.
122122
// Its generic security strength is 256 bits against all attacks if
123123
// at least 64 bytes of its output are used.
124124
func NewShake256() ShakeHash {
125+
return newShake256()
126+
}
127+
128+
func newShake128Generic() *state {
129+
return &state{rate: rate128, outputLen: 32, dsbyte: dsbyteShake}
130+
}
131+
132+
func newShake256Generic() *state {
125133
return &state{rate: rate256, outputLen: 64, dsbyte: dsbyteShake}
126134
}
127135

sha3/shake_noasm.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2023 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+
5+
//go:build !gc || purego || !s390x
6+
7+
package sha3
8+
9+
func newShake128() *state {
10+
return newShake128Generic()
11+
}
12+
13+
func newShake256() *state {
14+
return newShake256Generic()
15+
}

0 commit comments

Comments
 (0)