OpenCryptoProject / JCMathLib

Implementation of mathematical operations with big numbers and elliptic curve points for smart cards with JavaCard platform.
MIT License
85 stars 29 forks source link

Add not working in the simulator #29

Closed tintin10q closed 1 year ago

tintin10q commented 1 year ago

At first I could not multiplication did not work for me but then I just set everything to true in OperationSupport and then multiplication started to work.

I am getting a non supported error in the simulator for add. I am just running the example in the simulator like this:


public class testTerminal {

    // I took this from the ECExample.java from jcmathlib

 class SimulatedCardThread extends Thread {
        public void run() {
            // Obtain a CardTerminal
            CardTerminals cardTerminals = CardTerminalSimulator.terminals("My terminal 1");
            CardTerminal terminal1 = cardTerminals.getTerminal("My terminal 1");

            // Create simulator and install applet
            CardSimulator simulator = new CardSimulator();
            AID testAppletAID = new AID(TEST_APPLET_AID, (byte) 0, (byte) 7);
            simulator.installApplet(testAppletAID, rentApplet.class);

            // Insert Card into "My terminal 1"
            simulator.assignToTerminal(terminal1);

            try {
                Card card = terminal1.connect("*");

                applet = card.getBasicChannel();
                ResponseAPDU resp = applet.transmit(SELECT_APDU);
                if (resp.getSW1() != 144) {
                    throw new Exception("Select failed");
                }
            } catch (Exception e) {
                System.err.println("Card status problem!");
                System.err.println(e);
            }
            System.err.println("Successfully connected");
        }
    }

public static void main(String[] arg) {

       ECConfig ecc = new ECConfig((short) 256);
        ECCurve curve = new ECCurve(false, SecP256r1.p, SecP256r1.a, SecP256r1.b, SecP256r1.G, SecP256r1.r);

        ECPoint point1 = new ECPoint(curve, ecc.ech);
        ECPoint point2 = new ECPoint(curve, ecc.ech);

        point1.add(point2);
 }
}

But I get this error:

Exception in thread "main" java.lang.IllegalArgumentException: RSA modulus is even
        at com.licel.jcardsim.bouncycastle.crypto.params.RSAKeyParameters.validate(Unknown Source)
        at com.licel.jcardsim.bouncycastle.crypto.params.RSAKeyParameters.<init>(Unknown Source)
        at com.licel.jcardsim.bouncycastle.crypto.params.RSAKeyParameters.<init>(Unknown Source)
        at com.licel.jcardsim.crypto.RSAKeyImpl.getParameters(RSAKeyImpl.java:110)
        at com.licel.jcardsim.crypto.AsymmetricCipherImpl.init(AsymmetricCipherImpl.java:82)
        at opencrypto.jcmathlib.Bignat.n_mod_exp(Bignat.java:1841)
        at opencrypto.jcmathlib.Bignat.mod_exp(Bignat.java:1742)
        at opencrypto.jcmathlib.Bignat.mod_inv(Bignat.java:1723)
        at opencrypto.jcmathlib.ECPoint.swDouble(ECPoint.java:183)
        at opencrypto.jcmathlib.ECPoint.multiplication(ECPoint.java:355)
        at opencrypto.jcmathlib.ECPoint.add(ECPoint.java:223)

I uploaded the simulator I am using to github.

jcardsim-3.0.5-20230313.131323-6.jar.zip

tintin10q commented 1 year ago

Do you know what is happening here? Is this possible to fix?

tintin10q commented 1 year ago

ok I changed the OperationSupport to be like this:

package opencrypto.jcmathlib;

/**
 * OperationSupport class
 * 
 * @author Antonin Dufka
 */
public class OperationSupport {
    private static OperationSupport instance;

    public static final short SIMULATOR = 0x0000;
    public static final short J2E145G = 0x0001;
    public static final short J3H145 = 0x0002;
    public static final short J3R180 = 0x0003;

    public boolean RSA_MULT_TRICK = false;
    public boolean RSA_MOD_EXP = true;
    public boolean RSA_PREPEND_ZEROS = true;
    public boolean RSA_KEY_REFRESH = true;
    public boolean ECDH_XY = true;
    public boolean ECDH_X_ONLY = true;
    public boolean EC_SW_DOUBLE = false;

    private OperationSupport() {}

    public static OperationSupport getInstance() {
        if (OperationSupport.instance == null)
            OperationSupport.instance = new OperationSupport();
        return OperationSupport.instance;
    }

    public void setCard(short card_identifier) {
        System.out.print("Card identifier SET CARD:");
        System.out.println(card_identifier);
        switch (card_identifier) {
            case SIMULATOR:
                RSA_MULT_TRICK = true;
                RSA_MOD_EXP = false;
                RSA_PREPEND_ZEROS = true;
                RSA_KEY_REFRESH = true;
                 ECDH_XY = true;
                EC_SW_DOUBLE = true;
                break;
            case J2E145G:
                RSA_MULT_TRICK = true;
                RSA_MOD_EXP = true;
                break;
            case J3H145:
                RSA_MULT_TRICK = true;
                RSA_MOD_EXP = true;
                break;
            case J3R180:
                RSA_MULT_TRICK = true;
                RSA_MOD_EXP = true;
                break;
            default:
                break;
        }
    }
}

Now it does multiplication and it adds point to itself but only if they are the same points. Other points does not work yet.

tintin10q commented 1 year ago

I think the issue is that the bouncy castle in the simulator is checking if the modulus is even and trowing an error if it is.

dufkan commented 1 year ago

Yes, that is likely the case. New versions of jCardSim introduced more restrictions on modulus, and in the default setting, it is not compatible with JCMathLib 1.1.0. If you want to use this new simulator version with JCMathLib, you need to set property com.licel.jcardsim.bouncycastle.rsa.allow_unsafe_mod to true. Still, it may require more changes, as I remember we had to also change the computation a bit to get JCMathLib fully working in this simulator. See the new version of JCMathLib.

The property is already set for the simulator used for testing in JCMathLib version 2.0.

https://github.com/OpenCryptoProject/JCMathLib/blob/35f70516f91d100af99868670b993b2395072afa/applet/src/test/java/tests/BaseTest.java#L62

dufkan commented 1 year ago

The new version of the library has been released, which should fix the issue. If the problem persists, please, reopen this issue.