@@ -30,6 +30,8 @@ import (
30
30
31
31
"crypto/x509"
32
32
33
+ "crypto/hmac"
34
+
33
35
"github.com/hyperledger/fabric/core/crypto/bccsp"
34
36
"github.com/hyperledger/fabric/core/crypto/primitives"
35
37
"github.com/hyperledger/fabric/core/crypto/utils"
@@ -40,10 +42,17 @@ var (
40
42
logger = logging .MustGetLogger ("SW_BCCSP" )
41
43
)
42
44
43
- // New returns a new instance of the software-based BCCSP.
44
- func New () (bccsp.BCCSP , error ) {
45
+ // NewDefaultSecurityLevel returns a new instance of the software-based BCCSP
46
+ // at security level 256 and hash family SHA2
47
+ func NewDefaultSecurityLevel () (bccsp.BCCSP , error ) {
48
+ return New (256 , "SHA2" )
49
+ }
50
+
51
+ // New returns a new instance of the software-based BCCSP
52
+ // set at the passed security level and hash family.
53
+ func New (securityLevel int , hashFamily string ) (bccsp.BCCSP , error ) {
45
54
conf := & config {}
46
- err := conf .init ()
55
+ err := conf .init (securityLevel , hashFamily )
47
56
if err != nil {
48
57
return nil , fmt .Errorf ("Failed initializing configuration [%s]" , err )
49
58
}
@@ -52,14 +61,16 @@ func New() (bccsp.BCCSP, error) {
52
61
if err := ks .init (nil , conf ); err != nil {
53
62
return nil , fmt .Errorf ("Failed initializing key store [%s]" , err )
54
63
}
55
- return & impl {ks }, nil
64
+ return & impl {conf , ks }, nil
56
65
}
57
66
58
67
// SoftwareBasedBCCSP is the software-based implementation of the BCCSP.
59
- // It is based on code used in the primitives package.
68
+ // It uses util code in the primitives package but does not depend on the
69
+ // initialization of that package.
60
70
// It can be configured via viper.
61
71
type impl struct {
62
- ks * keyStore
72
+ conf * config
73
+ ks * keyStore
63
74
}
64
75
65
76
// KeyGen generates a key using opts.
@@ -72,7 +83,7 @@ func (csp *impl) KeyGen(opts bccsp.KeyGenOpts) (k bccsp.Key, err error) {
72
83
// Parse algorithm
73
84
switch opts .Algorithm () {
74
85
case bccsp .ECDSA :
75
- lowLevelKey , err := primitives . NewECDSAKey ( )
86
+ lowLevelKey , err := ecdsa . GenerateKey ( csp . conf . ellipticCurve , rand . Reader )
76
87
if err != nil {
77
88
return nil , fmt .Errorf ("Failed generating ECDSA key [%s]" , err )
78
89
}
@@ -90,7 +101,7 @@ func (csp *impl) KeyGen(opts bccsp.KeyGenOpts) (k bccsp.Key, err error) {
90
101
91
102
return k , nil
92
103
case bccsp .AES :
93
- lowLevelKey , err := primitives .GenAESKey ( )
104
+ lowLevelKey , err := primitives .GetRandomBytes ( csp . conf . aesBitLength )
94
105
95
106
if err != nil {
96
107
return nil , fmt .Errorf ("Failed generating AES key [%s]" , err )
@@ -109,7 +120,7 @@ func (csp *impl) KeyGen(opts bccsp.KeyGenOpts) (k bccsp.Key, err error) {
109
120
110
121
return k , nil
111
122
case bccsp .RSA :
112
- lowLevelKey , err := primitives . NewRSAKey ( )
123
+ lowLevelKey , err := rsa . GenerateKey ( rand . Reader , csp . conf . rsaBitLength )
113
124
114
125
if err != nil {
115
126
return nil , fmt .Errorf ("Failed generating RSA (2048) key [%s]" , err )
@@ -216,7 +227,9 @@ func (csp *impl) KeyDeriv(k bccsp.Key, opts bccsp.KeyDerivOpts) (dk bccsp.Key, e
216
227
case * bccsp.HMACTruncated256AESDeriveKeyOpts :
217
228
hmacOpts := opts .(* bccsp.HMACTruncated256AESDeriveKeyOpts )
218
229
219
- hmacedKey := & aesPrivateKey {primitives .HMACAESTruncated (aesK .k , hmacOpts .Argument ()), false }
230
+ mac := hmac .New (csp .conf .hashFunction , aesK .k )
231
+ mac .Write (hmacOpts .Argument ())
232
+ hmacedKey := & aesPrivateKey {mac .Sum (nil )[:csp .conf .aesBitLength ], false }
220
233
221
234
// If the key is not Ephemeral, store it.
222
235
if ! opts .Ephemeral () {
@@ -233,7 +246,9 @@ func (csp *impl) KeyDeriv(k bccsp.Key, opts bccsp.KeyDerivOpts) (dk bccsp.Key, e
233
246
234
247
hmacOpts := opts .(* bccsp.HMACDeriveKeyOpts )
235
248
236
- hmacedKey := & aesPrivateKey {primitives .HMAC (aesK .k , hmacOpts .Argument ()), true }
249
+ mac := hmac .New (csp .conf .hashFunction , aesK .k )
250
+ mac .Write (hmacOpts .Argument ())
251
+ hmacedKey := & aesPrivateKey {mac .Sum (nil ), true }
237
252
238
253
// If the key is not Ephemeral, store it.
239
254
if ! opts .Ephemeral () {
@@ -484,12 +499,16 @@ func (csp *impl) GetKey(ski []byte) (k bccsp.Key, err error) {
484
499
// Hash hashes messages msg using options opts.
485
500
func (csp * impl ) Hash (msg []byte , opts bccsp.HashOpts ) (hash []byte , err error ) {
486
501
if opts == nil {
487
- return primitives .Hash (msg ), nil
502
+ hash := csp .conf .hashFunction ()
503
+ hash .Write (msg )
504
+ return hash .Sum (nil ), nil
488
505
}
489
506
490
507
switch opts .Algorithm () {
491
508
case bccsp .DefaultHash , bccsp .SHA :
492
- return primitives .Hash (msg ), nil
509
+ hash := csp .conf .hashFunction ()
510
+ hash .Write (msg )
511
+ return hash .Sum (nil ), nil
493
512
default :
494
513
return nil , fmt .Errorf ("Algorithm not recognized [%s]" , opts .Algorithm ())
495
514
}
@@ -499,12 +518,12 @@ func (csp *impl) Hash(msg []byte, opts bccsp.HashOpts) (hash []byte, err error)
499
518
// If opts is nil then the default hash function is returned.
500
519
func (csp * impl ) GetHash (opts bccsp.HashOpts ) (h hash.Hash , err error ) {
501
520
if opts == nil {
502
- return primitives . NewHash (), nil
521
+ return csp . conf . hashFunction (), nil
503
522
}
504
523
505
524
switch opts .Algorithm () {
506
525
case bccsp .SHA , bccsp .DefaultHash :
507
- return primitives . NewHash (), nil
526
+ return csp . conf . hashFunction (), nil
508
527
default :
509
528
return nil , fmt .Errorf ("Algorithm not recognized [%s]" , opts .Algorithm ())
510
529
}
0 commit comments