Module: JOSE::JWA::ConcatKDF

Extended by:
ConcatKDF
Included in:
ConcatKDF
Defined in:
lib/jose/jwa/concat_kdf.rb

Instance Method Summary collapse

Instance Method Details

#kdf(hash, z, other_info, key_data_len = nil) ⇒ Object


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/jose/jwa/concat_kdf.rb', line 5

def kdf(hash, z, other_info, key_data_len = nil)
  if hash.is_a?(String)
    hash = OpenSSL::Digest.new(hash)
  end
  if key_data_len.nil?
    key_data_len = hash.digest('').bytesize * 8
  end
  if other_info.is_a?(Array)
    algorithm_id, party_u_info, party_v_info, supp_pub_info, supp_priv_info = other_info
    supp_pub_info ||= ''
    supp_priv_info ||= ''
    other_info = [
      algorithm_id.bytesize, algorithm_id,
      party_u_info.bytesize, party_u_info,
      party_v_info.bytesize, party_v_info,
      supp_pub_info,
      supp_priv_info
    ].pack('Na*Na*Na*a*a*')
  end
  hash_len = hash.digest('').bytesize * 8
  reps = (key_data_len / hash_len.to_f).ceil
  if reps == 1
    concatenation = [ 0, 0, 0, 1, z, other_info ].pack('C4a*a*')
    derived_key = [hash.digest(concatenation).unpack('B*')[0][0...key_data_len]].pack('B*')
    return derived_key
  elsif reps > 0xFFFFFFFF
    raise ArgumentError, "too many reps"
  else
    return derive_key(hash, 1, reps, key_data_len, [z, other_info].join, '')
  end
end