Skip to content

Commit

Permalink
drop unsafe in TEA encryption
Browse files Browse the repository at this point in the history
name         old time/op    new time/op    delta
TEAen/16-8      245ns ± 0%     237ns ± 0%  -3.29%  (p=0.008 n=5+5)
TEAen/256-8    1.73µs ± 0%    1.69µs ± 0%  -2.77%  (p=0.008 n=5+5)
TEAen/4K-8     24.9µs ± 0%    25.0µs ± 1%    ~     (p=0.690 n=5+5)
TEAen/32K-8     198µs ± 0%     199µs ± 1%  +0.73%  (p=0.008 n=5+5)
TEAde/16-8      215ns ± 1%     196ns ± 1%  -8.93%  (p=0.008 n=5+5)
TEAde/256-8    1.70µs ± 0%    1.56µs ± 0%  -8.08%  (p=0.008 n=5+5)
TEAde/4K-8     24.9µs ± 1%    23.5µs ± 0%  -5.50%  (p=0.008 n=5+5)
TEAde/32K-8     198µs ± 0%     187µs ± 1%  -5.34%  (p=0.008 n=5+5)

name         old speed      new speed      delta
TEAen/16-8   65.4MB/s ± 0%  67.6MB/s ± 1%  +3.39%  (p=0.008 n=5+5)
TEAen/256-8   148MB/s ± 0%   152MB/s ± 0%  +2.84%  (p=0.008 n=5+5)
TEAen/4K-8    164MB/s ± 0%   164MB/s ± 1%    ~     (p=0.587 n=5+5)
TEAen/32K-8   166MB/s ± 0%   165MB/s ± 1%  -0.72%  (p=0.008 n=5+5)
TEAde/16-8    149MB/s ± 1%   163MB/s ± 1%  +9.81%  (p=0.008 n=5+5)
TEAde/256-8   160MB/s ± 0%   174MB/s ± 0%  +8.80%  (p=0.008 n=5+5)
TEAde/4K-8    165MB/s ± 1%   175MB/s ± 0%  +5.81%  (p=0.008 n=5+5)
TEAde/32K-8   166MB/s ± 0%   175MB/s ± 1%  +5.70%  (p=0.008 n=5+5)
  • Loading branch information
wdvxdr1123 committed Nov 16, 2021
1 parent 511e8c4 commit a694870
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 74 deletions.
109 changes: 52 additions & 57 deletions binary/tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,82 +3,47 @@ package binary
import (
"encoding/binary"
"math/rand"
"reflect"
"unsafe"

"github.com/segmentio/asm/bswap"

"github.com/Mrs4s/MiraiGo/internal/cpu"
)

func xorQ(a, b []byte, c []byte) { // MAGIC
*(*uint64)(unsafe.Pointer(&c[0])) =
*(*uint64)(unsafe.Pointer(&a[0])) ^ *(*uint64)(unsafe.Pointer(&b[0]))
}

type TEA [4]uint32

// Encrypt tea 加密
// http://bbs.chinaunix.net/thread-583468-1-1.html
// 感谢xichen大佬对TEA的解释
func (t *TEA) Encrypt(src []byte) (dst []byte) {
func (t TEA) Encrypt(src []byte) (dst []byte) {
lens := len(src)
fill := 10 - (lens+1)%8
dst = make([]byte, fill+lens+7)
_, _ = rand.Read(dst[0:fill])
dst[0] = byte(fill-3) | 0xF8 // 存储pad长度
copy(dst[fill:], src)
if cpu.LittleEndian {
bswap.Swap64(dst)
}

var iv1, iv2, holder int64
var blocks []int64
dstHeader := (*reflect.SliceHeader)(unsafe.Pointer(&dst))
blocksHeader := (*reflect.SliceHeader)(unsafe.Pointer(&blocks))
blocksHeader.Data = dstHeader.Data
blocksHeader.Len = dstHeader.Len / 8
blocksHeader.Cap = blocksHeader.Len
for i, block := range blocks {
var iv1, iv2, holder uint64
for i := 0; i < len(dst); i += 8 {
block := binary.BigEndian.Uint64(dst[i:])
holder = block ^ iv1
iv1 = t.encode(holder)
iv1 = iv1 ^ iv2
iv2 = holder
blocks[i] = iv1
}
if cpu.LittleEndian {
bswap.Swap64(dst)
binary.BigEndian.PutUint64(dst[i:], iv1)
}

return dst
}

func (t *TEA) Decrypt(data []byte) []byte {
func (t TEA) Decrypt(data []byte) []byte {
if len(data) < 16 || len(data)%8 != 0 {
return nil
}
dst := make([]byte, len(data))
copy(dst, data)
if cpu.LittleEndian {
bswap.Swap64(dst)
}

var iv1, iv2, holder, tmp int64
var blocks []int64
dstHeader := (*reflect.SliceHeader)(unsafe.Pointer(&dst))
blocksHeader := (*reflect.SliceHeader)(unsafe.Pointer(&blocks))
blocksHeader.Data = dstHeader.Data
blocksHeader.Len = dstHeader.Len / 8
blocksHeader.Cap = blocksHeader.Len
for i, block := range blocks {
var iv1, iv2, holder, tmp uint64
for i := 0; i < len(dst); i += 8 {
block := binary.BigEndian.Uint64(data[i:])
tmp = t.decode(block ^ iv2)
iv2 = tmp
holder = tmp ^ iv1
iv1 = block
blocks[i] = holder
}

if cpu.LittleEndian {
bswap.Swap64(dst)
binary.BigEndian.PutUint64(dst[i:], holder)
}
return dst[dst[0]&7+3 : len(data)-7]
}
Expand All @@ -103,32 +68,62 @@ var sumTable = [0x10]uint32{
}

//go:nosplit
func (t *TEA) encode(n int64) int64 {
func (t *TEA) encode(n uint64) uint64 {
v0, v1 := uint32(n>>32), uint32(n)
for i := 0; i < 0x10; i++ {
v0 += ((v1 << 4) + t[0]) ^ (v1 + sumTable[i]) ^ ((v1 >> 5) + t[1])
v1 += ((v0 << 4) + t[2]) ^ (v0 + sumTable[i]) ^ ((v0 >> 5) + t[3])
}
return int64(v0)<<32 | int64(v1)
return uint64(v0)<<32 | uint64(v1)
}

// 每次8字节
//go:nosplit
func (t *TEA) decode(n int64) int64 {
func (t *TEA) decode(n uint64) uint64 {
v0, v1 := uint32(n>>32), uint32(n)
for i := 0xf; i >= 0; i-- {
v1 -= ((v0 << 4) + t[2]) ^ (v0 + sumTable[i]) ^ ((v0 >> 5) + t[3])
v0 -= ((v1 << 4) + t[0]) ^ (v1 + sumTable[i]) ^ ((v1 >> 5) + t[1])
}
return int64(v0)<<32 | int64(v1)
t0, t1, t2, t3 := t[0], t[1], t[2], t[3]

v1 -= ((v0 << 4) + t2) ^ (v0 + 0xe3779b90) ^ ((v0 >> 5) + t3)
v0 -= ((v1 << 4) + t0) ^ (v1 + 0xe3779b90) ^ ((v1 >> 5) + t1)
v1 -= ((v0 << 4) + t2) ^ (v0 + 0x454021d7) ^ ((v0 >> 5) + t3)
v0 -= ((v1 << 4) + t0) ^ (v1 + 0x454021d7) ^ ((v1 >> 5) + t1)
v1 -= ((v0 << 4) + t2) ^ (v0 + 0xa708a81e) ^ ((v0 >> 5) + t3)
v0 -= ((v1 << 4) + t0) ^ (v1 + 0xa708a81e) ^ ((v1 >> 5) + t1)
v1 -= ((v0 << 4) + t2) ^ (v0 + 0x8d12e65) ^ ((v0 >> 5) + t3)
v0 -= ((v1 << 4) + t0) ^ (v1 + 0x8d12e65) ^ ((v1 >> 5) + t1)
v1 -= ((v0 << 4) + t2) ^ (v0 + 0x6a99b4ac) ^ ((v0 >> 5) + t3)
v0 -= ((v1 << 4) + t0) ^ (v1 + 0x6a99b4ac) ^ ((v1 >> 5) + t1)
v1 -= ((v0 << 4) + t2) ^ (v0 + 0xcc623af3) ^ ((v0 >> 5) + t3)
v0 -= ((v1 << 4) + t0) ^ (v1 + 0xcc623af3) ^ ((v1 >> 5) + t1)
v1 -= ((v0 << 4) + t2) ^ (v0 + 0x2e2ac13a) ^ ((v0 >> 5) + t3)
v0 -= ((v1 << 4) + t0) ^ (v1 + 0x2e2ac13a) ^ ((v1 >> 5) + t1)
v1 -= ((v0 << 4) + t2) ^ (v0 + 0x8ff34781) ^ ((v0 >> 5) + t3)
v0 -= ((v1 << 4) + t0) ^ (v1 + 0x8ff34781) ^ ((v1 >> 5) + t1)
v1 -= ((v0 << 4) + t2) ^ (v0 + 0xf1bbcdc8) ^ ((v0 >> 5) + t3)
v0 -= ((v1 << 4) + t0) ^ (v1 + 0xf1bbcdc8) ^ ((v1 >> 5) + t1)
v1 -= ((v0 << 4) + t2) ^ (v0 + 0x5384540f) ^ ((v0 >> 5) + t3)
v0 -= ((v1 << 4) + t0) ^ (v1 + 0x5384540f) ^ ((v1 >> 5) + t1)
v1 -= ((v0 << 4) + t2) ^ (v0 + 0xb54cda56) ^ ((v0 >> 5) + t3)
v0 -= ((v1 << 4) + t0) ^ (v1 + 0xb54cda56) ^ ((v1 >> 5) + t1)
v1 -= ((v0 << 4) + t2) ^ (v0 + 0x1715609d) ^ ((v0 >> 5) + t3)
v0 -= ((v1 << 4) + t0) ^ (v1 + 0x1715609d) ^ ((v1 >> 5) + t1)
v1 -= ((v0 << 4) + t2) ^ (v0 + 0x78dde6e4) ^ ((v0 >> 5) + t3)
v0 -= ((v1 << 4) + t0) ^ (v1 + 0x78dde6e4) ^ ((v1 >> 5) + t1)
v1 -= ((v0 << 4) + t2) ^ (v0 + 0xdaa66d2b) ^ ((v0 >> 5) + t3)
v0 -= ((v1 << 4) + t0) ^ (v1 + 0xdaa66d2b) ^ ((v1 >> 5) + t1)
v1 -= ((v0 << 4) + t2) ^ (v0 + 0x3c6ef372) ^ ((v0 >> 5) + t3)
v0 -= ((v1 << 4) + t0) ^ (v1 + 0x3c6ef372) ^ ((v1 >> 5) + t1)
v1 -= ((v0 << 4) + t2) ^ (v0 + 0x9e3779b9) ^ ((v0 >> 5) + t3)
v0 -= ((v1 << 4) + t0) ^ (v1 + 0x9e3779b9) ^ ((v1 >> 5) + t1)

return uint64(v0)<<32 | uint64(v1)
}

//go:nosplit
func NewTeaCipher(key []byte) *TEA {
func NewTeaCipher(key []byte) (t TEA) {
if len(key) != 16 {
return nil
return TEA{}
}
t := new(TEA)
t[3] = binary.BigEndian.Uint32(key[12:])
t[2] = binary.BigEndian.Uint32(key[8:])
t[1] = binary.BigEndian.Uint32(key[4:])
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.16
require (
github.com/klauspost/compress v1.13.6
github.com/pkg/errors v0.9.1
github.com/segmentio/asm v1.1.0
github.com/stretchr/testify v1.3.0
github.com/tidwall/gjson v1.11.0
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@ github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc=
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/klauspost/cpuid/v2 v2.0.6 h1:dQ5ueTiftKxp0gyjKSx5+8BtPWkyQbd95m8Gys/RarI=
github.com/klauspost/cpuid/v2 v2.0.6/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/segmentio/asm v1.1.0 h1:fkVr8k5J4sKoFjTGVD6r1yKvDKqmvrEh3K7iyVxgBs8=
github.com/segmentio/asm v1.1.0/go.mod h1:4EUJGaKsB8ImLUwOGORVsNd9vTRDeh44JGsY4aKp5I4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand Down
6 changes: 0 additions & 6 deletions internal/cpu/big_endian.go

This file was deleted.

6 changes: 0 additions & 6 deletions internal/cpu/little_endian.go

This file was deleted.

0 comments on commit a694870

Please sign in to comment.