]> Cypherpunks.ru repositories - gogost.git/blob - src/cypherpunks.ru/gogost/gost341264/cipher.go
b8c4dbd56ea6fabe02ed801d961fa26be3e87051
[gogost.git] / src / cypherpunks.ru / gogost / gost341264 / cipher.go
1 // GoGOST -- Pure Go GOST cryptographic functions library
2 // Copyright (C) 2015-2019 Sergey Matveev <stargrave@stargrave.org>
3 //
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, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 // GOST 34.12-2015 64-bit (Магма (Magma)) block cipher.
18 package gost341264
19
20 import (
21         "cypherpunks.ru/gogost/gost28147"
22 )
23
24 const (
25         BlockSize = 8
26         KeySize   = 32
27 )
28
29 type Cipher struct {
30         c   *gost28147.Cipher
31         blk *[BlockSize]byte
32 }
33
34 func NewCipher(key [KeySize]byte) *Cipher {
35         keyCompatible := new([KeySize]byte)
36         for i := 0; i < KeySize/4; i++ {
37                 keyCompatible[i*4+0] = key[i*4+3]
38                 keyCompatible[i*4+1] = key[i*4+2]
39                 keyCompatible[i*4+2] = key[i*4+1]
40                 keyCompatible[i*4+3] = key[i*4+0]
41         }
42         return &Cipher{
43                 c:   gost28147.NewCipher(*keyCompatible, &gost28147.Gost28147_tc26_ParamZ),
44                 blk: new([BlockSize]byte),
45         }
46 }
47
48 func (c *Cipher) BlockSize() int {
49         return BlockSize
50 }
51
52 func (c *Cipher) Encrypt(dst, src []byte) {
53         c.blk[0] = src[7]
54         c.blk[1] = src[6]
55         c.blk[2] = src[5]
56         c.blk[3] = src[4]
57         c.blk[4] = src[3]
58         c.blk[5] = src[2]
59         c.blk[6] = src[1]
60         c.blk[7] = src[0]
61         c.c.Encrypt(c.blk[:], c.blk[:])
62         dst[0] = c.blk[7]
63         dst[1] = c.blk[6]
64         dst[2] = c.blk[5]
65         dst[3] = c.blk[4]
66         dst[4] = c.blk[3]
67         dst[5] = c.blk[2]
68         dst[6] = c.blk[1]
69         dst[7] = c.blk[0]
70 }
71
72 func (c *Cipher) Decrypt(dst, src []byte) {
73         c.blk[0] = src[7]
74         c.blk[1] = src[6]
75         c.blk[2] = src[5]
76         c.blk[3] = src[4]
77         c.blk[4] = src[3]
78         c.blk[5] = src[2]
79         c.blk[6] = src[1]
80         c.blk[7] = src[0]
81         c.c.Decrypt(c.blk[:], c.blk[:])
82         dst[0] = c.blk[7]
83         dst[1] = c.blk[6]
84         dst[2] = c.blk[5]
85         dst[3] = c.blk[4]
86         dst[4] = c.blk[3]
87         dst[5] = c.blk[2]
88         dst[6] = c.blk[1]
89         dst[7] = c.blk[0]
90 }