CryptoPAn 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a9678848491807457dd6e54cae840a125c0b74ff
4
+ data.tar.gz: 779499268696a5b96e04f9a64aabfc9246019ae4
5
+ SHA512:
6
+ metadata.gz: 6cc78646ad88c3a60c6dc49c6c7800b88d2c9f92731b9e58bd1b3c287706d264375278e63672763e9f31970748b626a244938f5d4eb0d823de50e325680ceb2b
7
+ data.tar.gz: 17b23374d9f6664cd390023bcbd8a15d39c3fc7fffc6a1b24fd65ed6d0473cfa23f7fd231064f50d1a8313e4e897cdb4d77e5c8c9c604b222406cbd83ada3f77
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cryptopan.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Peter Wood
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # Cryptopan
2
+
3
+ [![Build Status](https://travis-ci.org/pwood/cryptopan.svg?branch=master)](https://travis-ci.org/pwood/cryptopan)
4
+
5
+ This is a ruby implementation of the Crypto-PAn function providing prefix
6
+ consistency while anonymising IP addresses. This program is based on the paper
7
+ "Prefix-Preserving IP Address Anonymization: Measurement-based
8
+ Security Evaluation and a New Cryptography-based Scheme" written by
9
+ Jun Xu, Jinliang Fan, Mostafa H. Ammar, and Sue B. Moon. The detailed
10
+ explanation can be found in [Xu2002].
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'cryptopan'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install cryptopan
27
+
28
+ ## Usage
29
+
30
+ *Note* Currently only supports IPv4 addresses.
31
+
32
+ ```
33
+ 2.0.0-p598 :001 > key = SecureRandom.random_bytes(32).bytes
34
+ => [27, 8, 236, 31, 47, 181, 230, 41, 198, 24, 254, 3, 151, 151, 200, 75, 255, 197, 170, 3, 235, 236, 200, 41, 12, 10, 206, 127, 203, 162, 176, 206]
35
+ 2.0.0-p598 :002 > c = CryptoPAn.new(key)
36
+ => #<CryptoPAn:0x0000000349b350 @key="\e\b\xEC\x1F/\xB5\xE6)\xC6\x18\xFE\x03\x97\x97\xC8K", @pad=[214, 202, 47, 81, 14, 48, 182, 196, 30, 19, 88, 145, 215, 2, 249, 52]>
37
+ 2.0.0-p598 :003 > c.anonymise("8.8.8.8")
38
+ => "50.54.20.240"
39
+ 2.0.0-p598 :004 > c.anonymise("8.8.4.4")
40
+ => "50.54.28.11"
41
+ ```
42
+
43
+ ## Development
44
+
45
+ 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.
46
+
47
+ 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).
48
+
49
+ ## Contributing
50
+
51
+ Bug reports and pull requests are welcome on GitHub at https://github.com/pwood/cryptopan.
52
+
53
+
54
+ ## References
55
+
56
+ [Xu2002] Jun Xu, Jinliang Fan, Mostafa H. Ammar, and Sue B. Moon,
57
+ "Prefix-Preserving IP Address Anonymization: Measurement-based
58
+ Security Evaluation and a New Cryptography-based Scheme", in
59
+ Proceedings of the IEEE International Conference on Network Protocols,
60
+ Paris, 2002.
61
+
62
+ ## License
63
+
64
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
65
+
66
+ ## Contributors
67
+
68
+ - Keiichi SHIMA, (https://github.com/keiichishima) - README.md and some test data shamelessly appropriated.
data/Rakefile ADDED
@@ -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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cryptopan"
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
data/bin/setup ADDED
@@ -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
data/cryptopan.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cryptopan/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "CryptoPAn"
8
+ spec.version = CRYPTOPAN_VERSION
9
+ spec.authors = ["Peter Wood"]
10
+ spec.email = ["peter@alastria.net"]
11
+
12
+ spec.summary = 'Ruby implementation of Crypto-PAn IP Anonymisation.'
13
+ spec.description = 'Ruby implementation of Crypto-PAn IP Anonymisation, providing consistency accross prefixes.'
14
+ spec.homepage = 'http://github.com/pwood/crypto-pan'
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_runtime_dependency "netaddr", "~> 1.5"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.11"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ end
data/lib/cryptopan.rb ADDED
@@ -0,0 +1,70 @@
1
+ require 'netaddr'
2
+
3
+ class CryptoPAn
4
+ def initialize(key)
5
+ if key.is_a? Array
6
+ material = key.pack("c*")
7
+ else
8
+ material = key
9
+ end
10
+
11
+ raise "Key material is not 32 bytes." if material.length != 32
12
+
13
+ @key = material[0..15]
14
+ unencrypted_pad = material[16..31]
15
+
16
+ @pad = encrypt(unencrypted_pad)
17
+ end
18
+
19
+ def anonymise(ip)
20
+ long_ip = NetAddr.ip_to_i ip
21
+
22
+ encrypt_input = @pad.dup
23
+
24
+ long_pad = @pad[0].to_i << 24
25
+ long_pad += @pad[1].to_i << 16
26
+ long_pad += @pad[2].to_i << 8
27
+ long_pad += @pad[3].to_i
28
+
29
+ result = 0
30
+
31
+ 32.times do |i|
32
+ first4bytes_input = long_pad
33
+
34
+ if i > 0
35
+ first4bytes_input = (long_ip >> (32-i)) << (32-i) | ((long_pad << i) & 0xffffffff) >> i
36
+ end
37
+
38
+ encrypt_input[0] = first4bytes_input >> 24
39
+ encrypt_input[1] = (first4bytes_input << 8) >> 24
40
+ encrypt_input[2] = (first4bytes_input << 16) >> 24
41
+ encrypt_input[3] = (first4bytes_input << 24) >> 24
42
+
43
+ encrypt_output = encrypt(encrypt_input.pack("c*"))[0]
44
+
45
+ result |= (((encrypt_output & 0xff) >> 7) << (31-i))
46
+ end
47
+
48
+ value = result ^ long_ip
49
+
50
+ output_value = []
51
+ output_value[0] = value >> 24 & 0xff
52
+ output_value[1] = value >> 16 & 0xff
53
+ output_value[2] = value >> 8 & 0xff
54
+ output_value[3] = value & 0xff
55
+
56
+ output_value.join(".")
57
+ end
58
+
59
+ private
60
+
61
+ def encrypt(value)
62
+ cipher = OpenSSL::Cipher.new('AES-128-ECB')
63
+ cipher.padding = 0
64
+ cipher.encrypt
65
+
66
+ cipher.key = @key
67
+
68
+ (cipher.update(value) + cipher.final).bytes
69
+ end
70
+ end
@@ -0,0 +1 @@
1
+ CRYPTOPAN_VERSION = "0.1.0"
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: CryptoPAn
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Peter Wood
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-09-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: netaddr
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: Ruby implementation of Crypto-PAn IP Anonymisation, providing consistency
70
+ accross prefixes.
71
+ email:
72
+ - peter@alastria.net
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .gitignore
78
+ - .rspec
79
+ - .travis.yml
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - cryptopan.gemspec
87
+ - lib/cryptopan.rb
88
+ - lib/cryptopan/version.rb
89
+ homepage: http://github.com/pwood/crypto-pan
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.4.5
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Ruby implementation of Crypto-PAn IP Anonymisation.
113
+ test_files: []