ECC384.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
  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. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. // This is glue code to ease the use of the NIST P-384 elliptic curve.
  27. // Note that most of the code inside ECC384.cpp is third party code and
  28. // is under the BSD 2-clause license rather than ZeroTier's license.
  29. #ifndef ZT_ECC384_HPP
  30. #define ZT_ECC384_HPP
  31. #include "Constants.hpp"
  32. /**
  33. * Size of a (point compressed) P-384 public key
  34. */
  35. #define ZT_ECC384_PUBLIC_KEY_SIZE 49
  36. /**
  37. * Size of a P-384 private key
  38. */
  39. #define ZT_ECC384_PRIVATE_KEY_SIZE 48
  40. /**
  41. * Size of the hash that should be signed using P-384
  42. */
  43. #define ZT_ECC384_SIGNATURE_HASH_SIZE 48
  44. /**
  45. * Size of a P-384 signature
  46. */
  47. #define ZT_ECC384_SIGNATURE_SIZE 96
  48. /**
  49. * Size of raw shared secret generated by ECDH key agreement
  50. */
  51. #define ZT_ECC384_SHARED_SECRET_SIZE 48
  52. namespace ZeroTier {
  53. /**
  54. * Generate a NIST P-384 key pair
  55. *
  56. * @param pub Buffer to receive point compressed public key
  57. * @param priv Buffer to receiver private key
  58. */
  59. void ECC384GenerateKey(uint8_t pub[ZT_ECC384_PUBLIC_KEY_SIZE],uint8_t priv[ZT_ECC384_PRIVATE_KEY_SIZE]);
  60. /**
  61. * Sign a hash with a NIST P-384 private key
  62. *
  63. * The hash must be 48 bytes in size and is typically the first 48 bytes
  64. * of a SHA512 hash or something similar. Extra bytes of course are ignored.
  65. *
  66. * @param priv Private key
  67. * @param hash 48-byte hash
  68. * @param sig Buffer to receive signature
  69. */
  70. void ECC384ECDSASign(const uint8_t priv[ZT_ECC384_PRIVATE_KEY_SIZE],const uint8_t hash[ZT_ECC384_SIGNATURE_HASH_SIZE],uint8_t sig[ZT_ECC384_SIGNATURE_SIZE]);
  71. /**
  72. * Verify a signature
  73. *
  74. * @param pub Public key
  75. * @param hash 48-byte hash (usually first 48 bytes of SHA512(msg))
  76. * @param sig Signature to check
  77. * @return True if signature is valid
  78. */
  79. bool ECC384ECDSAVerify(const uint8_t pub[ZT_ECC384_PUBLIC_KEY_SIZE],const uint8_t hash[ZT_ECC384_SIGNATURE_HASH_SIZE],const uint8_t sig[ZT_ECC384_SIGNATURE_SIZE]);
  80. /**
  81. * Perform ECDH key agreement
  82. *
  83. * The secret generated here is the raw 48-byte result of ECDH.
  84. * It's typically hashed prior to use.
  85. *
  86. * @param theirPub Remote public key
  87. * @param ourPriv Local private key
  88. * @param secret Buffer to receive 48-byte secret
  89. */
  90. bool ECC384ECDH(const uint8_t theirPub[ZT_ECC384_PUBLIC_KEY_SIZE],const uint8_t ourPriv[ZT_ECC384_PRIVATE_KEY_SIZE],uint8_t secret[ZT_ECC384_SHARED_SECRET_SIZE]);
  91. } // namespace ZeroTier
  92. #endif