Java Card中受限制的椭圆曲线

时间:2015-07-02 12:52:29

标签: java cryptography javacard elliptic-curve

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

首先,我在256位椭圆曲线(NIST one)上实现了它并且运行良好。

现在我想在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);

函数setFieldFP使用原因代码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);

工作正常......

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

那又怎样?我的卡只支持特定字段的椭圆曲线吗?有人遇到过这种问题吗?

1 个答案:

答案 0 :(得分:4)

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

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