Class: Ccrypto::CipherConfig

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AlgoConfig

include

Constructor Details

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

Returns a new instance of CipherConfig.



27
28
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
# File 'lib/ccrypto/configs/cipher_config.rb', line 27

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

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

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

    @iv_required = (@ivLength.nil? ? false : @ivLength.to_i > 0)

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

    @padding = opts[:padding]

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

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

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

    @provider_config = opts[:provider_config]
  end

end

Instance Attribute Details

#algoObject (readonly)

set while this config is initialize and should not be changed



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

def algo
  @algo
end

#ccrypto_keyObject

given later by the provider



10
11
12
# File 'lib/ccrypto/configs/cipher_config.rb', line 10

def ccrypto_key
  @ccrypto_key
end

#ivObject

given later by the provider



10
11
12
# File 'lib/ccrypto/configs/cipher_config.rb', line 10

def iv
  @iv
end

#ivLengthObject (readonly)

Returns the value of attribute ivLength.



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

def ivLength
  @ivLength
end

#key_configObject

construct a standard key config for key generation engine



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

def key_config
  @key_config
end

#keysizeObject (readonly)

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



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

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



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

def min_input_length
  @min_input_length
end

#modeObject (readonly)

set while this config is initialize and should not be changed



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

def mode
  @mode
end

#paddingObject (readonly)

set while this config is initialize and should not be changed



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

def padding
  @padding
end

#provider_configObject

provider specific



22
23
24
# File 'lib/ccrypto/configs/cipher_config.rb', line 22

def provider_config
  @provider_config
end

Instance Method Details

#<=>(val) ⇒ Object

enable sort



137
138
139
# File 'lib/ccrypto/configs/cipher_config.rb', line 137

def <=>(val)
  @algo <=> val.algo 
end

#decrypt_cipher_modeObject Also known as: set_decrypt_mode



123
124
125
# File 'lib/ccrypto/configs/cipher_config.rb', line 123

def decrypt_cipher_mode
  @cipherOps = :decrypt
end

#encrypt_cipher_modeObject Also known as: set_encrypt_mode



115
116
117
# File 'lib/ccrypto/configs/cipher_config.rb', line 115

def encrypt_cipher_mode
  @cipherOps = :encrypt
end

#has_iv?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/ccrypto/configs/cipher_config.rb', line 67

def has_iv?
  not_empty?(@iv)
end

#has_key?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/ccrypto/configs/cipher_config.rb', line 71

def has_key?
  not_empty?(@ccrypto_key)
end

#has_mandatory_block_size?Boolean

def has_fixed_auth_tag_length?

not_empty?(@fixed_auth_tag_length) and @fixed_auth_tag_length.to_i > -1

end

Returns:

  • (Boolean)


83
84
85
# File 'lib/ccrypto/configs/cipher_config.rb', line 83

def has_mandatory_block_size?
  not_empty?(@mandatory_block_size) and @mandatory_block_size.to_i > -1
end

#has_min_input_length?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/ccrypto/configs/cipher_config.rb', line 75

def has_min_input_length?
  not_empty?(@min_input_length) and @min_input_length.to_i > -1
end

#is_algo?(algo) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
94
95
96
97
# File 'lib/ccrypto/configs/cipher_config.rb', line 91

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)


87
88
89
# File 'lib/ccrypto/configs/cipher_config.rb', line 87

def is_auth_mode_cipher?
  @authMode == true
end

#is_decrypt_cipher_mode?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/ccrypto/configs/cipher_config.rb', line 127

def is_decrypt_cipher_mode?
  @cipherOps == :decrypt
end

#is_encrypt_cipher_mode?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/ccrypto/configs/cipher_config.rb', line 119

def is_encrypt_cipher_mode?
  @cipherOps == :encrypt
end

#is_mode?(mode) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
102
103
104
105
# File 'lib/ccrypto/configs/cipher_config.rb', line 99

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

#iv_required?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/ccrypto/configs/cipher_config.rb', line 63

def iv_required?
  @iv_required
end

#needs_ciphertext_length?Boolean

Returns:

  • (Boolean)


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

def needs_ciphertext_length?
  is_mode?(:ccm)
end

#needs_plaintext_length?Boolean

Returns:

  • (Boolean)


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

def needs_plaintext_length?
  is_mode?(:ccm)
end

#to_sObject



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

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