Noise
A Ruby implementation of the Noise Protocol framework(http://noiseprotocol.org/).
Supported Features:
- DH Functions
- ED25519
- ED448
- Secp256k1
- Secp256k1 is required for Lightning Network, layer-2 protocol for bitcoin. see BOLT #8: Encrypted and Authenticated Transport
- Cipher Algorithm
- ChaCha20-Poly1305
- AES-GCM
- Hash Functions
- SHA256
- SHA512
- BLAKE2s
- BLAKE2b
- BLAKE3
- Pattens
- One-way Patterns(3)
- N, K, X
- Fundamental Interactive Patterns(12)
- NN, KN, NK, KK, NX, KX, XN, IN, XK, IK, XX, IX
- Defferred Interactive Patterns(23)
- NK1, NX1, X1N, X1K, XK1, X1K1, X1X, XX1, X1X1, K1N, K1K, KK1, K1K1,K1X, KX1, K1X1, I1N, I1K, IK1, I1K1, I1X, IX1, I1X1
- One-way Patterns(3)
- Fallback
- PSK
Installation
This gem needs libsodium library. To install libsodium, see https://github.com/jedisct1/libsodium
Add this line to your application's Gemfile:
gem 'noise-ruby'
And then execute:
$ bundle
Or install it yourself as:
$ gem install noise-ruby
If you use Secp256k1, you must install libsecp256k1.
$ git clone https://github.com/bitcoin-core/secp256k1
$ cd secp256k1
$ ./autogen.sh
$ ./configure --enable-module-recovery --enable-experimental --enable-module-ecdh
$ make
$ sudo make install
and, add this line to your Gemfile:
gem 'secp256k1-ruby'
If you use BLAKE3, you must install Rust and Cargo. And add this line to your Gemfile:
gem 'blake3'
Usage
Followings shows handshake protocol with "Noise_NN_25519_ChaChaPoly_BLAKE2b"
Handshake
initiator
initiator = Noise::Connection::Initiator.new("Noise_NN_25519_ChaChaPoly_BLAKE2b")
initiator.prologue = "test" # => "test"
initiator.start_handshake # => true
cipher = initiator.("") # => "\xB6\xF7gmxi\xAB\xBCY|t\xF0\x9D\x01A\ad\x92\xBBvp\x80ZNU\f=\x83\x81^\xFD\x15"
then initiator sends cipher to the responder.
responder
The responder receives cipher from the initiator.
The responder responds messages to the initiator.
responder = Noise::Connection::Responder.new("Noise_NN_25519_ChaChaPoly_BLAKE2b")
responder.prologue = "test" # => "test"
responder.start_handshake # => true
plain = responder.(cipher) # => ""
cipher = responder.("") # => "\v\xD9\x97'\xC0\xB1\xC9\xFFD\x8C\x7F\x18L\xB0\xF2\x14\xB0\x11\xC0\x90\xAAZ\xE1\x03\x17z)\xB81/5L\x16\xE3\xD1\xBE<{\xB8\xBB\xD6\xF1\x00\x10]\x99=\xD7"
initiator
plain = initiator.(cipher) # => ""
Transport (after handshake finished)
Send transport message
cipher = initiator.encrypt("Hello, World!") # => "\xDA\xC7\xD7as\v\xFA\xCC,\xB3\xC7\xD0/xL\xE8I,\xD9\n\xEExh\x8F\xFA\xD6\x01\x99W"
Receive transport message
plain = responder.decrypt(cipher) # => "Hello, World!"
Out-of-order transport messages
Each party counts the transport messages of each direction with a nonce. If the transport layer can deliver messages out of order or lose them, it has to carry the nonce of each message, and the receiver sets the nonce before decrypting. Restore the previous value if the message fails to authenticate, so that the following messages are still decryptable.
initiator.encryption_nonce # => 0
responder.decryption_nonce = 2 # decrypt the message numbered 2 next
plain = responder.decrypt(cipher)
Rekey
Rekeying replaces the key of one direction with REKEY(k), so that a key compromised later cannot
decrypt the messages that came before it. The nonce keeps counting. Both parties must rekey the
matching direction at the same point of the message stream; when that happens is up to the
application protocol.
initiator.rekey_encryption
responder.rekey_decryption
Development
After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/Yamaguchi/noise. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
Code of Conduct
Everyone interacting in the Noise project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.