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

     1  package jcc
     2  
     3  import (
     4  	"crypto/elliptic"
     5  	"crypto/rand"
     6  	"github.com/ronperry/cryptoedge/eccutil"
     7  	"testing"
     8  )
     9  
    10  func TestSetCurve(t *testing.T) {
    11  	c := eccutil.SetCurve(elliptic.P256, rand.Reader, eccutil.Sha1Hash)
    12  	_ = c
    13  	t.Skip()
    14  }
    15  
    16  func TestGenerateKey(t *testing.T) {
    17  	c := eccutil.SetCurve(elliptic.P256, rand.Reader, eccutil.Sha1Hash)
    18  	priv, pub, err := c.GenerateKey()
    19  	if err != nil {
    20  		t.Errorf("Generate key failed: %s", err)
    21  	}
    22  	_, _ = priv, pub
    23  }
    24  
    25  func TestBlind(t *testing.T) {
    26  	msg := []byte("Random message without meaning, should be unique")
    27  	c := eccutil.SetCurve(elliptic.P256, rand.Reader, eccutil.Sha1Hash)
    28  	bc := NewBlindingClient(c, nil)
    29  	bmsg, bfac, err := bc.Blind(msg)
    30  	if err != nil {
    31  		t.Errorf("Blinding failed: %s", err)
    32  	}
    33  	_, _ = bmsg, bfac
    34  }
    35  
    36  func TestSign(t *testing.T) {
    37  	msg := []byte("Random message without meaning, should be unique")
    38  	c := eccutil.SetCurve(elliptic.P256, rand.Reader, eccutil.Sha1Hash)
    39  	sigpriv, sigpub, err := c.GenerateKey()
    40  	if err != nil {
    41  		t.Errorf("Signer key gen failed: %s", err)
    42  	}
    43  
    44  	bc := NewBlindingClient(c, sigpub)
    45  	bmsg, bfac, err := bc.Blind(msg)
    46  	if err != nil {
    47  		t.Errorf("Blinding failed: %s", err)
    48  	}
    49  
    50  	bs := NewBlindingServer(sigpriv, sigpub, c, Fakeunique)
    51  
    52  	r, s, err := bs.Sign(bmsg)
    53  	if err != nil {
    54  		t.Errorf("Signature failed")
    55  	}
    56  	_, _ = r, s
    57  	_, _ = bmsg, bfac
    58  }
    59  
    60  func TestUnblind(t *testing.T) {
    61  	msg := []byte("Random message without meaning, should be unique")
    62  	c := eccutil.SetCurve(elliptic.P256, rand.Reader, eccutil.Sha1Hash)
    63  	sigpriv, sigpub, err := c.GenerateKey()
    64  	if err != nil {
    65  		t.Errorf("Signer key gen failed: %s", err)
    66  	}
    67  
    68  	bc := NewBlindingClient(c, sigpub)
    69  	bmsg, bfac, err := bc.Blind(msg)
    70  	if err != nil {
    71  		t.Errorf("Blinding failed: %s", err)
    72  	}
    73  	bs := NewBlindingServer(sigpriv, sigpub, c, Fakeunique)
    74  
    75  	r, s, err := bs.Sign(bmsg)
    76  	if err != nil {
    77  		t.Errorf("Signature failed")
    78  	}
    79  	st, mt := bc.Unblind(bfac, msg, s)
    80  	_, _, _ = st, mt, r
    81  	_, _ = r, s
    82  	_, _ = bmsg, bfac
    83  }
    84  
    85  func TestVerify(t *testing.T) {
    86  	msg := []byte("Random message without meaning, should be unique")
    87  	c := eccutil.SetCurve(elliptic.P256, rand.Reader, eccutil.Sha1Hash)
    88  	sigpriv, sigpub, err := c.GenerateKey()
    89  	if err != nil {
    90  		t.Errorf("Signer key gen failed: %s", err)
    91  	}
    92  
    93  	bc := NewBlindingClient(c, sigpub)
    94  	bmsg, bfac, err := bc.Blind(msg)
    95  	if err != nil {
    96  		t.Errorf("Blinding failed: %s", err)
    97  	}
    98  	bs := NewBlindingServer(sigpriv, sigpub, c, Fakeunique)
    99  
   100  	r, s, err := bs.Sign(bmsg)
   101  	if err != nil {
   102  		t.Errorf("Signature failed")
   103  	}
   104  	st, mt := bc.Unblind(bfac, msg, s)
   105  	ok := bc.Verify(r, st, mt)
   106  	if !ok {
   107  		t.Errorf("Signature verification failed")
   108  	}
   109  }
   110  
   111  func Test_ProtocolVerifyOneMessage(t *testing.T) {
   112  	msga := []byte("Random message without meaning, should be unique")
   113  	// Setup
   114  	c := eccutil.SetCurve(elliptic.P256, rand.Reader, eccutil.Sha1Hash)
   115  	sigpriv, sigpub, err := c.GenerateKey()
   116  	if err != nil {
   117  		t.Errorf("Signer key gen failed: %s", err)
   118  	}
   119  	bc := NewBlindingClient(c, sigpub)
   120  	bs := NewBlindingServer(sigpriv, sigpub, c, Fakeunique)
   121  
   122  	// generate two signatures per message
   123  	bmsga1, bfaca1, err := bc.Blind(msga)
   124  	if err != nil {
   125  		t.Errorf("Blinding failed: %s", err)
   126  	}
   127  	ra11, sa11, err := bs.Sign(bmsga1)
   128  	if err != nil {
   129  		t.Errorf("Signature failed")
   130  	}
   131  	ra12, sa12, err := bs.Sign(bmsga1)
   132  	if err != nil {
   133  		t.Errorf("Signature failed")
   134  	}
   135  	sta11, mta11 := bc.Unblind(bfaca1, msga, sa11)
   136  	ok := bc.Verify(ra11, sta11, mta11)
   137  	if !ok {
   138  		t.Errorf("Signature verification failed")
   139  	}
   140  	sta12, mta12 := bc.Unblind(bfaca1, msga, sa12)
   141  	ok = bc.Verify(ra12, sta12, mta12)
   142  	if !ok {
   143  		t.Errorf("Signature verification failed")
   144  	}
   145  
   146  	ok = bc.Verify(ra11, sta12, mta12)
   147  	if ok {
   148  		t.Errorf("Signature must fail on r-switch")
   149  	}
   150  	ok = bc.Verify(ra12, sta11, mta12)
   151  	if ok {
   152  		t.Errorf("Signature must fail on s-switch")
   153  	}
   154  	ok = bc.Verify(ra11, sta11, mta12)
   155  	if !ok {
   156  		t.Errorf("Signature must work on signature over same message")
   157  	}
   158  }
   159  
   160  func Test_ProtocolVerifyTwoMessages(t *testing.T) {
   161  	msga := []byte("Random message without meaning, should be unique")
   162  	msgb := []byte("Another message without meaning, should be unique")
   163  	// Setup
   164  	c := eccutil.SetCurve(elliptic.P256, rand.Reader, eccutil.Sha1Hash)
   165  	sigpriv, sigpub, err := c.GenerateKey()
   166  	if err != nil {
   167  		t.Errorf("Signer key gen failed: %s", err)
   168  	}
   169  	bc := NewBlindingClient(c, sigpub)
   170  	bs := NewBlindingServer(sigpriv, sigpub, c, Fakeunique)
   171  
   172  	// generate two signatures per message
   173  	bmsga1, bfaca1, err := bc.Blind(msga)
   174  	if err != nil {
   175  		t.Errorf("Blinding failed: %s", err)
   176  	}
   177  	bmsgb1, bfacb1, err := bc.Blind(msgb)
   178  	if err != nil {
   179  		t.Errorf("Blinding failed: %s", err)
   180  	}
   181  	ra11, sa11, err := bs.Sign(bmsga1)
   182  	if err != nil {
   183  		t.Errorf("Signature failed")
   184  	}
   185  	rb11, sb11, err := bs.Sign(bmsgb1)
   186  	if err != nil {
   187  		t.Errorf("Signature failed")
   188  	}
   189  	sta11, mta11 := bc.Unblind(bfaca1, msga, sa11)
   190  	ok := bc.Verify(ra11, sta11, mta11)
   191  	if !ok {
   192  		t.Errorf("Signature verification failed")
   193  	}
   194  	stb11, mtb11 := bc.Unblind(bfacb1, msgb, sb11)
   195  	ok = bc.Verify(rb11, stb11, mtb11)
   196  	if !ok {
   197  		t.Errorf("Signature verification failed")
   198  	}
   199  
   200  	ok = bc.Verify(ra11, stb11, mta11)
   201  	if ok {
   202  		t.Errorf("Signature must fail on r-switch")
   203  	}
   204  	ok = bc.Verify(rb11, sta11, mta11)
   205  	if ok {
   206  		t.Errorf("Signature must fail on s-switch")
   207  	}
   208  	ok = bc.Verify(ra11, sta11, mtb11)
   209  	if ok {
   210  		t.Errorf("Signature must fail on different message")
   211  	}
   212  	_ = sb11
   213  }