-
Notifications
You must be signed in to change notification settings - Fork 0
/
key_ecdsa.go
133 lines (110 loc) · 3.13 KB
/
key_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
// SPDX-FileCopyrightText: 2023-2024 Steffen Vogel <[email protected]>
// SPDX-License-Identifier: Apache-2.0
package openpgp
import (
"crypto"
"crypto/ecdh"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/sha1" //nolint:gosec
"encoding/asn1"
"encoding/binary"
"io"
"math/big"
"time"
iso "cunicu.li/go-iso7816"
"cunicu.li/go-iso7816/encoding/tlv"
)
var (
_ crypto.Signer = (*PrivateKeyECDSA)(nil)
_ crypto.Decrypter = (*PrivateKeyECDSA)(nil)
)
type PrivateKeyECDSA struct {
card *Card
curve Curve
key KeyRef
public *ecdsa.PublicKey
}
func (k *PrivateKeyECDSA) Public() crypto.PublicKey {
return k.public
}
// See: OpenPGP Smart Card Application - Section 7.2.10 PSO: COMPUTE DIGITAL SIGNATURE
func (k *PrivateKeyECDSA) Sign(_ io.Reader, digest []byte, opts crypto.SignerOpts) (signature []byte, err error) {
if l := len(digest); (opts != nil && l != opts.HashFunc().Size()) || (l != 32 && l != 48 && l != 64) {
return nil, ErrInvalidLength
}
ds, err := send(k.card.tx, iso.InsPerformSecurityOperation, 0x9e, 0x9a, digest)
if err != nil {
return nil, err
}
return asn1.Marshal(struct {
R, S *big.Int
}{
R: new(big.Int).SetBytes(ds[:len(ds)/2]),
S: new(big.Int).SetBytes(ds[len(ds)/2:]),
})
}
// See: OpenPGP Smart Card Application - Section 7.2.11 PSO: DECIPHER
func (k *PrivateKeyECDSA) Decrypt(_ io.Reader, _ /*msg*/ []byte, _ /*opts*/ crypto.DecrypterOpts) (plaintext []byte, err error) {
return nil, ErrUnsupported
}
func (k PrivateKeyECDSA) fingerprint(creationTime time.Time) []byte {
var alg AlgPubkey
switch {
case k.curve == CurveX25519:
alg = AlgPubkeyEdDSA
default:
alg = AlgPubkeyECDSA
}
buf := []byte{
0x99, // Prefix
0, 0, // Packet length
0x04, // Version
0, 0, 0, 0, // Creation timestamp
byte(alg),
}
pk, err := k.public.ECDH()
if err != nil {
return nil
}
buf = append(buf, k.curve.OID()...)
buf = appendBytesMPI(buf, pk.Bytes())
buf = appendKDF(buf, AlgHashSHA512, AlgSymAES256) // same default values as Sequoia
binary.BigEndian.PutUint16(buf[1:], uint16(len(buf)-3)) // Fill in packet length
binary.BigEndian.PutUint32(buf[4:], uint32(creationTime.Unix())) // Fill in generation timestamp
digest := sha1.New() //nolint:gosec
digest.Write(buf)
return digest.Sum(nil)
}
func decodePublicECDSA(tvs tlv.TagValues, curve Curve) (*ecdsa.PublicKey, error) {
pkECDH, err := decodePublicECDH(tvs, curve)
if err != nil {
return nil, err
}
return ecdhToECDSAPublicKey(pkECDH)
}
func ecdhToECDSAPublicKey(key *ecdh.PublicKey) (*ecdsa.PublicKey, error) {
rawKey := key.Bytes()
switch key.Curve() {
case ecdh.P256():
return &ecdsa.PublicKey{
Curve: elliptic.P256(),
X: big.NewInt(0).SetBytes(rawKey[1:33]),
Y: big.NewInt(0).SetBytes(rawKey[33:]),
}, nil
case ecdh.P384():
return &ecdsa.PublicKey{
Curve: elliptic.P384(),
X: big.NewInt(0).SetBytes(rawKey[1:49]),
Y: big.NewInt(0).SetBytes(rawKey[49:]),
}, nil
case ecdh.P521():
return &ecdsa.PublicKey{
Curve: elliptic.P521(),
X: big.NewInt(0).SetBytes(rawKey[1:67]),
Y: big.NewInt(0).SetBytes(rawKey[67:]),
}, nil
default:
return nil, ErrUnsupportedCurve
}
}