簡體   English   中英

當我只有原始簽名(R,S)和原始公鑰點(Qx,Qy)時,我如何 ECDSA 驗證 Java 中的數據塊

[英]How do I ECDSA verify a block of data in Java when I only have the raw signature (R, S) and raw public key point (Qx, Qy)

我有一個字節數組消息以及簽名的四個 32 字節原始組件: QxQyRS 如何將這些格式化/編碼為Signature::verify function 期望的ECPublicKey和簽名byte[] 簽名是使用 SHA-256 ECDSA 創建的。

該解決方案需要結合其他兩個答案(並在 Java 中實現):

創建公鑰涉及生成一個虛擬密鑰對並使用其ECParameterSpec ,然后替換您的原始公鑰點 [Qx, Qy]。 還必須從原始字節創建BigInteger 這可以在下面的createPublicKey function 中看到。

簽名(由 R 和 S 組成)必須以DER 格式編碼 - 我找不到現有的 Java 實用程序createDERSigniture手動完成,如下所示。

/**
 * Check to see if `message` matches signature [R, S] and public key point [qx, qy]
 *
 * @param message device ID
 * @param r       first part of the ECDSA signature
 * @param s       second part of the ECDSA signature
 * @param qx      x part of the public key point
 * @param qy      y part of the public key point
 * @return true iff the signature signed the message
 * @note ECDSA SHA-256 is used - r, s, qx and qy must be 32 bytes long
 */
private static boolean ecdsaVerify(@NotNull final byte[] message, @NotNull final byte[] r, @NotNull final byte[] s,
                                   @NotNull final byte[] qx, @NotNull final byte[] qy)
{
   try
   {
      // convert from raw bytes to something `Signature` can understand
      ECPublicKey publicKey = createPublicKey(qx, qy);
      byte[] derSignature = createDERSigniture(r, s);

      // do the actual verification
      Signature sig = Signature.getInstance("SHA256withECDSA");
      sig.initVerify(publicKey);
      sig.update(message);
      return sig.verify(derSignature);
   }
   catch (NoSuchAlgorithmException | InvalidKeySpecException | InvalidKeyException | SignatureException | InvalidAlgorithmParameterException e)
   {
      return false;
   }
}

/**
 * Format the raw elliptic curve point [qx, qy] with the NIST P-256
 *
 * @param qx the x coordinate - should be 32 bytes
 * @param qy the y coordinate - should be 32 bytes
 * @return the public key from the raw coordinates
 * @see https://stackoverflow.com/a/22652372/1229250
 */
private static ECPublicKey createPublicKey(byte[] qx, byte[] qy)
        throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeySpecException
{
   // generate bogus keypair so we can get its spec
   KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC");
   kpg.initialize(new ECGenParameterSpec("secp256r1"));// NIST P-256
   ECPublicKey apub = (ECPublicKey)kpg.generateKeyPair().getPublic();
   ECParameterSpec aspec = apub.getParams();

   ECPoint point = new ECPoint(new BigInteger(1, qx), new BigInteger(1, qy));
   ECPublicKeySpec pks = new ECPublicKeySpec(point, aspec);
   return (ECPublicKey)KeyFactory.getInstance("EC").generatePublic(pks);
}

/**
 * Encode a raw signature in the DER format
 *
 * @param r first part of the raw signature - should be 32 bytes
 * @param s second part of the raw signature - should be 32 bytes
 * @return a DER formatted signature
 * @see https://crypto.stackexchange.com/a/57734/89173
 */
private static byte[] createDERSigniture(byte[] r, byte[] s)
{
   // build backwards
   byte[] der = {};
   der = prependPoint(der, s);
   der = prependPoint(der, r);
   return prependHeader(der);
}

/**
 * Take in a raw coordinate value, `p` and then wrap and prepend it to `derSig`
 *
 * Wrapping includes adding the header by (0x02), the length as well as a leading zero if needed.
 *
 * @param derSig the end of the DER formatted signature, so far (may be empty)
 * @param p      a part of the coordinate to prepend
 * @return the signature so far with an addition component
 */
private static byte[] prependPoint(byte[] derSig, byte[] p)
{
   // append a zero byte if the leading *bit* is one (so as a whole, it is a positive number)
   final boolean prependZero = (p[0] & 0x80) == 0x80;
   final int pointLength = p.length + (prependZero ? 1 : 0);
   final int prependSize = 2 + pointLength;
   final int totalNewSize = prependSize + derSig.length;

   byte[] result = new byte[totalNewSize];
   result[0] = 2;
   result[1] = (byte) pointLength;
   if (prependZero)
   {
      result[2] = 0;
   }
   System.arraycopy(p, 0, result, prependZero ? 3 : 2, p.length);
   System.arraycopy(derSig, 0, result, prependSize, derSig.length);

   return result;
}

/**
 * Add the DER header - the 0x30 magic number and the length of the point
 *
 * @param derSig the DER signature so far - must have the two points
 * @return the signature with the proper header
 */
private static byte[] prependHeader(byte[] derSig)
{
   byte[] result = new byte[derSig.length + 2];
   result[0] = 0x30;
   result[1] = (byte) derSig.length;
   System.arraycopy(derSig, 0, result, 2, derSig.length);
   return result;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM