Skip to content

Commit

Permalink
sha3: reenable s390x assembly
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
mauri870 authored and gopherbot committed May 7, 2024
1 parent 477a5b4 commit 67b1361
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 33 deletions.
14 changes: 11 additions & 3 deletions sha3/allocations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package sha3_test

import (
"runtime"
"testing"

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

func TestAllocations(t *testing.T) {
want := 0.0

if runtime.GOARCH == "s390x" {
// On s390x the returned hash.Hash is conditional so it escapes.
want = 3.0
}

t.Run("New", func(t *testing.T) {
if allocs := testing.AllocsPerRun(10, func() {
h := sha3.New256()
Expand All @@ -23,7 +31,7 @@ func TestAllocations(t *testing.T) {
out := make([]byte, 0, 32)
out = h.Sum(out)
sink ^= out[0]
}); allocs > 0 {
}); allocs > want {
t.Errorf("expected zero allocations, got %0.1f", allocs)
}
})
Expand All @@ -37,7 +45,7 @@ func TestAllocations(t *testing.T) {
sink ^= out[0]
h.Read(out)
sink ^= out[0]
}); allocs > 0 {
}); allocs > want {
t.Errorf("expected zero allocations, got %0.1f", allocs)
}
})
Expand All @@ -46,7 +54,7 @@ func TestAllocations(t *testing.T) {
b := []byte("ABC")
out := sha3.Sum256(b)
sink ^= out[0]
}); allocs > 0 {
}); allocs > want {
t.Errorf("expected zero allocations, got %0.1f", allocs)
}
})
Expand Down
22 changes: 19 additions & 3 deletions sha3/hashes.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,43 @@ import (
// Its generic security strength is 224 bits against preimage attacks,
// and 112 bits against collision attacks.
func New224() hash.Hash {
return &state{rate: 144, outputLen: 28, dsbyte: 0x06}
return new224()
}

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

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

// New512 creates a new SHA3-512 hash.
// Its generic security strength is 512 bits against preimage attacks,
// and 256 bits against collision attacks.
func New512() hash.Hash {
return new512()
}

func new224Generic() *state {
return &state{rate: 144, outputLen: 28, dsbyte: 0x06}
}

func new256Generic() *state {
return &state{rate: 136, outputLen: 32, dsbyte: 0x06}
}

func new384Generic() *state {
return &state{rate: 104, outputLen: 48, dsbyte: 0x06}
}

func new512Generic() *state {
return &state{rate: 72, outputLen: 64, dsbyte: 0x06}
}

Expand Down
23 changes: 23 additions & 0 deletions sha3/hashes_noasm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build !gc || purego || !s390x

package sha3

func new224() *state {
return new224Generic()
}

func new256() *state {
return new256Generic()
}

func new384() *state {
return new384Generic()
}

func new512() *state {
return new512Generic()
}
50 changes: 25 additions & 25 deletions sha3/sha3_s390x.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build gc && !purego && ignore
//go:build gc && !purego

package sha3

Expand Down Expand Up @@ -248,56 +248,56 @@ func (s *asmState) Clone() ShakeHash {
return s.clone()
}

// new224Asm returns an assembly implementation of SHA3-224 if available,
// otherwise it returns nil.
func new224Asm() hash.Hash {
// new224 returns an assembly implementation of SHA3-224 if available,
// otherwise it returns a generic implementation.
func new224() hash.Hash {
if cpu.S390X.HasSHA3 {
return newAsmState(sha3_224)
}
return nil
return new224Generic()
}

// new256Asm returns an assembly implementation of SHA3-256 if available,
// otherwise it returns nil.
func new256Asm() hash.Hash {
// new256 returns an assembly implementation of SHA3-256 if available,
// otherwise it returns a generic implementation.
func new256() hash.Hash {
if cpu.S390X.HasSHA3 {
return newAsmState(sha3_256)
}
return nil
return new256Generic()
}

// new384Asm returns an assembly implementation of SHA3-384 if available,
// otherwise it returns nil.
func new384Asm() hash.Hash {
// new384 returns an assembly implementation of SHA3-384 if available,
// otherwise it returns a generic implementation.
func new384() hash.Hash {
if cpu.S390X.HasSHA3 {
return newAsmState(sha3_384)
}
return nil
return new384Generic()
}

// new512Asm returns an assembly implementation of SHA3-512 if available,
// otherwise it returns nil.
func new512Asm() hash.Hash {
// new512 returns an assembly implementation of SHA3-512 if available,
// otherwise it returns a generic implementation.
func new512() hash.Hash {
if cpu.S390X.HasSHA3 {
return newAsmState(sha3_512)
}
return nil
return new512Generic()
}

// newShake128Asm returns an assembly implementation of SHAKE-128 if available,
// otherwise it returns nil.
func newShake128Asm() ShakeHash {
// newShake128 returns an assembly implementation of SHAKE-128 if available,
// otherwise it returns a generic implementation.
func newShake128() ShakeHash {
if cpu.S390X.HasSHA3 {
return newAsmState(shake_128)
}
return nil
return newShake128Generic()
}

// newShake256Asm returns an assembly implementation of SHAKE-256 if available,
// otherwise it returns nil.
func newShake256Asm() ShakeHash {
// newShake256 returns an assembly implementation of SHAKE-256 if available,
// otherwise it returns a generic implementation.
func newShake256() ShakeHash {
if cpu.S390X.HasSHA3 {
return newAsmState(shake_256)
}
return nil
return newShake256Generic()
}
2 changes: 1 addition & 1 deletion sha3/sha3_s390x.s
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build gc && !purego && ignore
//go:build gc && !purego

#include "textflag.h"

Expand Down
10 changes: 9 additions & 1 deletion sha3/shake.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,21 @@ func (c *state) Clone() ShakeHash {
// Its generic security strength is 128 bits against all attacks if at
// least 32 bytes of its output are used.
func NewShake128() ShakeHash {
return &state{rate: rate128, outputLen: 32, dsbyte: dsbyteShake}
return newShake128()
}

// NewShake256 creates a new SHAKE256 variable-output-length ShakeHash.
// Its generic security strength is 256 bits against all attacks if
// at least 64 bytes of its output are used.
func NewShake256() ShakeHash {
return newShake256()
}

func newShake128Generic() *state {
return &state{rate: rate128, outputLen: 32, dsbyte: dsbyteShake}
}

func newShake256Generic() *state {
return &state{rate: rate256, outputLen: 64, dsbyte: dsbyteShake}
}

Expand Down
15 changes: 15 additions & 0 deletions sha3/shake_noasm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build !gc || purego || !s390x

package sha3

func newShake128() *state {
return newShake128Generic()
}

func newShake256() *state {
return newShake256Generic()
}

0 comments on commit 67b1361

Please sign in to comment.