]> Cypherpunks.ru repositories - gogost.git/blob - gost3410/vko2001_test.go
Do not alter ukm in gost3410.KEK*
[gogost.git] / gost3410 / vko2001_test.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 package gost3410
17
18 import (
19         "bytes"
20         "encoding/hex"
21         "math/big"
22         "testing"
23         "testing/quick"
24 )
25
26 func TestVKO2001(t *testing.T) {
27         c := CurveIdGostR34102001TestParamSet()
28         ukmRaw, _ := hex.DecodeString("5172be25f852a233")
29         ukm := NewUKM(ukmRaw)
30         prvRaw1, _ := hex.DecodeString("1df129e43dab345b68f6a852f4162dc69f36b2f84717d08755cc5c44150bf928")
31         prvRaw2, _ := hex.DecodeString("5b9356c6474f913f1e83885ea0edd5df1a43fd9d799d219093241157ac9ed473")
32         kek, _ := hex.DecodeString("ee4618a0dbb10cb31777b4b86a53d9e7ef6cb3e400101410f0c0f2af46c494a6")
33         prv1, _ := NewPrivateKey(c, prvRaw1)
34         prv2, _ := NewPrivateKey(c, prvRaw2)
35         pub1, _ := prv1.PublicKey()
36         pub2, _ := prv2.PublicKey()
37         kek1, _ := prv1.KEK2001(pub2, ukm)
38         kek2, _ := prv2.KEK2001(pub1, ukm)
39         if bytes.Compare(kek1, kek2) != 0 {
40                 t.FailNow()
41         }
42         if bytes.Compare(kek1, kek) != 0 {
43                 t.FailNow()
44         }
45 }
46
47 func TestVKOUKMAltering(t *testing.T) {
48         c := CurveIdtc26gost34102012256paramSetA()
49         ukm := big.NewInt(1)
50         prv, err := NewPrivateKey(c, bytes.Repeat([]byte{0x12}, 32))
51         if err != nil {
52                 panic(err)
53         }
54         pub, err := prv.PublicKey()
55         if err != nil {
56                 panic(err)
57         }
58         _, err = prv.KEK(pub, ukm)
59         if err != nil {
60                 panic(err)
61         }
62         if ukm.Cmp(big.NewInt(1)) != 0 {
63                 t.FailNow()
64         }
65 }
66
67 func TestRandomVKO2001(t *testing.T) {
68         c := CurveIdGostR34102001TestParamSet()
69         f := func(prvRaw1 [32]byte, prvRaw2 [32]byte, ukmRaw [8]byte) bool {
70                 prv1, err := NewPrivateKey(c, prvRaw1[:])
71                 if err != nil {
72                         return false
73                 }
74                 prv2, err := NewPrivateKey(c, prvRaw2[:])
75                 if err != nil {
76                         return false
77                 }
78                 pub1, _ := prv1.PublicKey()
79                 pub2, _ := prv2.PublicKey()
80                 ukm := NewUKM(ukmRaw[:])
81                 kek1, _ := prv1.KEK2001(pub2, ukm)
82                 kek2, _ := prv2.KEK2001(pub1, ukm)
83                 return bytes.Compare(kek1, kek2) == 0
84         }
85         if err := quick.Check(f, nil); err != nil {
86                 t.Error(err)
87         }
88 }