EllipticCurveKeyPair.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 "Constants.hpp"
  31. #ifdef __WINDOWS__
  32. #include <WinSock2.h>
  33. #include <Windows.h>
  34. #endif
  35. #include <openssl/bn.h>
  36. #include <openssl/obj_mac.h>
  37. #include <openssl/rand.h>
  38. #include <openssl/ec.h>
  39. #include <openssl/ecdh.h>
  40. #include <openssl/ecdsa.h>
  41. #include <openssl/sha.h>
  42. #include "EllipticCurveKey.hpp"
  43. #include "EllipticCurveKeyPair.hpp"
  44. namespace ZeroTier {
  45. class _EC_Group
  46. {
  47. public:
  48. _EC_Group()
  49. {
  50. g = EC_GROUP_new_by_curve_name(ZT_EC_OPENSSL_CURVE);
  51. }
  52. ~_EC_Group() {}
  53. EC_GROUP *g;
  54. };
  55. static _EC_Group ZT_EC_GROUP;
  56. /**
  57. * Key derivation function
  58. *
  59. * TODO:
  60. * If/when we document the protocol, this will have to be documented as
  61. * well. It's a fairly standard KDF that uses SHA-256 to transform the
  62. * raw EC key. It's generally considered good crypto practice to do this
  63. * to eliminate the possibility of leaking information from EC exchange to
  64. * downstream algorithms.
  65. *
  66. * In our code it is used to produce a two 32-bit keys. One key is used
  67. * for Salsa20 and the other for HMAC-SHA-256. They are generated together
  68. * as a single 64-bit key.
  69. */
  70. static void *_zt_EC_KDF(const void *in,size_t inlen,void *out,size_t *outlen)
  71. {
  72. SHA256_CTX sha;
  73. unsigned char dig[SHA256_DIGEST_LENGTH];
  74. SHA256_Init(&sha);
  75. SHA256_Update(&sha,(const unsigned char *)in,inlen);
  76. SHA256_Final(dig,&sha);
  77. for(unsigned long i=0,k=0;i<(unsigned long)*outlen;) {
  78. if (k == SHA256_DIGEST_LENGTH) {
  79. k = 0;
  80. SHA256_Init(&sha);
  81. SHA256_Update(&sha,(const unsigned char *)in,inlen);
  82. SHA256_Update(&sha,dig,SHA256_DIGEST_LENGTH);
  83. SHA256_Final(dig,&sha);
  84. }
  85. ((unsigned char *)out)[i++] = dig[k++];
  86. }
  87. return out;
  88. }
  89. EllipticCurveKeyPair::EllipticCurveKeyPair() :
  90. _pub(),
  91. _priv(),
  92. _internal_key((void *)0)
  93. {
  94. }
  95. EllipticCurveKeyPair::EllipticCurveKeyPair(const EllipticCurveKeyPair &pair) :
  96. _pub(pair._pub),
  97. _priv(pair._priv),
  98. _internal_key((void *)0)
  99. {
  100. }
  101. EllipticCurveKeyPair::EllipticCurveKeyPair(const EllipticCurveKey &pubk,const EllipticCurveKey &privk) :
  102. _pub(pubk),
  103. _priv(privk),
  104. _internal_key((void *)0)
  105. {
  106. }
  107. EllipticCurveKeyPair::~EllipticCurveKeyPair()
  108. {
  109. if (_internal_key)
  110. EC_KEY_free((EC_KEY *)_internal_key);
  111. }
  112. const EllipticCurveKeyPair &EllipticCurveKeyPair::operator=(const EllipticCurveKeyPair &pair)
  113. {
  114. if (_internal_key)
  115. EC_KEY_free((EC_KEY *)_internal_key);
  116. _pub = pair._pub;
  117. _priv = pair._priv;
  118. _internal_key = (void *)0;
  119. return *this;
  120. }
  121. bool EllipticCurveKeyPair::generate()
  122. {
  123. EC_KEY *key;
  124. int len;
  125. key = EC_KEY_new();
  126. if (!key) return false;
  127. if (!EC_KEY_set_group(key,ZT_EC_GROUP.g)) {
  128. EC_KEY_free(key);
  129. return false;
  130. }
  131. if (!EC_KEY_generate_key(key)) {
  132. EC_KEY_free(key);
  133. return false;
  134. }
  135. memset(_priv._key,0,sizeof(_priv._key));
  136. len = (int)BN_num_bytes(EC_KEY_get0_private_key(key));
  137. if ((len > ZT_EC_PRIME_BYTES)||(len < 0)) {
  138. EC_KEY_free(key);
  139. return false;
  140. }
  141. BN_bn2bin(EC_KEY_get0_private_key(key),&(_priv._key[ZT_EC_PRIME_BYTES - len]));
  142. _priv._bytes = ZT_EC_PRIME_BYTES;
  143. memset(_pub._key,0,sizeof(_pub._key));
  144. len = (int)EC_POINT_point2oct(ZT_EC_GROUP.g,EC_KEY_get0_public_key(key),POINT_CONVERSION_COMPRESSED,_pub._key,sizeof(_pub._key),0);
  145. if (len != ZT_EC_PUBLIC_KEY_BYTES) {
  146. EC_KEY_free(key);
  147. return false;
  148. }
  149. _pub._bytes = ZT_EC_PUBLIC_KEY_BYTES;
  150. if (_internal_key)
  151. EC_KEY_free((EC_KEY *)_internal_key);
  152. _internal_key = key;
  153. return true;
  154. }
  155. bool EllipticCurveKeyPair::agree(const EllipticCurveKey &theirPublicKey,unsigned char *agreedUponKey,unsigned int agreedUponKeyLength) const
  156. {
  157. if (theirPublicKey._bytes != ZT_EC_PUBLIC_KEY_BYTES)
  158. return false;
  159. if (!_internal_key) {
  160. if (!(const_cast <EllipticCurveKeyPair *> (this))->initInternalKey())
  161. return false;
  162. }
  163. EC_POINT *pub = EC_POINT_new(ZT_EC_GROUP.g);
  164. if (!pub)
  165. return false;
  166. EC_POINT_oct2point(ZT_EC_GROUP.g,pub,theirPublicKey._key,ZT_EC_PUBLIC_KEY_BYTES,0);
  167. int i = ECDH_compute_key(agreedUponKey,agreedUponKeyLength,pub,(EC_KEY *)_internal_key,&_zt_EC_KDF);
  168. EC_POINT_free(pub);
  169. return (i == (int)agreedUponKeyLength);
  170. }
  171. std::string EllipticCurveKeyPair::sign(const void *sha256) const
  172. {
  173. unsigned char buf[256];
  174. std::string sigbin;
  175. if (!_internal_key) {
  176. if (!(const_cast <EllipticCurveKeyPair *> (this))->initInternalKey())
  177. return std::string();
  178. }
  179. ECDSA_SIG *sig = ECDSA_do_sign((const unsigned char *)sha256,SHA256_DIGEST_LENGTH,(EC_KEY *)_internal_key);
  180. if (!sig)
  181. return std::string();
  182. int rlen = BN_num_bytes(sig->r);
  183. if ((rlen > 255)||(rlen <= 0)) {
  184. ECDSA_SIG_free(sig);
  185. return std::string();
  186. }
  187. sigbin.push_back((char)rlen);
  188. BN_bn2bin(sig->r,buf);
  189. sigbin.append((const char *)buf,rlen);
  190. int slen = BN_num_bytes(sig->s);
  191. if ((slen > 255)||(slen <= 0)) {
  192. ECDSA_SIG_free(sig);
  193. return std::string();
  194. }
  195. sigbin.push_back((char)slen);
  196. BN_bn2bin(sig->s,buf);
  197. sigbin.append((const char *)buf,slen);
  198. ECDSA_SIG_free(sig);
  199. return sigbin;
  200. }
  201. std::string EllipticCurveKeyPair::sign(const void *data,unsigned int len) const
  202. {
  203. SHA256_CTX sha;
  204. unsigned char dig[SHA256_DIGEST_LENGTH];
  205. SHA256_Init(&sha);
  206. SHA256_Update(&sha,(const unsigned char *)data,len);
  207. SHA256_Final(dig,&sha);
  208. return sign(dig);
  209. }
  210. bool EllipticCurveKeyPair::verify(const void *sha256,const EllipticCurveKey &pk,const void *sigbytes,unsigned int siglen)
  211. {
  212. bool result = false;
  213. ECDSA_SIG *sig = (ECDSA_SIG *)0;
  214. EC_POINT *pub = (EC_POINT *)0;
  215. EC_KEY *key = (EC_KEY *)0;
  216. int rlen,slen;
  217. if (!siglen)
  218. goto verify_sig_return;
  219. rlen = ((const unsigned char *)sigbytes)[0];
  220. if (!rlen)
  221. goto verify_sig_return;
  222. if (siglen < (unsigned int)(rlen + 2))
  223. goto verify_sig_return;
  224. slen = ((const unsigned char *)sigbytes)[rlen + 1];
  225. if (!slen)
  226. goto verify_sig_return;
  227. if (siglen < (unsigned int)(rlen + slen + 2))
  228. goto verify_sig_return;
  229. sig = ECDSA_SIG_new();
  230. if (!sig)
  231. goto verify_sig_return;
  232. BN_bin2bn((const unsigned char *)sigbytes + 1,rlen,sig->r);
  233. BN_bin2bn((const unsigned char *)sigbytes + (1 + rlen + 1),slen,sig->s);
  234. pub = EC_POINT_new(ZT_EC_GROUP.g);
  235. if (!pub)
  236. goto verify_sig_return;
  237. EC_POINT_oct2point(ZT_EC_GROUP.g,pub,pk._key,ZT_EC_PUBLIC_KEY_BYTES,0);
  238. key = EC_KEY_new();
  239. if (!key)
  240. goto verify_sig_return;
  241. if (!EC_KEY_set_group(key,ZT_EC_GROUP.g))
  242. goto verify_sig_return;
  243. EC_KEY_set_public_key(key,pub);
  244. result = (ECDSA_do_verify((const unsigned char *)sha256,SHA256_DIGEST_LENGTH,sig,key) == 1);
  245. verify_sig_return:
  246. if (key)
  247. EC_KEY_free(key);
  248. if (pub)
  249. EC_POINT_free(pub);
  250. if (sig)
  251. ECDSA_SIG_free(sig);
  252. return result;
  253. }
  254. bool EllipticCurveKeyPair::verify(const void *data,unsigned int len,const EllipticCurveKey &pk,const void *sigbytes,unsigned int siglen)
  255. {
  256. SHA256_CTX sha;
  257. unsigned char dig[SHA256_DIGEST_LENGTH];
  258. SHA256_Init(&sha);
  259. SHA256_Update(&sha,(const unsigned char *)data,len);
  260. SHA256_Final(dig,&sha);
  261. return verify(dig,pk,sigbytes,siglen);
  262. }
  263. bool EllipticCurveKeyPair::initInternalKey()
  264. {
  265. EC_KEY *key;
  266. EC_POINT *kxy;
  267. BIGNUM *pn;
  268. if (_priv._bytes != ZT_EC_PRIME_BYTES) return false;
  269. if (_pub._bytes != ZT_EC_PUBLIC_KEY_BYTES) return false;
  270. key = EC_KEY_new();
  271. if (!key) return false;
  272. if (!EC_KEY_set_group(key,ZT_EC_GROUP.g)) {
  273. EC_KEY_free(key);
  274. return false;
  275. }
  276. pn = BN_new();
  277. if (!pn) {
  278. EC_KEY_free(key);
  279. return false;
  280. }
  281. if (!BN_bin2bn(_priv._key,ZT_EC_PRIME_BYTES,pn)) {
  282. BN_free(pn);
  283. EC_KEY_free(key);
  284. return false;
  285. }
  286. if (!EC_KEY_set_private_key(key,pn)) {
  287. BN_free(pn);
  288. EC_KEY_free(key);
  289. return false;
  290. }
  291. BN_free(pn);
  292. kxy = EC_POINT_new(ZT_EC_GROUP.g);
  293. if (!kxy) {
  294. EC_KEY_free(key);
  295. return false;
  296. }
  297. EC_POINT_oct2point(ZT_EC_GROUP.g,kxy,_pub._key,ZT_EC_PUBLIC_KEY_BYTES,0);
  298. if (!EC_KEY_set_public_key(key,kxy)) {
  299. EC_POINT_free(kxy);
  300. EC_KEY_free(key);
  301. return false;
  302. }
  303. EC_POINT_free(kxy);
  304. if (_internal_key)
  305. EC_KEY_free((EC_KEY *)_internal_key);
  306. _internal_key = key;
  307. return true;
  308. }
  309. } // namespace ZeroTier