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,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Izokatu
4
+ # Izokatu importer for importing data from some file
5
+ class FileImporter
6
+ extend Callable
7
+
8
+ include Contracts
9
+ include Izokatu::Helpers
10
+
11
+ # @return [String] name of file to import from
12
+ attr_reader :filename
13
+ # @return [TrueClass, FalseClass] status of ability to delete file after import
14
+ attr_reader :delete_imported
15
+ # @return [TrueClass || FalseClass] status of ability to decode imported data
16
+ attr_reader :decode
17
+
18
+ Contract Contracts::HashOf[Symbol, Or[String, Bool]] => Any
19
+ # Initializing options for import
20
+ #
21
+ # @param filename (#filename)
22
+ # @param delete_imported (#delete_imported)
23
+ # @param decode (#decode)
24
+ #
25
+ # @since 0.1.0
26
+ def initialize(filename:, delete_imported:, decode:)
27
+ @filename = filename
28
+ @delete_imported = delete_imported
29
+ @decode = decode
30
+ end
31
+
32
+ Contract None => Contracts::HashOf[Symbol, String]
33
+ # Performing import of data
34
+ #
35
+ # @return [Hash] imported data
36
+ #
37
+ # @since 0.1.0
38
+ def perform
39
+ raise 'No data file!' unless File.exist?(filename)
40
+
41
+ encoded = File.readlines(filename, chomp: true)
42
+ File.delete(filename) if delete_imported
43
+ data = { data: encoded.length == 1 ? encoded[0] : encoded.join("\n") }
44
+ decode ? decode_data(data) : data
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Izokatu
4
+ # Izokatu importer for importing data from some function, via :data param
5
+ class FunctionImporter
6
+ extend Callable
7
+
8
+ include Contracts
9
+ include Izokatu::Helpers
10
+
11
+ # @return [String] data for import
12
+ attr_reader :data
13
+
14
+ Contract Contracts::HashOf[Symbol, Or[Contracts::HashOf[Symbol, Maybe[String]], String, Bool]] =>
15
+ Or[Contracts::HashOf[Symbol, Maybe[String]], String]
16
+ # Initializing options for import
17
+ #
18
+ # @param data (#data)
19
+ # @param decode [TrueClass || FalseClass] Enable/disable decoding of imported data
20
+ #
21
+ # @since 0.1.0
22
+ def initialize(data:, decode:)
23
+ @data = decode ? decode_data(data) : data
24
+ end
25
+
26
+ Contract None => Or[Contracts::HashOf[Symbol, Maybe[String]], String]
27
+ # Performing import of data
28
+ #
29
+ # @return [Hash] imported data
30
+ #
31
+ # @since 0.1.0
32
+ def perform
33
+ data
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'callable'
4
+ require_relative 'ciphers'
5
+ require_relative 'helpers'
6
+
7
+ require_relative 'exporter'
8
+ require_relative 'exporter/function_exporter'
9
+ require_relative 'exporter/file_exporter'
10
+ require_relative 'exporter/stdout_exporter'
11
+
12
+ require_relative 'importer/function_importer'
13
+ require_relative 'importer/file_importer'
14
+
15
+ require_relative 'keys_generator'
16
+ require_relative 'encrypter'
17
+ require_relative 'decrypter'
18
+
19
+ require_relative 'openssl/private_key/default/encrypter'
20
+ require_relative 'openssl/private_key/auth/encrypter'
21
+ require_relative 'openssl/private_key/auth/ccm/encrypter'
22
+
23
+ require_relative 'openssl/private_key/default/decrypter'
24
+ require_relative 'openssl/private_key/auth/decrypter'
25
+ require_relative 'openssl/private_key/auth/ccm/decrypter'
26
+
27
+ require_relative 'openssl/public_key/rsa/keys_generator'
28
+ require_relative 'openssl/public_key/rsa/encrypter'
29
+ require_relative 'openssl/public_key/rsa/decrypter'
30
+
31
+ require_relative 'openssl/public_key/ec/keys_generator'
32
+ require_relative 'openssl/public_key/ec/encrypter'
33
+ require_relative 'openssl/public_key/ec/decrypter'
34
+
35
+ require_relative 'rbnacl/encrypter'
36
+ require_relative 'rbnacl/decrypter'
37
+
38
+ require_relative 'rbnacl/private_key/encrypter'
39
+ require_relative 'rbnacl/private_key/decrypter'
40
+
41
+ require_relative 'rbnacl/public_key/keys_generator'
42
+ require_relative 'rbnacl/public_key/encrypter'
43
+ require_relative 'rbnacl/public_key/decrypter'
44
+
45
+ require_relative 'action_call_selector'
46
+ require_relative 'action_call_options_selector'
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Izokatu
4
+ # Abstract class for keys generators
5
+ #
6
+ # @abstract Subclasses are containing implementation of {#generate_private_key} and {#generate_public_key}
7
+ class KeysGenerator
8
+ extend Callable
9
+
10
+ include Contracts
11
+
12
+ # Default key symbols for RbNaCl and OpenSSL keys
13
+ KEYS_SYMBOLS = %i[private_key public_key].freeze
14
+ # Classes of RbNaCl and OpenSSL keys
15
+ KEY_CLASSES = [
16
+ RbNaCl::Boxes::Curve25519XSalsa20Poly1305::PrivateKey,
17
+ RbNaCl::Boxes::Curve25519XSalsa20Poly1305::PublicKey,
18
+ OpenSSL::PKey::RSA,
19
+ OpenSSL::PKey::EC
20
+ ].freeze
21
+
22
+ Contract None => Contracts::HashOf[Or[*KEYS_SYMBOLS], Or[*KEY_CLASSES]]
23
+ # Performing generation of private and public keys
24
+ #
25
+ # @return [Hash] public and private keys
26
+ #
27
+ # @since 0.1.0
28
+ def perform
29
+ generate_private_key
30
+ .then { |private_key| { private_key: private_key, public_key: generate_public_key(private_key) } }
31
+ end
32
+
33
+ private
34
+
35
+ # Performing generation of private key
36
+ #
37
+ # @raise RuntimeError
38
+ #
39
+ # @since 0.1.0
40
+ def generate_private_key
41
+ raise 'Not implemented!'
42
+ end
43
+
44
+ # Performing generation of public key
45
+ #
46
+ # @raise RuntimeError
47
+ #
48
+ # @since 0.1.0
49
+ def generate_public_key(_private_key)
50
+ raise 'Not implemented!'
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Izokatu
4
+ module Openssl
5
+ module PrivateKey
6
+ module Auth
7
+ # Namespace for OpenSSL private key authenticated CCM classes
8
+ module CCM
9
+ # OpenSSL private key decrypter for authenticated ciphers in CCM mode
10
+ class Decrypter < Izokatu::Openssl::PrivateKey::Auth::Decrypter
11
+ # Default iv (nonce) length for ciphers in CCM mode
12
+ DEFAULT_CCM_IV_LENGTH = 7
13
+ # Default authentication tag length for ciphers in CCM mode
14
+ DEFAULT_CCM_AUTH_TAG_LENGTH = 8
15
+
16
+ private
17
+
18
+ Contract None => Any
19
+ # Initializing decrypter params
20
+ #
21
+ # @since 0.1.0
22
+ def initialize_decrypter_params!
23
+ initialize_auth_ccm_decrypter_params!
24
+ super
25
+ end
26
+
27
+ Contract None => Any
28
+ # Set length for iv and auth_tag before their initialization
29
+ #
30
+ # @since 0.1.0
31
+ def initialize_auth_ccm_decrypter_params!
32
+ decrypter.iv_len = DEFAULT_CCM_IV_LENGTH
33
+ decrypter.auth_tag_len = DEFAULT_CCM_AUTH_TAG_LENGTH
34
+ end
35
+
36
+ Contract None => Any
37
+ # Initializing decrypter auth params
38
+ #
39
+ # @since 0.1.0
40
+ def initialize_auth_decrypter_params!
41
+ initialize_static_message_length!
42
+ super
43
+ end
44
+
45
+ Contract None => Any
46
+ # Initializing message length for cipher in CCM mode
47
+ #
48
+ # @since 0.1.0
49
+ def initialize_static_message_length!
50
+ decrypter.ccm_data_len = encrypted_data.length unless cipher.include?('ARIA')
51
+ end
52
+
53
+ # Raising exception if auth tag is truncated
54
+ # @note Redefined to use local auth tag length
55
+ #
56
+ # @raise RuntimeError
57
+ #
58
+ # @since 0.1.0
59
+ def verify_tag_size!
60
+ raise 'tag is truncated!' unless auth_tag.bytesize == DEFAULT_CCM_AUTH_TAG_LENGTH
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Izokatu
4
+ module Openssl
5
+ module PrivateKey
6
+ module Auth
7
+ module CCM
8
+ # OpenSSL private key encrypter for authenticated ciphers in CCM mode
9
+ class Encrypter < Izokatu::Openssl::PrivateKey::Auth::Encrypter
10
+ # Default iv (nonce) length for ciphers in CCM mode
11
+ DEFAULT_CCM_IV_LENGTH = 7
12
+ # Default authentication tag length for ciphers in CCM mode
13
+ DEFAULT_CCM_AUTH_TAG_LENGTH = 8
14
+
15
+ private
16
+
17
+ Contract None => Any
18
+ # Initializing encrypter params
19
+ #
20
+ # @since 0.1.0
21
+ def initialize_encrypter_params!
22
+ initialize_auth_ccm_encrypter_params!
23
+ super
24
+ end
25
+
26
+ Contract None => Any
27
+ # Set length for iv and auth_tag before their initialization
28
+ #
29
+ # @since 0.1.0
30
+ def initialize_auth_ccm_encrypter_params!
31
+ encrypter.iv_len = DEFAULT_CCM_IV_LENGTH
32
+ encrypter.auth_tag_len = DEFAULT_CCM_AUTH_TAG_LENGTH
33
+ end
34
+
35
+ Contract Maybe[String] => Any
36
+ # Initializing encrypter auth params
37
+ #
38
+ # @since 0.1.0
39
+ def initialize_auth_encrypter_params!(auth_data)
40
+ initialize_static_message_length!
41
+ super
42
+ end
43
+
44
+ Contract None => Any
45
+ # Initializing message length for cipher in CCM mode
46
+ #
47
+ # @since 0.1.0
48
+ def initialize_static_message_length!
49
+ encrypter.ccm_data_len = clear_data.length unless cipher.include?('ARIA')
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Izokatu
4
+ module Openssl
5
+ module PrivateKey
6
+ # Namespace for OpenSSL private key authenticated classes
7
+ module Auth
8
+ # OpenSSL private key decrypter for authenticated ciphers
9
+ class Decrypter < Izokatu::Openssl::PrivateKey::Default::Decrypter
10
+ # @return [String] authenticated data
11
+ attr_reader :auth_data
12
+ # @return [String] authentication tag
13
+ attr_reader :auth_tag
14
+
15
+ # Default Openssl::PrivateKey::Auth::Decrypter option
16
+ DEFAULT_AUTH_TAG_LENGTH = 16
17
+
18
+ Contract Contracts::HashOf[Symbol, Maybe[String]] => Any
19
+ # Initializing options for OpenSSL EC decryption
20
+ #
21
+ # @param encrypted_data (#encrypted_data)
22
+ # @param cipher (#cipher)
23
+ # @param key (#key)
24
+ # @param nonce (#nonce)
25
+ # @param auth_data (#auth_data)
26
+ # @param auth_tag (#auth_tag)
27
+ #
28
+ # @since 0.1.0
29
+ def initialize(auth_data:, auth_tag:, cipher:, encrypted_data:, key:, nonce:)
30
+ @auth_data = auth_data
31
+ @auth_tag = auth_tag
32
+ super(cipher: cipher, encrypted_data: encrypted_data, key: key, nonce: nonce)
33
+ end
34
+
35
+ Contract None => Contracts::HashOf[Symbol, String]
36
+ # Initializing decrypter
37
+ #
38
+ # @return [OpenSSL::Cipher] decrypter instance
39
+ #
40
+ # @since 0.1.0
41
+ def perform
42
+ verify_tag_size!
43
+ super
44
+ end
45
+
46
+ private
47
+
48
+ Contract None => Any
49
+ # Initializing decrypter params
50
+ #
51
+ # @since 0.1.0
52
+ def initialize_decrypter_params!
53
+ super
54
+ initialize_auth_decrypter_params!
55
+ end
56
+
57
+ Contract None => Any
58
+ # Initializing decrypter auth params
59
+ #
60
+ # @since 0.1.0
61
+ def initialize_auth_decrypter_params!
62
+ # unless added just for ARIA-***-CCM ciphers
63
+ decrypter.auth_data = auth_data unless cipher.include?('ARIA')
64
+ decrypter.auth_tag = auth_tag
65
+ end
66
+
67
+ # Raising exception if auth tag is truncated
68
+ #
69
+ # @raise RuntimeError
70
+ #
71
+ # @since 0.1.0
72
+ def verify_tag_size!
73
+ raise 'tag is truncated!' unless auth_tag.bytesize == DEFAULT_AUTH_TAG_LENGTH
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Izokatu
4
+ module Openssl
5
+ module PrivateKey
6
+ module Auth
7
+ # OpenSSL private key encrypter for authenticated ciphers
8
+ class Encrypter < Izokatu::Openssl::PrivateKey::Default::Encrypter
9
+ # @return [String] authenticated data
10
+ attr_reader :auth_data
11
+
12
+ # Default Openssl::PrivateKey::Auth::Encrypter option
13
+ DEFAULT_AUTH_OPTIONS = {
14
+ auth_data: ''
15
+ }.freeze
16
+
17
+ Contract Contracts::HashOf[Symbol, Maybe[String]] => Any
18
+ # Initializing options for OpenSSL EC encryption
19
+ #
20
+ # @param cipher (#cipher)
21
+ # @param clear_data (#clear_data)
22
+ # @param auth_data (#auth_data)
23
+ #
24
+ # @since 0.1.0
25
+ def initialize(cipher:, clear_data:, auth_data:)
26
+ super(cipher: cipher, clear_data: clear_data)
27
+ initialize_auth_encrypter_params!(auth_data)
28
+ end
29
+
30
+ private
31
+
32
+ Contract Maybe[String] => Maybe[String]
33
+ # Initializing encrypter auth param
34
+ #
35
+ # @return [String] authenticated data
36
+ #
37
+ # @since 0.1.0
38
+ def initialize_auth_encrypter_params!(auth_data)
39
+ @auth_data = auth_data || DEFAULT_AUTH_OPTIONS[:auth_data]
40
+ encrypter.auth_data = @auth_data
41
+ end
42
+
43
+ Contract None => Contracts::ArrayOf[Contracts::HashOf[Symbol, String]]
44
+ # Encrypting data
45
+ #
46
+ # @return [Array] encrypted data with decrypter params
47
+ #
48
+ # @since 0.1.0
49
+ def encrypt_data!
50
+ encrypted_data, decrypter_params = super
51
+ decrypter_params[:auth_data] = auth_data
52
+ decrypter_params.merge!(compute_auth_tag!)
53
+ [encrypted_data, decrypter_params]
54
+ end
55
+
56
+ Contract None => Contracts::HashOf[Symbol, String]
57
+ # Computing authentication tag
58
+ #
59
+ # @return [Hash] authentication tag
60
+ #
61
+ # @since 0.1.0
62
+ def compute_auth_tag!
63
+ { auth_tag: encrypter.auth_tag }
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end