Skip to content

Commit

Permalink
Removing unused crypto/primitives methods
Browse files Browse the repository at this point in the history
This change-sets removed all the unused methods
from the primitives package.
In addition, it transfers tests to the bccsp package.

Change-Id: Ic1676961be3d9f128133b6d0179cfa49b5c8fca7
Signed-off-by: Angelo De Caro <adc@zurich.ibm.com>
  • Loading branch information
adecaro committed Dec 8, 2016
1 parent 5c9fcc3 commit 36e8dd8
Show file tree
Hide file tree
Showing 21 changed files with 665 additions and 2,465 deletions.
478 changes: 478 additions & 0 deletions core/crypto/bccsp/sw/aes_test.go

Large diffs are not rendered by default.

186 changes: 186 additions & 0 deletions core/crypto/bccsp/utils/keys_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
package utils

import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"testing"
)

func TestECDSAKeys(t *testing.T) {
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
t.Fatalf("Failed generating ECDSA key [%s]", err)
}

// Private Key DER format
der, err := PrivateKeyToDER(key)
if err != nil {
t.Fatalf("Failed converting private key to DER [%s]", err)
}
keyFromDER, err := DERToPrivateKey(der)
if err != nil {
t.Fatalf("Failed converting DER to private key [%s]", err)
}
ecdsaKeyFromDer := keyFromDER.(*ecdsa.PrivateKey)
// TODO: check the curve
if key.D.Cmp(ecdsaKeyFromDer.D) != 0 {
t.Fatal("Failed converting DER to private key. Invalid D.")
}
if key.X.Cmp(ecdsaKeyFromDer.X) != 0 {
t.Fatal("Failed converting DER to private key. Invalid X coordinate.")
}
if key.Y.Cmp(ecdsaKeyFromDer.Y) != 0 {
t.Fatal("Failed converting DER to private key. Invalid Y coordinate.")
}

// Private Key PEM format
pem, err := PrivateKeyToPEM(key, nil)
if err != nil {
t.Fatalf("Failed converting private key to PEM [%s]", err)
}
keyFromPEM, err := PEMtoPrivateKey(pem, nil)
if err != nil {
t.Fatalf("Failed converting DER to private key [%s]", err)
}
ecdsaKeyFromPEM := keyFromPEM.(*ecdsa.PrivateKey)
// TODO: check the curve
if key.D.Cmp(ecdsaKeyFromPEM.D) != 0 {
t.Fatal("Failed converting PEM to private key. Invalid D.")
}
if key.X.Cmp(ecdsaKeyFromPEM.X) != 0 {
t.Fatal("Failed converting PEM to private key. Invalid X coordinate.")
}
if key.Y.Cmp(ecdsaKeyFromPEM.Y) != 0 {
t.Fatal("Failed converting PEM to private key. Invalid Y coordinate.")
}

// Nil Private Key <-> PEM
_, err = PrivateKeyToPEM(nil, nil)
if err == nil {
t.Fatal("PublicKeyToPEM should fail on nil")
}

_, err = PEMtoPrivateKey(nil, nil)
if err == nil {
t.Fatal("PEMtoPublicKey should fail on nil")
}

_, err = PEMtoPrivateKey([]byte{0, 1, 3, 4}, nil)
if err == nil {
t.Fatal("PEMtoPublicKey should fail invalid PEM")
}

_, err = DERToPrivateKey(nil)
if err == nil {
t.Fatal("DERToPrivateKey should fail on nil")
}

_, err = DERToPrivateKey([]byte{0, 1, 3, 4})
if err == nil {
t.Fatal("DERToPrivateKey should fail on invalid DER")
}

_, err = PrivateKeyToDER(nil)
if err == nil {
t.Fatal("DERToPrivateKey should fail on nil")
}

// Private Key Encrypted PEM format
encPEM, err := PrivateKeyToPEM(key, []byte("passwd"))
if err != nil {
t.Fatalf("Failed converting private key to encrypted PEM [%s]", err)
}
encKeyFromPEM, err := PEMtoPrivateKey(encPEM, []byte("passwd"))
if err != nil {
t.Fatalf("Failed converting DER to private key [%s]", err)
}
ecdsaKeyFromEncPEM := encKeyFromPEM.(*ecdsa.PrivateKey)
// TODO: check the curve
if key.D.Cmp(ecdsaKeyFromEncPEM.D) != 0 {
t.Fatal("Failed converting encrypted PEM to private key. Invalid D.")
}
if key.X.Cmp(ecdsaKeyFromEncPEM.X) != 0 {
t.Fatal("Failed converting encrypted PEM to private key. Invalid X coordinate.")
}
if key.Y.Cmp(ecdsaKeyFromEncPEM.Y) != 0 {
t.Fatal("Failed converting encrypted PEM to private key. Invalid Y coordinate.")
}

// Public Key PEM format
pem, err = PublicKeyToPEM(&key.PublicKey, nil)
if err != nil {
t.Fatalf("Failed converting public key to PEM [%s]", err)
}
keyFromPEM, err = PEMtoPublicKey(pem, nil)
if err != nil {
t.Fatalf("Failed converting DER to public key [%s]", err)
}
ecdsaPkFromPEM := keyFromPEM.(*ecdsa.PublicKey)
// TODO: check the curve
if key.X.Cmp(ecdsaPkFromPEM.X) != 0 {
t.Fatal("Failed converting PEM to private key. Invalid X coordinate.")
}
if key.Y.Cmp(ecdsaPkFromPEM.Y) != 0 {
t.Fatal("Failed converting PEM to private key. Invalid Y coordinate.")
}

// Nil Public Key <-> PEM
_, err = PublicKeyToPEM(nil, nil)
if err == nil {
t.Fatal("PublicKeyToPEM should fail on nil")
}

_, err = PEMtoPublicKey(nil, nil)
if err == nil {
t.Fatal("PEMtoPublicKey should fail on nil")
}

_, err = PEMtoPublicKey([]byte{0, 1, 3, 4}, nil)
if err == nil {
t.Fatal("PEMtoPublicKey should fail on invalid PEM")
}

// Public Key Encrypted PEM format
encPEM, err = PublicKeyToPEM(&key.PublicKey, []byte("passwd"))
if err != nil {
t.Fatalf("Failed converting private key to encrypted PEM [%s]", err)
}
pkFromEncPEM, err := PEMtoPublicKey(encPEM, []byte("passwd"))
if err != nil {
t.Fatalf("Failed converting DER to private key [%s]", err)
}
ecdsaPkFromEncPEM := pkFromEncPEM.(*ecdsa.PublicKey)
// TODO: check the curve
if key.X.Cmp(ecdsaPkFromEncPEM.X) != 0 {
t.Fatal("Failed converting encrypted PEM to private key. Invalid X coordinate.")
}
if key.Y.Cmp(ecdsaPkFromEncPEM.Y) != 0 {
t.Fatal("Failed converting encrypted PEM to private key. Invalid Y coordinate.")
}

_, err = PEMtoPublicKey(encPEM, []byte("passw"))
if err == nil {
t.Fatal("PEMtoPublicKey should fail on wrong password")
}

_, err = PEMtoPublicKey(encPEM, []byte("passw"))
if err == nil {
t.Fatal("PEMtoPublicKey should fail on nil password")
}

_, err = PEMtoPublicKey(nil, []byte("passwd"))
if err == nil {
t.Fatal("PEMtoPublicKey should fail on nil PEM")
}

_, err = PEMtoPublicKey([]byte{0, 1, 3, 4}, []byte("passwd"))
if err == nil {
t.Fatal("PEMtoPublicKey should fail on invalid PEM")
}

_, err = PEMtoPublicKey(nil, []byte("passw"))
if err == nil {
t.Fatal("PEMtoPublicKey should fail on nil PEM and wrong password")
}
}
188 changes: 0 additions & 188 deletions core/crypto/primitives/crypto.go

This file was deleted.

0 comments on commit 36e8dd8

Please sign in to comment.