Skip to content

Commit

Permalink
[FAB-9672] Idemix User Key Generator
Browse files Browse the repository at this point in the history
This change-set does the following:
- implement the idemix user key generator
- tests

Change-Id: Iaa2b1b1fda5eee2278cd45e235bf17489f8351a2
Signed-off-by: Angelo De Caro <adc@zurich.ibm.com>
  • Loading branch information
adecaro committed Nov 4, 2018
1 parent 0125e23 commit a8fc602
Show file tree
Hide file tree
Showing 6 changed files with 389 additions and 0 deletions.
12 changes: 12 additions & 0 deletions bccsp/idemix/idemix.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,15 @@ type Issuer interface {
// NewKey generates a new idemix issuer key w.r.t the passed attribute names.
NewKey(AttributeNames []string) (IssuerSecretKey, error)
}

// Big represent a big integer
type Big interface {
// Bytes returns the byte representation of this key
Bytes() ([]byte, error)
}

// User is a local interface to decouple from the idemix implementation
type User interface {
// NewKey generates a new User secret key
NewKey() (Big, error)
}
2 changes: 2 additions & 0 deletions bccsp/idemix/idemix_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
//go:generate counterfeiter -o mock/issuer.go -fake-name Issuer . Issuer
//go:generate counterfeiter -o mock/issuer_secret_key.go -fake-name IssuerSecretKey . IssuerSecretKey
//go:generate counterfeiter -o mock/issuer_public_key.go -fake-name IssuerPublicKey . IssuerPublicKey
//go:generate counterfeiter -o mock/user.go -fake-name User . User
//go:generate counterfeiter -o mock/big.go -fake-name Big . Big

func TestPlain(t *testing.T) {
RegisterFailHandler(Fail)
Expand Down
93 changes: 93 additions & 0 deletions bccsp/idemix/mock/big.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

93 changes: 93 additions & 0 deletions bccsp/idemix/mock/user.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 72 additions & 0 deletions bccsp/idemix/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package idemix

import (
"crypto/sha256"

"github.com/hyperledger/fabric/bccsp"
"github.com/pkg/errors"
)

// userSecretKey contains the User secret key
type userSecretKey struct {
// sk is the idemix reference to the User key
sk Big
// Exportable if true, sk can be exported via the Bytes function
exportable bool
}

func NewUserSecretKey(sk Big, exportable bool) *userSecretKey {
return &userSecretKey{sk: sk, exportable: exportable}
}

func (k *userSecretKey) Bytes() ([]byte, error) {
if k.exportable {
return k.sk.Bytes()
}

return nil, errors.New("not exportable")
}

func (k *userSecretKey) SKI() []byte {
raw, err := k.sk.Bytes()
if err != nil {
return nil
}
hash := sha256.New()
hash.Write(raw)
return hash.Sum(nil)
}

func (*userSecretKey) Symmetric() bool {
return true
}

func (*userSecretKey) Private() bool {
return true
}

func (k *userSecretKey) PublicKey() (bccsp.Key, error) {
return nil, errors.New("cannot call this method on a symmetric key")
}

type UserKeyGen struct {
// Exportable is a flag to allow an issuer secret key to be marked as Exportable.
// If a secret key is marked as Exportable, its Bytes method will return the key's byte representation.
Exportable bool
// User implements the underlying cryptographic algorithms
User User
}

func (g *UserKeyGen) KeyGen(opts bccsp.KeyGenOpts) (bccsp.Key, error) {
sk, err := g.User.NewKey()
if err != nil {
return nil, err
}

return &userSecretKey{exportable: g.Exportable, sk: sk}, nil
}
Loading

0 comments on commit a8fc602

Please sign in to comment.