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.

Change the cipher length to 8

Algorithm School

Moderator: UNKNwYSHSA

gorgekin
Posts: 20
Joined: Sun Nov 29, 2015 4:38 am
Points :83
Contact:

Change the cipher length to 8

Post by gorgekin » Mon Jan 25, 2016 11:53 pm

I'm trying to encrypt a byte array of size 8. The size of cipher obtained is 16, But I need the cipher length to be 8 for further processing.

Code: Select all

Cipher cipherinstance = Cipher.getInstance(Cipher.ALG_DES_CBC_ISO9797_M2, false);
DESKey deskey = (DESKey)KeyBuilder.buildKey(KeyBuilder.TYPE_DES, KeyBuilder.LENGTH_DES, false);
deskey.setKey(KEY_8_BYTES, (short)0);
outlength = cipherinstance.doFinal(temp, (short)0, (short)temp.length, res, (short)0);


Size of 'temp' is 8. But 'outlength' is 16. I was expecting this to be 8.
How should I do? Thanks in advance.

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

Re: Change the cipher length to 8

Post by UNKNwYSHSA » Tue Jan 26, 2016 1:51 am

You can use the signature instead.
Here's my test code:

Code: Select all

package testDES;

import javacard.framework.*;
import javacard.security.*;
import javacardx.crypto.*;

public class testDES extends Applet
{
   private Signature signatureInstance;
   private Cipher cipherInstance;
   private DESKey desKey;
   private byte[] tempBuf;
   
   public testDES() {
      signatureInstance = Signature.getInstance(Signature.ALG_DES_MAC8_ISO9797_M1, false);
      cipherInstance = Cipher.getInstance(Cipher.ALG_DES_CBC_ISO9797_M1, false);
      desKey = (DESKey)KeyBuilder.buildKey(KeyBuilder.TYPE_DES, KeyBuilder.LENGTH_DES3_2KEY, false);
      tempBuf = JCSystem.makeTransientByteArray((short) 0x100, JCSystem.CLEAR_ON_DESELECT);
   }
   
   public static void install(byte[] bArray, short bOffset, byte bLength)
   {
      new testDES().register(bArray, (short) (bOffset + 1), bArray[bOffset]);
   }

   public void process(APDU apdu)
   {
      if (selectingApplet())
      {
         return;
      }

      byte[] buf = apdu.getBuffer();
      switch (buf[ISO7816.OFFSET_INS])
      {
      case (byte)0x00:
         desKey.setKey(buf, ISO7816.OFFSET_CDATA);
         break;
      case (byte)0x01:
         signatureInstance.init(desKey, Signature.MODE_SIGN);
         short signatureLength = signatureInstance.sign(buf, ISO7816.OFFSET_CDATA, Util.makeShort((byte) 0, buf[ISO7816.OFFSET_LC]), tempBuf, (short) 0);
         apdu.setOutgoing();
         apdu.setOutgoingLength(signatureLength);
         apdu.sendBytesLong(tempBuf, (short) 0, signatureLength);
         break;
      case (byte)0x02:
         cipherInstance.init(desKey, Cipher.MODE_ENCRYPT);
         short resultLength = cipherInstance.doFinal(buf, ISO7816.OFFSET_CDATA, Util.makeShort((byte) 0, buf[ISO7816.OFFSET_LC]), tempBuf, (short) 0);
         apdu.setOutgoing();
         apdu.setOutgoingLength(resultLength);
         apdu.sendBytesLong(tempBuf, (short) 0, resultLength);
         break;
      default:
         ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
      }
   }

}

And the test script:

Code: Select all

00A40400 06 112233445500;
00000000 10 000102030405060708090A0B0C0D0E0F;
00010000 01 00;
00020000 01 00;
00010000 10 000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F;
00020000 10 000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F;
00010000 20 000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F;
00020000 20 000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F;

test result:
No.1
Send: 00 A4 04 00 06 11 22 33 44 55 00
Expt: 90 00 [IGNORE DATA]
Recv: 90 00 [SW: No Error]
Time used: 12.000 ms

No.2
Send: 00 00 00 00 10 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
Expt: 90 00 [IGNORE DATA]
Recv: 90 00 [SW: No Error]
Time used: 16.000 ms

No.3
Send: 00 01 00 00 01 00
Expt: 90 00 [IGNORE DATA]
Recv: DD AD A1 61 E8 D7 96 73 90 00 [SW: No Error]
Time used: 14.000 ms

No.4
Send: 00 02 00 00 01 00
Expt: 90 00 [IGNORE DATA]
Recv: DD AD A1 61 E8 D7 96 73 90 00 [SW: No Error]
Time used: 15.000 ms

No.5
Send: 00 01 00 00 10 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
Expt: 90 00 [IGNORE DATA]
Recv: EE B3 0A 81 67 1A 4F 49 90 00 [SW: No Error]
Time used: 13.000 ms

No.6
Send: 00 02 00 00 10 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
Expt: 90 00 [IGNORE DATA]
Recv: DF 0B 6C 9C 31 CD 0C E4 EE B3 0A 81 67 1A 4F 49 90 00 [SW: No Error]
Time used: 15.000 ms

No.7
Send: 00 01 00 00 20 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
Expt: 90 00 [IGNORE DATA]
Recv: B7 61 A1 9E 6F F9 4C 6B 90 00 [SW: No Error]
Time used: 18.000 ms

No.8
Send: 00 02 00 00 20 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
Expt: 90 00 [IGNORE DATA]
Recv: DF 0B 6C 9C 31 CD 0C E4 EE B3 0A 81 67 1A 4F 49 54 DD 42 F3 9B D0 80 22 B7 61 A1 9E 6F F9 4C 6B 90 00 [SW: No Error]
Time used: 20.000 ms
sense and simplicity

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

Re: Change the cipher length to 8

Post by btwtiger » Tue Jan 26, 2016 10:07 pm

That is because of the padding method of that cipher. That cipher padding mode pads input data according to the ISO 9797 method 2. The data will be padded even if it is already on a block size boundary.
Onward...

Post Reply Previous topicNext topic

Who is online

Users browsing this forum: No registered users and 26 guests

JavaCard OS : Disclaimer