crypto_unittests.cc 24 KB

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