feat(QMCv2): Add mapCipher & mflac/mgg key discovery
parent
1552a667f6
commit
38648d57e6
@ -0,0 +1,5 @@
|
||||
package qmc
|
||||
|
||||
type streamCipher interface {
|
||||
Decrypt(buf []byte, offset int)
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package qmc
|
||||
|
||||
import "errors"
|
||||
|
||||
type mapCipher struct {
|
||||
key []byte
|
||||
box []byte
|
||||
size int
|
||||
}
|
||||
|
||||
func NewMapCipher(key []byte) (*mapCipher, error) {
|
||||
if len(key) == 0 {
|
||||
return nil, errors.New("qmc/cipher_map: invalid key size")
|
||||
}
|
||||
c := &mapCipher{key: key, size: len(key)}
|
||||
c.box = make([]byte, c.size)
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func (c *mapCipher) getMask(offset int) byte {
|
||||
if offset > 0x7FFF {
|
||||
offset %= 0x7FFF
|
||||
}
|
||||
idx := (offset*offset + 71214) % c.size
|
||||
return c.rotate(c.key[idx], byte(idx)&0x7)
|
||||
}
|
||||
|
||||
func (c *mapCipher) rotate(value byte, bits byte) byte {
|
||||
rotate := (bits + 4) % 8
|
||||
left := value << rotate
|
||||
right := value >> rotate
|
||||
return left | right
|
||||
}
|
||||
|
||||
func (c *mapCipher) Decrypt(buf []byte, offset int) {
|
||||
for i := 0; i < len(buf); i++ {
|
||||
buf[i] ^= c.getMask(offset + i)
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package qmc
|
||||
|
||||
import (
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func loadTestMapCipherData() ([]byte, []byte, []byte, error) {
|
||||
key, err := os.ReadFile("./testdata/mflac_map_key.bin")
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
raw, err := os.ReadFile("./testdata/mflac_map_raw.bin")
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
target, err := os.ReadFile("./testdata/mflac_map_target.bin")
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
return key, raw, target, nil
|
||||
}
|
||||
func Test_mapCipher_Decrypt(t *testing.T) {
|
||||
key, raw, target, err := loadTestMapCipherData()
|
||||
if err != nil {
|
||||
t.Fatalf("load testing data failed: %s", err)
|
||||
}
|
||||
t.Run("overall", func(t *testing.T) {
|
||||
c, err := NewMapCipher(key)
|
||||
if err != nil {
|
||||
t.Errorf("init mapCipher failed: %s", err)
|
||||
return
|
||||
}
|
||||
c.Decrypt(raw, 0)
|
||||
if !reflect.DeepEqual(raw, target) {
|
||||
t.Error("overall")
|
||||
}
|
||||
})
|
||||
}
|
@ -0,0 +1,90 @@
|
||||
package qmc
|
||||
|
||||
import (
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func loadTestRC4CipherData() ([]byte, []byte, []byte, error) {
|
||||
key, err := os.ReadFile("./testdata/mflac0_rc4_key.bin")
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
raw, err := os.ReadFile("./testdata/mflac0_rc4_raw.bin")
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
target, err := os.ReadFile("./testdata/mflac0_rc4_target.bin")
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
|
||||
return key, raw, target, nil
|
||||
}
|
||||
func Test_rc4Cipher_Decrypt(t *testing.T) {
|
||||
key, raw, target, err := loadTestRC4CipherData()
|
||||
if err != nil {
|
||||
t.Fatalf("load testing data failed: %s", err)
|
||||
}
|
||||
t.Run("overall", func(t *testing.T) {
|
||||
c, err := NewRC4Cipher(key)
|
||||
if err != nil {
|
||||
t.Errorf("init rc4Cipher failed: %s", err)
|
||||
return
|
||||
}
|
||||
c.Decrypt(raw, 0)
|
||||
if !reflect.DeepEqual(raw, target) {
|
||||
t.Error("overall")
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func Test_rc4Cipher_encFirstSegment(t *testing.T) {
|
||||
key, raw, target, err := loadTestRC4CipherData()
|
||||
if err != nil {
|
||||
t.Fatalf("load testing data failed: %s", err)
|
||||
}
|
||||
t.Run("first-block(0~128)", func(t *testing.T) {
|
||||
c, err := NewRC4Cipher(key)
|
||||
if err != nil {
|
||||
t.Errorf("init rc4Cipher failed: %s", err)
|
||||
return
|
||||
}
|
||||
c.Decrypt(raw[:128], 0)
|
||||
if !reflect.DeepEqual(raw[:128], target[:128]) {
|
||||
t.Error("first-block(0~128)")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func Test_rc4Cipher_encASegment(t *testing.T) {
|
||||
key, raw, target, err := loadTestRC4CipherData()
|
||||
if err != nil {
|
||||
t.Fatalf("load testing data failed: %s", err)
|
||||
}
|
||||
|
||||
t.Run("align-block(128~5120)", func(t *testing.T) {
|
||||
c, err := NewRC4Cipher(key)
|
||||
if err != nil {
|
||||
t.Errorf("init rc4Cipher failed: %s", err)
|
||||
return
|
||||
}
|
||||
c.Decrypt(raw[128:5120], 128)
|
||||
if !reflect.DeepEqual(raw[128:5120], target[128:5120]) {
|
||||
t.Error("align-block(128~5120)")
|
||||
}
|
||||
})
|
||||
t.Run("simple-block(5120~10240)", func(t *testing.T) {
|
||||
c, err := NewRC4Cipher(key)
|
||||
if err != nil {
|
||||
t.Errorf("init rc4Cipher failed: %s", err)
|
||||
return
|
||||
}
|
||||
c.Decrypt(raw[5120:10240], 5120)
|
||||
if !reflect.DeepEqual(raw[5120:10240], target[5120:10240]) {
|
||||
t.Error("align-block(128~5120)")
|
||||
}
|
||||
})
|
||||
}
|
@ -1,72 +0,0 @@
|
||||
package qmc
|
||||
|
||||
import (
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func loadTestData() (*rc4Cipher, []byte, []byte, error) {
|
||||
key, err := os.ReadFile("./testdata/rc4_key.bin")
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
raw, err := os.ReadFile("./testdata/rc4_raw.bin")
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
target, err := os.ReadFile("./testdata/rc4_target.bin")
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
c, err := NewRC4Cipher(key)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
return c, raw, target, nil
|
||||
}
|
||||
func Test_rc4Cipher_Process(t *testing.T) {
|
||||
c, raw, target, err := loadTestData()
|
||||
if err != nil {
|
||||
t.Errorf("load testing data failed: %s", err)
|
||||
}
|
||||
t.Run("overall", func(t *testing.T) {
|
||||
c.Process(raw, 0)
|
||||
if !reflect.DeepEqual(raw, target) {
|
||||
t.Error("overall")
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func Test_rc4Cipher_encFirstSegment(t *testing.T) {
|
||||
c, raw, target, err := loadTestData()
|
||||
if err != nil {
|
||||
t.Errorf("load testing data failed: %s", err)
|
||||
}
|
||||
t.Run("first-block(0~128)", func(t *testing.T) {
|
||||
c.Process(raw[:128], 0)
|
||||
if !reflect.DeepEqual(raw[:128], target[:128]) {
|
||||
t.Error("first-block(0~128)")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func Test_rc4Cipher_encASegment(t *testing.T) {
|
||||
c, raw, target, err := loadTestData()
|
||||
if err != nil {
|
||||
t.Errorf("load testing data failed: %s", err)
|
||||
}
|
||||
t.Run("align-block(128~5120)", func(t *testing.T) {
|
||||
c.Process(raw[128:5120], 128)
|
||||
if !reflect.DeepEqual(raw[128:5120], target[128:5120]) {
|
||||
t.Error("align-block(128~5120)")
|
||||
}
|
||||
})
|
||||
t.Run("simple-block(5120~10240)", func(t *testing.T) {
|
||||
c.Process(raw[5120:10240], 5120)
|
||||
if !reflect.DeepEqual(raw[5120:10240], target[5120:10240]) {
|
||||
t.Error("align-block(128~5120)")
|
||||
}
|
||||
})
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue