OpenCryptoProject / JCMathLib

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

ArrayIndexOutOfBoundsException in all the mathematical operations #31

Closed Faedon closed 1 year ago

Faedon commented 1 year ago

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)

dufkan commented 1 year ago

This looks like the applet was not initialized properly. JCMathLib uses deferred initialization on the JCOP4 P71 card, during which object allocation happens after an instruction of the applet is used. However, if it fails (typically due to memory constraints), this behavior can occur. If multiple applets are installed on the card, you can try uninstalling them to free up more space for JCMathLib.