-
Notifications
You must be signed in to change notification settings - Fork 23
/
gost.go
172 lines (134 loc) · 3.87 KB
/
gost.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
package gost
import (
"strconv"
"crypto/cipher"
"github.com/deatil/go-cryptobin/tool/alias"
)
type KeySizeError int
func (k KeySizeError) Error() string {
return "cryptobin/gost: invalid key size: " + strconv.Itoa(int(k))
}
type SboxSizeError int
func (k SboxSizeError) Error() string {
return "cryptobin/gost: invalid sbox size: " + strconv.Itoa(int(k))
}
// GOST 28147-89 defines a block size of 64 bits
const BlockSize = 8
// Internal state of the GOST block cipher
type gostCipher struct {
key []uint32 // Encryption key
s [][]byte // S-box provided as parameter
k [][]byte // Expanded s-box
}
// NewCipher creates and returns a new cipher.Block. The key argument
// should be the 32 byte GOST 28147-89 key. The sbox argument should be a
// 64 byte substitution table, represented as a two-dimensional array of 8 rows
// of 16 4-bit integers.
func NewCipher(key []byte, sbox [][]byte) (cipher.Block, error) {
if len(key) != 32 {
return nil, KeySizeError(len(key))
}
if len(sbox) != 8 {
return nil, SboxSizeError(len(sbox))
}
for i := 0; i < len(sbox); i++ {
if len(sbox[i]) != 16 {
return nil, SboxSizeError(len(sbox[i]))
}
}
newKey := bytesToUint32s(key)
kbox := sboxExpansion(sbox)
c := &gostCipher{
key: newKey,
s: sbox,
k: kbox,
}
return c, nil
}
func (this *gostCipher) BlockSize() int {
return BlockSize
}
func (this *gostCipher) Encrypt(dst, src []byte) {
if len(src) < BlockSize {
panic("cryptobin/gost: input not full block")
}
if len(dst) < BlockSize {
panic("cryptobin/gost: output not full block")
}
if alias.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
panic("cryptobin/gost: invalid buffer overlap")
}
encSrc := bytesToUint32s(src)
encDst := make([]uint32, len(encSrc))
this.encrypt(encDst, encSrc)
resBytes := uint32sToBytes(encDst)
copy(dst, resBytes)
}
func (this *gostCipher) Decrypt(dst, src []byte) {
if len(src) < BlockSize {
panic("cryptobin/gost: input not full block")
}
if len(dst) < BlockSize {
panic("cryptobin/gost: output not full block")
}
if alias.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
panic("cryptobin/gost: invalid buffer overlap")
}
encSrc := bytesToUint32s(src)
encDst := make([]uint32, len(encSrc))
this.decrypt(encDst, encSrc)
resBytes := uint32sToBytes(encDst)
copy(dst, resBytes)
}
// Encrypt one block from src into dst.
func (this *gostCipher) encrypt(dst, src []uint32) {
n1, n2 := src[0], src[1]
var j int
// three rounds
for i := 0; i < 3; i++ {
for j = 0; j < 8; j++ {
if j%2 == 0 {
n2 = n2 ^ this.round(n1 + this.key[j])
} else {
n1 = n1 ^ this.round(n2 + this.key[j])
}
}
}
// last round
for j = 7; j >= 0; j-- {
if j%2 != 0 {
n2 = n2 ^ this.round(n1 + this.key[j])
} else {
n1 = n1 ^ this.round(n2 + this.key[j])
}
}
dst[0], dst[1] = n2, n1
}
// Decrypt one block from src into dst.
func (this *gostCipher) decrypt(dst, src []uint32) {
n1, n2 := src[0], src[1]
var j int
// first round
for j = 0; j < 8; j++ {
if j%2 == 0 {
n2 = n2 ^ this.round(n1 + this.key[j])
} else {
n1 = n1 ^ this.round(n2 + this.key[j])
}
}
// three rounds
for i := 0; i < 3; i++ {
for j = 7; j >= 0; j-- {
if j%2 != 0 {
n2 = n2 ^ this.round(n1 + this.key[j])
} else {
n1 = n1 ^ this.round(n2 + this.key[j])
}
}
}
dst[0], dst[1] = n2, n1
}
// GOST block cipher round function
func (this *gostCipher) round(x uint32) uint32 {
return cycle(x, this.k)
}