Skip to content

Commit

Permalink
Fix regression in key buffer output
Browse files Browse the repository at this point in the history
  • Loading branch information
cedws committed Apr 24, 2023
1 parent 71564cb commit 583ba54
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 91 deletions.
8 changes: 1 addition & 7 deletions cmd/eject.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,9 @@ var ejectCmd = &cobra.Command{
os.Exit(1)
}

raw, err := kr.MarshalBinary()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
enc.Encode(output{raw, kr})
enc.Encode(kr)
},
}

Expand Down
10 changes: 2 additions & 8 deletions cmd/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,14 @@ var injectCmd = &cobra.Command{
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if err := os.WriteFile(binPath, newData, 0644); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

raw, err := kr.MarshalBinary()
if err != nil {
if err := os.WriteFile(binPath, newData, 0o644); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
enc.Encode(output{raw, kr})
enc.Encode(kr)
},
}

Expand Down
6 changes: 0 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@ import (
"fmt"
"os"

"github.com/cedws/ki-keyring/keyring"
"github.com/spf13/cobra"
)

type output struct {
Raw []byte `json:"raw"`
Decoded *keyring.Keyring `json:"decoded"`
}

var binPath string

var rootCmd = &cobra.Command{
Expand Down
59 changes: 26 additions & 33 deletions keyring/keyring.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ type Key struct {
Private []byte `json:"private"`
}

type Keyring [keyCount]Key
type Keyring struct {
Raw []byte `json:"raw"`
Decoded [keyCount]Key `json:"decoded"`
}

func (k Key) MarshalBinary() ([]byte, error) {
var cipher CipherType
Expand Down Expand Up @@ -118,48 +121,31 @@ func Extract(gameData []byte) (*Keyring, error) {
if err != nil {
return nil, err
}

if len(rawKeys) < 1 {
return nil, fmt.Errorf("raw key buffer too short")
}
rawKeys = rawKeys[1:]

var keyring Keyring
if err := keyring.UnmarshalBinary(rawKeys); err != nil {
return nil, err
kr := Keyring{
Raw: rawKeys,
}
rawKeys = rawKeys[1:]

return &keyring, nil
}
for i := range kr.Decoded {
length := 3 + binary.LittleEndian.Uint16(rawKeys[:2])

func (k Keyring) MarshalBinary() (buf []byte, err error) {
for _, key := range k {
bytes, err := key.MarshalBinary()
if err != nil {
if err := kr.Decoded[i].UnmarshalBinary(rawKeys[:length]); err != nil {
return nil, err
}

buf = append(buf, bytes...)
rawKeys = rawKeys[length:]
}

return
}

func (k *Keyring) UnmarshalBinary(bytes []byte) error {
for i := range k {
length := 3 + binary.LittleEndian.Uint16(bytes[:2])

if err := k[i].UnmarshalBinary(bytes[:length]); err != nil {
return err
}

bytes = bytes[length:]
}

return nil
return &kr, nil
}

func (k *Keyring) Regenerate() error {
for i := range k {
for i := range k.Decoded {
keypair, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return err
Expand All @@ -169,13 +155,13 @@ func (k *Keyring) Regenerate() error {
if err != nil {
return err
}
k[i].Public = pub
k.Decoded[i].Public = pub

priv, err := x509.MarshalPKCS8PrivateKey(keypair)
if err != nil {
return err
}
k[i].Private = priv
k.Decoded[i].Private = priv
}

return nil
Expand All @@ -187,15 +173,22 @@ func (kr *Keyring) Inject(gameData []byte) ([]byte, error) {
if err != nil {
return nil, err
}

if len(rawKeys) < 1 {
return nil, fmt.Errorf("raw key buffer too short")
}
rawKeys = rawKeys[1:]

buf, err := kr.MarshalBinary()
if err != nil {
return nil, err
var buf []byte
for _, key := range kr.Decoded {
bytes, err := key.MarshalBinary()
if err != nil {
return nil, err
}

buf = append(buf, bytes...)
}

if len(rawKeys) != len(buf) {
panic(fmt.Sprintf("unexpected length diff between original and new key buffer (%v => %v)", len(rawKeys), len(buf)))
}
Expand Down
86 changes: 49 additions & 37 deletions keyring/keyring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,24 @@ func joinKeys(keys map[int]string) (s string) {
return
}

func TestSingleKeyUnmarshal(t *testing.T) {
func TestKey_MarshalBinary(t *testing.T) {
expected, err := hex.DecodeString(keys[0])
assert.Nil(t, err)

key := Key{
Cipher: "ARIA",
Operand: 251,
DecryptionKey: []byte{0xce, 0x52, 0x83, 0x72, 0xe3, 0xed, 0xd5, 0x85, 0x45, 0x30, 0xf4, 0x5f, 0x13, 0x38, 0x0d, 0x00},
DecryptionIV: []byte{0x51, 0x57, 0x3b, 0x7f, 0x75, 0x88, 0x32, 0x6e, 0x67, 0xe4, 0xda, 0x8a, 0x74, 0x4d, 0xdd, 0x86},
Public: []byte{0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xc8, 0x81, 0x96, 0x44, 0xeb, 0x0c, 0x00, 0x53, 0x0f, 0xe8, 0xe8, 0x92, 0x30, 0x69, 0x1f, 0xd4, 0xaf, 0x2c, 0x9a, 0x22, 0x23, 0x29, 0x88, 0x99, 0x1a, 0x0c, 0x4f, 0x6d, 0x32, 0xa3, 0xa0, 0x48, 0xfe, 0x40, 0x54, 0x09, 0xb9, 0x92, 0xa3, 0xde, 0x3c, 0x1f, 0xd2, 0x0f, 0xd6, 0x45, 0x8b, 0x8b, 0xc7, 0x32, 0xae, 0xd1, 0x36, 0x12, 0x9a, 0xd5, 0x4b, 0x98, 0xf6, 0xb9, 0x10, 0x2a, 0x04, 0x1b, 0x8a, 0x07, 0x97, 0x00, 0x6c, 0x63, 0x24, 0xee, 0x77, 0x9d, 0x71, 0x30, 0x52, 0x7a, 0xec, 0xe1, 0xe0, 0x20, 0x61, 0x90, 0x0d, 0x16, 0x09, 0xe3, 0x03, 0x16, 0x15, 0x89, 0x80, 0xc7, 0xb8, 0x49, 0x7d, 0xbf, 0xc5, 0x0d, 0x7e, 0x79, 0xfd, 0x3c, 0x00, 0x00, 0x27, 0xab, 0x5f, 0xe4, 0xdf, 0xc5, 0x7b, 0x29, 0xdf, 0x16, 0x4d, 0x73, 0xc1, 0xc6, 0x80, 0x9f, 0xda, 0x79, 0x16, 0x82, 0x91, 0x19, 0x96, 0x94, 0xf2, 0xa3, 0x0f, 0x93, 0xa2, 0xf2, 0xc6, 0x5c, 0x72, 0x16, 0x20, 0x58, 0xf6, 0x3e, 0x10, 0x59, 0x64, 0x78, 0x56, 0x1e, 0x7f, 0x5c, 0x53, 0x02, 0x60, 0xfe, 0xb0, 0x2c, 0x00, 0x6e, 0x66, 0x32, 0xfd, 0x38, 0x57, 0x48, 0x75, 0x6e, 0x69, 0x9d, 0x47, 0x35, 0x8e, 0xd3, 0x17, 0xe5, 0xc4, 0x74, 0xb0, 0x19, 0xb1, 0xc4, 0xd3, 0x45, 0xc3, 0xc2, 0x23, 0xae, 0x73, 0x9a, 0x32, 0x10, 0xb4, 0x0a, 0xba, 0xfe, 0xf5, 0x55, 0x75, 0xd0, 0x3d, 0xa5, 0x14, 0xe0, 0x47, 0x35, 0xeb, 0xac, 0x4b, 0xb4, 0x2e, 0x1e, 0xbd, 0x27, 0xad, 0x94, 0x0f, 0x61, 0x70, 0x43, 0x40, 0x3c, 0x62, 0x00, 0x0e, 0x3c, 0x6b, 0x2c, 0x43, 0x32, 0xe8, 0xbe, 0xd4, 0xcd, 0xc0, 0xf5, 0xbf, 0x33, 0xfa, 0x98, 0x54, 0x8a, 0x49, 0x7c, 0x2e, 0x88, 0xb9, 0x09, 0x2b, 0xcc, 0x7a, 0x68, 0xad, 0xf9, 0x16, 0xb9, 0x02, 0x03, 0x01, 0x00, 0x01},
}
buf, err := key.MarshalBinary()
assert.Nil(t, err)

assert.Equal(t, expected, buf)
}

func TestKey_UnmarshalBinary(t *testing.T) {
buf, err := hex.DecodeString(keys[0])
assert.Nil(t, err)

Expand All @@ -34,49 +51,44 @@ func TestSingleKeyUnmarshal(t *testing.T) {

assert.Equal(t, "ARIA", key.Cipher)
assert.Equal(t, byte(0xfb), key.Operand)
assert.Equal(t, []byte{0xce, 0x52, 0x83, 0x72, 0xe3, 0xed, 0xd5, 0x85, 0x45, 0x30, 0xf4, 0x5f, 0x13, 0x38, 0x0d, 0x00}, key.DecryptionKey)
assert.Equal(t, []byte{0x51, 0x57, 0x3b, 0x7f, 0x75, 0x88, 0x32, 0x6e, 0x67, 0xe4, 0xda, 0x8a, 0x74, 0x4d, 0xdd, 0x86}, key.DecryptionIV)
assert.Equal(t, []byte{0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xc8, 0x81, 0x96, 0x44, 0xeb, 0x0c, 0x00, 0x53, 0x0f, 0xe8, 0xe8, 0x92, 0x30, 0x69, 0x1f, 0xd4, 0xaf, 0x2c, 0x9a, 0x22, 0x23, 0x29, 0x88, 0x99, 0x1a, 0x0c, 0x4f, 0x6d, 0x32, 0xa3, 0xa0, 0x48, 0xfe, 0x40, 0x54, 0x09, 0xb9, 0x92, 0xa3, 0xde, 0x3c, 0x1f, 0xd2, 0x0f, 0xd6, 0x45, 0x8b, 0x8b, 0xc7, 0x32, 0xae, 0xd1, 0x36, 0x12, 0x9a, 0xd5, 0x4b, 0x98, 0xf6, 0xb9, 0x10, 0x2a, 0x04, 0x1b, 0x8a, 0x07, 0x97, 0x00, 0x6c, 0x63, 0x24, 0xee, 0x77, 0x9d, 0x71, 0x30, 0x52, 0x7a, 0xec, 0xe1, 0xe0, 0x20, 0x61, 0x90, 0x0d, 0x16, 0x09, 0xe3, 0x03, 0x16, 0x15, 0x89, 0x80, 0xc7, 0xb8, 0x49, 0x7d, 0xbf, 0xc5, 0x0d, 0x7e, 0x79, 0xfd, 0x3c, 0x00, 0x00, 0x27, 0xab, 0x5f, 0xe4, 0xdf, 0xc5, 0x7b, 0x29, 0xdf, 0x16, 0x4d, 0x73, 0xc1, 0xc6, 0x80, 0x9f, 0xda, 0x79, 0x16, 0x82, 0x91, 0x19, 0x96, 0x94, 0xf2, 0xa3, 0x0f, 0x93, 0xa2, 0xf2, 0xc6, 0x5c, 0x72, 0x16, 0x20, 0x58, 0xf6, 0x3e, 0x10, 0x59, 0x64, 0x78, 0x56, 0x1e, 0x7f, 0x5c, 0x53, 0x02, 0x60, 0xfe, 0xb0, 0x2c, 0x00, 0x6e, 0x66, 0x32, 0xfd, 0x38, 0x57, 0x48, 0x75, 0x6e, 0x69, 0x9d, 0x47, 0x35, 0x8e, 0xd3, 0x17, 0xe5, 0xc4, 0x74, 0xb0, 0x19, 0xb1, 0xc4, 0xd3, 0x45, 0xc3, 0xc2, 0x23, 0xae, 0x73, 0x9a, 0x32, 0x10, 0xb4, 0x0a, 0xba, 0xfe, 0xf5, 0x55, 0x75, 0xd0, 0x3d, 0xa5, 0x14, 0xe0, 0x47, 0x35, 0xeb, 0xac, 0x4b, 0xb4, 0x2e, 0x1e, 0xbd, 0x27, 0xad, 0x94, 0x0f, 0x61, 0x70, 0x43, 0x40, 0x3c, 0x62, 0x00, 0x0e, 0x3c, 0x6b, 0x2c, 0x43, 0x32, 0xe8, 0xbe, 0xd4, 0xcd, 0xc0, 0xf5, 0xbf, 0x33, 0xfa, 0x98, 0x54, 0x8a, 0x49, 0x7c, 0x2e, 0x88, 0xb9, 0x09, 0x2b, 0xcc, 0x7a, 0x68, 0xad, 0xf9, 0x16, 0xb9, 0x02, 0x03, 0x01, 0x00, 0x01}, key.Public)
}

func TestMultiKeyUmmarshal(t *testing.T) {
buf, err := hex.DecodeString(allKeys)
assert.Nil(t, err)

func TestKeyringDecoded_UnmarshalBinary(t *testing.T) {
var keyring Keyring
err = keyring.UnmarshalBinary(buf)
assert.Nil(t, err)
for i := 0; i < len(keyring.Decoded); i++ {
buf, err := hex.DecodeString(keys[i])
assert.Nil(t, err)

assert.Equal(t, "ARIA", keyring[0].Cipher)
assert.Equal(t, "LEA", keyring[1].Cipher)
assert.Equal(t, "SEED", keyring[2].Cipher)
assert.Equal(t, "SEED", keyring[3].Cipher)
assert.Equal(t, "LEA", keyring[4].Cipher)

assert.Equal(t, byte(251), keyring[0].Operand)
assert.Equal(t, byte(87), keyring[1].Operand)
assert.Equal(t, byte(192), keyring[2].Operand)
assert.Equal(t, byte(39), keyring[3].Operand)
assert.Equal(t, byte(234), keyring[4].Operand)
}
err = keyring.Decoded[i].UnmarshalBinary(buf)
assert.Nil(t, err)
}

func TestKeyringRegenerate(t *testing.T) {
buf, err := hex.DecodeString(allKeys)
assert.Nil(t, err)
assert.Equal(t, "ARIA", keyring.Decoded[0].Cipher)
assert.Equal(t, "LEA", keyring.Decoded[1].Cipher)
assert.Equal(t, "SEED", keyring.Decoded[2].Cipher)
assert.Equal(t, "SEED", keyring.Decoded[3].Cipher)
assert.Equal(t, "LEA", keyring.Decoded[4].Cipher)

assert.Equal(t, byte(251), keyring.Decoded[0].Operand)
assert.Equal(t, byte(87), keyring.Decoded[1].Operand)
assert.Equal(t, byte(192), keyring.Decoded[2].Operand)
assert.Equal(t, byte(39), keyring.Decoded[3].Operand)
assert.Equal(t, byte(234), keyring.Decoded[4].Operand)
}

func TestKeyring_Regenerate(t *testing.T) {
var keyring Keyring
err = keyring.UnmarshalBinary(buf)
assert.Nil(t, err)
for i := 0; i < len(keyring.Decoded); i++ {
buf, err := hex.DecodeString(keys[i])
assert.Nil(t, err)

err = keyring.Regenerate()
assert.Nil(t, err)
err = keyring.Decoded[i].UnmarshalBinary(buf)
assert.Nil(t, err)
}

assert.Equal(t, "ARIA", keyring[0].Cipher)
assert.Equal(t, "LEA", keyring[1].Cipher)
assert.Equal(t, "SEED", keyring[2].Cipher)
assert.Equal(t, "SEED", keyring[3].Cipher)
assert.Equal(t, "LEA", keyring[4].Cipher)

assert.Equal(t, byte(251), keyring[0].Operand)
assert.Equal(t, byte(87), keyring[1].Operand)
assert.Equal(t, byte(192), keyring[2].Operand)
assert.Equal(t, byte(39), keyring[3].Operand)
assert.Equal(t, byte(234), keyring[4].Operand)
err := keyring.Regenerate()
assert.Nil(t, err)
}

0 comments on commit 583ba54

Please sign in to comment.