Update SEED.cs

This commit is contained in:
Namhyeon Go 2024-12-05 18:52:33 +09:00
parent 3b022d68a4
commit 7a8ef89f09

View File

@ -42,27 +42,27 @@ namespace WelsonJS.Cryptography
private static ENDIAN DEFAULT_ENDIAN = ENDIAN.BIG;
// Constants for Key schedule
private static uint KC0 = 0x9e3779b9;
private static uint KC1 = 0x3c6ef373;
private static uint KC2 = 0x78dde6e6;
private static uint KC3 = 0xf1bbcdcc;
private static uint KC4 = 0xe3779b99;
private static uint KC5 = 0xc6ef3733;
private static uint KC6 = 0x8dde6e67;
private static uint KC7 = 0x1bbcdccf;
private static uint KC8 = 0x3779b99e;
private static uint KC9 = 0x6ef3733c;
private static uint KC10 = 0xdde6e678;
private static uint KC11 = 0xbbcdccf1;
private static uint KC12 = 0x779b99e3;
private static uint KC13 = 0xef3733c6;
private static uint KC14 = 0xde6e678d;
private static uint KC15 = 0xbcdccf1b;
private const uint KC0 = 0x9e3779b9;
private const uint KC1 = 0x3c6ef373;
private const uint KC2 = 0x78dde6e6;
private const uint KC3 = 0xf1bbcdcc;
private const uint KC4 = 0xe3779b99;
private const uint KC5 = 0xc6ef3733;
private const uint KC6 = 0x8dde6e67;
private const uint KC7 = 0x1bbcdccf;
private const uint KC8 = 0x3779b99e;
private const uint KC9 = 0x6ef3733c;
private const uint KC10 = 0xdde6e678;
private const uint KC11 = 0xbbcdccf1;
private const uint KC12 = 0x779b99e3;
private const uint KC13 = 0xef3733c6;
private const uint KC14 = 0xde6e678d;
private const uint KC15 = 0xbcdccf1b;
private static int ABCD_A = 0;
private static int ABCD_B = 1;
private static int ABCD_C = 2;
private static int ABCD_D = 3;
private const int ABCD_A = 0;
private const int ABCD_B = 1;
private const int ABCD_C = 2;
private const int ABCD_D = 3;
private const int LR_L0 = 0;
private const int LR_L1 = 1;
@ -73,7 +73,7 @@ namespace WelsonJS.Cryptography
private const int BLOCK_SIZE_SEED_INT = 4;
// S-BOX
private static uint[] SS0 = new uint[] {
private static readonly uint[] SS0 = new uint[] {
0x2989a1a8, 0x05858184, 0x16c6d2d4, 0x13c3d3d0, 0x14445054, 0x1d0d111c, 0x2c8ca0ac, 0x25052124,
0x1d4d515c, 0x03434340, 0x18081018, 0x1e0e121c, 0x11415150, 0x3cccf0fc, 0x0acac2c8, 0x23436360,
0x28082028, 0x04444044, 0x20002020, 0x1d8d919c, 0x20c0e0e0, 0x22c2e2e0, 0x08c8c0c8, 0x17071314,
@ -108,7 +108,7 @@ namespace WelsonJS.Cryptography
0x28c8e0e8, 0x1b0b1318, 0x05050104, 0x39497178, 0x10809090, 0x2a4a6268, 0x2a0a2228, 0x1a8a9298
};
private static uint[] SS1 = new uint[]
private static readonly uint[] SS1 = new uint[]
{
0x38380830, 0xe828c8e0, 0x2c2d0d21, 0xa42686a2, 0xcc0fcfc3, 0xdc1eced2, 0xb03383b3, 0xb83888b0,
0xac2f8fa3, 0x60204060, 0x54154551, 0xc407c7c3, 0x44044440, 0x6c2f4f63, 0x682b4b63, 0x581b4b53,
@ -144,7 +144,7 @@ namespace WelsonJS.Cryptography
0xd819c9d1, 0x4c0c4c40, 0x80038383, 0x8c0f8f83, 0xcc0ecec2, 0x383b0b33, 0x480a4a42, 0xb43787b3
};
private static uint[] SS2 = new uint[]
private static readonly uint[] SS2 = new uint[]
{
0xa1a82989, 0x81840585, 0xd2d416c6, 0xd3d013c3, 0x50541444, 0x111c1d0d, 0xa0ac2c8c, 0x21242505,
0x515c1d4d, 0x43400343, 0x10181808, 0x121c1e0e, 0x51501141, 0xf0fc3ccc, 0xc2c80aca, 0x63602343,
@ -288,17 +288,19 @@ namespace WelsonJS.Cryptography
ABCD[ABCD_D] = (ABCD[ABCD_D] << 8) ^ ((T[0] >> 24) & 0x000000ff);
}
public static void KISA_SEED_Encrypt_Block_forECB(in uint[] _in, uint in_offset, ref uint[] _out, int out_offset, KISA_SEED_KEY ks)
public class ECB
{
public void EncryptBlock(in uint[] _in, uint in_offset, ref uint[] _out, int out_offset, KISA_SEED_KEY ks)
{
uint[] LR = new uint[4]; // Iuput/output values at each rounds
uint[] T = new uint[2]; // Temporary variables for round function F
uint[] K = ks.key_data; // Pointer of round keys
// Set up input values for first round
LR[LR_L0] = _in[in_offset+0];
LR[LR_L1] = _in[in_offset+1];
LR[LR_R0] = _in[in_offset+2];
LR[LR_R1] = _in[in_offset+3];
LR[LR_L0] = _in[in_offset + 0];
LR[LR_L1] = _in[in_offset + 1];
LR[LR_R0] = _in[in_offset + 2];
LR[LR_R1] = _in[in_offset + 3];
// Reorder for big endian
// Because SEED use little endian order in default
@ -342,17 +344,17 @@ namespace WelsonJS.Cryptography
_out[out_offset + 3] = LR[LR_L1];
}
public static void KISA_SEED_Decrypt_Block_forECB(in uint[] _in, int in_offset, ref uint[] _out, int out_offset, KISA_SEED_KEY ks)
public void DecryptBlock(in uint[] _in, int in_offset, ref uint[] _out, int out_offset, KISA_SEED_KEY ks)
{
uint[] LR = new uint[4]; // Iuput/output values at each rounds
uint[] T = new uint[2]; // Temporary variables for round function F
uint[] K = ks.key_data; // Pointer of round keys
// Set up input values for first round
LR[LR_L0] = _in[in_offset+0];
LR[LR_L1] = _in[in_offset+1];
LR[LR_R0] = _in[in_offset+2];
LR[LR_R1] = _in[in_offset+3];
LR[LR_L0] = _in[in_offset + 0];
LR[LR_L1] = _in[in_offset + 1];
LR[LR_R0] = _in[in_offset + 2];
LR[LR_R1] = _in[in_offset + 3];
// Reorder for big endian
if (ENDIAN.BIG != DEFAULT_ENDIAN)
@ -395,7 +397,7 @@ namespace WelsonJS.Cryptography
_out[out_offset + 3] = LR[LR_L1];
}
public static bool SEED_ECB_init(KISA_SEED_INFO pInfo, KISA_ENC_DEC enc, byte[] pbszUserKey)
public bool Init(KISA_SEED_INFO pInfo, KISA_ENC_DEC enc, byte[] pbszUserKey)
{
uint[] ABCD = new uint[4]; // Iuput/output values at each rounds
uint[] T = new uint[2]; // Temporary variable
@ -453,6 +455,7 @@ namespace WelsonJS.Cryptography
return true;
}
}
public static void SetDefaultEndian(ENDIAN endian)
{