Example #1
0
static int calc_and_compare_hash_512(const uint8_t *msg, size_t msg_len, const uint8_t *expected)
{
    static unsigned char hash[SHA3_512_DIGEST_LENGTH];

    sha3_512(hash, msg, msg_len);

    return (memcmp(expected, hash, sizeof(hash)) == 0);
}
Example #2
0
static void
sha3_512_t (ut_test_t *const t)
{
  const char *const msg = "abc";
  const char *const expected_msg_digest_as_string =
    "b751850b1a57168a" "5693cd924b6b096e" "08f621827444f70d" "884f5d0240d2712e"
    "10e116e9192af3c9" "1a7ec57647e39340" "57340b4cf408d5a5" "6592f8274eec53f0";

  char msg_digest[sha3_512_digest_lenght];
  char msg_digest_as_string[sha3_512_digest_lenght * 2 + 1]; // Two hex digit per byte plus string termination.
  size_t msg_len = strlen(msg);

  sha3_512(msg_digest, msg, msg_len);

  sha3_msg_digest_to_string(msg_digest_as_string, msg_digest, sha3_512_digest_lenght);

  ut_assert(t, strcmp(expected_msg_digest_as_string, msg_digest_as_string) == 0);
}
Example #3
0
int sha3_test(const unsigned char *input, unsigned long input_bits, int print) {
unsigned char *hash;
	hash = sha3_224(input, input_bits);
	if (hash) {
		if (print) {
			sha3_print_hash(hash, 224UL, "sha3_224(input, %lu) =", input_bits);
		}
		free(hash);
	}
	else {
		return EXIT_FAILURE;
	}
	hash = sha3_256(input, input_bits);
	if (hash) {
		if (print) {
			sha3_print_hash(hash, 256UL, "sha3_256(input, %lu) =", input_bits);
		}
		free(hash);
	}
	else {
		return EXIT_FAILURE;
	}
	hash = sha3_384(input, input_bits);
	if (hash) {
		if (print) {
			sha3_print_hash(hash, 384UL, "sha3_384(input, %lu) =", input_bits);
		}
		free(hash);
	}
	else {
		return EXIT_FAILURE;
	}
	hash = sha3_512(input, input_bits);
	if (hash) {
		if (print) {
			sha3_print_hash(hash, 512UL, "sha3_512(input, %lu) =", input_bits);
		}
		free(hash);
	}
	else {
		return EXIT_FAILURE;
	}
	return EXIT_SUCCESS;
}
Example #4
0
// here we go !
int main(int argc, char** argv)
{
  int errors = 0;

  // http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA_All.pdf
  // or more compact overview: http://www.di-mgt.com.au/sha_testvectors.html
  std::string empty;
  std::string abc = "abc";
  std::string abc448bits = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  std::string abc896bits = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  std::string million(1000000, 'a');

  std::cout << "test SHA1 ...\n";
  errors += check<SHA1>(empty,      "da39a3ee5e6b4b0d3255bfef95601890afd80709");
  errors += check<SHA1>(abc,        "a9993e364706816aba3e25717850c26c9cd0d89d");
  errors += check<SHA1>(abc448bits, "84983e441c3bd26ebaae4aa1f95129e5e54670f1");
  errors += check<SHA1>(abc896bits, "a49b2446a02c645bf419f995b67091253a04a259");
  errors += check<SHA1>(million,    "34aa973cd4c4daa4f61eeb2bdbad27316534016f");

  std::cout << "test SHA2/256 ...\n";
  errors += check<SHA256>(empty,      "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
  errors += check<SHA256>(abc,        "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
  errors += check<SHA256>(abc448bits, "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1");
  errors += check<SHA256>(abc896bits, "cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1");
  errors += check<SHA256>(million,    "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0");

  std::cout << "test SHA3/256 ...\n";
  errors += check<SHA3>(empty,      "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a");
  errors += check<SHA3>(abc,        "3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532");
  errors += check<SHA3>(abc448bits, "41c0dba2a9d6240849100376a8235e2c82e1b9998a999e21db32dd97496d3376");
  errors += check<SHA3>(abc896bits, "916f6061fe879741ca6469b43971dfdb28b1a32dc36cb3254e812be27aad1d18");
  errors += check<SHA3>(million,    "5c8875ae474a3634ba4fd55ec85bffd661f32aca75c6d699d0cdcb6c115891c1");

  // next test cases produced an error until February 2015, reported by Gary Singer
  // note: automatic test case 71 failed, too, same bug
  std::cout << "test SHA3/512 ...\n";
  SHA3 sha3_512(SHA3::Bits512);
  std::vector<unsigned char> sha3bug = hex2bin("13bd2811f6ed2b6f04ff3895aceed7bef8dcd45eb121791bc194a0f806206bffc3b9281c2b308b1a729ce008119dd3066e9378acdcc50a98a82e20738800b6cddbe5fe9694ad6d");
  if (sha3_512(sha3bug.data(), sha3bug.size())
      != "def4ab6cda8839729a03e000846604b17f03c5d5d7ec23c483670a13e11573c1e9347a63ec69a5abb21305f9382ecdaaabc6850f92840e86f88f4dabfcd93cc0")
    std::cerr << "SHA3 bug present" << std::endl;

  // check all automatically generated testsets
  std::cout << "generic testsets (CRC32,MD5,SHA1,SHA256,SHA3) ..." << std::endl;
  for (size_t i = 0; i < NumTests; i++)
  {
    errors += check<CRC32 >(hex2bin(testset[i].input), testset[i].crc32b);
    errors += check< MD5  >(hex2bin(testset[i].input), testset[i].md5);
    errors += check< SHA1 >(hex2bin(testset[i].input), testset[i].sha1);
    errors += check<SHA256>(hex2bin(testset[i].input), testset[i].sha256);
    errors += check< SHA3 >(hex2bin(testset[i].input), testset[i].sha3_256);
  }

  // HMAC MD5 and SHA1 test vectors from RFC2202 http://www.ietf.org/rfc/rfc2202.txt
  std::cout << "test HMAC(MD5) ...\n";
  errors += checkHmac< MD5  >(std::string("Hi There"),
                              hex2bin("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"),
                              "9294727a3638bb1c13f48ef8158bfc9d");
  errors += checkHmac< SHA1 >(std::string("Hi There"),
                              hex2bin("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"),
                              "b617318655057264e28bc0b6fb378c8ef146be00");
  errors += checkHmac< MD5  >(std::string("what do ya want for nothing?"),
                              std::string("Jefe"),
                              "750c783e6ab0b503eaa86e310a5db738");
  errors += checkHmac< SHA1 >(std::string("what do ya want for nothing?"),
                              std::string("Jefe"),
                              "effcdf6ae5eb2fa2d27416d5f184df9c259a7c79");
  errors += checkHmac< MD5  >(hex2bin("dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"),
                              hex2bin("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
                              "56be34521d144c88dbb8c733f0e8b3f6");
  errors += checkHmac< SHA1 >(hex2bin("dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"),
                              hex2bin("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
                              "125d7342b9ac11cd91a39af48aa17b4f63f175d3");
  errors += checkHmac< MD5  >(hex2bin("cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"),
                              hex2bin("0102030405060708090a0b0c0d0e0f10111213141516171819"),
                              "697eaf0aca3a3aea3a75164746ffaa79");
  errors += checkHmac< SHA1 >(hex2bin("cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"),
                              hex2bin("0102030405060708090a0b0c0d0e0f10111213141516171819"),
                              "4c9007f4026250c6bc8414f9bf50c86c2d7235da");
  // test case 5 skipped
  errors += checkHmac< MD5  >(std::string("Test Using Larger Than Block-Size Key - Hash Key First"),
                              hex2bin("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
                              "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd");
  errors += checkHmac< SHA1 >(std::string("Test Using Larger Than Block-Size Key - Hash Key First"),
                              hex2bin("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
                              "aa4ae5e15272d00e95705637ce8a3b55ed402112");
  errors += checkHmac< MD5  >(std::string("Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"),
                              hex2bin("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
                              "6f630fad67cda0ee1fb1f562db3aa53e");
  errors += checkHmac< SHA1 >(std::string("Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"),
                              hex2bin("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
                              "e8e99d0f45237d786d6bbaa7965c7808bbff1a91");

  // HMAC SHA256 test vectors from RFC4231 http://www.ietf.org/rfc/rfc4231.txt
  std::cout << "test HMAC(SHA256) ...\n";
  errors += checkHmac<SHA256>(std::string("Hi There"),
                              hex2bin("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"),
                              "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7");
  errors += checkHmac<SHA256>(std::string("what do ya want for nothing?"),
                              std::string("Jefe"),
                              "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843");
  errors += checkHmac<SHA256>(hex2bin("dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"),
                              hex2bin("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
                              "773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe");
  errors += checkHmac<SHA256>(hex2bin("cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"),
                              hex2bin("0102030405060708090a0b0c0d0e0f10111213141516171819"),
                              "82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b");
  // test case 5 skipped
  errors += checkHmac<SHA256>(std::string("Test Using Larger Than Block-Size Key - Hash Key First"),
                              hex2bin("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
                              "60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54");
  errors += checkHmac<SHA256>(std::string("This is a test using a larger than block-size key and a larger than block-size data. The key needs to be hashed before being used by the HMAC algorithm."),
                              hex2bin("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
                              "9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f51535c3a35e2");

  // summary
  if (errors == 0)
    std::cout << "all tests ok" << std::endl;
  else
    std::cerr << errors << " tests failed" << std::endl;

  return errors;
}