You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
3.1 KiB

  1. # Secure P2P
  2. The Tendermint p2p protocol uses an authenticated encryption scheme
  3. based on the [Station-to-Station
  4. Protocol](https://en.wikipedia.org/wiki/Station-to-Station_protocol).
  5. The implementation uses
  6. [golang's](https://godoc.org/golang.org/x/crypto/nacl/box) [nacl
  7. box](http://nacl.cr.yp.to/box.html) for the actual authenticated
  8. encryption algorithm.
  9. Each peer generates an ED25519 key-pair to use as a persistent
  10. (long-term) id.
  11. When two peers establish a TCP connection, they first each generate an
  12. ephemeral ED25519 key-pair to use for this session, and send each other
  13. their respective ephemeral public keys. This happens in the clear.
  14. They then each compute the shared secret. The shared secret is the
  15. multiplication of the peer's ephemeral private key by the other peer's
  16. ephemeral public key. The result is the same for both peers by the magic
  17. of [elliptic
  18. curves](https://en.wikipedia.org/wiki/Elliptic_curve_cryptography). The
  19. shared secret is used as the symmetric key for the encryption algorithm.
  20. The two ephemeral public keys are sorted to establish a canonical order.
  21. Then a 24-byte nonce is generated by concatenating the public keys and
  22. hashing them with Ripemd160. Note Ripemd160 produces 20byte hashes, so
  23. the nonce ends with four 0s.
  24. The nonce is used to seed the encryption - it is critical that the same
  25. nonce never be used twice with the same private key. For convenience,
  26. the last bit of the nonce is flipped, giving us two nonces: one for
  27. encrypting our own messages, one for decrypting our peer's. Which ever
  28. peer has the higher public key uses the "bit-flipped" nonce for
  29. encryption.
  30. Now, a challenge is generated by concatenating the ephemeral public keys
  31. and taking the SHA256 hash.
  32. Each peer signs the challenge with their persistent private key, and
  33. sends the other peer an AuthSigMsg, containing their persistent public
  34. key and the signature. On receiving an AuthSigMsg, the peer verifies the
  35. signature.
  36. The peers are now authenticated.
  37. All future communications can now be encrypted using the shared secret
  38. and the generated nonces, where each nonce is incremented by one each
  39. time it is used. The communications maintain Perfect Forward Secrecy, as
  40. the persistent key pair was not used for generating secrets - only for
  41. authenticating.
  42. ## Caveat
  43. This system is still vulnerable to a Man-In-The-Middle attack if the
  44. persistent public key of the remote node is not known in advance. The
  45. only way to mitigate this is with a public key authentication system,
  46. such as the Web-of-Trust or Certificate Authorities. In our case, we can
  47. use the blockchain itself as a certificate authority to ensure that we
  48. are connected to at least one validator.
  49. ## Config
  50. Authenticated encryption is enabled by default.
  51. ## Additional Reading
  52. - [Implementation](https://github.com/tendermint/tendermint/blob/64bae01d007b5bee0d0827ab53259ffd5910b4e6/p2p/conn/secret_connection.go#L47)
  53. - [Original STS paper by Whitfield Diffie, Paul C. van Oorschot and
  54. Michael J.
  55. Wiener](http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.216.6107&rep=rep1&type=pdf)
  56. - [Further work on secret
  57. handshakes](https://dominictarr.github.io/secret-handshake-paper/shs.pdf)