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, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
26 func TestRFCVectors(t *testing.T) {
28 0x28, 0x3b, 0xec, 0x91, 0x98, 0xce, 0x19, 0x1d,
29 0xee, 0x7e, 0x39, 0x49, 0x1f, 0x96, 0x60, 0x1b,
30 0xc1, 0x72, 0x9a, 0xd3, 0x9d, 0x35, 0xed, 0x10,
31 0xbe, 0xb9, 0x9b, 0x78, 0xde, 0x9a, 0x92, 0x7a,
34 0x0b, 0xd8, 0x6f, 0xe5, 0xd8, 0xdb, 0x89, 0x66,
35 0x8f, 0x78, 0x9b, 0x4e, 0x1d, 0xba, 0x85, 0x85,
36 0xc5, 0x50, 0x8b, 0x45, 0xec, 0x5b, 0x59, 0xd8,
37 0x90, 0x6d, 0xdb, 0x70, 0xe2, 0x49, 0x2b, 0x7f,
40 0xda, 0x77, 0xff, 0x87, 0x1a, 0x10, 0xfb, 0xdf,
41 0x27, 0x66, 0xd2, 0x93, 0xc5, 0xd1, 0x64, 0xaf,
42 0xbb, 0x3c, 0x7b, 0x97, 0x3a, 0x41, 0xc8, 0x85,
43 0xd1, 0x1d, 0x70, 0xd6, 0x89, 0xb4, 0xf1, 0x26,
46 0x2d, 0xfb, 0xc1, 0xb3, 0x72, 0xd8, 0x9a, 0x11,
47 0x88, 0xc0, 0x9c, 0x52, 0xe0, 0xee, 0xc6, 0x1f,
48 0xce, 0x52, 0x03, 0x2a, 0xb1, 0x02, 0x2e, 0x8e,
49 0x67, 0xec, 0xe6, 0x67, 0x2b, 0x04, 0x3e, 0xe5,
52 0x01, 0x45, 0x6c, 0x64, 0xba, 0x46, 0x42, 0xa1,
53 0x65, 0x3c, 0x23, 0x5a, 0x98, 0xa6, 0x02, 0x49,
54 0xbc, 0xd6, 0xd3, 0xf7, 0x46, 0xb6, 0x31, 0xdf,
55 0x92, 0x80, 0x14, 0xf6, 0xc5, 0xbf, 0x9c, 0x40,
56 0x41, 0xaa, 0x28, 0xd2, 0xf1, 0xab, 0x14, 0x82,
57 0x80, 0xcd, 0x9e, 0xd5, 0x6f, 0xed, 0xa4, 0x19,
58 0x74, 0x05, 0x35, 0x54, 0xa4, 0x27, 0x67, 0xb8,
59 0x3a, 0xd0, 0x43, 0xfd, 0x39, 0xdc, 0x04, 0x93,
62 c := CurveIdGostR34102001TestParamSet()
63 prv, err := NewPrivateKey(c, Mode2001, priv)
67 pub, err := prv.PublicKey()
71 if bytes.Compare(pub.Raw()[:32], pubX) != 0 {
74 if bytes.Compare(pub.Raw()[32:], pubY) != 0 {
77 ourSign, err := prv.SignDigest(digest, rand.Reader)
81 valid, err := pub.VerifyDigest(digest, ourSign)
82 if err != nil || !valid {
85 valid, err = pub.VerifyDigest(digest, signature)
86 if err != nil || !valid {
91 func TestRandom2001(t *testing.T) {
92 c := CurveIdGostR34102001TestParamSet()
93 f := func(data [31]byte, digest [32]byte) bool {
94 prv, err := NewPrivateKey(
97 append([]byte{0xde}, data[:]...),
102 pub, err := prv.PublicKey()
107 pub, err = NewPublicKey(c, Mode2001, pubRaw)
111 sign, err := prv.SignDigest(digest[:], rand.Reader)
115 valid, err := pub.VerifyDigest(digest[:], sign)
121 if err := quick.Check(f, nil); err != nil {
126 func BenchmarkSign2001(b *testing.B) {
127 c := CurveIdGostR34102001TestParamSet()
128 prv, err := GenPrivateKey(c, Mode2001, rand.Reader)
132 digest := make([]byte, 32)
135 for i := 0; i < b.N; i++ {
136 prv.SignDigest(digest, rand.Reader)
140 func BenchmarkVerify2001(b *testing.B) {
141 c := CurveIdGostR34102001TestParamSet()
142 prv, err := GenPrivateKey(c, Mode2001, rand.Reader)
146 digest := make([]byte, 32)
148 sign, err := prv.SignDigest(digest, rand.Reader)
152 pub, err := prv.PublicKey()
157 for i := 0; i < b.N; i++ {
158 pub.VerifyDigest(digest, sign)
162 func TestPrvEqualsTo1(t *testing.T) {
163 c := CurveIdGostR34102001TestParamSet()
164 prvRaw := make([]byte, int(Mode2001))
165 prvRaw[len(prvRaw)-1] = 1
166 prv, err := NewPrivateKey(c, Mode2001, prvRaw)
170 pub, err := prv.PublicKey()
175 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
176 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
177 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
178 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
180 sign, err := prv.SignDigest(digest, rand.Reader)
184 valid, err := pub.VerifyDigest(digest, sign)
185 if err != nil || !valid {