-
Notifications
You must be signed in to change notification settings - Fork 1
/
dsa.go
145 lines (128 loc) · 3.23 KB
/
dsa.go
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
package dsa
import (
"crypto/dsa"
"crypto/rand"
"crypto/sha1"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"encoding/pem"
"math/big"
"github.com/abemedia/appcast/pkg/crypto"
)
type (
PrivateKey = dsa.PrivateKey
PublicKey = dsa.PublicKey
)
type privateKey struct {
Version int
P, Q, G, Y, X *big.Int
}
type signature struct {
R, S *big.Int
}
// Sign signs the data with the private key.
func Sign(key *PrivateKey, data []byte) ([]byte, error) {
if key == nil {
return nil, crypto.ErrInvalidKey
}
sum := sha1.Sum(data)
r, s, err := dsa.Sign(rand.Reader, key, sum[:])
if err != nil {
return nil, err
}
return asn1.Marshal(signature{r, s})
}
// Verify verifies the signature of the data with the public key.
func Verify(key *PublicKey, data, sig []byte) bool {
if key == nil {
return false
}
var s signature
if _, err := asn1.Unmarshal(sig, &s); err != nil {
return false
}
sum := sha1.Sum(data)
return dsa.Verify(key, sum[:], s.R, s.S)
}
// NewPrivateKey returns a new private key.
func NewPrivateKey() (*PrivateKey, error) {
var key PrivateKey
err := dsa.GenerateParameters(&key.Parameters, rand.Reader, dsa.L3072N256)
if err != nil {
return nil, err
}
err = dsa.GenerateKey(&key, rand.Reader)
if err != nil {
return nil, err
}
return &key, nil
}
// MarshalPrivateKey returns the PEM encoded private key.
func MarshalPrivateKey(key *PrivateKey) ([]byte, error) {
if key == nil {
return nil, crypto.ErrInvalidKey
}
b, err := asn1.Marshal(privateKey{0, key.P, key.Q, key.G, key.Y, key.X})
if err != nil {
return nil, err
}
return pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: b}), nil
}
// UnmarshalPrivateKey returns a private key from a PEM encoded key.
func UnmarshalPrivateKey(b []byte) (*PrivateKey, error) {
block, _ := pem.Decode(b)
if block == nil {
return nil, crypto.ErrInvalidKey
}
var k privateKey
if _, err := asn1.Unmarshal(block.Bytes, &k); err != nil {
return nil, crypto.ErrInvalidKey
}
return &dsa.PrivateKey{
PublicKey: dsa.PublicKey{
Parameters: dsa.Parameters{P: k.P, Q: k.Q, G: k.G},
Y: k.Y,
},
X: k.X,
}, nil
}
// Public extracts the public key from a private key.
func Public(key *PrivateKey) *PublicKey {
return &key.PublicKey
}
// MarshalPublicKey returns the PEM encoded public key.
func MarshalPublicKey(key *PublicKey) ([]byte, error) {
if key == nil {
return nil, crypto.ErrInvalidKey
}
var pub struct {
Algo pkix.AlgorithmIdentifier
BitString asn1.BitString
}
pub.Algo.Algorithm = []int{1, 2, 840, 10040, 4, 1}
pub.Algo.Parameters.FullBytes, _ = asn1.Marshal(key.Parameters)
pub.BitString.Bytes, _ = asn1.Marshal(key.Y)
pub.BitString.BitLength = len(pub.BitString.Bytes) * 8
b, err := asn1.Marshal(pub)
if err != nil {
return nil, err
}
return pem.EncodeToMemory(&pem.Block{Type: "PUBLIC KEY", Bytes: b}), nil
}
// UnmarshalPublicKey returns a public key from a PEM encoded key.
func UnmarshalPublicKey(b []byte) (*PublicKey, error) {
block, _ := pem.Decode(b)
if block == nil {
return nil, crypto.ErrInvalidKey
}
key, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, crypto.ErrInvalidKey
}
dsaKey, ok := key.(*PublicKey)
if !ok {
return nil, crypto.ErrWrongKeyType
}
return dsaKey, nil
}