-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FAB-3441] bccsp/sw KeyGen test coverage
Using the approach discussed in FAB-3465, this change-sets refactors the way key generatiion is done at bccsp/sw. Essentially, the switch has been replaced by a map. The approach decouples the testing of the bccsp interface implementation from the cryptographic algorithms. Test-coverage of the entire bccsp/sw is now at more than 85%y Change-Id: I6447cb6774a6dfcbd3139b22a7cc62666f39382d Signed-off-by: Angelo De Caro <adc@zurich.ibm.com>
- Loading branch information
Showing
8 changed files
with
237 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
Copyright IBM Corp. 2017 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 sw | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
"crypto/elliptic" | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"fmt" | ||
|
||
"github.com/hyperledger/fabric/bccsp" | ||
) | ||
|
||
type ecdsaKeyGenerator struct { | ||
curve elliptic.Curve | ||
} | ||
|
||
func (kg *ecdsaKeyGenerator) KeyGen(opts bccsp.KeyGenOpts) (k bccsp.Key, err error) { | ||
privKey, err := ecdsa.GenerateKey(kg.curve, rand.Reader) | ||
if err != nil { | ||
return nil, fmt.Errorf("Failed generating ECDSA key for [%v]: [%s]", kg.curve, err) | ||
} | ||
|
||
return &ecdsaPrivateKey{privKey}, nil | ||
} | ||
|
||
type aesKeyGenerator struct { | ||
length int | ||
} | ||
|
||
func (kg *aesKeyGenerator) KeyGen(opts bccsp.KeyGenOpts) (k bccsp.Key, err error) { | ||
lowLevelKey, err := GetRandomBytes(int(kg.length)) | ||
if err != nil { | ||
return nil, fmt.Errorf("Failed generating AES %d key [%s]", kg.length, err) | ||
} | ||
|
||
return &aesPrivateKey{lowLevelKey, false}, nil | ||
} | ||
|
||
type rsaKeyGenerator struct { | ||
length int | ||
} | ||
|
||
func (kg *rsaKeyGenerator) KeyGen(opts bccsp.KeyGenOpts) (k bccsp.Key, err error) { | ||
lowLevelKey, err := rsa.GenerateKey(rand.Reader, int(kg.length)) | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("Failed generating RSA %d key [%s]", kg.length, err) | ||
} | ||
|
||
return &rsaPrivateKey{lowLevelKey}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
Copyright IBM Corp. 2017 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 sw | ||
|
||
import ( | ||
"crypto/elliptic" | ||
"errors" | ||
"reflect" | ||
"testing" | ||
|
||
mocks2 "github.com/hyperledger/fabric/bccsp/mocks" | ||
"github.com/hyperledger/fabric/bccsp/sw/mocks" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestKeyGen(t *testing.T) { | ||
expectedOpts := &mocks2.KeyGenOpts{EphemeralValue: true} | ||
expectetValue := &mocks2.MockKey{} | ||
expectedErr := errors.New("no error") | ||
|
||
keyGenerators := make(map[reflect.Type]KeyGenerator) | ||
keyGenerators[reflect.TypeOf(&mocks2.KeyGenOpts{})] = &mocks.KeyGenerator{ | ||
OptsArg: expectedOpts, | ||
Value: expectetValue, | ||
Err: expectedErr, | ||
} | ||
csp := impl{keyGenerators: keyGenerators} | ||
value, err := csp.KeyGen(expectedOpts) | ||
assert.Equal(t, nil, value) | ||
assert.Equal(t, expectedErr, err) | ||
|
||
keyGenerators = make(map[reflect.Type]KeyGenerator) | ||
keyGenerators[reflect.TypeOf(&mocks2.KeyGenOpts{})] = &mocks.KeyGenerator{ | ||
OptsArg: expectedOpts, | ||
Value: expectetValue, | ||
Err: nil, | ||
} | ||
csp = impl{keyGenerators: keyGenerators} | ||
value, err = csp.KeyGen(expectedOpts) | ||
assert.Equal(t, expectetValue, value) | ||
assert.Equal(t, nil, err) | ||
|
||
} | ||
|
||
func TestECDSAKeyGenerator(t *testing.T) { | ||
kg := &ecdsaKeyGenerator{curve: elliptic.P256()} | ||
|
||
k, err := kg.KeyGen(nil) | ||
assert.NoError(t, err) | ||
|
||
ecdsaK, ok := k.(*ecdsaPrivateKey) | ||
assert.True(t, ok) | ||
assert.NotNil(t, ecdsaK.privKey) | ||
assert.Equal(t, ecdsaK.privKey.Curve, elliptic.P256()) | ||
} | ||
|
||
func TestRSAKeyGenerator(t *testing.T) { | ||
kg := &rsaKeyGenerator{length: 512} | ||
|
||
k, err := kg.KeyGen(nil) | ||
assert.NoError(t, err) | ||
|
||
rsaK, ok := k.(*rsaPrivateKey) | ||
assert.True(t, ok) | ||
assert.NotNil(t, rsaK.privKey) | ||
assert.Equal(t, rsaK.privKey.N.BitLen(), 512) | ||
} | ||
|
||
func TestAESKeyGenerator(t *testing.T) { | ||
kg := &aesKeyGenerator{length: 32} | ||
|
||
k, err := kg.KeyGen(nil) | ||
assert.NoError(t, err) | ||
|
||
aesK, ok := k.(*aesPrivateKey) | ||
assert.True(t, ok) | ||
assert.NotNil(t, aesK.privKey) | ||
assert.Equal(t, len(aesK.privKey), 32) | ||
} | ||
|
||
func TestAESKeyGeneratorInvalidInputs(t *testing.T) { | ||
kg := &aesKeyGenerator{length: -1} | ||
|
||
_, err := kg.KeyGen(nil) | ||
assert.Error(t, err) | ||
assert.Contains(t, err.Error(), "Len must be larger than 0") | ||
} | ||
|
||
func TestRSAKeyGeneratorInvalidInputs(t *testing.T) { | ||
kg := &rsaKeyGenerator{length: -1} | ||
|
||
_, err := kg.KeyGen(nil) | ||
assert.Error(t, err) | ||
assert.Contains(t, err.Error(), "Failed generating RSA -1 key") | ||
} |
Oops, something went wrong.