EllipticCurveKey.hpp 3.5 KB

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