Utils.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "Utils.h"
  2. #include <AES.h>
  3. #include <SHA256.h>
  4. #ifdef ARDUINO
  5. #include <Arduino.h>
  6. #endif
  7. namespace mesh {
  8. uint32_t RNG::nextInt(uint32_t _min, uint32_t _max) {
  9. uint32_t num;
  10. random((uint8_t *) &num, sizeof(num));
  11. return (num % (_max - _min)) + _min;
  12. }
  13. void Utils::sha256(uint8_t *hash, size_t hash_len, const uint8_t* msg, int msg_len) {
  14. SHA256 sha;
  15. sha.update(msg, msg_len);
  16. sha.finalize(hash, hash_len);
  17. }
  18. void Utils::sha256(uint8_t *hash, size_t hash_len, const uint8_t* frag1, int frag1_len, const uint8_t* frag2, int frag2_len) {
  19. SHA256 sha;
  20. sha.update(frag1, frag1_len);
  21. sha.update(frag2, frag2_len);
  22. sha.finalize(hash, hash_len);
  23. }
  24. int Utils::decrypt(const uint8_t* shared_secret, uint8_t* dest, const uint8_t* src, int src_len) {
  25. AES128 aes;
  26. uint8_t* dp = dest;
  27. const uint8_t* sp = src;
  28. aes.setKey(shared_secret, CIPHER_KEY_SIZE);
  29. while (sp - src < src_len) {
  30. aes.decryptBlock(dp, sp);
  31. dp += 16; sp += 16;
  32. }
  33. return sp - src; // will always be multiple of 16
  34. }
  35. int Utils::encrypt(const uint8_t* shared_secret, uint8_t* dest, const uint8_t* src, int src_len) {
  36. AES128 aes;
  37. uint8_t* dp = dest;
  38. aes.setKey(shared_secret, CIPHER_KEY_SIZE);
  39. while (src_len >= 16) {
  40. aes.encryptBlock(dp, src);
  41. dp += 16; src += 16; src_len -= 16;
  42. }
  43. if (src_len > 0) { // remaining partial block
  44. uint8_t tmp[16];
  45. memset(tmp, 0, 16);
  46. memcpy(tmp, src, src_len);
  47. aes.encryptBlock(dp, tmp);
  48. dp += 16;
  49. }
  50. return dp - dest; // will always be multiple of 16
  51. }
  52. int Utils::encryptThenMAC(const uint8_t* shared_secret, uint8_t* dest, const uint8_t* src, int src_len) {
  53. int enc_len = encrypt(shared_secret, dest + CIPHER_MAC_SIZE, src, src_len);
  54. SHA256 sha;
  55. sha.resetHMAC(shared_secret, PUB_KEY_SIZE);
  56. sha.update(dest + CIPHER_MAC_SIZE, enc_len);
  57. sha.finalizeHMAC(shared_secret, PUB_KEY_SIZE, dest, CIPHER_MAC_SIZE);
  58. return CIPHER_MAC_SIZE + enc_len;
  59. }
  60. int Utils::MACThenDecrypt(const uint8_t* shared_secret, uint8_t* dest, const uint8_t* src, int src_len) {
  61. if (src_len <= CIPHER_MAC_SIZE) return 0; // invalid src bytes
  62. uint8_t hmac[CIPHER_MAC_SIZE];
  63. {
  64. SHA256 sha;
  65. sha.resetHMAC(shared_secret, PUB_KEY_SIZE);
  66. sha.update(src + CIPHER_MAC_SIZE, src_len - CIPHER_MAC_SIZE);
  67. sha.finalizeHMAC(shared_secret, PUB_KEY_SIZE, hmac, CIPHER_MAC_SIZE);
  68. }
  69. if (memcmp(hmac, src, CIPHER_MAC_SIZE) == 0) {
  70. return decrypt(shared_secret, dest, src + CIPHER_MAC_SIZE, src_len - CIPHER_MAC_SIZE);
  71. }
  72. return 0; // invalid HMAC
  73. }
  74. static const char hex_chars[] = "0123456789ABCDEF";
  75. void Utils::toHex(char* dest, const uint8_t* src, size_t len) {
  76. while (len > 0) {
  77. uint8_t b = *src++;
  78. *dest++ = hex_chars[b >> 4];
  79. *dest++ = hex_chars[b & 0x0F];
  80. len--;
  81. }
  82. *dest = 0;
  83. }
  84. void Utils::printHex(Stream& s, const uint8_t* src, size_t len) {
  85. while (len > 0) {
  86. uint8_t b = *src++;
  87. s.print(hex_chars[b >> 4]);
  88. s.print(hex_chars[b & 0x0F]);
  89. len--;
  90. }
  91. }
  92. static uint8_t hexVal(char c) {
  93. if (c >= 'A' && c <= 'F') return c - 'A' + 10;
  94. if (c >= 'a' && c <= 'f') return c - 'a' + 10;
  95. if (c >= '0' && c <= '9') return c - '0';
  96. return 0;
  97. }
  98. bool Utils::isHexChar(char c) {
  99. return c == '0' || hexVal(c) > 0;
  100. }
  101. bool Utils::fromHex(uint8_t* dest, int dest_size, const char *src_hex) {
  102. int len = strlen(src_hex);
  103. if (len != dest_size*2) return false; // incorrect length
  104. uint8_t* dp = dest;
  105. while (dp - dest < dest_size) {
  106. char ch = *src_hex++;
  107. char cl = *src_hex++;
  108. *dp++ = (hexVal(ch) << 4) | hexVal(cl);
  109. }
  110. return true;
  111. }
  112. int Utils::parseTextParts(char* text, const char* parts[], int max_num, char separator) {
  113. int num = 0;
  114. char* sp = text;
  115. while (*sp && num < max_num) {
  116. parts[num++] = sp;
  117. while (*sp && *sp != separator) sp++;
  118. if (*sp) {
  119. *sp++ = 0; // replace the seperator with a null, and skip past it
  120. }
  121. }
  122. // if we hit the maximum parts, make sure LAST entry does NOT have separator
  123. while (*sp && *sp != separator) sp++;
  124. if (*sp) {
  125. *sp = 0; // replace the separator with null
  126. }
  127. return num;
  128. }
  129. }