Page 1 of 1
Random Data Genaration
Posted: Thu May 11, 2017 11:00 pm
by shibulijack
I am trying to generate a 32 bytes random number. However, I have never succeed yet.I always got the same data back.
It seems that my code doesn't work at all, I mean,it doesn't generate random as I expected.
Where did I do wrongly? Any help.
Code: Select all
RandomData rd = RandomData.getInstance(RandomData.ALG_SECURE_RANDOM);
...
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);
Re: Random Data Genaration
Posted: Fri May 12, 2017 5:34 am
by mabel
I didn't find out any question in your code.
You can try to use this code to check if this can work.
Code: Select all
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;
}
Re: Random Data Genaration
Posted: Fri May 12, 2017 6:27 am
by shibulijack
Does this code work on your card? Unfortunately it did not solve the problem.
Re: Random Data Genaration
Posted: Sun May 14, 2017 5:39 am
by tay00000
I have no problem running your code fragment.
Here's my process() method.
Code: Select all
public void process(APDU apdu) {
if (selectingApplet()) {
return;
}
byte[] buffer = apdu.getBuffer();
short bytesRead = apdu.setIncomingAndReceive();
if (buffer[ISO7816.OFFSET_CLA] == (byte) 0x00 && buffer[ISO7816.OFFSET_INS] == (byte) 0x01) {
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);
}
}
Here's my APDU command transactions on my test card
Code: Select all
>>> 00 01 00 00 0C 00 00 00 00 00 00 00 00 00 00 00 00
<<< 6C 89 24 6D 45 20 0C 79 1C 8B 03 46 90 00
>>> 00 01 00 00 20 B2 0A 60 28 E4 18 93 D7 71 19 18 B2 63 83 63 3B EC 17 2F 37 C3 15 6C C0 36 5C D2 B6 44 2D E3 D3
<<< 45 5B 6B AD A8 77 3E BA 47 90 D5 EF 14 7B 0E 23 A0 FA E1 B6 1E E3 9D 7C 28 62 0E 5E C0 C4 7E DD 90 00
You try to use the process() method as per what I used above and see if it works. Also I assume your card has ALG_SECURE_RANDOM.