forked from burrowers/garble
/
swap.go
156 lines (143 loc) · 4.01 KB
/
swap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (c) 2020, The Garble Authors.
// See LICENSE for licensing information.
package literals
import (
"go/ast"
"go/token"
"math"
mathrand "math/rand"
ah "github.com/oxific/garble/internal/asthelper"
)
type swap struct{}
// check that the obfuscator interface is implemented
var _ obfuscator = swap{}
func getIndexType(dataLen int64) string {
switch {
case dataLen <= math.MaxUint8:
return "byte"
case dataLen <= math.MaxUint16:
return "uint16"
case dataLen <= math.MaxUint32:
return "uint32"
default:
return "uint64"
}
}
func positionsToSlice(data []int) *ast.CompositeLit {
arr := &ast.CompositeLit{
Type: &ast.ArrayType{
Len: &ast.Ellipsis{}, // Performance optimization
Elt: ast.NewIdent(getIndexType(int64(len(data)))),
},
Elts: []ast.Expr{},
}
for _, data := range data {
arr.Elts = append(arr.Elts, ah.IntLit(data))
}
return arr
}
// Generates a random even swap count based on the length of data
func generateSwapCount(obfRand *mathrand.Rand, dataLen int) int {
swapCount := dataLen
maxExtraPositions := dataLen / 2 // Limit the number of extra positions to half the data length
if maxExtraPositions > 1 {
swapCount += obfRand.Intn(maxExtraPositions)
}
if swapCount%2 != 0 { // Swap count must be even
swapCount++
}
return swapCount
}
func (swap) obfuscate(obfRand *mathrand.Rand, data []byte) *ast.BlockStmt {
swapCount := generateSwapCount(obfRand, len(data))
shiftKey := byte(obfRand.Uint32())
op := randOperator(obfRand)
positions := genRandIntSlice(obfRand, len(data), swapCount)
for i := len(positions) - 2; i >= 0; i -= 2 {
// Generate local key for xor based on random key and byte position
localKey := byte(i) + byte(positions[i]^positions[i+1]) + shiftKey
// Swap bytes from i+1 to i and encrypt using operator and local key
data[positions[i]], data[positions[i+1]] = evalOperator(op, data[positions[i+1]], localKey), evalOperator(op, data[positions[i]], localKey)
}
return ah.BlockStmt(
&ast.AssignStmt{
Lhs: []ast.Expr{ast.NewIdent("data")},
Tok: token.DEFINE,
Rhs: []ast.Expr{ah.DataToByteSlice(data)},
},
&ast.AssignStmt{
Lhs: []ast.Expr{ast.NewIdent("positions")},
Tok: token.DEFINE,
Rhs: []ast.Expr{positionsToSlice(positions)},
},
&ast.ForStmt{
Init: &ast.AssignStmt{
Lhs: []ast.Expr{ast.NewIdent("i")},
Tok: token.DEFINE,
Rhs: []ast.Expr{ah.IntLit(0)},
},
Cond: &ast.BinaryExpr{
X: ast.NewIdent("i"),
Op: token.LSS,
Y: ah.IntLit(len(positions)),
},
Post: &ast.AssignStmt{
Lhs: []ast.Expr{ast.NewIdent("i")},
Tok: token.ADD_ASSIGN,
Rhs: []ast.Expr{ah.IntLit(2)},
},
Body: ah.BlockStmt(
&ast.AssignStmt{
Lhs: []ast.Expr{ast.NewIdent("localKey")},
Tok: token.DEFINE,
Rhs: []ast.Expr{&ast.BinaryExpr{
X: &ast.BinaryExpr{
X: ah.CallExpr(ast.NewIdent("byte"), ast.NewIdent("i")),
Op: token.ADD,
Y: ah.CallExpr(ast.NewIdent("byte"), &ast.BinaryExpr{
X: ah.IndexExpr("positions", ast.NewIdent("i")),
Op: token.XOR,
Y: ah.IndexExpr("positions", &ast.BinaryExpr{
X: ast.NewIdent("i"),
Op: token.ADD,
Y: ah.IntLit(1),
}),
}),
},
Op: token.ADD,
Y: ah.IntLit(int(shiftKey)),
}},
},
&ast.AssignStmt{
Lhs: []ast.Expr{
ah.IndexExpr("data", ah.IndexExpr("positions", ast.NewIdent("i"))),
ah.IndexExpr("data", ah.IndexExpr("positions", &ast.BinaryExpr{
X: ast.NewIdent("i"),
Op: token.ADD,
Y: ah.IntLit(1),
})),
},
Tok: token.ASSIGN,
Rhs: []ast.Expr{
operatorToReversedBinaryExpr(
op,
ah.IndexExpr("data",
ah.IndexExpr("positions", &ast.BinaryExpr{
X: ast.NewIdent("i"),
Op: token.ADD,
Y: ah.IntLit(1),
}),
),
ast.NewIdent("localKey"),
),
operatorToReversedBinaryExpr(
op,
ah.IndexExpr("data", ah.IndexExpr("positions", ast.NewIdent("i"))),
ast.NewIdent("localKey"),
),
},
},
),
},
)
}