Research/Bitcoin Dev Archive / lightning-dev list
Onion routing design.
9 messages, Sep 18 to 22, 2015. Started by Rusty Russell.
Rusty Russell (4), Anthony Towns (4), Zooko Wilcox-OHearn
Hi all!
So, we've handwaved that we're going to have onion routing, so each node can only see the immediate neighbors.
Here's my first attempt; please fix :)
The format of a route looks like so:
required bytes route;
A node decrypts that using its pubkey (probably some counter mode scheme, but that's for a different post!), expecting:
// Sum of this whole thing after decryption.
required sha256_hash sum = 1;
// Where to next?
oneof next {
// Actually, this is the last one
bool end = 2;
// Next lightning node.
pubkey lightning = 3;
// Other realms go here...
}
// How much fee you can take (== all, if last node)
required int32 fee = 4;
// Remainder (route blob for next node).
required bytes route = 5;
If the sum is wrong, the routing has failed (it's been corrupted or was malformed to start).
Nodes create the route backwards, to calculate the size. Then picks a total size randomly between 1024 and 4096, and pads to that size (at least 32 bytes of random padding). Then walks backwards to wrap and encrypt it.
This offers some protection from guessing the route length.
Route Probing Attacks ===================== Now, there's a weakness here: No MAC! A nosy node can't corrupt the routing past the next hop, but it could replace it entirely (this is fundamental to the scheme of R values). If it guesses the final destination right, the HTLC will succeed, otherwise it will fail, so it can use this to probe.
One partial defence is to fail to allow two HTLCs with the same R value, forcing probe serialization. Unfortunately that allows a simple way to probe back to the source, so we shouldn't do this!
We may be able to do some probabalistic backoff of duplicate R values, such that you can't tell if I've received one before? A more sophisticated probe sequence could get a probability though...
I can't see a fix for this in general. :(
Error Messages ============== Another issue is that we should try not to leak information to passive observers on the route due to errors, so signing errors and using the 'sum' field as a secret key seems sensible. This means your padding in the original message needs to be "random" so 'sum' is random.
Thoughts welcome!
Rusty.
PS. As noted before, nodes can trivially correlate HTLCs by R value, so
onioning the routing only gets you so far...On 19 September 2015 9:39:44 am AEST, Rusty Russell <rusty at rustcorp.com.au> wrote:
Quoted text (18 lines)
>Route Probing Attacks >===================== >Now, there's a weakness here: No MAC! A nosy node can't corrupt the >routing past the next hop, but it could replace it entirely (this is >fundamental to the scheme of R values). If it guesses the final >destination right, the HTLC will succeed, otherwise it will fail, so it >can use this to probe. > >One partial defence is to fail to allow two HTLCs with the same R >value, >forcing probe serialization. Unfortunately that allows a simple way to >probe back to the source, so we shouldn't do this! > >We may be able to do some probabalistic backoff of duplicate R values, >such that you can't tell if I've received one before? A more >sophisticated probe sequence could get a probability though... > >I can't see a fix for this in general. :(
I don't think parallel probes work well - if any of your probes succeed, your neighbour knows R and can claim all of your probes. Parallelization is also limited by channel capacity, assuming the payee knows how much to expect.
I'm not sure probing is really plausible given mass deployment, is it? You have to guess the eventual recipient but given randomised routing you have every person or business using lightning as a potential candidate with possibly equal probability?
For a general solution, I think you could completely rule out probing by having two R values, one known only by the recipient, and one by the sender (call it S say). Then make the htlcs payable on presentation of both R and S and include S encrypted to the final recipient in the onion payload. Munging the payload then makes the htlc irredeemable so misrouting it gives no information.
(Please let me know if the formatting of this mail is too hopeless; trying out a new setup)
Cheers, aj
On 19 September 2015 9:39:44 am AEST, Rusty Russell <rusty at rustcorp.com.au> wrote:
Quoted text (23 lines)
> // Sum of this whole thing after decryption.
> required sha256_hash sum = 1;
>
> // Where to next?
> oneof next {
> // Actually, this is the last one
> bool end = 2;
> // Next lightning node.
> pubkey lightning = 3;
> // Other realms go here...
> }
>
> // How much fee you can take (== all, if last node)
> required int32 fee = 4;
>
> // Remainder (route blob for next node).
> required bytes route = 5;
>Nodes create the route backwards, to calculate the size. Then picks a
>total size randomly between 1024 and 4096, and pads to that size (at
>least 32 bytes of random padding). Then walks backwards to wrap and
>encrypt it.
>
>This offers some protection from guessing the route length.Wouldn't it be better to store the expected balance to be forwarded rather than the fee? That way if someone takes too much, the next node can immediately abort the transaction.
Shouldn't the onion blob be re-padded when forwarded? Otherwise it'd sometimes drop below 1024 bytes and you'd be able to tell you're near the end of the chain. Just adding random bytes on to the end should work I think - they'll just be treated as extra padding and ignored. Alternatively, maybe you could just append the encrypted bytes that made up your post of the payload from your incoming message: it was encrypted to you so should be effectively random to anyone else and will be garbled further when the next node "decrypts" it. That would save on entropy/crypto ops, and a constant message size would leak minimal info I think.
Cheers, aj
Anthony Towns <aj at erisian.com.au> writes:
Quoted text (8 lines)
> On 19 September 2015 9:39:44 am AEST, Rusty Russell <rusty at rustcorp.com.au> wrote: >>Route Probing Attacks >>===================== >>Now, there's a weakness here: No MAC! A nosy node can't corrupt the >>routing past the next hop, but it could replace it entirely (this is >>fundamental to the scheme of R values). If it guesses the final >>destination right, the HTLC will succeed, otherwise it will fail, so it >>can use this to probe.
...
Quoted text (3 lines)
>>I can't see a fix for this in general. :( > > I don't think parallel probes work well - if any of your probes succeed, your neighbour knows R and can claim all of your probes. Parallelization is also limited by channel capacity, assuming the payee knows how much to expect.
Channel capacity might not be an issue for tiny micropayments, but the reveal of R is a good point: such probing should have a real cost on success. I'll be sure to implement that properly :)
Quoted text (1 line)
> I'm not sure probing is really plausible given mass deployment, is it? You have to guess the eventual recipient but given randomised routing you have every person or business using lightning as a potential candidate with possibly equal probability?
If someone wants to know whether I'm sending money to you, it would work. Get a cheap hub near you, and one near me, and probe every payment which passes through both.
But I guess it's a fairly boutique surveillance, which doesn't scale.
Quoted text (1 line)
> For a general solution, I think you could completely rule out probing by having two R values, one known only by the recipient, and one by the sender (call it S say). Then make the htlcs payable on presentation of both R and S and include S encrypted to the final recipient in the onion payload. Munging the payload then makes the htlc irredeemable so misrouting it gives no information.
That's clever. And I think it works. I will need more coffee to figure out if we should revise the transaction structure to include this.
Quoted text (1 line)
> (Please let me know if the formatting of this mail is too hopeless; trying out a new setup)
No work wrap, but it seemed to work fine.
Cheers, Rusty.
Anthony Towns <aj at erisian.com.au> writes:
Quoted text (26 lines)
> On 19 September 2015 9:39:44 am AEST, Rusty Russell <rusty at rustcorp.com.au> wrote:
>> // Sum of this whole thing after decryption.
>> required sha256_hash sum = 1;
>>
>> // Where to next?
>> oneof next {
>> // Actually, this is the last one
>> bool end = 2;
>> // Next lightning node.
>> pubkey lightning = 3;
>> // Other realms go here...
>> }
>>
>> // How much fee you can take (== all, if last node)
>> required int32 fee = 4;
>>
>> // Remainder (route blob for next node).
>> required bytes route = 5;
>
>>Nodes create the route backwards, to calculate the size. Then picks a
>>total size randomly between 1024 and 4096, and pads to that size (at
>>least 32 bytes of random padding). Then walks backwards to wrap and
>>encrypt it.
>>
>>This offers some protection from guessing the route length.
> Wouldn't it be better to store the expected balance to be forwarded rather than the fee? That way if someone takes too much, the next node can immediately abort the transaction.Yes, excellent point. I flip-flopped on that; so I flipped again.
Quoted text (3 lines)
> Shouldn't the onion blob be re-padded when forwarded? Otherwise it'd > sometimes drop below 1024 bytes and you'd be able to tell you're near > the end of the chain.
Yes and this applies for almost any length selection method.
Quoted text (2 lines)
> Just adding random bytes on to the end should > work I think - they'll just be treated as extra padding and ignored.
My original plan. But it enables a probing attack where you corrupt the packet and if it succeeds anyway you know those bytes weren't used.
Quoted text (1 line)
> Alternatively, maybe you could just append the encrypted bytes that made up your post of the payload from your incoming message: it was encrypted to you so should be effectively random to anyone else and will be garbled further when the next node "decrypts" it. That would save on entropy/crypto ops, and a constant message size would leak minimal info I think.
So rotate the payload? I don't think that's verifiable by the recipient, so has the same probing attack issue as above.
Hmm, what if we include a pubkey, and you use that to encrypt 0 padding? The last hop gets the privkey (and boundary information), and she can verify the padding.
Off the top of my head, that solves both the length problem, and the "route replacement" problem.
But I've not had coffee yet, plus jetlag. Could be missing a flaw or simplification!
Thanks, Rusty.
On Mon, Sep 21, 2015 at 06:35:58AM +0930, Rusty Russell wrote:
Quoted text (8 lines)
> Anthony Towns <aj at erisian.com.au> writes: > > On 19 September 2015 9:39:44 am AEST, Rusty Russell <rusty at rustcorp.com.au> wrote: > >> // Sum of this whole thing after decryption. > >> required sha256_hash sum = 1; > > Just adding random bytes on to the end should > > work I think - they'll just be treated as extra padding and ignored. > My original plan. But it enables a probing attack where you corrupt the > packet and if it succeeds anyway you know those bytes weren't used.
Actually, any random bytes on the end would be included (after decryption) when calculating the sha256_hash sum by the next node, no? Otherwise you'd at least need a length field to tell how much of the packet needs verifying.
Hmm, I'm not sure it's possible to have the hash actually work at the same time as letting nodes add padding?
You could have the hash only verify the current message and the following (encrypted) message. None of the hashes would ever then verify the added padding; but you'd still be vulnerable to probing attacks.
Probing attacks only reveal the length of the chain, of course. AFAICS tor is vulnerable to this sort of probing attack? Weird.
Quoted text (3 lines)
> Hmm, what if we include a pubkey, and you use that to encrypt 0 padding? > The last hop gets the privkey (and boundary information), and she can > verify the padding.
Actually, this is missing a step isn't it? Don't we have to generate symmetric keys for each step in an onion route, transmitting to Bob:
E_B( KB ) E_KB( forward $20 to Carol; E_C( KC ) E_KC( ... ) )
In which case, I think Bob can append E_KB(00000..) as padding, which we can predict, and eg, the final message to Dave will look like:
E_D( KD ) E_KD( H; here's $15 )
E_KD( grbg grbg ... grbg )
D_KC( D_KB( E_KA( 0000 ) ) )
D_KC( E_KB( 0000 ) )
E_KC( 0000 )
If you use OFB or CTR mode for the symmetric cypher, you can calculate D_KD() of all the padding and use that to work out the hash H of the plaintex message:
here's $15
grbg grbg ... grbg
D_KD(
D_KC( D_KB( E_KA( 0000 ) ) )
...
)
I don't think you can use CBC mode, because you'd need to know E_KD(grbg) in order to know D_KD( padding ), but you need to know grbg in order to know H, and you need to know E_KD(H + ...) in order to work out E_KD(grbg).
You can't use sum for Kx because you need to know Kx before working out the sum...
I think that means that:
Quoted text (2 lines)
>>> The format of a route looks like so: >>> required bytes route;
should actually be something like:
required bytes hop_key; // encrypted with public key
required bytes route; // encrypted with hop_key
(possibly with an IV as well, if the block cypher mode doesn't just include that in the cyphertext)
Quoted text (2 lines)
> But I've not had coffee yet, plus jetlag. Could be missing a flaw or > simplification!
It's crypto protocols; it's almost always safe to assume you're missing a flaw...
Cheers, aj
Could you please point me to any notes about requirements/desiderata?
Thanks!
--Zooko On Sep 18, 2015 5:47 PM, "Rusty Russell" <rusty at rustcorp.com.au> wrote:
Quoted text (72 lines)
> Hi all!
>
> So, we've handwaved that we're going to have onion routing, so
> each node can only see the immediate neighbors.
>
> Here's my first attempt; please fix :)
>
> The format of a route looks like so:
>
> required bytes route;
>
> A node decrypts that using its pubkey (probably some counter mode
> scheme, but that's for a different post!), expecting:
>
> // Sum of this whole thing after decryption.
> required sha256_hash sum = 1;
>
> // Where to next?
> oneof next {
> // Actually, this is the last one
> bool end = 2;
> // Next lightning node.
> pubkey lightning = 3;
> // Other realms go here...
> }
>
> // How much fee you can take (== all, if last node)
> required int32 fee = 4;
>
> // Remainder (route blob for next node).
> required bytes route = 5;
>
> If the sum is wrong, the routing has failed (it's been corrupted or was
> malformed to start).
>
> Nodes create the route backwards, to calculate the size. Then picks a
> total size randomly between 1024 and 4096, and pads to that size (at
> least 32 bytes of random padding). Then walks backwards to wrap and
> encrypt it.
>
> This offers some protection from guessing the route length.
>
> Route Probing Attacks
> =====================
> Now, there's a weakness here: No MAC! A nosy node can't corrupt the
> routing past the next hop, but it could replace it entirely (this is
> fundamental to the scheme of R values). If it guesses the final
> destination right, the HTLC will succeed, otherwise it will fail, so it
> can use this to probe.
>
> One partial defence is to fail to allow two HTLCs with the same R value,
> forcing probe serialization. Unfortunately that allows a simple way to
> probe back to the source, so we shouldn't do this!
>
> We may be able to do some probabalistic backoff of duplicate R values,
> such that you can't tell if I've received one before? A more
> sophisticated probe sequence could get a probability though...
>
> I can't see a fix for this in general. :(
>
> Error Messages
> ==============
> Another issue is that we should try not to leak information to passive
> observers on the route due to errors, so signing errors and using the
> 'sum' field as a secret key seems sensible. This means your padding in
> the original message needs to be "random" so 'sum' is random.
>
> Thoughts welcome!
> Rusty.
> PS. As noted before, nodes can trivially correlate HTLCs by R value, so
> onioning the routing only gets you so far...
>On Tue, Sep 22, 2015 at 11:22:57AM +1000, Anthony Towns wrote:
Quoted text (10 lines)
> If you use OFB or CTR mode for the symmetric cypher, you can calculate > D_KD() of all the padding and use that to work out the hash H of the > plaintex message: > here's $15 > grbg grbg ... grbg > D_KD( > D_KC( D_KB( E_KA( 0000 ) ) ) > D_KC( E_KB( 0000 ) ) > E_KC( 0000 ) > )
On Mon, Sep 21, 2015 at 06:18:37AM +0930, Rusty Russell wrote:
Quoted text (7 lines)
> > For a general solution, I think you could completely rule out probing > > by having two R values, one known only by the recipient, and one by > > the sender (call it S say). Then make the htlcs payable on > > presentation of both R and S and include S encrypted to the final > > recipient in the onion payload. Munging the payload then makes the > > htlc irredeemable so misrouting it gives no information. > That's clever. And I think it works.
You could combine these two approaches actually. If X is the plaintext routing message the payee gets ("here's $15 grbg grbg ..."), and H is its hash that was prefixed to the plaintext, then set S=sha256(H+X), and require revealing S as well as R for payment redemption (ie, include "OP_SHA256 sha256(S) OP_EQ" in the HTLC).
That way *any* attempt to garble the padding makes S unrecoverable and renders the payment unredeemable, without relying on any verification/cooperation from anyone else on the network.
Cheers, aj
Zooko Wilcox-OHearn <zooko at leastauthority.com> writes:
Quoted text (1 line)
> Could you please point me to any notes about requirements/desiderata?
They're mainly spread out over the mailing list discussions, but it's pretty simple:
1) It's a source routing network, so the sender details the payment path. Since fees vary with path, and the sender pays, this is the simplest.
2) For financial privacy reasons, minimizing the knowledge of any part of the network makes sense.
Thus, an onion routing-style setup where each node only knows prev and next seems most sensible. It's not quite as good as you might expect, because the payment is trivially correlated (it requires the same R value) so if you control two nodes on the path you can connect them.
I am currently working on some test code which works like so:
#define MESSAGE_SIZE 128 #define MAX_HOPS 20
// One node hop
struct hop {
unsigned char hmac[SHA256_DIGEST_LENGTH];
/* FIXME: Must use parse/serialize functions. */
secp256k1_pubkey_type pubkey;
unsigned char msg[MESSAGE_SIZE];
};
// Full route description.
struct onion {
struct hop hop[MAX_HOPS];
};
Receive on a node works like so: 1) Use ECDH on onion->hop[0].pubkey and my privkey to extract secret key. 2) Use this to derive - enckey (SHA256(seckey || 0) - hmackey (SHA256(seckey || 1) - iv (SHA256(seckey || 2) - pad_iv (SHA256(seckey || 3) 3) Check HMAC of every part of struct onion after hmac (including pubkey, why not?). If wrong, stop. 4) Decrypt (AES 256 CTR mode, using enckey and iv above) struct onion from pubkey onwards. 5) First struct hop is for us. 6) To forward, remove first hop, and append padding (thus keeping total length the same). 7) Padding is generated by AES-256-CTR encrypting all-zeroes with enckey and pad_iv.
The result should be that even final node has no idea of path length.
It's a bit tricky to generate this onion correctly (I know, my code is still buggy!). But it's perfectly possible. Unused hops are filled with random garbage.
Cheers, Rusty.