Skip to content

Commit

Permalink
Use optimized cipher
Browse files Browse the repository at this point in the history
  • Loading branch information
robinbraemer committed Dec 29, 2023
1 parent ac98a93 commit c480ed6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 69 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ require (

require (
buf.build/gen/go/minekube/connect/protocolbuffers/go v1.31.0-20230517110945-04c17e7d2fd9.1 // indirect
github.com/Tnze/go-mc v1.20.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/df-mc/atomic v1.10.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ dmitri.shuralyov.com/state v0.0.0-20180228185332-28bcc343414c/go.mod h1:0PRwlb0D
git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999/go.mod h1:fPE2ZNJGynbRyZ4dJvy6G277gSllfV2HJqblrnkyeyg=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Tnze/go-mc v1.20.1 h1:1Mwxyg2nvIVgRtebf6CpjMjvXLtRIng95so5hTEQtXA=
github.com/Tnze/go-mc v1.20.1/go.mod h1:c1znJQglgqa1Jjs3Dr29woN/msguiJrlNtWXhKedh2U=
github.com/agext/levenshtein v1.2.3 h1:YB2fHEn0UJagG8T1rrWknE3ZQzWM06O8AMAatNn7lmo=
github.com/agext/levenshtein v1.2.3/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
Expand Down
84 changes: 15 additions & 69 deletions pkg/edition/java/proto/codec/cipher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,92 +3,38 @@ package codec
import (
"crypto/aes"
"crypto/cipher"
"fmt"
cfb8 "github.com/Tnze/go-mc/net/CFB8"
"io"
)

func NewDecryptReader(r io.Reader, secret []byte) (reader io.Reader, err error) {
var block cipher.Block
block, err = aes.NewCipher(secret)
if err != nil {
return
}
cfb, err := newCFB8(block, secret, true)
cfb, err := newCFB8FromSecret(secret, true)
if err != nil {
return nil, err
}
return &cipher.StreamReader{
S: cfb,
R: r,
}, nil
return &cipher.StreamReader{S: cfb, R: r}, nil
}

func NewEncryptWriter(w io.Writer, secret []byte) (wr io.Writer, err error) {
var block cipher.Block
block, err = aes.NewCipher(secret)
if err != nil {
return
}
cfb, err := newCFB8(block, secret, false)
cfb, err := newCFB8FromSecret(secret, false)
if err != nil {
return nil, err
}
return &cipher.StreamWriter{
S: cfb,
W: w,
}, nil
return &cipher.StreamWriter{S: cfb, W: w}, nil
}

//
//
//
//
//
//

// AES CFB-8, version from stdlib is not working?
type cfb8 struct {
c cipher.Block
blockSize int
iv, ivReal, tmp []byte
de bool
}

func newCFB8(c cipher.Block, iv []byte, decrypt bool) (cipher.Stream, error) {
const ivLen = 16
if len(iv) != ivLen {
return nil, fmt.Errorf("invalid iv length, expected %d, got %d", ivLen, len(iv))
func newCFB8FromSecret(secret []byte, decrypt bool) (cipher.Stream, error) {
block, err := aes.NewCipher(secret)
if err != nil {
return nil, err
}
cp := make([]byte, 256)
copy(cp, iv)
return &cfb8{
c: c,
blockSize: c.BlockSize(),
iv: cp[:16],
ivReal: cp,
tmp: make([]byte, 16),
de: decrypt,
}, nil
return newCFB8(block, secret, decrypt), nil
}

func (cf *cfb8) XORKeyStream(dst, src []byte) {
for i := 0; i < len(src); i++ {
val := src[i]
cf.c.Encrypt(cf.tmp, cf.iv)
val = val ^ cf.tmp[0]

if cap(cf.iv) >= 17 {
cf.iv = cf.iv[1:17]
} else {
copy(cf.ivReal, cf.iv[1:])
cf.iv = cf.ivReal[:16]
}

if cf.de {
cf.iv[15] = src[i]
} else {
cf.iv[15] = val
}
dst[i] = val
func newCFB8(c cipher.Block, iv []byte, decrypt bool) cipher.Stream {
if decrypt {
return cfb8.NewCFB8Decrypt(c, iv)
} else {
return cfb8.NewCFB8Encrypt(c, iv)
}
}

0 comments on commit c480ed6

Please sign in to comment.