PageRenderTime 203ms CodeModel.GetById 100ms app.highlight 9ms RepoModel.GetById 91ms app.codeStats 0ms

/mordor/tests/hmac.cpp

http://github.com/mozy/mordor
C++ | 107 lines | 87 code | 18 blank | 2 comment | 0 complexity | a720b622d7504e810d6ddcf6a5839f89 MD5 | raw file
  1#include "mordor/pch.h"
  2
  3#include "mordor/string.h"
  4#include "mordor/test/test.h"
  5
  6using namespace Mordor;
  7using namespace Mordor::Test;
  8
  9MORDOR_UNITTEST(HMAC, HMAC_MD5_1)
 10{
 11    std::string key;
 12    key.append(16, 0x0b);
 13    std::string data("Hi There");
 14    MORDOR_TEST_ASSERT(hmacMd5(data, key) ==
 15        "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc\x9d");
 16}
 17
 18MORDOR_UNITTEST(HMAC, HMAC_MD5_2)
 19{
 20    std::string key("Jefe");
 21    std::string data("what do ya want for nothing?");
 22    MORDOR_TEST_ASSERT(hmacMd5(data, key) ==
 23        "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7\x38");
 24}
 25
 26MORDOR_UNITTEST(HMAC, HMAC_MD5_3)
 27{
 28    std::string key;
 29    key.append(16, (uint8_t)0xaa);
 30    std::string data;
 31    data.append(50, (uint8_t)0xdd);
 32    MORDOR_TEST_ASSERT(hmacMd5(data, key) ==
 33        "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3\xf6");
 34}
 35
 36// test cases from http://tools.ietf.org/html/rfc4231
 37MORDOR_UNITTEST(HMAC, HMAC_SHA256_RFC4231_1)
 38{
 39    std::string hmac = hmacSha256("Hi There", std::string(20, '\x0b'));
 40
 41    MORDOR_TEST_ASSERT_EQUAL(
 42         hexstringFromData(hmac.c_str(), SHA256_DIGEST_LENGTH),
 43         "b0344c61d8db38535ca8afceaf0bf12b"
 44         "881dc200c9833da726e9376c2e32cff7");
 45}
 46
 47MORDOR_UNITTEST(HMAC, HMAC_SHA256_RFC4231_2)
 48{
 49    std::string hmac = hmacSha256("what do ya want for nothing?", "Jefe");
 50
 51    MORDOR_TEST_ASSERT_EQUAL(
 52        hexstringFromData(hmac.c_str(), SHA256_DIGEST_LENGTH),
 53        "5bdcc146bf60754e6a042426089575c7"
 54        "5a003f089d2739839dec58b964ec3843");
 55}
 56
 57MORDOR_UNITTEST(HMAC, HMAC_SHA256_RFC4231_3)
 58{
 59    std::string hmac = hmacSha256(std::string(50, '\xdd'),
 60                                  std::string(20, '\xaa'));
 61
 62    MORDOR_TEST_ASSERT_EQUAL(
 63        hexstringFromData(hmac.c_str(), SHA256_DIGEST_LENGTH),
 64        "773ea91e36800e46854db8ebd09181a7"
 65        "2959098b3ef8c122d9635514ced565fe");
 66}
 67
 68MORDOR_UNITTEST(HMAC, HMAC_SHA256_RFC4231_4)
 69{
 70    std::string hmac = hmacSha256(
 71        std::string(50, '\xcd'),
 72        "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
 73        "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19");
 74
 75    MORDOR_TEST_ASSERT_EQUAL(
 76        hexstringFromData(hmac.c_str(), SHA256_DIGEST_LENGTH),
 77        "82558a389a443c0ea4cc819899f2083a"
 78        "85f0faa3e578f8077a2e3ff46729665b");
 79}
 80
 81// RFC 4231 Test Case 5 deals with output truncation, omitted
 82
 83MORDOR_UNITTEST(HMAC, HMAC_SHA256_RFC4231_6)
 84{
 85    std::string hmac = hmacSha256(
 86        "Test Using Larger Than Block-Size Key - Hash Key First",
 87        std::string(131, '\xaa'));
 88
 89    MORDOR_TEST_ASSERT_EQUAL(
 90        hexstringFromData(hmac.c_str(), SHA256_DIGEST_LENGTH),
 91        "60e431591ee0b67f0d8a26aacbf5b77f"
 92        "8e0bc6213728c5140546040f0ee37f54");
 93}
 94
 95MORDOR_UNITTEST(HMAC, HMAC_SHA256_RFC4231_7)
 96{
 97    std::string hmac = hmacSha256(
 98        "This is a test using a larger than block-size key and a "
 99        "larger than block-size data. The key needs to be hashed "
100        "before being used by the HMAC algorithm.",
101        std::string(131, '\xaa'));
102
103    MORDOR_TEST_ASSERT_EQUAL(
104        hexstringFromData(hmac.c_str(), SHA256_DIGEST_LENGTH),
105        "9b09ffa71b942fcb27635fbcd5b0e944"
106        "bfdc63644f0713938a7f51535c3a35e2");
107}