-
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.
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 <adc@zurich.ibm.com>
- Loading branch information
Showing
13 changed files
with
722 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/* | ||
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. | ||
*/ | ||
/* | ||
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 sw | ||
|
||
import ( | ||
"bytes" | ||
"crypto/aes" | ||
"crypto/cipher" | ||
"crypto/rand" | ||
"errors" | ||
"fmt" | ||
"io" | ||
) | ||
|
||
// GetRandomBytes returns len random looking bytes | ||
func GetRandomBytes(len int) ([]byte, error) { | ||
buffer := make([]byte, len) | ||
|
||
n, err := rand.Read(buffer) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if n != len { | ||
return nil, fmt.Errorf("Buffer not filled. Requested [%d], got [%d]", len, n) | ||
} | ||
|
||
return buffer, nil | ||
} | ||
|
||
func pkcs7Padding(src []byte) []byte { | ||
padding := aes.BlockSize - len(src)%aes.BlockSize | ||
padtext := bytes.Repeat([]byte{byte(padding)}, padding) | ||
return append(src, padtext...) | ||
} | ||
|
||
func pkcs7UnPadding(src []byte) ([]byte, error) { | ||
length := len(src) | ||
unpadding := int(src[length-1]) | ||
|
||
if unpadding > aes.BlockSize || unpadding == 0 { | ||
return nil, errors.New("Invalid pkcs7 padding (unpadding > aes.BlockSize || unpadding == 0)") | ||
} | ||
|
||
pad := src[len(src)-unpadding:] | ||
for i := 0; i < unpadding; i++ { | ||
if pad[i] != byte(unpadding) { | ||
return nil, errors.New("Invalid pkcs7 padding (pad[i] != unpadding)") | ||
} | ||
} | ||
|
||
return src[:(length - unpadding)], nil | ||
} | ||
|
||
func aesCBCEncrypt(key, s []byte) ([]byte, error) { | ||
if len(s)%aes.BlockSize != 0 { | ||
return nil, errors.New("Invalid plaintext. It must be a multiple of the block size") | ||
} | ||
|
||
block, err := aes.NewCipher(key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ciphertext := make([]byte, aes.BlockSize+len(s)) | ||
iv := ciphertext[:aes.BlockSize] | ||
if _, err := io.ReadFull(rand.Reader, iv); err != nil { | ||
return nil, err | ||
} | ||
|
||
mode := cipher.NewCBCEncrypter(block, iv) | ||
mode.CryptBlocks(ciphertext[aes.BlockSize:], s) | ||
|
||
return ciphertext, nil | ||
} | ||
|
||
func aesCBCDecrypt(key, src []byte) ([]byte, error) { | ||
block, err := aes.NewCipher(key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(src) < aes.BlockSize { | ||
return nil, errors.New("Invalid ciphertext. It must be a multiple of the block size") | ||
} | ||
iv := src[:aes.BlockSize] | ||
src = src[aes.BlockSize:] | ||
|
||
if len(src)%aes.BlockSize != 0 { | ||
return nil, errors.New("Invalid ciphertext. It must be a multiple of the block size") | ||
} | ||
|
||
mode := cipher.NewCBCDecrypter(block, iv) | ||
|
||
mode.CryptBlocks(src, src) | ||
|
||
return src, nil | ||
} | ||
|
||
// AESCBCPKCS7Encrypt combines CBC encryption and PKCS7 padding | ||
func AESCBCPKCS7Encrypt(key, src []byte) ([]byte, error) { | ||
// First pad | ||
tmp := pkcs7Padding(src) | ||
|
||
// Then encrypt | ||
return aesCBCEncrypt(key, tmp) | ||
} | ||
|
||
// AESCBCPKCS7Decrypt combines CBC decryption and PKCS7 unpadding | ||
func AESCBCPKCS7Decrypt(key, src []byte) ([]byte, error) { | ||
// First decrypt | ||
pt, err := aesCBCDecrypt(key, src) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Then remove padding | ||
original, err := pkcs7UnPadding(pt) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return original, 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,23 @@ | ||
/* | ||
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 sw | ||
|
||
import "math/big" | ||
|
||
// ECDSASignature represents an ECDSA signature | ||
type ecdsaSignature struct { | ||
R, S *big.Int | ||
} |
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
Oops, something went wrong.