Skip to content

Commit f365b02

Browse files
committed
Add the NIST KDF
1 parent 9b9849b commit f365b02

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

lib/msf_autoload.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,9 @@ def custom_inflections
298298
'uds_errors' => 'UDSErrors',
299299
'smb_hash_capture' => 'SMBHashCapture',
300300
'rex_ntlm' => 'RexNTLM',
301-
'teamcity' => 'TeamCity'
301+
'teamcity' => 'TeamCity',
302+
'kdf' => 'KDF',
303+
'nist_sp_800_108_r1' => 'NIST_SP_800_108_R1'
302304
}
303305
end
304306

lib/rex/crypto/kdf.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module Rex::Crypto::KDF
2+
require 'rex/crypto/kdf/nist_sp_800_108_r1'
3+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require 'openssl'
2+
3+
module Rex::Crypto::KDF::NIST_SP_800_108_R1
4+
5+
def self.counter(secret, length, prf, keys: 1, label: ''.b, context: ''.b)
6+
key_block = ''
7+
8+
counter = 0
9+
while key_block.length < (length * keys)
10+
counter += 1
11+
raise RangeError.new("counter overflow") if counter > 0xffffffff
12+
13+
info = [ counter ].pack('L>') + label.force_encoding('ASCII-8BIT') + "\x00".b + context.force_encoding('ASCII-8BIT') + [ length * keys * 8 ].pack('L>')
14+
key_block << prf.call(secret, info)
15+
end
16+
17+
key_block.bytes.each_slice(length).to_a[...keys].map { |slice| slice.pack('C*') }
18+
end
19+
20+
def self.counter_hmac(secret, length, digest, keys: 1, label: ''.b, context: ''.b)
21+
prf = -> (key, data) { OpenSSL::HMAC.digest(digest, key, data) }
22+
counter(secret, length, prf, keys: keys, label: label, context: context)
23+
end
24+
25+
end

0 commit comments

Comments
 (0)