-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removing unused crypto/primitives methods
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
Showing
21 changed files
with
665 additions
and
2,465 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,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") | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.