RE: CryptVerifySignature failing with bad_signature error



bearshare,
The problem turned out to be with the way the SHA1 hash was being generated
for my file. Once that was fixed, everything worked fine.
Thanks
Bose.

"bearshare" wrote:

Bose, I have same issue as you, have you find the problem?
Thanks

"Bose" wrote:

Hi All,

I am trying to build a smart card authentication app. I create a context to
the smart card CSP and then obtain the AT_KEYEXCHANGE from the smart card
container. After this i am creating a hash using CryptCreateHash and the
hCryptProvHandle to the container on the smart card. When i retreive the
hash value, it is correct. (I have verified using openssl and hashcalc tool).
I then try to create a signature of the hash using CryptSignHash. This
results in a signed value.
I then try to do a cryptverifysignature but it fails with the error bad
signature.
I have verified that the hash data I am providing to CryptVerifySignature is
correct and so is the signature buffer. Could the public key be creating
problem here ?

I am following the general procedure shown in the sample code:
http://msdn2.microsoft.com/En-US/library/aa382371.aspx

Here's a snip of the code:
<snip>
// Get the user exchange key ------
if(!CryptGetUserKey(hCryptProv,
AT_KEYEXCHANGE,
&hUsrXchgKey))
{
lReturn = GetLastError();
printf("Failed CryptgetProvParam ! \n");
}
// get the pubkeyblob

if(!CryptExportKey(
hUsrXchgKey,
NULL,
PUBLICKEYBLOB,
0,
NULL,
&dwBlobLen))
{
lReturn = GetLastError();
return -1;
}

if(!(pbKeyBlob = (BYTE*)malloc(dwBlobLen)))
{
lReturn = GetLastError();
return -1;
}

if(!CryptExportKey(
hUsrXchgKey,
NULL,
PUBLICKEYBLOB,
0,
pbKeyBlob,
&dwBlobLen))
{
lReturn = GetLastError();
return -1;
}
</snip>
.
.
<snip>
// Use CryptSignHash to sign the hash with private key of key exchange pair
// Determine the size of the signature and allocate memory and sign the hash
object

if(!CryptSignHash(
hHash,
AT_KEYEXCHANGE,
NULL,
0,
NULL,
&dwSigLen))
{
lReturn = GetLastError();
printf("Failed CryptSignHash ! \n");
} //

if(!(pbSignature = (BYTE *)malloc(dwSigLen)))
{
printf("Failed to allocalte memory\n");
}//

memset(pbSignature,0,dwSigLen);

if(!CryptSignHash(
hHash,
AT_KEYEXCHANGE,
NULL,
0,
pbSignature,
&dwSigLen))
{
lReturn = GetLastError();
printf("Failed CryptSignHash ! \n");
} //

// Destroy hash object
if(hHash)
CryptDestroyHash(hHash);

// Verify signature
hHash_val=NULL;
// get public key of encrypter
if(!CryptImportKey(
hCryptProv,
pbKeyBlob,
dwBlobLen,
0,
0,
&hPubKey))
{
lReturn = GetLastError();
return -1;
}

// Create a new hash object.
if(!CryptCreateHash(
hCryptProv,
CALG_SHA1,
0,
0,
&hHash_val))
{
lReturn = GetLastError();
return -1;
}

// Compute the cryptographic hash of the buffer.
if(!CryptHashData(
hHash,
data_buffer,
data_len,
0))
{
lReturn = GetLastError();
return -1;
}

// Validate the digital signature -- THIS FAILS WITH BAD SIGNATURE
if(!CryptVerifySignature(
hHash,
pbSignature,
dwSigLen,
hPubKey,
NULL,
0))
{
lReturn = GetLastError();
return -1;
}


// Destroy hash object
if(hHash)
CryptDestroyHash(hHash);
</snip>







.



Relevant Pages

  • RE: CryptVerifySignature failing with bad_signature error
    ... I am trying to build a smart card authentication app. ... After this i am creating a hash using CryptCreateHash and the ... I then try to create a signature of the hash using CryptSignHash. ... lReturn = GetLastError; ...
    (microsoft.public.platformsdk.security)
  • CryptVerifySignature failing with bad_signature error
    ... I am trying to build a smart card authentication app. ... After this i am creating a hash using CryptCreateHash and the ... I then try to create a signature of the hash using CryptSignHash. ... lReturn = GetLastError; ...
    (microsoft.public.platformsdk.security)
  • Re: rsa implementation question
    ... > There is a notion of blocks in many public-key ciphers, ... It's not about decrypting to sign, encrypting to ... as it would mean that you'd have to find hash collisions. ... I generate a signature for a string "some string" with SHA. ...
    (comp.lang.python)
  • how to verify signature with DSACryptoServiceProvider
    ... computer) and DSACryptoServideProvider for signature of the hash (my ... signed hash to the end of the encrypted file. ... int securedSaltLength = bReader.ReadInt32; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Sign with RsaCryptoService Provider Verify with win32 Crypto A
    ... Normally the signature should contain ... hash), so I think the .Net version of the code always put the hash id into ... What flags are you using in CryptSignHash and CryptVerifySignature ... I specify CRYPT_NOHASHOID in CryptSignHash and CryptVerifySignature ...
    (microsoft.public.platformsdk.security)