izokatu 0.1.1

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 (54) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +3 -0
  3. data.tar.gz.sig +2 -0
  4. data/.gitignore +19 -0
  5. data/.rspec +3 -0
  6. data/.rubocop.yml +42 -0
  7. data/.yardopts +1 -0
  8. data/Gemfile +8 -0
  9. data/Gemfile.lock +86 -0
  10. data/LICENSE.txt +21 -0
  11. data/README.md +152 -0
  12. data/Rakefile +8 -0
  13. data/bin/console +15 -0
  14. data/bin/setup +8 -0
  15. data/certs/mongalless.pem +26 -0
  16. data/izokatu.gemspec +47 -0
  17. data/lib/izokatu.rb +423 -0
  18. data/lib/izokatu/action_call_options_selector.rb +214 -0
  19. data/lib/izokatu/action_call_selector.rb +132 -0
  20. data/lib/izokatu/callable.rb +13 -0
  21. data/lib/izokatu/ciphers.rb +18 -0
  22. data/lib/izokatu/decrypter.rb +62 -0
  23. data/lib/izokatu/encrypter.rb +67 -0
  24. data/lib/izokatu/exporter.rb +36 -0
  25. data/lib/izokatu/exporter/file_exporter.rb +36 -0
  26. data/lib/izokatu/exporter/function_exporter.rb +16 -0
  27. data/lib/izokatu/exporter/stdout_exporter.rb +18 -0
  28. data/lib/izokatu/helpers.rb +213 -0
  29. data/lib/izokatu/importer/file_importer.rb +47 -0
  30. data/lib/izokatu/importer/function_importer.rb +36 -0
  31. data/lib/izokatu/izokatu_elements_requires.rb +46 -0
  32. data/lib/izokatu/keys_generator.rb +53 -0
  33. data/lib/izokatu/openssl/private_key/auth/ccm/decrypter.rb +67 -0
  34. data/lib/izokatu/openssl/private_key/auth/ccm/encrypter.rb +56 -0
  35. data/lib/izokatu/openssl/private_key/auth/decrypter.rb +79 -0
  36. data/lib/izokatu/openssl/private_key/auth/encrypter.rb +69 -0
  37. data/lib/izokatu/openssl/private_key/default/decrypter.rb +75 -0
  38. data/lib/izokatu/openssl/private_key/default/encrypter.rb +75 -0
  39. data/lib/izokatu/openssl/public_key/ec/decrypter.rb +105 -0
  40. data/lib/izokatu/openssl/public_key/ec/encrypter.rb +106 -0
  41. data/lib/izokatu/openssl/public_key/ec/keys_generator.rb +77 -0
  42. data/lib/izokatu/openssl/public_key/rsa/decrypter.rb +53 -0
  43. data/lib/izokatu/openssl/public_key/rsa/encrypter.rb +55 -0
  44. data/lib/izokatu/openssl/public_key/rsa/keys_generator.rb +64 -0
  45. data/lib/izokatu/rbnacl/decrypter.rb +42 -0
  46. data/lib/izokatu/rbnacl/encrypter.rb +45 -0
  47. data/lib/izokatu/rbnacl/private_key/decrypter.rb +56 -0
  48. data/lib/izokatu/rbnacl/private_key/encrypter.rb +61 -0
  49. data/lib/izokatu/rbnacl/public_key/decrypter.rb +51 -0
  50. data/lib/izokatu/rbnacl/public_key/encrypter.rb +61 -0
  51. data/lib/izokatu/rbnacl/public_key/keys_generator.rb +33 -0
  52. data/lib/izokatu/version.rb +6 -0
  53. metadata +315 -0
  54. metadata.gz.sig +0 -0
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Izokatu
4
+ module Openssl
5
+ # Namespace for Openssl private key classes
6
+ module PrivateKey
7
+ # Namespace for OpenSSL private key non-authenticated classes
8
+ module Default
9
+ # OpenSSL private key decrypter for non-authenticated ciphers
10
+ class Decrypter < Izokatu::Decrypter
11
+ # @return [String] OpenSSL private key cipher
12
+ attr_reader :cipher
13
+ # @return [String] key for private key encryption/decryption
14
+ attr_reader :key
15
+ # @return [String] initialization vector for one-time use
16
+ attr_reader :nonce
17
+
18
+ # Default Openssl::PrivateKey::Default::Decrypter option
19
+ DEFAULT_OPTIONS = {
20
+ cipher: 'AES256'
21
+ }.freeze
22
+
23
+ Contract Contracts::HashOf[Symbol, String] => Any
24
+ # Initialize options for OpenSSL EC decryption
25
+ #
26
+ # @param encrypted_data (#encrypted_data)
27
+ # @param cipher (#cipher)
28
+ # @param key (#key)
29
+ # @param nonce (#nonce)
30
+ #
31
+ # @since 0.1.0
32
+ def initialize(encrypted_data:, cipher:, key:, nonce:)
33
+ super(encrypted_data: encrypted_data)
34
+ @cipher = cipher || DEFAULT_OPTIONS[:cipher]
35
+ @key = key
36
+ @nonce = nonce
37
+ create_decrypter!
38
+ initialize_decrypter_params!
39
+ end
40
+
41
+ private
42
+
43
+ Contract None => OpenSSL::Cipher
44
+ # Initializing decrypter
45
+ #
46
+ # @return [OpenSSL::Cipher] decrypter instance
47
+ #
48
+ # @since 0.1.0
49
+ def create_decrypter!
50
+ @decrypter = OpenSSL::Cipher.new(cipher).decrypt
51
+ end
52
+
53
+ Contract None => Any
54
+ # Initializing decrypter params
55
+ #
56
+ # @since 0.1.0
57
+ def initialize_decrypter_params!
58
+ decrypter.key = key
59
+ decrypter.iv = nonce
60
+ end
61
+
62
+ Contract None => Contracts::HashOf[Symbol, String]
63
+ # Decrypting data
64
+ #
65
+ # @return [Hash] decrypted data
66
+ #
67
+ # @since 0.1.0
68
+ def decrypt_data!
69
+ { decrypted_data_string: decrypter.update(encrypted_data) + decrypter.final }
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Izokatu
4
+ module Openssl
5
+ module PrivateKey
6
+ module Default
7
+ # OpenSSL private key encrypter for non-authenticated ciphers
8
+ class Encrypter < Izokatu::Encrypter
9
+ # @return [String] OpenSSL private key cipher
10
+ attr_reader :cipher
11
+ # @return [String] key for private key encryption/decryption
12
+ attr_reader :key
13
+ # @return [String] initialization vector for one-time use
14
+ attr_reader :nonce
15
+
16
+ # Default Openssl::PrivateKey::Default::Encrypter option
17
+ DEFAULT_OPTIONS = {
18
+ cipher: 'AES256'
19
+ }.freeze
20
+
21
+ Contract Contracts::HashOf[Symbol, String] => Any
22
+ # Initializing options for OpenSSL EC encryption
23
+ #
24
+ # @param clear_data (#clear_data)
25
+ # @param cipher (#cipher)
26
+ #
27
+ # @since 0.1.0
28
+ def initialize(clear_data:, cipher:)
29
+ super(clear_data: clear_data)
30
+ @cipher = cipher || DEFAULT_OPTIONS[:cipher]
31
+ create_encrypter!
32
+ @key = encrypter.random_key
33
+ @nonce = encrypter.random_iv
34
+ initialize_encrypter_params!
35
+ end
36
+
37
+ private
38
+
39
+ Contract None => OpenSSL::Cipher
40
+ # Initializing encrypter
41
+ #
42
+ # @return [OpenSSL::Cipher] encrypter instance
43
+ #
44
+ # @since 0.1.0
45
+ def create_encrypter!
46
+ @encrypter = OpenSSL::Cipher.new(cipher).encrypt
47
+ end
48
+
49
+ Contract None => Any
50
+ # Initializing encrypter params
51
+ #
52
+ # @since 0.1.0
53
+ def initialize_encrypter_params!
54
+ # OpenSSL::Cipher instances has only key=, iv= and auth_data= methods
55
+ encrypter.key = key
56
+ encrypter.iv = nonce
57
+ end
58
+
59
+ Contract None => Contracts::ArrayOf[Contracts::HashOf[Symbol, String]]
60
+ # Encrypting data
61
+ #
62
+ # @return [Array] encrypted data with decrypter params
63
+ #
64
+ # @since 0.1.0
65
+ def encrypt_data!
66
+ [
67
+ { encrypted_data_string: encrypter.update(clear_data) + encrypter.final },
68
+ { key: key, nonce: nonce }
69
+ ]
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Izokatu
4
+ module Openssl
5
+ module PublicKey
6
+ module EC
7
+ # OpenSSL public key EC decrypter
8
+ class Decrypter < Izokatu::Decrypter
9
+ # @return [String] private key string for decryption
10
+ attr_reader :private_key
11
+ # @return [String] ECIES cipher name
12
+ attr_reader :ecies_cipher
13
+ # @return [String] ECIES digest name
14
+ attr_reader :ecies_digest
15
+ # @return [Symbol] ECIES MAC length
16
+ attr_reader :ecies_mac_length
17
+ # @return [String] ECIES KDF digest name
18
+ attr_reader :ecies_kdf_digest
19
+ # @return [String] ECIES MAC digest name
20
+ attr_reader :ecies_mac_digest
21
+
22
+ # Default options for ECIES
23
+ DEFAULT_ECIES_OPTIONS = {
24
+ ecies_cipher: 'AES-256-CTR',
25
+ ecies_digest: 'SHA512',
26
+ ecies_mac_length: :full,
27
+ ecies_kdf_digest: 'SHA512',
28
+ ecies_mac_digest: 'SHA512'
29
+ }.freeze
30
+
31
+ Contract Contracts::HashOf[Symbol, Maybe[String]] => Any
32
+ def initialize(encrypted_data:, private_key:, ecies_options:)
33
+ # Initialize options for OpenSSL EC decryption
34
+ #
35
+ # @param encrypted_data (#encrypted_data)
36
+ # @param private_key (#private_key)
37
+ # @param ecies_options Hash with ECIES options
38
+ #
39
+ # @since 0.1.0
40
+ super(encrypted_data: encrypted_data)
41
+ initialize_private_key!(private_key)
42
+ initialize_ecies_options!(ecies_options || DEFAULT_ECIES_OPTIONS)
43
+ initialize_decrypter!
44
+ end
45
+
46
+ Contract String => OpenSSL::PKey::EC
47
+ # Initialize EC private key from private key string
48
+ #
49
+ # @param private_key (#private_key)
50
+ #
51
+ # @return [OpenSSL:PKey::EC] OpenSSL private key instance
52
+ #
53
+ # @since 0.1.0
54
+ def initialize_private_key!(private_key)
55
+ raise 'ERROR: No private key!' unless private_key
56
+
57
+ @private_key = OpenSSL::PKey.read(private_key)
58
+ end
59
+
60
+ Contract Contracts::HashOf[Symbol, Or[String, Symbol]] => Any
61
+ # Initialize ECIES options
62
+ #
63
+ # @param ecies_options Hash with ECIES options
64
+ #
65
+ # @since 0.1.0
66
+ def initialize_ecies_options!(ecies_options)
67
+ @ecies_cipher = ecies_options[:ecies_cipher]
68
+ @ecies_digest = ecies_options[:ecies_digest]
69
+ @ecies_mac_length = ecies_options[:ecies_mac_length]
70
+ @ecies_kdf_digest = ecies_options[:ecies_kdf_digest]
71
+ @ecies_mac_digest = ecies_options[:ecies_mac_digest]
72
+ end
73
+
74
+ Contract None => ECIES::Crypt
75
+ # Initialize decrypter
76
+ #
77
+ # @return [ECIES::Crypt] decrypter instance
78
+ #
79
+ # @since 0.1.0
80
+ def initialize_decrypter!
81
+ @decrypter = ECIES::Crypt.new(
82
+ cipher: ecies_cipher,
83
+ digest: ecies_digest,
84
+ mac_length: ecies_mac_length,
85
+ kdf_digest: ecies_kdf_digest,
86
+ mac_digest: ecies_mac_digest
87
+ )
88
+ end
89
+
90
+ private
91
+
92
+ Contract None => Contracts::HashOf[Symbol, String]
93
+ # Decrypting data
94
+ #
95
+ # @return [Hash] decrypted data
96
+ #
97
+ # @since 0.1.0
98
+ def decrypt_data!
99
+ { decrypted_data_string: decrypter.decrypt(private_key, encrypted_data) }
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Izokatu
4
+ module Openssl
5
+ module PublicKey
6
+ module EC
7
+ # OpenSSL public key EC encrypter
8
+ class Encrypter < Izokatu::Encrypter
9
+ # @return [String] public key string for decryption
10
+ attr_reader :public_key
11
+ # @return [String] ECIES cipher name
12
+ attr_reader :ecies_cipher
13
+ # @return [String] ECIES digest name
14
+ attr_reader :ecies_digest
15
+ # @return [Symbol] ECIES MAC length
16
+ attr_reader :ecies_mac_length
17
+ # @return [String] ECIES KDF digest name
18
+ attr_reader :ecies_kdf_digest
19
+ # @return [String] ECIES MAC digest name
20
+ attr_reader :ecies_mac_digest
21
+
22
+ # Default options for ECIES
23
+ DEFAULT_ECIES_OPTIONS = {
24
+ ecies_cipher: 'AES-256-CTR',
25
+ ecies_digest: 'SHA512',
26
+ ecies_mac_length: :full,
27
+ ecies_kdf_digest: 'SHA512',
28
+ ecies_mac_digest: 'SHA512'
29
+ }.freeze
30
+
31
+ Contract Contracts::HashOf[Symbol, Maybe[String]] => Any
32
+ # Initialize options for OpenSSL EC encryption
33
+ #
34
+ # @param clear_data (#clear_data)
35
+ # @param public_key (#public_key)
36
+ # @param ecies_options Hash with ECIES options
37
+ #
38
+ # @since 0.1.0
39
+ def initialize(clear_data:, public_key:, ecies_options:)
40
+ super(clear_data: clear_data)
41
+ @public_key = public_key
42
+ initialize_public_key!(public_key)
43
+ initialize_ecies_options!(ecies_options || DEFAULT_ECIES_OPTIONS)
44
+ initialize_encrypter!
45
+ end
46
+
47
+ Contract String => OpenSSL::PKey::EC
48
+ # Initialize EC public key from public key string
49
+ #
50
+ # @param public_key (#public_key)
51
+ #
52
+ # @return [OpenSSL:PKey::EC] OpenSSL public key instance
53
+ #
54
+ # @since 0.1.0
55
+ def initialize_public_key!(public_key)
56
+ raise 'ERROR: No public key!' unless public_key
57
+
58
+ @public_key = OpenSSL::PKey.read(public_key)
59
+ end
60
+
61
+ Contract Contracts::HashOf[Symbol, Or[String, Symbol]] => Any
62
+ # Initialize ECIES options
63
+ #
64
+ # @param ecies_options Hash with ECIES options
65
+ #
66
+ # @since 0.1.0
67
+ def initialize_ecies_options!(ecies_options)
68
+ @ecies_cipher = ecies_options[:ecies_cipher]
69
+ @ecies_digest = ecies_options[:ecies_digest]
70
+ @ecies_mac_length = ecies_options[:ecies_mac_length]
71
+ @ecies_kdf_digest = ecies_options[:ecies_kdf_digest]
72
+ @ecies_mac_digest = ecies_options[:ecies_mac_digest]
73
+ end
74
+
75
+ Contract None => ECIES::Crypt
76
+ # Initialize encrypter
77
+ #
78
+ # @return [ECIES::Crypt] encrypter instance
79
+ #
80
+ # @since 0.1.0
81
+ def initialize_encrypter!
82
+ @encrypter = ECIES::Crypt.new(
83
+ cipher: ecies_cipher,
84
+ digest: ecies_digest,
85
+ mac_length: ecies_mac_length,
86
+ kdf_digest: ecies_kdf_digest,
87
+ mac_digest: ecies_mac_digest
88
+ )
89
+ end
90
+
91
+ private
92
+
93
+ Contract None => Contracts::ArrayOf[Contracts::HashOf[Symbol, String]]
94
+ # Encrypting data
95
+ #
96
+ # @return [Array] encrypted data with empty hash in place of params
97
+ #
98
+ # @since 0.1.0
99
+ def encrypt_data!
100
+ [{ encrypted_data_string: encrypter.encrypt(public_key, clear_data) }, {}]
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Izokatu
4
+ module Openssl
5
+ # Namespace for OpenSSL public key classes
6
+ module PublicKey
7
+ # Namespace for OpenSSL public key EC classes
8
+ module EC
9
+ # OpenSSL EC keys generator
10
+ class KeysGenerator < Izokatu::KeysGenerator
11
+ # @return [String] cipher for keys generation
12
+ attr_reader :cipher
13
+
14
+ # Default options for OpenSSL EC keys generation
15
+ DEFAULT_OPTIONS = {
16
+ cipher: 'secp521r1'
17
+ }.freeze
18
+
19
+ Contract Contracts::HashOf[Symbol, String] => Any
20
+ # Initialize option for OpenSSL EC keys generation
21
+ #
22
+ # @param cipher (#cipher)
23
+ #
24
+ # @since 0.1.0
25
+ def initialize(cipher:)
26
+ super()
27
+ @cipher = cipher || DEFAULT_OPTIONS[:cipher]
28
+ end
29
+
30
+ Contract None => Contracts::HashOf[Or[*KEYS_SYMBOLS], String]
31
+ # Performing generation of OpenSSL EC private and public keys
32
+ #
33
+ # @return [Hash] OpenSSL EC public and private keys
34
+ #
35
+ # @since 0.1.0
36
+ def perform
37
+ validate_ec_cipher!
38
+ super.transform_values(&:to_pem)
39
+ end
40
+
41
+ private
42
+
43
+ # Verifying EC cipher
44
+ #
45
+ # @raise RuntimeError
46
+ #
47
+ # @since 0.1.0
48
+ def validate_ec_cipher!
49
+ raise 'ERROR: Unknown EC cipher!' unless PBKEY_EC_CIPHERS.include?(cipher)
50
+ end
51
+
52
+ Contract None => OpenSSL::PKey::EC
53
+ # Performing generation of OpenSSL EC private key
54
+ #
55
+ # @return [OpenSSL::PKey::EC] OpenSSL private key
56
+ #
57
+ # @since 0.1.0
58
+ def generate_private_key
59
+ OpenSSL::PKey::EC.new(cipher).generate_key
60
+ end
61
+
62
+ Contract OpenSSL::PKey::EC => OpenSSL::PKey::EC
63
+ # Performing generation of OpenSSL EC private key
64
+ #
65
+ # @return [OpenSSL::PKey::EC] OpenSSL private key
66
+ #
67
+ # @since 0.1.0
68
+ def generate_public_key(private_key)
69
+ private_key_copy = OpenSSL::PKey::EC.new(private_key.public_key.group)
70
+ private_key_copy.public_key = private_key.public_key
71
+ private_key_copy
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Izokatu
4
+ module Openssl
5
+ module PublicKey
6
+ module RSA
7
+ # OpenSSL public key RSA decrypter
8
+ class Decrypter < Izokatu::Decrypter
9
+ # @return [String] private key string for decryption
10
+ attr_reader :private_key
11
+
12
+ Contract Contracts::HashOf[Symbol, String] => Any
13
+ # Initialize options for OpenSSL RSA decryption
14
+ #
15
+ # @param encrypted_data (#encrypted_data)
16
+ # @param private_key (#private_key)
17
+ #
18
+ # @since 0.1.0
19
+ def initialize(encrypted_data:, private_key:)
20
+ super(encrypted_data: encrypted_data)
21
+ initialize_private_key!(private_key)
22
+ end
23
+
24
+ private
25
+
26
+ Contract String => OpenSSL::PKey::RSA
27
+ # Initialize RSA private key from private key string
28
+ #
29
+ # @param private_key (#private_key)
30
+ #
31
+ # @return [OpenSSL:PKey::RSA] OpenSSL private key instance
32
+ #
33
+ # @since 0.1.0
34
+ def initialize_private_key!(private_key)
35
+ raise 'ERROR: No private key!' unless private_key
36
+
37
+ @private_key = OpenSSL::PKey.read(private_key)
38
+ end
39
+
40
+ Contract None => Contracts::HashOf[Symbol, String]
41
+ # Decrypting data
42
+ #
43
+ # @return [Hash] decrypted data
44
+ #
45
+ # @since 0.1.0
46
+ def decrypt_data!
47
+ { decrypted_data_string: private_key.private_decrypt(encrypted_data) }
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end