-
Notifications
You must be signed in to change notification settings - Fork 0
/
key_eddsa.go
76 lines (60 loc) · 1.81 KB
/
key_eddsa.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
// SPDX-FileCopyrightText: 2023-2024 Steffen Vogel <[email protected]>
// SPDX-License-Identifier: Apache-2.0
package openpgp
import (
"crypto"
"crypto/ed25519"
"crypto/sha1" //nolint:gosec
"encoding/binary"
"fmt"
"io"
"time"
iso "cunicu.li/go-iso7816"
"cunicu.li/go-iso7816/encoding/tlv"
)
var _ crypto.Signer = (*PrivateKeyEdDSA)(nil)
//nolint:unused
type PrivateKeyEdDSA struct {
card *Card
attrs AlgorithmAttributes
key KeyRef
public ed25519.PublicKey
}
//nolint:unused
func (k PrivateKeyEdDSA) fingerprint(creationTime time.Time) []byte {
buf := []byte{
0x99, // Prefix
0, 0, // Packet length
0x04, // Version
0, 0, 0, 0, // Creation timestamp
byte(AlgPubkeyEdDSA),
}
buf = append(buf, k.attrs.OID...)
buf = append(buf, k.public...)
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 (k PrivateKeyEdDSA) Public() crypto.PublicKey {
return k.public
}
// See: OpenPGP Smart Card Application - Section 7.2.10 PSO: COMPUTE DIGITAL SIGNATURE
func (k PrivateKeyEdDSA) 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
}
return send(k.card.tx, iso.InsPerformSecurityOperation, 0x9e, 0x9a, digest)
}
func decodePublicEdDSA(tvs tlv.TagValues) (ed25519.PublicKey, error) {
_, tvs, ok := tvs.Get(tagPublicKey)
if !ok {
return nil, fmt.Errorf("%w: public key", errMissingTag)
}
p, _, ok := tvs.Get(tagPublicKeyEC)
if !ok {
return nil, fmt.Errorf("%w: points", errMissingTag)
}
return p, nil
}