diff --git a/Source/Framework/Cryptography/SessionKeyGeneration.cs b/Source/Framework/Cryptography/SessionKeyGeneration.cs index fd6de0d58..9a38f65b5 100644 --- a/Source/Framework/Cryptography/SessionKeyGeneration.cs +++ b/Source/Framework/Cryptography/SessionKeyGeneration.cs @@ -19,16 +19,16 @@ using System.Security.Cryptography; namespace Framework.Cryptography { - public class SessionKeyGenerator + public class SessionKeyGenerator256 { - public SessionKeyGenerator(byte[] buff, int size = 0) + public SessionKeyGenerator256(byte[] buff, int size = 0) { if (size == 0) size = buff.Length; int halfSize = size / 2; - sh = SHA1.Create(); + sh = SHA256.Create(); sh.TransformFinalBlock(buff, 0, halfSize); o1 = sh.Hash; @@ -62,6 +62,56 @@ namespace Framework.Cryptography taken = 0; } + SHA256 sh; + uint taken; + byte[] o0 = new byte[32]; + byte[] o1 = new byte[32]; + byte[] o2 = new byte[32]; + } + + public class SessionKeyGenerator + { + public SessionKeyGenerator(byte[] buff, int size = 0) + { + if (size == 0) + size = buff.Length; + + int halfSize = size / 2; + + sh = SHA1.Create(); + sh.TransformFinalBlock(buff, 0, halfSize); + o1 = sh.Hash; + + sh.Initialize(); + sh.TransformFinalBlock(buff, halfSize, size - halfSize); + o2 = sh.Hash; + + FillUp(); + } + + public void Generate(byte[] buf, uint sz) + { + for (uint i = 0; i < sz; ++i) + { + if (taken == 20) + FillUp(); + + buf[i] = o0[taken]; + taken++; + } + } + + void FillUp() + { + sh.Initialize(); + sh.TransformBlock(o1, 0, 20, o1, 0); + sh.TransformBlock(o0, 0, 20, o0, 0); + sh.TransformFinalBlock(o2, 0, 20); + o0 = sh.Hash; + + taken = 0; + } + SHA1 sh; uint taken; byte[] o0 = new byte[32]; diff --git a/Source/Game/Networking/WorldSocket.cs b/Source/Game/Networking/WorldSocket.cs index d1d8a4159..8449ce45d 100644 --- a/Source/Game/Networking/WorldSocket.cs +++ b/Source/Game/Networking/WorldSocket.cs @@ -537,7 +537,7 @@ namespace Game.Networking sessionKeyHmac.Finish(SessionKeySeed, 16); _sessionKey = new byte[40]; - var sessionKeyGenerator = new SessionKeyGenerator(sessionKeyHmac.Digest, 32); + var sessionKeyGenerator = new SessionKeyGenerator256(sessionKeyHmac.Digest, 32); sessionKeyGenerator.Generate(_sessionKey, 40); HmacSha256 encryptKeyGen = new(_sessionKey);