-
Notifications
You must be signed in to change notification settings - Fork 21
/
kalyna128_128.go
192 lines (145 loc) · 4.07 KB
/
kalyna128_128.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
package kalyna
import (
"crypto/cipher"
"github.com/deatil/go-cryptobin/tool/alias"
)
const BlockSize128_128 = 16
type kalynaCipher128_128 struct {
erk [24]uint64
drk [24]uint64
}
// NewCipher128_128 creates and returns a new cipher.Block.
func NewCipher128_128(key []byte) (cipher.Block, error) {
keylen := len(key)
if keylen != 16 {
return nil, KeySizeError(keylen)
}
c := new(kalynaCipher128_128)
c.expandKey(key)
return c, nil
}
func (this *kalynaCipher128_128) BlockSize() int {
return BlockSize128_128
}
func (this *kalynaCipher128_128) Encrypt(dst, src []byte) {
if len(src) < BlockSize128_128 {
panic("cryptobin/kalyna: input not full block")
}
if len(dst) < BlockSize128_128 {
panic("cryptobin/kalyna: output not full block")
}
if alias.InexactOverlap(dst[:BlockSize128_128], src[:BlockSize128_128]) {
panic("cryptobin/kalyna: invalid buffer overlap")
}
this.encrypt(dst, src)
}
func (this *kalynaCipher128_128) Decrypt(dst, src []byte) {
if len(src) < BlockSize128_128 {
panic("cryptobin/kalyna: input not full block")
}
if len(dst) < BlockSize128_128 {
panic("cryptobin/kalyna: output not full block")
}
if alias.InexactOverlap(dst[:BlockSize128_128], src[:BlockSize128_128]) {
panic("cryptobin/kalyna: invalid buffer overlap")
}
this.decrypt(dst, src)
}
func (this *kalynaCipher128_128) encrypt(out []byte, in []byte) {
var t1, t2 []uint64
t1, t2 = make([]uint64, 2), make([]uint64, 2)
ins := bytesToUint64s(in)
rk := this.erk[:]
addkey128(ins, t1, rk)
var i int
for i = 2; i < 18; i += 4 {
G128(t1, t2, rk[i :]) // i + 1
G128(t2, t1, rk[i + 2:]) // i + 2
}
G128(t1, t2, rk[18:]) // 9
GL128(t2, t1, rk[20:]) // 10
ct := uint64sToBytes(t1)
copy(out, ct)
}
func (this *kalynaCipher128_128) decrypt(out []byte, in []byte) {
var t1, t2 []uint64
t1, t2 = make([]uint64, 2), make([]uint64, 2)
ins := bytesToUint64s(in)
rk := this.drk[:]
subkey128(ins, t1, rk[20:])
IMC128(t1)
var i int
for i = 18; i > 2; i -= 4 {
IG128(t1, t2, rk[i :])
IG128(t2, t1, rk[i - 2:])
}
IG128(t1, t2, rk[2:])
IGL128(t2, t1, rk[0:])
pt := uint64sToBytes(t1)
copy(out, pt)
}
func (this *kalynaCipher128_128) expandKey(key []byte) {
var ks, ksc, t1, t2, k, kswapped []uint64
ks = make([]uint64, 2)
ksc = make([]uint64, 2)
t1 = make([]uint64, 2)
t2 = make([]uint64, 2)
k = make([]uint64, 2)
kswapped = make([]uint64, 2)
keys := bytesToUint64s(key)
t1[0] = (128 + 128 + 64) / 64
addkey128(t1, t2, keys)
G128(t2, t1, keys)
GL128(t1, t2, keys)
G0128(t2, ks)
var constant uint64 = 0x0001000100010001
rk := make([]uint64, 24)
copy(k, keys[:2])
kswapped[1] = k[0]
kswapped[0] = k[1]
// round 0
add_constant128(ks, ksc, constant)
addkey128(k, t2, ksc)
G128(t2, t1, ksc)
GL128(t1, rk[0:], ksc)
make_odd_key128(rk[0:], rk[2:])
// round 2
constant <<= 1
add_constant128(ks, ksc, constant)
addkey128(kswapped, t2, ksc)
G128(t2, t1, ksc)
GL128(t1, rk[4:], ksc)
make_odd_key128(rk[4:], rk[6:])
// round 4
constant <<= 1
add_constant128(ks, ksc, constant)
addkey128(k, t2, ksc)
G128(t2, t1, ksc)
GL128(t1, rk[8:], ksc)
make_odd_key128(rk[8:], rk[10:])
// round 6
constant <<= 1
add_constant128(ks, ksc, constant)
addkey128(kswapped, t2, ksc)
G128(t2, t1, ksc)
GL128(t1, rk[12:], ksc)
make_odd_key128(rk[12:], rk[14:])
// round 8
constant <<= 1
add_constant128(ks, ksc, constant)
addkey128(k, t2, ksc)
G128(t2, t1, ksc)
GL128(t1, rk[16:], ksc)
make_odd_key128(rk[16:], rk[18:])
// round 10
constant <<= 1
add_constant128(ks, ksc, constant)
addkey128(kswapped, t2, ksc)
G128(t2, t1, ksc)
GL128(t1, rk[20:], ksc)
copy(this.erk[:], rk)
for i := 18; i > 0; i -= 2 {
IMC128(rk[i:])
}
copy(this.drk[:], rk)
}