Class: Ccrypto::Ruby::CipherEngine

Inherits:
Object
  • Object
show all
Includes:
DataConversion, TR::CondUtils, TeLogger::TeLogHelper
Defined in:
lib/ccrypto/ruby/engines/cipher_engine.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DataConversion

#from_b64, #from_hex, included, #to_b64, #to_hex, #to_int_array

Constructor Details

#initialize(*args, &block) ⇒ CipherEngine

Returns a new instance of CipherEngine.

[View source]

61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ccrypto/ruby/engines/cipher_engine.rb', line 61

def initialize(*args, &block)
  @spec = args.first

  #teLogger = TteLogger.new
  teLogger.debug "Cipher spec : #{@spec}"

  begin
    case @spec
    #when String
    #  @cipher = OpenSSL::Cipher.new(@spec)
    when Ccrypto::CipherEngineConfig
      @cipher = OpenSSL::Cipher.new(@spec.provider_config)
    when Ccrypto::DirectCipherConfig
      @cipher = OpenSSL::Cipher.new(self.class.to_openssl_spec(@spec))
    else
      raise Ccrypto::CipherEngineException, "Not supported cipher init type #{@spec.class}"
    end
  rescue OpenSSL::Cipher::CipherError, RuntimeError => ex
    raise Ccrypto::CipherEngineException, ex
  end

  case @spec.cipherOps
  when :encrypt, :enc
    teLogger.debug "Operation encrypt"
    @cipher.encrypt
  when :decrypt, :dec
    teLogger.debug "Operation decrypt"
    @cipher.decrypt
  else
    raise Ccrypto::CipherEngineException, "Cipher operation (encrypt/decrypt) must be given"
  end


  if @spec.has_iv?
    teLogger.debug "IV from spec"
    @cipher.iv = @spec.iv
    teLogger.debug "IV : #{to_hex(@spec.iv)}"
  else
    teLogger.debug "Generate random IV"
    @spec.iv = @cipher.random_iv
    teLogger.debug "IV : #{to_hex(@spec.iv)}"
  end


  if @spec.has_key?
    teLogger.debug "Key from spec"
    case @spec.key
    when Ccrypto::SecretKey
      @cipher.key = @spec.key.to_bin
    when String
      @cipher.key = @spec.key
    else
      raise Ccrypto::CipherEngineException, "Unknown key type for processing #{@spec.key}"
    end
  else
    teLogger.debug "Generate random Key"
    @spec.key = @cipher.random_key
  end


  if @spec.is_mode?(:gcm)

    if not_empty?(@spec.auth_data) 
      teLogger.debug "Setting auth data"
      @cipher.auth_data = @spec.auth_data
    end

    if not_empty?(@spec.auth_tag) 
      raise CipherEngineException, "Tag length of 16 bytes is expected" if @spec.auth_tag.bytesize != 16
      teLogger.debug "Setting auth tag"
      @cipher.auth_tag = @spec.auth_tag
    end

  end

end

Class Method Details

.is_supported_cipher?(c) ⇒ Boolean

Returns:

  • (Boolean)
[View source]

23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ccrypto/ruby/engines/cipher_engine.rb', line 23

def self.is_supported_cipher?(c)
  case c
  when String
    supported_ciphers.include?(c)  
  when Hash
    spec = to_openssl_spec(c)
    begin
      OpenSSL::Cipher.new(spec)
      true
    rescue Exception => ex
      false
    end
  else
    raise Ccrypto::CipherEngineException, "Unsupported input #{c} to check supported cipher"
  end
end

.supported_ciphersObject

[View source]

14
15
16
17
18
19
20
21
# File 'lib/ccrypto/ruby/engines/cipher_engine.rb', line 14

def self.supported_ciphers
  if @sCipher.nil?
    @sCipher = OpenSSL::Cipher.ciphers
  end

  @sCipher

end

.to_openssl_spec(spec) ⇒ Object

[View source]

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ccrypto/ruby/engines/cipher_engine.rb', line 40

def self.to_openssl_spec(spec)
  res = []

  teLogger.debug "to_openssl_spec #{spec}"
  case spec.algo
  when :blowfish
    res << "bf"
  else
    res << spec.algo
  end

  res << spec.keysize if not_empty?(spec.keysize) and spec.keysize.to_i > 0 and not spec.is_algo?(:chacha20) and not spec.is_algo?(:seed) and not spec.is_algo?(:sm4) and not spec.is_algo?(:blowfish)

  res << spec.mode 

  teLogger.debug "to_openssl_spec #{res}"

  res.join("-")
  
end

Instance Method Details

#final(val = nil) ⇒ Object

[View source]

142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/ccrypto/ruby/engines/cipher_engine.rb', line 142

def final(val = nil)
  res = []

  begin

    if not_empty?(val)
      res << @cipher.update(val)
    end

    res << @cipher.final

  rescue Exception => ex
    raise CipherEngineException, ex
  end

  if @spec.is_mode?(:gcm) and @spec.is_encrypt_cipher_mode?
    @spec.auth_tag = @cipher.auth_tag 
  end

  res.join
end

#resetObject

[View source]

164
165
166
# File 'lib/ccrypto/ruby/engines/cipher_engine.rb', line 164

def reset
  @cipher.reset
end

#update(val) ⇒ Object

[View source]

138
139
140
# File 'lib/ccrypto/ruby/engines/cipher_engine.rb', line 138

def update(val)
  @cipher.update(val) 
end