summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRyosuke Yamazaki <ryosuke.yamazaki@mac.com>2011-12-16 17:03:07 +0900
committerRyosuke Yamazaki <ryosuke.yamazaki@mac.com>2011-12-16 17:11:29 +0900
commit4339264156536df1466c4d8433592d1e06cf7f54 (patch)
treedb59a2c80f5d9561e57a33806ccff0d05b9090f9 /lib
parentafbc2e42a09e51c9b7c52a4bc9fa744326143932 (diff)
downloadnet-ssh-4339264156536df1466c4d8433592d1e06cf7f54.tar.gz
cleanup.
Diffstat (limited to 'lib')
-rw-r--r--lib/net/ssh/transport/cipher_factory.rb24
-rw-r--r--lib/net/ssh/transport/hmac.rb25
-rw-r--r--lib/net/ssh/transport/key_expander.rb26
3 files changed, 31 insertions, 44 deletions
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