0

I want to create method for checking message sign on C#. I get PublicKey Data, Message, Sign. And I have C++ example which work.

C++ Code:

#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;
}

In C# I try:

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");
}

But I dont know how I should use pubkey. How can I add openkey to ECDsa.

And I use ECDsa.Create because I want to have cross platform code.

I tried CngKey.Import(key, CngKeyBlobFormat.EccPublicBlob), but this used for ECDsaCng which used only windows.

I can use ECDsa.Create(ECParameters) but Q parameter contains X and Y field, I don't know where I should get them.

I tried

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

but I got error

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

I try

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))

But I get Data is bad

Any ideas? Maybe should I use something else?

dmitriy
  • 256
  • 2
  • 17

1 Answers1

0

I reviewed this link

And tried to use BouncyCastle. It's work

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);
}

Upd. For the first option, it was necessary to add 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);
}
dmitriy
  • 256
  • 2
  • 17