Page 1 of 1

Random number generator

Posted: Tue Aug 01, 2017 9:05 am
by Ninapina
Hello everyone,
I'm an absolute javacard-newbie, so please condone my eventually wrong javacard-comprehension.

I wrote a mini random number generator program. But ist doesen't work.

Code: Select all

import javacard.framework.*;
import javacard.framework.APDU;
import javacard.framework.ISO7816;
import javacard.framework.ISOException;
import javacard.framework.Util;
import javacard.security.RandomData;



public class zufall extends Applet {
   
   
   
   /* constants declaration */

    // code of CLA byte in the command APDU header
    final static byte RANDOM_CLA = (byte) 0x80;
 
    // codes of INS byte in the command APDU header
    final static byte random = (byte) 0x20;
   
   
    RandomData m_rngRandom;
   
   
   
     public static void install(byte[] bArray, short bOffset, byte bLength) {
        new zufall();
    }

    /**
     * Only this class's install method should create the applet object.
     */
    protected  zufall() {
        register();
    }

    public void process(APDU apdu) {
       

        byte[] buffer = apdu.getBuffer();
        // check SELECT APDU command

        if (apdu.isISOInterindustryCLA()) {
            if (buffer[ISO7816.OFFSET_INS] == (byte) (0xA4)) {
                return;
            }
            ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
        }

        // verify the reset of commands have the
        // correct CLA byte, which specifies the
        // command structure
        if (buffer[ISO7816.OFFSET_CLA] != RANDOM_CLA) {
            ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
        }

        switch (buffer[ISO7816.OFFSET_INS]) {
            case random:
               doRandom(apdu);
                return;
           
            default:
                ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
        }

    } // end of process method
       
    private void doRandom(APDU apdu) {

       byte[] buffer=apdu.getBuffer();
       byte[] testArray1=new byte[16];
       
       m_rngRandom = RandomData.getInstance(RandomData.ALG_TRNG);
       m_rngRandom.nextBytes(testArray1, (short) 0, (short)testArray1.length);

       Util.arrayCopyNonAtomic(testArray1, (short)0, buffer, (short)0,(short) testArray1.length);
        apdu.setOutgoingAndSend((short)0, (short)testArray1.length);
       
           
        }
}


my APDU-command:

CMD>//doRandom
0x80 0x20 0x00 0x00 0x00 0x00;
APDU|CLA: 80, INS: 20, P1: 00, P2: 00, Lc: 00, Le: 03, 11, 11, 11, SW1: 6f, SW2: 00
CMD>

"SW1: 6f, SW2: 00" means "Command aborted - more exact diagnosis not possible (e.g., operating system error)."

So what did I wrong? :(

Ich would be very grateful, if you could give me some information, help or inspiration:)
best regards,
Nina

Re: Random number generator

Posted: Sat Aug 05, 2017 1:19 pm
by UNKNwYSHSA
The argument value of RandomData.getInstance() shall only be RandomData.ALG_PSEUDO_RANDOM or RandomData.ALG_SECURE_RANDOM.
And you can use the method generateData() of RandomData instance to generate random bytes, not method nextBytes.

Here is the description of class RandomData in JavaCard API specification.

Re: Random number generator

Posted: Mon Aug 07, 2017 9:44 pm
by tay00000
Your attempt to call ALG_TRNG is technically correct in the sense that is JC 3.0.5 specification API. The thing is if your card does not support JC 3.0.5, you are better off using ALG_SECURE_RANDOM and ALG_PSEUDO_RANDOM which is in the JC 2.2.X version of the API.

Also note that ALG_SECURE_RANDOM and ALG_PSEUDO_RANDOM are considered depreciated in JC 3.0.5 and above but I am very very doubtful if many of the cards in market would actually implement JC 3.0.5 API so it is safer bet to stick to JC 2.2.X's ALG_SECURE_RANDOM and ALG_PSEUDO_RANDOM selection.