Browse Source

[trac781] add sha-1 tests

Jelte Jansen 14 years ago
parent
commit
97feb3a88b
2 changed files with 71 additions and 5 deletions
  1. 2 2
      src/lib/crypto/crypto.cc
  2. 69 3
      src/lib/crypto/tests/crypto_unittests.cc

+ 2 - 2
src/lib/crypto/crypto.cc

@@ -133,9 +133,9 @@ void doHMAC(const OutputBuffer& data, TSIGKey key, isc::dns::OutputBuffer& resul
     std::cout << "KEY (" << (int)key.getSecretLength() << "): ";
     const uint8_t *k = static_cast<const uint8_t*>(key.getSecret());
     for(size_t s = 0; s < key.getSecretLength(); ++s) {
-		std::cout << s << ": ";
+		//std::cout << s << ": ";
         std::cout << hex << setfill('0') << setw(2) << nouppercase << (unsigned int)k[s] << " ";
-		std::cout << std::endl;
+		//std::cout << std::endl;
     }
     std::cout << std::endl;
     std::cout << "HASH: ";

+ 69 - 3
src/lib/crypto/tests/crypto_unittests.cc

@@ -28,8 +28,8 @@ namespace {
 		ASSERT_EQ(buf.getLength(), len);
 		const uint8_t* buf_d = static_cast<const uint8_t*>(buf.getData());
 		for (size_t i = 0; i < len; ++i) {
-			std::cout << "[XX] I: " << i << ", buf: " << buf_d[i] << ", dat: " << data[i] << std::endl;
-			EXPECT_EQ(buf_d[i], data[i]);
+			//std::cout << "[XX] I: " << i << ", buf: " << buf_d[i] << ", dat: " << data[i] << std::endl;
+			ASSERT_EQ(buf_d[i], data[i]);
 		}
 	}
 	
@@ -47,7 +47,7 @@ namespace {
 }
 
 // Test values taken from RFC 2202
-TEST(CryptoTest, HMAC_SIGN) {
+TEST(CryptoTest, HMAC_MD5_RFC2202_SIGN) {
 	// 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b = CwsLCwsLCwsLCwsLCwsLCw==
     uint8_t hmac_expected[] = { 0x92, 0x94, 0x72, 0x7a, 0x36, 0x38,
                                 0xbb, 0x1c, 0x13, 0xf4, 0x8e, 0xf8,
@@ -107,6 +107,72 @@ TEST(CryptoTest, HMAC_SIGN) {
 
 }
 
+TEST(CryptoTest, HMAC_SHA1_RFC2202_SIGN) {
+    uint8_t hmac_expected[] = { 0xb6, 0x17, 0x31, 0x86, 0x55, 0x05,
+                                0x72, 0x64, 0xe2, 0x8b, 0xc0, 0xb6,
+                                0xfb, 0x37, 0x8c, 0x8e, 0xf1, 0x46,
+                                0xbe, 0x00 };
+	doHMACTest("Hi There",
+	           "test.example:CwsLCwsLCwsLCwsLCwsLCwsLCws=:hmac-sha1",
+	           hmac_expected, 20);
+
+	uint8_t hmac_expected2[] = { 0xef, 0xfc, 0xdf, 0x6a, 0xe5, 0xeb,
+	                             0x2f, 0xa2, 0xd2, 0x74, 0x16, 0xd5,
+	                             0xf1, 0x84, 0xdf, 0x9c, 0x25, 0x9a,
+	                             0x7c, 0x79 };
+	doHMACTest("what do ya want for nothing?",
+	           "test.example:SmVmZQ==:hmac-sha1",
+	           hmac_expected2, 20);
+
+	std::string data3;
+	for (int i = 0; i < 50; ++i) {
+		data3.push_back(0xdd);
+	}
+	uint8_t hmac_expected3[] = { 0x12, 0x5d, 0x73, 0x42, 0xb9, 0xac,
+	                             0x11, 0xcd, 0x91, 0xa3, 0x9a, 0xf4,
+	                             0x8a, 0xa1, 0x7b, 0x4f, 0x63, 0xf1,
+	                             0x75, 0xd3 };
+	doHMACTest(data3,
+	           "test.example:qqqqqqqqqqqqqqqqqqqqqqqqqqo=:hmac-sha1",
+	           hmac_expected3, 20);
+
+	std::string data4;
+	for (int i = 0; i < 50; ++i) {
+		data4.push_back(0xcd);
+	}
+	uint8_t hmac_expected4[] = { 0x4c, 0x90, 0x07, 0xf4, 0x02, 0x62,
+	                             0x50, 0xc6, 0xbc, 0x84, 0x14, 0xf9,
+	                             0xbf, 0x50, 0xc8, 0x6c, 0x2d, 0x72,
+	                             0x35, 0xda };
+	doHMACTest(data4,
+	           "test.example:AQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGQ==:hmac-sha1",
+	           hmac_expected4, 20);
+
+	uint8_t hmac_expected5[] = { 0x4c, 0x1a, 0x03, 0x42, 0x4b, 0x55,
+	                             0xe0, 0x7f, 0xe7, 0xf2, 0x7b, 0xe1,
+	                             0xd5, 0x8b, 0xb9, 0x32, 0x4a, 0x9a,
+	                             0x5a, 0x04 };
+	doHMACTest("Test With Truncation",
+	           "test.example:DAwMDAwMDAwMDAwMDAwMDAwMDAw=:hmac-sha1",
+	           hmac_expected5, 20);
+	           
+	uint8_t hmac_expected6[] = { 0xaa, 0x4a, 0xe5, 0xe1, 0x52, 0x72,
+	                             0xd0, 0x0e, 0x95, 0x70, 0x56, 0x37,
+	                             0xce, 0x8a, 0x3b, 0x55, 0xed, 0x40,
+	                             0x21, 0x12 };
+	doHMACTest("Test Using Larger Than Block-Size Key - Hash Key First",
+	           "test.example:qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqo=:hmac-sha1",
+	           hmac_expected6, 20);
+
+	uint8_t hmac_expected7[] = { 0xe8, 0xe9, 0x9d, 0x0f, 0x45, 0x23,
+	                             0x7d, 0x78, 0x6d, 0x6b, 0xba, 0xa7,
+	                             0x96, 0x5c, 0x78, 0x08, 0xbb, 0xff,
+	                             0x1a, 0x91 };
+	doHMACTest("Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data",
+	           "test.example:qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqo=:hmac-sha1",
+	           hmac_expected7, 20);
+}
+
 TEST(CryptoText, TSIGKeyFromString) {
 	TSIGKey k1 = TSIGKeyFromString("test.example:MSG6Ng==:hmac-md5.sig-alg.reg.int");
 	TSIGKey k2 = TSIGKeyFromString("test.example.:MSG6Ng==:hmac-md5.sig-alg.reg.int.");