Advertisement
TCB13

stkeygen.c | Generate keys from router serial / ripkey.bin

Dec 30th, 2013
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.41 KB | None | 0 0
  1. /**
  2. * The MIT License:
  3. *
  4. * Copyright (c) 2012 Kevin Devine, James Hall
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute,
  10. * sublicense, and/or sell copies of the Software, and to permit persons to
  11. * whom the Software is furnished to do so, subject to the following
  12. * conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  22. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. * OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. * COMPILE WITH: gcc -lcrypto stkeygen.c -ostkeygen
  26. *
  27. */
  28. #include <stdint.h>
  29. #include <sys/stat.h>
  30. #include <openssl/sha.h>
  31.  
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <ctype.h>
  36.  
  37. #define BT_KEY_LEN 10
  38. #define BT_PASS_LEN 8
  39. #define BT_SSID_LEN 4
  40.  
  41. #define PRODUCT_NBR_LEN 12
  42. #define RIP_KEY_LEN 32
  43.  
  44. #define BIN2HEX(x) (x < 10) ? (x + '0') : (x + '7')
  45. #define HEX2BIN(x) (x - '0' < 10) ? (x - '0') : (x - '7')
  46.  
  47. /**
  48. * return 4-bit checksum of product
  49. *
  50. * product[] : 12-byte string
  51. *
  52. */
  53. uint32_t chksum(char product[]) {
  54. uint32_t sum = 16;
  55. int i;
  56.  
  57. for (i = 0; i < PRODUCT_NBR_LEN - 1; i++) {
  58. sum += HEX2BIN(product[i]);
  59. sum = (sum >= 17) ? (sum - 16) : sum;
  60.  
  61. sum <<= 1;
  62. sum = (sum >= 17) ? (sum - 17) : sum;
  63. }
  64.  
  65. sum = (17 - sum);
  66. return (sum != 16) ? sum : 0;
  67. }
  68.  
  69. /**
  70. * generate product number from RIP key
  71. *
  72. * product[] : buffer for 12 byte string
  73. * rip_key[] : 256-bit key from flash memory
  74. *
  75. */
  76. void ripkey2product(char product[], uint8_t rip_key[]) {
  77. uint8_t dgst[20];
  78. SHA_CTX ctx;
  79. int i, j;
  80.  
  81. product[PRODUCT_NBR_LEN] = 0;
  82.  
  83. SHA1_Init(&ctx);
  84. SHA1_Update(&ctx, rip_key, RIP_KEY_LEN);
  85. SHA1_Update(&ctx, "WLAN", 4);
  86. SHA1_Final(dgst, &ctx);
  87.  
  88. for (i = 0, j = 0; i < PRODUCT_NBR_LEN / 2; i++) {
  89. product[j++] = BIN2HEX((dgst[i] >> 4));
  90. product[j++] = BIN2HEX((dgst[i] & 0xF));
  91. }
  92. product[11] = BIN2HEX(chksum(product));
  93. }
  94.  
  95. /**
  96. * generate default WEP/WPA key
  97. *
  98. * key[] : buffer for 10-byte string
  99. * dgst[] : SHA-1 hash
  100. *
  101. */
  102. char *bt_key(char key[], uint8_t dgst[]) {
  103. const char *format = "23456789abcdef";
  104. size_t tbl_len = strlen(format);
  105. uint32_t x1, x2, r;
  106. int i;
  107.  
  108. key[BT_KEY_LEN] = 0;
  109.  
  110. // use 40 bits
  111. x1 = dgst[2] | (dgst[1] << 8) | (dgst[0] << 16);
  112. x2 = dgst[4] | (dgst[3] << 8);
  113.  
  114. for (i = BT_KEY_LEN - 1; i >= 0; --i) {
  115. r = ((x2 + ((x1 % tbl_len) << 16)) % tbl_len);
  116.  
  117. key[i] = format[r];
  118.  
  119. if (x2 < r) {
  120. x1--;
  121. x2++;
  122. }
  123.  
  124. x2 -= r;
  125. x2 += ((x1 % tbl_len) << 16);
  126.  
  127. x1 /= tbl_len;
  128. x2 /= tbl_len;
  129. }
  130. return key;
  131. }
  132.  
  133. /**
  134. * generate default SSID for BT HomeHub 2
  135. *
  136. * ssid[] : buffer for 4-byte string
  137. * dgst[] : SHA-1 hash
  138. *
  139. */
  140. char* bt_ssid(char ssid[], uint8_t dgst[]) {
  141. const char *format = "23456789CFGHJKMNPQRSTWZ";
  142. size_t tbl_len = strlen(format);
  143. uint32_t x1;
  144. int i;
  145.  
  146. ssid[BT_SSID_LEN] = 0;
  147.  
  148. // use last 20 bits
  149. x1 = dgst[19] | (dgst[18] << 8) | (dgst[17] & 0xF) << 16;
  150.  
  151. for (i = BT_SSID_LEN - 1; i >= 0; --i) {
  152. ssid[i] = format[x1 % tbl_len];
  153. x1 /= tbl_len;
  154. }
  155. return ssid;
  156. }
  157.  
  158. /**
  159. * generate default Admin password for BT HomeHub 2
  160. *
  161. * passw[] : buffer for 8-byte string
  162. * rip_key[] : 256-bit key from flash memory
  163. *
  164. */
  165. char *bt_passw(char passw[], uint8_t rip_key[]) {
  166. const char *format = "0123456789ACEFGHJKMNPQRSTWYZ";
  167. size_t tbl_len = strlen(format);
  168. char key[RIP_KEY_LEN*2];
  169. uint8_t dgst[20];
  170. uint32_t p1, p2;
  171. SHA_CTX ctx;
  172. int i, j;
  173.  
  174. passw[BT_PASS_LEN] = 0;
  175. memset(key, 0, sizeof(key));
  176.  
  177. for (i = 0, j = 0; i < RIP_KEY_LEN; i++) {
  178. key[j++] = BIN2HEX((rip_key[i] >> 4));
  179. key[j++] = BIN2HEX((rip_key[i] & 0xF));
  180. }
  181.  
  182. SHA1_Init(&ctx);
  183. SHA1_Update(&ctx, key, RIP_KEY_LEN * 2);
  184. SHA1_Update(&ctx, "admin", 5);
  185. SHA1_Final(dgst, &ctx);
  186.  
  187. p1 = (dgst[2] | (dgst[1] << 8) | (dgst[0] << 16)) << 4;
  188. p2 = (dgst[5] | (dgst[4] << 8) | (dgst[3] << 16)) >> 4;
  189.  
  190. for (i = BT_PASS_LEN - 1; i >= 0; --i) {
  191. p2 += ((p1 % tbl_len) << 16);
  192. p1 /= tbl_len;
  193. passw[i] = format[p2 % tbl_len];
  194. p2 /= tbl_len;
  195. }
  196. return passw;
  197. }
  198.  
  199. void genkeys(uint8_t rip_key[], char serial[]) {
  200. char product[PRODUCT_NBR_LEN+1];
  201. SHA_CTX ctx;
  202. uint8_t dgst[20];
  203. int i, j;
  204. char key[BT_KEY_LEN+1], ssid[BT_SSID_LEN+1], passw[BT_PASS_LEN+1];
  205.  
  206. if (serial != 0) {
  207. for (i = 0; i < 6; i++) {
  208. product[i] = toupper((int)serial[i]);
  209. }
  210.  
  211. for (i = 0, j = 6; i < 3; i++) {
  212. product[j++] = BIN2HEX((toupper((int)serial[8+i]) >> 4));
  213. product[j++] = BIN2HEX((toupper((int)serial[8+i]) & 0xF));
  214. }
  215. } else {
  216. printf("\n RIP Key : ");
  217.  
  218. for (i = 0; i < RIP_KEY_LEN; i++) {
  219. printf("%02x", rip_key[i]);
  220. }
  221. ripkey2product(product, rip_key);
  222. }
  223.  
  224. printf("\n Serial : %s\n", product);
  225.  
  226. SHA1_Init(&ctx);
  227. SHA1_Update(&ctx, product, PRODUCT_NBR_LEN);
  228. SHA1_Final(dgst, &ctx);
  229.  
  230. // Thomson format
  231. printf("\n SSID : SpeedTouch%02X%02X%02X",
  232. dgst[17], dgst[18], dgst[19]);
  233.  
  234. printf("\n WPA/WEP : %02X%02X%02X%02X%02X\n",
  235. dgst[ 0], dgst[ 1], dgst[ 2], dgst[3], dgst[4]);
  236.  
  237. // British Telecom format
  238. printf("\n SSID : BTHomeHub2-%s", bt_ssid(ssid, dgst));
  239. printf("\n WPA/WEP : %s", bt_key(key, dgst));
  240. printf("\n Password : %s\n\n",
  241. (rip_key != 0) ? bt_passw(passw, rip_key) : "N/A");
  242. }
  243.  
  244. uint8_t key1[RIP_KEY_LEN] = { 0xd0, 0x7f, 0x92, 0xee, 0x7f, 0x24, 0xa2, 0x47,
  245. 0x61, 0x68, 0x80, 0x28, 0x53, 0x35, 0x94, 0x02,
  246. 0xba, 0x5b, 0x2a, 0x48, 0x7c, 0xbd, 0x4d, 0xff,
  247. 0xa7, 0xd3, 0xcb, 0xa2, 0x52, 0x05, 0x60, 0xf8 };
  248.  
  249. uint8_t key2[RIP_KEY_LEN] = { 0xdf, 0x94, 0x30, 0xc0, 0x27, 0x40, 0x74, 0xa3,
  250. 0x63, 0x21, 0xe3, 0xac, 0x80, 0xed, 0x60, 0x00,
  251. 0xe3, 0x7d, 0x6e, 0xfa, 0xe1, 0xe1, 0x02, 0xc4,
  252. 0x3f, 0x9c, 0x67, 0x47, 0x64, 0x99, 0xcb, 0x50 };
  253.  
  254. int main(int argc, char *argv[]) {
  255. struct stat fs = {0};
  256. int ret;
  257. FILE *fd;
  258. uint8_t rip_key[RIP_KEY_LEN];
  259.  
  260. puts("\n Thomson Router Key Generator v1.0"
  261. "\n Copright (c) 2012 Kevin Devine and James Hall\n");
  262.  
  263. // argument can be serial number or binary file with RIP key in it
  264. if (argc == 2) {
  265. // check for rip key first
  266. if (stat(argv[1], &fs) == 0) {
  267. if (fs.st_size != 1024) {
  268. printf("\n RIP dump should should be 1024 bytes exactly"
  269. " - %s is %lld bytes", argv[1], fs.st_size);
  270. exit(-1);
  271. }
  272.  
  273. fd = fopen(argv[1], "rb");
  274.  
  275. if (fd == NULL) {
  276. perror(argv[1]);
  277. }
  278.  
  279. ret = fread(rip_key, 1, RIP_KEY_LEN, fd);
  280. fclose(fd);
  281.  
  282. if (ret != RIP_KEY_LEN) {
  283. printf("\nRead error of %s", argv[1]);
  284. exit(-1);
  285. }
  286. genkeys(rip_key, 0);
  287. } else if (strlen(argv[1]) == 11 &&
  288. toupper((int)argv[1][0]) == 'C' &&
  289. toupper((int)argv[1][1]) == 'P') {
  290. genkeys(0, argv[1]);
  291.  
  292. } else {
  293. printf("\n Usage: stkeygen <serial> | <ripkey.bin>\n\n");
  294. exit(-1);
  295. }
  296. } else {
  297. genkeys(key1, 0);
  298. genkeys(key2, 0);
  299. }
  300. return 0;
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement