简体   繁体   English

验证Java Card签名

[英]Verify Java Card signature

I am writing a Java Card 3.0.2 application on a NXP J3D081 card. 我正在NXP J3D081卡上编写Java Card 3.0.2应用程序。 I have it both signing and verifying a signature using ALG_ECDSA_SHA_256. 我有使用ALG_ECDSA_SHA_256签名和验证签名。 The keys have been written to the card by my test app. 我的测试应用程序已将密钥写入卡中。 If I sign 32 bytes of data and pass the signature back to the card the Verify code successfully verifies the signature. 如果我签署了32个字节的数据并将签名传回卡,则验证代码会成功验证签名。 If I sign 32 bytes in Bouncy Castle with the Private key and pass to the Verify on the Card it successfully verifies the signature. 如果我使用私钥在Bouncy Castle中签署32个字节并传递给卡上的验证,则它会成功验证签名。 The bouncy castle Verify Code successfully verifies signatures created from the bouncy castle signing routine. 充气城堡验证码成功验证了从充气城堡签署例程创建的签名。

BUT if I take the returned signature from the Java Card and pass it to the C# bouncy castle code it FAILS to verify the signature. 但是,如果我从Java卡中取回返回的签名并将其传递给C#bouncy城​​堡代码,则FAILS将验证签名。 I have checked all input values and they are correct. 我检查了所有输入值,它们是正确的。 My code is here (note I pass Public keys as 64 bytes and prepend them with 0x04) 我的代码在这里(注意我将公钥作为64字节传递并在其前面加上0x04)

public bool HashAndVerifyDSA(byte[] pb, byte[] inData, byte[] sig)
{
    byte[] pub = new byte[65];
    pub[0] = 0x4;
    Array.Copy(pb, 0, pub, 1, 64);
    ECCurve curve = parameters.Curve;
    ECPoint q = curve.DecodePoint(pub);
    ICipherParameters Public = new ECPublicKeyParameters(algorithm, q, parameters);
    ISigner bSigner = SignerUtilities.GetSigner("SHA-256withECDSA");

    bSigner.Init(false, Public);
    bSigner.BlockUpdate(inData, 0, inData.Length);
    return (bSigner.VerifySignature(sig));
}

I should note that the parameters specify the P-256 curve and are used successfully in the encrypted communication to the card. 我应该注意,这些参数指定了P-256曲线,并成功用于与卡的加密通信。 The Public key is successfully created. 公钥已成功创建。

I seem to have less hair now then I did two days ago. 我现在的头发似乎比两天前少了。 Any pointers would be welcome. 任何指针都会受到欢迎。

Apart from steps you have performed to debug the thing, you can check the following also: - 除了您为调试事物而执行的步骤外,您还可以检查以下内容: -

  1. Verify the signature using some online available tool. 使用一些在线可用工具验证签名。 Do not forget to use same curve parameters and public key generated from javacard. 不要忘记使用从javacard生成的相同曲线参数和公钥。
  2. Verify the same using bouncy castle java library. 使用充气城堡java库验证相同。 I perform the same steps in one of my tools and it was matched successfully. 我在其中一个工具中执行了相同的步骤,并且成功匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM