Take the 2-minute tour ×
Cryptography Stack Exchange is a question and answer site for software developers, mathematicians and others interested in cryptography. It's 100% free, no registration required.

I am writing a small module which will verify signed message. Message will be signed using private key in Smart Card.

The inputs are:

  1. Signed Message
  2. Original Message
  3. Public Key / Certificate with public key.

Signed Message and public key both will be in Hexadecimal form. I can retrieve the Public key from certificate.

I tried to verify using both Microsoft Crypto API and Openssl but each time it is failing to verify signature.

I guess the format of the data what I am getting is the main problem. Please go through the DATA and help me to learn how can I verify it.

You can find the data at http://pastebin.com/ExYDcJ2t

PUBLIC KEY->
30 82 01 0a 02 82 01 01 00 b8 f8 dc 2c a5 03 84 ba 72 c6 0e 03 89 51 6f 39 a8 41 e3 49 b3 f7 14 31 d3 43 b7 fc 1f 61 c2 43 b0 77 9e 19 af f4 8b 02 99 72 c1 17 21 1d 23 da ab 53 54 74 33 e4 ab 9d 82 d2 68 33 9a b5 9c 99 cb f0 12 e0 f8 44 4f e8 91 3f 60 ed ca fa 3b 40 bd 64 50 92 d3 c2 c1 48 ad 24 3e ca 64 2c 50 a9 01 b5 9f f4 a4 46 e5 84 e9 a4 87 41 86 a1 7a 7f fc a6 f0 e0 b1 de f0 c1 f2 5d c8 84 16 15 4d e4 df 43 43 3a cd ad ec eb af 1b 9c a7 5c 40 dc ae 1f 71 6e a4 c6 0f dd 3e 3c c8 0d 25 4c 61 74 df aa ed b5 d5 b9 06 6a 8e b0 b7 c0 e6 c9 bf db b1 07 2e a2 76 aa e7 28 1c 8d 32 4e b3 58 1d 34 89 96 ed 3e da 29 e0 1e c9 c2 2e 18 19 a6 ba 91 32 b7 85 97 87 92 16 c5 01 b4 4f 57 5c 56 1b f5 f4 6a 29 6b 2e 51 8b f5 4c 6f b8 fd cb 09 d9 fd 66 09 04 49 b6 ba 7e d0 af 70 3a 51 41 5a a5 04 bf 02 03 01 00 01

DATA-> Hello

SIGNED DATA->
14 5F 65 CE 7C 2D 8A 0A FA B0 FB 86 CE 28 90 84 37 2D 04 63 B2 35 FA 40 4A B6 35 C8 90 AF 55 7F B1 CA FE FD 5B F9 1B 7C DB 74 63 BF 16 5B B3 6D E8 2D B6 D7 2E 90 AF 0A 5E CF 78 73 E3 37 02 C2 97 0E F9 B3 40 4C 67 CD E4 7C D9 4B D3 C9 86 51 8E 1E 84 81 B4 30 AC 68 96 59 CB 63 E5 C8 28 48 C7 1D E8 E9 FC E8 C9 BE 36 33 0A F0 A9 35 C4 D4 BF 60 66 21 5C 41 8F 48 91 D4 BB AF 75 75 7A B3 2A 8A 28 B8 30 D1 B4 6B 69 23 82 2D 28 77 30 05 D5 C9 AB 41 17 C1 68 6D D9 80 0F F2 C1 FC 32 6E 22 61 27 97 9C DD C3 50 33 AA DB F4 BA 98 29 FA 4F E2 B4 BC C5 9E 90 34 F3 BC 3D 78 01 47 AF 96 20 06 6F F9 41 30 D7 35 52 D3 DE 85 E3 FE 0B B7 15 4D 1A 73 B8 36 F4 A1 59 A2 7E 05 50 8B 52 AC B4 EF 2D D9 29 9D D9 BB C8 DF F3 67 C5 D1 D9 C0 0C 65 68 A8 12 9B 24 92 4E EB 98 D8 B0 D9 2E 6A

SIGNED DATA IN STRING FORMAT->
._eÎ|-Š.ú°û†Î(„7-.c²5ú@J¶5ȯU±Êþý[ù.|Ûtc¿.[³mè-¶×.¯.^Ïxsã7.—.ù³@LgÍä|ÙKÓɆQŽ.„´0¬h– YËcåÈ(HÇ.èéüèɾ63.ð©5ÄÔ¿`f!\AH‘Ô»¯uuz³*Š(¸0Ñ´ki#‚-(w0.ÕÉ«A.ÁhmÙ€.òÁü2n"a'—œÝÃP3ªÛôº˜)úOâ ´¼Åž4ó¼=x.G¯– .oùA0×5RÓÞ…ãþ.·.M.s¸6ô¡Y¢~.P‹R¬´ï-Ù)Ù»ÈßógÅÑÙÀ.eh¨.›$’Në˜Ø°Ù.j

and the code can be found at http://pastebin.com/CdPkW8wp

#include "opensslhashsign.h"

#ifndef MAX_LEN
#define MAX_LEN 256
#endif

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Entry point for the program
//
/* DATA WHICH IS SIGNED IS -> Hello
   Have pem file converted using openssl from certificate
   Pls Check http://pastebin.com/ExYDcJ2t for data.
*/
int main()
{
    //
    // Local variables definition
    //
    const char      cert_filestr[]      = "./TestCert.pem";
    EVP_PKEY*       evpkey              = NULL;
    BIO*            certbio             = NULL;
    BIO*            outbio              = NULL;
    X509*           cert                = NULL;
    EVP_MD_CTX*     ctx                 = NULL;
    unsigned char   sigBuf[MAX_LEN]     = {0};
    char            buf[]               = "Hello";
    int             bufSize             = sizeof (buf);
    int             ret                 = 1;
    int             bytesRet            = 0;

    //
    // Filling sigBuf with zeros
    //
    memset(sigBuf, 0, MAX_LEN);

    //
    // Function logic begins here
    //
    for (;;)
    {
        //
        //  These function calls initialize openssl for correct work
        //
        OpenSSL_add_all_algorithms();
        ERR_load_BIO_strings();
        ERR_load_crypto_strings();

        //
        //  Create the Input/Output BIO's
        //
        certbio = BIO_new(BIO_s_file());
        outbio  = BIO_new(BIO_s_file());
        outbio  = BIO_new_fp(stdout, BIO_NOCLOSE);

        //
        // Loading the certificate from file (PEM)
        //
        ret = BIO_read_filename(certbio, cert_filestr);
        cert = PEM_read_bio_X509(certbio, NULL, 0, NULL);
        if (NULL == cert)
        {
            BIO_printf(outbio, "Error loading cert into memory\n");
            break;
        } // if
        printf("Certificate loaded from the .pem file\n\n");

        //
        // Extract the certificate's public key data
        //
        evpkey = X509_get_pubkey(cert);
        if (NULL == evpkey)
        {
            BIO_printf(outbio, "Error getting public key from certificate\n");
            break;
        } // if
        printf("Public key extracted from the certificate\n\n");

        //
        // Allocating memory for EVP_MD_CTX Context object
        //
        ctx = (EVP_MD_CTX *) malloc(sizeof (EVP_MD_CTX));
        if (NULL == ctx)
        {
            printf("Memory is not allocated for EVP_MD_CTX object\n");
            break;
        } // if
        printf("Memory allocated for EVP_MD_CTX object\n\n");

        //
        // Initializing EVP_MD_CTX Context object
        EVP_MD_CTX_init(ctx);

        FILE *fp = fopen("./hellosigneddata.txt", "r");
        bytesRet = fread(sigBuf, 1, MAX_LEN, fp);
        if (MAX_LEN != bytesRet)
        {
            printf("Signed buffer has no proper data\n");
            break;
        } // if
        printf("Data in the signed buffer is : %s\n\n", sigBuf);

        //
        // Calling EVP_VerifyInit_ex() function to initialize context
        // for verification
        //
        EVP_VerifyInit_ex(ctx, EVP_sha256(), NULL);

        //
        // After initializing the context, the signed data to be verified
        // is fed into context. This is done with EVP_VerifyUpdate() function
        //
        EVP_VerifyUpdate(ctx, buf, bufSize);

        //
        // Performing the actual verification of the signature using
        // EVP_VerifyFinal() function
        //
        ret = EVP_VerifyFinal(ctx, sigBuf, MAX_LEN, evpkey);
        if (0 == ret)
        {
            printf("Signature doesn't match\n\n");
            break;
        } // if
        printf("Signature verified successfully\n\n");

        //
        // Final break statement
        //
        break;
    } // for

    getchar();

    return 0;
} // main()
share|improve this question

closed as off topic by Hendrik Brummermann Jun 20 '13 at 15:40

Questions on Cryptography Stack Exchange are expected to relate to cryptography within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
Welcome to Cryptography. Please note that your question is off topic according to the on-topic list. You can ask such questions on StackOverflow but please show the code in the question. –  Uwe Plonus Jun 20 '13 at 10:12
    
Thanks, and links i have pasted in the question itself. –  user2035532 Jun 20 '13 at 10:21

1 Answer 1

up vote 2 down vote accepted

You should have specification of the format of the data you process. Else, you have to resort to trial and guessing.

Which is easy here. In your test data, the public key $(n,e)$ is

b8f8dc2ca50384ba72c60e0389516f39a841e349b3f71431d343b7fc1f61c243b077
9e19aff48b029972c117211d23daab53547433e4ab9d82d268339ab59c99cbf012e0
f8444fe8913f60edcafa3b40bd645092d3c2c148ad243eca642c50a901b59ff4a446
e584e9a4874186a17a7ffca6f0e0b1def0c1f25dc88416154de4df43433acdadeceb
af1b9ca75c40dcae1f716ea4c60fdd3e3cc80d254c6174dfaaedb5d5b9066a8eb0b7
c0e6c9bfdbb1072ea276aae7281c8d324eb3581d348996ed3eda29e01ec9c22e1819
a6ba9132b78597879216c501b44f575c561bf5f46a296b2e518bf54c6fb8fdcb09d9
fd66090449b6ba7ed0af703a51415aa504bf

and

010001

The signature $s$ is

145F65CE7C2D8A0AFAB0FB86CE289084372D0463B235FA404AB635C890AF557FB1CA
FEFD5BF91B7CDB7463BF165BB36DE82DB6D72E90AF0A5ECF7873E33702C2970EF9B3
404C67CDE47CD94BD3C986518E1E8481B430AC689659CB63E5C82848C71DE8E9FCE8
C9BE36330AF0A935C4D4BF6066215C418F4891D4BBAF75757AB32A8A28B830D1B46B
6923822D28773005D5C9AB4117C1686DD9800FF2C1FC326E226127979CDDC35033AA
DBF4BA9829FA4FE2B4BCC59E9034F3BC3D780147AF9620066FF94130D73552D3DE85
E3FE0BB7154D1A73B836F4A159A27E05508B52ACB4EF2DD9299DD9BBC8DFF367C5D1
D9C00C6568A8129B24924EEB98D8B0D92E6A

Computing $s^e\bmod n$ yields

0001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF003021300906052B0E03021A05000414F7FF
9E8B7BB2E09B70935A5D785E0CC5D9D0ABF0

So with overwhelming certainty, this signature (mislabeled as SIGNED DATA-> in the pastebin) is formatted as per RSAES-PKCS1-V1_5 in PKCS#1, with 160-bit SHA-1 hash. See how $s^e\bmod n$ precisely is as defined by EMSA-PKCS1-V1_5, including the 0001FF..FF00 filler, the 3021300906052B0E03021A05000414 indicating SHA-1, and the final 20 bytes which are the SHA-1 of whatever data this signature is about the string Hello encoded in ASCII. The verification procedure for this signature is given in the aforementioned PKCS#1 reference. Sorry, I won't help with OpenSSL.

share|improve this answer
    
Thanks for Help,but the certificate i have says Signature Algo. is SHA256RSA and Hash algo. is SHA256. –  user2035532 Jun 20 '13 at 12:55
    
Also as u have experience in smart card system.DATA i am getting is from smart card application.Private Key is somewhere in smart card.and Off-Card application is signing DATA with PIN. –  user2035532 Jun 20 '13 at 12:59
    
@user2035532: The signature that you posted is internally labeled as using SHA-1 (that's the meaning of 3021300906052B0E03021A05000414; further the following seems like a 160-bit hash). No SHA-256 in sight. BTW this signature is mislabeled as SIGNED DATA->, but really is the signature of some signed data; I have no idea what this signed data is, nor what this signature attests, nor what the certificate you are talking about is, much less what it is meaning. –  fgrieu Jun 20 '13 at 14:29
    
@user2035532: Stating that data is from a Smart Card is as non-specific as stating it is from a portable computer. What kind of Smart Card? According to what specification? –  fgrieu Jun 20 '13 at 15:42
1  
Thank You very Much.Its SHA-1 only.and good thing its working with same code just replacing SHA256 with SHA-1.Finally Signature is verified.Thank You Again. :) –  user2035532 Jun 21 '13 at 13:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.