如何使用 ECDsa 的公钥

How to use PublicKey for ECDsa

我想在 C# 上创建检查消息签名的方法。我得到公钥数据、消息、签名。我有有效的 C++ 示例。

C++代码:

#include <stdio.h>
#include <string.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/ec.h>
#include <openssl/bio.h>

int curve = NID_X9_62_prime256v1;

void print_err()
{
    printf("%s\n", ERR_error_string(ERR_get_error(), NULL));
}

bool Verify(EVP_PKEY* pubKey, unsigned char* MsgHash, size_t MsgHashLen, char* Msg, size_t MsgLen)
{
    bool result = false;

    EVP_MD_CTX* ctx = EVP_MD_CTX_create();

    if (EVP_DigestVerifyInit(ctx, NULL, EVP_sha256(), NULL, pubKey) <= 0) {
        return false;
    }
    if (EVP_DigestVerifyUpdate(ctx, Msg, MsgLen) <= 0) {
        return false;
    }
    int AuthStatus = EVP_DigestVerifyFinal(ctx, MsgHash, MsgHashLen);

    if (AuthStatus == 1) 
        result = true;
    else if (AuthStatus != 0) 
        printf("verify err %s\n", ERR_error_string(ERR_get_error(), NULL));

    EVP_MD_CTX_free(ctx);

    return result;
}

EVP_PKEY* make_EC_key()
{
    EVP_PKEY* pkey = NULL;
    EC_KEY* ec_key = EC_KEY_new_by_curve_name(curve);
    pkey = EVP_PKEY_new();
    EVP_PKEY_assign_EC_KEY(pkey, ec_key);
    return pkey;
}

int main()
{
    int i;
    char msg[] = "Hello world ever whatever anything else";

    unsigned char pubkey[65] = { 0x04, 0x1f, 0x5c, 0x0e, 0xb0, 0xeb, 0xf1, 0x09, 0x43, 0x1d, 0x9b, 0x24, 0xb4, 0x08, 0xb2, 0xa5, 0x74, 0x1a, 0xee, 0x25, 0x98, 0xad, 0x0e, 0xd9, 0x80, 0xcd, 0xd4, 0x60, 0xd1, 0x0d, 0x43, 0xaf, 0x37, 0xd6, 0x76, 0x83, 0x8f, 0x05, 0xe6, 0x21, 0xed, 0xa6, 0x03, 0x11, 0xa9, 0x25, 0x60, 0xe4, 0x6c, 0xc4, 0x52, 0x85, 0x31, 0x89, 0x42, 0x89, 0xac, 0xae, 0x4a, 0x31, 0x3b, 0x91, 0x24, 0x04, 0x05 };
    unsigned char msgsign[72] = { 0x30, 0x46, 0x02, 0x21, 0x00, 0xfe, 0x25, 0x0f, 0x51, 0xb6, 0x15, 0x73, 0x54, 0x67, 0x68, 0x66, 0xdc, 0xf7, 0x23, 0xba, 0xab, 0x53, 0xcc, 0xd1, 0x77, 0x12, 0x37, 0x82, 0x21, 0xe3, 0xfc, 0x60, 0xe3, 0x18, 0xfa, 0x69, 0x8f, 0x02, 0x21, 0x00, 0x9c, 0xa3, 0x80, 0x35, 0xa2, 0x86, 0x30, 0x70, 0x6a, 0xa4, 0xd0, 0xbd, 0x59, 0x8e, 0xe3, 0x63, 0x24, 0xed, 0x5d, 0xa3, 0x57, 0x9f, 0xe8, 0x56, 0xcd, 0x9f, 0xd1, 0x86, 0x78, 0x07, 0x22, 0x86 };

    EVP_PKEY* clientkey = NULL;    

    OPENSSL_init();
    OpenSSL_add_all_algorithms();


    clientkey = make_EC_key();

    const unsigned char* pubdata = pubkey;//WTF!

    if (clientkey != d2i_PublicKey(EVP_PKEY_EC, &clientkey, &pubdata, sizeof(pubkey))) 
        print_err();
    else 
        printf("clientkey successfully restored from byte sequence\n");


    bool v = Verify(clientkey, msgsign, sizeof(msgsign), msg, strlen(msg));

    printf("%s verified!\n", v ? "Yes," : "Not");

    EVP_PKEY_free(clientkey);

    return 0;
}

在 C# 中我尝试:

var curve = ECCurve.NamedCurves.nistP256;

string msg = "Hello world ever whatever anything else";
var data = Encoding.UTF8.GetBytes(msg);
byte[] pubkey = new byte[] { 0x04, 0x1f, 0x5c, 0x0e, 0xb0, 0xeb, 0xf1, 0x09, 0x43, 0x1d, 0x9b, 0x24, 0xb4, 0x08, 0xb2, 0xa5, 0x74, 0x1a, 0xee, 0x25, 0x98, 0xad, 0x0e, 0xd9, 0x80, 0xcd, 0xd4, 0x60, 0xd1, 0x0d, 0x43, 0xaf, 0x37, 0xd6, 0x76, 0x83, 0x8f, 0x05, 0xe6, 0x21, 0xed, 0xa6, 0x03, 0x11, 0xa9, 0x25, 0x60, 0xe4, 0x6c, 0xc4, 0x52, 0x85, 0x31, 0x89, 0x42, 0x89, 0xac, 0xae, 0x4a, 0x31, 0x3b, 0x91, 0x24, 0x04, 0x05 };
byte[] signature = new byte[] { 0x30, 0x46, 0x02, 0x21, 0x00, 0xfe, 0x25, 0x0f, 0x51, 0xb6, 0x15, 0x73, 0x54, 0x67, 0x68, 0x66, 0xdc, 0xf7, 0x23, 0xba, 0xab, 0x53, 0xcc, 0xd1, 0x77, 0x12, 0x37, 0x82, 0x21, 0xe3, 0xfc, 0x60, 0xe3, 0x18, 0xfa, 0x69, 0x8f, 0x02, 0x21, 0x00, 0x9c, 0xa3, 0x80, 0x35, 0xa2, 0x86, 0x30, 0x70, 0x6a, 0xa4, 0xd0, 0xbd, 0x59, 0x8e, 0xe3, 0x63, 0x24, 0xed, 0x5d, 0xa3, 0x57, 0x9f, 0xe8, 0x56, 0xcd, 0x9f, 0xd1, 0x86, 0x78, 0x07, 0x22, 0x86 };


using (ECDsa ecsdKey = ECDsa.Create(curve))
{    
    if (ecsdKey.VerifyData(data, signature, HashAlgorithmName.SHA256))
        Console.WriteLine("Data is good");
    else
        Console.WriteLine("Data is bad");
}

但是我不知道应该怎么用pubkey。如何将 openkey 添加到 ECDsa。

我使用ECDsa.Create因为我想要跨平台代码。

我尝试了 CngKey.Import(key, CngKeyBlobFormat.EccPublicBlob),但这用于仅使用 windows.

的 ECDsaCng

我可以使用 ECDsa.Create(ECParameters) 但是 Q 参数包含 X 和 Y 字段,我不知道应该从哪里获取它们。

我试过了

ecsdKey.ImportSubjectPublicKeyInfo(source: pubkey, bytesRead: out _);

但是我得到了错误

AsnContentException: The provided data is tagged with 'Universal' class value '4', but it should have been 'Universal' class value '16'.

我试试

byte[] keyX = new byte[pubkey.Length / 2];
byte[] keyY = new byte[keyX.Length];
Buffer.BlockCopy(pubkey, 1, keyX, 0, keyX.Length);
Buffer.BlockCopy(pubkey, 1 + keyX.Length, keyY, 0, keyY.Length);

var parameters = new ECParameters
{
    Curve = curve,
    Q =
    {
        X = keyX,
        Y = keyY
    }
};
using (ECDsa ecsdKey = ECDsa.Create(parameters))

但是我得到了Data is bad

有什么想法吗? 也许我应该使用其他东西?

我评论了这个

并尝试使用 BouncyCastle。工作了

static void Verify(byte[] message, byte[] signature, byte[] pubkey)
{
    byte[] keyX = new byte[pubkey.Length / 2];
    byte[] keyY = new byte[keyX.Length];
    Buffer.BlockCopy(pubkey, 1, keyX, 0, keyX.Length);
    Buffer.BlockCopy(pubkey, 1 + keyX.Length, keyY, 0, keyY.Length);

    BigInteger x = new BigInteger(1, keyX);
    BigInteger y = new BigInteger(1, keyY);

    X9ECParameters ecParams = NistNamedCurves.GetByName("P-256");
    ECDomainParameters domainParameters = new ECDomainParameters(ecParams.Curve, ecParams.G, ecParams.N, ecParams.H, ecParams.GetSeed());
    var G = ecParams.G;
    Org.BouncyCastle.Math.EC.ECCurve curve = ecParams.Curve;
    Org.BouncyCastle.Math.EC.ECPoint q = curve.CreatePoint(x, y);

    ECPublicKeyParameters pubkeyParam = new ECPublicKeyParameters(q, domainParameters);

    var verifier = SignerUtilities.GetSigner("SHA-256withECDSA");
    verifier.Init(false, pubkeyParam);
    verifier.BlockUpdate(message, 0, message.Length);
    bool result = verifier.VerifySignature(signature);

    Console.WriteLine("result: " + result);
}

更新。对于第一个选项,有必要添加 DSASignatureFormat DSASignatureFormat.Rfc3279DerSequence

static void Verify2(byte[] message, byte[] signature, byte[] pubkey)
{
    var Q = new System.Security.Cryptography.ECPoint();
    var param = new ECParameters();

    byte[] keyX = new byte[pubkey.Length / 2];
    byte[] keyY = new byte[keyX.Length];


    Buffer.BlockCopy(pubkey, 1, keyX, 0, keyX.Length);
    Buffer.BlockCopy(pubkey, 1 + keyX.Length, keyY, 0, keyY.Length);

    Q.X = keyX;
    Q.Y = keyY;
    param.Curve = System.Security.Cryptography.ECCurve.CreateFromFriendlyName("ECDSA_P256");
    param.Q = Q;   

    var ecdsa = ECDsa.Create(param);    

    bool result = ecdsa.VerifyData(message, signature, HashAlgorithmName.SHA256, DSASignatureFormat.Rfc3279DerSequence);
    Console.WriteLine("result: " + result);
}