@@ -93,7 +93,8 @@ public Key Parse()
9393 cipherKey = keyData . Take ( 32 ) ;
9494 cipherIV = keyData . Take ( 32 , 16 ) ;
9595
96- hmac = new HMACSHA256 ( keyData . Take ( 48 , 32 ) ) ;
96+ var macKey = keyData . Take ( 48 , 32 ) ;
97+ hmac = new HMACSHA256 ( macKey ) ;
9798
9899 break ;
99100 case "2" :
@@ -102,14 +103,8 @@ public Key Parse()
102103 cipherKey = keyData . Take ( 32 ) ;
103104 cipherIV = new byte [ 16 ] ;
104105
105- using ( var sha1 = SHA1 . Create ( ) )
106- {
107- var part1 = Encoding . UTF8 . GetBytes ( "putty-private-key-file-mac-key" ) ;
108- var part2 = Encoding . UTF8 . GetBytes ( _passPhrase ) ;
109- _ = sha1 . TransformBlock ( part1 , 0 , part1 . Length , outputBuffer : null , 0 ) ;
110- _ = sha1 . TransformFinalBlock ( part2 , 0 , part2 . Length ) ;
111- hmac = new HMACSHA1 ( sha1 . Hash . Take ( 20 ) ) ;
112- }
106+ macKey = CryptoAbstraction . HashSHA1 ( Encoding . UTF8 . GetBytes ( "putty-private-key-file-mac-key" + _passPhrase ) ) . Take ( 20 ) ;
107+ hmac = new HMACSHA1 ( macKey ) ;
113108
114109 break ;
115110 default :
@@ -129,8 +124,8 @@ public Key Parse()
129124 hmac = new HMACSHA256 ( Array . Empty < byte > ( ) ) ;
130125 break ;
131126 case "2" :
132- var hash = CryptoAbstraction . HashSHA1 ( Encoding . UTF8 . GetBytes ( "putty-private-key-file-mac-key" ) ) ;
133- hmac = new HMACSHA1 ( hash ) ;
127+ var macKey = CryptoAbstraction . HashSHA1 ( Encoding . UTF8 . GetBytes ( "putty-private-key-file-mac-key" ) ) ;
128+ hmac = new HMACSHA1 ( macKey ) ;
134129 break ;
135130 default :
136131 throw new SshException ( "PuTTY key file version " + _version + " is not supported" ) ;
@@ -139,33 +134,33 @@ public Key Parse()
139134 privateKey = _data ;
140135 break ;
141136 default :
142- throw new SshException ( "encryption " + _encryptionType + " is not supported for PuTTY key file" ) ;
137+ throw new SshException ( "Encryption " + _encryptionType + " is not supported for PuTTY key file" ) ;
143138 }
144139
145- using ( var macData = new SshDataStream ( 256 ) )
140+ byte [ ] macData ;
141+ using ( var macStream = new SshDataStream ( 256 ) )
146142 {
147- macData . Write ( _algorithmName , Encoding . UTF8 ) ;
148- macData . Write ( _encryptionType , Encoding . UTF8 ) ;
149- macData . Write ( _comment , Encoding . UTF8 ) ;
150- macData . WriteBinary ( _publicKey ) ;
151- macData . WriteBinary ( privateKey ) ;
152- try
153- {
154- var encoded = hmac . ComputeHash ( macData . ToArray ( ) ) ;
143+ macStream . Write ( _algorithmName , Encoding . UTF8 ) ;
144+ macStream . Write ( _encryptionType , Encoding . UTF8 ) ;
145+ macStream . Write ( _comment , Encoding . UTF8 ) ;
146+ macStream . WriteBinary ( _publicKey ) ;
147+ macStream . WriteBinary ( privateKey ) ;
148+ macData = macStream . ToArray ( ) ;
149+ }
150+
151+ byte [ ] macValue ;
152+ using ( hmac )
153+ {
154+ macValue = hmac . ComputeHash ( macData ) ;
155+ }
155156#if NET
156- var reference = Convert . FromHexString ( _mac ) ;
157+ var reference = Convert . FromHexString ( _mac ) ;
157158#else
158- var reference = Org . BouncyCastle . Utilities . Encoders . Hex . Decode ( _mac ) ;
159+ var reference = Org . BouncyCastle . Utilities . Encoders . Hex . Decode ( _mac ) ;
159160#endif
160- if ( ! encoded . SequenceEqual ( reference ) )
161- {
162- throw new SshException ( "MAC verification failed for PuTTY key file" ) ;
163- }
164- }
165- finally
166- {
167- hmac . Dispose ( ) ;
168- }
161+ if ( ! macValue . SequenceEqual ( reference ) )
162+ {
163+ throw new SshException ( "MAC verification failed for PuTTY key file" ) ;
169164 }
170165
171166 var publicKeyReader = new SshDataReader ( _publicKey ) ;
@@ -183,7 +178,7 @@ public Key Parse()
183178 break ;
184179 case "ecdsa-sha2-nistp256" :
185180 case "ecdsa-sha2-nistp384" :
186- case "ecdsa-sha2-nistp512 " :
181+ case "ecdsa-sha2-nistp521 " :
187182 var curve = publicKeyReader . ReadString ( Encoding . ASCII ) ;
188183 var pub = publicKeyReader . ReadBignum2 ( ) ;
189184 var prv = privateKeyReader . ReadBignum2 ( ) ;
@@ -207,7 +202,7 @@ public Key Parse()
207202 parsedKey = new RsaKey ( modulus , exponent , d , p , q , inverseQ ) ;
208203 break ;
209204 default :
210- throw new SshException ( "key type " + keyType + " is not supported for PuTTY key file" ) ;
205+ throw new SshException ( "Key type " + keyType + " is not supported for PuTTY key file" ) ;
211206 }
212207
213208 parsedKey . Comment = _comment ;
@@ -229,7 +224,7 @@ private static byte[] Argon2(string type, int iterations, int memory, int parall
229224 param = Argon2Parameters . Argon2id ;
230225 break ;
231226 default :
232- throw new SshException ( "kdf " + type + " is not supported for PuTTY key file" ) ;
227+ throw new SshException ( "KDF " + type + " is not supported for PuTTY key file" ) ;
233228 }
234229
235230 var a2p = new Argon2Parameters . Builder ( param )
@@ -254,18 +249,18 @@ private static byte[] Argon2(string type, int iterations, int memory, int parall
254249 return output ;
255250 }
256251
257- private static byte [ ] V2KDF ( string passphrase )
252+ private static byte [ ] V2KDF ( string passPhrase )
258253 {
259254 var cipherKey = new List < byte > ( ) ;
260255
261- var passwordBytes = Encoding . UTF8 . GetBytes ( passphrase ) ;
256+ var passPhraseBytes = Encoding . UTF8 . GetBytes ( passPhrase ) ;
262257 for ( var sequenceNumber = 0 ; sequenceNumber < 2 ; sequenceNumber ++ )
263258 {
264259 using ( var sha1 = SHA1 . Create ( ) )
265260 {
266261 var sequence = new byte [ ] { 0 , 0 , 0 , ( byte ) sequenceNumber } ;
267262 _ = sha1 . TransformBlock ( sequence , 0 , 4 , outputBuffer : null , 0 ) ;
268- _ = sha1 . TransformFinalBlock ( passwordBytes , 0 , passwordBytes . Length ) ;
263+ _ = sha1 . TransformFinalBlock ( passPhraseBytes , 0 , passPhraseBytes . Length ) ;
269264 Debug . Assert ( sha1 . Hash != null , "Hash is null" ) ;
270265 cipherKey . AddRange ( sha1 . Hash ) ;
271266 }
0 commit comments