-
Notifications
You must be signed in to change notification settings - Fork 21
/
anubis.go
242 lines (195 loc) · 5.41 KB
/
anubis.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
package anubis2
import (
"fmt"
"crypto/cipher"
"github.com/deatil/go-cryptobin/tool/alias"
)
const BlockSize = 16
type KeySizeError int
func (k KeySizeError) Error() string {
return fmt.Sprintf("cryptobin/anubis: invalid key size %d", int(k))
}
type anubisCipher struct {
R int32
roundKeyEnc [19][4]uint32
roundKeyDec [19][4]uint32
}
// NewCipher creates and returns a new cipher.Block.
func NewCipher(key []byte) (cipher.Block, error) {
keylen := len(key)
// Valid sizes (in bytes) are 16, 20, 24, 28, 32, 36, and 40.
if ((keylen & 3) > 0 || (keylen < 16) || (keylen > 40)) {
return nil, KeySizeError(len(key))
}
c := new(anubisCipher)
c.expandKey(key)
return c, nil
}
func (this *anubisCipher) BlockSize() int {
return BlockSize
}
func (this *anubisCipher) Encrypt(dst, src []byte) {
if len(src) < BlockSize {
panic("cryptobin/anubis: input not full block")
}
if len(dst) < BlockSize {
panic("cryptobin/anubis: output not full block")
}
if alias.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
panic("cryptobin/anubis: invalid buffer overlap")
}
this.crypt(dst, src, this.roundKeyEnc)
}
func (this *anubisCipher) Decrypt(dst, src []byte) {
if len(src) < BlockSize {
panic("cryptobin/anubis: input not full block")
}
if len(dst) < BlockSize {
panic("cryptobin/anubis: output not full block")
}
if alias.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
panic("cryptobin/anubis: invalid buffer overlap")
}
this.crypt(dst, src, this.roundKeyDec)
}
func (this *anubisCipher) crypt(ciphertext []byte, plaintext []byte, roundKey [19][4]uint32) {
var i, j, r int32
var state, inter [4]uint32
var ss [][]byte
R := this.R
pt := bytesToUint32s(plaintext)
/*
* map plaintext block to cipher state (mu)
* and add initial round key (sigma[K^0]):
*/
for i = 0; i < 4; i++ {
state[i] = pt[i] ^ roundKey[0][i]
}
/*
* R - 1 full rounds:
*/
for r = 1; r < R; r++ {
for j = 0; j < 4; j++ {
ss = uint32sToByteArray(state[:])
inter[j] =
T0[ss[0][j]] ^
T1[ss[1][j]] ^
T2[ss[2][j]] ^
T3[ss[3][j]] ^
roundKey[r][j]
}
copy(state[:], inter[:])
}
/*
* last round:
*/
ss = uint32sToByteArray(state[:])
for j = 0; j < 4; j++ {
inter[j] =
(T0[ss[0][j]] & states[0]) ^
(T1[ss[1][j]] & states[1]) ^
(T2[ss[2][j]] & states[2]) ^
(T3[ss[3][j]] & states[3]) ^
roundKey[R][j]
}
/*
* map cipher state to ciphertext block (mu^{-1}):
*/
ct := uint32sToBytes(inter[:])
copy(ciphertext, ct)
}
func (this *anubisCipher) expandKey(key []byte) {
var N, R, i, j, r int32
var kappa [MAX_N]uint32
var inter [MAX_N]uint32 /* initialize as all zeroes */
var v uint32
var ks [4]uint32
var kappas [][]byte
var kss, vv [4]byte
/*
* determine the N length parameter:
* (N.B. it is assumed that the key length is valid!)
*/
N = int32(len(key)) / 4
/*
* determine number of rounds from key size:
*/
R = 8 + N
this.R = R
/*
* map cipher key to initial key state (mu):
*/
keys := bytesToUint32s(key)
for i = 0; i < N; i++ {
kappa[i] = keys[i]
}
/*
* generate R + 1 round keys:
*/
for r = 0; r <= R; r++ {
kappas = uint32sToByteArray(kappa[:])
/*
* generate r-th round key K^r:
*/
for j = 0; j < 4; j++ {
ks[j] = T4[kappas[N - 1][j]]
}
for i = N - 2; i >= 0; i-- {
for j = 0; j < 4; j++ {
putu32(kss[:], ks[j])
ks[j] = T4[kappas[i][j]] ^
(T5[kss[0]] & states[0]) ^
(T5[kss[1]] & states[1]) ^
(T5[kss[2]] & states[2]) ^
(T5[kss[3]] & states[3])
}
}
copy(this.roundKeyEnc[r][:], ks[:])
/*
* compute kappa^{r+1} from kappa^r:
*/
if r == R {
break;
}
for i = 0; i < N; i++ {
var j int32 = i
inter[i] = T0[kappas[j][0]]
j--
if j < 0 {
j = N - 1
}
inter[i] ^= T1[kappas[j][1]]
j--
if j < 0 {
j = N - 1
}
inter[i] ^= T2[kappas[j][2]]
j--
if j < 0 {
j = N - 1
}
inter[i] ^= T3[kappas[j][3]]
}
kappa[0] = inter[0] ^ rc[r]
for i = 1; i < N; i++ {
kappa[i] = inter[i]
}
}
/*
* generate inverse key schedule: K'^0 = K^R, K'^R = K^0, K'^r = theta(K^{R-r}):
*/
for i = 0; i < 4; i++ {
this.roundKeyDec[0][i] = this.roundKeyEnc[R][i]
this.roundKeyDec[R][i] = this.roundKeyEnc[0][i]
}
for r = 1; r < R; r++ {
for i = 0; i < 4; i++ {
v = this.roundKeyEnc[R - r][i]
putu32(vv[:], v)
this.roundKeyDec[r][i] = T0[byte(T4[vv[0]])] ^
T1[byte(T4[vv[1]])] ^
T2[byte(T4[vv[2]])] ^
T3[byte(T4[vv[3]])]
}
}
}