1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-04 19:17:37 +08:00

optimize(t544): drop unsafe (#2076)

Updates #2075 #2072 #2051
This commit is contained in:
源文雨 2023-04-09 17:25:57 +08:00 committed by GitHub
parent 0b90074a48
commit 91b4394d9b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 104 additions and 55 deletions

View File

@ -50,4 +50,4 @@ func (c *state) init(key []byte, data []byte, counter uint64, nr uint8) {
initState(c, key, data, counter) initState(c, key, data, counter)
} }
func sub_ad([]uint32) func refreshState(c *state)

View File

@ -10,8 +10,8 @@ DATA LC0<>+12(SB)/4, $1797285236
GLOBL LC0<>(SB), NOPTR, $16 GLOBL LC0<>(SB), NOPTR, $16
TEXT ·sub_a(SB), NOSPLIT, $0-48 TEXT ·sub_a(SB), NOSPLIT, $0-48
MOVQ ·a+0(FP), DI MOVQ a+0(FP), DI
MOVQ ·b+24(FP), CX MOVQ b+24(FP), CX
MOVQ CX, DX MOVQ CX, DX
MOVBLZX 3(CX), CX MOVBLZX 3(CX), CX
XORB CX, (DI) XORB CX, (DI)
@ -48,8 +48,8 @@ TEXT ·sub_a(SB), NOSPLIT, $0-48
RET RET
TEXT ·sub_b(SB), NOSPLIT, $0-48 TEXT ·sub_b(SB), NOSPLIT, $0-48
MOVQ ·a+0(FP), DI MOVQ a+0(FP), DI
MOVQ ·b+24(FP), CX MOVQ b+24(FP), CX
MOVQ CX, DX MOVQ CX, DX
MOVBLZX 3(CX), CX MOVBLZX 3(CX), CX
XORB CX, (DI) XORB CX, (DI)
@ -87,8 +87,8 @@ TEXT ·sub_b(SB), NOSPLIT, $0-48
TEXT ·sub_c(SB), NOSPLIT, $0-32 TEXT ·sub_c(SB), NOSPLIT, $0-32
MOVQ ·a+0(FP), DI MOVQ a+0(FP), DI
MOVQ ·b+8(FP), SI MOVQ b+8(FP), SI
MOVQ SI, AX MOVQ SI, AX
MOVBLZX (SI), SI MOVBLZX (SI), SI
MOVL SI, CX MOVL SI, CX
@ -236,26 +236,27 @@ TEXT ·sub_c(SB), NOSPLIT, $0-32
MOVB CX, 15(AX) MOVB CX, 15(AX)
RET RET
TEXT ·sub_d(SB), NOSPLIT, $16-32 TEXT ·sub_d(SB), NOSPLIT, $24-32
MOVQ ·t+0(FP), BX MOVQ t+0(FP), BX
MOVQ ·s+8(FP), SI MOVQ s+8(FP), DI
MOVOU (SI), X0 MOVOU (DI), X0
MOVOU X0, in-16(SP) MOVOU X0, in-16(SP)
MOVQ SI, DI MOVQ $16, CX
ADDQ $15, DI ADDQ $15, DI
MOVB $16, CX PUSHFQ
STD
lop: lop:
LEAQ -1(CX), AX LEAQ -1(CX), AX
XLAT XLAT
MOVBLZX in-16(SP)(AX*1), AX LEAQ in-16(SP)(AX*1), SI
STD MOVSB
STOSB
LOOP lop LOOP lop
POPFQ
RET RET
TEXT ·sub_e(SB), NOSPLIT, $0-32 TEXT ·sub_e(SB), NOSPLIT, $0-32
MOVQ ·a+0(FP), DI MOVQ a+0(FP), DI
MOVQ ·n+8(FP), SI MOVQ n+8(FP), SI
MOVQ $4, AX MOVQ $4, AX
lop: lop:
MOVBQZX -4(SI)(AX*4), DX MOVBQZX -4(SI)(AX*4), DX
@ -295,9 +296,9 @@ lop:
JNZ lop JNZ lop
RET RET
TEXT sub_ab(SB), NOSPLIT, $0-24 TEXT sub_ab<>(SB), NOSPLIT, $0-24
MOVQ ·s+0(FP), DI MOVQ s+0(FP), DI
MOVQ ·w+8(FP), SI MOVQ w+8(FP), SI
MOVL SI, AX MOVL SI, AX
MOVL SI, CX MOVL SI, CX
MOVL SI, DX MOVL SI, DX
@ -329,14 +330,14 @@ TEXT sub_ab(SB), NOSPLIT, $0-24
MOVBLZX (DI)(DX*1), DX MOVBLZX (DI)(DX*1), DX
SALL $16, DX SALL $16, DX
ORL DX, AX ORL DX, AX
MOVQ AX, ·retval+16(FP) MOVQ AX, retval+16(FP)
RET RET
TEXT ·sub_f(SB), NOSPLIT, $24-68 TEXT ·sub_f(SB), NOSPLIT, $24-68
MOVQ ·k+0(FP), DI MOVQ k+0(FP), DI
MOVQ ·r+8(FP), SI MOVQ r+8(FP), SI
MOVQ ·s+16(FP), DX MOVQ s+16(FP), DX
MOVQ $·w+24(FP), CX MOVQ $w+24(FP), CX
MOVQ CX, R10 MOVQ CX, R10
MOVQ SI, R9 MOVQ SI, R9
MOVQ DX, R8 MOVQ DX, R8
@ -367,7 +368,7 @@ inner:
ROLL $8, AX ROLL $8, AX
MOVQ R8, 0(SP) MOVQ R8, 0(SP)
MOVL AX, 8(SP) MOVL AX, 8(SP)
CALL sub_ab(SB) CALL sub_ab<>(SB)
MOVQ 16(SP), AX MOVQ 16(SP), AX
LEAL -1(BX), DX LEAL -1(BX), DX
SARL $2, DX SARL $2, DX
@ -378,10 +379,10 @@ end:
RET RET
TEXT ·sub_aa(SB), NOSPLIT, $0-56 TEXT ·sub_aa(SB), NOSPLIT, $0-56
MOVQ ·i+0(FP), DI MOVQ i+0(FP), DI
MOVQ ·t+8(FP), SI MOVQ t+8(FP), SI
MOVQ ·b+16(FP), DX MOVQ b+16(FP), DX
MOVQ ·m+24(FP), CX MOVQ m+24(FP), CX
MOVL DI, AX MOVL DI, AX
MOVLQSX DI, DI MOVLQSX DI, DI
MOVQ SI, R8 MOVQ SI, R8
@ -407,13 +408,13 @@ TEXT ·sub_aa(SB), NOSPLIT, $0-56
MOVBLZX (AX)(DI*1), AX MOVBLZX (AX)(DI*1), AX
SALL $4, AX SALL $4, AX
ORB 256(SI)(DX*1), AX ORB 256(SI)(DX*1), AX
MOVQ AX, ·retval+48(FP) MOVQ AX, retval+48(FP)
RET RET
// func transformInner(x *[0x15]byte, tab *[32][16]byte) // func transformInner(x *[0x15]byte, tab *[32][16]byte)
TEXT ·transformInner(SB), NOSPLIT, $0-16 TEXT ·transformInner(SB), NOSPLIT, $0-16
MOVQ ·x+0(FP), DI MOVQ x+0(FP), DI
MOVQ ·tab+8(FP), SI MOVQ tab+8(FP), SI
MOVQ DI, AX MOVQ DI, AX
MOVL $1, CX MOVL $1, CX
MOVQ SI, DI MOVQ SI, DI
@ -446,10 +447,10 @@ lop:
RET RET
TEXT ·initState(SB), NOSPLIT, $0-64 TEXT ·initState(SB), NOSPLIT, $0-64
MOVQ ·c+0(FP), DI MOVQ c+0(FP), DI
MOVQ ·key+8(FP), SI MOVQ key+8(FP), SI
MOVQ ·data+32(FP), R8 MOVQ data+32(FP), R8
MOVQ ·counter+56(FP), AX MOVQ counter+56(FP), AX
MOVOA LC0<>(SB), X0 MOVOA LC0<>(SB), X0
MOVUPS X0, (DI) MOVUPS X0, (DI)
MOVOU (SI), X1 MOVOU (SI), X1
@ -467,8 +468,8 @@ TEXT ·initState(SB), NOSPLIT, $0-64
MOVUPS X6,112(DI) MOVUPS X6,112(DI)
RET RET
TEXT ·sub_ad(SB), NOSPLIT, $8-24 TEXT sub_ad<>(SB), NOSPLIT, $8-8
MOVQ ·a+0(FP), DI MOVQ a+0(FP), DI
MOVQ DI, AX MOVQ DI, AX
MOVL 40(DI), R10 MOVL 40(DI), R10
MOVL 12(DI), R12 MOVL 12(DI), R12
@ -610,11 +611,42 @@ TEXT ·sub_ad(SB), NOSPLIT, $8-24
MOVUPS X0, 32(AX) MOVUPS X0, 32(AX)
RET RET
TEXT ·refreshState(SB), NOSPLIT, $16-8
MOVQ i+0(FP), BX
MOVB 128(BX), CX
JE ad
SHRQ $1, CX
fr:
MOVQ BX, 0(SP)
MOVQ CX, c-8(SP)
CALL sub_ad<>(SB)
MOVQ c-8(SP), CX
MOVQ i+0(FP), BX
LOOP fr
ad:
MOVOU (BX), X0
MOVOU 64(BX), X1
MOVOU 80(BX), X2
MOVOU 96(BX), X3
PADDD X1, X0
MOVOU 48(BX), X4
MOVUPS X0, (BX)
MOVOU 16(BX), X0
PADDD X2, X0
MOVUPS X0, 16(BX)
MOVOU 32(BX), X0
PADDD X3, X0
MOVUPS X0, 32(BX)
MOVOU 112(BX), X0
PADDD X4, X0
MOVUPS X0, 48(BX)
RET
// func tencentCrc32(tab *crc32.Table, b []byte) uint32 // func tencentCrc32(tab *crc32.Table, b []byte) uint32
TEXT ·tencentCrc32(SB), NOSPLIT, $0-40 TEXT ·tencentCrc32(SB), NOSPLIT, $0-40
MOVQ ·tab+0(FP), DI MOVQ tab+0(FP), DI
MOVQ ·bptr+8(FP), SI MOVQ bptr+8(FP), SI
MOVQ ·bngas+16(FP), DX MOVQ bngas+16(FP), DX
TESTQ DX, DX TESTQ DX, DX
JE quickend JE quickend
ADDQ SI, DX ADDQ SI, DX
@ -629,7 +661,7 @@ lop:
CMPQ SI, DX CMPQ SI, DX
JNE lop JNE lop
NOTL AX NOTL AX
MOVQ AX, ·bngas+32(FP) MOVQ AX, bngas+32(FP)
RET RET
quickend: quickend:
XORL AX, AX XORL AX, AX

View File

@ -91,12 +91,7 @@ func (c *state) encrypt(data []byte) {
dataLen := uint32(len(data)) dataLen := uint32(len(data))
for dataLen > 0 { for dataLen > 0 {
if c.p == 0 { if c.p == 0 {
for i := uint8(0); i < c.nr; i += 2 { refreshState(c)
sub_ad(c.state[:])
}
for i := 0; i < 16; i++ {
c.state[i] += c.orgstate[i]
}
} }
var sb [16 * 4]byte var sb [16 * 4]byte
for i, v := range c.state { for i, v := range c.state {

View File

@ -7,7 +7,6 @@ import (
"crypto/rc4" "crypto/rc4"
"encoding/binary" "encoding/binary"
"math/rand" "math/rand"
"unsafe"
"github.com/Mrs4s/go-cqhttp/internal/encryption" "github.com/Mrs4s/go-cqhttp/internal/encryption"
) )
@ -31,8 +30,10 @@ func init() {
// sign t544 algorithm // sign t544 algorithm
// special thanks to the anonymous contributor who provided the algorithm // special thanks to the anonymous contributor who provided the algorithm
func sign(curr int64, input []byte) []byte { func sign(curr int64, input []byte) []byte {
var crcData [0x15]byte
curr %= 1000000 curr %= 1000000
input = append(input, []byte{byte(curr >> 24), byte(curr >> 16), byte(curr >> 8), byte(curr)}...) binary.BigEndian.PutUint32(crcData[:4], uint32(curr))
input = append(input, crcData[:4]...)
var kt [4 + 32 + 4]byte var kt [4 + 32 + 4]byte
r := rand.New(rand.NewSource(curr)) r := rand.New(rand.NewSource(curr))
for i := 0; i < 2; i++ { for i := 0; i < 2; i++ {
@ -51,8 +52,7 @@ func sign(curr int64, input []byte) []byte {
k3calc[6], k3calc[7] = 0, 0 k3calc[6], k3calc[7] = 0, 0
rc4Cipher, _ := rc4.NewCipher(key3) rc4Cipher, _ := rc4.NewCipher(key3)
rc4Cipher.XORKeyStream(key3, key3) rc4Cipher.XORKeyStream(key3, key3)
var crcData [0x15]byte binary.LittleEndian.PutUint64(crcData[4:4+8], magic)
copy(crcData[4:4+8], (*[8]byte)(unsafe.Pointer(&magic))[:])
tencentEncryptionA(input, kt[4:4+32], crcData[4:4+8]) tencentEncryptionA(input, kt[4:4+32], crcData[4:4+8])
result := md5.Sum(input) result := md5.Sum(input)
crcData[2] = 1 crcData[2] = 1
@ -61,7 +61,7 @@ func sign(curr int64, input []byte) []byte {
binary.BigEndian.PutUint32(crcData[9:13], uint32(curr)) binary.BigEndian.PutUint32(crcData[9:13], uint32(curr))
copy(crcData[13:], result[:8]) copy(crcData[13:], result[:8])
calcCrc := tencentCrc32(&crc32Table, crcData[2:]) calcCrc := tencentCrc32(&crc32Table, crcData[2:])
copy(kt[4+32:4+32+4], (*[4]byte)(unsafe.Pointer(&calcCrc))[:]) binary.LittleEndian.PutUint32(kt[4+32:4+32+4], calcCrc)
crcData[0] = kt[4+32] crcData[0] = kt[4+32]
crcData[1] = kt[4+32+3] crcData[1] = kt[4+32+3]
nonce := uint32(r.Int() ^ r.Int() ^ r.Int()) nonce := uint32(r.Int() ^ r.Int() ^ r.Int())

View File

@ -0,0 +1,22 @@
package t544
import (
"crypto/rand"
"encoding/hex"
"testing"
)
func TestT544(t *testing.T) {
r := hex.EncodeToString(sign(0, []byte{}))
if r != "0c05d28b405bce1595c70ffa694ff163d4b600f229482e07de32c8000000003525382c00000000" {
t.Fatal(r)
}
}
func TestCrash(t *testing.T) {
brand := make([]byte, 4096)
for i := 1; i <= 1024; i++ {
rand.Reader.Read(brand)
sign(123, brand)
}
}