1 // GoGOST -- Pure Go GOST cryptographic functions library
2 // Copyright (C) 2015-2019 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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/>.
25 "go.cypherpunks.ru/gogost/v4/gost3412128"
26 "go.cypherpunks.ru/gogost/v4/gost341264"
29 func TestVector(t *testing.T) {
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,
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,
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,
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,
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,
78 _, err := aead.Open(sealed[:0], nonce, sealed, additionalData)
82 if bytes.Compare(sealed[:len(plaintext)], plaintext) != 0 {
87 func TestSymmetric(t *testing.T) {
88 sym := func(keySize, blockSize int, c cipher.Block, nonce []byte) {
90 plaintext, additionalData []byte,
94 if len(plaintext) == 0 && len(additionalData) == 0 {
97 tagSize = 4 + tagSize%uint8(blockSize-4)
98 aead, err := NewMGM(c, int(tagSize))
102 for _, initial := range initials {
103 sealed := aead.Seal(initial, nonce, plaintext, additionalData)
104 if bytes.Compare(sealed[:len(initial)], initial) != 0 {
107 pt, err := aead.Open(
110 sealed[len(initial):],
113 if err != nil || bytes.Compare(pt, plaintext) != 0 {
119 if err := quick.Check(f, nil); err != nil {
124 key128 := new([gost3412128.KeySize]byte)
125 if _, err := rand.Read(key128[:]); err != nil {
128 nonce := make([]byte, gost3412128.BlockSize)
129 if _, err := rand.Read(key128[1:]); err != nil {
134 gost3412128.BlockSize,
135 gost3412128.NewCipher(key128[:]),
136 nonce[:gost3412128.BlockSize],
139 key64 := new([gost341264.KeySize]byte)
140 copy(key64[:], key128[:])
143 gost341264.BlockSize,
144 gost341264.NewCipher(key64[:]),
145 nonce[:gost341264.BlockSize],