Module: CcipherFactory::SymKeyCipher::SymKeyDecrypt

Includes:
Common, Compression::CompressionHelper, TR::CondUtils
Defined in:
lib/ccipher_factory/symkey_cipher/symkey_decrypt.rb

Defined Under Namespace

Classes: SymKeyDecryptError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Compression::CompressionHelper

#compress_data_if_active, #compression_off, #compression_on, #compressor, #decompress_data_if_active, #decompressor, #decompressor_from_encoded, #encode_null_compressor, #is_compression_on?

Methods included from Common

#attach_mode, #cleanup_intOutputBuf, #cleanup_intOutputFile, #detach_mode, #disposeOutput, #intOutputBuf, #intOutputFile, #is_attach_mode?, #is_output_given?, #output, #output_obj, #sanitize_symbol, #write_to_output

Instance Attribute Details

#keyObject

Returns the value of attribute key.



13
14
15
# File 'lib/ccipher_factory/symkey_cipher/symkey_decrypt.rb', line 13

def key
  @key
end

Instance Method Details

#decrypt_finalObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/ccipher_factory/symkey_cipher/symkey_decrypt.rb', line 112

def decrypt_final

  begin
    dec = @cipher.final 
    logger.debug "Final length : #{dec.length}"
    res = decompress_data_if_active(dec)
    write_to_output(res)
  rescue Ccrypto::CipherEngineException => ex
    raise SymKeyDecryptionError, ex
  end

  @cipher = nil

  @key = nil
  # this is to clear up the cipher object from memory 
  # including key and IV value
  # Tested with aes-finder utility on ruby 3.0.2
  # https://github.com/mmozeiko/aes-finder
  GC.start

end

#decrypt_init(*args, &block) ⇒ Object

Raises:



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ccipher_factory/symkey_cipher/symkey_decrypt.rb', line 19

def decrypt_init(*args, &block)

  #@decKey = args.first
  raise SymKeyDecryptError, "Decryption key is required" if is_empty?(@key)

  if block
    instance_eval(&block)
    decrypt_final
  else
    self
  end

end

#decrypt_update_cipher(val) ⇒ Object

Raises:



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/ccipher_factory/symkey_cipher/symkey_decrypt.rb', line 78

def decrypt_update_cipher(val)

  raise SymKeyCipherError, "Please call update_meta() first before update_cipher()" if @cipher.nil?
  
  logger.debug "Given cipher data : #{val.length}"

  dec = @cipher.update(val)


  if not_empty?(dec) and dec.length > 0

    logger.debug "After cipher before compression check : #{dec.length}"
    res = decompress_data_if_active(dec)
    write_to_output(res)

    #if @decompressor.nil?
    #  dc = dec
    #else
    #  begin
    #    dc = @decompressor.decompress_update(dec)
    #  rescue Zlib::Error => ex
    #    raise SymKeyDecryptionError, "Data decompression failed: #{ex.message}"
    #  end
    #end

    #write_to_output(dc)

  else

    logger.debug "Cipher update returns nothing"
  end

end

#decrypt_update_meta(val) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ccipher_factory/symkey_cipher/symkey_decrypt.rb', line 33

def decrypt_update_meta(val)

  intOutputBuf.write(val)
  begin
    Encoding.extract_meta(intOutputBuf) do |meta, bal|

      ts = BinStruct.instance.struct_from_bin(meta)
      @mode = BTag.value_constant(ts.mode)
      iv = ts.iv
      comp = ts.compression

      cts = BinStruct.instance.struct_from_bin(comp)
      if cts.oid == BTag.constant_value(:compression_zlib)
        @decompressor = CcipherFactory::Compression::Compressor.new
        @decompressor.decompress
        @decompressor.decompress_init
        @decompressor.decompress_update_meta(comp)

        compression_on
        logger.tdebug :symkey_dec, "Compression is active"
      else
        compression_off
        logger.tdebug :symkey_dec, "Compression is NOT active"
      end

      authTag = ts.auth_tag

      algoDef = SymKeyCipher.algo_default(@key.keytype)

      cconf = Ccrypto::DirectCipherConfig.new({ algo: @key.keytype, keysize: @key.keysize, mode: @mode, padding: :pkcs5 })
      cconf.cipherOps = :decrypt
      cconf.key = @key.key
      cconf.iv = iv if not_empty?(iv)
      cconf.auth_tag = authTag if cconf.respond_to?(:auth_tag=)
      @cipher = Ccrypto::AlgoFactory.engine(cconf)

      @cipher

    end
  rescue Encoding::InsufficientData => e
  end


end

#initObject



15
16
17
# File 'lib/ccipher_factory/symkey_cipher/symkey_decrypt.rb', line 15

def init

end

#loggerObject



134
135
136
137
138
139
140
# File 'lib/ccipher_factory/symkey_cipher/symkey_decrypt.rb', line 134

def logger
  if @logger.nil?
    @logger = Tlogger.new
    @logger.tag = :symkey_dec
  end
  @logger
end