Repository navigation
Expand file tree
/
Copy pathecdsa_test.go
More file actions
124 lines (111 loc) · 3.02 KB
/
Copy pathecdsa_test.go
File metadata and controls
124 lines (111 loc) · 3.02 KB
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
package signedxml
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"crypto/sha512"
"crypto/x509"
"encoding/asn1"
"math/big"
"testing"
)
func TestIsECDSAAlgorithm(t *testing.T) {
tests := []struct {
alg x509.SignatureAlgorithm
want bool
}{
{x509.ECDSAWithSHA1, true},
{x509.ECDSAWithSHA256, true},
{x509.ECDSAWithSHA384, true},
{x509.ECDSAWithSHA512, true},
{x509.SHA256WithRSA, false},
{x509.SHA256WithRSAPSS, false},
{x509.PureEd25519, false},
}
for _, tt := range tests {
if got := isECDSAAlgorithm(tt.alg); got != tt.want {
t.Errorf("isECDSAAlgorithm(%v) = %v, want %v", tt.alg, got, tt.want)
}
}
}
func TestConvertECDSARawToASN1(t *testing.T) {
t.Run("P256_roundtrip", func(t *testing.T) {
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
t.Fatal(err)
}
msg := []byte("test data for ECDSA signature")
hash := sha256.Sum256(msg)
r, s, err := ecdsa.Sign(rand.Reader, key, hash[:])
if err != nil {
t.Fatal(err)
}
// Build raw r||s (32 bytes each for P-256)
raw := make([]byte, 64)
rBytes := r.Bytes()
sBytes := s.Bytes()
copy(raw[32-len(rBytes):32], rBytes)
copy(raw[64-len(sBytes):64], sBytes)
der, err := convertECDSARawToASN1(x509.ECDSAWithSHA256, raw)
if err != nil {
t.Fatal(err)
}
// Parse back and verify r, s match
var parsed struct{ R, S *big.Int }
_, err = asn1.Unmarshal(der, &parsed)
if err != nil {
t.Fatalf("failed to unmarshal DER: %v", err)
}
if parsed.R.Cmp(r) != 0 {
t.Errorf("r mismatch: got %v, want %v", parsed.R, r)
}
if parsed.S.Cmp(s) != 0 {
t.Errorf("s mismatch: got %v, want %v", parsed.S, s)
}
// Verify with Go's ecdsa.Verify using the DER-decoded values
if !ecdsa.Verify(&key.PublicKey, hash[:], parsed.R, parsed.S) {
t.Error("signature verification failed after roundtrip")
}
})
t.Run("P384_roundtrip", func(t *testing.T) {
key, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
if err != nil {
t.Fatal(err)
}
msg := []byte("test P384")
hash := sha512.Sum384(msg)
r, s, err := ecdsa.Sign(rand.Reader, key, hash[:])
if err != nil {
t.Fatal(err)
}
raw := make([]byte, 96)
rBytes := r.Bytes()
sBytes := s.Bytes()
copy(raw[48-len(rBytes):48], rBytes)
copy(raw[96-len(sBytes):96], sBytes)
der, err := convertECDSARawToASN1(x509.ECDSAWithSHA384, raw)
if err != nil {
t.Fatal(err)
}
var parsed struct{ R, S *big.Int }
if _, err := asn1.Unmarshal(der, &parsed); err != nil {
t.Fatalf("failed to unmarshal: %v", err)
}
if !ecdsa.Verify(&key.PublicKey, hash[:], parsed.R, parsed.S) {
t.Error("P-384 signature verification failed after roundtrip")
}
})
t.Run("empty_input", func(t *testing.T) {
_, err := convertECDSARawToASN1(x509.ECDSAWithSHA256, []byte{})
if err == nil {
t.Error("expected error for empty input")
}
})
t.Run("odd_length", func(t *testing.T) {
_, err := convertECDSARawToASN1(x509.ECDSAWithSHA256, make([]byte, 63))
if err == nil {
t.Error("expected error for odd-length input")
}
})
}