@@ -1170,28 +1170,29 @@ function _key_exchange($kexinit_payload_server)
11701170 // see http://tools.ietf.org/html/rfc2409#section-6.2 and  
11711171 // http://tools.ietf.org/html/rfc2412, appendex E 
11721172 case  'diffie-hellman-group1-sha1 ' :
1173-  $ ppack ('H256 ' , 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74 '  . 
1174-  '020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F1437 '  . 
1175-  '4FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED '  . 
1176-  'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF ' );
1177-  $ keyLength$ keyLength20  ? $ keyLength20 ;
1178-  $ hash'sha1 ' ;
1173+  $ prime'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74 '  . 
1174+  '020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F1437 '  . 
1175+  '4FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED '  . 
1176+  'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF ' ;
11791177 break ;
11801178 // see http://tools.ietf.org/html/rfc3526#section-3 
11811179 case  'diffie-hellman-group14-sha1 ' :
1182-  $ ppack ('H512 ' , 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74 '  . 
1183-  '020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F1437 '  . 
1184-  '4FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED '  . 
1185-  'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF05 '  . 
1186-  '98DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB '  . 
1187-  '9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B '  . 
1188-  'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718 '  . 
1189-  '3995497CEA956AE515D2261898FA051015728E5A8AACAA68FFFFFFFFFFFFFFFF ' );
1190-  $ keyLength$ keyLength20  ? $ keyLength20 ;
1191-  $ hash'sha1 ' ;
1192-  }
1193- 
1194-  $ pnew  Math_BigInteger ($ p256 );
1180+  $ prime'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74 '  . 
1181+  '020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F1437 '  . 
1182+  '4FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED '  . 
1183+  'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF05 '  . 
1184+  '98DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB '  . 
1185+  '9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B '  . 
1186+  'E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718 '  . 
1187+  '3995497CEA956AE515D2261898FA051015728E5A8AACAA68FFFFFFFFFFFFFFFF ' ;
1188+  break ;
1189+  }
1190+ 
1191+  // For both diffie-hellman-group1-sha1 and diffie-hellman-group14-sha1 
1192+  // the generator field element is 2 (decimal) and the hash function is sha1. 
1193+  $ gnew  Math_BigInteger (2 );
1194+  $ primenew  Math_BigInteger ($ prime16 );
1195+  $ kexHashnew  Crypt_Hash ('sha1 ' );
11951196 //$q = $p->bitwise_rightShift(1); 
11961197
11971198 /* To increase the speed of the key exchange, both client and server may 
@@ -1201,14 +1202,12 @@ function _key_exchange($kexinit_payload_server)
12011202 [VAN-OORSCHOT]. 
12021203
12031204 -- http://tools.ietf.org/html/rfc4419#section-6.2 */ 
1204-  $ q new  Math_BigInteger (1 );
1205-  $ q $ q -> bitwise_leftShift ( 16  *  $ keyLength );  // 2 * 8 * $keyLength 
1206-  $ q $ q -> subtract ( new   Math_BigInteger ( 1 )); 
1205+  $ one new  Math_BigInteger (1 );
1206+  $ keyLength min ( $ keyLength ,  $ kexHash -> getLength ()); 
1207+  $ max $ one -> bitwise_leftShift ( 16  *  $ keyLength )-> subtract ( $ one );  // 2 * 8 * $keyLength 
12071208
1208-  $ gnew  Math_BigInteger (2 );
1209-  $ xnew  Math_BigInteger ();
1210-  $ x$ xrandom (new  Math_BigInteger (1 ), $ q
1211-  $ e$ gmodPow ($ x$ p
1209+  $ x$ onerandom ($ one$ max
1210+  $ e$ gmodPow ($ x$ prime
12121211
12131212 $ eBytes$ etoBytes (true );
12141213 $ datapack ('CNa* ' , NET_SSH2_MSG_KEXDH_INIT , strlen ($ eBytes$ eBytes
@@ -1246,7 +1245,7 @@ function _key_exchange($kexinit_payload_server)
12461245 $ tempunpack ('Nlength ' , $ this _string_shift ($ this signature , 4 ));
12471246 $ this signature_format  = $ this _string_shift ($ this signature , $ temp'length ' ]);
12481247
1249-  $ key$ fmodPow ($ x$ p 
1248+  $ key$ fmodPow ($ x$ prime 
12501249 $ keyBytes$ keytoBytes (true );
12511250
12521251 $ this exchange_hash  = pack ('Na*Na*Na*Na*Na*Na*Na*Na* ' ,
@@ -1256,7 +1255,7 @@ function _key_exchange($kexinit_payload_server)
12561255 $ eBytesstrlen ($ fBytes$ fBytesstrlen ($ keyBytes$ keyBytes
12571256 );
12581257
1259-  $ this exchange_hash  = pack ( ' H* ' ,  $ hash$ this exchange_hash ) );
1258+  $ this exchange_hash  = $ kexHash -> hash ($ this exchange_hash );
12601259
12611260 if  ($ this session_id  === false ) {
12621261 $ this session_id  = $ this exchange_hash ;
@@ -1455,15 +1454,15 @@ function _key_exchange($kexinit_payload_server)
14551454 $ this encrypt ->enableContinuousBuffer ();
14561455 $ this encrypt ->disablePadding ();
14571456
1458-  $ ivpack ( ' H* ' ,  $ hash$ keyBytes$ this exchange_hash  . 'A '  . $ this session_id ) );
1457+  $ iv$ kexHash -> hash ($ keyBytes$ this exchange_hash  . 'A '  . $ this session_id );
14591458 while  ($ this encrypt_block_size  > strlen ($ iv
1460-  $ ivpack ( ' H* ' ,  $ hash$ keyBytes$ this exchange_hash  . $ iv) );
1459+  $ iv$ kexHash -> hash ($ keyBytes$ this exchange_hash  . $ iv
14611460 }
14621461 $ this encrypt ->setIV (substr ($ iv0 , $ this encrypt_block_size ));
14631462
1464-  $ keypack ( ' H* ' ,  $ hash$ keyBytes$ this exchange_hash  . 'C '  . $ this session_id ) );
1463+  $ key$ kexHash -> hash ($ keyBytes$ this exchange_hash  . 'C '  . $ this session_id );
14651464 while  ($ encryptKeyLengthstrlen ($ key
1466-  $ keypack ( ' H* ' ,  $ hash$ keyBytes$ this exchange_hash  . $ key) );
1465+  $ key$ kexHash -> hash ($ keyBytes$ this exchange_hash  . $ key
14671466 }
14681467 $ this encrypt ->setKey (substr ($ key0 , $ encryptKeyLength
14691468 }
@@ -1472,15 +1471,15 @@ function _key_exchange($kexinit_payload_server)
14721471 $ this decrypt ->enableContinuousBuffer ();
14731472 $ this decrypt ->disablePadding ();
14741473
1475-  $ ivpack ( ' H* ' ,  $ hash$ keyBytes$ this exchange_hash  . 'B '  . $ this session_id ) );
1474+  $ iv$ kexHash -> hash ($ keyBytes$ this exchange_hash  . 'B '  . $ this session_id );
14761475 while  ($ this decrypt_block_size  > strlen ($ iv
1477-  $ ivpack ( ' H* ' ,  $ hash$ keyBytes$ this exchange_hash  . $ iv) );
1476+  $ iv$ kexHash -> hash ($ keyBytes$ this exchange_hash  . $ iv
14781477 }
14791478 $ this decrypt ->setIV (substr ($ iv0 , $ this decrypt_block_size ));
14801479
1481-  $ keypack ( ' H* ' ,  $ hash$ keyBytes$ this exchange_hash  . 'D '  . $ this session_id ) );
1480+  $ key$ kexHash -> hash ($ keyBytes$ this exchange_hash  . 'D '  . $ this session_id );
14821481 while  ($ decryptKeyLengthstrlen ($ key
1483-  $ keypack ( ' H* ' ,  $ hash$ keyBytes$ this exchange_hash  . $ key) );
1482+  $ key$ kexHash -> hash ($ keyBytes$ this exchange_hash  . $ key
14841483 }
14851484 $ this decrypt ->setKey (substr ($ key0 , $ decryptKeyLength
14861485 }
@@ -1554,15 +1553,15 @@ function _key_exchange($kexinit_payload_server)
15541553 $ this hmac_size  = 12 ;
15551554 }
15561555
1557-  $ keypack ( ' H* ' ,  $ hash$ keyBytes$ this exchange_hash  . 'E '  . $ this session_id ) );
1556+  $ key$ kexHash -> hash ($ keyBytes$ this exchange_hash  . 'E '  . $ this session_id );
15581557 while  ($ createKeyLengthstrlen ($ key
1559-  $ keypack ( ' H* ' ,  $ hash$ keyBytes$ this exchange_hash  . $ key) );
1558+  $ key$ kexHash -> hash ($ keyBytes$ this exchange_hash  . $ key
15601559 }
15611560 $ this hmac_create ->setKey (substr ($ key0 , $ createKeyLength
15621561
1563-  $ keypack ( ' H* ' ,  $ hash$ keyBytes$ this exchange_hash  . 'F '  . $ this session_id ) );
1562+  $ key$ kexHash -> hash ($ keyBytes$ this exchange_hash  . 'F '  . $ this session_id );
15641563 while  ($ checkKeyLengthstrlen ($ key
1565-  $ keypack ( ' H* ' ,  $ hash$ keyBytes$ this exchange_hash  . $ key) );
1564+  $ key$ kexHash -> hash ($ keyBytes$ this exchange_hash  . $ key
15661565 }
15671566 $ this hmac_check ->setKey (substr ($ key0 , $ checkKeyLength
15681567
0 commit comments