]> Cypherpunks.ru repositories - gogost.git/blob - mgm/mode_test.go
7d1d984316915227bdc46fe957a570175572fbea
[gogost.git] / mgm / mode_test.go
1 // GoGOST -- Pure Go GOST cryptographic functions library
2 // Copyright (C) 2015-2021 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 package mgm
17
18 import (
19         "bytes"
20         "crypto/cipher"
21         "crypto/rand"
22         "testing"
23         "testing/quick"
24
25         "go.cypherpunks.ru/gogost/v5/gost3412128"
26         "go.cypherpunks.ru/gogost/v5/gost341264"
27 )
28
29 func TestVector(t *testing.T) {
30         key := []byte{
31                 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
32                 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
33                 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
34                 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
35         }
36         additionalData := []byte{
37                 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
38                 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
39                 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
40                 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
41                 0xEA, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
42                 0x05,
43         }
44         plaintext := []byte{
45                 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x00,
46                 0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x99, 0x88,
47                 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
48                 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xEE, 0xFF, 0x0A,
49                 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
50                 0x99, 0xAA, 0xBB, 0xCC, 0xEE, 0xFF, 0x0A, 0x00,
51                 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99,
52                 0xAA, 0xBB, 0xCC, 0xEE, 0xFF, 0x0A, 0x00, 0x11,
53                 0xAA, 0xBB, 0xCC,
54         }
55         c := gost3412128.NewCipher(key)
56         nonce := plaintext[:16]
57         aead, _ := NewMGM(c, 16)
58         sealed := aead.Seal(nil, nonce, plaintext, additionalData)
59         if bytes.Compare(sealed[:len(plaintext)], []byte{
60                 0xA9, 0x75, 0x7B, 0x81, 0x47, 0x95, 0x6E, 0x90,
61                 0x55, 0xB8, 0xA3, 0x3D, 0xE8, 0x9F, 0x42, 0xFC,
62                 0x80, 0x75, 0xD2, 0x21, 0x2B, 0xF9, 0xFD, 0x5B,
63                 0xD3, 0xF7, 0x06, 0x9A, 0xAD, 0xC1, 0x6B, 0x39,
64                 0x49, 0x7A, 0xB1, 0x59, 0x15, 0xA6, 0xBA, 0x85,
65                 0x93, 0x6B, 0x5D, 0x0E, 0xA9, 0xF6, 0x85, 0x1C,
66                 0xC6, 0x0C, 0x14, 0xD4, 0xD3, 0xF8, 0x83, 0xD0,
67                 0xAB, 0x94, 0x42, 0x06, 0x95, 0xC7, 0x6D, 0xEB,
68                 0x2C, 0x75, 0x52,
69         }) != 0 {
70                 t.FailNow()
71         }
72         if bytes.Compare(sealed[len(plaintext):], []byte{
73                 0xCF, 0x5D, 0x65, 0x6F, 0x40, 0xC3, 0x4F, 0x5C,
74                 0x46, 0xE8, 0xBB, 0x0E, 0x29, 0xFC, 0xDB, 0x4C,
75         }) != 0 {
76                 t.FailNow()
77         }
78         _, err := aead.Open(sealed[:0], nonce, sealed, additionalData)
79         if err != nil {
80                 t.FailNow()
81         }
82         if bytes.Compare(sealed[:len(plaintext)], plaintext) != 0 {
83                 t.FailNow()
84         }
85 }
86
87 func TestSymmetric(t *testing.T) {
88         sym := func(keySize, blockSize int, c cipher.Block, nonce []byte) {
89                 f := func(
90                         plaintext, additionalData []byte,
91                         initials [][]byte,
92                         tagSize uint8,
93                 ) bool {
94                         if len(plaintext) == 0 && len(additionalData) == 0 {
95                                 return true
96                         }
97                         tagSize = 4 + tagSize%uint8(blockSize-4)
98                         aead, err := NewMGM(c, int(tagSize))
99                         if err != nil {
100                                 return false
101                         }
102                         for _, initial := range initials {
103                                 sealed := aead.Seal(initial, nonce, plaintext, additionalData)
104                                 if bytes.Compare(sealed[:len(initial)], initial) != 0 {
105                                         return false
106                                 }
107                                 pt, err := aead.Open(
108                                         sealed[:0],
109                                         nonce,
110                                         sealed[len(initial):],
111                                         additionalData,
112                                 )
113                                 if err != nil || bytes.Compare(pt, plaintext) != 0 {
114                                         return false
115                                 }
116                         }
117                         return true
118                 }
119                 if err := quick.Check(f, nil); err != nil {
120                         t.Error(err)
121                 }
122         }
123
124         key128 := new([gost3412128.KeySize]byte)
125         if _, err := rand.Read(key128[:]); err != nil {
126                 panic(err)
127         }
128         nonce := make([]byte, gost3412128.BlockSize)
129         if _, err := rand.Read(key128[1:]); err != nil {
130                 panic(err)
131         }
132         sym(
133                 gost3412128.KeySize,
134                 gost3412128.BlockSize,
135                 gost3412128.NewCipher(key128[:]),
136                 nonce[:gost3412128.BlockSize],
137         )
138
139         key64 := new([gost341264.KeySize]byte)
140         copy(key64[:], key128[:])
141         sym(
142                 gost341264.KeySize,
143                 gost341264.BlockSize,
144                 gost341264.NewCipher(key64[:]),
145                 nonce[:gost341264.BlockSize],
146         )
147 }