-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecdsa.go
187 lines (157 loc) · 4.79 KB
/
ecdsa.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package jws
import (
"crypto/ecdsa"
"crypto/rand"
"crypto/sha256"
"crypto/sha512"
"fmt"
"hash"
"math/big"
)
const (
// ECDSA using P-256 and SHA-256
ALG_ES256 SignatureAlgorithm = "ES256"
// ECDSA using P-384 and SHA-384
ALG_ES384 SignatureAlgorithm = "ES384"
// ECDSA using P-521 and SHA-512
ALG_ES512 SignatureAlgorithm = "ES512"
)
// ecdsaSigner implements a signature signer using an ECDSA algorithm with
// SHA-2 based hashing as defined in RFC 7518 section 3.4
// (https://www.rfc-editor.org/rfc/rfc7518.html#section-3.4)
type ecdsaSigner struct {
alg SignatureAlgorithm
privateKey *ecdsa.PrivateKey
hf func() hash.Hash
keyBitSize int
}
func (e *ecdsaSigner) Alg() SignatureAlgorithm {
return e.alg
}
func (e *ecdsaSigner) Sign(data []byte) ([]byte, error) {
h := e.hf()
h.Write(data)
hashed := h.Sum(nil)
r, s, err := ecdsa.Sign(rand.Reader, e.privateKey, hashed)
if err != nil {
return nil, err
}
keyBytes := e.keyBitSize / 8
if e.keyBitSize%8 > 0 {
keyBytes++
}
out := make([]byte, 2*keyBytes)
rBytes := r.Bytes()
copy(out[keyBytes-len(rBytes):], rBytes)
sBytes := s.Bytes()
copy(out[keyBytes+keyBytes-len(sBytes):], sBytes)
return out, nil
}
// ES256Signer creates a Signer providing ECDSA using P-256 and SHA-256
// signatures using the given private key which must use
// ellipctic.P256() as the underlying curve.
func ES256Signer(privateKey *ecdsa.PrivateKey) (Signer, error) {
if privateKey.Curve.Params().BitSize != 256 {
return nil, fmt.Errorf("invalid key: must use elliptic curve key with curve bit size of 256")
}
return &ecdsaSigner{
alg: ALG_ES256,
privateKey: privateKey,
hf: sha256.New,
keyBitSize: 256,
}, nil
}
// ES384Signer creates a Signer providing ECDSA using P-384 and SHA-384
// signatures using the given private key which must use
// ellipctic.P384() as the underlying curve.
func ES384Signer(privateKey *ecdsa.PrivateKey) (Signer, error) {
if privateKey.Curve.Params().BitSize != 384 {
return nil, fmt.Errorf("invalid key: must use elliptic curve key with curve bit size of 384")
}
return &ecdsaSigner{
alg: ALG_ES384,
privateKey: privateKey,
hf: sha512.New384,
keyBitSize: 384,
}, nil
}
// ES512Signer creates a Signer providing ECDSA using P-512 and SHA-512
// signatures using the given private key which must use
// ellipctic.P512() as the underlying curve.
func ES512Signer(privateKey *ecdsa.PrivateKey) (Signer, error) {
if privateKey.Curve.Params().BitSize != 521 {
return nil, fmt.Errorf("invalid key: must use elliptic curve key with curve bit size of 521")
}
return &ecdsaSigner{
alg: ALG_ES512,
privateKey: privateKey,
hf: sha512.New,
keyBitSize: 521,
}, nil
}
type ecdsaVerifier struct {
alg SignatureAlgorithm
publicKey *ecdsa.PublicKey
hf func() hash.Hash
keyBitSize int
}
func (e *ecdsaVerifier) Verify(alg SignatureAlgorithm, data, signature []byte) error {
if alg != e.alg {
return fmt.Errorf("%w: %s", ErrInvalidSignature, "invalid algorithm")
}
r := big.NewInt(0)
s := big.NewInt(0)
n := len(signature) / 2
r.SetBytes(signature[:n])
s.SetBytes(signature[n:])
h := e.hf()
h.Write(data)
hashed := h.Sum(nil)
ok := ecdsa.Verify(e.publicKey, hashed, r, s)
if !ok {
return ErrInvalidSignature
}
return nil
}
// ES256Verifier creates a Verifier verifying ECDSA using P-256 and SHA-256
// signatures using the given public key which must use
// ellipctic.P256() as the underlying curve.
func ES256Verifier(publicKey *ecdsa.PublicKey) (Verifier, error) {
if publicKey.Params().BitSize != 256 {
return nil, fmt.Errorf("invalid key: must use elliptic curve key with curve bit size of 256")
}
return &ecdsaVerifier{
alg: ALG_ES256,
publicKey: publicKey,
hf: sha256.New,
keyBitSize: 256,
}, nil
}
// ES384Verifier creates a Verifier verifying ECDSA using P-384 and SHA-384
// signatures using the given public key which must use
// ellipctic.P384() as the underlying curve.
func ES384Verifier(publicKey *ecdsa.PublicKey) (Verifier, error) {
if publicKey.Params().BitSize != 384 {
return nil, fmt.Errorf("invalid key: must use elliptic curve key with curve bit size of 384")
}
return &ecdsaVerifier{
alg: ALG_ES384,
publicKey: publicKey,
hf: sha512.New384,
keyBitSize: 384,
}, nil
}
// ES512Verifier creates a Verifier verifying ECDSA using P-512 and SHA-512
// signatures using the given public key which must use
// ellipctic.P512() as the underlying curve.
func ES512Verifier(publicKey *ecdsa.PublicKey) (Verifier, error) {
if publicKey.Params().BitSize != 521 {
return nil, fmt.Errorf("invalid key: must use elliptic curve key with curve bit size of 521")
}
return &ecdsaVerifier{
alg: ALG_ES512,
publicKey: publicKey,
hf: sha512.New,
keyBitSize: 521,
}, nil
}