selftest.cpp 16 KB

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