selftest.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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/Http.hpp"
  45. #include "node/Condition.hpp"
  46. using namespace ZeroTier;
  47. static unsigned char fuzzbuf[1048576];
  48. static const char *hmacShaTV0Key = "key";
  49. static const char *hmacShaTV0Msg = "The quick brown fox jumps over the lazy dog";
  50. 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 };
  51. 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 };
  52. static const unsigned char s20TV0Iv[8] = { 0x28,0x8f,0xf6,0x5d,0xc4,0x2b,0x92,0xf9 };
  53. 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 };
  54. static int testCrypto()
  55. {
  56. unsigned char buf1[16384];
  57. unsigned char buf2[sizeof(buf1)],buf3[sizeof(buf1)];
  58. std::cout << "[crypto] Testing ECDSA... "; std::cout.flush();
  59. for(unsigned int k=0;k<64;++k) {
  60. EllipticCurveKeyPair kp;
  61. kp.generate();
  62. for(int i=0;i<32;++i)
  63. buf1[i] = (unsigned char)rand();
  64. std::string sig = kp.sign(buf1);
  65. if (!EllipticCurveKeyPair::verify(buf1,kp.pub(),sig.data(),sig.length())) {
  66. std::cout << "FAIL" << std::endl;
  67. return -1;
  68. }
  69. }
  70. std::cout << "PASS" << std::endl;
  71. std::cout << "[crypto] Testing HMAC-SHA256... "; std::cout.flush();
  72. memset(buf1,0,sizeof(buf1));
  73. HMAC::sha256(hmacShaTV0Key,strlen(hmacShaTV0Key),hmacShaTV0Msg,strlen(hmacShaTV0Msg),buf1);
  74. if (memcmp(buf1,hmacShaTV0Mac,32)) {
  75. std::cout << "FAIL (test vector 0) (" << Utils::hex(buf1,32) << ")" << std::endl;
  76. return -1;
  77. }
  78. std::cout << "PASS" << std::endl;
  79. std::cout << "[crypto] Testing Salsa20... "; std::cout.flush();
  80. for(unsigned int i=0;i<4;++i) {
  81. for(unsigned int k=0;k<sizeof(buf1);++k)
  82. buf1[k] = (unsigned char)rand();
  83. memset(buf2,0,sizeof(buf2));
  84. memset(buf3,0,sizeof(buf3));
  85. Salsa20 s20;
  86. s20.init("12345678123456781234567812345678",256,"12345678");
  87. s20.encrypt(buf1,buf2,sizeof(buf1));
  88. s20.init("12345678123456781234567812345678",256,"12345678");
  89. s20.decrypt(buf2,buf3,sizeof(buf2));
  90. if (memcmp(buf1,buf3,sizeof(buf1))) {
  91. std::cout << "FAIL (encrypt/decrypt test)" << std::endl;
  92. return -1;
  93. }
  94. }
  95. Salsa20 s20(s20TV0Key,256,s20TV0Iv);
  96. memset(buf1,0,sizeof(buf1));
  97. memset(buf2,0,sizeof(buf2));
  98. s20.encrypt(buf1,buf2,64);
  99. if (memcmp(buf2,s20TV0Ks,64)) {
  100. std::cout << "FAIL (test vector 0)" << std::endl;
  101. return -1;
  102. }
  103. std::cout << "PASS" << std::endl;
  104. return 0;
  105. }
  106. static int testIdentity()
  107. {
  108. Identity id;
  109. Buffer<512> buf;
  110. std::cout << "[identity] Generate identity... "; std::cout.flush();
  111. uint64_t genstart = Utils::now();
  112. id.generate();
  113. uint64_t genend = Utils::now();
  114. std::cout << "(took " << (genend - genstart) << "ms): " << id.toString(true) << std::endl;
  115. std::cout << "[identity] Locally validate identity: ";
  116. if (id.locallyValidate(false)) {
  117. std::cout << "PASS" << std::endl;
  118. } else {
  119. std::cout << "FAIL" << std::endl;
  120. return -1;
  121. }
  122. {
  123. Identity id2;
  124. buf.clear();
  125. id.serialize(buf,true);
  126. id2.deserialize(buf);
  127. std::cout << "[identity] Serialize and deserialize (w/private): ";
  128. if ((id == id2)&&(id2.locallyValidate(false))) {
  129. std::cout << "PASS" << std::endl;
  130. } else {
  131. std::cout << "FAIL" << std::endl;
  132. return -1;
  133. }
  134. }
  135. {
  136. Identity id2;
  137. buf.clear();
  138. id.serialize(buf,false);
  139. id2.deserialize(buf);
  140. std::cout << "[identity] Serialize and deserialize (no private): ";
  141. if ((id == id2)&&(id2.locallyValidate(false))) {
  142. std::cout << "PASS" << std::endl;
  143. } else {
  144. std::cout << "FAIL" << std::endl;
  145. return -1;
  146. }
  147. }
  148. {
  149. Identity id2;
  150. id2.fromString(id.toString(true).c_str());
  151. std::cout << "[identity] Serialize and deserialize (ASCII w/private): ";
  152. if ((id == id2)&&(id2.locallyValidate(false))) {
  153. std::cout << "PASS" << std::endl;
  154. } else {
  155. std::cout << "FAIL" << std::endl;
  156. return -1;
  157. }
  158. }
  159. {
  160. Identity id2;
  161. id2.fromString(id.toString(false).c_str());
  162. std::cout << "[identity] Serialize and deserialize (ASCII no private): ";
  163. if ((id == id2)&&(id2.locallyValidate(false))) {
  164. std::cout << "PASS" << std::endl;
  165. } else {
  166. std::cout << "FAIL" << std::endl;
  167. return -1;
  168. }
  169. }
  170. return 0;
  171. }
  172. static int testPacket()
  173. {
  174. unsigned char salsaKey[32],hmacKey[32];
  175. Packet a,b;
  176. for(unsigned int i=0;i<32;++i) {
  177. salsaKey[i] = (unsigned char)rand();
  178. hmacKey[i] = (unsigned char)rand();
  179. }
  180. std::cout << "[packet] Testing Packet encoder/decoder... ";
  181. a = Packet();
  182. a.setVerb(Packet::VERB_HELLO);
  183. for(int i=0;i<32;++i)
  184. a.append("supercalifragilisticexpealidocious",strlen("supercalifragilisticexpealidocious"));
  185. b = a;
  186. a.compress();
  187. unsigned int complen = a.size();
  188. a.uncompress();
  189. std::cout << "(compressed: " << complen << ", decompressed: " << a.size() << ") ";
  190. if (a != b) {
  191. std::cout << "FAIL (compresssion)" << std::endl;
  192. return -1;
  193. }
  194. a.compress();
  195. a.encrypt(salsaKey);
  196. a.decrypt(salsaKey);
  197. a.uncompress();
  198. if (a != b) {
  199. std::cout << "FAIL (encrypt-decrypt)" << std::endl;
  200. return -1;
  201. }
  202. a.hmacSet(hmacKey);
  203. if (!a.hmacVerify(hmacKey)) {
  204. std::cout << "FAIL (hmacVerify)" << std::endl;
  205. return -1;
  206. }
  207. std::cout << "PASS" << std::endl;
  208. return 0;
  209. }
  210. static int testOther()
  211. {
  212. std::cout << "[other] Testing Base64 encode/decode... "; std::cout.flush();
  213. for(unsigned int k=0;k<1000;++k) {
  214. unsigned int flen = (rand() % 8194) + 1;
  215. for(unsigned int i=0;i<flen;++i)
  216. fuzzbuf[i] = (unsigned char)(rand() & 0xff);
  217. std::string dec = Utils::base64Decode(Utils::base64Encode(fuzzbuf,flen));
  218. if ((dec.length() != flen)||(memcmp(dec.data(),fuzzbuf,dec.length()))) {
  219. std::cout << "FAILED!" << std::endl;
  220. return -1;
  221. }
  222. }
  223. std::cout << "PASS" << std::endl;
  224. std::cout << "[other] Testing hex encode/decode... "; std::cout.flush();
  225. for(unsigned int k=0;k<1000;++k) {
  226. unsigned int flen = (rand() % 8194) + 1;
  227. for(unsigned int i=0;i<flen;++i)
  228. fuzzbuf[i] = (unsigned char)(rand() & 0xff);
  229. std::string dec = Utils::unhex(Utils::hex(fuzzbuf,flen).c_str());
  230. if ((dec.length() != flen)||(memcmp(dec.data(),fuzzbuf,dec.length()))) {
  231. std::cout << "FAILED!" << std::endl;
  232. std::cout << Utils::hex(fuzzbuf,flen) << std::endl;
  233. std::cout << Utils::hex(dec.data(),dec.length()) << std::endl;
  234. return -1;
  235. }
  236. }
  237. std::cout << "PASS" << std::endl;
  238. return 0;
  239. }
  240. static Condition testHttpDoneCondition;
  241. static bool testHttpHandler(Http::Request *req,void *arg,const std::string &url,int code,const std::map<std::string,std::string> &headers,const std::string &body)
  242. {
  243. if (code)
  244. std::cout << "[net] " << url << " " << code << " bytes: " << body.length() << std::endl;
  245. else std::cout << "[net] " << url << " FAILED: " << body << std::endl;
  246. testHttpDoneCondition.signal();
  247. return false;
  248. }
  249. static int testNet()
  250. {
  251. std::cout << "[net] GET http://www.uc.edu/" << std::endl;
  252. new Http::Request(Http::HTTP_METHOD_GET,"http://www.uc.edu/",Http::EMPTY_HEADERS,std::string(),&testHttpHandler,(void *)0);
  253. testHttpDoneCondition.wait();
  254. std::cout << "[net] GET http://zerotier.com/" << std::endl;
  255. new Http::Request(Http::HTTP_METHOD_GET,"http://zerotier.com/",Http::EMPTY_HEADERS,std::string(),&testHttpHandler,(void *)0);
  256. testHttpDoneCondition.wait();
  257. std::cout << "[net] GET http://www.google.com/" << std::endl;
  258. new Http::Request(Http::HTTP_METHOD_GET,"http://www.google.com/",Http::EMPTY_HEADERS,std::string(),&testHttpHandler,(void *)0);
  259. testHttpDoneCondition.wait();
  260. return 0;
  261. }
  262. int main(int argc,char **argv)
  263. {
  264. int r = 0;
  265. srand(time(0));
  266. r |= testNet();
  267. r |= testCrypto();
  268. r |= testPacket();
  269. r |= testOther();
  270. r |= testIdentity();
  271. if (r)
  272. std::cout << std::endl << "SOMETHING FAILED!" << std::endl;
  273. return r;
  274. }