-
Notifications
You must be signed in to change notification settings - Fork 1
/
xoodoo.go
204 lines (183 loc) · 5.96 KB
/
xoodoo.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package xoodoo
import (
"encoding/binary"
"fmt"
"math/bits"
)
const (
// MaxRounds is current ceiling on how many iterations of Xoodoo can be done in the permutation
// function
MaxRounds = 12
// StateSizeBytes describes the Xoodoo object in term of the number of bytes it is made up of
StateSizeBytes = 48
// StateSizeBytes describes the Xoodoo object in term of the number of 32-bit unsigned ints it is made up of
StateSizeWords = 12
)
var (
// RoundConstants is the sequence of 32-bit constants applied in each round of Xoodoo
RoundConstants = [StateSizeWords]uint32{
0x00000058,
0x00000038,
0x000003C0,
0x000000D0,
0x00000120,
0x00000014,
0x00000060,
0x0000002C,
0x00000380,
0x000000F0,
0x000001A0,
0x00000012,
}
)
type XooDooState [StateSizeWords]uint32
type XooDoo struct {
State XooDooState
rounds int
}
func XorState(a, b XooDooState) XooDooState {
return XooDooState{
a[0] ^ b[0],
a[1] ^ b[1],
a[2] ^ b[2],
a[3] ^ b[3],
a[4] ^ b[4],
a[5] ^ b[5],
a[6] ^ b[6],
a[7] ^ b[7],
a[8] ^ b[8],
a[9] ^ b[9],
a[10] ^ b[10],
a[11] ^ b[11],
}
}
func (xds *XooDooState) XorStateBytes(in []byte) {
xds[0] ^= (binary.LittleEndian.Uint32(in[0:4]))
xds[1] ^= (binary.LittleEndian.Uint32(in[4:8]))
xds[2] ^= (binary.LittleEndian.Uint32(in[8:12]))
xds[3] ^= (binary.LittleEndian.Uint32(in[12:16]))
xds[4] ^= (binary.LittleEndian.Uint32(in[16:20]))
xds[5] ^= (binary.LittleEndian.Uint32(in[20:24]))
xds[6] ^= (binary.LittleEndian.Uint32(in[24:28]))
xds[7] ^= (binary.LittleEndian.Uint32(in[28:32]))
xds[8] ^= (binary.LittleEndian.Uint32(in[32:36]))
xds[9] ^= (binary.LittleEndian.Uint32(in[36:40]))
xds[10] ^= (binary.LittleEndian.Uint32(in[40:44]))
xds[11] ^= (binary.LittleEndian.Uint32(in[44:48]))
}
func (xds *XooDooState) XorByte(x byte, offset int) error {
if offset < 0 || offset >= StateSizeBytes {
return fmt.Errorf("xor byte offset out of range:%d", offset)
}
xInt := uint32(x) << (8 * (offset % 4))
xds[(offset >> 2)] ^= xInt
return nil
}
func (xds *XooDoo) XorExtractBytes(x []byte) ([]byte, error) {
size := len(x)
if size <= 0 || size > StateSizeBytes {
return nil, fmt.Errorf("xor and extract bytes size out of range:%d", size)
}
out := make([]byte, size)
stateBytes := xds.Bytes()
for i := 0; i < size; i++ {
out[i] = stateBytes[i] ^ x[i]
}
return out, nil
}
func (xds *XooDooState) UnmarshalBinary(data []byte) error {
if len(data) != StateSizeBytes {
return fmt.Errorf("input data (%d bytes) != xoodoo state size (%d bytes)", len(data), StateSizeBytes)
}
xds[0] = (binary.LittleEndian.Uint32(data[0:4]))
xds[1] = (binary.LittleEndian.Uint32(data[4:8]))
xds[2] = (binary.LittleEndian.Uint32(data[8:12]))
xds[3] = (binary.LittleEndian.Uint32(data[12:16]))
xds[4] = (binary.LittleEndian.Uint32(data[16:20]))
xds[5] = (binary.LittleEndian.Uint32(data[20:24]))
xds[6] = (binary.LittleEndian.Uint32(data[24:28]))
xds[7] = (binary.LittleEndian.Uint32(data[28:32]))
xds[8] = (binary.LittleEndian.Uint32(data[32:36]))
xds[9] = (binary.LittleEndian.Uint32(data[36:40]))
xds[10] = (binary.LittleEndian.Uint32(data[40:44]))
xds[11] = (binary.LittleEndian.Uint32(data[44:48]))
return nil
}
func (xds *XooDooState) MarshalBinary() (data []byte, err error) {
data = make([]byte, StateSizeBytes)
binary.LittleEndian.PutUint32(data[0:4], xds[0])
binary.LittleEndian.PutUint32(data[4:8], xds[1])
binary.LittleEndian.PutUint32(data[8:12], xds[2])
binary.LittleEndian.PutUint32(data[12:16], xds[3])
binary.LittleEndian.PutUint32(data[16:20], xds[4])
binary.LittleEndian.PutUint32(data[20:24], xds[5])
binary.LittleEndian.PutUint32(data[24:28], xds[6])
binary.LittleEndian.PutUint32(data[28:32], xds[7])
binary.LittleEndian.PutUint32(data[32:36], xds[8])
binary.LittleEndian.PutUint32(data[36:40], xds[9])
binary.LittleEndian.PutUint32(data[40:44], xds[10])
binary.LittleEndian.PutUint32(data[44:48], xds[11])
return data, nil
}
func NewXooDoo(rounds int, state [StateSizeBytes]byte) (*XooDoo, error) {
var new XooDoo
new.rounds = rounds
if rounds > len(RoundConstants) {
return nil, fmt.Errorf("invalid number of rounds: %d", rounds)
}
err := new.State.UnmarshalBinary(state[:])
if err != nil {
return nil, fmt.Errorf("invalid initial state")
}
return &new, nil
}
func (xd *XooDoo) Bytes() []byte {
buf, _ := xd.State.MarshalBinary()
return buf
}
// Permutation executes an optimized implementation of Xoodoo permutation over the provided
// xoodoo state
func (xd *XooDoo) Permutation() {
xds := xd.State
var tmp XooDooState
var P, E [4]uint32
for i := MaxRounds - xd.rounds; i < MaxRounds; i++ {
P = [4]uint32{
xds[0] ^ xds[4] ^ xds[8],
xds[1] ^ xds[5] ^ xds[9],
xds[2] ^ xds[6] ^ xds[10],
xds[3] ^ xds[7] ^ xds[11],
}
E = [4]uint32{
bits.RotateLeft32(P[3], 5) ^ bits.RotateLeft32(P[3], 14),
bits.RotateLeft32(P[0], 5) ^ bits.RotateLeft32(P[0], 14),
bits.RotateLeft32(P[1], 5) ^ bits.RotateLeft32(P[1], 14),
bits.RotateLeft32(P[2], 5) ^ bits.RotateLeft32(P[2], 14),
}
tmp[0] = E[0] ^ xds[0] ^ RoundConstants[i]
tmp[1] = E[1] ^ xds[1]
tmp[2] = E[2] ^ xds[2]
tmp[3] = E[3] ^ xds[3]
tmp[4] = E[3] ^ xds[7]
tmp[5] = E[0] ^ xds[4]
tmp[6] = E[1] ^ xds[5]
tmp[7] = E[2] ^ xds[6]
tmp[8] = bits.RotateLeft32(E[0]^xds[8], 11)
tmp[9] = bits.RotateLeft32(E[1]^xds[9], 11)
tmp[10] = bits.RotateLeft32(E[2]^xds[10], 11)
tmp[11] = bits.RotateLeft32(E[3]^xds[11], 11)
xds[0] = (^tmp[4] & tmp[8]) ^ tmp[0]
xds[1] = (^tmp[5] & tmp[9]) ^ tmp[1]
xds[2] = (^tmp[6] & tmp[10]) ^ tmp[2]
xds[3] = (^tmp[7] & tmp[11]) ^ tmp[3]
xds[4] = bits.RotateLeft32((^tmp[8]&tmp[0])^tmp[4], 1)
xds[5] = bits.RotateLeft32((^tmp[9]&tmp[1])^tmp[5], 1)
xds[6] = bits.RotateLeft32((^tmp[10]&tmp[2])^tmp[6], 1)
xds[7] = bits.RotateLeft32((^tmp[11]&tmp[3])^tmp[7], 1)
xds[8] = bits.RotateLeft32((^tmp[2]&tmp[6])^tmp[10], 8)
xds[9] = bits.RotateLeft32((^tmp[3]&tmp[7])^tmp[11], 8)
xds[10] = bits.RotateLeft32((^tmp[0]&tmp[4])^tmp[8], 8)
xds[11] = bits.RotateLeft32((^tmp[1]&tmp[5])^tmp[9], 8)
}
xd.State = xds
}