github.com/ronperry/cryptoedge@v0.0.0-20150815114006-cc363e290743/jcc/generic.go (about) 1 package jcc 2 3 //ToDo: Verify Scheme, PublicKey, DataType 4 //ToDo: Test for interface conversion errors 5 6 import ( 7 "github.com/ronperry/cryptoedge/eccutil" 8 "github.com/ronperry/cryptoedge/genericblinding" 9 ) 10 11 // GenericBlindingClient a blinding client using the generic interface 12 type GenericBlindingClient struct { 13 BlindingClient 14 } 15 16 // GenericBlindingServer is holds a blinding server 17 type GenericBlindingServer struct { 18 BlindingServer 19 } 20 21 // NewGenericBlindingClient returns a new GenericBlindingClient 22 func NewGenericBlindingClient(curve *eccutil.Curve, pubKey *eccutil.Point) *GenericBlindingClient { 23 bc := new(GenericBlindingClient) 24 bc.curve = curve 25 bc.PubKey = pubKey 26 return bc 27 } 28 29 // Blind returns a blinded message and the blinding factor. BlindingParamClient can be nil 30 func (client GenericBlindingClient) Blind(bpci genericblinding.BlindingParamClient, cmi genericblinding.ClearMessage) (genericblinding.BlindingFactors, genericblinding.BlindMessage, error) { 31 //bpc := bpci.(BlindingParamClient) // Nil anyways 32 _, err := genericblinding.MatchMessage(cmi, SchemeName, genericblinding.TypeClearMessage, client.PubKey) 33 if err != nil { 34 return nil, nil, err 35 } 36 cm, ok := cmi.(ClearMessage) 37 if !ok { 38 return nil, nil, genericblinding.ErrBadType 39 } 40 c := NewBlindingClient(client.curve, client.PubKey) 41 bmt, bft, err := c.Blind(cm.UniqueID()) 42 if err != nil { 43 return nil, nil, err 44 } 45 bf := NewBlindingFactors(client.PubKey) 46 bm := NewBlindMessage(client.PubKey) 47 bm.Message = *bmt 48 bf.Factor = bft 49 return bf, bm, nil 50 } 51 52 // Unblind unblinds a signature 53 func (client GenericBlindingClient) Unblind(bfi genericblinding.BlindingFactors, cmi genericblinding.ClearMessage, bsi genericblinding.BlindSignature) (genericblinding.ClearSignature, genericblinding.ClearMessage, error) { 54 _, err := genericblinding.MatchMessage(bfi, SchemeName, genericblinding.TypeBlindingFactors, client.PubKey) 55 if err != nil { 56 return nil, nil, err 57 } 58 bf, ok := bfi.(BlindingFactors) 59 if !ok { 60 return nil, nil, genericblinding.ErrBadType 61 } 62 63 _, err = genericblinding.MatchMessage(cmi, SchemeName, genericblinding.TypeClearMessage, client.PubKey) 64 if err != nil { 65 return nil, nil, err 66 } 67 cm, ok := cmi.(ClearMessage) 68 if !ok { 69 return nil, nil, genericblinding.ErrBadType 70 } 71 72 _, err = genericblinding.MatchMessage(bsi, SchemeName, genericblinding.TypeBlindSignature, client.PubKey) 73 if err != nil { 74 return nil, nil, err 75 } 76 bs, ok := bsi.(BlindSignature) 77 if !ok { 78 return nil, nil, genericblinding.ErrBadType 79 } 80 81 c := NewBlindingClient(client.curve, client.PubKey) 82 sb, mb := c.Unblind(bf.Factor, cm.UniqueID(), &bs.S) 83 cmo := NewClearMessage(mb) 84 csig := NewClearSignature(client.PubKey) 85 csig.R = bs.R 86 csig.SB = *sb 87 return csig, cmo, nil 88 } 89 90 // Verify a signature 91 func (client GenericBlindingClient) Verify(csi genericblinding.ClearSignature, cmi genericblinding.ClearMessage) (bool, error) { 92 _, err := genericblinding.MatchMessage(csi, SchemeName, genericblinding.TypeClearSignature, client.PubKey) 93 if err != nil { 94 return false, err 95 } 96 cs, ok := csi.(ClearSignature) 97 if !ok { 98 return false, genericblinding.ErrBadType 99 } 100 101 _, err = genericblinding.MatchMessage(cmi, SchemeName, genericblinding.TypeClearMessage, client.PubKey) 102 if err != nil { 103 return false, err 104 } 105 cm, ok := cmi.(ClearMessage) 106 if !ok { 107 return false, genericblinding.ErrBadType 108 } 109 c := NewBlindingClient(client.curve, client.PubKey) 110 return c.Verify(&cs.R, &cs.SB, cm.Message), nil 111 } 112 113 // NewGenericBlindingServer creates a new BlindingServer 114 func NewGenericBlindingServer(privkey []byte, pubkey *eccutil.Point, curve *eccutil.Curve, uniqueTest func([32]byte) bool) *GenericBlindingServer { 115 bs := new(GenericBlindingServer) 116 bs.PubKey = pubkey 117 bs.privKey = privkey 118 bs.curve = curve 119 bs.uniqueTest = uniqueTest 120 return bs 121 } 122 123 // Sign a message. BlindingParamServer can be nil (not used in JCC) 124 func (server GenericBlindingServer) Sign(bpsi genericblinding.BlindingParamServer, bmi genericblinding.BlindMessage) (genericblinding.BlindSignature, error) { 125 //bpsi is nil for this scheme, not tested 126 _, err := genericblinding.MatchMessage(bmi, SchemeName, genericblinding.TypeBlindMessage, server.PubKey) 127 if err != nil { 128 return nil, err 129 } 130 bm, ok := bmi.(BlindMessage) 131 if !ok { 132 return nil, genericblinding.ErrBadType 133 } 134 135 bs := NewBlindingServer(server.privKey, server.PubKey, server.curve, server.uniqueTest) 136 r, s, err := bs.Sign(&bm.Message) 137 if err != nil { 138 return nil, err 139 } 140 bsig := NewBlindSignature(server.PubKey) 141 bsig.R = *r 142 bsig.S = *s 143 return bsig, nil 144 } 145 146 // GetParams returns signature request parameters. Unused in JCC 147 func (server GenericBlindingServer) GetParams() (genericblinding.BlindingParamClient, genericblinding.BlindingParamServer, error) { 148 bpc := NewBlindingParamClient(server.PubKey) 149 bps := NewBlindingParamServer(server.PubKey) 150 return bpc, bps, nil 151 }