EncryptionContext: Allow passing encryption algo as a constructor parameter

This commit is contained in:
Dylan K. Taylor 2021-06-13 19:57:48 +01:00
parent cc00b3e19b
commit 0df2677464
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
2 changed files with 6 additions and 6 deletions

View File

@ -670,7 +670,7 @@ class NetworkSession{
} }
$this->sendDataPacket(ServerToClientHandshakePacket::create($handshakeJwt), true); //make sure this gets sent before encryption is enabled $this->sendDataPacket(ServerToClientHandshakePacket::create($handshakeJwt), true); //make sure this gets sent before encryption is enabled
$this->cipher = new EncryptionContext($encryptionKey); $this->cipher = new EncryptionContext($encryptionKey, EncryptionContext::ENCRYPTION_SCHEME);
$this->setHandler(new HandshakePacketHandler(function() : void{ $this->setHandler(new HandshakePacketHandler(function() : void{
$this->onServerLoginSuccess(); $this->onServerLoginSuccess();

View File

@ -32,7 +32,7 @@ use function strlen;
use function substr; use function substr;
class EncryptionContext{ class EncryptionContext{
private const ENCRYPTION_SCHEME = "AES-256-GCM"; public const ENCRYPTION_SCHEME = "AES-256-GCM";
private const CHECKSUM_ALGO = "sha256"; private const CHECKSUM_ALGO = "sha256";
/** @var bool */ /** @var bool */
@ -43,22 +43,22 @@ class EncryptionContext{
/** @var Cipher */ /** @var Cipher */
private $decryptCipher; private $decryptCipher;
/** @var int */ /** @var int */
private $decryptCounter = 0; private $decryptCounter = 0;
/** @var Cipher */ /** @var Cipher */
private $encryptCipher; private $encryptCipher;
/** @var int */ /** @var int */
private $encryptCounter = 0; private $encryptCounter = 0;
public function __construct(string $encryptionKey){ public function __construct(string $encryptionKey, string $algorithm){
$this->key = $encryptionKey; $this->key = $encryptionKey;
$this->decryptCipher = new Cipher(self::ENCRYPTION_SCHEME); $this->decryptCipher = new Cipher($algorithm);
$ivLength = $this->decryptCipher->getIVLength(); $ivLength = $this->decryptCipher->getIVLength();
$this->decryptCipher->decryptInit($this->key, substr($this->key, 0, $ivLength)); $this->decryptCipher->decryptInit($this->key, substr($this->key, 0, $ivLength));
$this->encryptCipher = new Cipher(self::ENCRYPTION_SCHEME); $this->encryptCipher = new Cipher($algorithm);
$ivLength = $this->encryptCipher->getIVLength(); $ivLength = $this->encryptCipher->getIVLength();
$this->encryptCipher->encryptInit($this->key, substr($this->key, 0, $ivLength)); $this->encryptCipher->encryptInit($this->key, substr($this->key, 0, $ivLength));
} }