Skip to content

Commit

Permalink
cmd/asm: add KMA and KMCTR instructions on s390x.
Browse files Browse the repository at this point in the history
This CL is to add assembly instruction mnemonics for the following instructions, mainly used in crypto packages.

 * KMA    - cipher message with authentication
 * KMCTR  - cipher message with counter

Fixes #61163

Change-Id: Iff9a69911aeb4fab4bca8755b23a106eaebb2332
Reviewed-on: https://go-review.googlesource.com/c/go/+/515195
Reviewed-by: Carlos Amedee <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Reviewed-by: Cherry Mui <[email protected]>
Run-TryBot: Cherry Mui <[email protected]>
  • Loading branch information
Vishwanatha-HD authored and cherrymui committed Sep 5, 2023
1 parent a2647f0 commit a40404d
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/cmd/asm/internal/asm/testdata/s390x.s
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,8 @@ TEXT main·foo(SB),DUPOK|NOSPLIT,$16-0 // TEXT main.foo(SB), DUPOK|NOSPLIT, $16-
KLMD R2, R8 // b93f0028
KIMD R0, R4 // b93e0004
KDSA R0, R8 // b93a0008
KMA R6, R2, R4 // b9296024
KMCTR R6, R2, R4 // b92d6024

// vector add and sub instructions
VAB V3, V4, V4 // e743400000f3
Expand Down
2 changes: 2 additions & 0 deletions src/cmd/internal/obj/s390x/a.out.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,8 @@ const (
AKLMD
AKIMD
AKDSA
AKMA
AKMCTR

// vector
AVA
Expand Down
2 changes: 2 additions & 0 deletions src/cmd/internal/obj/s390x/anames.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions src/cmd/internal/obj/s390x/asmz.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,9 @@ var optab = []Optab{
// KDSA
{i: 125, as: AKDSA, a1: C_REG, a6: C_REG},

// KMA
{i: 126, as: AKMA, a1: C_REG, a2: C_REG, a6: C_REG},

// vector instructions

// VRX store
Expand Down Expand Up @@ -1492,6 +1495,8 @@ func buildop(ctxt *obj.Link) {
opset(AKMC, r)
opset(AKLMD, r)
opset(AKIMD, r)
case AKMA:
opset(AKMCTR, r)
}
}
}
Expand Down Expand Up @@ -1896,6 +1901,7 @@ const (
op_KM uint32 = 0xB92E // FORMAT_RRE CIPHER MESSAGE
op_KMAC uint32 = 0xB91E // FORMAT_RRE COMPUTE MESSAGE AUTHENTICATION CODE
op_KMC uint32 = 0xB92F // FORMAT_RRE CIPHER MESSAGE WITH CHAINING
op_KMA uint32 = 0xB929 // FORMAT_RRF2 CIPHER MESSAGE WITH AUTHENTICATION
op_KMCTR uint32 = 0xB92D // FORMAT_RRF2 CIPHER MESSAGE WITH COUNTER
op_KMF uint32 = 0xB92A // FORMAT_RRE CIPHER MESSAGE WITH CFB
op_KMO uint32 = 0xB92B // FORMAT_RRE CIPHER MESSAGE WITH OFB
Expand Down Expand Up @@ -4428,6 +4434,40 @@ func (c *ctxtz) asmout(p *obj.Prog, asm *[]byte) {
}
zRRE(op_KDSA, uint32(p.From.Reg), uint32(p.To.Reg), asm)

case 126: // KMA and KMCTR - CIPHER MESSAGE WITH AUTHENTICATION; CIPHER MESSAGE WITH
var opcode uint32
switch p.As {
default:
c.ctxt.Diag("unexpected opcode %v", p.As)
case AKMA, AKMCTR:
if p.From.Reg == REG_R0 {
c.ctxt.Diag("input argument must not be R0 in %v", p)
}
if p.From.Reg&1 != 0 {
c.ctxt.Diag("input argument must be even register in %v", p)
}
if p.To.Reg == REG_R0 {
c.ctxt.Diag("output argument must not be R0 in %v", p)
}
if p.To.Reg&1 != 0 {
c.ctxt.Diag("output argument must be an even register in %v", p)
}
if p.Reg == REG_R0 {
c.ctxt.Diag("third argument must not be R0 in %v", p)
}
if p.Reg&1 != 0 {
c.ctxt.Diag("third argument must be even register in %v", p)
}
if p.Reg == p.To.Reg || p.Reg == p.From.Reg {
c.ctxt.Diag("third argument must not be input or output argument registers in %v", p)
}
if p.As == AKMA {
opcode = op_KMA
} else if p.As == AKMCTR {
opcode = op_KMCTR
}
}
zRRF(opcode, uint32(p.From.Reg), 0, uint32(p.Reg), uint32(p.To.Reg), asm)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/crypto/aes/asm_s390x.s
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ crypt:
MOVD src_base+56(FP), R6 // src
MOVD src_len+64(FP), R7 // len
loop:
WORD $0xB92D2046 // cipher message with counter (KMCTR)
KMCTR R6, R2, R4 // cipher message with counter (KMCTR)
BVS loop // branch back if interrupted
RET
crash:
Expand Down Expand Up @@ -180,7 +180,7 @@ TEXT ·kmaGCM(SB),NOSPLIT,$112-120
MVC $8, 24(R8), 104(R1)

kma:
WORD $0xb9296024 // kma %r6,%r2,%r4
KMA R6, R2, R4 // Cipher Message with Authentication
BVS kma

MOVD tag+104(FP), R2
Expand Down
4 changes: 2 additions & 2 deletions src/internal/cpu/cpu_s390x.s
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ TEXT ·kmcQuery(SB), NOSPLIT|NOFRAME, $0-16
TEXT ·kmctrQuery(SB), NOSPLIT|NOFRAME, $0-16
MOVD $0, R0 // set function code to 0 (KMCTR-Query)
MOVD $ret+0(FP), R1 // address of 16-byte return value
WORD $0xB92D4024 // cipher message with counter (KMCTR)
KMCTR R6, R2, R4 // cipher message with counter (KMCTR)
RET

// func kmaQuery() queryResult
TEXT ·kmaQuery(SB), NOSPLIT|NOFRAME, $0-16
MOVD $0, R0 // set function code to 0 (KMA-Query)
MOVD $ret+0(FP), R1 // address of 16-byte return value
WORD $0xb9296024 // cipher message with authentication (KMA)
KMA R6, R2, R4 // cipher message with authentication (KMA)
RET

// func kimdQuery() queryResult
Expand Down

0 comments on commit a40404d

Please sign in to comment.