poohgr8
poohgr8

Reputation: 11

RSA_sign and RSA_verify varied behavior

I have a sample signature generator in C which will create a hash message <count:mac-addr> and generate a signature.

When I use char *message = "120:08:00:27:7c:b6:18"; and sign, the signature is verified successfully.

But when I use

char * generate_hash()
{
    xmlDoc         *document;
    xmlNode        *root, *first_child, *node;
    char           *filename;
    char            *ap_count;
    char            *ap_mac_address;
    char            *message;

    filename = "/license.xml";

    document = xmlReadFile(filename, NULL, 0);
    root = xmlDocGetRootElement(document);
    first_child = root->children;
    for (node = first_child; node; node = node->next) {
        if ( strcmp((char*)node->name, "ap_count") == 0 ) {
            ap_count = (char*)xmlNodeGetContent(node);
        }
        if ( strcmp((char*)node->name, "ap_mac_address") == 0 ){
            ap_mac_address = (char*)xmlNodeGetContent(node);
        }
    }
    message = (char *) malloc(strlen(ap_count)+ strlen(ap_mac_address) +1 );
    memset(message,0x0,(1 + strlen(ap_count)+ strlen(ap_mac_address)));
    strcpy(message,ap_count);
    strcat(message,":");
    strcat(message,ap_mac_address);
    printf(" %d \n", (1 + strlen(ap_count)+ strlen(ap_mac_address)));
    return message;
}

--- while verifying,

char* message;
message = generate_hash();

I am using the below function call to generate the signature in both the cases.

if(RSA_sign(NID_sha256, (unsigned char*) message, strlen(message),
signature, &slen, private_key) != 1) {
    ERR_print_errors_fp(stdout);
    return 1;
}

The signature verification fails with this above procedure. Not sure what I am doing wrong here.

Below is the call I am using to verify the same.

verified = RSA_verify(NID_sha256, (unsigned char*) message,
        strlen(message), sign, file_len, public_key);

Upvotes: 1

Views: 4650

Answers (2)

grekhss
grekhss

Reputation: 165

The description of RSA_verify tells following:

RSA_verify() verifies that the signature sigbuf of size siglen matches a given message digest m of size m_len. type denotes the message digest algorithm that was used to generate the signature. rsa is the signer's public key.

So, using RSA_verify with original message is not correct: message digest should be used instead.

Upvotes: 0

jww
jww

Reputation: 102205

verified = RSA_verify(NID_sha256, (unsigned char*) message,
        strlen(message), sign, file_len, public_key);

The signature could have an embedded NULL. Do not treat it like string data, and don't use strlen on it.

You have to manage a pointer and an explicit length.

Upvotes: 2

Related Questions