only:m74.9 SocketCAN option

This commit is contained in:
rusefillc 2023-11-29 00:53:28 -05:00
parent 97c1f0c917
commit 58f78c150b
4 changed files with 73 additions and 31 deletions

View File

@ -0,0 +1,27 @@
package com.rusefi.io.can;
import com.rusefi.uds.CanConnector;
import tel.schich.javacan.RawCanChannel;
import java.io.IOException;
public class SocketCanConnector {
public static CanConnector create() {
RawCanChannel socket = SocketCANHelper.createSocket();
return new CanConnector() {
@Override
public void send(int id, byte[] payLoad) {
SocketCANHelper.send(id, payLoad, socket);
}
@Override
public CanPacket read() {
try {
return SocketCANHelper.read(socket);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
};
}
}

View File

@ -0,0 +1,30 @@
package com.rusefi.uds;
import com.devexperts.logging.Logging;
import static com.devexperts.logging.Logging.getLogging;
public class SeedKeyCalculator {
private static Logging log = getLogging(SeedKeyCalculator.class);
public static final int BOOTLOADER_SECRET = 0xB24F5249;
public static final int SECRET = 0x57649392;
public static int Uds_Security_CalcKey(int secret, int seed, int rnd) {
rnd = rnd & 0xFF;
int originalSeed = seed;
if (rnd < 220)
rnd += 35;
else
rnd = 255;
for (int i = 0; i < rnd; i++) {
if (seed < 0)
seed = secret ^ seed << 1;
else
seed <<= 1;
}
log.info(String.format("seed %x secret %x rnd %x makes %x", originalSeed, secret, rnd, seed));
return seed;
}
}

View File

@ -1,6 +1,7 @@
package com.rusefi.uds;
import com.devexperts.logging.Logging;
import com.rusefi.io.can.SocketCanConnector;
import com.rusefi.util.HexBinary;
import java.nio.ByteBuffer;
@ -8,14 +9,21 @@ import java.nio.ByteOrder;
import static com.devexperts.logging.Logging.getLogging;
public class PCanUdsSandbox {
public class UdsSandbox {
public static final byte RND = (byte) 0xA1;
private static final Logging log = getLogging(PCanUdsSandbox.class);
private static final Logging log = getLogging(UdsSandbox.class);
private static final int UDS_OUT = 0x7E0;
public static void main(String[] args) {
CanConnector connector = PCanConnector.createPCanConnector();
boolean useSocketCan = args.length > 0 && args[0].toLowerCase().contains("socket");
CanConnector connector;
if (useSocketCan) {
connector = SocketCanConnector.create();
} else {
connector = PCanConnector.createPCanConnector();
}
startThread(connector);
// hello
connector.send(UDS_OUT, new byte[]{0x02, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00});
@ -37,6 +45,7 @@ public class PCanUdsSandbox {
byte[] data = packet.payload();
if (isProgramAck(data)) {
log.info("Program Request ACK");
// technically this is IsoTP
connector.send(UDS_OUT, new byte[]{0x03, 0x27, 0x01, RND, 0x00, 0x00, 0x00, 0x00});
@ -47,8 +56,9 @@ public class PCanUdsSandbox {
int seed = byteBuffer.getInt();
log.info(String.format("SECURITY_ACCESS REQUEST_SEED %x from %s", seed, HexBinary.printByteArray(data)));
int key = SeedKeyTest.Uds_Security_CalcKey(SeedKeyTest.BOOTLOADER_SECRET, seed, RND);
int key = SeedKeyCalculator.Uds_Security_CalcKey(SeedKeyCalculator.BOOTLOADER_SECRET, seed, RND);
// technically this is IsoTP
byte[] keyResponse = new byte[]{0x06, 0x27, 0x02, 0, 0, 0, 0, 0x00};
ByteBuffer responseBuffer = ByteBuffer.wrap(keyResponse, 3, 4);
responseBuffer.putInt(key);

View File

@ -1,38 +1,13 @@
package com.rusefi.uds;
import com.devexperts.logging.Logging;
import org.junit.Test;
import static com.devexperts.logging.Logging.getLogging;
import static org.junit.Assert.assertEquals;
public class SeedKeyTest {
private static Logging log = getLogging(SeedKeyTest.class);
public static final int BOOTLOADER_SECRET = 0xB24F5249;
public static final int SECRET = 0x57649392;
public static int Uds_Security_CalcKey(int secret, int seed, int rnd) {
rnd = rnd & 0xFF;
int originalSeed = seed;
if (rnd < 220)
rnd += 35;
else
rnd = 255;
for (int i = 0; i < rnd; i++) {
if (seed < 0)
seed = secret ^ seed << 1;
else
seed <<= 1;
}
log.info(String.format("seed %x secret %x rnd %x makes %x", originalSeed, secret, rnd, seed));
return seed;
}
@Test
public void testAssert() {
assertEquals(0xC42F15AE, Uds_Security_CalcKey(BOOTLOADER_SECRET, 0x5D8A2010, 0xF0));
assertEquals(0x001B6F78, Uds_Security_CalcKey(SECRET, 0x5DA0B808, 0xA4));
assertEquals(0xC42F15AE, SeedKeyCalculator.Uds_Security_CalcKey(SeedKeyCalculator.BOOTLOADER_SECRET, 0x5D8A2010, 0xF0));
assertEquals(0x001B6F78, SeedKeyCalculator.Uds_Security_CalcKey(SeedKeyCalculator.SECRET, 0x5DA0B808, 0xA4));
}
}