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.

Javacard CRC32 calculation results are different

Algorithm School

Moderator: UNKNwYSHSA

User avatar
btwtiger
Posts: 28
Joined: Wed Jun 10, 2015 7:22 am
Points :134
Contact:

Javacard CRC32 calculation results are different

Post by btwtiger » Sat Sep 12, 2015 3:02 am

I am trying to implement CRC32 algorithm in my java card applet, and I used two methods, but I got two different results.

1. The first way is to use " javacard.security.Checksum" class.

Code: Select all

package CRC_Test;

import javacard.framework.*;
import javacard.security.Checksum;
import javacard.security.CryptoException;

public class CRC_Test extends Applet
{

   public static void install(byte[] bArray, short bOffset, byte bLength)
   {
      new CRC_Test().register(bArray, (short) (bOffset + 1), bArray[bOffset]);
   }
   
   public void process(APDU apdu)
   {
      
      Checksum cs = Checksum.getInstance(Checksum.ALG_ISO3309_CRC32, false);

      if (selectingApplet())
      {
         return;
      }
      byte[] buf = apdu.getBuffer();
      
      switch (buf[ISO7816.OFFSET_INS])
      {
      
      case (byte)0x00:
         short lc = apdu.setIncomingAndReceive();
         byte[] tmp = JCSystem.makeTransientByteArray((short) 4, JCSystem.CLEAR_ON_DESELECT);
         tmp[0] = (byte) 0xFF;
         tmp[1] = (byte) 0xFF;
         tmp[2] = (byte) 0xFF;
         tmp[3] = (byte) 0xFF;
         cs.init(tmp, (short) 0, (short) 4);
         short ret = cs.doFinal(buf, (short) ISO7816.OFFSET_CDATA, lc, buf, (short) 2);
         Util.setShort(buf, (short) 0, ret);
         apdu.setOutgoingAndSend((short) 0, (short) 6);
         break;
      
      default:
         ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
      }   
       
   }

}


Using this method, when I send "11 22 33 44 55", it returns "38 A0 5A 29".

2. The second is to use CRC32 class developed by jcardsim.

Code: Select all

/*
 * Copyright 2011 Licel LLC.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.licel.jcardsim.crypto;

import javacard.framework.JCSystem;
import javacard.framework.Util;
import javacard.security.Checksum;
import javacard.security.CryptoException;

/*
 * Implementation <code>Checksum</code>
 * ISO/IEC 3309 compliant 32 bit CRC algorithm.
 * on BouncyCastle CryptoAPI
 * @see Checksum
 */
public class CRC32 extends Checksum {

    final static byte LENGTH = 4;
    private byte crc32[];
    private final byte polynom[] = {
        4, -63, 29, -73
    };

    public CRC32() {
        crc32 = JCSystem.makeTransientByteArray(LENGTH, JCSystem.CLEAR_ON_DESELECT);
    }

    public byte getAlgorithm() {
        return ALG_ISO3309_CRC32;
    }

    public void init(byte bArray[], short bOff, short bLen)
            throws CryptoException {
        if (bLen != LENGTH) {
            CryptoException.throwIt(CryptoException.ILLEGAL_VALUE);
        }
        Util.arrayCopyNonAtomic(bArray, bOff, crc32, (short) 0, bLen);
    }

    public short doFinal(byte inBuff[], short inOffset, short inLength, byte outBuff[], short outOffset) {
        update(inBuff, inOffset, inLength);
        for (short i = 0; i < 4; i++) {
            crc32[i] ^= 0xff;
        }

        Util.arrayCopy(crc32, (short) 0, outBuff, outOffset, (short) 4);
        Util.arrayFillNonAtomic(crc32, (short) 0, LENGTH, (byte) 0);
        return LENGTH;
    }

    public void update(byte inBuff[], short inOffset, short inLength) {
        crc32(inBuff, inOffset, inLength);
    }

    private void crc32(byte inBuf[], short inOff, short inLen) {
        short fcs_h = Util.getShort(crc32, (short) 0);
        short fcs_l = Util.getShort(crc32, (short) 2);
        short poly_h = Util.getShort(polynom, (short) 0);
        short poly_l = Util.getShort(polynom, (short) 2);
        byte carry = 0;
        for (short i = inOff; i < (short) (inOff + inLen); i++) {
            short d_h = (short) (inBuf[i] << 8);
            for (short k = 0; k < 8; k++) {
                if (((fcs_h ^ d_h) & 0x8000) != 0) {
                    carry = 0;
                    short lfcs_h = shift(fcs_h);
                    if ((fcs_l & 0x8000) != 0) {
                        carry = 1;
                    }
                    short lfcs_l = shift(fcs_l);
                    if (carry == 1) {
                        lfcs_h++;
                    }
                    fcs_h = (short) (lfcs_h ^ poly_h);
                    fcs_l = (short) (lfcs_l ^ poly_l);
                } else {
                    carry = 0;
                    short lfcs_h = shift(fcs_h);
                    if ((fcs_l & 0x8000) != 0) {
                        carry = 1;
                    }
                    short lfcs_l = shift(fcs_l);
                    if (carry == 1) {
                        lfcs_h++;
                    }
                    fcs_h = lfcs_h;
                    fcs_l = lfcs_l;
                }
                d_h <<= 1;
            }

        }

        Util.setShort(crc32, (short) 2, fcs_l);
        Util.setShort(crc32, (short) 0, fcs_h);
    }

    short shift(short s) {
        return s <<= 1;
    }
}


When I send "11 22 33 44 55", it returns "6B 05 63 5A".
This result is different with the result of using "javacard.security.Checksum". I m comfused.
Am I making any mistake ?
Which result is the right one?
Onward...

User avatar
UNKNwYSHSA
Posts: 630
Joined: Thu May 21, 2015 4:05 am
Points :3053
Contact:

Re: Javacard CRC32 calculation results are different

Post by UNKNwYSHSA » Fri Oct 02, 2015 2:14 pm

The different is the initial value, the first uses 0xFFFFFFFF, what is the value of the second?
sense and simplicity

Post Reply Previous topicNext topic

Who is online

Users browsing this forum: No registered users and 5 guests

JavaCard OS : Disclaimer