crypto_unittests.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <config.h>
  15. #include <gtest/gtest.h>
  16. #include <crypto/crypto.h>
  17. #include <dns/buffer.h>
  18. #include <exceptions/exceptions.h>
  19. #include <boost/scoped_ptr.hpp>
  20. using namespace isc::dns;
  21. using namespace isc::crypto;
  22. namespace {
  23. void checkData(const uint8_t* data, const uint8_t* expected,
  24. size_t len) {
  25. for (size_t i = 0; i < len; ++i) {
  26. ASSERT_EQ(expected[i], data[i]);
  27. }
  28. }
  29. void checkBuffer(const OutputBuffer& buf, const uint8_t* expected,
  30. size_t len)
  31. {
  32. ASSERT_EQ(len, buf.getLength());
  33. checkData(static_cast<const uint8_t*>(buf.getData()), expected,
  34. len);
  35. }
  36. // Sign and verify with the convenience functions
  37. void doHMACTestConv(const std::string& data,
  38. const void* secret,
  39. size_t secret_len,
  40. const HMAC::HashAlgorithm hash_algorithm,
  41. const uint8_t* expected_hmac,
  42. size_t hmac_len) {
  43. OutputBuffer data_buf(data.size());
  44. data_buf.writeData(data.c_str(), data.size());
  45. OutputBuffer hmac_sig(0);
  46. // Sign it
  47. signHMAC(data_buf.getData(), data_buf.getLength(),
  48. secret, secret_len, hash_algorithm, hmac_sig, hmac_len);
  49. // Check if the signature is what we expect
  50. checkBuffer(hmac_sig, expected_hmac, hmac_len);
  51. // Check whether we can verify it ourselves
  52. EXPECT_TRUE(verifyHMAC(data_buf.getData(), data_buf.getLength(),
  53. secret, secret_len, hash_algorithm,
  54. hmac_sig.getData(),
  55. hmac_sig.getLength()));
  56. // Change the sig by flipping the first octet, and check
  57. // whether verification fails then
  58. hmac_sig.writeUint8At(~hmac_sig[0], 0);
  59. EXPECT_FALSE(verifyHMAC(data_buf.getData(), data_buf.getLength(),
  60. secret, secret_len, hash_algorithm,
  61. hmac_sig.getData(),
  62. hmac_sig.getLength()));
  63. }
  64. // Sign and verify with an instantiation of an HMAC object
  65. void doHMACTestDirect(const std::string& data,
  66. const void* secret,
  67. size_t secret_len,
  68. const HMAC::HashAlgorithm hash_algorithm,
  69. const uint8_t* expected_hmac,
  70. size_t hmac_len) {
  71. OutputBuffer data_buf(data.size());
  72. data_buf.writeData(data.c_str(), data.size());
  73. OutputBuffer hmac_sig(1);
  74. // Sign it
  75. boost::scoped_ptr<HMAC> hmac_sign(Crypto::getCrypto().createHMAC(secret, secret_len, hash_algorithm));
  76. hmac_sign->update(data_buf.getData(), data_buf.getLength());
  77. hmac_sign->sign(hmac_sig, hmac_len);
  78. // Check if the signature is what we expect
  79. checkBuffer(hmac_sig, expected_hmac, hmac_len);
  80. // Check whether we can verify it ourselves
  81. boost::scoped_ptr<HMAC> hmac_verify(Crypto::getCrypto().createHMAC(secret, secret_len, hash_algorithm));
  82. hmac_verify->update(data_buf.getData(), data_buf.getLength());
  83. EXPECT_TRUE(hmac_verify->verify(hmac_sig.getData(),
  84. hmac_sig.getLength()));
  85. // Change the sig by flipping the first octet, and check
  86. // whether verification fails then
  87. hmac_sig.writeUint8At(~hmac_sig[0], 0);
  88. EXPECT_FALSE(hmac_verify->verify(hmac_sig.getData(),
  89. hmac_sig.getLength()));
  90. }
  91. void doHMACTestVector(const std::string& data,
  92. const void* secret,
  93. size_t secret_len,
  94. const HMAC::HashAlgorithm hash_algorithm,
  95. const uint8_t* expected_hmac,
  96. size_t hmac_len) {
  97. boost::scoped_ptr<HMAC> hmac_sign(Crypto::getCrypto().createHMAC(secret, secret_len, hash_algorithm));
  98. hmac_sign->update(data.c_str(), data.size());
  99. std::vector<uint8_t> sig = hmac_sign->sign(hmac_len);
  100. ASSERT_EQ(hmac_len, sig.size());
  101. checkData(&sig[0], expected_hmac, hmac_len);
  102. boost::scoped_ptr<HMAC> hmac_verify(Crypto::getCrypto().createHMAC(secret, secret_len, hash_algorithm));
  103. hmac_verify->update(data.c_str(), data.size());
  104. EXPECT_TRUE(hmac_verify->verify(&sig[0], sig.size()));
  105. sig[0] = ~sig[0];
  106. EXPECT_FALSE(hmac_verify->verify(&sig[0], sig.size()));
  107. }
  108. void doHMACTestArray(const std::string& data,
  109. const void* secret,
  110. size_t secret_len,
  111. const HMAC::HashAlgorithm hash_algorithm,
  112. const uint8_t* expected_hmac,
  113. size_t hmac_len) {
  114. boost::scoped_ptr<HMAC> hmac_sign(Crypto::getCrypto().createHMAC(secret, secret_len, hash_algorithm));
  115. hmac_sign->update(data.c_str(), data.size());
  116. // note: this is not exception-safe, and will leak, but
  117. // if there is an unexpected exception in the code below we
  118. // have more important things to fix.
  119. uint8_t* sig = new uint8_t[hmac_len];
  120. hmac_sign->sign(sig, hmac_len);
  121. checkData(sig, expected_hmac, hmac_len);
  122. boost::scoped_ptr<HMAC> hmac_verify(Crypto::getCrypto().createHMAC(secret, secret_len, hash_algorithm));
  123. hmac_verify->update(data.c_str(), data.size());
  124. EXPECT_TRUE(hmac_verify->verify(sig, hmac_len));
  125. sig[0] = ~sig[0];
  126. EXPECT_FALSE(hmac_verify->verify(sig, hmac_len));
  127. delete[] sig;
  128. }
  129. void doHMACTest(const std::string& data,
  130. const void* secret,
  131. size_t secret_len,
  132. const HMAC::HashAlgorithm hash_algorithm,
  133. const uint8_t* expected_hmac,
  134. size_t hmac_len) {
  135. doHMACTestConv(data, secret, secret_len, hash_algorithm,
  136. expected_hmac, hmac_len);
  137. doHMACTestDirect(data, secret, secret_len, hash_algorithm,
  138. expected_hmac, hmac_len);
  139. doHMACTestVector(data, secret, secret_len, hash_algorithm,
  140. expected_hmac, hmac_len);
  141. doHMACTestArray(data, secret, secret_len, hash_algorithm,
  142. expected_hmac, hmac_len);
  143. }
  144. }
  145. //
  146. // Test values taken from RFC 2202
  147. //
  148. TEST(CryptoTest, HMAC_MD5_RFC2202_SIGN) {
  149. const uint8_t secret[] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  150. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  151. 0x0b, 0x0b };
  152. const uint8_t hmac_expected[] = { 0x92, 0x94, 0x72, 0x7a, 0x36,
  153. 0x38, 0xbb, 0x1c, 0x13, 0xf4,
  154. 0x8e, 0xf8, 0x15, 0x8b, 0xfc,
  155. 0x9d };
  156. doHMACTest("Hi There", secret, 16, HMAC::MD5, hmac_expected, 16);
  157. const uint8_t hmac_expected2[] = { 0x75, 0x0c, 0x78, 0x3e, 0x6a,
  158. 0xb0, 0xb5, 0x03, 0xea, 0xa8,
  159. 0x6e, 0x31, 0x0a, 0x5d, 0xb7,
  160. 0x38 };
  161. doHMACTest("what do ya want for nothing?", "Jefe", 4, HMAC::MD5,
  162. hmac_expected2, 16);
  163. const std::string data3(50, 0xdd);
  164. const uint8_t secret3[] = { 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  165. 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  166. 0xaa, 0xaa, 0xaa, 0xaa };
  167. const uint8_t hmac_expected3[] = { 0x56, 0xbe, 0x34, 0x52, 0x1d,
  168. 0x14, 0x4c, 0x88, 0xdb, 0xb8,
  169. 0xc7, 0x33, 0xf0, 0xe8, 0xb3,
  170. 0xf6};
  171. doHMACTest(data3, secret3, 16, HMAC::MD5, hmac_expected3, 16);
  172. const std::string data4(50, 0xcd);
  173. const uint8_t secret4[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
  174. 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
  175. 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
  176. 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
  177. 0x19 };
  178. const uint8_t hmac_expected4[] = { 0x69, 0x7e, 0xaf, 0x0a, 0xca,
  179. 0x3a, 0x3a, 0xea, 0x3a, 0x75,
  180. 0x16, 0x47, 0x46, 0xff, 0xaa,
  181. 0x79 };
  182. doHMACTest(data4, secret4, 25, HMAC::MD5, hmac_expected4, 16);
  183. const uint8_t secret5[] = { 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
  184. 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
  185. 0x0c, 0x0c, 0x0c, 0x0c };
  186. const uint8_t hmac_expected5[] = { 0x56, 0x46, 0x1e, 0xf2, 0x34,
  187. 0x2e, 0xdc, 0x00, 0xf9, 0xba,
  188. 0xb9, 0x95, 0x69, 0x0e, 0xfd,
  189. 0x4c };
  190. doHMACTest("Test With Truncation", secret5, 16, HMAC::MD5,
  191. hmac_expected5, 16);
  192. doHMACTest("Test With Truncation", secret5, 16, HMAC::MD5,
  193. hmac_expected5, 12);
  194. const std::string secret6(80, 0xaa);
  195. const uint8_t hmac_expected6[] = { 0x6b, 0x1a, 0xb7, 0xfe, 0x4b,
  196. 0xd7, 0xbf, 0x8f, 0x0b, 0x62,
  197. 0xe6, 0xce, 0x61, 0xb9, 0xd0,
  198. 0xcd };
  199. doHMACTest("Test Using Larger Than Block-Size Key - Hash Key First",
  200. secret6.c_str(), 80, HMAC::MD5, hmac_expected6, 16);
  201. // same secret as for test 6
  202. const uint8_t hmac_expected7[] = { 0x6f, 0x63, 0x0f, 0xad, 0x67,
  203. 0xcd, 0xa0, 0xee, 0x1f, 0xb1,
  204. 0xf5, 0x62, 0xdb, 0x3a, 0xa5,
  205. 0x3e };
  206. doHMACTest("Test Using Larger Than Block-Size Key and Larger Than "
  207. "One Block-Size Data",
  208. secret6.c_str(), 80, HMAC::MD5, hmac_expected7, 16);
  209. }
  210. //
  211. // Test values taken from RFC 2202
  212. //
  213. TEST(CryptoTest, HMAC_SHA1_RFC2202_SIGN) {
  214. const uint8_t secret[] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  215. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  216. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  217. const uint8_t hmac_expected[] = { 0xb6, 0x17, 0x31, 0x86, 0x55,
  218. 0x05, 0x72, 0x64, 0xe2, 0x8b,
  219. 0xc0, 0xb6, 0xfb, 0x37, 0x8c,
  220. 0x8e, 0xf1, 0x46, 0xbe, 0x00 };
  221. doHMACTest("Hi There", secret, 20, HMAC::SHA1, hmac_expected, 20);
  222. const uint8_t hmac_expected2[] = { 0xef, 0xfc, 0xdf, 0x6a, 0xe5,
  223. 0xeb, 0x2f, 0xa2, 0xd2, 0x74,
  224. 0x16, 0xd5, 0xf1, 0x84, 0xdf,
  225. 0x9c, 0x25, 0x9a, 0x7c, 0x79 };
  226. doHMACTest("what do ya want for nothing?", "Jefe", 4, HMAC::SHA1,
  227. hmac_expected2, 20);
  228. const std::string data3(50, 0xdd);
  229. const uint8_t secret3[] = { 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  230. 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  231. 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  232. 0xaa, 0xaa };
  233. const uint8_t hmac_expected3[] = { 0x12, 0x5d, 0x73, 0x42, 0xb9,
  234. 0xac, 0x11, 0xcd, 0x91, 0xa3,
  235. 0x9a, 0xf4, 0x8a, 0xa1, 0x7b,
  236. 0x4f, 0x63, 0xf1, 0x75, 0xd3 };
  237. doHMACTest(data3, secret3, 20, HMAC::SHA1, hmac_expected3, 20);
  238. const std::string data4(50, 0xcd);
  239. const uint8_t secret4[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
  240. 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
  241. 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
  242. 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
  243. 0x19 };
  244. const uint8_t hmac_expected4[] = { 0x4c, 0x90, 0x07, 0xf4, 0x02,
  245. 0x62, 0x50, 0xc6, 0xbc, 0x84,
  246. 0x14, 0xf9, 0xbf, 0x50, 0xc8,
  247. 0x6c, 0x2d, 0x72, 0x35, 0xda };
  248. doHMACTest(data4, secret4, 25, HMAC::SHA1, hmac_expected4, 20);
  249. const uint8_t secret5[] = { 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
  250. 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
  251. 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
  252. 0x0c, 0x0c };
  253. const uint8_t hmac_expected5[] = { 0x4c, 0x1a, 0x03, 0x42, 0x4b,
  254. 0x55, 0xe0, 0x7f, 0xe7, 0xf2,
  255. 0x7b, 0xe1, 0xd5, 0x8b, 0xb9,
  256. 0x32, 0x4a, 0x9a, 0x5a, 0x04 };
  257. doHMACTest("Test With Truncation", secret5, 20, HMAC::SHA1,
  258. hmac_expected5, 20);
  259. doHMACTest("Test With Truncation", secret5, 20, HMAC::SHA1,
  260. hmac_expected5, 12);
  261. const std::string secret6(80, 0xaa);
  262. const uint8_t hmac_expected6[] = { 0xaa, 0x4a, 0xe5, 0xe1, 0x52,
  263. 0x72, 0xd0, 0x0e, 0x95, 0x70,
  264. 0x56, 0x37, 0xce, 0x8a, 0x3b,
  265. 0x55, 0xed, 0x40, 0x21, 0x12 };
  266. doHMACTest("Test Using Larger Than Block-Size Key - Hash Key First",
  267. secret6.c_str(), 80, HMAC::SHA1, hmac_expected6, 20);
  268. // same secret as for test 6
  269. const uint8_t hmac_expected7[] = { 0xe8, 0xe9, 0x9d, 0x0f, 0x45,
  270. 0x23, 0x7d, 0x78, 0x6d, 0x6b,
  271. 0xba, 0xa7, 0x96, 0x5c, 0x78,
  272. 0x08, 0xbb, 0xff, 0x1a, 0x91 };
  273. doHMACTest("Test Using Larger Than Block-Size Key and Larger Than "
  274. "One Block-Size Data",
  275. secret6.c_str(), 80, HMAC::SHA1, hmac_expected7, 20);
  276. }
  277. //
  278. // Test values taken from RFC 4231
  279. //
  280. TEST(CryptoTest, HMAC_SHA256_RFC2202_SIGN) {
  281. const uint8_t secret[] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  282. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  283. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  284. const uint8_t hmac_expected[] = { 0xb0, 0x34, 0x4c, 0x61, 0xd8,
  285. 0xdb, 0x38, 0x53, 0x5c, 0xa8,
  286. 0xaf, 0xce, 0xaf, 0x0b, 0xf1,
  287. 0x2b, 0x88, 0x1d, 0xc2, 0x00,
  288. 0xc9, 0x83, 0x3d, 0xa7, 0x26,
  289. 0xe9, 0x37, 0x6c, 0x2e, 0x32,
  290. 0xcf, 0xf7 };
  291. doHMACTest("Hi There", secret, 20, HMAC::SHA256, hmac_expected, 32);
  292. const uint8_t hmac_expected2[] = { 0x5b, 0xdc, 0xc1, 0x46, 0xbf,
  293. 0x60, 0x75, 0x4e, 0x6a, 0x04,
  294. 0x24, 0x26, 0x08, 0x95, 0x75,
  295. 0xc7, 0x5a, 0x00, 0x3f, 0x08,
  296. 0x9d, 0x27, 0x39, 0x83, 0x9d,
  297. 0xec, 0x58, 0xb9, 0x64, 0xec,
  298. 0x38, 0x43 };
  299. doHMACTest("what do ya want for nothing?", "Jefe", 4, HMAC::SHA256,
  300. hmac_expected2, 32);
  301. const std::string data3(50, 0xdd);
  302. const uint8_t secret3[] = { 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  303. 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  304. 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  305. 0xaa, 0xaa };
  306. const uint8_t hmac_expected3[] = { 0x77, 0x3e, 0xa9, 0x1e, 0x36,
  307. 0x80, 0x0e, 0x46, 0x85, 0x4d,
  308. 0xb8, 0xeb, 0xd0, 0x91, 0x81,
  309. 0xa7, 0x29, 0x59, 0x09, 0x8b,
  310. 0x3e, 0xf8, 0xc1, 0x22, 0xd9,
  311. 0x63, 0x55, 0x14, 0xce, 0xd5,
  312. 0x65, 0xfe };
  313. doHMACTest(data3, secret3, 20, HMAC::SHA256, hmac_expected3, 32);
  314. const std::string data4(50, 0xcd);
  315. const uint8_t secret4[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
  316. 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
  317. 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
  318. 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
  319. 0x19 };
  320. const uint8_t hmac_expected4[] = { 0x82, 0x55, 0x8a, 0x38, 0x9a,
  321. 0x44, 0x3c, 0x0e, 0xa4, 0xcc,
  322. 0x81, 0x98, 0x99, 0xf2, 0x08,
  323. 0x3a, 0x85, 0xf0, 0xfa, 0xa3,
  324. 0xe5, 0x78, 0xf8, 0x07, 0x7a,
  325. 0x2e, 0x3f, 0xf4, 0x67, 0x29,
  326. 0x66, 0x5b };
  327. doHMACTest(data4, secret4, 25, HMAC::SHA256, hmac_expected4, 32);
  328. const uint8_t secret5[] = { 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
  329. 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
  330. 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
  331. 0x0c, 0x0c };
  332. const uint8_t hmac_expected5[] = { 0xa3, 0xb6, 0x16, 0x74, 0x73,
  333. 0x10, 0x0e, 0xe0, 0x6e, 0x0c,
  334. 0x79, 0x6c, 0x29, 0x55, 0x55,
  335. 0x2b };
  336. doHMACTest("Test With Truncation", secret5, 20, HMAC::SHA256,
  337. hmac_expected5, 16);
  338. const std::string secret6(131, 0xaa);
  339. const uint8_t hmac_expected6[] = { 0x60, 0xe4, 0x31, 0x59, 0x1e,
  340. 0xe0, 0xb6, 0x7f, 0x0d, 0x8a,
  341. 0x26, 0xaa, 0xcb, 0xf5, 0xb7,
  342. 0x7f, 0x8e, 0x0b, 0xc6, 0x21,
  343. 0x37, 0x28, 0xc5, 0x14, 0x05,
  344. 0x46, 0x04, 0x0f, 0x0e, 0xe3,
  345. 0x7f, 0x54 };
  346. doHMACTest("Test Using Larger Than Block-Size Key - Hash Key First",
  347. secret6.c_str(), 131, HMAC::SHA256, hmac_expected6, 32);
  348. // Same secret as test 6
  349. const uint8_t hmac_expected7[] = { 0x9b, 0x09, 0xff, 0xa7, 0x1b,
  350. 0x94, 0x2f, 0xcb, 0x27, 0x63,
  351. 0x5f, 0xbc, 0xd5, 0xb0, 0xe9,
  352. 0x44, 0xbf, 0xdc, 0x63, 0x64,
  353. 0x4f, 0x07, 0x13, 0x93, 0x8a,
  354. 0x7f, 0x51, 0x53, 0x5c, 0x3a,
  355. 0x35, 0xe2 };
  356. doHMACTest("This is a test using a larger than block-size key and a"
  357. " larger than block-size data. The key needs to be hashe"
  358. "d before being used by the HMAC algorithm.",
  359. secret6.c_str(), 131, HMAC::SHA256, hmac_expected7, 32);
  360. }
  361. namespace {
  362. size_t
  363. sigVectorLength(HMAC::HashAlgorithm alg, size_t len) {
  364. boost::scoped_ptr<HMAC> hmac_sign(Crypto::getCrypto().createHMAC("asdf", 4, alg));
  365. hmac_sign->update("asdf", 4);
  366. const std::vector<uint8_t> sig = hmac_sign->sign(len);
  367. return sig.size();
  368. }
  369. size_t
  370. sigBufferLength(HMAC::HashAlgorithm alg, size_t len) {
  371. boost::scoped_ptr<HMAC> hmac_sign(Crypto::getCrypto().createHMAC("asdf", 4, alg));
  372. hmac_sign->update("asdf", 4);
  373. OutputBuffer sig(0);
  374. hmac_sign->sign(sig, len);
  375. return sig.getLength();
  376. }
  377. }
  378. TEST(CryptoTest, HMACSigLengthArgument)
  379. {
  380. std::vector<uint8_t> sig;
  381. EXPECT_EQ(16, sigVectorLength(HMAC::MD5, 0));
  382. EXPECT_EQ(8, sigVectorLength(HMAC::MD5, 8));
  383. EXPECT_EQ(16, sigVectorLength(HMAC::MD5, 16));
  384. EXPECT_EQ(16, sigVectorLength(HMAC::MD5, 40));
  385. EXPECT_EQ(16, sigVectorLength(HMAC::MD5, 2000));
  386. EXPECT_EQ(20, sigBufferLength(HMAC::SHA1, 0));
  387. EXPECT_EQ(8, sigBufferLength(HMAC::SHA1, 8));
  388. EXPECT_EQ(20, sigBufferLength(HMAC::SHA1, 20));
  389. EXPECT_EQ(20, sigBufferLength(HMAC::SHA1, 40));
  390. EXPECT_EQ(20, sigBufferLength(HMAC::SHA1, 2000));
  391. EXPECT_EQ(32, sigBufferLength(HMAC::SHA256, 0));
  392. EXPECT_EQ(8, sigBufferLength(HMAC::SHA256, 8));
  393. EXPECT_EQ(32, sigBufferLength(HMAC::SHA256, 32));
  394. EXPECT_EQ(32, sigBufferLength(HMAC::SHA256, 40));
  395. EXPECT_EQ(32, sigBufferLength(HMAC::SHA256, 3200));
  396. EXPECT_EQ(16, sigBufferLength(HMAC::MD5, 0));
  397. EXPECT_EQ(8, sigBufferLength(HMAC::MD5, 8));
  398. EXPECT_EQ(16, sigBufferLength(HMAC::MD5, 16));
  399. EXPECT_EQ(16, sigBufferLength(HMAC::MD5, 40));
  400. EXPECT_EQ(16, sigBufferLength(HMAC::MD5, 2000));
  401. EXPECT_EQ(20, sigBufferLength(HMAC::SHA1, 0));
  402. EXPECT_EQ(8, sigBufferLength(HMAC::SHA1, 8));
  403. EXPECT_EQ(20, sigBufferLength(HMAC::SHA1, 20));
  404. EXPECT_EQ(20, sigBufferLength(HMAC::SHA1, 40));
  405. EXPECT_EQ(20, sigBufferLength(HMAC::SHA1, 2000));
  406. EXPECT_EQ(32, sigBufferLength(HMAC::SHA256, 0));
  407. EXPECT_EQ(8, sigBufferLength(HMAC::SHA256, 8));
  408. EXPECT_EQ(32, sigBufferLength(HMAC::SHA256, 32));
  409. EXPECT_EQ(32, sigBufferLength(HMAC::SHA256, 40));
  410. EXPECT_EQ(32, sigBufferLength(HMAC::SHA256, 3200));
  411. }
  412. TEST(CryptoTest, BadKey) {
  413. OutputBuffer data_buf(0);
  414. OutputBuffer hmac_sig(0);
  415. EXPECT_THROW(Crypto::getCrypto().createHMAC(NULL, 0, HMAC::MD5), BadKey);
  416. EXPECT_THROW(Crypto::getCrypto().createHMAC(NULL, 0, HMAC::UNKNOWN), UnsupportedAlgorithm);
  417. EXPECT_THROW(signHMAC(data_buf.getData(), data_buf.getLength(),
  418. NULL, 0, HMAC::MD5, hmac_sig), BadKey);
  419. EXPECT_THROW(signHMAC(data_buf.getData(), data_buf.getLength(),
  420. NULL, 0, HMAC::UNKNOWN, hmac_sig),
  421. UnsupportedAlgorithm);
  422. EXPECT_THROW(verifyHMAC(data_buf.getData(), data_buf.getLength(),
  423. NULL, 0, HMAC::MD5, hmac_sig.getData(),
  424. hmac_sig.getLength()), BadKey);
  425. EXPECT_THROW(verifyHMAC(data_buf.getData(), data_buf.getLength(),
  426. NULL, 0, HMAC::UNKNOWN, hmac_sig.getData(),
  427. hmac_sig.getLength()),
  428. UnsupportedAlgorithm);
  429. }
  430. TEST(CryptoTest, Singleton) {
  431. /*
  432. Crypto& c1 = Crypto::getCrypto();
  433. Crypto& c2 = Crypto::getCrypto();
  434. ASSERT_EQ(&c1, &c2);
  435. */
  436. }