Skip to content

Commit 7bfee99

Browse files
committed
test(cryptography): verify GOST RFC vector
1 parent 01ffd8a commit 7bfee99

1 file changed

Lines changed: 30 additions & 19 deletions

File tree

cryptography/curve_test.go

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,38 @@
11
package cryptography
22

33
import (
4-
"math/big"
4+
"encoding/hex"
55
"testing"
66
)
77

8-
func TestGOST256SignatureEquation(t *testing.T) {
9-
private := big.NewInt(1)
10-
base := point{new(big.Int).Set(curve256.gx), new(big.Int).Set(curve256.gy), false}
11-
publicPoint := curve256.mul(base, private)
12-
public := make([]byte, 64)
13-
publicPoint.x.FillBytes(public[:32])
14-
publicPoint.y.FillBytes(public[32:])
15-
message := []byte("i2pd gost signature vector")
16-
digest := Sum256(message)
17-
k := big.NewInt(2)
18-
c := curve256.mul(base, k)
19-
r := new(big.Int).Mod(c.x, curve256.q)
20-
s := new(big.Int).Mod(new(big.Int).Add(new(big.Int).Mul(r, private), new(big.Int).Mul(k, new(big.Int).SetBytes(digest[:]))), curve256.q)
21-
rawR, rawS := make([]byte, 32), make([]byte, 32)
22-
r.FillBytes(rawR)
23-
s.FillBytes(rawS)
24-
if !Verify256(public, message, rawR, rawS) {
25-
t.Fatal("GOST R 34.10-2012 256 verification failed")
8+
func TestGOST256VerificationMatchesRFC7091(t *testing.T) {
9+
testCurve := newCurve([6]string{
10+
"7",
11+
"5FBFF498AA938CE739B8E022FBAFEF40563F6E6A3472FC2A514C0CE9DAE23B7E",
12+
"8000000000000000000000000000000000000000000000000000000000000431",
13+
"8000000000000000000000000000000150FE8A1892976154C59CFC193ACCF5B3",
14+
"2",
15+
"8E2A8A0E65147D4BD6316030E16D19C85C97F0A9CA267122B96ABBCEA7E8FC8",
16+
})
17+
decode := func(name, value string) []byte {
18+
t.Helper()
19+
decoded, err := hex.DecodeString(value)
20+
if err != nil {
21+
t.Fatalf("decode %s: %v", name, err)
22+
}
23+
return decoded
24+
}
25+
26+
// RFC 7091, Section 7, publishes the curve, digest scalar, public key,
27+
// and signature independently of this implementation.
28+
public := decode("public key",
29+
"7F2B49E270DB6D90D8595BEC458B50C58585BA1D4E9B788F6689DBD8E56FD80B"+
30+
"26F1B489D6701DD185C8413A977B3CBBAF64D1C593D26627DFFB101A87FF77DA")
31+
digest := decode("digest", "2DFBC1B372D89A1188C09C52E0EEC61FCE52032AB1022E8E67ECE6672B043EE5")
32+
r := decode("r", "41AA28D2F1AB148280CD9ED56FEDA41974053554A42767B83AD043FD39DC0493")
33+
s := decode("s", "01456C64BA4642A1653C235A98A60249BCD6D3F746B631DF928014F6C5BF9C40")
34+
35+
if !testCurve.verify(public, digest, r, s) {
36+
t.Fatal("GOST R 34.10-2012 RFC 7091 vector verification failed")
2637
}
2738
}

0 commit comments

Comments
 (0)