hmac_unittests.cc 30 KB

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