]> Cypherpunks.ru repositories - gogost.git/blob - gost3410/2001_test.go
Raise copyright years
[gogost.git] / gost3410 / 2001_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 gost3410
17
18 import (
19         "bytes"
20         "crypto/rand"
21         "testing"
22         "testing/quick"
23 )
24
25 func TestRFCVectors(t *testing.T) {
26         priv := []byte{
27                 0x28, 0x3b, 0xec, 0x91, 0x98, 0xce, 0x19, 0x1d,
28                 0xee, 0x7e, 0x39, 0x49, 0x1f, 0x96, 0x60, 0x1b,
29                 0xc1, 0x72, 0x9a, 0xd3, 0x9d, 0x35, 0xed, 0x10,
30                 0xbe, 0xb9, 0x9b, 0x78, 0xde, 0x9a, 0x92, 0x7a,
31         }
32         pubX := []byte{
33                 0x0b, 0xd8, 0x6f, 0xe5, 0xd8, 0xdb, 0x89, 0x66,
34                 0x8f, 0x78, 0x9b, 0x4e, 0x1d, 0xba, 0x85, 0x85,
35                 0xc5, 0x50, 0x8b, 0x45, 0xec, 0x5b, 0x59, 0xd8,
36                 0x90, 0x6d, 0xdb, 0x70, 0xe2, 0x49, 0x2b, 0x7f,
37         }
38         pubY := []byte{
39                 0xda, 0x77, 0xff, 0x87, 0x1a, 0x10, 0xfb, 0xdf,
40                 0x27, 0x66, 0xd2, 0x93, 0xc5, 0xd1, 0x64, 0xaf,
41                 0xbb, 0x3c, 0x7b, 0x97, 0x3a, 0x41, 0xc8, 0x85,
42                 0xd1, 0x1d, 0x70, 0xd6, 0x89, 0xb4, 0xf1, 0x26,
43         }
44         digest := []byte{
45                 0x2d, 0xfb, 0xc1, 0xb3, 0x72, 0xd8, 0x9a, 0x11,
46                 0x88, 0xc0, 0x9c, 0x52, 0xe0, 0xee, 0xc6, 0x1f,
47                 0xce, 0x52, 0x03, 0x2a, 0xb1, 0x02, 0x2e, 0x8e,
48                 0x67, 0xec, 0xe6, 0x67, 0x2b, 0x04, 0x3e, 0xe5,
49         }
50         signature := []byte{
51                 0x01, 0x45, 0x6c, 0x64, 0xba, 0x46, 0x42, 0xa1,
52                 0x65, 0x3c, 0x23, 0x5a, 0x98, 0xa6, 0x02, 0x49,
53                 0xbc, 0xd6, 0xd3, 0xf7, 0x46, 0xb6, 0x31, 0xdf,
54                 0x92, 0x80, 0x14, 0xf6, 0xc5, 0xbf, 0x9c, 0x40,
55                 0x41, 0xaa, 0x28, 0xd2, 0xf1, 0xab, 0x14, 0x82,
56                 0x80, 0xcd, 0x9e, 0xd5, 0x6f, 0xed, 0xa4, 0x19,
57                 0x74, 0x05, 0x35, 0x54, 0xa4, 0x27, 0x67, 0xb8,
58                 0x3a, 0xd0, 0x43, 0xfd, 0x39, 0xdc, 0x04, 0x93,
59         }
60
61         c := CurveIdGostR34102001TestParamSet()
62         prv, err := NewPrivateKey(c, priv)
63         if err != nil {
64                 t.FailNow()
65         }
66         pub, err := prv.PublicKey()
67         if err != nil {
68                 t.FailNow()
69         }
70         if bytes.Compare(pub.Raw()[:32], pubX) != 0 {
71                 t.FailNow()
72         }
73         if bytes.Compare(pub.Raw()[32:], pubY) != 0 {
74                 t.FailNow()
75         }
76         ourSign, err := prv.SignDigest(digest, rand.Reader)
77         if err != nil {
78                 t.FailNow()
79         }
80         valid, err := pub.VerifyDigest(digest, ourSign)
81         if err != nil || !valid {
82                 t.FailNow()
83         }
84         valid, err = pub.VerifyDigest(digest, signature)
85         if err != nil || !valid {
86                 t.FailNow()
87         }
88 }
89
90 func TestRandom2001(t *testing.T) {
91         c := CurveIdGostR34102001TestParamSet()
92         f := func(data [31]byte, digest [32]byte) bool {
93                 prv, err := NewPrivateKey(c, append([]byte{0xde}, data[:]...))
94                 if err != nil {
95                         return false
96                 }
97                 pub, err := prv.PublicKey()
98                 if err != nil {
99                         return false
100                 }
101                 pubRaw := pub.Raw()
102                 pub, err = NewPublicKey(c, pubRaw)
103                 if err != nil {
104                         return false
105                 }
106                 sign, err := prv.SignDigest(digest[:], rand.Reader)
107                 if err != nil {
108                         return false
109                 }
110                 valid, err := pub.VerifyDigest(digest[:], sign)
111                 if err != nil {
112                         return false
113                 }
114                 return valid
115         }
116         if err := quick.Check(f, nil); err != nil {
117                 t.Error(err)
118         }
119 }
120
121 func BenchmarkSign2001(b *testing.B) {
122         c := CurveIdGostR34102001TestParamSet()
123         prv, err := GenPrivateKey(c, rand.Reader)
124         if err != nil {
125                 b.FailNow()
126         }
127         digest := make([]byte, 32)
128         rand.Read(digest)
129         b.ResetTimer()
130         for i := 0; i < b.N; i++ {
131                 prv.SignDigest(digest, rand.Reader)
132         }
133 }
134
135 func BenchmarkVerify2001(b *testing.B) {
136         c := CurveIdGostR34102001TestParamSet()
137         prv, err := GenPrivateKey(c, rand.Reader)
138         if err != nil {
139                 b.FailNow()
140         }
141         digest := make([]byte, 32)
142         rand.Read(digest)
143         sign, err := prv.SignDigest(digest, rand.Reader)
144         if err != nil {
145                 b.FailNow()
146         }
147         pub, err := prv.PublicKey()
148         if err != nil {
149                 b.FailNow()
150         }
151         b.ResetTimer()
152         for i := 0; i < b.N; i++ {
153                 pub.VerifyDigest(digest, sign)
154         }
155 }
156
157 func TestPrvEqualsTo1(t *testing.T) {
158         c := CurveIdGostR34102001TestParamSet()
159         prvRaw := make([]byte, 32)
160         prvRaw[len(prvRaw)-1] = 1
161         prv, err := NewPrivateKey(c, prvRaw)
162         if err != nil {
163                 t.FailNow()
164         }
165         pub, err := prv.PublicKey()
166         if err != nil {
167                 t.FailNow()
168         }
169         digest := []byte{
170                 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
171                 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
172                 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
173                 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
174         }
175         sign, err := prv.SignDigest(digest, rand.Reader)
176         if err != nil {
177                 t.FailNow()
178         }
179         valid, err := pub.VerifyDigest(digest, sign)
180         if err != nil || !valid {
181                 t.FailNow()
182         }
183 }