@@ -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
0 commit comments