selftest.cpp 13 KB

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