Skip to content

Commit

Permalink
Merge pull request #147 from crocs-muni/devel
Browse files Browse the repository at this point in the history
version 1.7.2
  • Loading branch information
petrs authored May 6, 2017
2 parents c094aae + da488af commit f65f196
Show file tree
Hide file tree
Showing 27 changed files with 38,519 additions and 5,652 deletions.
7 changes: 4 additions & 3 deletions !readme.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
To obtain list of supported algorithms by your card:
1. Upload AlgTest_v1.7_jc222.cap to target card (upload AlgTest_v1.7_supportOnly_jc222.cap if you are NOT interested in performance tests)
How to obtain list of supported algorithms by your card:
1. Upload AlgTest_v1.7.2_jc222.cap to target card (if your card doesn't support java Card 2.2.2, please use older version AlgTest_v1.7.1)
2. Run java -jar AlgTestJClient.jar
3. Select option 1 -> SUPPORTED ALGORITHMS
4. Fill identification (name) of your card
5. Wait for test to finish (2-10 min)
6. Inspect resulting CSV file
6. Inspect resulting CSV file
7. Consider sending the result to us at jcalgtest.org so you can contribute to community database and compare the results with others
Binary file modified AlgTest_JClient/dist/AlgTestJClient.jar
Binary file not shown.
71 changes: 44 additions & 27 deletions AlgTest_JClient/src/algtest/AlgKeyHarvest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ public class AlgKeyHarvest {

private static final byte KeyPair_ALG_RSA = 1;
private static final byte KeyPair_ALG_RSA_CRT = 2;
private static final byte KeyBuilder_ALG_TYPE_RSA_PUBLIC = 11;
private static final byte KeyBuilder_ALG_TYPE_RSA_PRIVATE = 12;
private static final byte KeyBuilder_ALG_TYPE_RSA_CRT_PRIVATE = 13;
private static final byte KeyBuilder_ALG_TYPE_RSA_PUBLIC = 4;
private static final byte KeyBuilder_ALG_TYPE_RSA_PRIVATE = 5;
private static final byte KeyBuilder_ALG_TYPE_RSA_CRT_PRIVATE = 6;


AlgKeyHarvest() {
Expand All @@ -67,6 +67,7 @@ public byte process(APDU apdu) throws ISOException {
if (apduBuffer[ISO7816.OFFSET_CLA] == AlgTest.Consts.CLA_CARD_ALGTEST) {
bProcessed = 1;
switch ( apduBuffer[ISO7816.OFFSET_INS]) {
case AlgTest.Consts.INS_PREPARE_CIPHERENGINE: PrepareRSAEngine(apdu); break;
case AlgTest.Consts.INS_CARD_GETRSAKEY: GetRSAKey(apdu); break;
case AlgTest.Consts.INS_CARD_GETRANDOMDATA: GetRandomData(apdu); break;
default : {
Expand All @@ -79,19 +80,26 @@ public byte process(APDU apdu) throws ISOException {
return bProcessed;
}

void PrepareRSAEngine(APDU apdu) {
byte[] apdubuf = apdu.getBuffer();
m_testSettings.parse(apdu);

m_keyPair = new KeyPair((byte) m_testSettings.keyClass, m_testSettings.keyLength);
}


/**
* Method for on-card generation of RSA keypair and export of result outside (in two apdu)
* @param apdu
*/
void GetRSAKey(APDU apdu) {
byte[] apdubuf = apdu.getBuffer();
apdu.setIncomingAndReceive();
m_testSettings.parse(apdu);

// Generate new object if not before yet
if (m_keyPair == null) {
m_keyPair = new KeyPair((byte)m_testSettings.keyClass, m_testSettings.keyLength);
// m_keyPair = new KeyPair(KeyPair.ALG_RSA_CRT, KeyBuilder.LENGTH_RSA_512);
// m_keyPair = new KeyPair(KeyPair.ALG_RSA_CRT, KeyBuilder.LENGTH_RSA_2048);
}

switch (m_testSettings.keyType) {
Expand All @@ -100,18 +108,23 @@ void GetRSAKey(APDU apdu) {
m_rsaPublicKey = (RSAPublicKey) m_keyPair.getPublic();

short offset = 0;
apdubuf[offset] = (byte)0x82; offset++;
short len = m_rsaPublicKey.getExponent(apdubuf, (short)(offset + 2));
Util.setShort(apdubuf, offset, len);
offset += 2; // length
offset += len; // value
short len = 0;
if (apdubuf[ISO7816.OFFSET_P1] == (byte) 0x00 || apdubuf[ISO7816.OFFSET_P1] == (byte) 0x01) {
apdubuf[offset] = (byte)0x81; offset++;
len = m_rsaPublicKey.getExponent(apdubuf, (short)(offset + 2));
Util.setShort(apdubuf, offset, len);
offset += 2; // length
offset += len; // value
}

if (apdubuf[ISO7816.OFFSET_P1] == (byte) 0x00 || apdubuf[ISO7816.OFFSET_P1] == (byte) 0x02) {
apdubuf[offset] = (byte)0x82; offset++;
len = m_rsaPublicKey.getModulus(apdubuf, (short) (offset + 2));
Util.setShort(apdubuf, offset, len);
offset += 2; // length
offset += len; // value
}

apdubuf[offset] = (byte)0x82; offset++;
len = m_rsaPublicKey.getModulus(apdubuf, (short) (offset + 2));
Util.setShort(apdubuf, offset, len);
offset += 2; // length
offset += len; // value

apdu.setOutgoingAndSend((short) 0, offset);

break;
Expand All @@ -121,21 +134,25 @@ void GetRSAKey(APDU apdu) {
if(m_testSettings.keyClass == KeyPair_ALG_RSA_CRT) {
m_rsaPrivateCrtKey = (RSAPrivateCrtKey) m_keyPair.getPrivate();

short len = m_rsaPrivateCrtKey.getP(apdubuf, (short)(offset + 3));
apdubuf[offset] = (byte)0x82; offset++;
Util.setShort(apdubuf, offset, len); offset += 2;
offset += len;

len = m_rsaPrivateCrtKey.getQ(apdubuf, (short)(offset + 3));
apdubuf[offset] = (byte)0x82; offset++;
Util.setShort(apdubuf, offset, len); offset += 2;
offset += len;
short len = 0;
if (apdubuf[ISO7816.OFFSET_P1] == (byte) 0x00 || apdubuf[ISO7816.OFFSET_P1] == (byte) 0x01) {
len = m_rsaPrivateCrtKey.getP(apdubuf, (short)(offset + 3));
apdubuf[offset] = (byte)0x83; offset++;
Util.setShort(apdubuf, offset, len); offset += 2;
offset += len;
}
if (apdubuf[ISO7816.OFFSET_P1] == (byte) 0x00 || apdubuf[ISO7816.OFFSET_P1] == (byte) 0x02) {
len = m_rsaPrivateCrtKey.getQ(apdubuf, (short)(offset + 3));
apdubuf[offset] = (byte)0x84; offset++;
Util.setShort(apdubuf, offset, len); offset += 2;
offset += len;
}
}
else if(m_testSettings.keyClass == KeyPair_ALG_RSA) {
m_rsaPrivateKey = (RSAPrivateKey) m_keyPair.getPrivate();

short len = m_rsaPrivateKey.getExponent(apdubuf, (short)(offset + 3));
apdubuf[offset] = (byte)0x82; offset++;
apdubuf[offset] = (byte)0x85; offset++;
Util.setShort(apdubuf, offset, len); offset += 2;
offset += len;

Expand Down
2 changes: 2 additions & 0 deletions AlgTest_JClient/src/algtest/Consts.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class Consts {
public final static byte INS_CARD_ALLOWED_KEYS = (byte) 0x79;
public final static byte INS_CARD_ALLOWED_ENGINES = (byte) 0x80;
public final static byte INS_CARD_GETRANDOMDATA = (byte) 0x81;
public final static byte INS_PREPARE_CIPHERENGINE = (byte) 0x82;




Expand Down
109 changes: 75 additions & 34 deletions AlgTest_JClient/src/algtestjclient/AlgTestJClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ may be distributed under the terms of the GNU General Public License (GPL),
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.PatternSyntaxException;
import javax.smartcardio.ATR;
import javax.smartcardio.Card;
import javax.smartcardio.CardException;
Expand All @@ -52,6 +53,13 @@ public class AlgTestJClient {
public static final String ALGTEST_SINGLEPERAPDU = "AT_SINGLEPERAPDU"; // for 'New' AlgTest
public static final String ALGTEST_PERFORMANCE = "AT_PERFORMANCE"; // for performance testing

/**
* Version 1.7.2 (06.05.2017)
* + support for RSA key generation and export within given range
* + minor improvements of interface
*/
public final static String ALGTEST_JCLIENT_VERSION_1_7_2 = "1.7.2";

/**
* Version 1.7.1 (03.10.2016)
* + support for reader access via JNA
Expand Down Expand Up @@ -100,7 +108,7 @@ public class AlgTestJClient {
/**
* Current version
*/
public final static String ALGTEST_JCLIENT_VERSION = ALGTEST_JCLIENT_VERSION_1_7_1;
public final static String ALGTEST_JCLIENT_VERSION = ALGTEST_JCLIENT_VERSION_1_7_2;

public final static int STAT_OK = 0;
/**
Expand All @@ -115,7 +123,7 @@ public static void main(String[] args) throws IOException, Exception {

m_SystemOutLogger.println("\n----------------------------------------------------------------------- ");
m_SystemOutLogger.println("JCAlgTest " + ALGTEST_JCLIENT_VERSION + " - comprehensive tool for JavaCard smart card testing.");
m_SystemOutLogger.println("Visit jcalgtest.org for results from 50+ cards. CRoCS.cz lab 2007-2016.");
m_SystemOutLogger.println("Visit jcalgtest.org for results from 60+ cards. CRoCS.cz lab 2007-2016.");
m_SystemOutLogger.println("Please check if you use the latest version at\n https://github.com/crocs-muni/JCAlgTest/releases/latest.");

m_SystemOutLogger.println("-----------------------------------------------------------------------\n");
Expand Down Expand Up @@ -199,39 +207,69 @@ static void performKeyHarvest() throws CardException {
KeyHarvest keyHarvest = new KeyHarvest(m_SystemOutLogger);
Scanner sc = new Scanner(System.in);
// Remove new line character from stream after load integer as type of test
sc.nextLine();
m_SystemOutLogger.print("Upload applet before harvest (y/n): ");
//sc.nextLine();
m_SystemOutLogger.print("\nUpload applet before harvest (y/n): ");
String autoUploadBeforeString = sc.nextLine();
m_SystemOutLogger.println(autoUploadBeforeString);
boolean autoUploadBefore = false;
if (autoUploadBeforeString.toLowerCase().equals("y")) {
autoUploadBefore = true;
} else if (!autoUploadBeforeString.toLowerCase().equals("n")) {
m_SystemOutLogger.println("Wrong answer. Auto upload applet before harvest is disabled.");
}

m_SystemOutLogger.print("Bit length of key to generate (512, 1024 or 2048): ");
m_SystemOutLogger.println("\nBit length of key to generate.");
m_SystemOutLogger.println("Can be any number between 512 and 4096 bits (based on the card support)\n\t or range given as [start_bit_length:step_bits:end_bit_length]");
m_SystemOutLogger.println("Example inputs: '2048' or '1024:32:2048'");
m_SystemOutLogger.print("Bit length of key to generate: ");
String bitLengthString = sc.nextLine();
int acceptedInputs[] = {512, 1024};
short bitLength = JCConsts.KeyBuilder_LENGTH_RSA_512;
m_SystemOutLogger.println(bitLengthString);
int acceptedInputs[] = {512, 736, 768, 896, 960, 1024, 1280, 1536, 1984, 2048, 3072, 4096};
//short bitLength = JCConsts.KeyBuilder_LENGTH_RSA_512;
short bitLength_start = JCConsts.KeyBuilder_LENGTH_RSA_512;
short bitLength_step = (short) 0;
short bitLength_end = JCConsts.KeyBuilder_LENGTH_RSA_512;
try {
int input = Integer.parseInt(bitLengthString);
boolean isAcceptedInput = false;
for (int acceptedInput : acceptedInputs) {
if (input == acceptedInput) {
isAcceptedInput = true;
bitLength = (short) acceptedInput;
break;
}
// Detect range if submitted
if (bitLengthString.contains(":")) {
bitLengthString = bitLengthString.trim();
String[] rangeVals = bitLengthString.split(":");
int input = Integer.parseInt(rangeVals[0]);
bitLength_start = (short) input;
input = Integer.parseInt(rangeVals[1]);
bitLength_step = (short) input;
input = Integer.parseInt(rangeVals[2]);
bitLength_end = (short) input;
}
if (!isAcceptedInput) {
throw new NumberFormatException();
else {
// Single bit length submitted
int input = Integer.parseInt(bitLengthString);
boolean isAcceptedInput = false;
for (int acceptedInput : acceptedInputs) {
if (input == acceptedInput) {
isAcceptedInput = true;
// Simulated range with single value only
bitLength_start = (short) acceptedInput;
bitLength_step = (short) 0;
bitLength_end = (short) acceptedInput;
break;
}
}
if (!isAcceptedInput) {
throw new NumberFormatException();
}
}
} catch (NumberFormatException ex) {
m_SystemOutLogger.println("Wrong number. Bit length is set to " + bitLength + ".");
}
catch (NumberFormatException ex) {
m_SystemOutLogger.println("Wrong number. Bit length is set to " + bitLength_start + ".");
}
catch (PatternSyntaxException ex) {
m_SystemOutLogger.println("Wrong range input. Correct format is '1024:32:2048'. Bit length is set to " + bitLength_start + ".");
}

m_SystemOutLogger.print("Use RSA harvest with CRT (y/n): ");
m_SystemOutLogger.print("\nUse RSA harvest with CRT (y/n): ");
String useCrtString = sc.nextLine();
m_SystemOutLogger.println(useCrtString);
boolean useCrt = false;
if (useCrtString.toLowerCase().equals("y")) {
useCrt = true;
Expand All @@ -240,31 +278,34 @@ static void performKeyHarvest() throws CardException {
}

// Check if folder !card_uploaders is correctly set
File fileCardUploadersFolder = new File(CardMngr.cardUploadersFolder);
if (!fileCardUploadersFolder.exists()) {
m_SystemOutLogger.println("Cannot find folder with card uploaders. Default folder: " + CardMngr.cardUploadersFolder);
m_SystemOutLogger.print("Card uploaders folder path: ");
String newPath = sc.nextLine();
fileCardUploadersFolder = new File(CardMngr.cardUploadersFolder);
// If new path is also incorrect
if (autoUploadBeforeString.toLowerCase().equals("y")) {
File fileCardUploadersFolder = new File(CardMngr.cardUploadersFolder);
if (!fileCardUploadersFolder.exists()) {
System.err.println("Folder " + newPath + " does not exist. Cannot start gathering RSA keys.");
return;
m_SystemOutLogger.println("Cannot find folder with card uploaders. Default folder: " + CardMngr.cardUploadersFolder);
m_SystemOutLogger.print("Card uploaders folder path: ");
String newPath = sc.nextLine();
fileCardUploadersFolder = new File(CardMngr.cardUploadersFolder);
// If new path is also incorrect
if (!fileCardUploadersFolder.exists()) {
System.err.println("Folder " + newPath + " does not exist. Cannot start gathering RSA keys.");
return;
}
// Set new path to !card_uploaders folder
CardMngr.cardUploadersFolder = newPath;
}
// Set new path to !card_uploaders folder
CardMngr.cardUploadersFolder = newPath;
}

m_SystemOutLogger.print("Number of keys to generate: ");
m_SystemOutLogger.print("\nNumber of keys to generate: ");
String numOfKeysString = sc.nextLine();
m_SystemOutLogger.println(numOfKeysString);
int numOfKeys = 10;
try {
numOfKeys = Integer.parseInt(numOfKeysString);
} catch (NumberFormatException ex) {
m_SystemOutLogger.println("Wrong number. Number of keys to generate is set to " + numOfKeys + ".");
}

keyHarvest.gatherRSAKeys(autoUploadBefore, bitLength, useCrt, numOfKeys);
keyHarvest.gatherRSAKeys(autoUploadBefore, bitLength_start, bitLength_step, bitLength_end, useCrt, numOfKeys);
}

static CardTerminal selectTargetReader() {
Expand Down
Loading

0 comments on commit f65f196

Please sign in to comment.