PDA

View Full Version : Crypto++ How do I use a message authentication code ?



Thành Viên Mới
15th July 2010, 11:35
i want use VMAC in crypto++ lib on QT,i was tried study but i dont find example of it

can you help me
can i use Funcition DefaultDecryptorWithMAC ?

FileSource f(in, true, new DefaultDecryptorWithMAC(passPhrase, new FileSink(out)));

Ginsengelf
15th July 2010, 16:58
Hi, you can use any C++ library together with Qt (which is just a library itself) so you don't need special examples, any examples you find for crypto++ will be fine.
Crypto++ (http://www.cryptopp.com/) should provide something.

Ginsengelf

Thành Viên Mới
24th July 2010, 12:25
can you help me for VMAC use Crypto++ ?

squidge
24th July 2010, 17:00
This isn't a Qt question. You need to post to the crypto++ mailing list if you don't understand how to use the library.

Thành Viên Mới
26th July 2010, 04:39
void VmacFile(const char *hexKey, const char *hexIV, const char *infile, const char *outfile)
{
SecByteBlock keyBlock = HexDecodeString(hexKey);
SecByteBlock iv = HexDecodeString(hexIV);
VMAC<AES,128 > vmacEncryptFile;
vmacEncryptFile.SetKeyWithIV(keyBlock, keyBlock.size(), iv);
CryptoPP::VMAC<CryptoPP::AES, 64> hasher;
//vmacEncryptFile.Resynchronize(pbIV2);
FileSource(infile, true, new IteratedHashBase(vmacEncryptFile, new FileSink(outfile)));
}




#include <vector>
#include "vmac.h"
using namespace std;
void TestVMac( int _num, vector<uint64> *_res )
{
const char key[16] = "somedummykey345";
const byte pattern[100] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
if ( sizeof(pattern) < _num )
_num = sizeof(pattern);
const uint64 nonce[2] = { 0x10, 0x0 };
CryptoPP::VMAC<CryptoPP::AES, 64> hasher;
hasher.SetKey( (byte*)key, 16, CryptoPP::MakeParameters
( CryptoPP::Name::IV(), (const byte*)&nonce, false )
( CryptoPP::Name::KeySize(), 128 ) );
_res->clear();
for ( size_t i = 1, e = _num; i != e; ++ i )
{
uint64 cv;
hasher.Resynchronize( (const byte*)nonce );
hasher.CalculateDigest( (byte*)&cv, pattern, i );
_res->push_back( cv );
}
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
return a.exec();
}

Thành Viên Mới
5th August 2010, 11:22
#include <QtCore/QCoreApplication>
#include "vmac.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>

unsigned prime(void)
{
volatile uint64_t i;
volatile uint64_t j=1;
unsigned cnt=0;
volatile clock_t ticks = clock();
do
{
for (i = 0; i < 500000; i++)
{
uint64_t x = get64PE(&j);
j = x * x + (uint64_t)ticks;
}
cnt++;
} while (clock() - ticks < (CLOCKS_PER_SEC / 2));
return cnt; /* cnt is millions of iterations per second */
}

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
ALIGN(16) vmac_ctx_t ctx, ctx_aio, ctx_inc1, ctx_inc2;
uint64_t res, tagl;
void *p;
unsigned char *pbData;
ALIGN(4) unsigned char pbKey[] = "abcdefghijklmnop";
ALIGN(4) unsigned char nonce[] = "\0\0\0\0\0\0\0\0bcdefghi";
unsigned int uiVectorLength[] = {0,3,48,300,3000000};
#if (VMAC_TAG_LEN == 64)
ALIGN(4) char *should_be[] = {"2576BE1C56D8B81B","2D376CF5B1813CE5",
"E8421F61D573D298","4492DF6C5CAC1BBE",
"09BA597DD7601113"};
#else
ALIGN(4) char *should_be[] =
{ "472766C70F74ED23481D6D7DE4E80DAC",
"4EE815A06A1D71EDD36FC75D51188A42",
"09F2C80C8E1007A0C12FAE19FE4504AE",
"66438817154850C61D8A412164803BCB",
"2B6B02288FFC461B75485DE893C629DC"};
#endif
unsigned speed_lengths[] = {16, 32, 64, 128, 256, 512, 1024, 2048, 4096};
unsigned i, j, *speed_iters;
clock_t ticks;
double cpb;
const unsigned int buf_len = 3 * (1 << 20);
j = prime();
i = sizeof(speed_lengths)/sizeof(speed_lengths[0]);
speed_iters = (unsigned *)malloc(i*sizeof(speed_iters[0]));
speed_iters[i-1] = j * (1 << 12);
while (--i)
{
speed_iters[i-1] = (unsigned)(1.3 * speed_iters[i]);
}
/* Initialize context and message buffer, all 16-byte aligned */
p = malloc(buf_len + 32);
pbData = (unsigned char *)(((size_t)p + 16) & ~((size_t)15));
memset(pbData, 0, buf_len + 16);
vmac_set_key(pbKey, &ctx);
/* Test incremental and all-in-one interfaces for correctness */
vmac_set_key(pbKey, &ctx_aio);
vmac_set_key(pbKey, &ctx_inc1);
vmac_set_key(pbKey, &ctx_inc2);
/* Generate vectors */
for (i = 0; i < sizeof(uiVectorLength)/sizeof(unsigned int); i++)
{
for (j = 0; j < uiVectorLength[i]; j++)
{
pbData[j] = (unsigned char)('a'+j%3);
}
res = vmac(pbData, uiVectorLength[i], nonce, &tagl, &ctx);
#if (VMAC_TAG_LEN == 64)
printf("\'abc\' * %7u: %016llX Should be: %s\n",
uiVectorLength[i]/3,res,should_be[i]);
#else
printf("\'abc\' * %7u: %016llX%016llX\nShould be: %s\n",uiVectorLength[i]/3,res,tagl,should_be[i]);
#endif
}
/* Speed test */
for (i = 0; i < sizeof(speed_lengths)/sizeof(unsigned int); i++)
{
ticks = clock();
for (j = 0; j < speed_iters[i]; j++)
{
#if HASH_ONLY
res = vhash(pbData, speed_lengths[i], &tagl, &ctx);
#else
res = vmac(pbData, speed_lengths[i], nonce, &tagl, &ctx);
nonce[7]++;
#endif
}
ticks = clock() - ticks;
cpb = ((ticks*VMAC_HZ)/((double)CLOCKS_PER_SEC*speed_lengths[i]*speed_iters[i]));
printf("%4u bytes, %2.2f cpb\n", speed_lengths[i], cpb);
}
return a.exec();
}