]> Cypherpunks.ru repositories - gogost.git/blob - gost341264/cipher.go
Raise copyright years
[gogost.git] / gost341264 / cipher.go
1 // GoGOST -- Pure Go GOST cryptographic functions library
2 // Copyright (C) 2015-2020 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, version 3 of the License.
7 //
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.
12 //
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/>.
15
16 // GOST 34.12-2015 64-bit (Магма (Magma)) block cipher.
17 package gost341264
18
19 import (
20         "go.cypherpunks.ru/gogost/v4/gost28147"
21 )
22
23 const (
24         BlockSize = 8
25         KeySize   = 32
26 )
27
28 type Cipher struct {
29         c   *gost28147.Cipher
30         blk *[BlockSize]byte
31 }
32
33 func NewCipher(key []byte) *Cipher {
34         if len(key) != KeySize {
35                 panic("invalid key size")
36         }
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]
43         }
44         return &Cipher{
45                 c:   gost28147.NewCipher(keyCompatible, &gost28147.SboxIdtc26gost28147paramZ),
46                 blk: new([BlockSize]byte),
47         }
48 }
49
50 func (c *Cipher) BlockSize() int {
51         return BlockSize
52 }
53
54 func (c *Cipher) Encrypt(dst, src []byte) {
55         c.blk[0] = src[7]
56         c.blk[1] = src[6]
57         c.blk[2] = src[5]
58         c.blk[3] = src[4]
59         c.blk[4] = src[3]
60         c.blk[5] = src[2]
61         c.blk[6] = src[1]
62         c.blk[7] = src[0]
63         c.c.Encrypt(c.blk[:], c.blk[:])
64         dst[0] = c.blk[7]
65         dst[1] = c.blk[6]
66         dst[2] = c.blk[5]
67         dst[3] = c.blk[4]
68         dst[4] = c.blk[3]
69         dst[5] = c.blk[2]
70         dst[6] = c.blk[1]
71         dst[7] = c.blk[0]
72 }
73
74 func (c *Cipher) Decrypt(dst, src []byte) {
75         c.blk[0] = src[7]
76         c.blk[1] = src[6]
77         c.blk[2] = src[5]
78         c.blk[3] = src[4]
79         c.blk[4] = src[3]
80         c.blk[5] = src[2]
81         c.blk[6] = src[1]
82         c.blk[7] = src[0]
83         c.c.Decrypt(c.blk[:], c.blk[:])
84         dst[0] = c.blk[7]
85         dst[1] = c.blk[6]
86         dst[2] = c.blk[5]
87         dst[3] = c.blk[4]
88         dst[4] = c.blk[3]
89         dst[5] = c.blk[2]
90         dst[6] = c.blk[1]
91         dst[7] = c.blk[0]
92 }