• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 13586005509

28 Feb 2025 10:14AM UTC coverage: 68.629% (+9.9%) from 58.77%
13586005509

Pull #9521

github

web-flow
Merge 37d3a70a5 into 8532955b3
Pull Request #9521: unit: remove GOACC, use Go 1.20 native coverage functionality

129950 of 189351 relevant lines covered (68.63%)

23726.46 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.05
/lnencrypt/crypto.go
1
package lnencrypt
2

3
import (
4
        "crypto/rand"
5
        "crypto/sha256"
6
        "fmt"
7
        "io"
8

9
        "github.com/btcsuite/btcd/btcec/v2"
10
        "github.com/lightningnetwork/lnd/keychain"
11
        "golang.org/x/crypto/chacha20poly1305"
12
)
13

14
// baseEncryptionKeyLoc is the KeyLocator that we'll use to derive the base
15
// encryption key used for encrypting all payloads. We use this to then
16
// derive the actual key that we'll use for encryption. We do this
17
// rather than using the raw key, as we assume that we can't obtain the raw
18
// keys, and we don't want to require that the HSM know our target cipher for
19
// encryption.
20
//
21
// TODO(roasbeef): possibly unique encrypt?
22
var baseEncryptionKeyLoc = keychain.KeyLocator{
23
        Family: keychain.KeyFamilyBaseEncryption,
24
        Index:  0,
25
}
26

27
// EncrypterDecrypter is an interface representing an object that encrypts or
28
// decrypts data.
29
type EncrypterDecrypter interface {
30
        // EncryptPayloadToWriter attempts to write the set of provided bytes
31
        // into the passed io.Writer in an encrypted form.
32
        EncryptPayloadToWriter([]byte, io.Writer) error
33

34
        // DecryptPayloadFromReader attempts to decrypt the encrypted bytes
35
        // within the passed io.Reader instance using the key derived from
36
        // the passed keyRing.
37
        DecryptPayloadFromReader(io.Reader) ([]byte, error)
38
}
39

40
// Encrypter is a struct responsible for encrypting and decrypting data.
41
type Encrypter struct {
42
        encryptionKey []byte
43
}
44

45
// KeyRingEncrypter derives an encryption key to encrypt all our files that are
46
// written to disk and returns an Encrypter object holding the key.
47
//
48
// The key itself, is the sha2 of a base key that we get from the keyring. We
49
// derive the key this way as we don't force the HSM (or any future
50
// abstractions) to be able to derive and know of the cipher that we'll use
51
// within our protocol.
52
func KeyRingEncrypter(keyRing keychain.KeyRing) (*Encrypter, error) {
148✔
53
        //  key = SHA256(baseKey)
148✔
54
        baseKey, err := keyRing.DeriveKey(
148✔
55
                baseEncryptionKeyLoc,
148✔
56
        )
148✔
57
        if err != nil {
152✔
58
                return nil, err
4✔
59
        }
4✔
60

61
        encryptionKey := sha256.Sum256(
144✔
62
                baseKey.PubKey.SerializeCompressed(),
144✔
63
        )
144✔
64

144✔
65
        // TODO(roasbeef): throw back in ECDH?
144✔
66

144✔
67
        return &Encrypter{
144✔
68
                encryptionKey: encryptionKey[:],
144✔
69
        }, nil
144✔
70
}
71

72
// ECDHEncrypter derives an encryption key by performing an ECDH operation on
73
// the passed keys. The resulting key is used to encrypt or decrypt files with
74
// sensitive content.
75
func ECDHEncrypter(localKey *btcec.PrivateKey,
76
        remoteKey *btcec.PublicKey) (*Encrypter, error) {
1✔
77

1✔
78
        ecdh := keychain.PrivKeyECDH{
1✔
79
                PrivKey: localKey,
1✔
80
        }
1✔
81
        encryptionKey, err := ecdh.ECDH(remoteKey)
1✔
82
        if err != nil {
1✔
83
                return nil, fmt.Errorf("error deriving encryption key: %w", err)
×
84
        }
×
85

86
        return &Encrypter{
1✔
87
                encryptionKey: encryptionKey[:],
1✔
88
        }, nil
1✔
89
}
90

91
// EncryptPayloadToWriter attempts to write the set of provided bytes into the
92
// passed io.Writer in an encrypted form. We use a 24-byte chachapoly AEAD
93
// instance with a randomized nonce that's pre-pended to the final payload and
94
// used as associated data in the AEAD.
95
func (e Encrypter) EncryptPayloadToWriter(payload []byte,
96
        w io.Writer) error {
66✔
97

66✔
98
        // Before encryption, we'll initialize our cipher with the target
66✔
99
        // encryption key, and also read out our random 24-byte nonce we use
66✔
100
        // for encryption. Note that we use NewX, not New, as the latter
66✔
101
        // version requires a 12-byte nonce, not a 24-byte nonce.
66✔
102
        cipher, err := chacha20poly1305.NewX(e.encryptionKey)
66✔
103
        if err != nil {
66✔
104
                return err
×
105
        }
×
106
        var nonce [chacha20poly1305.NonceSizeX]byte
66✔
107
        if _, err := rand.Read(nonce[:]); err != nil {
66✔
108
                return err
×
109
        }
×
110

111
        // Finally, we encrypted the final payload, and write out our
112
        // ciphertext with nonce pre-pended.
113
        ciphertext := cipher.Seal(nil, nonce[:], payload, nonce[:])
66✔
114

66✔
115
        if _, err := w.Write(nonce[:]); err != nil {
66✔
116
                return err
×
117
        }
×
118
        if _, err := w.Write(ciphertext); err != nil {
66✔
119
                return err
×
120
        }
×
121

122
        return nil
66✔
123
}
124

125
// DecryptPayloadFromReader attempts to decrypt the encrypted bytes within the
126
// passed io.Reader instance using the key derived from the passed keyRing. For
127
// further details regarding the key derivation protocol, see the
128
// KeyRingEncrypter function.
129
func (e Encrypter) DecryptPayloadFromReader(payload io.Reader) ([]byte,
130
        error) {
92✔
131

92✔
132
        // Next, we'll read out the entire blob as we need to isolate the nonce
92✔
133
        // from the rest of the ciphertext.
92✔
134
        packedPayload, err := io.ReadAll(payload)
92✔
135
        if err != nil {
92✔
136
                return nil, err
×
137
        }
×
138
        if len(packedPayload) < chacha20poly1305.NonceSizeX {
94✔
139
                return nil, fmt.Errorf("payload size too small, must be at "+
2✔
140
                        "least %v bytes", chacha20poly1305.NonceSizeX)
2✔
141
        }
2✔
142

143
        nonce := packedPayload[:chacha20poly1305.NonceSizeX]
90✔
144
        ciphertext := packedPayload[chacha20poly1305.NonceSizeX:]
90✔
145

90✔
146
        // Now that we have the cipher text and the nonce separated, we can go
90✔
147
        // ahead and decrypt the final blob so we can properly serialize.
90✔
148
        cipher, err := chacha20poly1305.NewX(e.encryptionKey)
90✔
149
        if err != nil {
90✔
150
                return nil, err
×
151
        }
×
152
        plaintext, err := cipher.Open(nil, nonce, ciphertext, nonce)
90✔
153
        if err != nil {
93✔
154
                return nil, err
3✔
155
        }
3✔
156

157
        return plaintext, nil
87✔
158
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc