7 Replies Latest reply: Oct 12, 2012 6:10 AM by Umer RSS

    Generating Random Data

    966781
      Hi,

      i need to generate a 32byte random number but i dont understand the javacard.security.RandomData class.
      Heres the code:
      //Creating the class
      RandomData rd = RandomData.getInstance(RandomData.ALG_SECURE_RANDOM);
      and later:
      byte[] buffer = apdu.getBuffer();
      short bytesRead = apdu.setIncomingAndReceive(); //bytesRead = 32byte
                
      rd.setSeed(buffer, (short)ISO7816.OFFSET_CDATA, bytesRead);
      rd.generateData(buffer, (short)ISO7816.OFFSET_CDATA, bytesRead);
                
      apdu.setOutgoing();
      apdu.setOutgoingLength(bytesRead);
      apdu.sendBytesLong(buffer, (short)ISO7816.OFFSET_CDATA, bytesRead);
      But it does not work as i expect...
      Everytime i get the same 32bytes back. Starting at FF down to E0. This doesnt seem really random :)
      Neither with "setSeed" nor without "setSeed" changes the result.

      I tried a 8byte random number but the result is always the same. The number starts at FF down to F8.
      Wheres my problem ?

      Thanks for help.
        • 1. Re: Generating Random Data
          Jean-Damien LEBUI
          I'm not sure that it would solve your problem but it seems to me that your problem lies in the way you are using buffer array to store random number.

          Try this one to see whether it solves your problem or not
          byte[] arrRN= JCSystem.getTransientByteArray(COD,16);
          RandomData rng = RandomData.getInstance(ALG_SECURE_RANDOM);
          rng.generateData(arrRN,0,16);
          then, Using array copy methods from Util class to copy this array to your buffer and then send it out to your terminal

          Hope that it would help

          Best regards,
          JDL
          • 2. Re: Generating Random Data
            966781
            Hi,

            thanks for your answer.

            Unfortunately it did not solve the problem, i still get the same 32byte random number starting at FF down to E0. It does not make any differents whether i generate 8byte or 16byte or 32byte or something else.

            Does your snippet work on your card ?
            • 3. Re: Generating Random Data
              Jean-Damien LEBUI
              Hi,

              Basically, it must work because it works with my card. I think that it's a typical snippet to generate random data on javacard.

              You can look through this code and hope that it would help you further
              Link: http://sourceforge.net/scm/?type=svn&group_id=256890 (svn check out)

              File: LicenseApplet.java

              code snippet:
              private byte[] rnd;
              private RandomData randomData;
              static final short RND_LENGTH = 32;
              
              public LicenseApplet() {
                              randomData = RandomData.getInstance(RandomData.ALG_PSEUDO_RANDOM);
                              rnd = JCSystem.makeTransientByteArray(RND_LENGTH, JCSystem.CLEAR_ON_RESET);
              }
              private short processGetChallenge(APDU apdu, boolean protectedApdu, short le) {
                              //There're more things but I cut them out
                           byte[] buffer = apdu.getBuffer();
              
                           randomData.generateData(rnd, (short) 0, le);
                           rndLength = le;
                           Util.arrayCopyNonAtomic(rnd, (short) 0, buffer, bufferOffset, le);
                   
                           return le;
                  }
              Best regards,
              JDL
              • 4. Re: Generating Random Data
                966781
                Hi again,

                deep in the oracle docs i found the reason is does not work and it is a bit embarrassing :)
                6.1 Random sequence generation
                [...]
                Note that RandomData.ALG SECURE RANDOM is not implemented in the simulator;
                [...]
                So i have to wait for the real hardware to test it again.
                • 5. Re: Generating Random Data
                  Umer
                  what simulator you are using ?
                  • 6. Re: Generating Random Data
                    966781
                    Sm@rtCafe Expert 5.0
                    • 7. Re: Generating Random Data
                      Umer
                      I don't know where you read this and why your simulator is not working but below is the output i am getting from Jcop:
                      cm>  /select |chal.app
                       => 00 A4 04 00 08 63 68 61 6C 2E 61 70 70 00          .....chal.app.
                       (197375 nsec)
                       <= 90 00                                              ..
                      Status: No Error
                      cm>  /send 8001000000
                       => 80 01 00 00 00                                     .....
                       (291132 nsec)
                       <= 28 05 4C D7 B1 65 28 3B EC 6F 37 DB BE BC 2A 8A    (.L..e(;.o7...*.
                          5C 93 6A C6 BE EA E8 21 F9 89 A2 CB B8 E4 FB 8E    \.j....!........
                          90 00                                              ..
                      Status: No Error
                      cm>  /send 8001000000
                       => 80 01 00 00 00                                     .....
                       (321933 nsec)
                       <= 58 F5 2E 40 D4 CE 00 92 C8 39 22 00 23 ED 52 CC    X..@.....9".#.R.
                          69 73 56 48 33 0A 13 BE 44 1D D3 D1 68 B1 84 FB    isVH3...D...h...
                          90 00                                              ..
                      Status: No Error
                      cm>  /send 8001000000
                       => 80 01 00 00 00                                     .....
                       (274328 nsec)
                       <= B3 14 11 10 D8 53 FC 56 92 45 8A FB 61 00 1B 4C    .....S.V.E..a..L
                          45 1F A4 35 5C 70 89 F2 5E 1B 62 48 A3 BC 0E BD    E..5\p..^.bH....
                          90 00                                              ..
                      Status: No Error
                      cm>  /send 8001000000
                       => 80 01 00 00 00                                     .....
                       (271528 nsec)
                       <= EB 13 15 40 7E C5 77 66 D0 A0 BD 6C 46 3B A7 E6    ...@~.wf...lF;..
                          4B 51 D3 80 80 9F 1A 50 49 A5 3C ED 2A 34 5F F5    KQ.....PI.<.*4_.
                          90 00                                              ..
                      Status: No Error
                      *Applet is:*
                      package getRandom;
                       
                      import javacard.framework.Applet;
                      import javacard.framework.ISO7816;
                      import javacard.framework.ISOException;
                      import javacard.framework.Util;
                      import javacard.framework.APDU;
                      import javacard.security.RandomData;
                       
                       
                      public class GetRandom extends Applet {
                           
                           // Instruction- and class-bytes
                           final static byte CLASS = (byte) 0x80;
                           final static byte getRand = (byte) 0x01;
                           
                          RandomData rd = RandomData.getInstance(RandomData.ALG_SECURE_RANDOM);
                          
                           private GetRandom (byte bArray[], short bOffset, byte bLength) 
                           { 
                                register();             
                           }
                       
                           public static void install(byte bArray[], short bOffset, byte bLength)
                                     throws ISOException {     
                                new GetRandom (bArray,bOffset,bLength);
                           }
                       
                           public void process(APDU apdu) throws ISOException {
                                
                                if (selectingApplet())
                                     return;
                                
                                byte[] apdu_buffer = apdu.getBuffer();
                                switch(apdu_buffer[ISO7816.OFFSET_INS]) {
                                
                                case getRand:
                                     getRandomNo(apdu);
                                     break;
                                default:
                                     ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
                                     
                                }
                           }
                           
                           public void getRandomNo(APDU apdu){
                                
                                byte[] buffer = apdu.getBuffer();     
                                short randLen = (short)0x20;
                                rd.generateData(buffer, (short)ISO7816.OFFSET_CDATA, (short)randLen);
                                          
                                apdu.setOutgoing();
                                apdu.setOutgoingLength(randLen);
                                apdu.sendBytesLong(buffer, (short)ISO7816.OFFSET_CDATA, randLen);
                           }
                      }