Code Monkey home page Code Monkey logo

jcmathlib's Introduction

MIT licensed

JCMathLib is an open-source library for the JavaCard platform that aims to enable low-level cryptographic computations unavailable in the standard JavaCard API. In particular, it focuses on providing efficient modular arithmetic and elliptic curve operations.

If you want to get into the technical details of JCMathLib, you can find them in this paper: https://arxiv.org/abs/1810.01662.

When citing our work, please use the following reference:

@inproceedings{mavroudis2020jcmathlib,
  title={JCMathLib: wrapper cryptographic library for transparent and certifiable JavaCard applets},
  author={Mavroudis, Vasilios and Svenda, Petr},
  booktitle={2020 IEEE European Symposium on Security and Privacy Workshops (EuroS\&PW)},
  pages={89--96},
  year={2020},
  organization={IEEE}
}

Table of Contents

Features and Limitations

JCMathLib includes the following features:

  • BigNat arithmetic including modular operations
  • Elliptic curve point addition and multiplication
  • Option to accelerate computation by utilizing int native type on smartcards that support it ( branch ints)
  • No dependencies on proprietary interfaces (only public JavaCard API)
  • Selection of appropriate algorithm implementation based on the card's algorithm support (OperationSupport)
  • Resource management (ObjectAllocator, ObjectLocker)
  • Predefined common elliptic curves (SecP256r1, SecP256k1, SecP512r1)
  • Tool for packaging JCMathLib into a single file for easy integration

Although higher-level cryptographic primitives and protocols can be constructed using JCMathLib, they are not included in the library. In case you need a higher-level implementation, you may try looking for projects building on top of JCMathLib (e.g., see our users).

As JCMathLib is implemented for the JavaCard Platform and relies only on public JavaCard API, it is not as efficient as a native implementation could be. This approach has a number of advantages, like easy portability and the possibility to open-source code. However, it makes it much harder (if not impossible) to perform the operations in constant time, and we do not aim to. The library is thus vulnerable to timing side-channel attacks and is NOT suited for production use.

Getting Started

Clone this repository:

git clone --recurse-submodules https://github.com/OpenCryptoProject/JCMathLib.git

For compilation for JavaCards, you need to obtain JavaCard SDKs, which are included as submodule. If you did not use --recurse-submodules in the previous command and your libs-sdks folder is empty, run:

git submodule update --init --recursive

Before using JCMathLib in your projects, you should test that it works properly on your smartcard. For that, you may want to run UnitTests. If you plan to work only with a simulator, you can skip to the last step of the following section.

Running unit tests

  1. Set your card type in the JCMathLib/applet/src/main/java/opencrypto/jcmathlib/UnitTests class. The supported options are listed in class OperationSupport.{SIMULATOR, JCOP21, JCOP3_P60, JCOP4_P71, GD60, GD70, SECORA}.
public class UnitTests extends Applet {
    public final static short CARD_TYPE = OperationSupport.SIMULATOR; // TODO set your card here
  1. OPTIONAL (depending on card selected in step 1). Change the JavaCard API version in applet/build.gradle file if you wish to run the code on cards with a JavaCard API version different from 3.0.5.
// JC310b43 supports building also for lower versions (cap.targetsdk).
// If another SDK is selected, please comment the cap.targetsdk setting.
final def JC_SELECTED = JC310b43 <----
...
// JC310b43 supports compilation targeting for lower API versions.
// Here you can specify path to the SDK you want to use.
// Only JC304 and higher are supported for targeting.
// If JC310b43 is not used, targetsdk cannot be set.
targetsdk JC305  <----

If you would like to build for lower versions, comment out line with targetsdk JC305 and set final def JC_SELECTED = JC310b43 to other value like final def JC_SELECTED = JC222.

The list of settings is summarized here:

Card OperationSupport JC_SELECTED targetsdk Notes
jCardSim simulator SIMULATOR -- -- (JavaCard API settings are ignored)
NXP J2E145G JCOP21 JC303 remove
NXP JCOP3 J3H145 P60 JCOP3_P60 JC310b43 JC304
NXP JCOP4 J3Rxxx P71 JCOP4_P71 JC310b43 JC305
G+D Sm@rtcafe 6.0 GD60 JC303 remove
G+D Sm@rtcafe 7.0 GD70 JC310b43 JC304
Infineon Secora ID S SECORA JC310b43 JC305 (may require AES256 GP keys)
  1. Build the applet by running the following command.
./gradlew buildJavaCard
  1. If the build completes successfully, you may install it on a card by running the following command. In case you encounter some issues, you may want to try using GlobalPlatformPro directly and install the built cap file applet/build/javacard/unit_tests.cap.
./gradlew installJavaCard

If the installation completes successfully, you can run the tests. If the UnitTests contain your card type, the following command will try to run the tests with a connected card. Otherwise, it will run the tests just in a simulator.

./gradlew --rerun-tasks test

If you have multiple readers connected to your device, you may need to adjust the reader index (runCfg.setTargetReaderIndex in JCMathLib/applet/src/test/java/tests/BaseTest).

Example usage

For an example usage of the library, see the Example applet.

Integration With Your Applet

To enable easy integration of JCMathLib with your applet, we provide a Python script that bundles JCMathLib into a single .java that can be included in your code.

The script provides the following interface, allowing to specify which parts of JCMathLib to include (to save memory).

$ python package.py -h
usage: package.py [-h] [-d DIR] [-k] [-c {SecP256k1,SecP256r1,SecP512r1} [{SecP256k1,SecP256r1,SecP512r1} ...]] [-p PACKAGE] [-o OUTPUT]

Package the JCMathLib library into a single file.

options:
  -h, --help            show this help message and exit
  -d DIR, --dir DIR     Directory to package
  -k, --keep-locks      Keep locks
  -c {SecP256k1,SecP256r1,SecP512r1} [{SecP256k1,SecP256r1,SecP512r1} ...], --curves {SecP256k1,SecP256r1,SecP512r1} [{SecP256k1,SecP256r1,SecP512r1} ...]
                        Curves to include
  -p PACKAGE, --package PACKAGE
                        Package name
  -o OUTPUT, --output OUTPUT
                        Output file

For example, to bundle JCMathLib for your applet test in which you use curve SecP256k1, use the following. The output will be stored in jcmathlib.java file.

$ python package.py -p test -c SecP256k1 -o jcmathlib.java

Community

JCMathLib is kindly supported by:

How to contribute

We welcome all contributions, but we especially appreciate contributions in the following form:

  • Code improvements. If you discover a bug or have an idea for improving the code, please, submit the change in a Pull Request.
  • Features. If you wish certain feature was included in JCMathLib, let us know via Issues or implement it yourself and submit a Pull Request.
  • Testing on cards. If you have a smart card model that is not yet included in JCMathLib and you manage to get it working, please, create a pull request with the corresponding OperationSupport configuration and include information about the smart card. Also consider submitting your card results to JCAlgTest.

Our users

  • Myst: Secure Multiparty Key Generation, Signature and Decryption JavaCard applet and host application
  • BioID: a Privacy-Friendly Identity Document
  • JCEd25519: a JavaCard implementation of Ed25519 signing

(If you can't find yourself here, please let us know via Issues)

jcmathlib's People

Contributors

dufkan avatar jjanku avatar lzaoral avatar mavroudisv avatar mvondracek avatar petrs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jcmathlib's Issues

Create large table with supported operations on selected cards

So far, wiki lists three cards which fully supports all JCMathLib operations. Other cards typically supports some subset as cryptographic engines refuses to perform some operations with non-standard arguments (e.g., RSA public key equal to 2 we use for acceleration of modular multiplication) or do not have enough memory to run in the fully optimized version.

Goal: Create table listing supported high-operations and identify part of code which causes faulty behaviour.

ArrayIndexOutOfBoundsException in all the mathematical operations

Hello @petrs,
When i run the tests or manually send APDUs to my card, I am getting FF02 as response for all the operations for all the different data I provided. An example of integerMultiplication() test can be seen below. All the other functions give exactly the same error. Only thing that i have changed from the original test code is the curve Secp256k1 instead of Secp256r1.

I am using JCOP4 P71 card, which normally supports these operations.
Codes are updated to JCMathLib 2.0.

How can i solve this problem?

tests.JCMathLibTest > IntegerTest STANDARD_OUT
DEBUG | 2023-07-21 18:30:43 | [Test worker] client.CardManager:161 | Looking for physical cards...
DEBUG | 2023-07-21 18:30:43 | [Test worker] client.CardManager:264 | Connecting...
DEBUG | 2023-07-21 18:30:43 | [Test worker] client.CardManager:269 | Terminal connected
DEBUG | 2023-07-21 18:30:43 | [Test worker] client.CardManager:271 | Establishing channel...
DEBUG | 2023-07-21 18:30:43 | [Test worker] client.CardManager:273 | Channel established
DEBUG | 2023-07-21 18:30:43 | [Test worker] client.CardManager:258 | Smartcard: Selecting applet...
DEBUG | 2023-07-21 18:30:43 | [Test worker] client.Util:120 | --> [00A404000B4A434D6174684C69625554] (16 B)
DEBUG | 2023-07-21 18:30:43 | [Test worker] client.Util:130 | <-- 9000
DEBUG | 2023-07-21 18:30:43 | [Test worker] client.Util:133 | Elapsed time 16 ms

tests.JCMathLibTest > IntegerTest > integerMultiplication() STANDARD_OUT
DEBUG | 2023-07-21 18:30:43 | [Test worker] client.Util:120 | --> [B01205000A000000534D0000001D46] (15 B)
DEBUG | 2023-07-21 18:30:43 | [Test worker] client.Util:130 | <-- FF02
DEBUG | 2023-07-21 18:30:43 | [Test worker] client.Util:133 | Elapsed time 16 ms

tests.JCMathLibTest > IntegerTest > integerMultiplication() FAILED
org.opentest4j.AssertionFailedError: expected: <36864> but was: <65282>
at org.junit.jupiter.api.AssertionUtils.fail(AssertionUtils.java:52)
at org.junit.jupiter.api.AssertEquals.failNotEqual(AssertEquals.java:197)
at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:154)
at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:149)
at org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:305)
at tests.JCMathLibTest$IntegerTest.integerMultiplication(JCMathLibTest.java:579)

Add not working in the simulator

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

Add conditional execution of JCMathLib methods based on card-specific configuration files

Not all operations of JCMathLib are supported on all cards. If executed, some operations will fail or may even block the card (e.g., n_mod_exp on JCOP3 J3H145G see #13). Currently, developer must remove these operations manually from the code.

Solution:
Add simple java file CardSupportCfg.java containing true/false param for each method supported by JCMathLib. The parameter is controlled in runtime and if false, then method is not executed (exception) despite being present in code.

For new card, developer can select pre-prepared CardSupportCfg for tested smartcards or create own using simple testing tool

Typo in classname in README

The second example of code in readme should use an ECConfig object, instead of OCConfig, therefore the correct code is:

public ECExample() {
        // Pre-allocate all helper structures
        ecc = new ECConfig((short) 256); 
        // Pre-allocate standard SecP256r1 curve and two EC points on this curve
        ...
}

Inquiry about JCmathlib Support for JavaCard 3.0.4

I have written a JavaCard application and configured the card as JCOP3_P60, which supports JCDK 3.0.4. However, I faced issues when testing with the card, particularly because I noticed that JCmathlib uses ALG_EC_SVDP_DH_PLAIN_XY and ALG_EC_PACE_GM parameters.

The card in version 3.0.5 is working properly.

Could you please clarify if JCmathlib supports JavaCard 3.0.4 cards, or if there are any specific modifications required to achieve compatibility?

Can't get add JCMathLib to work with two ECPoints

Hello, I downloaded the latest version of JCMathLib and got the below errors.
I then downloaded the latest release (JCMathLib 1.1.0) and got the same result.

I need to be able to add two public keys (BrainpoolP256R1) and read the result. Can you help, please?

        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.randomize(); 
        point2.randomize(); 
        point1.add(point2);

This gave a Crypto error.

I also tried

        ECConfig ecc = new ECConfig((short)256);
        ECCurve curve = new ECCurve(
                false,
                Brainpool256R1.getFp(),
                Brainpool256R1.getA(),
                Brainpool256R1.getB(),
                Brainpool256R1.getG(),
                Brainpool256R1.getR()
                );
        ECPoint point1 = new ECPoint(curve, ecc.ech);
        ECPoint point2 = new ECPoint(curve, ecc.ech);
        point1.setW(cryptoBuf, (short)0, hashLen);
        point2.setW(CA, (short)0, (short)CA.length);
        point1.add(point2);

This gave the same error.

Testing card G&D7.0

Not working correctly so far

EC scalar_Point Multiplication: (0)
--> B0432000615C3EF475BB7E30F7FA387D3A83241C9F98AAAA48A39C5D30473ADC016C5CA7F3046B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C2964FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5
<-- F105 [1285 ms]
fail (0xf105)

EC Point Double: (0)
--> B04541004104817B6288CBAD197920ED7AA8C0C2A968B77A15C9DE776DC3015C4E1AA8426D9AE73A562E85B536BDF8CAB8466A354EDEC3F9935662D4228DF0090D6E93175E5A
javax.smartcardio.CardException: sun.security.smartcardio.PCSCException: Unknown error 0x1f
at java.smartcardio/sun.security.smartcardio.ChannelImpl.doTransmit(ChannelImpl.java:223)

BigNatural Exponentiation (Modulo) and BigNatural Inversion (Modulo) unit test failed

Hi, When I tried to use the physical sim card to test the BigNatural Exponentiation (Modulo) and BigNatural Inversion (Modulo) unit tests, it always shows me the failed message.

[java] BigNatural Exponentiation (Modulo): (0)
[java] num1: 650c556c89d8b9af65f42c3c88af4fcb
[java] num2: 2
[java] num3: 3983260343894b673635da5714ff2c71
[java] --> B033100121650C556C89D8B9AF65F42C3C88AF4FCB023983260343894B673635DA5714FF2C71
[java] javax.smartcardio.CardException: sun.security.smartcardio.PCSCException: Unknown error 0x8010002f
[java] at sun.security.smartcardio.ChannelImpl.doTransmit(ChannelImpl.java:219)
[java] at sun.security.smartcardio.ChannelImpl.transmit(ChannelImpl.java:90)
[java] at opencrypto.test.CardManager.transmit(Unknown Source)
[java] at opencrypto.test.TestClient.performCommand(Unknown Source)
[java] at opencrypto.test.TestClient.OpenCryptoFunctionalTests(Unknown Source)
[java] at opencrypto.test.TestClient.main(Unknown Source)
[java] Caused by: sun.security.smartcardio.PCSCException: Unknown error 0x8010002f
[java] at sun.security.smartcardio.PCSC.SCardTransmit(Native Method)
[java] at sun.security.smartcardio.ChannelImpl.doTransmit(ChannelImpl.java:189)
[java] ... 5 more

Do you have the same issue on you latest code? Thank you.

Problem Using `INS_BN_EXP_MOD` with simulator

I am trying to use modular exponentiation for two big numbers in JCIDE simulator. I have run the test code and try to calculate:

a = e39de17291bce3c29354424b959f330cbce9cab40c3f7c76fd4fdd8fc31ff553
b = 9ec03b35bc2f449ecd4e16724320d9df17f18cd5f04dbe441d16c2343ef002f3
c = 1669c4b9e5a03f1382f661beaf89b1ff451dd65a2a0aaf192765d78011d2b345
a^b mod c

I send APDU below:
<<B0 33 20 20 60 E3 9D E1 72 91 BC E3 C2 93 54 42 4B 95 9F 33 0C BC E9 CA B4 0C 3F 7C 76 FD 4F DD 8F C3 1F F5 53 9E C0 3B 35 BC 2F 44 9E CD 4E 16 72 43 20 D9 DF 17 F1 8C D5 F0 4D BE 44 1D 16 C2 34 3E F0 02 F3 16 69 C4 B9 E5 A0 3F 13 82 F6 61 BE AF 89 B1 FF 45 1D D6 5A 2A 0A AF 19 27 65 D7 80 11 D2 B3 45

but I get an exception F1 01
what am I doing wrong?

Comply with JC Specification

Since JCMathLib implements some classes from the specification (int, BigNumber) it would be useful to fully comply with it, whenever possible.

In most cases, we provide additional functionality and that's ok.
I'm planning to do two things:

  • Make sure that we provide all the methods found on the specification and we have the same signature.
  • Rename classes and methods to match those of the spec.

This will temporarily hurt backwards compatibility, but in the long run will makes things easier, especially in cases where some of these classes start to appear in commercial cards.

EC scalar_Point Multiplication and EC Point Double fails with 0xff05 (SW_NullPointerException)

EC scalar_Point Multiplication:
cardMngr.transmit(new CommandAPDU(hexStringToByteArray("B043200061201A1C4AA2D77D116F1730C186AF4F1BAA617FB82670D074AE24CD40457F59DD046B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C2964FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5")));
static byte[] failedCommand = {(byte) 0xb0, (byte) 0x43, (byte) 0x20, (byte) 0x0, (byte) 0x61, (byte) 0x20, (byte) 0x1a, (byte) 0x1c, (byte) 0x4a, (byte) 0xa2, (byte) 0xd7, (byte) 0x7d, (byte) 0x11, (byte) 0x6f, (byte) 0x17, (byte) 0x30, (byte) 0xc1, (byte) 0x86, (byte) 0xaf, (byte) 0x4f, (byte) 0x1b, (byte) 0xaa, (byte) 0x61, (byte) 0x7f, (byte) 0xb8, (byte) 0x26, (byte) 0x70, (byte) 0xd0, (byte) 0x74, (byte) 0xae, (byte) 0x24, (byte) 0xcd, (byte) 0x40, (byte) 0x45, (byte) 0x7f, (byte) 0x59, (byte) 0xdd, (byte) 0x4, (byte) 0x6b, (byte) 0x17, (byte) 0xd1, (byte) 0xf2, (byte) 0xe1, (byte) 0x2c, (byte) 0x42, (byte) 0x47, (byte) 0xf8, (byte) 0xbc, (byte) 0xe6, (byte) 0xe5, (byte) 0x63, (byte) 0xa4, (byte) 0x40, (byte) 0xf2, (byte) 0x77, (byte) 0x3, (byte) 0x7d, (byte) 0x81, (byte) 0x2d, (byte) 0xeb, (byte) 0x33, (byte) 0xa0, (byte) 0xf4, (byte) 0xa1, (byte) 0x39, (byte) 0x45, (byte) 0xd8, (byte) 0x98, (byte) 0xc2, (byte) 0x96, (byte) 0x4f, (byte) 0xe3, (byte) 0x42, (byte) 0xe2, (byte) 0xfe, (byte) 0x1a, (byte) 0x7f, (byte) 0x9b, (byte) 0x8e, (byte) 0xe7, (byte) 0xeb, (byte) 0x4a, (byte) 0x7c, (byte) 0xf, (byte) 0x9e, (byte) 0x16, (byte) 0x2b, (byte) 0xce, (byte) 0x33, (byte) 0x57, (byte) 0x6b, (byte) 0x31, (byte) 0x5e, (byte) 0xce, (byte) 0xcb, (byte) 0xb6, (byte) 0x40, (byte) 0x68, (byte) 0x37, (byte) 0xbf, (byte) 0x51, (byte) 0xf5};

EC Point Double:
cardMngr.transmit(new CommandAPDU(hexStringToByteArray("B041000041044BA5A6CD62B7DAACDC2467B74D30D546FBCFF4002481A3C29867C03D39CBE0CF5EAF576F6DC24FD4DE1754A4E97E4DC4B7A18B3C18C62BC8A7057E653E8FA896")));
static byte[] failedCommand = {(byte) 0xb0, (byte) 0x41, (byte) 0x0, (byte) 0x0, (byte) 0x41, (byte) 0x4, (byte) 0x4b, (byte) 0xa5, (byte) 0xa6, (byte) 0xcd, (byte) 0x62, (byte) 0xb7, (byte) 0xda, (byte) 0xac, (byte) 0xdc, (byte) 0x24, (byte) 0x67, (byte) 0xb7, (byte) 0x4d, (byte) 0x30, (byte) 0xd5, (byte) 0x46, (byte) 0xfb, (byte) 0xcf, (byte) 0xf4, (byte) 0x0, (byte) 0x24, (byte) 0x81, (byte) 0xa3, (byte) 0xc2, (byte) 0x98, (byte) 0x67, (byte) 0xc0, (byte) 0x3d, (byte) 0x39, (byte) 0xcb, (byte) 0xe0, (byte) 0xcf, (byte) 0x5e, (byte) 0xaf, (byte) 0x57, (byte) 0x6f, (byte) 0x6d, (byte) 0xc2, (byte) 0x4f, (byte) 0xd4, (byte) 0xde, (byte) 0x17, (byte) 0x54, (byte) 0xa4, (byte) 0xe9, (byte) 0x7e, (byte) 0x4d, (byte) 0xc4, (byte) 0xb7, (byte) 0xa1, (byte) 0x8b, (byte) 0x3c, (byte) 0x18, (byte) 0xc6, (byte) 0x2b, (byte) 0xc8, (byte) 0xa7, (byte) 0x5, (byte) 0x7e, (byte) 0x65, (byte) 0x3e, (byte) 0x8f, (byte) 0xa8, (byte) 0x96};

library import

Does anyone have any experience turning this into "jar / exp" files, and integrating into an existing applet project?

I understand I can copy the source files into my project, but that's a bit messy. Wondering if there's a better way

Thanks!

Help getting the public key

Hi!

I have this code that gets a private key generated on the Secp256k1 curve and returns a 65 byte public key. Can you help me convert it to use JCMathLib? Thanks!

KeyAgreement keyAgreement = com.licel.jcardsim.crypto.KeyAgreementImpl.getInstance(KeyAgreement.ALG_EC_SVDP_DH_PLAIN_XY, false);

public boolean getUncompressedPublicPoint(byte[] privateKey,
                                              short privateKeyOffset, byte[] publicPoint, short publicPointOffset) {
if (ecAlgorithm != KeyBuilder.TYPE_EC_FP_PRIVATE) {
                    Secp256k1.setCommonCurveParameters(this.privateKey);
                }
                this.privateKey.setS(privateKey, privateKeyOffset, (short)32);
                keyAgreement.init(this.privateKey);
                keyAgreement.generateSecret(Secp256k1.SECP256K1_G, (short)0, (short)Secp256k1.SECP256K1_G.length, publicPoint, publicPointOffset);
}


import javacard.security.ECKey;

public class Secp256k1 {
	
	// Nice SECp256k1 constants, only available during NIST opening hours
	
    protected static final byte SECP256K1_FP[] = {
        (byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,
        (byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,
        (byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,
        (byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFE,(byte)0xFF,(byte)0xFF,(byte)0xFC,(byte)0x2F 
    };    
    protected static final byte SECP256K1_A[] = {
        (byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00, 
        (byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,
        (byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,
        (byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00  
    };
    protected static final byte SECP256K1_B[] = {
        (byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00, 
        (byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,
        (byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,
        (byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x07  
    };
    protected static final byte SECP256K1_G[] = {
        (byte)0x04, (byte)0x79,(byte)0xBE,(byte)0x66,(byte)0x7E,(byte)0xF9,(byte)0xDC,(byte)0xBB,(byte)0xAC,
        (byte)0x55,(byte)0xA0,(byte)0x62,(byte)0x95,(byte)0xCE,(byte)0x87,(byte)0x0B,(byte)0x07,
        (byte)0x02,(byte)0x9B,(byte)0xFC,(byte)0xDB,(byte)0x2D,(byte)0xCE,(byte)0x28,(byte)0xD9,
        (byte)0x59,(byte)0xF2,(byte)0x81,(byte)0x5B,(byte)0x16,(byte)0xF8,(byte)0x17,(byte)0x98,
        (byte)0x48,(byte)0x3A,(byte)0xDA,(byte)0x77,(byte)0x26,(byte)0xA3,(byte)0xC4,(byte)0x65,
        (byte)0x5D,(byte)0xA4,(byte)0xFB,(byte)0xFC,(byte)0x0E,(byte)0x11,(byte)0x08,(byte)0xA8,
        (byte)0xFD,(byte)0x17,(byte)0xB4,(byte)0x48,(byte)0xA6,(byte)0x85,(byte)0x54,(byte)0x19,
        (byte)0x9C,(byte)0x47,(byte)0xD0,(byte)0x8F,(byte)0xFB,(byte)0x10,(byte)0xD4,(byte)0xB8  
    };
    protected static final byte SECP256K1_R[] = {
        (byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,
        (byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFE,
        (byte)0xBA,(byte)0xAE,(byte)0xDC,(byte)0xE6,(byte)0xAF,(byte)0x48,(byte)0xA0,(byte)0x3B,
        (byte)0xBF,(byte)0xD2,(byte)0x5E,(byte)0x8C,(byte)0xD0,(byte)0x36,(byte)0x41,(byte)0x41
    };
    protected static final byte SECP256K1_K = (byte)0x01;
    
    protected static boolean setCommonCurveParameters(ECKey key) {
    	try {
    		key.setA(SECP256K1_A, (short)0, (short)SECP256K1_A.length);
    		key.setB(SECP256K1_B, (short)0, (short)SECP256K1_B.length);
    		key.setFieldFP(SECP256K1_FP, (short)0, (short)SECP256K1_FP.length);
    		key.setG(SECP256K1_G, (short)0, (short)SECP256K1_G.length);
    		key.setR(SECP256K1_R, (short)0, (short)SECP256K1_R.length);
    		key.setK(SECP256K1_K);
    		return true;
    	}
    	catch(Exception e) {
    		return false;
    	}
    }
}

Problem with NXP J3H145G P60 - UNRESPONSIVE_CARD after EC_ADD operation

Operation which causes the card to become unresponsive.
Fortunately, after around 1 minute outside reader, card becomes working again

EC Point Add: (0)
--> B0420000820492E223622FC472D4B465F484E9926397D557E85FFFB26EEB5E42043B715605DD70B782E70E269FB0B58C45B8ACEF2ABA5B5844040B831A1848892C3315E22E460404F77A3222A39F60ACAF6EC6D724841586C178231BA768F7212309561E93D8D440C1E478C12EAAEB6B15FA19D799F263311A8A272803E47F8CB71C8E2ED1EB77
javax.smartcardio.CardException: sun.security.smartcardio.PCSCException: Unknown error 0x45d
at java.smartcardio/sun.security.smartcardio.ChannelImpl.doTransmit(ChannelImpl.java:223)
...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.