Page 1 of 1

Multiply big numbers

Posted: Tue Apr 17, 2018 11:27 am
by djordjem
Hello everyone

Does anyone know how I can multiply two big numbers on javacard? I have two byte arrays size of 128 bytes.
I was trying to implement code in link below but it doesn't work for me.
https://stackoverflow.com/questions/36966764/using-rsa-for-modulo-multiplication-leads-to-error-on-java-card

Best regards,
djordjem

Re: Multiply big numbers

Posted: Tue Apr 17, 2018 10:07 pm
by Tolice
You can specifically describe the problem in here, so that others can help you better

Re: Multiply big numbers

Posted: Wed Apr 18, 2018 5:00 am
by roundtable
HI, guy, are you sure your javacard provide those special API for big integer modular arithmetic ? the standard javacard API can't support your requirements. you may need special javacard equipped with non-standard API to support big integer operation, such as modular add/sub/multiply/GCD and so on. you can contact to javacardos@gmail.com for details.

Re: Multiply big numbers

Posted: Wed Apr 18, 2018 5:39 am
by Riddle
What format do you used? decimal or hexadecimal? The big number is modular multiplication algorithm, the decimal and hexadecimal are not the same, but the hexadecimal is easier.

Re: Multiply big numbers

Posted: Wed Apr 18, 2018 5:57 am
by BirdKing
It's like private APIs defined by the company. Is big-end or little-end ?

Re: Multiply big numbers

Posted: Wed Apr 18, 2018 8:06 am
by djordjem
Riddle wrote:What format do you used? decimal or hexadecimal? The big number is modular multiplication algorithm, the decimal and hexadecimal are not the same, but the hexadecimal is easier.

I'm using hexadecimal format.

BirdKing wrote:It's like private APIs defined by the company. Is big-end or little-end ?

It's big-end.

Code: Select all

x = {(byte) 0x3E, (byte) 0x42, (byte) 0xD0, (byte) 0x84, (byte) 0xA1, (byte) 0x9E, (byte) 0xCF, (byte) 0x56, (byte) 0x1B, (byte) 0xBD, (byte) 0x7E, (byte) 0x06, (byte) 0xF6, (byte) 0x4A, (byte) 0x47, (byte) 0xB7, (byte) 0x60, (byte) 0x8B, (byte) 0x3E, (byte) 0x42, (byte) 0xD0, (byte) 0x84, (byte) 0xA1, (byte) 0x9E, (byte) 0xCF, (byte) 0x56, (byte) 0x1B, (byte) 0xBD, (byte) 0x7E, (byte) 0x06, (byte) 0xF6, (byte) 0x4A, (byte) 0x47, (byte) 0xB7, (byte) 0x60, (byte) 0x8B, (byte) 0x3E, (byte) 0x42, (byte) 0xD0, (byte) 0x84, (byte) 0xA1, (byte) 0x9E, (byte) 0xCF, (byte) 0x56, (byte) 0x1B, (byte) 0xBD, (byte) 0x7E, (byte) 0x06, (byte) 0xF6, (byte) 0x4A, (byte) 0x47, (byte) 0xB7, (byte) 0x60, (byte) 0x8B, (byte) 0x3E, (byte) 0x42, (byte) 0xD0, (byte) 0x84, (byte) 0xA1, (byte) 0x9E, (byte) 0xCF, (byte) 0x56, (byte) 0x1B, (byte) 0xBD, (byte) 0x7E, (byte) 0x06, (byte) 0xF6, (byte) 0x4A, (byte) 0x47, (byte) 0xB7, (byte) 0x60, (byte) 0x8B, (byte) 0x3E, (byte) 0x42, (byte) 0xD0, (byte) 0x84, (byte) 0xA1, (byte) 0x9E, (byte) 0xCF, (byte) 0x56, (byte) 0x1B, (byte) 0xBD, (byte) 0x7E, (byte) 0x06, (byte) 0xF6, (byte) 0x4A, (byte) 0x47, (byte) 0xB7, (byte) 0x60, (byte) 0x8B, (byte) 0x3E, (byte) 0x42, (byte) 0xD0, (byte) 0x84, (byte) 0xA1, (byte) 0x9E, (byte) 0xCF, (byte) 0x56, (byte) 0x1B, (byte) 0xBD, (byte) 0x7E, (byte) 0x06, (byte) 0xF6, (byte) 0x4A, (byte) 0x47, (byte) 0xB7, (byte) 0x60, (byte) 0x8B, (byte) 0x3E, (byte) 0x42, (byte) 0xD0, (byte) 0x84, (byte) 0xA1, (byte) 0x9E, (byte) 0xCF, (byte) 0x56, (byte) 0x1B, (byte) 0xBD, (byte) 0x7E, (byte) 0x06, (byte) 0xF6, (byte) 0x4A, (byte) 0x47, (byte) 0xB7, (byte) 0x60, (byte) 0x8B, (byte) 0x3E, (byte) 0x42};

y = {(byte) 0x3D, (byte) 0x21, (byte) 0x22, (byte) 0x9F, (byte) 0x05, (byte) 0x63, (byte) 0x03, (byte) 0xF2, (byte) 0xF9, (byte) 0x87, (byte) 0x7E, (byte) 0x9A, (byte) 0x3E, (byte) 0x7D, (byte) 0x40, (byte) 0xE1, (byte) 0x30, (byte) 0x0C, (byte) 0x06, (byte) 0x03, (byte) 0x55, (byte) 0x1D, (byte) 0x3D, (byte) 0x21, (byte) 0x22, (byte) 0x9F, (byte) 0x05, (byte) 0x63, (byte) 0x03, (byte) 0xF2, (byte) 0xF9, (byte) 0x87, (byte) 0x7E, (byte) 0x9A, (byte) 0x3E, (byte) 0x7D, (byte) 0x40, (byte) 0xE1, (byte) 0x30, (byte) 0x0C, (byte) 0x06, (byte) 0x03, (byte) 0x55, (byte) 0x1D, (byte) 0x3D, (byte) 0x21, (byte) 0x22, (byte) 0x9F, (byte) 0x05, (byte) 0x63, (byte) 0x03, (byte) 0xF2, (byte) 0xF9, (byte) 0x87, (byte) 0x7E, (byte) 0x9A, (byte) 0x3E, (byte) 0x7D, (byte) 0x40, (byte) 0xE1, (byte) 0x30, (byte) 0x0C, (byte) 0x06, (byte) 0x03, (byte) 0x55, (byte) 0x1D, (byte) 0x3D, (byte) 0x21, (byte) 0x22, (byte) 0x9F, (byte) 0x05, (byte) 0x63, (byte) 0x03, (byte) 0xF2, (byte) 0xF9, (byte) 0x87, (byte) 0x7E, (byte) 0x9A, (byte) 0x3E, (byte) 0x7D, (byte) 0x40, (byte) 0xE1, (byte) 0x30, (byte) 0x0C, (byte) 0x06, (byte) 0x03, (byte) 0x55, (byte) 0x1D, (byte) 0x3D, (byte) 0x21, (byte) 0x22, (byte) 0x9F, (byte) 0x05, (byte) 0x63, (byte) 0x03, (byte) 0xF2, (byte) 0xF9, (byte) 0x87, (byte) 0x7E, (byte) 0x9A, (byte) 0x3E, (byte) 0x7D, (byte) 0x40, (byte) 0xE1, (byte) 0x30, (byte) 0x0C, (byte) 0x06, (byte) 0x03, (byte) 0x55, (byte) 0x1D, (byte) 0x3D, (byte) 0x21, (byte) 0x22, (byte) 0x9F, (byte) 0x05, (byte) 0x63, (byte) 0x03, (byte) 0xF2, (byte) 0xF9, (byte) 0x87, (byte) 0x7E, (byte) 0x9A, (byte) 0x3E, (byte) 0x7D, (byte) 0x40, (byte) 0xE1, (byte) 0x30, (byte) 0x0C};

p = 0x00b10b8f96a080e01dde92de5eae5d54ec52c99fbcfb06a3c69a6a9dca52d23b616073e28675a23d189838ef1e2ee652c013ecb4aea906112324975c3cd49b83bfaccbdd7d90c4bd7098488e9c219a73724effd6fae5644738faa31a4ff55bccc0a151af5f0dc8b4bd45bf37df365c1a65e68cfda76d4da708df1fb2bc2e4a4371


x*y = 0x0eddfab8081c2f96d29f27332b9323d0fad3bc7b7539c9ad130a6f325a2fd877daff708e46c186cc774a8d8c30c33da3750b0dfc1d72fdeffc8701909f1ede60b825369bf16fba0630eca96bb8bf14d968a6c9b7e4396bf17b97146045feebbbc1be1fd53f726e7f7d8429348ee2078fcf2f5e5152d1ec5725adf33eb37aa07481ec518375c3ff9902b52443688a5535c70b99f625fcfcc1f640089c558b64f29ab06238709bbe9614cbcab98f9a4c326ad9cbf85ea0b61659c2eb716352a7a420abf1d7c29f6487e75e562b30ebfe7f7e49b94509e19718eb85de055c126233fae7c3c45471e0872fb2038fb27ee31058db00cef50c50da2601cadeda354b18

x*y mod p = 0x869ad6700a26f16d80f0f8e3ecdddc3982012a85839e8e46041deba1cf22f61292f47a20538eb089180196b2ebf4e0cc1da106c7c5b3dc7264c5ef26f48dbfacd3cc854cc64a345d08359f2ff539449d1d221930626e8df9969275b961a22b32ebab4cebf3dbf963bb25ef663258cf00d35d076aad9adabae4d0573f4c37e058


I'm trying to do something like this.
I done this in Java with class BigInteger, but cannot do it in JavaCard.
I know how to do mod but cannot multiple 2big numbers

Re: Multiply big numbers

Posted: Wed Apr 18, 2018 10:14 pm
by roundtable
You should know something important about javacard:
1. javacard is the mini subset of standard Java, so most of features, packages and classes are not supported in javacard. For example , class BigInterger, it is only available on standard Java (JDK) , if you use it on javacad, you can't build it successfully.

2. javacard is run on limited resource (RAM,eeprom) smartcad IC, if you implement your big number calculation using normal operation just lIke on x86,where algorithm in elementary school is used. the performance is terrible and complicated.

Instead, some javacard vendor has done it, providing special their private javacrd API to accelerate those big integer arithmetic via smartcard IC coprocessor, based on that, you can implement your own customized algorithm, such as gen prime, inverse ....

I

Re: Multiply big numbers

Posted: Thu Apr 19, 2018 8:36 am
by djordjem
roundtable wrote:You should know something important about javacard:
1. javacard is the mini subset of standard Java, so most of features, packages and classes are not supported in javacard. For example , class BigInterger, it is only available on standard Java (JDK) , if you use it on javacad, you can't build it successfully.

2. javacard is run on limited resource (RAM,eeprom) smartcad IC, if you implement your big number calculation using normal operation just lIke on x86,where algorithm in elementary school is used. the performance is terrible and complicated.

Instead, some javacard vendor has done it, providing special their private javacrd API to accelerate those big integer arithmetic via smartcard IC coprocessor, based on that, you can implement your own customized algorithm, such as gen prime, inverse ....

I


I know all of this thing. I try to implement PACE protocol in passport applet and one of things is to multiply two big numbers. I am working with RSA keys and i must calculate (G^s * h) mod P where G = 128 byte array, s = 16 byte array and h = 128 byte array, p = 128 byte array. Formula that i use for multiply is:
a = (G^s mod P)
b = h mod P
((a + b)^2 mod P - a^2 mod P - b^2 mod P) / 2 = a * b
For square and mod i am using doFinal method from RSA Chipper.

Re: Multiply big numbers

Posted: Thu Apr 19, 2018 9:38 pm
by wumindejia
Sorry ! The jmrtd call a class of java.math.BigInteger.