github.com/ronperry/cryptoedge@v0.0.0-20150815114006-cc363e290743/singhdas/verifier_test.go (about)

     1  package singhdas
     2  
     3  import (
     4  	"crypto/elliptic"
     5  	"crypto/rand"
     6  	"github.com/ronperry/cryptoedge/eccutil"
     7  	"math/big"
     8  	"testing"
     9  )
    10  
    11  func TestVerify(t *testing.T) {
    12  	msg := []byte("Something to sign")
    13  	c := eccutil.SetCurve(elliptic.P256, rand.Reader, eccutil.Sha1Hash)
    14  	privKey, pubKey, err := c.GenerateKey() // Signer long-term key
    15  	if err != nil {
    16  		t.Fatalf("Long term key gen failed: %s", err)
    17  	}
    18  	sig := NewSigner(privKey, pubKey, c)
    19  	sp, err := sig.NewRequest()
    20  	if err != nil {
    21  		t.Errorf("Cannot create request parameters: %s", err)
    22  	}
    23  	sc := NewSignerClient(pubKey, c)
    24  	blindmsg, blindfac, err := sc.Blind(msg, sp.Q)
    25  	if err != nil {
    26  		t.Errorf("Cannot blind: %s", err)
    27  	}
    28  	blindsig, err := sig.Sign(blindmsg, sp)
    29  	if err != nil {
    30  		t.Errorf("Cannot sign: %s", err)
    31  	}
    32  	unblindsig, err := sc.UnBlind(blindsig, blindfac)
    33  	if err != nil {
    34  		t.Errorf("Cannot unblind: %s", err)
    35  	}
    36  	ok, err := sc.Verify(msg, unblindsig)
    37  	if err != nil {
    38  		t.Errorf("Cannot unblind: %s", err)
    39  	}
    40  	if !ok {
    41  		t.Error("Does not verify")
    42  	}
    43  	msg2 := []byte("Message that must fail")
    44  	Hm := c.GenHash(msg2)
    45  	ok, _ = sc.Verify(msg2, unblindsig)
    46  	if ok {
    47  		t.Error("Verify must fail")
    48  	}
    49  	badsig1 := new(SignatureInt)
    50  	badsig1.Hm = Hm
    51  	badsig1.R = unblindsig.R
    52  	badsig1.S = unblindsig.S
    53  	badsig1.r2 = unblindsig.r2
    54  	ok, _ = sc.Verify(msg2, badsig1)
    55  	if ok {
    56  		t.Error("Verify must fail")
    57  	}
    58  	badsig2 := new(SignatureInt)
    59  	badsig2.Hm = unblindsig.Hm
    60  	badsig2.R = unblindsig.R
    61  	badsig2.S = unblindsig.S
    62  	badsig2.r2 = new(big.Int)
    63  	badsig2.r2 = badsig2.r2.Sub(unblindsig.r2, big.NewInt(1))
    64  	ok, _ = sc.Verify(msg, badsig2)
    65  	if ok {
    66  		t.Error("Verify must fail")
    67  	}
    68  	badsig3 := new(SignatureInt)
    69  	badsig3.Hm = unblindsig.Hm
    70  	badsig3.R = unblindsig.R
    71  	badsig3.S = new(big.Int)
    72  	badsig3.S = badsig3.S.Sub(unblindsig.S, big.NewInt(1))
    73  	badsig3.r2 = unblindsig.r2
    74  	ok, _ = sc.Verify(msg, badsig3)
    75  	if ok {
    76  		t.Error("Verify must fail")
    77  	}
    78  	badsig4 := new(SignatureInt)
    79  	badsig4.Hm = unblindsig.Hm
    80  	badsig4.R = new(eccutil.Point)
    81  	badsig4.R.Y = unblindsig.R.Y
    82  	badsig4.R.X = new(big.Int)
    83  	badsig4.R.X = badsig4.R.X.Sub(unblindsig.R.X, big.NewInt(1))
    84  	badsig4.S = unblindsig.S
    85  	badsig4.r2 = unblindsig.r2
    86  	ok, _ = sc.Verify(msg, badsig4)
    87  	if ok {
    88  		t.Error("Verify must fail")
    89  	}
    90  
    91  	_, _ = Hm, unblindsig
    92  }