Skip to content

Commit

Permalink
[FAB-6221] Import BCCSP as third_party
Browse files Browse the repository at this point in the history
Change-Id: I1cdbbb37af1917bd0e6e0cded80caa6ba6c2c777
Signed-off-by: Troy Ronda <troy.ronda@securekey.com>
  • Loading branch information
troyronda committed Sep 19, 2017
1 parent eff58ec commit 860a3b5
Show file tree
Hide file tree
Showing 40 changed files with 5,680 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Gopkg.lock

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package bccsp

import "io"

// AES128KeyGenOpts contains options for AES key generation at 128 security level
type AES128KeyGenOpts struct {
Temporary bool
}

// Algorithm returns the key generation algorithm identifier (to be used).
func (opts *AES128KeyGenOpts) Algorithm() string {
return AES128
}

// Ephemeral returns true if the key to generate has to be ephemeral,
// false otherwise.
func (opts *AES128KeyGenOpts) Ephemeral() bool {
return opts.Temporary
}

// AES192KeyGenOpts contains options for AES key generation at 192 security level
type AES192KeyGenOpts struct {
Temporary bool
}

// Algorithm returns the key generation algorithm identifier (to be used).
func (opts *AES192KeyGenOpts) Algorithm() string {
return AES192
}

// Ephemeral returns true if the key to generate has to be ephemeral,
// false otherwise.
func (opts *AES192KeyGenOpts) Ephemeral() bool {
return opts.Temporary
}

// AES256KeyGenOpts contains options for AES key generation at 256 security level
type AES256KeyGenOpts struct {
Temporary bool
}

// Algorithm returns the key generation algorithm identifier (to be used).
func (opts *AES256KeyGenOpts) Algorithm() string {
return AES256
}

// Ephemeral returns true if the key to generate has to be ephemeral,
// false otherwise.
func (opts *AES256KeyGenOpts) Ephemeral() bool {
return opts.Temporary
}

// AESCBCPKCS7ModeOpts contains options for AES encryption in CBC mode
// with PKCS7 padding.
// Notice that both IV and PRNG can be nil. In that case, the BCCSP implementation
// is supposed to sample the IV using a cryptographic secure PRNG.
// Notice also that either IV or PRNG can be different from nil.
type AESCBCPKCS7ModeOpts struct {
// IV is the initialization vector to be used by the underlying cipher.
// The length of IV must be the same as the Block's block size.
// It is used only if different from nil.
IV []byte
// PRNG is an instance of a PRNG to be used by the underlying cipher.
// It is used only if different from nil.
PRNG io.Reader
}
144 changes: 144 additions & 0 deletions third_party/github.com/hyperledger/fabric/bccsp/bccsp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package bccsp

import (
"crypto"
"hash"
)

// Key represents a cryptographic key
type Key interface {

// Bytes converts this key to its byte representation,
// if this operation is allowed.
Bytes() ([]byte, error)

// SKI returns the subject key identifier of this key.
SKI() []byte

// Symmetric returns true if this key is a symmetric key,
// false is this key is asymmetric
Symmetric() bool

// Private returns true if this key is a private key,
// false otherwise.
Private() bool

// PublicKey returns the corresponding public key part of an asymmetric public/private key pair.
// This method returns an error in symmetric key schemes.
PublicKey() (Key, error)
}

// KeyGenOpts contains options for key-generation with a CSP.
type KeyGenOpts interface {

// Algorithm returns the key generation algorithm identifier (to be used).
Algorithm() string

// Ephemeral returns true if the key to generate has to be ephemeral,
// false otherwise.
Ephemeral() bool
}

// KeyDerivOpts contains options for key-derivation with a CSP.
type KeyDerivOpts interface {

// Algorithm returns the key derivation algorithm identifier (to be used).
Algorithm() string

// Ephemeral returns true if the key to derived has to be ephemeral,
// false otherwise.
Ephemeral() bool
}

// KeyImportOpts contains options for importing the raw material of a key with a CSP.
type KeyImportOpts interface {

// Algorithm returns the key importation algorithm identifier (to be used).
Algorithm() string

// Ephemeral returns true if the key generated has to be ephemeral,
// false otherwise.
Ephemeral() bool
}

// HashOpts contains options for hashing with a CSP.
type HashOpts interface {

// Algorithm returns the hash algorithm identifier (to be used).
Algorithm() string
}

// SignerOpts contains options for signing with a CSP.
type SignerOpts interface {
crypto.SignerOpts
}

// EncrypterOpts contains options for encrypting with a CSP.
type EncrypterOpts interface{}

// DecrypterOpts contains options for decrypting with a CSP.
type DecrypterOpts interface{}

// BCCSP is the blockchain cryptographic service provider that offers
// the implementation of cryptographic standards and algorithms.
type BCCSP interface {

// KeyGen generates a key using opts.
KeyGen(opts KeyGenOpts) (k Key, err error)

// KeyDeriv derives a key from k using opts.
// The opts argument should be appropriate for the primitive used.
KeyDeriv(k Key, opts KeyDerivOpts) (dk Key, err error)

// KeyImport imports a key from its raw representation using opts.
// The opts argument should be appropriate for the primitive used.
KeyImport(raw interface{}, opts KeyImportOpts) (k Key, err error)

// GetKey returns the key this CSP associates to
// the Subject Key Identifier ski.
GetKey(ski []byte) (k Key, err error)

// Hash hashes messages msg using options opts.
// If opts is nil, the default hash function will be used.
Hash(msg []byte, opts HashOpts) (hash []byte, err error)

// GetHash returns and instance of hash.Hash using options opts.
// If opts is nil, the default hash function will be returned.
GetHash(opts HashOpts) (h hash.Hash, err error)

// Sign signs digest using key k.
// The opts argument should be appropriate for the algorithm used.
//
// Note that when a signature of a hash of a larger message is needed,
// the caller is responsible for hashing the larger message and passing
// the hash (as digest).
Sign(k Key, digest []byte, opts SignerOpts) (signature []byte, err error)

// Verify verifies signature against key k and digest
// The opts argument should be appropriate for the algorithm used.
Verify(k Key, signature, digest []byte, opts SignerOpts) (valid bool, err error)

// Encrypt encrypts plaintext using key k.
// The opts argument should be appropriate for the algorithm used.
Encrypt(k Key, plaintext []byte, opts EncrypterOpts) (ciphertext []byte, err error)

// Decrypt decrypts ciphertext using key k.
// The opts argument should be appropriate for the algorithm used.
Decrypt(k Key, ciphertext []byte, opts DecrypterOpts) (plaintext []byte, err error)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package bccsp

// ECDSAP256KeyGenOpts contains options for ECDSA key generation with curve P-256.
type ECDSAP256KeyGenOpts struct {
Temporary bool
}

// Algorithm returns the key generation algorithm identifier (to be used).
func (opts *ECDSAP256KeyGenOpts) Algorithm() string {
return ECDSAP256
}

// Ephemeral returns true if the key to generate has to be ephemeral,
// false otherwise.
func (opts *ECDSAP256KeyGenOpts) Ephemeral() bool {
return opts.Temporary
}

// ECDSAP384KeyGenOpts contains options for ECDSA key generation with curve P-384.
type ECDSAP384KeyGenOpts struct {
Temporary bool
}

// Algorithm returns the key generation algorithm identifier (to be used).
func (opts *ECDSAP384KeyGenOpts) Algorithm() string {
return ECDSAP384
}

// Ephemeral returns true if the key to generate has to be ephemeral,
// false otherwise.
func (opts *ECDSAP384KeyGenOpts) Ephemeral() bool {
return opts.Temporary
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package factory

import (
"sync"

"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/bccsp"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/common/flogging"
"github.com/pkg/errors"
)

var (
// Default BCCSP
defaultBCCSP bccsp.BCCSP

// when InitFactories has not been called yet (should only happen
// in test cases), use this BCCSP temporarily
bootBCCSP bccsp.BCCSP

// BCCSP Factories
bccspMap map[string]bccsp.BCCSP

// factories' Sync on Initialization
factoriesInitOnce sync.Once
bootBCCSPInitOnce sync.Once

// Factories' Initialization Error
factoriesInitError error

logger = flogging.MustGetLogger("bccsp")
)

// BCCSPFactory is used to get instances of the BCCSP interface.
// A Factory has name used to address it.
type BCCSPFactory interface {

// Name returns the name of this factory
Name() string

// Get returns an instance of BCCSP using opts.
Get(opts *FactoryOpts) (bccsp.BCCSP, error)
}

// GetDefault returns a non-ephemeral (long-term) BCCSP
func GetDefault() bccsp.BCCSP {
if defaultBCCSP == nil {
logger.Warning("Before using BCCSP, please call InitFactories(). Falling back to bootBCCSP.")
bootBCCSPInitOnce.Do(func() {
var err error
f := &SWFactory{}
bootBCCSP, err = f.Get(GetDefaultOpts())
if err != nil {
panic("BCCSP Internal error, failed initialization with GetDefaultOpts!")
}
})
return bootBCCSP
}
return defaultBCCSP
}

// GetBCCSP returns a BCCSP created according to the options passed in input.
func GetBCCSP(name string) (bccsp.BCCSP, error) {
csp, ok := bccspMap[name]
if !ok {
return nil, errors.Errorf("Could not find BCCSP, no '%s' provider", name)
}
return csp, nil
}

func initBCCSP(f BCCSPFactory, config *FactoryOpts) error {
csp, err := f.Get(config)
if err != nil {
return errors.Errorf("Could not initialize BCCSP %s [%s]", f.Name(), err)
}

logger.Debugf("Initialize BCCSP [%s]", f.Name())
bccspMap[f.Name()] = csp
return nil
}
Loading

0 comments on commit 860a3b5

Please sign in to comment.