Module: Izokatu
- Extended by:
- Helpers
- Includes:
- Contracts
- Defined in:
- lib/izokatu.rb,
lib/izokatu/ciphers.rb,
lib/izokatu/helpers.rb,
lib/izokatu/version.rb,
lib/izokatu/callable.rb,
lib/izokatu/exporter.rb,
lib/izokatu/decrypter.rb,
lib/izokatu/encrypter.rb,
lib/izokatu/keys_generator.rb,
lib/izokatu/rbnacl/decrypter.rb,
lib/izokatu/rbnacl/encrypter.rb,
lib/izokatu/action_call_selector.rb,
lib/izokatu/exporter/file_exporter.rb,
lib/izokatu/importer/file_importer.rb,
lib/izokatu/exporter/stdout_exporter.rb,
lib/izokatu/exporter/function_exporter.rb,
lib/izokatu/importer/function_importer.rb,
lib/izokatu/rbnacl/public_key/decrypter.rb,
lib/izokatu/rbnacl/public_key/encrypter.rb,
lib/izokatu/action_call_options_selector.rb,
lib/izokatu/rbnacl/private_key/decrypter.rb,
lib/izokatu/rbnacl/private_key/encrypter.rb,
lib/izokatu/openssl/public_key/ec/decrypter.rb,
lib/izokatu/openssl/public_key/ec/encrypter.rb,
lib/izokatu/openssl/public_key/rsa/decrypter.rb,
lib/izokatu/openssl/public_key/rsa/encrypter.rb,
lib/izokatu/rbnacl/public_key/keys_generator.rb,
lib/izokatu/openssl/private_key/auth/decrypter.rb,
lib/izokatu/openssl/private_key/auth/encrypter.rb,
lib/izokatu/openssl/public_key/ec/keys_generator.rb,
lib/izokatu/openssl/private_key/default/decrypter.rb,
lib/izokatu/openssl/private_key/default/encrypter.rb,
lib/izokatu/openssl/public_key/rsa/keys_generator.rb,
lib/izokatu/openssl/private_key/auth/ccm/decrypter.rb,
lib/izokatu/openssl/private_key/auth/ccm/encrypter.rb
Overview
Main module to work with.
Defined Under Namespace
Modules: Callable, Helpers, Openssl, Rbnacl Classes: ActionCallOptionsSelector, ActionCallSelector, Decrypter, Encrypter, Exporter, FileExporter, FileImporter, FunctionExporter, FunctionImporter, KeysGenerator, StdoutExporter
Constant Summary collapse
- DEFAULT_OPTIONS =
Default options for Izokatu call
{ via: :rbnacl, mode: :private_key, action: :encryption, asym_cipher_type: :ec, exporter: :function, importer: :function, cipher: 'secp521r1', encrypted_data_filename: 'encrypted_data', decrypter_params_filename: 'decrypter_params', delete_imported: false }.freeze
- EXPORTER_MAPPING =
Map of exporters and their symbol aliases
{ function: FunctionExporter, file: FileExporter, stdout: StdoutExporter }.freeze
- IMPORTER_MAPPING =
Map of importers and their symbol aliases
{ function: FunctionImporter, file: FileImporter }.freeze
- VERSION =
Gem version
'0.1.2'
Constants included from Helpers
Helpers::EC_CIPHER, Helpers::KEY_SYMBOL, Helpers::RBNACL_KEY_CLASSES
Class Attribute Summary collapse
-
.action ⇒ Symbol
readonly
Action to execute.
-
.asym_cipher_type ⇒ Symbol
readonly
OpenSSL public key cipher type.
-
.cipher ⇒ String
readonly
OpenSSL private key cipher.
-
.exporter ⇒ Symbol
readonly
Alias of Izokatu exporter.
-
.importer ⇒ Symbol
readonly
Alias of Izokatu importer.
-
.mode ⇒ Symbol
readonly
Mode of encryption/decryption.
-
.options ⇒ Hash
readonly
Options from user merged with default.
-
.via ⇒ Symbol
readonly
Library used for encryption/decryption.
Class Method Summary collapse
-
.call(**options) ⇒ Hash
Public method to work with.
-
.ccm_cipher? ⇒ Bool
private
Verifying cipher mode is equal to CCM.
-
.cipher_authenticated? ⇒ Bool
private
Verifying cipher as authenticated.
-
.format_cipher(cipher) ⇒ String
private
Formatting name of OpenSSL private key ciphers.
-
.initialize!(options) ⇒ Object
private
Initializing Izokatu variables.
-
.izokatu_export(data:, params:, encode:) ⇒ Hash
private
Izokatu export function.
-
.merge_options!(options) ⇒ Hash
private
Merging user option with default.
-
.openssl_auth_exception? ⇒ Bool
private
Verifying cipher is not using CBC mode or equal to RC4-HMAC-MD5.
-
.perform ⇒ Hash
private
Verifying and processing merged options.
-
.process_izokatu_options! ⇒ Hash
private
Importing encrypted data, selecting action class with options to call, exporting result of call.
-
.select_action ⇒ Class
private
Selecting action class to be called, based on options.
-
.select_action_options(action_class) ⇒ Hash
private
Selecting options for action class, based on action class.
-
.select_exporter_class! ⇒ Class
private
Changing exporter options value from alias of exporter class to exporter class.
-
.select_importer_class! ⇒ Class
private
Changing importer options value from alias of importer class to importer class.
-
.unknown_cipher? ⇒ Bool
private
Verifying cipher is not from OpenSSL private key ciphers or EC ciphers.
-
.verify_exporter_class! ⇒ Object
private
Verifying exporter alias.
-
.verify_importer_class! ⇒ Object
private
Verifying importer alias.
-
.verify_izokatu_cipher! ⇒ Object
private
Verifying cipher.
-
.verify_izokatu_options! ⇒ Object
private
Verifying options value.
-
.wrap_cipher? ⇒ Bool
private
Verifying cipher is using WRAP mode.
Methods included from Helpers
decode_data, encode_data, export_data, export_decrypted!, export_encrypted!, generate_ec_keypair, generate_rbnacl_keypair, generate_rsa_keypair, import_data, import_encrypted!, import_encrypted_in_options!
Class Attribute Details
.action ⇒ Symbol (readonly)
Returns action to execute.
29 30 31 |
# File 'lib/izokatu.rb', line 29 def action @action end |
.asym_cipher_type ⇒ Symbol (readonly)
Returns OpenSSL public key cipher type.
31 32 33 |
# File 'lib/izokatu.rb', line 31 def asym_cipher_type @asym_cipher_type end |
.cipher ⇒ String (readonly)
also used for OpenSSL public key ec key generation
Returns OpenSSL private key cipher.
38 39 40 |
# File 'lib/izokatu.rb', line 38 def cipher @cipher end |
.exporter ⇒ Symbol (readonly)
Returns alias of Izokatu exporter.
33 34 35 |
# File 'lib/izokatu.rb', line 33 def exporter @exporter end |
.importer ⇒ Symbol (readonly)
Returns alias of Izokatu importer.
35 36 37 |
# File 'lib/izokatu.rb', line 35 def importer @importer end |
.mode ⇒ Symbol (readonly)
Returns mode of encryption/decryption.
27 28 29 |
# File 'lib/izokatu.rb', line 27 def mode @mode end |
.options ⇒ Hash (readonly)
Returns options from user merged with default.
40 41 42 |
# File 'lib/izokatu.rb', line 40 def @options end |
.via ⇒ Symbol (readonly)
Returns library used for encryption/decryption.
25 26 27 |
# File 'lib/izokatu.rb', line 25 def via @via end |
Class Method Details
.call(**options) ⇒ Hash
Public method to work with
171 172 173 174 |
# File 'lib/izokatu.rb', line 171 def call(**) initialize!() perform end |
.ccm_cipher? ⇒ Bool (private)
Verifying cipher mode is equal to CCM
390 391 392 |
# File 'lib/izokatu.rb', line 390 def ccm_cipher? cipher.include?('CCM') end |
.cipher_authenticated? ⇒ Bool (private)
passing EC ciphers as authenticated. Those ciphers used only for key generation
Verifying cipher as authenticated. If cipher is authenticated, authenticated tag will be computed from encrypted data.
402 403 404 405 406 |
# File 'lib/izokatu.rb', line 402 def cipher_authenticated? return true if Openssl::PBKEY_EC_CIPHERS.include?(cipher) OpenSSL::Cipher.new(cipher).encrypt.authenticated? && openssl_auth_exception? end |
.format_cipher(cipher) ⇒ String (private)
Formatting name of OpenSSL private key ciphers
217 218 219 |
# File 'lib/izokatu.rb', line 217 def format_cipher(cipher) Openssl::PKEY_CIPHERS.include?(cipher) ? cipher.upcase : cipher end |
.initialize!(options) ⇒ Object (private)
Initializing Izokatu variables
184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/izokatu.rb', line 184 def initialize!() = () @via = [:via] @mode = [:mode] @action = [:action] @asym_cipher_type = [:asym_cipher_type] @exporter = [:exporter] @importer = [:importer] @cipher = format_cipher([:cipher]) @options = end |
.izokatu_export(data:, params:, encode:) ⇒ Hash (private)
Izokatu export function
343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/izokatu.rb', line 343 def izokatu_export(data:, params:, encode:) # WTF: Somehow, even Contract Any => Any for this method is violated case action when :encryption export_encrypted!(encrypted_data: data, decrypter_params: params || {}, encode: encode) when :decryption export_decrypted!(decrypted_data: data, encode: false) else data end end |
.merge_options!(options) ⇒ Hash (private)
Merging user option with default
205 206 207 |
# File 'lib/izokatu.rb', line 205 def () ? DEFAULT_OPTIONS.merge() : DEFAULT_OPTIONS end |
.openssl_auth_exception? ⇒ Bool (private)
Verifying cipher is not using CBC mode or equal to RC4-HMAC-MD5. These conditions specifying ciphers which passing authenticated? check from OpenSSL, but are not authenticated.
415 416 417 418 419 420 421 422 423 |
# File 'lib/izokatu.rb', line 415 def openssl_auth_exception? # In tests of openssl gem, I don't saw assigment of auth_tag or auth_data for cbc ciphers, only padding # (https://github.com/ruby/openssl/blob/master/test/openssl/test_cipher.rb) # Get this error: # OpenSSL::Cipher::CipherError: retrieving the authentication tag failed: ctrl operation not implemented # If not assigning authentication tag, get this error: # ':in `iv_len=': cipher does not support AEAD (OpenSSL::Cipher::CipherError)' !cipher.include?('CBC') && cipher != 'RC4-HMAC-MD5' end |
.perform ⇒ Hash (private)
Verifying and processing merged options
227 228 229 230 231 232 233 234 235 |
# File 'lib/izokatu.rb', line 227 def perform verify_exporter_class! verify_importer_class! verify_izokatu_cipher! select_exporter_class! select_importer_class! end |
.process_izokatu_options! ⇒ Hash (private)
Importing encrypted data, selecting action class with options to call, exporting result of call
327 328 329 330 331 332 333 |
# File 'lib/izokatu.rb', line 327 def import_encrypted!(options: , decode: true) if action == :decryption action_class = select_action = (action_class) data, params = action_class.call(**) izokatu_export(data: data, params: params, encode: true) end |
.select_action ⇒ Class (private)
Selecting action class to be called, based on options
361 362 363 364 365 366 367 368 369 370 |
# File 'lib/izokatu.rb', line 361 def select_action ActionCallSelector.call( via: via, mode: mode, action: action, asym_cipher_type: asym_cipher_type, ccm_cipher: ccm_cipher?, auth_cipher: cipher_authenticated? ) end |
.select_action_options(action_class) ⇒ Hash (private)
Selecting options for action class, based on action class
380 381 382 |
# File 'lib/izokatu.rb', line 380 def (action_class) ActionCallOptionsSelector.call(action_class: action_class, options: ) end |
.select_exporter_class! ⇒ Class (private)
Changing exporter options value from alias of exporter class to exporter class
305 306 307 308 |
# File 'lib/izokatu.rb', line 305 def select_exporter_class! [:exporter] = EXPORTER_MAPPING[exporter] @exporter = [:exporter] end |
.select_importer_class! ⇒ Class (private)
Changing importer options value from alias of importer class to importer class
316 317 318 319 |
# File 'lib/izokatu.rb', line 316 def select_importer_class! [:importer] = IMPORTER_MAPPING[importer] @importer = [:importer] end |
.unknown_cipher? ⇒ Bool (private)
Verifying cipher is not from OpenSSL private key ciphers or EC ciphers
285 286 287 |
# File 'lib/izokatu.rb', line 285 def unknown_cipher? !Openssl::PKEY_CIPHERS.include?(cipher) && !Openssl::PBKEY_EC_CIPHERS.include?(cipher) end |
.verify_exporter_class! ⇒ Object (private)
Verifying exporter alias
254 255 256 |
# File 'lib/izokatu.rb', line 254 def verify_exporter_class! raise 'ERROR: Unknown exporter!' unless %i[stdout file function].include?(exporter) end |
.verify_importer_class! ⇒ Object (private)
Verifying importer alias
263 264 265 |
# File 'lib/izokatu.rb', line 263 def verify_importer_class! raise 'ERROR: Unknown importer!' unless %i[file function].include?(importer) end |
.verify_izokatu_cipher! ⇒ Object (private)
also raising exception if cipher is using unsupported WRAP mode
Verifying cipher
274 275 276 277 |
# File 'lib/izokatu.rb', line 274 def verify_izokatu_cipher! raise 'ERROR: Unknown cipher!' if unknown_cipher? raise 'ERROR: Wrap ciphers are not supported!' if wrap_cipher? end |
.verify_izokatu_options! ⇒ Object (private)
Verifying options value
242 243 244 245 246 247 |
# File 'lib/izokatu.rb', line 242 def raise 'ERROR: Unknown library!' unless %i[openssl rbnacl].include?(via) raise 'ERROR: Unknown mode!' unless %i[private_key public_key].include?(mode) raise 'ERROR: Unknown action!' unless %i[encryption decryption keys_generation].include?(action) raise 'ERROR: Unknown asym_cipher_type!' unless %i[ec rsa].include?(asym_cipher_type) end |
.wrap_cipher? ⇒ Bool (private)
Verifying cipher is using WRAP mode
295 296 297 |
# File 'lib/izokatu.rb', line 295 def wrap_cipher? cipher.include?('wrap') || cipher.include?('WRAP') end |