]> Cypherpunks.ru repositories - gogost.git/blob - src/cypherpunks.ru/gogost/gost341264/cipher.go
Sbox names comply with OIDs
[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(
44                         *keyCompatible,
45                         &gost28147.SboxIdtc26gost28147paramZ,
46                 ),
47                 blk: new([BlockSize]byte),
48         }
49 }
50
51 func (c *Cipher) BlockSize() int {
52         return BlockSize
53 }
54
55 func (c *Cipher) Encrypt(dst, src []byte) {
56         c.blk[0] = src[7]
57         c.blk[1] = src[6]
58         c.blk[2] = src[5]
59         c.blk[3] = src[4]
60         c.blk[4] = src[3]
61         c.blk[5] = src[2]
62         c.blk[6] = src[1]
63         c.blk[7] = src[0]
64         c.c.Encrypt(c.blk[:], c.blk[:])
65         dst[0] = c.blk[7]
66         dst[1] = c.blk[6]
67         dst[2] = c.blk[5]
68         dst[3] = c.blk[4]
69         dst[4] = c.blk[3]
70         dst[5] = c.blk[2]
71         dst[6] = c.blk[1]
72         dst[7] = c.blk[0]
73 }
74
75 func (c *Cipher) Decrypt(dst, src []byte) {
76         c.blk[0] = src[7]
77         c.blk[1] = src[6]
78         c.blk[2] = src[5]
79         c.blk[3] = src[4]
80         c.blk[4] = src[3]
81         c.blk[5] = src[2]
82         c.blk[6] = src[1]
83         c.blk[7] = src[0]
84         c.c.Decrypt(c.blk[:], c.blk[:])
85         dst[0] = c.blk[7]
86         dst[1] = c.blk[6]
87         dst[2] = c.blk[5]
88         dst[3] = c.blk[4]
89         dst[4] = c.blk[3]
90         dst[5] = c.blk[2]
91         dst[6] = c.blk[1]
92         dst[7] = c.blk[0]
93 }