From 4339264156536df1466c4d8433592d1e06cf7f54 Mon Sep 17 00:00:00 2001 From: Ryosuke Yamazaki Date: Fri, 16 Dec 2011 17:03:07 +0900 Subject: cleanup. --- lib/net/ssh/transport/cipher_factory.rb | 24 +++--------------------- lib/net/ssh/transport/hmac.rb | 25 ++----------------------- lib/net/ssh/transport/key_expander.rb | 26 ++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 44 deletions(-) create mode 100644 lib/net/ssh/transport/key_expander.rb (limited to 'lib') diff --git a/lib/net/ssh/transport/cipher_factory.rb b/lib/net/ssh/transport/cipher_factory.rb index ae57f2c..4a07aa2 100644 --- a/lib/net/ssh/transport/cipher_factory.rb +++ b/lib/net/ssh/transport/cipher_factory.rb @@ -1,4 +1,5 @@ require 'openssl' +require 'net/ssh/transport/key_expander' require 'net/ssh/transport/identity_cipher' module Net; module SSH; module Transport @@ -50,10 +51,10 @@ module Net; module SSH; module Transport cipher.send(options[:encrypt] ? :encrypt : :decrypt) cipher.padding = 0 - cipher.iv = make_key(cipher.iv_len, options[:iv], options) if ossl_name != "rc4" + cipher.iv = Net::SSH::Transport::KeyExpander.expand_key(cipher.iv_len, options[:iv], options) if ossl_name != "rc4" key_len = KEY_LEN_OVERRIDE[name] || cipher.key_len cipher.key_len = key_len - cipher.key = make_key(key_len, options[:key], options) + cipher.key = Net::SSH::Transport::KeyExpander.expand_key(key_len, options[:key], options) cipher.update(" " * 1536) if ossl_name == "rc4" return cipher @@ -73,25 +74,6 @@ module Net; module SSH; module Transport return [key_len, ossl_name=="rc4" ? 8 : cipher.block_size] end - - private - - # Generate a key value in accordance with the SSH2 specification. - def self.make_key(bytes, start, options={}) - k = start[0, bytes] - - digester = options[:digester] or raise 'No digester supplied' - shared = options[:shared] or raise 'No shared secret supplied' - hash = options[:hash] or raise 'No hash supplied' - - while k.length < bytes - step = digester.digest(shared + hash + k) - bytes_needed = bytes - k.length - k << step[0, bytes_needed] - end - - return k - end end end; end; end diff --git a/lib/net/ssh/transport/hmac.rb b/lib/net/ssh/transport/hmac.rb index 1a2173d..8452046 100644 --- a/lib/net/ssh/transport/hmac.rb +++ b/lib/net/ssh/transport/hmac.rb @@ -1,3 +1,4 @@ +require 'net/ssh/transport/key_expander' require 'net/ssh/transport/hmac/md5' require 'net/ssh/transport/hmac/md5_96' require 'net/ssh/transport/hmac/sha1' @@ -30,7 +31,7 @@ module Net::SSH::Transport::HMAC # given, the new instance will be initialized with that key. def self.get(name, key="", parameters = {}) impl = MAP[name] or raise ArgumentError, "hmac not found: #{name.inspect}" - impl.new(make_key(impl.key_length, key, parameters)) + impl.new(Net::SSH::Transport::KeyExpander.expand_key(impl.key_length, key, parameters)) end # Retrieves the key length for the hmac of the given SSH type (+name+). @@ -38,26 +39,4 @@ module Net::SSH::Transport::HMAC impl = MAP[name] or raise ArgumentError, "hmac not found: #{name.inspect}" impl.key_length end - - # XXX copied - # Generate a key value in accordance with the SSH2 specification. - def self.make_key(bytes, start, options={}) - if bytes == 0 - return "" - end - - k = start[0, bytes] - - digester = options[:digester] or raise 'No digester supplied' - shared = options[:shared] or raise 'No shared secret supplied' - hash = options[:hash] or raise 'No hash supplied' - - while k.length < bytes - step = digester.digest(shared + hash + k) - bytes_needed = bytes - k.length - k << step[0, bytes_needed] - end - - return k - end end diff --git a/lib/net/ssh/transport/key_expander.rb b/lib/net/ssh/transport/key_expander.rb new file mode 100644 index 0000000..86e608d --- /dev/null +++ b/lib/net/ssh/transport/key_expander.rb @@ -0,0 +1,26 @@ +module Net; module SSH; module Transport + module KeyExpander + + # Generate a key value in accordance with the SSH2 specification. + # (RFC4253 7.2. "Output from Key Exchange") + def self.expand_key(bytes, start, options={}) + if bytes == 0 + return "" + end + + k = start[0, bytes] + + digester = options[:digester] or raise 'No digester supplied' + shared = options[:shared] or raise 'No shared secret supplied' + hash = options[:hash] or raise 'No hash supplied' + + while k.length < bytes + step = digester.digest(shared + hash + k) + bytes_needed = bytes - k.length + k << step[0, bytes_needed] + end + + return k + end +end +end; end; end -- cgit v1.2.1