selftest.cpp 14 KB

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