-
Notifications
You must be signed in to change notification settings - Fork 6
/
ecc_curves.go
88 lines (75 loc) · 1.97 KB
/
ecc_curves.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
package dtls
import (
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"crypto/x509"
"encoding/asn1"
"errors"
"math/big"
)
type eccCurve uint16
type eccKeypair struct {
curve eccCurve
publicKey []byte
privateKey []byte
}
const (
EccCurve_P256 eccCurve = 0x0017
)
func eccGetKeypair(ec eccCurve) (*eccKeypair, error) {
switch ec { //nolint:golint
case EccCurve_P256:
privateKey, x, y, err := elliptic.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return nil, err
}
return &eccKeypair{ec, elliptic.Marshal(elliptic.P256(), x, y), privateKey}, nil
default:
return nil, errors.New("dtls: invalid ecc curve")
}
}
func eccGetKeyMessage(clientRandom, serverRandom, publicKey []byte, ec eccCurve) []byte {
w := newByteWriter()
w.PutBytes(clientRandom)
w.PutBytes(serverRandom)
w.PutUint8(3)
w.PutUint16(uint16(ec))
w.PutUint8(uint8(len(publicKey)))
w.PutBytes(publicKey)
return w.Bytes()
}
func eccGetKeySignature(clientRandom, serverRandom, publicKey []byte, ec eccCurve, privateKey crypto.PrivateKey) ([]byte, error) {
msg := eccGetKeyMessage(clientRandom, serverRandom, publicKey, ec)
hashed := sha256.Sum256(msg)
return privateKey.(*ecdsa.PrivateKey).Sign(rand.Reader, hashed[:], crypto.SHA256)
}
type ecdsaSignature struct {
R, S *big.Int
}
func eccVerifySignature(hash []byte, sig []byte, certs [][]byte) error {
if len(certs) == 0 {
return errors.New("dtls: no certificates")
}
cert, err := x509.ParseCertificate(certs[0])
if err != nil {
return err
}
switch p := cert.PublicKey.(type) {
case *ecdsa.PublicKey:
ecdsaSig := &ecdsaSignature{}
if _, err := asn1.Unmarshal(sig, ecdsaSig); err != nil {
return err
}
if ecdsaSig.R.Sign() <= 0 || ecdsaSig.S.Sign() <= 0 {
return errors.New("dtls: invalid ecdsa signature")
}
if !ecdsa.Verify(p, hash, ecdsaSig.R, ecdsaSig.S) {
return errors.New("dtls: signature mismatch")
}
return nil
}
return errors.New("dtls: unsupported certificate type")
}