Skip to content

Commit 36e8dd8

Browse files
committed
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 <[email protected]>
1 parent 5c9fcc3 commit 36e8dd8

21 files changed

+665
-2465
lines changed

core/crypto/bccsp/sw/aes_test.go

+478
Large diffs are not rendered by default.

core/crypto/bccsp/utils/keys_test.go

+186
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
package utils
2+
3+
import (
4+
"crypto/ecdsa"
5+
"crypto/elliptic"
6+
"crypto/rand"
7+
"testing"
8+
)
9+
10+
func TestECDSAKeys(t *testing.T) {
11+
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
12+
if err != nil {
13+
t.Fatalf("Failed generating ECDSA key [%s]", err)
14+
}
15+
16+
// Private Key DER format
17+
der, err := PrivateKeyToDER(key)
18+
if err != nil {
19+
t.Fatalf("Failed converting private key to DER [%s]", err)
20+
}
21+
keyFromDER, err := DERToPrivateKey(der)
22+
if err != nil {
23+
t.Fatalf("Failed converting DER to private key [%s]", err)
24+
}
25+
ecdsaKeyFromDer := keyFromDER.(*ecdsa.PrivateKey)
26+
// TODO: check the curve
27+
if key.D.Cmp(ecdsaKeyFromDer.D) != 0 {
28+
t.Fatal("Failed converting DER to private key. Invalid D.")
29+
}
30+
if key.X.Cmp(ecdsaKeyFromDer.X) != 0 {
31+
t.Fatal("Failed converting DER to private key. Invalid X coordinate.")
32+
}
33+
if key.Y.Cmp(ecdsaKeyFromDer.Y) != 0 {
34+
t.Fatal("Failed converting DER to private key. Invalid Y coordinate.")
35+
}
36+
37+
// Private Key PEM format
38+
pem, err := PrivateKeyToPEM(key, nil)
39+
if err != nil {
40+
t.Fatalf("Failed converting private key to PEM [%s]", err)
41+
}
42+
keyFromPEM, err := PEMtoPrivateKey(pem, nil)
43+
if err != nil {
44+
t.Fatalf("Failed converting DER to private key [%s]", err)
45+
}
46+
ecdsaKeyFromPEM := keyFromPEM.(*ecdsa.PrivateKey)
47+
// TODO: check the curve
48+
if key.D.Cmp(ecdsaKeyFromPEM.D) != 0 {
49+
t.Fatal("Failed converting PEM to private key. Invalid D.")
50+
}
51+
if key.X.Cmp(ecdsaKeyFromPEM.X) != 0 {
52+
t.Fatal("Failed converting PEM to private key. Invalid X coordinate.")
53+
}
54+
if key.Y.Cmp(ecdsaKeyFromPEM.Y) != 0 {
55+
t.Fatal("Failed converting PEM to private key. Invalid Y coordinate.")
56+
}
57+
58+
// Nil Private Key <-> PEM
59+
_, err = PrivateKeyToPEM(nil, nil)
60+
if err == nil {
61+
t.Fatal("PublicKeyToPEM should fail on nil")
62+
}
63+
64+
_, err = PEMtoPrivateKey(nil, nil)
65+
if err == nil {
66+
t.Fatal("PEMtoPublicKey should fail on nil")
67+
}
68+
69+
_, err = PEMtoPrivateKey([]byte{0, 1, 3, 4}, nil)
70+
if err == nil {
71+
t.Fatal("PEMtoPublicKey should fail invalid PEM")
72+
}
73+
74+
_, err = DERToPrivateKey(nil)
75+
if err == nil {
76+
t.Fatal("DERToPrivateKey should fail on nil")
77+
}
78+
79+
_, err = DERToPrivateKey([]byte{0, 1, 3, 4})
80+
if err == nil {
81+
t.Fatal("DERToPrivateKey should fail on invalid DER")
82+
}
83+
84+
_, err = PrivateKeyToDER(nil)
85+
if err == nil {
86+
t.Fatal("DERToPrivateKey should fail on nil")
87+
}
88+
89+
// Private Key Encrypted PEM format
90+
encPEM, err := PrivateKeyToPEM(key, []byte("passwd"))
91+
if err != nil {
92+
t.Fatalf("Failed converting private key to encrypted PEM [%s]", err)
93+
}
94+
encKeyFromPEM, err := PEMtoPrivateKey(encPEM, []byte("passwd"))
95+
if err != nil {
96+
t.Fatalf("Failed converting DER to private key [%s]", err)
97+
}
98+
ecdsaKeyFromEncPEM := encKeyFromPEM.(*ecdsa.PrivateKey)
99+
// TODO: check the curve
100+
if key.D.Cmp(ecdsaKeyFromEncPEM.D) != 0 {
101+
t.Fatal("Failed converting encrypted PEM to private key. Invalid D.")
102+
}
103+
if key.X.Cmp(ecdsaKeyFromEncPEM.X) != 0 {
104+
t.Fatal("Failed converting encrypted PEM to private key. Invalid X coordinate.")
105+
}
106+
if key.Y.Cmp(ecdsaKeyFromEncPEM.Y) != 0 {
107+
t.Fatal("Failed converting encrypted PEM to private key. Invalid Y coordinate.")
108+
}
109+
110+
// Public Key PEM format
111+
pem, err = PublicKeyToPEM(&key.PublicKey, nil)
112+
if err != nil {
113+
t.Fatalf("Failed converting public key to PEM [%s]", err)
114+
}
115+
keyFromPEM, err = PEMtoPublicKey(pem, nil)
116+
if err != nil {
117+
t.Fatalf("Failed converting DER to public key [%s]", err)
118+
}
119+
ecdsaPkFromPEM := keyFromPEM.(*ecdsa.PublicKey)
120+
// TODO: check the curve
121+
if key.X.Cmp(ecdsaPkFromPEM.X) != 0 {
122+
t.Fatal("Failed converting PEM to private key. Invalid X coordinate.")
123+
}
124+
if key.Y.Cmp(ecdsaPkFromPEM.Y) != 0 {
125+
t.Fatal("Failed converting PEM to private key. Invalid Y coordinate.")
126+
}
127+
128+
// Nil Public Key <-> PEM
129+
_, err = PublicKeyToPEM(nil, nil)
130+
if err == nil {
131+
t.Fatal("PublicKeyToPEM should fail on nil")
132+
}
133+
134+
_, err = PEMtoPublicKey(nil, nil)
135+
if err == nil {
136+
t.Fatal("PEMtoPublicKey should fail on nil")
137+
}
138+
139+
_, err = PEMtoPublicKey([]byte{0, 1, 3, 4}, nil)
140+
if err == nil {
141+
t.Fatal("PEMtoPublicKey should fail on invalid PEM")
142+
}
143+
144+
// Public Key Encrypted PEM format
145+
encPEM, err = PublicKeyToPEM(&key.PublicKey, []byte("passwd"))
146+
if err != nil {
147+
t.Fatalf("Failed converting private key to encrypted PEM [%s]", err)
148+
}
149+
pkFromEncPEM, err := PEMtoPublicKey(encPEM, []byte("passwd"))
150+
if err != nil {
151+
t.Fatalf("Failed converting DER to private key [%s]", err)
152+
}
153+
ecdsaPkFromEncPEM := pkFromEncPEM.(*ecdsa.PublicKey)
154+
// TODO: check the curve
155+
if key.X.Cmp(ecdsaPkFromEncPEM.X) != 0 {
156+
t.Fatal("Failed converting encrypted PEM to private key. Invalid X coordinate.")
157+
}
158+
if key.Y.Cmp(ecdsaPkFromEncPEM.Y) != 0 {
159+
t.Fatal("Failed converting encrypted PEM to private key. Invalid Y coordinate.")
160+
}
161+
162+
_, err = PEMtoPublicKey(encPEM, []byte("passw"))
163+
if err == nil {
164+
t.Fatal("PEMtoPublicKey should fail on wrong password")
165+
}
166+
167+
_, err = PEMtoPublicKey(encPEM, []byte("passw"))
168+
if err == nil {
169+
t.Fatal("PEMtoPublicKey should fail on nil password")
170+
}
171+
172+
_, err = PEMtoPublicKey(nil, []byte("passwd"))
173+
if err == nil {
174+
t.Fatal("PEMtoPublicKey should fail on nil PEM")
175+
}
176+
177+
_, err = PEMtoPublicKey([]byte{0, 1, 3, 4}, []byte("passwd"))
178+
if err == nil {
179+
t.Fatal("PEMtoPublicKey should fail on invalid PEM")
180+
}
181+
182+
_, err = PEMtoPublicKey(nil, []byte("passw"))
183+
if err == nil {
184+
t.Fatal("PEMtoPublicKey should fail on nil PEM and wrong password")
185+
}
186+
}

core/crypto/primitives/crypto.go

-188
This file was deleted.

0 commit comments

Comments
 (0)