-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
61 lines (41 loc) · 1.48 KB
/
auth.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
package tapcards
import (
"crypto/sha256"
"fmt"
"log/slog"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
)
func (satscard *Satscard) authenticate(cvc string, command command) (*auth, error) {
slog.Debug("AUTH", "CVC", cvc)
slog.Debug("AUTH", "Command", command.Cmd)
cardPublicKey, err := btcec.ParsePubKey(satscard.cardPublicKey[:])
if err != nil {
return nil, err
}
// Derive an ephemeral public/private keypair for performing ECDHE with
// the recipient.
ephemeralPrivateKey, err := secp256k1.GeneratePrivateKey()
if err != nil {
return nil, err
}
ephemeralPublicKey := ephemeralPrivateKey.PubKey().SerializeCompressed()
slog.Debug("AUTH", "EphemeralPublicKey", fmt.Sprintf("%x", ephemeralPublicKey))
// Using ECDHE, derive a shared symmetric key for encryption of the plaintext.
satscard.sessionKey = sha256.Sum256(generateSharedSecret(ephemeralPrivateKey, cardPublicKey))
slog.Debug("AUTH", "SessionKey", fmt.Sprintf("%x", satscard.sessionKey))
slog.Debug("AUTH", "CurrentCardNonce", fmt.Sprintf("%x", satscard.currentCardNonce))
md := sha256.Sum256(append(satscard.currentCardNonce[:], []byte(command.Cmd)...))
f, err := xor(satscard.sessionKey[:], md[:])
if err != nil {
return nil, err
}
mask := f[:len(cvc)]
xcvc, err := xor([]byte(cvc), mask)
if err != nil {
return nil, err
}
slog.Debug("AUTH", "XCVC", fmt.Sprintf("%x", xcvc))
auth := auth{EphemeralPubKey: ephemeralPublicKey, XCVC: xcvc}
return &auth, nil
}