crypto_unittests.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 <dns/name.h>
  19. #include <exceptions/exceptions.h>
  20. using namespace isc::dns;
  21. using namespace isc::crypto;
  22. namespace {
  23. void checkBuffer(const OutputBuffer& buf, uint8_t *data, size_t len) {
  24. ASSERT_EQ(len, buf.getLength());
  25. const uint8_t* buf_d = static_cast<const uint8_t*>(buf.getData());
  26. for (size_t i = 0; i < len; ++i) {
  27. ASSERT_EQ(data[i], buf_d[i]);
  28. }
  29. }
  30. // Sign and verify with the convenience functions
  31. void doHMACTestConv(const std::string& data,
  32. const std::string& key_str,
  33. uint8_t* expected_hmac,
  34. size_t hmac_len) {
  35. OutputBuffer data_buf(data.size());
  36. data_buf.writeData(data.c_str(), data.size());
  37. OutputBuffer hmac_sig(1);
  38. TSIGKey key(key_str);
  39. // Sign it
  40. signHMAC(data_buf.getData(), data_buf.getLength(), key,
  41. hmac_sig);
  42. // Check if the signature is what we expect
  43. checkBuffer(hmac_sig, expected_hmac, hmac_len);
  44. // Check whether we can verify it ourselves
  45. EXPECT_TRUE(verifyHMAC(data_buf.getData(), data_buf.getLength(),
  46. key, hmac_sig.getData(),
  47. hmac_sig.getLength()));
  48. // Change the sig by flipping the first octet, and check
  49. // whether verification fails then
  50. hmac_sig.writeUint8At(~hmac_sig[0], 0);
  51. EXPECT_FALSE(verifyHMAC(data_buf.getData(), data_buf.getLength(),
  52. key, hmac_sig.getData(),
  53. hmac_sig.getLength()));
  54. }
  55. // Sign and verify with an instantiation of an HMAC object
  56. void doHMACTestDirect(const std::string& data,
  57. const std::string& key_str,
  58. uint8_t* expected_hmac,
  59. size_t hmac_len) {
  60. OutputBuffer data_buf(data.size());
  61. data_buf.writeData(data.c_str(), data.size());
  62. OutputBuffer hmac_sig(1);
  63. TSIGKey key(key_str);
  64. // Sign it
  65. HMAC hmac_sign(key);
  66. hmac_sign.update(data_buf.getData(), data_buf.getLength());
  67. hmac_sign.sign(hmac_sig);
  68. // Check if the signature is what we expect
  69. checkBuffer(hmac_sig, expected_hmac, hmac_len);
  70. // Check whether we can verify it ourselves
  71. HMAC hmac_verify(key);
  72. hmac_verify.update(data_buf.getData(), data_buf.getLength());
  73. EXPECT_TRUE(hmac_verify.verify(hmac_sig.getData(),
  74. hmac_sig.getLength()));
  75. // Change the sig by flipping the first octet, and check
  76. // whether verification fails then
  77. hmac_sig.writeUint8At(~hmac_sig[0], 0);
  78. EXPECT_FALSE(hmac_verify.verify(hmac_sig.getData(),
  79. hmac_sig.getLength()));
  80. }
  81. void doHMACTest(const std::string& data,
  82. const std::string& key_str,
  83. uint8_t* expected_hmac,
  84. size_t hmac_len) {
  85. doHMACTestConv(data, key_str, expected_hmac, hmac_len);
  86. doHMACTestDirect(data, key_str, expected_hmac, hmac_len);
  87. }
  88. }
  89. //
  90. // Test values taken from RFC 2202
  91. //
  92. TEST(CryptoTest, HMAC_MD5_RFC2202_SIGN) {
  93. uint8_t hmac_expected[] = { 0x92, 0x94, 0x72, 0x7a, 0x36, 0x38,
  94. 0xbb, 0x1c, 0x13, 0xf4, 0x8e, 0xf8,
  95. 0x15, 0x8b, 0xfc, 0x9d };
  96. doHMACTest("Hi There",
  97. "test.example:CwsLCwsLCwsLCwsLCwsLCw==:hmac-md5.sig-alg.reg.int",
  98. hmac_expected, 16);
  99. uint8_t hmac_expected2[] = { 0x75, 0x0c, 0x78, 0x3e, 0x6a, 0xb0,
  100. 0xb5, 0x03, 0xea, 0xa8, 0x6e, 0x31,
  101. 0x0a, 0x5d, 0xb7, 0x38 };
  102. doHMACTest("what do ya want for nothing?",
  103. "test.example:SmVmZQ==:hmac-md5.sig-alg.reg.int",
  104. hmac_expected2, 16);
  105. std::string data3;
  106. for (int i = 0; i < 50; ++i) {
  107. data3.push_back(0xdd);
  108. }
  109. uint8_t hmac_expected3[] = { 0x56, 0xbe, 0x34, 0x52, 0x1d, 0x14,
  110. 0x4c, 0x88, 0xdb, 0xb8, 0xc7, 0x33,
  111. 0xf0, 0xe8, 0xb3, 0xf6};
  112. doHMACTest(data3,
  113. "test.example:qqqqqqqqqqqqqqqqqqqqqg==:hmac-md5.sig-alg.reg.int",
  114. hmac_expected3, 16);
  115. std::string data4;
  116. for (int i = 0; i < 50; ++i) {
  117. data4.push_back(0xcd);
  118. }
  119. uint8_t hmac_expected4[] = { 0x69, 0x7e, 0xaf, 0x0a, 0xca, 0x3a,
  120. 0x3a, 0xea, 0x3a, 0x75, 0x16, 0x47,
  121. 0x46, 0xff, 0xaa, 0x79 };
  122. doHMACTest(data4,
  123. "test.example:AQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGQ==:hmac-md5.sig-alg.reg.int",
  124. hmac_expected4, 16);
  125. uint8_t hmac_expected5[] = { 0x56, 0x46, 0x1e, 0xf2, 0x34, 0x2e,
  126. 0xdc, 0x00, 0xf9, 0xba, 0xb9, 0x95,
  127. 0x69, 0x0e, 0xfd, 0x4c };
  128. doHMACTest("Test With Truncation",
  129. "test.example:DAwMDAwMDAwMDAwMDAwMDA==:hmac-md5.sig-alg.reg.int",
  130. hmac_expected5, 16);
  131. uint8_t hmac_expected6[] = { 0x6b, 0x1a, 0xb7, 0xfe, 0x4b, 0xd7,
  132. 0xbf, 0x8f, 0x0b, 0x62, 0xe6, 0xce,
  133. 0x61, 0xb9, 0xd0, 0xcd };
  134. doHMACTest("Test Using Larger Than Block-Size Key - Hash Key First",
  135. "test.example:qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq"
  136. "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq"
  137. "qqqqqqqqqo=:hmac-md5.sig-alg.reg.int",
  138. hmac_expected6, 16);
  139. uint8_t hmac_expected7[] = { 0x6f, 0x63, 0x0f, 0xad, 0x67, 0xcd,
  140. 0xa0, 0xee, 0x1f, 0xb1, 0xf5, 0x62,
  141. 0xdb, 0x3a, 0xa5, 0x3e };
  142. doHMACTest("Test Using Larger Than Block-Size Key and Larger Than "
  143. "One Block-Size Data",
  144. "test.example:qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq"
  145. "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq"
  146. "qqqqqqqqqo=:hmac-md5.sig-alg.reg.int",
  147. hmac_expected7, 16);
  148. }
  149. //
  150. // Test values taken from RFC 2202
  151. //
  152. TEST(CryptoTest, HMAC_SHA1_RFC2202_SIGN) {
  153. uint8_t hmac_expected[] = { 0xb6, 0x17, 0x31, 0x86, 0x55, 0x05,
  154. 0x72, 0x64, 0xe2, 0x8b, 0xc0, 0xb6,
  155. 0xfb, 0x37, 0x8c, 0x8e, 0xf1, 0x46,
  156. 0xbe, 0x00 };
  157. doHMACTest("Hi There",
  158. "test.example:CwsLCwsLCwsLCwsLCwsLCwsLCws=:hmac-sha1",
  159. hmac_expected, 20);
  160. uint8_t hmac_expected2[] = { 0xef, 0xfc, 0xdf, 0x6a, 0xe5, 0xeb,
  161. 0x2f, 0xa2, 0xd2, 0x74, 0x16, 0xd5,
  162. 0xf1, 0x84, 0xdf, 0x9c, 0x25, 0x9a,
  163. 0x7c, 0x79 };
  164. doHMACTest("what do ya want for nothing?",
  165. "test.example:SmVmZQ==:hmac-sha1",
  166. hmac_expected2, 20);
  167. std::string data3;
  168. for (int i = 0; i < 50; ++i) {
  169. data3.push_back(0xdd);
  170. }
  171. uint8_t hmac_expected3[] = { 0x12, 0x5d, 0x73, 0x42, 0xb9, 0xac,
  172. 0x11, 0xcd, 0x91, 0xa3, 0x9a, 0xf4,
  173. 0x8a, 0xa1, 0x7b, 0x4f, 0x63, 0xf1,
  174. 0x75, 0xd3 };
  175. doHMACTest(data3,
  176. "test.example:qqqqqqqqqqqqqqqqqqqqqqqqqqo=:hmac-sha1",
  177. hmac_expected3, 20);
  178. std::string data4;
  179. for (int i = 0; i < 50; ++i) {
  180. data4.push_back(0xcd);
  181. }
  182. uint8_t hmac_expected4[] = { 0x4c, 0x90, 0x07, 0xf4, 0x02, 0x62,
  183. 0x50, 0xc6, 0xbc, 0x84, 0x14, 0xf9,
  184. 0xbf, 0x50, 0xc8, 0x6c, 0x2d, 0x72,
  185. 0x35, 0xda };
  186. doHMACTest(data4,
  187. "test.example:AQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGQ==:hmac-sha1",
  188. hmac_expected4, 20);
  189. uint8_t hmac_expected5[] = { 0x4c, 0x1a, 0x03, 0x42, 0x4b, 0x55,
  190. 0xe0, 0x7f, 0xe7, 0xf2, 0x7b, 0xe1,
  191. 0xd5, 0x8b, 0xb9, 0x32, 0x4a, 0x9a,
  192. 0x5a, 0x04 };
  193. doHMACTest("Test With Truncation",
  194. "test.example:DAwMDAwMDAwMDAwMDAwMDAwMDAw=:hmac-sha1",
  195. hmac_expected5, 20);
  196. uint8_t hmac_expected6[] = { 0xaa, 0x4a, 0xe5, 0xe1, 0x52, 0x72,
  197. 0xd0, 0x0e, 0x95, 0x70, 0x56, 0x37,
  198. 0xce, 0x8a, 0x3b, 0x55, 0xed, 0x40,
  199. 0x21, 0x12 };
  200. doHMACTest("Test Using Larger Than Block-Size Key - Hash Key First",
  201. "test.example:qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq"
  202. "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq"
  203. "qqqqqqqqqo=:hmac-sha1",
  204. hmac_expected6, 20);
  205. uint8_t hmac_expected7[] = { 0xe8, 0xe9, 0x9d, 0x0f, 0x45, 0x23,
  206. 0x7d, 0x78, 0x6d, 0x6b, 0xba, 0xa7,
  207. 0x96, 0x5c, 0x78, 0x08, 0xbb, 0xff,
  208. 0x1a, 0x91 };
  209. doHMACTest("Test Using Larger Than Block-Size Key and Larger Than "
  210. "One Block-Size Data",
  211. "test.example:qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq"
  212. "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq"
  213. "qqqqqqqqqo=:hmac-sha1",
  214. hmac_expected7, 20);
  215. }
  216. //
  217. // Test values taken from RFC 4231
  218. //
  219. TEST(CryptoTest, HMAC_SHA256_RFC2202_SIGN) {
  220. uint8_t hmac_expected[] = { 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb,
  221. 0x38, 0x53, 0x5c, 0xa8, 0xaf, 0xce,
  222. 0xaf, 0x0b, 0xf1, 0x2b, 0x88, 0x1d,
  223. 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  224. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32,
  225. 0xcf, 0xf7 };
  226. doHMACTest("Hi There",
  227. "test.example:CwsLCwsLCwsLCwsLCwsLCwsLCws=:hmac-sha256",
  228. hmac_expected, 32);
  229. uint8_t hmac_expected2[] = { 0x5b, 0xdc, 0xc1, 0x46, 0xbf, 0x60,
  230. 0x75, 0x4e, 0x6a, 0x04, 0x24, 0x26,
  231. 0x08, 0x95, 0x75, 0xc7, 0x5a, 0x00,
  232. 0x3f, 0x08, 0x9d, 0x27, 0x39, 0x83,
  233. 0x9d, 0xec, 0x58, 0xb9, 0x64, 0xec,
  234. 0x38, 0x43 };
  235. doHMACTest("what do ya want for nothing?",
  236. "test.example:SmVmZQ==:hmac-sha256",
  237. hmac_expected2, 32);
  238. std::string data3;
  239. for (int i = 0; i < 50; ++i) {
  240. data3.push_back(0xdd);
  241. }
  242. uint8_t hmac_expected3[] = { 0x77, 0x3e, 0xa9, 0x1e, 0x36, 0x80,
  243. 0x0e, 0x46, 0x85, 0x4d, 0xb8, 0xeb,
  244. 0xd0, 0x91, 0x81, 0xa7, 0x29, 0x59,
  245. 0x09, 0x8b, 0x3e, 0xf8, 0xc1, 0x22,
  246. 0xd9, 0x63, 0x55, 0x14, 0xce, 0xd5,
  247. 0x65, 0xfe };
  248. doHMACTest(data3,
  249. "test.example:qqqqqqqqqqqqqqqqqqqqqqqqqqo=:hmac-sha256",
  250. hmac_expected3, 32);
  251. std::string data4;
  252. for (int i = 0; i < 50; ++i) {
  253. data4.push_back(0xcd);
  254. }
  255. uint8_t hmac_expected4[] = { 0x82, 0x55, 0x8a, 0x38, 0x9a, 0x44,
  256. 0x3c, 0x0e, 0xa4, 0xcc, 0x81, 0x98,
  257. 0x99, 0xf2, 0x08, 0x3a, 0x85, 0xf0,
  258. 0xfa, 0xa3, 0xe5, 0x78, 0xf8, 0x07,
  259. 0x7a, 0x2e, 0x3f, 0xf4, 0x67, 0x29,
  260. 0x66, 0x5b };
  261. doHMACTest(data4,
  262. "test.example:AQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGQ==:hmac-sha256",
  263. hmac_expected4, 32);
  264. uint8_t hmac_expected6[] = { 0x60, 0xe4, 0x31, 0x59, 0x1e, 0xe0,
  265. 0xb6, 0x7f, 0x0d, 0x8a, 0x26, 0xaa,
  266. 0xcb, 0xf5, 0xb7, 0x7f, 0x8e, 0x0b,
  267. 0xc6, 0x21, 0x37, 0x28, 0xc5, 0x14,
  268. 0x05, 0x46, 0x04, 0x0f, 0x0e, 0xe3,
  269. 0x7f, 0x54 };
  270. doHMACTest("Test Using Larger Than Block-Size Key - Hash Key First",
  271. "test.example:qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq"
  272. "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq"
  273. "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq"
  274. "qqqqqqqqqqqqqqqqqqqqqqo=:hmac-sha256",
  275. hmac_expected6, 32);
  276. uint8_t hmac_expected7[] = { 0x9b, 0x09, 0xff, 0xa7, 0x1b, 0x94,
  277. 0x2f, 0xcb, 0x27, 0x63, 0x5f, 0xbc,
  278. 0xd5, 0xb0, 0xe9, 0x44, 0xbf, 0xdc,
  279. 0x63, 0x64, 0x4f, 0x07, 0x13, 0x93,
  280. 0x8a, 0x7f, 0x51, 0x53, 0x5c, 0x3a,
  281. 0x35, 0xe2 };
  282. doHMACTest("This is a test using a larger than block-size key and a"
  283. " larger than block-size data. The key needs to be hashe"
  284. "d before being used by the HMAC algorithm.",
  285. "test.example:qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq"
  286. "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq"
  287. "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq"
  288. "qqqqqqqqqqqqqqqqqqqqqqo=:hmac-sha256",
  289. hmac_expected7, 32);
  290. }
  291. TEST(CryptoTest, BadKey) {
  292. TSIGKey bad_key = TSIGKey(Name("test.example."), Name("hmac-sha1."),
  293. NULL, 0);
  294. OutputBuffer data_buf(0);
  295. OutputBuffer hmac_sig(0);
  296. EXPECT_THROW(new HMAC(bad_key), BadKey);
  297. EXPECT_THROW(signHMAC(data_buf.getData(), data_buf.getLength(),
  298. bad_key, hmac_sig), BadKey);
  299. EXPECT_THROW(verifyHMAC(data_buf.getData(), data_buf.getLength(),
  300. bad_key, hmac_sig.getData(),
  301. hmac_sig.getLength()), BadKey);
  302. }