Class: Ccrypto::CipherConfig

Inherits:
Object
  • Object
show all
Includes:
AlgoConfig, TR::CondUtils
Defined in:
lib/ccrypto/configs/cipher_config.rb

Instance Attribute Summary collapse

Attributes included from AlgoConfig

#provider_config

Instance Method Summary collapse

Methods included from AlgoConfig

include, #provider_info

Constructor Details

#initialize(algo, opts = { }, &block) ⇒ CipherConfig

Returns a new instance of CipherConfig.



29
30
31
32
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ccrypto/configs/cipher_config.rb', line 29

def initialize(algo, opts = {  }, &block)
  @algo = algo

  @logger = Tlogger.new
  @logger.tag = :cipher_conf

  @authMode = false
  @plaintext_length = 0
  @ciphertext_length = 0
  @min_input_length = -1
  @mandatory_Block_size = -1
  
  if not_empty?(opts) and opts.is_a?(Hash)
    @mode = opts[:mode]

    @authMode = opts[:authMode] || false
    #if is_mode?(:gcm)
    if @authMode
      self.extend CipherAuthMode
      @logger.debug "Extending auth mode"

      @auth_data = opts[:auth_data]
      @auth_tag = opts[:auth_tag]

    end

    @iv = opts[:iv] 
    @ivLength = opts[:ivLength] if is_empty?(@iv)

    @key = opts[:key]
    @keysize = opts[:keysize] if is_empty?(@key)

    @padding = opts[:padding]

    @cipherOps = opts[:cipherOps]

    @min_input_length = opts[:min_input_length] || -1 

    @mandatory_block_size = opts[:mandatory_block_size] || -1

  end

  #if block
  #  @mode = block.call(:mode)

  #  #if is_mode?(:gcm)
  #  if @authMode
  #    self.extend CipherAuthMode
  #    @logger.debug "Extending auth mode"

  #    @auth_data = block.call(:auth_data)
  #    @auth_tag = block.call(:auth_tag)
  #  end

  #  @iv = block.call(:iv)
  #  @ivLength = block.call(:ivLength) || 16 if @iv.nil?

  #  @key = block.call(:key)
  #  @keysize = block.call(:keysize) if @key.nil?

  #  @padding = block.call(:padding)

  #  @cipherOps = block.call(:cipherOps)

  #  @plaintext_length = 0
  #  @ciphertext_length = 0

  #  @min_input_length = opts[:min_input_length] || -1 

  #end

end

Instance Attribute Details

#algoObject

Returns the value of attribute algo.



13
14
15
# File 'lib/ccrypto/configs/cipher_config.rb', line 13

def algo
  @algo
end

#cipherOpsObject

Returns the value of attribute cipherOps.



16
17
18
# File 'lib/ccrypto/configs/cipher_config.rb', line 16

def cipherOps
  @cipherOps
end

#ciphertext_lengthObject

required by certain mode such as CCM



19
20
21
# File 'lib/ccrypto/configs/cipher_config.rb', line 19

def ciphertext_length
  @ciphertext_length
end

#ivObject

Returns the value of attribute iv.



15
16
17
# File 'lib/ccrypto/configs/cipher_config.rb', line 15

def iv
  @iv
end

#ivLengthObject

Returns the value of attribute ivLength.



15
16
17
# File 'lib/ccrypto/configs/cipher_config.rb', line 15

def ivLength
  @ivLength
end

#keyObject

Returns the value of attribute key.



13
14
15
# File 'lib/ccrypto/configs/cipher_config.rb', line 13

def key
  @key
end

#keysizeObject

Returns the value of attribute keysize.



14
15
16
# File 'lib/ccrypto/configs/cipher_config.rb', line 14

def keysize
  @keysize
end

#mandatory_block_sizeObject (readonly)

Use cases : openssl aes-128-xts only accepts input min 16 bytes other no padding mode aes128-wrap only works on block of 8 bytes



24
25
26
# File 'lib/ccrypto/configs/cipher_config.rb', line 24

def mandatory_block_size
  @mandatory_block_size
end

#min_input_lengthObject (readonly)

Use cases : openssl aes-128-xts only accepts input min 16 bytes other no padding mode aes128-wrap only works on block of 8 bytes



24
25
26
# File 'lib/ccrypto/configs/cipher_config.rb', line 24

def min_input_length
  @min_input_length
end

#modeObject

Returns the value of attribute mode.



14
15
16
# File 'lib/ccrypto/configs/cipher_config.rb', line 14

def mode
  @mode
end

#native_configObject

provider specific



27
28
29
# File 'lib/ccrypto/configs/cipher_config.rb', line 27

def native_config
  @native_config
end

#paddingObject

Returns the value of attribute padding.



14
15
16
# File 'lib/ccrypto/configs/cipher_config.rb', line 14

def padding
  @padding
end

#plaintext_lengthObject

required by certain mode such as CCM



19
20
21
# File 'lib/ccrypto/configs/cipher_config.rb', line 19

def plaintext_length
  @plaintext_length
end

Instance Method Details

#decrypt_cipher_modeObject



142
143
144
# File 'lib/ccrypto/configs/cipher_config.rb', line 142

def decrypt_cipher_mode
  @cipherOps = :decrypt
end

#encrypt_cipher_modeObject



130
131
132
# File 'lib/ccrypto/configs/cipher_config.rb', line 130

def encrypt_cipher_mode
  @cipherOps = :encrypt
end

#has_iv?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/ccrypto/configs/cipher_config.rb', line 102

def has_iv?
  not_empty?(@iv)
end

#has_key?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/ccrypto/configs/cipher_config.rb', line 106

def has_key?
  not_empty?(@key)
end

#is_algo?(algo) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
117
118
119
120
# File 'lib/ccrypto/configs/cipher_config.rb', line 114

def is_algo?(algo)
  if @algo.nil? or is_empty?(@algo)
    false
  else
    (@algo.to_s.downcase =~ /#{algo}/) != nil
  end
end

#is_auth_mode_cipher?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/ccrypto/configs/cipher_config.rb', line 110

def is_auth_mode_cipher?
  @authMode
end

#is_decrypt_cipher_mode?Boolean

Returns:

  • (Boolean)


145
146
147
148
149
150
151
152
# File 'lib/ccrypto/configs/cipher_config.rb', line 145

def is_decrypt_cipher_mode?
  case @cipherOps
  when :decrypt, :dec
    true
  else
    false
  end
end

#is_encrypt_cipher_mode?Boolean

Returns:

  • (Boolean)


133
134
135
136
137
138
139
140
# File 'lib/ccrypto/configs/cipher_config.rb', line 133

def is_encrypt_cipher_mode?
  case @cipherOps
  when :encrypt, :enc
    true
  else
    false
  end
end

#is_mode?(mode) ⇒ Boolean

Returns:

  • (Boolean)


122
123
124
125
126
127
128
# File 'lib/ccrypto/configs/cipher_config.rb', line 122

def is_mode?(mode)
  if @mode.nil? or is_empty?(@mode)
    false
  else
    (@mode.to_s.downcase =~ /#{mode.to_s}/) != nil
  end
end

#loggerObject



160
161
162
163
164
165
166
# File 'lib/ccrypto/configs/cipher_config.rb', line 160

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

#to_sObject



154
155
156
157
158
# File 'lib/ccrypto/configs/cipher_config.rb', line 154

def to_s
  res = [@algo, @keysize, @mode, @padding].reject { |v| is_empty?(v) }.join("-")
  "#{res} (#{@authMode})"
  #"#{@algo}-#{@keysize}-#{@mode}-#{@padding}"
end