ECC384.hpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2023-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. // This is glue code to ease the use of the NIST P-384 elliptic curve.
  14. // Note that most of the code inside ECC384.cpp is third party code and
  15. // is under the BSD 2-clause license rather than ZeroTier's license.
  16. #ifndef ZT_ECC384_HPP
  17. #define ZT_ECC384_HPP
  18. #include "Constants.hpp"
  19. /**
  20. * Size of a (point compressed) P-384 public key
  21. */
  22. #define ZT_ECC384_PUBLIC_KEY_SIZE 49
  23. /**
  24. * Size of a P-384 private key
  25. */
  26. #define ZT_ECC384_PRIVATE_KEY_SIZE 48
  27. /**
  28. * Size of the hash that should be signed using P-384
  29. */
  30. #define ZT_ECC384_SIGNATURE_HASH_SIZE 48
  31. /**
  32. * Size of a P-384 signature
  33. */
  34. #define ZT_ECC384_SIGNATURE_SIZE 96
  35. /**
  36. * Size of raw shared secret generated by ECDH key agreement
  37. */
  38. #define ZT_ECC384_SHARED_SECRET_SIZE 48
  39. namespace ZeroTier {
  40. /**
  41. * Generate a NIST P-384 key pair
  42. *
  43. * @param pub Buffer to receive point compressed public key
  44. * @param priv Buffer to receiver private key
  45. */
  46. void ECC384GenerateKey(uint8_t pub[ZT_ECC384_PUBLIC_KEY_SIZE],uint8_t priv[ZT_ECC384_PRIVATE_KEY_SIZE]);
  47. /**
  48. * Sign a hash with a NIST P-384 private key
  49. *
  50. * The hash must be 48 bytes in size. If it's longer only the first 48
  51. * bytes are used.
  52. *
  53. * @param priv Private key
  54. * @param hash 48-byte hash
  55. * @param sig Buffer to receive signature
  56. */
  57. 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]);
  58. /**
  59. * Verify a signature
  60. *
  61. * @param pub Public key
  62. * @param hash 48-byte hash (usually first 48 bytes of SHA512(msg))
  63. * @param sig Signature to check
  64. * @return True if signature is valid
  65. */
  66. 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]);
  67. /**
  68. * Perform ECDH key agreement
  69. *
  70. * The secret generated here is the raw 48-byte result of ECDH.
  71. * It's typically hashed prior to use.
  72. *
  73. * @param theirPub Remote public key
  74. * @param ourPriv Local private key
  75. * @param secret Buffer to receive 48-byte secret
  76. */
  77. 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]);
  78. } // namespace ZeroTier
  79. #endif