diff options
| author | petrs | 2017-06-08 14:56:23 +0200 |
|---|---|---|
| committer | GitHub | 2017-06-08 14:56:23 +0200 |
| commit | 359b4bb5be1a822e389e54b9697504f4f0b43d34 (patch) | |
| tree | 771dac996ecab9e676a0b1b3c6d14692c5dad8a9 /src/simpleapdu | |
| parent | 7cf4a9d7272c1abd97a160c92ec1534d5ff82373 (diff) | |
| parent | 3f1483635615a0c599ae2e75858be4e39eb08a64 (diff) | |
| download | ECTester-359b4bb5be1a822e389e54b9697504f4f0b43d34.tar.gz ECTester-359b4bb5be1a822e389e54b9697504f4f0b43d34.tar.zst ECTester-359b4bb5be1a822e389e54b9697504f4f0b43d34.zip | |
Merge pull request #1 from petrs/devel
Merge current dev version
Diffstat (limited to 'src/simpleapdu')
| -rw-r--r-- | src/simpleapdu/CardMngr.java | 210 | ||||
| -rw-r--r-- | src/simpleapdu/ISO7816_status_words.txt | 71 | ||||
| -rw-r--r-- | src/simpleapdu/SimpleAPDU.java | 291 |
3 files changed, 0 insertions, 572 deletions
diff --git a/src/simpleapdu/CardMngr.java b/src/simpleapdu/CardMngr.java deleted file mode 100644 index 1ab6408..0000000 --- a/src/simpleapdu/CardMngr.java +++ /dev/null @@ -1,210 +0,0 @@ -package simpleapdu; - -import com.licel.jcardsim.io.CAD; -import com.licel.jcardsim.io.JavaxSmartCardInterface; -import java.util.List; -import javacard.framework.AID; -import javax.smartcardio.*; - -/** - * - * @author xsvenda - */ -public class CardMngr { - CardTerminal m_terminal = null; - CardChannel m_channel = null; - Card m_card = null; - - // Simulator related attributes - private static CAD m_cad = null; - private static JavaxSmartCardInterface m_simulator = null; - - - private final byte selectCM[] = { - (byte) 0x00, (byte) 0xa4, (byte) 0x04, (byte) 0x00, (byte) 0x07, (byte) 0xa0, (byte) 0x00, (byte) 0x00, - (byte) 0x00, (byte) 0x18, (byte) 0x43, (byte) 0x4d}; - - public static final byte OFFSET_CLA = 0x00; - public static final byte OFFSET_INS = 0x01; - public static final byte OFFSET_P1 = 0x02; - public static final byte OFFSET_P2 = 0x03; - public static final byte OFFSET_LC = 0x04; - public static final byte OFFSET_DATA = 0x05; - public static final byte HEADER_LENGTH = 0x05; - public final static short DATA_RECORD_LENGTH = (short) 0x80; // 128B per record - public final static short NUMBER_OF_RECORDS = (short) 0x0a; // 10 records - - public boolean ConnectToCard() throws Exception { - // TRY ALL READERS, FIND FIRST SELECTABLE - List terminalList = GetReaderList(); - - if (terminalList.isEmpty()) { - System.out.println("No terminals found"); - } - - //List numbers of Card readers - boolean cardFound = false; - for (int i = 0; i < terminalList.size(); i++) { - System.out.println(i + " : " + terminalList.get(i)); - m_terminal = (CardTerminal) terminalList.get(i); - if (m_terminal.isCardPresent()) { - m_card = m_terminal.connect("*"); - System.out.println("card: " + m_card); - m_channel = m_card.getBasicChannel(); - - //reset the card - ATR atr = m_card.getATR(); - System.out.println(bytesToHex(m_card.getATR().getBytes())); - - cardFound = true; - } - } - - return cardFound; - } - - public void DisconnectFromCard() throws Exception { - if (m_card != null) { - m_card.disconnect(false); - m_card = null; - } - } - - public byte[] GetCPLCData() throws Exception { - byte[] data; - - // TODO: Modify to obtain CPLC data - byte apdu[] = new byte[HEADER_LENGTH]; - apdu[OFFSET_CLA] = (byte) 0x00; - apdu[OFFSET_INS] = (byte) 0x00; - apdu[OFFSET_P1] = (byte) 0x00; - apdu[OFFSET_P2] = (byte) 0x00; - apdu[OFFSET_LC] = (byte) 0x00; - - ResponseAPDU resp = sendAPDU(apdu); - if (resp.getSW() != 0x9000) { // 0x9000 is "OK" - System.out.println("Fail to obtain card's response data"); - data = null; - } else { - byte temp[] = resp.getBytes(); - data = new byte[temp.length - 2]; - System.arraycopy(temp, 0, data, 0, temp.length - 2); - // Last two bytes are status word (also obtainable by resp.getSW()) - // Take a look at ISO7816_status_words.txt for common codes - } - - return data; - } - - public void ProbeCardCommands() throws Exception { - // TODO: modify to probe for instruction - for (int i = 0; i <= 0; i++) { - byte apdu[] = new byte[HEADER_LENGTH]; - apdu[OFFSET_CLA] = (byte) 0x00; - apdu[OFFSET_INS] = (byte) 0x00; - apdu[OFFSET_P1] = (byte) 0x00; - apdu[OFFSET_P2] = (byte) 0x00; - apdu[OFFSET_LC] = (byte) 0x00; - - ResponseAPDU resp = sendAPDU(apdu); - - System.out.println("Response: " + Integer.toHexString(resp.getSW())); - - if (resp.getSW() != 0x6D00) { // Note: 0x6D00 is SW_INS_NOT_SUPPORTED - // something? - } - } - } - - public List GetReaderList() { - try { - TerminalFactory factory = TerminalFactory.getDefault(); - List readersList = factory.terminals().list(); - return readersList; - } catch (Exception ex) { - System.out.println("Exception : " + ex); - return null; - } - } - - public ResponseAPDU sendAPDU(byte apdu[]) throws Exception { - CommandAPDU commandAPDU = new CommandAPDU(apdu); - - System.out.println(">>>>"); - System.out.println(commandAPDU); - - System.out.println(bytesToHex(commandAPDU.getBytes())); - - long elapsed = -System.nanoTime(); - - ResponseAPDU responseAPDU = m_channel.transmit(commandAPDU); - - elapsed += System.nanoTime(); - - System.out.println(responseAPDU); - System.out.println(bytesToHex(responseAPDU.getBytes())); - - if (responseAPDU.getSW1() == (byte) 0x61) { - CommandAPDU apduToSend = new CommandAPDU((byte) 0x00, - (byte) 0xC0, (byte) 0x00, (byte) 0x00, - (int) responseAPDU.getSW1()); - - responseAPDU = m_channel.transmit(apduToSend); - System.out.println(bytesToHex(responseAPDU.getBytes())); - } - - System.out.println("<<<<"); - System.out.println("Elapsed time (ms): " + elapsed / 1000000); - return (responseAPDU); - } - - public String byteToHex(byte data) { - StringBuilder buf = new StringBuilder(); - buf.append(toHexChar((data >>> 4) & 0x0F)); - buf.append(toHexChar(data & 0x0F)); - return buf.toString(); - } - - - public char toHexChar(int i) { - if ((0 <= i) && (i <= 9)) { - return (char) ('0' + i); - } else { - return (char) ('a' + (i - 10)); - } - } - - public String bytesToHex(byte[] data) { - StringBuilder buf = new StringBuilder(); - for (int i = 0; i < data.length; i++) { - buf.append(byteToHex(data[i])); - buf.append(" "); - } - return (buf.toString()); - } - - - public boolean prepareLocalSimulatorApplet(byte[] appletAIDArray, byte[] installData, Class appletClass) { - System.setProperty("com.licel.jcardsim.terminal.type", "2"); - m_cad = new CAD(System.getProperties()); - m_simulator = (JavaxSmartCardInterface) m_cad.getCardInterface(); - AID appletAID = new AID(appletAIDArray, (short) 0, (byte) appletAIDArray.length); - - AID appletAIDRes = m_simulator.installApplet(appletAID, appletClass, installData, (short) 0, (byte) installData.length); - return m_simulator.selectApplet(appletAID); - } - - public byte[] sendAPDUSimulator(byte apdu[]) throws Exception { - System.out.println(">>>>"); - System.out.println(bytesToHex(apdu)); - - byte[] responseBytes = m_simulator.transmitCommand(apdu); - - System.out.println(bytesToHex(responseBytes)); - System.out.println("<<<<"); - - return responseBytes; - } - - -} diff --git a/src/simpleapdu/ISO7816_status_words.txt b/src/simpleapdu/ISO7816_status_words.txt deleted file mode 100644 index bf5af2b..0000000 --- a/src/simpleapdu/ISO7816_status_words.txt +++ /dev/null @@ -1,71 +0,0 @@ -public interface ISO7816 { - - // Fields - public static final byte INS_EXTERNAL_AUTHENTICATE = -126; - public static final byte INS_SELECT = -92; - public static final byte CLA_ISO7816 = 0; - public static final byte OFFSET_CDATA = 5; - public static final byte OFFSET_LC = 4; - public static final byte OFFSET_P2 = 3; - public static final byte OFFSET_P1 = 2; - public static final byte OFFSET_INS = 1; - public static final byte OFFSET_CLA = 0; - public static final short SW_FILE_FULL = 27268; 0x6A84 - public static final short SW_UNKNOWN = 28416; 0x6F00 - public static final short SW_CLA_NOT_SUPPORTED = 28160; 0x6E00 - public static final short SW_INS_NOT_SUPPORTED = 27904; 0x6D00 - public static final short SW_CORRECT_LENGTH_00 = 27648; 0x6C00 - public static final short SW_WRONG_P1P2 = 27392; 0x6B00 - public static final short SW_INCORRECT_P1P2 = 27270; 0x6A86 - public static final short SW_RECORD_NOT_FOUND = 27267; 0x6A83 - public static final short SW_FILE_NOT_FOUND = 27266; 0x6A82 - public static final short SW_FUNC_NOT_SUPPORTED = 27265; 0x6A81 - public static final short SW_WRONG_DATA = 27264; 0x6A80 - public static final short SW_APPLET_SELECT_FAILED = 27033; 0x6999 - public static final short SW_COMMAND_NOT_ALLOWED = 27014; 0x6986 - public static final short SW_CONDITIONS_NOT_SATISFIED = 27013; 0x6985 - public static final short SW_DATA_INVALID = 27012; 0x6984 - public static final short SW_FILE_INVALID = 27011; 0x6983 - public static final short SW_SECURITY_STATUS_NOT_SATISFIED = 27010; 0x6982 - public static final short SW_WRONG_LENGTH = 26368; 0x6700 - public static final short SW_BYTES_REMAINING_00 = 24832; 0x6100 - public static final short SW_NO_ERROR = -28672; 0x9000 -} - -public interface JCStatus { -static int ALGORITHM_NOT_SUPPORTED = 0x9484; -static int APPLET_INVALIDATED = 0x6283; -static int APPLET_SELECT_FAILED = 0x6999 -static int AUTHENTICATION_FAILED = 0x6300 -static int AUTHORIZATION_FAILED = 0x9482 -static int CHECKSUM_FAILED = 0x9584 -static int CLA_NOT_SUPPORTED = 0x6E00 -static int COMMAND_NOT_ALLOWED = 0x6986 -static int CONDITIONS_NOT_SATISFIED = 0x6985 -static int CORRECT_LENGTH_00 = 0x6C00 -static int DATA_INVALID = 0x6984 -static int DECRYPTION_FAILED = 0x9583 -static int FILE_FULL = 0x6A84 -static int FILE_INVALID = 0x6983 -static int FILE_NOT_FOUND = 0x6A82 -static int FUNC_NOT_SUPPORTED = 0x6A81 -static int INCORRECT_P1P2 = 0x6A86 -static int INS_NOT_SUPPORTED = 0x6D00 -static int INSTALLATION_FAILED = 0x9585 -static int INVALID_STATE = 0x9481 -static int NO_ERROR = 0x9000 -static int NO_SPECIFIC_DIAGNOSIS = 0x6400 -static int PIN_REQUIRED = 0x6982 -static int RECORD_NOT_FOUND = 0x6A83 -static int REFERENCE_DATA_NOT_FOUND = 0x6A88 -static int REGISTRATION_FAILED = 0x9586 -static int SECURITY_STATUS_NOT_SATISFIED = 0x6982 -static int SIGNATURE_CHECK_FAILED = 0x9582 -static int SM_INCORRECT = 0x6988 -static int SM_MISSING = 0x6987 -static int TRUNCATED_DATA = 0x6100 -static int UNKNOWN = 0x6F00 -static int WRONG_DATA = 0x6A80 -static int WRONG_LENGTH = 0x6700 -static int WRONG_P1P2 = 0x6B00 -}
\ No newline at end of file diff --git a/src/simpleapdu/SimpleAPDU.java b/src/simpleapdu/SimpleAPDU.java deleted file mode 100644 index 0ea9ca3..0000000 --- a/src/simpleapdu/SimpleAPDU.java +++ /dev/null @@ -1,291 +0,0 @@ -package simpleapdu; - -import applets.SimpleECCApplet; -import static applets.SimpleECCApplet.ECTEST_GENERATE_KEYPAIR_CUSTOMCURVE; -import static applets.SimpleECCApplet.ECTEST_SET_INVALIDCURVE; -import javacard.framework.ISO7816; -import javacard.security.CryptoException; -import javacard.security.KeyPair; -import javax.smartcardio.ResponseAPDU; -import org.bouncycastle.util.Arrays; - -/** - * - * @author Petr Svenda petr@svenda.com - */ -public class SimpleAPDU { - static CardMngr cardManager = new CardMngr(); - - private final static byte SELECT_ECTESTERAPPLET[] = {(byte) 0x00, (byte) 0xa4, (byte) 0x04, (byte) 0x00, (byte) 0x0a, - (byte) 0x45, (byte) 0x43, (byte) 0x54, (byte) 0x65, (byte) 0x73, (byte) 0x74, (byte) 0x65, (byte) 0x72, (byte) 0x30, (byte) 0x31}; - - private static final byte TESTECSUPPORTALL_FP[] = {(byte) 0xB0, (byte) 0x5E, (byte) 0x00, (byte) 0x00, (byte) 0x00}; - private static final byte TESTECSUPPORTALL_F2M[] = {(byte) 0xB0, (byte) 0x5F, (byte) 0x00, (byte) 0x00, (byte) 0x00}; - private static final byte TESTECSUPPORT_GIVENALG[] = {(byte) 0xB0, (byte) 0x71, (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0x00, (byte) 0x00, (byte) 0x00}; - private static final short TESTECSUPPORT_ALG_OFFSET = 5; - private static final short TESTECSUPPORT_KEYLENGTH_OFFSET = 6; - - private static final byte TESTECSUPPORTALL_LASTUSEDPARAMS[] = {(byte) 0xB0, (byte) 0x40, (byte) 0x00, (byte) 0x00, (byte) 0x00}; - - private static final byte TESTECSUPPORTALL_FP_KEYGEN_INVALIDCURVEB[] = {(byte) 0xB0, (byte) 0x70, (byte) 0x00, (byte) 0x00, (byte) 0x05, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00}; - private static final short INVALIDCURVEB_NUMREPEATS_OFFSET = 5; - private static final short INVALIDCURVEB_CORRUPTIONTYPE_OFFSET = 7; - private static final short INVALIDCURVEB_REWINDONSUCCESS_OFFSET = 9; - - static short getShort(byte[] array, int offset) { - return (short) (((array[offset] & 0xFF) << 8) | (array[offset + 1] & 0xFF)); - } - static void setShort(byte[] array, int offset, short value) { - array[offset + 1] = (byte) (value & 0xFF); - array[offset] = (byte) ((value >> 8) & 0xFF); - } - static void testFPkeyGen_setNumRepeats(byte[] apduArray, short numRepeats) { - setShort(apduArray, INVALIDCURVEB_NUMREPEATS_OFFSET, numRepeats); - } - static void testFPkeyGen_setCorruptionType(byte[] apduArray, short corruptionType) { - setShort(apduArray, INVALIDCURVEB_CORRUPTIONTYPE_OFFSET, corruptionType); - } - static void testFPkeyGen_rewindOnSuccess(byte[] apduArray, boolean bRewind) { - apduArray[INVALIDCURVEB_REWINDONSUCCESS_OFFSET] = bRewind ? (byte) 1 : (byte) 0; - } - - static CardMngr ReconnnectToCard() throws Exception { - cardManager.DisconnectFromCard(); - if (cardManager.ConnectToCard()) { - // Select our application on card - cardManager.sendAPDU(SELECT_ECTESTERAPPLET); - } - return cardManager; - } - - static void testSupportECGivenAlg(byte[] apdu, CardMngr cardManager) throws Exception { - ReconnnectToCard(); - ResponseAPDU resp = cardManager.sendAPDU(apdu); - PrintECSupport(resp); - } - static void testSupportECAll(CardMngr cardManager) throws Exception { - byte[] testAPDU = Arrays.clone(TESTECSUPPORT_GIVENALG); - - testAPDU[TESTECSUPPORT_ALG_OFFSET] = KeyPair.ALG_EC_FP; - setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 128); - testSupportECGivenAlg(testAPDU, cardManager); - setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 160); - testSupportECGivenAlg(testAPDU, cardManager); - setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 192); - testSupportECGivenAlg(testAPDU, cardManager); - setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 224); - testSupportECGivenAlg(testAPDU, cardManager); - setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 256); - testSupportECGivenAlg(testAPDU, cardManager); - setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 384); - testSupportECGivenAlg(testAPDU, cardManager); - setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 521); - testSupportECGivenAlg(testAPDU, cardManager); - - testAPDU[TESTECSUPPORT_ALG_OFFSET] = KeyPair.ALG_EC_F2M; - setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 113); - testSupportECGivenAlg(testAPDU, cardManager); - setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 131); - testSupportECGivenAlg(testAPDU, cardManager); - setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 163); - testSupportECGivenAlg(testAPDU, cardManager); - setShort(testAPDU, TESTECSUPPORT_KEYLENGTH_OFFSET, (short) 193); - testSupportECGivenAlg(testAPDU, cardManager); - - } - public static void main(String[] args) { - try { - // - // REAL CARDS - // - if (cardManager.ConnectToCard()) { - - testSupportECAll(cardManager); - - // Test setting invalid parameter B of curve - byte[] testAPDU = Arrays.clone(TESTECSUPPORTALL_FP_KEYGEN_INVALIDCURVEB); - //testFPkeyGen_setCorruptionType(testAPDU, SimpleECCApplet.CORRUPT_B_LASTBYTEINCREMENT); - testFPkeyGen_setCorruptionType(testAPDU, SimpleECCApplet.CORRUPT_B_ONEBYTERANDOM); - //testFPkeyGen_setCorruptionType(testAPDU, SimpleECCApplet.CORRUPT_B_FULLRANDOM); - testFPkeyGen_setNumRepeats(testAPDU, (short) 10); - testFPkeyGen_rewindOnSuccess(testAPDU, true); - ReconnnectToCard(); - ResponseAPDU resp_fp_keygen = cardManager.sendAPDU(testAPDU); - ResponseAPDU resp_keygen_params = cardManager.sendAPDU(TESTECSUPPORTALL_LASTUSEDPARAMS); - PrintECKeyGenInvalidCurveB(resp_fp_keygen); - PrintECKeyGenInvalidCurveB_lastUserParams(resp_keygen_params); - - /* - // Test support for different types of curves - ReconnnectToCard(); - ResponseAPDU resp_fp = cardManager.sendAPDU(TESTECSUPPORTALL_FP); - ReconnnectToCard(); - ResponseAPDU resp_f2m = cardManager.sendAPDU(TESTECSUPPORTALL_F2M); - PrintECSupport(resp_fp); - PrintECSupport(resp_f2m); - */ - - cardManager.DisconnectFromCard(); - } else { - System.out.println("Failed to connect to card"); - } - } catch (Exception ex) { - System.out.println("Exception : " + ex); - } - } - - static String getPrintError(short code) { - if (code == ISO7816.SW_NO_ERROR) { - return "OK\t(0x9000)"; - } - else { - String codeStr = "unknown"; - if (code == CryptoException.ILLEGAL_VALUE) { - codeStr = "ILLEGAL_VALUE"; - } - if (code == CryptoException.UNINITIALIZED_KEY) { - codeStr = "UNINITIALIZED_KEY"; - } - if (code == CryptoException.NO_SUCH_ALGORITHM) { - codeStr = "NO_SUCH_ALG"; - } - if (code == CryptoException.INVALID_INIT) { - codeStr = "INVALID_INIT"; - } - if (code == CryptoException.ILLEGAL_USE) { - codeStr = "ILLEGAL_USE"; - } - if (code == SimpleECCApplet.SW_SKIPPED) { - codeStr = "skipped"; - } - if (code == SimpleECCApplet.SW_KEYPAIR_GENERATED_INVALID) { - codeStr = "SW_KEYPAIR_GENERATED_INVALID"; - } - if (code == SimpleECCApplet.SW_INVALID_CORRUPTION_TYPE) { - codeStr = "SW_INVALID_CORRUPTION_TYPE"; - } - return String.format("fail\t(%s,\t0x%4x)", codeStr, code); - } - } - - enum ExpResult { - SHOULD_SUCCEDD, - MAY_FAIL, - MUST_FAIL - } - static int VerifyPrintResult(String message, byte expectedTag, byte[] buffer, int bufferOffset, ExpResult expRes) { - if (bufferOffset >= buffer.length) { - System.out.println(" No more data returned"); - } - else { - if (buffer[bufferOffset] != expectedTag) { - System.out.println(" ERROR: mismatched tag"); - assert(buffer[bufferOffset] == expectedTag); - } - bufferOffset++; - short resCode = getShort(buffer, bufferOffset); - bufferOffset += 2; - - boolean bHiglight = false; - if ((expRes == ExpResult.MUST_FAIL) && (resCode == ISO7816.SW_NO_ERROR)) { - bHiglight = true; - } - if ((expRes == ExpResult.SHOULD_SUCCEDD) && (resCode != ISO7816.SW_NO_ERROR)) { - bHiglight = true; - } - if (bHiglight) { - System.out.println(String.format("!! %-50s%s", message, getPrintError(resCode))); - } - else { - System.out.println(String.format(" %-50s%s", message, getPrintError(resCode))); - } - } - return bufferOffset; - } - static void PrintECSupport(ResponseAPDU resp) { - byte[] buffer = resp.getData(); - - System.out.println(); - System.out.println("### Test for support and with valid and invalid EC curves"); - int bufferOffset = 0; - while (bufferOffset < buffer.length) { - assert(buffer[bufferOffset] == SimpleECCApplet.ECTEST_SEPARATOR); - bufferOffset++; - String ecType = "unknown"; - if (buffer[bufferOffset] == KeyPair.ALG_EC_FP) { - ecType = "ALG_EC_FP"; - } - if (buffer[bufferOffset] == KeyPair.ALG_EC_F2M) { - ecType = "ALG_EC_F2M"; - } - System.out.println(String.format("%-53s%s", "EC type:", ecType)); - bufferOffset++; - short keyLen = getShort(buffer, bufferOffset); - System.out.println(String.format("%-53s%d bits", "EC key length (bits):", keyLen)); - bufferOffset += 2; - - bufferOffset = VerifyPrintResult("KeyPair object allocation:", SimpleECCApplet.ECTEST_ALLOCATE_KEYPAIR, buffer, bufferOffset, ExpResult.SHOULD_SUCCEDD); - bufferOffset = VerifyPrintResult("Generate key with def curve (fails if no def):", SimpleECCApplet.ECTEST_GENERATE_KEYPAIR_DEFCURVE, buffer, bufferOffset, ExpResult.MAY_FAIL); - bufferOffset = VerifyPrintResult("Set valid custom curve:", SimpleECCApplet.ECTEST_SET_VALIDCURVE, buffer, bufferOffset, ExpResult.SHOULD_SUCCEDD); - bufferOffset = VerifyPrintResult("Generate key with valid curve:", SimpleECCApplet.ECTEST_GENERATE_KEYPAIR_CUSTOMCURVE, buffer, bufferOffset, ExpResult.SHOULD_SUCCEDD); - bufferOffset = VerifyPrintResult("ECDH agreement with valid point:", SimpleECCApplet.ECTEST_ECDH_AGREEMENT_VALID_POINT, buffer, bufferOffset, ExpResult.SHOULD_SUCCEDD); - bufferOffset = VerifyPrintResult("ECDH agreement with invalid point (fail is good):", SimpleECCApplet.ECTEST_ECDH_AGREEMENT_INVALID_POINT, buffer, bufferOffset, ExpResult.MUST_FAIL); - bufferOffset = VerifyPrintResult("Set invalid custom curve (may fail):", SimpleECCApplet.ECTEST_SET_INVALIDCURVE, buffer, bufferOffset, ExpResult.MAY_FAIL); - bufferOffset = VerifyPrintResult("Generate key with invalid curve (fail is good):", SimpleECCApplet.ECTEST_GENERATE_KEYPAIR_INVALIDCUSTOMCURVE, buffer, bufferOffset, ExpResult.MUST_FAIL); - - System.out.println(); - } - } - static void PrintECKeyGenInvalidCurveB(ResponseAPDU resp) { - byte[] buffer = resp.getData(); - - System.out.println(); - System.out.println("### Test for computation with invalid parameter B for EC curve"); - int bufferOffset = 0; - while (bufferOffset < buffer.length) { - assert (buffer[bufferOffset] == SimpleECCApplet.ECTEST_SEPARATOR); - bufferOffset++; - String ecType = "unknown"; - if (buffer[bufferOffset] == KeyPair.ALG_EC_FP) { - ecType = "ALG_EC_FP"; - } - if (buffer[bufferOffset] == KeyPair.ALG_EC_F2M) { - ecType = "ALG_EC_F2M"; - } - System.out.println(String.format("%-53s%s", "EC type:", ecType)); - bufferOffset++; - short keyLen = getShort(buffer, bufferOffset); - System.out.println(String.format("%-53s%d bits", "EC key length (bits):", keyLen)); - bufferOffset += 2; - - short numRepeats = getShort(buffer, bufferOffset); - bufferOffset += 2; - System.out.println(String.format("%-53s%d times", "Executed repeats before unexpected error: ", numRepeats)); - - - bufferOffset = VerifyPrintResult("KeyPair object allocation:", SimpleECCApplet.ECTEST_ALLOCATE_KEYPAIR, buffer, bufferOffset, ExpResult.SHOULD_SUCCEDD); - while (bufferOffset < buffer.length) { - bufferOffset = VerifyPrintResult("Set invalid custom curve:", SimpleECCApplet.ECTEST_SET_INVALIDCURVE, buffer, bufferOffset, ExpResult.SHOULD_SUCCEDD); - bufferOffset = VerifyPrintResult("Generate key with invalid curve (fail is good):", SimpleECCApplet.ECTEST_GENERATE_KEYPAIR_INVALIDCUSTOMCURVE, buffer, bufferOffset, ExpResult.MUST_FAIL); - if (buffer[bufferOffset] == SimpleECCApplet.ECTEST_DH_GENERATESECRET) { - bufferOffset = VerifyPrintResult("ECDH agreement with invalid point (fail is good):", SimpleECCApplet.ECTEST_DH_GENERATESECRET, buffer, bufferOffset, ExpResult.MUST_FAIL); - } - bufferOffset = VerifyPrintResult("Set valid custom curve:", SimpleECCApplet.ECTEST_SET_VALIDCURVE, buffer, bufferOffset, ExpResult.SHOULD_SUCCEDD); - bufferOffset = VerifyPrintResult("Generate key with valid curve:", SimpleECCApplet.ECTEST_GENERATE_KEYPAIR_CUSTOMCURVE, buffer, bufferOffset, ExpResult.SHOULD_SUCCEDD); - } - - System.out.println(); - } - } - - static void PrintECKeyGenInvalidCurveB_lastUserParams(ResponseAPDU resp) { - byte[] buffer = resp.getData(); - short offset = 0; - System.out.print("Last used value of B: "); - while (offset < buffer.length) { - System.out.print(String.format("%x ", buffer[offset])); - offset++; - } - - } -} |
