ccipher_factory 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +3 -0
  3. data/Gemfile +30 -0
  4. data/Gemfile.lock-java +65 -0
  5. data/Gemfile.lock-ruby +67 -0
  6. data/README.md +80 -0
  7. data/Rakefile +10 -0
  8. data/bin/console +15 -0
  9. data/bin/setup +8 -0
  10. data/ccipher_factory.gemspec +46 -0
  11. data/lib/ccipher_factory/asymkey/asymkey.rb +16 -0
  12. data/lib/ccipher_factory/asymkey/asymkey_generator.rb +87 -0
  13. data/lib/ccipher_factory/asymkey/ecc_keypair.rb +56 -0
  14. data/lib/ccipher_factory/asymkey_cipher/asymkey_cipher.rb +63 -0
  15. data/lib/ccipher_factory/asymkey_cipher/asymkey_signer.rb +44 -0
  16. data/lib/ccipher_factory/asymkey_cipher/ecc/ecc_att_decrypt.rb +55 -0
  17. data/lib/ccipher_factory/asymkey_cipher/ecc/ecc_att_encrypt.rb +70 -0
  18. data/lib/ccipher_factory/asymkey_cipher/ecc/ecc_att_signer.rb +88 -0
  19. data/lib/ccipher_factory/asymkey_cipher/ecc/ecc_att_verifier.rb +100 -0
  20. data/lib/ccipher_factory/asymkey_cipher/ecc/ecc_decrypt.rb +80 -0
  21. data/lib/ccipher_factory/asymkey_cipher/ecc/ecc_encrypt.rb +101 -0
  22. data/lib/ccipher_factory/asymkey_cipher/ecc/ecc_signer.rb +80 -0
  23. data/lib/ccipher_factory/asymkey_cipher/ecc/ecc_verifier.rb +56 -0
  24. data/lib/ccipher_factory/composite_cipher/composite_cipher.rb +28 -0
  25. data/lib/ccipher_factory/composite_cipher/decrypt_verifier.rb +116 -0
  26. data/lib/ccipher_factory/composite_cipher/sign_encryptor.rb +100 -0
  27. data/lib/ccipher_factory/compression/compression_helper.rb +103 -0
  28. data/lib/ccipher_factory/compression/compressor.rb +55 -0
  29. data/lib/ccipher_factory/compression/zlib_compressor.rb +48 -0
  30. data/lib/ccipher_factory/compression/zlib_decompressor.rb +67 -0
  31. data/lib/ccipher_factory/digest/digest.rb +180 -0
  32. data/lib/ccipher_factory/digest/supported_digest.rb +47 -0
  33. data/lib/ccipher_factory/encoding/asn1.rb +43 -0
  34. data/lib/ccipher_factory/encoding/bin_struct.rb +207 -0
  35. data/lib/ccipher_factory/encoding/binenc_constant.rb +149 -0
  36. data/lib/ccipher_factory/helpers/common.rb +124 -0
  37. data/lib/ccipher_factory/kcv/kcv.rb +89 -0
  38. data/lib/ccipher_factory/kdf/hkdf.rb +114 -0
  39. data/lib/ccipher_factory/kdf/kdf.rb +73 -0
  40. data/lib/ccipher_factory/kdf/pbkdf2.rb +82 -0
  41. data/lib/ccipher_factory/kdf/scrypt.rb +105 -0
  42. data/lib/ccipher_factory/shamir/shamir_sharing.rb +293 -0
  43. data/lib/ccipher_factory/shamir/shamir_sharing_helper.rb +88 -0
  44. data/lib/ccipher_factory/symkey/derived_symkey.rb +110 -0
  45. data/lib/ccipher_factory/symkey/hardware_symkey.rb +0 -0
  46. data/lib/ccipher_factory/symkey/soft_symkey.rb +63 -0
  47. data/lib/ccipher_factory/symkey/symkey.rb +122 -0
  48. data/lib/ccipher_factory/symkey/symkey_generator.rb +70 -0
  49. data/lib/ccipher_factory/symkey_cipher/symkey_att_decrypt.rb +64 -0
  50. data/lib/ccipher_factory/symkey_cipher/symkey_att_encrypt.rb +65 -0
  51. data/lib/ccipher_factory/symkey_cipher/symkey_att_sign.rb +84 -0
  52. data/lib/ccipher_factory/symkey_cipher/symkey_att_verify.rb +85 -0
  53. data/lib/ccipher_factory/symkey_cipher/symkey_cipher.rb +101 -0
  54. data/lib/ccipher_factory/symkey_cipher/symkey_decrypt.rb +144 -0
  55. data/lib/ccipher_factory/symkey_cipher/symkey_encrypt.rb +164 -0
  56. data/lib/ccipher_factory/symkey_cipher/symkey_sign.rb +70 -0
  57. data/lib/ccipher_factory/symkey_cipher/symkey_signer.rb +59 -0
  58. data/lib/ccipher_factory/symkey_cipher/symkey_verify.rb +76 -0
  59. data/lib/ccipher_factory/version.rb +5 -0
  60. data/lib/ccipher_factory.rb +52 -0
  61. data/run_test.rb +27 -0
  62. metadata +172 -0
@@ -0,0 +1,70 @@
1
+
2
+
3
+ module CcipherFactory
4
+ module AsymKeyCipher
5
+ module ECCAttEncrypt
6
+ include Common
7
+ include TR::CondUtils
8
+ include Compression::CompressionHelper
9
+
10
+ attr_accessor :recipient_key, :sender_keypair
11
+
12
+ def att_encrypt_init(opts = { }, &block)
13
+
14
+ @enc = AsymKeyCipher.encryptor(:ecc)
15
+ @enc.output(intOutputFile)
16
+
17
+ if is_compression_on?
18
+ logger.tdebug :ecc_att_enc, "Compression on"
19
+ @enc.compression_on
20
+ else
21
+ logger.tdebug :ecc_att_enc, "Compression off"
22
+ @enc.compression_off
23
+ end
24
+
25
+ @enc.recipient_key = @recipient_key
26
+ @enc.sender_keypair = @sender_keypair
27
+
28
+ @enc.encrypt_init(opts)
29
+
30
+ if block
31
+ instance_eval(&block)
32
+ att_encrypt_final
33
+ else
34
+ self
35
+ end
36
+
37
+ end
38
+
39
+ def att_encrypt_update(val)
40
+ raise ECCCipherError, "Output is required for encryption" if not is_output_given?
41
+ @enc.encrypt_update(val)
42
+ end
43
+
44
+ def att_encrypt_final
45
+
46
+ ts = @enc.encrypt_final
47
+
48
+ write_to_output(ts)
49
+
50
+ intOutputFile.rewind
51
+ while not intOutputFile.eof?
52
+ write_to_output(intOutputFile.read)
53
+ end
54
+
55
+ intOutputFile.close!
56
+
57
+ @output
58
+
59
+ end
60
+
61
+ def logger
62
+ if @logger.nil?
63
+ @logger = Tlogger.new
64
+ end
65
+ @logger
66
+ end
67
+
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,88 @@
1
+
2
+
3
+
4
+
5
+ module CcipherFactory
6
+ module AsymKeySigner
7
+
8
+ module ECCAttSigner
9
+ include Common
10
+ include Compression::CompressionHelper
11
+
12
+ attr_accessor :signing_key
13
+ def att_sign_init(*args, &block)
14
+
15
+ @signer = AsymKeySigner.signer
16
+ @signer.signing_key = @signing_key
17
+
18
+ @signer.sign_init(*args)
19
+
20
+ @totalPlain = 0
21
+ @totalCompressed = 0
22
+
23
+ if block
24
+ instance_eval(&block)
25
+ att_sign_final
26
+ else
27
+ self
28
+ end
29
+
30
+ end
31
+
32
+ def att_sign_update(val)
33
+
34
+ raise ECCSignerError, "Output is required for attached sign with ECC" if not is_output_given?
35
+
36
+ @totalPlain += val.length
37
+ @signer.sign_update(val)
38
+ res = compress_data_if_active(val)
39
+ intOutputFile.write(res)
40
+ @totalCompressed += res.length
41
+
42
+ res
43
+
44
+ end
45
+
46
+ def att_sign_final
47
+ meta = @signer.sign_final
48
+
49
+ #ts = Encoding::ASN1Encoder.instance(:ecc_att_sign)
50
+ ts = BinStruct.instance.struct(:ecc_att_sign)
51
+ ts.ecc_signature = meta
52
+
53
+ #ts.set(:ecc_signature, meta)
54
+ if is_compression_on?
55
+ #ts.set(:compression, compressor.compress_final)
56
+ ts.compression = compressor.compress_final
57
+ else
58
+ #ts.set(:compression, encode_null_compressor)
59
+ ts.compression = encode_null_compressor
60
+ end
61
+
62
+ smeta = ts.encoded
63
+ write_to_output(smeta)
64
+
65
+ intOutputFile.rewind
66
+ while not intOutputFile.eof?
67
+ write_to_output(intOutputFile.read)
68
+ end
69
+
70
+ disposeOutput(intOutputFile)
71
+
72
+ logger.tdebug :ecc_att_sign, "Total Plain : #{@totalPlain} / Total Compressed : #{@totalCompressed} = #{(@totalCompressed*1.0)/@totalPlain*100} %" if is_compression_on?
73
+
74
+ smeta
75
+
76
+ end
77
+
78
+ def logger
79
+ if @logger.nil?
80
+ @logger = Tlogger.new
81
+ end
82
+ @logger
83
+ end
84
+
85
+ end
86
+
87
+ end
88
+ end
@@ -0,0 +1,100 @@
1
+
2
+
3
+
4
+ module CcipherFactory
5
+ module AsymKeySigner
6
+
7
+ module ECCAttVerifier
8
+ include Common
9
+ include Compression::CompressionHelper
10
+
11
+ def embedded_signer
12
+ if not_empty?(@ver)
13
+ @ver.embedded_signer
14
+ else
15
+ nil
16
+ end
17
+ end
18
+
19
+ def att_verify_init(*args, &block)
20
+
21
+ @params = args
22
+
23
+ if block
24
+ instance_eval(&block)
25
+ att_verify_final
26
+ else
27
+ self
28
+ end
29
+
30
+ end
31
+
32
+ def att_verify_update(val)
33
+
34
+ if @ver.nil?
35
+
36
+ intOutputBuf.write(val)
37
+ begin
38
+ Encoding.extract_meta(intOutputBuf) do |meta, bal|
39
+
40
+ ts = BinStruct.instance.struct_from_bin(meta)
41
+ smeta = ts.ecc_signature
42
+ compression = ts.compression
43
+
44
+ decompressor_from_encoded(compression)
45
+ if is_compression_on?
46
+ logger.tdebug :ecc_att_ver, "Compression on"
47
+ else
48
+ logger.tdebug :ecc_att_ver, "No compression"
49
+ end
50
+
51
+ @ver = AsymKeySigner.verifier
52
+ @ver.output(@output) if is_output_given?
53
+
54
+ @ver.verify_init(*@params)
55
+ @ver.verify_update_meta(smeta)
56
+
57
+ att_verify_update(bal) if bal.length > 0
58
+
59
+ disposeOutput(intOutputBuf)
60
+
61
+ end
62
+ rescue Encoding::InsufficientData
63
+ end
64
+
65
+ else
66
+ logger.tdebug :ecc_att_ver, "Compressed size : #{val.length}" if is_compression_on?
67
+ res = decompress_data_if_active(val)
68
+ @ver.verify_update_data(res)
69
+ intOutputFile.write(res)
70
+ end
71
+
72
+ end
73
+
74
+ def att_verify_final
75
+
76
+ res = @ver.verify_final
77
+
78
+ if is_output_given?
79
+ intOutputFile.rewind
80
+ while not intOutputFile.eof?
81
+ write_to_output(intOutputFile.read)
82
+ end
83
+ end
84
+
85
+ disposeOutput(intOutputFile)
86
+ res
87
+
88
+ end
89
+
90
+ def logger
91
+ if @logger.nil?
92
+ @logger = Tlogger.new
93
+ end
94
+ @logger
95
+ end
96
+
97
+ end
98
+
99
+ end
100
+ end
@@ -0,0 +1,80 @@
1
+
2
+
3
+ require_relative '../../symkey_cipher/symkey_cipher'
4
+ require_relative '../../kdf/kdf'
5
+ require_relative '../../asymkey/ecc_keypair'
6
+
7
+ module CcipherFactory
8
+ module AsymKeyCipher
9
+ module ECCDecrypt
10
+ include TR::CondUtils
11
+ include Common
12
+
13
+ class ECCCipherError < AsymKeyCipherError; end
14
+
15
+ attr_accessor :decryption_key
16
+ def decrypt_init(opts = { }, &block)
17
+
18
+ #raise ECCCipherError, "Decryption keypair is mandatory" if is_empty?(eccKeypair)
19
+
20
+ #@eccKeypair = eccKeypair
21
+
22
+ if block
23
+ instance_eval(&block)
24
+ decrypt_final
25
+ else
26
+ self
27
+ end
28
+
29
+ end
30
+
31
+ def decrypt_update_meta(meta)
32
+
33
+ raise ECCCipherError, "Output is required" if not is_output_given?
34
+
35
+ ts = BinStruct.instance.struct_from_bin(meta)
36
+ senderPub = ts.sender_public
37
+ cipherConf = ts.cipher_config
38
+ keyConf = ts.key_config
39
+
40
+ sender = Ccrypto::AlgoFactory.engine(Ccrypto::ECCPublicKey).to_key(senderPub)
41
+ derived = @decryption_key.derive_dh_shared_secret(sender)
42
+
43
+ sessKey = DerivedSymKey.from_encoded(keyConf) do |ops|
44
+ case ops
45
+ when :password
46
+ derived
47
+ end
48
+ end
49
+
50
+ @cipher = SymKeyCipher.decryptor
51
+ @cipher.output(@output)
52
+ @cipher.key = sessKey
53
+ @cipher.decrypt_init
54
+ @cipher.decrypt_update_meta(cipherConf)
55
+
56
+ end
57
+
58
+ def decrypt_update_cipher(cipher)
59
+ raise ECCCipherError, "Please update meta first before update cipher" if is_empty?(@cipher)
60
+
61
+ @cipher.decrypt_update_cipher(cipher)
62
+ end
63
+
64
+ def decrypt_final
65
+
66
+ @cipher.decrypt_final
67
+
68
+ end
69
+
70
+ def logger
71
+ if @logger.nil?
72
+ @logger = Tlogger.new
73
+ @logger.tag = :ecc_dec
74
+ end
75
+ @logger
76
+ end
77
+
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,101 @@
1
+
2
+ require_relative '../../symkey_cipher/symkey_cipher'
3
+ require_relative '../../kdf/kdf'
4
+
5
+ require_relative '../../asymkey/asymkey_generator'
6
+ require_relative '../../compression/compression_helper'
7
+
8
+ module CcipherFactory
9
+ module AsymKeyCipher
10
+ module ECCEncrypt
11
+ include TR::CondUtils
12
+ include Common
13
+ include Compression::CompressionHelper
14
+
15
+ class ECCCipherError < AsymKeyCipher::AsymKeyCipherError; end
16
+
17
+ attr_accessor :recipient_key, :sender_keypair
18
+
19
+ def encrypt_init(opts = { }, &block)
20
+
21
+ #@sender = opts[:sender_keypair]
22
+ #recpPub = opts[:recipient_public]
23
+ recpPub = @recipient_key
24
+
25
+ raise ECCCipherError, "Receipient public key is required" if is_empty?(recpPub)
26
+ raise ECCCipherError, "Cipher requires output to be set" if not is_output_given?
27
+ raise ECCCipherError, "Sender Keypair is required" if is_empty?(@sender_keypair)
28
+
29
+ #if is_empty?(@sender_keypair)
30
+ # @sender_keypair = AsymKeyGenerator.generate(:ecc)
31
+ #end
32
+
33
+ #derived = @sender_keypair.dh_compute_key(recpPub)
34
+ #logger.debug "sender : #{@sender_keypair.inspect} / #{@sender_keypair.private?}"
35
+ #logger.debug "recp : #{recpPub.inspect}"
36
+ derived = @sender_keypair.derive_dh_shared_secret(recpPub)
37
+
38
+ @sessKey = SymKeyGenerator.derive(:aes, 256) do |ops|
39
+ case ops
40
+ when :password
41
+ derived
42
+ end
43
+ end
44
+
45
+ @cipher = SymKeyCipher.encryptor
46
+ @cipher.output(intOutputFile)
47
+ @cipher.key = @sessKey
48
+
49
+ if is_compression_on?
50
+ logger.debug "Turning on compression"
51
+ @cipher.compression_on
52
+ else
53
+ logger.debug "Compression not active"
54
+ @cipher.compression_off
55
+ end
56
+
57
+ @cipher.encrypt_init
58
+
59
+ if block
60
+ instance_eval(&block)
61
+ encrypt_final
62
+ else
63
+ self
64
+ end
65
+
66
+ end
67
+
68
+ def encrypt_update(val)
69
+ @cipher.encrypt_update(val)
70
+ end
71
+
72
+ def encrypt_final
73
+
74
+ cipherConfig = @cipher.encrypt_final
75
+
76
+ intOutputFile.rewind
77
+ while not intOutputFile.eof?
78
+ write_to_output(intOutputFile.read)
79
+ end
80
+ cleanup_intOutputFile
81
+
82
+ pkBin = @sender_keypair.public_key.to_bin
83
+ ts = BinStruct.instance.struct(:ecc_cipher)
84
+ ts.sender_public = @sender_keypair.public_key.to_bin
85
+ ts.cipher_config = cipherConfig
86
+ ts.key_config = @sessKey.encoded
87
+ ts.encoded
88
+
89
+ end
90
+
91
+ def logger
92
+ if @logger.nil?
93
+ @logger = Tlogger.new
94
+ @logger.tag = :ecc_enc
95
+ end
96
+ @logger
97
+ end
98
+
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,80 @@
1
+
2
+ require_relative '../../digest/digest'
3
+ require_relative '../../compression/compressor'
4
+
5
+ module CcipherFactory
6
+ module AsymKeySigner
7
+ module ECCSigner
8
+ include TR::CondUtils
9
+ include Common
10
+
11
+ class ECCSignerError < AsymKeySignerError; end
12
+
13
+ attr_accessor :signing_key
14
+
15
+ def compression_on
16
+ @compress = true
17
+ end
18
+
19
+ def compression_off
20
+ @compress = false
21
+ end
22
+
23
+ def sign_init(opts = { }, &block)
24
+
25
+ raise ECCSignerError, "Signer must be given" if is_empty?(@signing_key)
26
+
27
+ @digest = Digest.instance
28
+ @digest.output(intOutputBuf)
29
+ @digest.digest_init
30
+
31
+ if @compress
32
+ logger.tdebug :asymkey_enc, "Compression on"
33
+ @compressor = CcipherFactory::Compression::Compressor.new
34
+ @compressor.compress
35
+ @compressor.compress_init
36
+ else
37
+ logger.tdebug :asymkey_enc, "Compression off"
38
+ end
39
+
40
+
41
+ if block
42
+ instance_eval(&block)
43
+ sign_final
44
+ else
45
+ self
46
+ end
47
+
48
+ end
49
+
50
+ def sign_update(val)
51
+ @digest.digest_update(val)
52
+ end
53
+
54
+ def sign_final
55
+ dig = @digest.digest_final
56
+
57
+ eccConf = Ccrypto::ECCConfig.new
58
+ eccConf.keypair = @signing_key.keypair
59
+ eccEng = Ccrypto::AlgoFactory.engine(eccConf)
60
+ sign = eccEng.sign(intOutputBuf.bytes)
61
+
62
+ ts = BinStruct.instance.struct(:ecc_signature)
63
+ ts.digest_info = dig
64
+ ts.signer_info = @signing_key.to_signer_info
65
+ ts.signature = sign
66
+ ts.encoded
67
+
68
+ end
69
+
70
+ def logger
71
+ if @logger.nil?
72
+ @logger = Tlogger.new
73
+ end
74
+ @logger
75
+ end
76
+
77
+
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,56 @@
1
+
2
+ require_relative '../../asymkey/ecc_keypair'
3
+
4
+ module CcipherFactory
5
+ module AsymKeySigner
6
+ module ECCVerifier
7
+ include Common
8
+
9
+ attr_accessor :verification_key
10
+ attr_reader :embedded_signer
11
+ def verify_init(*args, &block)
12
+
13
+ if block
14
+ instance_eval(&block)
15
+ verify_final
16
+ else
17
+ self
18
+ end
19
+
20
+ end
21
+
22
+ def verify_update_meta(meta)
23
+
24
+ ts = BinStruct.instance.struct_from_bin(meta)
25
+ digInfo = ts.digest_info
26
+ sigInfo = ts.signer_info
27
+ @sign = ts.signature
28
+
29
+ @digest = Digest.from_encoded(digInfo)
30
+ @digest.output(intOutputBuf)
31
+
32
+ @signer = KeyPair::ECCKeyPair.from_signer_info(sigInfo)
33
+ @embedded_signer = @signer
34
+
35
+ end
36
+
37
+ def verify_update_data(data)
38
+ @digest.digest_update(data)
39
+ end
40
+
41
+ def verify_final
42
+
43
+ @digest.digest_final
44
+
45
+ res = Ccrypto::AlgoFactory.engine(Ccrypto::ECCConfig).verify(@signer, intOutputBuf.bytes, @sign)
46
+
47
+ #res = @signer.dsa_verify_asn1(intOutputBuf.string, @sign)
48
+
49
+ res
50
+
51
+ end
52
+
53
+ end
54
+ end
55
+
56
+ end
@@ -0,0 +1,28 @@
1
+
2
+
3
+ module CcipherFactory
4
+ module CompositeCipher
5
+
6
+ class CompositeCipherError < StandardError; end
7
+
8
+ class CompCipher; end
9
+
10
+ def self.sign_encryptor(opts = { })
11
+ cc = CompCipher.new
12
+ cc.extend(SignEncryptor)
13
+ cc
14
+ end
15
+
16
+ def self.decrypt_verifier(opts = { })
17
+ cc = CompCipher.new
18
+ cc.extend(DecryptVerifier)
19
+ cc
20
+ end
21
+
22
+ end
23
+ end
24
+
25
+ require_relative 'sign_encryptor'
26
+ require_relative 'decrypt_verifier'
27
+
28
+