1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
package pki
import (
"bytes"
"crypto"
"crypto/elliptic"
"encoding/pem"
"testing"
)
var (
SignatureMessage = []byte("foobar")
SignatureHash = crypto.SHA512
)
type (
Loader func(raw []byte) (PublicKey, error)
)
// run the marshal test
func RunMarshalTest(pkType string, pe Pemmer, label string, t *testing.T) ([]byte, error) {
marshPem, err := pe.MarshalPem()
if err != nil {
t.Errorf("%s: marshal pem not working: %s", pkType, err)
return nil, err
}
buf := &bytes.Buffer{}
marshPem.WriteTo(buf)
block, _ := pem.Decode(buf.Bytes())
if block.Type != label {
t.Errorf("%s: marshalled pem wrong: %s", pkType, err)
return nil, err
}
return block.Bytes, nil
}
// test other private key functions
func RunPrivateKeyTests(pkType string, pk PrivateKey, pu PublicKey, t *testing.T) {
signature, err := pk.Sign(SignatureMessage, SignatureHash)
if err != nil {
t.Errorf("%s: error creating a signature: %s", pkType, err)
}
valid, err := pu.Verify(SignatureMessage, signature, SignatureHash)
if err != nil {
t.Errorf("%s: could not verify message: %s", pkType, err)
}
if !valid {
t.Errorf("%s: signature invalid, but should be valid!", pkType)
}
}
// test ecdsa private key functions
func TestEcdsaFunctions(t *testing.T) {
pk, err := NewPrivateKeyEcdsa(elliptic.P521())
if err != nil {
t.Errorf("ecdsa: creating private key failed: %s", err)
return
}
blockBytes, err := RunMarshalTest("ecdsa", pk, PemLabelEcdsa, t)
if err != nil {
return
}
pk, err = LoadPrivateKeyEcdsa(blockBytes)
if err != nil {
t.Errorf("ecdsa: pem content wrong: %s", err)
return
}
blockBytes, err = RunMarshalTest("ecdsa-public", pk.Public(), PemLabelPublic, t)
if err != nil {
return
}
pu, err := LoadPublicKeyEcdsa(blockBytes)
if err != nil {
t.Errorf("ecdsa-public: pem content wrong: %s", err)
return
}
RunPrivateKeyTests("ecdsa", pk, pu, t)
}
// test rsa private key functions
func TestRsaFunctions(t *testing.T) {
pk, err := NewPrivateKeyRsa(2048)
if err != nil {
t.Errorf("rsa: creating private key failed: %s", err)
return
}
blockBytes, err := RunMarshalTest("rsa", pk, PemLabelRsa, t)
if err != nil {
return
}
pk, err = LoadPrivateKeyRsa(blockBytes)
if err != nil {
t.Errorf("rsa: pem content wrong: %s", err)
return
}
blockBytes, err = RunMarshalTest("rsa-public", pk.Public(), PemLabelPublic, t)
if err != nil {
return
}
pu, err := LoadPublicKeyRsa(blockBytes)
if err != nil {
t.Errorf("rsa-public: pem content wrong: %s", err)
return
}
RunPrivateKeyTests("rsa", pk, pu, t)
}
// test rsa private key functions
func TestEd25519Functions(t *testing.T) {
pk, err := NewPrivateKeyEd25519()
if err != nil {
t.Errorf("ed25519: creating private key failed: %s", err)
return
}
blockBytes, err := RunMarshalTest("ed25519", pk, PemLabelEd25519, t)
if err != nil {
return
}
pk, err = LoadPrivateKeyEd25519(blockBytes)
if err != nil {
t.Errorf("ed25519: pem content wrong: %s", err)
return
}
blockBytes, err = RunMarshalTest("ed25519-public", pk.Public(), PemLabelPublic, t)
if err != nil {
return
}
pu, err := LoadPublicKeyEd25519(blockBytes)
if err != nil {
t.Errorf("ed25519-public: pem content wrong: %s", err)
return
}
RunPrivateKeyTests("ed25519", pk, pu, t)
}
|