EllipticCurveKey.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #ifndef _ZT_ELLIPTICCURVEKEY_H
  28. #define _ZT_ELLIPTICCURVEKEY_H
  29. #include <string>
  30. #include <algorithm>
  31. #include <string.h>
  32. #include "Utils.hpp"
  33. /**
  34. * Key type ID for identifying our use of NIST-P-521
  35. *
  36. * If in the future other types of keys are supported (post-quantum crypto?)
  37. * then we'll need a key type 2, etc. When keys are stored in the database
  38. * they are prefixed by this key type ID byte.
  39. */
  40. #define ZT_KEY_TYPE 1
  41. #define ZT_EC_OPENSSL_CURVE NID_secp521r1
  42. #define ZT_EC_CURVE_NAME "NIST-P-521"
  43. #define ZT_EC_PRIME_BYTES 66
  44. #define ZT_EC_PUBLIC_KEY_BYTES (ZT_EC_PRIME_BYTES + 1)
  45. #define ZT_EC_PRIVATE_KEY_BYTES ZT_EC_PRIME_BYTES
  46. #define ZT_EC_MAX_BYTES ZT_EC_PUBLIC_KEY_BYTES
  47. namespace ZeroTier {
  48. class EllipticCurveKeyPair;
  49. /**
  50. * An elliptic curve public or private key
  51. */
  52. class EllipticCurveKey
  53. {
  54. friend class EllipticCurveKeyPair;
  55. public:
  56. EllipticCurveKey()
  57. throw() :
  58. _bytes(0)
  59. {
  60. memset(_key,0,sizeof(_key));
  61. }
  62. EllipticCurveKey(const void *data,unsigned int len)
  63. throw()
  64. {
  65. if (len <= ZT_EC_MAX_BYTES) {
  66. _bytes = len;
  67. memcpy(_key,data,len);
  68. } else _bytes = 0;
  69. }
  70. EllipticCurveKey(const EllipticCurveKey &k)
  71. throw()
  72. {
  73. _bytes = k._bytes;
  74. memcpy(_key,k._key,_bytes);
  75. }
  76. inline EllipticCurveKey &operator=(const EllipticCurveKey &k)
  77. throw()
  78. {
  79. _bytes = k._bytes;
  80. memcpy(_key,k._key,_bytes);
  81. return *this;
  82. }
  83. inline void set(const void *data,unsigned int len)
  84. throw()
  85. {
  86. if (len <= ZT_EC_MAX_BYTES) {
  87. _bytes = len;
  88. memcpy(_key,data,len);
  89. } else _bytes = 0;
  90. }
  91. inline const unsigned char *data() const throw() { return _key; }
  92. inline unsigned int size() const throw() { return _bytes; }
  93. inline std::string toHex() const throw() { return Utils::hex(_key,_bytes); }
  94. inline unsigned char operator[](const unsigned int i) const throw() { return _key[i]; }
  95. inline bool operator==(const EllipticCurveKey &k) const throw() { return ((_bytes == k._bytes)&&(!memcmp(_key,k._key,_bytes))); }
  96. inline bool operator<(const EllipticCurveKey &k) const throw() { return std::lexicographical_compare(_key,&_key[_bytes],k._key,&k._key[k._bytes]); }
  97. inline bool operator!=(const EllipticCurveKey &k) const throw() { return !(*this == k); }
  98. inline bool operator>(const EllipticCurveKey &k) const throw() { return (k < *this); }
  99. inline bool operator<=(const EllipticCurveKey &k) const throw() { return !(k < *this); }
  100. inline bool operator>=(const EllipticCurveKey &k) const throw() { return !(*this < k); }
  101. private:
  102. unsigned int _bytes;
  103. unsigned char _key[ZT_EC_MAX_BYTES];
  104. };
  105. } // namespace ZeroTier
  106. #endif