JavacardOS will not accept order any more, please contact our partner Feitian online Store:
https://ftsafe.en.alibaba.com/index.html
https://ftsafe.en.alibaba.com/index.html
CRC32 on java card
Moderator: UNKNwYSHSA
CRC32 on java card
I used javacard.security.checksum class in my applet, but it seems that only CRC16 is supported. Does java card 2.2.2 support CRC32? If not, how can I implement it on java card? Thanks
Re: CRC32 on java card
Some java card has built-in support for CRC32, but some java card doesn't support CRC32 and you have to implement it by yourself.
Re: CRC32 on java card
Hope the code below can help you.
Code: Select all
import javacard.framework.JCSystem;
import javacard.framework.Util;
public class CRC32
{
private static byte[] crc = JCSystem.makeTransientByteArray((short) 4, JCSystem.CLEAR_ON_DESELECT);
private static final byte[] poly = { (byte) 0xED, (byte) 0xB8, (byte) 0x83, (byte) 0x20 };
private static byte[] temp1 = JCSystem.makeTransientByteArray((short) 4, JCSystem.CLEAR_ON_DESELECT);
private static byte[] temp2 = JCSystem.makeTransientByteArray((short) 4, JCSystem.CLEAR_ON_DESELECT);
private void CRC32()
{
}
public static byte[] getCRC32()
{
return crc;
}
public static void dofinal(byte[] data, short dataOffset, short dataLen)
{
short i;
//init crc with 0xFF FF FF FF
for (i = 0; i < (short) 4; i++)
{
crc[i] = (byte) 0xFF;
}
for ( i = dataOffset; i < (short) (dataOffset + dataLen); i++)
{
convertByteToArray(data[i], temp2);
for (short h = 0; h < (short) 4; h++)
{
temp1[h] = (byte) (crc[h] ^ temp2[h]);
}
for (short f = 0; f < (short) 3; f++)
{
temp1[f] = (byte) ((byte) temp1[f] & (byte) 0x00);
}
temp1[3] = (byte) ((byte) temp1[3] & (byte) 0xFF);
// read 8 bits one at a time
for (short j = 0; j < 8; j++)
{
if ((temp1[3] & (byte) 1) == 1)
{
unsigned1BitRigthShift(temp1);
for (short k = 0; k < 4; k++)
{
temp1[k] ^= poly[k];
}
} else
unsigned1BitRigthShift(temp1);
}
unsigned8BitsRightShift(crc, temp2);
Util.arrayCopy(temp2, (short) 0, crc, (short) 0, (short) 4);
for (short k = 0; k < (short) 4; k++)
{
crc[k] ^= temp1[k];
}
}
// flip bits
for ( i = 0; i < (short) 4; i++)
{
crc[i] ^= 0xff;
}
//the current crc is equal with java crc
//but for getting the crc32 which is equal to desfire EV1 crc32, the following operations is required
//-------DESFIRE desired result-------------
for ( i = 0; i < (short) 4; i++)
{
crc[i] = (byte) ~crc[i];
}
// reverse result
byte n1, n2, n3, n4;
n1 = crc[0];
n2 = crc[1];
n3 = crc[2];
n4 = crc[3];
crc[0] = n4;
crc[1] = n3;
crc[2] = n2;
crc[3] = n1;
}
private static void unsigned8BitsRightShift(byte[] crc, byte[] result)
{
temp2[3] = crc[2];
temp2[2] = crc[1];
temp2[1] = crc[0];
temp2[0] = 0;
}
private static void unsigned1BitRigthShift(byte[] a)
{
byte currentLSB, previousLSB = 0;
for (short i = (short) 0; i < a.length; i++)
{
currentLSB = (byte) (a[i] & (byte) 0x01);
a[i] = (byte) ((a[i] & 0xFF) >>> 1);
if (previousLSB == (byte) 0x01)
a[i] = (byte) (a[i] | (byte) 0x80);
previousLSB = currentLSB;
currentLSB = 0;
}
}
private static void convertByteToArray(byte data, byte[] result)
{
byte carry = 0;
if ((byte) (data & (byte) 0x80) != 0)
carry = 1;
if (carry == 1)
{
result[0] = (byte) 0xff;
result[1] = (byte) 0xff;
result[2] = (byte) 0xff;
}
result[3] = data;
}
}
Who is online
Users browsing this forum: No registered users and 23 guests