selftest.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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 <stdexcept>
  32. #include <iostream>
  33. #include <string>
  34. #include <vector>
  35. #include "node/Constants.hpp"
  36. #include "node/RuntimeEnvironment.hpp"
  37. #include "node/InetAddress.hpp"
  38. #include "node/Utils.hpp"
  39. #include "node/Identity.hpp"
  40. #include "node/Packet.hpp"
  41. #include "node/Salsa20.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 "node/EthernetTap.hpp"
  48. #include "node/SHA512.hpp"
  49. #include "node/C25519.hpp"
  50. #include "node/Poly1305.hpp"
  51. #include "node/CertificateOfMembership.hpp"
  52. #ifdef __WINDOWS__
  53. #include <tchar.h>
  54. #endif
  55. using namespace ZeroTier;
  56. #include "selftest-crypto-vectors.hpp"
  57. static unsigned char fuzzbuf[1048576];
  58. static int testCrypto()
  59. {
  60. unsigned char buf1[16384];
  61. unsigned char buf2[sizeof(buf1)],buf3[sizeof(buf1)];
  62. for(int i=0;i<3;++i) {
  63. Utils::getSecureRandom(buf1,64);
  64. std::cout << "[crypto] getSecureRandom: " << Utils::hex(buf1,64) << std::endl;
  65. }
  66. std::cout << "[crypto] Testing SHA-512... "; std::cout.flush();
  67. SHA512::hash(buf1,sha512TV0Input,strlen(sha512TV0Input));
  68. if (memcmp(buf1,sha512TV0Digest,64)) {
  69. std::cout << "FAIL" << std::endl;
  70. return -1;
  71. }
  72. std::cout << "PASS" << std::endl;
  73. std::cout << "[crypto] Testing Poly1305... "; std::cout.flush();
  74. Poly1305::compute(buf1,poly1305TV0Input,sizeof(poly1305TV0Input),poly1305TV0Key);
  75. if (memcmp(buf1,poly1305TV0Tag,16)) {
  76. std::cout << "FAIL (1)" << std::endl;
  77. return -1;
  78. }
  79. Poly1305::compute(buf1,poly1305TV1Input,sizeof(poly1305TV1Input),poly1305TV1Key);
  80. if (memcmp(buf1,poly1305TV1Tag,16)) {
  81. std::cout << "FAIL (2)" << std::endl;
  82. return -1;
  83. }
  84. std::cout << "PASS" << std::endl;
  85. std::cout << "[crypto] Testing C25519 and Ed25519 against test vectors... "; std::cout.flush();
  86. for(int k=0;k<ZT_NUM_C25519_TEST_VECTORS;++k) {
  87. C25519::Pair p1,p2;
  88. memcpy(p1.pub.data,C25519_TEST_VECTORS[k].pub1,p1.pub.size());
  89. memcpy(p1.priv.data,C25519_TEST_VECTORS[k].priv1,p1.priv.size());
  90. memcpy(p2.pub.data,C25519_TEST_VECTORS[k].pub2,p2.pub.size());
  91. memcpy(p2.priv.data,C25519_TEST_VECTORS[k].priv2,p2.priv.size());
  92. C25519::agree(p1,p2.pub,buf1,64);
  93. C25519::agree(p2,p1.pub,buf2,64);
  94. if (memcmp(buf1,buf2,64)) {
  95. std::cout << "FAIL (1)" << std::endl;
  96. return -1;
  97. }
  98. if (memcmp(buf1,C25519_TEST_VECTORS[k].agreement,64)) {
  99. std::cout << "FAIL (2)" << std::endl;
  100. return -1;
  101. }
  102. C25519::Signature sig1 = C25519::sign(p1,buf1,64);
  103. if (memcmp(sig1.data,C25519_TEST_VECTORS[k].agreementSignedBy1,64)) {
  104. std::cout << "FAIL (3)" << std::endl;
  105. return -1;
  106. }
  107. C25519::Signature sig2 = C25519::sign(p2,buf1,64);
  108. if (memcmp(sig2.data,C25519_TEST_VECTORS[k].agreementSignedBy2,64)) {
  109. std::cout << "FAIL (4)" << std::endl;
  110. return -1;
  111. }
  112. }
  113. std::cout << "PASS" << std::endl;
  114. std::cout << "[crypto] Testing C25519 ECC key agreement... "; std::cout.flush();
  115. for(unsigned int i=0;i<100;++i) {
  116. memset(buf1,64,sizeof(buf1));
  117. memset(buf2,64,sizeof(buf2));
  118. memset(buf3,64,sizeof(buf3));
  119. C25519::Pair p1 = C25519::generate();
  120. C25519::Pair p2 = C25519::generate();
  121. C25519::Pair p3 = C25519::generate();
  122. C25519::agree(p1,p2.pub,buf1,64);
  123. C25519::agree(p2,p1.pub,buf2,64);
  124. C25519::agree(p3,p1.pub,buf3,64);
  125. // p1<>p2 should equal p1<>p2
  126. if (memcmp(buf1,buf2,64)) {
  127. std::cout << "FAIL (1)" << std::endl;
  128. return -1;
  129. }
  130. // p2<>p1 should not equal p3<>p1
  131. if (!memcmp(buf2,buf3,64)) {
  132. std::cout << "FAIL (2)" << std::endl;
  133. return -1;
  134. }
  135. }
  136. std::cout << "PASS" << std::endl;
  137. std::cout << "[crypto] Testing Ed25519 ECC signatures... "; std::cout.flush();
  138. C25519::Pair didntSign = C25519::generate();
  139. for(unsigned int i=0;i<10;++i) {
  140. C25519::Pair p1 = C25519::generate();
  141. for(unsigned int k=0;k<sizeof(buf1);++k)
  142. buf1[k] = (unsigned char)rand();
  143. C25519::Signature sig = C25519::sign(p1,buf1,sizeof(buf1));
  144. if (!C25519::verify(p1.pub,buf1,sizeof(buf1),sig)) {
  145. std::cout << "FAIL (1)" << std::endl;
  146. return -1;
  147. }
  148. ++buf1[17];
  149. if (C25519::verify(p1.pub,buf1,sizeof(buf1),sig)) {
  150. std::cout << "FAIL (2)" << std::endl;
  151. return -1;
  152. }
  153. --buf1[17];
  154. if (!C25519::verify(p1.pub,buf1,sizeof(buf1),sig)) {
  155. std::cout << "FAIL (3)" << std::endl;
  156. return -1;
  157. }
  158. if (C25519::verify(didntSign.pub,buf1,sizeof(buf1),sig)) {
  159. std::cout << "FAIL (2)" << std::endl;
  160. return -1;
  161. }
  162. for(unsigned int k=0;k<64;++k) {
  163. C25519::Signature sig2(sig);
  164. sig2.data[rand() % sig2.size()] ^= (unsigned char)(1 << (rand() & 7));
  165. if (C25519::verify(p1.pub,buf1,sizeof(buf1),sig2)) {
  166. std::cout << "FAIL (5)" << std::endl;
  167. return -1;
  168. }
  169. }
  170. }
  171. std::cout << "PASS" << std::endl;
  172. std::cout << "[crypto] Testing Salsa20... "; std::cout.flush();
  173. for(unsigned int i=0;i<4;++i) {
  174. for(unsigned int k=0;k<sizeof(buf1);++k)
  175. buf1[k] = (unsigned char)rand();
  176. memset(buf2,0,sizeof(buf2));
  177. memset(buf3,0,sizeof(buf3));
  178. Salsa20 s20;
  179. s20.init("12345678123456781234567812345678",256,"12345678",20);
  180. s20.encrypt(buf1,buf2,sizeof(buf1));
  181. s20.init("12345678123456781234567812345678",256,"12345678",20);
  182. s20.decrypt(buf2,buf3,sizeof(buf2));
  183. if (memcmp(buf1,buf3,sizeof(buf1))) {
  184. std::cout << "FAIL (encrypt/decrypt test)" << std::endl;
  185. return -1;
  186. }
  187. }
  188. Salsa20 s20(s20TV0Key,256,s20TV0Iv,20);
  189. memset(buf1,0,sizeof(buf1));
  190. memset(buf2,0,sizeof(buf2));
  191. s20.encrypt(buf1,buf2,64);
  192. if (memcmp(buf2,s20TV0Ks,64)) {
  193. std::cout << "FAIL (test vector 0)" << std::endl;
  194. return -1;
  195. }
  196. s20.init(s2012TV0Key,256,s2012TV0Iv,12);
  197. memset(buf1,0,sizeof(buf1));
  198. memset(buf2,0,sizeof(buf2));
  199. s20.encrypt(buf1,buf2,64);
  200. if (memcmp(buf2,s2012TV0Ks,64)) {
  201. std::cout << "FAIL (test vector 1)" << std::endl;
  202. return -1;
  203. }
  204. std::cout << "PASS" << std::endl;
  205. return 0;
  206. }
  207. static int testIdentity()
  208. {
  209. Identity id;
  210. Buffer<512> buf;
  211. std::cout << "[identity] Validate known-good identity... "; std::cout.flush();
  212. if (!id.fromString(KNOWN_GOOD_IDENTITY)) {
  213. std::cout << "FAIL (1)" << std::endl;
  214. return -1;
  215. }
  216. if (!id.locallyValidate()) {
  217. std::cout << "FAIL (2)" << std::endl;
  218. return -1;
  219. }
  220. std::cout << "PASS" << std::endl;
  221. std::cout << "[identity] Validate known-bad identity... "; std::cout.flush();
  222. if (!id.fromString(KNOWN_BAD_IDENTITY)) {
  223. std::cout << "FAIL (1)" << std::endl;
  224. return -1;
  225. }
  226. if (id.locallyValidate()) {
  227. std::cout << "FAIL (2)" << std::endl;
  228. return -1;
  229. }
  230. std::cout << "PASS (i.e. it failed)" << std::endl;
  231. for(unsigned int k=0;k<4;++k) {
  232. std::cout << "[identity] Generate identity... "; std::cout.flush();
  233. uint64_t genstart = Utils::now();
  234. id.generate();
  235. uint64_t genend = Utils::now();
  236. std::cout << "(took " << (genend - genstart) << "ms): " << id.toString(true) << std::endl;
  237. std::cout << "[identity] Locally validate identity: ";
  238. if (id.locallyValidate()) {
  239. std::cout << "PASS" << std::endl;
  240. } else {
  241. std::cout << "FAIL" << std::endl;
  242. return -1;
  243. }
  244. }
  245. {
  246. Identity id2;
  247. buf.clear();
  248. id.serialize(buf,true);
  249. id2.deserialize(buf);
  250. std::cout << "[identity] Serialize and deserialize (w/private): ";
  251. if ((id == id2)&&(id2.locallyValidate())) {
  252. std::cout << "PASS" << std::endl;
  253. } else {
  254. std::cout << "FAIL" << std::endl;
  255. return -1;
  256. }
  257. }
  258. {
  259. Identity id2;
  260. buf.clear();
  261. id.serialize(buf,false);
  262. id2.deserialize(buf);
  263. std::cout << "[identity] Serialize and deserialize (no private): ";
  264. if ((id == id2)&&(id2.locallyValidate())) {
  265. std::cout << "PASS" << std::endl;
  266. } else {
  267. std::cout << "FAIL" << std::endl;
  268. return -1;
  269. }
  270. }
  271. {
  272. Identity id2;
  273. id2.fromString(id.toString(true).c_str());
  274. std::cout << "[identity] Serialize and deserialize (ASCII w/private): ";
  275. if ((id == id2)&&(id2.locallyValidate())) {
  276. std::cout << "PASS" << std::endl;
  277. } else {
  278. std::cout << "FAIL" << std::endl;
  279. return -1;
  280. }
  281. }
  282. {
  283. Identity id2;
  284. id2.fromString(id.toString(false).c_str());
  285. std::cout << "[identity] Serialize and deserialize (ASCII no private): ";
  286. if ((id == id2)&&(id2.locallyValidate())) {
  287. std::cout << "PASS" << std::endl;
  288. } else {
  289. std::cout << "FAIL" << std::endl;
  290. return -1;
  291. }
  292. }
  293. return 0;
  294. }
  295. static int testCertificate()
  296. {
  297. Identity authority;
  298. std::cout << "[certificate] Generating identity to act as authority... "; std::cout.flush();
  299. authority.generate();
  300. std::cout << authority.address().toString() << std::endl;
  301. Identity idA,idB;
  302. std::cout << "[certificate] Generating identities A and B... "; std::cout.flush();
  303. idA.generate();
  304. idB.generate();
  305. std::cout << idA.address().toString() << ", " << idB.address().toString() << std::endl;
  306. std::cout << "[certificate] Generating certificates A and B...";
  307. CertificateOfMembership cA(10000,100,1,idA.address());
  308. CertificateOfMembership cB(10099,100,1,idB.address());
  309. std::cout << std::endl;
  310. std::cout << "[certificate] Signing certificates A and B with authority...";
  311. cA.sign(authority);
  312. cB.sign(authority);
  313. std::cout << std::endl;
  314. //std::cout << "[certificate] A: " << cA.toString() << std::endl;
  315. //std::cout << "[certificate] B: " << cB.toString() << std::endl;
  316. std::cout << "[certificate] A agrees with B and B with A... ";
  317. if (cA.agreesWith(cB))
  318. std::cout << "yes, ";
  319. else {
  320. std::cout << "FAIL" << std::endl;
  321. return -1;
  322. }
  323. if (cB.agreesWith(cA))
  324. std::cout << "yes." << std::endl;
  325. else {
  326. std::cout << "FAIL" << std::endl;
  327. return -1;
  328. }
  329. std::cout << "[certificate] Testing string serialization... ";
  330. CertificateOfMembership copyA(cA.toString());
  331. CertificateOfMembership copyB(cB.toString());
  332. if (copyA != cA) {
  333. std::cout << "FAIL" << std::endl;
  334. return -1;
  335. }
  336. if (copyB != cB) {
  337. std::cout << "FAIL" << std::endl;
  338. return -1;
  339. }
  340. std::cout << "PASS" << std::endl;
  341. std::cout << "[certificate] Generating two certificates that should not agree...";
  342. cA = CertificateOfMembership(10000,100,1,idA.address());
  343. cB = CertificateOfMembership(10101,100,1,idB.address());
  344. std::cout << std::endl;
  345. std::cout << "[certificate] A agrees with B and B with A... ";
  346. if (!cA.agreesWith(cB))
  347. std::cout << "no, ";
  348. else {
  349. std::cout << "FAIL" << std::endl;
  350. return -1;
  351. }
  352. if (!cB.agreesWith(cA))
  353. std::cout << "no." << std::endl;
  354. else {
  355. std::cout << "FAIL" << std::endl;
  356. return -1;
  357. }
  358. return 0;
  359. }
  360. static int testPacket()
  361. {
  362. unsigned char salsaKey[32],hmacKey[32];
  363. Packet a,b;
  364. a.zeroAll();
  365. b.zeroAll();
  366. for(unsigned int i=0;i<32;++i) {
  367. salsaKey[i] = (unsigned char)rand();
  368. hmacKey[i] = (unsigned char)rand();
  369. }
  370. std::cout << "[packet] Testing Packet encoder/decoder... ";
  371. a.reset(Address(),Address(),Packet::VERB_HELLO);
  372. for(int i=0;i<32;++i)
  373. a.append("supercalifragilisticexpealidocious",strlen("supercalifragilisticexpealidocious"));
  374. b = a;
  375. if (a != b) {
  376. std::cout << "FAIL (assign)" << std::endl;
  377. return -1;
  378. }
  379. a.compress();
  380. unsigned int complen = a.size();
  381. a.uncompress();
  382. std::cout << "(compressed: " << complen << ", decompressed: " << a.size() << ") ";
  383. if (a != b) {
  384. std::cout << "FAIL (compresssion)" << std::endl;
  385. return -1;
  386. }
  387. a.armor(salsaKey,true);
  388. if (!a.dearmor(salsaKey)) {
  389. std::cout << "FAIL (encrypt-decrypt/verify)" << std::endl;
  390. return -1;
  391. }
  392. std::cout << "PASS" << std::endl;
  393. return 0;
  394. }
  395. static int testOther()
  396. {
  397. std::cout << "[other] Testing hex encode/decode... "; std::cout.flush();
  398. for(unsigned int k=0;k<1000;++k) {
  399. unsigned int flen = (rand() % 8194) + 1;
  400. for(unsigned int i=0;i<flen;++i)
  401. fuzzbuf[i] = (unsigned char)(rand() & 0xff);
  402. std::string dec = Utils::unhex(Utils::hex(fuzzbuf,flen).c_str());
  403. if ((dec.length() != flen)||(memcmp(dec.data(),fuzzbuf,dec.length()))) {
  404. std::cout << "FAILED!" << std::endl;
  405. std::cout << Utils::hex(fuzzbuf,flen) << std::endl;
  406. std::cout << Utils::hex(dec.data(),dec.length()) << std::endl;
  407. return -1;
  408. }
  409. }
  410. std::cout << "PASS" << std::endl;
  411. std::cout << "[other] Testing command bus encode/decode... "; std::cout.flush();
  412. try {
  413. static char key[32] = { 0 };
  414. for(unsigned int k=0;k<100;++k) {
  415. std::vector<std::string> original;
  416. for(unsigned int i=0,j=rand() % 256,l=(rand() % 1024)+1;i<j;++i)
  417. original.push_back(std::string(l,'x'));
  418. std::vector< Buffer<ZT_NODECONFIG_MAX_PACKET_SIZE> > packets(NodeConfig::encodeControlMessage(key,1,original));
  419. //std::cout << packets.size() << ' '; std::cout.flush();
  420. std::vector<std::string> after;
  421. for(std::vector< Buffer<ZT_NODECONFIG_MAX_PACKET_SIZE> >::iterator i(packets.begin());i!=packets.end();++i) {
  422. unsigned long convId = 9999;
  423. if (!NodeConfig::decodeControlMessagePacket(key,i->data(),i->size(),convId,after)) {
  424. std::cout << "FAIL (decode)" << std::endl;
  425. return -1;
  426. }
  427. if (convId != 1) {
  428. std::cout << "FAIL (conversation ID)" << std::endl;
  429. return -1;
  430. }
  431. }
  432. if (after != original) {
  433. std::cout << "FAIL (compare)" << std::endl;
  434. return -1;
  435. }
  436. }
  437. } catch (std::exception &exc) {
  438. std::cout << "FAIL (" << exc.what() << ")" << std::endl;
  439. return -1;
  440. }
  441. std::cout << "PASS" << std::endl;
  442. std::cout << "[other] Testing Dictionary... "; std::cout.flush();
  443. for(int k=0;k<1000;++k) {
  444. Dictionary a,b;
  445. int nk = rand() % 32;
  446. for(int q=0;q<nk;++q) {
  447. std::string k,v;
  448. int kl = (rand() % 512);
  449. int vl = (rand() % 512);
  450. for(int i=0;i<kl;++i)
  451. k.push_back((char)rand());
  452. for(int i=0;i<vl;++i)
  453. v.push_back((char)rand());
  454. a[k] = v;
  455. }
  456. std::string aser = a.toString();
  457. b.fromString(aser);
  458. if (a != b) {
  459. std::cout << "FAIL!" << std::endl;
  460. return -1;
  461. }
  462. }
  463. std::cout << "PASS" << std::endl;
  464. return 0;
  465. }
  466. #ifdef __WINDOWS__
  467. int _tmain(int argc, _TCHAR* argv[])
  468. #else
  469. int main(int argc,char **argv)
  470. #endif
  471. {
  472. int r = 0;
  473. // Code to generate the C25519 test vectors -- did this once and then
  474. // put these up top so that we can ensure that every platform produces
  475. // the same result.
  476. /*
  477. for(int k=0;k<32;++k) {
  478. C25519::Pair p1 = C25519::generate();
  479. C25519::Pair p2 = C25519::generate();
  480. unsigned char agg[64];
  481. C25519::agree(p1,p2.pub,agg,64);
  482. C25519::Signature sig1 = C25519::sign(p1,agg,64);
  483. C25519::Signature sig2 = C25519::sign(p2,agg,64);
  484. printf("{{");
  485. for(int i=0;i<64;++i)
  486. printf("%s0x%.2x",((i > 0) ? "," : ""),(unsigned int)p1.pub.data[i]);
  487. printf("},{");
  488. for(int i=0;i<64;++i)
  489. printf("%s0x%.2x",((i > 0) ? "," : ""),(unsigned int)p1.priv.data[i]);
  490. printf("},{");
  491. for(int i=0;i<64;++i)
  492. printf("%s0x%.2x",((i > 0) ? "," : ""),(unsigned int)p2.pub.data[i]);
  493. printf("},{");
  494. for(int i=0;i<64;++i)
  495. printf("%s0x%.2x",((i > 0) ? "," : ""),(unsigned int)p2.priv.data[i]);
  496. printf("},{");
  497. for(int i=0;i<64;++i)
  498. printf("%s0x%.2x",((i > 0) ? "," : ""),(unsigned int)agg[i]);
  499. printf("},{");
  500. for(int i=0;i<96;++i)
  501. printf("%s0x%.2x",((i > 0) ? "," : ""),(unsigned int)sig1.data[i]);
  502. printf("},{");
  503. for(int i=0;i<96;++i)
  504. printf("%s0x%.2x",((i > 0) ? "," : ""),(unsigned int)sig2.data[i]);
  505. printf("}}\n");
  506. }
  507. exit(0);
  508. */
  509. std::cout << "[info] sizeof(void *) == " << sizeof(void *) << std::endl;
  510. srand((unsigned int)time(0));
  511. r |= testCrypto();
  512. r |= testPacket();
  513. r |= testOther();
  514. r |= testIdentity();
  515. r |= testCertificate();
  516. if (r)
  517. std::cout << std::endl << "SOMETHING FAILED!" << std::endl;
  518. return r;
  519. }