1 // GoGOST -- Pure Go GOST cryptographic functions library
2 // Copyright (C) 2015-2021 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 func TestRFCVectors(t *testing.T) {
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,
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,
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,
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,
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,
61 c := CurveIdGostR34102001TestParamSet()
62 prv, err := NewPrivateKey(c, priv)
66 pub, err := prv.PublicKey()
70 if bytes.Compare(pub.Raw()[:32], pubX) != 0 {
73 if bytes.Compare(pub.Raw()[32:], pubY) != 0 {
76 ourSign, err := prv.SignDigest(digest, rand.Reader)
80 valid, err := pub.VerifyDigest(digest, ourSign)
81 if err != nil || !valid {
84 valid, err = pub.VerifyDigest(digest, signature)
85 if err != nil || !valid {
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[:]...))
97 pub, err := prv.PublicKey()
102 pub, err = NewPublicKey(c, pubRaw)
106 sign, err := prv.SignDigest(digest[:], rand.Reader)
110 valid, err := pub.VerifyDigest(digest[:], sign)
116 if err := quick.Check(f, nil); err != nil {
121 func BenchmarkSign2001(b *testing.B) {
122 c := CurveIdGostR34102001TestParamSet()
123 prv, err := GenPrivateKey(c, rand.Reader)
127 digest := make([]byte, 32)
130 for i := 0; i < b.N; i++ {
131 prv.SignDigest(digest, rand.Reader)
135 func BenchmarkVerify2001(b *testing.B) {
136 c := CurveIdGostR34102001TestParamSet()
137 prv, err := GenPrivateKey(c, rand.Reader)
141 digest := make([]byte, 32)
143 sign, err := prv.SignDigest(digest, rand.Reader)
147 pub, err := prv.PublicKey()
152 for i := 0; i < b.N; i++ {
153 pub.VerifyDigest(digest, sign)
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)
165 pub, err := prv.PublicKey()
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,
175 sign, err := prv.SignDigest(digest, rand.Reader)
179 valid, err := pub.VerifyDigest(digest, sign)
180 if err != nil || !valid {