ECC384.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c)2013-2020 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: 2025-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. /*
  14. * The contents of ECC384.cpp are third party code and are licensed under
  15. * the BSD 2-clause license.
  16. *
  17. * The built-in implementation is easy-ecc by Kenneth MacKay and can be found
  18. * here: https://github.com/esxgx/easy-ecc
  19. *
  20. * Our copy is trimmed down with unused stuff removed and also contains a few
  21. * ZeroTier shims to implement these function interfaces. Otherwise it is
  22. * unmodified from the original. It's a nice and fairly fast portable
  23. * implementation that should build everywhere.
  24. *
  25. * For FIPS-compliant builds this will eventually link against FIPS-compliant
  26. * crypto libraries instead of using the built-in version.
  27. */
  28. #ifndef ZT_ECC384_HPP
  29. #define ZT_ECC384_HPP
  30. #include "Constants.hpp"
  31. /**
  32. * Size of a (point compressed) P-384 public key
  33. */
  34. #define ZT_ECC384_PUBLIC_KEY_SIZE 49
  35. /**
  36. * Size of a P-384 private key
  37. */
  38. #define ZT_ECC384_PRIVATE_KEY_SIZE 48
  39. /**
  40. * Size of the hash that should be signed using P-384
  41. */
  42. #define ZT_ECC384_SIGNATURE_HASH_SIZE 48
  43. /**
  44. * Size of a P-384 signature
  45. */
  46. #define ZT_ECC384_SIGNATURE_SIZE 96
  47. /**
  48. * Size of raw shared secret generated by ECDH key agreement
  49. */
  50. #define ZT_ECC384_SHARED_SECRET_SIZE 48
  51. namespace ZeroTier {
  52. /**
  53. * Generate a NIST P-384 key pair
  54. *
  55. * @param pub Buffer to receive point compressed public key
  56. * @param priv Buffer to receiver private key
  57. */
  58. void ECC384GenerateKey(uint8_t pub[ZT_ECC384_PUBLIC_KEY_SIZE],uint8_t priv[ZT_ECC384_PRIVATE_KEY_SIZE]);
  59. /**
  60. * Sign a hash with a NIST P-384 private key
  61. *
  62. * The hash must be 48 bytes in size. If it's longer only the first 48
  63. * bytes are used.
  64. *
  65. * @param priv Private key
  66. * @param hash 48-byte hash
  67. * @param sig Buffer to receive signature
  68. */
  69. 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]);
  70. /**
  71. * Verify a signature
  72. *
  73. * @param pub Public key
  74. * @param hash 48-byte hash (usually first 48 bytes of SHA512(msg))
  75. * @param sig Signature to check
  76. * @return True if signature is valid
  77. */
  78. 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]);
  79. /**
  80. * Perform ECDH key agreement
  81. *
  82. * The secret generated here is the raw 48-byte result of ECDH.
  83. * It's typically hashed prior to use.
  84. *
  85. * @param theirPub Remote public key
  86. * @param ourPriv Local private key
  87. * @param secret Buffer to receive 48-byte secret
  88. */
  89. 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]);
  90. } // namespace ZeroTier
  91. #endif