Identity.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "Identity.h"
  2. #include <string.h>
  3. #define ED25519_NO_SEED 1
  4. #include <ed_25519.h>
  5. #include <Ed25519.h>
  6. namespace mesh {
  7. Identity::Identity() {
  8. memset(pub_key, 0, sizeof(pub_key));
  9. }
  10. Identity::Identity(const char* pub_hex) {
  11. Utils::fromHex(pub_key, PUB_KEY_SIZE, pub_hex);
  12. }
  13. bool Identity::verify(const uint8_t* sig, const uint8_t* message, int msg_len) const {
  14. #if 0
  15. // NOTE: memory corruption bug was found in this function!!
  16. return ed25519_verify(sig, message, msg_len, pub_key);
  17. #else
  18. return Ed25519::verify(sig, this->pub_key, message, msg_len);
  19. #endif
  20. }
  21. bool Identity::readFrom(Stream& s) {
  22. return (s.readBytes(pub_key, PUB_KEY_SIZE) == PUB_KEY_SIZE);
  23. }
  24. bool Identity::writeTo(Stream& s) const {
  25. return (s.write(pub_key, PUB_KEY_SIZE) == PUB_KEY_SIZE);
  26. }
  27. void Identity::printTo(Stream& s) const {
  28. Utils::printHex(s, pub_key, PUB_KEY_SIZE);
  29. }
  30. LocalIdentity::LocalIdentity() {
  31. memset(prv_key, 0, sizeof(prv_key));
  32. }
  33. LocalIdentity::LocalIdentity(const char* prv_hex, const char* pub_hex) : Identity(pub_hex) {
  34. Utils::fromHex(prv_key, PRV_KEY_SIZE, prv_hex);
  35. }
  36. LocalIdentity::LocalIdentity(RNG* rng) {
  37. uint8_t seed[SEED_SIZE];
  38. rng->random(seed, SEED_SIZE);
  39. ed25519_create_keypair(pub_key, prv_key, seed);
  40. }
  41. bool LocalIdentity::readFrom(Stream& s) {
  42. bool success = (s.readBytes(pub_key, PUB_KEY_SIZE) == PUB_KEY_SIZE);
  43. success = success && (s.readBytes(prv_key, PRV_KEY_SIZE) == PRV_KEY_SIZE);
  44. return success;
  45. }
  46. bool LocalIdentity::writeTo(Stream& s) const {
  47. bool success = (s.write(pub_key, PUB_KEY_SIZE) == PUB_KEY_SIZE);
  48. success = success && (s.write(prv_key, PRV_KEY_SIZE) == PRV_KEY_SIZE);
  49. return success;
  50. }
  51. void LocalIdentity::printTo(Stream& s) const {
  52. s.print("pub_key: "); Utils::printHex(s, pub_key, PUB_KEY_SIZE); s.println();
  53. s.print("prv_key: "); Utils::printHex(s, prv_key, PRV_KEY_SIZE); s.println();
  54. }
  55. size_t LocalIdentity::writeTo(uint8_t* dest, size_t max_len) {
  56. if (max_len < PRV_KEY_SIZE) return 0; // not big enough
  57. if (max_len < PRV_KEY_SIZE + PUB_KEY_SIZE) { // only room for prv_key
  58. memcpy(dest, prv_key, PRV_KEY_SIZE);
  59. return PRV_KEY_SIZE;
  60. }
  61. memcpy(dest, prv_key, PRV_KEY_SIZE); // otherwise can fit prv + pub keys
  62. memcpy(&dest[PRV_KEY_SIZE], pub_key, PUB_KEY_SIZE);
  63. return PRV_KEY_SIZE + PUB_KEY_SIZE;
  64. }
  65. void LocalIdentity::readFrom(const uint8_t* src, size_t len) {
  66. if (len == PRV_KEY_SIZE + PUB_KEY_SIZE) { // has prv + pub keys
  67. memcpy(prv_key, src, PRV_KEY_SIZE);
  68. memcpy(pub_key, &src[PRV_KEY_SIZE], PUB_KEY_SIZE);
  69. } else if (len == PRV_KEY_SIZE) {
  70. memcpy(prv_key, src, PRV_KEY_SIZE);
  71. // now need to re-calculate the pub_key
  72. ed25519_derive_pub(pub_key, prv_key);
  73. }
  74. }
  75. void LocalIdentity::sign(uint8_t* sig, const uint8_t* message, int msg_len) const {
  76. ed25519_sign(sig, message, msg_len, pub_key, prv_key);
  77. }
  78. void LocalIdentity::calcSharedSecret(uint8_t* secret, const uint8_t* other_pub_key) const {
  79. ed25519_key_exchange(secret, other_pub_key, prv_key);
  80. }
  81. }