NidNmk.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*===========================================================================
  2. Combined Charging System (CCS): SECC
  3. NidNmk.c
  4. initiated by Vern, Joseph
  5. (since 2019/07/19)
  6. =============================================================================*/
  7. #include "NidNmk.h"
  8. #include "define.h"
  9. #include <time.h>
  10. #include<sys/time.h>
  11. unsigned char buf_log_nidmmk[SIZE_OF_LOG_BUFFER];
  12. /*===========================================================================
  13. FUNCTION: StoreLogMsg
  14. DESCRIPTION:
  15. PRE-CONDITION:
  16. INPUT:
  17. OUTPUT:
  18. GLOBAL VARIABLES:
  19. =============================================================================*/
  20. #if SAVE_SYS_LOG_MSG_NIDNMK_SWITCH == ENABLE
  21. int StoreLogMsg(unsigned char *DataString)
  22. {
  23. static unsigned char Buf[1024];
  24. static time_t CurrentTime;
  25. static struct tm *tm;
  26. static struct timeval tv;
  27. memset(Buf, 0, sizeof(Buf));
  28. CurrentTime = time(NULL);
  29. tm = localtime(&CurrentTime);
  30. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  31. sprintf(Buf, "echo \"[%04d%02d%02d: %02d:%02d:%02d.%06d][NidNmk]%s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  32. tm->tm_year + 1900,
  33. tm->tm_mon + 1,
  34. tm->tm_mday,
  35. tm->tm_hour,
  36. tm->tm_min,
  37. tm->tm_sec,
  38. tv.tv_usec,
  39. DataString,
  40. tm->tm_year + 1900,
  41. tm->tm_mon + 1);
  42. system(Buf);
  43. DEBUG_PRINTF_NIDNMK_SYSTEM_LOG("[%02d:%02d:%02d.%06d][NidNmk]%s \n",
  44. tm->tm_hour,
  45. tm->tm_min,
  46. tm->tm_sec,
  47. tv.tv_usec,
  48. DataString);
  49. //Reset the buf_log_nidmmk Buffer, i.e. DataString
  50. memset(buf_log_nidmmk, 0, SIZE_OF_LOG_BUFFER);
  51. }
  52. #endif
  53. void HPAVKeyNMK(uint8_t NMK [], const char *string)
  54. {
  55. struct sha256 sha256;
  56. uint8_t digest [SHA256_DIGEST_LENGTH];
  57. const uint8_t secret [] =
  58. {
  59. 0x08,
  60. 0x85,
  61. 0x6D,
  62. 0xAF,
  63. 0x7C,
  64. 0xF5,
  65. 0x81,
  66. 0x86
  67. };
  68. unsigned rehash = 999;
  69. SHA256Reset(&sha256);
  70. SHA256Write(&sha256, string, strlen(string));
  71. SHA256Write(&sha256, secret, sizeof(secret));
  72. SHA256Fetch(&sha256, digest);
  73. while(rehash--)
  74. {
  75. SHA256Reset(&sha256);
  76. SHA256Write(&sha256, digest, sizeof(digest));
  77. SHA256Fetch(&sha256, digest);
  78. }
  79. memcpy(NMK, digest, HPAVKEY_NMK_LEN);
  80. return;
  81. }
  82. void HPAVKeyNID(uint8_t NID[], const uint8_t NMK[], uint8_t level)
  83. {
  84. struct sha256 sha256;
  85. uint8_t digest [SHA256_DIGEST_LENGTH];
  86. unsigned int rehash = 4;
  87. SHA256Reset(&sha256);
  88. SHA256Write(&sha256, NMK, HPAVKEY_NMK_LEN);
  89. SHA256Fetch(&sha256, digest);
  90. while(rehash--)
  91. {
  92. SHA256Reset(&sha256);
  93. SHA256Write(&sha256, digest, sizeof(digest));
  94. SHA256Fetch(&sha256, digest);
  95. }
  96. #if 1
  97. level <<= 4;
  98. digest [HPAVKEY_NID_LEN - 1] >>= 4;
  99. digest [HPAVKEY_NID_LEN - 1] |= level;
  100. #else
  101. digest [HPAVKEY_NID_LEN - 1] &= ~0xC0;
  102. digest [HPAVKEY_NID_LEN - 1] |= level << 6;
  103. #endif
  104. memcpy(NID, digest, HPAVKEY_NID_LEN);
  105. return;
  106. }
  107. void SHA256Reset(struct sha256 *sha256)
  108. {
  109. memset(sha256, 0, sizeof(struct sha256));
  110. sha256->state [0] = 0x6A09E667;
  111. sha256->state [1] = 0xBB67AE85;
  112. sha256->state [2] = 0x3C6EF372;
  113. sha256->state [3] = 0xA54FF53A;
  114. sha256->state [4] = 0x510E527F;
  115. sha256->state [5] = 0x9B05688C;
  116. sha256->state [6] = 0x1F83D9AB;
  117. sha256->state [7] = 0x5BE0CD19;
  118. sha256->extra [0] = 0x80;
  119. return;
  120. }
  121. void SHA256Block(struct sha256 *sha256, void const *memory)
  122. {
  123. static const uint32_t K [sizeof(sha256->block)] =
  124. {
  125. 0x428A2F98,
  126. 0x71374491,
  127. 0xB5C0FBCF,
  128. 0xE9B5DBA5,
  129. 0x3956C25B,
  130. 0x59F111F1,
  131. 0x923F82A4,
  132. 0xAB1C5ED5,
  133. 0xD807AA98,
  134. 0x12835B01,
  135. 0x243185BE,
  136. 0x550C7DC3,
  137. 0x72BE5D74,
  138. 0x80DEB1FE,
  139. 0x9BDC06A7,
  140. 0xC19BF174,
  141. 0xE49B69C1,
  142. 0xEFBE4786,
  143. 0x0FC19DC6,
  144. 0x240CA1CC,
  145. 0x2DE92C6F,
  146. 0x4A7484AA,
  147. 0x5CB0A9DC,
  148. 0x76F988DA,
  149. 0x983E5152,
  150. 0xA831C66D,
  151. 0xB00327C8,
  152. 0xBF597FC7,
  153. 0xC6E00BF3,
  154. 0xD5A79147,
  155. 0x06CA6351,
  156. 0x14292967,
  157. 0x27B70A85,
  158. 0x2E1B2138,
  159. 0x4D2C6DFC,
  160. 0x53380D13,
  161. 0x650A7354,
  162. 0x766A0ABB,
  163. 0x81C2C92E,
  164. 0x92722C85,
  165. 0xA2BFE8A1,
  166. 0xA81A664B,
  167. 0xC24B8B70,
  168. 0xC76C51A3,
  169. 0xD192E819,
  170. 0xD6990624,
  171. 0xF40E3585,
  172. 0x106AA070,
  173. 0x19A4C116,
  174. 0x1E376C08,
  175. 0x2748774C,
  176. 0x34B0BCB5,
  177. 0x391C0CB3,
  178. 0x4ED8AA4A,
  179. 0x5B9CCA4F,
  180. 0x682E6FF3,
  181. 0x748F82EE,
  182. 0x78A5636F,
  183. 0x84C87814,
  184. 0x8CC70208,
  185. 0x90BEFFFA,
  186. 0xA4506CEB,
  187. 0xBEF9A3F7,
  188. 0xC67178F2
  189. };
  190. unsigned int pass;
  191. unsigned int word;
  192. uint32_t H [sizeof(sha256->state) / sizeof(uint32_t)];
  193. uint32_t W [sizeof(sha256->block)];
  194. uint8_t *buffer = (uint8_t *)(memory);
  195. for(word = 0; word < 16; word++)
  196. {
  197. W [word] = 0;
  198. W [word] |= (uint32_t)(*buffer++) << 24;
  199. W [word] |= (uint32_t)(*buffer++) << 16;
  200. W [word] |= (uint32_t)(*buffer++) << 8;
  201. W [word] |= (uint32_t)(*buffer++) << 0;;
  202. }
  203. for(word = word; word < sizeof(sha256->block); word++)
  204. {
  205. uint32_t s0 = ROTR(W [word - 15], 7) ^ ROTR(W [word - 15], 18) ^ SHR(W [word - 15], 3);
  206. uint32_t s1 = ROTR(W [word - 2], 17) ^ ROTR(W [word - 2], 19) ^ SHR(W [word - 2], 10);
  207. W [word] = W [word - 16] + s0 + W [word - 7] + s1;
  208. }
  209. for(word = 0; word < (sizeof(sha256->state) / sizeof(uint32_t)); word++)
  210. {
  211. H [word] = sha256->state [word];
  212. }
  213. for(pass = 0; pass < sizeof(sha256->block); pass++)
  214. {
  215. uint32_t s2 = ROTR(H [0], 2) ^ ROTR(H [0], 13) ^ ROTR(H [0], 22);
  216. uint32_t maj = (H [0] & H [1]) ^ (H [0] & H [2]) ^ (H [1] & H [2]);
  217. uint32_t t2 = s2 + maj;
  218. uint32_t s3 = ROTR(H [4], 6) ^ ROTR(H [4], 11) ^ ROTR(H [4], 25);
  219. uint32_t ch = (H [4] & H [5]) ^ ((~H [4]) & H [6]);
  220. uint32_t t1 = H [7] + s3 + ch + K [pass] + W [pass];
  221. for(word = (sizeof(sha256->state) / sizeof(uint32_t)) - 1; word > 0; word--)
  222. {
  223. H [word] = H [word - 1];
  224. }
  225. H [0] = t1 + t2;
  226. H [4] += t1;
  227. }
  228. for(word = 0; word < (sizeof(sha256->state) / sizeof(uint32_t)); word++)
  229. {
  230. sha256->state [word] += H [word];
  231. }
  232. return;
  233. }
  234. void SHA256Write(struct sha256 *sha256, void const *memory, uint16_t extent)
  235. {
  236. if(extent)
  237. {
  238. uint8_t *buffer = (uint8_t *)(memory);
  239. unsigned int left = sha256->count [0] & 0x3F;
  240. unsigned int fill = sizeof(sha256->block) - left;
  241. sha256->count [0] += (uint32_t)(extent);
  242. sha256->count [0] &= 0xFFFFFFFF;
  243. if(sha256->count [0] < extent)
  244. {
  245. sha256->count [1]++;
  246. }
  247. if((left) && (extent >= fill))
  248. {
  249. memcpy(sha256->block + left, buffer, fill);
  250. SHA256Block(sha256, sha256->block);
  251. extent -= fill;
  252. buffer += fill;
  253. left = 0;
  254. }
  255. while(extent >= sizeof(sha256->block))
  256. {
  257. SHA256Block(sha256, buffer);
  258. extent -= sizeof(sha256->block);
  259. buffer += sizeof(sha256->block);
  260. }
  261. if(extent)
  262. {
  263. memcpy(sha256->block + left, buffer, extent);
  264. }
  265. }
  266. return;
  267. }
  268. void SHAEncode(uint8_t memory[], uint32_t number)
  269. {
  270. *memory++ = (uint8_t)(number >> 24);
  271. *memory++ = (uint8_t)(number >> 16);
  272. *memory++ = (uint8_t)(number >> 8);
  273. *memory++ = (uint8_t)(number >> 0);
  274. return;
  275. }
  276. void SHA256Fetch(struct sha256 *sha256, uint8_t digest[])
  277. {
  278. unsigned int word;
  279. uint8_t bits [8];
  280. uint32_t upper = (sha256->count [0] >> 29) | (sha256->count [1] << 3);
  281. uint32_t lower = (sha256->count [0] << 3);
  282. uint32_t final = (sha256->count [0] & 0x3F);
  283. uint32_t extra = (final < 56) ? (56 - final) : (120 - final);
  284. SHAEncode(&bits[0], upper);
  285. SHAEncode(&bits[4], lower);
  286. SHA256Write(sha256, sha256->extra, extra);
  287. SHA256Write(sha256, bits, sizeof(bits));
  288. for(word = 0; word < sizeof(sha256->state) / sizeof(uint32_t); word++)
  289. {
  290. SHAEncode(digest, sha256->state [word]);
  291. digest += sizeof(uint32_t);
  292. }
  293. memset(sha256, 0, sizeof(struct sha256));
  294. return;
  295. }