JavaCard中的BigInteger减法 [英] BigInteger subtraction in JavaCard

查看:170
本文介绍了JavaCard中的BigInteger减法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在非常受限的技术条件下进行概念验证.我的问题是:如何有效地减去Java卡中的大整数(表示为字节数组)?.

I am attempting a proof of concept under very constrained technological conditions. My question is: how to efficiently subtract big integers (represented as byte arrays) in a Java Card?.

现在,细节使任务变得棘手.我可以使用一张智能卡.该模型为 Feitian JavaCOS A22 ,并运行Java Card 2.2.有关完整的详细信息,Java Card支持使用Java API的非常受限制的子集(,即没有 int ,没有 char ,自然也没有 BigInteger ),但是它确实支持一系列加密原语,可以在此

Now, the details are what make the task tricky. I have access to one smart card. The model is Feitian JavaCOS A22 and runs Java Card 2.2. For full detail, Java Card enables the usage of a very restricted subset of the Java API (namely, no int, no char, and naturally, no BigInteger), but it does support a series of cryptographic primitives that can be detailed on this list.

尤其是,我的任务是在卡上实现经典的ElGamal.到目前为止,我发现了两个相关的答复.在第一个一个中,Maarten指出ElGamal不在标准中,并且因此将需要实现该功能.在此 answer 中,thotheolh共享了一个链接基于相同原理的 Java Card 2.2中DiffieHellman的实现:由于本机不支持它,因此它利用了RSA的功能.

In particular, my task is to implement classic ElGamal on card. I found two relevant replies so far. In the first one, Maarten points out that ElGamal is not on the standard, and therefore the functionality would need to be implemented. In this answer, thotheolh shares a link to an implementation of DiffieHellman in Java Card 2.2 based on the same principle: since it is not natively supported, it leverages on the functionality of RSA.

逻辑是无缝的:RSA,ElGamal和DiffieHellman依赖于相同的基本操作$ a ^ b mod c $.根据thotheolh的代码,我设法实现了密钥生成.加密是在卡外进行的,因此与我无关.但是解密需要特定的变体.对于解密$ b = p-1-x $,其中$ p $和$ x $都是BigIntegers.这就是我遇到的问题:如何有效地计算$ p-1-x $?

The logic is seamless: RSA, ElGamal and DiffieHellman rely on the same basic operation $a^b mod c$. Based on thotheolh's code, I have managed to achieve key generation. Encryption occurs out of the card so it is not my concern. But decryption requires a particular variant. For decryption $b=p-1-x$, where both $p$ and $x$ are BigIntegers. This is the point where I get stuck: how to calculate efficiently $p-1-x$?

推荐答案

好吧,事实上,没有像Java Card那样的本机 real BigInteger支持.有 BigNumber ,但是我认为这不符合您的要求.

Well, in fact there is no such thing like native real BigInteger support for JavaCard. There is BigNumber, but I don't think it will fit your requirements.

但是,有一种方法可以进行此限制.

However, there is a way to undertake this limitation.

有一些JavaCard库应该允许您处理任意长的大整数-问题是您的小程序可能会耗尽内存. 库的来源是此处 ,并且此处是预构建的.jar.

There is some JavaCard library that should allow you to deal with arbitrary long big integers - the problem is that your applet could run out of memory. Sources of library are here, and here is the prebuilt .jar.

此方法可能有效,但在真实卡上可能会大大减慢速度.但是,如果仅在PoC中在模拟器中运行这样的代码,则这不是问题.

This approach might work but also likely to be drastically slow on real card. However this isn't an issue, if you run such code in simulator just for PoC.

我不知道您的IDE是什么,但是是如何为IntelliJ添加此库.

I've no idea what is your IDE but this is how you can add this library for IntelliJ.

但是,正如Maarten Bodewes指出的那样,由于任何BigInteger JavaCard库可能效率低下,您最好将焦点放在字节减上.

However, as Maarten Bodewes pointed out, you might be better focus on bytes substraction, just because of probable inefficency of any BigInteger JavaCard library.

希望这会有所帮助.

UPD

BigNumber保证至少为 8个字节,但是就我所试,它允许恰好 8个字节,这对于减小拥有一些安全性强的参数.说,它不包含等于57896044618658058097711785492504343953926634992332820282019728792003956564821041的安全素数p.

BigNumber is guaranteed to be at least 8 bytes, but as far, as I tried it, it allows exactly 8 bytes, which is way to small to hold some security-robust parameters. Say, it cat not contain safe prime p that equals to 57896044618658097711785492504343953926634992332820282019728792003956564821041.

您可以使用方法getMaxBytesSupported()自己尝试,以确保事实成立.

You can try this yourself with method getMaxBytesSupported() just to ensure the fact.

因此,如您所见,BigNumber对于JavaCard来说相对较大,但仍然比大多数加密协议所需的要小.

So, as you can see, BigNumber is relatively big for JavaCard, but still smaller, than most crypto protocols needs.

这篇关于JavaCard中的BigInteger减法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆