crypto_unittests.cc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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 <string>
  16. #include <vector>
  17. #include <boost/lexical_cast.hpp>
  18. #include <gtest/gtest.h>
  19. #include <util/encode/hex.h>
  20. #include <cryptolink/cryptolink.h>
  21. #include <cryptolink/crypto_hmac.h>
  22. #include <util/buffer.h>
  23. #include <exceptions/exceptions.h>
  24. #include <boost/shared_ptr.hpp>
  25. using namespace boost;
  26. using namespace isc::util;
  27. using namespace isc::util::encode;
  28. using namespace isc::cryptolink;
  29. namespace {
  30. void checkData(const uint8_t* data, const uint8_t* expected,
  31. size_t len) {
  32. for (size_t i = 0; i < len; ++i) {
  33. ASSERT_EQ(expected[i], data[i]);
  34. }
  35. }
  36. void checkBuffer(const OutputBuffer& buf, const uint8_t* expected,
  37. size_t len)
  38. {
  39. ASSERT_EQ(len, buf.getLength());
  40. checkData(static_cast<const uint8_t*>(buf.getData()), expected,
  41. len);
  42. }
  43. // Sign and verify with the convenience functions
  44. void doHMACTestConv(const std::string& data,
  45. const void* secret,
  46. size_t secret_len,
  47. const HashAlgorithm hash_algorithm,
  48. const uint8_t* expected_hmac,
  49. size_t hmac_len) {
  50. OutputBuffer data_buf(data.size());
  51. data_buf.writeData(data.c_str(), data.size());
  52. OutputBuffer hmac_sig(0);
  53. // Sign it
  54. signHMAC(data_buf.getData(), data_buf.getLength(),
  55. secret, secret_len, hash_algorithm, hmac_sig, hmac_len);
  56. // Check if the signature is what we expect
  57. checkBuffer(hmac_sig, expected_hmac, hmac_len);
  58. // Check whether we can verify it ourselves
  59. EXPECT_TRUE(verifyHMAC(data_buf.getData(), data_buf.getLength(),
  60. secret, secret_len, hash_algorithm,
  61. hmac_sig.getData(),
  62. hmac_sig.getLength()));
  63. // Change the sig by flipping the first octet, and check
  64. // whether verification fails then
  65. hmac_sig.writeUint8At(~hmac_sig[0], 0);
  66. EXPECT_FALSE(verifyHMAC(data_buf.getData(), data_buf.getLength(),
  67. secret, secret_len, hash_algorithm,
  68. hmac_sig.getData(),
  69. hmac_sig.getLength()));
  70. }
  71. // Sign and verify with an instantiation of an HMAC object
  72. void doHMACTestDirect(const std::string& data,
  73. const void* secret,
  74. size_t secret_len,
  75. const HashAlgorithm hash_algorithm,
  76. const uint8_t* expected_hmac,
  77. size_t hmac_len) {
  78. OutputBuffer data_buf(data.size());
  79. data_buf.writeData(data.c_str(), data.size());
  80. OutputBuffer hmac_sig(1);
  81. CryptoLink& crypto = CryptoLink::getCryptoLink();
  82. // Sign it
  83. boost::shared_ptr<HMAC> hmac_sign(crypto.createHMAC(secret,
  84. secret_len,
  85. hash_algorithm),
  86. deleteHMAC);
  87. hmac_sign->update(data_buf.getData(), data_buf.getLength());
  88. hmac_sign->sign(hmac_sig, hmac_len);
  89. // Check if the signature is what we expect
  90. checkBuffer(hmac_sig, expected_hmac, hmac_len);
  91. // Check whether we can verify it ourselves
  92. boost::shared_ptr<HMAC> hmac_verify(crypto.createHMAC(secret,
  93. secret_len,
  94. hash_algorithm),
  95. deleteHMAC);
  96. hmac_verify->update(data_buf.getData(), data_buf.getLength());
  97. EXPECT_TRUE(hmac_verify->verify(hmac_sig.getData(),
  98. hmac_sig.getLength()));
  99. // Change the sig by flipping the first octet, and check
  100. // whether verification fails then
  101. hmac_sig.writeUint8At(~hmac_sig[0], 0);
  102. EXPECT_FALSE(hmac_verify->verify(hmac_sig.getData(),
  103. hmac_sig.getLength()));
  104. }
  105. void doHMACTestVector(const std::string& data,
  106. const void* secret,
  107. size_t secret_len,
  108. const HashAlgorithm hash_algorithm,
  109. const uint8_t* expected_hmac,
  110. size_t hmac_len) {
  111. CryptoLink& crypto = CryptoLink::getCryptoLink();
  112. boost::shared_ptr<HMAC> hmac_sign(crypto.createHMAC(secret,
  113. secret_len,
  114. hash_algorithm),
  115. deleteHMAC);
  116. hmac_sign->update(data.c_str(), data.size());
  117. std::vector<uint8_t> sig = hmac_sign->sign(hmac_len);
  118. ASSERT_EQ(hmac_len, sig.size());
  119. checkData(&sig[0], expected_hmac, hmac_len);
  120. boost::shared_ptr<HMAC> hmac_verify(crypto.createHMAC(secret,
  121. secret_len,
  122. hash_algorithm),
  123. deleteHMAC);
  124. hmac_verify->update(data.c_str(), data.size());
  125. EXPECT_TRUE(hmac_verify->verify(&sig[0], sig.size()));
  126. sig[0] = ~sig[0];
  127. EXPECT_FALSE(hmac_verify->verify(&sig[0], sig.size()));
  128. }
  129. void doHMACTestArray(const std::string& data,
  130. const void* secret,
  131. size_t secret_len,
  132. const HashAlgorithm hash_algorithm,
  133. const uint8_t* expected_hmac,
  134. size_t hmac_len) {
  135. CryptoLink& crypto = CryptoLink::getCryptoLink();
  136. boost::shared_ptr<HMAC> hmac_sign(crypto.createHMAC(secret,
  137. secret_len,
  138. hash_algorithm),
  139. deleteHMAC);
  140. hmac_sign->update(data.c_str(), data.size());
  141. // note: this is not exception-safe, and can leak, but
  142. // if there is an unexpected exception in the code below we
  143. // have more important things to fix.
  144. uint8_t* sig = new uint8_t[hmac_len];
  145. hmac_sign->sign(sig, hmac_len);
  146. checkData(sig, expected_hmac, hmac_len);
  147. boost::shared_ptr<HMAC> hmac_verify(crypto.createHMAC(secret,
  148. secret_len,
  149. hash_algorithm),
  150. deleteHMAC);
  151. hmac_verify->update(data.c_str(), data.size());
  152. EXPECT_TRUE(hmac_verify->verify(sig, hmac_len));
  153. sig[0] = ~sig[0];
  154. EXPECT_FALSE(hmac_verify->verify(sig, hmac_len));
  155. delete[] sig;
  156. }
  157. void doHMACTest(const std::string& data,
  158. const void* secret,
  159. size_t secret_len,
  160. const HashAlgorithm hash_algorithm,
  161. const uint8_t* expected_hmac,
  162. size_t hmac_len) {
  163. doHMACTestConv(data, secret, secret_len, hash_algorithm,
  164. expected_hmac, hmac_len);
  165. doHMACTestDirect(data, secret, secret_len, hash_algorithm,
  166. expected_hmac, hmac_len);
  167. doHMACTestVector(data, secret, secret_len, hash_algorithm,
  168. expected_hmac, hmac_len);
  169. doHMACTestArray(data, secret, secret_len, hash_algorithm,
  170. expected_hmac, hmac_len);
  171. }
  172. }
  173. //
  174. // Test values taken from RFC 2202
  175. //
  176. TEST(CryptoLinkTest, HMAC_MD5_RFC2202_SIGN) {
  177. const uint8_t secret[] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  178. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  179. 0x0b, 0x0b };
  180. const uint8_t hmac_expected[] = { 0x92, 0x94, 0x72, 0x7a, 0x36,
  181. 0x38, 0xbb, 0x1c, 0x13, 0xf4,
  182. 0x8e, 0xf8, 0x15, 0x8b, 0xfc,
  183. 0x9d };
  184. doHMACTest("Hi There", secret, 16, MD5, hmac_expected, 16);
  185. const uint8_t hmac_expected2[] = { 0x75, 0x0c, 0x78, 0x3e, 0x6a,
  186. 0xb0, 0xb5, 0x03, 0xea, 0xa8,
  187. 0x6e, 0x31, 0x0a, 0x5d, 0xb7,
  188. 0x38 };
  189. doHMACTest("what do ya want for nothing?", "Jefe", 4, MD5,
  190. hmac_expected2, 16);
  191. const uint8_t secret3[] = { 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  192. 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  193. 0xaa, 0xaa, 0xaa, 0xaa };
  194. const uint8_t hmac_expected3[] = { 0x56, 0xbe, 0x34, 0x52, 0x1d,
  195. 0x14, 0x4c, 0x88, 0xdb, 0xb8,
  196. 0xc7, 0x33, 0xf0, 0xe8, 0xb3,
  197. 0xf6};
  198. doHMACTest(std::string(50, 0xdd), secret3, 16, MD5, hmac_expected3, 16);
  199. const std::string data4(50, 0xcd);
  200. const uint8_t secret4[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
  201. 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
  202. 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
  203. 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
  204. 0x19 };
  205. const uint8_t hmac_expected4[] = { 0x69, 0x7e, 0xaf, 0x0a, 0xca,
  206. 0x3a, 0x3a, 0xea, 0x3a, 0x75,
  207. 0x16, 0x47, 0x46, 0xff, 0xaa,
  208. 0x79 };
  209. doHMACTest(data4, secret4, 25, MD5, hmac_expected4, 16);
  210. const uint8_t hmac_expected6[] = { 0x6b, 0x1a, 0xb7, 0xfe, 0x4b,
  211. 0xd7, 0xbf, 0x8f, 0x0b, 0x62,
  212. 0xe6, 0xce, 0x61, 0xb9, 0xd0,
  213. 0xcd };
  214. doHMACTest("Test Using Larger Than Block-Size Key - Hash Key First",
  215. std::string(80, 0xaa).c_str(), 80, MD5, hmac_expected6, 16);
  216. const uint8_t hmac_expected7[] = { 0x6f, 0x63, 0x0f, 0xad, 0x67,
  217. 0xcd, 0xa0, 0xee, 0x1f, 0xb1,
  218. 0xf5, 0x62, 0xdb, 0x3a, 0xa5,
  219. 0x3e };
  220. doHMACTest("Test Using Larger Than Block-Size Key and Larger Than "
  221. "One Block-Size Data",
  222. std::string(80, 0xaa).c_str(), 80, MD5, hmac_expected7, 16);
  223. }
  224. // Temporarily disabled
  225. TEST(CryptoLinkTest, DISABLED_HMAC_MD5_RFC2202_SIGN_TRUNCATED) {
  226. const uint8_t secret5[] = { 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
  227. 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
  228. 0x0c, 0x0c, 0x0c, 0x0c };
  229. const uint8_t hmac_expected5[] = { 0x56, 0x46, 0x1e, 0xf2, 0x34,
  230. 0x2e, 0xdc, 0x00, 0xf9, 0xba,
  231. 0xb9, 0x95, 0x69, 0x0e, 0xfd,
  232. 0x4c };
  233. doHMACTest("Test With Truncation", secret5, 16, MD5,
  234. hmac_expected5, 16);
  235. doHMACTest("Test With Truncation", secret5, 16, MD5,
  236. hmac_expected5, 12);
  237. }
  238. //
  239. // Test values taken from RFC 2202
  240. //
  241. TEST(CryptoLinkTest, HMAC_SHA1_RFC2202_SIGN) {
  242. const uint8_t secret[] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  243. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  244. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  245. const uint8_t hmac_expected[] = { 0xb6, 0x17, 0x31, 0x86, 0x55,
  246. 0x05, 0x72, 0x64, 0xe2, 0x8b,
  247. 0xc0, 0xb6, 0xfb, 0x37, 0x8c,
  248. 0x8e, 0xf1, 0x46, 0xbe, 0x00 };
  249. doHMACTest("Hi There", secret, 20, SHA1, hmac_expected, 20);
  250. const uint8_t hmac_expected2[] = { 0xef, 0xfc, 0xdf, 0x6a, 0xe5,
  251. 0xeb, 0x2f, 0xa2, 0xd2, 0x74,
  252. 0x16, 0xd5, 0xf1, 0x84, 0xdf,
  253. 0x9c, 0x25, 0x9a, 0x7c, 0x79 };
  254. doHMACTest("what do ya want for nothing?", "Jefe", 4, SHA1,
  255. hmac_expected2, 20);
  256. const uint8_t secret3[] = { 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  257. 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  258. 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  259. 0xaa, 0xaa };
  260. const uint8_t hmac_expected3[] = { 0x12, 0x5d, 0x73, 0x42, 0xb9,
  261. 0xac, 0x11, 0xcd, 0x91, 0xa3,
  262. 0x9a, 0xf4, 0x8a, 0xa1, 0x7b,
  263. 0x4f, 0x63, 0xf1, 0x75, 0xd3 };
  264. doHMACTest(std::string(50, 0xdd), secret3, 20, SHA1, hmac_expected3, 20);
  265. const uint8_t secret4[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
  266. 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
  267. 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
  268. 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
  269. 0x19 };
  270. const uint8_t hmac_expected4[] = { 0x4c, 0x90, 0x07, 0xf4, 0x02,
  271. 0x62, 0x50, 0xc6, 0xbc, 0x84,
  272. 0x14, 0xf9, 0xbf, 0x50, 0xc8,
  273. 0x6c, 0x2d, 0x72, 0x35, 0xda };
  274. doHMACTest(std::string(50, 0xcd), secret4, 25, SHA1, hmac_expected4, 20);
  275. const uint8_t hmac_expected6[] = { 0xaa, 0x4a, 0xe5, 0xe1, 0x52,
  276. 0x72, 0xd0, 0x0e, 0x95, 0x70,
  277. 0x56, 0x37, 0xce, 0x8a, 0x3b,
  278. 0x55, 0xed, 0x40, 0x21, 0x12 };
  279. doHMACTest("Test Using Larger Than Block-Size Key - Hash Key First",
  280. std::string(80, 0xaa).c_str(), 80, SHA1, hmac_expected6, 20);
  281. const uint8_t hmac_expected7[] = { 0xe8, 0xe9, 0x9d, 0x0f, 0x45,
  282. 0x23, 0x7d, 0x78, 0x6d, 0x6b,
  283. 0xba, 0xa7, 0x96, 0x5c, 0x78,
  284. 0x08, 0xbb, 0xff, 0x1a, 0x91 };
  285. doHMACTest("Test Using Larger Than Block-Size Key and Larger Than "
  286. "One Block-Size Data",
  287. std::string(80, 0xaa).c_str(), 80, SHA1, hmac_expected7, 20);
  288. }
  289. // Temporarily disabled
  290. TEST(CryptoLinkTest, DISABLED_HMAC_SHA1_RFC2202_SIGN_TRUNCATED) {
  291. const uint8_t secret5[] = { 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
  292. 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
  293. 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
  294. 0x0c, 0x0c };
  295. const uint8_t hmac_expected5[] = { 0x4c, 0x1a, 0x03, 0x42, 0x4b,
  296. 0x55, 0xe0, 0x7f, 0xe7, 0xf2,
  297. 0x7b, 0xe1, 0xd5, 0x8b, 0xb9,
  298. 0x32, 0x4a, 0x9a, 0x5a, 0x04 };
  299. doHMACTest("Test With Truncation", secret5, 20, SHA1,
  300. hmac_expected5, 20);
  301. doHMACTest("Test With Truncation", secret5, 20, SHA1,
  302. hmac_expected5, 12);
  303. }
  304. //
  305. // Test values taken from RFC 4231
  306. //
  307. // Test data from RFC4231, including secret key
  308. // and source data, they are common for sha224/256/384/512
  309. // so put them together within the separate function.
  310. void
  311. doRFC4231Tests(HashAlgorithm hash_algorithm,
  312. const std::vector<std::vector<uint8_t> >& hmac_list)
  313. {
  314. std::vector<std::string> data_list;
  315. std::vector<std::string> secret_list;
  316. data_list.push_back("Hi There");
  317. data_list.push_back("what do ya want for nothing?");
  318. data_list.push_back(std::string(50, 0xdd));
  319. data_list.push_back(std::string(50, 0xcd));
  320. data_list.push_back("Test With Truncation");
  321. data_list.push_back("Test Using Larger Than Block-Size Key - "
  322. "Hash Key First");
  323. data_list.push_back("This is a test using a larger than block-size "
  324. "key and a larger than block-size data. The key "
  325. "needs to be hashed before being used by the HMAC "
  326. "algorithm.");
  327. secret_list.push_back(std::string(20, 0x0b));
  328. secret_list.push_back("Jefe");
  329. secret_list.push_back(std::string(20, 0xaa));
  330. const uint8_t secret_array[] = {
  331. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
  332. 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
  333. 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
  334. 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
  335. 0x19
  336. };
  337. secret_list.push_back(std::string(secret_array,
  338. secret_array + sizeof(secret_array)));
  339. secret_list.push_back(std::string(20, 0x0c));
  340. secret_list.push_back(std::string(131, 0xaa));
  341. secret_list.push_back(std::string(131, 0xaa));
  342. // Make sure we provide a consistent size of test data
  343. ASSERT_EQ(secret_list.size(), data_list.size());
  344. ASSERT_EQ(secret_list.size(), hmac_list.size());
  345. for (std::vector<std::string>::size_type i = 0;
  346. i < data_list.size(); ++i) {
  347. SCOPED_TRACE("RFC4231 HMAC test for algorithm ID: " +
  348. lexical_cast<std::string>(hash_algorithm) +
  349. ", data ID: " + lexical_cast<std::string>(i));
  350. // Until #920 is resolved we have to skip truncation cases.
  351. if (data_list[i] == "Test With Truncation") {
  352. continue;
  353. }
  354. doHMACTest(data_list[i], secret_list[i].c_str(), secret_list[i].size(),
  355. hash_algorithm, &hmac_list[i][0], hmac_list[i].size());
  356. }
  357. }
  358. TEST(CryptoLinkTest, HMAC_SHA256_RFC4231_SIGN) {
  359. std::vector<std::vector<uint8_t> > hmac_expected_list(7);
  360. int i = 0;
  361. decodeHex(
  362. "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7",
  363. hmac_expected_list[i++]);
  364. decodeHex(
  365. "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843",
  366. hmac_expected_list[i++]);
  367. decodeHex(
  368. "773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe",
  369. hmac_expected_list[i++]);
  370. decodeHex(
  371. "82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b",
  372. hmac_expected_list[i++]);
  373. decodeHex("a3b6167473100ee06e0c796c2955552b", hmac_expected_list[i++]);
  374. decodeHex(
  375. "60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54",
  376. hmac_expected_list[i++]);
  377. decodeHex(
  378. "9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f51535c3a35e2",
  379. hmac_expected_list[i++]);
  380. doRFC4231Tests(SHA256, hmac_expected_list);
  381. }
  382. //
  383. // Test values taken from RFC 4231, test optional algorithm 224,384,512
  384. //
  385. TEST(CryptoLinkTest, HMAC_SHA224_RFC4231_SIGN) {
  386. std::vector<std::vector<uint8_t> > hmac_expected_list(7);
  387. int i = 0;
  388. decodeHex("896fb1128abbdf196832107cd49df33f47b4b1169912ba4f53684b22",
  389. hmac_expected_list[i++]);
  390. decodeHex("a30e01098bc6dbbf45690f3a7e9e6d0f8bbea2a39e6148008fd05e44",
  391. hmac_expected_list[i++]);
  392. decodeHex("7fb3cb3588c6c1f6ffa9694d7d6ad2649365b0c1f65d69d1ec8333ea",
  393. hmac_expected_list[i++]);
  394. decodeHex("6c11506874013cac6a2abc1bb382627cec6a90d86efc012de7afec5a",
  395. hmac_expected_list[i++]);
  396. decodeHex("0e2aea68a90c8d37c988bcdb9fca6fa8", hmac_expected_list[i++]);
  397. decodeHex("95e9a0db962095adaebe9b2d6f0dbce2d499f112f2d2b7273fa6870e",
  398. hmac_expected_list[i++]);
  399. decodeHex("3a854166ac5d9f023f54d517d0b39dbd946770db9c2b95c9f6f565d1",
  400. hmac_expected_list[i++]);
  401. doRFC4231Tests(SHA224, hmac_expected_list);
  402. }
  403. TEST(CryptoLinkTest, HMAC_SHA384_RFC4231_SIGN) {
  404. std::vector<std::vector<uint8_t> > hmac_expected_list(7);
  405. int i = 0;
  406. decodeHex("afd03944d84895626b0825f4ab46907f15f9dadbe4101ec682aa034c7cebc5"
  407. "9cfaea9ea9076ede7f4af152e8b2fa9cb6", hmac_expected_list[i++]);
  408. decodeHex("af45d2e376484031617f78d2b58a6b1b9c7ef464f5a01b47e42ec373632244"
  409. "5e8e2240ca5e69e2c78b3239ecfab21649", hmac_expected_list[i++]);
  410. decodeHex("88062608d3e6ad8a0aa2ace014c8a86f0aa635d947ac9febe83ef4e5596614"
  411. "4b2a5ab39dc13814b94e3ab6e101a34f27", hmac_expected_list[i++]);
  412. decodeHex("3e8a69b7783c25851933ab6290af6ca77a9981480850009cc5577c6e1f573b"
  413. "4e6801dd23c4a7d679ccf8a386c674cffb", hmac_expected_list[i++]);
  414. decodeHex("3abf34c3503b2a23a46efc619baef897", hmac_expected_list[i++]);
  415. decodeHex("4ece084485813e9088d2c63a041bc5b44f9ef1012a2b588f3cd11f05033ac4"
  416. "c60c2ef6ab4030fe8296248df163f44952", hmac_expected_list[i++]);
  417. decodeHex("6617178e941f020d351e2f254e8fd32c602420feb0b8fb9adccebb82461e99"
  418. "c5a678cc31e799176d3860e6110c46523e", hmac_expected_list[i++]);
  419. doRFC4231Tests(SHA384, hmac_expected_list);
  420. }
  421. TEST(CryptoLinkTest, HMAC_SHA512_RFC4231_SIGN) {
  422. std::vector<std::vector<uint8_t> > hmac_expected_list(7);
  423. int i = 0;
  424. decodeHex("87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17c"
  425. "dedaa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a12"
  426. "6854", hmac_expected_list[i++]);
  427. decodeHex("164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea2505"
  428. "549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bc"
  429. "e737", hmac_expected_list[i++]);
  430. decodeHex("fa73b0089d56a284efb0f0756c890be9b1b5dbdd8ee81a3655f83e33b2279d"
  431. "39bf3e848279a722c806b485a47e67c807b946a337bee8942674278859e132"
  432. "92fb", hmac_expected_list[i++]);
  433. decodeHex("b0ba465637458c6990e5a8c5f61d4af7e576d97ff94b872de76f8050361ee3"
  434. "dba91ca5c11aa25eb4d679275cc5788063a5f19741120c4f2de2adebeb10a2"
  435. "98dd", hmac_expected_list[i++]);
  436. decodeHex("415fad6271580a531d4179bc891d87a6", hmac_expected_list[i++]);
  437. decodeHex("80b24263c7c1a3ebb71493c1dd7be8b49b46d1f41b4aeec1121b013783f8f3"
  438. "526b56d037e05f2598bd0fd2215d6a1e5295e64f73f63f0aec8b915a985d78"
  439. "6598", hmac_expected_list[i++]);
  440. decodeHex("e37b6a775dc87dbaa4dfa9f96e5e3ffddebd71f8867289865df5a32d20cdc9"
  441. "44b6022cac3c4982b10d5eeb55c3e4de15134676fb6de0446065c97440fa8c"
  442. "6a58", hmac_expected_list[i++]);
  443. doRFC4231Tests(SHA512, hmac_expected_list);
  444. }
  445. TEST(CryptoLinkTest, DISABLED_HMAC_SHA256_RFC2202_SIGN_TRUNCATED) {
  446. const uint8_t secret5[] = { 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
  447. 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
  448. 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
  449. 0x0c, 0x0c };
  450. const uint8_t hmac_expected5[] = { 0xa3, 0xb6, 0x16, 0x74, 0x73,
  451. 0x10, 0x0e, 0xe0, 0x6e, 0x0c,
  452. 0x79, 0x6c, 0x29, 0x55, 0x55,
  453. 0x2b };
  454. doHMACTest("Test With Truncation", secret5, 20, SHA256,
  455. hmac_expected5, 16);
  456. }
  457. namespace {
  458. size_t
  459. sigVectorLength(HashAlgorithm alg, size_t len) {
  460. boost::shared_ptr<HMAC> hmac_sign(
  461. CryptoLink::getCryptoLink().createHMAC("asdf", 4, alg),
  462. deleteHMAC);
  463. hmac_sign->update("asdf", 4);
  464. const std::vector<uint8_t> sig = hmac_sign->sign(len);
  465. return (sig.size());
  466. }
  467. size_t
  468. sigBufferLength(HashAlgorithm alg, size_t len) {
  469. boost::shared_ptr<HMAC> hmac_sign(
  470. CryptoLink::getCryptoLink().createHMAC("asdf", 4, alg),
  471. deleteHMAC);
  472. hmac_sign->update("asdf", 4);
  473. OutputBuffer sig(0);
  474. hmac_sign->sign(sig, len);
  475. return (sig.getLength());
  476. }
  477. }
  478. TEST(CryptoLinkTest, HMACSigLengthArgument) {
  479. std::vector<uint8_t> sig;
  480. EXPECT_EQ(16, sigVectorLength(MD5, 0));
  481. EXPECT_EQ(8, sigVectorLength(MD5, 8));
  482. EXPECT_EQ(16, sigVectorLength(MD5, 16));
  483. EXPECT_EQ(16, sigVectorLength(MD5, 40));
  484. EXPECT_EQ(16, sigVectorLength(MD5, 2000));
  485. EXPECT_EQ(20, sigBufferLength(SHA1, 0));
  486. EXPECT_EQ(8, sigBufferLength(SHA1, 8));
  487. EXPECT_EQ(20, sigBufferLength(SHA1, 20));
  488. EXPECT_EQ(20, sigBufferLength(SHA1, 40));
  489. EXPECT_EQ(20, sigBufferLength(SHA1, 2000));
  490. EXPECT_EQ(32, sigBufferLength(SHA256, 0));
  491. EXPECT_EQ(8, sigBufferLength(SHA256, 8));
  492. EXPECT_EQ(32, sigBufferLength(SHA256, 32));
  493. EXPECT_EQ(32, sigBufferLength(SHA256, 40));
  494. EXPECT_EQ(32, sigBufferLength(SHA256, 3200));
  495. EXPECT_EQ(16, sigBufferLength(MD5, 0));
  496. EXPECT_EQ(8, sigBufferLength(MD5, 8));
  497. EXPECT_EQ(16, sigBufferLength(MD5, 16));
  498. EXPECT_EQ(16, sigBufferLength(MD5, 40));
  499. EXPECT_EQ(16, sigBufferLength(MD5, 2000));
  500. EXPECT_EQ(20, sigBufferLength(SHA1, 0));
  501. EXPECT_EQ(8, sigBufferLength(SHA1, 8));
  502. EXPECT_EQ(20, sigBufferLength(SHA1, 20));
  503. EXPECT_EQ(20, sigBufferLength(SHA1, 40));
  504. EXPECT_EQ(20, sigBufferLength(SHA1, 2000));
  505. EXPECT_EQ(32, sigBufferLength(SHA256, 0));
  506. EXPECT_EQ(8, sigBufferLength(SHA256, 8));
  507. EXPECT_EQ(32, sigBufferLength(SHA256, 32));
  508. EXPECT_EQ(32, sigBufferLength(SHA256, 40));
  509. EXPECT_EQ(32, sigBufferLength(SHA256, 3200));
  510. }
  511. TEST(CryptoLinkTest, BadKey) {
  512. OutputBuffer data_buf(0);
  513. OutputBuffer hmac_sig(0);
  514. CryptoLink& crypto = CryptoLink::getCryptoLink();
  515. EXPECT_THROW(crypto.createHMAC(NULL, 0, MD5), BadKey);
  516. EXPECT_THROW(crypto.createHMAC(NULL, 0, UNKNOWN_HASH), UnsupportedAlgorithm);
  517. EXPECT_THROW(signHMAC(data_buf.getData(), data_buf.getLength(),
  518. NULL, 0, MD5, hmac_sig), BadKey);
  519. EXPECT_THROW(signHMAC(data_buf.getData(), data_buf.getLength(),
  520. NULL, 0, UNKNOWN_HASH, hmac_sig),
  521. UnsupportedAlgorithm);
  522. EXPECT_THROW(verifyHMAC(data_buf.getData(), data_buf.getLength(),
  523. NULL, 0, MD5, hmac_sig.getData(),
  524. hmac_sig.getLength()), BadKey);
  525. EXPECT_THROW(verifyHMAC(data_buf.getData(), data_buf.getLength(),
  526. NULL, 0, UNKNOWN_HASH, hmac_sig.getData(),
  527. hmac_sig.getLength()),
  528. UnsupportedAlgorithm);
  529. }
  530. TEST(CryptoLinkTest, Singleton) {
  531. const CryptoLink& c1 = CryptoLink::getCryptoLink();
  532. const CryptoLink& c2 = CryptoLink::getCryptoLink();
  533. ASSERT_EQ(&c1, &c2);
  534. }