Excited to tell you that our welfare activity has been upgraded - Paying only $5.00 + Freight to get JCOP J3H145 card and A40CR card.
Please check this post for more details.

Moreover, if you want to get Free Samples by Paying Freight only , please check this page.

Random number generator

Applets Development Guide

Moderator: product

Ninapina
Posts: 3
Joined: Thu Jul 27, 2017 4:49 am
Points :62
Contact:

Random number generator

Post by Ninapina » Tue Aug 01, 2017 9:05 am

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

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

Re: Random number generator

Post by UNKNwYSHSA » Sat Aug 05, 2017 1:19 pm

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.
sense and simplicity

tay00000
Posts: 119
Joined: Tue Sep 27, 2016 10:58 am
Points :1570
Contact:

Re: Random number generator

Post by tay00000 » Mon Aug 07, 2017 9:44 pm

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.

Post Reply Previous topicNext topic

Who is online

Users browsing this forum: No registered users and 1 guest

JavaCard OS : Disclaimer