]> Cypherpunks.ru repositories - gogost.git/blob - gost28147/cipher.go
e926faaf78c797c0ef09b181dbe956a74a363ed6
[gogost.git] / gost28147 / 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, 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 28147-89 block cipher with ECB, CFB, CTR, MAC modes of operation.
17 // RFC 5830.
18 package gost28147
19
20 const (
21         BlockSize = 8
22         KeySize   = 32
23 )
24
25 // All 28147 operations are going with two 32-bit halves of the whole
26 // block. nv is representation of that one half.
27 type nv uint32
28
29 // Cyclic 11-bit shift.
30 func (n nv) shift11() nv {
31         return ((n << 11) & (1<<32 - 1)) | ((n >> (32 - 11)) & (1<<32 - 1))
32 }
33
34 // Seq contains iteration numbers used in the encryption function
35 // itself. For example 28147 encryption and decryption process differs
36 // only with this sequence.
37 type Seq []uint8
38
39 var (
40         SeqEncrypt = Seq([]uint8{
41                 0, 1, 2, 3, 4, 5, 6, 7,
42                 0, 1, 2, 3, 4, 5, 6, 7,
43                 0, 1, 2, 3, 4, 5, 6, 7,
44                 7, 6, 5, 4, 3, 2, 1, 0,
45         })
46         SeqDecrypt = Seq([]uint8{
47                 0, 1, 2, 3, 4, 5, 6, 7,
48                 7, 6, 5, 4, 3, 2, 1, 0,
49                 7, 6, 5, 4, 3, 2, 1, 0,
50                 7, 6, 5, 4, 3, 2, 1, 0,
51         })
52 )
53
54 type Cipher struct {
55         key  [KeySize]byte
56         sbox *Sbox
57         x    [8]nv
58 }
59
60 func NewCipher(key []byte, sbox *Sbox) *Cipher {
61         if len(key) != KeySize {
62                 panic("invalid key size")
63         }
64         c := Cipher{sbox: sbox}
65         copy(c.key[:], key)
66         c.x = [8]nv{
67                 nv(key[0]) | nv(key[1])<<8 | nv(key[2])<<16 | nv(key[3])<<24,
68                 nv(key[4]) | nv(key[5])<<8 | nv(key[6])<<16 | nv(key[7])<<24,
69                 nv(key[8]) | nv(key[9])<<8 | nv(key[10])<<16 | nv(key[11])<<24,
70                 nv(key[12]) | nv(key[13])<<8 | nv(key[14])<<16 | nv(key[15])<<24,
71                 nv(key[16]) | nv(key[17])<<8 | nv(key[18])<<16 | nv(key[19])<<24,
72                 nv(key[20]) | nv(key[21])<<8 | nv(key[22])<<16 | nv(key[23])<<24,
73                 nv(key[24]) | nv(key[25])<<8 | nv(key[26])<<16 | nv(key[27])<<24,
74                 nv(key[28]) | nv(key[29])<<8 | nv(key[30])<<16 | nv(key[31])<<24,
75         }
76         return &c
77 }
78
79 func (c *Cipher) BlockSize() int {
80         return BlockSize
81 }
82
83 // Convert binary byte block to two 32-bit internal integers.
84 func block2nvs(b []byte) (n1, n2 nv) {
85         n1 = nv(b[0]) | nv(b[1])<<8 | nv(b[2])<<16 | nv(b[3])<<24
86         n2 = nv(b[4]) | nv(b[5])<<8 | nv(b[6])<<16 | nv(b[7])<<24
87         return
88 }
89
90 // Convert two 32-bit internal integers to binary byte block.
91 func nvs2block(n1, n2 nv, b []byte) {
92         b[0] = byte((n2 >> 0) & 255)
93         b[1] = byte((n2 >> 8) & 255)
94         b[2] = byte((n2 >> 16) & 255)
95         b[3] = byte((n2 >> 24) & 255)
96         b[4] = byte((n1 >> 0) & 255)
97         b[5] = byte((n1 >> 8) & 255)
98         b[6] = byte((n1 >> 16) & 255)
99         b[7] = byte((n1 >> 24) & 255)
100 }
101
102 func (c *Cipher) xcrypt(seq Seq, n1, n2 nv) (nv, nv) {
103         for _, i := range seq {
104                 n1, n2 = c.sbox.k(n1+c.x[i]).shift11()^n2, n1
105         }
106         return n1, n2
107 }
108
109 // Encrypt single block.
110 // If provided slices are shorter than the block size, then it will panic.
111 func (c *Cipher) Encrypt(dst, src []byte) {
112         n1, n2 := block2nvs(src)
113         n1, n2 = c.xcrypt(SeqEncrypt, n1, n2)
114         nvs2block(n1, n2, dst)
115 }
116
117 // Decrypt single block.
118 // If provided slices are shorter than the block size, then it will panic.
119 func (c *Cipher) Decrypt(dst, src []byte) {
120         n1, n2 := block2nvs(src)
121         n1, n2 = c.xcrypt(SeqDecrypt, n1, n2)
122         nvs2block(n1, n2, dst)
123 }