Skip to content

Commit e37aea1

Browse files
committed
sha3: use x/sys/cpu for s390x feature detection
Use the recently added CPU feature detection API rather than custom assembly. While we are here also make vet happy by renaming params in the asm file to chain to match the go function declaration. Change-Id: Ide0171793c9fa5ef6671b394e97a27f2c6e44a9f Reviewed-on: https://go-review.googlesource.com/c/164381 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
1 parent 31a3858 commit e37aea1

File tree

2 files changed

+12
-33
lines changed

2 files changed

+12
-33
lines changed

sha3/sha3_s390x.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ package sha3
1212

1313
import (
1414
"hash"
15+
16+
"golang.org/x/sys/cpu"
1517
)
1618

1719
// codes represent 7-bit KIMD/KLMD function codes as defined in
@@ -29,13 +31,6 @@ const (
2931
nopad = 0x100
3032
)
3133

32-
// hasMSA6 reports whether the machine supports the SHA-3 and SHAKE function
33-
// codes, as defined in message-security-assist extension 6.
34-
func hasMSA6() bool
35-
36-
// hasAsm caches the result of hasMSA6 (which might be expensive to call).
37-
var hasAsm = hasMSA6()
38-
3934
// kimd is a wrapper for the 'compute intermediate message digest' instruction.
4035
// src must be a multiple of the rate for the given function code.
4136
//go:noescape
@@ -237,7 +232,7 @@ func (s *asmState) Clone() ShakeHash {
237232
// new224Asm returns an assembly implementation of SHA3-224 if available,
238233
// otherwise it returns nil.
239234
func new224Asm() hash.Hash {
240-
if hasAsm {
235+
if cpu.S390X.HasSHA3 {
241236
return newAsmState(sha3_224)
242237
}
243238
return nil
@@ -246,7 +241,7 @@ func new224Asm() hash.Hash {
246241
// new256Asm returns an assembly implementation of SHA3-256 if available,
247242
// otherwise it returns nil.
248243
func new256Asm() hash.Hash {
249-
if hasAsm {
244+
if cpu.S390X.HasSHA3 {
250245
return newAsmState(sha3_256)
251246
}
252247
return nil
@@ -255,7 +250,7 @@ func new256Asm() hash.Hash {
255250
// new384Asm returns an assembly implementation of SHA3-384 if available,
256251
// otherwise it returns nil.
257252
func new384Asm() hash.Hash {
258-
if hasAsm {
253+
if cpu.S390X.HasSHA3 {
259254
return newAsmState(sha3_384)
260255
}
261256
return nil
@@ -264,7 +259,7 @@ func new384Asm() hash.Hash {
264259
// new512Asm returns an assembly implementation of SHA3-512 if available,
265260
// otherwise it returns nil.
266261
func new512Asm() hash.Hash {
267-
if hasAsm {
262+
if cpu.S390X.HasSHA3 {
268263
return newAsmState(sha3_512)
269264
}
270265
return nil
@@ -273,7 +268,7 @@ func new512Asm() hash.Hash {
273268
// newShake128Asm returns an assembly implementation of SHAKE-128 if available,
274269
// otherwise it returns nil.
275270
func newShake128Asm() ShakeHash {
276-
if hasAsm {
271+
if cpu.S390X.HasSHA3 {
277272
return newAsmState(shake_128)
278273
}
279274
return nil
@@ -282,7 +277,7 @@ func newShake128Asm() ShakeHash {
282277
// newShake256Asm returns an assembly implementation of SHAKE-256 if available,
283278
// otherwise it returns nil.
284279
func newShake256Asm() ShakeHash {
285-
if hasAsm {
280+
if cpu.S390X.HasSHA3 {
286281
return newAsmState(shake_256)
287282
}
288283
return nil

sha3/sha3_s390x.s

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,10 @@
66

77
#include "textflag.h"
88

9-
TEXT ·hasMSA6(SB), NOSPLIT, $16-1
10-
MOVD $0, R0 // KIMD-Query function code
11-
MOVD $tmp-16(SP), R1 // parameter block
12-
XC $16, (R1), (R1) // clear the parameter block
13-
WORD $0xB93E0002 // KIMD --, --
14-
WORD $0x91FC1004 // TM 4(R1), 0xFC (test bits [32-37])
15-
BVS yes
16-
17-
no:
18-
MOVB $0, ret+0(FP)
19-
RET
20-
21-
yes:
22-
MOVB $1, ret+0(FP)
23-
RET
24-
25-
// func kimd(function code, params *[200]byte, src []byte)
9+
// func kimd(function code, chain *[200]byte, src []byte)
2610
TEXT ·kimd(SB), NOFRAME|NOSPLIT, $0-40
2711
MOVD function+0(FP), R0
28-
MOVD params+8(FP), R1
12+
MOVD chain+8(FP), R1
2913
LMG src+16(FP), R2, R3 // R2=base, R3=len
3014

3115
continue:
@@ -34,11 +18,11 @@ continue:
3418
MOVD $0, R0 // reset R0 for pre-go1.8 compilers
3519
RET
3620

37-
// func klmd(function code, params *[200]byte, dst, src []byte)
21+
// func klmd(function code, chain *[200]byte, dst, src []byte)
3822
TEXT ·klmd(SB), NOFRAME|NOSPLIT, $0-64
3923
// TODO: SHAKE support
4024
MOVD function+0(FP), R0
41-
MOVD params+8(FP), R1
25+
MOVD chain+8(FP), R1
4226
LMG dst+16(FP), R2, R3 // R2=base, R3=len
4327
LMG src+40(FP), R4, R5 // R4=base, R5=len
4428

0 commit comments

Comments
 (0)