ccipher_box 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
+ SHA256:
3
+ metadata.gz: 588c5b4cfb98d61250968d893559547a833211609fd120002f57b65ee49c6a56
4
+ data.tar.gz: ca46003f1b54cea793e1784566685ba351e905498b0f05b4f7cfdf352ba18c2e
5
+ SHA512:
6
+ metadata.gz: ab0df6ac4faabafc663bd9bc10acb8d28c8dea10995779c24996e8d9bf09db2d1e3f5034f7955b2e97d6ff19e48f2bfdf67cb19277907295ff92cdeb1fd1c3cc
7
+ data.tar.gz: de43f592c27a000d9a3a16a12acd0c3c1b4fac29f75531c5211a77605c1603c4f7605367368388860a558553293475883d1f9e106b11d52d4f20820aa28b05ef
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in ccipher_box.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ #gem 'teLogger', git: 'teLogger', branch: "main"
13
+
14
+ #gem 'ccrypto', git: 'ccrypto', branch: 'main'
15
+
16
+ require 'toolrack'
17
+ if TR::RTUtils.on_java?
18
+ #gem 'ccrypto-java', git: 'ccrypto-java', branch: 'main'
19
+ gem 'ccrypto-java'
20
+ else
21
+ #gem 'ccrypto-ruby', git: 'ccrypto-ruby', branch: 'main'
22
+ gem 'ccrypto-ruby'
23
+ end
24
+
25
+ #gem 'ccipher_factory', git: 'ccipher_factory', branch: 'master'
26
+
27
+ #gem 'binenc', git: 'binenc', branch: 'master'
28
+ if TR::RTUtils.on_java?
29
+ #gem 'binenc-java', git: 'binenc-java', branch: 'master'
30
+ gem 'binenc-java'
31
+ else
32
+ #gem 'binenc-ruby', git: 'binenc-ruby', branch: 'master'
33
+ gem 'binenc-ruby'
34
+ end
35
+
data/Gemfile.lock-java ADDED
@@ -0,0 +1,89 @@
1
+ GIT
2
+ remote: binenc
3
+ revision: 632483d860fbd853915276043b42354ebd2d9aea
4
+ branch: master
5
+ specs:
6
+ binenc (0.1.0)
7
+ teLogger
8
+ toolrack
9
+
10
+ GIT
11
+ remote: binenc-java
12
+ revision: 3bfd9d07b7e440bace27082731a5f4361eb36f32
13
+ branch: master
14
+ specs:
15
+ binenc-java (0.1.0)
16
+ teLogger
17
+ toolrack
18
+
19
+ GIT
20
+ remote: ccipher_factory
21
+ revision: a7a6d8f59204ca028c60f02c1989c1020cae2ae6
22
+ branch: master
23
+ specs:
24
+ ccipher_factory (0.1.0)
25
+ tlogger
26
+
27
+ GIT
28
+ remote: ccrypto
29
+ revision: 00f0b61a565e75d297c42dc20e01aac167df2a1a
30
+ branch: main
31
+ specs:
32
+ ccrypto (0.1.0)
33
+ teLogger
34
+ toolrack
35
+
36
+ GIT
37
+ remote: ccrypto-java
38
+ revision: 36893ec953006080052ebe318ef7ae0551be30cd
39
+ branch: main
40
+ specs:
41
+ ccrypto-java (0.1.0)
42
+
43
+ PATH
44
+ remote: .
45
+ specs:
46
+ ccipher_box (0.1.0)
47
+ teLogger
48
+ toolrack
49
+
50
+ GEM
51
+ remote: https://rubygems.org/
52
+ specs:
53
+ base58 (0.2.3)
54
+ diff-lcs (1.5.0)
55
+ rake (13.0.6)
56
+ rspec (3.11.0)
57
+ rspec-core (~> 3.11.0)
58
+ rspec-expectations (~> 3.11.0)
59
+ rspec-mocks (~> 3.11.0)
60
+ rspec-core (3.11.0)
61
+ rspec-support (~> 3.11.0)
62
+ rspec-expectations (3.11.0)
63
+ diff-lcs (>= 1.2.0, < 2.0)
64
+ rspec-support (~> 3.11.0)
65
+ rspec-mocks (3.11.1)
66
+ diff-lcs (>= 1.2.0, < 2.0)
67
+ rspec-support (~> 3.11.0)
68
+ rspec-support (3.11.0)
69
+ teLogger (0.1.0)
70
+ tlogger (0.26.3)
71
+ toolrack (0.18.6)
72
+ base58
73
+ tlogger
74
+
75
+ PLATFORMS
76
+ universal-java-11
77
+
78
+ DEPENDENCIES
79
+ binenc!
80
+ binenc-java!
81
+ ccipher_box!
82
+ ccipher_factory!
83
+ ccrypto!
84
+ ccrypto-java!
85
+ rake (~> 13.0)
86
+ rspec (~> 3.0)
87
+
88
+ BUNDLED WITH
89
+ 2.3.6
data/Gemfile.lock-ruby ADDED
@@ -0,0 +1,91 @@
1
+ GIT
2
+ remote: binenc
3
+ revision: 632483d860fbd853915276043b42354ebd2d9aea
4
+ branch: master
5
+ specs:
6
+ binenc (0.1.0)
7
+ teLogger
8
+ toolrack
9
+
10
+ GIT
11
+ remote: binenc-ruby
12
+ revision: 1c7ae0ec51c41d7258014f72dafbb9d8f5d90b89
13
+ branch: master
14
+ specs:
15
+ binenc-ruby (0.1.0)
16
+ teLogger
17
+ toolrack
18
+
19
+ GIT
20
+ remote: ccipher_factory
21
+ revision: a7a6d8f59204ca028c60f02c1989c1020cae2ae6
22
+ branch: master
23
+ specs:
24
+ ccipher_factory (0.1.0)
25
+ tlogger
26
+
27
+ GIT
28
+ remote: ccrypto
29
+ revision: 00f0b61a565e75d297c42dc20e01aac167df2a1a
30
+ branch: main
31
+ specs:
32
+ ccrypto (0.1.0)
33
+ teLogger
34
+ toolrack
35
+
36
+ GIT
37
+ remote: ccrypto-ruby
38
+ revision: ddd3d62aef5d51c698f295486d4f69d686cf7d48
39
+ branch: main
40
+ specs:
41
+ ccrypto-ruby (0.1.0)
42
+ teLogger
43
+ toolrack
44
+
45
+ PATH
46
+ remote: .
47
+ specs:
48
+ ccipher_box (0.1.0)
49
+ teLogger
50
+ toolrack
51
+
52
+ GEM
53
+ remote: https://rubygems.org/
54
+ specs:
55
+ base58 (0.2.3)
56
+ diff-lcs (1.5.0)
57
+ rake (13.0.6)
58
+ rspec (3.11.0)
59
+ rspec-core (~> 3.11.0)
60
+ rspec-expectations (~> 3.11.0)
61
+ rspec-mocks (~> 3.11.0)
62
+ rspec-core (3.11.0)
63
+ rspec-support (~> 3.11.0)
64
+ rspec-expectations (3.11.0)
65
+ diff-lcs (>= 1.2.0, < 2.0)
66
+ rspec-support (~> 3.11.0)
67
+ rspec-mocks (3.11.1)
68
+ diff-lcs (>= 1.2.0, < 2.0)
69
+ rspec-support (~> 3.11.0)
70
+ rspec-support (3.11.0)
71
+ teLogger (0.1.0)
72
+ tlogger (0.26.3)
73
+ toolrack (0.18.6)
74
+ base58
75
+ tlogger
76
+
77
+ PLATFORMS
78
+ x86_64-linux
79
+
80
+ DEPENDENCIES
81
+ binenc!
82
+ binenc-ruby!
83
+ ccipher_box!
84
+ ccipher_factory!
85
+ ccrypto!
86
+ ccrypto-ruby!
87
+ rake (~> 13.0)
88
+ rspec (~> 3.0)
89
+
90
+ BUNDLED WITH
91
+ 2.2.28
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # CcipherBox
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/ccipher_box`. 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 'ccipher_box'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install ccipher_box
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 the created tag, 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]/ccipher_box.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ require 'devops_assist'
7
+
8
+ RSpec::Core::RakeTask.new(:spec)
9
+
10
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "ccipher_box"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
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
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/ccipher_box/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "ccipher_box"
7
+ spec.version = CcipherBox::VERSION
8
+ spec.authors = ["Ian"]
9
+ spec.email = ["cameronian0@protonmail.com"]
10
+
11
+ spec.summary = ""
12
+ spec.description = ""
13
+ spec.homepage = ""
14
+ spec.required_ruby_version = ">= 2.4.0"
15
+
16
+ #spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
17
+
18
+ #spec.metadata["homepage_uri"] = spec.homepage
19
+ #spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
20
+ #spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
25
+ `git ls-files -z`.split("\x0").reject do |f|
26
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
27
+ end
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_dependency "toolrack"
34
+ spec.add_dependency "teLogger"
35
+
36
+ spec.add_dependency 'ccrypto'
37
+ spec.add_dependency 'binenc'
38
+ spec.add_dependency 'ccipher_factory'
39
+
40
+ spec.add_development_dependency 'devops_assist'
41
+
42
+ # Uncomment to register a new dependency of your gem
43
+ # spec.add_dependency "example-gem", "~> 1.0"
44
+
45
+ # For more information and examples about making a new gem, checkout our
46
+ # guide at: https://bundler.io/guides/creating_gem.html
47
+ end
@@ -0,0 +1,167 @@
1
+
2
+ require 'singleton'
3
+ require_relative 'binenc_constant'
4
+
5
+ module CcipherBox
6
+ class BinStruct
7
+ include Singleton
8
+
9
+ def struct(key, ver = "1.0")
10
+ st = structure(ver)[key]
11
+ st.clone if not st.nil?
12
+ end
13
+
14
+ def struct_from_bin(bin)
15
+ type, ver = struct_fields_from_bin(bin, 0, 1)
16
+ c = CBTag.value_constant(type)
17
+ st = struct(c, translate_version(ver))
18
+ st.from_bin(bin) if not st.nil?
19
+ end
20
+
21
+ def struct_fields_from_bin(bin, *args)
22
+ Binenc::EngineFactory.instance(:bin_struct).value_from_bin_struct(bin, *args)
23
+ end
24
+
25
+ def find_struct(buf, &block)
26
+
27
+ cpos = buf.pos
28
+
29
+ begin
30
+
31
+ #len = find_asn1_length(buf.string)
32
+ len = Ccrypto::ASN1.engine.asn1_length(buf.bytes)
33
+ #logger.debug "Found meta length : #{len}" if not logger.nil?
34
+ raise InsufficientData if len == 0
35
+
36
+ buf.rewind
37
+ meta = buf.read(len)
38
+
39
+ if block
40
+ block.call(meta, buf.read(cpos-len))
41
+ else
42
+ meta
43
+ end
44
+
45
+ #rescue OpenSSL::ASN1::ASN1Error => ex
46
+ rescue Ccrypto::ASN1EngineException => ex
47
+ logger.error ex
48
+ buf.seek(cpos)
49
+ raise InsufficientData
50
+ end
51
+
52
+ end
53
+
54
+ private
55
+ def logger
56
+ if @logger.nil?
57
+ @logger = TeLogger::Tlogger.new
58
+ @logger.tag = :binstruct
59
+ end
60
+ @logger
61
+ end
62
+
63
+ def structure(ver = "1.0")
64
+
65
+ if @struct.nil?
66
+ @struct = { }
67
+
68
+ @struct["1.0"] = {
69
+
70
+ keybox: Binenc::EngineFactory.instance(:bin_struct).define do
71
+ oid :oid, CBTag.constant_value(:keybox)
72
+ int :version, 0x0100
73
+ bin :kdfConfig
74
+ end,
75
+
76
+
77
+ mem_key_layer: Binenc::EngineFactory.instance(:bin_struct).define do
78
+ oid :oid, CBTag.constant_value(:mem_key_layer)
79
+ int :version, 0x0100
80
+ bin :material
81
+ bin :payload
82
+ end,
83
+
84
+ mem_key_envp: Binenc::EngineFactory.instance(:bin_struct).define do
85
+ oid :oid, CBTag.constant_value(:mem_key_envp)
86
+ int :version, 0x0100
87
+ bin :kcv
88
+ bin :kcvconfig
89
+ bin :layer
90
+ end,
91
+
92
+ ccipherbox_keywrap: Binenc::EngineFactory.instance(:bin_struct).define do
93
+ oid :oid, CBTag.constant_value(:ccipherbox_keywrap)
94
+ int :version, 0x0100
95
+ bin :keyid
96
+ bin :keyConfig
97
+ bin :cipher
98
+ end,
99
+
100
+ keyConfig: Binenc::EngineFactory.instance(:bin_struct).define do
101
+ oid :oid, CBTag.constant_value(:keyConfig)
102
+ int :version, 0x0100
103
+ str :name
104
+ bin :keyConfig
105
+ end,
106
+
107
+ keyConfig_from_base: Binenc::EngineFactory.instance(:bin_struct).define do
108
+ oid :oid, CBTag.constant_value(:keyConfig_from_base)
109
+ int :version, 0x0100
110
+ str :name
111
+ str :baseName
112
+ bin :baseKeyConfig
113
+ bin :keyConfig
114
+ end,
115
+
116
+ secure_ring: Binenc::EngineFactory.instance(:bin_struct).define do
117
+ oid :oid, CBTag.constant_value(:secure_ring)
118
+ int :version, 0x0100
119
+ str :name
120
+ bin :cipherSeed
121
+ seq :keyConfigs
122
+ end,
123
+
124
+ secure_rings: Binenc::EngineFactory.instance(:bin_struct).define do
125
+ oid :oid, CBTag.constant_value(:secure_rings)
126
+ int :version, 0x0100
127
+ seq :secure_rings
128
+ end,
129
+
130
+
131
+
132
+ ccipherbox_cipher: Binenc::EngineFactory.instance(:bin_struct).define do
133
+ oid :oid, CBTag.constant_value(:ccipherbox_cipher)
134
+ int :version, 0x0100
135
+ bin :keyConfig
136
+ seq :baseMaterial
137
+ bin :cipherConfig
138
+ end,
139
+
140
+ securebox: Binenc::EngineFactory.instance(:bin_struct).define do
141
+ oid :oid, CBTag.constant_value(:securebox)
142
+ int :version, 0x0100
143
+ # ccipherboxes struct
144
+ bin :engines
145
+ seq :keyConfigs
146
+ end,
147
+
148
+
149
+ }
150
+ end
151
+
152
+ @struct[ver]
153
+
154
+ end
155
+
156
+ def translate_version(ver)
157
+ case ver.to_i
158
+ when 0x0100
159
+ "1.0"
160
+ else
161
+ raise Exception, "Version #{ver} is unknown"
162
+ end
163
+ end
164
+
165
+
166
+ end
167
+ end
@@ -0,0 +1,38 @@
1
+
2
+
3
+ if not defined?(CBTag)
4
+
5
+ CBTag = Binenc::BinTag.new
6
+
7
+ CBTag.load do
8
+
9
+ # hierarchy
10
+ define_constant(:root, '2.8.8.0') do
11
+ define_constant(:mem_key_layer, "#.10")
12
+ define_constant(:mem_key_envp, "#.11")
13
+
14
+
15
+ define_constant(:keybox, "#.30")
16
+
17
+ define_constant(:secure_ring, "#.50")
18
+ define_constant(:secure_rings, "#.51")
19
+
20
+ define_constant(:ccipherbox, "#.80") do
21
+ define_constant(:keyConfig, "#.1")
22
+ define_constant(:keyConfig_from_base, "#.2")
23
+
24
+ define_constant(:ccipherbox_cipher, "#.10")
25
+ define_constant(:ccipherbox_keywrap, "#.20")
26
+ #define_constant(:ccipherboxes, "#.20")
27
+ end
28
+
29
+ define_constant(:securebox, "#.90")
30
+ end
31
+
32
+ # constant
33
+ #define_constant(:sha1, 0x0101)
34
+
35
+ end
36
+
37
+
38
+ end
@@ -0,0 +1,74 @@
1
+
2
+
3
+ module CcipherBox
4
+ class DecryptionEngine
5
+ include TR::CondUtils
6
+
7
+ def initialize(vault)
8
+ @vault = vault
9
+ end
10
+
11
+ def init(output)
12
+
13
+ raise CcipherBox::Error, "Output is mandatory" if output.nil?
14
+
15
+ @output = output
16
+ @intOut = MemBuf.new
17
+
18
+ end
19
+
20
+ def update(data)
21
+
22
+ if @dec.nil?
23
+ @intOut.write(data)
24
+
25
+ BinStruct.instance.find_struct(@intOut) do |meta, data|
26
+
27
+ st = BinStruct.instance.struct_from_bin(meta)
28
+
29
+ st.baseMaterial.each do |ebm|
30
+
31
+ begin
32
+
33
+ baseMat = @vault.decrypt(ebm)
34
+
35
+ sk = CcipherFactory::SymKey.from_encoded(st.keyConfig) do |ops|
36
+ case ops
37
+ when :password
38
+ baseMat
39
+ end
40
+ end
41
+
42
+ @dec = CcipherFactory::SymKeyCipher.decryptor
43
+ @dec.output(@output)
44
+ @dec.key = sk
45
+ @dec.decrypt_init
46
+
47
+ @dec.decrypt_update_meta(st.cipherConfig)
48
+
49
+ @dec.decrypt_update_cipher(data) if not_empty?(data)
50
+
51
+ break
52
+
53
+ rescue KeyNotRegistered
54
+ # retry with next key
55
+ end
56
+
57
+ end
58
+
59
+ raise KeyNotRegistered, "Cannot find any loaded key decrypt this data" if @dec.nil?
60
+
61
+ end
62
+
63
+ else
64
+ @dec.decrypt_update_cipher(data)
65
+ end
66
+
67
+ end
68
+
69
+ def final
70
+ @dec.decrypt_final if not @dec.nil?
71
+ end
72
+
73
+ end
74
+ end
@@ -0,0 +1,78 @@
1
+
2
+
3
+ module CcipherBox
4
+
5
+ # link between data encryption key name with their
6
+ # respective key configs
7
+ # In a specific configurations, there might be many
8
+ # data encryption key and the key config to derive
9
+ # the data encryption key from a base key is kept
10
+ # here for later rebuild the same key again
11
+ # Only used inside SecureRing
12
+ class EncKeyConfig
13
+ include TR::CondUtils
14
+
15
+ def initialize
16
+ @keyConfigs = { }
17
+ end
18
+
19
+ def register_config(name, keyConfig)
20
+ @keyConfigs[name] = { config: keyConfig }
21
+ end
22
+
23
+ def register_derive_config(name, keyConfig, baseName, baseKeyConfig)
24
+ @keyConfigs[name] = { config: keyConfig, base: baseName, baseConfig: baseKeyConfig }
25
+ end
26
+
27
+ def keyConfigs
28
+ @keyConfigs.freeze
29
+ end
30
+
31
+ def is_derived_key?(hash)
32
+ not_empty?(hash[:base])
33
+ end
34
+
35
+ def encoded
36
+
37
+ configs = []
38
+ @keyConfigs.each do |name, val|
39
+
40
+ if not_empty?(val[:base])
41
+ st = BinStruct.instance.struct(:keyConfig_from_base)
42
+ st.name = name
43
+ st.keyConfig = val[:config]
44
+ st.baseName = val[:base]
45
+ st.baseKeyConfig = val[:baseConfig]
46
+ else
47
+ st = BinStruct.instance.struct(:keyConfig)
48
+ st.name = name
49
+ st.keyConfig = val[:config]
50
+ end
51
+
52
+ configs << st.encoded
53
+
54
+ end
55
+
56
+ configs
57
+
58
+ end
59
+
60
+ def self.from_encoded(seq)
61
+
62
+ ekc = EncKeyConfig.new
63
+ seq.each do |sst|
64
+ st = BinStruct.instance.struct_from_bin(sst)
65
+ case st.oid
66
+ when CBTag.constant_value(:keyConfig)
67
+ ekc.register_config(st.name, st.keyConfig)
68
+ when CBTag.constant_value(:keyConfig_from_base)
69
+ ekc.register_config(st.name, st.keyConfig, st.baseName, st.baseKeyConfig)
70
+ end
71
+ end
72
+
73
+ ekc
74
+
75
+ end
76
+
77
+ end
78
+ end