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

     1  package jjm
     2  
     3  import (
     4  	"crypto/elliptic"
     5  	"crypto/rand"
     6  	"github.com/ronperry/cryptoedge/eccutil"
     7  	"testing"
     8  )
     9  
    10  func Test_GenericRequest(t *testing.T) {
    11  	c := eccutil.SetCurve(elliptic.P256, rand.Reader, eccutil.Sha1Hash)
    12  	privkey, pubkey, err := c.GenerateKey()
    13  	if err != nil {
    14  		t.Fatalf("Error creating keys: %s", err)
    15  	}
    16  	signer := NewGenericBlindingServer(privkey, pubkey, c)
    17  	clientParams, serverParams, err := signer.GetParams()
    18  	if err != nil {
    19  		t.Errorf("Error occured throughout parameter creation: %s", err)
    20  	}
    21  	_, _ = clientParams, serverParams
    22  }
    23  
    24  func Test_GenericBlind(t *testing.T) {
    25  	msg := []byte("Message to be blinded")
    26  	c := eccutil.SetCurve(elliptic.P256, rand.Reader, eccutil.Sha1Hash)
    27  	privkey, pubkey, err := c.GenerateKey()
    28  	if err != nil {
    29  		t.Fatalf("Error creating keys: %s", err)
    30  	}
    31  	signer := NewGenericBlindingServer(privkey, pubkey, c)
    32  	client := NewGenericBlindingClient(pubkey, c)
    33  
    34  	clientParams, serverParams, err := signer.GetParams()
    35  	if err != nil {
    36  		t.Errorf("Error occured throughout parameter creation: %s", err)
    37  	}
    38  	cm := NewClearMessage(msg)
    39  	clientFactors, blindMessage, err := client.Blind(clientParams, cm)
    40  	if err != nil {
    41  		t.Errorf("Error occured throughout blinding: %s", err)
    42  	}
    43  	_, _, _, _, _ = clientParams, serverParams, client, clientFactors, blindMessage
    44  }
    45  
    46  func Test_GenericSign(t *testing.T) {
    47  	msg := []byte("Message to be blinded")
    48  	c := eccutil.SetCurve(elliptic.P256, rand.Reader, eccutil.Sha1Hash)
    49  	privkey, pubkey, err := c.GenerateKey()
    50  	if err != nil {
    51  		t.Fatalf("Error creating keys: %s", err)
    52  	}
    53  	signer := NewGenericBlindingServer(privkey, pubkey, c)
    54  	client := NewGenericBlindingClient(pubkey, c)
    55  
    56  	clientParams, serverParams, err := signer.GetParams()
    57  	if err != nil {
    58  		t.Errorf("Error occured throughout parameter creation: %s", err)
    59  	}
    60  	cm := NewClearMessage(msg)
    61  	clientFactors, blindMessage, err := client.Blind(clientParams, cm)
    62  	if err != nil {
    63  		t.Errorf("Error occured throughout blinding: %s", err)
    64  	}
    65  	blindsignature, err := signer.Sign(serverParams, blindMessage)
    66  	if err != nil {
    67  		t.Errorf("Error occured throughout signing: %s", err)
    68  	}
    69  	_, _, _, _, _ = clientParams, blindsignature, client, clientFactors, blindMessage
    70  }
    71  
    72  func Test_GenericUnBlind(t *testing.T) {
    73  	msg := []byte("Message to be blinded")
    74  	c := eccutil.SetCurve(elliptic.P256, rand.Reader, eccutil.Sha1Hash)
    75  	privkey, pubkey, err := c.GenerateKey()
    76  	if err != nil {
    77  		t.Fatalf("Error creating keys: %s", err)
    78  	}
    79  	signer := NewGenericBlindingServer(privkey, pubkey, c)
    80  	client := NewGenericBlindingClient(pubkey, c)
    81  
    82  	clientParams, serverParams, err := signer.GetParams()
    83  	if err != nil {
    84  		t.Errorf("Error occured throughout parameter creation: %s", err)
    85  	}
    86  	cm := NewClearMessage(msg)
    87  	clientFactors, blindMessage, err := client.Blind(clientParams, cm)
    88  	if err != nil {
    89  		t.Errorf("Error occured throughout blinding: %s", err)
    90  	}
    91  	blindsignature, err := signer.Sign(serverParams, blindMessage)
    92  	if err != nil {
    93  		t.Errorf("Error occured throughout signing: %s", err)
    94  	}
    95  	clearsig, clearmsg, err := client.Unblind(clientFactors, cm, blindsignature)
    96  	if err != nil {
    97  		t.Errorf("Error occured throughout unblinding: %s", err)
    98  	}
    99  	_, _, _, _, _ = clearsig, clearmsg, client, clientFactors, blindMessage
   100  }
   101  
   102  func Test_GenericVerify(t *testing.T) {
   103  	msg := []byte("Message to be blinded")
   104  	c := eccutil.SetCurve(elliptic.P256, rand.Reader, eccutil.Sha1Hash)
   105  	privkey, pubkey, err := c.GenerateKey()
   106  	if err != nil {
   107  		t.Fatalf("Error creating keys: %s", err)
   108  	}
   109  	signer := NewGenericBlindingServer(privkey, pubkey, c)
   110  	client := NewGenericBlindingClient(pubkey, c)
   111  
   112  	clientParams, serverParams, err := signer.GetParams()
   113  	if err != nil {
   114  		t.Errorf("Error occured throughout parameter creation: %s", err)
   115  	}
   116  	cm := NewClearMessage(msg)
   117  	clientFactors, blindMessage, err := client.Blind(clientParams, cm)
   118  	if err != nil {
   119  		t.Errorf("Error occured throughout blinding: %s", err)
   120  	}
   121  	blindsignature, err := signer.Sign(serverParams, blindMessage)
   122  	if err != nil {
   123  		t.Errorf("Error occured throughout signing: %s", err)
   124  	}
   125  	clearsig, clearmsg, err := client.Unblind(clientFactors, cm, blindsignature)
   126  	if err != nil {
   127  		t.Errorf("Error occured throughout unblinding: %s", err)
   128  	}
   129  	ok, err := client.Verify(clearsig, clearmsg)
   130  	if err != nil {
   131  		t.Errorf("Error occured throughout verify: %s", err)
   132  	}
   133  	if !ok {
   134  		t.Error("Message does not verify")
   135  	}
   136  	_, _, _, _, _ = clearsig, clearmsg, client, clientFactors, blindMessage
   137  }