1
0
mirror of https://github.com/fumiama/WireGold.git synced 2026-06-27 06:10:26 +08:00

fix tea in big endian

This commit is contained in:
源文雨
2022-04-12 19:40:40 +08:00
parent 58f950109f
commit 68b2e8cac5
9 changed files with 46 additions and 41 deletions

View File

@@ -1,14 +1,8 @@
package link
import (
"unsafe"
tea "github.com/fumiama/gofastTEA"
)
// Encode 使用 TEA 加密
func (l *Link) Encode(b []byte) (eb []byte) {
if b == nil {
func (l *Link) Encode(teatype uint8, b []byte) (eb []byte) {
if b == nil || teatype >= 16 {
return
}
if l.key == nil {
@@ -16,14 +10,14 @@ func (l *Link) Encode(b []byte) (eb []byte) {
} else {
// 在此处填写加密逻辑密钥是l.key输入是b输出是eb
// 不用写return直接赋值给eb即可
eb = (*tea.TEA)(unsafe.Pointer(l.key)).Encrypt(b)
eb = l.key[teatype].Encrypt(b)
}
return
}
// Decode 使用 TEA 解密
func (l *Link) Decode(b []byte) (db []byte) {
if b == nil {
func (l *Link) Decode(teatype uint8, b []byte) (db []byte) {
if b == nil || teatype >= 16 {
return
}
if l.key == nil {
@@ -31,7 +25,7 @@ func (l *Link) Decode(b []byte) (db []byte) {
} else {
// 在此处填写解密逻辑密钥是l.key输入是b输出是db
// 不用写return直接赋值给db即可
db = (*tea.TEA)(unsafe.Pointer(l.key)).Decrypt(b)
db = l.key[teatype].Decrypt(b)
}
return
}

View File

@@ -7,6 +7,7 @@ import (
"github.com/fumiama/WireGold/gold/head"
"github.com/fumiama/WireGold/helper"
base14 "github.com/fumiama/go-base16384"
tea "github.com/fumiama/gofastTEA"
)
// Link 是本机到 peer 的连接抽象
@@ -28,7 +29,7 @@ type Link struct {
// 连接的状态,详见下方 const
status int
// 连接所用对称加密密钥
key *[32]byte
key []tea.TEA
// 本机信息
me *Me
}

View File

@@ -22,7 +22,8 @@ func (m *Me) listen() (conn *net.UDPConn, err error) {
lbf = lbf[:n]
packet := m.wait(lbf)
if packet != nil {
r := int(packet.DataSZ) - len(packet.Data)
sz := packet.TeaTypeDataSZ & 0x00ffffff
r := int(sz) - len(packet.Data)
if r > 0 {
remain, err := readAll(conn, r)
if err == nil {
@@ -38,7 +39,7 @@ func (m *Me) listen() (conn *net.UDPConn, err error) {
p.endpoint = addr
}
if p.IsToMe(packet.Dst) {
packet.Data = p.Decode(packet.Data)
packet.Data = p.Decode(uint8(packet.TeaTypeDataSZ>>24), packet.Data)
if packet.IsVaildHash() {
switch packet.Proto {
case head.ProtoHello:

View File

@@ -3,10 +3,10 @@ package link
import (
"net"
"time"
"unsafe"
"github.com/fumiama/WireGold/gold/head"
curve "github.com/fumiama/go-x25519"
tea "github.com/fumiama/gofastTEA"
"github.com/sirupsen/logrus"
)
@@ -32,7 +32,10 @@ func (m *Me) AddPeer(peerip string, pubicKey *[32]byte, endPoint string, allowed
c := curve.Get(m.privKey[:])
k, err := c.Shared(pubicKey)
if err == nil {
l.key = (*[32]byte)(*(*unsafe.Pointer)(unsafe.Pointer(&k)))
l.key = make([]tea.TEA, 16)
for i := range l.key {
l.key[i] = tea.NewTeaCipherLittleEndian(k[i : 16+i])
}
}
}
if endPoint != "" {

View File

@@ -3,6 +3,7 @@ package link
import (
"errors"
"fmt"
"math/rand"
"github.com/fumiama/WireGold/gold/head"
"github.com/sirupsen/logrus"
@@ -10,16 +11,17 @@ import (
// Write 向 peer 发包
func (l *Link) Write(p *head.Packet, istransfer bool) (n int, err error) {
teatype := uint8(rand.Intn(16))
if len(p.Data) <= int(l.me.mtu) {
if !istransfer {
p.FillHash()
p.Data = l.Encode(p.Data)
p.Data = l.Encode(teatype, p.Data)
}
return l.write(p, uint32(len(p.Data)), 0, istransfer, false)
return l.write(p, teatype, uint32(len(p.Data)), 0, istransfer, false)
}
if !istransfer {
p.FillHash()
p.Data = l.Encode(p.Data)
p.Data = l.Encode(teatype, p.Data)
}
data := p.Data
totl := uint32(len(data))
@@ -28,7 +30,7 @@ func (l *Link) Write(p *head.Packet, istransfer bool) (n int, err error) {
logrus.Debugln("[link] split frag", i, ":", i+int(l.me.mtu), ", remain:", int(totl)-i-int(l.me.mtu))
packet := *p
packet.Data = data[:int(l.me.mtu)]
cnt, err := l.write(&packet, totl, uint16(uint(i)>>3), istransfer, true)
cnt, err := l.write(&packet, teatype, totl, uint16(uint(i)>>3), istransfer, true)
n += cnt
if err != nil {
return n, err
@@ -36,7 +38,7 @@ func (l *Link) Write(p *head.Packet, istransfer bool) (n int, err error) {
data = data[int(l.me.mtu):]
}
p.Data = data
cnt, err := l.write(p, totl, uint16(uint(i)>>3), istransfer, false)
cnt, err := l.write(p, teatype, totl, uint16(uint(i)>>3), istransfer, false)
n += cnt
if err != nil {
return n, err
@@ -45,16 +47,16 @@ func (l *Link) Write(p *head.Packet, istransfer bool) (n int, err error) {
}
// write 向 peer 发一个包
func (l *Link) write(p *head.Packet, datasz uint32, offset uint16, istransfer, hasmore bool) (n int, err error) {
func (l *Link) write(p *head.Packet, teatype uint8, datasz uint32, offset uint16, istransfer, hasmore bool) (n int, err error) {
var d []byte
var cl func()
if istransfer {
if p.Flags&0x4000 == 0x4000 && len(p.Data) > int(l.me.mtu) {
return len(p.Data), errors.New("drop dont fragmnet big trans packet")
}
d, cl = p.Marshal(nil, 0, 0, false, false)
d, cl = p.Marshal(nil, teatype, 0, 0, false, false)
} else {
d, cl = p.Marshal(l.me.me, datasz, offset, false, hasmore)
d, cl = p.Marshal(l.me.me, teatype, datasz, offset, false, hasmore)
}
if d == nil {
return 0, errors.New("[link] ttl exceeded")