Skip to content

Commit 51dc761

Browse files
committed
BCCSP: Removing dependency to crypto/primitives package
This change-set removes all the dependencies to the crypto/primitives package. The bccsp is now self-contained. It does not depend on any other fabric package. This change-set comes in the context of: https://jira.hyperledger.org/browse/FAB-354 Notice that no new methods have been introduced. Only required methods have been copied from the crypto/primitives package. Change-Id: I6be03eb4f345592ab1a89e544ec2be278cc7103c Signed-off-by: Angelo De Caro <[email protected]>
1 parent da16559 commit 51dc761

File tree

13 files changed

+722
-42
lines changed

13 files changed

+722
-42
lines changed

core/crypto/bccsp/signer/signer.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"io"
2323

2424
"github.com/hyperledger/fabric/core/crypto/bccsp"
25-
"github.com/hyperledger/fabric/core/crypto/primitives"
25+
"github.com/hyperledger/fabric/core/crypto/bccsp/utils"
2626
)
2727

2828
// CryptoSigner is the BCCSP-based implementation of a crypto.Signer
@@ -56,7 +56,7 @@ func (s *CryptoSigner) Init(csp bccsp.BCCSP, key bccsp.Key) error {
5656
return fmt.Errorf("Failed marshalling public key [%s]", err)
5757
}
5858

59-
pk, err := primitives.DERToPublicKey(raw)
59+
pk, err := utils.DERToPublicKey(raw)
6060
if err != nil {
6161
return fmt.Errorf("Failed marshalling public key [%s]", err)
6262
}

core/crypto/bccsp/sw/aes.go

+152
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/*
2+
Copyright IBM Corp. 2016 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
/*
17+
Copyright IBM Corp. 2016 All Rights Reserved.
18+
19+
Licensed under the Apache License, Version 2.0 (the "License");
20+
you may not use this file except in compliance with the License.
21+
You may obtain a copy of the License at
22+
23+
http://www.apache.org/licenses/LICENSE-2.0
24+
25+
Unless required by applicable law or agreed to in writing, software
26+
distributed under the License is distributed on an "AS IS" BASIS,
27+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28+
See the License for the specific language governing permissions and
29+
limitations under the License.
30+
*/
31+
32+
package sw
33+
34+
import (
35+
"bytes"
36+
"crypto/aes"
37+
"crypto/cipher"
38+
"crypto/rand"
39+
"errors"
40+
"fmt"
41+
"io"
42+
)
43+
44+
// GetRandomBytes returns len random looking bytes
45+
func GetRandomBytes(len int) ([]byte, error) {
46+
buffer := make([]byte, len)
47+
48+
n, err := rand.Read(buffer)
49+
if err != nil {
50+
return nil, err
51+
}
52+
if n != len {
53+
return nil, fmt.Errorf("Buffer not filled. Requested [%d], got [%d]", len, n)
54+
}
55+
56+
return buffer, nil
57+
}
58+
59+
func pkcs7Padding(src []byte) []byte {
60+
padding := aes.BlockSize - len(src)%aes.BlockSize
61+
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
62+
return append(src, padtext...)
63+
}
64+
65+
func pkcs7UnPadding(src []byte) ([]byte, error) {
66+
length := len(src)
67+
unpadding := int(src[length-1])
68+
69+
if unpadding > aes.BlockSize || unpadding == 0 {
70+
return nil, errors.New("Invalid pkcs7 padding (unpadding > aes.BlockSize || unpadding == 0)")
71+
}
72+
73+
pad := src[len(src)-unpadding:]
74+
for i := 0; i < unpadding; i++ {
75+
if pad[i] != byte(unpadding) {
76+
return nil, errors.New("Invalid pkcs7 padding (pad[i] != unpadding)")
77+
}
78+
}
79+
80+
return src[:(length - unpadding)], nil
81+
}
82+
83+
func aesCBCEncrypt(key, s []byte) ([]byte, error) {
84+
if len(s)%aes.BlockSize != 0 {
85+
return nil, errors.New("Invalid plaintext. It must be a multiple of the block size")
86+
}
87+
88+
block, err := aes.NewCipher(key)
89+
if err != nil {
90+
return nil, err
91+
}
92+
93+
ciphertext := make([]byte, aes.BlockSize+len(s))
94+
iv := ciphertext[:aes.BlockSize]
95+
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
96+
return nil, err
97+
}
98+
99+
mode := cipher.NewCBCEncrypter(block, iv)
100+
mode.CryptBlocks(ciphertext[aes.BlockSize:], s)
101+
102+
return ciphertext, nil
103+
}
104+
105+
func aesCBCDecrypt(key, src []byte) ([]byte, error) {
106+
block, err := aes.NewCipher(key)
107+
if err != nil {
108+
return nil, err
109+
}
110+
111+
if len(src) < aes.BlockSize {
112+
return nil, errors.New("Invalid ciphertext. It must be a multiple of the block size")
113+
}
114+
iv := src[:aes.BlockSize]
115+
src = src[aes.BlockSize:]
116+
117+
if len(src)%aes.BlockSize != 0 {
118+
return nil, errors.New("Invalid ciphertext. It must be a multiple of the block size")
119+
}
120+
121+
mode := cipher.NewCBCDecrypter(block, iv)
122+
123+
mode.CryptBlocks(src, src)
124+
125+
return src, nil
126+
}
127+
128+
// AESCBCPKCS7Encrypt combines CBC encryption and PKCS7 padding
129+
func AESCBCPKCS7Encrypt(key, src []byte) ([]byte, error) {
130+
// First pad
131+
tmp := pkcs7Padding(src)
132+
133+
// Then encrypt
134+
return aesCBCEncrypt(key, tmp)
135+
}
136+
137+
// AESCBCPKCS7Decrypt combines CBC decryption and PKCS7 unpadding
138+
func AESCBCPKCS7Decrypt(key, src []byte) ([]byte, error) {
139+
// First decrypt
140+
pt, err := aesCBCDecrypt(key, src)
141+
if err != nil {
142+
return nil, err
143+
}
144+
145+
// Then remove padding
146+
original, err := pkcs7UnPadding(pt)
147+
if err != nil {
148+
return nil, err
149+
}
150+
151+
return original, nil
152+
}

core/crypto/bccsp/sw/ecdsa.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Copyright IBM Corp. 2016 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package sw
17+
18+
import "math/big"
19+
20+
// ECDSASignature represents an ECDSA signature
21+
type ecdsaSignature struct {
22+
R, S *big.Int
23+
}

core/crypto/bccsp/sw/ecdsakey.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"errors"
2626

2727
"github.com/hyperledger/fabric/core/crypto/bccsp"
28-
"github.com/hyperledger/fabric/core/crypto/primitives"
28+
"github.com/hyperledger/fabric/core/crypto/bccsp/utils"
2929
)
3030

3131
type ecdsaPrivateKey struct {
@@ -40,7 +40,7 @@ func (k *ecdsaPrivateKey) Bytes() (raw []byte, err error) {
4040

4141
// SKI returns the subject key identifier of this key.
4242
func (k *ecdsaPrivateKey) SKI() (ski []byte) {
43-
raw, _ := primitives.PrivateKeyToDER(k.privKey)
43+
raw, _ := utils.PrivateKeyToDER(k.privKey)
4444
// TODO: Error should not be thrown. Anyway, move the marshalling at initialization.
4545

4646
hash := sha256.New()
@@ -82,7 +82,7 @@ func (k *ecdsaPublicKey) Bytes() (raw []byte, err error) {
8282

8383
// SKI returns the subject key identifier of this key.
8484
func (k *ecdsaPublicKey) SKI() (ski []byte) {
85-
raw, _ := primitives.PublicKeyToPEM(k.pubKey, nil)
85+
raw, _ := utils.PublicKeyToPEM(k.pubKey, nil)
8686
// TODO: Error should not be thrown. Anyway, move the marshalling at initialization.
8787

8888
hash := sha256.New()

core/crypto/bccsp/sw/fileks.go

+7-9
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import (
2020
"os"
2121
"sync"
2222

23-
"github.com/hyperledger/fabric/core/crypto/utils"
24-
2523
"errors"
2624
"strings"
2725

@@ -32,7 +30,7 @@ import (
3230
"path/filepath"
3331

3432
"github.com/hyperledger/fabric/core/crypto/bccsp"
35-
"github.com/hyperledger/fabric/core/crypto/primitives"
33+
"github.com/hyperledger/fabric/core/crypto/bccsp/utils"
3634
)
3735

3836
// FileBasedKeyStore is a folder-based KeyStore.
@@ -234,7 +232,7 @@ func (ks *FileBasedKeyStore) getSuffix(alias string) string {
234232
}
235233

236234
func (ks *FileBasedKeyStore) storePrivateKey(alias string, privateKey interface{}) error {
237-
rawKey, err := primitives.PrivateKeyToPEM(privateKey, ks.pwd)
235+
rawKey, err := utils.PrivateKeyToPEM(privateKey, ks.pwd)
238236
if err != nil {
239237
logger.Errorf("Failed converting private key to PEM [%s]: [%s]", alias, err)
240238
return err
@@ -250,7 +248,7 @@ func (ks *FileBasedKeyStore) storePrivateKey(alias string, privateKey interface{
250248
}
251249

252250
func (ks *FileBasedKeyStore) storePublicKey(alias string, publicKey interface{}) error {
253-
rawKey, err := primitives.PublicKeyToPEM(publicKey, ks.pwd)
251+
rawKey, err := utils.PublicKeyToPEM(publicKey, ks.pwd)
254252
if err != nil {
255253
logger.Errorf("Failed converting public key to PEM [%s]: [%s]", alias, err)
256254
return err
@@ -266,7 +264,7 @@ func (ks *FileBasedKeyStore) storePublicKey(alias string, publicKey interface{})
266264
}
267265

268266
func (ks *FileBasedKeyStore) storeKey(alias string, key []byte) error {
269-
pem, err := primitives.AEStoEncryptedPEM(key, ks.pwd)
267+
pem, err := utils.AEStoEncryptedPEM(key, ks.pwd)
270268
if err != nil {
271269
logger.Errorf("Failed converting key to PEM [%s]: [%s]", alias, err)
272270
return err
@@ -292,7 +290,7 @@ func (ks *FileBasedKeyStore) loadPrivateKey(alias string) (interface{}, error) {
292290
return nil, err
293291
}
294292

295-
privateKey, err := primitives.PEMtoPrivateKey(raw, ks.pwd)
293+
privateKey, err := utils.PEMtoPrivateKey(raw, ks.pwd)
296294
if err != nil {
297295
logger.Errorf("Failed parsing private key [%s]: [%s].", alias, err.Error())
298296

@@ -313,7 +311,7 @@ func (ks *FileBasedKeyStore) loadPublicKey(alias string) (interface{}, error) {
313311
return nil, err
314312
}
315313

316-
privateKey, err := primitives.PEMtoPublicKey(raw, ks.pwd)
314+
privateKey, err := utils.PEMtoPublicKey(raw, ks.pwd)
317315
if err != nil {
318316
logger.Errorf("Failed parsing private key [%s]: [%s].", alias, err.Error())
319317

@@ -334,7 +332,7 @@ func (ks *FileBasedKeyStore) loadKey(alias string) ([]byte, error) {
334332
return nil, err
335333
}
336334

337-
key, err := primitives.PEMtoAES(pem, ks.pwd)
335+
key, err := utils.PEMtoAES(pem, ks.pwd)
338336
if err != nil {
339337
logger.Errorf("Failed parsing key [%s]: [%s]", alias, err)
340338

0 commit comments

Comments
 (0)