Skip to content

Commit

Permalink
core/crypto/primitives cleanup: second step
Browse files Browse the repository at this point in the history
This change-set continues the cleanup of the core/crypto/primitives
package. Refactoring has been applied to move methods and files under
the packages which need them.

Change-Id: Icfe6adf938b9d96df9dfde3dfebf95f3004fcae7
Signed-off-by: Angelo De Caro <adc@zurich.ibm.com>
  • Loading branch information
adecaro committed Dec 14, 2016
1 parent 4c63856 commit 9ed9ce4
Show file tree
Hide file tree
Showing 19 changed files with 117 additions and 97 deletions.
15 changes: 15 additions & 0 deletions accesscontrol/api.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
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 accesscontrol

// Attribute defines a name, value pair to be verified.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ import (
"strconv"
"strings"

pb "github.com/hyperledger/fabric/core/crypto/attributes/proto"
pb "github.com/hyperledger/fabric/accesscontrol/attributes/proto"
"github.com/hyperledger/fabric/core/crypto/primitives"

"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/accesscontrol/crypto/utils"
)

var (
Expand Down Expand Up @@ -80,7 +81,7 @@ func ReadAttributeHeader(tcert *x509.Certificate, headerKey []byte) (map[string]
var err error
var headerRaw []byte
encrypted := false
if headerRaw, err = primitives.GetCriticalExtension(tcert, TCertAttributesHeaders); err != nil {
if headerRaw, err = utils.GetCriticalExtension(tcert, TCertAttributesHeaders); err != nil {
return nil, encrypted, err
}
headerStr := string(headerRaw)
Expand Down Expand Up @@ -112,7 +113,7 @@ func ReadTCertAttributeByPosition(tcert *x509.Certificate, position int) ([]byte
}

oid := asn1.ObjectIdentifier{1, 2, 3, 4, 5, 6, 9 + position}
value, err := primitives.GetCriticalExtension(tcert, oid)
value, err := utils.GetCriticalExtension(tcert, oid)
if err != nil {
return nil, err
}
Expand All @@ -139,7 +140,7 @@ func ReadTCertAttribute(tcert *x509.Certificate, attributeName string, headerKey
//EncryptAttributeValue encrypts "attributeValue" using "attributeKey"
func EncryptAttributeValue(attributeKey []byte, attributeValue []byte) ([]byte, error) {
value := append(attributeValue, padding...)
return primitives.CBCPKCS7Encrypt(attributeKey, value)
return utils.CBCPKCS7Encrypt(attributeKey, value)
}

//getAttributeKey returns the attributeKey derived from the preK0 to the attributeName.
Expand All @@ -155,7 +156,7 @@ func EncryptAttributeValuePK0(preK0 []byte, attributeName string, attributeValue

//DecryptAttributeValue decrypts "encryptedValue" using "attributeKey" and return the decrypted value.
func DecryptAttributeValue(attributeKey []byte, encryptedValue []byte) ([]byte, error) {
value, err := primitives.CBCPKCS7Decrypt(attributeKey, encryptedValue)
value, err := utils.CBCPKCS7Decrypt(attributeKey, encryptedValue)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -238,7 +239,7 @@ func CreateAttributesMetadataFromCert(cert *x509.Certificate, metadata []byte, p

//CreateAttributesMetadata create the AttributesMetadata from the original metadata
func CreateAttributesMetadata(raw []byte, metadata []byte, preK0 []byte, attributeKeys []string) ([]byte, error) {
cert, err := primitives.DERToX509Certificate(raw)
cert, err := utils.DERToX509Certificate(raw)
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"testing"

"github.com/golang/protobuf/proto"
pb "github.com/hyperledger/fabric/core/crypto/attributes/proto"
pb "github.com/hyperledger/fabric/accesscontrol/attributes/proto"
"github.com/hyperledger/fabric/core/crypto/primitives"
)

Expand Down
6 changes: 3 additions & 3 deletions accesscontrol/crypto/attr/attr_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"errors"

"github.com/hyperledger/fabric/accesscontrol"
"github.com/hyperledger/fabric/core/crypto/attributes"
"github.com/hyperledger/fabric/core/crypto/primitives"
"github.com/hyperledger/fabric/accesscontrol/attributes"
"github.com/hyperledger/fabric/accesscontrol/crypto/utils"
)

// chaincodeHolder is the struct that hold the certificate and the metadata. An implementation is ChaincodeStub
Expand Down Expand Up @@ -107,7 +107,7 @@ func NewAttributesHandlerImpl(holder chaincodeHolder) (*AttributesHandlerImpl, e
return nil, errors.New("The certificate can't be nil.")
}
var tcert *x509.Certificate
tcert, err = primitives.DERToX509Certificate(certRaw)
tcert, err = utils.DERToX509Certificate(certRaw)
if err != nil {
return nil, err
}
Expand Down
25 changes: 13 additions & 12 deletions accesscontrol/crypto/ecdsa/ecdsa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,21 @@ package ecdsa
import (
"testing"

"github.com/hyperledger/fabric/accesscontrol/crypto/utils"
"github.com/hyperledger/fabric/core/crypto/primitives"
)

func TestSignatureVerifier(t *testing.T) {
// Create a signature
primitives.SetSecurityLevel("SHA3", 256)

cert, key, err := primitives.NewSelfSignedCert()
cert, key, err := utils.NewSelfSignedCert()
if err != nil {
t.Fatal(err)
}

message := []byte("Hello World!")
signature, err := primitives.ECDSASign(key, message)
signature, err := utils.ECDSASign(key, message)
if err != nil {
t.Fatal(err)
}
Expand All @@ -54,13 +55,13 @@ func TestSignatureVerifierSHA2(t *testing.T) {
// Create a signature
primitives.SetSecurityLevel("SHA2", 256)

cert, key, err := primitives.NewSelfSignedCert()
cert, key, err := utils.NewSelfSignedCert()
if err != nil {
t.Fatal(err)
}

message := []byte("Hello World!")
signature, err := primitives.ECDSASign(key, message)
signature, err := utils.ECDSASign(key, message)
if err != nil {
t.Fatal(err)
}
Expand All @@ -82,13 +83,13 @@ func TestSignatureVerifierSHA2_384(t *testing.T) {
// Create a signature
primitives.SetSecurityLevel("SHA2", 384)

cert, key, err := primitives.NewSelfSignedCert()
cert, key, err := utils.NewSelfSignedCert()
if err != nil {
t.Fatal(err)
}

message := []byte("Hello World!")
signature, err := primitives.ECDSASign(key, message)
signature, err := utils.ECDSASign(key, message)
if err != nil {
t.Fatal(err)
}
Expand All @@ -110,13 +111,13 @@ func TestSignatureVerifierSHA3_384(t *testing.T) {
// Create a signature
primitives.SetSecurityLevel("SHA3", 384)

cert, key, err := primitives.NewSelfSignedCert()
cert, key, err := utils.NewSelfSignedCert()
if err != nil {
t.Fatal(err)
}

message := []byte("Hello World!")
signature, err := primitives.ECDSASign(key, message)
signature, err := utils.ECDSASign(key, message)
if err != nil {
t.Fatal(err)
}
Expand All @@ -138,13 +139,13 @@ func TestSignatureVerifierSHA2_512(t *testing.T) {
// Create a signature
primitives.SetSecurityLevel("SHA2", 512)

cert, key, err := primitives.NewSelfSignedCert()
cert, key, err := utils.NewSelfSignedCert()
if err != nil {
t.Fatal(err)
}

message := []byte("Hello World!")
signature, err := primitives.ECDSASign(key, message)
signature, err := utils.ECDSASign(key, message)
if err != nil {
t.Fatal(err)
}
Expand All @@ -166,13 +167,13 @@ func TestSignatureVerifierSHA3_512(t *testing.T) {
// Create a signature
primitives.SetSecurityLevel("SHA3", 512)

cert, key, err := primitives.NewSelfSignedCert()
cert, key, err := utils.NewSelfSignedCert()
if err != nil {
t.Fatal(err)
}

message := []byte("Hello World!")
signature, err := primitives.ECDSASign(key, message)
signature, err := utils.ECDSASign(key, message)
if err != nil {
t.Fatal(err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package primitives
package utils

import (
"bytes"
Expand All @@ -24,6 +24,8 @@ import (
"errors"
"fmt"
"io"

"github.com/hyperledger/fabric/core/crypto/primitives"
)

const (
Expand All @@ -36,7 +38,7 @@ const (

// GenAESKey returns a random AES key of length AESKeyLength
func GenAESKey() ([]byte, error) {
return GetRandomBytes(AESKeyLength)
return primitives.GetRandomBytes(AESKeyLength)
}

// PKCS7Padding pads as prescribed by the PKCS7 standard
Expand Down
Loading

0 comments on commit 9ed9ce4

Please sign in to comment.