IC172Algo2 : rewrite and bugfix; Cleanup build messages

Attempts to address the mismatch in the issue here: https://github.com/jglim/UnlockECU/issues/2#issuecomment-1028035906 (E38E582E)
This commit is contained in:
JinGen Lim 2022-03-02 22:11:59 +08:00
parent 9f733f323c
commit 6d0d159c80
11 changed files with 76 additions and 377 deletions

View File

@ -71,6 +71,8 @@ Currently, these security providers are available:
- EDIFF290
- EsLibEd25519
- ESPSecurityAlgoLevel1
- IC172Algo1
- IC172Algo2
- MarquardtSecurityAlgo
- OCM172
- PowertrainBoschContiSecurityAlgo1
@ -78,6 +80,7 @@ Currently, these security providers are available:
- PowertrainDelphiSecurityAlgo
- PowertrainSecurityAlgo
- PowertrainSecurityAlgo2
- PowertrainSecurityAlgo3
- PowertrainSecurityAlgoNFZ
- RBTM
- RDU222
@ -85,6 +88,7 @@ Currently, these security providers are available:
- SWSP177
- VGSSecurityAlgo
- VolkswagenSA2
- XorAlgo
The definitions file `db.json` should be found alongside the application's main binary.

View File

@ -22,8 +22,8 @@ namespace UnlockECU
return false;
}
Ed25519PrivateKeyParameters privateKey = new Ed25519PrivateKeyParameters(privateKeyBytes, 0);
Ed25519phSigner signer = new Ed25519phSigner(new byte[] { });
Ed25519PrivateKeyParameters privateKey = new(privateKeyBytes, 0);
Ed25519phSigner signer = new(Array.Empty<byte>());
signer.Init(true, privateKey);
signer.BlockUpdate(inSeed, 0, inSeed.Length);
byte[] signature = signer.GenerateSignature();

View File

@ -23,7 +23,7 @@ namespace UnlockECU
byte[] seedInput = new byte[] { inSeed[0], inSeed[2], inSeed[4], inSeed[6] };
seedInput = ExpandByteArrayToNibbles(seedInput);
List<byte[]> keyPool = new List<byte[]>();
List<byte[]> keyPool = new();
keyPool.Add(new byte[] { 0xEF, 0xCD, 0xAB, 0x89, 0x67, 0x45, 0x23, 0x01 });
keyPool.Add(new byte[] { 0x45, 0x67, 0x01, 0x23, 0xCD, 0xEF, 0x89, 0xAB });
keyPool.Add(new byte[] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF });
@ -52,31 +52,6 @@ namespace UnlockECU
return true;
}
byte[] ExpandByteArrayToNibbles(byte[] inputArray)
{
byte[] result = new byte[inputArray.Length * 2];
for (int i = 0; i < inputArray.Length; i++)
{
result[i * 2] = (byte)((inputArray[i] >> 4) & 0xF);
result[i * 2 + 1] = (byte)(inputArray[i] & 0xF);
}
return result;
}
byte[] CollapseByteArrayFromNibbles(byte[] inputArray)
{
if ((inputArray.Length % 2) != 0)
{
throw new Exception("Attempted to form a byte array from an odd-numbered set of nibbles.");
}
byte[] result = new byte[inputArray.Length / 2];
for (int i = 0; i < result.Length; i++)
{
result[i] = (byte)((inputArray[i * 2] << 4) | (inputArray[i * 2 + 1]));
}
return result;
}
public override string GetProviderName()
{
return "IC172Algo1";

View File

@ -9,7 +9,8 @@ namespace UnlockECU
/// <summary>
/// IC172 Level 113 (Experimental)
/// Originally discovered by Sergey (@Feezex) at https://github.com/jglim/UnlockECU/issues/2#issuecomment-962647700
/// Preliminary and likely to be revised, though the generated output will be unaffected
/// Currently at a second revision that addresses a bug at https://github.com/jglim/UnlockECU/issues/2#issuecomment-1028035906
/// Output samples have been verified with a recently released generator by Sergey Klenov (thank you!)
/// </summary>
class IC172Algo2 : SecurityProvider
{
@ -20,336 +21,27 @@ namespace UnlockECU
return false;
}
IntToBytes(GenSeedKeyHL(inSeed), outKey, Endian.Big);
List<int[]> keyPool = new();
keyPool.Add(new int[] { 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15 });
keyPool.Add(new int[] { 251, 252, 253, 254, 255, 256, 257, 258, 243, 244, 245, 246, 247, 248, 249, 250 });
keyPool.Add(new int[] { 0, 1, -2, -1, 4, 5, 2, 3, 8, 9, 6, 7, 12, 13, 10, 11 });
keyPool.Add(new int[] { 73, 72, 75, 74, 69, 68, 71, 70, 81, 80, 83, 82, 77, 76, 79, 78 });
keyPool.Add(new int[] { 0, -1, -2, -3, 4, 3, 2, 1, 8, 7, 6, 5, 12, 11, 10, 9, });
keyPool.Add(new int[] { 203, 202, 205, 204, 207, 206, 209, 208, 195, 194, 197, 196, 199, 198, 201, 200 });
keyPool.Add(new int[] { 0, 1, 2, 3, -4, -3, -2, -1, 8, 9, 10, 11, 4, 5, 6, 7 });
keyPool.Add(new int[] { 185, 184, 183, 182, 181, 180, 179, 178, 193, 192, 191, 190, 189, 188, 187, 186 });
byte[] nibbles = ExpandByteArrayToNibbles(inSeed);
int key_result = 0;
for (int i = 0; i < 8; i++)
{
key_result += keyPool[i][nibbles[i]] << ((7 - i) * 4);
}
IntToBytes((uint)key_result, outKey, Endian.Big);
return true;
}
uint GenSeedKeyHL(byte[] seed_bytes)
{
uint sb3 = Convert.ToUInt32(seed_bytes[3]);
uint sb3_h = sb3 / 0x10;
uint sb3_l = sb3 - sb3_h * 0x10;
uint CORR8 = 0;
if (sb3_l > 7)
{
CORR8 = 0x10;
}
uint k8 = 185 - sb3_l + CORR8;
uint CORR7 = 0;
uint CORR72 = 0;
if (sb3_h >= 4)
{
CORR7 = 0x80;
}
if (sb3_h >= 8)
{
CORR72 = 0x80;
}
if (sb3_h >= 0x0C)
{
CORR7 = 0x100;
}
uint k7 = (16 * sb3_h) - CORR7 + CORR72;
uint sb2_h = (byte)(seed_bytes[2] >> 4);
uint sb2_l = (byte)(seed_bytes[2] & 0xF);
uint CORR61 = 0;
uint CORR62 = 0;
uint CORR63 = 0;
uint CORR64 = 0;
uint CORR65 = 0;
uint CORR66 = 0;
uint CORR67 = 0;
uint CORR68 = 0;
uint CORR69 = 0;
uint CORR6A = 0;
uint CORR6B = 0;
uint CORR6C = 0;
uint CORR6D = 0;
uint CORR6E = 0;
uint CORR6F = 0;
if (sb2_l > 0)
{
CORR61 = 0x100;
}
if (sb2_l > 1)
{
CORR62 = 0x100 * 3;
}
if (sb2_l > 2)
{
CORR63 = 0x100;
}
if (sb2_l > 3)
{
CORR64 = 0x100 * 3;
}
if (sb2_l > 4)
{
CORR65 = 0x100;
}
if (sb2_l > 5)
{
CORR66 = 0x100 * 3;
}
if (sb2_l > 6)
{
CORR67 = 0x100;
}
if (sb2_l > 7)
{
CORR68 = 0xD00;
}
if (sb2_l > 8)
{
CORR69 = 0x100;
}
if (sb2_l > 9)
{
CORR6A = 0x100 * 3;
}
if (sb2_l > 0x0A)
{
CORR6B = 0x100;
}
if (sb2_l > 0x0B)
{
CORR6C = 0x100 * 3;
}
if (sb2_l > 0x0C)
{
CORR6D = 0x100;
}
if (sb2_l > 0x0D)
{
CORR6E = 0x100 * 3;
}
if (sb2_l > 0x0E)
{
CORR6F = 0x100;
}
uint k6 = 0xCB00 - CORR61 + CORR62 - CORR63 + CORR64 - CORR65 + CORR66 - CORR67 - CORR68 - CORR69 + CORR6A - CORR6B + CORR6C - CORR6D + CORR6E - CORR6F;
// 6 done
uint CORR5 = 0;
uint CORR52 = 0;
if (sb2_h >= 1)
{
CORR5 = 0x1000 * sb2_h;
}
if (sb2_h >= 4)
{
CORR52 = 0x1000 + 0x7000;
}
if (sb2_h >= 5)
{
CORR5 = 0x1000 * sb2_h;
}
if (sb2_h >= 8)
{
CORR52 = 0x7000 + 0x1000 * sb2_h + 0x1000;
}
if (sb2_h >= 9)
{
CORR5 = 0x1000 * sb2_h + 0x1000;
}
if (sb2_h >= 10)
{
CORR5 = 0x1000 * sb2_h + 0x1000 * 2;
}
if (sb2_h >= 11)
{
CORR5 = 0x1000 * sb2_h + 0x1000 * 3;
}
if (sb2_h >= 12)
{
CORR5 = 0x1000 * sb2_h - 0x1000 * 4;
}
if (sb2_h >= 13)
{
CORR5 = 0x1000 * sb2_h - 0x1000 * 3;
}
if (sb2_h >= 14)
{
CORR5 = 0x1000 * sb2_h - 0x1000 * 2;
}
if (sb2_h >= 15)
{
CORR5 = 0x1000 * sb2_h - 0x1000;
}
uint k5 = k6 - CORR5 + CORR52;
// 5 done
uint sb1_h = (byte)(seed_bytes[1] >> 4);
uint sb1_l = (byte)(seed_bytes[1] & 0xF);
uint CORR4 = 0;
uint CORR41 = 0;
if (sb1_l >= 1)
{
CORR4 = 0x10000;
}
if (sb1_l >= 2)
{
CORR41 = 0x10000 * 3;
}
if (sb1_l >= 3)
{
CORR41 = 0x10000 * (sb1_l - 1);
}
if (sb1_l >= 4)
{
CORR41 = 0x10000 * (sb1_l - 7);
}
if (sb1_l >= 5)
{
CORR41 = 0x10000 * (sb1_l - 9);
}
if (sb1_l >= 6)
{
CORR41 = 0x10000 * (sb1_l - 7);
}
if (sb1_l >= 7)
{
CORR41 = 0x10000 * (sb1_l - 9);
}
if (sb1_l >= 8)
{
CORR41 = 0x10000 * (sb1_l + 1);
}
if (sb1_l >= 9)
{
CORR41 = 0x10000 * (sb1_l - 1);
}
if (sb1_l >= 10)
{
CORR41 = 0x10000 * (sb1_l + 1);
}
if (sb1_l >= 11)
{
CORR41 = 0x10000 * (sb1_l - 1);
}
if (sb1_l >= 12)
{
CORR41 = 0x10000 * (sb1_l - 7);
}
if (sb1_l >= 13)
{
CORR41 = 0x10000 * (sb1_l - 9);
}
if (sb1_l >= 14)
{
CORR41 = 0x10000 * (sb1_l + 9);
}
if (sb1_l >= 15)
{
CORR41 = 0x10000 * (sb1_l - 9);
}
uint k4 = 0x490000 - CORR4 + CORR41;
// 4 done
uint CORR3 = 0;
uint CORR31 = 0;
if (sb1_h >= 1)
{
CORR31 = 1;
}
if (sb1_h >= 2)
{
CORR3 = (sb1_h + 1);
}
if (sb1_h >= 3)
{
CORR31 = (sb1_h);
}
if (sb1_h >= 4)
{
CORR31 = (sb1_h + 5);
}
if (sb1_h >= 5)
{
CORR31 = (sb1_h + 6);
}
if (sb1_h >= 6)
{
CORR3 = (sb1_h + 4);
}
if (sb1_h >= 7)
{
CORR31 = (sb1_h + 7);
}
if (sb1_h >= 8)
{
CORR31 = (sb1_h + 12);
}
if (sb1_h >= 9)
{
CORR31 = (sb1_h + 13);
}
if (sb1_h >= 10)
{
CORR3 = (sb1_h + 7);
}
if (sb1_h >= 11)
{
CORR31 = (sb1_h + 14);
}
if (sb1_h >= 12)
{
CORR31 = (sb1_h + 19);
}
if (sb1_h >= 13)
{
CORR31 = (sb1_h + 20);
}
if (sb1_h >= 14)
{
CORR3 = (sb1_h + 10);
}
if (sb1_h >= 15)
{
CORR31 = (sb1_h + 21);
}
CORR3 <<= 20; // 1048576
CORR31 <<= 20;
uint k3 = k4 - CORR3 + CORR31;
// 3 done
uint U1 = Convert.ToUInt32(seed_bytes[0]);
uint sb0_h = (byte)(seed_bytes[0] >> 4);
uint sb0_l = (byte)(seed_bytes[0] & 0xF);
uint CORR2 = 0x1000000; // 1 << 24
uint k2 = CORR2 * sb0_l;
if (sb0_l >= 8)
{
k2 = CORR2 * (sb0_l - 16);
}
uint k1 = sb0_h << 28;
uint constant_msb = 0xFB000000; // 0xFB << 24
uint key_final = constant_msb + k8 + k7 + k5 + k3 + k2 - k1;
return key_final;
}
public override string GetProviderName()
{
return "IC172Algo2";

View File

@ -24,7 +24,7 @@ namespace UnlockECU
return false;
}
uint outKeyInt = 0;
uint outKeyInt;
unchecked
{
outKeyInt = constC + inSeedAsInt * constA % constM;

View File

@ -71,7 +71,7 @@ namespace UnlockECU
return true;
}
private uint CreateDValue(int bit2Enabled, int bit1Enabled, int bit0Enabled, byte[][] matrix)
private static uint CreateDValue(int bit2Enabled, int bit1Enabled, int bit0Enabled, byte[][] matrix)
{
uint i = 0;
byte j = 0;
@ -94,7 +94,7 @@ namespace UnlockECU
return i;
}
private uint CreateGValue(int bit2Enabled, int bit1Enabled, int bit0Enabled, byte[][] matrix)
private static uint CreateGValue(int bit2Enabled, int bit1Enabled, int bit0Enabled, byte[][] matrix)
{
uint i = 0;
byte j = 0;

View File

@ -71,7 +71,7 @@ namespace UnlockECU
return true;
}
private uint CreateDValue(int bit2Enabled, int bit1Enabled, int bit0Enabled, byte[][] matrix)
private static uint CreateDValue(int bit2Enabled, int bit1Enabled, int bit0Enabled, byte[][] matrix)
{
uint i = 0;
byte j = 0;
@ -94,7 +94,7 @@ namespace UnlockECU
return i;
}
private uint CreateGValue(int bit2Enabled, int bit1Enabled, int bit0Enabled, byte[][] matrix)
private static uint CreateGValue(int bit2Enabled, int bit1Enabled, int bit0Enabled, byte[][] matrix)
{
uint i = 0;
byte j = 0;

View File

@ -15,9 +15,9 @@ namespace UnlockECU
/// </summary>
class PowertrainSecurityAlgo3 : SecurityProvider
{
private static uint FinalXorKey = 0x40088C88;
private static int[] SourceBitPositions = { 3, 7, 10, 11, 15, 19, 30 };
private static uint[] LookupTable =
private static readonly uint FinalXorKey = 0x40088C88;
private static readonly int[] SourceBitPositions = { 3, 7, 10, 11, 15, 19, 30 };
private static readonly uint[] LookupTable =
{
0x45D145D1, 0x406E47C6, 0x5450C446, 0x51EFC651, 0x47CE507A, 0x4271526D, 0x3121A3DA, 0x349EA1CD,
0x0CECABBF, 0x0953A9A8, 0x105B10DF, 0x15E412C8, 0x3E1D91A6, 0x3BA293B1, 0xA316122F, 0xA6A91038,
@ -59,7 +59,7 @@ namespace UnlockECU
return true;
}
public uint RemapBit(uint inValue, int sourceBitPosition, int destinationBitPosition)
public static uint RemapBit(uint inValue, int sourceBitPosition, int destinationBitPosition)
{
return (uint)((inValue & (1 << sourceBitPosition)) > 0 ? (1 << destinationBitPosition) : 0);
}

View File

@ -62,7 +62,7 @@ namespace UnlockECU
return true;
}
private uint CreateDValue(int bit2Enabled, int bit1Enabled, int bit0Enabled, byte[][] matrix)
private static uint CreateDValue(int bit2Enabled, int bit1Enabled, int bit0Enabled, byte[][] matrix)
{
uint i = 0;
byte j = 0;

View File

@ -21,7 +21,7 @@ namespace UnlockECU
throw new Exception("GenerateKey was not overridden");
}
public byte GetParameterByte(List<Parameter> parameters, string key)
public static byte GetParameterByte(List<Parameter> parameters, string key)
{
foreach (Parameter row in parameters)
{
@ -32,7 +32,7 @@ namespace UnlockECU
}
throw new Exception($"Failed to fetch byte parameter for key: {key}");
}
public int GetParameterInteger(List<Parameter> parameters, string key)
public static int GetParameterInteger(List<Parameter> parameters, string key)
{
foreach (Parameter row in parameters)
{
@ -43,7 +43,7 @@ namespace UnlockECU
}
throw new Exception($"Failed to fetch Int32 parameter for key: {key}");
}
public long GetParameterLong(List<Parameter> parameters, string key)
public static long GetParameterLong(List<Parameter> parameters, string key)
{
foreach (Parameter row in parameters)
{
@ -54,7 +54,7 @@ namespace UnlockECU
}
throw new Exception($"Failed to fetch Int64 parameter for key: {key}");
}
public byte[] GetParameterBytearray(List<Parameter> parameters, string key)
public static byte[] GetParameterBytearray(List<Parameter> parameters, string key)
{
foreach (Parameter row in parameters)
{
@ -67,7 +67,7 @@ namespace UnlockECU
}
private static bool IsInitialized = false;
private static List<SecurityProvider> SecurityProviders = new List<SecurityProvider>();
private static List<SecurityProvider> SecurityProviders = new();
public static List<SecurityProvider> GetSecurityProviders()
{
@ -94,7 +94,7 @@ namespace UnlockECU
Little,
}
public uint BytesToInt(byte[] inBytes, Endian endian, int offset = 0)
public static uint BytesToInt(byte[] inBytes, Endian endian, int offset = 0)
{
uint result = 0;
if (endian == Endian.Big)
@ -113,7 +113,7 @@ namespace UnlockECU
}
return result;
}
public void IntToBytes(uint inInt, byte[] outBytes, Endian endian)
public static void IntToBytes(uint inInt, byte[] outBytes, Endian endian)
{
if (endian == Endian.Big)
{
@ -132,7 +132,7 @@ namespace UnlockECU
}
// WARNING: endian unaware:
public byte GetBit(byte inByte, int bitPosition)
public static byte GetBit(byte inByte, int bitPosition)
{
if (bitPosition > 7)
{
@ -141,7 +141,7 @@ namespace UnlockECU
return (byte)((inByte >> bitPosition) & 1);
}
public byte GetByte(uint inInt, int bytePosition)
public static byte GetByte(uint inInt, int bytePosition)
{
if (bytePosition > 3)
{
@ -150,7 +150,7 @@ namespace UnlockECU
return (byte)(inInt >> (8 * bytePosition));
}
public byte SetBit(byte inByte, int bitPosition)
public static byte SetBit(byte inByte, int bitPosition)
{
if (bitPosition > 7)
{
@ -159,7 +159,7 @@ namespace UnlockECU
return inByte |= (byte)(1 << bitPosition);
}
public uint SetByte(uint inInt, byte byteToSet, int bytePosition)
public static uint SetByte(uint inInt, byte byteToSet, int bytePosition)
{
if (bytePosition > 3)
{
@ -170,5 +170,33 @@ namespace UnlockECU
inInt |= (uint)(byteToSet << bitPosition);
return inInt;
}
public static byte[] ExpandByteArrayToNibbles(byte[] inputArray)
{
// Primarily used for IC172
byte[] result = new byte[inputArray.Length * 2];
for (int i = 0; i < inputArray.Length; i++)
{
result[i * 2] = (byte)((inputArray[i] >> 4) & 0xF);
result[i * 2 + 1] = (byte)(inputArray[i] & 0xF);
}
return result;
}
public static byte[] CollapseByteArrayFromNibbles(byte[] inputArray)
{
// Primarily used for IC172
if ((inputArray.Length % 2) != 0)
{
throw new Exception("Attempted to form a byte array from an odd-numbered set of nibbles.");
}
byte[] result = new byte[inputArray.Length / 2];
for (int i = 0; i < result.Length; i++)
{
result[i] = (byte)((inputArray[i * 2] << 4) | (inputArray[i * 2 + 1]));
}
return result;
}
}
}

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>