noise-ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -0
  3. data/.rspec +2 -0
  4. data/.rubocop.yml +15 -0
  5. data/.ruby-version +1 -0
  6. data/.travis.yml +5 -0
  7. data/CODE_OF_CONDUCT.md +74 -0
  8. data/Gemfile +6 -0
  9. data/README.md +39 -0
  10. data/Rakefile +6 -0
  11. data/bin/console +14 -0
  12. data/bin/setup +8 -0
  13. data/lib/noise.rb +19 -0
  14. data/lib/noise/connection.rb +96 -0
  15. data/lib/noise/exceptions.rb +10 -0
  16. data/lib/noise/exceptions/max_nonce_error.rb +8 -0
  17. data/lib/noise/exceptions/noise_handshake_error.rb +8 -0
  18. data/lib/noise/exceptions/noise_validation_error.rb +8 -0
  19. data/lib/noise/exceptions/protocol_name_error.rb +8 -0
  20. data/lib/noise/functions.rb +9 -0
  21. data/lib/noise/functions/cipher.rb +10 -0
  22. data/lib/noise/functions/cipher/aes_gcm.rb +21 -0
  23. data/lib/noise/functions/cipher/cha_cha_poly.rb +23 -0
  24. data/lib/noise/functions/dh.rb +11 -0
  25. data/lib/noise/functions/dh/dh25519.rb +34 -0
  26. data/lib/noise/functions/dh/dh448.rb +25 -0
  27. data/lib/noise/functions/dh/secp256k1.rb +28 -0
  28. data/lib/noise/functions/hash.rb +32 -0
  29. data/lib/noise/functions/hash/blake2b.rb +23 -0
  30. data/lib/noise/functions/hash/blake2s.rb +23 -0
  31. data/lib/noise/functions/hash/sha256.rb +23 -0
  32. data/lib/noise/functions/hash/sha512.rb +23 -0
  33. data/lib/noise/pattern.rb +223 -0
  34. data/lib/noise/protocol.rb +107 -0
  35. data/lib/noise/state.rb +9 -0
  36. data/lib/noise/state/cipher_state.rb +54 -0
  37. data/lib/noise/state/handshake_state.rb +141 -0
  38. data/lib/noise/state/symmetric_state.rb +86 -0
  39. data/lib/noise/utils/hash.rb +9 -0
  40. data/lib/noise/utils/string.rb +10 -0
  41. data/lib/noise/version.rb +5 -0
  42. data/noise.gemspec +29 -0
  43. metadata +168 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b0c3d02c47e652a285fbe31fcc7b5ff4c3d7cc05
4
+ data.tar.gz: '0930939cdcee4d1215b35eb89351f12f1faf6538'
5
+ SHA512:
6
+ metadata.gz: e3a80f068ff0e387d2bca7cb0df4ecd19edb2728a6ee6d4c5b54a5487f1a7f0cc470d2e8d0ec173257d3efb25c1f121659b3cf61029dfba072faa39e4e5f6d5b
7
+ data.tar.gz: 1359c45f4a39ea567809dff276c6e91da4711b4d45a89909a6f7682e627986ab37f2ab14cba7d89c96fe71752e7db2c045ab9a6e3315d01b749a0304037d95e7
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,15 @@
1
+ Metrics/BlockLength:
2
+ Enabled: false
3
+
4
+ Metrics/LineLength:
5
+ Max: 120
6
+
7
+ Style/Documentation:
8
+ Enabled: false
9
+
10
+ Style/LambdaCall:
11
+ Enabled: false
12
+
13
+ AllCops:
14
+ TargetRubyVersion: 2.4.1
15
+
@@ -0,0 +1 @@
1
+ 2.4.1
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.15.3
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at h_yamaguchi@haw.co.jp. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in noise.gemspec
6
+ gemspec
@@ -0,0 +1,39 @@
1
+ # Noise
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/noise`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'noise'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install noise
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ 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](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/noise. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## Code of Conduct
38
+
39
+ Everyone interacting in the Noise project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/noise/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'noise'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require 'pry'
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'noise/version'
4
+
5
+ require 'ecdsa'
6
+ require 'rbnacl'
7
+ require 'securerandom'
8
+
9
+ require 'noise/utils/hash'
10
+ require 'noise/utils/string'
11
+
12
+ module Noise
13
+ autoload :Connection, 'noise/connection'
14
+ autoload :Protocol, 'noise/protocol'
15
+ autoload :Pattern, 'noise/pattern'
16
+ autoload :Exceptions, 'noise/exceptions'
17
+ autoload :Functions, 'noise/functions'
18
+ autoload :State, 'noise/state'
19
+ end
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Noise
4
+ module KeyPair
5
+ STATIC = 's'
6
+ EPHEMERAL = 'e'
7
+ REMOTE_STATIC = 'rs'
8
+ REMOTE_EPHEMERAL = 're'
9
+ end
10
+ class Connection
11
+ module Status
12
+ STATIC = 1
13
+ REMOTE_STATIC = 2
14
+ EPHEMERAL = 3
15
+ REMOTE_EPHEMERAL = 4
16
+ end
17
+
18
+ attr_accessor :protocol, :handshake_started, :handshake_finished, :fn
19
+
20
+ def initialize(name)
21
+ @protocol = Protocol.create(name)
22
+ @handshake_started = false
23
+ @handshake_finished = false
24
+ @fn = nil
25
+ @write_message_proc = lambda {|payload| write_message(payload)}
26
+ @read_message_proc = lambda {|payload| read_message(payload)}
27
+ end
28
+
29
+ def prologue=(prologue)
30
+ @protocol.prologue = prologue
31
+ end
32
+
33
+ def set_as_initiator!
34
+ @protocol.initiator = true
35
+ @fn = @write_message_proc
36
+ end
37
+
38
+ def set_as_responder!
39
+ @protocol.initiator = false
40
+ @fn = @read_message_proc
41
+ end
42
+
43
+ def set_keypair_from_private(keypair, private_key)
44
+ @protocol.keypairs[keypair.to_sym] = @protocol.dh_fn.class.from_private(private_key)
45
+ end
46
+
47
+ def set_keypair_from_public(keypair, public_key)
48
+ @protocol.keypairs[keypair.to_sym] = @protocol.dh_fn.class.from_public(public_key)
49
+ end
50
+
51
+ def start_handshake
52
+ @protocol.validate
53
+ @protocol.initialise_handshake_state
54
+ @handshake_started = true
55
+ end
56
+
57
+ def write_message(payload = '')
58
+ # Call NoiseConnection.start_handshake first
59
+ raise Noise::Exceptions::NoiseHandshakeError unless @handshake_started
60
+ raise Noise::Exceptions::NoiseHandshakeError if @fn != @write_message_proc
61
+ # Handshake finished. NoiseConnection.encrypt should be used now
62
+ raise Noise::Exceptions::NoiseHandshakeError if @handshake_finished
63
+ @fn = @read_message_proc
64
+ buffer = +''
65
+ result = @protocol.handshake_state.write_message(payload, buffer)
66
+ @handshake_finished = true if result
67
+ buffer
68
+ end
69
+
70
+ def read_message(data)
71
+ # Call NoiseConnection.start_handshake first
72
+ raise Noise::Exceptions::NoiseHandshakeError unless @handshake_started
73
+ raise Noise::Exceptions::NoiseHandshakeError if @fn != @read_message_proc
74
+ # Handshake finished. NoiseConnection.encrypt should be used now
75
+ raise Noise::Exceptions::NoiseHandshakeError if @handshake_finished
76
+
77
+ @fn = @write_message_proc
78
+ buffer = +''
79
+ result = @protocol.handshake_state.read_message(data, buffer)
80
+ @handshake_finished = true if result
81
+ buffer
82
+ end
83
+
84
+ def encrypt(data)
85
+ raise Noise::Exceptions::NoiseHandshakeError unless @handshake_finished
86
+ # raise Noise::Exceptions::NoiseInvalidMessage
87
+ @protocol.cipher_state_encrypt.encrypt_with_ad('', data)
88
+ end
89
+
90
+ def decrypt(data)
91
+ raise Noise::Exceptions::NoiseHandshakeError unless @handshake_finished
92
+ # raise Noise::Exceptions::NoiseInvalidMessage
93
+ @protocol.cipher_state_decrypt.decrypt_with_ad('', data)
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Noise
4
+ module Exceptions
5
+ autoload :MaxNonceError, 'noise/exceptions/max_nonce_error'
6
+ autoload :ProtocolNameError, 'noise/exceptions/protocol_name_error'
7
+ autoload :NoiseHandshakeError, 'noise/exceptions/noise_handshake_error'
8
+ autoload :NoiseValidationError, 'noise/exceptions/noise_validation_error'
9
+ end
10
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Noise
4
+ module Exceptions
5
+ class MaxNonceError < RuntimeError
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Noise
4
+ module Exceptions
5
+ class NoiseHandshakeError < RuntimeError
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Noise
4
+ module Exceptions
5
+ class NoiseValidationError < RuntimeError
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Noise
4
+ module Exceptions
5
+ class ProtocolNameError < RuntimeError
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Noise
4
+ module Functions
5
+ autoload :Cipher, 'noise/functions/cipher'
6
+ autoload :DH, 'noise/functions/dh'
7
+ autoload :Hash, 'noise/functions/hash'
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Noise
4
+ module Functions
5
+ module Cipher
6
+ autoload :AesGcm, 'noise/functions/cipher/aes_gcm'
7
+ autoload :ChaChaPoly, 'noise/functions/cipher/cha_cha_poly'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Noise
4
+ module Functions
5
+ module Cipher
6
+ class AesGcm
7
+ def encrypt(k, n, ad, plaintext)
8
+ throw NotImplementedError
9
+ end
10
+
11
+ def decrypt(k, n, ad, ciphertext)
12
+ throw NotImplementedError
13
+ end
14
+
15
+ def nonce_to_bytes(n)
16
+ "\00" * 4 + sprintf('%16x', n).htb
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Noise
4
+ module Functions
5
+ module Cipher
6
+ class ChaChaPoly
7
+ def encrypt(k, n, ad, plaintext)
8
+ @cipher = RbNaCl::AEAD::ChaCha20Poly1305IETF.new(String.new(k).force_encoding('ASCII-8BIT'))
9
+ @cipher.encrypt(nonce_to_bytes(n), plaintext, ad)
10
+ end
11
+
12
+ def decrypt(k, n, ad, ciphertext)
13
+ @cipher = RbNaCl::AEAD::ChaCha20Poly1305IETF.new(String.new(k).force_encoding('ASCII-8BIT'))
14
+ @cipher.decrypt(nonce_to_bytes(n), ciphertext, ad)
15
+ end
16
+
17
+ def nonce_to_bytes(n)
18
+ "\00" * 4 + sprintf('%16x', n).htb.reverse
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end