Java Card中受限制的椭圆曲线

Rao*_*722 5 java cryptography elliptic-curve javacard

我正在尝试在Java Card中的椭圆曲线上实现加密算法.

首先,我在256位椭圆曲线(NIST一曲线)上实现了它并且效果很好.

现在我想在512位曲线上测试它(而不是像NIST那样测试521).我的卡支持这个大小,我找到了这个大小的椭圆曲线数据库(加密定义很好).但我遇到了一个奇怪的问题......

当我尝试初始化我的密钥时:

ECPublicKey pubKey = (ECPublicKey) KeyBuilder.buildKey(
            KeyBuilder.TYPE_EC_FP_PUBLIC, (short) 0x0200, false);

pubKey.setFieldFP(new byte[] { (byte) 0x25, (byte) 0x37,
            (byte) 0xD2, (byte) 0x9C, (byte) 0x8B, (byte) 0xFE,
            (byte) 0x7D, (byte) 0x9F, (byte) 0x48, (byte) 0x98,
            (byte) 0xF7, (byte) 0x60, (byte) 0xF8, (byte) 0x7D,
            (byte) 0xBF, (byte) 0x63, (byte) 0x90, (byte) 0x6E,
            (byte) 0x28, (byte) 0x99, (byte) 0x0A, (byte) 0x27,
            (byte) 0x0C, (byte) 0xA6, (byte) 0x15, (byte) 0xD9,
            (byte) 0x1D, (byte) 0xC4, (byte) 0x89, (byte) 0xA8,
            (byte) 0xD0, (byte) 0xA1, (byte) 0xA0, (byte) 0xE7,
            (byte) 0x52, (byte) 0x43, (byte) 0xB0, (byte) 0x39,
            (byte) 0x01, (byte) 0x6A, (byte) 0x61, (byte) 0x43,
            (byte) 0x5C, (byte) 0xA5, (byte) 0x91, (byte) 0xE9,
            (byte) 0x4B, (byte) 0x1A, (byte) 0xF7, (byte) 0x60,
            (byte) 0xC9, (byte) 0xAE, (byte) 0xE2, (byte) 0xCE,
            (byte) 0xE0, (byte) 0x15, (byte) 0x53, (byte) 0x51,
            (byte) 0x1C, (byte) 0x93, (byte) 0x0E, (byte) 0xF3,
            (byte) 0xBA, (byte) 0x0B }, (short) 0x0000, (short) 0x0040);
Run Code Online (Sandbox Code Playgroud)

该函数使用原因代码setFieldFP引发a CryptoException,ILLEGAL_VALUE这意味着密钥长度不匹配......但它确实(0x0200是以位为单位的曲线大小,0X0040是以字节为单位的素数长度)!

我说这很奇怪,因为如果我尝试使用以下值:

ECPublicKey pubKey = (ECPublicKey) KeyBuilder.buildKey(
            KeyBuilder.TYPE_EC_FP_PUBLIC, (short) 0x0200, false);

pubKey.setFieldFP(new byte[] { (byte) 0xFF, (byte) 0xFF,
            (byte) 0xFF, (byte) 0xFF, (byte) 0x00, (byte) 0x00,
            (byte) 0x00, (byte) 0x01, (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) 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) 0x00, (byte) 0x00,
            (byte) 0x00, (byte) 0x01, (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) 0xFF, (byte) 0xFF,
            (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
            (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
            (byte) 0xFF, (byte) 0xFF}, (short) 0x0000, (short) 0x0040);
Run Code Online (Sandbox Code Playgroud)

它工作正常......

所以我必须得出结论,CryptoException提出的并不是真正涉及参数的大小,因为在这两种情况下,大小是相同的......

所以呢?我的卡只支持特定字段的椭圆曲线吗?有人遇到过这种问题吗?

Maa*_*wes 5

你的素数不够大。对于 512 位的 F(p) 曲线,您应该使用 512 位素数。(byte) 0x25但是,您的第一个字节以十六进制数字开头2。这意味着第一个字节首先以 2 个二进制数字开头,设置为0,这意味着您已经定义了 512 - 2 = 510 位素数。

请仅使用定义明确的曲线,例如 NIST P521 曲线或 BrainpoolP512r1 曲线。

  • 好吧,不,生成 512 位质数的难度是生成 1024 位密钥对的一半。不难,但需要时间。然而,不应在受控环境之外尝试生成域参数(包括素数),当然也不应在智能卡上尝试。例如,素数代似乎被限制为`[1.. 2^512)`而不是`[2^511 ... 2^512)`。 (5认同)
  • 嗯,是的,除非该卡也支持 510 位曲线。但我想知道素数是如何生成的(我检查过它*是*一个素数)。 (4认同)