Asif Sayyad
Asif Sayyad

Reputation: 23

Error while call to function EVP_DigestSignFinal

I'm trying to implement ECDSA with custome engine and I'm facing error at this line EVP_DigestSignFinal(mdctx, sig, slen)).Can anyone please guide me.

Engine part -->

EVP_PKEY_meth_set_sign(dasync_ec, dasync_ec_signinit,
           dasync_ec_sign);

static int dasync_ec_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, const unsigned char *tbs, size_t tbslen)
{
    static int (*psign)(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, const unsigned char *tbs, size_t tbslen);
    if (psign == NULL)
        EVP_PKEY_meth_get_sign(dasync_ec_orig, NULL, &psign);    
    return psign(ctx,sig,siglen,tbs,tbslen);
}

Tester side -->

 EVP_MD_CTX *mdctx=NULL;
 EVP_PKEY *pkey =  EVP_PKEY_new(); 
 EC_KEY *eckey=NULL;
 eckey=EC_KEY_new();
 size_t *slen;
 EVP_PKEY_assign_EC_KEY(pkey,eckey);

if(!(mdctx = EVP_MD_CTX_create())) 
   goto err;
if(1 != EVP_DigestSignInit(mdctx, NULL, EVP_sha256(), my_engine, pkey)) 
   goto err; 
if(1 != EVP_DigestSignUpdate(mdctx, msg, strlen(msg))) 
   goto err;
if(1 != EVP_DigestSignFinal(mdctx, NULL, slen)) 
   goto err;
if(!(*sig = OPENSSL_malloc(sizeof(unsigned char) * (*slen)))) 
   goto err;
if(1 != EVP_DigestSignFinal(mdctx, sig, slen)) 
 goto err;

Upvotes: 0

Views: 632

Answers (1)

user2728975
user2728975

Reputation: 1

What i see is that from test program you would need to replace your custom sign method via EVP Ctx calls.

static void set_custom_pkey_method_ec()
{
    /* Define a new EVP PKEY METHODS openssl style */
    EVP_PKEY_METHOD *orig_meth, *new_meth;
        
    /* Get openssl default EVP PKEY Method */
    orig_meth = (EVP_PKEY_METHOD *)EVP_PKEY_meth_find(EVP_PKEY_EC);
    EVP_PKEY_meth_get_sign(orig_meth, &orig_pkey_ec_sign_init, &orig_pkey_ec_sign);

    /* create a EVP PKEY method  and replace default method with our method */
    new_meth = EVP_PKEY_meth_new(EVP_PKEY_EC, EVP_PKEY_FLAG_AUTOARGLEN);
    EVP_PKEY_meth_copy(new_meth, orig_meth);
    EVP_PKEY_meth_set_sign(new_meth, orig_pkey_ec_sign_init, custom_pkey_ec_sign);

    /* Add our custom signing method */
    EVP_PKEY_meth_add0(new_meth);
    return;
}

Your Test program should be modified as below

EVP_MD_CTX *mdctx=NULL;
EVP_PKEY *pkey =  EVP_PKEY_new(); 
EC_KEY *eckey=NULL;
eckey=EC_KEY_new();
size_t *slen;
EVP_PKEY_assign_EC_KEY(pkey,eckey);

**set_custom_pkey_method_ec();**

EVP_Digist_Init();

Upvotes: 0

Related Questions