1 // GoGOST -- Pure Go GOST cryptographic functions library
2 // Copyright (C) 2015-2019 Sergey Matveev <stargrave@stargrave.org>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // GOST 34.12-2015 64-bit (Магма (Magma)) block cipher.
20 "go.cypherpunks.ru/gogost/v4/gost28147"
33 func NewCipher(key []byte) *Cipher {
34 if len(key) != KeySize {
35 panic("invalid key size")
37 keyCompatible := make([]byte, KeySize)
38 for i := 0; i < KeySize/4; i++ {
39 keyCompatible[i*4+0] = key[i*4+3]
40 keyCompatible[i*4+1] = key[i*4+2]
41 keyCompatible[i*4+2] = key[i*4+1]
42 keyCompatible[i*4+3] = key[i*4+0]
45 c: gost28147.NewCipher(keyCompatible, &gost28147.SboxIdtc26gost28147paramZ),
46 blk: new([BlockSize]byte),
50 func (c *Cipher) BlockSize() int {
54 func (c *Cipher) Encrypt(dst, src []byte) {
63 c.c.Encrypt(c.blk[:], c.blk[:])
74 func (c *Cipher) Decrypt(dst, src []byte) {
83 c.c.Decrypt(c.blk[:], c.blk[:])