4 Replies Latest reply: Mar 20, 2009 10:16 PM by 843851 RSS

    sample code of AES

    843851
      package handson_crypto_aes;
      
      import javacard.framework.*;
      import javacard.security.*;
      import javacardx.crypto.*;
      
      /**
       *
       * ClassHandsOnCryptoAES
       *
       */
      
      public class HandsOnCryptoAES extends javacard.framework.Applet
      {
      
           //globals
           AESKey aesKey;
           Cipher cipherAES;
           RandomData random;
           static byte a[];
           final short dataOffset = (short) ISO7816.OFFSET_CDATA;
      
           //constructor
           private HandsOnCryptoAES (byte bArray[], short bOffset, byte bLength)
           {
                aesKey = (AESKey) KeyBuilder.buildKey(KeyBuilder.TYPE_AES, KeyBuilder.LENGTH_AES_128, false);
                cipherAES = Cipher.getInstance(Cipher.ALG_AES_BLOCK_128_CBC_NOPAD, false);
                a = new byte[ (short) 128];
                random.generateData(a, (short)0, (short)128);
                aesKey.setKey(a, (short) 0);
                register(bArray, (short) (bOffset + 1), bArray[bOffset]);
           }
      
           //install
           public static void install(byte bArray[], short bOffset, byte bLength)
           {
                new HandsOnCryptoAES (bArray, bOffset, bLength);
           }
      
           public void process(APDU apdu)
           {
                byte[] buf = apdu.getBuffer();
                if (selectingApplet())
                {
                     return;
                }
                if (buf[ISO7816.OFFSET_CLA] != 0) ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
                
                if (buf[ISO7816.OFFSET_INS] != (byte) (0xAA)) ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
                
                switch (buf[ISO7816.OFFSET_P1])
                {
                case (byte) 0x01:
                     doAES(apdu);
                     return;
                default:
                     ISOException.throwIt(ISO7816.SW_WRONG_P1P2);
                }
           }
      
      
           private void doAES(APDU apdu)
           {
                
                byte b[] = apdu.getBuffer();
                
                short incomingLength = (short) (apdu.setIncomingAndReceive());
                if (incomingLength != 24) ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
      
                //perform encryption and append results in APDU Buffer a[] automatically 
                
                cipherAES.init(aesKey, Cipher.MODE_ENCRYPT);
                cipherAES.doFinal(b, (short) dataOffset, incomingLength, a, (short) (dataOffset + 24));
                cipherAES.init(aesKey, Cipher.MODE_DECRYPT);
                cipherAES.doFinal(b, (short) (dataOffset + 24), incomingLength, a, (short) (dataOffset + 48));
      
                // Send results
                apdu.setOutgoing();
                apdu.setOutgoingLength((short) 72);
                apdu.sendBytesLong(b, (short) dataOffset, (short) 72);
           }
        • 1. Re: sample code of AES
          843851
          Hi,
          I find your code very usefull, I want to use this one for a Gemplus card. I am not understanding why your data length has to be 24. What if I want to chipper strings which is just 10 bytes long... or pin's which are 4 bytes long.. can I use AES too ?. Could you please let me know why
          if (incomingLength != 24) ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
          and how is 72 coming here
          apdu.setOutgoing();
          apdu.setOutgoingLength((short) 72);
          apdu.sendBytesLong(b, (short) dataOffset, (short) 72);
               
          it would be very helpful if you could explain a bit on it.
          cheers
          Domnic
          • 2. Re: sample code of AES
            843851
            hi budy.. AES can use only 128, 192, 256 block of bits. So your input data should be like that only. and also some cards wont support AES algorithm.
            • 3. Re: sample code of AES
              843851
              I want to use this class in simulator with eclipse jcde plugin.

              Is it possible?
              • 4. Re: sample code of AES
                843851
                Why load the sample code test it yuorself, there are tons of card which support AES. Get real card.