NidNmk.c 7.7 KB

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