📄 hmactest.c
字号:
/*____________________________________________________________________________
HMACtest.c
Copyright (C) 2003, 2004 PGP Corporation
All rights reserved.
HMACtest.c - This file contains functions which are used to verify
the HMAC function It runs a known answer test using vectors from
the ANSI X9.71 document.
$Id: HMACtest.c 48493 2006-10-12 21:19:56Z vinnie $
____________________________________________________________________________*/
#include <stdio.h>
#include <string.h>
#include "pgpErrors.h"
#include "pgpUtilities.h"
#include "pgpHash.h"
#include "pgpHMAC.h"
#include "optest.h"
/*
Run a specific HMAC test and compare against known answer.
*/
static PGPError
TestHMACkat(
PGPContextRef context,
PGPHashAlgorithm algor,
PGPByte * key,
PGPSize keyLen,
char * data,
PGPSize dataLen,
PGPByte * expected )
{
PGPHMACContextRef hmacRef = kInvalidPGPHMACContextRef;
PGPHashContextRef hashRef = kInvalidPGPHashContextRef;
PGPError err = kPGPError_NoErr;
PGPSize hmacSize;
PGPByte hmacBuf [64];
if(gVerbose_flag)
OPTESTPrintF("\t%10s %s\n", hash_algor_table(algor), data );
/* get hash size which is same as hmac length */
err = PGPNewHashContext(context, algor, &hashRef); CKERR;
err = PGPGetHashSize(hashRef, &hmacSize);CKERR;
if(hmacSize > sizeof(hmacBuf)) RETERR(kPGPError_BufferTooSmall);
/* create and setup the HMAC context */
err = PGPNewHMACContext(context, algor, key, keyLen, &hmacRef);CKERR;
/* calculate the HMAC.. */
err = PGPContinueHMAC( hmacRef, data, dataLen);CKERR;
/* output the HMAC */
err = PGPFinalizeHMAC(hmacRef, hmacBuf);CKERR;
/* check against know answer */
err = pgpMemoryEqual(hmacBuf, expected, hmacSize)
? kPGPError_NoErr : kPGPError_SelfTestFailed;
CKERR;
/* no reason to do this other than testing */
PGPResetHMAC(hmacRef);
done:
if( PGPHashContextRefIsValid( hashRef ) )
(void) PGPFreeHashContext( hashRef );
if( PGPHMACContextRefIsValid( hmacRef ) )
(void) PGPFreeHMACContext( hmacRef );
return err;
}
/*
FIPS140 state Run HMAC known answer self test
*/
PGPError TestHMAC(PGPContextRef context)
{
PGPError err = kPGPError_NoErr;
int i;
/*
Test vectors for HMAC known answer test
NOTE: These examples were taken from FIPS-198 doc
*/
struct {
char *msg;
int keysize;
PGPByte *key;
PGPByte sha1[20];
PGPByte sha256[32];
PGPByte sha384[48];
PGPByte sha512[64];
} test_vectors[] =
{
{ "Sample #1",
64,
(PGPByte *)
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f"
"\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f"
"\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f",
{ 0x4f, 0x4c, 0xa3, 0xd5, 0xd6, 0x8b, 0xa7, 0xcc, 0x0a, 0x12,
0x08, 0xc9, 0xc6, 0x1e, 0x9c, 0x5d, 0xa0, 0x40, 0x3c, 0x0a },
{ 0x35, 0x19, 0xf0, 0xcd, 0xdf, 0xa0, 0x90, 0xf8,
0xac, 0xe8, 0x19, 0xd9, 0xae, 0x85, 0x01, 0x57,
0x8c, 0x46, 0x92, 0x05, 0x02, 0xc6, 0x2b, 0xaa,
0x47, 0xbf, 0xe6, 0x01, 0x48, 0x64, 0xa9, 0x3a },
{ 0x1d, 0x43, 0x71, 0xaa, 0xf0, 0x55, 0x84, 0x3e,
0xf0, 0xb9, 0x50, 0xf5, 0x4b, 0xd4, 0xb8, 0x90,
0xd3, 0x5f, 0x5c, 0xd1, 0x18, 0x31, 0x04, 0x35,
0x0d, 0xe1, 0xcb, 0xe3, 0x1c, 0xfd, 0xd6, 0xb9,
0x64, 0x32, 0xa2, 0x0d, 0x73, 0x35, 0x6e, 0xb7,
0x2d, 0xe6, 0xc4, 0x65, 0xc9, 0xa1, 0x50, 0x23 },
{ 0xe7, 0xa9, 0xe4, 0x21, 0x95, 0xcf, 0x36, 0xe6,
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xbd, 0x04, 0x13, 0x16, 0xdb, 0x0f, 0xac,
0x44, 0xd5, 0xa4, 0xd5, 0xd0, 0x89, 0x2b, 0xd0,
0x4e, 0x86, 0x64, 0x12, 0xc0, 0x90, 0x77, 0x68,
0xf1, 0x87, 0xb7, 0x7c, 0x4f, 0xae, 0x2c, 0x2f,
0x21, 0xa5, 0xb5, 0x65, 0x9a, 0x4f, 0x4b, 0xa7,
0x47, 0x02, 0xa3, 0xde, 0x9b, 0x51, 0xf1, 0x45,
0xbd, 0x4f, 0x25, 0x27, 0x42, 0x98, 0x99, 0x05 }
},
{ "Sample #3",
100,
(PGPByte *)
"\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f"
"\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f"
"\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f"
"\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f"
"\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f"
"\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf"
"\xb0\xb1\xb2\xb3",
{ 0xbc, 0xf4, 0x1e, 0xab, 0x8b, 0xb2, 0xd8, 0x02, 0xf3, 0xd0,
0x5c, 0xaf, 0x7c, 0xb0, 0x92, 0xec, 0xf8, 0xd1, 0xa3, 0xaa },
{ 0x2d, 0x7d, 0x0d, 0x7f, 0x3e, 0x52, 0xff, 0xe8,
0x9d, 0x65, 0xc9, 0x78, 0xf3, 0x9d, 0x55, 0x5b,
0xb4, 0x8b, 0x0b, 0xa4, 0x8d, 0x5b, 0x6e, 0xb4,
0x04, 0x65, 0x4a, 0xd1, 0xaf, 0xdb, 0x4c, 0xa3 },
{ 0x4f, 0x00, 0xad, 0x30, 0x48, 0x4c, 0xe7, 0xd2,
0x08, 0x9e, 0xe7, 0xf9, 0x44, 0x6c, 0x9e, 0x07,
0xeb, 0xa2, 0xc8, 0x18, 0x7c, 0x3c, 0x1d, 0x59,
0x44, 0x2a, 0xa2, 0xc6, 0x15, 0x0e, 0xfe, 0x74,
0xab, 0x67, 0xdc, 0x6d, 0x41, 0xe6, 0x15, 0x1a,
0x1b, 0x8d, 0x2d, 0x42, 0x3e, 0xbd, 0x04, 0x78 },
{ 0x5b, 0x28, 0x53, 0x80, 0xd8, 0xf5, 0xb6, 0xee,
0xa7, 0x32, 0x85, 0xae, 0x63, 0x0b, 0xfa, 0x51,
0x0d, 0x40, 0xa1, 0xb4, 0x7e, 0x8f, 0xf7, 0x58,
0xe9, 0x0c, 0x56, 0x16, 0xf2, 0x00, 0x99, 0x51,
0x18, 0xdf, 0x92, 0xae, 0x6a, 0xfd, 0xd2, 0xf3,
0x5b, 0x32, 0xe6, 0x77, 0x8f, 0xb6, 0x1b, 0x3e,
0xc9, 0x95, 0xee, 0x52, 0x3d, 0x56, 0xa4, 0x14,
0xb4, 0x7c, 0x9e, 0x6c, 0x52, 0x61, 0x3a, 0x82 }
},
};
/* create PGP context */
err = PGPNewContext( kPGPsdkAPIVersion, &context );
if( IsPGPError( err ) ) goto done;
/* run test array */
for (i = 0; i < 3; i++)
{
err = TestHMACkat(context,
kPGPHashAlgorithm_SHA,
test_vectors[i].key,
test_vectors[i].keysize,
test_vectors[i].msg,
strlen( (char *) test_vectors[i].msg),
test_vectors[i].sha1 ); CKERR;
err = TestHMACkat(context,
kPGPHashAlgorithm_SHA256,
test_vectors[i].key,
test_vectors[i].keysize,
test_vectors[i].msg,
strlen( (char *) test_vectors[i].msg),
test_vectors[i].sha256 ); CKERR;
err = TestHMACkat(context,
kPGPHashAlgorithm_SHA384,
test_vectors[i].key,
test_vectors[i].keysize,
test_vectors[i].msg,
strlen( (char *) test_vectors[i].msg),
test_vectors[i].sha384 ); CKERR;
err = TestHMACkat(context,
kPGPHashAlgorithm_SHA512,
test_vectors[i].key,
test_vectors[i].keysize,
test_vectors[i].msg,
strlen( (char *) test_vectors[i].msg),
test_vectors[i].sha512 ); CKERR;
}
done:
return( err );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -