-
Notifications
You must be signed in to change notification settings - Fork 5
/
ecc.go
164 lines (154 loc) · 4.14 KB
/
ecc.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
package jwt
import (
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"math/big"
)
// EccSigner provides an Elliptic Curve Signer.
type EccSigner struct {
alg Algorithm
curve elliptic.Curve
hash crypto.Hash
priv *ecdsa.PrivateKey
pub *ecdsa.PublicKey
keyLen int
}
// NewEllipticSigner creates an Elliptic Curve Signer for the specified curve.
func NewEllipticSigner(alg Algorithm, curve elliptic.Curve) func(Store, crypto.Hash) (Signer, error) {
// precompute curve key len
curveBitSize := curve.Params().BitSize
keyLen := curveBitSize / 8
if curveBitSize%8 > 0 {
keyLen++
}
return func(store Store, hash crypto.Hash) (Signer, error) {
var ok bool
var privRaw, pubRaw interface{}
var priv *ecdsa.PrivateKey
var pub *ecdsa.PublicKey
// check private key
if privRaw, ok = store.PrivateKey(); ok {
if priv, ok = privRaw.(*ecdsa.PrivateKey); !ok {
return nil, ErrInvalidPrivateKey
}
// check curve type matches private key curve type
if curveBitSize != priv.Curve.Params().BitSize {
return nil, ErrInvalidPrivateKeySize
}
}
// check public key
if pubRaw, ok = store.PublicKey(); ok {
if pub, ok = pubRaw.(*ecdsa.PublicKey); !ok {
return nil, ErrInvalidPublicKey
}
}
// check that either a private or public key has been provided
if priv == nil && pub == nil {
return nil, ErrMissingPrivateOrPublicKey
}
return &EccSigner{
alg: alg,
curve: curve,
hash: hash,
priv: priv,
pub: pub,
keyLen: keyLen,
}, nil
}
}
// Mksig creates a byte slice of length 2*keyLen, copying the bytes from r and
// s into the slice, left padding r and i to keyLen.
func (s *EccSigner) Mksig(r, i *big.Int) ([]byte, error) {
buf := make([]byte, 2*s.keyLen)
// copy r into buf
rb := r.Bytes()
if n := copy(buf[s.keyLen-len(rb):], rb); n != len(rb) {
return nil, ErrMismatchedBytesCopied
}
// copy s into buf
sb := i.Bytes()
if n := copy(buf[s.keyLen+(s.keyLen-(len(sb))):], sb); n != len(sb) {
return nil, ErrMismatchedBytesCopied
}
return buf, nil
}
// SignBytes creates a signature for buf.
func (s *EccSigner) SignBytes(buf []byte) ([]byte, error) {
// check es.priv
if s.priv == nil {
return nil, ErrInvalidPrivateKey
}
// hash
h := s.hash.New()
if _, err := h.Write(buf); err != nil {
return nil, err
}
// sign
r, i, err := ecdsa.Sign(rand.Reader, s.priv, h.Sum(nil))
if err != nil {
return nil, err
}
// make sig
return s.Mksig(r, i)
}
// Sign creates a signature for buf, returning it as a URL-safe base64 encoded
// byte slice.
func (s *EccSigner) Sign(buf []byte) ([]byte, error) {
sig, err := s.SignBytes(buf)
if err != nil {
return nil, err
}
enc := make([]byte, b64.EncodedLen(len(sig)))
b64.Encode(enc, sig)
return enc, nil
}
// VerifyBytes creates a signature for buf, comparing it against the raw sig.
// If the sig is invalid, then ErrInvalidSignature is returned.
func (s *EccSigner) VerifyBytes(buf, sig []byte) error {
// check es.pub
if s.pub == nil {
return ErrInvalidPublicKey
}
// hash
h := s.hash.New()
if _, err := h.Write(buf); err != nil {
return err
}
// check decoded length
if len(sig) != 2*s.keyLen {
return ErrInvalidSignature
}
r := big.NewInt(0).SetBytes(sig[:s.keyLen])
i := big.NewInt(0).SetBytes(sig[s.keyLen:])
// verify
if !ecdsa.Verify(s.pub, h.Sum(nil), r, i) {
return ErrInvalidSignature
}
return nil
}
// Verify creates a signature for buf, comparing it against the URL-safe base64
// encoded sig and returning the decoded signature. If the sig is invalid, then
// ErrInvalidSignature will be returned.
func (s *EccSigner) Verify(buf, sig []byte) ([]byte, error) {
// decode
dec, err := b64.DecodeString(string(sig))
if err != nil {
return nil, err
}
// verify
if err = s.VerifyBytes(buf, dec); err != nil {
return nil, err
}
return dec, nil
}
// Encode serializes the JSON marshalable obj data as a JWT.
func (s *EccSigner) Encode(obj interface{}) ([]byte, error) {
return s.alg.Encode(s, obj)
}
// Decode decodes a serialized token, verifying the signature, storing the
// decoded data from the token in obj.
func (s *EccSigner) Decode(buf []byte, obj interface{}) error {
return s.alg.Decode(s, buf, obj)
}