NidNmk.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*===========================================================================
  2. Combined Charging System (CCS): SECC
  3. NidNmk.h
  4. initiated by Vern, Joseph
  5. (since 2019/07/19)
  6. =============================================================================*/
  7. #include <stdint.h>
  8. #include <unistd.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #if FIRMWARE_VERSION_COMPILE_SETTING_RELEASE_MODE == ENABLE
  12. //Saving System Log
  13. #define SAVE_SYS_LOG_MSG_NIDNMK_SWITCH DISABLE //ENABLE, DISABLE
  14. //Configuration for Debug Print Messages
  15. #define DEBUG_PRINTF_NIDNMK_SHOW DISABLE //ENABLE, DISABLE
  16. #define DEBUG_PRINTF_NIDNMK_BRIEF_SHOW DISABLE //ENABLE, DISABLE
  17. #define DEBUG_PRINTF_NIDNMK_DETAIL_SHOW DISABLE //ENABLE, DISABLE
  18. #define DEBUG_PRINTF_NIDNMK_SYSTEM_LOG_SHOW DISABLE //ENABLE, DISABLE
  19. #else //debug mode
  20. //Saving System Log
  21. #define SAVE_SYS_LOG_MSG_NIDNMK_SWITCH ENABLE //ENABLE, DISABLE
  22. //Configuration for Debug Print Messages
  23. #define DEBUG_PRINTF_NIDNMK_SHOW ENABLE //ENABLE, DISABLE
  24. #define DEBUG_PRINTF_NIDNMK_BRIEF_SHOW ENABLE //ENABLE, DISABLE
  25. #define DEBUG_PRINTF_NIDNMK_DETAIL_SHOW ENABLE //ENABLE, DISABLE
  26. #define DEBUG_PRINTF_NIDNMK_SYSTEM_LOG_SHOW ENABLE //ENABLE, DISABLE
  27. #endif
  28. typedef struct sha256
  29. {
  30. uint32_t count [2];
  31. uint32_t state [8];
  32. uint8_t block [64];
  33. uint8_t extra [64];
  34. } NidSHA256;
  35. #define SHA256_DIGEST_LENGTH 256 / 8
  36. #define HPAVKEY_NMK_LEN 16
  37. #define HPAVKEY_NID_LEN 7
  38. #define SHR(word, bits) ((word & 0xFFFFFFFF) >> bits)
  39. #define ROTR(word, bits) (SHR(word, bits) | (word << (32 - bits)))
  40. #define DEFAULT_LEVEL 0
  41. //--------------------------------------------------------------------------
  42. /* Functions -----------------------------------------------------------*/
  43. //--------------------------------------------------------------------------
  44. void HPAVKeyNMK(uint8_t NMK [], const char *string);
  45. void HPAVKeyNID(uint8_t NID [], const uint8_t NMK [], uint8_t level);
  46. void SHA256Reset(struct sha256 *sha256);
  47. void SHA256Block(struct sha256 *sha256, void const *memory);
  48. void SHA256Write(struct sha256 *sha256, void const *memory, uint16_t extent);
  49. void SHAEncode(uint8_t memory [], uint32_t number);
  50. void SHA256Fetch(struct sha256 *sha256, uint8_t digest []);
  51. //System Log
  52. #if SAVE_SYS_LOG_MSG_NIDNMK_SWITCH == ENABLE
  53. #define SAVE_SYS_LOG_MSG_NIDNMK StoreLogMsg
  54. #else
  55. #define SAVE_SYS_LOG_MSG_NIDNMK(...)
  56. #endif
  57. //Debug Message
  58. #if DEBUG_PRINTF_NIDNMK_SHOW == ENABLE
  59. //Brief Messages
  60. #if DEBUG_PRINTF_NIDNMK_BRIEF_SHOW == ENABLE
  61. #define DEBUG_PRINTF_NIDNMK_BRIEF printf
  62. #else
  63. #define DEBUG_PRINTF_NIDNMK_BRIEF(...)
  64. #endif
  65. //Detail Messages
  66. #if DEBUG_PRINTF_NIDNMK_DETAIL_SHOW == ENABLE
  67. #define DEBUG_PRINTF_NIDNMK_DETAIL printf
  68. #else
  69. #define DEBUG_PRINTF_NIDNMK_DETAIL(...)
  70. #endif
  71. //System Log Messages
  72. #if DEBUG_PRINTF_NIDNMK_SYSTEM_LOG_SHOW == ENABLE
  73. #define DEBUG_PRINTF_NIDNMK_SYSTEM_LOG printf
  74. #else
  75. #define DEBUG_PRINTF_NIDNMK_SYSTEM_LOG(...)
  76. #endif
  77. #else
  78. #define DEBUG_PRINTF_NIDNMK_BRIEF(...)
  79. #define DEBUG_PRINTF_NIDNMK_DETAIL(...)
  80. #define DEBUG_PRINTF_NIDNMK_SYSTEM_LOG(...)
  81. #endif