Skip to content

Commit a593cd0

Browse files
committed
Use OpenSSL cipher to get cipher input lengths
1 parent dedd02f commit a593cd0

2 files changed

Lines changed: 24 additions & 23 deletions

File tree

lib/net/ssh/authentication/ed25519.rb

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,26 +64,29 @@ def self.read(datafull, password)
6464

6565
len = buffer.read_long
6666

67-
keylen, blocksize, ivlen = CipherFactory.get_lengths(ciphername, iv_len: true)
68-
raise ArgumentError.new("Private key len:#{len} is not a multiple of #{blocksize}") if
69-
((len < blocksize) || ((blocksize > 0) && (len % blocksize) != 0))
70-
71-
if kdfname == 'bcrypt'
72-
salt = kdfopts.read_string
73-
rounds = kdfopts.read_long
74-
75-
raise "BCryptPbkdf is not implemented for jruby" if RUBY_PLATFORM == "java"
76-
77-
key = BCryptPbkdf::key(password, salt, keylen + ivlen, rounds)
78-
raise DecryptError.new("BCyryptPbkdf failed", encrypted_key: true) unless key
79-
else
80-
key = '\x00' * (keylen + ivlen)
81-
end
82-
8367
if ciphername == 'none'
8468
cipher = Transport::IdentityCipher
8569
else
8670
cipher = OpenSSL::Cipher.new(CipherFactory::SSH_TO_OSSL[ciphername])
71+
keylen = cipher.key_len
72+
ivlen = cipher.iv_len
73+
blocksize = cipher.block_size
74+
75+
raise ArgumentError.new("Private key len:#{len} is not a multiple of #{blocksize}") if
76+
((len < blocksize) || ((blocksize > 0) && (len % blocksize) != 0))
77+
78+
if kdfname == 'bcrypt'
79+
salt = kdfopts.read_string
80+
rounds = kdfopts.read_long
81+
82+
raise "BCryptPbkdf is not implemented for jruby" if RUBY_PLATFORM == "java"
83+
84+
key = BCryptPbkdf::key(password, salt, keylen + ivlen, rounds)
85+
raise DecryptError.new("BCryptPbkdf failed", encrypted_key: true) unless key
86+
else
87+
key = '\x00' * (keylen + ivlen)
88+
end
89+
8790
cipher.decrypt
8891
cipher.key = key[0...keylen]
8992
cipher.iv = key[keylen...keylen + ivlen]
@@ -94,14 +97,12 @@ def self.read(datafull, password)
9497

9598
# TODO: test with chacha poly
9699
decoded = if cipher.authenticated?
97-
# tested with GCM
98100
ciphertext = encrypted_data[0...-16]
99101
auth_tag = encrypted_data[-16..]
100102
cipher.auth_tag = auth_tag
101103
cipher.auth_data = ''
102104
cipher.update(ciphertext)
103105
else
104-
# tested with CBC
105106
cipher.update(encrypted_data)
106107
end
107108

test/authentication/test_open_ssh_private_key_loader.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ def test_chacha20_poly1305_key
4949
key = <<~PRIVATEKEY
5050
-----BEGIN OPENSSH PRIVATE KEY-----
5151
b3BlbnNzaC1rZXktdjEAAAAAHWNoYWNoYTIwLXBvbHkxMzA1QG9wZW5zc2guY29tAAAABm
52-
JjcnlwdAAAABgAAAAQYf8G9VsDZqFN+GKW7A0XewAAABgAAAABAAAAMwAAAAtzc2gtZWQy
53-
NTUxOQAAACAF+rfLEozMyDub+8gOsb+WssHKSzh+5ffWiyKC3efLfQAAAJDcniGJhUGXaK
54-
A7v6DMkskqZA70Sqs1Pjz4ahZ6uBXImAHk04VYskUkcD9FW6GkevWrQA69stLQcmMvuS1Y
55-
AyrWopSzwJ0HEZj55b5mnjH8Iob0jPVjAuf5vtjfFeb/rncVgprs6AtEVItgIwZ+LoJTLN
56-
yytSz1DNyO0oyZiowww6RMmr3lNjPHNtB71X4XZ3jDo7ySUF24MKmdsPiOjc+R
52+
JjcnlwdAAAABgAAAAQgMsN42jlw2C+pMgTPx+suAAAABgAAAABAAAAMwAAAAtzc2gtZWQy
53+
NTUxOQAAACCHThbU/SJU7ntvbok6ANB0ob4Q36gXQxUj40PDGJGw4AAAAJADmcQtG5SDxI
54+
srhPwRMOUvwK3niQ6R/vxuHrAXiCt9oMymG2ALOmt08no/MVgxeQwKGGFgSzVjFaq6Nyzg
55+
yWA5df/AxUK72z7cqUaGzyMWQ+N4pC1q5pOINIiDxtjUTgo2Nv3ZbNV8EBGeDYX95iTN5G
56+
YHeAFEd6hZKLOSMUDcKdj1vkZClWTHZBNJtIg4a4ZlQ8/mSJCf7TBv9z1ibaOh
5757
-----END OPENSSH PRIVATE KEY-----
5858
PRIVATEKEY
5959
pwd = 'test'

0 commit comments

Comments
 (0)