selftest.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <time.h>
  31. #include <iostream>
  32. #include <string>
  33. #include <vector>
  34. #include "node/InetAddress.hpp"
  35. #include "node/EllipticCurveKey.hpp"
  36. #include "node/EllipticCurveKeyPair.hpp"
  37. #include "node/Utils.hpp"
  38. #include "node/Identity.hpp"
  39. #include "node/Packet.hpp"
  40. #include "node/Salsa20.hpp"
  41. #include "node/HMAC.hpp"
  42. #include "node/MAC.hpp"
  43. #include "node/Peer.hpp"
  44. #include "node/Condition.hpp"
  45. #include "node/NodeConfig.hpp"
  46. #include "node/Dictionary.hpp"
  47. #include <openssl/rand.h>
  48. using namespace ZeroTier;
  49. // ---------------------------------------------------------------------------
  50. // Override libcrypto default RAND_ with Utils::getSecureRandom(), which uses
  51. // a system strong random source. This is because OpenSSL libcrypto's default
  52. // RAND_ implementation uses uninitialized memory as one of its entropy
  53. // sources, which plays havoc with all kinds of debuggers and auditing tools.
  54. static void _zeroTier_rand_cleanup() {}
  55. static void _zeroTier_rand_add(const void *buf, int num, double add_entropy) {}
  56. static int _zeroTier_rand_status() { return 1; }
  57. static void _zeroTier_rand_seed(const void *buf, int num) {}
  58. static int _zeroTier_rand_bytes(unsigned char *buf, int num)
  59. {
  60. Utils::getSecureRandom(buf,num);
  61. return 1;
  62. }
  63. static RAND_METHOD _zeroTierRandMethod = {
  64. _zeroTier_rand_seed,
  65. _zeroTier_rand_bytes,
  66. _zeroTier_rand_cleanup,
  67. _zeroTier_rand_add,
  68. _zeroTier_rand_bytes,
  69. _zeroTier_rand_status
  70. };
  71. static void _initLibCrypto()
  72. {
  73. RAND_set_rand_method(&_zeroTierRandMethod);
  74. }
  75. // ---------------------------------------------------------------------------
  76. static unsigned char fuzzbuf[1048576];
  77. static const char *hmacShaTV0Key = "key";
  78. static const char *hmacShaTV0Msg = "The quick brown fox jumps over the lazy dog";
  79. static const unsigned char hmacShaTV0Mac[32] = { 0xf7,0xbc,0x83,0xf4,0x30,0x53,0x84,0x24,0xb1,0x32,0x98,0xe6,0xaa,0x6f,0xb1,0x43,0xef,0x4d,0x59,0xa1,0x49,0x46,0x17,0x59,0x97,0x47,0x9d,0xbc,0x2d,0x1a,0x3c,0xd8 };
  80. static const unsigned char s20TV0Key[32] = { 0x0f,0x62,0xb5,0x08,0x5b,0xae,0x01,0x54,0xa7,0xfa,0x4d,0xa0,0xf3,0x46,0x99,0xec,0x3f,0x92,0xe5,0x38,0x8b,0xde,0x31,0x84,0xd7,0x2a,0x7d,0xd0,0x23,0x76,0xc9,0x1c };
  81. static const unsigned char s20TV0Iv[8] = { 0x28,0x8f,0xf6,0x5d,0xc4,0x2b,0x92,0xf9 };
  82. static const unsigned char s20TV0Ks[64] = { 0x5e,0x5e,0x71,0xf9,0x01,0x99,0x34,0x03,0x04,0xab,0xb2,0x2a,0x37,0xb6,0x62,0x5b,0xf8,0x83,0xfb,0x89,0xce,0x3b,0x21,0xf5,0x4a,0x10,0xb8,0x10,0x66,0xef,0x87,0xda,0x30,0xb7,0x76,0x99,0xaa,0x73,0x79,0xda,0x59,0x5c,0x77,0xdd,0x59,0x54,0x2d,0xa2,0x08,0xe5,0x95,0x4f,0x89,0xe4,0x0e,0xb7,0xaa,0x80,0xa8,0x4a,0x61,0x76,0x66,0x3f };
  83. static int testCrypto()
  84. {
  85. unsigned char buf1[16384];
  86. unsigned char buf2[sizeof(buf1)],buf3[sizeof(buf1)];
  87. //Utils::getSecureRandom(buf1,1024);
  88. //std::cout << "[crypto] getSecureRandom() -> " << Utils::hex(buf1,1024) << std::endl;
  89. std::cout << "[crypto] Testing ECDSA... "; std::cout.flush();
  90. for(unsigned int k=0;k<64;++k) {
  91. EllipticCurveKeyPair kp;
  92. kp.generate();
  93. for(int i=0;i<32;++i)
  94. buf1[i] = (unsigned char)rand();
  95. std::string sig = kp.sign(buf1);
  96. if (!EllipticCurveKeyPair::verify(buf1,kp.pub(),sig.data(),sig.length())) {
  97. std::cout << "FAIL" << std::endl;
  98. return -1;
  99. }
  100. }
  101. std::cout << "PASS" << std::endl;
  102. std::cout << "[crypto] Testing HMAC-SHA256... "; std::cout.flush();
  103. memset(buf1,0,sizeof(buf1));
  104. HMAC::sha256(hmacShaTV0Key,strlen(hmacShaTV0Key),hmacShaTV0Msg,strlen(hmacShaTV0Msg),buf1);
  105. if (memcmp(buf1,hmacShaTV0Mac,32)) {
  106. std::cout << "FAIL (test vector 0) (" << Utils::hex(buf1,32) << ")" << std::endl;
  107. return -1;
  108. }
  109. std::cout << "PASS" << std::endl;
  110. std::cout << "[crypto] Testing Salsa20... "; std::cout.flush();
  111. for(unsigned int i=0;i<4;++i) {
  112. for(unsigned int k=0;k<sizeof(buf1);++k)
  113. buf1[k] = (unsigned char)rand();
  114. memset(buf2,0,sizeof(buf2));
  115. memset(buf3,0,sizeof(buf3));
  116. Salsa20 s20;
  117. s20.init("12345678123456781234567812345678",256,"12345678");
  118. s20.encrypt(buf1,buf2,sizeof(buf1));
  119. s20.init("12345678123456781234567812345678",256,"12345678");
  120. s20.decrypt(buf2,buf3,sizeof(buf2));
  121. if (memcmp(buf1,buf3,sizeof(buf1))) {
  122. std::cout << "FAIL (encrypt/decrypt test)" << std::endl;
  123. return -1;
  124. }
  125. }
  126. Salsa20 s20(s20TV0Key,256,s20TV0Iv);
  127. memset(buf1,0,sizeof(buf1));
  128. memset(buf2,0,sizeof(buf2));
  129. s20.encrypt(buf1,buf2,64);
  130. if (memcmp(buf2,s20TV0Ks,64)) {
  131. std::cout << "FAIL (test vector 0)" << std::endl;
  132. return -1;
  133. }
  134. std::cout << "PASS" << std::endl;
  135. return 0;
  136. }
  137. static int testIdentity()
  138. {
  139. Identity id;
  140. Buffer<512> buf;
  141. std::cout << "[identity] Generate identity... "; std::cout.flush();
  142. uint64_t genstart = Utils::now();
  143. id.generate();
  144. uint64_t genend = Utils::now();
  145. std::cout << "(took " << (genend - genstart) << "ms): " << id.toString(true) << std::endl;
  146. std::cout << "[identity] Locally validate identity: ";
  147. if (id.locallyValidate(false)) {
  148. std::cout << "PASS" << std::endl;
  149. } else {
  150. std::cout << "FAIL" << std::endl;
  151. return -1;
  152. }
  153. {
  154. Identity id2;
  155. buf.clear();
  156. id.serialize(buf,true);
  157. id2.deserialize(buf);
  158. std::cout << "[identity] Serialize and deserialize (w/private): ";
  159. if ((id == id2)&&(id2.locallyValidate(false))) {
  160. std::cout << "PASS" << std::endl;
  161. } else {
  162. std::cout << "FAIL" << std::endl;
  163. return -1;
  164. }
  165. }
  166. {
  167. Identity id2;
  168. buf.clear();
  169. id.serialize(buf,false);
  170. id2.deserialize(buf);
  171. std::cout << "[identity] Serialize and deserialize (no private): ";
  172. if ((id == id2)&&(id2.locallyValidate(false))) {
  173. std::cout << "PASS" << std::endl;
  174. } else {
  175. std::cout << "FAIL" << std::endl;
  176. return -1;
  177. }
  178. }
  179. {
  180. Identity id2;
  181. id2.fromString(id.toString(true).c_str());
  182. std::cout << "[identity] Serialize and deserialize (ASCII w/private): ";
  183. if ((id == id2)&&(id2.locallyValidate(false))) {
  184. std::cout << "PASS" << std::endl;
  185. } else {
  186. std::cout << "FAIL" << std::endl;
  187. return -1;
  188. }
  189. }
  190. {
  191. Identity id2;
  192. id2.fromString(id.toString(false).c_str());
  193. std::cout << "[identity] Serialize and deserialize (ASCII no private): ";
  194. if ((id == id2)&&(id2.locallyValidate(false))) {
  195. std::cout << "PASS" << std::endl;
  196. } else {
  197. std::cout << "FAIL" << std::endl;
  198. return -1;
  199. }
  200. }
  201. return 0;
  202. }
  203. static int testPacket()
  204. {
  205. unsigned char salsaKey[32],hmacKey[32];
  206. Packet a,b;
  207. a.zeroAll();
  208. b.zeroAll();
  209. for(unsigned int i=0;i<32;++i) {
  210. salsaKey[i] = (unsigned char)rand();
  211. hmacKey[i] = (unsigned char)rand();
  212. }
  213. std::cout << "[packet] Testing Packet encoder/decoder... ";
  214. a.reset(Address(),Address(),Packet::VERB_HELLO);
  215. for(int i=0;i<32;++i)
  216. a.append("supercalifragilisticexpealidocious",strlen("supercalifragilisticexpealidocious"));
  217. b = a;
  218. if (a != b) {
  219. std::cout << "FAIL (assign)" << std::endl;
  220. return -1;
  221. }
  222. a.compress();
  223. unsigned int complen = a.size();
  224. a.uncompress();
  225. std::cout << "(compressed: " << complen << ", decompressed: " << a.size() << ") ";
  226. if (a != b) {
  227. std::cout << "FAIL (compresssion)" << std::endl;
  228. return -1;
  229. }
  230. a.compress();
  231. a.encrypt(salsaKey);
  232. a.decrypt(salsaKey);
  233. a.uncompress();
  234. if (a != b) {
  235. std::cout << "FAIL (encrypt-decrypt)" << std::endl;
  236. return -1;
  237. }
  238. a.hmacSet(hmacKey);
  239. if (!a.hmacVerify(hmacKey)) {
  240. std::cout << "FAIL (hmacVerify)" << std::endl;
  241. return -1;
  242. }
  243. std::cout << "PASS" << std::endl;
  244. return 0;
  245. }
  246. static int testOther()
  247. {
  248. std::cout << "[other] Testing Base64 encode/decode... "; std::cout.flush();
  249. for(unsigned int k=0;k<1000;++k) {
  250. unsigned int flen = (rand() % 8194) + 1;
  251. for(unsigned int i=0;i<flen;++i)
  252. fuzzbuf[i] = (unsigned char)(rand() & 0xff);
  253. std::string dec = Utils::base64Decode(Utils::base64Encode(fuzzbuf,flen));
  254. if ((dec.length() != flen)||(memcmp(dec.data(),fuzzbuf,dec.length()))) {
  255. std::cout << "FAILED!" << std::endl;
  256. return -1;
  257. }
  258. }
  259. std::cout << "PASS" << std::endl;
  260. std::cout << "[other] Testing hex encode/decode... "; std::cout.flush();
  261. for(unsigned int k=0;k<1000;++k) {
  262. unsigned int flen = (rand() % 8194) + 1;
  263. for(unsigned int i=0;i<flen;++i)
  264. fuzzbuf[i] = (unsigned char)(rand() & 0xff);
  265. std::string dec = Utils::unhex(Utils::hex(fuzzbuf,flen).c_str());
  266. if ((dec.length() != flen)||(memcmp(dec.data(),fuzzbuf,dec.length()))) {
  267. std::cout << "FAILED!" << std::endl;
  268. std::cout << Utils::hex(fuzzbuf,flen) << std::endl;
  269. std::cout << Utils::hex(dec.data(),dec.length()) << std::endl;
  270. return -1;
  271. }
  272. }
  273. std::cout << "PASS" << std::endl;
  274. std::cout << "[other] Testing command bus encode/decode... "; std::cout.flush();
  275. try {
  276. static char key[32] = { 0 };
  277. for(unsigned int k=0;k<1000;++k) {
  278. std::vector<std::string> original;
  279. for(unsigned int i=0,j=rand() % 256,l=(rand() % 1024)+1;i<j;++i)
  280. original.push_back(std::string(l,'x'));
  281. std::vector< Buffer<ZT_NODECONFIG_MAX_PACKET_SIZE> > packets(NodeConfig::encodeControlMessage(key,1,original));
  282. //std::cout << packets.size() << ' '; std::cout.flush();
  283. std::vector<std::string> after;
  284. for(std::vector< Buffer<ZT_NODECONFIG_MAX_PACKET_SIZE> >::iterator i(packets.begin());i!=packets.end();++i) {
  285. unsigned long convId = 9999;
  286. if (!NodeConfig::decodeControlMessagePacket(key,i->data(),i->size(),convId,after)) {
  287. std::cout << "FAIL (decode)" << std::endl;
  288. return -1;
  289. }
  290. if (convId != 1) {
  291. std::cout << "FAIL (conversation ID)" << std::endl;
  292. return -1;
  293. }
  294. }
  295. if (after != original) {
  296. std::cout << "FAIL (compare)" << std::endl;
  297. return -1;
  298. }
  299. }
  300. } catch (std::exception &exc) {
  301. std::cout << "FAIL (" << exc.what() << ")" << std::endl;
  302. return -1;
  303. }
  304. std::cout << "PASS" << std::endl;
  305. std::cout << "[other] Testing Dictionary... "; std::cout.flush();
  306. for(int k=0;k<10000;++k) {
  307. Dictionary a,b;
  308. int nk = rand() % 32;
  309. for(int q=0;q<nk;++q) {
  310. std::string k,v;
  311. int kl = (rand() % 512);
  312. int vl = (rand() % 512);
  313. for(int i=0;i<kl;++i)
  314. k.push_back((char)rand());
  315. for(int i=0;i<vl;++i)
  316. v.push_back((char)rand());
  317. a[k] = v;
  318. }
  319. std::string aser = a.toString();
  320. b.fromString(aser);
  321. if (a != b) {
  322. std::cout << "FAIL!" << std::endl;
  323. return -1;
  324. }
  325. }
  326. std::cout << "PASS" << std::endl;
  327. return 0;
  328. }
  329. int main(int argc,char **argv)
  330. {
  331. int r = 0;
  332. _initLibCrypto();
  333. srand(time(0));
  334. r |= testCrypto();
  335. r |= testPacket();
  336. r |= testOther();
  337. r |= testIdentity();
  338. if (r)
  339. std::cout << std::endl << "SOMETHING FAILED!" << std::endl;
  340. return r;
  341. }