Research/Bitcoin Dev Archive / lightning-dev list
Onion routing strawman proposal
11 messages, Oct 2 to 6, 2015. Started by Rusty Russell.
Anthony Towns (6), Rusty Russell (4), Richard Kiss
Hi all!
So, I've pushed some test onion routing code in an acceptable format: https://github.com/ElementsProject/lightning/blob/onion/test/test_onion.c
#define MESSAGE_SIZE 128
#define MAX_HOPS 20
/* Prepend 0x02 to get pubkey for libsecp256k1 */
struct pubkey {
unsigned char u8[32];
};
struct hop {
unsigned char msg[MESSAGE_SIZE];
struct pubkey pubkey;
struct sha256 hmac;
};
struct onion {
struct hop hop[MAX_HOPS];
};
That's a fixed 3840 bytes; each node decrypts and removed the tail entry, then prepends padding. (Working backwards like that turns out to be slightly more efficient for generating, see git commits).
There's nothing exotic in here: it's AES128 counter mode, HMAC using SHA256, and Bitcoin's EC with ECDH. But crypto review particularly welcome!
Cheers, Rusty.
On Fri, Oct 02, 2015 at 03:18:39PM +0930, Rusty Russell wrote:
Quoted text (9 lines)
> So, I've pushed some test onion routing code in an acceptable > format: > https://github.com/ElementsProject/lightning/blob/onion/test/test_onion.c > struct hop { > unsigned char msg[MESSAGE_SIZE]; > struct pubkey pubkey; > struct sha256 hmac; > }; > That's a fixed 3840 bytes;
Is the idea to switch that to non-fixed size using protobufs, or?
Quoted text (1 line)
> then prepends padding.
(prefixes...)
My understandng is it works as follows:
* random EC key pairs are generated for each hop, known to the
initiator
* the public key for each hop is included in plaintext as hop.pubkey
* ECDH is used to establish a shared secret with each hop
(initiator uses routing node's pubkey (ie, their lightning node id)
and the generated privkey; routing node uses their privkey and the
hop.pubkey value)
* symmetric AES & HMAC keys are derived based on the secret
When you receive a message (say you're hop 5) what you'll get is:
P4 P3 P2 P1 H20 H19 ... H5
where P1 is padding added at node 1, and H19 is the hop structure intended for node 19, etc; all of those in various stages of encryption/decryption.
You then grab the pubkey from H5.pubkey; which is already plaintext, and calculate your shared secret, and generate AES key, HMAC key, IV and padding IV from those. That then lets you run a hmac of everything you've received, excluding the final hmac (so, P4 ... P1 H20 .. H6 + msg + pubkey), and compare that to the hmac.
You then decrypt everything (except maybe the hmac and pubkey, but whatever) using AES in CTR mode with a nonce of IV, giving you:
P4' P3' P2' P1' H20' H19' .. H4' [M5]
M5 lets you know what to do, and if you need to forward it, you generate P5 by encrypting 0000's with AES in CTR mode with a nonce of PAD_IV. You forward:
P5 P4' P3' .. H4'
and you're done.
Question:
- I think this means lightning nodes are identified by the full 512 bit (or 257 bit?) public key used for routing -- (ie, knowing the HASH160 of the pubkey isn't enough, unlike in normal bitcoin pay2pubkey transactions).
- I think you can still use different keys for routing and anchors/commitments so far. (Using the anchor transaction to turn your routing id into a beacon would probably change that though)
Sigh. I've had a go at reimplementing it in python, but I'm stuck trying to reproduce the secret info using pyelliptic (which in turn uses openssl).
...
Ah, it looks like the problem is that libsec256k1 actually goes a step further and runs SHA256(y||x), where "x" is the value I'm getting and y is '\x02' is the y value is even and '\x03' if it's odd. If I try both, one of them turns out right:
Secret1: d9946724c6bd8d5b58bdd2256a0251816a42f9707c794427a410075e4dbb199c Secret2: 105e0c04f0a910d72dcf2683c21903ba08cd8b225e4124afc41bb2341dc40f49
Unfortunately openssl throws away y and just gives us x, so I'm not sure if I can work out the right secret directly. I guess I can run the HMAC twice and pick the value that worked?
Okay, we'll see if we can get any further tomorrow.
Cheers, aj
Quoted text (19 lines)
> > > Ah, it looks like the problem is that libsec256k1 actually goes a step > further and runs SHA256(y||x), where "x" is the value I'm getting and y > is '\x02' is the y value is even and '\x03' if it's odd. If I try both, > one of them turns out right: > > Secret1: d9946724c6bd8d5b58bdd2256a0251816a42f9707c794427a410075e4dbb199c > Secret2: 105e0c04f0a910d72dcf2683c21903ba08cd8b225e4124afc41bb2341dc40f49 > > Unfortunately openssl throws away y and just gives us x, so I'm not sure > if I can work out the right secret directly. I guess I can run the HMAC > twice and pick the value that worked? > > Okay, we'll see if we can get any further tomorrow. > > Cheers, > aj >
My open source library pycoin has a function that will give you Y from X, so you can use that or just pilfer enough code to reproduce it (it's not native, but it's not a very complex operation).
https://github.com/richardkiss/pycoin/blob/master/pycoin/ecdsa/ecdsa.py#L115
Richard
On Sun, Oct 04, 2015 at 02:38:56AM +1000, Anthony Towns wrote:
Quoted text (1 line)
> Sigh. I've had a go at reimplementing it in python, but ...
But nothing! The decoding/forwarding is successfully working in python with pyelliptic. (I'm running the HMAC twice rather than actually working out y)
$ test/test_onion 15 > TEST.OUT $ python test/test_onion.py < TEST.OUT Message to 0 Message to 1 Message to 2 Message to 3 Message to 4 Message to 5 Message to 6 Message to 7 Message to 8 Message to 9 Message to 10 Message to 11 Message to 12 Message to 13 Message to 14 done
I changed test_onion.c to output the generated node keypairs (private key and non-compressed public key) and to dump the first message, like so:
$ grep -v Decrypting TEST.OUT | tail -n3 | cut -c1-300 * Keypair 13: 3f62802944de7ca5894e5759d351adac869580ec17e485f18c0c66f17cc07cbb 047d1b9ad310727493cf5fab067ec9b7476eac30eca03348ddce13f63f73ccce235dfa2807ab608a81628df72aa29d16c284379e8a48e2ddc1b98cfccf07c12a42 * Keypair 14: 22fce466da610b63af62bc83b4692f3affaf271693ac071fb86d11342d8def4f 045319588ea4788b57452821af386aaf25842c96e680fc3a0f16076bd4bf21f1647144bc5ff1e492fcb6c12c1f893a02dc5d12ba85d96d4eaea0faaeded0f452fa * Message: 9c9e39281020fe4e4193ba2c1147753038c88e7ca98f4cbc1e074011fc0f85f15dd7576d9e400779eff31b27872b7762e1844447a92772a0bd495f86389a9ae0eae16f0f8dff246d57156a3a5aa36ebae2eaafd50e3bb7e08fc6b97e435b8e62f715980068f23bcf6c177d50b61507efdaf377fd1de20374ca556ec323acd7533e19031c36945e895be7935ec9d6c181
Eh, I made it a pull request; those are fun right?
https://github.com/ElementsProject/lightning/pull/8
Some notes:
- AES128 just uses the first half of the calculated enckey, iv and pad_iv - I couldn't get pyCrypto's AES128 to work -- I think because I wasn't sure exactly how openssl turns an "IV" into a "counter". I couldn't find docs on what openssl is trying to do here either, but didn't bother looking at the source - since libsecp256k1 already sha256s the ecdh secret; the hmac/enckey/etc end up being sha256'ed twice (with a byte added in between). confused me for a minute.
Cheers, aj
On Sat, Oct 03, 2015 at 11:02:39AM -0700, Richard Kiss wrote:
Quoted text (12 lines)
> > Ah, it looks like the problem is that libsec256k1 actually goes a step > > further and runs SHA256(y||x), where "x" is the value I'm getting and y > > is '\x02' is the y value is even and '\x03' if it's odd. If I try both, > > one of them turns out right: > > > > Unfortunately openssl throws away y and just gives us x, so I'm not sure > > if I can work out the right secret directly. I guess I can run the HMAC > > twice and pick the value that worked? > > My open source library pycoin has a function that will give you Y from X, > so you can use that or just pilfer enough code to reproduce it (it's not > native, but it's not a very complex operation).
Yep, but I'd still need to know whether to set is_even True or False, which is what I'm trying to find out.
And if I want to be able to send onion messages not just forward them, then I need to know. So to this end I've implemented ECDH directly (it's just a single EC_POINT_mul operation afterall). Aside from a bug where I was checking eveness by comparing the last byte to 0 rather than the last bit, all good.
Still, it'd probably be friendlier to alternative implementations for ECDH to only use the x value when creating secrets. I've added a comment to an existing secp256k1 pull request to that effect:
https://github.com/bitcoin/secp256k1/pull/262#issuecomment-145473597
Cheers, aj
On Sun, Oct 04, 2015 at 03:57:17PM +1000, Anthony Towns wrote:
Quoted text (9 lines)
> On Sun, Oct 04, 2015 at 02:38:56AM +1000, Anthony Towns wrote: > > Sigh. I've had a go at reimplementing it in python, but ... > But nothing! The decoding/forwarding is successfully working in python > with pyelliptic. (I'm running the HMAC twice rather than actually working > out y) > $ test/test_onion 15 > TEST.OUT > $ python test/test_onion.py < TEST.OUT > Message to 0 > Message to 1
Okay, and now the python version does creation as well, though I haven't checked if the C version can decode it:
$ ./test/test_onion 3 | python test/test_onion.py Message to 0 Message to 1 Message to 2 done
$ python test/test_onion.py generate 3 | python test/test_onion.py Howzit 0... Howzit 1... Howzit 2... done
$ wc test/test_onion.{c,py} -l
684 test/test_onion.c
329 test/test_onion.py
(though about 1/3 of the python lines are just implementing "determine y given x" and "ecdh with y as well as x", bleh. It also doesn't have the "export-friendly encryption" mode implemented ;)
Quoted text (2 lines)
> Eh, I made it a pull request; those are fun right? > https://github.com/ElementsProject/lightning/pull/8
(This is updated correspondingly)
Cheers, aj
Anthony Towns <aj at erisian.com.au> writes:
Quoted text (7 lines)
> On Fri, Oct 02, 2015 at 03:18:39PM +0930, Rusty Russell wrote: > Question: > > - I think this means lightning nodes are identified by the > full 512 bit (or 257 bit?) public key used for routing -- (ie, > knowing the HASH160 of the pubkey isn't enough, unlike in normal > bitcoin pay2pubkey transactions).
Yes, since you need to encrypt to it, you need the full pubkey.
Quoted text (3 lines)
> - I think you can still use different keys for routing and > anchors/commitments so far. (Using the anchor transaction to turn > your routing id into a beacon would probably change that though)
Yes, that's still TBD.
Quoted text (3 lines)
>From the other email: > - AES128 just uses the first half of the calculated enckey, iv and > pad_iv
True.
Quoted text (3 lines)
> - since libsecp256k1 already sha256s the ecdh secret; the hmac/enckey/etc > end up being sha256'ed twice (with a byte added in between). confused > me for a minute.
I didn't notice that. Perhaps this is somewhere we should optimize?
Cheers,
Rusty.
PS. Message size of 128 bytes is completely made up, as is 20 hops. We
might want to reduce to 96 or 64 bytes.Anthony Towns <aj at erisian.com.au> writes:
Quoted text (8 lines)
> (though about 1/3 of the python lines are just implementing "determine > y given x" and "ecdh with y as well as x", bleh. It also doesn't have > the "export-friendly encryption" mode implemented ;) > >> Eh, I made it a pull request; those are fun right? >> https://github.com/ElementsProject/lightning/pull/8 > > (This is updated correspondingly)
Thanks, merged.
I reworked test_onion and Makefile test to separate generate and decode; we should mix in the python version there too.
See updated onion branch.
Cheers, Rusty.
On Tue, Oct 06, 2015 at 12:19:14PM +1030, Rusty Russell wrote:
Quoted text (4 lines)
> Anthony Towns <aj at erisian.com.au> writes: > >> https://github.com/ElementsProject/lightning/pull/8 > > (This is updated correspondingly) > Thanks, merged.
Nifty.
Quoted text (3 lines)
> I reworked test_onion and Makefile test to separate generate and > decode; > we should mix in the python version there too.
In onion_key.c, I don't see why you're using flip_key() -- that's only needed for the single use onion-msg-keys, aiui?
On Tue, Oct 06, 2015 at 12:18:11PM +1030, Rusty Russell wrote:
Quoted text (8 lines)
> Anthony Towns <aj at erisian.com.au> writes: > > - AES128 just uses the first half of the calculated enckey, iv and > > pad_iv > True. > > - since libsecp256k1 already sha256s the ecdh secret; the hmac/enckey/etc > > end up being sha256'ed twice (with a byte added in between). confused > > me for a minute. > I didn't notice that. Perhaps this is somewhere we should optimize?
You could save a sha call by using both halves of a sha for iv and pad_iv, rather than two separate sha's. Otherwise don't see anything obvious to do? I assume the ECDH step dominates as far as performance goes anyway?
Quoted text (2 lines)
> PS. Message size of 128 bytes is completely made up, as is 20 hops. We > might want to reduce to 96 or 64 bytes.
I figured 20=floor(4096/192) which made sense to me. AFAICS the message *needs* to include fee info and the forwarding address; so that's 4B and 20B-32B respectively (you could use the hash160 here and assume the forwarding node can figure it out).
But maybe some nodes want to do clever things, like allow you to tell it to deliberately delay forwarding for an hour or two, or deduct (different) fees from multiple HTLC's simultaneously. But I guess 64B is probably plenty for that?
Other thing is if you want to redirect an onion routed payment (which we talked about as a possibility originally, as compared to only allowing source-based routing). ie, a HTLC is sent from A to B->C->D->E, but C actually needs to send it to D via X->Y->Z->D rather than doing it directly. AFAICS, in that case you need to add and drop hops, ie:
A->B: - - - - - - - E D C B B->C: b - - - - - - - E D C C->X: c b - - - - - - - E D Z Y X X->Y: x c b - - - - - - - E D Z Y Y->Z: y x c b - - - - - - - E D Z Z->D: c b - - - - - - - E D D->E: d c b - - - - - - - E
ie, Z needs to be able to be told "don't add padding, in fact, drop two blocks worth of padding before forwarding". That presumably just needs a byte or two of data; but it completely messes up the constant-size packets. :-/
Cheers, aj
Quoted text (4 lines)
> On Tue, Oct 06, 2015 at 12:19:14PM +1030, Rusty Russell wrote: > > I reworked test_onion and Makefile test to separate generate and > > decode; > > we should mix in the python version there too.
Okay, branch updated:
https://github.com/ElementsProject/lightning/pull/9
On Tue, Oct 06, 2015 at 03:22:36PM +1000, Anthony Towns wrote:
Quoted text (2 lines)
> In onion_key.c, I don't see why you're using flip_key() -- that's only > needed for the single use onion-msg-keys, aiui?
So I disabled that, and made the 02/03 prefix explicit. Maybe saving a bit/byte somewhere will be worth it; but it seems way premature and just gave me the heebie-jeebies. :)
I also changed the "Message for blah..." to serialize the pubkey before turning it into hex -- aiui you were dumping the internal representation of the point, which would be hard to duplicate compatibly.
I tweaked onion_key to avoid the for loop and cut calls. pyelliptic doesn't like to generate a pubkey given a privkey, so I'm just passing both to it directly.
Running decoding through python is slooooow -- unfortunately most of the time is in loading the interpretor and crypto modules, so only way to improve it would be to run the decodes in a single python process, which I'm not sure is actually desirable?
Anyway, fwiw.
Was there any conclusion on the wiki/rfc/whatever ideas about documenting lightning protocol/technology decisions? That'd be good to try, no?
Cheers, aj
Anthony Towns <aj at erisian.com.au> writes:
Quoted text (16 lines)
>> On Tue, Oct 06, 2015 at 12:19:14PM +1030, Rusty Russell wrote: >> > I reworked test_onion and Makefile test to separate generate and >> > decode; >> > we should mix in the python version there too. > > Okay, branch updated: > > https://github.com/ElementsProject/lightning/pull/9 > > On Tue, Oct 06, 2015 at 03:22:36PM +1000, Anthony Towns wrote: >> In onion_key.c, I don't see why you're using flip_key() -- that's only >> needed for the single use onion-msg-keys, aiui? > > So I disabled that, and made the 02/03 prefix explicit. Maybe saving > a bit/byte somewhere will be worth it; but it seems way premature and > just gave me the heebie-jeebies. :)
Yeah, it made more sense when it was common code. The 32 bytes fits neatly into the onion in a way 33 bytes really doesn't (esp. for AES).
But this only applies for the temporary keys used for the onion, not so much the node keys. Will apply your patches then cleanup.
Quoted text (3 lines)
> I also changed the "Message for blah..." to serialize the pubkey before > turning it into hex -- aiui you were dumping the internal representation > of the point, which would be hard to duplicate compatibly.
Oops, yeah.
Quoted text (3 lines)
> I tweaked onion_key to avoid the for loop and cut calls. pyelliptic > doesn't like to generate a pubkey given a privkey, so I'm just passing > both to it directly.
Sure.
Quoted text (9 lines)
> Running decoding through python is slooooow -- unfortunately most of > the time is in loading the interpretor and crypto modules, so only way > to improve it would be to run the decodes in a single python process, > which I'm not sure is actually desirable? > > Anyway, fwiw. > > Was there any conclusion on the wiki/rfc/whatever ideas about documenting > lightning protocol/technology decisions? That'd be good to try, no?
Hmm, let me try to write an RFC...
Cheers, Rusty.