Module: CcipherFactory::KDF::HKDF

Includes:
Common, TR::CondUtils, TR::DataConvUtils
Defined in:
lib/ccipher_factory/kdf/hkdf.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#attachedDigestObject

Returns the value of attribute attachedDigest.



16
17
18
# File 'lib/ccipher_factory/kdf/hkdf.rb', line 16

def attachedDigest
  @attachedDigest
end

#attachedValueObject

Returns the value of attribute attachedValue.



16
17
18
# File 'lib/ccipher_factory/kdf/hkdf.rb', line 16

def attachedValue
  @attachedValue
end

#derivedValObject (readonly)

Returns the value of attribute derivedVal.



17
18
19
# File 'lib/ccipher_factory/kdf/hkdf.rb', line 17

def derivedVal
  @derivedVal
end

#digestAlgoObject

Returns the value of attribute digestAlgo.



15
16
17
# File 'lib/ccipher_factory/kdf/hkdf.rb', line 15

def digestAlgo
  @digestAlgo
end

#outByteLengthObject

Returns the value of attribute outByteLength.



14
15
16
# File 'lib/ccipher_factory/kdf/hkdf.rb', line 14

def outByteLength
  @outByteLength
end

#saltObject

Returns the value of attribute salt.



14
15
16
# File 'lib/ccipher_factory/kdf/hkdf.rb', line 14

def salt
  @salt
end

Instance Method Details

#derive_finalObject

Raises:



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
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ccipher_factory/kdf/hkdf.rb', line 44

def derive_final

  raise KDFError, "outByteLength is required" if is_empty?(@outByteLength)

  digest = nil
  digestId = nil
  #if not_empty?(@digest)

  #  case @digest
  #  when String, Symbol
  #    logger.tdebug :hkdf_derive_final, "Given digest object is a string/symbol #{@digest}"
  #    @digestAlgo = @digest

  #  #when OpenSSL::Digest
  #  #  digestId = Digest.to_digest_string(@digest.name)
  #  #  logger.tdebug :hkdf_derive_final, "Using user given OpenSSL digest object #{@digest}"
  #  #  digest = @digest

  #  #when CcipherFactory::Digest
  #  #  digestId = @digest.algo

  #  #  logger.tdebug :hkdf_derive_final, "Using user given CipherFactory digest #{@digest}"
  #  #  digest = OpenSSL::Digest.new(digestId)

  #  else
  #    raise KDFError, "Digest object #{@digest.class} is not supported"
  #  end

  #end

  if is_empty?(@digestAlgo)
    digestVal = CcipherFactory::Digest::SupportedDigest.instance.default_digest
    digestId = digestVal

    logger.tdebug :hkdf_derive_final, "digest algo is nil. Using default digest #{digestVal}"

  else
    raise KDFError, "Given digest algo '#{@digestAlgo}' is not supported" if not Digest::SupportedDigest.instance.is_supported?(@digestAlgo)

    logger.tdebug :hkdf_derive_final, "Using user given digest algo #{@digestAlgo}"

    digestId = @digestAlgo
  end

  @info = "" if @info.nil?

  hconf = Ccrypto::HKDFConfig.new
  hconf.digest = digestId
  hconf.salt = @salt
  hconf.info = @info
  hconf.outBitLength = @outByteLength*8

  hkdf = Ccrypto::AlgoFactory.engine(hconf)

  @derivedVal = hkdf.derive(intOutputBuf.bytes)

  write_to_output(@derivedVal) if is_output_given?

  ts = BinStruct.instance.struct(:kdf_hkdf)
  ts.digest = BTag.constant_value(digestId)
  ts.salt = @salt
  ts.outByteLength = @outByteLength
  if is_bool?(@attachedDigest) and @attachedDigest
    ts.value = @derivedVal
  else
    ts.value = ""
  end
  ts.encoded

end

#derive_init(*args, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ccipher_factory/kdf/hkdf.rb', line 18

def derive_init(*args, &block)

  len = args.first
  @outByteLength = len/8 if not_empty?(len)

  @salt = SecureRandom.random_bytes(@outByteLength) if is_empty?(@salt)

  if is_empty?(@attachedValue)
    @attachedDigest = false if is_empty?(@attachedDigest)
  else
    @attachedDigest = true
  end

  if block
    instance_eval(&block)
    derive_final
  else
    self
  end

end

#derive_update(val) ⇒ Object



40
41
42
# File 'lib/ccipher_factory/kdf/hkdf.rb', line 40

def derive_update(val)
  intOutputBuf.write(val)
end

#is_attached_mode?Boolean



115
116
117
118
119
120
121
# File 'lib/ccipher_factory/kdf/hkdf.rb', line 115

def is_attached_mode?
  if is_empty?(@attachedValue) 
    @attachedDigest
  else
    true
  end
end