Class: CcipherBox::Keybox

Inherits:
Object
  • Object
show all
Includes:
TR::CondUtils
Defined in:
lib/ccipher_box/keybox.rb

Overview

Abstraction of deriving a key from a given base key (baseMat) and the Key Derivation Function (KDF). With the two inputs, a derive key would be provided via dkey() API

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kdfEngine = nil) ⇒ Keybox

Returns a new instance of Keybox.

Raises:



14
15
16
17
18
19
20
21
22
# File 'lib/ccipher_box/keybox.rb', line 14

def initialize(kdfEngine = nil)
  raise KeyboxException, "Instance of KDF engine (CcipherFactory::KDF::KDFEngine) required" if not_empty?(kdfEngine) and not kdfEngine.is_a?(CcipherFactory::KDF::KDFEngine)

  @kdfEng = kdfEngine
  if not @kdfEng.nil?
    @kdfOut = MemBuf.new
    @kdfEng.output(@kdfOut)
  end
end

Instance Attribute Details

#baseMatObject

Returns the value of attribute baseMat.



12
13
14
# File 'lib/ccipher_box/keybox.rb', line 12

def baseMat
  @baseMat
end

#kdfObject

Returns the value of attribute kdf.



12
13
14
# File 'lib/ccipher_box/keybox.rb', line 12

def kdf
  @kdf
end

#outBitLengthObject

Returns the value of attribute outBitLength.



12
13
14
# File 'lib/ccipher_box/keybox.rb', line 12

def outBitLength
  @outBitLength
end

Class Method Details

.from_encoded(bin) ⇒ Object

Raises:



24
25
26
27
28
29
30
31
32
# File 'lib/ccipher_box/keybox.rb', line 24

def self.from_encoded(bin)
  
  raise KeyboxException, "Given binary is empty" if is_empty?(bin)

  st = BinStruct.instance.struct_from_bin(bin)
  kdfEng = CcipherFactory::KDF.from_encoded(st.kdfConfig)
  Keybox.new(kdfEng)

end

Instance Method Details

#dkeyObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ccipher_box/keybox.rb', line 66

def dkey

  if @dkeyVal.nil?

    raise KeyboxException, "BaseMat not given" if is_empty?(@baseMat)

    kdfEng.derive_update(@baseMat)

    @kdfConfig = kdfEng.derive_final

    @dkeyVal = @kdfOut.bytes.clone
    @kdfOut.dispose

  end

  @dkeyVal

end

#encodedObject



85
86
87
88
89
# File 'lib/ccipher_box/keybox.rb', line 85

def encoded
  st = BinStruct.instance.struct(:keybox)
  st.kdfConfig = @kdfConfig
  st.encoded
end