Update ARIA.cs

This commit is contained in:
Namhyeon Go 2024-12-09 17:13:58 +09:00
parent ca857d70eb
commit 1b31859786

View File

@ -161,73 +161,85 @@ namespace WelsonJS.Cryptography
/** /**
* Resets the class so that it can be reused for another master key. * Resets the class so that it can be reused for another master key.
*/ */
void Reset() private void Reset()
{ {
this.keySize = 0; keySize = 0;
this.numberOfRounds = 0; numberOfRounds = 0;
this.masterKey = null; masterKey = null;
this.encRoundKeys = null; encRoundKeys = null;
this.decRoundKeys = null; decRoundKeys = null;
} }
int GetKeySize() public int GetKeySize()
{ {
return this.keySize; return keySize;
} }
void SetKeySize(int keySize) private void SetKeySize(int _keySize)
{ {
this.Reset(); Reset();
if (keySize != 128 && keySize != 192 && keySize != 256)
throw new CryptographicException("keySize=" + keySize); if (_keySize != 128 && _keySize != 192 && _keySize != 256)
this.keySize = keySize; throw new CryptographicException("keySize=" + _keySize);
switch (keySize)
switch (_keySize)
{ {
case 128: case 128:
this.numberOfRounds = 12; numberOfRounds = 12;
break; break;
case 192: case 192:
this.numberOfRounds = 14; numberOfRounds = 14;
break; break;
case 256: case 256:
this.numberOfRounds = 16; numberOfRounds = 16;
break; break;
} }
keySize = _keySize;
} }
public void SetKey(byte[] masterKey) private void SetKey(byte[] masterKey)
{ {
if (masterKey.Length * 8 < keySize) if (masterKey.Length * 8 < keySize)
throw new CryptographicException("masterKey size=" + masterKey.Length); throw new CryptographicException("masterKey size=" + masterKey.Length);
this.decRoundKeys = null;
this.encRoundKeys = null; decRoundKeys = null;
this.masterKey = (byte[])masterKey.Clone(); encRoundKeys = null;
masterKey = (byte[])masterKey.Clone();
} }
void SetupEncRoundKeys() private void SetupEncRoundKeys()
{ {
if (this.keySize == 0) if (keySize == 0)
throw new CryptographicException("keySize"); throw new CryptographicException("keySize");
if (this.masterKey == null) if (masterKey == null)
throw new CryptographicException("masterKey"); throw new CryptographicException("masterKey");
if (this.encRoundKeys == null) if (encRoundKeys == null)
this.encRoundKeys = new uint[4 * (this.numberOfRounds + 1)]; encRoundKeys = new uint[4 * (numberOfRounds + 1)];
this.decRoundKeys = null;
DoEncKeySetup(this.masterKey, this.encRoundKeys, this.keySize); decRoundKeys = null;
DoEncKeySetup(masterKey, encRoundKeys, keySize);
} }
void SetupDecRoundKeys() void SetupDecRoundKeys()
{ {
if (this.keySize == 0) if (keySize == 0)
throw new CryptographicException("keySize"); throw new CryptographicException("keySize");
if (this.encRoundKeys == null)
if (this.masterKey == null)
throw new CryptographicException("masterKey");
else
SetupEncRoundKeys();
this.decRoundKeys = (uint[])encRoundKeys.Clone(); if (encRoundKeys == null)
DoDecKeySetup(this.masterKey, this.decRoundKeys, this.keySize); {
if (masterKey == null)
{
throw new CryptographicException("masterKey");
}
else
{
SetupEncRoundKeys();
}
}
decRoundKeys = (uint[])encRoundKeys.Clone();
DoDecKeySetup(masterKey, decRoundKeys, keySize);
} }
public void SetupRoundKeys() public void SetupRoundKeys()
@ -294,39 +306,56 @@ namespace WelsonJS.Cryptography
public void Encrypt(byte[] i, int ioffset, byte[] o, int ooffset) public void Encrypt(byte[] i, int ioffset, byte[] o, int ooffset)
{ {
if (this.keySize == 0) if (keySize == 0)
throw new CryptographicException("keySize"); throw new CryptographicException("keySize");
if (this.encRoundKeys == null)
if (this.masterKey == null) if (encRoundKeys == null)
{
if (masterKey == null)
{
throw new CryptographicException("masterKey"); throw new CryptographicException("masterKey");
}
else else
{
SetupEncRoundKeys(); SetupEncRoundKeys();
DoCrypt(i, ioffset, this.encRoundKeys, this.numberOfRounds, o, ooffset); }
}
DoCrypt(i, ioffset, encRoundKeys, numberOfRounds, o, ooffset);
} }
public byte[] Encrypt(byte[] i, int ioffset) public byte[] Encrypt(byte[] i, int ioffset)
{ {
byte[] o = new byte[16]; byte[] o = new byte[16];
this.Encrypt(i, ioffset, o, 0); Encrypt(i, ioffset, o, 0);
return o; return o;
} }
public void Decrypt(byte[] i, int ioffset, byte[] o, int ooffset) public void Decrypt(byte[] i, int ioffset, byte[] o, int ooffset)
{ {
if (this.keySize == 0) if (keySize == 0)
throw new CryptographicException("keySize"); throw new CryptographicException("keySize");
if (this.decRoundKeys == null)
if (this.masterKey == null) if (decRoundKeys == null)
{
if (masterKey == null)
{
throw new CryptographicException("masterKey"); throw new CryptographicException("masterKey");
}
else else
{
SetupDecRoundKeys(); SetupDecRoundKeys();
DoCrypt(i, ioffset, this.decRoundKeys, this.numberOfRounds, o, ooffset); }
}
DoCrypt(i, ioffset, decRoundKeys, numberOfRounds, o, ooffset);
} }
public byte[] Decrypt(byte[] i, int ioffset) public byte[] Decrypt(byte[] i, int ioffset)
{ {
byte[] o = new byte[16]; byte[] o = new byte[16];
this.Decrypt(i, ioffset, o, 0); Decrypt(i, ioffset, o, 0);
return o; return o;
} }