Our Online Store have the new products: RFID antenna board. Currently it can work with JC10M24R and JCOP4 card chips.
Compared with normal cards, the antenna board module has a smaller size and fixed holes, which is easy to integrate in the IOT(Internet Of Things) project.

CRC32 on java card

Algorithm School

Moderator: UNKNwYSHSA

FUchai
Posts: 6
Joined: Thu Aug 25, 2016 5:45 am
Points :86
Contact:

CRC32 on java card

Post by FUchai » Mon Oct 10, 2016 11:43 pm

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

tomc2016
Posts: 12
Joined: Tue Feb 16, 2016 3:45 am
Points :109
Contact:

Re: CRC32 on java card

Post by tomc2016 » Wed Oct 12, 2016 3:09 am

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.

JCaberham
Posts: 26
Joined: Mon Nov 30, 2015 3:02 am
Points :208
Contact:

Re: CRC32 on java card

Post by JCaberham » Wed Oct 12, 2016 6:16 am

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;
 
    }
 
}

Post Reply Previous topicNext topic

Who is online

Users browsing this forum: No registered users and 28 guests

JavaCard OS : Disclaimer