NidNmk.c 7.1 KB

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