EllipticCurveKeyPair.hpp 3.8 KB

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