Mehic.info

Crypto++ VMAC issue

Well, I tried code available on http://www.cryptopp.com/wiki/VMAC in my NS-3 project on the following way:

    inputString = base64_encode (inputString);
 
    byte* Kkey = key->GetKey();
    byte digestBytes[key->GetSize()]; 
    byte digestBytes2[key->GetSize()];

    CryptoPP::VMAC vmac;

    vmac.SetKeyWithIV(Kkey, key->GetSize(), m_iv, CryptoPP::AES::BLOCKSIZE);
    vmac.CalculateDigest(digestBytes, (byte *) inputString.c_str(), inputString.length());
    
    //VMAC Verification
    vmac.SetKeyWithIV(Kkey, key->GetSize(), m_iv, CryptoPP::AES::BLOCKSIZE);
    vmac.CalculateDigest(digestBytes2, (byte *) inputString.c_str(), inputString.length());
 
    std::string outputString = std::string( (char*)digestBytes, sizeof(digestBytes ) );
    std::string outputString2 = std::string( (char*)digestBytes2, sizeof(digestBytes2) );

    outputString = base64_encode (outputString);
    outputString2 = base64_encode (outputString2);

But the output strings were not the same :

qJnazhQlR+ay3XsDKYz+ZOBeiMf9fwAAWBy3vI9/AAA= 
and 
qJnazhQlR+ay3XsDKYz+ZOBeiMf9fwAA4xy3vI9/AAA=

Finally, I made it working using the following code:

byte* Kkey = key->GetKey();
    byte digestBytes[key->GetSize()];  

    CryptoPP::VMAC vmac;

    vmac.SetKeyWithIV(Kkey, key->GetSize(), m_iv, CryptoPP::AES::BLOCKSIZE);
    vmac.CalculateDigest(digestBytes, (byte *) inputString.c_str(), inputString.length());

    std::string outputString;
    CryptoPP::HexEncoder encoder;
    encoder.Attach(new CryptoPP::StringSink(outputString));
    encoder.Put(digestBytes, sizeof(digestBytes));
    encoder.MessageEnd();

return outputString;
    

Which generated the same HMAC value for same inputString. Hope it helps!