| 362 | } |
| 363 | |
| 364 | void TestHMAC() |
| 365 | { |
| 366 | // RFC 2202 / RFC 4231 known-answer tests for HMAC-SHA1 and HMAC-SHA256 |
| 367 | |
| 368 | // HMAC-SHA1, Test Case 1 (RFC 2202): key=0x0b*20, data="Hi There" |
| 369 | { |
| 370 | uint8 key[20]; uint32 cbKey = 20; |
| 371 | for ( int i = 0; i < 20; ++i ) key[i] = 0x0b; |
| 372 | const char *pszData = "Hi There"; |
| 373 | SHADigest_t digest; |
| 374 | CCrypto::GenerateHMAC( (const uint8 *)pszData, (uint32)strlen(pszData), key, cbKey, &digest ); |
| 375 | char hex[sizeof(SHADigest_t)*2+1]; |
| 376 | CCrypto::HexEncode( digest, sizeof(digest), hex, sizeof(hex) ); |
| 377 | for ( int i = 0; hex[i]; ++i ) hex[i] = tolower(hex[i]); |
| 378 | CHECK_EQUAL( 0, strcmp( hex, "b617318655057264e28bc0b6fb378c8ef146be00" ) ); |
| 379 | } |
| 380 | // HMAC-SHA1, Test Case 2 (RFC 2202): key="Jefe", data="what do ya want for nothing?" |
| 381 | { |
| 382 | const char *pszKey = "Jefe"; |
| 383 | const char *pszData = "what do ya want for nothing?"; |
| 384 | SHADigest_t digest; |
| 385 | CCrypto::GenerateHMAC( (const uint8 *)pszData, (uint32)strlen(pszData), (const uint8 *)pszKey, (uint32)strlen(pszKey), &digest ); |
| 386 | char hex[sizeof(SHADigest_t)*2+1]; |
| 387 | CCrypto::HexEncode( digest, sizeof(digest), hex, sizeof(hex) ); |
| 388 | for ( int i = 0; hex[i]; ++i ) hex[i] = tolower(hex[i]); |
| 389 | CHECK_EQUAL( 0, strcmp( hex, "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79" ) ); |
| 390 | } |
| 391 | |
| 392 | // HMAC-SHA256, Test Case 1 (RFC 4231): key=0x0b*20, data="Hi There" |
| 393 | { |
| 394 | uint8 key[20]; uint32 cbKey = 20; |
| 395 | for ( int i = 0; i < 20; ++i ) key[i] = 0x0b; |
| 396 | const char *pszData = "Hi There"; |
| 397 | SHA256Digest_t digest; |
| 398 | CCrypto::GenerateHMAC256( (const uint8 *)pszData, (uint32)strlen(pszData), key, cbKey, &digest ); |
| 399 | char hex[sizeof(SHA256Digest_t)*2+1]; |
| 400 | CCrypto::HexEncode( digest, sizeof(digest), hex, sizeof(hex) ); |
| 401 | for ( int i = 0; hex[i]; ++i ) hex[i] = tolower(hex[i]); |
| 402 | CHECK_EQUAL( 0, strcmp( hex, "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7" ) ); |
| 403 | } |
| 404 | // HMAC-SHA256, Test Case 2 (RFC 4231): key="Jefe", data="what do ya want for nothing?" |
| 405 | { |
| 406 | const char *pszKey = "Jefe"; |
| 407 | const char *pszData = "what do ya want for nothing?"; |
| 408 | SHA256Digest_t digest; |
| 409 | CCrypto::GenerateHMAC256( (const uint8 *)pszData, (uint32)strlen(pszData), (const uint8 *)pszKey, (uint32)strlen(pszKey), &digest ); |
| 410 | char hex[sizeof(SHA256Digest_t)*2+1]; |
| 411 | CCrypto::HexEncode( digest, sizeof(digest), hex, sizeof(hex) ); |
| 412 | for ( int i = 0; hex[i]; ++i ) hex[i] = tolower(hex[i]); |
| 413 | CHECK_EQUAL( 0, strcmp( hex, "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843" ) ); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | void TestEllipticCrypto() |
| 418 | { |