Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix javacard.security.RandomData not be random #212

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/main/java/com/licel/jcardsim/crypto/RandomDataImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.bouncycastle.crypto.prng.DigestRandomGenerator;
import org.bouncycastle.crypto.prng.RandomGenerator;

import java.security.SecureRandom;

/**
* Implementation <code>RandomData</code> based
* on BouncyCastle CryptoAPI.
Expand All @@ -34,6 +36,7 @@ public class RandomDataImpl extends RandomData {
public RandomDataImpl(byte algorithm) {
this.algorithm = algorithm;
this.engine = new DigestRandomGenerator(new SHA1Digest());
this.engine.addSeedMaterial(new SecureRandom().generateSeed(8));
}

public void generateData(byte[] buffer, short offset, short length) throws CryptoException {
Expand Down
50 changes: 38 additions & 12 deletions src/test/java/com/licel/jcardsim/crypto/RandomDataImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
*/
package com.licel.jcardsim.crypto;

import javacard.framework.Util;
import javacard.security.RandomData;
import junit.framework.TestCase;

import java.util.Arrays;

/**
* Test for <code>RandomDataImpl</code>
*/
Expand All @@ -40,35 +43,58 @@ protected void tearDown() throws Exception {
*/
public void testGenerateData() {
System.out.println("generateData");
byte[] buffer = new byte[8];
byte[] buffer0 = new byte[]{0,0,0,0,0,0,0,0};
byte[] buffer1 = new byte[]{0,0,0,0,0,0,0,0};

RandomData instance = RandomData.getInstance(RandomData.ALG_PSEUDO_RANDOM);
instance.generateData(buffer, (short) 0, (short) buffer.length);
instance.generateData(buffer0, (short) 0, (short) buffer0.length);
assertTrue( 0 != Util.arrayCompare(buffer0, (short) 0,buffer1, (short) 0, (short) buffer0.length));

instance = RandomData.getInstance(RandomData.ALG_SECURE_RANDOM);
instance.generateData(buffer, (short) 0, (short) buffer.length);
instance.generateData(buffer1, (short) 0, (short) buffer1.length);
assertTrue( 0 != Util.arrayCompare(buffer0, (short) 0,buffer1, (short) 0, (short) buffer0.length));

instance = RandomData.getInstance(RandomData.ALG_TRNG);
instance.generateData(buffer, (short) 0, (short) buffer.length);
instance.generateData(buffer0, (short) 0, (short) buffer0.length);
assertTrue( 0 != Util.arrayCompare(buffer0, (short) 0,buffer1, (short) 0, (short) buffer0.length));

instance = RandomData.getInstance(RandomData.ALG_FAST);
instance.generateData(buffer, (short) 0, (short) buffer.length);
instance.generateData(buffer1, (short) 0, (short) buffer1.length);
assertTrue( 0 != Util.arrayCompare(buffer0, (short) 0,buffer1, (short) 0, (short) buffer0.length));

instance = RandomData.getInstance(RandomData.ALG_KEYGENERATION);
instance.generateData(buffer, (short) 0, (short) buffer.length);
instance.generateData(buffer0, (short) 0, (short) buffer0.length);
assertTrue( 0 != Util.arrayCompare(buffer0, (short) 0,buffer1, (short) 0, (short) buffer0.length));

}

/**
* Test of generateData method, of class RandomDataImpl.
*/
public void testNextBytes() {
System.out.println("nextBytes");
byte[] buffer = new byte[8];
byte[] buffer0 = new byte[]{0,0,0,0,0,0,0,0};
byte[] buffer1 = new byte[]{0,0,0,0,0,0,0,0};

RandomData instance = RandomData.getInstance(RandomData.ALG_PSEUDO_RANDOM);
instance.nextBytes(buffer, (short) 0, (short) buffer.length);
instance.nextBytes(buffer0, (short) 0, (short) buffer0.length);
assertTrue( 0 != Util.arrayCompare(buffer0, (short) 0,buffer1, (short) 0, (short) buffer0.length));

instance = RandomData.getInstance(RandomData.ALG_SECURE_RANDOM);
instance.nextBytes(buffer, (short) 0, (short) buffer.length);
instance.nextBytes(buffer1, (short) 0, (short) buffer1.length);
assertTrue( 0 != Util.arrayCompare(buffer0, (short) 0,buffer1, (short) 0, (short) buffer0.length));

instance = RandomData.getInstance(RandomData.ALG_TRNG);
instance.nextBytes(buffer, (short) 0, (short) buffer.length);
instance.nextBytes(buffer0, (short) 0, (short) buffer0.length);
assertTrue( 0 != Util.arrayCompare(buffer0, (short) 0,buffer1, (short) 0, (short) buffer0.length));

instance = RandomData.getInstance(RandomData.ALG_FAST);
instance.nextBytes(buffer, (short) 0, (short) buffer.length);
instance.nextBytes(buffer1, (short) 0, (short) buffer1.length);
assertTrue( 0 != Util.arrayCompare(buffer0, (short) 0,buffer1, (short) 0, (short) buffer0.length));

instance = RandomData.getInstance(RandomData.ALG_KEYGENERATION);
instance.nextBytes(buffer, (short) 0, (short) buffer.length);
instance.nextBytes(buffer0, (short) 0, (short) buffer0.length);
assertTrue( 0 != Util.arrayCompare(buffer0, (short) 0,buffer1, (short) 0, (short) buffer0.length));
}

/**
Expand Down