Research/Bitcoin Dev Archive / lightning-dev list
daemon: encrypted communication (version 3)
10 messages, Oct 23 to 28, 2015. Started by Rusty Russell.
Rusty Russell (6), Pierre (2), Mats Jerratsch, Peter Todd
OK, once I started trying to send data, I refined this a little so we always send protobufs after the initial session key.
Feedback gratefully accepted!
Thanks, Rusty.
Hi Rusty,
Quoted text (5 lines)
> 5) Unknown protobuf fields are handled in the protocol as follows > (including in the initial Authenticate packet): > > 1) Odd numbered fields are optional, and backwards compatible. > 2) Even numbered fields are required; abort if you get one.
I don't get it, what is it about ?
Thanks,
Pierre
A few more remarks:
Quoted text (5 lines)
> 2) ECDH is used to derive a shared secret. From this we generate > the following transmission encoding parameters for each side: > Session AES-128 key: SHA256(shared-secret || my-sessionpubkey || 0) > Session HMAC key: SHA256(shared-secret || my-sessionpubkey || 1) > IV for AES: SHA256(shared-secret || my-sessionpubkey || 2)
I suppose we use only the first 16 bytes for aes_key and aes_iv ?
Quoted text (7 lines)
> 3) All packets from then on are encrypted of form: > /* HMAC, covering totlen and data */ > struct sha256 hmac; > /* Total data transmitted (including this). */ > le64 totlen; > /* Encrypted contents, rounded up to 16 byte boundary. */ > u8 data[];
Looking at your code it seems totlen is actually the size of the unencrypted serialized protobuf message, not the total data transmitted right ? If so, the comment is a bit misleading, and why make totlen include the length of itself since it doesn't define the encrypted message boundaries anyway ? Also, why encode the length on 64 bits rather than 32 bits ?
Cheers,
Pierre
Quoted text (13 lines)
>> 3) All packets from then on are encrypted of form: >> /* HMAC, covering totlen and data */ >> struct sha256 hmac; >> /* Total data transmitted (including this). */ >> le64 totlen; >> /* Encrypted contents, rounded up to 16 byte boundary. */ >> u8 data[]; > Looking at your code it seems totlen is actually the size of the > unencrypted serialized protobuf message, not the total data > transmitted right ? If so, the comment is a bit misleading, and why > make totlen include the length of itself since it doesn't define the > encrypted message boundaries anyway ? > Also, why encode the length on 64 bits rather than 32 bits ?
Actually I think we do not need this field. Initially, the idea was to provide replay protection. You keep track of totlen locally, and compare it with the value the other party sends to you.
However, as we are using AES-CTR, we do not need to do that. We have a dedicated counter in the IV that does keep track of all messages in each direction respectively. If some attacker tries to replay the same message towards us, we are unable to decrypt it, as the IV is not correct (as it is assuming a different counter)
Pierre <pm+lists at acinq.fr> writes:
Quoted text (9 lines)
> Hi Rusty, > >> 5) Unknown protobuf fields are handled in the protocol as follows >> (including in the initial Authenticate packet): >> >> 1) Odd numbered fields are optional, and backwards compatible. >> 2) Even numbered fields are required; abort if you get one. > > I don't get it, what is it about ?
Yes, I really need to write this up in Matsjj's lightning-core docs repository.
Since protobuf fields are explicitly numbered, we can use this to deliberately break backwards compatibility in future after some transition.
For example, if we want to add a "currency identifier" field in HTLC, for non-bitcoin transactions. That would be an even numbered field, since you need to understand it. (There would also need to be some way to indicate you support those, during connection setup or something).
But if we want to add an optional new field, we'd make it odd, and existing implementations could ignore it.
Cheers, Rusty.
Pierre <pm+lists at acinq.fr> writes:
Quoted text (8 lines)
> A few more remarks: > >> 2) ECDH is used to derive a shared secret. From this we generate >> the following transmission encoding parameters for each side: >> Session AES-128 key: SHA256(shared-secret || my-sessionpubkey || 0) >> Session HMAC key: SHA256(shared-secret || my-sessionpubkey || 1) >> IV for AES: SHA256(shared-secret || my-sessionpubkey || 2) > I suppose we use only the first 16 bytes for aes_key and aes_iv ?
Yes, indeed...
Quoted text (10 lines)
>> 3) All packets from then on are encrypted of form: >> /* HMAC, covering totlen and data */ >> struct sha256 hmac; >> /* Total data transmitted (including this). */ >> le64 totlen; >> /* Encrypted contents, rounded up to 16 byte boundary. */ >> u8 data[]; > Looking at your code it seems totlen is actually the size of the > unencrypted serialized protobuf message, not the total data > transmitted right ?
Yeah, bad wording. "data" here doesn't count the header, just the contents.
Quoted text (3 lines)
> If so, the comment is a bit misleading, and why > make totlen include the length of itself since it doesn't define the > encrypted message boundaries anyway ?
We need to know how much padding to ignore.
Quoted text (1 line)
> Also, why encode the length on 64 bits rather than 32 bits ?
So it never wraps.
Cheers, Rusty.
Mats Jerratsch <matsjj at gmail.com> writes:
Quoted text (23 lines)
>>> 3) All packets from then on are encrypted of form: >>> /* HMAC, covering totlen and data */ >>> struct sha256 hmac; >>> /* Total data transmitted (including this). */ >>> le64 totlen; >>> /* Encrypted contents, rounded up to 16 byte boundary. */ >>> u8 data[]; >> Looking at your code it seems totlen is actually the size of the >> unencrypted serialized protobuf message, not the total data >> transmitted right ? If so, the comment is a bit misleading, and why >> make totlen include the length of itself since it doesn't define the >> encrypted message boundaries anyway ? >> Also, why encode the length on 64 bits rather than 32 bits ? > > Actually I think we do not need this field. Initially, the idea was to > provide replay protection. You keep track of totlen locally, and > compare it with the value the other party sends to you. > > However, as we are using AES-CTR, we do not need to do that. We have a > dedicated counter in the IV that does keep track of all messages in > each direction respectively. If some attacker tries to replay the same > message towards us, we are unable to decrypt it, as the IV is not > correct (as it is assuming a different counter)
This is true. However, the protocol actually requires some replay, for robustness when a node crashes (I think the worst case is 5 packets). There's a FIXME in the current code for this case.
It's certainly overkill, but I thought combining counter and length was kinda cute...
Cheers, Rusty.
On Wed, Oct 28, 2015 at 06:11:20AM +1030, Rusty Russell wrote:
Quoted text (25 lines)
> Pierre <pm+lists at acinq.fr> writes: > > Hi Rusty, > > > >> 5) Unknown protobuf fields are handled in the protocol as follows > >> (including in the initial Authenticate packet): > >> > >> 1) Odd numbered fields are optional, and backwards compatible. > >> 2) Even numbered fields are required; abort if you get one. > > > > I don't get it, what is it about ? > > Yes, I really need to write this up in Matsjj's lightning-core docs > repository. > > Since protobuf fields are explicitly numbered, we can use this to > deliberately break backwards compatibility in future after some > transition. > > For example, if we want to add a "currency identifier" field in HTLC, > for non-bitcoin transactions. That would be an even numbered field, > since you need to understand it. (There would also need to be some way > to indicate you support those, during connection setup or something). > > But if we want to add an optional new field, we'd make it odd, and > existing implementations could ignore it.
FWIW an analogous idea is OpenPGP's "critical bit" for packets, which indicates that if the software doesn't understand the packet it should consider the signature to be invalid:
Peter Todd <pete at petertodd.org> writes:
Quoted text (32 lines)
> On Wed, Oct 28, 2015 at 06:11:20AM +1030, Rusty Russell wrote: >> Pierre <pm+lists at acinq.fr> writes: >> > Hi Rusty, >> > >> >> 5) Unknown protobuf fields are handled in the protocol as follows >> >> (including in the initial Authenticate packet): >> >> >> >> 1) Odd numbered fields are optional, and backwards compatible. >> >> 2) Even numbered fields are required; abort if you get one. >> > >> > I don't get it, what is it about ? >> >> Yes, I really need to write this up in Matsjj's lightning-core docs >> repository. >> >> Since protobuf fields are explicitly numbered, we can use this to >> deliberately break backwards compatibility in future after some >> transition. >> >> For example, if we want to add a "currency identifier" field in HTLC, >> for non-bitcoin transactions. That would be an even numbered field, >> since you need to understand it. (There would also need to be some way >> to indicate you support those, during connection setup or something). >> >> But if we want to add an optional new field, we'd make it odd, and >> existing implementations could ignore it. > > FWIW an analogous idea is OpenPGP's "critical bit" for packets, which > indicates that if the software doesn't understand the packet it should > consider the signature to be invalid: > > https://tools.ietf.org/html/rfc4880#section-5.2.3.1
Yes, it's a pretty simple idea. In lightning, you should probably never send an implementation a packet it won't understand (after the initial handshake stage), but it still serves as belt-and-braces check.
eg. if we want to introduce a field with the intent to eventually make it optional, we would add it as *two* optional fields, say 11 and 12. You could set one or the other, not both, but accept either. In a few years' time, implementations would stop using 11.
Cheers, Rusty.
Rusty Russell <rusty at rustcorp.com.au> writes:
Quoted text (2 lines)
> eg. if we want to introduce a field with the intent to eventually make > it optional, we would add it as *two* optional fields, say 11 and 12.
I mean, "eventually make it compulsory"...
Thanks AJ, Rusty.