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

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

Open cgm7487 opened 5 years ago

cgm7487 commented 5 years ago

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.

petrs commented 5 years ago

Hi, thank you for reporting the problem. What card do you use please?

cgm7487 commented 5 years ago

Hi. I use this card: https://www.taisys.com/p-detail?lang=&id=9bf9kZO3CJ682Mn-Uuesyonyz9d5ZdPycGwMJF2i3Q Basically, it supports all Java card 3.0.4 functions. Here is its algorithms test report: https://github.com/crocs-muni/JCAlgTest/blob/master/Profiles/results/Taisys_SIMoME_VAULT_ICFabDate_2016_ALGSUPPORT__3b_9f_95_80_3f_c7_a0_80_31_e0_73_fa_21_10_63_00_00_00_83_f0_90_00_bb_(provided_by_PetrS).csv

petrs commented 5 years ago

Hi, I do have this card so I can test and verify (we haven't tested on this one yet). However, it will take some time so if you will find the problem in meantime, your pull request is welcomed

cgm7487 commented 5 years ago

OK. It's awesome.

cgm7487 commented 5 years ago

Hi. I found this issue is from "modulo.append_zeros()". Because it appends the zeros to the modulo, it makes modulo become even value. It seems that Taisys sim card will throw the exception when the modulo value is not illegal.

sm3q96 commented 5 years ago

Hi, I found that it's because exponent and modulo cannot be even when utilizing RSA, so there were errors when append zeros to modulo and when the exponent is even.

I modified append_zeros() to multiply a big odd prime as "q" in RSA (N=pq). Take this calculation [ANS_ExpMod = base^exp(mod p)] as an example,

First, there is a conditional expression to check if exp is even. If yes, exp will be (exp-1) and the answer should multiply base once at final.

Second, modulo = p = FFFFFFFF 00000001 00000000 00000000 00000000 FFFFFFFF FFFFFFFF FFFFFFFF. Then, I didn't use "append zeros()". Instead, I choose a big odd prime q and utilize RSA to calculate TMP = base^exp(mod N) = base^exp(mod pq).

Finally, ANS_ExpMod = TMP (mod p). If exp is even, then ANS_ExpMod = mod_mult(TMP, base, p)