-
Notifications
You must be signed in to change notification settings - Fork 21
/
mars.go
323 lines (268 loc) · 8.19 KB
/
mars.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package mars
import (
"strconv"
"crypto/cipher"
"github.com/deatil/go-cryptobin/tool/alias"
)
const BlockSize = 16
type KeySizeError int
func (k KeySizeError) Error() string {
return "cryptobin/mars: invalid key size " + strconv.Itoa(int(k))
}
type marsCipher struct {
key [40]uint32
}
// NewCipher creates and returns a new cipher.Block.
func NewCipher(key []byte) (cipher.Block, error) {
keyLen := len(key)
// MARS has a variable key size from 128 to 448 bits in 32-bit increments
if keyLen < 16 || keyLen > 56 || (keyLen % 4) != 0 {
return nil, KeySizeError(keyLen)
}
c := new(marsCipher)
c.expandKey(key)
return c, nil
}
func (this *marsCipher) BlockSize() int {
return BlockSize
}
func (this *marsCipher) Encrypt(dst, src []byte) {
if len(src) < BlockSize {
panic("cryptobin/mars: input not full block")
}
if len(dst) < BlockSize {
panic("cryptobin/mars: output not full block")
}
if alias.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
panic("cryptobin/mars: invalid buffer overlap")
}
this.encryptBlock(dst, src)
}
func (this *marsCipher) Decrypt(dst, src []byte) {
if len(src) < BlockSize {
panic("cryptobin/mars: input not full block")
}
if len(dst) < BlockSize {
panic("cryptobin/mars: output not full block")
}
if alias.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
panic("cryptobin/mars: invalid buffer overlap")
}
this.decryptBlock(dst, src)
}
func (this *marsCipher) expandKey(key []byte) {
var i uint
var j uint
var n uint
var m uint32
var p uint32
var r uint32
var w uint32
var t1 uint32
var t2 uint32
var t [15]uint32
keyLen := len(key)
// Determine the number of 32-bit words in the key
n = uint(keyLen) / 4
// Initialize T with the original key data
for i = 0; i < n; i++ {
t[i] = bytesToUint32(key[4 * i:])
}
// Let T[n] = n
t[i] = uint32(n)
i++
// Let T[n+1 ... 14] = 0
for i < 15 {
t[i] = 0
i++
}
// Compute 10 words of K in each iteration
for j = 0; j < 4; j++ {
// Save T[i-2] and T[i-1]
t1 = t[13]
t2 = t[14]
// Linear key-word expansion
for i = 0; i < 15; i++ {
t1 ^= t[(i + 8) % 15]
t[i] ^= ROL32(t1, 3) ^ uint32(4 * i + j)
t1 = t2
t2 = t[i]
}
// Repeat 4 rounds of stirring
for n = 0; n < 4; n++ {
// Save T[i-1]
t1 = t[14]
//S-box based stirring of key-words
for i = 0; i < 15; i++ {
t1 = t[i] + S(t1)
t[i] = ROL32(t1, 9)
t1 = t[i]
}
}
// Store next 10 key words into K
for i = 0; i < 10; i++ {
this.key[10 * j + i] = t[(4 * i) % 15]
}
}
// Modifying multiplication key-words
for i = 5; i < 37; i += 2 {
// Let j be the least two bits of K[i]
j = uint(this.key[i] & 0x03)
// Let w = K[i] with both of the lowest two bits set to 1
w = uint32(this.key[i] | 0x03)
//Generate the word mask M
MASK_GEN(&m, w)
//Let r be the least five bits of K[i-1]
r = this.key[i - 1] & 0x1F
//Calculate p = B[j] <<< r
p = ROL32(btab[j], r)
//Calculate K[i] = w xor (p and M)
this.key[i] = w ^ (p & m)
}
}
func (this *marsCipher) encryptBlock(output []byte, input []byte){
var a uint32
var b uint32
var c uint32
var d uint32
// The 16 bytes of plaintext are split into 4 words
a = bytesToUint32(input[0:])
b = bytesToUint32(input[4:])
c = bytesToUint32(input[8:])
d = bytesToUint32(input[12:])
// Compute (A,B,C,D) = (A,B,C,D) + (K[0],K[1],K[2],K[3])
a += this.key[0]
b += this.key[1]
c += this.key[2]
d += this.key[3]
//Forward mixing (8 rounds)
F_MIX(&a, &b, &c, &d)
a += d
F_MIX(&b, &c, &d, &a)
b += c
F_MIX(&c, &d, &a, &b)
F_MIX(&d, &a, &b, &c)
F_MIX(&a, &b, &c, &d)
a += d;
F_MIX(&b, &c, &d, &a);
b += c
F_MIX(&c, &d, &a, &b)
F_MIX(&d, &a, &b, &c)
//Cryptographic core (16 rounds)
CORE(&a, &b, &c, &d, this.key[4], this.key[5])
CORE(&b, &c, &d, &a, this.key[6], this.key[7])
CORE(&c, &d, &a, &b, this.key[8], this.key[9])
CORE(&d, &a, &b, &c, this.key[10], this.key[11])
CORE(&a, &b, &c, &d, this.key[12], this.key[13])
CORE(&b, &c, &d, &a, this.key[14], this.key[15])
CORE(&c, &d, &a, &b, this.key[16], this.key[17])
CORE(&d, &a, &b, &c, this.key[18], this.key[19])
CORE(&a, &d, &c, &b, this.key[20], this.key[21])
CORE(&b, &a, &d, &c, this.key[22], this.key[23])
CORE(&c, &b, &a, &d, this.key[24], this.key[25])
CORE(&d, &c, &b, &a, this.key[26], this.key[27])
CORE(&a, &d, &c, &b, this.key[28], this.key[29])
CORE(&b, &a, &d, &c, this.key[30], this.key[31])
CORE(&c, &b, &a, &d, this.key[32], this.key[33])
CORE(&d, &c, &b, &a, this.key[34], this.key[35])
// Backwards mixing (8 rounds)
B_MIX(&a, &b, &c, &d)
B_MIX(&b, &c, &d, &a)
c -= b;
B_MIX(&c, &d, &a, &b)
d -= a;
B_MIX(&d, &a, &b, &c)
B_MIX(&a, &b, &c, &d)
B_MIX(&b, &c, &d, &a)
c -= b;
B_MIX(&c, &d, &a, &b)
d -= a;
B_MIX(&d, &a, &b, &c)
//Compute (A,B,C,D) = (A,B,C,D) - (K[36],K[37],K[38],K[39])
a -= this.key[36]
b -= this.key[37]
c -= this.key[38]
d -= this.key[39]
// The 4 words of ciphertext are then written as 16 bytes
aBytes := uint32ToBytes(a)
bBytes := uint32ToBytes(b)
cBytes := uint32ToBytes(c)
dBytes := uint32ToBytes(d)
copy(output[0:], aBytes[:])
copy(output[4:], bBytes[:])
copy(output[8:], cBytes[:])
copy(output[12:], dBytes[:])
}
func (this *marsCipher) decryptBlock(output []byte, input []byte) {
var d uint32
var c uint32
var b uint32
var a uint32
// The 16 bytes of ciphertext are split into 4 words
a = bytesToUint32(input[0:])
b = bytesToUint32(input[4:])
c = bytesToUint32(input[8:])
d = bytesToUint32(input[12:])
//Compute (A,B,C,D) = (A,B,C,D) + (K[36],K[37],K[38],K[39])
a += this.key[36]
b += this.key[37]
c += this.key[38]
d += this.key[39]
//Forward mixing (8 rounds)
F_MIX(&d, &c, &b, &a)
d += a
F_MIX(&c, &b, &a, &d)
c += b
F_MIX(&b, &a, &d, &c)
F_MIX(&a, &d, &c, &b)
F_MIX(&d, &c, &b, &a)
d += a
F_MIX(&c, &b, &a, &d)
c += b
F_MIX(&b, &a, &d, &c)
F_MIX(&a, &d, &c, &b)
//Cryptographic core (16 rounds)
CORE_INV(&d, &c, &b, &a, this.key[34], this.key[35])
CORE_INV(&c, &b, &a, &d, this.key[32], this.key[33])
CORE_INV(&b, &a, &d, &c, this.key[30], this.key[31])
CORE_INV(&a, &d, &c, &b, this.key[28], this.key[29])
CORE_INV(&d, &c, &b, &a, this.key[26], this.key[27])
CORE_INV(&c, &b, &a, &d, this.key[24], this.key[25])
CORE_INV(&b, &a, &d, &c, this.key[22], this.key[23])
CORE_INV(&a, &d, &c, &b, this.key[20], this.key[21])
CORE_INV(&d, &a, &b, &c, this.key[18], this.key[19])
CORE_INV(&c, &d, &a, &b, this.key[16], this.key[17])
CORE_INV(&b, &c, &d, &a, this.key[14], this.key[15])
CORE_INV(&a, &b, &c, &d, this.key[12], this.key[13])
CORE_INV(&d, &a, &b, &c, this.key[10], this.key[11])
CORE_INV(&c, &d, &a, &b, this.key[8], this.key[9])
CORE_INV(&b, &c, &d, &a, this.key[6], this.key[7])
CORE_INV(&a, &b, &c, &d, this.key[4], this.key[5])
//Backwards mixing (8 rounds)
B_MIX(&d, &c, &b, &a)
B_MIX(&c, &b, &a, &d)
b -= c
B_MIX(&b, &a, &d, &c)
a -= d
B_MIX(&a, &d, &c, &b)
B_MIX(&d, &c, &b, &a)
B_MIX(&c, &b, &a, &d)
b -= c
B_MIX(&b, &a, &d, &c)
a -= d
B_MIX(&a, &d, &c, &b)
//Compute (A,B,C,D) = (A,B,C,D) - (K[0],K[1],K[2],K[3])
a -= this.key[0]
b -= this.key[1]
c -= this.key[2]
d -= this.key[3]
//The 4 words of plaintext are then written as 16 bytes
aBytes := uint32ToBytes(a)
bBytes := uint32ToBytes(b)
cBytes := uint32ToBytes(c)
dBytes := uint32ToBytes(d)
copy(output[0:], aBytes[:])
copy(output[4:], bBytes[:])
copy(output[8:], cBytes[:])
copy(output[12:], dBytes[:])
}