EllipticCurveKeyPair.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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_ELLIPTICCURVEKEYPAIR_HPP
  28. #define _ZT_ELLIPTICCURVEKEYPAIR_HPP
  29. #include <string>
  30. #include "EllipticCurveKey.hpp"
  31. namespace ZeroTier {
  32. /**
  33. * An elliptic curve key pair supporting generation and key agreement
  34. */
  35. class EllipticCurveKeyPair
  36. {
  37. public:
  38. EllipticCurveKeyPair();
  39. EllipticCurveKeyPair(const EllipticCurveKeyPair &pair);
  40. EllipticCurveKeyPair(const EllipticCurveKey &pubk,const EllipticCurveKey &privk);
  41. ~EllipticCurveKeyPair();
  42. const EllipticCurveKeyPair &operator=(const EllipticCurveKeyPair &pair);
  43. /**
  44. * Fill this structure with a newly generated public/private key pair
  45. *
  46. * @return True if key generation is successful
  47. */
  48. bool generate();
  49. /**
  50. * Perform elliptic curve key agreement
  51. *
  52. * @param theirPublicKey Remote side's public key
  53. * @param agreedUponKey Buffer to fill with agreed-upon symmetric key
  54. * @param agreedUponKeyLength Number of bytes to generate
  55. * @return True if key agreement is successful
  56. */
  57. bool agree(const EllipticCurveKey &theirPublicKey,unsigned char *agreedUponKey,unsigned int agreedUponKeyLength) const;
  58. /**
  59. * Sign a SHA256 hash
  60. *
  61. * @param sha256 Pointer to 256-bit / 32-byte SHA hash to sign
  62. * @return ECDSA signature (r and s in binary format, each prefixed by an 8-bit size)
  63. */
  64. std::string sign(const void *sha256) const;
  65. /**
  66. * Sign something with this pair's private key, computing its hash first
  67. *
  68. * @param data Data to hash and sign
  69. * @param len Length of data
  70. * @return Signature bytes
  71. */
  72. std::string sign(const void *data,unsigned int len) const;
  73. /**
  74. * Verify a signature
  75. *
  76. * @param sha256 Pointer to 256-bit / 32-byte SHA hash to verify
  77. * @param pk Public key to verify against
  78. * @param sigbytes Signature bytes
  79. * @param siglen Length of signature
  80. */
  81. static bool verify(const void *sha256,const EllipticCurveKey &pk,const void *sigbytes,unsigned int siglen);
  82. /**
  83. * Verify a signature
  84. *
  85. * @param data Data to verify
  86. * @param len Length of data
  87. * @param pk Public key to verify against
  88. * @param sigbytes Signature bytes
  89. * @param siglen Length of signature
  90. */
  91. static bool verify(const void *data,unsigned int len,const EllipticCurveKey &pk,const void *sigbytes,unsigned int siglen);
  92. inline bool operator==(const EllipticCurveKeyPair &kp) const
  93. throw()
  94. {
  95. return ((_pub == kp._pub)&&(_priv == kp._priv));
  96. }
  97. inline bool operator!=(const EllipticCurveKeyPair &kp) const
  98. throw()
  99. {
  100. return ((_pub != kp._pub)||(_priv != kp._priv));
  101. }
  102. inline const EllipticCurveKey &pub() const throw() { return _pub; }
  103. inline const EllipticCurveKey &priv() const throw() { return _priv; }
  104. private:
  105. bool initInternalKey();
  106. EllipticCurveKey _pub;
  107. EllipticCurveKey _priv;
  108. void *_internal_key;
  109. };
  110. } // namespace ZeroTier
  111. #endif